OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [binutils/] [readelf.c] - Blame information for rev 94

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 15 khays
/* readelf.c -- display contents of an ELF format file
2
   Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3
   2008, 2009, 2010, 2011
4
   Free Software Foundation, Inc.
5
 
6
   Originally developed by Eric Youngdale <eric@andante.jic.com>
7
   Modifications by Nick Clifton <nickc@redhat.com>
8
 
9
   This file is part of GNU Binutils.
10
 
11
   This program is free software; you can redistribute it and/or modify
12
   it under the terms of the GNU General Public License as published by
13
   the Free Software Foundation; either version 3 of the License, or
14
   (at your option) any later version.
15
 
16
   This program is distributed in the hope that it will be useful,
17
   but WITHOUT ANY WARRANTY; without even the implied warranty of
18
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
   GNU General Public License for more details.
20
 
21
   You should have received a copy of the GNU General Public License
22
   along with this program; if not, write to the Free Software
23
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
24
   02110-1301, USA.  */
25
 
26
/* The difference between readelf and objdump:
27
 
28
  Both programs are capable of displaying the contents of ELF format files,
29
  so why does the binutils project have two file dumpers ?
30
 
31
  The reason is that objdump sees an ELF file through a BFD filter of the
32
  world; if BFD has a bug where, say, it disagrees about a machine constant
33
  in e_flags, then the odds are good that it will remain internally
34
  consistent.  The linker sees it the BFD way, objdump sees it the BFD way,
35
  GAS sees it the BFD way.  There was need for a tool to go find out what
36
  the file actually says.
37
 
38
  This is why the readelf program does not link against the BFD library - it
39
  exists as an independent program to help verify the correct working of BFD.
40
 
41
  There is also the case that readelf can provide more information about an
42
  ELF file than is provided by objdump.  In particular it can display DWARF
43
  debugging information which (at the moment) objdump cannot.  */
44
 
45
#include "config.h"
46
#include "sysdep.h"
47
#include <assert.h>
48
#include <sys/stat.h>
49
#include <time.h>
50
#ifdef HAVE_ZLIB_H
51
#include <zlib.h>
52
#endif
53
 
54
#if __GNUC__ >= 2
55
/* Define BFD64 here, even if our default architecture is 32 bit ELF
56
   as this will allow us to read in and parse 64bit and 32bit ELF files.
57
   Only do this if we believe that the compiler can support a 64 bit
58
   data type.  For now we only rely on GCC being able to do this.  */
59
#define BFD64
60
#endif
61
 
62
#include "bfd.h"
63
#include "bucomm.h"
64
#include "elfcomm.h"
65
#include "dwarf.h"
66
 
67
#include "elf/common.h"
68
#include "elf/external.h"
69
#include "elf/internal.h"
70
 
71
 
72
/* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
73
   we can obtain the H8 reloc numbers.  We need these for the
74
   get_reloc_size() function.  We include h8.h again after defining
75
   RELOC_MACROS_GEN_FUNC so that we get the naming function as well.  */
76
 
77
#include "elf/h8.h"
78
#undef _ELF_H8_H
79
 
80
/* Undo the effects of #including reloc-macros.h.  */
81
 
82
#undef START_RELOC_NUMBERS
83
#undef RELOC_NUMBER
84
#undef FAKE_RELOC
85
#undef EMPTY_RELOC
86
#undef END_RELOC_NUMBERS
87
#undef _RELOC_MACROS_H
88
 
89
/* The following headers use the elf/reloc-macros.h file to
90
   automatically generate relocation recognition functions
91
   such as elf_mips_reloc_type()  */
92
 
93
#define RELOC_MACROS_GEN_FUNC
94
 
95
#include "elf/alpha.h"
96
#include "elf/arc.h"
97
#include "elf/arm.h"
98
#include "elf/avr.h"
99
#include "elf/bfin.h"
100
#include "elf/cr16.h"
101
#include "elf/cris.h"
102
#include "elf/crx.h"
103
#include "elf/d10v.h"
104
#include "elf/d30v.h"
105
#include "elf/dlx.h"
106
#include "elf/fr30.h"
107
#include "elf/frv.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/microblaze.h"
125
#include "elf/mips.h"
126
#include "elf/mmix.h"
127
#include "elf/mn10200.h"
128
#include "elf/mn10300.h"
129
#include "elf/moxie.h"
130
#include "elf/mt.h"
131
#include "elf/msp430.h"
132
#include "elf/open8.h"
133
#include "elf/or32.h"
134
#include "elf/pj.h"
135
#include "elf/ppc.h"
136
#include "elf/ppc64.h"
137
#include "elf/rx.h"
138
#include "elf/s390.h"
139
#include "elf/score.h"
140
#include "elf/sh.h"
141
#include "elf/sparc.h"
142
#include "elf/spu.h"
143
#include "elf/tic6x.h"
144
#include "elf/v850.h"
145
#include "elf/vax.h"
146
#include "elf/x86-64.h"
147
#include "elf/xc16x.h"
148
#include "elf/xstormy16.h"
149
#include "elf/xtensa.h"
150
 
151
#include "getopt.h"
152
#include "libiberty.h"
153
#include "safe-ctype.h"
154
#include "filenames.h"
155
 
156
char * program_name = "readelf";
157
static long archive_file_offset;
158
static unsigned long archive_file_size;
159
static unsigned long dynamic_addr;
160
static bfd_size_type dynamic_size;
161
static unsigned int dynamic_nent;
162
static char * dynamic_strings;
163
static unsigned long dynamic_strings_length;
164
static char * string_table;
165
static unsigned long string_table_length;
166
static unsigned long num_dynamic_syms;
167
static Elf_Internal_Sym * dynamic_symbols;
168
static Elf_Internal_Syminfo * dynamic_syminfo;
169
static unsigned long dynamic_syminfo_offset;
170
static unsigned int dynamic_syminfo_nent;
171
static char program_interpreter[PATH_MAX];
172
static bfd_vma dynamic_info[DT_ENCODING];
173
static bfd_vma dynamic_info_DT_GNU_HASH;
174
static bfd_vma version_info[16];
175
static Elf_Internal_Ehdr elf_header;
176
static Elf_Internal_Shdr * section_headers;
177
static Elf_Internal_Phdr * program_headers;
178
static Elf_Internal_Dyn *  dynamic_section;
179
static Elf_Internal_Shdr * symtab_shndx_hdr;
180
static int show_name;
181
static int do_dynamic;
182
static int do_syms;
183
static int do_dyn_syms;
184
static int do_reloc;
185
static int do_sections;
186
static int do_section_groups;
187
static int do_section_details;
188
static int do_segments;
189
static int do_unwind;
190
static int do_using_dynamic;
191
static int do_header;
192
static int do_dump;
193
static int do_version;
194
static int do_histogram;
195
static int do_debugging;
196
static int do_arch;
197
static int do_notes;
198
static int do_archive_index;
199
static int is_32bit_elf;
200
 
201
struct group_list
202
{
203
  struct group_list * next;
204
  unsigned int section_index;
205
};
206
 
207
struct group
208
{
209
  struct group_list * root;
210
  unsigned int group_index;
211
};
212
 
213
static size_t group_count;
214
static struct group * section_groups;
215
static struct group ** section_headers_groups;
216
 
217
 
218
/* Flag bits indicating particular types of dump.  */
219
#define HEX_DUMP        (1 << 0)        /* The -x command line switch.  */
220
#define DISASS_DUMP     (1 << 1)        /* The -i command line switch.  */
221
#define DEBUG_DUMP      (1 << 2)        /* The -w command line switch.  */
222
#define STRING_DUMP     (1 << 3)        /* The -p command line switch.  */
223
#define RELOC_DUMP      (1 << 4)        /* The -R command line switch.  */
224
 
225
typedef unsigned char dump_type;
226
 
227
/* A linked list of the section names for which dumps were requested.  */
228
struct dump_list_entry
229
{
230
  char * name;
231
  dump_type type;
232
  struct dump_list_entry * next;
233
};
234
static struct dump_list_entry * dump_sects_byname;
235
 
236
/* A dynamic array of flags indicating for which sections a dump
237
   has been requested via command line switches.  */
238
static dump_type *   cmdline_dump_sects = NULL;
239
static unsigned int  num_cmdline_dump_sects = 0;
240
 
241
/* A dynamic array of flags indicating for which sections a dump of
242
   some kind has been requested.  It is reset on a per-object file
243
   basis and then initialised from the cmdline_dump_sects array,
244
   the results of interpreting the -w switch, and the
245
   dump_sects_byname list.  */
246
static dump_type *   dump_sects = NULL;
247
static unsigned int  num_dump_sects = 0;
248
 
249
 
250
/* How to print a vma value.  */
251
typedef enum print_mode
252
{
253
  HEX,
254
  DEC,
255
  DEC_5,
256
  UNSIGNED,
257
  PREFIX_HEX,
258
  FULL_HEX,
259
  LONG_HEX
260
}
261
print_mode;
262
 
263
#define UNKNOWN -1
264
 
265
#define SECTION_NAME(X)                                         \
266
  ((X) == NULL ? _("<none>")                                    \
267
   : string_table == NULL ? _("<no-name>")                      \
268
   : ((X)->sh_name >= string_table_length ? _("<corrupt>")      \
269
  : string_table + (X)->sh_name))
270
 
271
#define DT_VERSIONTAGIDX(tag)   (DT_VERNEEDNUM - (tag)) /* Reverse order!  */
272
 
273
#define GET_ELF_SYMBOLS(file, section)                  \
274
  (is_32bit_elf ? get_32bit_elf_symbols (file, section) \
275
   : get_64bit_elf_symbols (file, section))
276
 
277
#define VALID_DYNAMIC_NAME(offset)      ((dynamic_strings != NULL) && (offset < dynamic_strings_length))
278
/* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
279
   already been called and verified that the string exists.  */
280
#define GET_DYNAMIC_NAME(offset)        (dynamic_strings + offset)
281
 
282
#define REMOVE_ARCH_BITS(ADDR)                  \
283
  do                                            \
284
    {                                           \
285
      if (elf_header.e_machine == EM_ARM)       \
286
        (ADDR) &= ~1;                           \
287
    }                                           \
288
  while (0)
289
 
290
static void *
291
get_data (void * var, FILE * file, long offset, size_t size, size_t nmemb,
292
          const char * reason)
293
{
294
  void * mvar;
295
 
296
  if (size == 0 || nmemb == 0)
297
    return NULL;
298
 
299
  if (fseek (file, archive_file_offset + offset, SEEK_SET))
300
    {
301
      error (_("Unable to seek to 0x%lx for %s\n"),
302
             (unsigned long) archive_file_offset + offset, reason);
303
      return NULL;
304
    }
305
 
306
  mvar = var;
307
  if (mvar == NULL)
308
    {
309
      /* Check for overflow.  */
310
      if (nmemb < (~(size_t) 0 - 1) / size)
311
        /* + 1 so that we can '\0' terminate invalid string table sections.  */
312
        mvar = malloc (size * nmemb + 1);
313
 
314
      if (mvar == NULL)
315
        {
316
          error (_("Out of memory allocating 0x%lx bytes for %s\n"),
317
                 (unsigned long)(size * nmemb), reason);
318
          return NULL;
319
        }
320
 
321
      ((char *) mvar)[size * nmemb] = '\0';
322
    }
323
 
324
  if (fread (mvar, size, nmemb, file) != nmemb)
325
    {
326
      error (_("Unable to read in 0x%lx bytes of %s\n"),
327
             (unsigned long)(size * nmemb), reason);
328
      if (mvar != var)
329
        free (mvar);
330
      return NULL;
331
    }
332
 
333
  return mvar;
334
}
335
 
336
/* Print a VMA value.  */
337
 
338
static int
339
print_vma (bfd_vma vma, print_mode mode)
340
{
341
  int nc = 0;
342
 
343
  switch (mode)
344
    {
345
    case FULL_HEX:
346
      nc = printf ("0x");
347
      /* Drop through.  */
348
 
349
    case LONG_HEX:
350
#ifdef BFD64
351
      if (is_32bit_elf)
352
        return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
353
#endif
354
      printf_vma (vma);
355
      return nc + 16;
356
 
357
    case DEC_5:
358
      if (vma <= 99999)
359
        return printf ("%5" BFD_VMA_FMT "d", vma);
360
      /* Drop through.  */
361
 
362
    case PREFIX_HEX:
363
      nc = printf ("0x");
364
      /* Drop through.  */
365
 
366
    case HEX:
367
      return nc + printf ("%" BFD_VMA_FMT "x", vma);
368
 
369
    case DEC:
370
      return printf ("%" BFD_VMA_FMT "d", vma);
371
 
372
    case UNSIGNED:
373
      return printf ("%" BFD_VMA_FMT "u", vma);
374
    }
375
  return 0;
376
}
377
 
378
/* Display a symbol on stdout.  Handles the display of non-printing characters.
379
 
380
   If DO_WIDE is not true then format the symbol to be at most WIDTH characters,
381
   truncating as necessary.  If WIDTH is negative then format the string to be
382
   exactly - WIDTH characters, truncating or padding as necessary.
383
 
384
   Returns the number of emitted characters.  */
385
 
386
static unsigned int
387
print_symbol (int width, const char *symbol)
388
{
389
  const char *c;
390
  bfd_boolean extra_padding = FALSE;
391
  unsigned int num_printed = 0;
392
 
393
  if (do_wide)
394
    {
395
      /* Set the width to a very large value.  This simplifies the
396
         code below.  */
397
      width = INT_MAX;
398
    }
399
  else if (width < 0)
400
    {
401
      /* Keep the width positive.  This also helps.  */
402
      width = - width;
403
      extra_padding = TRUE;
404
    }
405
 
406
  while (width)
407
    {
408
      int len;
409
 
410
      c = symbol;
411
 
412
      /* Look for non-printing symbols inside the symbol's name.
413
         This test is triggered in particular by the names generated
414
         by the assembler for local labels.  */
415
      while (ISPRINT (*c))
416
        c++;
417
 
418
      len = c - symbol;
419
 
420
      if (len)
421
        {
422
          if (len > width)
423
            len = width;
424
 
425
          printf ("%.*s", len, symbol);
426
 
427
          width -= len;
428
          num_printed += len;
429
        }
430
 
431
      if (*c == 0 || width == 0)
432
        break;
433
 
434
      /* Now display the non-printing character, if
435
         there is room left in which to dipslay it.  */
436
      if ((unsigned char) *c < 32)
437
        {
438
          if (width < 2)
439
            break;
440
 
441
          printf ("^%c", *c + 0x40);
442
 
443
          width -= 2;
444
          num_printed += 2;
445
        }
446
      else
447
        {
448
          if (width < 6)
449
            break;
450
 
451
          printf ("<0x%.2x>", (unsigned char) *c);
452
 
453
          width -= 6;
454
          num_printed += 6;
455
        }
456
 
457
      symbol = c + 1;
458
    }
459
 
460
  if (extra_padding && width > 0)
461
    {
462
      /* Fill in the remaining spaces.  */
463
      printf ("%-*s", width, " ");
464
      num_printed += 2;
465
    }
466
 
467
  return num_printed;
468
}
469
 
470
/* Return a pointer to section NAME, or NULL if no such section exists.  */
471
 
472
static Elf_Internal_Shdr *
473
find_section (const char * name)
474
{
475
  unsigned int i;
476
 
477
  for (i = 0; i < elf_header.e_shnum; i++)
478
    if (streq (SECTION_NAME (section_headers + i), name))
479
      return section_headers + i;
480
 
481
  return NULL;
482
}
483
 
484
/* Return a pointer to a section containing ADDR, or NULL if no such
485
   section exists.  */
486
 
487
static Elf_Internal_Shdr *
488
find_section_by_address (bfd_vma addr)
489
{
490
  unsigned int i;
491
 
492
  for (i = 0; i < elf_header.e_shnum; i++)
493
    {
494
      Elf_Internal_Shdr *sec = section_headers + i;
495
      if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
496
        return sec;
497
    }
498
 
499
  return NULL;
500
}
501
 
502
/* Read an unsigned LEB128 encoded value from p.  Set *PLEN to the number of
503
   bytes read.  */
504
 
505
static unsigned long
506
read_uleb128 (unsigned char *data, unsigned int *length_return)
507
{
508
  return read_leb128 (data, length_return, 0);
509
}
510
 
511
/* Return true if the current file is for IA-64 machine and OpenVMS ABI.
512
   This OS has so many departures from the ELF standard that we test it at
513
   many places.  */
514
 
515
static inline int
516
is_ia64_vms (void)
517
{
518
  return elf_header.e_machine == EM_IA_64
519
    && elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
520
}
521
 
522
/* Guess the relocation size commonly used by the specific machines.  */
523
 
524
static int
525
guess_is_rela (unsigned int e_machine)
526
{
527
  switch (e_machine)
528
    {
529
      /* Targets that use REL relocations.  */
530
    case EM_386:
531
    case EM_486:
532
    case EM_960:
533
    case EM_ARM:
534
    case EM_D10V:
535
    case EM_CYGNUS_D10V:
536
    case EM_DLX:
537
    case EM_MIPS:
538
    case EM_MIPS_RS3_LE:
539
    case EM_CYGNUS_M32R:
540
    case EM_OPENRISC:
541
    case EM_OR32:
542
    case EM_SCORE:
543
      return FALSE;
544
 
545
      /* Targets that use RELA relocations.  */
546
    case EM_68K:
547
    case EM_860:
548
    case EM_ALPHA:
549
    case EM_ALTERA_NIOS2:
550
    case EM_AVR:
551
    case EM_AVR_OLD:
552
    case EM_BLACKFIN:
553
    case EM_CR16:
554
    case EM_CR16_OLD:
555
    case EM_CRIS:
556
    case EM_CRX:
557
    case EM_D30V:
558
    case EM_CYGNUS_D30V:
559
    case EM_FR30:
560
    case EM_CYGNUS_FR30:
561
    case EM_CYGNUS_FRV:
562
    case EM_H8S:
563
    case EM_H8_300:
564
    case EM_H8_300H:
565
    case EM_IA_64:
566
    case EM_IP2K:
567
    case EM_IP2K_OLD:
568
    case EM_IQ2000:
569
    case EM_LATTICEMICO32:
570
    case EM_M32C_OLD:
571
    case EM_M32C:
572
    case EM_M32R:
573
    case EM_MCORE:
574
    case EM_CYGNUS_MEP:
575
    case EM_MMIX:
576
    case EM_MN10200:
577
    case EM_CYGNUS_MN10200:
578
    case EM_MN10300:
579
    case EM_CYGNUS_MN10300:
580
    case EM_MOXIE:
581
    case EM_MSP430:
582
    case EM_MSP430_OLD:
583
    case EM_MT:
584
    case EM_NIOS32:
585
    case EM_OPEN8:
586
    case EM_PPC64:
587
    case EM_PPC:
588
    case EM_RX:
589
    case EM_S390:
590
    case EM_S390_OLD:
591
    case EM_SH:
592
    case EM_SPARC:
593
    case EM_SPARC32PLUS:
594
    case EM_SPARCV9:
595
    case EM_SPU:
596
    case EM_TI_C6000:
597
    case EM_V850:
598
    case EM_CYGNUS_V850:
599
    case EM_VAX:
600
    case EM_X86_64:
601
    case EM_L1OM:
602
    case EM_XSTORMY16:
603
    case EM_XTENSA:
604
    case EM_XTENSA_OLD:
605
    case EM_MICROBLAZE:
606
    case EM_MICROBLAZE_OLD:
607
      return TRUE;
608
 
609
    case EM_68HC05:
610
    case EM_68HC08:
611
    case EM_68HC11:
612
    case EM_68HC16:
613
    case EM_FX66:
614
    case EM_ME16:
615
    case EM_MMA:
616
    case EM_NCPU:
617
    case EM_NDR1:
618
    case EM_PCP:
619
    case EM_ST100:
620
    case EM_ST19:
621
    case EM_ST7:
622
    case EM_ST9PLUS:
623
    case EM_STARCORE:
624
    case EM_SVX:
625
    case EM_TINYJ:
626
    default:
627
      warn (_("Don't know about relocations on this machine architecture\n"));
628
      return FALSE;
629
    }
630
}
631
 
632
static int
633
slurp_rela_relocs (FILE * file,
634
                   unsigned long rel_offset,
635
                   unsigned long rel_size,
636
                   Elf_Internal_Rela ** relasp,
637
                   unsigned long * nrelasp)
638
{
639
  Elf_Internal_Rela * relas;
640
  unsigned long nrelas;
641
  unsigned int i;
642
 
643
  if (is_32bit_elf)
644
    {
645
      Elf32_External_Rela * erelas;
646
 
647
      erelas = (Elf32_External_Rela *) get_data (NULL, file, rel_offset, 1,
648
                                                 rel_size, _("relocs"));
649
      if (!erelas)
650
        return 0;
651
 
652
      nrelas = rel_size / sizeof (Elf32_External_Rela);
653
 
654
      relas = (Elf_Internal_Rela *) cmalloc (nrelas,
655
                                             sizeof (Elf_Internal_Rela));
656
 
657
      if (relas == NULL)
658
        {
659
          free (erelas);
660
          error (_("out of memory parsing relocs\n"));
661
          return 0;
662
        }
663
 
664
      for (i = 0; i < nrelas; i++)
665
        {
666
          relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
667
          relas[i].r_info   = BYTE_GET (erelas[i].r_info);
668
          relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
669
        }
670
 
671
      free (erelas);
672
    }
673
  else
674
    {
675
      Elf64_External_Rela * erelas;
676
 
677
      erelas = (Elf64_External_Rela *) get_data (NULL, file, rel_offset, 1,
678
                                                 rel_size, _("relocs"));
679
      if (!erelas)
680
        return 0;
681
 
682
      nrelas = rel_size / sizeof (Elf64_External_Rela);
683
 
684
      relas = (Elf_Internal_Rela *) cmalloc (nrelas,
685
                                             sizeof (Elf_Internal_Rela));
686
 
687
      if (relas == NULL)
688
        {
689
          free (erelas);
690
          error (_("out of memory parsing relocs\n"));
691
          return 0;
692
        }
693
 
694
      for (i = 0; i < nrelas; i++)
695
        {
696
          relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
697
          relas[i].r_info   = BYTE_GET (erelas[i].r_info);
698
          relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
699
 
700
          /* The #ifdef BFD64 below is to prevent a compile time
701
             warning.  We know that if we do not have a 64 bit data
702
             type that we will never execute this code anyway.  */
703
#ifdef BFD64
704
          if (elf_header.e_machine == EM_MIPS
705
              && elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
706
            {
707
              /* In little-endian objects, r_info isn't really a
708
                 64-bit little-endian value: it has a 32-bit
709
                 little-endian symbol index followed by four
710
                 individual byte fields.  Reorder INFO
711
                 accordingly.  */
712
              bfd_vma inf = relas[i].r_info;
713
              inf = (((inf & 0xffffffff) << 32)
714
                      | ((inf >> 56) & 0xff)
715
                      | ((inf >> 40) & 0xff00)
716
                      | ((inf >> 24) & 0xff0000)
717
                      | ((inf >> 8) & 0xff000000));
718
              relas[i].r_info = inf;
719
            }
720
#endif /* BFD64 */
721
        }
722
 
723
      free (erelas);
724
    }
725
  *relasp = relas;
726
  *nrelasp = nrelas;
727
  return 1;
728
}
729
 
730
static int
731
slurp_rel_relocs (FILE * file,
732
                  unsigned long rel_offset,
733
                  unsigned long rel_size,
734
                  Elf_Internal_Rela ** relsp,
735
                  unsigned long * nrelsp)
736
{
737
  Elf_Internal_Rela * rels;
738
  unsigned long nrels;
739
  unsigned int i;
740
 
741
  if (is_32bit_elf)
742
    {
743
      Elf32_External_Rel * erels;
744
 
745
      erels = (Elf32_External_Rel *) get_data (NULL, file, rel_offset, 1,
746
                                               rel_size, _("relocs"));
747
      if (!erels)
748
        return 0;
749
 
750
      nrels = rel_size / sizeof (Elf32_External_Rel);
751
 
752
      rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
753
 
754
      if (rels == NULL)
755
        {
756
          free (erels);
757
          error (_("out of memory parsing relocs\n"));
758
          return 0;
759
        }
760
 
761
      for (i = 0; i < nrels; i++)
762
        {
763
          rels[i].r_offset = BYTE_GET (erels[i].r_offset);
764
          rels[i].r_info   = BYTE_GET (erels[i].r_info);
765
          rels[i].r_addend = 0;
766
        }
767
 
768
      free (erels);
769
    }
770
  else
771
    {
772
      Elf64_External_Rel * erels;
773
 
774
      erels = (Elf64_External_Rel *) get_data (NULL, file, rel_offset, 1,
775
                                               rel_size, _("relocs"));
776
      if (!erels)
777
        return 0;
778
 
779
      nrels = rel_size / sizeof (Elf64_External_Rel);
780
 
781
      rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
782
 
783
      if (rels == NULL)
784
        {
785
          free (erels);
786
          error (_("out of memory parsing relocs\n"));
787
          return 0;
788
        }
789
 
790
      for (i = 0; i < nrels; i++)
791
        {
792
          rels[i].r_offset = BYTE_GET (erels[i].r_offset);
793
          rels[i].r_info   = BYTE_GET (erels[i].r_info);
794
          rels[i].r_addend = 0;
795
 
796
          /* The #ifdef BFD64 below is to prevent a compile time
797
             warning.  We know that if we do not have a 64 bit data
798
             type that we will never execute this code anyway.  */
799
#ifdef BFD64
800
          if (elf_header.e_machine == EM_MIPS
801
              && elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
802
            {
803
              /* In little-endian objects, r_info isn't really a
804
                 64-bit little-endian value: it has a 32-bit
805
                 little-endian symbol index followed by four
806
                 individual byte fields.  Reorder INFO
807
                 accordingly.  */
808
              bfd_vma inf = rels[i].r_info;
809
              inf = (((inf & 0xffffffff) << 32)
810
                     | ((inf >> 56) & 0xff)
811
                     | ((inf >> 40) & 0xff00)
812
                     | ((inf >> 24) & 0xff0000)
813
                     | ((inf >> 8) & 0xff000000));
814
              rels[i].r_info = inf;
815
            }
816
#endif /* BFD64 */
817
        }
818
 
819
      free (erels);
820
    }
821
  *relsp = rels;
822
  *nrelsp = nrels;
823
  return 1;
824
}
825
 
826
/* Returns the reloc type extracted from the reloc info field.  */
827
 
828
static unsigned int
829
get_reloc_type (bfd_vma reloc_info)
830
{
831
  if (is_32bit_elf)
832
    return ELF32_R_TYPE (reloc_info);
833
 
834
  switch (elf_header.e_machine)
835
    {
836
    case EM_MIPS:
837
      /* Note: We assume that reloc_info has already been adjusted for us.  */
838
      return ELF64_MIPS_R_TYPE (reloc_info);
839
 
840
    case EM_SPARCV9:
841
      return ELF64_R_TYPE_ID (reloc_info);
842
 
843
    default:
844
      return ELF64_R_TYPE (reloc_info);
845
    }
846
}
847
 
848
/* Return the symbol index extracted from the reloc info field.  */
849
 
850
static bfd_vma
851
get_reloc_symindex (bfd_vma reloc_info)
852
{
853
  return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
854
}
855
 
856
/* Display the contents of the relocation data found at the specified
857
   offset.  */
858
 
859
static void
860
dump_relocations (FILE * file,
861
                  unsigned long rel_offset,
862
                  unsigned long rel_size,
863
                  Elf_Internal_Sym * symtab,
864
                  unsigned long nsyms,
865
                  char * strtab,
866
                  unsigned long strtablen,
867
                  int is_rela)
868
{
869
  unsigned int i;
870
  Elf_Internal_Rela * rels;
871
 
872
  if (is_rela == UNKNOWN)
873
    is_rela = guess_is_rela (elf_header.e_machine);
874
 
875
  if (is_rela)
876
    {
877
      if (!slurp_rela_relocs (file, rel_offset, rel_size, &rels, &rel_size))
878
        return;
879
    }
880
  else
881
    {
882
      if (!slurp_rel_relocs (file, rel_offset, rel_size, &rels, &rel_size))
883
        return;
884
    }
885
 
886
  if (is_32bit_elf)
887
    {
888
      if (is_rela)
889
        {
890
          if (do_wide)
891
            printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name + Addend\n"));
892
          else
893
            printf (_(" Offset     Info    Type            Sym.Value  Sym. Name + Addend\n"));
894
        }
895
      else
896
        {
897
          if (do_wide)
898
            printf (_(" Offset     Info    Type                Sym. Value  Symbol's Name\n"));
899
          else
900
            printf (_(" Offset     Info    Type            Sym.Value  Sym. Name\n"));
901
        }
902
    }
903
  else
904
    {
905
      if (is_rela)
906
        {
907
          if (do_wide)
908
            printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend\n"));
909
          else
910
            printf (_("  Offset          Info           Type           Sym. Value    Sym. Name + Addend\n"));
911
        }
912
      else
913
        {
914
          if (do_wide)
915
            printf (_("    Offset             Info             Type               Symbol's Value  Symbol's Name\n"));
916
          else
917
            printf (_("  Offset          Info           Type           Sym. Value    Sym. Name\n"));
918
        }
919
    }
920
 
921
  for (i = 0; i < rel_size; i++)
922
    {
923
      const char * rtype;
924
      bfd_vma offset;
925
      bfd_vma inf;
926
      bfd_vma symtab_index;
927
      bfd_vma type;
928
 
929
      offset = rels[i].r_offset;
930
      inf    = rels[i].r_info;
931
 
932
      type = get_reloc_type (inf);
933
      symtab_index = get_reloc_symindex  (inf);
934
 
935
      if (is_32bit_elf)
936
        {
937
          printf ("%8.8lx  %8.8lx ",
938
                  (unsigned long) offset & 0xffffffff,
939
                  (unsigned long) inf & 0xffffffff);
940
        }
941
      else
942
        {
943
#if BFD_HOST_64BIT_LONG
944
          printf (do_wide
945
                  ? "%16.16lx  %16.16lx "
946
                  : "%12.12lx  %12.12lx ",
947
                  offset, inf);
948
#elif BFD_HOST_64BIT_LONG_LONG
949
#ifndef __MSVCRT__
950
          printf (do_wide
951
                  ? "%16.16llx  %16.16llx "
952
                  : "%12.12llx  %12.12llx ",
953
                  offset, inf);
954
#else
955
          printf (do_wide
956
                  ? "%16.16I64x  %16.16I64x "
957
                  : "%12.12I64x  %12.12I64x ",
958
                  offset, inf);
959
#endif
960
#else
961
          printf (do_wide
962
                  ? "%8.8lx%8.8lx  %8.8lx%8.8lx "
963
                  : "%4.4lx%8.8lx  %4.4lx%8.8lx ",
964
                  _bfd_int64_high (offset),
965
                  _bfd_int64_low (offset),
966
                  _bfd_int64_high (inf),
967
                  _bfd_int64_low (inf));
968
#endif
969
        }
970
 
971
      switch (elf_header.e_machine)
972
        {
973
        default:
974
          rtype = NULL;
975
          break;
976
 
977
        case EM_M32R:
978
        case EM_CYGNUS_M32R:
979
          rtype = elf_m32r_reloc_type (type);
980
          break;
981
 
982
        case EM_386:
983
        case EM_486:
984
          rtype = elf_i386_reloc_type (type);
985
          break;
986
 
987
        case EM_68HC11:
988
        case EM_68HC12:
989
          rtype = elf_m68hc11_reloc_type (type);
990
          break;
991
 
992
        case EM_68K:
993
          rtype = elf_m68k_reloc_type (type);
994
          break;
995
 
996
        case EM_960:
997
          rtype = elf_i960_reloc_type (type);
998
          break;
999
 
1000
        case EM_AVR:
1001
        case EM_AVR_OLD:
1002
          rtype = elf_avr_reloc_type (type);
1003
          break;
1004
 
1005
        case EM_OLD_SPARCV9:
1006
        case EM_SPARC32PLUS:
1007
        case EM_SPARCV9:
1008
        case EM_SPARC:
1009
          rtype = elf_sparc_reloc_type (type);
1010
          break;
1011
 
1012
        case EM_SPU:
1013
          rtype = elf_spu_reloc_type (type);
1014
          break;
1015
 
1016
        case EM_V850:
1017
        case EM_CYGNUS_V850:
1018
          rtype = v850_reloc_type (type);
1019
          break;
1020
 
1021
        case EM_D10V:
1022
        case EM_CYGNUS_D10V:
1023
          rtype = elf_d10v_reloc_type (type);
1024
          break;
1025
 
1026
        case EM_D30V:
1027
        case EM_CYGNUS_D30V:
1028
          rtype = elf_d30v_reloc_type (type);
1029
          break;
1030
 
1031
        case EM_DLX:
1032
          rtype = elf_dlx_reloc_type (type);
1033
          break;
1034
 
1035
        case EM_SH:
1036
          rtype = elf_sh_reloc_type (type);
1037
          break;
1038
 
1039
        case EM_MN10300:
1040
        case EM_CYGNUS_MN10300:
1041
          rtype = elf_mn10300_reloc_type (type);
1042
          break;
1043
 
1044
        case EM_MN10200:
1045
        case EM_CYGNUS_MN10200:
1046
          rtype = elf_mn10200_reloc_type (type);
1047
          break;
1048
 
1049
        case EM_FR30:
1050
        case EM_CYGNUS_FR30:
1051
          rtype = elf_fr30_reloc_type (type);
1052
          break;
1053
 
1054
        case EM_CYGNUS_FRV:
1055
          rtype = elf_frv_reloc_type (type);
1056
          break;
1057
 
1058
        case EM_MCORE:
1059
          rtype = elf_mcore_reloc_type (type);
1060
          break;
1061
 
1062
        case EM_MMIX:
1063
          rtype = elf_mmix_reloc_type (type);
1064
          break;
1065
 
1066
        case EM_MOXIE:
1067
          rtype = elf_moxie_reloc_type (type);
1068
          break;
1069
 
1070
        case EM_MSP430:
1071
        case EM_MSP430_OLD:
1072
          rtype = elf_msp430_reloc_type (type);
1073
          break;
1074
 
1075
        case EM_OPEN8:
1076
          rtype = elf_open8_reloc_type (type);
1077
          break;
1078
 
1079
        case EM_PPC:
1080
          rtype = elf_ppc_reloc_type (type);
1081
          break;
1082
 
1083
        case EM_PPC64:
1084
          rtype = elf_ppc64_reloc_type (type);
1085
          break;
1086
 
1087
        case EM_MIPS:
1088
        case EM_MIPS_RS3_LE:
1089
          rtype = elf_mips_reloc_type (type);
1090
          break;
1091
 
1092
        case EM_ALPHA:
1093
          rtype = elf_alpha_reloc_type (type);
1094
          break;
1095
 
1096
        case EM_ARM:
1097
          rtype = elf_arm_reloc_type (type);
1098
          break;
1099
 
1100
        case EM_ARC:
1101
          rtype = elf_arc_reloc_type (type);
1102
          break;
1103
 
1104
        case EM_PARISC:
1105
          rtype = elf_hppa_reloc_type (type);
1106
          break;
1107
 
1108
        case EM_H8_300:
1109
        case EM_H8_300H:
1110
        case EM_H8S:
1111
          rtype = elf_h8_reloc_type (type);
1112
          break;
1113
 
1114
        case EM_OPENRISC:
1115
        case EM_OR32:
1116
          rtype = elf_or32_reloc_type (type);
1117
          break;
1118
 
1119
        case EM_PJ:
1120
        case EM_PJ_OLD:
1121
          rtype = elf_pj_reloc_type (type);
1122
          break;
1123
        case EM_IA_64:
1124
          rtype = elf_ia64_reloc_type (type);
1125
          break;
1126
 
1127
        case EM_CRIS:
1128
          rtype = elf_cris_reloc_type (type);
1129
          break;
1130
 
1131
        case EM_860:
1132
          rtype = elf_i860_reloc_type (type);
1133
          break;
1134
 
1135
        case EM_X86_64:
1136
        case EM_L1OM:
1137
          rtype = elf_x86_64_reloc_type (type);
1138
          break;
1139
 
1140
        case EM_S370:
1141
          rtype = i370_reloc_type (type);
1142
          break;
1143
 
1144
        case EM_S390_OLD:
1145
        case EM_S390:
1146
          rtype = elf_s390_reloc_type (type);
1147
          break;
1148
 
1149
        case EM_SCORE:
1150
          rtype = elf_score_reloc_type (type);
1151
          break;
1152
 
1153
        case EM_XSTORMY16:
1154
          rtype = elf_xstormy16_reloc_type (type);
1155
          break;
1156
 
1157
        case EM_CRX:
1158
          rtype = elf_crx_reloc_type (type);
1159
          break;
1160
 
1161
        case EM_VAX:
1162
          rtype = elf_vax_reloc_type (type);
1163
          break;
1164
 
1165
        case EM_IP2K:
1166
        case EM_IP2K_OLD:
1167
          rtype = elf_ip2k_reloc_type (type);
1168
          break;
1169
 
1170
        case EM_IQ2000:
1171
          rtype = elf_iq2000_reloc_type (type);
1172
          break;
1173
 
1174
        case EM_XTENSA_OLD:
1175
        case EM_XTENSA:
1176
          rtype = elf_xtensa_reloc_type (type);
1177
          break;
1178
 
1179
        case EM_LATTICEMICO32:
1180
          rtype = elf_lm32_reloc_type (type);
1181
          break;
1182
 
1183
        case EM_M32C_OLD:
1184
        case EM_M32C:
1185
          rtype = elf_m32c_reloc_type (type);
1186
          break;
1187
 
1188
        case EM_MT:
1189
          rtype = elf_mt_reloc_type (type);
1190
          break;
1191
 
1192
        case EM_BLACKFIN:
1193
          rtype = elf_bfin_reloc_type (type);
1194
          break;
1195
 
1196
        case EM_CYGNUS_MEP:
1197
          rtype = elf_mep_reloc_type (type);
1198
          break;
1199
 
1200
        case EM_CR16:
1201
        case EM_CR16_OLD:
1202
          rtype = elf_cr16_reloc_type (type);
1203
          break;
1204
 
1205
        case EM_MICROBLAZE:
1206
        case EM_MICROBLAZE_OLD:
1207
          rtype = elf_microblaze_reloc_type (type);
1208
          break;
1209
 
1210
        case EM_RX:
1211
          rtype = elf_rx_reloc_type (type);
1212
          break;
1213
 
1214
        case EM_XC16X:
1215
        case EM_C166:
1216
          rtype = elf_xc16x_reloc_type (type);
1217
          break;
1218
 
1219
        case EM_TI_C6000:
1220
          rtype = elf_tic6x_reloc_type (type);
1221
          break;
1222
        }
1223
 
1224
      if (rtype == NULL)
1225
        printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1226
      else
1227
        printf (do_wide ? "%-22.22s" : "%-17.17s", rtype);
1228
 
1229
      if (elf_header.e_machine == EM_ALPHA
1230
          && rtype != NULL
1231
          && streq (rtype, "R_ALPHA_LITUSE")
1232
          && is_rela)
1233
        {
1234
          switch (rels[i].r_addend)
1235
            {
1236
            case LITUSE_ALPHA_ADDR:   rtype = "ADDR";   break;
1237
            case LITUSE_ALPHA_BASE:   rtype = "BASE";   break;
1238
            case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1239
            case LITUSE_ALPHA_JSR:    rtype = "JSR";    break;
1240
            case LITUSE_ALPHA_TLSGD:  rtype = "TLSGD";  break;
1241
            case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1242
            case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1243
            default: rtype = NULL;
1244
            }
1245
          if (rtype)
1246
            printf (" (%s)", rtype);
1247
          else
1248
            {
1249
              putchar (' ');
1250
              printf (_("<unknown addend: %lx>"),
1251
                      (unsigned long) rels[i].r_addend);
1252
            }
1253
        }
1254
      else if (symtab_index)
1255
        {
1256
          if (symtab == NULL || symtab_index >= nsyms)
1257
            printf (_(" bad symbol index: %08lx"), (unsigned long) symtab_index);
1258
          else
1259
            {
1260
              Elf_Internal_Sym * psym;
1261
 
1262
              psym = symtab + symtab_index;
1263
 
1264
              printf (" ");
1265
 
1266
              if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1267
                {
1268
                  const char * name;
1269
                  unsigned int len;
1270
                  unsigned int width = is_32bit_elf ? 8 : 14;
1271
 
1272
                  /* Relocations against GNU_IFUNC symbols do not use the value
1273
                     of the symbol as the address to relocate against.  Instead
1274
                     they invoke the function named by the symbol and use its
1275
                     result as the address for relocation.
1276
 
1277
                     To indicate this to the user, do not display the value of
1278
                     the symbol in the "Symbols's Value" field.  Instead show
1279
                     its name followed by () as a hint that the symbol is
1280
                     invoked.  */
1281
 
1282
                  if (strtab == NULL
1283
                      || psym->st_name == 0
1284
                      || psym->st_name >= strtablen)
1285
                    name = "??";
1286
                  else
1287
                    name = strtab + psym->st_name;
1288
 
1289
                  len = print_symbol (width, name);
1290
                  printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1291
                }
1292
              else
1293
                {
1294
                  print_vma (psym->st_value, LONG_HEX);
1295
 
1296
                  printf (is_32bit_elf ? "   " : " ");
1297
                }
1298
 
1299
              if (psym->st_name == 0)
1300
                {
1301
                  const char * sec_name = "<null>";
1302
                  char name_buf[40];
1303
 
1304
                  if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1305
                    {
1306
                      if (psym->st_shndx < elf_header.e_shnum)
1307
                        sec_name
1308
                          = SECTION_NAME (section_headers + psym->st_shndx);
1309
                      else if (psym->st_shndx == SHN_ABS)
1310
                        sec_name = "ABS";
1311
                      else if (psym->st_shndx == SHN_COMMON)
1312
                        sec_name = "COMMON";
1313
                      else if ((elf_header.e_machine == EM_MIPS
1314
                                && psym->st_shndx == SHN_MIPS_SCOMMON)
1315
                               || (elf_header.e_machine == EM_TI_C6000
1316
                                   && psym->st_shndx == SHN_TIC6X_SCOMMON))
1317
                        sec_name = "SCOMMON";
1318
                      else if (elf_header.e_machine == EM_MIPS
1319
                               && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1320
                        sec_name = "SUNDEF";
1321
                      else if ((elf_header.e_machine == EM_X86_64
1322
                                || elf_header.e_machine == EM_L1OM)
1323
                               && psym->st_shndx == SHN_X86_64_LCOMMON)
1324
                        sec_name = "LARGE_COMMON";
1325
                      else if (elf_header.e_machine == EM_IA_64
1326
                               && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1327
                               && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1328
                        sec_name = "ANSI_COM";
1329
                      else if (is_ia64_vms ()
1330
                               && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1331
                        sec_name = "VMS_SYMVEC";
1332
                      else
1333
                        {
1334
                          sprintf (name_buf, "<section 0x%x>",
1335
                                   (unsigned int) psym->st_shndx);
1336
                          sec_name = name_buf;
1337
                        }
1338
                    }
1339
                  print_symbol (22, sec_name);
1340
                }
1341
              else if (strtab == NULL)
1342
                printf (_("<string table index: %3ld>"), psym->st_name);
1343
              else if (psym->st_name >= strtablen)
1344
                printf (_("<corrupt string table index: %3ld>"), psym->st_name);
1345
              else
1346
                print_symbol (22, strtab + psym->st_name);
1347
 
1348
              if (is_rela)
1349
                {
1350
                  bfd_signed_vma off = rels[i].r_addend;
1351
 
1352
                  if (off < 0)
1353
                    printf (" - %" BFD_VMA_FMT "x", - off);
1354
                  else
1355
                    printf (" + %" BFD_VMA_FMT "x", off);
1356
                }
1357
            }
1358
        }
1359
      else if (is_rela)
1360
        {
1361
          printf ("%*c", is_32bit_elf ?
1362
                  (do_wide ? 34 : 28) : (do_wide ? 26 : 20), ' ');
1363
          print_vma (rels[i].r_addend, LONG_HEX);
1364
        }
1365
 
1366
      if (elf_header.e_machine == EM_SPARCV9
1367
          && rtype != NULL
1368
          && streq (rtype, "R_SPARC_OLO10"))
1369
        printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
1370
 
1371
      putchar ('\n');
1372
 
1373
#ifdef BFD64
1374
      if (! is_32bit_elf && elf_header.e_machine == EM_MIPS)
1375
        {
1376
          bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
1377
          bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
1378
          const char * rtype2 = elf_mips_reloc_type (type2);
1379
          const char * rtype3 = elf_mips_reloc_type (type3);
1380
 
1381
          printf ("                    Type2: ");
1382
 
1383
          if (rtype2 == NULL)
1384
            printf (_("unrecognized: %-7lx"),
1385
                    (unsigned long) type2 & 0xffffffff);
1386
          else
1387
            printf ("%-17.17s", rtype2);
1388
 
1389
          printf ("\n                    Type3: ");
1390
 
1391
          if (rtype3 == NULL)
1392
            printf (_("unrecognized: %-7lx"),
1393
                    (unsigned long) type3 & 0xffffffff);
1394
          else
1395
            printf ("%-17.17s", rtype3);
1396
 
1397
          putchar ('\n');
1398
        }
1399
#endif /* BFD64 */
1400
    }
1401
 
1402
  free (rels);
1403
}
1404
 
1405
static const char *
1406
get_mips_dynamic_type (unsigned long type)
1407
{
1408
  switch (type)
1409
    {
1410
    case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1411
    case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1412
    case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1413
    case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1414
    case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1415
    case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1416
    case DT_MIPS_MSYM: return "MIPS_MSYM";
1417
    case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1418
    case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1419
    case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1420
    case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1421
    case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1422
    case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1423
    case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1424
    case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1425
    case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1426
    case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1427
    case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1428
    case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1429
    case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1430
    case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1431
    case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1432
    case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1433
    case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1434
    case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1435
    case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1436
    case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1437
    case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1438
    case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1439
    case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1440
    case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1441
    case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1442
    case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1443
    case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1444
    case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1445
    case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1446
    case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1447
    case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1448
    case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1449
    case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1450
    case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1451
    case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1452
    case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1453
    case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
1454
    case DT_MIPS_RWPLT: return "MIPS_RWPLT";
1455
    default:
1456
      return NULL;
1457
    }
1458
}
1459
 
1460
static const char *
1461
get_sparc64_dynamic_type (unsigned long type)
1462
{
1463
  switch (type)
1464
    {
1465
    case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1466
    default:
1467
      return NULL;
1468
    }
1469
}
1470
 
1471
static const char *
1472
get_ppc_dynamic_type (unsigned long type)
1473
{
1474
  switch (type)
1475
    {
1476
    case DT_PPC_GOT:    return "PPC_GOT";
1477
    case DT_PPC_TLSOPT: return "PPC_TLSOPT";
1478
    default:
1479
      return NULL;
1480
    }
1481
}
1482
 
1483
static const char *
1484
get_ppc64_dynamic_type (unsigned long type)
1485
{
1486
  switch (type)
1487
    {
1488
    case DT_PPC64_GLINK:  return "PPC64_GLINK";
1489
    case DT_PPC64_OPD:    return "PPC64_OPD";
1490
    case DT_PPC64_OPDSZ:  return "PPC64_OPDSZ";
1491
    case DT_PPC64_TLSOPT: return "PPC64_TLSOPT";
1492
    default:
1493
      return NULL;
1494
    }
1495
}
1496
 
1497
static const char *
1498
get_parisc_dynamic_type (unsigned long type)
1499
{
1500
  switch (type)
1501
    {
1502
    case DT_HP_LOAD_MAP:        return "HP_LOAD_MAP";
1503
    case DT_HP_DLD_FLAGS:       return "HP_DLD_FLAGS";
1504
    case DT_HP_DLD_HOOK:        return "HP_DLD_HOOK";
1505
    case DT_HP_UX10_INIT:       return "HP_UX10_INIT";
1506
    case DT_HP_UX10_INITSZ:     return "HP_UX10_INITSZ";
1507
    case DT_HP_PREINIT:         return "HP_PREINIT";
1508
    case DT_HP_PREINITSZ:       return "HP_PREINITSZ";
1509
    case DT_HP_NEEDED:          return "HP_NEEDED";
1510
    case DT_HP_TIME_STAMP:      return "HP_TIME_STAMP";
1511
    case DT_HP_CHECKSUM:        return "HP_CHECKSUM";
1512
    case DT_HP_GST_SIZE:        return "HP_GST_SIZE";
1513
    case DT_HP_GST_VERSION:     return "HP_GST_VERSION";
1514
    case DT_HP_GST_HASHVAL:     return "HP_GST_HASHVAL";
1515
    case DT_HP_EPLTREL:         return "HP_GST_EPLTREL";
1516
    case DT_HP_EPLTRELSZ:       return "HP_GST_EPLTRELSZ";
1517
    case DT_HP_FILTERED:        return "HP_FILTERED";
1518
    case DT_HP_FILTER_TLS:      return "HP_FILTER_TLS";
1519
    case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
1520
    case DT_HP_LAZYLOAD:        return "HP_LAZYLOAD";
1521
    case DT_HP_BIND_NOW_COUNT:  return "HP_BIND_NOW_COUNT";
1522
    case DT_PLT:                return "PLT";
1523
    case DT_PLT_SIZE:           return "PLT_SIZE";
1524
    case DT_DLT:                return "DLT";
1525
    case DT_DLT_SIZE:           return "DLT_SIZE";
1526
    default:
1527
      return NULL;
1528
    }
1529
}
1530
 
1531
static const char *
1532
get_ia64_dynamic_type (unsigned long type)
1533
{
1534
  switch (type)
1535
    {
1536
    case DT_IA_64_PLT_RESERVE:         return "IA_64_PLT_RESERVE";
1537
    case DT_IA_64_VMS_SUBTYPE:         return "VMS_SUBTYPE";
1538
    case DT_IA_64_VMS_IMGIOCNT:        return "VMS_IMGIOCNT";
1539
    case DT_IA_64_VMS_LNKFLAGS:        return "VMS_LNKFLAGS";
1540
    case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
1541
    case DT_IA_64_VMS_IDENT:           return "VMS_IDENT";
1542
    case DT_IA_64_VMS_NEEDED_IDENT:    return "VMS_NEEDED_IDENT";
1543
    case DT_IA_64_VMS_IMG_RELA_CNT:    return "VMS_IMG_RELA_CNT";
1544
    case DT_IA_64_VMS_SEG_RELA_CNT:    return "VMS_SEG_RELA_CNT";
1545
    case DT_IA_64_VMS_FIXUP_RELA_CNT:  return "VMS_FIXUP_RELA_CNT";
1546
    case DT_IA_64_VMS_FIXUP_NEEDED:    return "VMS_FIXUP_NEEDED";
1547
    case DT_IA_64_VMS_SYMVEC_CNT:      return "VMS_SYMVEC_CNT";
1548
    case DT_IA_64_VMS_XLATED:          return "VMS_XLATED";
1549
    case DT_IA_64_VMS_STACKSIZE:       return "VMS_STACKSIZE";
1550
    case DT_IA_64_VMS_UNWINDSZ:        return "VMS_UNWINDSZ";
1551
    case DT_IA_64_VMS_UNWIND_CODSEG:   return "VMS_UNWIND_CODSEG";
1552
    case DT_IA_64_VMS_UNWIND_INFOSEG:  return "VMS_UNWIND_INFOSEG";
1553
    case DT_IA_64_VMS_LINKTIME:        return "VMS_LINKTIME";
1554
    case DT_IA_64_VMS_SEG_NO:          return "VMS_SEG_NO";
1555
    case DT_IA_64_VMS_SYMVEC_OFFSET:   return "VMS_SYMVEC_OFFSET";
1556
    case DT_IA_64_VMS_SYMVEC_SEG:      return "VMS_SYMVEC_SEG";
1557
    case DT_IA_64_VMS_UNWIND_OFFSET:   return "VMS_UNWIND_OFFSET";
1558
    case DT_IA_64_VMS_UNWIND_SEG:      return "VMS_UNWIND_SEG";
1559
    case DT_IA_64_VMS_STRTAB_OFFSET:   return "VMS_STRTAB_OFFSET";
1560
    case DT_IA_64_VMS_SYSVER_OFFSET:   return "VMS_SYSVER_OFFSET";
1561
    case DT_IA_64_VMS_IMG_RELA_OFF:    return "VMS_IMG_RELA_OFF";
1562
    case DT_IA_64_VMS_SEG_RELA_OFF:    return "VMS_SEG_RELA_OFF";
1563
    case DT_IA_64_VMS_FIXUP_RELA_OFF:  return "VMS_FIXUP_RELA_OFF";
1564
    case DT_IA_64_VMS_PLTGOT_OFFSET:   return "VMS_PLTGOT_OFFSET";
1565
    case DT_IA_64_VMS_PLTGOT_SEG:      return "VMS_PLTGOT_SEG";
1566
    case DT_IA_64_VMS_FPMODE:          return "VMS_FPMODE";
1567
    default:
1568
      return NULL;
1569
    }
1570
}
1571
 
1572
static const char *
1573
get_alpha_dynamic_type (unsigned long type)
1574
{
1575
  switch (type)
1576
    {
1577
    case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
1578
    default:
1579
      return NULL;
1580
    }
1581
}
1582
 
1583
static const char *
1584
get_score_dynamic_type (unsigned long type)
1585
{
1586
  switch (type)
1587
    {
1588
    case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
1589
    case DT_SCORE_LOCAL_GOTNO:  return "SCORE_LOCAL_GOTNO";
1590
    case DT_SCORE_SYMTABNO:     return "SCORE_SYMTABNO";
1591
    case DT_SCORE_GOTSYM:       return "SCORE_GOTSYM";
1592
    case DT_SCORE_UNREFEXTNO:   return "SCORE_UNREFEXTNO";
1593
    case DT_SCORE_HIPAGENO:     return "SCORE_HIPAGENO";
1594
    default:
1595
      return NULL;
1596
    }
1597
}
1598
 
1599
static const char *
1600
get_tic6x_dynamic_type (unsigned long type)
1601
{
1602
  switch (type)
1603
    {
1604
    case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
1605
    case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
1606
    case DT_C6000_DSBT_BASE:   return "C6000_DSBT_BASE";
1607
    case DT_C6000_DSBT_SIZE:   return "C6000_DSBT_SIZE";
1608
    case DT_C6000_PREEMPTMAP:  return "C6000_PREEMPTMAP";
1609
    case DT_C6000_DSBT_INDEX:  return "C6000_DSBT_INDEX";
1610
    default:
1611
      return NULL;
1612
    }
1613
}
1614
 
1615
static const char *
1616
get_dynamic_type (unsigned long type)
1617
{
1618
  static char buff[64];
1619
 
1620
  switch (type)
1621
    {
1622
    case DT_NULL:       return "NULL";
1623
    case DT_NEEDED:     return "NEEDED";
1624
    case DT_PLTRELSZ:   return "PLTRELSZ";
1625
    case DT_PLTGOT:     return "PLTGOT";
1626
    case DT_HASH:       return "HASH";
1627
    case DT_STRTAB:     return "STRTAB";
1628
    case DT_SYMTAB:     return "SYMTAB";
1629
    case DT_RELA:       return "RELA";
1630
    case DT_RELASZ:     return "RELASZ";
1631
    case DT_RELAENT:    return "RELAENT";
1632
    case DT_STRSZ:      return "STRSZ";
1633
    case DT_SYMENT:     return "SYMENT";
1634
    case DT_INIT:       return "INIT";
1635
    case DT_FINI:       return "FINI";
1636
    case DT_SONAME:     return "SONAME";
1637
    case DT_RPATH:      return "RPATH";
1638
    case DT_SYMBOLIC:   return "SYMBOLIC";
1639
    case DT_REL:        return "REL";
1640
    case DT_RELSZ:      return "RELSZ";
1641
    case DT_RELENT:     return "RELENT";
1642
    case DT_PLTREL:     return "PLTREL";
1643
    case DT_DEBUG:      return "DEBUG";
1644
    case DT_TEXTREL:    return "TEXTREL";
1645
    case DT_JMPREL:     return "JMPREL";
1646
    case DT_BIND_NOW:   return "BIND_NOW";
1647
    case DT_INIT_ARRAY: return "INIT_ARRAY";
1648
    case DT_FINI_ARRAY: return "FINI_ARRAY";
1649
    case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
1650
    case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
1651
    case DT_RUNPATH:    return "RUNPATH";
1652
    case DT_FLAGS:      return "FLAGS";
1653
 
1654
    case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
1655
    case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
1656
 
1657
    case DT_CHECKSUM:   return "CHECKSUM";
1658
    case DT_PLTPADSZ:   return "PLTPADSZ";
1659
    case DT_MOVEENT:    return "MOVEENT";
1660
    case DT_MOVESZ:     return "MOVESZ";
1661
    case DT_FEATURE:    return "FEATURE";
1662
    case DT_POSFLAG_1:  return "POSFLAG_1";
1663
    case DT_SYMINSZ:    return "SYMINSZ";
1664
    case DT_SYMINENT:   return "SYMINENT"; /* aka VALRNGHI */
1665
 
1666
    case DT_ADDRRNGLO:  return "ADDRRNGLO";
1667
    case DT_CONFIG:     return "CONFIG";
1668
    case DT_DEPAUDIT:   return "DEPAUDIT";
1669
    case DT_AUDIT:      return "AUDIT";
1670
    case DT_PLTPAD:     return "PLTPAD";
1671
    case DT_MOVETAB:    return "MOVETAB";
1672
    case DT_SYMINFO:    return "SYMINFO"; /* aka ADDRRNGHI */
1673
 
1674
    case DT_VERSYM:     return "VERSYM";
1675
 
1676
    case DT_TLSDESC_GOT: return "TLSDESC_GOT";
1677
    case DT_TLSDESC_PLT: return "TLSDESC_PLT";
1678
    case DT_RELACOUNT:  return "RELACOUNT";
1679
    case DT_RELCOUNT:   return "RELCOUNT";
1680
    case DT_FLAGS_1:    return "FLAGS_1";
1681
    case DT_VERDEF:     return "VERDEF";
1682
    case DT_VERDEFNUM:  return "VERDEFNUM";
1683
    case DT_VERNEED:    return "VERNEED";
1684
    case DT_VERNEEDNUM: return "VERNEEDNUM";
1685
 
1686
    case DT_AUXILIARY:  return "AUXILIARY";
1687
    case DT_USED:       return "USED";
1688
    case DT_FILTER:     return "FILTER";
1689
 
1690
    case DT_GNU_PRELINKED: return "GNU_PRELINKED";
1691
    case DT_GNU_CONFLICT: return "GNU_CONFLICT";
1692
    case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
1693
    case DT_GNU_LIBLIST: return "GNU_LIBLIST";
1694
    case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
1695
    case DT_GNU_HASH:   return "GNU_HASH";
1696
 
1697
    default:
1698
      if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
1699
        {
1700
          const char * result;
1701
 
1702
          switch (elf_header.e_machine)
1703
            {
1704
            case EM_MIPS:
1705
            case EM_MIPS_RS3_LE:
1706
              result = get_mips_dynamic_type (type);
1707
              break;
1708
            case EM_SPARCV9:
1709
              result = get_sparc64_dynamic_type (type);
1710
              break;
1711
            case EM_PPC:
1712
              result = get_ppc_dynamic_type (type);
1713
              break;
1714
            case EM_PPC64:
1715
              result = get_ppc64_dynamic_type (type);
1716
              break;
1717
            case EM_IA_64:
1718
              result = get_ia64_dynamic_type (type);
1719
              break;
1720
            case EM_ALPHA:
1721
              result = get_alpha_dynamic_type (type);
1722
              break;
1723
            case EM_SCORE:
1724
              result = get_score_dynamic_type (type);
1725
              break;
1726
            case EM_TI_C6000:
1727
              result = get_tic6x_dynamic_type (type);
1728
              break;
1729
            default:
1730
              result = NULL;
1731
              break;
1732
            }
1733
 
1734
          if (result != NULL)
1735
            return result;
1736
 
1737
          snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
1738
        }
1739
      else if (((type >= DT_LOOS) && (type <= DT_HIOS))
1740
               || (elf_header.e_machine == EM_PARISC
1741
                   && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
1742
        {
1743
          const char * result;
1744
 
1745
          switch (elf_header.e_machine)
1746
            {
1747
            case EM_PARISC:
1748
              result = get_parisc_dynamic_type (type);
1749
              break;
1750
            case EM_IA_64:
1751
              result = get_ia64_dynamic_type (type);
1752
              break;
1753
            default:
1754
              result = NULL;
1755
              break;
1756
            }
1757
 
1758
          if (result != NULL)
1759
            return result;
1760
 
1761
          snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
1762
                    type);
1763
        }
1764
      else
1765
        snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
1766
 
1767
      return buff;
1768
    }
1769
}
1770
 
1771
static char *
1772
get_file_type (unsigned e_type)
1773
{
1774
  static char buff[32];
1775
 
1776
  switch (e_type)
1777
    {
1778
    case ET_NONE:       return _("NONE (None)");
1779
    case ET_REL:        return _("REL (Relocatable file)");
1780
    case ET_EXEC:       return _("EXEC (Executable file)");
1781
    case ET_DYN:        return _("DYN (Shared object file)");
1782
    case ET_CORE:       return _("CORE (Core file)");
1783
 
1784
    default:
1785
      if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
1786
        snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
1787
      else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
1788
        snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
1789
      else
1790
        snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
1791
      return buff;
1792
    }
1793
}
1794
 
1795
static char *
1796
get_machine_name (unsigned e_machine)
1797
{
1798
  static char buff[64]; /* XXX */
1799
 
1800
  switch (e_machine)
1801
    {
1802
    case EM_NONE:               return _("None");
1803
    case EM_M32:                return "WE32100";
1804
    case EM_SPARC:              return "Sparc";
1805
    case EM_SPU:                return "SPU";
1806
    case EM_386:                return "Intel 80386";
1807
    case EM_68K:                return "MC68000";
1808
    case EM_88K:                return "MC88000";
1809
    case EM_486:                return "Intel 80486";
1810
    case EM_860:                return "Intel 80860";
1811
    case EM_MIPS:               return "MIPS R3000";
1812
    case EM_S370:               return "IBM System/370";
1813
    case EM_MIPS_RS3_LE:        return "MIPS R4000 big-endian";
1814
    case EM_OLD_SPARCV9:        return "Sparc v9 (old)";
1815
    case EM_PARISC:             return "HPPA";
1816
    case EM_PPC_OLD:            return "Power PC (old)";
1817
    case EM_SPARC32PLUS:        return "Sparc v8+" ;
1818
    case EM_960:                return "Intel 90860";
1819
    case EM_PPC:                return "PowerPC";
1820
    case EM_PPC64:              return "PowerPC64";
1821
    case EM_V800:               return "NEC V800";
1822
    case EM_FR20:               return "Fujitsu FR20";
1823
    case EM_RH32:               return "TRW RH32";
1824
    case EM_MCORE:              return "MCORE";
1825
    case EM_ARM:                return "ARM";
1826
    case EM_OLD_ALPHA:          return "Digital Alpha (old)";
1827
    case EM_SH:                 return "Renesas / SuperH SH";
1828
    case EM_SPARCV9:            return "Sparc v9";
1829
    case EM_TRICORE:            return "Siemens Tricore";
1830
    case EM_ARC:                return "ARC";
1831
    case EM_H8_300:             return "Renesas H8/300";
1832
    case EM_H8_300H:            return "Renesas H8/300H";
1833
    case EM_H8S:                return "Renesas H8S";
1834
    case EM_H8_500:             return "Renesas H8/500";
1835
    case EM_IA_64:              return "Intel IA-64";
1836
    case EM_MIPS_X:             return "Stanford MIPS-X";
1837
    case EM_COLDFIRE:           return "Motorola Coldfire";
1838
    case EM_68HC12:             return "Motorola M68HC12";
1839
    case EM_ALPHA:              return "Alpha";
1840
    case EM_CYGNUS_D10V:
1841
    case EM_D10V:               return "d10v";
1842
    case EM_CYGNUS_D30V:
1843
    case EM_D30V:               return "d30v";
1844
    case EM_CYGNUS_M32R:
1845
    case EM_M32R:               return "Renesas M32R (formerly Mitsubishi M32r)";
1846
    case EM_CYGNUS_V850:
1847
    case EM_V850:               return "Renesas v850";
1848
    case EM_CYGNUS_MN10300:
1849
    case EM_MN10300:            return "mn10300";
1850
    case EM_CYGNUS_MN10200:
1851
    case EM_MN10200:            return "mn10200";
1852
    case EM_MOXIE:              return "Moxie";
1853
    case EM_CYGNUS_FR30:
1854
    case EM_FR30:               return "Fujitsu FR30";
1855
    case EM_CYGNUS_FRV:         return "Fujitsu FR-V";
1856
    case EM_PJ_OLD:
1857
    case EM_PJ:                 return "picoJava";
1858
    case EM_MMA:                return "Fujitsu Multimedia Accelerator";
1859
    case EM_PCP:                return "Siemens PCP";
1860
    case EM_NCPU:               return "Sony nCPU embedded RISC processor";
1861
    case EM_NDR1:               return "Denso NDR1 microprocesspr";
1862
    case EM_STARCORE:           return "Motorola Star*Core processor";
1863
    case EM_ME16:               return "Toyota ME16 processor";
1864
    case EM_ST100:              return "STMicroelectronics ST100 processor";
1865
    case EM_TINYJ:              return "Advanced Logic Corp. TinyJ embedded processor";
1866
    case EM_OPEN8:              return "Open8/V8/ARClite 8-bit MCUs";
1867
    case EM_PDSP:               return "Sony DSP processor";
1868
    case EM_PDP10:              return "Digital Equipment Corp. PDP-10";
1869
    case EM_PDP11:              return "Digital Equipment Corp. PDP-11";
1870
    case EM_FX66:               return "Siemens FX66 microcontroller";
1871
    case EM_ST9PLUS:            return "STMicroelectronics ST9+ 8/16 bit microcontroller";
1872
    case EM_ST7:                return "STMicroelectronics ST7 8-bit microcontroller";
1873
    case EM_68HC16:             return "Motorola MC68HC16 Microcontroller";
1874
    case EM_68HC11:             return "Motorola MC68HC11 Microcontroller";
1875
    case EM_68HC08:             return "Motorola MC68HC08 Microcontroller";
1876
    case EM_68HC05:             return "Motorola MC68HC05 Microcontroller";
1877
    case EM_SVX:                return "Silicon Graphics SVx";
1878
    case EM_ST19:               return "STMicroelectronics ST19 8-bit microcontroller";
1879
    case EM_VAX:                return "Digital VAX";
1880
    case EM_AVR_OLD:
1881
    case EM_AVR:                return "Atmel AVR 8-bit microcontroller";
1882
    case EM_CRIS:               return "Axis Communications 32-bit embedded processor";
1883
    case EM_JAVELIN:            return "Infineon Technologies 32-bit embedded cpu";
1884
    case EM_FIREPATH:           return "Element 14 64-bit DSP processor";
1885
    case EM_ZSP:                return "LSI Logic's 16-bit DSP processor";
1886
    case EM_MMIX:               return "Donald Knuth's educational 64-bit processor";
1887
    case EM_HUANY:              return "Harvard Universitys's machine-independent object format";
1888
    case EM_PRISM:              return "Vitesse Prism";
1889
    case EM_X86_64:             return "Advanced Micro Devices X86-64";
1890
    case EM_L1OM:               return "Intel L1OM";
1891
    case EM_S390_OLD:
1892
    case EM_S390:               return "IBM S/390";
1893
    case EM_SCORE:              return "SUNPLUS S+Core";
1894
    case EM_XSTORMY16:          return "Sanyo XStormy16 CPU core";
1895
    case EM_OPENRISC:
1896
    case EM_OR32:               return "OpenRISC";
1897
    case EM_ARC_A5:             return "ARC International ARCompact processor";
1898
    case EM_CRX:                return "National Semiconductor CRX microprocessor";
1899
    case EM_DLX:                return "OpenDLX";
1900
    case EM_IP2K_OLD:
1901
    case EM_IP2K:               return "Ubicom IP2xxx 8-bit microcontrollers";
1902
    case EM_IQ2000:             return "Vitesse IQ2000";
1903
    case EM_XTENSA_OLD:
1904
    case EM_XTENSA:             return "Tensilica Xtensa Processor";
1905
    case EM_VIDEOCORE:          return "Alphamosaic VideoCore processor";
1906
    case EM_TMM_GPP:            return "Thompson Multimedia General Purpose Processor";
1907
    case EM_NS32K:              return "National Semiconductor 32000 series";
1908
    case EM_TPC:                return "Tenor Network TPC processor";
1909
    case EM_ST200:              return "STMicroelectronics ST200 microcontroller";
1910
    case EM_MAX:                return "MAX Processor";
1911
    case EM_CR:                 return "National Semiconductor CompactRISC";
1912
    case EM_F2MC16:             return "Fujitsu F2MC16";
1913
    case EM_MSP430:             return "Texas Instruments msp430 microcontroller";
1914
    case EM_LATTICEMICO32:      return "Lattice Mico32";
1915
    case EM_M32C_OLD:
1916
    case EM_M32C:               return "Renesas M32c";
1917
    case EM_MT:                 return "Morpho Techologies MT processor";
1918
    case EM_BLACKFIN:           return "Analog Devices Blackfin";
1919
    case EM_SE_C33:             return "S1C33 Family of Seiko Epson processors";
1920
    case EM_SEP:                return "Sharp embedded microprocessor";
1921
    case EM_ARCA:               return "Arca RISC microprocessor";
1922
    case EM_UNICORE:            return "Unicore";
1923
    case EM_EXCESS:             return "eXcess 16/32/64-bit configurable embedded CPU";
1924
    case EM_DXP:                return "Icera Semiconductor Inc. Deep Execution Processor";
1925
    case EM_NIOS32:             return "Altera Nios";
1926
    case EM_ALTERA_NIOS2:       return "Altera Nios II";
1927
    case EM_C166:
1928
    case EM_XC16X:              return "Infineon Technologies xc16x";
1929
    case EM_M16C:               return "Renesas M16C series microprocessors";
1930
    case EM_DSPIC30F:           return "Microchip Technology dsPIC30F Digital Signal Controller";
1931
    case EM_CE:                 return "Freescale Communication Engine RISC core";
1932
    case EM_TSK3000:            return "Altium TSK3000 core";
1933
    case EM_RS08:               return "Freescale RS08 embedded processor";
1934
    case EM_ECOG2:              return "Cyan Technology eCOG2 microprocessor";
1935
    case EM_DSP24:              return "New Japan Radio (NJR) 24-bit DSP Processor";
1936
    case EM_VIDEOCORE3:         return "Broadcom VideoCore III processor";
1937
    case EM_SE_C17:             return "Seiko Epson C17 family";
1938
    case EM_TI_C6000:           return "Texas Instruments TMS320C6000 DSP family";
1939
    case EM_TI_C2000:           return "Texas Instruments TMS320C2000 DSP family";
1940
    case EM_TI_C5500:           return "Texas Instruments TMS320C55x DSP family";
1941
    case EM_MMDSP_PLUS:         return "STMicroelectronics 64bit VLIW Data Signal Processor";
1942
    case EM_CYPRESS_M8C:        return "Cypress M8C microprocessor";
1943
    case EM_R32C:               return "Renesas R32C series microprocessors";
1944
    case EM_TRIMEDIA:           return "NXP Semiconductors TriMedia architecture family";
1945
    case EM_QDSP6:              return "QUALCOMM DSP6 Processor";
1946
    case EM_8051:               return "Intel 8051 and variants";
1947
    case EM_STXP7X:             return "STMicroelectronics STxP7x family";
1948
    case EM_NDS32:              return "Andes Technology compact code size embedded RISC processor family";
1949
    case EM_ECOG1X:             return "Cyan Technology eCOG1X family";
1950
    case EM_MAXQ30:             return "Dallas Semiconductor MAXQ30 Core microcontrollers";
1951
    case EM_XIMO16:             return "New Japan Radio (NJR) 16-bit DSP Processor";
1952
    case EM_MANIK:              return "M2000 Reconfigurable RISC Microprocessor";
1953
    case EM_CRAYNV2:            return "Cray Inc. NV2 vector architecture";
1954
    case EM_CYGNUS_MEP:         return "Toshiba MeP Media Engine";
1955
    case EM_CR16:
1956
    case EM_CR16_OLD:           return "National Semiconductor's CR16";
1957
    case EM_MICROBLAZE:         return "Xilinx MicroBlaze";
1958
    case EM_MICROBLAZE_OLD:     return "Xilinx MicroBlaze";
1959
    case EM_RX:                 return "Renesas RX";
1960
    case EM_METAG:              return "Imagination Technologies META processor architecture";
1961
    case EM_MCST_ELBRUS:        return "MCST Elbrus general purpose hardware architecture";
1962
    case EM_ECOG16:             return "Cyan Technology eCOG16 family";
1963
    case EM_ETPU:               return "Freescale Extended Time Processing Unit";
1964
    case EM_SLE9X:              return "Infineon Technologies SLE9X core";
1965
    case EM_AVR32:              return "Atmel Corporation 32-bit microprocessor family";
1966
    case EM_STM8:               return "STMicroeletronics STM8 8-bit microcontroller";
1967
    case EM_TILE64:             return "Tilera TILE64 multicore architecture family";
1968
    case EM_TILEPRO:            return "Tilera TILEPro multicore architecture family";
1969
    case EM_CUDA:               return "NVIDIA CUDA architecture";
1970
    default:
1971
      snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
1972
      return buff;
1973
    }
1974
}
1975
 
1976
static void
1977
decode_ARM_machine_flags (unsigned e_flags, char buf[])
1978
{
1979
  unsigned eabi;
1980
  int unknown = 0;
1981
 
1982
  eabi = EF_ARM_EABI_VERSION (e_flags);
1983
  e_flags &= ~ EF_ARM_EABIMASK;
1984
 
1985
  /* Handle "generic" ARM flags.  */
1986
  if (e_flags & EF_ARM_RELEXEC)
1987
    {
1988
      strcat (buf, ", relocatable executable");
1989
      e_flags &= ~ EF_ARM_RELEXEC;
1990
    }
1991
 
1992
  if (e_flags & EF_ARM_HASENTRY)
1993
    {
1994
      strcat (buf, ", has entry point");
1995
      e_flags &= ~ EF_ARM_HASENTRY;
1996
    }
1997
 
1998
  /* Now handle EABI specific flags.  */
1999
  switch (eabi)
2000
    {
2001
    default:
2002
      strcat (buf, ", <unrecognized EABI>");
2003
      if (e_flags)
2004
        unknown = 1;
2005
      break;
2006
 
2007
    case EF_ARM_EABI_VER1:
2008
      strcat (buf, ", Version1 EABI");
2009
      while (e_flags)
2010
        {
2011
          unsigned flag;
2012
 
2013
          /* Process flags one bit at a time.  */
2014
          flag = e_flags & - e_flags;
2015
          e_flags &= ~ flag;
2016
 
2017
          switch (flag)
2018
            {
2019
            case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
2020
              strcat (buf, ", sorted symbol tables");
2021
              break;
2022
 
2023
            default:
2024
              unknown = 1;
2025
              break;
2026
            }
2027
        }
2028
      break;
2029
 
2030
    case EF_ARM_EABI_VER2:
2031
      strcat (buf, ", Version2 EABI");
2032
      while (e_flags)
2033
        {
2034
          unsigned flag;
2035
 
2036
          /* Process flags one bit at a time.  */
2037
          flag = e_flags & - e_flags;
2038
          e_flags &= ~ flag;
2039
 
2040
          switch (flag)
2041
            {
2042
            case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK.  */
2043
              strcat (buf, ", sorted symbol tables");
2044
              break;
2045
 
2046
            case EF_ARM_DYNSYMSUSESEGIDX:
2047
              strcat (buf, ", dynamic symbols use segment index");
2048
              break;
2049
 
2050
            case EF_ARM_MAPSYMSFIRST:
2051
              strcat (buf, ", mapping symbols precede others");
2052
              break;
2053
 
2054
            default:
2055
              unknown = 1;
2056
              break;
2057
            }
2058
        }
2059
      break;
2060
 
2061
    case EF_ARM_EABI_VER3:
2062
      strcat (buf, ", Version3 EABI");
2063
      break;
2064
 
2065
    case EF_ARM_EABI_VER4:
2066
      strcat (buf, ", Version4 EABI");
2067
      goto eabi;
2068
 
2069
    case EF_ARM_EABI_VER5:
2070
      strcat (buf, ", Version5 EABI");
2071
    eabi:
2072
      while (e_flags)
2073
        {
2074
          unsigned flag;
2075
 
2076
          /* Process flags one bit at a time.  */
2077
          flag = e_flags & - e_flags;
2078
          e_flags &= ~ flag;
2079
 
2080
          switch (flag)
2081
            {
2082
            case EF_ARM_BE8:
2083
              strcat (buf, ", BE8");
2084
              break;
2085
 
2086
            case EF_ARM_LE8:
2087
              strcat (buf, ", LE8");
2088
              break;
2089
 
2090
            default:
2091
              unknown = 1;
2092
              break;
2093
            }
2094
        }
2095
      break;
2096
 
2097
    case EF_ARM_EABI_UNKNOWN:
2098
      strcat (buf, ", GNU EABI");
2099
      while (e_flags)
2100
        {
2101
          unsigned flag;
2102
 
2103
          /* Process flags one bit at a time.  */
2104
          flag = e_flags & - e_flags;
2105
          e_flags &= ~ flag;
2106
 
2107
          switch (flag)
2108
            {
2109
            case EF_ARM_INTERWORK:
2110
              strcat (buf, ", interworking enabled");
2111
              break;
2112
 
2113
            case EF_ARM_APCS_26:
2114
              strcat (buf, ", uses APCS/26");
2115
              break;
2116
 
2117
            case EF_ARM_APCS_FLOAT:
2118
              strcat (buf, ", uses APCS/float");
2119
              break;
2120
 
2121
            case EF_ARM_PIC:
2122
              strcat (buf, ", position independent");
2123
              break;
2124
 
2125
            case EF_ARM_ALIGN8:
2126
              strcat (buf, ", 8 bit structure alignment");
2127
              break;
2128
 
2129
            case EF_ARM_NEW_ABI:
2130
              strcat (buf, ", uses new ABI");
2131
              break;
2132
 
2133
            case EF_ARM_OLD_ABI:
2134
              strcat (buf, ", uses old ABI");
2135
              break;
2136
 
2137
            case EF_ARM_SOFT_FLOAT:
2138
              strcat (buf, ", software FP");
2139
              break;
2140
 
2141
            case EF_ARM_VFP_FLOAT:
2142
              strcat (buf, ", VFP");
2143
              break;
2144
 
2145
            case EF_ARM_MAVERICK_FLOAT:
2146
              strcat (buf, ", Maverick FP");
2147
              break;
2148
 
2149
            default:
2150
              unknown = 1;
2151
              break;
2152
            }
2153
        }
2154
    }
2155
 
2156
  if (unknown)
2157
    strcat (buf,_(", <unknown>"));
2158
}
2159
 
2160
static char *
2161
get_machine_flags (unsigned e_flags, unsigned e_machine)
2162
{
2163
  static char buf[1024];
2164
 
2165
  buf[0] = '\0';
2166
 
2167
  if (e_flags)
2168
    {
2169
      switch (e_machine)
2170
        {
2171
        default:
2172
          break;
2173
 
2174
        case EM_ARM:
2175
          decode_ARM_machine_flags (e_flags, buf);
2176
          break;
2177
 
2178
        case EM_BLACKFIN:
2179
          if (e_flags & EF_BFIN_PIC)
2180
            strcat (buf, ", PIC");
2181
 
2182
          if (e_flags & EF_BFIN_FDPIC)
2183
            strcat (buf, ", FDPIC");
2184
 
2185
          if (e_flags & EF_BFIN_CODE_IN_L1)
2186
            strcat (buf, ", code in L1");
2187
 
2188
          if (e_flags & EF_BFIN_DATA_IN_L1)
2189
            strcat (buf, ", data in L1");
2190
 
2191
          break;
2192
 
2193
        case EM_CYGNUS_FRV:
2194
          switch (e_flags & EF_FRV_CPU_MASK)
2195
            {
2196
            case EF_FRV_CPU_GENERIC:
2197
              break;
2198
 
2199
            default:
2200
              strcat (buf, ", fr???");
2201
              break;
2202
 
2203
            case EF_FRV_CPU_FR300:
2204
              strcat (buf, ", fr300");
2205
              break;
2206
 
2207
            case EF_FRV_CPU_FR400:
2208
              strcat (buf, ", fr400");
2209
              break;
2210
            case EF_FRV_CPU_FR405:
2211
              strcat (buf, ", fr405");
2212
              break;
2213
 
2214
            case EF_FRV_CPU_FR450:
2215
              strcat (buf, ", fr450");
2216
              break;
2217
 
2218
            case EF_FRV_CPU_FR500:
2219
              strcat (buf, ", fr500");
2220
              break;
2221
            case EF_FRV_CPU_FR550:
2222
              strcat (buf, ", fr550");
2223
              break;
2224
 
2225
            case EF_FRV_CPU_SIMPLE:
2226
              strcat (buf, ", simple");
2227
              break;
2228
            case EF_FRV_CPU_TOMCAT:
2229
              strcat (buf, ", tomcat");
2230
              break;
2231
            }
2232
          break;
2233
 
2234
        case EM_68K:
2235
          if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
2236
            strcat (buf, ", m68000");
2237
          else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
2238
            strcat (buf, ", cpu32");
2239
          else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
2240
            strcat (buf, ", fido_a");
2241
          else
2242
            {
2243
              char const * isa = _("unknown");
2244
              char const * mac = _("unknown mac");
2245
              char const * additional = NULL;
2246
 
2247
              switch (e_flags & EF_M68K_CF_ISA_MASK)
2248
                {
2249
                case EF_M68K_CF_ISA_A_NODIV:
2250
                  isa = "A";
2251
                  additional = ", nodiv";
2252
                  break;
2253
                case EF_M68K_CF_ISA_A:
2254
                  isa = "A";
2255
                  break;
2256
                case EF_M68K_CF_ISA_A_PLUS:
2257
                  isa = "A+";
2258
                  break;
2259
                case EF_M68K_CF_ISA_B_NOUSP:
2260
                  isa = "B";
2261
                  additional = ", nousp";
2262
                  break;
2263
                case EF_M68K_CF_ISA_B:
2264
                  isa = "B";
2265
                  break;
2266
                case EF_M68K_CF_ISA_C:
2267
                  isa = "C";
2268
                  break;
2269
                case EF_M68K_CF_ISA_C_NODIV:
2270
                  isa = "C";
2271
                  additional = ", nodiv";
2272
                  break;
2273
                }
2274
              strcat (buf, ", cf, isa ");
2275
              strcat (buf, isa);
2276
              if (additional)
2277
                strcat (buf, additional);
2278
              if (e_flags & EF_M68K_CF_FLOAT)
2279
                strcat (buf, ", float");
2280
              switch (e_flags & EF_M68K_CF_MAC_MASK)
2281
                {
2282
                case 0:
2283
                  mac = NULL;
2284
                  break;
2285
                case EF_M68K_CF_MAC:
2286
                  mac = "mac";
2287
                  break;
2288
                case EF_M68K_CF_EMAC:
2289
                  mac = "emac";
2290
                  break;
2291
                case EF_M68K_CF_EMAC_B:
2292
                  mac = "emac_b";
2293
                  break;
2294
                }
2295
              if (mac)
2296
                {
2297
                  strcat (buf, ", ");
2298
                  strcat (buf, mac);
2299
                }
2300
            }
2301
          break;
2302
 
2303
        case EM_PPC:
2304
          if (e_flags & EF_PPC_EMB)
2305
            strcat (buf, ", emb");
2306
 
2307
          if (e_flags & EF_PPC_RELOCATABLE)
2308
            strcat (buf, _(", relocatable"));
2309
 
2310
          if (e_flags & EF_PPC_RELOCATABLE_LIB)
2311
            strcat (buf, _(", relocatable-lib"));
2312
          break;
2313
 
2314
        case EM_V850:
2315
        case EM_CYGNUS_V850:
2316
          switch (e_flags & EF_V850_ARCH)
2317
            {
2318
            case E_V850E2V3_ARCH:
2319
              strcat (buf, ", v850e2v3");
2320
              break;
2321
            case E_V850E2_ARCH:
2322
              strcat (buf, ", v850e2");
2323
              break;
2324
            case E_V850E1_ARCH:
2325
              strcat (buf, ", v850e1");
2326
              break;
2327
            case E_V850E_ARCH:
2328
              strcat (buf, ", v850e");
2329
              break;
2330
            case E_V850_ARCH:
2331
              strcat (buf, ", v850");
2332
              break;
2333
            default:
2334
              strcat (buf, _(", unknown v850 architecture variant"));
2335
              break;
2336
            }
2337
          break;
2338
 
2339
        case EM_M32R:
2340
        case EM_CYGNUS_M32R:
2341
          if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
2342
            strcat (buf, ", m32r");
2343
          break;
2344
 
2345
        case EM_MIPS:
2346
        case EM_MIPS_RS3_LE:
2347
          if (e_flags & EF_MIPS_NOREORDER)
2348
            strcat (buf, ", noreorder");
2349
 
2350
          if (e_flags & EF_MIPS_PIC)
2351
            strcat (buf, ", pic");
2352
 
2353
          if (e_flags & EF_MIPS_CPIC)
2354
            strcat (buf, ", cpic");
2355
 
2356
          if (e_flags & EF_MIPS_UCODE)
2357
            strcat (buf, ", ugen_reserved");
2358
 
2359
          if (e_flags & EF_MIPS_ABI2)
2360
            strcat (buf, ", abi2");
2361
 
2362
          if (e_flags & EF_MIPS_OPTIONS_FIRST)
2363
            strcat (buf, ", odk first");
2364
 
2365
          if (e_flags & EF_MIPS_32BITMODE)
2366
            strcat (buf, ", 32bitmode");
2367
 
2368
          switch ((e_flags & EF_MIPS_MACH))
2369
            {
2370
            case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
2371
            case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
2372
            case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
2373
            case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
2374
            case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
2375
            case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
2376
            case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
2377
            case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
2378
            case E_MIPS_MACH_SB1:  strcat (buf, ", sb1");  break;
2379
            case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
2380
            case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
2381
            case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
2382
            case E_MIPS_MACH_LS3A: strcat (buf, ", loongson-3a"); break;
2383
            case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
2384
            case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
2385
            case E_MIPS_MACH_XLR:  strcat (buf, ", xlr"); break;
2386
            case 0:
2387
            /* We simply ignore the field in this case to avoid confusion:
2388
               MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
2389
               extension.  */
2390
              break;
2391
            default: strcat (buf, _(", unknown CPU")); break;
2392
            }
2393
 
2394
          switch ((e_flags & EF_MIPS_ABI))
2395
            {
2396
            case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
2397
            case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
2398
            case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
2399
            case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
2400
            case 0:
2401
            /* We simply ignore the field in this case to avoid confusion:
2402
               MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
2403
               This means it is likely to be an o32 file, but not for
2404
               sure.  */
2405
              break;
2406
            default: strcat (buf, _(", unknown ABI")); break;
2407
            }
2408
 
2409
          if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
2410
            strcat (buf, ", mdmx");
2411
 
2412
          if (e_flags & EF_MIPS_ARCH_ASE_M16)
2413
            strcat (buf, ", mips16");
2414
 
2415
          switch ((e_flags & EF_MIPS_ARCH))
2416
            {
2417
            case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
2418
            case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
2419
            case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
2420
            case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
2421
            case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
2422
            case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
2423
            case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
2424
            case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
2425
            case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
2426
            default: strcat (buf, _(", unknown ISA")); break;
2427
            }
2428
 
2429
          if (e_flags & EF_SH_PIC)
2430
            strcat (buf, ", pic");
2431
 
2432
          if (e_flags & EF_SH_FDPIC)
2433
            strcat (buf, ", fdpic");
2434
          break;
2435
 
2436
        case EM_SH:
2437
          switch ((e_flags & EF_SH_MACH_MASK))
2438
            {
2439
            case EF_SH1: strcat (buf, ", sh1"); break;
2440
            case EF_SH2: strcat (buf, ", sh2"); break;
2441
            case EF_SH3: strcat (buf, ", sh3"); break;
2442
            case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
2443
            case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
2444
            case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
2445
            case EF_SH3E: strcat (buf, ", sh3e"); break;
2446
            case EF_SH4: strcat (buf, ", sh4"); break;
2447
            case EF_SH5: strcat (buf, ", sh5"); break;
2448
            case EF_SH2E: strcat (buf, ", sh2e"); break;
2449
            case EF_SH4A: strcat (buf, ", sh4a"); break;
2450
            case EF_SH2A: strcat (buf, ", sh2a"); break;
2451
            case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
2452
            case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
2453
            case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
2454
            case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
2455
            case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
2456
            case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
2457
            case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
2458
            case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
2459
            case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
2460
            default: strcat (buf, _(", unknown ISA")); break;
2461
            }
2462
 
2463
          break;
2464
 
2465
        case EM_SPARCV9:
2466
          if (e_flags & EF_SPARC_32PLUS)
2467
            strcat (buf, ", v8+");
2468
 
2469
          if (e_flags & EF_SPARC_SUN_US1)
2470
            strcat (buf, ", ultrasparcI");
2471
 
2472
          if (e_flags & EF_SPARC_SUN_US3)
2473
            strcat (buf, ", ultrasparcIII");
2474
 
2475
          if (e_flags & EF_SPARC_HAL_R1)
2476
            strcat (buf, ", halr1");
2477
 
2478
          if (e_flags & EF_SPARC_LEDATA)
2479
            strcat (buf, ", ledata");
2480
 
2481
          if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
2482
            strcat (buf, ", tso");
2483
 
2484
          if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
2485
            strcat (buf, ", pso");
2486
 
2487
          if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
2488
            strcat (buf, ", rmo");
2489
          break;
2490
 
2491
        case EM_PARISC:
2492
          switch (e_flags & EF_PARISC_ARCH)
2493
            {
2494
            case EFA_PARISC_1_0:
2495
              strcpy (buf, ", PA-RISC 1.0");
2496
              break;
2497
            case EFA_PARISC_1_1:
2498
              strcpy (buf, ", PA-RISC 1.1");
2499
              break;
2500
            case EFA_PARISC_2_0:
2501
              strcpy (buf, ", PA-RISC 2.0");
2502
              break;
2503
            default:
2504
              break;
2505
            }
2506
          if (e_flags & EF_PARISC_TRAPNIL)
2507
            strcat (buf, ", trapnil");
2508
          if (e_flags & EF_PARISC_EXT)
2509
            strcat (buf, ", ext");
2510
          if (e_flags & EF_PARISC_LSB)
2511
            strcat (buf, ", lsb");
2512
          if (e_flags & EF_PARISC_WIDE)
2513
            strcat (buf, ", wide");
2514
          if (e_flags & EF_PARISC_NO_KABP)
2515
            strcat (buf, ", no kabp");
2516
          if (e_flags & EF_PARISC_LAZYSWAP)
2517
            strcat (buf, ", lazyswap");
2518
          break;
2519
 
2520
        case EM_PJ:
2521
        case EM_PJ_OLD:
2522
          if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
2523
            strcat (buf, ", new calling convention");
2524
 
2525
          if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
2526
            strcat (buf, ", gnu calling convention");
2527
          break;
2528
 
2529
        case EM_IA_64:
2530
          if ((e_flags & EF_IA_64_ABI64))
2531
            strcat (buf, ", 64-bit");
2532
          else
2533
            strcat (buf, ", 32-bit");
2534
          if ((e_flags & EF_IA_64_REDUCEDFP))
2535
            strcat (buf, ", reduced fp model");
2536
          if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
2537
            strcat (buf, ", no function descriptors, constant gp");
2538
          else if ((e_flags & EF_IA_64_CONS_GP))
2539
            strcat (buf, ", constant gp");
2540
          if ((e_flags & EF_IA_64_ABSOLUTE))
2541
            strcat (buf, ", absolute");
2542
          if (elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
2543
            {
2544
              if ((e_flags & EF_IA_64_VMS_LINKAGES))
2545
                strcat (buf, ", vms_linkages");
2546
              switch ((e_flags & EF_IA_64_VMS_COMCOD))
2547
                {
2548
                case EF_IA_64_VMS_COMCOD_SUCCESS:
2549
                  break;
2550
                case EF_IA_64_VMS_COMCOD_WARNING:
2551
                  strcat (buf, ", warning");
2552
                  break;
2553
                case EF_IA_64_VMS_COMCOD_ERROR:
2554
                  strcat (buf, ", error");
2555
                  break;
2556
                case EF_IA_64_VMS_COMCOD_ABORT:
2557
                  strcat (buf, ", abort");
2558
                  break;
2559
                default:
2560
                  abort ();
2561
                }
2562
            }
2563
          break;
2564
 
2565
        case EM_VAX:
2566
          if ((e_flags & EF_VAX_NONPIC))
2567
            strcat (buf, ", non-PIC");
2568
          if ((e_flags & EF_VAX_DFLOAT))
2569
            strcat (buf, ", D-Float");
2570
          if ((e_flags & EF_VAX_GFLOAT))
2571
            strcat (buf, ", G-Float");
2572
          break;
2573
 
2574
        case EM_RX:
2575
          if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
2576
            strcat (buf, ", 64-bit doubles");
2577
          if (e_flags & E_FLAG_RX_DSP)
2578
            strcat (buf, ", dsp");
2579
 
2580
        case EM_S390:
2581
          if (e_flags & EF_S390_HIGH_GPRS)
2582
            strcat (buf, ", highgprs");
2583
 
2584
        case EM_TI_C6000:
2585
          if ((e_flags & EF_C6000_REL))
2586
            strcat (buf, ", relocatable module");
2587
        }
2588
    }
2589
 
2590
  return buf;
2591
}
2592
 
2593
static const char *
2594
get_osabi_name (unsigned int osabi)
2595
{
2596
  static char buff[32];
2597
 
2598
  switch (osabi)
2599
    {
2600
    case ELFOSABI_NONE:         return "UNIX - System V";
2601
    case ELFOSABI_HPUX:         return "UNIX - HP-UX";
2602
    case ELFOSABI_NETBSD:       return "UNIX - NetBSD";
2603
    case ELFOSABI_LINUX:        return "UNIX - Linux";
2604
    case ELFOSABI_HURD:         return "GNU/Hurd";
2605
    case ELFOSABI_SOLARIS:      return "UNIX - Solaris";
2606
    case ELFOSABI_AIX:          return "UNIX - AIX";
2607
    case ELFOSABI_IRIX:         return "UNIX - IRIX";
2608
    case ELFOSABI_FREEBSD:      return "UNIX - FreeBSD";
2609
    case ELFOSABI_TRU64:        return "UNIX - TRU64";
2610
    case ELFOSABI_MODESTO:      return "Novell - Modesto";
2611
    case ELFOSABI_OPENBSD:      return "UNIX - OpenBSD";
2612
    case ELFOSABI_OPENVMS:      return "VMS - OpenVMS";
2613
    case ELFOSABI_NSK:          return "HP - Non-Stop Kernel";
2614
    case ELFOSABI_AROS:         return "AROS";
2615
    case ELFOSABI_FENIXOS:      return "FenixOS";
2616
    default:
2617
      if (osabi >= 64)
2618
        switch (elf_header.e_machine)
2619
          {
2620
          case EM_ARM:
2621
            switch (osabi)
2622
              {
2623
              case ELFOSABI_ARM:        return "ARM";
2624
              default:
2625
                break;
2626
              }
2627
            break;
2628
 
2629
          case EM_MSP430:
2630
          case EM_MSP430_OLD:
2631
            switch (osabi)
2632
              {
2633
              case ELFOSABI_STANDALONE: return _("Standalone App");
2634
              default:
2635
                break;
2636
              }
2637
            break;
2638
 
2639
          case EM_TI_C6000:
2640
            switch (osabi)
2641
              {
2642
              case ELFOSABI_C6000_ELFABI:       return _("Bare-metal C6000");
2643
              case ELFOSABI_C6000_LINUX:        return "Linux C6000";
2644
              default:
2645
                break;
2646
              }
2647
            break;
2648
 
2649
          default:
2650
            break;
2651
          }
2652
      snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
2653
      return buff;
2654
    }
2655
}
2656
 
2657
static const char *
2658
get_arm_segment_type (unsigned long type)
2659
{
2660
  switch (type)
2661
    {
2662
    case PT_ARM_EXIDX:
2663
      return "EXIDX";
2664
    default:
2665
      break;
2666
    }
2667
 
2668
  return NULL;
2669
}
2670
 
2671
static const char *
2672
get_mips_segment_type (unsigned long type)
2673
{
2674
  switch (type)
2675
    {
2676
    case PT_MIPS_REGINFO:
2677
      return "REGINFO";
2678
    case PT_MIPS_RTPROC:
2679
      return "RTPROC";
2680
    case PT_MIPS_OPTIONS:
2681
      return "OPTIONS";
2682
    default:
2683
      break;
2684
    }
2685
 
2686
  return NULL;
2687
}
2688
 
2689
static const char *
2690
get_parisc_segment_type (unsigned long type)
2691
{
2692
  switch (type)
2693
    {
2694
    case PT_HP_TLS:             return "HP_TLS";
2695
    case PT_HP_CORE_NONE:       return "HP_CORE_NONE";
2696
    case PT_HP_CORE_VERSION:    return "HP_CORE_VERSION";
2697
    case PT_HP_CORE_KERNEL:     return "HP_CORE_KERNEL";
2698
    case PT_HP_CORE_COMM:       return "HP_CORE_COMM";
2699
    case PT_HP_CORE_PROC:       return "HP_CORE_PROC";
2700
    case PT_HP_CORE_LOADABLE:   return "HP_CORE_LOADABLE";
2701
    case PT_HP_CORE_STACK:      return "HP_CORE_STACK";
2702
    case PT_HP_CORE_SHM:        return "HP_CORE_SHM";
2703
    case PT_HP_CORE_MMF:        return "HP_CORE_MMF";
2704
    case PT_HP_PARALLEL:        return "HP_PARALLEL";
2705
    case PT_HP_FASTBIND:        return "HP_FASTBIND";
2706
    case PT_HP_OPT_ANNOT:       return "HP_OPT_ANNOT";
2707
    case PT_HP_HSL_ANNOT:       return "HP_HSL_ANNOT";
2708
    case PT_HP_STACK:           return "HP_STACK";
2709
    case PT_HP_CORE_UTSNAME:    return "HP_CORE_UTSNAME";
2710
    case PT_PARISC_ARCHEXT:     return "PARISC_ARCHEXT";
2711
    case PT_PARISC_UNWIND:      return "PARISC_UNWIND";
2712
    case PT_PARISC_WEAKORDER:   return "PARISC_WEAKORDER";
2713
    default:
2714
      break;
2715
    }
2716
 
2717
  return NULL;
2718
}
2719
 
2720
static const char *
2721
get_ia64_segment_type (unsigned long type)
2722
{
2723
  switch (type)
2724
    {
2725
    case PT_IA_64_ARCHEXT:      return "IA_64_ARCHEXT";
2726
    case PT_IA_64_UNWIND:       return "IA_64_UNWIND";
2727
    case PT_HP_TLS:             return "HP_TLS";
2728
    case PT_IA_64_HP_OPT_ANOT:  return "HP_OPT_ANNOT";
2729
    case PT_IA_64_HP_HSL_ANOT:  return "HP_HSL_ANNOT";
2730
    case PT_IA_64_HP_STACK:     return "HP_STACK";
2731
    default:
2732
      break;
2733
    }
2734
 
2735
  return NULL;
2736
}
2737
 
2738
static const char *
2739
get_tic6x_segment_type (unsigned long type)
2740
{
2741
  switch (type)
2742
    {
2743
    case PT_C6000_PHATTR:       return "C6000_PHATTR";
2744
    default:
2745
      break;
2746
    }
2747
 
2748
  return NULL;
2749
}
2750
 
2751
static const char *
2752
get_segment_type (unsigned long p_type)
2753
{
2754
  static char buff[32];
2755
 
2756
  switch (p_type)
2757
    {
2758
    case PT_NULL:       return "NULL";
2759
    case PT_LOAD:       return "LOAD";
2760
    case PT_DYNAMIC:    return "DYNAMIC";
2761
    case PT_INTERP:     return "INTERP";
2762
    case PT_NOTE:       return "NOTE";
2763
    case PT_SHLIB:      return "SHLIB";
2764
    case PT_PHDR:       return "PHDR";
2765
    case PT_TLS:        return "TLS";
2766
 
2767
    case PT_GNU_EH_FRAME:
2768
                        return "GNU_EH_FRAME";
2769
    case PT_GNU_STACK:  return "GNU_STACK";
2770
    case PT_GNU_RELRO:  return "GNU_RELRO";
2771
 
2772
    default:
2773
      if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
2774
        {
2775
          const char * result;
2776
 
2777
          switch (elf_header.e_machine)
2778
            {
2779
            case EM_ARM:
2780
              result = get_arm_segment_type (p_type);
2781
              break;
2782
            case EM_MIPS:
2783
            case EM_MIPS_RS3_LE:
2784
              result = get_mips_segment_type (p_type);
2785
              break;
2786
            case EM_PARISC:
2787
              result = get_parisc_segment_type (p_type);
2788
              break;
2789
            case EM_IA_64:
2790
              result = get_ia64_segment_type (p_type);
2791
              break;
2792
            case EM_TI_C6000:
2793
              result = get_tic6x_segment_type (p_type);
2794
              break;
2795
            default:
2796
              result = NULL;
2797
              break;
2798
            }
2799
 
2800
          if (result != NULL)
2801
            return result;
2802
 
2803
          sprintf (buff, "LOPROC+%lx", p_type - PT_LOPROC);
2804
        }
2805
      else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
2806
        {
2807
          const char * result;
2808
 
2809
          switch (elf_header.e_machine)
2810
            {
2811
            case EM_PARISC:
2812
              result = get_parisc_segment_type (p_type);
2813
              break;
2814
            case EM_IA_64:
2815
              result = get_ia64_segment_type (p_type);
2816
              break;
2817
            default:
2818
              result = NULL;
2819
              break;
2820
            }
2821
 
2822
          if (result != NULL)
2823
            return result;
2824
 
2825
          sprintf (buff, "LOOS+%lx", p_type - PT_LOOS);
2826
        }
2827
      else
2828
        snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
2829
 
2830
      return buff;
2831
    }
2832
}
2833
 
2834
static const char *
2835
get_mips_section_type_name (unsigned int sh_type)
2836
{
2837
  switch (sh_type)
2838
    {
2839
    case SHT_MIPS_LIBLIST:       return "MIPS_LIBLIST";
2840
    case SHT_MIPS_MSYM:          return "MIPS_MSYM";
2841
    case SHT_MIPS_CONFLICT:      return "MIPS_CONFLICT";
2842
    case SHT_MIPS_GPTAB:         return "MIPS_GPTAB";
2843
    case SHT_MIPS_UCODE:         return "MIPS_UCODE";
2844
    case SHT_MIPS_DEBUG:         return "MIPS_DEBUG";
2845
    case SHT_MIPS_REGINFO:       return "MIPS_REGINFO";
2846
    case SHT_MIPS_PACKAGE:       return "MIPS_PACKAGE";
2847
    case SHT_MIPS_PACKSYM:       return "MIPS_PACKSYM";
2848
    case SHT_MIPS_RELD:          return "MIPS_RELD";
2849
    case SHT_MIPS_IFACE:         return "MIPS_IFACE";
2850
    case SHT_MIPS_CONTENT:       return "MIPS_CONTENT";
2851
    case SHT_MIPS_OPTIONS:       return "MIPS_OPTIONS";
2852
    case SHT_MIPS_SHDR:          return "MIPS_SHDR";
2853
    case SHT_MIPS_FDESC:         return "MIPS_FDESC";
2854
    case SHT_MIPS_EXTSYM:        return "MIPS_EXTSYM";
2855
    case SHT_MIPS_DENSE:         return "MIPS_DENSE";
2856
    case SHT_MIPS_PDESC:         return "MIPS_PDESC";
2857
    case SHT_MIPS_LOCSYM:        return "MIPS_LOCSYM";
2858
    case SHT_MIPS_AUXSYM:        return "MIPS_AUXSYM";
2859
    case SHT_MIPS_OPTSYM:        return "MIPS_OPTSYM";
2860
    case SHT_MIPS_LOCSTR:        return "MIPS_LOCSTR";
2861
    case SHT_MIPS_LINE:          return "MIPS_LINE";
2862
    case SHT_MIPS_RFDESC:        return "MIPS_RFDESC";
2863
    case SHT_MIPS_DELTASYM:      return "MIPS_DELTASYM";
2864
    case SHT_MIPS_DELTAINST:     return "MIPS_DELTAINST";
2865
    case SHT_MIPS_DELTACLASS:    return "MIPS_DELTACLASS";
2866
    case SHT_MIPS_DWARF:         return "MIPS_DWARF";
2867
    case SHT_MIPS_DELTADECL:     return "MIPS_DELTADECL";
2868
    case SHT_MIPS_SYMBOL_LIB:    return "MIPS_SYMBOL_LIB";
2869
    case SHT_MIPS_EVENTS:        return "MIPS_EVENTS";
2870
    case SHT_MIPS_TRANSLATE:     return "MIPS_TRANSLATE";
2871
    case SHT_MIPS_PIXIE:         return "MIPS_PIXIE";
2872
    case SHT_MIPS_XLATE:         return "MIPS_XLATE";
2873
    case SHT_MIPS_XLATE_DEBUG:   return "MIPS_XLATE_DEBUG";
2874
    case SHT_MIPS_WHIRL:         return "MIPS_WHIRL";
2875
    case SHT_MIPS_EH_REGION:     return "MIPS_EH_REGION";
2876
    case SHT_MIPS_XLATE_OLD:     return "MIPS_XLATE_OLD";
2877
    case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
2878
    default:
2879
      break;
2880
    }
2881
  return NULL;
2882
}
2883
 
2884
static const char *
2885
get_parisc_section_type_name (unsigned int sh_type)
2886
{
2887
  switch (sh_type)
2888
    {
2889
    case SHT_PARISC_EXT:        return "PARISC_EXT";
2890
    case SHT_PARISC_UNWIND:     return "PARISC_UNWIND";
2891
    case SHT_PARISC_DOC:        return "PARISC_DOC";
2892
    case SHT_PARISC_ANNOT:      return "PARISC_ANNOT";
2893
    case SHT_PARISC_SYMEXTN:    return "PARISC_SYMEXTN";
2894
    case SHT_PARISC_STUBS:      return "PARISC_STUBS";
2895
    case SHT_PARISC_DLKM:       return "PARISC_DLKM";
2896
    default:
2897
      break;
2898
    }
2899
  return NULL;
2900
}
2901
 
2902
static const char *
2903
get_ia64_section_type_name (unsigned int sh_type)
2904
{
2905
  /* If the top 8 bits are 0x78 the next 8 are the os/abi ID.  */
2906
  if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
2907
    return get_osabi_name ((sh_type & 0x00FF0000) >> 16);
2908
 
2909
  switch (sh_type)
2910
    {
2911
    case SHT_IA_64_EXT:                return "IA_64_EXT";
2912
    case SHT_IA_64_UNWIND:             return "IA_64_UNWIND";
2913
    case SHT_IA_64_PRIORITY_INIT:      return "IA_64_PRIORITY_INIT";
2914
    case SHT_IA_64_VMS_TRACE:          return "VMS_TRACE";
2915
    case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
2916
    case SHT_IA_64_VMS_DEBUG:          return "VMS_DEBUG";
2917
    case SHT_IA_64_VMS_DEBUG_STR:      return "VMS_DEBUG_STR";
2918
    case SHT_IA_64_VMS_LINKAGES:       return "VMS_LINKAGES";
2919
    case SHT_IA_64_VMS_SYMBOL_VECTOR:  return "VMS_SYMBOL_VECTOR";
2920
    case SHT_IA_64_VMS_FIXUP:          return "VMS_FIXUP";
2921
    default:
2922
      break;
2923
    }
2924
  return NULL;
2925
}
2926
 
2927
static const char *
2928
get_x86_64_section_type_name (unsigned int sh_type)
2929
{
2930
  switch (sh_type)
2931
    {
2932
    case SHT_X86_64_UNWIND:     return "X86_64_UNWIND";
2933
    default:
2934
      break;
2935
    }
2936
  return NULL;
2937
}
2938
 
2939
static const char *
2940
get_arm_section_type_name (unsigned int sh_type)
2941
{
2942
  switch (sh_type)
2943
    {
2944
    case SHT_ARM_EXIDX:           return "ARM_EXIDX";
2945
    case SHT_ARM_PREEMPTMAP:      return "ARM_PREEMPTMAP";
2946
    case SHT_ARM_ATTRIBUTES:      return "ARM_ATTRIBUTES";
2947
    case SHT_ARM_DEBUGOVERLAY:    return "ARM_DEBUGOVERLAY";
2948
    case SHT_ARM_OVERLAYSECTION:  return "ARM_OVERLAYSECTION";
2949
    default:
2950
      break;
2951
    }
2952
  return NULL;
2953
}
2954
 
2955
static const char *
2956
get_tic6x_section_type_name (unsigned int sh_type)
2957
{
2958
  switch (sh_type)
2959
    {
2960
    case SHT_C6000_UNWIND:
2961
      return "C6000_UNWIND";
2962
    case SHT_C6000_PREEMPTMAP:
2963
      return "C6000_PREEMPTMAP";
2964
    case SHT_C6000_ATTRIBUTES:
2965
      return "C6000_ATTRIBUTES";
2966
    case SHT_TI_ICODE:
2967
      return "TI_ICODE";
2968
    case SHT_TI_XREF:
2969
      return "TI_XREF";
2970
    case SHT_TI_HANDLER:
2971
      return "TI_HANDLER";
2972
    case SHT_TI_INITINFO:
2973
      return "TI_INITINFO";
2974
    case SHT_TI_PHATTRS:
2975
      return "TI_PHATTRS";
2976
    default:
2977
      break;
2978
    }
2979
  return NULL;
2980
}
2981
 
2982
static const char *
2983
get_section_type_name (unsigned int sh_type)
2984
{
2985
  static char buff[32];
2986
 
2987
  switch (sh_type)
2988
    {
2989
    case SHT_NULL:              return "NULL";
2990
    case SHT_PROGBITS:          return "PROGBITS";
2991
    case SHT_SYMTAB:            return "SYMTAB";
2992
    case SHT_STRTAB:            return "STRTAB";
2993
    case SHT_RELA:              return "RELA";
2994
    case SHT_HASH:              return "HASH";
2995
    case SHT_DYNAMIC:           return "DYNAMIC";
2996
    case SHT_NOTE:              return "NOTE";
2997
    case SHT_NOBITS:            return "NOBITS";
2998
    case SHT_REL:               return "REL";
2999
    case SHT_SHLIB:             return "SHLIB";
3000
    case SHT_DYNSYM:            return "DYNSYM";
3001
    case SHT_INIT_ARRAY:        return "INIT_ARRAY";
3002
    case SHT_FINI_ARRAY:        return "FINI_ARRAY";
3003
    case SHT_PREINIT_ARRAY:     return "PREINIT_ARRAY";
3004
    case SHT_GNU_HASH:          return "GNU_HASH";
3005
    case SHT_GROUP:             return "GROUP";
3006
    case SHT_SYMTAB_SHNDX:      return "SYMTAB SECTION INDICIES";
3007
    case SHT_GNU_verdef:        return "VERDEF";
3008
    case SHT_GNU_verneed:       return "VERNEED";
3009
    case SHT_GNU_versym:        return "VERSYM";
3010
    case 0x6ffffff0:            return "VERSYM";
3011
    case 0x6ffffffc:            return "VERDEF";
3012
    case 0x7ffffffd:            return "AUXILIARY";
3013
    case 0x7fffffff:            return "FILTER";
3014
    case SHT_GNU_LIBLIST:       return "GNU_LIBLIST";
3015
 
3016
    default:
3017
      if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
3018
        {
3019
          const char * result;
3020
 
3021
          switch (elf_header.e_machine)
3022
            {
3023
            case EM_MIPS:
3024
            case EM_MIPS_RS3_LE:
3025
              result = get_mips_section_type_name (sh_type);
3026
              break;
3027
            case EM_PARISC:
3028
              result = get_parisc_section_type_name (sh_type);
3029
              break;
3030
            case EM_IA_64:
3031
              result = get_ia64_section_type_name (sh_type);
3032
              break;
3033
            case EM_X86_64:
3034
            case EM_L1OM:
3035
              result = get_x86_64_section_type_name (sh_type);
3036
              break;
3037
            case EM_ARM:
3038
              result = get_arm_section_type_name (sh_type);
3039
              break;
3040
            case EM_TI_C6000:
3041
              result = get_tic6x_section_type_name (sh_type);
3042
              break;
3043
            default:
3044
              result = NULL;
3045
              break;
3046
            }
3047
 
3048
          if (result != NULL)
3049
            return result;
3050
 
3051
          sprintf (buff, "LOPROC+%x", sh_type - SHT_LOPROC);
3052
        }
3053
      else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
3054
        {
3055
          const char * result;
3056
 
3057
          switch (elf_header.e_machine)
3058
            {
3059
            case EM_IA_64:
3060
              result = get_ia64_section_type_name (sh_type);
3061
              break;
3062
            default:
3063
              result = NULL;
3064
              break;
3065
            }
3066
 
3067
          if (result != NULL)
3068
            return result;
3069
 
3070
          sprintf (buff, "LOOS+%x", sh_type - SHT_LOOS);
3071
        }
3072
      else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
3073
        sprintf (buff, "LOUSER+%x", sh_type - SHT_LOUSER);
3074
      else
3075
        snprintf (buff, sizeof (buff), _("<unknown>: %x"), sh_type);
3076
 
3077
      return buff;
3078
    }
3079
}
3080
 
3081
#define OPTION_DEBUG_DUMP       512
3082
#define OPTION_DYN_SYMS         513
3083
#define OPTION_DWARF_DEPTH      514
3084
#define OPTION_DWARF_START      515
3085
 
3086
static struct option options[] =
3087
{
3088
  {"all",              no_argument, 0, 'a'},
3089
  {"file-header",      no_argument, 0, 'h'},
3090
  {"program-headers",  no_argument, 0, 'l'},
3091
  {"headers",          no_argument, 0, 'e'},
3092
  {"histogram",        no_argument, 0, 'I'},
3093
  {"segments",         no_argument, 0, 'l'},
3094
  {"sections",         no_argument, 0, 'S'},
3095
  {"section-headers",  no_argument, 0, 'S'},
3096
  {"section-groups",   no_argument, 0, 'g'},
3097
  {"section-details",  no_argument, 0, 't'},
3098
  {"full-section-name",no_argument, 0, 'N'},
3099
  {"symbols",          no_argument, 0, 's'},
3100
  {"syms",             no_argument, 0, 's'},
3101
  {"dyn-syms",         no_argument, 0, OPTION_DYN_SYMS},
3102
  {"relocs",           no_argument, 0, 'r'},
3103
  {"notes",            no_argument, 0, 'n'},
3104
  {"dynamic",          no_argument, 0, 'd'},
3105
  {"arch-specific",    no_argument, 0, 'A'},
3106
  {"version-info",     no_argument, 0, 'V'},
3107
  {"use-dynamic",      no_argument, 0, 'D'},
3108
  {"unwind",           no_argument, 0, 'u'},
3109
  {"archive-index",    no_argument, 0, 'c'},
3110
  {"hex-dump",         required_argument, 0, 'x'},
3111
  {"relocated-dump",   required_argument, 0, 'R'},
3112
  {"string-dump",      required_argument, 0, 'p'},
3113
#ifdef SUPPORT_DISASSEMBLY
3114
  {"instruction-dump", required_argument, 0, 'i'},
3115
#endif
3116
  {"debug-dump",       optional_argument, 0, OPTION_DEBUG_DUMP},
3117
 
3118
  {"dwarf-depth",      required_argument, 0, OPTION_DWARF_DEPTH},
3119
  {"dwarf-start",      required_argument, 0, OPTION_DWARF_START},
3120
 
3121
  {"version",          no_argument, 0, 'v'},
3122
  {"wide",             no_argument, 0, 'W'},
3123
  {"help",             no_argument, 0, 'H'},
3124
  {0,                   no_argument, 0, 0}
3125
};
3126
 
3127
static void
3128
usage (FILE * stream)
3129
{
3130
  fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
3131
  fprintf (stream, _(" Display information about the contents of ELF format files\n"));
3132
  fprintf (stream, _(" Options are:\n\
3133
  -a --all               Equivalent to: -h -l -S -s -r -d -V -A -I\n\
3134
  -h --file-header       Display the ELF file header\n\
3135
  -l --program-headers   Display the program headers\n\
3136
     --segments          An alias for --program-headers\n\
3137
  -S --section-headers   Display the sections' header\n\
3138
     --sections          An alias for --section-headers\n\
3139
  -g --section-groups    Display the section groups\n\
3140
  -t --section-details   Display the section details\n\
3141
  -e --headers           Equivalent to: -h -l -S\n\
3142
  -s --syms              Display the symbol table\n\
3143
     --symbols           An alias for --syms\n\
3144
  --dyn-syms             Display the dynamic symbol table\n\
3145
  -n --notes             Display the core notes (if present)\n\
3146
  -r --relocs            Display the relocations (if present)\n\
3147
  -u --unwind            Display the unwind info (if present)\n\
3148
  -d --dynamic           Display the dynamic section (if present)\n\
3149
  -V --version-info      Display the version sections (if present)\n\
3150
  -A --arch-specific     Display architecture specific information (if any).\n\
3151
  -c --archive-index     Display the symbol/file index in an archive\n\
3152
  -D --use-dynamic       Use the dynamic section info when displaying symbols\n\
3153
  -x --hex-dump=<number|name>\n\
3154
                         Dump the contents of section <number|name> as bytes\n\
3155
  -p --string-dump=<number|name>\n\
3156
                         Dump the contents of section <number|name> as strings\n\
3157
  -R --relocated-dump=<number|name>\n\
3158
                         Dump the contents of section <number|name> as relocated bytes\n\
3159
  -w[lLiaprmfFsoRt] or\n\
3160
  --debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
3161
               =frames-interp,=str,=loc,=Ranges,=pubtypes,\n\
3162
               =gdb_index,=trace_info,=trace_abbrev,=trace_aranges]\n\
3163
                         Display the contents of DWARF2 debug sections\n"));
3164
  fprintf (stream, _("\
3165
  --dwarf-depth=N        Do not display DIEs at depth N or greater\n\
3166
  --dwarf-start=N        Display DIEs starting with N, at the same depth\n\
3167
                         or deeper\n"));
3168
#ifdef SUPPORT_DISASSEMBLY
3169
  fprintf (stream, _("\
3170
  -i --instruction-dump=<number|name>\n\
3171
                         Disassemble the contents of section <number|name>\n"));
3172
#endif
3173
  fprintf (stream, _("\
3174
  -I --histogram         Display histogram of bucket list lengths\n\
3175
  -W --wide              Allow output width to exceed 80 characters\n\
3176
  @<file>                Read options from <file>\n\
3177
  -H --help              Display this information\n\
3178
  -v --version           Display the version number of readelf\n"));
3179
 
3180
  if (REPORT_BUGS_TO[0] && stream == stdout)
3181
    fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
3182
 
3183
  exit (stream == stdout ? 0 : 1);
3184
}
3185
 
3186
/* Record the fact that the user wants the contents of section number
3187
   SECTION to be displayed using the method(s) encoded as flags bits
3188
   in TYPE.  Note, TYPE can be zero if we are creating the array for
3189
   the first time.  */
3190
 
3191
static void
3192
request_dump_bynumber (unsigned int section, dump_type type)
3193
{
3194
  if (section >= num_dump_sects)
3195
    {
3196
      dump_type * new_dump_sects;
3197
 
3198
      new_dump_sects = (dump_type *) calloc (section + 1,
3199
                                             sizeof (* dump_sects));
3200
 
3201
      if (new_dump_sects == NULL)
3202
        error (_("Out of memory allocating dump request table.\n"));
3203
      else
3204
        {
3205
          /* Copy current flag settings.  */
3206
          memcpy (new_dump_sects, dump_sects, num_dump_sects * sizeof (* dump_sects));
3207
 
3208
          free (dump_sects);
3209
 
3210
          dump_sects = new_dump_sects;
3211
          num_dump_sects = section + 1;
3212
        }
3213
    }
3214
 
3215
  if (dump_sects)
3216
    dump_sects[section] |= type;
3217
 
3218
  return;
3219
}
3220
 
3221
/* Request a dump by section name.  */
3222
 
3223
static void
3224
request_dump_byname (const char * section, dump_type type)
3225
{
3226
  struct dump_list_entry * new_request;
3227
 
3228
  new_request = (struct dump_list_entry *)
3229
      malloc (sizeof (struct dump_list_entry));
3230
  if (!new_request)
3231
    error (_("Out of memory allocating dump request table.\n"));
3232
 
3233
  new_request->name = strdup (section);
3234
  if (!new_request->name)
3235
    error (_("Out of memory allocating dump request table.\n"));
3236
 
3237
  new_request->type = type;
3238
 
3239
  new_request->next = dump_sects_byname;
3240
  dump_sects_byname = new_request;
3241
}
3242
 
3243
static inline void
3244
request_dump (dump_type type)
3245
{
3246
  int section;
3247
  char * cp;
3248
 
3249
  do_dump++;
3250
  section = strtoul (optarg, & cp, 0);
3251
 
3252
  if (! *cp && section >= 0)
3253
    request_dump_bynumber (section, type);
3254
  else
3255
    request_dump_byname (optarg, type);
3256
}
3257
 
3258
 
3259
static void
3260
parse_args (int argc, char ** argv)
3261
{
3262
  int c;
3263
 
3264
  if (argc < 2)
3265
    usage (stderr);
3266
 
3267
  while ((c = getopt_long
3268
          (argc, argv, "ADHINR:SVWacdeghi:lnp:rstuvw::x:", options, NULL)) != EOF)
3269
    {
3270
      switch (c)
3271
        {
3272
        case 0:
3273
          /* Long options.  */
3274
          break;
3275
        case 'H':
3276
          usage (stdout);
3277
          break;
3278
 
3279
        case 'a':
3280
          do_syms++;
3281
          do_reloc++;
3282
          do_unwind++;
3283
          do_dynamic++;
3284
          do_header++;
3285
          do_sections++;
3286
          do_section_groups++;
3287
          do_segments++;
3288
          do_version++;
3289
          do_histogram++;
3290
          do_arch++;
3291
          do_notes++;
3292
          break;
3293
        case 'g':
3294
          do_section_groups++;
3295
          break;
3296
        case 't':
3297
        case 'N':
3298
          do_sections++;
3299
          do_section_details++;
3300
          break;
3301
        case 'e':
3302
          do_header++;
3303
          do_sections++;
3304
          do_segments++;
3305
          break;
3306
        case 'A':
3307
          do_arch++;
3308
          break;
3309
        case 'D':
3310
          do_using_dynamic++;
3311
          break;
3312
        case 'r':
3313
          do_reloc++;
3314
          break;
3315
        case 'u':
3316
          do_unwind++;
3317
          break;
3318
        case 'h':
3319
          do_header++;
3320
          break;
3321
        case 'l':
3322
          do_segments++;
3323
          break;
3324
        case 's':
3325
          do_syms++;
3326
          break;
3327
        case 'S':
3328
          do_sections++;
3329
          break;
3330
        case 'd':
3331
          do_dynamic++;
3332
          break;
3333
        case 'I':
3334
          do_histogram++;
3335
          break;
3336
        case 'n':
3337
          do_notes++;
3338
          break;
3339
        case 'c':
3340
          do_archive_index++;
3341
          break;
3342
        case 'x':
3343
          request_dump (HEX_DUMP);
3344
          break;
3345
        case 'p':
3346
          request_dump (STRING_DUMP);
3347
          break;
3348
        case 'R':
3349
          request_dump (RELOC_DUMP);
3350
          break;
3351
        case 'w':
3352
          do_dump++;
3353
          if (optarg == 0)
3354
            {
3355
              do_debugging = 1;
3356
              dwarf_select_sections_all ();
3357
            }
3358
          else
3359
            {
3360
              do_debugging = 0;
3361
              dwarf_select_sections_by_letters (optarg);
3362
            }
3363
          break;
3364
        case OPTION_DEBUG_DUMP:
3365
          do_dump++;
3366
          if (optarg == 0)
3367
            do_debugging = 1;
3368
          else
3369
            {
3370
              do_debugging = 0;
3371
              dwarf_select_sections_by_names (optarg);
3372
            }
3373
          break;
3374
        case OPTION_DWARF_DEPTH:
3375
          {
3376
            char *cp;
3377
 
3378
            dwarf_cutoff_level = strtoul (optarg, & cp, 0);
3379
          }
3380
          break;
3381
        case OPTION_DWARF_START:
3382
          {
3383
            char *cp;
3384
 
3385
            dwarf_start_die = strtoul (optarg, & cp, 0);
3386
          }
3387
          break;
3388
        case OPTION_DYN_SYMS:
3389
          do_dyn_syms++;
3390
          break;
3391
#ifdef SUPPORT_DISASSEMBLY
3392
        case 'i':
3393
          request_dump (DISASS_DUMP);
3394
          break;
3395
#endif
3396
        case 'v':
3397
          print_version (program_name);
3398
          break;
3399
        case 'V':
3400
          do_version++;
3401
          break;
3402
        case 'W':
3403
          do_wide++;
3404
          break;
3405
        default:
3406
          /* xgettext:c-format */
3407
          error (_("Invalid option '-%c'\n"), c);
3408
          /* Drop through.  */
3409
        case '?':
3410
          usage (stderr);
3411
        }
3412
    }
3413
 
3414
  if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
3415
      && !do_segments && !do_header && !do_dump && !do_version
3416
      && !do_histogram && !do_debugging && !do_arch && !do_notes
3417
      && !do_section_groups && !do_archive_index
3418
      && !do_dyn_syms)
3419
    usage (stderr);
3420
  else if (argc < 3)
3421
    {
3422
      warn (_("Nothing to do.\n"));
3423
      usage (stderr);
3424
    }
3425
}
3426
 
3427
static const char *
3428
get_elf_class (unsigned int elf_class)
3429
{
3430
  static char buff[32];
3431
 
3432
  switch (elf_class)
3433
    {
3434
    case ELFCLASSNONE: return _("none");
3435
    case ELFCLASS32:   return "ELF32";
3436
    case ELFCLASS64:   return "ELF64";
3437
    default:
3438
      snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
3439
      return buff;
3440
    }
3441
}
3442
 
3443
static const char *
3444
get_data_encoding (unsigned int encoding)
3445
{
3446
  static char buff[32];
3447
 
3448
  switch (encoding)
3449
    {
3450
    case ELFDATANONE: return _("none");
3451
    case ELFDATA2LSB: return _("2's complement, little endian");
3452
    case ELFDATA2MSB: return _("2's complement, big endian");
3453
    default:
3454
      snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
3455
      return buff;
3456
    }
3457
}
3458
 
3459
/* Decode the data held in 'elf_header'.  */
3460
 
3461
static int
3462
process_file_header (void)
3463
{
3464
  if (   elf_header.e_ident[EI_MAG0] != ELFMAG0
3465
      || elf_header.e_ident[EI_MAG1] != ELFMAG1
3466
      || elf_header.e_ident[EI_MAG2] != ELFMAG2
3467
      || elf_header.e_ident[EI_MAG3] != ELFMAG3)
3468
    {
3469
      error
3470
        (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
3471
      return 0;
3472
    }
3473
 
3474
  init_dwarf_regnames (elf_header.e_machine);
3475
 
3476
  if (do_header)
3477
    {
3478
      int i;
3479
 
3480
      printf (_("ELF Header:\n"));
3481
      printf (_("  Magic:   "));
3482
      for (i = 0; i < EI_NIDENT; i++)
3483
        printf ("%2.2x ", elf_header.e_ident[i]);
3484
      printf ("\n");
3485
      printf (_("  Class:                             %s\n"),
3486
              get_elf_class (elf_header.e_ident[EI_CLASS]));
3487
      printf (_("  Data:                              %s\n"),
3488
              get_data_encoding (elf_header.e_ident[EI_DATA]));
3489
      printf (_("  Version:                           %d %s\n"),
3490
              elf_header.e_ident[EI_VERSION],
3491
              (elf_header.e_ident[EI_VERSION] == EV_CURRENT
3492
               ? "(current)"
3493
               : (elf_header.e_ident[EI_VERSION] != EV_NONE
3494
                  ? _("<unknown: %lx>")
3495
                  : "")));
3496
      printf (_("  OS/ABI:                            %s\n"),
3497
              get_osabi_name (elf_header.e_ident[EI_OSABI]));
3498
      printf (_("  ABI Version:                       %d\n"),
3499
              elf_header.e_ident[EI_ABIVERSION]);
3500
      printf (_("  Type:                              %s\n"),
3501
              get_file_type (elf_header.e_type));
3502
      printf (_("  Machine:                           %s\n"),
3503
              get_machine_name (elf_header.e_machine));
3504
      printf (_("  Version:                           0x%lx\n"),
3505
              (unsigned long) elf_header.e_version);
3506
 
3507
      printf (_("  Entry point address:               "));
3508
      print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
3509
      printf (_("\n  Start of program headers:          "));
3510
      print_vma ((bfd_vma) elf_header.e_phoff, DEC);
3511
      printf (_(" (bytes into file)\n  Start of section headers:          "));
3512
      print_vma ((bfd_vma) elf_header.e_shoff, DEC);
3513
      printf (_(" (bytes into file)\n"));
3514
 
3515
      printf (_("  Flags:                             0x%lx%s\n"),
3516
              (unsigned long) elf_header.e_flags,
3517
              get_machine_flags (elf_header.e_flags, elf_header.e_machine));
3518
      printf (_("  Size of this header:               %ld (bytes)\n"),
3519
              (long) elf_header.e_ehsize);
3520
      printf (_("  Size of program headers:           %ld (bytes)\n"),
3521
              (long) elf_header.e_phentsize);
3522
      printf (_("  Number of program headers:         %ld"),
3523
              (long) elf_header.e_phnum);
3524
      if (section_headers != NULL
3525
          && elf_header.e_phnum == PN_XNUM
3526
          && section_headers[0].sh_info != 0)
3527
        printf (" (%ld)", (long) section_headers[0].sh_info);
3528
      putc ('\n', stdout);
3529
      printf (_("  Size of section headers:           %ld (bytes)\n"),
3530
              (long) elf_header.e_shentsize);
3531
      printf (_("  Number of section headers:         %ld"),
3532
              (long) elf_header.e_shnum);
3533
      if (section_headers != NULL && elf_header.e_shnum == SHN_UNDEF)
3534
        printf (" (%ld)", (long) section_headers[0].sh_size);
3535
      putc ('\n', stdout);
3536
      printf (_("  Section header string table index: %ld"),
3537
              (long) elf_header.e_shstrndx);
3538
      if (section_headers != NULL
3539
          && elf_header.e_shstrndx == (SHN_XINDEX & 0xffff))
3540
        printf (" (%u)", section_headers[0].sh_link);
3541
      else if (elf_header.e_shstrndx != SHN_UNDEF
3542
               && elf_header.e_shstrndx >= elf_header.e_shnum)
3543
        printf (_(" <corrupt: out of range>"));
3544
      putc ('\n', stdout);
3545
    }
3546
 
3547
  if (section_headers != NULL)
3548
    {
3549
      if (elf_header.e_phnum == PN_XNUM
3550
          && section_headers[0].sh_info != 0)
3551
        elf_header.e_phnum = section_headers[0].sh_info;
3552
      if (elf_header.e_shnum == SHN_UNDEF)
3553
        elf_header.e_shnum = section_headers[0].sh_size;
3554
      if (elf_header.e_shstrndx == (SHN_XINDEX & 0xffff))
3555
        elf_header.e_shstrndx = section_headers[0].sh_link;
3556
      else if (elf_header.e_shstrndx >= elf_header.e_shnum)
3557
        elf_header.e_shstrndx = SHN_UNDEF;
3558
      free (section_headers);
3559
      section_headers = NULL;
3560
    }
3561
 
3562
  return 1;
3563
}
3564
 
3565
 
3566
static int
3567
get_32bit_program_headers (FILE * file, Elf_Internal_Phdr * pheaders)
3568
{
3569
  Elf32_External_Phdr * phdrs;
3570
  Elf32_External_Phdr * external;
3571
  Elf_Internal_Phdr *   internal;
3572
  unsigned int i;
3573
 
3574
  phdrs = (Elf32_External_Phdr *) get_data (NULL, file, elf_header.e_phoff,
3575
                                            elf_header.e_phentsize,
3576
                                            elf_header.e_phnum,
3577
                                            _("program headers"));
3578
  if (!phdrs)
3579
    return 0;
3580
 
3581
  for (i = 0, internal = pheaders, external = phdrs;
3582
       i < elf_header.e_phnum;
3583
       i++, internal++, external++)
3584
    {
3585
      internal->p_type   = BYTE_GET (external->p_type);
3586
      internal->p_offset = BYTE_GET (external->p_offset);
3587
      internal->p_vaddr  = BYTE_GET (external->p_vaddr);
3588
      internal->p_paddr  = BYTE_GET (external->p_paddr);
3589
      internal->p_filesz = BYTE_GET (external->p_filesz);
3590
      internal->p_memsz  = BYTE_GET (external->p_memsz);
3591
      internal->p_flags  = BYTE_GET (external->p_flags);
3592
      internal->p_align  = BYTE_GET (external->p_align);
3593
    }
3594
 
3595
  free (phdrs);
3596
 
3597
  return 1;
3598
}
3599
 
3600
static int
3601
get_64bit_program_headers (FILE * file, Elf_Internal_Phdr * pheaders)
3602
{
3603
  Elf64_External_Phdr * phdrs;
3604
  Elf64_External_Phdr * external;
3605
  Elf_Internal_Phdr *   internal;
3606
  unsigned int i;
3607
 
3608
  phdrs = (Elf64_External_Phdr *) get_data (NULL, file, elf_header.e_phoff,
3609
                                            elf_header.e_phentsize,
3610
                                            elf_header.e_phnum,
3611
                                            _("program headers"));
3612
  if (!phdrs)
3613
    return 0;
3614
 
3615
  for (i = 0, internal = pheaders, external = phdrs;
3616
       i < elf_header.e_phnum;
3617
       i++, internal++, external++)
3618
    {
3619
      internal->p_type   = BYTE_GET (external->p_type);
3620
      internal->p_flags  = BYTE_GET (external->p_flags);
3621
      internal->p_offset = BYTE_GET (external->p_offset);
3622
      internal->p_vaddr  = BYTE_GET (external->p_vaddr);
3623
      internal->p_paddr  = BYTE_GET (external->p_paddr);
3624
      internal->p_filesz = BYTE_GET (external->p_filesz);
3625
      internal->p_memsz  = BYTE_GET (external->p_memsz);
3626
      internal->p_align  = BYTE_GET (external->p_align);
3627
    }
3628
 
3629
  free (phdrs);
3630
 
3631
  return 1;
3632
}
3633
 
3634
/* Returns 1 if the program headers were read into `program_headers'.  */
3635
 
3636
static int
3637
get_program_headers (FILE * file)
3638
{
3639
  Elf_Internal_Phdr * phdrs;
3640
 
3641
  /* Check cache of prior read.  */
3642
  if (program_headers != NULL)
3643
    return 1;
3644
 
3645
  phdrs = (Elf_Internal_Phdr *) cmalloc (elf_header.e_phnum,
3646
                                         sizeof (Elf_Internal_Phdr));
3647
 
3648
  if (phdrs == NULL)
3649
    {
3650
      error (_("Out of memory\n"));
3651
      return 0;
3652
    }
3653
 
3654
  if (is_32bit_elf
3655
      ? get_32bit_program_headers (file, phdrs)
3656
      : get_64bit_program_headers (file, phdrs))
3657
    {
3658
      program_headers = phdrs;
3659
      return 1;
3660
    }
3661
 
3662
  free (phdrs);
3663
  return 0;
3664
}
3665
 
3666
/* Returns 1 if the program headers were loaded.  */
3667
 
3668
static int
3669
process_program_headers (FILE * file)
3670
{
3671
  Elf_Internal_Phdr * segment;
3672
  unsigned int i;
3673
 
3674
  if (elf_header.e_phnum == 0)
3675
    {
3676
      /* PR binutils/12467.  */
3677
      if (elf_header.e_phoff != 0)
3678
        warn (_("possibly corrupt ELF header - it has a non-zero program"
3679
                " header offset, but no program headers"));
3680
      else if (do_segments)
3681
        printf (_("\nThere are no program headers in this file.\n"));
3682
      return 0;
3683
    }
3684
 
3685
  if (do_segments && !do_header)
3686
    {
3687
      printf (_("\nElf file type is %s\n"), get_file_type (elf_header.e_type));
3688
      printf (_("Entry point "));
3689
      print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
3690
      printf (_("\nThere are %d program headers, starting at offset "),
3691
              elf_header.e_phnum);
3692
      print_vma ((bfd_vma) elf_header.e_phoff, DEC);
3693
      printf ("\n");
3694
    }
3695
 
3696
  if (! get_program_headers (file))
3697
      return 0;
3698
 
3699
  if (do_segments)
3700
    {
3701
      if (elf_header.e_phnum > 1)
3702
        printf (_("\nProgram Headers:\n"));
3703
      else
3704
        printf (_("\nProgram Headers:\n"));
3705
 
3706
      if (is_32bit_elf)
3707
        printf
3708
          (_("  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align\n"));
3709
      else if (do_wide)
3710
        printf
3711
          (_("  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align\n"));
3712
      else
3713
        {
3714
          printf
3715
            (_("  Type           Offset             VirtAddr           PhysAddr\n"));
3716
          printf
3717
            (_("                 FileSiz            MemSiz              Flags  Align\n"));
3718
        }
3719
    }
3720
 
3721
  dynamic_addr = 0;
3722
  dynamic_size = 0;
3723
 
3724
  for (i = 0, segment = program_headers;
3725
       i < elf_header.e_phnum;
3726
       i++, segment++)
3727
    {
3728
      if (do_segments)
3729
        {
3730
          printf ("  %-14.14s ", get_segment_type (segment->p_type));
3731
 
3732
          if (is_32bit_elf)
3733
            {
3734
              printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
3735
              printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
3736
              printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
3737
              printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
3738
              printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
3739
              printf ("%c%c%c ",
3740
                      (segment->p_flags & PF_R ? 'R' : ' '),
3741
                      (segment->p_flags & PF_W ? 'W' : ' '),
3742
                      (segment->p_flags & PF_X ? 'E' : ' '));
3743
              printf ("%#lx", (unsigned long) segment->p_align);
3744
            }
3745
          else if (do_wide)
3746
            {
3747
              if ((unsigned long) segment->p_offset == segment->p_offset)
3748
                printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
3749
              else
3750
                {
3751
                  print_vma (segment->p_offset, FULL_HEX);
3752
                  putchar (' ');
3753
                }
3754
 
3755
              print_vma (segment->p_vaddr, FULL_HEX);
3756
              putchar (' ');
3757
              print_vma (segment->p_paddr, FULL_HEX);
3758
              putchar (' ');
3759
 
3760
              if ((unsigned long) segment->p_filesz == segment->p_filesz)
3761
                printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
3762
              else
3763
                {
3764
                  print_vma (segment->p_filesz, FULL_HEX);
3765
                  putchar (' ');
3766
                }
3767
 
3768
              if ((unsigned long) segment->p_memsz == segment->p_memsz)
3769
                printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
3770
              else
3771
                {
3772
                  print_vma (segment->p_offset, FULL_HEX);
3773
                }
3774
 
3775
              printf (" %c%c%c ",
3776
                      (segment->p_flags & PF_R ? 'R' : ' '),
3777
                      (segment->p_flags & PF_W ? 'W' : ' '),
3778
                      (segment->p_flags & PF_X ? 'E' : ' '));
3779
 
3780
              if ((unsigned long) segment->p_align == segment->p_align)
3781
                printf ("%#lx", (unsigned long) segment->p_align);
3782
              else
3783
                {
3784
                  print_vma (segment->p_align, PREFIX_HEX);
3785
                }
3786
            }
3787
          else
3788
            {
3789
              print_vma (segment->p_offset, FULL_HEX);
3790
              putchar (' ');
3791
              print_vma (segment->p_vaddr, FULL_HEX);
3792
              putchar (' ');
3793
              print_vma (segment->p_paddr, FULL_HEX);
3794
              printf ("\n                 ");
3795
              print_vma (segment->p_filesz, FULL_HEX);
3796
              putchar (' ');
3797
              print_vma (segment->p_memsz, FULL_HEX);
3798
              printf ("  %c%c%c    ",
3799
                      (segment->p_flags & PF_R ? 'R' : ' '),
3800
                      (segment->p_flags & PF_W ? 'W' : ' '),
3801
                      (segment->p_flags & PF_X ? 'E' : ' '));
3802
              print_vma (segment->p_align, HEX);
3803
            }
3804
        }
3805
 
3806
      switch (segment->p_type)
3807
        {
3808
        case PT_DYNAMIC:
3809
          if (dynamic_addr)
3810
            error (_("more than one dynamic segment\n"));
3811
 
3812
          /* By default, assume that the .dynamic section is the first
3813
             section in the DYNAMIC segment.  */
3814
          dynamic_addr = segment->p_offset;
3815
          dynamic_size = segment->p_filesz;
3816
 
3817
          /* Try to locate the .dynamic section. If there is
3818
             a section header table, we can easily locate it.  */
3819
          if (section_headers != NULL)
3820
            {
3821
              Elf_Internal_Shdr * sec;
3822
 
3823
              sec = find_section (".dynamic");
3824
              if (sec == NULL || sec->sh_size == 0)
3825
                {
3826
                  /* A corresponding .dynamic section is expected, but on
3827
                     IA-64/OpenVMS it is OK for it to be missing.  */
3828
                  if (!is_ia64_vms ())
3829
                    error (_("no .dynamic section in the dynamic segment\n"));
3830
                  break;
3831
                }
3832
 
3833
              if (sec->sh_type == SHT_NOBITS)
3834
                {
3835
                  dynamic_size = 0;
3836
                  break;
3837
                }
3838
 
3839
              dynamic_addr = sec->sh_offset;
3840
              dynamic_size = sec->sh_size;
3841
 
3842
              if (dynamic_addr < segment->p_offset
3843
                  || dynamic_addr > segment->p_offset + segment->p_filesz)
3844
                warn (_("the .dynamic section is not contained"
3845
                        " within the dynamic segment\n"));
3846
              else if (dynamic_addr > segment->p_offset)
3847
                warn (_("the .dynamic section is not the first section"
3848
                        " in the dynamic segment.\n"));
3849
            }
3850
          break;
3851
 
3852
        case PT_INTERP:
3853
          if (fseek (file, archive_file_offset + (long) segment->p_offset,
3854
                     SEEK_SET))
3855
            error (_("Unable to find program interpreter name\n"));
3856
          else
3857
            {
3858
              char fmt [32];
3859
              int ret = snprintf (fmt, sizeof (fmt), "%%%ds", PATH_MAX);
3860
 
3861
              if (ret >= (int) sizeof (fmt) || ret < 0)
3862
                error (_("Internal error: failed to create format string to display program interpreter\n"));
3863
 
3864
              program_interpreter[0] = 0;
3865
              if (fscanf (file, fmt, program_interpreter) <= 0)
3866
                error (_("Unable to read program interpreter name\n"));
3867
 
3868
              if (do_segments)
3869
                printf (_("\n      [Requesting program interpreter: %s]"),
3870
                    program_interpreter);
3871
            }
3872
          break;
3873
        }
3874
 
3875
      if (do_segments)
3876
        putc ('\n', stdout);
3877
    }
3878
 
3879
  if (do_segments && section_headers != NULL && string_table != NULL)
3880
    {
3881
      printf (_("\n Section to Segment mapping:\n"));
3882
      printf (_("  Segment Sections...\n"));
3883
 
3884
      for (i = 0; i < elf_header.e_phnum; i++)
3885
        {
3886
          unsigned int j;
3887
          Elf_Internal_Shdr * section;
3888
 
3889
          segment = program_headers + i;
3890
          section = section_headers + 1;
3891
 
3892
          printf ("   %2.2d     ", i);
3893
 
3894
          for (j = 1; j < elf_header.e_shnum; j++, section++)
3895
            {
3896
              if (!ELF_TBSS_SPECIAL (section, segment)
3897
                  && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
3898
                printf ("%s ", SECTION_NAME (section));
3899
            }
3900
 
3901
          putc ('\n',stdout);
3902
        }
3903
    }
3904
 
3905
  return 1;
3906
}
3907
 
3908
 
3909
/* Find the file offset corresponding to VMA by using the program headers.  */
3910
 
3911
static long
3912
offset_from_vma (FILE * file, bfd_vma vma, bfd_size_type size)
3913
{
3914
  Elf_Internal_Phdr * seg;
3915
 
3916
  if (! get_program_headers (file))
3917
    {
3918
      warn (_("Cannot interpret virtual addresses without program headers.\n"));
3919
      return (long) vma;
3920
    }
3921
 
3922
  for (seg = program_headers;
3923
       seg < program_headers + elf_header.e_phnum;
3924
       ++seg)
3925
    {
3926
      if (seg->p_type != PT_LOAD)
3927
        continue;
3928
 
3929
      if (vma >= (seg->p_vaddr & -seg->p_align)
3930
          && vma + size <= seg->p_vaddr + seg->p_filesz)
3931
        return vma - seg->p_vaddr + seg->p_offset;
3932
    }
3933
 
3934
  warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
3935
        (unsigned long) vma);
3936
  return (long) vma;
3937
}
3938
 
3939
 
3940
static int
3941
get_32bit_section_headers (FILE * file, unsigned int num)
3942
{
3943
  Elf32_External_Shdr * shdrs;
3944
  Elf_Internal_Shdr *   internal;
3945
  unsigned int i;
3946
 
3947
  shdrs = (Elf32_External_Shdr *) get_data (NULL, file, elf_header.e_shoff,
3948
                                            elf_header.e_shentsize, num,
3949
                                            _("section headers"));
3950
  if (!shdrs)
3951
    return 0;
3952
 
3953
  section_headers = (Elf_Internal_Shdr *) cmalloc (num,
3954
                                                   sizeof (Elf_Internal_Shdr));
3955
 
3956
  if (section_headers == NULL)
3957
    {
3958
      error (_("Out of memory\n"));
3959
      return 0;
3960
    }
3961
 
3962
  for (i = 0, internal = section_headers;
3963
       i < num;
3964
       i++, internal++)
3965
    {
3966
      internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
3967
      internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
3968
      internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
3969
      internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
3970
      internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
3971
      internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
3972
      internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
3973
      internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
3974
      internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
3975
      internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
3976
    }
3977
 
3978
  free (shdrs);
3979
 
3980
  return 1;
3981
}
3982
 
3983
static int
3984
get_64bit_section_headers (FILE * file, unsigned int num)
3985
{
3986
  Elf64_External_Shdr * shdrs;
3987
  Elf_Internal_Shdr *   internal;
3988
  unsigned int i;
3989
 
3990
  shdrs = (Elf64_External_Shdr *) get_data (NULL, file, elf_header.e_shoff,
3991
                                            elf_header.e_shentsize, num,
3992
                                            _("section headers"));
3993
  if (!shdrs)
3994
    return 0;
3995
 
3996
  section_headers = (Elf_Internal_Shdr *) cmalloc (num,
3997
                                                   sizeof (Elf_Internal_Shdr));
3998
 
3999
  if (section_headers == NULL)
4000
    {
4001
      error (_("Out of memory\n"));
4002
      return 0;
4003
    }
4004
 
4005
  for (i = 0, internal = section_headers;
4006
       i < num;
4007
       i++, internal++)
4008
    {
4009
      internal->sh_name      = BYTE_GET (shdrs[i].sh_name);
4010
      internal->sh_type      = BYTE_GET (shdrs[i].sh_type);
4011
      internal->sh_flags     = BYTE_GET (shdrs[i].sh_flags);
4012
      internal->sh_addr      = BYTE_GET (shdrs[i].sh_addr);
4013
      internal->sh_size      = BYTE_GET (shdrs[i].sh_size);
4014
      internal->sh_entsize   = BYTE_GET (shdrs[i].sh_entsize);
4015
      internal->sh_link      = BYTE_GET (shdrs[i].sh_link);
4016
      internal->sh_info      = BYTE_GET (shdrs[i].sh_info);
4017
      internal->sh_offset    = BYTE_GET (shdrs[i].sh_offset);
4018
      internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
4019
    }
4020
 
4021
  free (shdrs);
4022
 
4023
  return 1;
4024
}
4025
 
4026
static Elf_Internal_Sym *
4027
get_32bit_elf_symbols (FILE * file, Elf_Internal_Shdr * section)
4028
{
4029
  unsigned long number;
4030
  Elf32_External_Sym * esyms = NULL;
4031
  Elf_External_Sym_Shndx * shndx;
4032
  Elf_Internal_Sym * isyms = NULL;
4033
  Elf_Internal_Sym * psym;
4034
  unsigned int j;
4035
 
4036
  /* Run some sanity checks first.  */
4037
  if (section->sh_entsize == 0)
4038
    {
4039
      error (_("sh_entsize is zero\n"));
4040
      return NULL;
4041
    }
4042
 
4043
  number = section->sh_size / section->sh_entsize;
4044
 
4045
  if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
4046
    {
4047
      error (_("Invalid sh_entsize\n"));
4048
      return NULL;
4049
    }
4050
 
4051
  esyms = (Elf32_External_Sym *) get_data (NULL, file, section->sh_offset, 1,
4052
                                           section->sh_size, _("symbols"));
4053
  if (esyms == NULL)
4054
    return NULL;
4055
 
4056
  shndx = NULL;
4057
  if (symtab_shndx_hdr != NULL
4058
      && (symtab_shndx_hdr->sh_link
4059
          == (unsigned long) (section - section_headers)))
4060
    {
4061
      shndx = (Elf_External_Sym_Shndx *) get_data (NULL, file,
4062
                                                   symtab_shndx_hdr->sh_offset,
4063
                                                   1, symtab_shndx_hdr->sh_size,
4064
                                                   _("symtab shndx"));
4065
      if (shndx == NULL)
4066
        goto exit_point;
4067
    }
4068
 
4069
  isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
4070
 
4071
  if (isyms == NULL)
4072
    {
4073
      error (_("Out of memory\n"));
4074
      goto exit_point;
4075
    }
4076
 
4077
  for (j = 0, psym = isyms; j < number; j++, psym++)
4078
    {
4079
      psym->st_name  = BYTE_GET (esyms[j].st_name);
4080
      psym->st_value = BYTE_GET (esyms[j].st_value);
4081
      psym->st_size  = BYTE_GET (esyms[j].st_size);
4082
      psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
4083
      if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
4084
        psym->st_shndx
4085
          = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
4086
      else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
4087
        psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
4088
      psym->st_info  = BYTE_GET (esyms[j].st_info);
4089
      psym->st_other = BYTE_GET (esyms[j].st_other);
4090
    }
4091
 
4092
 exit_point:
4093
  if (shndx)
4094
    free (shndx);
4095
  if (esyms)
4096
    free (esyms);
4097
 
4098
  return isyms;
4099
}
4100
 
4101
static Elf_Internal_Sym *
4102
get_64bit_elf_symbols (FILE * file, Elf_Internal_Shdr * section)
4103
{
4104
  unsigned long number;
4105
  Elf64_External_Sym * esyms;
4106
  Elf_External_Sym_Shndx * shndx;
4107
  Elf_Internal_Sym * isyms;
4108
  Elf_Internal_Sym * psym;
4109
  unsigned int j;
4110
 
4111
  /* Run some sanity checks first.  */
4112
  if (section->sh_entsize == 0)
4113
    {
4114
      error (_("sh_entsize is zero\n"));
4115
      return NULL;
4116
    }
4117
 
4118
  number = section->sh_size / section->sh_entsize;
4119
 
4120
  if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
4121
    {
4122
      error (_("Invalid sh_entsize\n"));
4123
      return NULL;
4124
    }
4125
 
4126
  esyms = (Elf64_External_Sym *) get_data (NULL, file, section->sh_offset, 1,
4127
                                           section->sh_size, _("symbols"));
4128
  if (!esyms)
4129
    return NULL;
4130
 
4131
  shndx = NULL;
4132
  if (symtab_shndx_hdr != NULL
4133
      && (symtab_shndx_hdr->sh_link
4134
          == (unsigned long) (section - section_headers)))
4135
    {
4136
      shndx = (Elf_External_Sym_Shndx *) get_data (NULL, file,
4137
                                                   symtab_shndx_hdr->sh_offset,
4138
                                                   1, symtab_shndx_hdr->sh_size,
4139
                                                   _("symtab shndx"));
4140
      if (!shndx)
4141
        {
4142
          free (esyms);
4143
          return NULL;
4144
        }
4145
    }
4146
 
4147
  isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
4148
 
4149
  if (isyms == NULL)
4150
    {
4151
      error (_("Out of memory\n"));
4152
      if (shndx)
4153
        free (shndx);
4154
      free (esyms);
4155
      return NULL;
4156
    }
4157
 
4158
  for (j = 0, psym = isyms;
4159
       j < number;
4160
       j++, psym++)
4161
    {
4162
      psym->st_name  = BYTE_GET (esyms[j].st_name);
4163
      psym->st_info  = BYTE_GET (esyms[j].st_info);
4164
      psym->st_other = BYTE_GET (esyms[j].st_other);
4165
      psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
4166
      if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
4167
        psym->st_shndx
4168
          = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
4169
      else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
4170
        psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
4171
      psym->st_value = BYTE_GET (esyms[j].st_value);
4172
      psym->st_size  = BYTE_GET (esyms[j].st_size);
4173
    }
4174
 
4175
  if (shndx)
4176
    free (shndx);
4177
  free (esyms);
4178
 
4179
  return isyms;
4180
}
4181
 
4182
static const char *
4183
get_elf_section_flags (bfd_vma sh_flags)
4184
{
4185
  static char buff[1024];
4186
  char * p = buff;
4187
  int field_size = is_32bit_elf ? 8 : 16;
4188
  int sindex;
4189
  int size = sizeof (buff) - (field_size + 4 + 1);
4190
  bfd_vma os_flags = 0;
4191
  bfd_vma proc_flags = 0;
4192
  bfd_vma unknown_flags = 0;
4193
  static const struct
4194
    {
4195
      const char * str;
4196
      int len;
4197
    }
4198
  flags [] =
4199
    {
4200
      /*  0 */ { STRING_COMMA_LEN ("WRITE") },
4201
      /*  1 */ { STRING_COMMA_LEN ("ALLOC") },
4202
      /*  2 */ { STRING_COMMA_LEN ("EXEC") },
4203
      /*  3 */ { STRING_COMMA_LEN ("MERGE") },
4204
      /*  4 */ { STRING_COMMA_LEN ("STRINGS") },
4205
      /*  5 */ { STRING_COMMA_LEN ("INFO LINK") },
4206
      /*  6 */ { STRING_COMMA_LEN ("LINK ORDER") },
4207
      /*  7 */ { STRING_COMMA_LEN ("OS NONCONF") },
4208
      /*  8 */ { STRING_COMMA_LEN ("GROUP") },
4209
      /*  9 */ { STRING_COMMA_LEN ("TLS") },
4210
      /* IA-64 specific.  */
4211
      /* 10 */ { STRING_COMMA_LEN ("SHORT") },
4212
      /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
4213
      /* IA-64 OpenVMS specific.  */
4214
      /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
4215
      /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
4216
      /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
4217
      /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
4218
      /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
4219
      /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
4220
      /* Generic.  */
4221
      /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
4222
      /* SPARC specific.  */
4223
      /* 19 */ { STRING_COMMA_LEN ("ORDERED") }
4224
    };
4225
 
4226
  if (do_section_details)
4227
    {
4228
      sprintf (buff, "[%*.*lx]: ",
4229
               field_size, field_size, (unsigned long) sh_flags);
4230
      p += field_size + 4;
4231
    }
4232
 
4233
  while (sh_flags)
4234
    {
4235
      bfd_vma flag;
4236
 
4237
      flag = sh_flags & - sh_flags;
4238
      sh_flags &= ~ flag;
4239
 
4240
      if (do_section_details)
4241
        {
4242
          switch (flag)
4243
            {
4244
            case SHF_WRITE:             sindex = 0; break;
4245
            case SHF_ALLOC:             sindex = 1; break;
4246
            case SHF_EXECINSTR:         sindex = 2; break;
4247
            case SHF_MERGE:             sindex = 3; break;
4248
            case SHF_STRINGS:           sindex = 4; break;
4249
            case SHF_INFO_LINK:         sindex = 5; break;
4250
            case SHF_LINK_ORDER:        sindex = 6; break;
4251
            case SHF_OS_NONCONFORMING:  sindex = 7; break;
4252
            case SHF_GROUP:             sindex = 8; break;
4253
            case SHF_TLS:               sindex = 9; break;
4254
            case SHF_EXCLUDE:           sindex = 18; break;
4255
 
4256
            default:
4257
              sindex = -1;
4258
              switch (elf_header.e_machine)
4259
                {
4260
                case EM_IA_64:
4261
                  if (flag == SHF_IA_64_SHORT)
4262
                    sindex = 10;
4263
                  else if (flag == SHF_IA_64_NORECOV)
4264
                    sindex = 11;
4265
#ifdef BFD64
4266
                  else if (elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
4267
                    switch (flag)
4268
                      {
4269
                      case SHF_IA_64_VMS_GLOBAL:      sindex = 12; break;
4270
                      case SHF_IA_64_VMS_OVERLAID:    sindex = 13; break;
4271
                      case SHF_IA_64_VMS_SHARED:      sindex = 14; break;
4272
                      case SHF_IA_64_VMS_VECTOR:      sindex = 15; break;
4273
                      case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
4274
                      case SHF_IA_64_VMS_PROTECTED:   sindex = 17; break;
4275
                      default:                        break;
4276
                      }
4277
#endif
4278
                  break;
4279
 
4280
                case EM_386:
4281
                case EM_486:
4282
                case EM_X86_64:
4283
                case EM_L1OM:
4284
                case EM_OLD_SPARCV9:
4285
                case EM_SPARC32PLUS:
4286
                case EM_SPARCV9:
4287
                case EM_SPARC:
4288
                  if (flag == SHF_ORDERED)
4289
                    sindex = 19;
4290
                  break;
4291
                default:
4292
                  break;
4293
                }
4294
            }
4295
 
4296
          if (sindex != -1)
4297
            {
4298
              if (p != buff + field_size + 4)
4299
                {
4300
                  if (size < (10 + 2))
4301
                    abort ();
4302
                  size -= 2;
4303
                  *p++ = ',';
4304
                  *p++ = ' ';
4305
                }
4306
 
4307
              size -= flags [sindex].len;
4308
              p = stpcpy (p, flags [sindex].str);
4309
            }
4310
          else if (flag & SHF_MASKOS)
4311
            os_flags |= flag;
4312
          else if (flag & SHF_MASKPROC)
4313
            proc_flags |= flag;
4314
          else
4315
            unknown_flags |= flag;
4316
        }
4317
      else
4318
        {
4319
          switch (flag)
4320
            {
4321
            case SHF_WRITE:             *p = 'W'; break;
4322
            case SHF_ALLOC:             *p = 'A'; break;
4323
            case SHF_EXECINSTR:         *p = 'X'; break;
4324
            case SHF_MERGE:             *p = 'M'; break;
4325
            case SHF_STRINGS:           *p = 'S'; break;
4326
            case SHF_INFO_LINK:         *p = 'I'; break;
4327
            case SHF_LINK_ORDER:        *p = 'L'; break;
4328
            case SHF_OS_NONCONFORMING:  *p = 'O'; break;
4329
            case SHF_GROUP:             *p = 'G'; break;
4330
            case SHF_TLS:               *p = 'T'; break;
4331
            case SHF_EXCLUDE:           *p = 'E'; break;
4332
 
4333
            default:
4334
              if ((elf_header.e_machine == EM_X86_64
4335
                   || elf_header.e_machine == EM_L1OM)
4336
                  && flag == SHF_X86_64_LARGE)
4337
                *p = 'l';
4338
              else if (flag & SHF_MASKOS)
4339
                {
4340
                  *p = 'o';
4341
                  sh_flags &= ~ SHF_MASKOS;
4342
                }
4343
              else if (flag & SHF_MASKPROC)
4344
                {
4345
                  *p = 'p';
4346
                  sh_flags &= ~ SHF_MASKPROC;
4347
                }
4348
              else
4349
                *p = 'x';
4350
              break;
4351
            }
4352
          p++;
4353
        }
4354
    }
4355
 
4356
  if (do_section_details)
4357
    {
4358
      if (os_flags)
4359
        {
4360
          size -= 5 + field_size;
4361
          if (p != buff + field_size + 4)
4362
            {
4363
              if (size < (2 + 1))
4364
                abort ();
4365
              size -= 2;
4366
              *p++ = ',';
4367
              *p++ = ' ';
4368
            }
4369
          sprintf (p, "OS (%*.*lx)", field_size, field_size,
4370
                   (unsigned long) os_flags);
4371
          p += 5 + field_size;
4372
        }
4373
      if (proc_flags)
4374
        {
4375
          size -= 7 + field_size;
4376
          if (p != buff + field_size + 4)
4377
            {
4378
              if (size < (2 + 1))
4379
                abort ();
4380
              size -= 2;
4381
              *p++ = ',';
4382
              *p++ = ' ';
4383
            }
4384
          sprintf (p, "PROC (%*.*lx)", field_size, field_size,
4385
                   (unsigned long) proc_flags);
4386
          p += 7 + field_size;
4387
        }
4388
      if (unknown_flags)
4389
        {
4390
          size -= 10 + field_size;
4391
          if (p != buff + field_size + 4)
4392
            {
4393
              if (size < (2 + 1))
4394
                abort ();
4395
              size -= 2;
4396
              *p++ = ',';
4397
              *p++ = ' ';
4398
            }
4399
          sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
4400
                   (unsigned long) unknown_flags);
4401
          p += 10 + field_size;
4402
        }
4403
    }
4404
 
4405
  *p = '\0';
4406
  return buff;
4407
}
4408
 
4409
static int
4410
process_section_headers (FILE * file)
4411
{
4412
  Elf_Internal_Shdr * section;
4413
  unsigned int i;
4414
 
4415
  section_headers = NULL;
4416
 
4417
  if (elf_header.e_shnum == 0)
4418
    {
4419
      /* PR binutils/12467.  */
4420
      if (elf_header.e_shoff != 0)
4421
        warn (_("possibly corrupt ELF file header - it has a non-zero"
4422
                " section header offset, but no section headers\n"));
4423
      else if (do_sections)
4424
        printf (_("\nThere are no sections in this file.\n"));
4425
 
4426
      return 1;
4427
    }
4428
 
4429
  if (do_sections && !do_header)
4430
    printf (_("There are %d section headers, starting at offset 0x%lx:\n"),
4431
            elf_header.e_shnum, (unsigned long) elf_header.e_shoff);
4432
 
4433
  if (is_32bit_elf)
4434
    {
4435
      if (! get_32bit_section_headers (file, elf_header.e_shnum))
4436
        return 0;
4437
    }
4438
  else if (! get_64bit_section_headers (file, elf_header.e_shnum))
4439
    return 0;
4440
 
4441
  /* Read in the string table, so that we have names to display.  */
4442
  if (elf_header.e_shstrndx != SHN_UNDEF
4443
       && elf_header.e_shstrndx < elf_header.e_shnum)
4444
    {
4445
      section = section_headers + elf_header.e_shstrndx;
4446
 
4447
      if (section->sh_size != 0)
4448
        {
4449
          string_table = (char *) get_data (NULL, file, section->sh_offset,
4450
                                            1, section->sh_size,
4451
                                            _("string table"));
4452
 
4453
          string_table_length = string_table != NULL ? section->sh_size : 0;
4454
        }
4455
    }
4456
 
4457
  /* Scan the sections for the dynamic symbol table
4458
     and dynamic string table and debug sections.  */
4459
  dynamic_symbols = NULL;
4460
  dynamic_strings = NULL;
4461
  dynamic_syminfo = NULL;
4462
  symtab_shndx_hdr = NULL;
4463
 
4464
  eh_addr_size = is_32bit_elf ? 4 : 8;
4465
  switch (elf_header.e_machine)
4466
    {
4467
    case EM_MIPS:
4468
    case EM_MIPS_RS3_LE:
4469
      /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
4470
         FDE addresses.  However, the ABI also has a semi-official ILP32
4471
         variant for which the normal FDE address size rules apply.
4472
 
4473
         GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
4474
         section, where XX is the size of longs in bits.  Unfortunately,
4475
         earlier compilers provided no way of distinguishing ILP32 objects
4476
         from LP64 objects, so if there's any doubt, we should assume that
4477
         the official LP64 form is being used.  */
4478
      if ((elf_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
4479
          && find_section (".gcc_compiled_long32") == NULL)
4480
        eh_addr_size = 8;
4481
      break;
4482
 
4483
    case EM_H8_300:
4484
    case EM_H8_300H:
4485
      switch (elf_header.e_flags & EF_H8_MACH)
4486
        {
4487
        case E_H8_MACH_H8300:
4488
        case E_H8_MACH_H8300HN:
4489
        case E_H8_MACH_H8300SN:
4490
        case E_H8_MACH_H8300SXN:
4491
          eh_addr_size = 2;
4492
          break;
4493
        case E_H8_MACH_H8300H:
4494
        case E_H8_MACH_H8300S:
4495
        case E_H8_MACH_H8300SX:
4496
          eh_addr_size = 4;
4497
          break;
4498
        }
4499
      break;
4500
 
4501
    case EM_M32C_OLD:
4502
    case EM_M32C:
4503
      switch (elf_header.e_flags & EF_M32C_CPU_MASK)
4504
        {
4505
        case EF_M32C_CPU_M16C:
4506
          eh_addr_size = 2;
4507
          break;
4508
        }
4509
      break;
4510
    }
4511
 
4512
#define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
4513
  do                                                                        \
4514
    {                                                                       \
4515
      size_t expected_entsize                                               \
4516
        = is_32bit_elf ? size32 : size64;                                   \
4517
      if (section->sh_entsize != expected_entsize)                          \
4518
        error (_("Section %d has invalid sh_entsize %lx (expected %lx)\n"), \
4519
               i, (unsigned long int) section->sh_entsize,                  \
4520
               (unsigned long int) expected_entsize);                       \
4521
      section->sh_entsize = expected_entsize;                               \
4522
    }                                                                       \
4523
  while (0)
4524
#define CHECK_ENTSIZE(section, i, type) \
4525
  CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type),         \
4526
                        sizeof (Elf64_External_##type))
4527
 
4528
  for (i = 0, section = section_headers;
4529
       i < elf_header.e_shnum;
4530
       i++, section++)
4531
    {
4532
      char * name = SECTION_NAME (section);
4533
 
4534
      if (section->sh_type == SHT_DYNSYM)
4535
        {
4536
          if (dynamic_symbols != NULL)
4537
            {
4538
              error (_("File contains multiple dynamic symbol tables\n"));
4539
              continue;
4540
            }
4541
 
4542
          CHECK_ENTSIZE (section, i, Sym);
4543
          num_dynamic_syms = section->sh_size / section->sh_entsize;
4544
          dynamic_symbols = GET_ELF_SYMBOLS (file, section);
4545
        }
4546
      else if (section->sh_type == SHT_STRTAB
4547
               && streq (name, ".dynstr"))
4548
        {
4549
          if (dynamic_strings != NULL)
4550
            {
4551
              error (_("File contains multiple dynamic string tables\n"));
4552
              continue;
4553
            }
4554
 
4555
          dynamic_strings = (char *) get_data (NULL, file, section->sh_offset,
4556
                                               1, section->sh_size,
4557
                                               _("dynamic strings"));
4558
          dynamic_strings_length = section->sh_size;
4559
        }
4560
      else if (section->sh_type == SHT_SYMTAB_SHNDX)
4561
        {
4562
          if (symtab_shndx_hdr != NULL)
4563
            {
4564
              error (_("File contains multiple symtab shndx tables\n"));
4565
              continue;
4566
            }
4567
          symtab_shndx_hdr = section;
4568
        }
4569
      else if (section->sh_type == SHT_SYMTAB)
4570
        CHECK_ENTSIZE (section, i, Sym);
4571
      else if (section->sh_type == SHT_GROUP)
4572
        CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
4573
      else if (section->sh_type == SHT_REL)
4574
        CHECK_ENTSIZE (section, i, Rel);
4575
      else if (section->sh_type == SHT_RELA)
4576
        CHECK_ENTSIZE (section, i, Rela);
4577
      else if ((do_debugging || do_debug_info || do_debug_abbrevs
4578
                || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
4579
                || do_debug_aranges || do_debug_frames || do_debug_macinfo
4580
                || do_debug_str || do_debug_loc || do_debug_ranges)
4581
               && (const_strneq (name, ".debug_")
4582
                   || const_strneq (name, ".zdebug_")))
4583
        {
4584
          if (name[1] == 'z')
4585
            name += sizeof (".zdebug_") - 1;
4586
          else
4587
            name += sizeof (".debug_") - 1;
4588
 
4589
          if (do_debugging
4590
              || (do_debug_info     && streq (name, "info"))
4591
              || (do_debug_info     && streq (name, "types"))
4592
              || (do_debug_abbrevs  && streq (name, "abbrev"))
4593
              || (do_debug_lines    && streq (name, "line"))
4594
              || (do_debug_pubnames && streq (name, "pubnames"))
4595
              || (do_debug_pubtypes && streq (name, "pubtypes"))
4596
              || (do_debug_aranges  && streq (name, "aranges"))
4597
              || (do_debug_ranges   && streq (name, "ranges"))
4598
              || (do_debug_frames   && streq (name, "frame"))
4599
              || (do_debug_macinfo  && streq (name, "macinfo"))
4600
              || (do_debug_str      && streq (name, "str"))
4601
              || (do_debug_loc      && streq (name, "loc"))
4602
              )
4603
            request_dump_bynumber (i, DEBUG_DUMP);
4604
        }
4605
      /* Linkonce section to be combined with .debug_info at link time.  */
4606
      else if ((do_debugging || do_debug_info)
4607
               && const_strneq (name, ".gnu.linkonce.wi."))
4608
        request_dump_bynumber (i, DEBUG_DUMP);
4609
      else if (do_debug_frames && streq (name, ".eh_frame"))
4610
        request_dump_bynumber (i, DEBUG_DUMP);
4611
      else if (do_gdb_index && streq (name, ".gdb_index"))
4612
        request_dump_bynumber (i, DEBUG_DUMP);
4613
      /* Trace sections for Itanium VMS.  */
4614
      else if ((do_debugging || do_trace_info || do_trace_abbrevs
4615
                || do_trace_aranges)
4616
               && const_strneq (name, ".trace_"))
4617
        {
4618
          name += sizeof (".trace_") - 1;
4619
 
4620
          if (do_debugging
4621
              || (do_trace_info     && streq (name, "info"))
4622
              || (do_trace_abbrevs  && streq (name, "abbrev"))
4623
              || (do_trace_aranges  && streq (name, "aranges"))
4624
              )
4625
            request_dump_bynumber (i, DEBUG_DUMP);
4626
        }
4627
 
4628
    }
4629
 
4630
  if (! do_sections)
4631
    return 1;
4632
 
4633
  if (elf_header.e_shnum > 1)
4634
    printf (_("\nSection Headers:\n"));
4635
  else
4636
    printf (_("\nSection Header:\n"));
4637
 
4638
  if (is_32bit_elf)
4639
    {
4640
      if (do_section_details)
4641
        {
4642
          printf (_("  [Nr] Name\n"));
4643
          printf (_("       Type            Addr     Off    Size   ES   Lk Inf Al\n"));
4644
        }
4645
      else
4646
        printf
4647
          (_("  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al\n"));
4648
    }
4649
  else if (do_wide)
4650
    {
4651
      if (do_section_details)
4652
        {
4653
          printf (_("  [Nr] Name\n"));
4654
          printf (_("       Type            Address          Off    Size   ES   Lk Inf Al\n"));
4655
        }
4656
      else
4657
        printf
4658
          (_("  [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al\n"));
4659
    }
4660
  else
4661
    {
4662
      if (do_section_details)
4663
        {
4664
          printf (_("  [Nr] Name\n"));
4665
          printf (_("       Type              Address          Offset            Link\n"));
4666
          printf (_("       Size              EntSize          Info              Align\n"));
4667
        }
4668
      else
4669
        {
4670
          printf (_("  [Nr] Name              Type             Address           Offset\n"));
4671
          printf (_("       Size              EntSize          Flags  Link  Info  Align\n"));
4672
        }
4673
    }
4674
 
4675
  if (do_section_details)
4676
    printf (_("       Flags\n"));
4677
 
4678
  for (i = 0, section = section_headers;
4679
       i < elf_header.e_shnum;
4680
       i++, section++)
4681
    {
4682
      if (do_section_details)
4683
        {
4684
          printf ("  [%2u] %s\n",
4685
                  i,
4686
                  SECTION_NAME (section));
4687
          if (is_32bit_elf || do_wide)
4688
            printf ("       %-15.15s ",
4689
                    get_section_type_name (section->sh_type));
4690
        }
4691
      else
4692
        printf ((do_wide ? "  [%2u] %-17s %-15s "
4693
                         : "  [%2u] %-17.17s %-15.15s "),
4694
                i,
4695
                SECTION_NAME (section),
4696
                get_section_type_name (section->sh_type));
4697
 
4698
      if (is_32bit_elf)
4699
        {
4700
          const char * link_too_big = NULL;
4701
 
4702
          print_vma (section->sh_addr, LONG_HEX);
4703
 
4704
          printf ( " %6.6lx %6.6lx %2.2lx",
4705
                   (unsigned long) section->sh_offset,
4706
                   (unsigned long) section->sh_size,
4707
                   (unsigned long) section->sh_entsize);
4708
 
4709
          if (do_section_details)
4710
            fputs ("  ", stdout);
4711
          else
4712
            printf (" %3s ", get_elf_section_flags (section->sh_flags));
4713
 
4714
          if (section->sh_link >= elf_header.e_shnum)
4715
            {
4716
              link_too_big = "";
4717
              /* The sh_link value is out of range.  Normally this indicates
4718
                 an error but it can have special values in Solaris binaries.  */
4719
              switch (elf_header.e_machine)
4720
                {
4721
                case EM_386:
4722
                case EM_486:
4723
                case EM_X86_64:
4724
                case EM_L1OM:
4725
                case EM_OLD_SPARCV9:
4726
                case EM_SPARC32PLUS:
4727
                case EM_SPARCV9:
4728
                case EM_SPARC:
4729
                  if (section->sh_link == (SHN_BEFORE & 0xffff))
4730
                    link_too_big = "BEFORE";
4731
                  else if (section->sh_link == (SHN_AFTER & 0xffff))
4732
                    link_too_big = "AFTER";
4733
                  break;
4734
                default:
4735
                  break;
4736
                }
4737
            }
4738
 
4739
          if (do_section_details)
4740
            {
4741
              if (link_too_big != NULL && * link_too_big)
4742
                printf ("<%s> ", link_too_big);
4743
              else
4744
                printf ("%2u ", section->sh_link);
4745
              printf ("%3u %2lu\n", section->sh_info,
4746
                      (unsigned long) section->sh_addralign);
4747
            }
4748
          else
4749
            printf ("%2u %3u %2lu\n",
4750
                    section->sh_link,
4751
                    section->sh_info,
4752
                    (unsigned long) section->sh_addralign);
4753
 
4754
          if (link_too_big && ! * link_too_big)
4755
            warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
4756
                  i, section->sh_link);
4757
        }
4758
      else if (do_wide)
4759
        {
4760
          print_vma (section->sh_addr, LONG_HEX);
4761
 
4762
          if ((long) section->sh_offset == section->sh_offset)
4763
            printf (" %6.6lx", (unsigned long) section->sh_offset);
4764
          else
4765
            {
4766
              putchar (' ');
4767
              print_vma (section->sh_offset, LONG_HEX);
4768
            }
4769
 
4770
          if ((unsigned long) section->sh_size == section->sh_size)
4771
            printf (" %6.6lx", (unsigned long) section->sh_size);
4772
          else
4773
            {
4774
              putchar (' ');
4775
              print_vma (section->sh_size, LONG_HEX);
4776
            }
4777
 
4778
          if ((unsigned long) section->sh_entsize == section->sh_entsize)
4779
            printf (" %2.2lx", (unsigned long) section->sh_entsize);
4780
          else
4781
            {
4782
              putchar (' ');
4783
              print_vma (section->sh_entsize, LONG_HEX);
4784
            }
4785
 
4786
          if (do_section_details)
4787
            fputs ("  ", stdout);
4788
          else
4789
            printf (" %3s ", get_elf_section_flags (section->sh_flags));
4790
 
4791
          printf ("%2u %3u ", section->sh_link, section->sh_info);
4792
 
4793
          if ((unsigned long) section->sh_addralign == section->sh_addralign)
4794
            printf ("%2lu\n", (unsigned long) section->sh_addralign);
4795
          else
4796
            {
4797
              print_vma (section->sh_addralign, DEC);
4798
              putchar ('\n');
4799
            }
4800
        }
4801
      else if (do_section_details)
4802
        {
4803
          printf ("       %-15.15s  ",
4804
                  get_section_type_name (section->sh_type));
4805
          print_vma (section->sh_addr, LONG_HEX);
4806
          if ((long) section->sh_offset == section->sh_offset)
4807
            printf ("  %16.16lx", (unsigned long) section->sh_offset);
4808
          else
4809
            {
4810
              printf ("  ");
4811
              print_vma (section->sh_offset, LONG_HEX);
4812
            }
4813
          printf ("  %u\n       ", section->sh_link);
4814
          print_vma (section->sh_size, LONG_HEX);
4815
          putchar (' ');
4816
          print_vma (section->sh_entsize, LONG_HEX);
4817
 
4818
          printf ("  %-16u  %lu\n",
4819
                  section->sh_info,
4820
                  (unsigned long) section->sh_addralign);
4821
        }
4822
      else
4823
        {
4824
          putchar (' ');
4825
          print_vma (section->sh_addr, LONG_HEX);
4826
          if ((long) section->sh_offset == section->sh_offset)
4827
            printf ("  %8.8lx", (unsigned long) section->sh_offset);
4828
          else
4829
            {
4830
              printf ("  ");
4831
              print_vma (section->sh_offset, LONG_HEX);
4832
            }
4833
          printf ("\n       ");
4834
          print_vma (section->sh_size, LONG_HEX);
4835
          printf ("  ");
4836
          print_vma (section->sh_entsize, LONG_HEX);
4837
 
4838
          printf (" %3s ", get_elf_section_flags (section->sh_flags));
4839
 
4840
          printf ("     %2u   %3u     %lu\n",
4841
                  section->sh_link,
4842
                  section->sh_info,
4843
                  (unsigned long) section->sh_addralign);
4844
        }
4845
 
4846
      if (do_section_details)
4847
        printf ("       %s\n", get_elf_section_flags (section->sh_flags));
4848
    }
4849
 
4850
  if (!do_section_details)
4851
    {
4852
      if (elf_header.e_machine == EM_X86_64
4853
          || elf_header.e_machine == EM_L1OM)
4854
        printf (_("Key to Flags:\n\
4855
  W (write), A (alloc), X (execute), M (merge), S (strings), l (large)\n\
4856
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)\n\
4857
  O (extra OS processing required) o (OS specific), p (processor specific)\n"));
4858
      else
4859
        printf (_("Key to Flags:\n\
4860
  W (write), A (alloc), X (execute), M (merge), S (strings)\n\
4861
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)\n\
4862
  O (extra OS processing required) o (OS specific), p (processor specific)\n"));
4863
    }
4864
 
4865
  return 1;
4866
}
4867
 
4868
static const char *
4869
get_group_flags (unsigned int flags)
4870
{
4871
  static char buff[32];
4872
  switch (flags)
4873
    {
4874
    case 0:
4875
      return "";
4876
 
4877
    case GRP_COMDAT:
4878
      return "COMDAT ";
4879
 
4880
   default:
4881
      snprintf (buff, sizeof (buff), _("[<unknown>: 0x%x] "), flags);
4882
      break;
4883
    }
4884
  return buff;
4885
}
4886
 
4887
static int
4888
process_section_groups (FILE * file)
4889
{
4890
  Elf_Internal_Shdr * section;
4891
  unsigned int i;
4892
  struct group * group;
4893
  Elf_Internal_Shdr * symtab_sec;
4894
  Elf_Internal_Shdr * strtab_sec;
4895
  Elf_Internal_Sym * symtab;
4896
  char * strtab;
4897
  size_t strtab_size;
4898
 
4899
  /* Don't process section groups unless needed.  */
4900
  if (!do_unwind && !do_section_groups)
4901
    return 1;
4902
 
4903
  if (elf_header.e_shnum == 0)
4904
    {
4905
      if (do_section_groups)
4906
        printf (_("\nThere are no sections to group in this file.\n"));
4907
 
4908
      return 1;
4909
    }
4910
 
4911
  if (section_headers == NULL)
4912
    {
4913
      error (_("Section headers are not available!\n"));
4914
      abort ();
4915
    }
4916
 
4917
  section_headers_groups = (struct group **) calloc (elf_header.e_shnum,
4918
                                                     sizeof (struct group *));
4919
 
4920
  if (section_headers_groups == NULL)
4921
    {
4922
      error (_("Out of memory\n"));
4923
      return 0;
4924
    }
4925
 
4926
  /* Scan the sections for the group section.  */
4927
  group_count = 0;
4928
  for (i = 0, section = section_headers;
4929
       i < elf_header.e_shnum;
4930
       i++, section++)
4931
    if (section->sh_type == SHT_GROUP)
4932
      group_count++;
4933
 
4934
  if (group_count == 0)
4935
    {
4936
      if (do_section_groups)
4937
        printf (_("\nThere are no section groups in this file.\n"));
4938
 
4939
      return 1;
4940
    }
4941
 
4942
  section_groups = (struct group *) calloc (group_count, sizeof (struct group));
4943
 
4944
  if (section_groups == NULL)
4945
    {
4946
      error (_("Out of memory\n"));
4947
      return 0;
4948
    }
4949
 
4950
  symtab_sec = NULL;
4951
  strtab_sec = NULL;
4952
  symtab = NULL;
4953
  strtab = NULL;
4954
  strtab_size = 0;
4955
  for (i = 0, section = section_headers, group = section_groups;
4956
       i < elf_header.e_shnum;
4957
       i++, section++)
4958
    {
4959
      if (section->sh_type == SHT_GROUP)
4960
        {
4961
          char * name = SECTION_NAME (section);
4962
          char * group_name;
4963
          unsigned char * start;
4964
          unsigned char * indices;
4965
          unsigned int entry, j, size;
4966
          Elf_Internal_Shdr * sec;
4967
          Elf_Internal_Sym * sym;
4968
 
4969
          /* Get the symbol table.  */
4970
          if (section->sh_link >= elf_header.e_shnum
4971
              || ((sec = section_headers + section->sh_link)->sh_type
4972
                  != SHT_SYMTAB))
4973
            {
4974
              error (_("Bad sh_link in group section `%s'\n"), name);
4975
              continue;
4976
            }
4977
 
4978
          if (symtab_sec != sec)
4979
            {
4980
              symtab_sec = sec;
4981
              if (symtab)
4982
                free (symtab);
4983
              symtab = GET_ELF_SYMBOLS (file, symtab_sec);
4984
            }
4985
 
4986
          if (symtab == NULL)
4987
            {
4988
              error (_("Corrupt header in group section `%s'\n"), name);
4989
              continue;
4990
            }
4991
 
4992
          sym = symtab + section->sh_info;
4993
 
4994
          if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4995
            {
4996
              if (sym->st_shndx == 0
4997
                  || sym->st_shndx >= elf_header.e_shnum)
4998
                {
4999
                  error (_("Bad sh_info in group section `%s'\n"), name);
5000
                  continue;
5001
                }
5002
 
5003
              group_name = SECTION_NAME (section_headers + sym->st_shndx);
5004
              strtab_sec = NULL;
5005
              if (strtab)
5006
                free (strtab);
5007
              strtab = NULL;
5008
              strtab_size = 0;
5009
            }
5010
          else
5011
            {
5012
              /* Get the string table.  */
5013
              if (symtab_sec->sh_link >= elf_header.e_shnum)
5014
                {
5015
                  strtab_sec = NULL;
5016
                  if (strtab)
5017
                    free (strtab);
5018
                  strtab = NULL;
5019
                  strtab_size = 0;
5020
                }
5021
              else if (strtab_sec
5022
                       != (sec = section_headers + symtab_sec->sh_link))
5023
                {
5024
                  strtab_sec = sec;
5025
                  if (strtab)
5026
                    free (strtab);
5027
                  strtab = (char *) get_data (NULL, file, strtab_sec->sh_offset,
5028
                                              1, strtab_sec->sh_size,
5029
                                              _("string table"));
5030
                  strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
5031
                }
5032
              group_name = sym->st_name < strtab_size
5033
                ? strtab + sym->st_name : _("<corrupt>");
5034
            }
5035
 
5036
          start = (unsigned char *) get_data (NULL, file, section->sh_offset,
5037
                                              1, section->sh_size,
5038
                                              _("section data"));
5039
 
5040
          indices = start;
5041
          size = (section->sh_size / section->sh_entsize) - 1;
5042
          entry = byte_get (indices, 4);
5043
          indices += 4;
5044
 
5045
          if (do_section_groups)
5046
            {
5047
              printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
5048
                      get_group_flags (entry), i, name, group_name, size);
5049
 
5050
              printf (_("   [Index]    Name\n"));
5051
            }
5052
 
5053
          group->group_index = i;
5054
 
5055
          for (j = 0; j < size; j++)
5056
            {
5057
              struct group_list * g;
5058
 
5059
              entry = byte_get (indices, 4);
5060
              indices += 4;
5061
 
5062
              if (entry >= elf_header.e_shnum)
5063
                {
5064
                  error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
5065
                         entry, i, elf_header.e_shnum - 1);
5066
                  continue;
5067
                }
5068
 
5069
              if (section_headers_groups [entry] != NULL)
5070
                {
5071
                  if (entry)
5072
                    {
5073
                      error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
5074
                             entry, i,
5075
                             section_headers_groups [entry]->group_index);
5076
                      continue;
5077
                    }
5078
                  else
5079
                    {
5080
                      /* Intel C/C++ compiler may put section 0 in a
5081
                         section group. We just warn it the first time
5082
                         and ignore it afterwards.  */
5083
                      static int warned = 0;
5084
                      if (!warned)
5085
                        {
5086
                          error (_("section 0 in group section [%5u]\n"),
5087
                                 section_headers_groups [entry]->group_index);
5088
                          warned++;
5089
                        }
5090
                    }
5091
                }
5092
 
5093
              section_headers_groups [entry] = group;
5094
 
5095
              if (do_section_groups)
5096
                {
5097
                  sec = section_headers + entry;
5098
                  printf ("   [%5u]   %s\n", entry, SECTION_NAME (sec));
5099
                }
5100
 
5101
              g = (struct group_list *) xmalloc (sizeof (struct group_list));
5102
              g->section_index = entry;
5103
              g->next = group->root;
5104
              group->root = g;
5105
            }
5106
 
5107
          if (start)
5108
            free (start);
5109
 
5110
          group++;
5111
        }
5112
    }
5113
 
5114
  if (symtab)
5115
    free (symtab);
5116
  if (strtab)
5117
    free (strtab);
5118
  return 1;
5119
}
5120
 
5121
/* Data used to display dynamic fixups.  */
5122
 
5123
struct ia64_vms_dynfixup
5124
{
5125
  bfd_vma needed_ident;         /* Library ident number.  */
5126
  bfd_vma needed;               /* Index in the dstrtab of the library name.  */
5127
  bfd_vma fixup_needed;         /* Index of the library.  */
5128
  bfd_vma fixup_rela_cnt;       /* Number of fixups.  */
5129
  bfd_vma fixup_rela_off;       /* Fixups offset in the dynamic segment.  */
5130
};
5131
 
5132
/* Data used to display dynamic relocations.  */
5133
 
5134
struct ia64_vms_dynimgrela
5135
{
5136
  bfd_vma img_rela_cnt;         /* Number of relocations.  */
5137
  bfd_vma img_rela_off;         /* Reloc offset in the dynamic segment.  */
5138
};
5139
 
5140
/* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
5141
   library).  */
5142
 
5143
static void
5144
dump_ia64_vms_dynamic_fixups (FILE *file, struct ia64_vms_dynfixup *fixup,
5145
                              const char *strtab, unsigned int strtab_sz)
5146
{
5147
  Elf64_External_VMS_IMAGE_FIXUP *imfs;
5148
  long i;
5149
  const char *lib_name;
5150
 
5151
  imfs = get_data (NULL, file, dynamic_addr + fixup->fixup_rela_off,
5152
                   1, fixup->fixup_rela_cnt * sizeof (*imfs),
5153
                   _("dynamic section image fixups"));
5154
  if (!imfs)
5155
    return;
5156
 
5157
  if (fixup->needed < strtab_sz)
5158
    lib_name = strtab + fixup->needed;
5159
  else
5160
    {
5161
      warn ("corrupt library name index of 0x%lx found in dynamic entry",
5162
            (unsigned long) fixup->needed);
5163
      lib_name = "???";
5164
    }
5165
  printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
5166
          (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
5167
  printf
5168
    (_("Seg Offset           Type                             SymVec DataType\n"));
5169
 
5170
  for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
5171
    {
5172
      unsigned int type;
5173
      const char *rtype;
5174
 
5175
      printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
5176
      printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
5177
      type = BYTE_GET (imfs [i].type);
5178
      rtype = elf_ia64_reloc_type (type);
5179
      if (rtype == NULL)
5180
        printf (" 0x%08x                       ", type);
5181
      else
5182
        printf (" %-32s ", rtype);
5183
      printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
5184
      printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
5185
    }
5186
 
5187
  free (imfs);
5188
}
5189
 
5190
/* Display IA-64 OpenVMS dynamic relocations (used to relocate an image).  */
5191
 
5192
static void
5193
dump_ia64_vms_dynamic_relocs (FILE *file, struct ia64_vms_dynimgrela *imgrela)
5194
{
5195
  Elf64_External_VMS_IMAGE_RELA *imrs;
5196
  long i;
5197
 
5198
  imrs = get_data (NULL, file, dynamic_addr + imgrela->img_rela_off,
5199
                   1, imgrela->img_rela_cnt * sizeof (*imrs),
5200
                   _("dynamic section image relas"));
5201
  if (!imrs)
5202
    return;
5203
 
5204
  printf (_("\nImage relocs\n"));
5205
  printf
5206
    (_("Seg Offset   Type                            Addend            Seg Sym Off\n"));
5207
 
5208
  for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
5209
    {
5210
      unsigned int type;
5211
      const char *rtype;
5212
 
5213
      printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
5214
      printf ("%08" BFD_VMA_FMT "x ",
5215
              (bfd_vma) BYTE_GET (imrs [i].rela_offset));
5216
      type = BYTE_GET (imrs [i].type);
5217
      rtype = elf_ia64_reloc_type (type);
5218
      if (rtype == NULL)
5219
        printf ("0x%08x                      ", type);
5220
      else
5221
        printf ("%-31s ", rtype);
5222
      print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
5223
      printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
5224
      printf ("%08" BFD_VMA_FMT "x\n",
5225
              (bfd_vma) BYTE_GET (imrs [i].sym_offset));
5226
    }
5227
 
5228
  free (imrs);
5229
}
5230
 
5231
/* Display IA-64 OpenVMS dynamic relocations and fixups.  */
5232
 
5233
static int
5234
process_ia64_vms_dynamic_relocs (FILE *file)
5235
{
5236
  struct ia64_vms_dynfixup fixup;
5237
  struct ia64_vms_dynimgrela imgrela;
5238
  Elf_Internal_Dyn *entry;
5239
  int res = 0;
5240
  bfd_vma strtab_off = 0;
5241
  bfd_vma strtab_sz = 0;
5242
  char *strtab = NULL;
5243
 
5244
  memset (&fixup, 0, sizeof (fixup));
5245
  memset (&imgrela, 0, sizeof (imgrela));
5246
 
5247
  /* Note: the order of the entries is specified by the OpenVMS specs.  */
5248
  for (entry = dynamic_section;
5249
       entry < dynamic_section + dynamic_nent;
5250
       entry++)
5251
    {
5252
      switch (entry->d_tag)
5253
        {
5254
        case DT_IA_64_VMS_STRTAB_OFFSET:
5255
          strtab_off = entry->d_un.d_val;
5256
          break;
5257
        case DT_STRSZ:
5258
          strtab_sz = entry->d_un.d_val;
5259
          if (strtab == NULL)
5260
            strtab = get_data (NULL, file, dynamic_addr + strtab_off,
5261
                               1, strtab_sz, _("dynamic string section"));
5262
          break;
5263
 
5264
        case DT_IA_64_VMS_NEEDED_IDENT:
5265
          fixup.needed_ident = entry->d_un.d_val;
5266
          break;
5267
        case DT_NEEDED:
5268
          fixup.needed = entry->d_un.d_val;
5269
          break;
5270
        case DT_IA_64_VMS_FIXUP_NEEDED:
5271
          fixup.fixup_needed = entry->d_un.d_val;
5272
          break;
5273
        case DT_IA_64_VMS_FIXUP_RELA_CNT:
5274
          fixup.fixup_rela_cnt = entry->d_un.d_val;
5275
          break;
5276
        case DT_IA_64_VMS_FIXUP_RELA_OFF:
5277
          fixup.fixup_rela_off = entry->d_un.d_val;
5278
          res++;
5279
          dump_ia64_vms_dynamic_fixups (file, &fixup, strtab, strtab_sz);
5280
          break;
5281
 
5282
        case DT_IA_64_VMS_IMG_RELA_CNT:
5283
          imgrela.img_rela_cnt = entry->d_un.d_val;
5284
          break;
5285
        case DT_IA_64_VMS_IMG_RELA_OFF:
5286
          imgrela.img_rela_off = entry->d_un.d_val;
5287
          res++;
5288
          dump_ia64_vms_dynamic_relocs (file, &imgrela);
5289
          break;
5290
 
5291
        default:
5292
          break;
5293
        }
5294
    }
5295
 
5296
  if (strtab != NULL)
5297
    free (strtab);
5298
 
5299
  return res;
5300
}
5301
 
5302
static struct
5303
{
5304
  const char * name;
5305
  int reloc;
5306
  int size;
5307
  int rela;
5308
} dynamic_relocations [] =
5309
{
5310
    { "REL", DT_REL, DT_RELSZ, FALSE },
5311
    { "RELA", DT_RELA, DT_RELASZ, TRUE },
5312
    { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
5313
};
5314
 
5315
/* Process the reloc section.  */
5316
 
5317
static int
5318
process_relocs (FILE * file)
5319
{
5320
  unsigned long rel_size;
5321
  unsigned long rel_offset;
5322
 
5323
 
5324
  if (!do_reloc)
5325
    return 1;
5326
 
5327
  if (do_using_dynamic)
5328
    {
5329
      int is_rela;
5330
      const char * name;
5331
      int has_dynamic_reloc;
5332
      unsigned int i;
5333
 
5334
      has_dynamic_reloc = 0;
5335
 
5336
      for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
5337
        {
5338
          is_rela = dynamic_relocations [i].rela;
5339
          name = dynamic_relocations [i].name;
5340
          rel_size = dynamic_info [dynamic_relocations [i].size];
5341
          rel_offset = dynamic_info [dynamic_relocations [i].reloc];
5342
 
5343
          has_dynamic_reloc |= rel_size;
5344
 
5345
          if (is_rela == UNKNOWN)
5346
            {
5347
              if (dynamic_relocations [i].reloc == DT_JMPREL)
5348
                switch (dynamic_info[DT_PLTREL])
5349
                  {
5350
                  case DT_REL:
5351
                    is_rela = FALSE;
5352
                    break;
5353
                  case DT_RELA:
5354
                    is_rela = TRUE;
5355
                    break;
5356
                  }
5357
            }
5358
 
5359
          if (rel_size)
5360
            {
5361
              printf
5362
                (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
5363
                 name, rel_offset, rel_size);
5364
 
5365
              dump_relocations (file,
5366
                                offset_from_vma (file, rel_offset, rel_size),
5367
                                rel_size,
5368
                                dynamic_symbols, num_dynamic_syms,
5369
                                dynamic_strings, dynamic_strings_length, is_rela);
5370
            }
5371
        }
5372
 
5373
      if (is_ia64_vms ())
5374
        has_dynamic_reloc |= process_ia64_vms_dynamic_relocs (file);
5375
 
5376
      if (! has_dynamic_reloc)
5377
        printf (_("\nThere are no dynamic relocations in this file.\n"));
5378
    }
5379
  else
5380
    {
5381
      Elf_Internal_Shdr * section;
5382
      unsigned long i;
5383
      int found = 0;
5384
 
5385
      for (i = 0, section = section_headers;
5386
           i < elf_header.e_shnum;
5387
           i++, section++)
5388
        {
5389
          if (   section->sh_type != SHT_RELA
5390
              && section->sh_type != SHT_REL)
5391
            continue;
5392
 
5393
          rel_offset = section->sh_offset;
5394
          rel_size   = section->sh_size;
5395
 
5396
          if (rel_size)
5397
            {
5398
              Elf_Internal_Shdr * strsec;
5399
              int is_rela;
5400
 
5401
              printf (_("\nRelocation section "));
5402
 
5403
              if (string_table == NULL)
5404
                printf ("%d", section->sh_name);
5405
              else
5406
                printf (_("'%s'"), SECTION_NAME (section));
5407
 
5408
              printf (_(" at offset 0x%lx contains %lu entries:\n"),
5409
                 rel_offset, (unsigned long) (rel_size / section->sh_entsize));
5410
 
5411
              is_rela = section->sh_type == SHT_RELA;
5412
 
5413
              if (section->sh_link != 0
5414
                  && section->sh_link < elf_header.e_shnum)
5415
                {
5416
                  Elf_Internal_Shdr * symsec;
5417
                  Elf_Internal_Sym *  symtab;
5418
                  unsigned long nsyms;
5419
                  unsigned long strtablen = 0;
5420
                  char * strtab = NULL;
5421
 
5422
                  symsec = section_headers + section->sh_link;
5423
                  if (symsec->sh_type != SHT_SYMTAB
5424
                      && symsec->sh_type != SHT_DYNSYM)
5425
                    continue;
5426
 
5427
                  nsyms = symsec->sh_size / symsec->sh_entsize;
5428
                  symtab = GET_ELF_SYMBOLS (file, symsec);
5429
 
5430
                  if (symtab == NULL)
5431
                    continue;
5432
 
5433
                  if (symsec->sh_link != 0
5434
                      && symsec->sh_link < elf_header.e_shnum)
5435
                    {
5436
                      strsec = section_headers + symsec->sh_link;
5437
 
5438
                      strtab = (char *) get_data (NULL, file, strsec->sh_offset,
5439
                                                  1, strsec->sh_size,
5440
                                                  _("string table"));
5441
                      strtablen = strtab == NULL ? 0 : strsec->sh_size;
5442
                    }
5443
 
5444
                  dump_relocations (file, rel_offset, rel_size,
5445
                                    symtab, nsyms, strtab, strtablen, is_rela);
5446
                  if (strtab)
5447
                    free (strtab);
5448
                  free (symtab);
5449
                }
5450
              else
5451
                dump_relocations (file, rel_offset, rel_size,
5452
                                  NULL, 0, NULL, 0, is_rela);
5453
 
5454
              found = 1;
5455
            }
5456
        }
5457
 
5458
      if (! found)
5459
        printf (_("\nThere are no relocations in this file.\n"));
5460
    }
5461
 
5462
  return 1;
5463
}
5464
 
5465
/* Process the unwind section.  */
5466
 
5467
#include "unwind-ia64.h"
5468
 
5469
/* An absolute address consists of a section and an offset.  If the
5470
   section is NULL, the offset itself is the address, otherwise, the
5471
   address equals to LOAD_ADDRESS(section) + offset.  */
5472
 
5473
struct absaddr
5474
  {
5475
    unsigned short section;
5476
    bfd_vma offset;
5477
  };
5478
 
5479
#define ABSADDR(a) \
5480
  ((a).section \
5481
   ? section_headers [(a).section].sh_addr + (a).offset \
5482
   : (a).offset)
5483
 
5484
struct ia64_unw_table_entry
5485
  {
5486
    struct absaddr start;
5487
    struct absaddr end;
5488
    struct absaddr info;
5489
  };
5490
 
5491
struct ia64_unw_aux_info
5492
  {
5493
 
5494
    struct ia64_unw_table_entry *table; /* Unwind table.  */
5495
    unsigned long table_len;    /* Length of unwind table.  */
5496
    unsigned char * info;       /* Unwind info.  */
5497
    unsigned long info_size;    /* Size of unwind info.  */
5498
    bfd_vma info_addr;          /* starting address of unwind info.  */
5499
    bfd_vma seg_base;           /* Starting address of segment.  */
5500
    Elf_Internal_Sym * symtab;  /* The symbol table.  */
5501
    unsigned long nsyms;        /* Number of symbols.  */
5502
    char * strtab;              /* The string table.  */
5503
    unsigned long strtab_size;  /* Size of string table.  */
5504
  };
5505
 
5506
static void
5507
find_symbol_for_address (Elf_Internal_Sym * symtab,
5508
                         unsigned long nsyms,
5509
                         const char * strtab,
5510
                         unsigned long strtab_size,
5511
                         struct absaddr addr,
5512
                         const char ** symname,
5513
                         bfd_vma * offset)
5514
{
5515
  bfd_vma dist = 0x100000;
5516
  Elf_Internal_Sym * sym;
5517
  Elf_Internal_Sym * best = NULL;
5518
  unsigned long i;
5519
 
5520
  REMOVE_ARCH_BITS (addr.offset);
5521
 
5522
  for (i = 0, sym = symtab; i < nsyms; ++i, ++sym)
5523
    {
5524
      bfd_vma value = sym->st_value;
5525
 
5526
      REMOVE_ARCH_BITS (value);
5527
 
5528
      if (ELF_ST_TYPE (sym->st_info) == STT_FUNC
5529
          && sym->st_name != 0
5530
          && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
5531
          && addr.offset >= value
5532
          && addr.offset - value < dist)
5533
        {
5534
          best = sym;
5535
          dist = addr.offset - value;
5536
          if (!dist)
5537
            break;
5538
        }
5539
    }
5540
  if (best)
5541
    {
5542
      *symname = (best->st_name >= strtab_size
5543
                  ? _("<corrupt>") : strtab + best->st_name);
5544
      *offset = dist;
5545
      return;
5546
    }
5547
  *symname = NULL;
5548
  *offset = addr.offset;
5549
}
5550
 
5551
static void
5552
dump_ia64_unwind (struct ia64_unw_aux_info * aux)
5553
{
5554
  struct ia64_unw_table_entry * tp;
5555
  int in_body;
5556
 
5557
  for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
5558
    {
5559
      bfd_vma stamp;
5560
      bfd_vma offset;
5561
      const unsigned char * dp;
5562
      const unsigned char * head;
5563
      const char * procname;
5564
 
5565
      find_symbol_for_address (aux->symtab, aux->nsyms, aux->strtab,
5566
                               aux->strtab_size, tp->start, &procname, &offset);
5567
 
5568
      fputs ("\n<", stdout);
5569
 
5570
      if (procname)
5571
        {
5572
          fputs (procname, stdout);
5573
 
5574
          if (offset)
5575
            printf ("+%lx", (unsigned long) offset);
5576
        }
5577
 
5578
      fputs (">: [", stdout);
5579
      print_vma (tp->start.offset, PREFIX_HEX);
5580
      fputc ('-', stdout);
5581
      print_vma (tp->end.offset, PREFIX_HEX);
5582
      printf ("], info at +0x%lx\n",
5583
              (unsigned long) (tp->info.offset - aux->seg_base));
5584
 
5585
      head = aux->info + (ABSADDR (tp->info) - aux->info_addr);
5586
      stamp = byte_get ((unsigned char *) head, sizeof (stamp));
5587
 
5588
      printf ("  v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
5589
              (unsigned) UNW_VER (stamp),
5590
              (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
5591
              UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
5592
              UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
5593
              (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
5594
 
5595
      if (UNW_VER (stamp) != 1)
5596
        {
5597
          printf (_("\tUnknown version.\n"));
5598
          continue;
5599
        }
5600
 
5601
      in_body = 0;
5602
      for (dp = head + 8; dp < head + 8 + eh_addr_size * UNW_LENGTH (stamp);)
5603
        dp = unw_decode (dp, in_body, & in_body);
5604
    }
5605
}
5606
 
5607
static int
5608
slurp_ia64_unwind_table (FILE * file,
5609
                         struct ia64_unw_aux_info * aux,
5610
                         Elf_Internal_Shdr * sec)
5611
{
5612
  unsigned long size, nrelas, i;
5613
  Elf_Internal_Phdr * seg;
5614
  struct ia64_unw_table_entry * tep;
5615
  Elf_Internal_Shdr * relsec;
5616
  Elf_Internal_Rela * rela;
5617
  Elf_Internal_Rela * rp;
5618
  unsigned char * table;
5619
  unsigned char * tp;
5620
  Elf_Internal_Sym * sym;
5621
  const char * relname;
5622
 
5623
  /* First, find the starting address of the segment that includes
5624
     this section: */
5625
 
5626
  if (elf_header.e_phnum)
5627
    {
5628
      if (! get_program_headers (file))
5629
          return 0;
5630
 
5631
      for (seg = program_headers;
5632
           seg < program_headers + elf_header.e_phnum;
5633
           ++seg)
5634
        {
5635
          if (seg->p_type != PT_LOAD)
5636
            continue;
5637
 
5638
          if (sec->sh_addr >= seg->p_vaddr
5639
              && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
5640
            {
5641
              aux->seg_base = seg->p_vaddr;
5642
              break;
5643
            }
5644
        }
5645
    }
5646
 
5647
  /* Second, build the unwind table from the contents of the unwind section:  */
5648
  size = sec->sh_size;
5649
  table = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1, size,
5650
                                      _("unwind table"));
5651
  if (!table)
5652
    return 0;
5653
 
5654
  aux->table = (struct ia64_unw_table_entry *)
5655
      xcmalloc (size / (3 * eh_addr_size), sizeof (aux->table[0]));
5656
  tep = aux->table;
5657
  for (tp = table; tp < table + size; ++tep)
5658
    {
5659
      tep->start.section = SHN_UNDEF;
5660
      tep->end.section   = SHN_UNDEF;
5661
      tep->info.section  = SHN_UNDEF;
5662
      tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
5663
      tep->end.offset   = byte_get (tp, eh_addr_size); tp += eh_addr_size;
5664
      tep->info.offset  = byte_get (tp, eh_addr_size); tp += eh_addr_size;
5665
      tep->start.offset += aux->seg_base;
5666
      tep->end.offset   += aux->seg_base;
5667
      tep->info.offset  += aux->seg_base;
5668
    }
5669
  free (table);
5670
 
5671
  /* Third, apply any relocations to the unwind table:  */
5672
  for (relsec = section_headers;
5673
       relsec < section_headers + elf_header.e_shnum;
5674
       ++relsec)
5675
    {
5676
      if (relsec->sh_type != SHT_RELA
5677
          || relsec->sh_info >= elf_header.e_shnum
5678
          || section_headers + relsec->sh_info != sec)
5679
        continue;
5680
 
5681
      if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
5682
                              & rela, & nrelas))
5683
        return 0;
5684
 
5685
      for (rp = rela; rp < rela + nrelas; ++rp)
5686
        {
5687
          relname = elf_ia64_reloc_type (get_reloc_type (rp->r_info));
5688
          sym = aux->symtab + get_reloc_symindex (rp->r_info);
5689
 
5690
          if (! const_strneq (relname, "R_IA64_SEGREL"))
5691
            {
5692
              warn (_("Skipping unexpected relocation type %s\n"), relname);
5693
              continue;
5694
            }
5695
 
5696
          i = rp->r_offset / (3 * eh_addr_size);
5697
 
5698
          switch (rp->r_offset/eh_addr_size % 3)
5699
            {
5700
            case 0:
5701
              aux->table[i].start.section = sym->st_shndx;
5702
              aux->table[i].start.offset  = rp->r_addend + sym->st_value;
5703
              break;
5704
            case 1:
5705
              aux->table[i].end.section   = sym->st_shndx;
5706
              aux->table[i].end.offset    = rp->r_addend + sym->st_value;
5707
              break;
5708
            case 2:
5709
              aux->table[i].info.section  = sym->st_shndx;
5710
              aux->table[i].info.offset   = rp->r_addend + sym->st_value;
5711
              break;
5712
            default:
5713
              break;
5714
            }
5715
        }
5716
 
5717
      free (rela);
5718
    }
5719
 
5720
  aux->table_len = size / (3 * eh_addr_size);
5721
  return 1;
5722
}
5723
 
5724
static int
5725
ia64_process_unwind (FILE * file)
5726
{
5727
  Elf_Internal_Shdr * sec;
5728
  Elf_Internal_Shdr * unwsec = NULL;
5729
  Elf_Internal_Shdr * strsec;
5730
  unsigned long i, unwcount = 0, unwstart = 0;
5731
  struct ia64_unw_aux_info aux;
5732
 
5733
  memset (& aux, 0, sizeof (aux));
5734
 
5735
  for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
5736
    {
5737
      if (sec->sh_type == SHT_SYMTAB
5738
          && sec->sh_link < elf_header.e_shnum)
5739
        {
5740
          aux.nsyms = sec->sh_size / sec->sh_entsize;
5741
          aux.symtab = GET_ELF_SYMBOLS (file, sec);
5742
 
5743
          strsec = section_headers + sec->sh_link;
5744
          aux.strtab = (char *) get_data (NULL, file, strsec->sh_offset,
5745
                                          1, strsec->sh_size,
5746
                                          _("string table"));
5747
          aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
5748
        }
5749
      else if (sec->sh_type == SHT_IA_64_UNWIND)
5750
        unwcount++;
5751
    }
5752
 
5753
  if (!unwcount)
5754
    printf (_("\nThere are no unwind sections in this file.\n"));
5755
 
5756
  while (unwcount-- > 0)
5757
    {
5758
      char * suffix;
5759
      size_t len, len2;
5760
 
5761
      for (i = unwstart, sec = section_headers + unwstart;
5762
           i < elf_header.e_shnum; ++i, ++sec)
5763
        if (sec->sh_type == SHT_IA_64_UNWIND)
5764
          {
5765
            unwsec = sec;
5766
            break;
5767
          }
5768
 
5769
      unwstart = i + 1;
5770
      len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
5771
 
5772
      if ((unwsec->sh_flags & SHF_GROUP) != 0)
5773
        {
5774
          /* We need to find which section group it is in.  */
5775
          struct group_list * g = section_headers_groups [i]->root;
5776
 
5777
          for (; g != NULL; g = g->next)
5778
            {
5779
              sec = section_headers + g->section_index;
5780
 
5781
              if (streq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info))
5782
                break;
5783
            }
5784
 
5785
          if (g == NULL)
5786
            i = elf_header.e_shnum;
5787
        }
5788
      else if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind_once, len))
5789
        {
5790
          /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO.  */
5791
          len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
5792
          suffix = SECTION_NAME (unwsec) + len;
5793
          for (i = 0, sec = section_headers; i < elf_header.e_shnum;
5794
               ++i, ++sec)
5795
            if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info_once, len2)
5796
                && streq (SECTION_NAME (sec) + len2, suffix))
5797
              break;
5798
        }
5799
      else
5800
        {
5801
          /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
5802
             .IA_64.unwind or BAR -> .IA_64.unwind_info.  */
5803
          len = sizeof (ELF_STRING_ia64_unwind) - 1;
5804
          len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
5805
          suffix = "";
5806
          if (strneq (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind, len))
5807
            suffix = SECTION_NAME (unwsec) + len;
5808
          for (i = 0, sec = section_headers; i < elf_header.e_shnum;
5809
               ++i, ++sec)
5810
            if (strneq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info, len2)
5811
                && streq (SECTION_NAME (sec) + len2, suffix))
5812
              break;
5813
        }
5814
 
5815
      if (i == elf_header.e_shnum)
5816
        {
5817
          printf (_("\nCould not find unwind info section for "));
5818
 
5819
          if (string_table == NULL)
5820
            printf ("%d", unwsec->sh_name);
5821
          else
5822
            printf (_("'%s'"), SECTION_NAME (unwsec));
5823
        }
5824
      else
5825
        {
5826
          aux.info_size = sec->sh_size;
5827
          aux.info_addr = sec->sh_addr;
5828
          aux.info = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1,
5829
                                                 aux.info_size,
5830
                                                 _("unwind info"));
5831
 
5832
          printf (_("\nUnwind section "));
5833
 
5834
          if (string_table == NULL)
5835
            printf ("%d", unwsec->sh_name);
5836
          else
5837
            printf (_("'%s'"), SECTION_NAME (unwsec));
5838
 
5839
          printf (_(" at offset 0x%lx contains %lu entries:\n"),
5840
                  (unsigned long) unwsec->sh_offset,
5841
                  (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
5842
 
5843
          (void) slurp_ia64_unwind_table (file, & aux, unwsec);
5844
 
5845
          if (aux.table_len > 0)
5846
            dump_ia64_unwind (& aux);
5847
 
5848
          if (aux.table)
5849
            free ((char *) aux.table);
5850
          if (aux.info)
5851
            free ((char *) aux.info);
5852
          aux.table = NULL;
5853
          aux.info = NULL;
5854
        }
5855
    }
5856
 
5857
  if (aux.symtab)
5858
    free (aux.symtab);
5859
  if (aux.strtab)
5860
    free ((char *) aux.strtab);
5861
 
5862
  return 1;
5863
}
5864
 
5865
struct hppa_unw_table_entry
5866
  {
5867
    struct absaddr start;
5868
    struct absaddr end;
5869
    unsigned int Cannot_unwind:1;                       /* 0 */
5870
    unsigned int Millicode:1;                   /* 1 */
5871
    unsigned int Millicode_save_sr0:1;          /* 2 */
5872
    unsigned int Region_description:2;          /* 3..4 */
5873
    unsigned int reserved1:1;                   /* 5 */
5874
    unsigned int Entry_SR:1;                    /* 6 */
5875
    unsigned int Entry_FR:4;     /* number saved */     /* 7..10 */
5876
    unsigned int Entry_GR:5;     /* number saved */     /* 11..15 */
5877
    unsigned int Args_stored:1;                 /* 16 */
5878
    unsigned int Variable_Frame:1;                      /* 17 */
5879
    unsigned int Separate_Package_Body:1;               /* 18 */
5880
    unsigned int Frame_Extension_Millicode:1;   /* 19 */
5881
    unsigned int Stack_Overflow_Check:1;                /* 20 */
5882
    unsigned int Two_Instruction_SP_Increment:1;        /* 21 */
5883
    unsigned int Ada_Region:1;                  /* 22 */
5884
    unsigned int cxx_info:1;                    /* 23 */
5885
    unsigned int cxx_try_catch:1;                       /* 24 */
5886
    unsigned int sched_entry_seq:1;                     /* 25 */
5887
    unsigned int reserved2:1;                   /* 26 */
5888
    unsigned int Save_SP:1;                             /* 27 */
5889
    unsigned int Save_RP:1;                             /* 28 */
5890
    unsigned int Save_MRP_in_frame:1;           /* 29 */
5891
    unsigned int extn_ptr_defined:1;            /* 30 */
5892
    unsigned int Cleanup_defined:1;                     /* 31 */
5893
 
5894
    unsigned int MPE_XL_interrupt_marker:1;             /* 0 */
5895
    unsigned int HP_UX_interrupt_marker:1;              /* 1 */
5896
    unsigned int Large_frame:1;                 /* 2 */
5897
    unsigned int Pseudo_SP_Set:1;                       /* 3 */
5898
    unsigned int reserved4:1;                   /* 4 */
5899
    unsigned int Total_frame_size:27;           /* 5..31 */
5900
  };
5901
 
5902
struct hppa_unw_aux_info
5903
  {
5904
    struct hppa_unw_table_entry *table; /* Unwind table.  */
5905
    unsigned long table_len;    /* Length of unwind table.  */
5906
    bfd_vma seg_base;           /* Starting address of segment.  */
5907
    Elf_Internal_Sym * symtab;  /* The symbol table.  */
5908
    unsigned long nsyms;        /* Number of symbols.  */
5909
    char * strtab;              /* The string table.  */
5910
    unsigned long strtab_size;  /* Size of string table.  */
5911
  };
5912
 
5913
static void
5914
dump_hppa_unwind (struct hppa_unw_aux_info * aux)
5915
{
5916
  struct hppa_unw_table_entry * tp;
5917
 
5918
  for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
5919
    {
5920
      bfd_vma offset;
5921
      const char * procname;
5922
 
5923
      find_symbol_for_address (aux->symtab, aux->nsyms, aux->strtab,
5924
                               aux->strtab_size, tp->start, &procname,
5925
                               &offset);
5926
 
5927
      fputs ("\n<", stdout);
5928
 
5929
      if (procname)
5930
        {
5931
          fputs (procname, stdout);
5932
 
5933
          if (offset)
5934
            printf ("+%lx", (unsigned long) offset);
5935
        }
5936
 
5937
      fputs (">: [", stdout);
5938
      print_vma (tp->start.offset, PREFIX_HEX);
5939
      fputc ('-', stdout);
5940
      print_vma (tp->end.offset, PREFIX_HEX);
5941
      printf ("]\n\t");
5942
 
5943
#define PF(_m) if (tp->_m) printf (#_m " ");
5944
#define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
5945
      PF(Cannot_unwind);
5946
      PF(Millicode);
5947
      PF(Millicode_save_sr0);
5948
      /* PV(Region_description);  */
5949
      PF(Entry_SR);
5950
      PV(Entry_FR);
5951
      PV(Entry_GR);
5952
      PF(Args_stored);
5953
      PF(Variable_Frame);
5954
      PF(Separate_Package_Body);
5955
      PF(Frame_Extension_Millicode);
5956
      PF(Stack_Overflow_Check);
5957
      PF(Two_Instruction_SP_Increment);
5958
      PF(Ada_Region);
5959
      PF(cxx_info);
5960
      PF(cxx_try_catch);
5961
      PF(sched_entry_seq);
5962
      PF(Save_SP);
5963
      PF(Save_RP);
5964
      PF(Save_MRP_in_frame);
5965
      PF(extn_ptr_defined);
5966
      PF(Cleanup_defined);
5967
      PF(MPE_XL_interrupt_marker);
5968
      PF(HP_UX_interrupt_marker);
5969
      PF(Large_frame);
5970
      PF(Pseudo_SP_Set);
5971
      PV(Total_frame_size);
5972
#undef PF
5973
#undef PV
5974
    }
5975
 
5976
  printf ("\n");
5977
}
5978
 
5979
static int
5980
slurp_hppa_unwind_table (FILE * file,
5981
                         struct hppa_unw_aux_info * aux,
5982
                         Elf_Internal_Shdr * sec)
5983
{
5984
  unsigned long size, unw_ent_size, nentries, nrelas, i;
5985
  Elf_Internal_Phdr * seg;
5986
  struct hppa_unw_table_entry * tep;
5987
  Elf_Internal_Shdr * relsec;
5988
  Elf_Internal_Rela * rela;
5989
  Elf_Internal_Rela * rp;
5990
  unsigned char * table;
5991
  unsigned char * tp;
5992
  Elf_Internal_Sym * sym;
5993
  const char * relname;
5994
 
5995
  /* First, find the starting address of the segment that includes
5996
     this section.  */
5997
 
5998
  if (elf_header.e_phnum)
5999
    {
6000
      if (! get_program_headers (file))
6001
        return 0;
6002
 
6003
      for (seg = program_headers;
6004
           seg < program_headers + elf_header.e_phnum;
6005
           ++seg)
6006
        {
6007
          if (seg->p_type != PT_LOAD)
6008
            continue;
6009
 
6010
          if (sec->sh_addr >= seg->p_vaddr
6011
              && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
6012
            {
6013
              aux->seg_base = seg->p_vaddr;
6014
              break;
6015
            }
6016
        }
6017
    }
6018
 
6019
  /* Second, build the unwind table from the contents of the unwind
6020
     section.  */
6021
  size = sec->sh_size;
6022
  table = (unsigned char *) get_data (NULL, file, sec->sh_offset, 1, size,
6023
                                      _("unwind table"));
6024
  if (!table)
6025
    return 0;
6026
 
6027
  unw_ent_size = 16;
6028
  nentries = size / unw_ent_size;
6029
  size = unw_ent_size * nentries;
6030
 
6031
  tep = aux->table = (struct hppa_unw_table_entry *)
6032
      xcmalloc (nentries, sizeof (aux->table[0]));
6033
 
6034
  for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
6035
    {
6036
      unsigned int tmp1, tmp2;
6037
 
6038
      tep->start.section = SHN_UNDEF;
6039
      tep->end.section   = SHN_UNDEF;
6040
 
6041
      tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
6042
      tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
6043
      tmp1 = byte_get ((unsigned char *) tp + 8, 4);
6044
      tmp2 = byte_get ((unsigned char *) tp + 12, 4);
6045
 
6046
      tep->start.offset += aux->seg_base;
6047
      tep->end.offset   += aux->seg_base;
6048
 
6049
      tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
6050
      tep->Millicode = (tmp1 >> 30) & 0x1;
6051
      tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
6052
      tep->Region_description = (tmp1 >> 27) & 0x3;
6053
      tep->reserved1 = (tmp1 >> 26) & 0x1;
6054
      tep->Entry_SR = (tmp1 >> 25) & 0x1;
6055
      tep->Entry_FR = (tmp1 >> 21) & 0xf;
6056
      tep->Entry_GR = (tmp1 >> 16) & 0x1f;
6057
      tep->Args_stored = (tmp1 >> 15) & 0x1;
6058
      tep->Variable_Frame = (tmp1 >> 14) & 0x1;
6059
      tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
6060
      tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
6061
      tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
6062
      tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
6063
      tep->Ada_Region = (tmp1 >> 9) & 0x1;
6064
      tep->cxx_info = (tmp1 >> 8) & 0x1;
6065
      tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
6066
      tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
6067
      tep->reserved2 = (tmp1 >> 5) & 0x1;
6068
      tep->Save_SP = (tmp1 >> 4) & 0x1;
6069
      tep->Save_RP = (tmp1 >> 3) & 0x1;
6070
      tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
6071
      tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
6072
      tep->Cleanup_defined = tmp1 & 0x1;
6073
 
6074
      tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
6075
      tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
6076
      tep->Large_frame = (tmp2 >> 29) & 0x1;
6077
      tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
6078
      tep->reserved4 = (tmp2 >> 27) & 0x1;
6079
      tep->Total_frame_size = tmp2 & 0x7ffffff;
6080
    }
6081
  free (table);
6082
 
6083
  /* Third, apply any relocations to the unwind table.  */
6084
  for (relsec = section_headers;
6085
       relsec < section_headers + elf_header.e_shnum;
6086
       ++relsec)
6087
    {
6088
      if (relsec->sh_type != SHT_RELA
6089
          || relsec->sh_info >= elf_header.e_shnum
6090
          || section_headers + relsec->sh_info != sec)
6091
        continue;
6092
 
6093
      if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
6094
                              & rela, & nrelas))
6095
        return 0;
6096
 
6097
      for (rp = rela; rp < rela + nrelas; ++rp)
6098
        {
6099
          relname = elf_hppa_reloc_type (get_reloc_type (rp->r_info));
6100
          sym = aux->symtab + get_reloc_symindex (rp->r_info);
6101
 
6102
          /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64.  */
6103
          if (! const_strneq (relname, "R_PARISC_SEGREL"))
6104
            {
6105
              warn (_("Skipping unexpected relocation type %s\n"), relname);
6106
              continue;
6107
            }
6108
 
6109
          i = rp->r_offset / unw_ent_size;
6110
 
6111
          switch ((rp->r_offset % unw_ent_size) / eh_addr_size)
6112
            {
6113
            case 0:
6114
              aux->table[i].start.section = sym->st_shndx;
6115
              aux->table[i].start.offset  = sym->st_value + rp->r_addend;
6116
              break;
6117
            case 1:
6118
              aux->table[i].end.section   = sym->st_shndx;
6119
              aux->table[i].end.offset    = sym->st_value + rp->r_addend;
6120
              break;
6121
            default:
6122
              break;
6123
            }
6124
        }
6125
 
6126
      free (rela);
6127
    }
6128
 
6129
  aux->table_len = nentries;
6130
 
6131
  return 1;
6132
}
6133
 
6134
static int
6135
hppa_process_unwind (FILE * file)
6136
{
6137
  struct hppa_unw_aux_info aux;
6138
  Elf_Internal_Shdr * unwsec = NULL;
6139
  Elf_Internal_Shdr * strsec;
6140
  Elf_Internal_Shdr * sec;
6141
  unsigned long i;
6142
 
6143
  memset (& aux, 0, sizeof (aux));
6144
 
6145
  if (string_table == NULL)
6146
    return 1;
6147
 
6148
  for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
6149
    {
6150
      if (sec->sh_type == SHT_SYMTAB
6151
          && sec->sh_link < elf_header.e_shnum)
6152
        {
6153
          aux.nsyms = sec->sh_size / sec->sh_entsize;
6154
          aux.symtab = GET_ELF_SYMBOLS (file, sec);
6155
 
6156
          strsec = section_headers + sec->sh_link;
6157
          aux.strtab = (char *) get_data (NULL, file, strsec->sh_offset,
6158
                                          1, strsec->sh_size,
6159
                                          _("string table"));
6160
          aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
6161
        }
6162
      else if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
6163
        unwsec = sec;
6164
    }
6165
 
6166
  if (!unwsec)
6167
    printf (_("\nThere are no unwind sections in this file.\n"));
6168
 
6169
  for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
6170
    {
6171
      if (streq (SECTION_NAME (sec), ".PARISC.unwind"))
6172
        {
6173
          printf (_("\nUnwind section "));
6174
          printf (_("'%s'"), SECTION_NAME (sec));
6175
 
6176
          printf (_(" at offset 0x%lx contains %lu entries:\n"),
6177
                  (unsigned long) sec->sh_offset,
6178
                  (unsigned long) (sec->sh_size / (2 * eh_addr_size + 8)));
6179
 
6180
          slurp_hppa_unwind_table (file, &aux, sec);
6181
          if (aux.table_len > 0)
6182
            dump_hppa_unwind (&aux);
6183
 
6184
          if (aux.table)
6185
            free ((char *) aux.table);
6186
          aux.table = NULL;
6187
        }
6188
    }
6189
 
6190
  if (aux.symtab)
6191
    free (aux.symtab);
6192
  if (aux.strtab)
6193
    free ((char *) aux.strtab);
6194
 
6195
  return 1;
6196
}
6197
 
6198
struct arm_section
6199
{
6200
  unsigned char *data;
6201
 
6202
  Elf_Internal_Shdr *sec;
6203
  Elf_Internal_Rela *rela;
6204
  unsigned long nrelas;
6205
  unsigned int rel_type;
6206
 
6207
  Elf_Internal_Rela *next_rela;
6208
};
6209
 
6210
struct arm_unw_aux_info
6211
{
6212
  FILE *file;
6213
 
6214
  Elf_Internal_Sym *symtab;     /* The symbol table.  */
6215
  unsigned long nsyms;          /* Number of symbols.  */
6216
  char *strtab;                 /* The string table.  */
6217
  unsigned long strtab_size;    /* Size of string table.  */
6218
};
6219
 
6220
static const char *
6221
arm_print_vma_and_name (struct arm_unw_aux_info *aux,
6222
                        bfd_vma fn, struct absaddr addr)
6223
{
6224
  const char *procname;
6225
  bfd_vma sym_offset;
6226
 
6227
  if (addr.section == SHN_UNDEF)
6228
    addr.offset = fn;
6229
 
6230
  find_symbol_for_address (aux->symtab, aux->nsyms, aux->strtab,
6231
                           aux->strtab_size, addr, &procname,
6232
                           &sym_offset);
6233
 
6234
  print_vma (fn, PREFIX_HEX);
6235
 
6236
  if (procname)
6237
    {
6238
      fputs (" <", stdout);
6239
      fputs (procname, stdout);
6240
 
6241
      if (sym_offset)
6242
        printf ("+0x%lx", (unsigned long) sym_offset);
6243
      fputc ('>', stdout);
6244
    }
6245
 
6246
  return procname;
6247
}
6248
 
6249
static void
6250
arm_free_section (struct arm_section *arm_sec)
6251
{
6252
  if (arm_sec->data != NULL)
6253
    free (arm_sec->data);
6254
 
6255
  if (arm_sec->rela != NULL)
6256
    free (arm_sec->rela);
6257
}
6258
 
6259
static int
6260
arm_section_get_word (struct arm_unw_aux_info *aux,
6261
                      struct arm_section *arm_sec,
6262
                      Elf_Internal_Shdr *sec, bfd_vma word_offset,
6263
                      unsigned int *wordp, struct absaddr *addr)
6264
{
6265
  Elf_Internal_Rela *rp;
6266
  Elf_Internal_Sym *sym;
6267
  const char * relname;
6268
  unsigned int word;
6269
  bfd_boolean wrapped;
6270
 
6271
  addr->section = SHN_UNDEF;
6272
  addr->offset = 0;
6273
 
6274
  if (sec != arm_sec->sec)
6275
    {
6276
      Elf_Internal_Shdr *relsec;
6277
 
6278
      arm_free_section (arm_sec);
6279
 
6280
      arm_sec->sec = sec;
6281
      arm_sec->data = get_data (NULL, aux->file, sec->sh_offset, 1,
6282
                                sec->sh_size, _("unwind data"));
6283
 
6284
      arm_sec->rela = NULL;
6285
      arm_sec->nrelas = 0;
6286
 
6287
      for (relsec = section_headers;
6288
           relsec < section_headers + elf_header.e_shnum;
6289
           ++relsec)
6290
        {
6291
          if (relsec->sh_info >= elf_header.e_shnum
6292
              || section_headers + relsec->sh_info != sec)
6293
            continue;
6294
 
6295
          if (relsec->sh_type == SHT_REL)
6296
            {
6297
              if (!slurp_rel_relocs (aux->file, relsec->sh_offset,
6298
                                     relsec->sh_size,
6299
                                     & arm_sec->rela, & arm_sec->nrelas))
6300
                return 0;
6301
              break;
6302
            }
6303
          else if (relsec->sh_type == SHT_RELA)
6304
            {
6305
              if (!slurp_rela_relocs (aux->file, relsec->sh_offset,
6306
                                      relsec->sh_size,
6307
                                      & arm_sec->rela, & arm_sec->nrelas))
6308
                return 0;
6309
              break;
6310
            }
6311
        }
6312
 
6313
      arm_sec->next_rela = arm_sec->rela;
6314
    }
6315
 
6316
  if (arm_sec->data == NULL)
6317
    return 0;
6318
 
6319
  word = byte_get (arm_sec->data + word_offset, 4);
6320
 
6321
  wrapped = FALSE;
6322
  for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
6323
    {
6324
      bfd_vma prelval, offset;
6325
 
6326
      if (rp->r_offset > word_offset && !wrapped)
6327
        {
6328
          rp = arm_sec->rela;
6329
          wrapped = TRUE;
6330
        }
6331
      if (rp->r_offset > word_offset)
6332
        break;
6333
 
6334
      if (rp->r_offset & 3)
6335
        {
6336
          warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
6337
                (unsigned long) rp->r_offset);
6338
          continue;
6339
        }
6340
 
6341
      if (rp->r_offset < word_offset)
6342
        continue;
6343
 
6344
      switch (elf_header.e_machine)
6345
        {
6346
        case EM_ARM:
6347
          relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
6348
          break;
6349
 
6350
        case EM_TI_C6000:
6351
          relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
6352
          break;
6353
 
6354
        default:
6355
            abort();
6356
        }
6357
 
6358
      if (streq (relname, "R_ARM_NONE")
6359
          || streq (relname, "R_C6000_NONE"))
6360
        continue;
6361
 
6362
      if (!(streq (relname, "R_ARM_PREL31")
6363
            || streq (relname, "R_C6000_PREL31")))
6364
        {
6365
          warn (_("Skipping unexpected relocation type %s\n"), relname);
6366
          continue;
6367
        }
6368
 
6369
      sym = aux->symtab + ELF32_R_SYM (rp->r_info);
6370
 
6371
      if (arm_sec->rel_type == SHT_REL)
6372
        {
6373
          offset = word & 0x7fffffff;
6374
          if (offset & 0x40000000)
6375
            offset |= ~ (bfd_vma) 0x7fffffff;
6376
        }
6377
      else
6378
        offset = rp->r_addend;
6379
 
6380
      offset += sym->st_value;
6381
      prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
6382
 
6383
      if (streq (relname, "R_C6000_PREL31"))
6384
        prelval >>= 1;
6385
 
6386
      word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
6387
      addr->section = sym->st_shndx;
6388
      addr->offset = offset;
6389
      break;
6390
    }
6391
 
6392
  *wordp = word;
6393
  arm_sec->next_rela = rp;
6394
 
6395
  return 1;
6396
}
6397
 
6398
static const char *tic6x_unwind_regnames[16] = {
6399
    "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
6400
    "A14", "A13", "A12", "A11", "A10",
6401
    "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"};
6402
 
6403
static void
6404
decode_tic6x_unwind_regmask (unsigned int mask)
6405
{
6406
  int i;
6407
 
6408
  for (i = 12; mask; mask >>= 1, i--)
6409
    {
6410
      if (mask & 1)
6411
        {
6412
          fputs (tic6x_unwind_regnames[i], stdout);
6413
          if (mask > 1)
6414
            fputs (", ", stdout);
6415
        }
6416
    }
6417
}
6418
 
6419
#define ADVANCE                                                 \
6420
  if (remaining == 0 && more_words)                              \
6421
    {                                                           \
6422
      data_offset += 4;                                         \
6423
      if (!arm_section_get_word (aux, data_arm_sec, data_sec,   \
6424
                                 data_offset, &word, &addr))    \
6425
        return;                                                 \
6426
      remaining = 4;                                            \
6427
      more_words--;                                             \
6428
    }                                                           \
6429
 
6430
#define GET_OP(OP)                      \
6431
  ADVANCE;                              \
6432
  if (remaining)                        \
6433
    {                                   \
6434
      remaining--;                      \
6435
      (OP) = word >> 24;                \
6436
      word <<= 8;                       \
6437
    }                                   \
6438
  else                                  \
6439
    {                                   \
6440
      printf (_("[Truncated opcode]\n"));       \
6441
      return;                           \
6442
    }                                   \
6443
  printf ("0x%02x ", OP)
6444
 
6445
static void
6446
decode_arm_unwind_bytecode (struct arm_unw_aux_info *aux,
6447
                            unsigned int word, unsigned int remaining,
6448
                            unsigned int more_words,
6449
                            bfd_vma data_offset, Elf_Internal_Shdr *data_sec,
6450
                            struct arm_section *data_arm_sec)
6451
{
6452
  struct absaddr addr;
6453
 
6454
  /* Decode the unwinding instructions.  */
6455
  while (1)
6456
    {
6457
      unsigned int op, op2;
6458
 
6459
      ADVANCE;
6460
      if (remaining == 0)
6461
        break;
6462
      remaining--;
6463
      op = word >> 24;
6464
      word <<= 8;
6465
 
6466
      printf ("  0x%02x ", op);
6467
 
6468
      if ((op & 0xc0) == 0x00)
6469
        {
6470
          int offset = ((op & 0x3f) << 2) + 4;
6471
 
6472
          printf ("     vsp = vsp + %d", offset);
6473
        }
6474
      else if ((op & 0xc0) == 0x40)
6475
        {
6476
          int offset = ((op & 0x3f) << 2) + 4;
6477
 
6478
          printf ("     vsp = vsp - %d", offset);
6479
        }
6480
      else if ((op & 0xf0) == 0x80)
6481
        {
6482
          GET_OP (op2);
6483
          if (op == 0x80 && op2 == 0)
6484
            printf (_("Refuse to unwind"));
6485
          else
6486
            {
6487
              unsigned int mask = ((op & 0x0f) << 8) | op2;
6488
              int first = 1;
6489
              int i;
6490
 
6491
              printf ("pop {");
6492
              for (i = 0; i < 12; i++)
6493
                if (mask & (1 << i))
6494
                  {
6495
                    if (first)
6496
                      first = 0;
6497
                    else
6498
                      printf (", ");
6499
                    printf ("r%d", 4 + i);
6500
                  }
6501
              printf ("}");
6502
            }
6503
        }
6504
      else if ((op & 0xf0) == 0x90)
6505
        {
6506
          if (op == 0x9d || op == 0x9f)
6507
            printf (_("     [Reserved]"));
6508
          else
6509
            printf ("     vsp = r%d", op & 0x0f);
6510
        }
6511
      else if ((op & 0xf0) == 0xa0)
6512
        {
6513
          int end = 4 + (op & 0x07);
6514
          int first = 1;
6515
          int i;
6516
 
6517
          printf ("     pop {");
6518
          for (i = 4; i <= end; i++)
6519
            {
6520
              if (first)
6521
                first = 0;
6522
              else
6523
                printf (", ");
6524
              printf ("r%d", i);
6525
            }
6526
          if (op & 0x08)
6527
            {
6528
              if (first)
6529
                printf (", ");
6530
              printf ("r14");
6531
            }
6532
          printf ("}");
6533
        }
6534
      else if (op == 0xb0)
6535
        printf (_("     finish"));
6536
      else if (op == 0xb1)
6537
        {
6538
          GET_OP (op2);
6539
          if (op2 == 0 || (op2 & 0xf0) != 0)
6540
            printf (_("[Spare]"));
6541
          else
6542
            {
6543
              unsigned int mask = op2 & 0x0f;
6544
              int first = 1;
6545
              int i;
6546
 
6547
              printf ("pop {");
6548
              for (i = 0; i < 12; i++)
6549
                if (mask & (1 << i))
6550
                  {
6551
                    if (first)
6552
                      first = 0;
6553
                    else
6554
                      printf (", ");
6555
                    printf ("r%d", i);
6556
                  }
6557
              printf ("}");
6558
            }
6559
        }
6560
      else if (op == 0xb2)
6561
        {
6562
          unsigned char buf[9];
6563
          unsigned int i, len;
6564
          unsigned long offset;
6565
 
6566
          for (i = 0; i < sizeof (buf); i++)
6567
            {
6568
              GET_OP (buf[i]);
6569
              if ((buf[i] & 0x80) == 0)
6570
                break;
6571
            }
6572
          assert (i < sizeof (buf));
6573
          offset = read_uleb128 (buf, &len);
6574
          assert (len == i + 1);
6575
          offset = offset * 4 + 0x204;
6576
          printf ("vsp = vsp + %ld", offset);
6577
        }
6578
      else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
6579
        {
6580
          unsigned int first, last;
6581
 
6582
          GET_OP (op2);
6583
          first = op2 >> 4;
6584
          last = op2 & 0x0f;
6585
          if (op == 0xc8)
6586
            first = first + 16;
6587
          printf ("pop {D%d", first);
6588
          if (last)
6589
            printf ("-D%d", first + last);
6590
          printf ("}");
6591
        }
6592
      else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
6593
        {
6594
          unsigned int count = op & 0x07;
6595
 
6596
          printf ("pop {D8");
6597
          if (count)
6598
            printf ("-D%d", 8 + count);
6599
          printf ("}");
6600
        }
6601
      else if (op >= 0xc0 && op <= 0xc5)
6602
        {
6603
          unsigned int count = op & 0x07;
6604
 
6605
          printf ("     pop {wR10");
6606
          if (count)
6607
            printf ("-wR%d", 10 + count);
6608
          printf ("}");
6609
        }
6610
      else if (op == 0xc6)
6611
        {
6612
          unsigned int first, last;
6613
 
6614
          GET_OP (op2);
6615
          first = op2 >> 4;
6616
          last = op2 & 0x0f;
6617
          printf ("pop {wR%d", first);
6618
          if (last)
6619
            printf ("-wR%d", first + last);
6620
          printf ("}");
6621
        }
6622
      else if (op == 0xc7)
6623
        {
6624
          GET_OP (op2);
6625
          if (op2 == 0 || (op2 & 0xf0) != 0)
6626
            printf (_("[Spare]"));
6627
          else
6628
            {
6629
              unsigned int mask = op2 & 0x0f;
6630
              int first = 1;
6631
              int i;
6632
 
6633
              printf ("pop {");
6634
              for (i = 0; i < 4; i++)
6635
                if (mask & (1 << i))
6636
                  {
6637
                    if (first)
6638
                      first = 0;
6639
                    else
6640
                      printf (", ");
6641
                    printf ("wCGR%d", i);
6642
                  }
6643
              printf ("}");
6644
            }
6645
        }
6646
      else
6647
        printf (_("     [unsupported opcode]"));
6648
      printf ("\n");
6649
    }
6650
}
6651
 
6652
static void
6653
decode_tic6x_unwind_bytecode (struct arm_unw_aux_info *aux,
6654
                            unsigned int word, unsigned int remaining,
6655
                            unsigned int more_words,
6656
                            bfd_vma data_offset, Elf_Internal_Shdr *data_sec,
6657
                            struct arm_section *data_arm_sec)
6658
{
6659
  struct absaddr addr;
6660
 
6661
  /* Decode the unwinding instructions.  */
6662
  while (1)
6663
    {
6664
      unsigned int op, op2;
6665
 
6666
      ADVANCE;
6667
      if (remaining == 0)
6668
        break;
6669
      remaining--;
6670
      op = word >> 24;
6671
      word <<= 8;
6672
 
6673
      printf (_("  0x%02x "), op);
6674
 
6675
      if ((op & 0xc0) == 0x00)
6676
        {
6677
          int offset = ((op & 0x3f) << 3) + 8;
6678
          printf (_("     sp = sp + %d"), offset);
6679
        }
6680
      else if ((op & 0xc0) == 0x80)
6681
        {
6682
          GET_OP (op2);
6683
          if (op == 0x80 && op2 == 0)
6684
            printf (_("Refuse to unwind"));
6685
          else
6686
            {
6687
              unsigned int mask = ((op & 0x1f) << 8) | op2;
6688
              if (op & 0x20)
6689
                printf ("pop compact {");
6690
              else
6691
                printf ("pop {");
6692
 
6693
              decode_tic6x_unwind_regmask (mask);
6694
              printf("}");
6695
            }
6696
        }
6697
      else if ((op & 0xf0) == 0xc0)
6698
        {
6699
          unsigned int reg;
6700
          unsigned int nregs;
6701
          unsigned int i;
6702
          const char *name;
6703
          struct {
6704
              unsigned int offset;
6705
              unsigned int reg;
6706
          } regpos[16];
6707
 
6708
          /* Scan entire instruction first so that GET_OP output is not
6709
             interleaved with disassembly.  */
6710
          nregs = 0;
6711
          for (i = 0; nregs < (op & 0xf); i++)
6712
            {
6713
              GET_OP (op2);
6714
              reg = op2 >> 4;
6715
              if (reg != 0xf)
6716
                {
6717
                  regpos[nregs].offset = i * 2;
6718
                  regpos[nregs].reg = reg;
6719
                  nregs++;
6720
                }
6721
 
6722
              reg = op2 & 0xf;
6723
              if (reg != 0xf)
6724
                {
6725
                  regpos[nregs].offset = i * 2 + 1;
6726
                  regpos[nregs].reg = reg;
6727
                  nregs++;
6728
                }
6729
            }
6730
 
6731
          printf (_("pop frame {"));
6732
          reg = nregs - 1;
6733
          for (i = i * 2; i > 0; i--)
6734
            {
6735
              if (regpos[reg].offset == i - 1)
6736
                {
6737
                  name = tic6x_unwind_regnames[regpos[reg].reg];
6738
                  if (reg > 0)
6739
                    reg--;
6740
                }
6741
              else
6742
                name = _("[pad]");
6743
 
6744
              fputs (name, stdout);
6745
              if (i > 1)
6746
                printf (", ");
6747
            }
6748
 
6749
          printf ("}");
6750
        }
6751
      else if (op == 0xd0)
6752
        printf ("     MOV FP, SP");
6753
      else if (op == 0xd1)
6754
        printf ("     __c6xabi_pop_rts");
6755
      else if (op == 0xd2)
6756
        {
6757
          unsigned char buf[9];
6758
          unsigned int i, len;
6759
          unsigned long offset;
6760
          for (i = 0; i < sizeof (buf); i++)
6761
            {
6762
              GET_OP (buf[i]);
6763
              if ((buf[i] & 0x80) == 0)
6764
                break;
6765
            }
6766
          assert (i < sizeof (buf));
6767
          offset = read_uleb128 (buf, &len);
6768
          assert (len == i + 1);
6769
          offset = offset * 8 + 0x408;
6770
          printf (_("sp = sp + %ld"), offset);
6771
        }
6772
      else if ((op & 0xf0) == 0xe0)
6773
        {
6774
          if ((op & 0x0f) == 7)
6775
            printf ("     RETURN");
6776
          else
6777
            printf ("     MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
6778
        }
6779
      else
6780
        {
6781
          printf (_("     [unsupported opcode]"));
6782
        }
6783
      putchar ('\n');
6784
    }
6785
}
6786
 
6787
static bfd_vma
6788
expand_prel31 (bfd_vma word, bfd_vma where)
6789
{
6790
  bfd_vma offset;
6791
 
6792
  offset = word & 0x7fffffff;
6793
  if (offset & 0x40000000)
6794
    offset |= ~ (bfd_vma) 0x7fffffff;
6795
 
6796
  if (elf_header.e_machine == EM_TI_C6000)
6797
    offset <<= 1;
6798
 
6799
  return offset + where;
6800
}
6801
 
6802
static void
6803
decode_arm_unwind (struct arm_unw_aux_info *aux,
6804
                   unsigned int word, unsigned int remaining,
6805
                   bfd_vma data_offset, Elf_Internal_Shdr *data_sec,
6806
                   struct arm_section *data_arm_sec)
6807
{
6808
  int per_index;
6809
  unsigned int more_words = 0;
6810
  struct absaddr addr;
6811
 
6812
  if (remaining == 0)
6813
    {
6814
      /* Fetch the first word.  */
6815
      if (!arm_section_get_word (aux, data_arm_sec, data_sec, data_offset,
6816
                                 &word, &addr))
6817
        return;
6818
      remaining = 4;
6819
    }
6820
 
6821
  if ((word & 0x80000000) == 0)
6822
    {
6823
      /* Expand prel31 for personality routine.  */
6824
      bfd_vma fn;
6825
      const char *procname;
6826
 
6827
      fn = expand_prel31 (word, data_sec->sh_addr + data_offset);
6828
      printf (_("  Personality routine: "));
6829
      procname = arm_print_vma_and_name (aux, fn, addr);
6830
      fputc ('\n', stdout);
6831
 
6832
      /* The GCC personality routines use the standard compact
6833
         encoding, starting with one byte giving the number of
6834
         words.  */
6835
      if (procname != NULL
6836
          && (const_strneq (procname, "__gcc_personality_v0")
6837
              || const_strneq (procname, "__gxx_personality_v0")
6838
              || const_strneq (procname, "__gcj_personality_v0")
6839
              || const_strneq (procname, "__gnu_objc_personality_v0")))
6840
        {
6841
          remaining = 0;
6842
          more_words = 1;
6843
          ADVANCE;
6844
          if (!remaining)
6845
            {
6846
              printf (_("  [Truncated data]\n"));
6847
              return;
6848
            }
6849
          more_words = word >> 24;
6850
          word <<= 8;
6851
          remaining--;
6852
          per_index = -1;
6853
        }
6854
      else
6855
        return;
6856
    }
6857
  else
6858
    {
6859
 
6860
      per_index = (word >> 24) & 0x7f;
6861
      printf (_("  Compact model %d\n"), per_index);
6862
      if (per_index == 0)
6863
        {
6864
          more_words = 0;
6865
          word <<= 8;
6866
          remaining--;
6867
        }
6868
      else if (per_index < 3)
6869
        {
6870
          more_words = (word >> 16) & 0xff;
6871
          word <<= 16;
6872
          remaining -= 2;
6873
        }
6874
    }
6875
 
6876
  switch (elf_header.e_machine)
6877
    {
6878
    case EM_ARM:
6879
      if (per_index < 3)
6880
        {
6881
          decode_arm_unwind_bytecode (aux, word, remaining, more_words,
6882
                                      data_offset, data_sec, data_arm_sec);
6883
        }
6884
      else
6885
        printf ("  [reserved]\n");
6886
      break;
6887
 
6888
    case EM_TI_C6000:
6889
      if (per_index < 3)
6890
        {
6891
          decode_tic6x_unwind_bytecode (aux, word, remaining, more_words,
6892
                                      data_offset, data_sec, data_arm_sec);
6893
        }
6894
      else if (per_index < 5)
6895
        {
6896
          if (((word >> 17) & 0x7f) == 0x7f)
6897
            printf (_("  Restore stack from frame pointer\n"));
6898
          else
6899
            printf (_("  Stack increment %d\n"), (word >> 14) & 0x1fc);
6900
          printf (_("  Registers restored: "));
6901
          if (per_index == 4)
6902
            printf (" (compact) ");
6903
          decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
6904
          putchar ('\n');
6905
          printf (_("  Return register: %s\n"),
6906
                  tic6x_unwind_regnames[word & 0xf]);
6907
        }
6908
      else
6909
        printf ("  [reserved]\n");
6910
      break;
6911
 
6912
    default:
6913
      abort ();
6914
    }
6915
 
6916
  /* Decode the descriptors.  Not implemented.  */
6917
}
6918
 
6919
static void
6920
dump_arm_unwind (struct arm_unw_aux_info *aux, Elf_Internal_Shdr *exidx_sec)
6921
{
6922
  struct arm_section exidx_arm_sec, extab_arm_sec;
6923
  unsigned int i, exidx_len;
6924
 
6925
  memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
6926
  memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
6927
  exidx_len = exidx_sec->sh_size / 8;
6928
 
6929
  for (i = 0; i < exidx_len; i++)
6930
    {
6931
      unsigned int exidx_fn, exidx_entry;
6932
      struct absaddr fn_addr, entry_addr;
6933
      bfd_vma fn;
6934
 
6935
      fputc ('\n', stdout);
6936
 
6937
      if (!arm_section_get_word (aux, &exidx_arm_sec, exidx_sec,
6938
                                 8 * i, &exidx_fn, &fn_addr)
6939
          || !arm_section_get_word (aux, &exidx_arm_sec, exidx_sec,
6940
                                    8 * i + 4, &exidx_entry, &entry_addr))
6941
        {
6942
          arm_free_section (&exidx_arm_sec);
6943
          arm_free_section (&extab_arm_sec);
6944
          return;
6945
        }
6946
 
6947
      fn = expand_prel31 (exidx_fn, exidx_sec->sh_addr + 8 * i);
6948
 
6949
      arm_print_vma_and_name (aux, fn, entry_addr);
6950
      fputs (": ", stdout);
6951
 
6952
      if (exidx_entry == 1)
6953
        {
6954
          print_vma (exidx_entry, PREFIX_HEX);
6955
          fputs (" [cantunwind]\n", stdout);
6956
        }
6957
      else if (exidx_entry & 0x80000000)
6958
        {
6959
          print_vma (exidx_entry, PREFIX_HEX);
6960
          fputc ('\n', stdout);
6961
          decode_arm_unwind (aux, exidx_entry, 4, 0, NULL, NULL);
6962
        }
6963
      else
6964
        {
6965
          bfd_vma table, table_offset = 0;
6966
          Elf_Internal_Shdr *table_sec;
6967
 
6968
          fputs ("@", stdout);
6969
          table = expand_prel31 (exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
6970
          print_vma (table, PREFIX_HEX);
6971
          printf ("\n");
6972
 
6973
          /* Locate the matching .ARM.extab.  */
6974
          if (entry_addr.section != SHN_UNDEF
6975
              && entry_addr.section < elf_header.e_shnum)
6976
            {
6977
              table_sec = section_headers + entry_addr.section;
6978
              table_offset = entry_addr.offset;
6979
            }
6980
          else
6981
            {
6982
              table_sec = find_section_by_address (table);
6983
              if (table_sec != NULL)
6984
                table_offset = table - table_sec->sh_addr;
6985
            }
6986
          if (table_sec == NULL)
6987
            {
6988
              warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
6989
                    (unsigned long) table);
6990
              continue;
6991
            }
6992
          decode_arm_unwind (aux, 0, 0, table_offset, table_sec,
6993
                             &extab_arm_sec);
6994
        }
6995
    }
6996
 
6997
  printf ("\n");
6998
 
6999
  arm_free_section (&exidx_arm_sec);
7000
  arm_free_section (&extab_arm_sec);
7001
}
7002
 
7003
/* Used for both ARM and C6X unwinding tables.  */
7004
static int
7005
arm_process_unwind (FILE *file)
7006
{
7007
  struct arm_unw_aux_info aux;
7008
  Elf_Internal_Shdr *unwsec = NULL;
7009
  Elf_Internal_Shdr *strsec;
7010
  Elf_Internal_Shdr *sec;
7011
  unsigned long i;
7012
  unsigned int sec_type;
7013
 
7014
  memset (& aux, 0, sizeof (aux));
7015
  aux.file = file;
7016
 
7017
  switch (elf_header.e_machine)
7018
    {
7019
    case EM_ARM:
7020
      sec_type = SHT_ARM_EXIDX;
7021
      break;
7022
 
7023
    case EM_TI_C6000:
7024
      sec_type = SHT_C6000_UNWIND;
7025
      break;
7026
 
7027
    default:
7028
        abort();
7029
    }
7030
 
7031
  if (string_table == NULL)
7032
    return 1;
7033
 
7034
  for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
7035
    {
7036
      if (sec->sh_type == SHT_SYMTAB && sec->sh_link < elf_header.e_shnum)
7037
        {
7038
          aux.nsyms = sec->sh_size / sec->sh_entsize;
7039
          aux.symtab = GET_ELF_SYMBOLS (file, sec);
7040
 
7041
          strsec = section_headers + sec->sh_link;
7042
          aux.strtab = get_data (NULL, file, strsec->sh_offset,
7043
                                 1, strsec->sh_size, _("string table"));
7044
          aux.strtab_size = aux.strtab != NULL ? strsec->sh_size : 0;
7045
        }
7046
      else if (sec->sh_type == sec_type)
7047
        unwsec = sec;
7048
    }
7049
 
7050
  if (!unwsec)
7051
    printf (_("\nThere are no unwind sections in this file.\n"));
7052
 
7053
  for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
7054
    {
7055
      if (sec->sh_type == sec_type)
7056
        {
7057
          printf (_("\nUnwind table index '%s' at offset 0x%lx contains %lu entries:\n"),
7058
                  SECTION_NAME (sec),
7059
                  (unsigned long) sec->sh_offset,
7060
                  (unsigned long) (sec->sh_size / (2 * eh_addr_size)));
7061
 
7062
          dump_arm_unwind (&aux, sec);
7063
        }
7064
    }
7065
 
7066
  if (aux.symtab)
7067
    free (aux.symtab);
7068
  if (aux.strtab)
7069
    free ((char *) aux.strtab);
7070
 
7071
  return 1;
7072
}
7073
 
7074
static int
7075
process_unwind (FILE * file)
7076
{
7077
  struct unwind_handler
7078
  {
7079
    int machtype;
7080
    int (* handler)(FILE *);
7081
  } handlers[] =
7082
  {
7083
    { EM_ARM, arm_process_unwind },
7084
    { EM_IA_64, ia64_process_unwind },
7085
    { EM_PARISC, hppa_process_unwind },
7086
    { EM_TI_C6000, arm_process_unwind },
7087
    { 0, 0 }
7088
  };
7089
  int i;
7090
 
7091
  if (!do_unwind)
7092
    return 1;
7093
 
7094
  for (i = 0; handlers[i].handler != NULL; i++)
7095
    if (elf_header.e_machine == handlers[i].machtype)
7096
      return handlers[i].handler (file);
7097
 
7098
  printf (_("\nThere are no unwind sections in this file.\n"));
7099
  return 1;
7100
}
7101
 
7102
static void
7103
dynamic_section_mips_val (Elf_Internal_Dyn * entry)
7104
{
7105
  switch (entry->d_tag)
7106
    {
7107
    case DT_MIPS_FLAGS:
7108
      if (entry->d_un.d_val == 0)
7109
        printf (_("NONE\n"));
7110
      else
7111
        {
7112
          static const char * opts[] =
7113
          {
7114
            "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
7115
            "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
7116
            "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
7117
            "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
7118
            "RLD_ORDER_SAFE"
7119
          };
7120
          unsigned int cnt;
7121
          int first = 1;
7122
 
7123
          for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
7124
            if (entry->d_un.d_val & (1 << cnt))
7125
              {
7126
                printf ("%s%s", first ? "" : " ", opts[cnt]);
7127
                first = 0;
7128
              }
7129
          puts ("");
7130
        }
7131
      break;
7132
 
7133
    case DT_MIPS_IVERSION:
7134
      if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
7135
        printf (_("Interface Version: %s\n"), GET_DYNAMIC_NAME (entry->d_un.d_val));
7136
      else
7137
        printf (_("<corrupt: %ld>\n"), (long) entry->d_un.d_ptr);
7138
      break;
7139
 
7140
    case DT_MIPS_TIME_STAMP:
7141
      {
7142
        char timebuf[20];
7143
        struct tm * tmp;
7144
 
7145
        time_t atime = entry->d_un.d_val;
7146
        tmp = gmtime (&atime);
7147
        snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
7148
                  tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
7149
                  tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
7150
        printf (_("Time Stamp: %s\n"), timebuf);
7151
      }
7152
      break;
7153
 
7154
    case DT_MIPS_RLD_VERSION:
7155
    case DT_MIPS_LOCAL_GOTNO:
7156
    case DT_MIPS_CONFLICTNO:
7157
    case DT_MIPS_LIBLISTNO:
7158
    case DT_MIPS_SYMTABNO:
7159
    case DT_MIPS_UNREFEXTNO:
7160
    case DT_MIPS_HIPAGENO:
7161
    case DT_MIPS_DELTA_CLASS_NO:
7162
    case DT_MIPS_DELTA_INSTANCE_NO:
7163
    case DT_MIPS_DELTA_RELOC_NO:
7164
    case DT_MIPS_DELTA_SYM_NO:
7165
    case DT_MIPS_DELTA_CLASSSYM_NO:
7166
    case DT_MIPS_COMPACT_SIZE:
7167
      printf ("%ld\n", (long) entry->d_un.d_ptr);
7168
      break;
7169
 
7170
    default:
7171
      printf ("%#lx\n", (unsigned long) entry->d_un.d_ptr);
7172
    }
7173
}
7174
 
7175
static void
7176
dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
7177
{
7178
  switch (entry->d_tag)
7179
    {
7180
    case DT_HP_DLD_FLAGS:
7181
      {
7182
        static struct
7183
        {
7184
          long int bit;
7185
          const char * str;
7186
        }
7187
        flags[] =
7188
        {
7189
          { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
7190
          { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
7191
          { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
7192
          { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
7193
          { DT_HP_BIND_NOW, "HP_BIND_NOW" },
7194
          { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
7195
          { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
7196
          { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
7197
          { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
7198
          { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
7199
          { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
7200
          { DT_HP_GST, "HP_GST" },
7201
          { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
7202
          { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
7203
          { DT_HP_NODELETE, "HP_NODELETE" },
7204
          { DT_HP_GROUP, "HP_GROUP" },
7205
          { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
7206
        };
7207
        int first = 1;
7208
        size_t cnt;
7209
        bfd_vma val = entry->d_un.d_val;
7210
 
7211
        for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
7212
          if (val & flags[cnt].bit)
7213
            {
7214
              if (! first)
7215
                putchar (' ');
7216
              fputs (flags[cnt].str, stdout);
7217
              first = 0;
7218
              val ^= flags[cnt].bit;
7219
            }
7220
 
7221
        if (val != 0 || first)
7222
          {
7223
            if (! first)
7224
              putchar (' ');
7225
            print_vma (val, HEX);
7226
          }
7227
      }
7228
      break;
7229
 
7230
    default:
7231
      print_vma (entry->d_un.d_ptr, PREFIX_HEX);
7232
      break;
7233
    }
7234
  putchar ('\n');
7235
}
7236
 
7237
#ifdef BFD64
7238
 
7239
/* VMS vs Unix time offset and factor.  */
7240
 
7241
#define VMS_EPOCH_OFFSET 35067168000000000LL
7242
#define VMS_GRANULARITY_FACTOR 10000000
7243
 
7244
/* Display a VMS time in a human readable format.  */
7245
 
7246
static void
7247
print_vms_time (bfd_int64_t vmstime)
7248
{
7249
  struct tm *tm;
7250
  time_t unxtime;
7251
 
7252
  unxtime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
7253
  tm = gmtime (&unxtime);
7254
  printf ("%04u-%02u-%02uT%02u:%02u:%02u",
7255
          tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
7256
          tm->tm_hour, tm->tm_min, tm->tm_sec);
7257
}
7258
#endif /* BFD64 */
7259
 
7260
static void
7261
dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
7262
{
7263
  switch (entry->d_tag)
7264
    {
7265
    case DT_IA_64_PLT_RESERVE:
7266
      /* First 3 slots reserved.  */
7267
      print_vma (entry->d_un.d_ptr, PREFIX_HEX);
7268
      printf (" -- ");
7269
      print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
7270
      break;
7271
 
7272
    case DT_IA_64_VMS_LINKTIME:
7273
#ifdef BFD64
7274
      print_vms_time (entry->d_un.d_val);
7275
#endif
7276
      break;
7277
 
7278
    case DT_IA_64_VMS_LNKFLAGS:
7279
      print_vma (entry->d_un.d_ptr, PREFIX_HEX);
7280
      if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
7281
        printf (" CALL_DEBUG");
7282
      if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
7283
        printf (" NOP0BUFS");
7284
      if (entry->d_un.d_val & VMS_LF_P0IMAGE)
7285
        printf (" P0IMAGE");
7286
      if (entry->d_un.d_val & VMS_LF_MKTHREADS)
7287
        printf (" MKTHREADS");
7288
      if (entry->d_un.d_val & VMS_LF_UPCALLS)
7289
        printf (" UPCALLS");
7290
      if (entry->d_un.d_val & VMS_LF_IMGSTA)
7291
        printf (" IMGSTA");
7292
      if (entry->d_un.d_val & VMS_LF_INITIALIZE)
7293
        printf (" INITIALIZE");
7294
      if (entry->d_un.d_val & VMS_LF_MAIN)
7295
        printf (" MAIN");
7296
      if (entry->d_un.d_val & VMS_LF_EXE_INIT)
7297
        printf (" EXE_INIT");
7298
      if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
7299
        printf (" TBK_IN_IMG");
7300
      if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
7301
        printf (" DBG_IN_IMG");
7302
      if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
7303
        printf (" TBK_IN_DSF");
7304
      if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
7305
        printf (" DBG_IN_DSF");
7306
      if (entry->d_un.d_val & VMS_LF_SIGNATURES)
7307
        printf (" SIGNATURES");
7308
      if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
7309
        printf (" REL_SEG_OFF");
7310
      break;
7311
 
7312
    default:
7313
      print_vma (entry->d_un.d_ptr, PREFIX_HEX);
7314
      break;
7315
    }
7316
  putchar ('\n');
7317
}
7318
 
7319
static int
7320
get_32bit_dynamic_section (FILE * file)
7321
{
7322
  Elf32_External_Dyn * edyn;
7323
  Elf32_External_Dyn * ext;
7324
  Elf_Internal_Dyn * entry;
7325
 
7326
  edyn = (Elf32_External_Dyn *) get_data (NULL, file, dynamic_addr, 1,
7327
                                          dynamic_size, _("dynamic section"));
7328
  if (!edyn)
7329
    return 0;
7330
 
7331
/* SGI's ELF has more than one section in the DYNAMIC segment, and we
7332
   might not have the luxury of section headers.  Look for the DT_NULL
7333
   terminator to determine the number of entries.  */
7334
  for (ext = edyn, dynamic_nent = 0;
7335
       (char *) ext < (char *) edyn + dynamic_size;
7336
       ext++)
7337
    {
7338
      dynamic_nent++;
7339
      if (BYTE_GET (ext->d_tag) == DT_NULL)
7340
        break;
7341
    }
7342
 
7343
  dynamic_section = (Elf_Internal_Dyn *) cmalloc (dynamic_nent,
7344
                                                  sizeof (* entry));
7345
  if (dynamic_section == NULL)
7346
    {
7347
      error (_("Out of memory\n"));
7348
      free (edyn);
7349
      return 0;
7350
    }
7351
 
7352
  for (ext = edyn, entry = dynamic_section;
7353
       entry < dynamic_section + dynamic_nent;
7354
       ext++, entry++)
7355
    {
7356
      entry->d_tag      = BYTE_GET (ext->d_tag);
7357
      entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
7358
    }
7359
 
7360
  free (edyn);
7361
 
7362
  return 1;
7363
}
7364
 
7365
static int
7366
get_64bit_dynamic_section (FILE * file)
7367
{
7368
  Elf64_External_Dyn * edyn;
7369
  Elf64_External_Dyn * ext;
7370
  Elf_Internal_Dyn * entry;
7371
 
7372
  edyn = (Elf64_External_Dyn *) get_data (NULL, file, dynamic_addr, 1,
7373
                                          dynamic_size, _("dynamic section"));
7374
  if (!edyn)
7375
    return 0;
7376
 
7377
/* SGI's ELF has more than one section in the DYNAMIC segment, and we
7378
   might not have the luxury of section headers.  Look for the DT_NULL
7379
   terminator to determine the number of entries.  */
7380
  for (ext = edyn, dynamic_nent = 0;
7381
       (char *) ext < (char *) edyn + dynamic_size;
7382
       ext++)
7383
    {
7384
      dynamic_nent++;
7385
      if (BYTE_GET (ext->d_tag) == DT_NULL)
7386
        break;
7387
    }
7388
 
7389
  dynamic_section = (Elf_Internal_Dyn *) cmalloc (dynamic_nent,
7390
                                                  sizeof (* entry));
7391
  if (dynamic_section == NULL)
7392
    {
7393
      error (_("Out of memory\n"));
7394
      free (edyn);
7395
      return 0;
7396
    }
7397
 
7398
  for (ext = edyn, entry = dynamic_section;
7399
       entry < dynamic_section + dynamic_nent;
7400
       ext++, entry++)
7401
    {
7402
      entry->d_tag      = BYTE_GET (ext->d_tag);
7403
      entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
7404
    }
7405
 
7406
  free (edyn);
7407
 
7408
  return 1;
7409
}
7410
 
7411
static void
7412
print_dynamic_flags (bfd_vma flags)
7413
{
7414
  int first = 1;
7415
 
7416
  while (flags)
7417
    {
7418
      bfd_vma flag;
7419
 
7420
      flag = flags & - flags;
7421
      flags &= ~ flag;
7422
 
7423
      if (first)
7424
        first = 0;
7425
      else
7426
        putc (' ', stdout);
7427
 
7428
      switch (flag)
7429
        {
7430
        case DF_ORIGIN:         fputs ("ORIGIN", stdout); break;
7431
        case DF_SYMBOLIC:       fputs ("SYMBOLIC", stdout); break;
7432
        case DF_TEXTREL:        fputs ("TEXTREL", stdout); break;
7433
        case DF_BIND_NOW:       fputs ("BIND_NOW", stdout); break;
7434
        case DF_STATIC_TLS:     fputs ("STATIC_TLS", stdout); break;
7435
        default:                fputs (_("unknown"), stdout); break;
7436
        }
7437
    }
7438
  puts ("");
7439
}
7440
 
7441
/* Parse and display the contents of the dynamic section.  */
7442
 
7443
static int
7444
process_dynamic_section (FILE * file)
7445
{
7446
  Elf_Internal_Dyn * entry;
7447
 
7448
  if (dynamic_size == 0)
7449
    {
7450
      if (do_dynamic)
7451
        printf (_("\nThere is no dynamic section in this file.\n"));
7452
 
7453
      return 1;
7454
    }
7455
 
7456
  if (is_32bit_elf)
7457
    {
7458
      if (! get_32bit_dynamic_section (file))
7459
        return 0;
7460
    }
7461
  else if (! get_64bit_dynamic_section (file))
7462
    return 0;
7463
 
7464
  /* Find the appropriate symbol table.  */
7465
  if (dynamic_symbols == NULL)
7466
    {
7467
      for (entry = dynamic_section;
7468
           entry < dynamic_section + dynamic_nent;
7469
           ++entry)
7470
        {
7471
          Elf_Internal_Shdr section;
7472
 
7473
          if (entry->d_tag != DT_SYMTAB)
7474
            continue;
7475
 
7476
          dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
7477
 
7478
          /* Since we do not know how big the symbol table is,
7479
             we default to reading in the entire file (!) and
7480
             processing that.  This is overkill, I know, but it
7481
             should work.  */
7482
          section.sh_offset = offset_from_vma (file, entry->d_un.d_val, 0);
7483
 
7484
          if (archive_file_offset != 0)
7485
            section.sh_size = archive_file_size - section.sh_offset;
7486
          else
7487
            {
7488
              if (fseek (file, 0, SEEK_END))
7489
                error (_("Unable to seek to end of file!\n"));
7490
 
7491
              section.sh_size = ftell (file) - section.sh_offset;
7492
            }
7493
 
7494
          if (is_32bit_elf)
7495
            section.sh_entsize = sizeof (Elf32_External_Sym);
7496
          else
7497
            section.sh_entsize = sizeof (Elf64_External_Sym);
7498
 
7499
          num_dynamic_syms = section.sh_size / section.sh_entsize;
7500
          if (num_dynamic_syms < 1)
7501
            {
7502
              error (_("Unable to determine the number of symbols to load\n"));
7503
              continue;
7504
            }
7505
 
7506
          dynamic_symbols = GET_ELF_SYMBOLS (file, &section);
7507
        }
7508
    }
7509
 
7510
  /* Similarly find a string table.  */
7511
  if (dynamic_strings == NULL)
7512
    {
7513
      for (entry = dynamic_section;
7514
           entry < dynamic_section + dynamic_nent;
7515
           ++entry)
7516
        {
7517
          unsigned long offset;
7518
          long str_tab_len;
7519
 
7520
          if (entry->d_tag != DT_STRTAB)
7521
            continue;
7522
 
7523
          dynamic_info[DT_STRTAB] = entry->d_un.d_val;
7524
 
7525
          /* Since we do not know how big the string table is,
7526
             we default to reading in the entire file (!) and
7527
             processing that.  This is overkill, I know, but it
7528
             should work.  */
7529
 
7530
          offset = offset_from_vma (file, entry->d_un.d_val, 0);
7531
 
7532
          if (archive_file_offset != 0)
7533
            str_tab_len = archive_file_size - offset;
7534
          else
7535
            {
7536
              if (fseek (file, 0, SEEK_END))
7537
                error (_("Unable to seek to end of file\n"));
7538
              str_tab_len = ftell (file) - offset;
7539
            }
7540
 
7541
          if (str_tab_len < 1)
7542
            {
7543
              error
7544
                (_("Unable to determine the length of the dynamic string table\n"));
7545
              continue;
7546
            }
7547
 
7548
          dynamic_strings = (char *) get_data (NULL, file, offset, 1,
7549
                                               str_tab_len,
7550
                                               _("dynamic string table"));
7551
          dynamic_strings_length = str_tab_len;
7552
          break;
7553
        }
7554
    }
7555
 
7556
  /* And find the syminfo section if available.  */
7557
  if (dynamic_syminfo == NULL)
7558
    {
7559
      unsigned long syminsz = 0;
7560
 
7561
      for (entry = dynamic_section;
7562
           entry < dynamic_section + dynamic_nent;
7563
           ++entry)
7564
        {
7565
          if (entry->d_tag == DT_SYMINENT)
7566
            {
7567
              /* Note: these braces are necessary to avoid a syntax
7568
                 error from the SunOS4 C compiler.  */
7569
              assert (sizeof (Elf_External_Syminfo) == entry->d_un.d_val);
7570
            }
7571
          else if (entry->d_tag == DT_SYMINSZ)
7572
            syminsz = entry->d_un.d_val;
7573
          else if (entry->d_tag == DT_SYMINFO)
7574
            dynamic_syminfo_offset = offset_from_vma (file, entry->d_un.d_val,
7575
                                                      syminsz);
7576
        }
7577
 
7578
      if (dynamic_syminfo_offset != 0 && syminsz != 0)
7579
        {
7580
          Elf_External_Syminfo * extsyminfo;
7581
          Elf_External_Syminfo * extsym;
7582
          Elf_Internal_Syminfo * syminfo;
7583
 
7584
          /* There is a syminfo section.  Read the data.  */
7585
          extsyminfo = (Elf_External_Syminfo *)
7586
              get_data (NULL, file, dynamic_syminfo_offset, 1, syminsz,
7587
                        _("symbol information"));
7588
          if (!extsyminfo)
7589
            return 0;
7590
 
7591
          dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
7592
          if (dynamic_syminfo == NULL)
7593
            {
7594
              error (_("Out of memory\n"));
7595
              return 0;
7596
            }
7597
 
7598
          dynamic_syminfo_nent = syminsz / sizeof (Elf_External_Syminfo);
7599
          for (syminfo = dynamic_syminfo, extsym = extsyminfo;
7600
               syminfo < dynamic_syminfo + dynamic_syminfo_nent;
7601
               ++syminfo, ++extsym)
7602
            {
7603
              syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
7604
              syminfo->si_flags = BYTE_GET (extsym->si_flags);
7605
            }
7606
 
7607
          free (extsyminfo);
7608
        }
7609
    }
7610
 
7611
  if (do_dynamic && dynamic_addr)
7612
    printf (_("\nDynamic section at offset 0x%lx contains %u entries:\n"),
7613
            dynamic_addr, dynamic_nent);
7614
  if (do_dynamic)
7615
    printf (_("  Tag        Type                         Name/Value\n"));
7616
 
7617
  for (entry = dynamic_section;
7618
       entry < dynamic_section + dynamic_nent;
7619
       entry++)
7620
    {
7621
      if (do_dynamic)
7622
        {
7623
          const char * dtype;
7624
 
7625
          putchar (' ');
7626
          print_vma (entry->d_tag, FULL_HEX);
7627
          dtype = get_dynamic_type (entry->d_tag);
7628
          printf (" (%s)%*s", dtype,
7629
                  ((is_32bit_elf ? 27 : 19)
7630
                   - (int) strlen (dtype)),
7631
                  " ");
7632
        }
7633
 
7634
      switch (entry->d_tag)
7635
        {
7636
        case DT_FLAGS:
7637
          if (do_dynamic)
7638
            print_dynamic_flags (entry->d_un.d_val);
7639
          break;
7640
 
7641
        case DT_AUXILIARY:
7642
        case DT_FILTER:
7643
        case DT_CONFIG:
7644
        case DT_DEPAUDIT:
7645
        case DT_AUDIT:
7646
          if (do_dynamic)
7647
            {
7648
              switch (entry->d_tag)
7649
                {
7650
                case DT_AUXILIARY:
7651
                  printf (_("Auxiliary library"));
7652
                  break;
7653
 
7654
                case DT_FILTER:
7655
                  printf (_("Filter library"));
7656
                  break;
7657
 
7658
                case DT_CONFIG:
7659
                  printf (_("Configuration file"));
7660
                  break;
7661
 
7662
                case DT_DEPAUDIT:
7663
                  printf (_("Dependency audit library"));
7664
                  break;
7665
 
7666
                case DT_AUDIT:
7667
                  printf (_("Audit library"));
7668
                  break;
7669
                }
7670
 
7671
              if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
7672
                printf (": [%s]\n", GET_DYNAMIC_NAME (entry->d_un.d_val));
7673
              else
7674
                {
7675
                  printf (": ");
7676
                  print_vma (entry->d_un.d_val, PREFIX_HEX);
7677
                  putchar ('\n');
7678
                }
7679
            }
7680
          break;
7681
 
7682
        case DT_FEATURE:
7683
          if (do_dynamic)
7684
            {
7685
              printf (_("Flags:"));
7686
 
7687
              if (entry->d_un.d_val == 0)
7688
                printf (_(" None\n"));
7689
              else
7690
                {
7691
                  unsigned long int val = entry->d_un.d_val;
7692
 
7693
                  if (val & DTF_1_PARINIT)
7694
                    {
7695
                      printf (" PARINIT");
7696
                      val ^= DTF_1_PARINIT;
7697
                    }
7698
                  if (val & DTF_1_CONFEXP)
7699
                    {
7700
                      printf (" CONFEXP");
7701
                      val ^= DTF_1_CONFEXP;
7702
                    }
7703
                  if (val != 0)
7704
                    printf (" %lx", val);
7705
                  puts ("");
7706
                }
7707
            }
7708
          break;
7709
 
7710
        case DT_POSFLAG_1:
7711
          if (do_dynamic)
7712
            {
7713
              printf (_("Flags:"));
7714
 
7715
              if (entry->d_un.d_val == 0)
7716
                printf (_(" None\n"));
7717
              else
7718
                {
7719
                  unsigned long int val = entry->d_un.d_val;
7720
 
7721
                  if (val & DF_P1_LAZYLOAD)
7722
                    {
7723
                      printf (" LAZYLOAD");
7724
                      val ^= DF_P1_LAZYLOAD;
7725
                    }
7726
                  if (val & DF_P1_GROUPPERM)
7727
                    {
7728
                      printf (" GROUPPERM");
7729
                      val ^= DF_P1_GROUPPERM;
7730
                    }
7731
                  if (val != 0)
7732
                    printf (" %lx", val);
7733
                  puts ("");
7734
                }
7735
            }
7736
          break;
7737
 
7738
        case DT_FLAGS_1:
7739
          if (do_dynamic)
7740
            {
7741
              printf (_("Flags:"));
7742
              if (entry->d_un.d_val == 0)
7743
                printf (_(" None\n"));
7744
              else
7745
                {
7746
                  unsigned long int val = entry->d_un.d_val;
7747
 
7748
                  if (val & DF_1_NOW)
7749
                    {
7750
                      printf (" NOW");
7751
                      val ^= DF_1_NOW;
7752
                    }
7753
                  if (val & DF_1_GLOBAL)
7754
                    {
7755
                      printf (" GLOBAL");
7756
                      val ^= DF_1_GLOBAL;
7757
                    }
7758
                  if (val & DF_1_GROUP)
7759
                    {
7760
                      printf (" GROUP");
7761
                      val ^= DF_1_GROUP;
7762
                    }
7763
                  if (val & DF_1_NODELETE)
7764
                    {
7765
                      printf (" NODELETE");
7766
                      val ^= DF_1_NODELETE;
7767
                    }
7768
                  if (val & DF_1_LOADFLTR)
7769
                    {
7770
                      printf (" LOADFLTR");
7771
                      val ^= DF_1_LOADFLTR;
7772
                    }
7773
                  if (val & DF_1_INITFIRST)
7774
                    {
7775
                      printf (" INITFIRST");
7776
                      val ^= DF_1_INITFIRST;
7777
                    }
7778
                  if (val & DF_1_NOOPEN)
7779
                    {
7780
                      printf (" NOOPEN");
7781
                      val ^= DF_1_NOOPEN;
7782
                    }
7783
                  if (val & DF_1_ORIGIN)
7784
                    {
7785
                      printf (" ORIGIN");
7786
                      val ^= DF_1_ORIGIN;
7787
                    }
7788
                  if (val & DF_1_DIRECT)
7789
                    {
7790
                      printf (" DIRECT");
7791
                      val ^= DF_1_DIRECT;
7792
                    }
7793
                  if (val & DF_1_TRANS)
7794
                    {
7795
                      printf (" TRANS");
7796
                      val ^= DF_1_TRANS;
7797
                    }
7798
                  if (val & DF_1_INTERPOSE)
7799
                    {
7800
                      printf (" INTERPOSE");
7801
                      val ^= DF_1_INTERPOSE;
7802
                    }
7803
                  if (val & DF_1_NODEFLIB)
7804
                    {
7805
                      printf (" NODEFLIB");
7806
                      val ^= DF_1_NODEFLIB;
7807
                    }
7808
                  if (val & DF_1_NODUMP)
7809
                    {
7810
                      printf (" NODUMP");
7811
                      val ^= DF_1_NODUMP;
7812
                    }
7813
                  if (val & DF_1_CONLFAT)
7814
                    {
7815
                      printf (" CONLFAT");
7816
                      val ^= DF_1_CONLFAT;
7817
                    }
7818
                  if (val != 0)
7819
                    printf (" %lx", val);
7820
                  puts ("");
7821
                }
7822
            }
7823
          break;
7824
 
7825
        case DT_PLTREL:
7826
          dynamic_info[entry->d_tag] = entry->d_un.d_val;
7827
          if (do_dynamic)
7828
            puts (get_dynamic_type (entry->d_un.d_val));
7829
          break;
7830
 
7831
        case DT_NULL    :
7832
        case DT_NEEDED  :
7833
        case DT_PLTGOT  :
7834
        case DT_HASH    :
7835
        case DT_STRTAB  :
7836
        case DT_SYMTAB  :
7837
        case DT_RELA    :
7838
        case DT_INIT    :
7839
        case DT_FINI    :
7840
        case DT_SONAME  :
7841
        case DT_RPATH   :
7842
        case DT_SYMBOLIC:
7843
        case DT_REL     :
7844
        case DT_DEBUG   :
7845
        case DT_TEXTREL :
7846
        case DT_JMPREL  :
7847
        case DT_RUNPATH :
7848
          dynamic_info[entry->d_tag] = entry->d_un.d_val;
7849
 
7850
          if (do_dynamic)
7851
            {
7852
              char * name;
7853
 
7854
              if (VALID_DYNAMIC_NAME (entry->d_un.d_val))
7855
                name = GET_DYNAMIC_NAME (entry->d_un.d_val);
7856
              else
7857
                name = NULL;
7858
 
7859
              if (name)
7860
                {
7861
                  switch (entry->d_tag)
7862
                    {
7863
                    case DT_NEEDED:
7864
                      printf (_("Shared library: [%s]"), name);
7865
 
7866
                      if (streq (name, program_interpreter))
7867
                        printf (_(" program interpreter"));
7868
                      break;
7869
 
7870
                    case DT_SONAME:
7871
                      printf (_("Library soname: [%s]"), name);
7872
                      break;
7873
 
7874
                    case DT_RPATH:
7875
                      printf (_("Library rpath: [%s]"), name);
7876
                      break;
7877
 
7878
                    case DT_RUNPATH:
7879
                      printf (_("Library runpath: [%s]"), name);
7880
                      break;
7881
 
7882
                    default:
7883
                      print_vma (entry->d_un.d_val, PREFIX_HEX);
7884
                      break;
7885
                    }
7886
                }
7887
              else
7888
                print_vma (entry->d_un.d_val, PREFIX_HEX);
7889
 
7890
              putchar ('\n');
7891
            }
7892
          break;
7893
 
7894
        case DT_PLTRELSZ:
7895
        case DT_RELASZ  :
7896
        case DT_STRSZ   :
7897
        case DT_RELSZ   :
7898
        case DT_RELAENT :
7899
        case DT_SYMENT  :
7900
        case DT_RELENT  :
7901
          dynamic_info[entry->d_tag] = entry->d_un.d_val;
7902
        case DT_PLTPADSZ:
7903
        case DT_MOVEENT :
7904
        case DT_MOVESZ  :
7905
        case DT_INIT_ARRAYSZ:
7906
        case DT_FINI_ARRAYSZ:
7907
        case DT_GNU_CONFLICTSZ:
7908
        case DT_GNU_LIBLISTSZ:
7909
          if (do_dynamic)
7910
            {
7911
              print_vma (entry->d_un.d_val, UNSIGNED);
7912
              printf (_(" (bytes)\n"));
7913
            }
7914
          break;
7915
 
7916
        case DT_VERDEFNUM:
7917
        case DT_VERNEEDNUM:
7918
        case DT_RELACOUNT:
7919
        case DT_RELCOUNT:
7920
          if (do_dynamic)
7921
            {
7922
              print_vma (entry->d_un.d_val, UNSIGNED);
7923
              putchar ('\n');
7924
            }
7925
          break;
7926
 
7927
        case DT_SYMINSZ:
7928
        case DT_SYMINENT:
7929
        case DT_SYMINFO:
7930
        case DT_USED:
7931
        case DT_INIT_ARRAY:
7932
        case DT_FINI_ARRAY:
7933
          if (do_dynamic)
7934
            {
7935
              if (entry->d_tag == DT_USED
7936
                  && VALID_DYNAMIC_NAME (entry->d_un.d_val))
7937
                {
7938
                  char * name = GET_DYNAMIC_NAME (entry->d_un.d_val);
7939
 
7940
                  if (*name)
7941
                    {
7942
                      printf (_("Not needed object: [%s]\n"), name);
7943
                      break;
7944
                    }
7945
                }
7946
 
7947
              print_vma (entry->d_un.d_val, PREFIX_HEX);
7948
              putchar ('\n');
7949
            }
7950
          break;
7951
 
7952
        case DT_BIND_NOW:
7953
          /* The value of this entry is ignored.  */
7954
          if (do_dynamic)
7955
            putchar ('\n');
7956
          break;
7957
 
7958
        case DT_GNU_PRELINKED:
7959
          if (do_dynamic)
7960
            {
7961
              struct tm * tmp;
7962
              time_t atime = entry->d_un.d_val;
7963
 
7964
              tmp = gmtime (&atime);
7965
              printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
7966
                      tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
7967
                      tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
7968
 
7969
            }
7970
          break;
7971
 
7972
        case DT_GNU_HASH:
7973
          dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
7974
          if (do_dynamic)
7975
            {
7976
              print_vma (entry->d_un.d_val, PREFIX_HEX);
7977
              putchar ('\n');
7978
            }
7979
          break;
7980
 
7981
        default:
7982
          if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
7983
            version_info[DT_VERSIONTAGIDX (entry->d_tag)] =
7984
              entry->d_un.d_val;
7985
 
7986
          if (do_dynamic)
7987
            {
7988
              switch (elf_header.e_machine)
7989
                {
7990
                case EM_MIPS:
7991
                case EM_MIPS_RS3_LE:
7992
                  dynamic_section_mips_val (entry);
7993
                  break;
7994
                case EM_PARISC:
7995
                  dynamic_section_parisc_val (entry);
7996
                  break;
7997
                case EM_IA_64:
7998
                  dynamic_section_ia64_val (entry);
7999
                  break;
8000
                default:
8001
                  print_vma (entry->d_un.d_val, PREFIX_HEX);
8002
                  putchar ('\n');
8003
                }
8004
            }
8005
          break;
8006
        }
8007
    }
8008
 
8009
  return 1;
8010
}
8011
 
8012
static char *
8013
get_ver_flags (unsigned int flags)
8014
{
8015
  static char buff[32];
8016
 
8017
  buff[0] = 0;
8018
 
8019
  if (flags == 0)
8020
    return _("none");
8021
 
8022
  if (flags & VER_FLG_BASE)
8023
    strcat (buff, "BASE ");
8024
 
8025
  if (flags & VER_FLG_WEAK)
8026
    {
8027
      if (flags & VER_FLG_BASE)
8028
        strcat (buff, "| ");
8029
 
8030
      strcat (buff, "WEAK ");
8031
    }
8032
 
8033
  if (flags & VER_FLG_INFO)
8034
    {
8035
      if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
8036
        strcat (buff, "| ");
8037
 
8038
      strcat (buff, "INFO ");
8039
    }
8040
 
8041
  if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
8042
    strcat (buff, _("| <unknown>"));
8043
 
8044
  return buff;
8045
}
8046
 
8047
/* Display the contents of the version sections.  */
8048
 
8049
static int
8050
process_version_sections (FILE * file)
8051
{
8052
  Elf_Internal_Shdr * section;
8053
  unsigned i;
8054
  int found = 0;
8055
 
8056
  if (! do_version)
8057
    return 1;
8058
 
8059
  for (i = 0, section = section_headers;
8060
       i < elf_header.e_shnum;
8061
       i++, section++)
8062
    {
8063
      switch (section->sh_type)
8064
        {
8065
        case SHT_GNU_verdef:
8066
          {
8067
            Elf_External_Verdef * edefs;
8068
            unsigned int idx;
8069
            unsigned int cnt;
8070
            char * endbuf;
8071
 
8072
            found = 1;
8073
 
8074
            printf
8075
              (_("\nVersion definition section '%s' contains %u entries:\n"),
8076
               SECTION_NAME (section), section->sh_info);
8077
 
8078
            printf (_("  Addr: 0x"));
8079
            printf_vma (section->sh_addr);
8080
            printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
8081
                    (unsigned long) section->sh_offset, section->sh_link,
8082
                    section->sh_link < elf_header.e_shnum
8083
                    ? SECTION_NAME (section_headers + section->sh_link)
8084
                    : _("<corrupt>"));
8085
 
8086
            edefs = (Elf_External_Verdef *)
8087
                get_data (NULL, file, section->sh_offset, 1,section->sh_size,
8088
                          _("version definition section"));
8089
            endbuf = (char *) edefs + section->sh_size;
8090
            if (!edefs)
8091
              break;
8092
 
8093
            for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
8094
              {
8095
                char * vstart;
8096
                Elf_External_Verdef * edef;
8097
                Elf_Internal_Verdef ent;
8098
                Elf_External_Verdaux * eaux;
8099
                Elf_Internal_Verdaux aux;
8100
                int j;
8101
                int isum;
8102
 
8103
                /* Check for negative or very large indicies.  */
8104
                if ((unsigned char *) edefs + idx < (unsigned char *) edefs)
8105
                  break;
8106
 
8107
                vstart = ((char *) edefs) + idx;
8108
                if (vstart + sizeof (*edef) > endbuf)
8109
                  break;
8110
 
8111
                edef = (Elf_External_Verdef *) vstart;
8112
 
8113
                ent.vd_version = BYTE_GET (edef->vd_version);
8114
                ent.vd_flags   = BYTE_GET (edef->vd_flags);
8115
                ent.vd_ndx     = BYTE_GET (edef->vd_ndx);
8116
                ent.vd_cnt     = BYTE_GET (edef->vd_cnt);
8117
                ent.vd_hash    = BYTE_GET (edef->vd_hash);
8118
                ent.vd_aux     = BYTE_GET (edef->vd_aux);
8119
                ent.vd_next    = BYTE_GET (edef->vd_next);
8120
 
8121
                printf (_("  %#06x: Rev: %d  Flags: %s"),
8122
                        idx, ent.vd_version, get_ver_flags (ent.vd_flags));
8123
 
8124
                printf (_("  Index: %d  Cnt: %d  "),
8125
                        ent.vd_ndx, ent.vd_cnt);
8126
 
8127
                /* Check for overflow.  */
8128
                if ((unsigned char *)(vstart + ent.vd_aux) < (unsigned char *) vstart
8129
                    || (unsigned char *)(vstart + ent.vd_aux) > (unsigned char *) endbuf)
8130
                  break;
8131
 
8132
                vstart += ent.vd_aux;
8133
 
8134
                eaux = (Elf_External_Verdaux *) vstart;
8135
 
8136
                aux.vda_name = BYTE_GET (eaux->vda_name);
8137
                aux.vda_next = BYTE_GET (eaux->vda_next);
8138
 
8139
                if (VALID_DYNAMIC_NAME (aux.vda_name))
8140
                  printf (_("Name: %s\n"), GET_DYNAMIC_NAME (aux.vda_name));
8141
                else
8142
                  printf (_("Name index: %ld\n"), aux.vda_name);
8143
 
8144
                isum = idx + ent.vd_aux;
8145
 
8146
                for (j = 1; j < ent.vd_cnt; j++)
8147
                  {
8148
                    /* Check for overflow.  */
8149
                    if ((unsigned char *)(vstart + aux.vda_next) < (unsigned char *) vstart
8150
                        || (unsigned char *)(vstart + aux.vda_next) > (unsigned char *) endbuf)
8151
                      break;
8152
 
8153
                    isum   += aux.vda_next;
8154
                    vstart += aux.vda_next;
8155
 
8156
                    eaux = (Elf_External_Verdaux *) vstart;
8157
                    if (vstart + sizeof (*eaux) > endbuf)
8158
                      break;
8159
 
8160
                    aux.vda_name = BYTE_GET (eaux->vda_name);
8161
                    aux.vda_next = BYTE_GET (eaux->vda_next);
8162
 
8163
                    if (VALID_DYNAMIC_NAME (aux.vda_name))
8164
                      printf (_("  %#06x: Parent %d: %s\n"),
8165
                              isum, j, GET_DYNAMIC_NAME (aux.vda_name));
8166
                    else
8167
                      printf (_("  %#06x: Parent %d, name index: %ld\n"),
8168
                              isum, j, aux.vda_name);
8169
                  }
8170
 
8171
                if (j < ent.vd_cnt)
8172
                  printf (_("  Version def aux past end of section\n"));
8173
 
8174
                idx += ent.vd_next;
8175
              }
8176
 
8177
            if (cnt < section->sh_info)
8178
              printf (_("  Version definition past end of section\n"));
8179
 
8180
            free (edefs);
8181
          }
8182
          break;
8183
 
8184
        case SHT_GNU_verneed:
8185
          {
8186
            Elf_External_Verneed * eneed;
8187
            unsigned int idx;
8188
            unsigned int cnt;
8189
            char * endbuf;
8190
 
8191
            found = 1;
8192
 
8193
            printf (_("\nVersion needs section '%s' contains %u entries:\n"),
8194
                    SECTION_NAME (section), section->sh_info);
8195
 
8196
            printf (_(" Addr: 0x"));
8197
            printf_vma (section->sh_addr);
8198
            printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
8199
                    (unsigned long) section->sh_offset, section->sh_link,
8200
                    section->sh_link < elf_header.e_shnum
8201
                    ? SECTION_NAME (section_headers + section->sh_link)
8202
                    : _("<corrupt>"));
8203
 
8204
            eneed = (Elf_External_Verneed *) get_data (NULL, file,
8205
                                                       section->sh_offset, 1,
8206
                                                       section->sh_size,
8207
                                                       _("version need section"));
8208
            endbuf = (char *) eneed + section->sh_size;
8209
            if (!eneed)
8210
              break;
8211
 
8212
            for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
8213
              {
8214
                Elf_External_Verneed * entry;
8215
                Elf_Internal_Verneed ent;
8216
                int j;
8217
                int isum;
8218
                char * vstart;
8219
 
8220
                if ((unsigned char *) eneed + idx < (unsigned char *) eneed)
8221
                  break;
8222
 
8223
                vstart = ((char *) eneed) + idx;
8224
                if (vstart + sizeof (*entry) > endbuf)
8225
                  break;
8226
 
8227
                entry = (Elf_External_Verneed *) vstart;
8228
 
8229
                ent.vn_version = BYTE_GET (entry->vn_version);
8230
                ent.vn_cnt     = BYTE_GET (entry->vn_cnt);
8231
                ent.vn_file    = BYTE_GET (entry->vn_file);
8232
                ent.vn_aux     = BYTE_GET (entry->vn_aux);
8233
                ent.vn_next    = BYTE_GET (entry->vn_next);
8234
 
8235
                printf (_("  %#06x: Version: %d"), idx, ent.vn_version);
8236
 
8237
                if (VALID_DYNAMIC_NAME (ent.vn_file))
8238
                  printf (_("  File: %s"), GET_DYNAMIC_NAME (ent.vn_file));
8239
                else
8240
                  printf (_("  File: %lx"), ent.vn_file);
8241
 
8242
                printf (_("  Cnt: %d\n"), ent.vn_cnt);
8243
 
8244
                /* Check for overflow.  */
8245
                if ((unsigned char *)(vstart + ent.vn_aux) < (unsigned char *) vstart
8246
                    || (unsigned char *)(vstart + ent.vn_aux) > (unsigned char *) endbuf)
8247
                  break;
8248
 
8249
                vstart += ent.vn_aux;
8250
 
8251
                for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
8252
                  {
8253
                    Elf_External_Vernaux * eaux;
8254
                    Elf_Internal_Vernaux aux;
8255
 
8256
                    if (vstart + sizeof (*eaux) > endbuf)
8257
                      break;
8258
                    eaux = (Elf_External_Vernaux *) vstart;
8259
 
8260
                    aux.vna_hash  = BYTE_GET (eaux->vna_hash);
8261
                    aux.vna_flags = BYTE_GET (eaux->vna_flags);
8262
                    aux.vna_other = BYTE_GET (eaux->vna_other);
8263
                    aux.vna_name  = BYTE_GET (eaux->vna_name);
8264
                    aux.vna_next  = BYTE_GET (eaux->vna_next);
8265
 
8266
                    if (VALID_DYNAMIC_NAME (aux.vna_name))
8267
                      printf (_("  %#06x:   Name: %s"),
8268
                              isum, GET_DYNAMIC_NAME (aux.vna_name));
8269
                    else
8270
                      printf (_("  %#06x:   Name index: %lx"),
8271
                              isum, aux.vna_name);
8272
 
8273
                    printf (_("  Flags: %s  Version: %d\n"),
8274
                            get_ver_flags (aux.vna_flags), aux.vna_other);
8275
 
8276
                    /* Check for overflow.  */
8277
                    if ((unsigned char *)(vstart + aux.vna_next) < (unsigned char *) vstart
8278
                        || (unsigned char *)(vstart + aux.vna_next) > (unsigned char *) endbuf)
8279
                      break;
8280
 
8281
                    isum   += aux.vna_next;
8282
                    vstart += aux.vna_next;
8283
                  }
8284
                if (j < ent.vn_cnt)
8285
                  printf (_("  Version need aux past end of section\n"));
8286
 
8287
                idx += ent.vn_next;
8288
              }
8289
            if (cnt < section->sh_info)
8290
              printf (_("  Version need past end of section\n"));
8291
 
8292
            free (eneed);
8293
          }
8294
          break;
8295
 
8296
        case SHT_GNU_versym:
8297
          {
8298
            Elf_Internal_Shdr * link_section;
8299
            int total;
8300
            int cnt;
8301
            unsigned char * edata;
8302
            unsigned short * data;
8303
            char * strtab;
8304
            Elf_Internal_Sym * symbols;
8305
            Elf_Internal_Shdr * string_sec;
8306
            long off;
8307
 
8308
            if (section->sh_link >= elf_header.e_shnum)
8309
              break;
8310
 
8311
            link_section = section_headers + section->sh_link;
8312
            total = section->sh_size / sizeof (Elf_External_Versym);
8313
 
8314
            if (link_section->sh_link >= elf_header.e_shnum)
8315
              break;
8316
 
8317
            found = 1;
8318
 
8319
            symbols = GET_ELF_SYMBOLS (file, link_section);
8320
            if (symbols == NULL)
8321
              break;
8322
 
8323
            string_sec = section_headers + link_section->sh_link;
8324
 
8325
            strtab = (char *) get_data (NULL, file, string_sec->sh_offset, 1,
8326
                                        string_sec->sh_size,
8327
                                        _("version string table"));
8328
            if (!strtab)
8329
              {
8330
                free (symbols);
8331
                break;
8332
              }
8333
 
8334
            printf (_("\nVersion symbols section '%s' contains %d entries:\n"),
8335
                    SECTION_NAME (section), total);
8336
 
8337
            printf (_(" Addr: "));
8338
            printf_vma (section->sh_addr);
8339
            printf (_("  Offset: %#08lx  Link: %u (%s)\n"),
8340
                    (unsigned long) section->sh_offset, section->sh_link,
8341
                    SECTION_NAME (link_section));
8342
 
8343
            off = offset_from_vma (file,
8344
                                   version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
8345
                                   total * sizeof (short));
8346
            edata = (unsigned char *) get_data (NULL, file, off, total,
8347
                                                sizeof (short),
8348
                                                _("version symbol data"));
8349
            if (!edata)
8350
              {
8351
                free (strtab);
8352
                free (symbols);
8353
                break;
8354
              }
8355
 
8356
            data = (short unsigned int *) cmalloc (total, sizeof (short));
8357
 
8358
            for (cnt = total; cnt --;)
8359
              data[cnt] = byte_get (edata + cnt * sizeof (short),
8360
                                    sizeof (short));
8361
 
8362
            free (edata);
8363
 
8364
            for (cnt = 0; cnt < total; cnt += 4)
8365
              {
8366
                int j, nn;
8367
                int check_def, check_need;
8368
                char * name;
8369
 
8370
                printf ("  %03x:", cnt);
8371
 
8372
                for (j = 0; (j < 4) && (cnt + j) < total; ++j)
8373
                  switch (data[cnt + j])
8374
                    {
8375
                    case 0:
8376
                      fputs (_("   0 (*local*)    "), stdout);
8377
                      break;
8378
 
8379
                    case 1:
8380
                      fputs (_("   1 (*global*)   "), stdout);
8381
                      break;
8382
 
8383
                    default:
8384
                      nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
8385
                                   data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
8386
 
8387
                      /* If this index value is greater than the size of the symbols
8388
                         array, break to avoid an out-of-bounds read,  */
8389
                      if ((unsigned long)(cnt + j) >=
8390
                         ((unsigned long)link_section->sh_size /
8391
                          (unsigned long)link_section->sh_entsize))
8392
                        {
8393
                          warn (_("invalid index into symbol array\n"));
8394
                          break;
8395
                        }
8396
 
8397
                      check_def = 1;
8398
                      check_need = 1;
8399
                      if (symbols[cnt + j].st_shndx >= elf_header.e_shnum
8400
                          || section_headers[symbols[cnt + j].st_shndx].sh_type
8401
                             != SHT_NOBITS)
8402
                        {
8403
                          if (symbols[cnt + j].st_shndx == SHN_UNDEF)
8404
                            check_def = 0;
8405
                          else
8406
                            check_need = 0;
8407
                        }
8408
 
8409
                      if (check_need
8410
                          && version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
8411
                        {
8412
                          Elf_Internal_Verneed ivn;
8413
                          unsigned long offset;
8414
 
8415
                          offset = offset_from_vma
8416
                            (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
8417
                             sizeof (Elf_External_Verneed));
8418
 
8419
                          do
8420
                            {
8421
                              Elf_Internal_Vernaux ivna;
8422
                              Elf_External_Verneed evn;
8423
                              Elf_External_Vernaux evna;
8424
                              unsigned long a_off;
8425
 
8426
                              get_data (&evn, file, offset, sizeof (evn), 1,
8427
                                        _("version need"));
8428
 
8429
                              ivn.vn_aux  = BYTE_GET (evn.vn_aux);
8430
                              ivn.vn_next = BYTE_GET (evn.vn_next);
8431
 
8432
                              a_off = offset + ivn.vn_aux;
8433
 
8434
                              do
8435
                                {
8436
                                  get_data (&evna, file, a_off, sizeof (evna),
8437
                                            1, _("version need aux (2)"));
8438
 
8439
                                  ivna.vna_next  = BYTE_GET (evna.vna_next);
8440
                                  ivna.vna_other = BYTE_GET (evna.vna_other);
8441
 
8442
                                  a_off += ivna.vna_next;
8443
                                }
8444
                              while (ivna.vna_other != data[cnt + j]
8445
                                     && ivna.vna_next != 0);
8446
 
8447
                              if (ivna.vna_other == data[cnt + j])
8448
                                {
8449
                                  ivna.vna_name = BYTE_GET (evna.vna_name);
8450
 
8451
                                  if (ivna.vna_name >= string_sec->sh_size)
8452
                                    name = _("*invalid*");
8453
                                  else
8454
                                    name = strtab + ivna.vna_name;
8455
                                  nn += printf ("(%s%-*s",
8456
                                                name,
8457
                                                12 - (int) strlen (name),
8458
                                                ")");
8459
                                  check_def = 0;
8460
                                  break;
8461
                                }
8462
 
8463
                              offset += ivn.vn_next;
8464
                            }
8465
                          while (ivn.vn_next);
8466
                        }
8467
 
8468
                      if (check_def && data[cnt + j] != 0x8001
8469
                          && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
8470
                        {
8471
                          Elf_Internal_Verdef ivd;
8472
                          Elf_External_Verdef evd;
8473
                          unsigned long offset;
8474
 
8475
                          offset = offset_from_vma
8476
                            (file, version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
8477
                             sizeof evd);
8478
 
8479
                          do
8480
                            {
8481
                              get_data (&evd, file, offset, sizeof (evd), 1,
8482
                                        _("version def"));
8483
 
8484
                              ivd.vd_next = BYTE_GET (evd.vd_next);
8485
                              ivd.vd_ndx  = BYTE_GET (evd.vd_ndx);
8486
 
8487
                              offset += ivd.vd_next;
8488
                            }
8489
                          while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
8490
                                 && ivd.vd_next != 0);
8491
 
8492
                          if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
8493
                            {
8494
                              Elf_External_Verdaux evda;
8495
                              Elf_Internal_Verdaux ivda;
8496
 
8497
                              ivd.vd_aux = BYTE_GET (evd.vd_aux);
8498
 
8499
                              get_data (&evda, file,
8500
                                        offset - ivd.vd_next + ivd.vd_aux,
8501
                                        sizeof (evda), 1,
8502
                                        _("version def aux"));
8503
 
8504
                              ivda.vda_name = BYTE_GET (evda.vda_name);
8505
 
8506
                              if (ivda.vda_name >= string_sec->sh_size)
8507
                                name = _("*invalid*");
8508
                              else
8509
                                name = strtab + ivda.vda_name;
8510
                              nn += printf ("(%s%-*s",
8511
                                            name,
8512
                                            12 - (int) strlen (name),
8513
                                            ")");
8514
                            }
8515
                        }
8516
 
8517
                      if (nn < 18)
8518
                        printf ("%*c", 18 - nn, ' ');
8519
                    }
8520
 
8521
                putchar ('\n');
8522
              }
8523
 
8524
            free (data);
8525
            free (strtab);
8526
            free (symbols);
8527
          }
8528
          break;
8529
 
8530
        default:
8531
          break;
8532
        }
8533
    }
8534
 
8535
  if (! found)
8536
    printf (_("\nNo version information found in this file.\n"));
8537
 
8538
  return 1;
8539
}
8540
 
8541
static const char *
8542
get_symbol_binding (unsigned int binding)
8543
{
8544
  static char buff[32];
8545
 
8546
  switch (binding)
8547
    {
8548
    case STB_LOCAL:     return "LOCAL";
8549
    case STB_GLOBAL:    return "GLOBAL";
8550
    case STB_WEAK:      return "WEAK";
8551
    default:
8552
      if (binding >= STB_LOPROC && binding <= STB_HIPROC)
8553
        snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
8554
                  binding);
8555
      else if (binding >= STB_LOOS && binding <= STB_HIOS)
8556
        {
8557
          if (binding == STB_GNU_UNIQUE
8558
              && (elf_header.e_ident[EI_OSABI] == ELFOSABI_LINUX
8559
                  /* GNU/Linux is still using the default value 0.  */
8560
                  || elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
8561
            return "UNIQUE";
8562
          snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
8563
        }
8564
      else
8565
        snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
8566
      return buff;
8567
    }
8568
}
8569
 
8570
static const char *
8571
get_symbol_type (unsigned int type)
8572
{
8573
  static char buff[32];
8574
 
8575
  switch (type)
8576
    {
8577
    case STT_NOTYPE:    return "NOTYPE";
8578
    case STT_OBJECT:    return "OBJECT";
8579
    case STT_FUNC:      return "FUNC";
8580
    case STT_SECTION:   return "SECTION";
8581
    case STT_FILE:      return "FILE";
8582
    case STT_COMMON:    return "COMMON";
8583
    case STT_TLS:       return "TLS";
8584
    case STT_RELC:      return "RELC";
8585
    case STT_SRELC:     return "SRELC";
8586
    default:
8587
      if (type >= STT_LOPROC && type <= STT_HIPROC)
8588
        {
8589
          if (elf_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
8590
            return "THUMB_FUNC";
8591
 
8592
          if (elf_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
8593
            return "REGISTER";
8594
 
8595
          if (elf_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
8596
            return "PARISC_MILLI";
8597
 
8598
          snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
8599
        }
8600
      else if (type >= STT_LOOS && type <= STT_HIOS)
8601
        {
8602
          if (elf_header.e_machine == EM_PARISC)
8603
            {
8604
              if (type == STT_HP_OPAQUE)
8605
                return "HP_OPAQUE";
8606
              if (type == STT_HP_STUB)
8607
                return "HP_STUB";
8608
            }
8609
 
8610
          if (type == STT_GNU_IFUNC
8611
              && (elf_header.e_ident[EI_OSABI] == ELFOSABI_LINUX
8612
                  /* GNU/Linux is still using the default value 0.  */
8613
                  || elf_header.e_ident[EI_OSABI] == ELFOSABI_NONE))
8614
            return "IFUNC";
8615
 
8616
          snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
8617
        }
8618
      else
8619
        snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
8620
      return buff;
8621
    }
8622
}
8623
 
8624
static const char *
8625
get_symbol_visibility (unsigned int visibility)
8626
{
8627
  switch (visibility)
8628
    {
8629
    case STV_DEFAULT:   return "DEFAULT";
8630
    case STV_INTERNAL:  return "INTERNAL";
8631
    case STV_HIDDEN:    return "HIDDEN";
8632
    case STV_PROTECTED: return "PROTECTED";
8633
    default: abort ();
8634
    }
8635
}
8636
 
8637
static const char *
8638
get_mips_symbol_other (unsigned int other)
8639
{
8640
  switch (other)
8641
    {
8642
    case STO_OPTIONAL:  return "OPTIONAL";
8643
    case STO_MIPS16:    return "MIPS16";
8644
    case STO_MIPS_PLT:  return "MIPS PLT";
8645
    case STO_MIPS_PIC:  return "MIPS PIC";
8646
    default:            return NULL;
8647
    }
8648
}
8649
 
8650
static const char *
8651
get_ia64_symbol_other (unsigned int other)
8652
{
8653
  if (is_ia64_vms ())
8654
    {
8655
      static char res[32];
8656
 
8657
      res[0] = 0;
8658
 
8659
      /* Function types is for images and .STB files only.  */
8660
      switch (elf_header.e_type)
8661
        {
8662
        case ET_DYN:
8663
        case ET_EXEC:
8664
          switch (VMS_ST_FUNC_TYPE (other))
8665
            {
8666
            case VMS_SFT_CODE_ADDR:
8667
              strcat (res, " CA");
8668
              break;
8669
            case VMS_SFT_SYMV_IDX:
8670
              strcat (res, " VEC");
8671
              break;
8672
            case VMS_SFT_FD:
8673
              strcat (res, " FD");
8674
              break;
8675
            case VMS_SFT_RESERVE:
8676
              strcat (res, " RSV");
8677
              break;
8678
            default:
8679
              abort ();
8680
            }
8681
          break;
8682
        default:
8683
          break;
8684
        }
8685
      switch (VMS_ST_LINKAGE (other))
8686
        {
8687
        case VMS_STL_IGNORE:
8688
          strcat (res, " IGN");
8689
          break;
8690
        case VMS_STL_RESERVE:
8691
          strcat (res, " RSV");
8692
          break;
8693
        case VMS_STL_STD:
8694
          strcat (res, " STD");
8695
          break;
8696
        case VMS_STL_LNK:
8697
          strcat (res, " LNK");
8698
          break;
8699
        default:
8700
          abort ();
8701
        }
8702
 
8703
      if (res[0] != 0)
8704
        return res + 1;
8705
      else
8706
        return res;
8707
    }
8708
  return NULL;
8709
}
8710
 
8711
static const char *
8712
get_symbol_other (unsigned int other)
8713
{
8714
  const char * result = NULL;
8715
  static char buff [32];
8716
 
8717
  if (other == 0)
8718
    return "";
8719
 
8720
  switch (elf_header.e_machine)
8721
    {
8722
    case EM_MIPS:
8723
      result = get_mips_symbol_other (other);
8724
      break;
8725
    case EM_IA_64:
8726
      result = get_ia64_symbol_other (other);
8727
      break;
8728
    default:
8729
      break;
8730
    }
8731
 
8732
  if (result)
8733
    return result;
8734
 
8735
  snprintf (buff, sizeof buff, _("<other>: %x"), other);
8736
  return buff;
8737
}
8738
 
8739
static const char *
8740
get_symbol_index_type (unsigned int type)
8741
{
8742
  static char buff[32];
8743
 
8744
  switch (type)
8745
    {
8746
    case SHN_UNDEF:     return "UND";
8747
    case SHN_ABS:       return "ABS";
8748
    case SHN_COMMON:    return "COM";
8749
    default:
8750
      if (type == SHN_IA_64_ANSI_COMMON
8751
          && elf_header.e_machine == EM_IA_64
8752
          && elf_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
8753
        return "ANSI_COM";
8754
      else if ((elf_header.e_machine == EM_X86_64
8755
                || elf_header.e_machine == EM_L1OM)
8756
               && type == SHN_X86_64_LCOMMON)
8757
        return "LARGE_COM";
8758
      else if ((type == SHN_MIPS_SCOMMON
8759
                && elf_header.e_machine == EM_MIPS)
8760
               || (type == SHN_TIC6X_SCOMMON
8761
                   && elf_header.e_machine == EM_TI_C6000))
8762
        return "SCOM";
8763
      else if (type == SHN_MIPS_SUNDEFINED
8764
               && elf_header.e_machine == EM_MIPS)
8765
        return "SUND";
8766
      else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
8767
        sprintf (buff, "PRC[0x%04x]", type & 0xffff);
8768
      else if (type >= SHN_LOOS && type <= SHN_HIOS)
8769
        sprintf (buff, "OS [0x%04x]", type & 0xffff);
8770
      else if (type >= SHN_LORESERVE)
8771
        sprintf (buff, "RSV[0x%04x]", type & 0xffff);
8772
      else
8773
        sprintf (buff, "%3d", type);
8774
      break;
8775
    }
8776
 
8777
  return buff;
8778
}
8779
 
8780
static bfd_vma *
8781
get_dynamic_data (FILE * file, unsigned int number, unsigned int ent_size)
8782
{
8783
  unsigned char * e_data;
8784
  bfd_vma * i_data;
8785
 
8786
  e_data = (unsigned char *) cmalloc (number, ent_size);
8787
 
8788
  if (e_data == NULL)
8789
    {
8790
      error (_("Out of memory\n"));
8791
      return NULL;
8792
    }
8793
 
8794
  if (fread (e_data, ent_size, number, file) != number)
8795
    {
8796
      error (_("Unable to read in dynamic data\n"));
8797
      return NULL;
8798
    }
8799
 
8800
  i_data = (bfd_vma *) cmalloc (number, sizeof (*i_data));
8801
 
8802
  if (i_data == NULL)
8803
    {
8804
      error (_("Out of memory\n"));
8805
      free (e_data);
8806
      return NULL;
8807
    }
8808
 
8809
  while (number--)
8810
    i_data[number] = byte_get (e_data + number * ent_size, ent_size);
8811
 
8812
  free (e_data);
8813
 
8814
  return i_data;
8815
}
8816
 
8817
static void
8818
print_dynamic_symbol (bfd_vma si, unsigned long hn)
8819
{
8820
  Elf_Internal_Sym * psym;
8821
  int n;
8822
 
8823
  psym = dynamic_symbols + si;
8824
 
8825
  n = print_vma (si, DEC_5);
8826
  if (n < 5)
8827
    fputs ("     " + n, stdout);
8828
  printf (" %3lu: ", hn);
8829
  print_vma (psym->st_value, LONG_HEX);
8830
  putchar (' ');
8831
  print_vma (psym->st_size, DEC_5);
8832
 
8833
  printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
8834
  printf (" %-6s",  get_symbol_binding (ELF_ST_BIND (psym->st_info)));
8835
  printf (" %-7s",  get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
8836
  /* Check to see if any other bits in the st_other field are set.
8837
     Note - displaying this information disrupts the layout of the
8838
     table being generated, but for the moment this case is very
8839
     rare.  */
8840
  if (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other))
8841
    printf (" [%s] ", get_symbol_other (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other)));
8842
  printf (" %3.3s ", get_symbol_index_type (psym->st_shndx));
8843
  if (VALID_DYNAMIC_NAME (psym->st_name))
8844
    print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
8845
  else
8846
    printf (_(" <corrupt: %14ld>"), psym->st_name);
8847
  putchar ('\n');
8848
}
8849
 
8850
/* Dump the symbol table.  */
8851
static int
8852
process_symbol_table (FILE * file)
8853
{
8854
  Elf_Internal_Shdr * section;
8855
  bfd_vma nbuckets = 0;
8856
  bfd_vma nchains = 0;
8857
  bfd_vma * buckets = NULL;
8858
  bfd_vma * chains = NULL;
8859
  bfd_vma ngnubuckets = 0;
8860
  bfd_vma * gnubuckets = NULL;
8861
  bfd_vma * gnuchains = NULL;
8862
  bfd_vma gnusymidx = 0;
8863
 
8864
  if (!do_syms && !do_dyn_syms && !do_histogram)
8865
    return 1;
8866
 
8867
  if (dynamic_info[DT_HASH]
8868
      && (do_histogram
8869
          || (do_using_dynamic
8870
              && !do_dyn_syms
8871
              && dynamic_strings != NULL)))
8872
    {
8873
      unsigned char nb[8];
8874
      unsigned char nc[8];
8875
      int hash_ent_size = 4;
8876
 
8877
      if ((elf_header.e_machine == EM_ALPHA
8878
           || elf_header.e_machine == EM_S390
8879
           || elf_header.e_machine == EM_S390_OLD)
8880
          && elf_header.e_ident[EI_CLASS] == ELFCLASS64)
8881
        hash_ent_size = 8;
8882
 
8883
      if (fseek (file,
8884
                 (archive_file_offset
8885
                  + offset_from_vma (file, dynamic_info[DT_HASH],
8886
                                     sizeof nb + sizeof nc)),
8887
                 SEEK_SET))
8888
        {
8889
          error (_("Unable to seek to start of dynamic information\n"));
8890
          goto no_hash;
8891
        }
8892
 
8893
      if (fread (nb, hash_ent_size, 1, file) != 1)
8894
        {
8895
          error (_("Failed to read in number of buckets\n"));
8896
          goto no_hash;
8897
        }
8898
 
8899
      if (fread (nc, hash_ent_size, 1, file) != 1)
8900
        {
8901
          error (_("Failed to read in number of chains\n"));
8902
          goto no_hash;
8903
        }
8904
 
8905
      nbuckets = byte_get (nb, hash_ent_size);
8906
      nchains  = byte_get (nc, hash_ent_size);
8907
 
8908
      buckets = get_dynamic_data (file, nbuckets, hash_ent_size);
8909
      chains  = get_dynamic_data (file, nchains, hash_ent_size);
8910
 
8911
    no_hash:
8912
      if (buckets == NULL || chains == NULL)
8913
        {
8914
          if (do_using_dynamic)
8915
            return 0;
8916
          free (buckets);
8917
          free (chains);
8918
          buckets = NULL;
8919
          chains = NULL;
8920
          nbuckets = 0;
8921
          nchains = 0;
8922
        }
8923
    }
8924
 
8925
  if (dynamic_info_DT_GNU_HASH
8926
      && (do_histogram
8927
          || (do_using_dynamic
8928
              && !do_dyn_syms
8929
              && dynamic_strings != NULL)))
8930
    {
8931
      unsigned char nb[16];
8932
      bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
8933
      bfd_vma buckets_vma;
8934
 
8935
      if (fseek (file,
8936
                 (archive_file_offset
8937
                  + offset_from_vma (file, dynamic_info_DT_GNU_HASH,
8938
                                     sizeof nb)),
8939
                 SEEK_SET))
8940
        {
8941
          error (_("Unable to seek to start of dynamic information\n"));
8942
          goto no_gnu_hash;
8943
        }
8944
 
8945
      if (fread (nb, 16, 1, file) != 1)
8946
        {
8947
          error (_("Failed to read in number of buckets\n"));
8948
          goto no_gnu_hash;
8949
        }
8950
 
8951
      ngnubuckets = byte_get (nb, 4);
8952
      gnusymidx = byte_get (nb + 4, 4);
8953
      bitmaskwords = byte_get (nb + 8, 4);
8954
      buckets_vma = dynamic_info_DT_GNU_HASH + 16;
8955
      if (is_32bit_elf)
8956
        buckets_vma += bitmaskwords * 4;
8957
      else
8958
        buckets_vma += bitmaskwords * 8;
8959
 
8960
      if (fseek (file,
8961
                 (archive_file_offset
8962
                  + offset_from_vma (file, buckets_vma, 4)),
8963
                 SEEK_SET))
8964
        {
8965
          error (_("Unable to seek to start of dynamic information\n"));
8966
          goto no_gnu_hash;
8967
        }
8968
 
8969
      gnubuckets = get_dynamic_data (file, ngnubuckets, 4);
8970
 
8971
      if (gnubuckets == NULL)
8972
        goto no_gnu_hash;
8973
 
8974
      for (i = 0; i < ngnubuckets; i++)
8975
        if (gnubuckets[i] != 0)
8976
          {
8977
            if (gnubuckets[i] < gnusymidx)
8978
              return 0;
8979
 
8980
            if (maxchain == 0xffffffff || gnubuckets[i] > maxchain)
8981
              maxchain = gnubuckets[i];
8982
          }
8983
 
8984
      if (maxchain == 0xffffffff)
8985
        goto no_gnu_hash;
8986
 
8987
      maxchain -= gnusymidx;
8988
 
8989
      if (fseek (file,
8990
                 (archive_file_offset
8991
                  + offset_from_vma (file, buckets_vma
8992
                                           + 4 * (ngnubuckets + maxchain), 4)),
8993
                 SEEK_SET))
8994
        {
8995
          error (_("Unable to seek to start of dynamic information\n"));
8996
          goto no_gnu_hash;
8997
        }
8998
 
8999
      do
9000
        {
9001
          if (fread (nb, 4, 1, file) != 1)
9002
            {
9003
              error (_("Failed to determine last chain length\n"));
9004
              goto no_gnu_hash;
9005
            }
9006
 
9007
          if (maxchain + 1 == 0)
9008
            goto no_gnu_hash;
9009
 
9010
          ++maxchain;
9011
        }
9012
      while ((byte_get (nb, 4) & 1) == 0);
9013
 
9014
      if (fseek (file,
9015
                 (archive_file_offset
9016
                  + offset_from_vma (file, buckets_vma + 4 * ngnubuckets, 4)),
9017
                 SEEK_SET))
9018
        {
9019
          error (_("Unable to seek to start of dynamic information\n"));
9020
          goto no_gnu_hash;
9021
        }
9022
 
9023
      gnuchains = get_dynamic_data (file, maxchain, 4);
9024
 
9025
    no_gnu_hash:
9026
      if (gnuchains == NULL)
9027
        {
9028
          free (gnubuckets);
9029
          gnubuckets = NULL;
9030
          ngnubuckets = 0;
9031
          if (do_using_dynamic)
9032
            return 0;
9033
        }
9034
    }
9035
 
9036
  if ((dynamic_info[DT_HASH] || dynamic_info_DT_GNU_HASH)
9037
      && do_syms
9038
      && do_using_dynamic
9039
      && dynamic_strings != NULL)
9040
    {
9041
      unsigned long hn;
9042
 
9043
      if (dynamic_info[DT_HASH])
9044
        {
9045
          bfd_vma si;
9046
 
9047
          printf (_("\nSymbol table for image:\n"));
9048
          if (is_32bit_elf)
9049
            printf (_("  Num Buc:    Value  Size   Type   Bind Vis      Ndx Name\n"));
9050
          else
9051
            printf (_("  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name\n"));
9052
 
9053
          for (hn = 0; hn < nbuckets; hn++)
9054
            {
9055
              if (! buckets[hn])
9056
                continue;
9057
 
9058
              for (si = buckets[hn]; si < nchains && si > 0; si = chains[si])
9059
                print_dynamic_symbol (si, hn);
9060
            }
9061
        }
9062
 
9063
      if (dynamic_info_DT_GNU_HASH)
9064
        {
9065
          printf (_("\nSymbol table of `.gnu.hash' for image:\n"));
9066
          if (is_32bit_elf)
9067
            printf (_("  Num Buc:    Value  Size   Type   Bind Vis      Ndx Name\n"));
9068
          else
9069
            printf (_("  Num Buc:    Value          Size   Type   Bind Vis      Ndx Name\n"));
9070
 
9071
          for (hn = 0; hn < ngnubuckets; ++hn)
9072
            if (gnubuckets[hn] != 0)
9073
              {
9074
                bfd_vma si = gnubuckets[hn];
9075
                bfd_vma off = si - gnusymidx;
9076
 
9077
                do
9078
                  {
9079
                    print_dynamic_symbol (si, hn);
9080
                    si++;
9081
                  }
9082
                while ((gnuchains[off++] & 1) == 0);
9083
              }
9084
        }
9085
    }
9086
  else if (do_dyn_syms || (do_syms && !do_using_dynamic))
9087
    {
9088
      unsigned int i;
9089
 
9090
      for (i = 0, section = section_headers;
9091
           i < elf_header.e_shnum;
9092
           i++, section++)
9093
        {
9094
          unsigned int si;
9095
          char * strtab = NULL;
9096
          unsigned long int strtab_size = 0;
9097
          Elf_Internal_Sym * symtab;
9098
          Elf_Internal_Sym * psym;
9099
 
9100
          if ((section->sh_type != SHT_SYMTAB
9101
               && section->sh_type != SHT_DYNSYM)
9102
              || (!do_syms
9103
                  && section->sh_type == SHT_SYMTAB))
9104
            continue;
9105
 
9106
          if (section->sh_entsize == 0)
9107
            {
9108
              printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
9109
                      SECTION_NAME (section));
9110
              continue;
9111
            }
9112
 
9113
          printf (_("\nSymbol table '%s' contains %lu entries:\n"),
9114
                  SECTION_NAME (section),
9115
                  (unsigned long) (section->sh_size / section->sh_entsize));
9116
 
9117
          if (is_32bit_elf)
9118
            printf (_("   Num:    Value  Size Type    Bind   Vis      Ndx Name\n"));
9119
          else
9120
            printf (_("   Num:    Value          Size Type    Bind   Vis      Ndx Name\n"));
9121
 
9122
          symtab = GET_ELF_SYMBOLS (file, section);
9123
          if (symtab == NULL)
9124
            continue;
9125
 
9126
          if (section->sh_link == elf_header.e_shstrndx)
9127
            {
9128
              strtab = string_table;
9129
              strtab_size = string_table_length;
9130
            }
9131
          else if (section->sh_link < elf_header.e_shnum)
9132
            {
9133
              Elf_Internal_Shdr * string_sec;
9134
 
9135
              string_sec = section_headers + section->sh_link;
9136
 
9137
              strtab = (char *) get_data (NULL, file, string_sec->sh_offset,
9138
                                          1, string_sec->sh_size,
9139
                                          _("string table"));
9140
              strtab_size = strtab != NULL ? string_sec->sh_size : 0;
9141
            }
9142
 
9143
          for (si = 0, psym = symtab;
9144
               si < section->sh_size / section->sh_entsize;
9145
               si++, psym++)
9146
            {
9147
              printf ("%6d: ", si);
9148
              print_vma (psym->st_value, LONG_HEX);
9149
              putchar (' ');
9150
              print_vma (psym->st_size, DEC_5);
9151
              printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
9152
              printf (" %-6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
9153
              printf (" %-7s", get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
9154
              /* Check to see if any other bits in the st_other field are set.
9155
                 Note - displaying this information disrupts the layout of the
9156
                 table being generated, but for the moment this case is very rare.  */
9157
              if (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other))
9158
                printf (" [%s] ", get_symbol_other (psym->st_other ^ ELF_ST_VISIBILITY (psym->st_other)));
9159
              printf (" %4s ", get_symbol_index_type (psym->st_shndx));
9160
              print_symbol (25, psym->st_name < strtab_size
9161
                            ? strtab + psym->st_name : _("<corrupt>"));
9162
 
9163
              if (section->sh_type == SHT_DYNSYM &&
9164
                  version_info[DT_VERSIONTAGIDX (DT_VERSYM)] != 0)
9165
                {
9166
                  unsigned char data[2];
9167
                  unsigned short vers_data;
9168
                  unsigned long offset;
9169
                  int is_nobits;
9170
                  int check_def;
9171
 
9172
                  offset = offset_from_vma
9173
                    (file, version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
9174
                     sizeof data + si * sizeof (vers_data));
9175
 
9176
                  get_data (&data, file, offset + si * sizeof (vers_data),
9177
                            sizeof (data), 1, _("version data"));
9178
 
9179
                  vers_data = byte_get (data, 2);
9180
 
9181
                  is_nobits = (psym->st_shndx < elf_header.e_shnum
9182
                               && section_headers[psym->st_shndx].sh_type
9183
                                  == SHT_NOBITS);
9184
 
9185
                  check_def = (psym->st_shndx != SHN_UNDEF);
9186
 
9187
                  if ((vers_data & VERSYM_HIDDEN) || vers_data > 1)
9188
                    {
9189
                      if (version_info[DT_VERSIONTAGIDX (DT_VERNEED)]
9190
                          && (is_nobits || ! check_def))
9191
                        {
9192
                          Elf_External_Verneed evn;
9193
                          Elf_Internal_Verneed ivn;
9194
                          Elf_Internal_Vernaux ivna;
9195
 
9196
                          /* We must test both.  */
9197
                          offset = offset_from_vma
9198
                            (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
9199
                             sizeof evn);
9200
 
9201
                          do
9202
                            {
9203
                              unsigned long vna_off;
9204
 
9205
                              get_data (&evn, file, offset, sizeof (evn), 1,
9206
                                        _("version need"));
9207
 
9208
                              ivn.vn_aux  = BYTE_GET (evn.vn_aux);
9209
                              ivn.vn_next = BYTE_GET (evn.vn_next);
9210
 
9211
                              vna_off = offset + ivn.vn_aux;
9212
 
9213
                              do
9214
                                {
9215
                                  Elf_External_Vernaux evna;
9216
 
9217
                                  get_data (&evna, file, vna_off,
9218
                                            sizeof (evna), 1,
9219
                                            _("version need aux (3)"));
9220
 
9221
                                  ivna.vna_other = BYTE_GET (evna.vna_other);
9222
                                  ivna.vna_next  = BYTE_GET (evna.vna_next);
9223
                                  ivna.vna_name  = BYTE_GET (evna.vna_name);
9224
 
9225
                                  vna_off += ivna.vna_next;
9226
                                }
9227
                              while (ivna.vna_other != vers_data
9228
                                     && ivna.vna_next != 0);
9229
 
9230
                              if (ivna.vna_other == vers_data)
9231
                                break;
9232
 
9233
                              offset += ivn.vn_next;
9234
                            }
9235
                          while (ivn.vn_next != 0);
9236
 
9237
                          if (ivna.vna_other == vers_data)
9238
                            {
9239
                              printf ("@%s (%d)",
9240
                                      ivna.vna_name < strtab_size
9241
                                      ? strtab + ivna.vna_name : _("<corrupt>"),
9242
                                      ivna.vna_other);
9243
                              check_def = 0;
9244
                            }
9245
                          else if (! is_nobits)
9246
                            error (_("bad dynamic symbol\n"));
9247
                          else
9248
                            check_def = 1;
9249
                        }
9250
 
9251
                      if (check_def)
9252
                        {
9253
                          if (vers_data != 0x8001
9254
                              && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
9255
                            {
9256
                              Elf_Internal_Verdef ivd;
9257
                              Elf_Internal_Verdaux ivda;
9258
                              Elf_External_Verdaux evda;
9259
                              unsigned long off;
9260
 
9261
                              off = offset_from_vma
9262
                                (file,
9263
                                 version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
9264
                                 sizeof (Elf_External_Verdef));
9265
 
9266
                              do
9267
                                {
9268
                                  Elf_External_Verdef evd;
9269
 
9270
                                  get_data (&evd, file, off, sizeof (evd),
9271
                                            1, _("version def"));
9272
 
9273
                                  ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
9274
                                  ivd.vd_aux = BYTE_GET (evd.vd_aux);
9275
                                  ivd.vd_next = BYTE_GET (evd.vd_next);
9276
 
9277
                                  off += ivd.vd_next;
9278
                                }
9279
                              while (ivd.vd_ndx != (vers_data & VERSYM_VERSION)
9280
                                     && ivd.vd_next != 0);
9281
 
9282
                              off -= ivd.vd_next;
9283
                              off += ivd.vd_aux;
9284
 
9285
                              get_data (&evda, file, off, sizeof (evda),
9286
                                        1, _("version def aux"));
9287
 
9288
                              ivda.vda_name = BYTE_GET (evda.vda_name);
9289
 
9290
                              if (psym->st_name != ivda.vda_name)
9291
                                printf ((vers_data & VERSYM_HIDDEN)
9292
                                        ? "@%s" : "@@%s",
9293
                                        ivda.vda_name < strtab_size
9294
                                        ? strtab + ivda.vda_name : _("<corrupt>"));
9295
                            }
9296
                        }
9297
                    }
9298
                }
9299
 
9300
              putchar ('\n');
9301
            }
9302
 
9303
          free (symtab);
9304
          if (strtab != string_table)
9305
            free (strtab);
9306
        }
9307
    }
9308
  else if (do_syms)
9309
    printf
9310
      (_("\nDynamic symbol information is not available for displaying symbols.\n"));
9311
 
9312
  if (do_histogram && buckets != NULL)
9313
    {
9314
      unsigned long * lengths;
9315
      unsigned long * counts;
9316
      unsigned long hn;
9317
      bfd_vma si;
9318
      unsigned long maxlength = 0;
9319
      unsigned long nzero_counts = 0;
9320
      unsigned long nsyms = 0;
9321
 
9322
      printf (_("\nHistogram for bucket list length (total of %lu buckets):\n"),
9323
              (unsigned long) nbuckets);
9324
      printf (_(" Length  Number     %% of total  Coverage\n"));
9325
 
9326
      lengths = (unsigned long *) calloc (nbuckets, sizeof (*lengths));
9327
      if (lengths == NULL)
9328
        {
9329
          error (_("Out of memory\n"));
9330
          return 0;
9331
        }
9332
      for (hn = 0; hn < nbuckets; ++hn)
9333
        {
9334
          for (si = buckets[hn]; si > 0 && si < nchains; si = chains[si])
9335
            {
9336
              ++nsyms;
9337
              if (maxlength < ++lengths[hn])
9338
                ++maxlength;
9339
            }
9340
        }
9341
 
9342
      counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
9343
      if (counts == NULL)
9344
        {
9345
          error (_("Out of memory\n"));
9346
          return 0;
9347
        }
9348
 
9349
      for (hn = 0; hn < nbuckets; ++hn)
9350
        ++counts[lengths[hn]];
9351
 
9352
      if (nbuckets > 0)
9353
        {
9354
          unsigned long i;
9355
          printf ("      0  %-10lu (%5.1f%%)\n",
9356
                  counts[0], (counts[0] * 100.0) / nbuckets);
9357
          for (i = 1; i <= maxlength; ++i)
9358
            {
9359
              nzero_counts += counts[i] * i;
9360
              printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
9361
                      i, counts[i], (counts[i] * 100.0) / nbuckets,
9362
                      (nzero_counts * 100.0) / nsyms);
9363
            }
9364
        }
9365
 
9366
      free (counts);
9367
      free (lengths);
9368
    }
9369
 
9370
  if (buckets != NULL)
9371
    {
9372
      free (buckets);
9373
      free (chains);
9374
    }
9375
 
9376
  if (do_histogram && gnubuckets != NULL)
9377
    {
9378
      unsigned long * lengths;
9379
      unsigned long * counts;
9380
      unsigned long hn;
9381
      unsigned long maxlength = 0;
9382
      unsigned long nzero_counts = 0;
9383
      unsigned long nsyms = 0;
9384
 
9385
      lengths = (unsigned long *) calloc (ngnubuckets, sizeof (*lengths));
9386
      if (lengths == NULL)
9387
        {
9388
          error (_("Out of memory\n"));
9389
          return 0;
9390
        }
9391
 
9392
      printf (_("\nHistogram for `.gnu.hash' bucket list length (total of %lu buckets):\n"),
9393
              (unsigned long) ngnubuckets);
9394
      printf (_(" Length  Number     %% of total  Coverage\n"));
9395
 
9396
      for (hn = 0; hn < ngnubuckets; ++hn)
9397
        if (gnubuckets[hn] != 0)
9398
          {
9399
            bfd_vma off, length = 1;
9400
 
9401
            for (off = gnubuckets[hn] - gnusymidx;
9402
                 (gnuchains[off] & 1) == 0; ++off)
9403
              ++length;
9404
            lengths[hn] = length;
9405
            if (length > maxlength)
9406
              maxlength = length;
9407
            nsyms += length;
9408
          }
9409
 
9410
      counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
9411
      if (counts == NULL)
9412
        {
9413
          error (_("Out of memory\n"));
9414
          return 0;
9415
        }
9416
 
9417
      for (hn = 0; hn < ngnubuckets; ++hn)
9418
        ++counts[lengths[hn]];
9419
 
9420
      if (ngnubuckets > 0)
9421
        {
9422
          unsigned long j;
9423
          printf ("      0  %-10lu (%5.1f%%)\n",
9424
                  counts[0], (counts[0] * 100.0) / ngnubuckets);
9425
          for (j = 1; j <= maxlength; ++j)
9426
            {
9427
              nzero_counts += counts[j] * j;
9428
              printf ("%7lu  %-10lu (%5.1f%%)    %5.1f%%\n",
9429
                      j, counts[j], (counts[j] * 100.0) / ngnubuckets,
9430
                      (nzero_counts * 100.0) / nsyms);
9431
            }
9432
        }
9433
 
9434
      free (counts);
9435
      free (lengths);
9436
      free (gnubuckets);
9437
      free (gnuchains);
9438
    }
9439
 
9440
  return 1;
9441
}
9442
 
9443
static int
9444
process_syminfo (FILE * file ATTRIBUTE_UNUSED)
9445
{
9446
  unsigned int i;
9447
 
9448
  if (dynamic_syminfo == NULL
9449
      || !do_dynamic)
9450
    /* No syminfo, this is ok.  */
9451
    return 1;
9452
 
9453
  /* There better should be a dynamic symbol section.  */
9454
  if (dynamic_symbols == NULL || dynamic_strings == NULL)
9455
    return 0;
9456
 
9457
  if (dynamic_addr)
9458
    printf (_("\nDynamic info segment at offset 0x%lx contains %d entries:\n"),
9459
            dynamic_syminfo_offset, dynamic_syminfo_nent);
9460
 
9461
  printf (_(" Num: Name                           BoundTo     Flags\n"));
9462
  for (i = 0; i < dynamic_syminfo_nent; ++i)
9463
    {
9464
      unsigned short int flags = dynamic_syminfo[i].si_flags;
9465
 
9466
      printf ("%4d: ", i);
9467
      if (VALID_DYNAMIC_NAME (dynamic_symbols[i].st_name))
9468
        print_symbol (30, GET_DYNAMIC_NAME (dynamic_symbols[i].st_name));
9469
      else
9470
        printf (_("<corrupt: %19ld>"), dynamic_symbols[i].st_name);
9471
      putchar (' ');
9472
 
9473
      switch (dynamic_syminfo[i].si_boundto)
9474
        {
9475
        case SYMINFO_BT_SELF:
9476
          fputs ("SELF       ", stdout);
9477
          break;
9478
        case SYMINFO_BT_PARENT:
9479
          fputs ("PARENT     ", stdout);
9480
          break;
9481
        default:
9482
          if (dynamic_syminfo[i].si_boundto > 0
9483
              && dynamic_syminfo[i].si_boundto < dynamic_nent
9484
              && VALID_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val))
9485
            {
9486
              print_symbol (10, GET_DYNAMIC_NAME (dynamic_section[dynamic_syminfo[i].si_boundto].d_un.d_val));
9487
              putchar (' ' );
9488
            }
9489
          else
9490
            printf ("%-10d ", dynamic_syminfo[i].si_boundto);
9491
          break;
9492
        }
9493
 
9494
      if (flags & SYMINFO_FLG_DIRECT)
9495
        printf (" DIRECT");
9496
      if (flags & SYMINFO_FLG_PASSTHRU)
9497
        printf (" PASSTHRU");
9498
      if (flags & SYMINFO_FLG_COPY)
9499
        printf (" COPY");
9500
      if (flags & SYMINFO_FLG_LAZYLOAD)
9501
        printf (" LAZYLOAD");
9502
 
9503
      puts ("");
9504
    }
9505
 
9506
  return 1;
9507
}
9508
 
9509
/* Check to see if the given reloc needs to be handled in a target specific
9510
   manner.  If so then process the reloc and return TRUE otherwise return
9511
   FALSE.  */
9512
 
9513
static bfd_boolean
9514
target_specific_reloc_handling (Elf_Internal_Rela * reloc,
9515
                                unsigned char *     start,
9516
                                Elf_Internal_Sym *  symtab)
9517
{
9518
  unsigned int reloc_type = get_reloc_type (reloc->r_info);
9519
 
9520
  switch (elf_header.e_machine)
9521
    {
9522
    case EM_MN10300:
9523
    case EM_CYGNUS_MN10300:
9524
      {
9525
        static Elf_Internal_Sym * saved_sym = NULL;
9526
 
9527
        switch (reloc_type)
9528
          {
9529
          case 34: /* R_MN10300_ALIGN */
9530
            return TRUE;
9531
          case 33: /* R_MN10300_SYM_DIFF */
9532
            saved_sym = symtab + get_reloc_symindex (reloc->r_info);
9533
            return TRUE;
9534
          case 1: /* R_MN10300_32 */
9535
          case 2: /* R_MN10300_16 */
9536
            if (saved_sym != NULL)
9537
              {
9538
                bfd_vma value;
9539
 
9540
                value = reloc->r_addend
9541
                  + (symtab[get_reloc_symindex (reloc->r_info)].st_value
9542
                     - saved_sym->st_value);
9543
 
9544
                byte_put (start + reloc->r_offset, value, reloc_type == 1 ? 4 : 2);
9545
 
9546
                saved_sym = NULL;
9547
                return TRUE;
9548
              }
9549
            break;
9550
          default:
9551
            if (saved_sym != NULL)
9552
              error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc"));
9553
            break;
9554
          }
9555
        break;
9556
      }
9557
    }
9558
 
9559
  return FALSE;
9560
}
9561
 
9562
/* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
9563
   DWARF debug sections.  This is a target specific test.  Note - we do not
9564
   go through the whole including-target-headers-multiple-times route, (as
9565
   we have already done with <elf/h8.h>) because this would become very
9566
   messy and even then this function would have to contain target specific
9567
   information (the names of the relocs instead of their numeric values).
9568
   FIXME: This is not the correct way to solve this problem.  The proper way
9569
   is to have target specific reloc sizing and typing functions created by
9570
   the reloc-macros.h header, in the same way that it already creates the
9571
   reloc naming functions.  */
9572
 
9573
static bfd_boolean
9574
is_32bit_abs_reloc (unsigned int reloc_type)
9575
{
9576
  switch (elf_header.e_machine)
9577
    {
9578
    case EM_386:
9579
    case EM_486:
9580
      return reloc_type == 1; /* R_386_32.  */
9581
    case EM_68K:
9582
      return reloc_type == 1; /* R_68K_32.  */
9583
    case EM_860:
9584
      return reloc_type == 1; /* R_860_32.  */
9585
    case EM_960:
9586
      return reloc_type == 2; /* R_960_32.  */
9587
    case EM_ALPHA:
9588
      return reloc_type == 1; /* R_ALPHA_REFLONG.  */
9589
    case EM_ARC:
9590
      return reloc_type == 1; /* R_ARC_32.  */
9591
    case EM_ARM:
9592
      return reloc_type == 2; /* R_ARM_ABS32 */
9593
    case EM_AVR_OLD:
9594
    case EM_AVR:
9595
      return reloc_type == 1;
9596
    case EM_BLACKFIN:
9597
      return reloc_type == 0x12; /* R_byte4_data.  */
9598
    case EM_CRIS:
9599
      return reloc_type == 3; /* R_CRIS_32.  */
9600
    case EM_CR16:
9601
    case EM_CR16_OLD:
9602
      return reloc_type == 3; /* R_CR16_NUM32.  */
9603
    case EM_CRX:
9604
      return reloc_type == 15; /* R_CRX_NUM32.  */
9605
    case EM_CYGNUS_FRV:
9606
      return reloc_type == 1;
9607
    case EM_CYGNUS_D10V:
9608
    case EM_D10V:
9609
      return reloc_type == 6; /* R_D10V_32.  */
9610
    case EM_CYGNUS_D30V:
9611
    case EM_D30V:
9612
      return reloc_type == 12; /* R_D30V_32_NORMAL.  */
9613
    case EM_DLX:
9614
      return reloc_type == 3; /* R_DLX_RELOC_32.  */
9615
    case EM_CYGNUS_FR30:
9616
    case EM_FR30:
9617
      return reloc_type == 3; /* R_FR30_32.  */
9618
    case EM_H8S:
9619
    case EM_H8_300:
9620
    case EM_H8_300H:
9621
      return reloc_type == 1; /* R_H8_DIR32.  */
9622
    case EM_IA_64:
9623
      return reloc_type == 0x65; /* R_IA64_SECREL32LSB.  */
9624
    case EM_IP2K_OLD:
9625
    case EM_IP2K:
9626
      return reloc_type == 2; /* R_IP2K_32.  */
9627
    case EM_IQ2000:
9628
      return reloc_type == 2; /* R_IQ2000_32.  */
9629
    case EM_LATTICEMICO32:
9630
      return reloc_type == 3; /* R_LM32_32.  */
9631
    case EM_M32C_OLD:
9632
    case EM_M32C:
9633
      return reloc_type == 3; /* R_M32C_32.  */
9634
    case EM_M32R:
9635
      return reloc_type == 34; /* R_M32R_32_RELA.  */
9636
    case EM_MCORE:
9637
      return reloc_type == 1; /* R_MCORE_ADDR32.  */
9638
    case EM_CYGNUS_MEP:
9639
      return reloc_type == 4; /* R_MEP_32.  */
9640
    case EM_MICROBLAZE:
9641
      return reloc_type == 1; /* R_MICROBLAZE_32.  */
9642
    case EM_MIPS:
9643
      return reloc_type == 2; /* R_MIPS_32.  */
9644
    case EM_MMIX:
9645
      return reloc_type == 4; /* R_MMIX_32.  */
9646
    case EM_CYGNUS_MN10200:
9647
    case EM_MN10200:
9648
      return reloc_type == 1; /* R_MN10200_32.  */
9649
    case EM_CYGNUS_MN10300:
9650
    case EM_MN10300:
9651
      return reloc_type == 1; /* R_MN10300_32.  */
9652
    case EM_MOXIE:
9653
      return reloc_type == 1; /* R_MOXIE_32.  */
9654
    case EM_MSP430_OLD:
9655
    case EM_MSP430:
9656
      return reloc_type == 1; /* R_MSP43_32.  */
9657
    case EM_MT:
9658
      return reloc_type == 2; /* R_MT_32.  */
9659
    case EM_ALTERA_NIOS2:
9660
    case EM_NIOS32:
9661
      return reloc_type == 1; /* R_NIOS_32.  */
9662
    case EM_OPEN8:
9663
      return reloc_type == 1;
9664
    case EM_OPENRISC:
9665
    case EM_OR32:
9666
      return reloc_type == 1; /* R_OR32_32.  */
9667
    case EM_PARISC:
9668
      return (reloc_type == 1 /* R_PARISC_DIR32.  */
9669
              || reloc_type == 41); /* R_PARISC_SECREL32.  */
9670
    case EM_PJ:
9671
    case EM_PJ_OLD:
9672
      return reloc_type == 1; /* R_PJ_DATA_DIR32.  */
9673
    case EM_PPC64:
9674
      return reloc_type == 1; /* R_PPC64_ADDR32.  */
9675
    case EM_PPC:
9676
      return reloc_type == 1; /* R_PPC_ADDR32.  */
9677
    case EM_RX:
9678
      return reloc_type == 1; /* R_RX_DIR32.  */
9679
    case EM_S370:
9680
      return reloc_type == 1; /* R_I370_ADDR31.  */
9681
    case EM_S390_OLD:
9682
    case EM_S390:
9683
      return reloc_type == 4; /* R_S390_32.  */
9684
    case EM_SCORE:
9685
      return reloc_type == 8; /* R_SCORE_ABS32.  */
9686
    case EM_SH:
9687
      return reloc_type == 1; /* R_SH_DIR32.  */
9688
    case EM_SPARC32PLUS:
9689
    case EM_SPARCV9:
9690
    case EM_SPARC:
9691
      return reloc_type == 3 /* R_SPARC_32.  */
9692
        || reloc_type == 23; /* R_SPARC_UA32.  */
9693
    case EM_SPU:
9694
      return reloc_type == 6; /* R_SPU_ADDR32 */
9695
    case EM_TI_C6000:
9696
      return reloc_type == 1; /* R_C6000_ABS32.  */
9697
    case EM_CYGNUS_V850:
9698
    case EM_V850:
9699
      return reloc_type == 6; /* R_V850_ABS32.  */
9700
    case EM_VAX:
9701
      return reloc_type == 1; /* R_VAX_32.  */
9702
    case EM_X86_64:
9703
    case EM_L1OM:
9704
      return reloc_type == 10; /* R_X86_64_32.  */
9705
    case EM_XC16X:
9706
    case EM_C166:
9707
      return reloc_type == 3; /* R_XC16C_ABS_32.  */
9708
    case EM_XSTORMY16:
9709
      return reloc_type == 1; /* R_XSTROMY16_32.  */
9710
    case EM_XTENSA_OLD:
9711
    case EM_XTENSA:
9712
      return reloc_type == 1; /* R_XTENSA_32.  */
9713
    default:
9714
      error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
9715
             elf_header.e_machine);
9716
      abort ();
9717
    }
9718
}
9719
 
9720
/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
9721
   a 32-bit pc-relative RELA relocation used in DWARF debug sections.  */
9722
 
9723
static bfd_boolean
9724
is_32bit_pcrel_reloc (unsigned int reloc_type)
9725
{
9726
  switch (elf_header.e_machine)
9727
    {
9728
    case EM_386:
9729
    case EM_486:
9730
      return reloc_type == 2;  /* R_386_PC32.  */
9731
    case EM_68K:
9732
      return reloc_type == 4;  /* R_68K_PC32.  */
9733
    case EM_ALPHA:
9734
      return reloc_type == 10; /* R_ALPHA_SREL32.  */
9735
    case EM_ARM:
9736
      return reloc_type == 3;  /* R_ARM_REL32 */
9737
    case EM_MICROBLAZE:
9738
      return reloc_type == 2;  /* R_MICROBLAZE_32_PCREL.  */
9739
    case EM_PARISC:
9740
      return reloc_type == 9;  /* R_PARISC_PCREL32.  */
9741
    case EM_PPC:
9742
      return reloc_type == 26; /* R_PPC_REL32.  */
9743
    case EM_PPC64:
9744
      return reloc_type == 26; /* R_PPC64_REL32.  */
9745
    case EM_S390_OLD:
9746
    case EM_S390:
9747
      return reloc_type == 5;  /* R_390_PC32.  */
9748
    case EM_SH:
9749
      return reloc_type == 2;  /* R_SH_REL32.  */
9750
    case EM_SPARC32PLUS:
9751
    case EM_SPARCV9:
9752
    case EM_SPARC:
9753
      return reloc_type == 6;  /* R_SPARC_DISP32.  */
9754
    case EM_SPU:
9755
      return reloc_type == 13; /* R_SPU_REL32.  */
9756
    case EM_X86_64:
9757
    case EM_L1OM:
9758
      return reloc_type == 2;  /* R_X86_64_PC32.  */
9759
    case EM_XTENSA_OLD:
9760
    case EM_XTENSA:
9761
      return reloc_type == 14; /* R_XTENSA_32_PCREL.  */
9762
    default:
9763
      /* Do not abort or issue an error message here.  Not all targets use
9764
         pc-relative 32-bit relocs in their DWARF debug information and we
9765
         have already tested for target coverage in is_32bit_abs_reloc.  A
9766
         more helpful warning message will be generated by apply_relocations
9767
         anyway, so just return.  */
9768
      return FALSE;
9769
    }
9770
}
9771
 
9772
/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
9773
   a 64-bit absolute RELA relocation used in DWARF debug sections.  */
9774
 
9775
static bfd_boolean
9776
is_64bit_abs_reloc (unsigned int reloc_type)
9777
{
9778
  switch (elf_header.e_machine)
9779
    {
9780
    case EM_ALPHA:
9781
      return reloc_type == 2; /* R_ALPHA_REFQUAD.  */
9782
    case EM_IA_64:
9783
      return reloc_type == 0x27; /* R_IA64_DIR64LSB.  */
9784
    case EM_PARISC:
9785
      return reloc_type == 80; /* R_PARISC_DIR64.  */
9786
    case EM_PPC64:
9787
      return reloc_type == 38; /* R_PPC64_ADDR64.  */
9788
    case EM_SPARC32PLUS:
9789
    case EM_SPARCV9:
9790
    case EM_SPARC:
9791
      return reloc_type == 54; /* R_SPARC_UA64.  */
9792
    case EM_X86_64:
9793
    case EM_L1OM:
9794
      return reloc_type == 1; /* R_X86_64_64.  */
9795
    case EM_S390_OLD:
9796
    case EM_S390:
9797
      return reloc_type == 22;  /* R_S390_64 */
9798
    case EM_MIPS:
9799
      return reloc_type == 18;  /* R_MIPS_64 */
9800
    default:
9801
      return FALSE;
9802
    }
9803
}
9804
 
9805
/* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
9806
   a 64-bit pc-relative RELA relocation used in DWARF debug sections.  */
9807
 
9808
static bfd_boolean
9809
is_64bit_pcrel_reloc (unsigned int reloc_type)
9810
{
9811
  switch (elf_header.e_machine)
9812
    {
9813
    case EM_ALPHA:
9814
      return reloc_type == 11; /* R_ALPHA_SREL64 */
9815
    case EM_IA_64:
9816
      return reloc_type == 0x4f; /* R_IA64_PCREL64LSB */
9817
    case EM_PARISC:
9818
      return reloc_type == 72; /* R_PARISC_PCREL64 */
9819
    case EM_PPC64:
9820
      return reloc_type == 44; /* R_PPC64_REL64 */
9821
    case EM_SPARC32PLUS:
9822
    case EM_SPARCV9:
9823
    case EM_SPARC:
9824
      return reloc_type == 46; /* R_SPARC_DISP64 */
9825
    case EM_X86_64:
9826
    case EM_L1OM:
9827
      return reloc_type == 24; /* R_X86_64_PC64 */
9828
    case EM_S390_OLD:
9829
    case EM_S390:
9830
      return reloc_type == 23;  /* R_S390_PC64 */
9831
    default:
9832
      return FALSE;
9833
    }
9834
}
9835
 
9836
/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
9837
   a 24-bit absolute RELA relocation used in DWARF debug sections.  */
9838
 
9839
static bfd_boolean
9840
is_24bit_abs_reloc (unsigned int reloc_type)
9841
{
9842
  switch (elf_header.e_machine)
9843
    {
9844
    case EM_CYGNUS_MN10200:
9845
    case EM_MN10200:
9846
      return reloc_type == 4; /* R_MN10200_24.  */
9847
    default:
9848
      return FALSE;
9849
    }
9850
}
9851
 
9852
/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
9853
   a 16-bit absolute RELA relocation used in DWARF debug sections.  */
9854
 
9855
static bfd_boolean
9856
is_16bit_abs_reloc (unsigned int reloc_type)
9857
{
9858
  switch (elf_header.e_machine)
9859
    {
9860
    case EM_AVR_OLD:
9861
    case EM_AVR:
9862
      return reloc_type == 4; /* R_AVR_16.  */
9863
    case EM_CYGNUS_D10V:
9864
    case EM_D10V:
9865
      return reloc_type == 3; /* R_D10V_16.  */
9866
    case EM_H8S:
9867
    case EM_H8_300:
9868
    case EM_H8_300H:
9869
      return reloc_type == R_H8_DIR16;
9870
    case EM_IP2K_OLD:
9871
    case EM_IP2K:
9872
      return reloc_type == 1; /* R_IP2K_16.  */
9873
    case EM_M32C_OLD:
9874
    case EM_M32C:
9875
      return reloc_type == 1; /* R_M32C_16 */
9876
    case EM_MSP430_OLD:
9877
    case EM_MSP430:
9878
      return reloc_type == 5; /* R_MSP430_16_BYTE.  */
9879
    case EM_ALTERA_NIOS2:
9880
    case EM_NIOS32:
9881
      return reloc_type == 9; /* R_NIOS_16.  */
9882
    case EM_OPEN8:
9883
      return reloc_type == 4; /* R_OPEN8_16.  */
9884
    case EM_TI_C6000:
9885
      return reloc_type == 2; /* R_C6000_ABS16.  */
9886
    case EM_XC16X:
9887
    case EM_C166:
9888
      return reloc_type == 2; /* R_XC16C_ABS_16.  */
9889
    default:
9890
      return FALSE;
9891
    }
9892
}
9893
 
9894
/* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
9895
   relocation entries (possibly formerly used for SHT_GROUP sections).  */
9896
 
9897
static bfd_boolean
9898
is_none_reloc (unsigned int reloc_type)
9899
{
9900
  switch (elf_header.e_machine)
9901
    {
9902
    case EM_68K:     /* R_68K_NONE.  */
9903
    case EM_386:     /* R_386_NONE.  */
9904
    case EM_SPARC32PLUS:
9905
    case EM_SPARCV9:
9906
    case EM_SPARC:   /* R_SPARC_NONE.  */
9907
    case EM_MIPS:    /* R_MIPS_NONE.  */
9908
    case EM_PARISC:  /* R_PARISC_NONE.  */
9909
    case EM_ALPHA:   /* R_ALPHA_NONE.  */
9910
    case EM_PPC:     /* R_PPC_NONE.  */
9911
    case EM_PPC64:   /* R_PPC64_NONE.  */
9912
    case EM_ARM:     /* R_ARM_NONE.  */
9913
    case EM_IA_64:   /* R_IA64_NONE.  */
9914
    case EM_SH:      /* R_SH_NONE.  */
9915
    case EM_S390_OLD:
9916
    case EM_S390:    /* R_390_NONE.  */
9917
    case EM_CRIS:    /* R_CRIS_NONE.  */
9918
    case EM_X86_64:  /* R_X86_64_NONE.  */
9919
    case EM_L1OM:    /* R_X86_64_NONE.  */
9920
    case EM_MN10300: /* R_MN10300_NONE.  */
9921
    case EM_MOXIE:   /* R_MOXIE_NONE.  */
9922
    case EM_M32R:    /* R_M32R_NONE.  */
9923
    case EM_TI_C6000:/* R_C6000_NONE.  */
9924
    case EM_XC16X:
9925
    case EM_C166:    /* R_XC16X_NONE.  */
9926
      return reloc_type == 0;
9927
    case EM_XTENSA_OLD:
9928
    case EM_XTENSA:
9929
      return (reloc_type == 0      /* R_XTENSA_NONE.  */
9930
              || reloc_type == 17  /* R_XTENSA_DIFF8.  */
9931
              || reloc_type == 18  /* R_XTENSA_DIFF16.  */
9932
              || reloc_type == 19  /* R_XTENSA_DIFF32.  */);
9933
    }
9934
  return FALSE;
9935
}
9936
 
9937
/* Apply relocations to a section.
9938
   Note: So far support has been added only for those relocations
9939
   which can be found in debug sections.
9940
   FIXME: Add support for more relocations ?  */
9941
 
9942
static void
9943
apply_relocations (void * file,
9944
                   Elf_Internal_Shdr * section,
9945
                   unsigned char * start)
9946
{
9947
  Elf_Internal_Shdr * relsec;
9948
  unsigned char * end = start + section->sh_size;
9949
 
9950
  if (elf_header.e_type != ET_REL)
9951
    return;
9952
 
9953
  /* Find the reloc section associated with the section.  */
9954
  for (relsec = section_headers;
9955
       relsec < section_headers + elf_header.e_shnum;
9956
       ++relsec)
9957
    {
9958
      bfd_boolean is_rela;
9959
      unsigned long num_relocs;
9960
      Elf_Internal_Rela * relocs;
9961
      Elf_Internal_Rela * rp;
9962
      Elf_Internal_Shdr * symsec;
9963
      Elf_Internal_Sym * symtab;
9964
      Elf_Internal_Sym * sym;
9965
 
9966
      if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
9967
          || relsec->sh_info >= elf_header.e_shnum
9968
          || section_headers + relsec->sh_info != section
9969
          || relsec->sh_size == 0
9970
          || relsec->sh_link >= elf_header.e_shnum)
9971
        continue;
9972
 
9973
      is_rela = relsec->sh_type == SHT_RELA;
9974
 
9975
      if (is_rela)
9976
        {
9977
          if (!slurp_rela_relocs ((FILE *) file, relsec->sh_offset,
9978
                                  relsec->sh_size, & relocs, & num_relocs))
9979
            return;
9980
        }
9981
      else
9982
        {
9983
          if (!slurp_rel_relocs ((FILE *) file, relsec->sh_offset,
9984
                                 relsec->sh_size, & relocs, & num_relocs))
9985
            return;
9986
        }
9987
 
9988
      /* SH uses RELA but uses in place value instead of the addend field.  */
9989
      if (elf_header.e_machine == EM_SH)
9990
        is_rela = FALSE;
9991
 
9992
      symsec = section_headers + relsec->sh_link;
9993
      symtab = GET_ELF_SYMBOLS ((FILE *) file, symsec);
9994
 
9995
      for (rp = relocs; rp < relocs + num_relocs; ++rp)
9996
        {
9997
          bfd_vma         addend;
9998
          unsigned int    reloc_type;
9999
          unsigned int    reloc_size;
10000
          unsigned char * rloc;
10001
 
10002
          reloc_type = get_reloc_type (rp->r_info);
10003
 
10004
          if (target_specific_reloc_handling (rp, start, symtab))
10005
            continue;
10006
          else if (is_none_reloc (reloc_type))
10007
            continue;
10008
          else if (is_32bit_abs_reloc (reloc_type)
10009
                   || is_32bit_pcrel_reloc (reloc_type))
10010
            reloc_size = 4;
10011
          else if (is_64bit_abs_reloc (reloc_type)
10012
                   || is_64bit_pcrel_reloc (reloc_type))
10013
            reloc_size = 8;
10014
          else if (is_24bit_abs_reloc (reloc_type))
10015
            reloc_size = 3;
10016
          else if (is_16bit_abs_reloc (reloc_type))
10017
            reloc_size = 2;
10018
          else
10019
            {
10020
              warn (_("unable to apply unsupported reloc type %d to section %s\n"),
10021
                    reloc_type, SECTION_NAME (section));
10022
              continue;
10023
            }
10024
 
10025
          rloc = start + rp->r_offset;
10026
          if ((rloc + reloc_size) > end)
10027
            {
10028
              warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
10029
                    (unsigned long) rp->r_offset,
10030
                    SECTION_NAME (section));
10031
              continue;
10032
            }
10033
 
10034
          sym = symtab + get_reloc_symindex (rp->r_info);
10035
 
10036
          /* If the reloc has a symbol associated with it,
10037
             make sure that it is of an appropriate type.
10038
 
10039
             Relocations against symbols without type can happen.
10040
             Gcc -feliminate-dwarf2-dups may generate symbols
10041
             without type for debug info.
10042
 
10043
             Icc generates relocations against function symbols
10044
             instead of local labels.
10045
 
10046
             Relocations against object symbols can happen, eg when
10047
             referencing a global array.  For an example of this see
10048
             the _clz.o binary in libgcc.a.  */
10049
          if (sym != symtab
10050
              && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
10051
            {
10052
              warn (_("skipping unexpected symbol type %s in %ld'th relocation in section %s\n"),
10053
                    get_symbol_type (ELF_ST_TYPE (sym->st_info)),
10054
                    (long int)(rp - relocs),
10055
                    SECTION_NAME (relsec));
10056
              continue;
10057
            }
10058
 
10059
          addend = 0;
10060
          if (is_rela)
10061
            addend += rp->r_addend;
10062
          /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
10063
             partial_inplace.  */
10064
          if (!is_rela
10065
              || (elf_header.e_machine == EM_XTENSA
10066
                  && reloc_type == 1)
10067
              || ((elf_header.e_machine == EM_PJ
10068
                   || elf_header.e_machine == EM_PJ_OLD)
10069
                  && reloc_type == 1)
10070
              || ((elf_header.e_machine == EM_D30V
10071
                   || elf_header.e_machine == EM_CYGNUS_D30V)
10072
                  && reloc_type == 12))
10073
            addend += byte_get (rloc, reloc_size);
10074
 
10075
          if (is_32bit_pcrel_reloc (reloc_type)
10076
              || is_64bit_pcrel_reloc (reloc_type))
10077
            {
10078
              /* On HPPA, all pc-relative relocations are biased by 8.  */
10079
              if (elf_header.e_machine == EM_PARISC)
10080
                addend -= 8;
10081
              byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
10082
                        reloc_size);
10083
            }
10084
          else
10085
            byte_put (rloc, addend + sym->st_value, reloc_size);
10086
        }
10087
 
10088
      free (symtab);
10089
      free (relocs);
10090
      break;
10091
    }
10092
}
10093
 
10094
#ifdef SUPPORT_DISASSEMBLY
10095
static int
10096
disassemble_section (Elf_Internal_Shdr * section, FILE * file)
10097
{
10098
  printf (_("\nAssembly dump of section %s\n"),
10099
          SECTION_NAME (section));
10100
 
10101
  /* XXX -- to be done --- XXX */
10102
 
10103
  return 1;
10104
}
10105
#endif
10106
 
10107
/* Reads in the contents of SECTION from FILE, returning a pointer
10108
   to a malloc'ed buffer or NULL if something went wrong.  */
10109
 
10110
static char *
10111
get_section_contents (Elf_Internal_Shdr * section, FILE * file)
10112
{
10113
  bfd_size_type num_bytes;
10114
 
10115
  num_bytes = section->sh_size;
10116
 
10117
  if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
10118
    {
10119
      printf (_("\nSection '%s' has no data to dump.\n"),
10120
              SECTION_NAME (section));
10121
      return NULL;
10122
    }
10123
 
10124
  return  (char *) get_data (NULL, file, section->sh_offset, 1, num_bytes,
10125
                             _("section contents"));
10126
}
10127
 
10128
 
10129
static void
10130
dump_section_as_strings (Elf_Internal_Shdr * section, FILE * file)
10131
{
10132
  Elf_Internal_Shdr * relsec;
10133
  bfd_size_type num_bytes;
10134
  char * data;
10135
  char * end;
10136
  char * start;
10137
  char * name = SECTION_NAME (section);
10138
  bfd_boolean some_strings_shown;
10139
 
10140
  start = get_section_contents (section, file);
10141
  if (start == NULL)
10142
    return;
10143
 
10144
  printf (_("\nString dump of section '%s':\n"), name);
10145
 
10146
  /* If the section being dumped has relocations against it the user might
10147
     be expecting these relocations to have been applied.  Check for this
10148
     case and issue a warning message in order to avoid confusion.
10149
     FIXME: Maybe we ought to have an option that dumps a section with
10150
     relocs applied ?  */
10151
  for (relsec = section_headers;
10152
       relsec < section_headers + elf_header.e_shnum;
10153
       ++relsec)
10154
    {
10155
      if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
10156
          || relsec->sh_info >= elf_header.e_shnum
10157
          || section_headers + relsec->sh_info != section
10158
          || relsec->sh_size == 0
10159
          || relsec->sh_link >= elf_header.e_shnum)
10160
        continue;
10161
 
10162
      printf (_("  Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
10163
      break;
10164
    }
10165
 
10166
  num_bytes = section->sh_size;
10167
  data = start;
10168
  end  = start + num_bytes;
10169
  some_strings_shown = FALSE;
10170
 
10171
  while (data < end)
10172
    {
10173
      while (!ISPRINT (* data))
10174
        if (++ data >= end)
10175
          break;
10176
 
10177
      if (data < end)
10178
        {
10179
#ifndef __MSVCRT__
10180
          /* PR 11128: Use two separate invocations in order to work
10181
             around bugs in the Solaris 8 implementation of printf.  */
10182
          printf ("  [%6tx]  ", data - start);
10183
          printf ("%s\n", data);
10184
#else
10185
          printf ("  [%6Ix]  %s\n", (size_t) (data - start), data);
10186
#endif
10187
          data += strlen (data);
10188
          some_strings_shown = TRUE;
10189
        }
10190
    }
10191
 
10192
  if (! some_strings_shown)
10193
    printf (_("  No strings found in this section."));
10194
 
10195
  free (start);
10196
 
10197
  putchar ('\n');
10198
}
10199
 
10200
static void
10201
dump_section_as_bytes (Elf_Internal_Shdr * section,
10202
                       FILE * file,
10203
                       bfd_boolean relocate)
10204
{
10205
  Elf_Internal_Shdr * relsec;
10206
  bfd_size_type bytes;
10207
  bfd_vma addr;
10208
  unsigned char * data;
10209
  unsigned char * start;
10210
 
10211
  start = (unsigned char *) get_section_contents (section, file);
10212
  if (start == NULL)
10213
    return;
10214
 
10215
  printf (_("\nHex dump of section '%s':\n"), SECTION_NAME (section));
10216
 
10217
  if (relocate)
10218
    {
10219
      apply_relocations (file, section, start);
10220
    }
10221
  else
10222
    {
10223
      /* If the section being dumped has relocations against it the user might
10224
         be expecting these relocations to have been applied.  Check for this
10225
         case and issue a warning message in order to avoid confusion.
10226
         FIXME: Maybe we ought to have an option that dumps a section with
10227
         relocs applied ?  */
10228
      for (relsec = section_headers;
10229
           relsec < section_headers + elf_header.e_shnum;
10230
           ++relsec)
10231
        {
10232
          if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
10233
              || relsec->sh_info >= elf_header.e_shnum
10234
              || section_headers + relsec->sh_info != section
10235
              || relsec->sh_size == 0
10236
              || relsec->sh_link >= elf_header.e_shnum)
10237
            continue;
10238
 
10239
          printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
10240
          break;
10241
        }
10242
    }
10243
 
10244
  addr = section->sh_addr;
10245
  bytes = section->sh_size;
10246
  data = start;
10247
 
10248
  while (bytes)
10249
    {
10250
      int j;
10251
      int k;
10252
      int lbytes;
10253
 
10254
      lbytes = (bytes > 16 ? 16 : bytes);
10255
 
10256
      printf ("  0x%8.8lx ", (unsigned long) addr);
10257
 
10258
      for (j = 0; j < 16; j++)
10259
        {
10260
          if (j < lbytes)
10261
            printf ("%2.2x", data[j]);
10262
          else
10263
            printf ("  ");
10264
 
10265
          if ((j & 3) == 3)
10266
            printf (" ");
10267
        }
10268
 
10269
      for (j = 0; j < lbytes; j++)
10270
        {
10271
          k = data[j];
10272
          if (k >= ' ' && k < 0x7f)
10273
            printf ("%c", k);
10274
          else
10275
            printf (".");
10276
        }
10277
 
10278
      putchar ('\n');
10279
 
10280
      data  += lbytes;
10281
      addr  += lbytes;
10282
      bytes -= lbytes;
10283
    }
10284
 
10285
  free (start);
10286
 
10287
  putchar ('\n');
10288
}
10289
 
10290
/* Uncompresses a section that was compressed using zlib, in place.  */
10291
 
10292
static int
10293
uncompress_section_contents (unsigned char **buffer ATTRIBUTE_UNUSED,
10294
                             dwarf_size_type *size ATTRIBUTE_UNUSED)
10295
{
10296
#ifndef HAVE_ZLIB_H
10297
  return FALSE;
10298
#else
10299
  dwarf_size_type compressed_size = *size;
10300
  unsigned char * compressed_buffer = *buffer;
10301
  dwarf_size_type uncompressed_size;
10302
  unsigned char * uncompressed_buffer;
10303
  z_stream strm;
10304
  int rc;
10305
  dwarf_size_type header_size = 12;
10306
 
10307
  /* Read the zlib header.  In this case, it should be "ZLIB" followed
10308
     by the uncompressed section size, 8 bytes in big-endian order.  */
10309
  if (compressed_size < header_size
10310
      || ! streq ((char *) compressed_buffer, "ZLIB"))
10311
    return 0;
10312
 
10313
  uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
10314
  uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
10315
  uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
10316
  uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
10317
  uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
10318
  uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
10319
  uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
10320
  uncompressed_size += compressed_buffer[11];
10321
 
10322
  /* It is possible the section consists of several compressed
10323
     buffers concatenated together, so we uncompress in a loop.  */
10324
  strm.zalloc = NULL;
10325
  strm.zfree = NULL;
10326
  strm.opaque = NULL;
10327
  strm.avail_in = compressed_size - header_size;
10328
  strm.next_in = (Bytef *) compressed_buffer + header_size;
10329
  strm.avail_out = uncompressed_size;
10330
  uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
10331
 
10332
  rc = inflateInit (& strm);
10333
  while (strm.avail_in > 0)
10334
    {
10335
      if (rc != Z_OK)
10336
        goto fail;
10337
      strm.next_out = ((Bytef *) uncompressed_buffer
10338
                       + (uncompressed_size - strm.avail_out));
10339
      rc = inflate (&strm, Z_FINISH);
10340
      if (rc != Z_STREAM_END)
10341
        goto fail;
10342
      rc = inflateReset (& strm);
10343
    }
10344
  rc = inflateEnd (& strm);
10345
  if (rc != Z_OK
10346
      || strm.avail_out != 0)
10347
    goto fail;
10348
 
10349
  free (compressed_buffer);
10350
  *buffer = uncompressed_buffer;
10351
  *size = uncompressed_size;
10352
  return 1;
10353
 
10354
 fail:
10355
  free (uncompressed_buffer);
10356
  /* Indicate decompression failure.  */
10357
  *buffer = NULL;
10358
  return 0;
10359
#endif  /* HAVE_ZLIB_H */
10360
}
10361
 
10362
static int
10363
load_specific_debug_section (enum dwarf_section_display_enum debug,
10364
                             Elf_Internal_Shdr * sec, void * file)
10365
{
10366
  struct dwarf_section * section = &debug_displays [debug].section;
10367
  char buf [64];
10368
 
10369
  /* If it is already loaded, do nothing.  */
10370
  if (section->start != NULL)
10371
    return 1;
10372
 
10373
  snprintf (buf, sizeof (buf), _("%s section data"), section->name);
10374
  section->address = sec->sh_addr;
10375
  section->size = sec->sh_size;
10376
  section->start = (unsigned char *) get_data (NULL, (FILE *) file,
10377
                                               sec->sh_offset, 1,
10378
                                               sec->sh_size, buf);
10379
  if (uncompress_section_contents (&section->start, &section->size))
10380
    sec->sh_size = section->size;
10381
 
10382
  if (section->start == NULL)
10383
    return 0;
10384
 
10385
  if (debug_displays [debug].relocate)
10386
    apply_relocations ((FILE *) file, sec, section->start);
10387
 
10388
  return 1;
10389
}
10390
 
10391
int
10392
load_debug_section (enum dwarf_section_display_enum debug, void * file)
10393
{
10394
  struct dwarf_section * section = &debug_displays [debug].section;
10395
  Elf_Internal_Shdr * sec;
10396
 
10397
  /* Locate the debug section.  */
10398
  sec = find_section (section->uncompressed_name);
10399
  if (sec != NULL)
10400
    section->name = section->uncompressed_name;
10401
  else
10402
    {
10403
      sec = find_section (section->compressed_name);
10404
      if (sec != NULL)
10405
        section->name = section->compressed_name;
10406
    }
10407
  if (sec == NULL)
10408
    return 0;
10409
 
10410
  return load_specific_debug_section (debug, sec, (FILE *) file);
10411
}
10412
 
10413
void
10414
free_debug_section (enum dwarf_section_display_enum debug)
10415
{
10416
  struct dwarf_section * section = &debug_displays [debug].section;
10417
 
10418
  if (section->start == NULL)
10419
    return;
10420
 
10421
  free ((char *) section->start);
10422
  section->start = NULL;
10423
  section->address = 0;
10424
  section->size = 0;
10425
}
10426
 
10427
static int
10428
display_debug_section (Elf_Internal_Shdr * section, FILE * file)
10429
{
10430
  char * name = SECTION_NAME (section);
10431
  bfd_size_type length;
10432
  int result = 1;
10433
  int i;
10434
 
10435
  length = section->sh_size;
10436
  if (length == 0)
10437
    {
10438
      printf (_("\nSection '%s' has no debugging data.\n"), name);
10439
      return 0;
10440
    }
10441
  if (section->sh_type == SHT_NOBITS)
10442
    {
10443
      /* There is no point in dumping the contents of a debugging section
10444
         which has the NOBITS type - the bits in the file will be random.
10445
         This can happen when a file containing a .eh_frame section is
10446
         stripped with the --only-keep-debug command line option.  */
10447
      printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"), name);
10448
      return 0;
10449
    }
10450
 
10451
  if (const_strneq (name, ".gnu.linkonce.wi."))
10452
    name = ".debug_info";
10453
 
10454
  /* See if we know how to display the contents of this section.  */
10455
  for (i = 0; i < max; i++)
10456
    if (streq (debug_displays[i].section.uncompressed_name, name)
10457
        || streq (debug_displays[i].section.compressed_name, name))
10458
      {
10459
        struct dwarf_section * sec = &debug_displays [i].section;
10460
        int secondary = (section != find_section (name));
10461
 
10462
        if (secondary)
10463
          free_debug_section ((enum dwarf_section_display_enum) i);
10464
 
10465
        if (streq (sec->uncompressed_name, name))
10466
          sec->name = sec->uncompressed_name;
10467
        else
10468
          sec->name = sec->compressed_name;
10469
        if (load_specific_debug_section ((enum dwarf_section_display_enum) i,
10470
                                         section, file))
10471
          {
10472
            result &= debug_displays[i].display (sec, file);
10473
 
10474
            if (secondary || (i != info && i != abbrev))
10475
              free_debug_section ((enum dwarf_section_display_enum) i);
10476
          }
10477
 
10478
        break;
10479
      }
10480
 
10481
  if (i == max)
10482
    {
10483
      printf (_("Unrecognized debug section: %s\n"), name);
10484
      result = 0;
10485
    }
10486
 
10487
  return result;
10488
}
10489
 
10490
/* Set DUMP_SECTS for all sections where dumps were requested
10491
   based on section name.  */
10492
 
10493
static void
10494
initialise_dumps_byname (void)
10495
{
10496
  struct dump_list_entry * cur;
10497
 
10498
  for (cur = dump_sects_byname; cur; cur = cur->next)
10499
    {
10500
      unsigned int i;
10501
      int any;
10502
 
10503
      for (i = 0, any = 0; i < elf_header.e_shnum; i++)
10504
        if (streq (SECTION_NAME (section_headers + i), cur->name))
10505
          {
10506
            request_dump_bynumber (i, cur->type);
10507
            any = 1;
10508
          }
10509
 
10510
      if (!any)
10511
        warn (_("Section '%s' was not dumped because it does not exist!\n"),
10512
              cur->name);
10513
    }
10514
}
10515
 
10516
static void
10517
process_section_contents (FILE * file)
10518
{
10519
  Elf_Internal_Shdr * section;
10520
  unsigned int i;
10521
 
10522
  if (! do_dump)
10523
    return;
10524
 
10525
  initialise_dumps_byname ();
10526
 
10527
  for (i = 0, section = section_headers;
10528
       i < elf_header.e_shnum && i < num_dump_sects;
10529
       i++, section++)
10530
    {
10531
#ifdef SUPPORT_DISASSEMBLY
10532
      if (dump_sects[i] & DISASS_DUMP)
10533
        disassemble_section (section, file);
10534
#endif
10535
      if (dump_sects[i] & HEX_DUMP)
10536
        dump_section_as_bytes (section, file, FALSE);
10537
 
10538
      if (dump_sects[i] & RELOC_DUMP)
10539
        dump_section_as_bytes (section, file, TRUE);
10540
 
10541
      if (dump_sects[i] & STRING_DUMP)
10542
        dump_section_as_strings (section, file);
10543
 
10544
      if (dump_sects[i] & DEBUG_DUMP)
10545
        display_debug_section (section, file);
10546
    }
10547
 
10548
  /* Check to see if the user requested a
10549
     dump of a section that does not exist.  */
10550
  while (i++ < num_dump_sects)
10551
    if (dump_sects[i])
10552
      warn (_("Section %d was not dumped because it does not exist!\n"), i);
10553
}
10554
 
10555
static void
10556
process_mips_fpe_exception (int mask)
10557
{
10558
  if (mask)
10559
    {
10560
      int first = 1;
10561
      if (mask & OEX_FPU_INEX)
10562
        fputs ("INEX", stdout), first = 0;
10563
      if (mask & OEX_FPU_UFLO)
10564
        printf ("%sUFLO", first ? "" : "|"), first = 0;
10565
      if (mask & OEX_FPU_OFLO)
10566
        printf ("%sOFLO", first ? "" : "|"), first = 0;
10567
      if (mask & OEX_FPU_DIV0)
10568
        printf ("%sDIV0", first ? "" : "|"), first = 0;
10569
      if (mask & OEX_FPU_INVAL)
10570
        printf ("%sINVAL", first ? "" : "|");
10571
    }
10572
  else
10573
    fputs ("0", stdout);
10574
}
10575
 
10576
/* ARM EABI attributes section.  */
10577
typedef struct
10578
{
10579
  int tag;
10580
  const char * name;
10581
  /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup.  */
10582
  int type;
10583
  const char ** table;
10584
} arm_attr_public_tag;
10585
 
10586
static const char * arm_attr_tag_CPU_arch[] =
10587
  {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
10588
   "v6K", "v7", "v6-M", "v6S-M", "v7E-M"};
10589
static const char * arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
10590
static const char * arm_attr_tag_THUMB_ISA_use[] =
10591
  {"No", "Thumb-1", "Thumb-2"};
10592
static const char * arm_attr_tag_FP_arch[] =
10593
  {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16"};
10594
static const char * arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
10595
static const char * arm_attr_tag_Advanced_SIMD_arch[] =
10596
  {"No", "NEONv1", "NEONv1 with Fused-MAC"};
10597
static const char * arm_attr_tag_PCS_config[] =
10598
  {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
10599
   "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
10600
static const char * arm_attr_tag_ABI_PCS_R9_use[] =
10601
  {"V6", "SB", "TLS", "Unused"};
10602
static const char * arm_attr_tag_ABI_PCS_RW_data[] =
10603
  {"Absolute", "PC-relative", "SB-relative", "None"};
10604
static const char * arm_attr_tag_ABI_PCS_RO_data[] =
10605
  {"Absolute", "PC-relative", "None"};
10606
static const char * arm_attr_tag_ABI_PCS_GOT_use[] =
10607
  {"None", "direct", "GOT-indirect"};
10608
static const char * arm_attr_tag_ABI_PCS_wchar_t[] =
10609
  {"None", "??? 1", "2", "??? 3", "4"};
10610
static const char * arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
10611
static const char * arm_attr_tag_ABI_FP_denormal[] =
10612
  {"Unused", "Needed", "Sign only"};
10613
static const char * arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
10614
static const char * arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
10615
static const char * arm_attr_tag_ABI_FP_number_model[] =
10616
  {"Unused", "Finite", "RTABI", "IEEE 754"};
10617
static const char * arm_attr_tag_ABI_enum_size[] =
10618
  {"Unused", "small", "int", "forced to int"};
10619
static const char * arm_attr_tag_ABI_HardFP_use[] =
10620
  {"As Tag_FP_arch", "SP only", "DP only", "SP and DP"};
10621
static const char * arm_attr_tag_ABI_VFP_args[] =
10622
  {"AAPCS", "VFP registers", "custom"};
10623
static const char * arm_attr_tag_ABI_WMMX_args[] =
10624
  {"AAPCS", "WMMX registers", "custom"};
10625
static const char * arm_attr_tag_ABI_optimization_goals[] =
10626
  {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
10627
    "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
10628
static const char * arm_attr_tag_ABI_FP_optimization_goals[] =
10629
  {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
10630
    "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
10631
static const char * arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
10632
static const char * arm_attr_tag_FP_HP_extension[] =
10633
  {"Not Allowed", "Allowed"};
10634
static const char * arm_attr_tag_ABI_FP_16bit_format[] =
10635
  {"None", "IEEE 754", "Alternative Format"};
10636
static const char * arm_attr_tag_MPextension_use[] =
10637
  {"Not Allowed", "Allowed"};
10638
static const char * arm_attr_tag_DIV_use[] =
10639
  {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
10640
    "Allowed in v7-A with integer division extension"};
10641
static const char * arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
10642
static const char * arm_attr_tag_Virtualization_use[] =
10643
  {"Not Allowed", "TrustZone", "Virtualization Extensions",
10644
    "TrustZone and Virtualization Extensions"};
10645
static const char * arm_attr_tag_MPextension_use_legacy[] =
10646
  {"Not Allowed", "Allowed"};
10647
 
10648
#define LOOKUP(id, name) \
10649
  {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
10650
static arm_attr_public_tag arm_attr_public_tags[] =
10651
{
10652
  {4, "CPU_raw_name", 1, NULL},
10653
  {5, "CPU_name", 1, NULL},
10654
  LOOKUP(6, CPU_arch),
10655
  {7, "CPU_arch_profile", 0, NULL},
10656
  LOOKUP(8, ARM_ISA_use),
10657
  LOOKUP(9, THUMB_ISA_use),
10658
  LOOKUP(10, FP_arch),
10659
  LOOKUP(11, WMMX_arch),
10660
  LOOKUP(12, Advanced_SIMD_arch),
10661
  LOOKUP(13, PCS_config),
10662
  LOOKUP(14, ABI_PCS_R9_use),
10663
  LOOKUP(15, ABI_PCS_RW_data),
10664
  LOOKUP(16, ABI_PCS_RO_data),
10665
  LOOKUP(17, ABI_PCS_GOT_use),
10666
  LOOKUP(18, ABI_PCS_wchar_t),
10667
  LOOKUP(19, ABI_FP_rounding),
10668
  LOOKUP(20, ABI_FP_denormal),
10669
  LOOKUP(21, ABI_FP_exceptions),
10670
  LOOKUP(22, ABI_FP_user_exceptions),
10671
  LOOKUP(23, ABI_FP_number_model),
10672
  {24, "ABI_align_needed", 0, NULL},
10673
  {25, "ABI_align_preserved", 0, NULL},
10674
  LOOKUP(26, ABI_enum_size),
10675
  LOOKUP(27, ABI_HardFP_use),
10676
  LOOKUP(28, ABI_VFP_args),
10677
  LOOKUP(29, ABI_WMMX_args),
10678
  LOOKUP(30, ABI_optimization_goals),
10679
  LOOKUP(31, ABI_FP_optimization_goals),
10680
  {32, "compatibility", 0, NULL},
10681
  LOOKUP(34, CPU_unaligned_access),
10682
  LOOKUP(36, FP_HP_extension),
10683
  LOOKUP(38, ABI_FP_16bit_format),
10684
  LOOKUP(42, MPextension_use),
10685
  LOOKUP(44, DIV_use),
10686
  {64, "nodefaults", 0, NULL},
10687
  {65, "also_compatible_with", 0, NULL},
10688
  LOOKUP(66, T2EE_use),
10689
  {67, "conformance", 1, NULL},
10690
  LOOKUP(68, Virtualization_use),
10691
  LOOKUP(70, MPextension_use_legacy)
10692
};
10693
#undef LOOKUP
10694
 
10695
static unsigned char *
10696
display_arm_attribute (unsigned char * p)
10697
{
10698
  int tag;
10699
  unsigned int len;
10700
  int val;
10701
  arm_attr_public_tag * attr;
10702
  unsigned i;
10703
  int type;
10704
 
10705
  tag = read_uleb128 (p, &len);
10706
  p += len;
10707
  attr = NULL;
10708
  for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
10709
    {
10710
      if (arm_attr_public_tags[i].tag == tag)
10711
        {
10712
          attr = &arm_attr_public_tags[i];
10713
          break;
10714
        }
10715
    }
10716
 
10717
  if (attr)
10718
    {
10719
      printf ("  Tag_%s: ", attr->name);
10720
      switch (attr->type)
10721
        {
10722
        case 0:
10723
          switch (tag)
10724
            {
10725
            case 7: /* Tag_CPU_arch_profile.  */
10726
              val = read_uleb128 (p, &len);
10727
              p += len;
10728
              switch (val)
10729
                {
10730
                case 0: printf (_("None\n")); break;
10731
                case 'A': printf (_("Application\n")); break;
10732
                case 'R': printf (_("Realtime\n")); break;
10733
                case 'M': printf (_("Microcontroller\n")); break;
10734
                case 'S': printf (_("Application or Realtime\n")); break;
10735
                default: printf ("??? (%d)\n", val); break;
10736
                }
10737
              break;
10738
 
10739
            case 24: /* Tag_align_needed.  */
10740
              val = read_uleb128 (p, &len);
10741
              p += len;
10742
              switch (val)
10743
                {
10744
                case 0: printf (_("None\n")); break;
10745
                case 1: printf (_("8-byte\n")); break;
10746
                case 2: printf (_("4-byte\n")); break;
10747
                case 3: printf ("??? 3\n"); break;
10748
                default:
10749
                  if (val <= 12)
10750
                    printf (_("8-byte and up to %d-byte extended\n"),
10751
                            1 << val);
10752
                  else
10753
                    printf ("??? (%d)\n", val);
10754
                  break;
10755
                }
10756
              break;
10757
 
10758
            case 25: /* Tag_align_preserved.  */
10759
              val = read_uleb128 (p, &len);
10760
              p += len;
10761
              switch (val)
10762
                {
10763
                case 0: printf (_("None\n")); break;
10764
                case 1: printf (_("8-byte, except leaf SP\n")); break;
10765
                case 2: printf (_("8-byte\n")); break;
10766
                case 3: printf ("??? 3\n"); break;
10767
                default:
10768
                  if (val <= 12)
10769
                    printf (_("8-byte and up to %d-byte extended\n"),
10770
                            1 << val);
10771
                  else
10772
                    printf ("??? (%d)\n", val);
10773
                  break;
10774
                }
10775
              break;
10776
 
10777
            case 32: /* Tag_compatibility.  */
10778
              val = read_uleb128 (p, &len);
10779
              p += len;
10780
              printf (_("flag = %d, vendor = %s\n"), val, p);
10781
              p += strlen ((char *) p) + 1;
10782
              break;
10783
 
10784
            case 64: /* Tag_nodefaults.  */
10785
              p++;
10786
              printf (_("True\n"));
10787
              break;
10788
 
10789
            case 65: /* Tag_also_compatible_with.  */
10790
              val = read_uleb128 (p, &len);
10791
              p += len;
10792
              if (val == 6 /* Tag_CPU_arch.  */)
10793
                {
10794
                  val = read_uleb128 (p, &len);
10795
                  p += len;
10796
                  if ((unsigned int)val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
10797
                    printf ("??? (%d)\n", val);
10798
                  else
10799
                    printf ("%s\n", arm_attr_tag_CPU_arch[val]);
10800
                }
10801
              else
10802
                printf ("???\n");
10803
              while (*(p++) != '\0' /* NUL terminator.  */);
10804
              break;
10805
 
10806
            default:
10807
              abort ();
10808
            }
10809
          return p;
10810
 
10811
        case 1:
10812
        case 2:
10813
          type = attr->type;
10814
          break;
10815
 
10816
        default:
10817
          assert (attr->type & 0x80);
10818
          val = read_uleb128 (p, &len);
10819
          p += len;
10820
          type = attr->type & 0x7f;
10821
          if (val >= type)
10822
            printf ("??? (%d)\n", val);
10823
          else
10824
            printf ("%s\n", attr->table[val]);
10825
          return p;
10826
        }
10827
    }
10828
  else
10829
    {
10830
      if (tag & 1)
10831
        type = 1; /* String.  */
10832
      else
10833
        type = 2; /* uleb128.  */
10834
      printf ("  Tag_unknown_%d: ", tag);
10835
    }
10836
 
10837
  if (type == 1)
10838
    {
10839
      printf ("\"%s\"\n", p);
10840
      p += strlen ((char *) p) + 1;
10841
    }
10842
  else
10843
    {
10844
      val = read_uleb128 (p, &len);
10845
      p += len;
10846
      printf ("%d (0x%x)\n", val, val);
10847
    }
10848
 
10849
  return p;
10850
}
10851
 
10852
static unsigned char *
10853
display_gnu_attribute (unsigned char * p,
10854
                       unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int))
10855
{
10856
  int tag;
10857
  unsigned int len;
10858
  int val;
10859
  int type;
10860
 
10861
  tag = read_uleb128 (p, &len);
10862
  p += len;
10863
 
10864
  /* Tag_compatibility is the only generic GNU attribute defined at
10865
     present.  */
10866
  if (tag == 32)
10867
    {
10868
      val = read_uleb128 (p, &len);
10869
      p += len;
10870
      printf (_("flag = %d, vendor = %s\n"), val, p);
10871
      p += strlen ((char *) p) + 1;
10872
      return p;
10873
    }
10874
 
10875
  if ((tag & 2) == 0 && display_proc_gnu_attribute)
10876
    return display_proc_gnu_attribute (p, tag);
10877
 
10878
  if (tag & 1)
10879
    type = 1; /* String.  */
10880
  else
10881
    type = 2; /* uleb128.  */
10882
  printf ("  Tag_unknown_%d: ", tag);
10883
 
10884
  if (type == 1)
10885
    {
10886
      printf ("\"%s\"\n", p);
10887
      p += strlen ((char *) p) + 1;
10888
    }
10889
  else
10890
    {
10891
      val = read_uleb128 (p, &len);
10892
      p += len;
10893
      printf ("%d (0x%x)\n", val, val);
10894
    }
10895
 
10896
  return p;
10897
}
10898
 
10899
static unsigned char *
10900
display_power_gnu_attribute (unsigned char * p, int tag)
10901
{
10902
  int type;
10903
  unsigned int len;
10904
  int val;
10905
 
10906
  if (tag == Tag_GNU_Power_ABI_FP)
10907
    {
10908
      val = read_uleb128 (p, &len);
10909
      p += len;
10910
      printf ("  Tag_GNU_Power_ABI_FP: ");
10911
 
10912
      switch (val)
10913
        {
10914
        case 0:
10915
          printf (_("Hard or soft float\n"));
10916
          break;
10917
        case 1:
10918
          printf (_("Hard float\n"));
10919
          break;
10920
        case 2:
10921
          printf (_("Soft float\n"));
10922
          break;
10923
        case 3:
10924
          printf (_("Single-precision hard float\n"));
10925
          break;
10926
        default:
10927
          printf ("??? (%d)\n", val);
10928
          break;
10929
        }
10930
      return p;
10931
   }
10932
 
10933
  if (tag == Tag_GNU_Power_ABI_Vector)
10934
    {
10935
      val = read_uleb128 (p, &len);
10936
      p += len;
10937
      printf ("  Tag_GNU_Power_ABI_Vector: ");
10938
      switch (val)
10939
        {
10940
        case 0:
10941
          printf (_("Any\n"));
10942
          break;
10943
        case 1:
10944
          printf (_("Generic\n"));
10945
          break;
10946
        case 2:
10947
          printf ("AltiVec\n");
10948
          break;
10949
        case 3:
10950
          printf ("SPE\n");
10951
          break;
10952
        default:
10953
          printf ("??? (%d)\n", val);
10954
          break;
10955
        }
10956
      return p;
10957
   }
10958
 
10959
  if (tag == Tag_GNU_Power_ABI_Struct_Return)
10960
    {
10961
      val = read_uleb128 (p, &len);
10962
      p += len;
10963
      printf ("  Tag_GNU_Power_ABI_Struct_Return: ");
10964
      switch (val)
10965
       {
10966
       case 0:
10967
         printf (_("Any\n"));
10968
         break;
10969
       case 1:
10970
         printf ("r3/r4\n");
10971
         break;
10972
       case 2:
10973
         printf (_("Memory\n"));
10974
         break;
10975
       default:
10976
         printf ("??? (%d)\n", val);
10977
         break;
10978
       }
10979
      return p;
10980
    }
10981
 
10982
  if (tag & 1)
10983
    type = 1; /* String.  */
10984
  else
10985
    type = 2; /* uleb128.  */
10986
  printf ("  Tag_unknown_%d: ", tag);
10987
 
10988
  if (type == 1)
10989
    {
10990
      printf ("\"%s\"\n", p);
10991
      p += strlen ((char *) p) + 1;
10992
    }
10993
  else
10994
    {
10995
      val = read_uleb128 (p, &len);
10996
      p += len;
10997
      printf ("%d (0x%x)\n", val, val);
10998
    }
10999
 
11000
  return p;
11001
}
11002
 
11003
static unsigned char *
11004
display_mips_gnu_attribute (unsigned char * p, int tag)
11005
{
11006
  int type;
11007
  unsigned int len;
11008
  int val;
11009
 
11010
  if (tag == Tag_GNU_MIPS_ABI_FP)
11011
    {
11012
      val = read_uleb128 (p, &len);
11013
      p += len;
11014
      printf ("  Tag_GNU_MIPS_ABI_FP: ");
11015
 
11016
      switch (val)
11017
        {
11018
        case 0:
11019
          printf (_("Hard or soft float\n"));
11020
          break;
11021
        case 1:
11022
          printf (_("Hard float (double precision)\n"));
11023
          break;
11024
        case 2:
11025
          printf (_("Hard float (single precision)\n"));
11026
          break;
11027
        case 3:
11028
          printf (_("Soft float\n"));
11029
          break;
11030
        case 4:
11031
          printf (_("Hard float (MIPS32r2 64-bit FPU)\n"));
11032
          break;
11033
        default:
11034
          printf ("??? (%d)\n", val);
11035
          break;
11036
        }
11037
      return p;
11038
   }
11039
 
11040
  if (tag & 1)
11041
    type = 1; /* String.  */
11042
  else
11043
    type = 2; /* uleb128.  */
11044
  printf ("  Tag_unknown_%d: ", tag);
11045
 
11046
  if (type == 1)
11047
    {
11048
      printf ("\"%s\"\n", p);
11049
      p += strlen ((char *) p) + 1;
11050
    }
11051
  else
11052
    {
11053
      val = read_uleb128 (p, &len);
11054
      p += len;
11055
      printf ("%d (0x%x)\n", val, val);
11056
    }
11057
 
11058
  return p;
11059
}
11060
 
11061
static unsigned char *
11062
display_tic6x_attribute (unsigned char * p)
11063
{
11064
  int tag;
11065
  unsigned int len;
11066
  int val;
11067
 
11068
  tag = read_uleb128 (p, &len);
11069
  p += len;
11070
 
11071
  switch (tag)
11072
    {
11073
    case Tag_ISA:
11074
      val = read_uleb128 (p, &len);
11075
      p += len;
11076
      printf ("  Tag_ISA: ");
11077
 
11078
      switch (val)
11079
        {
11080
        case C6XABI_Tag_ISA_none:
11081
          printf (_("None\n"));
11082
          break;
11083
        case C6XABI_Tag_ISA_C62X:
11084
          printf ("C62x\n");
11085
          break;
11086
        case C6XABI_Tag_ISA_C67X:
11087
          printf ("C67x\n");
11088
          break;
11089
        case C6XABI_Tag_ISA_C67XP:
11090
          printf ("C67x+\n");
11091
          break;
11092
        case C6XABI_Tag_ISA_C64X:
11093
          printf ("C64x\n");
11094
          break;
11095
        case C6XABI_Tag_ISA_C64XP:
11096
          printf ("C64x+\n");
11097
          break;
11098
        case C6XABI_Tag_ISA_C674X:
11099
          printf ("C674x\n");
11100
          break;
11101
        default:
11102
          printf ("??? (%d)\n", val);
11103
          break;
11104
        }
11105
      return p;
11106
 
11107
    case Tag_ABI_wchar_t:
11108
      val = read_uleb128 (p, &len);
11109
      p += len;
11110
      printf ("  Tag_ABI_wchar_t: ");
11111
      switch (val)
11112
        {
11113
        case 0:
11114
          printf (_("Not used\n"));
11115
          break;
11116
        case 1:
11117
          printf (_("2 bytes\n"));
11118
          break;
11119
        case 2:
11120
          printf (_("4 bytes\n"));
11121
          break;
11122
        default:
11123
          printf ("??? (%d)\n", val);
11124
          break;
11125
        }
11126
      return p;
11127
 
11128
    case Tag_ABI_stack_align_needed:
11129
      val = read_uleb128 (p, &len);
11130
      p += len;
11131
      printf ("  Tag_ABI_stack_align_needed: ");
11132
      switch (val)
11133
        {
11134
        case 0:
11135
          printf (_("8-byte\n"));
11136
          break;
11137
        case 1:
11138
          printf (_("16-byte\n"));
11139
          break;
11140
        default:
11141
          printf ("??? (%d)\n", val);
11142
          break;
11143
        }
11144
      return p;
11145
 
11146
    case Tag_ABI_stack_align_preserved:
11147
      val = read_uleb128 (p, &len);
11148
      p += len;
11149
      printf ("  Tag_ABI_stack_align_preserved: ");
11150
      switch (val)
11151
        {
11152
        case 0:
11153
          printf (_("8-byte\n"));
11154
          break;
11155
        case 1:
11156
          printf (_("16-byte\n"));
11157
          break;
11158
        default:
11159
          printf ("??? (%d)\n", val);
11160
          break;
11161
        }
11162
      return p;
11163
 
11164
    case Tag_ABI_DSBT:
11165
      val = read_uleb128 (p, &len);
11166
      p += len;
11167
      printf ("  Tag_ABI_DSBT: ");
11168
      switch (val)
11169
        {
11170
        case 0:
11171
          printf (_("DSBT addressing not used\n"));
11172
          break;
11173
        case 1:
11174
          printf (_("DSBT addressing used\n"));
11175
          break;
11176
        default:
11177
          printf ("??? (%d)\n", val);
11178
          break;
11179
        }
11180
      return p;
11181
 
11182
    case Tag_ABI_PID:
11183
      val = read_uleb128 (p, &len);
11184
      p += len;
11185
      printf ("  Tag_ABI_PID: ");
11186
      switch (val)
11187
        {
11188
        case 0:
11189
          printf (_("Data addressing position-dependent\n"));
11190
          break;
11191
        case 1:
11192
          printf (_("Data addressing position-independent, GOT near DP\n"));
11193
          break;
11194
        case 2:
11195
          printf (_("Data addressing position-independent, GOT far from DP\n"));
11196
          break;
11197
        default:
11198
          printf ("??? (%d)\n", val);
11199
          break;
11200
        }
11201
      return p;
11202
 
11203
    case Tag_ABI_PIC:
11204
      val = read_uleb128 (p, &len);
11205
      p += len;
11206
      printf ("  Tag_ABI_PIC: ");
11207
      switch (val)
11208
        {
11209
        case 0:
11210
          printf (_("Code addressing position-dependent\n"));
11211
          break;
11212
        case 1:
11213
          printf (_("Code addressing position-independent\n"));
11214
          break;
11215
        default:
11216
          printf ("??? (%d)\n", val);
11217
          break;
11218
        }
11219
      return p;
11220
 
11221
    case Tag_ABI_array_object_alignment:
11222
      val = read_uleb128 (p, &len);
11223
      p += len;
11224
      printf ("  Tag_ABI_array_object_alignment: ");
11225
      switch (val)
11226
        {
11227
        case 0:
11228
          printf (_("8-byte\n"));
11229
          break;
11230
        case 1:
11231
          printf (_("4-byte\n"));
11232
          break;
11233
        case 2:
11234
          printf (_("16-byte\n"));
11235
          break;
11236
        default:
11237
          printf ("??? (%d)\n", val);
11238
          break;
11239
        }
11240
      return p;
11241
 
11242
    case Tag_ABI_array_object_align_expected:
11243
      val = read_uleb128 (p, &len);
11244
      p += len;
11245
      printf ("  Tag_ABI_array_object_align_expected: ");
11246
      switch (val)
11247
        {
11248
        case 0:
11249
          printf (_("8-byte\n"));
11250
          break;
11251
        case 1:
11252
          printf (_("4-byte\n"));
11253
          break;
11254
        case 2:
11255
          printf (_("16-byte\n"));
11256
          break;
11257
        default:
11258
          printf ("??? (%d)\n", val);
11259
          break;
11260
        }
11261
      return p;
11262
 
11263
    case Tag_ABI_compatibility:
11264
      val = read_uleb128 (p, &len);
11265
      p += len;
11266
      printf ("  Tag_ABI_compatibility: ");
11267
      printf (_("flag = %d, vendor = %s\n"), val, p);
11268
      p += strlen ((char *) p) + 1;
11269
      return p;
11270
 
11271
    case Tag_ABI_conformance:
11272
      printf ("  Tag_ABI_conformance: ");
11273
      printf ("\"%s\"\n", p);
11274
      p += strlen ((char *) p) + 1;
11275
      return p;
11276
    }
11277
 
11278
  printf ("  Tag_unknown_%d: ", tag);
11279
 
11280
  if (tag & 1)
11281
    {
11282
      printf ("\"%s\"\n", p);
11283
      p += strlen ((char *) p) + 1;
11284
    }
11285
  else
11286
    {
11287
      val = read_uleb128 (p, &len);
11288
      p += len;
11289
      printf ("%d (0x%x)\n", val, val);
11290
    }
11291
 
11292
  return p;
11293
}
11294
 
11295
static int
11296
process_attributes (FILE * file,
11297
                    const char * public_name,
11298
                    unsigned int proc_type,
11299
                    unsigned char * (* display_pub_attribute) (unsigned char *),
11300
                    unsigned char * (* display_proc_gnu_attribute) (unsigned char *, int))
11301
{
11302
  Elf_Internal_Shdr * sect;
11303
  unsigned char * contents;
11304
  unsigned char * p;
11305
  unsigned char * end;
11306
  bfd_vma section_len;
11307
  bfd_vma len;
11308
  unsigned i;
11309
 
11310
  /* Find the section header so that we get the size.  */
11311
  for (i = 0, sect = section_headers;
11312
       i < elf_header.e_shnum;
11313
       i++, sect++)
11314
    {
11315
      if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
11316
        continue;
11317
 
11318
      contents = (unsigned char *) get_data (NULL, file, sect->sh_offset, 1,
11319
                                             sect->sh_size, _("attributes"));
11320
      if (contents == NULL)
11321
        continue;
11322
 
11323
      p = contents;
11324
      if (*p == 'A')
11325
        {
11326
          len = sect->sh_size - 1;
11327
          p++;
11328
 
11329
          while (len > 0)
11330
            {
11331
              int namelen;
11332
              bfd_boolean public_section;
11333
              bfd_boolean gnu_section;
11334
 
11335
              section_len = byte_get (p, 4);
11336
              p += 4;
11337
 
11338
              if (section_len > len)
11339
                {
11340
                  printf (_("ERROR: Bad section length (%d > %d)\n"),
11341
                          (int) section_len, (int) len);
11342
                  section_len = len;
11343
                }
11344
 
11345
              len -= section_len;
11346
              printf (_("Attribute Section: %s\n"), p);
11347
 
11348
              if (public_name && streq ((char *) p, public_name))
11349
                public_section = TRUE;
11350
              else
11351
                public_section = FALSE;
11352
 
11353
              if (streq ((char *) p, "gnu"))
11354
                gnu_section = TRUE;
11355
              else
11356
                gnu_section = FALSE;
11357
 
11358
              namelen = strlen ((char *) p) + 1;
11359
              p += namelen;
11360
              section_len -= namelen + 4;
11361
 
11362
              while (section_len > 0)
11363
                {
11364
                  int tag = *(p++);
11365
                  int val;
11366
                  bfd_vma size;
11367
 
11368
                  size = byte_get (p, 4);
11369
                  if (size > section_len)
11370
                    {
11371
                      printf (_("ERROR: Bad subsection length (%d > %d)\n"),
11372
                              (int) size, (int) section_len);
11373
                      size = section_len;
11374
                    }
11375
 
11376
                  section_len -= size;
11377
                  end = p + size - 1;
11378
                  p += 4;
11379
 
11380
                  switch (tag)
11381
                    {
11382
                    case 1:
11383
                      printf (_("File Attributes\n"));
11384
                      break;
11385
                    case 2:
11386
                      printf (_("Section Attributes:"));
11387
                      goto do_numlist;
11388
                    case 3:
11389
                      printf (_("Symbol Attributes:"));
11390
                    do_numlist:
11391
                      for (;;)
11392
                        {
11393
                          unsigned int j;
11394
 
11395
                          val = read_uleb128 (p, &j);
11396
                          p += j;
11397
                          if (val == 0)
11398
                            break;
11399
                          printf (" %d", val);
11400
                        }
11401
                      printf ("\n");
11402
                      break;
11403
                    default:
11404
                      printf (_("Unknown tag: %d\n"), tag);
11405
                      public_section = FALSE;
11406
                      break;
11407
                    }
11408
 
11409
                  if (public_section)
11410
                    {
11411
                      while (p < end)
11412
                        p = display_pub_attribute (p);
11413
                    }
11414
                  else if (gnu_section)
11415
                    {
11416
                      while (p < end)
11417
                        p = display_gnu_attribute (p,
11418
                                                   display_proc_gnu_attribute);
11419
                    }
11420
                  else
11421
                    {
11422
                      /* ??? Do something sensible, like dump hex.  */
11423
                      printf (_("  Unknown section contexts\n"));
11424
                      p = end;
11425
                    }
11426
                }
11427
            }
11428
        }
11429
      else
11430
        printf (_("Unknown format '%c'\n"), *p);
11431
 
11432
      free (contents);
11433
    }
11434
  return 1;
11435
}
11436
 
11437
static int
11438
process_arm_specific (FILE * file)
11439
{
11440
  return process_attributes (file, "aeabi", SHT_ARM_ATTRIBUTES,
11441
                             display_arm_attribute, NULL);
11442
}
11443
 
11444
static int
11445
process_power_specific (FILE * file)
11446
{
11447
  return process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
11448
                             display_power_gnu_attribute);
11449
}
11450
 
11451
static int
11452
process_tic6x_specific (FILE * file)
11453
{
11454
  return process_attributes (file, "c6xabi", SHT_C6000_ATTRIBUTES,
11455
                             display_tic6x_attribute, NULL);
11456
}
11457
 
11458
/* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
11459
   Print the Address, Access and Initial fields of an entry at VMA ADDR
11460
   and return the VMA of the next entry.  */
11461
 
11462
static bfd_vma
11463
print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
11464
{
11465
  printf ("  ");
11466
  print_vma (addr, LONG_HEX);
11467
  printf (" ");
11468
  if (addr < pltgot + 0xfff0)
11469
    printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
11470
  else
11471
    printf ("%10s", "");
11472
  printf (" ");
11473
  if (data == NULL)
11474
    printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
11475
  else
11476
    {
11477
      bfd_vma entry;
11478
 
11479
      entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
11480
      print_vma (entry, LONG_HEX);
11481
    }
11482
  return addr + (is_32bit_elf ? 4 : 8);
11483
}
11484
 
11485
/* DATA points to the contents of a MIPS PLT GOT that starts at VMA
11486
   PLTGOT.  Print the Address and Initial fields of an entry at VMA
11487
   ADDR and return the VMA of the next entry.  */
11488
 
11489
static bfd_vma
11490
print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
11491
{
11492
  printf ("  ");
11493
  print_vma (addr, LONG_HEX);
11494
  printf (" ");
11495
  if (data == NULL)
11496
    printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
11497
  else
11498
    {
11499
      bfd_vma entry;
11500
 
11501
      entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
11502
      print_vma (entry, LONG_HEX);
11503
    }
11504
  return addr + (is_32bit_elf ? 4 : 8);
11505
}
11506
 
11507
static int
11508
process_mips_specific (FILE * file)
11509
{
11510
  Elf_Internal_Dyn * entry;
11511
  size_t liblist_offset = 0;
11512
  size_t liblistno = 0;
11513
  size_t conflictsno = 0;
11514
  size_t options_offset = 0;
11515
  size_t conflicts_offset = 0;
11516
  size_t pltrelsz = 0;
11517
  size_t pltrel = 0;
11518
  bfd_vma pltgot = 0;
11519
  bfd_vma mips_pltgot = 0;
11520
  bfd_vma jmprel = 0;
11521
  bfd_vma local_gotno = 0;
11522
  bfd_vma gotsym = 0;
11523
  bfd_vma symtabno = 0;
11524
 
11525
  process_attributes (file, NULL, SHT_GNU_ATTRIBUTES, NULL,
11526
                      display_mips_gnu_attribute);
11527
 
11528
  /* We have a lot of special sections.  Thanks SGI!  */
11529
  if (dynamic_section == NULL)
11530
    /* No information available.  */
11531
    return 0;
11532
 
11533
  for (entry = dynamic_section; entry->d_tag != DT_NULL; ++entry)
11534
    switch (entry->d_tag)
11535
      {
11536
      case DT_MIPS_LIBLIST:
11537
        liblist_offset
11538
          = offset_from_vma (file, entry->d_un.d_val,
11539
                             liblistno * sizeof (Elf32_External_Lib));
11540
        break;
11541
      case DT_MIPS_LIBLISTNO:
11542
        liblistno = entry->d_un.d_val;
11543
        break;
11544
      case DT_MIPS_OPTIONS:
11545
        options_offset = offset_from_vma (file, entry->d_un.d_val, 0);
11546
        break;
11547
      case DT_MIPS_CONFLICT:
11548
        conflicts_offset
11549
          = offset_from_vma (file, entry->d_un.d_val,
11550
                             conflictsno * sizeof (Elf32_External_Conflict));
11551
        break;
11552
      case DT_MIPS_CONFLICTNO:
11553
        conflictsno = entry->d_un.d_val;
11554
        break;
11555
      case DT_PLTGOT:
11556
        pltgot = entry->d_un.d_ptr;
11557
        break;
11558
      case DT_MIPS_LOCAL_GOTNO:
11559
        local_gotno = entry->d_un.d_val;
11560
        break;
11561
      case DT_MIPS_GOTSYM:
11562
        gotsym = entry->d_un.d_val;
11563
        break;
11564
      case DT_MIPS_SYMTABNO:
11565
        symtabno = entry->d_un.d_val;
11566
        break;
11567
      case DT_MIPS_PLTGOT:
11568
        mips_pltgot = entry->d_un.d_ptr;
11569
        break;
11570
      case DT_PLTREL:
11571
        pltrel = entry->d_un.d_val;
11572
        break;
11573
      case DT_PLTRELSZ:
11574
        pltrelsz = entry->d_un.d_val;
11575
        break;
11576
      case DT_JMPREL:
11577
        jmprel = entry->d_un.d_ptr;
11578
        break;
11579
      default:
11580
        break;
11581
      }
11582
 
11583
  if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
11584
    {
11585
      Elf32_External_Lib * elib;
11586
      size_t cnt;
11587
 
11588
      elib = (Elf32_External_Lib *) get_data (NULL, file, liblist_offset,
11589
                                              liblistno,
11590
                                              sizeof (Elf32_External_Lib),
11591
                                              _("liblist"));
11592
      if (elib)
11593
        {
11594
          printf (_("\nSection '.liblist' contains %lu entries:\n"),
11595
                  (unsigned long) liblistno);
11596
          fputs (_("     Library              Time Stamp          Checksum   Version Flags\n"),
11597
                 stdout);
11598
 
11599
          for (cnt = 0; cnt < liblistno; ++cnt)
11600
            {
11601
              Elf32_Lib liblist;
11602
              time_t atime;
11603
              char timebuf[20];
11604
              struct tm * tmp;
11605
 
11606
              liblist.l_name = BYTE_GET (elib[cnt].l_name);
11607
              atime = BYTE_GET (elib[cnt].l_time_stamp);
11608
              liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
11609
              liblist.l_version = BYTE_GET (elib[cnt].l_version);
11610
              liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
11611
 
11612
              tmp = gmtime (&atime);
11613
              snprintf (timebuf, sizeof (timebuf),
11614
                        "%04u-%02u-%02uT%02u:%02u:%02u",
11615
                        tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11616
                        tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
11617
 
11618
              printf ("%3lu: ", (unsigned long) cnt);
11619
              if (VALID_DYNAMIC_NAME (liblist.l_name))
11620
                print_symbol (20, GET_DYNAMIC_NAME (liblist.l_name));
11621
              else
11622
                printf (_("<corrupt: %9ld>"), liblist.l_name);
11623
              printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
11624
                      liblist.l_version);
11625
 
11626
              if (liblist.l_flags == 0)
11627
                puts (_(" NONE"));
11628
              else
11629
                {
11630
                  static const struct
11631
                  {
11632
                    const char * name;
11633
                    int bit;
11634
                  }
11635
                  l_flags_vals[] =
11636
                  {
11637
                    { " EXACT_MATCH", LL_EXACT_MATCH },
11638
                    { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
11639
                    { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
11640
                    { " EXPORTS", LL_EXPORTS },
11641
                    { " DELAY_LOAD", LL_DELAY_LOAD },
11642
                    { " DELTA", LL_DELTA }
11643
                  };
11644
                  int flags = liblist.l_flags;
11645
                  size_t fcnt;
11646
 
11647
                  for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
11648
                    if ((flags & l_flags_vals[fcnt].bit) != 0)
11649
                      {
11650
                        fputs (l_flags_vals[fcnt].name, stdout);
11651
                        flags ^= l_flags_vals[fcnt].bit;
11652
                      }
11653
                  if (flags != 0)
11654
                    printf (" %#x", (unsigned int) flags);
11655
 
11656
                  puts ("");
11657
                }
11658
            }
11659
 
11660
          free (elib);
11661
        }
11662
    }
11663
 
11664
  if (options_offset != 0)
11665
    {
11666
      Elf_External_Options * eopt;
11667
      Elf_Internal_Shdr * sect = section_headers;
11668
      Elf_Internal_Options * iopt;
11669
      Elf_Internal_Options * option;
11670
      size_t offset;
11671
      int cnt;
11672
 
11673
      /* Find the section header so that we get the size.  */
11674
      while (sect->sh_type != SHT_MIPS_OPTIONS)
11675
        ++sect;
11676
 
11677
      eopt = (Elf_External_Options *) get_data (NULL, file, options_offset, 1,
11678
                                                sect->sh_size, _("options"));
11679
      if (eopt)
11680
        {
11681
          iopt = (Elf_Internal_Options *)
11682
              cmalloc ((sect->sh_size / sizeof (eopt)), sizeof (* iopt));
11683
          if (iopt == NULL)
11684
            {
11685
              error (_("Out of memory\n"));
11686
              return 0;
11687
            }
11688
 
11689
          offset = cnt = 0;
11690
          option = iopt;
11691
 
11692
          while (offset < sect->sh_size)
11693
            {
11694
              Elf_External_Options * eoption;
11695
 
11696
              eoption = (Elf_External_Options *) ((char *) eopt + offset);
11697
 
11698
              option->kind = BYTE_GET (eoption->kind);
11699
              option->size = BYTE_GET (eoption->size);
11700
              option->section = BYTE_GET (eoption->section);
11701
              option->info = BYTE_GET (eoption->info);
11702
 
11703
              offset += option->size;
11704
 
11705
              ++option;
11706
              ++cnt;
11707
            }
11708
 
11709
          printf (_("\nSection '%s' contains %d entries:\n"),
11710
                  SECTION_NAME (sect), cnt);
11711
 
11712
          option = iopt;
11713
 
11714
          while (cnt-- > 0)
11715
            {
11716
              size_t len;
11717
 
11718
              switch (option->kind)
11719
                {
11720
                case ODK_NULL:
11721
                  /* This shouldn't happen.  */
11722
                  printf (" NULL       %d %lx", option->section, option->info);
11723
                  break;
11724
                case ODK_REGINFO:
11725
                  printf (" REGINFO    ");
11726
                  if (elf_header.e_machine == EM_MIPS)
11727
                    {
11728
                      /* 32bit form.  */
11729
                      Elf32_External_RegInfo * ereg;
11730
                      Elf32_RegInfo reginfo;
11731
 
11732
                      ereg = (Elf32_External_RegInfo *) (option + 1);
11733
                      reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
11734
                      reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
11735
                      reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
11736
                      reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
11737
                      reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
11738
                      reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
11739
 
11740
                      printf ("GPR %08lx  GP 0x%lx\n",
11741
                              reginfo.ri_gprmask,
11742
                              (unsigned long) reginfo.ri_gp_value);
11743
                      printf ("            CPR0 %08lx  CPR1 %08lx  CPR2 %08lx  CPR3 %08lx\n",
11744
                              reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
11745
                              reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
11746
                    }
11747
                  else
11748
                    {
11749
                      /* 64 bit form.  */
11750
                      Elf64_External_RegInfo * ereg;
11751
                      Elf64_Internal_RegInfo reginfo;
11752
 
11753
                      ereg = (Elf64_External_RegInfo *) (option + 1);
11754
                      reginfo.ri_gprmask    = BYTE_GET (ereg->ri_gprmask);
11755
                      reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
11756
                      reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
11757
                      reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
11758
                      reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
11759
                      reginfo.ri_gp_value   = BYTE_GET (ereg->ri_gp_value);
11760
 
11761
                      printf ("GPR %08lx  GP 0x",
11762
                              reginfo.ri_gprmask);
11763
                      printf_vma (reginfo.ri_gp_value);
11764
                      printf ("\n");
11765
 
11766
                      printf ("            CPR0 %08lx  CPR1 %08lx  CPR2 %08lx  CPR3 %08lx\n",
11767
                              reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
11768
                              reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
11769
                    }
11770
                  ++option;
11771
                  continue;
11772
                case ODK_EXCEPTIONS:
11773
                  fputs (" EXCEPTIONS fpe_min(", stdout);
11774
                  process_mips_fpe_exception (option->info & OEX_FPU_MIN);
11775
                  fputs (") fpe_max(", stdout);
11776
                  process_mips_fpe_exception ((option->info & OEX_FPU_MAX) >> 8);
11777
                  fputs (")", stdout);
11778
 
11779
                  if (option->info & OEX_PAGE0)
11780
                    fputs (" PAGE0", stdout);
11781
                  if (option->info & OEX_SMM)
11782
                    fputs (" SMM", stdout);
11783
                  if (option->info & OEX_FPDBUG)
11784
                    fputs (" FPDBUG", stdout);
11785
                  if (option->info & OEX_DISMISS)
11786
                    fputs (" DISMISS", stdout);
11787
                  break;
11788
                case ODK_PAD:
11789
                  fputs (" PAD       ", stdout);
11790
                  if (option->info & OPAD_PREFIX)
11791
                    fputs (" PREFIX", stdout);
11792
                  if (option->info & OPAD_POSTFIX)
11793
                    fputs (" POSTFIX", stdout);
11794
                  if (option->info & OPAD_SYMBOL)
11795
                    fputs (" SYMBOL", stdout);
11796
                  break;
11797
                case ODK_HWPATCH:
11798
                  fputs (" HWPATCH   ", stdout);
11799
                  if (option->info & OHW_R4KEOP)
11800
                    fputs (" R4KEOP", stdout);
11801
                  if (option->info & OHW_R8KPFETCH)
11802
                    fputs (" R8KPFETCH", stdout);
11803
                  if (option->info & OHW_R5KEOP)
11804
                    fputs (" R5KEOP", stdout);
11805
                  if (option->info & OHW_R5KCVTL)
11806
                    fputs (" R5KCVTL", stdout);
11807
                  break;
11808
                case ODK_FILL:
11809
                  fputs (" FILL       ", stdout);
11810
                  /* XXX Print content of info word?  */
11811
                  break;
11812
                case ODK_TAGS:
11813
                  fputs (" TAGS       ", stdout);
11814
                  /* XXX Print content of info word?  */
11815
                  break;
11816
                case ODK_HWAND:
11817
                  fputs (" HWAND     ", stdout);
11818
                  if (option->info & OHWA0_R4KEOP_CHECKED)
11819
                    fputs (" R4KEOP_CHECKED", stdout);
11820
                  if (option->info & OHWA0_R4KEOP_CLEAN)
11821
                    fputs (" R4KEOP_CLEAN", stdout);
11822
                  break;
11823
                case ODK_HWOR:
11824
                  fputs (" HWOR      ", stdout);
11825
                  if (option->info & OHWA0_R4KEOP_CHECKED)
11826
                    fputs (" R4KEOP_CHECKED", stdout);
11827
                  if (option->info & OHWA0_R4KEOP_CLEAN)
11828
                    fputs (" R4KEOP_CLEAN", stdout);
11829
                  break;
11830
                case ODK_GP_GROUP:
11831
                  printf (" GP_GROUP  %#06lx  self-contained %#06lx",
11832
                          option->info & OGP_GROUP,
11833
                          (option->info & OGP_SELF) >> 16);
11834
                  break;
11835
                case ODK_IDENT:
11836
                  printf (" IDENT     %#06lx  self-contained %#06lx",
11837
                          option->info & OGP_GROUP,
11838
                          (option->info & OGP_SELF) >> 16);
11839
                  break;
11840
                default:
11841
                  /* This shouldn't happen.  */
11842
                  printf (" %3d ???     %d %lx",
11843
                          option->kind, option->section, option->info);
11844
                  break;
11845
                }
11846
 
11847
              len = sizeof (* eopt);
11848
              while (len < option->size)
11849
                if (((char *) option)[len] >= ' '
11850
                    && ((char *) option)[len] < 0x7f)
11851
                  printf ("%c", ((char *) option)[len++]);
11852
                else
11853
                  printf ("\\%03o", ((char *) option)[len++]);
11854
 
11855
              fputs ("\n", stdout);
11856
              ++option;
11857
            }
11858
 
11859
          free (eopt);
11860
        }
11861
    }
11862
 
11863
  if (conflicts_offset != 0 && conflictsno != 0)
11864
    {
11865
      Elf32_Conflict * iconf;
11866
      size_t cnt;
11867
 
11868
      if (dynamic_symbols == NULL)
11869
        {
11870
          error (_("conflict list found without a dynamic symbol table\n"));
11871
          return 0;
11872
        }
11873
 
11874
      iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
11875
      if (iconf == NULL)
11876
        {
11877
          error (_("Out of memory\n"));
11878
          return 0;
11879
        }
11880
 
11881
      if (is_32bit_elf)
11882
        {
11883
          Elf32_External_Conflict * econf32;
11884
 
11885
          econf32 = (Elf32_External_Conflict *)
11886
              get_data (NULL, file, conflicts_offset, conflictsno,
11887
                        sizeof (* econf32), _("conflict"));
11888
          if (!econf32)
11889
            return 0;
11890
 
11891
          for (cnt = 0; cnt < conflictsno; ++cnt)
11892
            iconf[cnt] = BYTE_GET (econf32[cnt]);
11893
 
11894
          free (econf32);
11895
        }
11896
      else
11897
        {
11898
          Elf64_External_Conflict * econf64;
11899
 
11900
          econf64 = (Elf64_External_Conflict *)
11901
              get_data (NULL, file, conflicts_offset, conflictsno,
11902
                        sizeof (* econf64), _("conflict"));
11903
          if (!econf64)
11904
            return 0;
11905
 
11906
          for (cnt = 0; cnt < conflictsno; ++cnt)
11907
            iconf[cnt] = BYTE_GET (econf64[cnt]);
11908
 
11909
          free (econf64);
11910
        }
11911
 
11912
      printf (_("\nSection '.conflict' contains %lu entries:\n"),
11913
              (unsigned long) conflictsno);
11914
      puts (_("  Num:    Index       Value  Name"));
11915
 
11916
      for (cnt = 0; cnt < conflictsno; ++cnt)
11917
        {
11918
          Elf_Internal_Sym * psym = & dynamic_symbols[iconf[cnt]];
11919
 
11920
          printf ("%5lu: %8lu  ", (unsigned long) cnt, iconf[cnt]);
11921
          print_vma (psym->st_value, FULL_HEX);
11922
          putchar (' ');
11923
          if (VALID_DYNAMIC_NAME (psym->st_name))
11924
            print_symbol (25, GET_DYNAMIC_NAME (psym->st_name));
11925
          else
11926
            printf (_("<corrupt: %14ld>"), psym->st_name);
11927
          putchar ('\n');
11928
        }
11929
 
11930
      free (iconf);
11931
    }
11932
 
11933
  if (pltgot != 0 && local_gotno != 0)
11934
    {
11935
      bfd_vma ent, local_end, global_end;
11936
      size_t i, offset;
11937
      unsigned char * data;
11938
      int addr_size;
11939
 
11940
      ent = pltgot;
11941
      addr_size = (is_32bit_elf ? 4 : 8);
11942
      local_end = pltgot + local_gotno * addr_size;
11943
      global_end = local_end + (symtabno - gotsym) * addr_size;
11944
 
11945
      offset = offset_from_vma (file, pltgot, global_end - pltgot);
11946
      data = (unsigned char *) get_data (NULL, file, offset,
11947
                                         global_end - pltgot, 1, _("GOT"));
11948
      printf (_("\nPrimary GOT:\n"));
11949
      printf (_(" Canonical gp value: "));
11950
      print_vma (pltgot + 0x7ff0, LONG_HEX);
11951
      printf ("\n\n");
11952
 
11953
      printf (_(" Reserved entries:\n"));
11954
      printf (_("  %*s %10s %*s Purpose\n"),
11955
              addr_size * 2, _("Address"), _("Access"),
11956
              addr_size * 2, _("Initial"));
11957
      ent = print_mips_got_entry (data, pltgot, ent);
11958
      printf (_(" Lazy resolver\n"));
11959
      if (data
11960
          && (byte_get (data + ent - pltgot, addr_size)
11961
              >> (addr_size * 8 - 1)) != 0)
11962
        {
11963
          ent = print_mips_got_entry (data, pltgot, ent);
11964
          printf (_(" Module pointer (GNU extension)\n"));
11965
        }
11966
      printf ("\n");
11967
 
11968
      if (ent < local_end)
11969
        {
11970
          printf (_(" Local entries:\n"));
11971
          printf ("  %*s %10s %*s\n",
11972
                  addr_size * 2, _("Address"), _("Access"),
11973
                  addr_size * 2, _("Initial"));
11974
          while (ent < local_end)
11975
            {
11976
              ent = print_mips_got_entry (data, pltgot, ent);
11977
              printf ("\n");
11978
            }
11979
          printf ("\n");
11980
        }
11981
 
11982
      if (gotsym < symtabno)
11983
        {
11984
          int sym_width;
11985
 
11986
          printf (_(" Global entries:\n"));
11987
          printf ("  %*s %10s %*s %*s %-7s %3s %s\n",
11988
                  addr_size * 2, _("Address"), _("Access"),
11989
                  addr_size * 2, _("Initial"),
11990
                  addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
11991
          sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
11992
          for (i = gotsym; i < symtabno; i++)
11993
            {
11994
              Elf_Internal_Sym * psym;
11995
 
11996
              psym = dynamic_symbols + i;
11997
              ent = print_mips_got_entry (data, pltgot, ent);
11998
              printf (" ");
11999
              print_vma (psym->st_value, LONG_HEX);
12000
              printf (" %-7s %3s ",
12001
                      get_symbol_type (ELF_ST_TYPE (psym->st_info)),
12002
                      get_symbol_index_type (psym->st_shndx));
12003
              if (VALID_DYNAMIC_NAME (psym->st_name))
12004
                print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
12005
              else
12006
                printf (_("<corrupt: %14ld>"), psym->st_name);
12007
              printf ("\n");
12008
            }
12009
          printf ("\n");
12010
        }
12011
 
12012
      if (data)
12013
        free (data);
12014
    }
12015
 
12016
  if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
12017
    {
12018
      bfd_vma ent, end;
12019
      size_t offset, rel_offset;
12020
      unsigned long count, i;
12021
      unsigned char * data;
12022
      int addr_size, sym_width;
12023
      Elf_Internal_Rela * rels;
12024
 
12025
      rel_offset = offset_from_vma (file, jmprel, pltrelsz);
12026
      if (pltrel == DT_RELA)
12027
        {
12028
          if (!slurp_rela_relocs (file, rel_offset, pltrelsz, &rels, &count))
12029
            return 0;
12030
        }
12031
      else
12032
        {
12033
          if (!slurp_rel_relocs (file, rel_offset, pltrelsz, &rels, &count))
12034
            return 0;
12035
        }
12036
 
12037
      ent = mips_pltgot;
12038
      addr_size = (is_32bit_elf ? 4 : 8);
12039
      end = mips_pltgot + (2 + count) * addr_size;
12040
 
12041
      offset = offset_from_vma (file, mips_pltgot, end - mips_pltgot);
12042
      data = (unsigned char *) get_data (NULL, file, offset, end - mips_pltgot,
12043
                                         1, _("PLT GOT"));
12044
      printf (_("\nPLT GOT:\n\n"));
12045
      printf (_(" Reserved entries:\n"));
12046
      printf (_("  %*s %*s Purpose\n"),
12047
              addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
12048
      ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
12049
      printf (_(" PLT lazy resolver\n"));
12050
      ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
12051
      printf (_(" Module pointer\n"));
12052
      printf ("\n");
12053
 
12054
      printf (_(" Entries:\n"));
12055
      printf ("  %*s %*s %*s %-7s %3s %s\n",
12056
              addr_size * 2, _("Address"),
12057
              addr_size * 2, _("Initial"),
12058
              addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
12059
      sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
12060
      for (i = 0; i < count; i++)
12061
        {
12062
          Elf_Internal_Sym * psym;
12063
 
12064
          psym = dynamic_symbols + get_reloc_symindex (rels[i].r_info);
12065
          ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
12066
          printf (" ");
12067
          print_vma (psym->st_value, LONG_HEX);
12068
          printf (" %-7s %3s ",
12069
                  get_symbol_type (ELF_ST_TYPE (psym->st_info)),
12070
                  get_symbol_index_type (psym->st_shndx));
12071
          if (VALID_DYNAMIC_NAME (psym->st_name))
12072
            print_symbol (sym_width, GET_DYNAMIC_NAME (psym->st_name));
12073
          else
12074
            printf (_("<corrupt: %14ld>"), psym->st_name);
12075
          printf ("\n");
12076
        }
12077
      printf ("\n");
12078
 
12079
      if (data)
12080
        free (data);
12081
      free (rels);
12082
    }
12083
 
12084
  return 1;
12085
}
12086
 
12087
static int
12088
process_gnu_liblist (FILE * file)
12089
{
12090
  Elf_Internal_Shdr * section;
12091
  Elf_Internal_Shdr * string_sec;
12092
  Elf32_External_Lib * elib;
12093
  char * strtab;
12094
  size_t strtab_size;
12095
  size_t cnt;
12096
  unsigned i;
12097
 
12098
  if (! do_arch)
12099
    return 0;
12100
 
12101
  for (i = 0, section = section_headers;
12102
       i < elf_header.e_shnum;
12103
       i++, section++)
12104
    {
12105
      switch (section->sh_type)
12106
        {
12107
        case SHT_GNU_LIBLIST:
12108
          if (section->sh_link >= elf_header.e_shnum)
12109
            break;
12110
 
12111
          elib = (Elf32_External_Lib *)
12112
              get_data (NULL, file, section->sh_offset, 1, section->sh_size,
12113
                        _("liblist"));
12114
 
12115
          if (elib == NULL)
12116
            break;
12117
          string_sec = section_headers + section->sh_link;
12118
 
12119
          strtab = (char *) get_data (NULL, file, string_sec->sh_offset, 1,
12120
                                      string_sec->sh_size,
12121
                                      _("liblist string table"));
12122
          strtab_size = string_sec->sh_size;
12123
 
12124
          if (strtab == NULL
12125
              || section->sh_entsize != sizeof (Elf32_External_Lib))
12126
            {
12127
              free (elib);
12128
              free (strtab);
12129
              break;
12130
            }
12131
 
12132
          printf (_("\nLibrary list section '%s' contains %lu entries:\n"),
12133
                  SECTION_NAME (section),
12134
                  (unsigned long) (section->sh_size / sizeof (Elf32_External_Lib)));
12135
 
12136
          puts (_("     Library              Time Stamp          Checksum   Version Flags"));
12137
 
12138
          for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
12139
               ++cnt)
12140
            {
12141
              Elf32_Lib liblist;
12142
              time_t atime;
12143
              char timebuf[20];
12144
              struct tm * tmp;
12145
 
12146
              liblist.l_name = BYTE_GET (elib[cnt].l_name);
12147
              atime = BYTE_GET (elib[cnt].l_time_stamp);
12148
              liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
12149
              liblist.l_version = BYTE_GET (elib[cnt].l_version);
12150
              liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
12151
 
12152
              tmp = gmtime (&atime);
12153
              snprintf (timebuf, sizeof (timebuf),
12154
                        "%04u-%02u-%02uT%02u:%02u:%02u",
12155
                        tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
12156
                        tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
12157
 
12158
              printf ("%3lu: ", (unsigned long) cnt);
12159
              if (do_wide)
12160
                printf ("%-20s", liblist.l_name < strtab_size
12161
                        ? strtab + liblist.l_name : _("<corrupt>"));
12162
              else
12163
                printf ("%-20.20s", liblist.l_name < strtab_size
12164
                        ? strtab + liblist.l_name : _("<corrupt>"));
12165
              printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
12166
                      liblist.l_version, liblist.l_flags);
12167
            }
12168
 
12169
          free (elib);
12170
          free (strtab);
12171
        }
12172
    }
12173
 
12174
  return 1;
12175
}
12176
 
12177
static const char *
12178
get_note_type (unsigned e_type)
12179
{
12180
  static char buff[64];
12181
 
12182
  if (elf_header.e_type == ET_CORE)
12183
    switch (e_type)
12184
      {
12185
      case NT_AUXV:
12186
        return _("NT_AUXV (auxiliary vector)");
12187
      case NT_PRSTATUS:
12188
        return _("NT_PRSTATUS (prstatus structure)");
12189
      case NT_FPREGSET:
12190
        return _("NT_FPREGSET (floating point registers)");
12191
      case NT_PRPSINFO:
12192
        return _("NT_PRPSINFO (prpsinfo structure)");
12193
      case NT_TASKSTRUCT:
12194
        return _("NT_TASKSTRUCT (task structure)");
12195
      case NT_PRXFPREG:
12196
        return _("NT_PRXFPREG (user_xfpregs structure)");
12197
      case NT_PPC_VMX:
12198
        return _("NT_PPC_VMX (ppc Altivec registers)");
12199
      case NT_PPC_VSX:
12200
        return _("NT_PPC_VSX (ppc VSX registers)");
12201
      case NT_X86_XSTATE:
12202
        return _("NT_X86_XSTATE (x86 XSAVE extended state)");
12203
      case NT_S390_HIGH_GPRS:
12204
        return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
12205
      case NT_S390_TIMER:
12206
        return _("NT_S390_TIMER (s390 timer register)");
12207
      case NT_S390_TODCMP:
12208
        return _("NT_S390_TODCMP (s390 TOD comparator register)");
12209
      case NT_S390_TODPREG:
12210
        return _("NT_S390_TODPREG (s390 TOD programmable register)");
12211
      case NT_S390_CTRS:
12212
        return _("NT_S390_CTRS (s390 control registers)");
12213
      case NT_S390_PREFIX:
12214
        return _("NT_S390_PREFIX (s390 prefix register)");
12215
      case NT_PSTATUS:
12216
        return _("NT_PSTATUS (pstatus structure)");
12217
      case NT_FPREGS:
12218
        return _("NT_FPREGS (floating point registers)");
12219
      case NT_PSINFO:
12220
        return _("NT_PSINFO (psinfo structure)");
12221
      case NT_LWPSTATUS:
12222
        return _("NT_LWPSTATUS (lwpstatus_t structure)");
12223
      case NT_LWPSINFO:
12224
        return _("NT_LWPSINFO (lwpsinfo_t structure)");
12225
      case NT_WIN32PSTATUS:
12226
        return _("NT_WIN32PSTATUS (win32_pstatus structure)");
12227
      default:
12228
        break;
12229
      }
12230
  else
12231
    switch (e_type)
12232
      {
12233
      case NT_VERSION:
12234
        return _("NT_VERSION (version)");
12235
      case NT_ARCH:
12236
        return _("NT_ARCH (architecture)");
12237
      default:
12238
        break;
12239
      }
12240
 
12241
  snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
12242
  return buff;
12243
}
12244
 
12245
static const char *
12246
get_gnu_elf_note_type (unsigned e_type)
12247
{
12248
  static char buff[64];
12249
 
12250
  switch (e_type)
12251
    {
12252
    case NT_GNU_ABI_TAG:
12253
      return _("NT_GNU_ABI_TAG (ABI version tag)");
12254
    case NT_GNU_HWCAP:
12255
      return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
12256
    case NT_GNU_BUILD_ID:
12257
      return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
12258
    case NT_GNU_GOLD_VERSION:
12259
      return _("NT_GNU_GOLD_VERSION (gold version)");
12260
    default:
12261
      break;
12262
    }
12263
 
12264
  snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
12265
  return buff;
12266
}
12267
 
12268
static int
12269
print_gnu_note (Elf_Internal_Note *pnote)
12270
{
12271
  switch (pnote->type)
12272
    {
12273
    case NT_GNU_BUILD_ID:
12274
      {
12275
        unsigned long i;
12276
 
12277
        printf (_("    Build ID: "));
12278
        for (i = 0; i < pnote->descsz; ++i)
12279
          printf ("%02x", pnote->descdata[i] & 0xff);
12280
        printf (_("\n"));
12281
      }
12282
      break;
12283
 
12284
    case NT_GNU_ABI_TAG:
12285
      {
12286
        unsigned long os, major, minor, subminor;
12287
        const char *osname;
12288
 
12289
        os = byte_get ((unsigned char *) pnote->descdata, 4);
12290
        major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
12291
        minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
12292
        subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
12293
 
12294
        switch (os)
12295
          {
12296
          case GNU_ABI_TAG_LINUX:
12297
            osname = "Linux";
12298
            break;
12299
          case GNU_ABI_TAG_HURD:
12300
            osname = "Hurd";
12301
            break;
12302
          case GNU_ABI_TAG_SOLARIS:
12303
            osname = "Solaris";
12304
            break;
12305
          case GNU_ABI_TAG_FREEBSD:
12306
            osname = "FreeBSD";
12307
            break;
12308
          case GNU_ABI_TAG_NETBSD:
12309
            osname = "NetBSD";
12310
            break;
12311
          default:
12312
            osname = "Unknown";
12313
            break;
12314
          }
12315
 
12316
        printf (_("    OS: %s, ABI: %ld.%ld.%ld\n"), osname,
12317
                major, minor, subminor);
12318
      }
12319
      break;
12320
    }
12321
 
12322
  return 1;
12323
}
12324
 
12325
static const char *
12326
get_netbsd_elfcore_note_type (unsigned e_type)
12327
{
12328
  static char buff[64];
12329
 
12330
  if (e_type == NT_NETBSDCORE_PROCINFO)
12331
    {
12332
      /* NetBSD core "procinfo" structure.  */
12333
      return _("NetBSD procinfo structure");
12334
    }
12335
 
12336
  /* As of Jan 2002 there are no other machine-independent notes
12337
     defined for NetBSD core files.  If the note type is less
12338
     than the start of the machine-dependent note types, we don't
12339
     understand it.  */
12340
 
12341
  if (e_type < NT_NETBSDCORE_FIRSTMACH)
12342
    {
12343
      snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
12344
      return buff;
12345
    }
12346
 
12347
  switch (elf_header.e_machine)
12348
    {
12349
    /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
12350
       and PT_GETFPREGS == mach+2.  */
12351
 
12352
    case EM_OLD_ALPHA:
12353
    case EM_ALPHA:
12354
    case EM_SPARC:
12355
    case EM_SPARC32PLUS:
12356
    case EM_SPARCV9:
12357
      switch (e_type)
12358
        {
12359
        case NT_NETBSDCORE_FIRSTMACH + 0:
12360
          return _("PT_GETREGS (reg structure)");
12361
        case NT_NETBSDCORE_FIRSTMACH + 2:
12362
          return _("PT_GETFPREGS (fpreg structure)");
12363
        default:
12364
          break;
12365
        }
12366
      break;
12367
 
12368
    /* On all other arch's, PT_GETREGS == mach+1 and
12369
       PT_GETFPREGS == mach+3.  */
12370
    default:
12371
      switch (e_type)
12372
        {
12373
        case NT_NETBSDCORE_FIRSTMACH + 1:
12374
          return _("PT_GETREGS (reg structure)");
12375
        case NT_NETBSDCORE_FIRSTMACH + 3:
12376
          return _("PT_GETFPREGS (fpreg structure)");
12377
        default:
12378
          break;
12379
        }
12380
    }
12381
 
12382
  snprintf (buff, sizeof (buff), _("PT_FIRSTMACH+%d"),
12383
            e_type - NT_NETBSDCORE_FIRSTMACH);
12384
  return buff;
12385
}
12386
 
12387
static const char *
12388
get_stapsdt_note_type (unsigned e_type)
12389
{
12390
  static char buff[64];
12391
 
12392
  switch (e_type)
12393
    {
12394
    case NT_STAPSDT:
12395
      return _("NT_STAPSDT (SystemTap probe descriptors)");
12396
 
12397
    default:
12398
      break;
12399
    }
12400
 
12401
  snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
12402
  return buff;
12403
}
12404
 
12405
static int
12406
print_stapsdt_note (Elf_Internal_Note *pnote)
12407
{
12408
  int addr_size = is_32bit_elf ? 4 : 8;
12409
  char *data = pnote->descdata;
12410
  char *data_end = pnote->descdata + pnote->descsz;
12411
  bfd_vma pc, base_addr, semaphore;
12412
  char *provider, *probe, *arg_fmt;
12413
 
12414
  pc = byte_get ((unsigned char *) data, addr_size);
12415
  data += addr_size;
12416
  base_addr = byte_get ((unsigned char *) data, addr_size);
12417
  data += addr_size;
12418
  semaphore = byte_get ((unsigned char *) data, addr_size);
12419
  data += addr_size;
12420
 
12421
  provider = data;
12422
  data += strlen (data) + 1;
12423
  probe = data;
12424
  data += strlen (data) + 1;
12425
  arg_fmt = data;
12426
  data += strlen (data) + 1;
12427
 
12428
  printf (_("    Provider: %s\n"), provider);
12429
  printf (_("    Name: %s\n"), probe);
12430
  printf (_("    Location: "));
12431
  print_vma (pc, FULL_HEX);
12432
  printf (_(", Base: "));
12433
  print_vma (base_addr, FULL_HEX);
12434
  printf (_(", Semaphore: "));
12435
  print_vma (semaphore, FULL_HEX);
12436
  printf (_("\n"));
12437
  printf (_("    Arguments: %s\n"), arg_fmt);
12438
 
12439
  return data == data_end;
12440
}
12441
 
12442
static const char *
12443
get_ia64_vms_note_type (unsigned e_type)
12444
{
12445
  static char buff[64];
12446
 
12447
  switch (e_type)
12448
    {
12449
    case NT_VMS_MHD:
12450
      return _("NT_VMS_MHD (module header)");
12451
    case NT_VMS_LNM:
12452
      return _("NT_VMS_LNM (language name)");
12453
    case NT_VMS_SRC:
12454
      return _("NT_VMS_SRC (source files)");
12455
    case NT_VMS_TITLE:
12456
      return _("NT_VMS_TITLE");
12457
    case NT_VMS_EIDC:
12458
      return _("NT_VMS_EIDC (consistency check)");
12459
    case NT_VMS_FPMODE:
12460
      return _("NT_VMS_FPMODE (FP mode)");
12461
    case NT_VMS_LINKTIME:
12462
      return _("NT_VMS_LINKTIME");
12463
    case NT_VMS_IMGNAM:
12464
      return _("NT_VMS_IMGNAM (image name)");
12465
    case NT_VMS_IMGID:
12466
      return _("NT_VMS_IMGID (image id)");
12467
    case NT_VMS_LINKID:
12468
      return _("NT_VMS_LINKID (link id)");
12469
    case NT_VMS_IMGBID:
12470
      return _("NT_VMS_IMGBID (build id)");
12471
    case NT_VMS_GSTNAM:
12472
      return _("NT_VMS_GSTNAM (sym table name)");
12473
    case NT_VMS_ORIG_DYN:
12474
      return _("NT_VMS_ORIG_DYN");
12475
    case NT_VMS_PATCHTIME:
12476
      return _("NT_VMS_PATCHTIME");
12477
    default:
12478
      snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
12479
      return buff;
12480
    }
12481
}
12482
 
12483
static int
12484
print_ia64_vms_note (Elf_Internal_Note * pnote)
12485
{
12486
  switch (pnote->type)
12487
    {
12488
    case NT_VMS_MHD:
12489
      if (pnote->descsz > 36)
12490
        {
12491
          size_t l = strlen (pnote->descdata + 34);
12492
          printf (_("    Creation date  : %.17s\n"), pnote->descdata);
12493
          printf (_("    Last patch date: %.17s\n"), pnote->descdata + 17);
12494
          printf (_("    Module name    : %s\n"), pnote->descdata + 34);
12495
          printf (_("    Module version : %s\n"), pnote->descdata + 34 + l + 1);
12496
        }
12497
      else
12498
        printf (_("    Invalid size\n"));
12499
      break;
12500
    case NT_VMS_LNM:
12501
      printf (_("   Language: %s\n"), pnote->descdata);
12502
      break;
12503
#ifdef BFD64
12504
    case NT_VMS_FPMODE:
12505
      printf (_("   FP mode: 0x%016" BFD_VMA_FMT "x\n"),
12506
              (bfd_vma)byte_get ((unsigned char *)pnote->descdata, 8));
12507
      break;
12508
    case NT_VMS_LINKTIME:
12509
      printf (_("   Link time: "));
12510
      print_vms_time
12511
        ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
12512
      printf ("\n");
12513
      break;
12514
    case NT_VMS_PATCHTIME:
12515
      printf (_("   Patch time: "));
12516
      print_vms_time
12517
        ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
12518
      printf ("\n");
12519
      break;
12520
    case NT_VMS_ORIG_DYN:
12521
      printf (_("   Major id: %u,  minor id: %u\n"),
12522
              (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
12523
              (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
12524
      printf (_("   Manip date  : "));
12525
      print_vms_time
12526
        ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
12527
      printf (_("\n"
12528
                "   Link flags  : 0x%016" BFD_VMA_FMT "x\n"),
12529
              (bfd_vma)byte_get ((unsigned char *)pnote->descdata + 16, 8));
12530
      printf (_("   Header flags: 0x%08x\n"),
12531
              (unsigned)byte_get ((unsigned char *)pnote->descdata + 24, 4));
12532
      printf (_("   Image id    : %s\n"), pnote->descdata + 32);
12533
      break;
12534
#endif
12535
    case NT_VMS_IMGNAM:
12536
      printf (_("    Image name: %s\n"), pnote->descdata);
12537
      break;
12538
    case NT_VMS_GSTNAM:
12539
      printf (_("    Global symbol table name: %s\n"), pnote->descdata);
12540
      break;
12541
    case NT_VMS_IMGID:
12542
      printf (_("    Image id: %s\n"), pnote->descdata);
12543
      break;
12544
    case NT_VMS_LINKID:
12545
      printf (_("    Linker id: %s\n"), pnote->descdata);
12546
      break;
12547
    default:
12548
      break;
12549
    }
12550
  return 1;
12551
}
12552
 
12553
/* Note that by the ELF standard, the name field is already null byte
12554
   terminated, and namesz includes the terminating null byte.
12555
   I.E. the value of namesz for the name "FSF" is 4.
12556
 
12557
   If the value of namesz is zero, there is no name present.  */
12558
static int
12559
process_note (Elf_Internal_Note * pnote)
12560
{
12561
  const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
12562
  const char * nt;
12563
 
12564
  if (pnote->namesz == 0)
12565
    /* If there is no note name, then use the default set of
12566
       note type strings.  */
12567
    nt = get_note_type (pnote->type);
12568
 
12569
  else if (const_strneq (pnote->namedata, "GNU"))
12570
    /* GNU-specific object file notes.  */
12571
    nt = get_gnu_elf_note_type (pnote->type);
12572
 
12573
  else if (const_strneq (pnote->namedata, "NetBSD-CORE"))
12574
    /* NetBSD-specific core file notes.  */
12575
    nt = get_netbsd_elfcore_note_type (pnote->type);
12576
 
12577
  else if (strneq (pnote->namedata, "SPU/", 4))
12578
    {
12579
      /* SPU-specific core file notes.  */
12580
      nt = pnote->namedata + 4;
12581
      name = "SPU";
12582
    }
12583
 
12584
  else if (const_strneq (pnote->namedata, "IPF/VMS"))
12585
    /* VMS/ia64-specific file notes.  */
12586
    nt = get_ia64_vms_note_type (pnote->type);
12587
 
12588
  else if (const_strneq (pnote->namedata, "stapsdt"))
12589
    nt = get_stapsdt_note_type (pnote->type);
12590
 
12591
  else
12592
    /* Don't recognize this note name; just use the default set of
12593
       note type strings.  */
12594
    nt = get_note_type (pnote->type);
12595
 
12596
  printf ("  %-20s 0x%08lx\t%s\n", name, pnote->descsz, nt);
12597
 
12598
  if (const_strneq (pnote->namedata, "IPF/VMS"))
12599
    return print_ia64_vms_note (pnote);
12600
  else if (const_strneq (pnote->namedata, "GNU"))
12601
    return print_gnu_note (pnote);
12602
  else if (const_strneq (pnote->namedata, "stapsdt"))
12603
    return print_stapsdt_note (pnote);
12604
  else
12605
    return 1;
12606
}
12607
 
12608
 
12609
static int
12610
process_corefile_note_segment (FILE * file, bfd_vma offset, bfd_vma length)
12611
{
12612
  Elf_External_Note * pnotes;
12613
  Elf_External_Note * external;
12614
  int res = 1;
12615
 
12616
  if (length <= 0)
12617
    return 0;
12618
 
12619
  pnotes = (Elf_External_Note *) get_data (NULL, file, offset, 1, length,
12620
                                           _("notes"));
12621
  if (pnotes == NULL)
12622
    return 0;
12623
 
12624
  external = pnotes;
12625
 
12626
  printf (_("\nNotes at offset 0x%08lx with length 0x%08lx:\n"),
12627
          (unsigned long) offset, (unsigned long) length);
12628
  printf (_("  %-20s %10s\tDescription\n"), _("Owner"), _("Data size"));
12629
 
12630
  while (external < (Elf_External_Note *) ((char *) pnotes + length))
12631
    {
12632
      Elf_External_Note * next;
12633
      Elf_Internal_Note inote;
12634
      char * temp = NULL;
12635
 
12636
      if (!is_ia64_vms ())
12637
        {
12638
          inote.type     = BYTE_GET (external->type);
12639
          inote.namesz   = BYTE_GET (external->namesz);
12640
          inote.namedata = external->name;
12641
          inote.descsz   = BYTE_GET (external->descsz);
12642
          inote.descdata = inote.namedata + align_power (inote.namesz, 2);
12643
          inote.descpos  = offset + (inote.descdata - (char *) pnotes);
12644
 
12645
          next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
12646
        }
12647
      else
12648
        {
12649
          Elf64_External_VMS_Note *vms_external;
12650
 
12651
          vms_external = (Elf64_External_VMS_Note *)external;
12652
          inote.type     = BYTE_GET (vms_external->type);
12653
          inote.namesz   = BYTE_GET (vms_external->namesz);
12654
          inote.namedata = vms_external->name;
12655
          inote.descsz   = BYTE_GET (vms_external->descsz);
12656
          inote.descdata = inote.namedata + align_power (inote.namesz, 3);
12657
          inote.descpos  = offset + (inote.descdata - (char *) pnotes);
12658
 
12659
          next = (Elf_External_Note *)
12660
            (inote.descdata + align_power (inote.descsz, 3));
12661
        }
12662
 
12663
      if (   ((char *) next > ((char *) pnotes) + length)
12664
          || ((char *) next <  (char *) pnotes))
12665
        {
12666
          warn (_("corrupt note found at offset %lx into core notes\n"),
12667
                (unsigned long) ((char *) external - (char *) pnotes));
12668
          warn (_(" type: %lx, namesize: %08lx, descsize: %08lx\n"),
12669
                inote.type, inote.namesz, inote.descsz);
12670
          break;
12671
        }
12672
 
12673
      external = next;
12674
 
12675
      /* Prevent out-of-bounds indexing.  */
12676
      if (inote.namedata + inote.namesz >= (char *) pnotes + length
12677
          || inote.namedata + inote.namesz < inote.namedata)
12678
        {
12679
          warn (_("corrupt note found at offset %lx into core notes\n"),
12680
                (unsigned long) ((char *) external - (char *) pnotes));
12681
          warn (_(" type: %lx, namesize: %08lx, descsize: %08lx\n"),
12682
                inote.type, inote.namesz, inote.descsz);
12683
          break;
12684
        }
12685
 
12686
      /* Verify that name is null terminated.  It appears that at least
12687
         one version of Linux (RedHat 6.0) generates corefiles that don't
12688
         comply with the ELF spec by failing to include the null byte in
12689
         namesz.  */
12690
      if (inote.namedata[inote.namesz] != '\0')
12691
        {
12692
          temp = (char *) malloc (inote.namesz + 1);
12693
 
12694
          if (temp == NULL)
12695
            {
12696
              error (_("Out of memory\n"));
12697
              res = 0;
12698
              break;
12699
            }
12700
 
12701
          strncpy (temp, inote.namedata, inote.namesz);
12702
          temp[inote.namesz] = 0;
12703
 
12704
          /* warn (_("'%s' NOTE name not properly null terminated\n"), temp);  */
12705
          inote.namedata = temp;
12706
        }
12707
 
12708
      res &= process_note (& inote);
12709
 
12710
      if (temp != NULL)
12711
        {
12712
          free (temp);
12713
          temp = NULL;
12714
        }
12715
    }
12716
 
12717
  free (pnotes);
12718
 
12719
  return res;
12720
}
12721
 
12722
static int
12723
process_corefile_note_segments (FILE * file)
12724
{
12725
  Elf_Internal_Phdr * segment;
12726
  unsigned int i;
12727
  int res = 1;
12728
 
12729
  if (! get_program_headers (file))
12730
      return 0;
12731
 
12732
  for (i = 0, segment = program_headers;
12733
       i < elf_header.e_phnum;
12734
       i++, segment++)
12735
    {
12736
      if (segment->p_type == PT_NOTE)
12737
        res &= process_corefile_note_segment (file,
12738
                                              (bfd_vma) segment->p_offset,
12739
                                              (bfd_vma) segment->p_filesz);
12740
    }
12741
 
12742
  return res;
12743
}
12744
 
12745
static int
12746
process_note_sections (FILE * file)
12747
{
12748
  Elf_Internal_Shdr * section;
12749
  unsigned long i;
12750
  int res = 1;
12751
 
12752
  for (i = 0, section = section_headers;
12753
       i < elf_header.e_shnum;
12754
       i++, section++)
12755
    if (section->sh_type == SHT_NOTE)
12756
      res &= process_corefile_note_segment (file,
12757
                                            (bfd_vma) section->sh_offset,
12758
                                            (bfd_vma) section->sh_size);
12759
 
12760
  return res;
12761
}
12762
 
12763
static int
12764
process_notes (FILE * file)
12765
{
12766
  /* If we have not been asked to display the notes then do nothing.  */
12767
  if (! do_notes)
12768
    return 1;
12769
 
12770
  if (elf_header.e_type != ET_CORE)
12771
    return process_note_sections (file);
12772
 
12773
  /* No program headers means no NOTE segment.  */
12774
  if (elf_header.e_phnum > 0)
12775
    return process_corefile_note_segments (file);
12776
 
12777
  printf (_("No note segments present in the core file.\n"));
12778
  return 1;
12779
}
12780
 
12781
static int
12782
process_arch_specific (FILE * file)
12783
{
12784
  if (! do_arch)
12785
    return 1;
12786
 
12787
  switch (elf_header.e_machine)
12788
    {
12789
    case EM_ARM:
12790
      return process_arm_specific (file);
12791
    case EM_MIPS:
12792
    case EM_MIPS_RS3_LE:
12793
      return process_mips_specific (file);
12794
      break;
12795
    case EM_PPC:
12796
      return process_power_specific (file);
12797
      break;
12798
    case EM_TI_C6000:
12799
      return process_tic6x_specific (file);
12800
      break;
12801
    default:
12802
      break;
12803
    }
12804
  return 1;
12805
}
12806
 
12807
static int
12808
get_file_header (FILE * file)
12809
{
12810
  /* Read in the identity array.  */
12811
  if (fread (elf_header.e_ident, EI_NIDENT, 1, file) != 1)
12812
    return 0;
12813
 
12814
  /* Determine how to read the rest of the header.  */
12815
  switch (elf_header.e_ident[EI_DATA])
12816
    {
12817
    default: /* fall through */
12818
    case ELFDATANONE: /* fall through */
12819
    case ELFDATA2LSB:
12820
      byte_get = byte_get_little_endian;
12821
      byte_put = byte_put_little_endian;
12822
      break;
12823
    case ELFDATA2MSB:
12824
      byte_get = byte_get_big_endian;
12825
      byte_put = byte_put_big_endian;
12826
      break;
12827
    }
12828
 
12829
  /* For now we only support 32 bit and 64 bit ELF files.  */
12830
  is_32bit_elf = (elf_header.e_ident[EI_CLASS] != ELFCLASS64);
12831
 
12832
  /* Read in the rest of the header.  */
12833
  if (is_32bit_elf)
12834
    {
12835
      Elf32_External_Ehdr ehdr32;
12836
 
12837
      if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, file) != 1)
12838
        return 0;
12839
 
12840
      elf_header.e_type      = BYTE_GET (ehdr32.e_type);
12841
      elf_header.e_machine   = BYTE_GET (ehdr32.e_machine);
12842
      elf_header.e_version   = BYTE_GET (ehdr32.e_version);
12843
      elf_header.e_entry     = BYTE_GET (ehdr32.e_entry);
12844
      elf_header.e_phoff     = BYTE_GET (ehdr32.e_phoff);
12845
      elf_header.e_shoff     = BYTE_GET (ehdr32.e_shoff);
12846
      elf_header.e_flags     = BYTE_GET (ehdr32.e_flags);
12847
      elf_header.e_ehsize    = BYTE_GET (ehdr32.e_ehsize);
12848
      elf_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
12849
      elf_header.e_phnum     = BYTE_GET (ehdr32.e_phnum);
12850
      elf_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
12851
      elf_header.e_shnum     = BYTE_GET (ehdr32.e_shnum);
12852
      elf_header.e_shstrndx  = BYTE_GET (ehdr32.e_shstrndx);
12853
    }
12854
  else
12855
    {
12856
      Elf64_External_Ehdr ehdr64;
12857
 
12858
      /* If we have been compiled with sizeof (bfd_vma) == 4, then
12859
         we will not be able to cope with the 64bit data found in
12860
         64 ELF files.  Detect this now and abort before we start
12861
         overwriting things.  */
12862
      if (sizeof (bfd_vma) < 8)
12863
        {
12864
          error (_("This instance of readelf has been built without support for a\n\
12865
64 bit data type and so it cannot read 64 bit ELF files.\n"));
12866
          return 0;
12867
        }
12868
 
12869
      if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, file) != 1)
12870
        return 0;
12871
 
12872
      elf_header.e_type      = BYTE_GET (ehdr64.e_type);
12873
      elf_header.e_machine   = BYTE_GET (ehdr64.e_machine);
12874
      elf_header.e_version   = BYTE_GET (ehdr64.e_version);
12875
      elf_header.e_entry     = BYTE_GET (ehdr64.e_entry);
12876
      elf_header.e_phoff     = BYTE_GET (ehdr64.e_phoff);
12877
      elf_header.e_shoff     = BYTE_GET (ehdr64.e_shoff);
12878
      elf_header.e_flags     = BYTE_GET (ehdr64.e_flags);
12879
      elf_header.e_ehsize    = BYTE_GET (ehdr64.e_ehsize);
12880
      elf_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
12881
      elf_header.e_phnum     = BYTE_GET (ehdr64.e_phnum);
12882
      elf_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
12883
      elf_header.e_shnum     = BYTE_GET (ehdr64.e_shnum);
12884
      elf_header.e_shstrndx  = BYTE_GET (ehdr64.e_shstrndx);
12885
    }
12886
 
12887
  if (elf_header.e_shoff)
12888
    {
12889
      /* There may be some extensions in the first section header.  Don't
12890
         bomb if we can't read it.  */
12891
      if (is_32bit_elf)
12892
        get_32bit_section_headers (file, 1);
12893
      else
12894
        get_64bit_section_headers (file, 1);
12895
    }
12896
 
12897
  return 1;
12898
}
12899
 
12900
/* Process one ELF object file according to the command line options.
12901
   This file may actually be stored in an archive.  The file is
12902
   positioned at the start of the ELF object.  */
12903
 
12904
static int
12905
process_object (char * file_name, FILE * file)
12906
{
12907
  unsigned int i;
12908
 
12909
  if (! get_file_header (file))
12910
    {
12911
      error (_("%s: Failed to read file header\n"), file_name);
12912
      return 1;
12913
    }
12914
 
12915
  /* Initialise per file variables.  */
12916
  for (i = ARRAY_SIZE (version_info); i--;)
12917
    version_info[i] = 0;
12918
 
12919
  for (i = ARRAY_SIZE (dynamic_info); i--;)
12920
    dynamic_info[i] = 0;
12921
  dynamic_info_DT_GNU_HASH = 0;
12922
 
12923
  /* Process the file.  */
12924
  if (show_name)
12925
    printf (_("\nFile: %s\n"), file_name);
12926
 
12927
  /* Initialise the dump_sects array from the cmdline_dump_sects array.
12928
     Note we do this even if cmdline_dump_sects is empty because we
12929
     must make sure that the dump_sets array is zeroed out before each
12930
     object file is processed.  */
12931
  if (num_dump_sects > num_cmdline_dump_sects)
12932
    memset (dump_sects, 0, num_dump_sects * sizeof (* dump_sects));
12933
 
12934
  if (num_cmdline_dump_sects > 0)
12935
    {
12936
      if (num_dump_sects == 0)
12937
        /* A sneaky way of allocating the dump_sects array.  */
12938
        request_dump_bynumber (num_cmdline_dump_sects, 0);
12939
 
12940
      assert (num_dump_sects >= num_cmdline_dump_sects);
12941
      memcpy (dump_sects, cmdline_dump_sects,
12942
              num_cmdline_dump_sects * sizeof (* dump_sects));
12943
    }
12944
 
12945
  if (! process_file_header ())
12946
    return 1;
12947
 
12948
  if (! process_section_headers (file))
12949
    {
12950
      /* Without loaded section headers we cannot process lots of
12951
         things.  */
12952
      do_unwind = do_version = do_dump = do_arch = 0;
12953
 
12954
      if (! do_using_dynamic)
12955
        do_syms = do_dyn_syms = do_reloc = 0;
12956
    }
12957
 
12958
  if (! process_section_groups (file))
12959
    {
12960
      /* Without loaded section groups we cannot process unwind.  */
12961
      do_unwind = 0;
12962
    }
12963
 
12964
  if (process_program_headers (file))
12965
    process_dynamic_section (file);
12966
 
12967
  process_relocs (file);
12968
 
12969
  process_unwind (file);
12970
 
12971
  process_symbol_table (file);
12972
 
12973
  process_syminfo (file);
12974
 
12975
  process_version_sections (file);
12976
 
12977
  process_section_contents (file);
12978
 
12979
  process_notes (file);
12980
 
12981
  process_gnu_liblist (file);
12982
 
12983
  process_arch_specific (file);
12984
 
12985
  if (program_headers)
12986
    {
12987
      free (program_headers);
12988
      program_headers = NULL;
12989
    }
12990
 
12991
  if (section_headers)
12992
    {
12993
      free (section_headers);
12994
      section_headers = NULL;
12995
    }
12996
 
12997
  if (string_table)
12998
    {
12999
      free (string_table);
13000
      string_table = NULL;
13001
      string_table_length = 0;
13002
    }
13003
 
13004
  if (dynamic_strings)
13005
    {
13006
      free (dynamic_strings);
13007
      dynamic_strings = NULL;
13008
      dynamic_strings_length = 0;
13009
    }
13010
 
13011
  if (dynamic_symbols)
13012
    {
13013
      free (dynamic_symbols);
13014
      dynamic_symbols = NULL;
13015
      num_dynamic_syms = 0;
13016
    }
13017
 
13018
  if (dynamic_syminfo)
13019
    {
13020
      free (dynamic_syminfo);
13021
      dynamic_syminfo = NULL;
13022
    }
13023
 
13024
  if (dynamic_section)
13025
    {
13026
      free (dynamic_section);
13027
      dynamic_section = NULL;
13028
    }
13029
 
13030
  if (section_headers_groups)
13031
    {
13032
      free (section_headers_groups);
13033
      section_headers_groups = NULL;
13034
    }
13035
 
13036
  if (section_groups)
13037
    {
13038
      struct group_list * g;
13039
      struct group_list * next;
13040
 
13041
      for (i = 0; i < group_count; i++)
13042
        {
13043
          for (g = section_groups [i].root; g != NULL; g = next)
13044
            {
13045
              next = g->next;
13046
              free (g);
13047
            }
13048
        }
13049
 
13050
      free (section_groups);
13051
      section_groups = NULL;
13052
    }
13053
 
13054
  free_debug_memory ();
13055
 
13056
  return 0;
13057
}
13058
 
13059
/* Process an ELF archive.
13060
   On entry the file is positioned just after the ARMAG string.  */
13061
 
13062
static int
13063
process_archive (char * file_name, FILE * file, bfd_boolean is_thin_archive)
13064
{
13065
  struct archive_info arch;
13066
  struct archive_info nested_arch;
13067
  size_t got;
13068
  int ret;
13069
 
13070
  show_name = 1;
13071
 
13072
  /* The ARCH structure is used to hold information about this archive.  */
13073
  arch.file_name = NULL;
13074
  arch.file = NULL;
13075
  arch.index_array = NULL;
13076
  arch.sym_table = NULL;
13077
  arch.longnames = NULL;
13078
 
13079
  /* The NESTED_ARCH structure is used as a single-item cache of information
13080
     about a nested archive (when members of a thin archive reside within
13081
     another regular archive file).  */
13082
  nested_arch.file_name = NULL;
13083
  nested_arch.file = NULL;
13084
  nested_arch.index_array = NULL;
13085
  nested_arch.sym_table = NULL;
13086
  nested_arch.longnames = NULL;
13087
 
13088
  if (setup_archive (&arch, file_name, file, is_thin_archive, do_archive_index) != 0)
13089
    {
13090
      ret = 1;
13091
      goto out;
13092
    }
13093
 
13094
  if (do_archive_index)
13095
    {
13096
      if (arch.sym_table == NULL)
13097
        error (_("%s: unable to dump the index as none was found\n"), file_name);
13098
      else
13099
        {
13100
          unsigned int i, l;
13101
          unsigned long current_pos;
13102
 
13103
          printf (_("Index of archive %s: (%ld entries, 0x%lx bytes in the symbol table)\n"),
13104
                  file_name, arch.index_num, arch.sym_size);
13105
          current_pos = ftell (file);
13106
 
13107
          for (i = l = 0; i < arch.index_num; i++)
13108
            {
13109
              if ((i == 0) || ((i > 0) && (arch.index_array[i] != arch.index_array[i - 1])))
13110
                {
13111
                  char * member_name;
13112
 
13113
                  member_name = get_archive_member_name_at (&arch, arch.index_array[i], &nested_arch);
13114
 
13115
                  if (member_name != NULL)
13116
                    {
13117
                      char * qualified_name = make_qualified_name (&arch, &nested_arch, member_name);
13118
 
13119
                      if (qualified_name != NULL)
13120
                        {
13121
                          printf (_("Binary %s contains:\n"), qualified_name);
13122
                          free (qualified_name);
13123
                        }
13124
                    }
13125
                }
13126
 
13127
              if (l >= arch.sym_size)
13128
                {
13129
                  error (_("%s: end of the symbol table reached before the end of the index\n"),
13130
                         file_name);
13131
                  break;
13132
                }
13133
              printf ("\t%s\n", arch.sym_table + l);
13134
              l += strlen (arch.sym_table + l) + 1;
13135
            }
13136
 
13137
          if (l & 01)
13138
            ++l;
13139
          if (l < arch.sym_size)
13140
            error (_("%s: symbols remain in the index symbol table, but without corresponding entries in the index table\n"),
13141
                   file_name);
13142
 
13143
          if (fseek (file, current_pos, SEEK_SET) != 0)
13144
            {
13145
              error (_("%s: failed to seek back to start of object files in the archive\n"), file_name);
13146
              ret = 1;
13147
              goto out;
13148
            }
13149
        }
13150
 
13151
      if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
13152
          && !do_segments && !do_header && !do_dump && !do_version
13153
          && !do_histogram && !do_debugging && !do_arch && !do_notes
13154
          && !do_section_groups && !do_dyn_syms)
13155
        {
13156
          ret = 0; /* Archive index only.  */
13157
          goto out;
13158
        }
13159
    }
13160
 
13161
  ret = 0;
13162
 
13163
  while (1)
13164
    {
13165
      char * name;
13166
      size_t namelen;
13167
      char * qualified_name;
13168
 
13169
      /* Read the next archive header.  */
13170
      if (fseek (file, arch.next_arhdr_offset, SEEK_SET) != 0)
13171
        {
13172
          error (_("%s: failed to seek to next archive header\n"), file_name);
13173
          return 1;
13174
        }
13175
      got = fread (&arch.arhdr, 1, sizeof arch.arhdr, file);
13176
      if (got != sizeof arch.arhdr)
13177
        {
13178
          if (got == 0)
13179
            break;
13180
          error (_("%s: failed to read archive header\n"), file_name);
13181
          ret = 1;
13182
          break;
13183
        }
13184
      if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
13185
        {
13186
          error (_("%s: did not find a valid archive header\n"), arch.file_name);
13187
          ret = 1;
13188
          break;
13189
        }
13190
 
13191
      arch.next_arhdr_offset += sizeof arch.arhdr;
13192
 
13193
      archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
13194
      if (archive_file_size & 01)
13195
        ++archive_file_size;
13196
 
13197
      name = get_archive_member_name (&arch, &nested_arch);
13198
      if (name == NULL)
13199
        {
13200
          error (_("%s: bad archive file name\n"), file_name);
13201
          ret = 1;
13202
          break;
13203
        }
13204
      namelen = strlen (name);
13205
 
13206
      qualified_name = make_qualified_name (&arch, &nested_arch, name);
13207
      if (qualified_name == NULL)
13208
        {
13209
          error (_("%s: bad archive file name\n"), file_name);
13210
          ret = 1;
13211
          break;
13212
        }
13213
 
13214
      if (is_thin_archive && arch.nested_member_origin == 0)
13215
        {
13216
          /* This is a proxy for an external member of a thin archive.  */
13217
          FILE * member_file;
13218
          char * member_file_name = adjust_relative_path (file_name, name, namelen);
13219
          if (member_file_name == NULL)
13220
            {
13221
              ret = 1;
13222
              break;
13223
            }
13224
 
13225
          member_file = fopen (member_file_name, "rb");
13226
          if (member_file == NULL)
13227
            {
13228
              error (_("Input file '%s' is not readable.\n"), member_file_name);
13229
              free (member_file_name);
13230
              ret = 1;
13231
              break;
13232
            }
13233
 
13234
          archive_file_offset = arch.nested_member_origin;
13235
 
13236
          ret |= process_object (qualified_name, member_file);
13237
 
13238
          fclose (member_file);
13239
          free (member_file_name);
13240
        }
13241
      else if (is_thin_archive)
13242
        {
13243
          /* This is a proxy for a member of a nested archive.  */
13244
          archive_file_offset = arch.nested_member_origin + sizeof arch.arhdr;
13245
 
13246
          /* The nested archive file will have been opened and setup by
13247
             get_archive_member_name.  */
13248
          if (fseek (nested_arch.file, archive_file_offset, SEEK_SET) != 0)
13249
            {
13250
              error (_("%s: failed to seek to archive member.\n"), nested_arch.file_name);
13251
              ret = 1;
13252
              break;
13253
            }
13254
 
13255
          ret |= process_object (qualified_name, nested_arch.file);
13256
        }
13257
      else
13258
        {
13259
          archive_file_offset = arch.next_arhdr_offset;
13260
          arch.next_arhdr_offset += archive_file_size;
13261
 
13262
          ret |= process_object (qualified_name, file);
13263
        }
13264
 
13265
      if (dump_sects != NULL)
13266
        {
13267
          free (dump_sects);
13268
          dump_sects = NULL;
13269
          num_dump_sects = 0;
13270
        }
13271
 
13272
      free (qualified_name);
13273
    }
13274
 
13275
 out:
13276
  if (nested_arch.file != NULL)
13277
    fclose (nested_arch.file);
13278
  release_archive (&nested_arch);
13279
  release_archive (&arch);
13280
 
13281
  return ret;
13282
}
13283
 
13284
static int
13285
process_file (char * file_name)
13286
{
13287
  FILE * file;
13288
  struct stat statbuf;
13289
  char armag[SARMAG];
13290
  int ret;
13291
 
13292
  if (stat (file_name, &statbuf) < 0)
13293
    {
13294
      if (errno == ENOENT)
13295
        error (_("'%s': No such file\n"), file_name);
13296
      else
13297
        error (_("Could not locate '%s'.  System error message: %s\n"),
13298
               file_name, strerror (errno));
13299
      return 1;
13300
    }
13301
 
13302
  if (! S_ISREG (statbuf.st_mode))
13303
    {
13304
      error (_("'%s' is not an ordinary file\n"), file_name);
13305
      return 1;
13306
    }
13307
 
13308
  file = fopen (file_name, "rb");
13309
  if (file == NULL)
13310
    {
13311
      error (_("Input file '%s' is not readable.\n"), file_name);
13312
      return 1;
13313
    }
13314
 
13315
  if (fread (armag, SARMAG, 1, file) != 1)
13316
    {
13317
      error (_("%s: Failed to read file's magic number\n"), file_name);
13318
      fclose (file);
13319
      return 1;
13320
    }
13321
 
13322
  if (memcmp (armag, ARMAG, SARMAG) == 0)
13323
    ret = process_archive (file_name, file, FALSE);
13324
  else if (memcmp (armag, ARMAGT, SARMAG) == 0)
13325
    ret = process_archive (file_name, file, TRUE);
13326
  else
13327
    {
13328
      if (do_archive_index)
13329
        error (_("File %s is not an archive so its index cannot be displayed.\n"),
13330
               file_name);
13331
 
13332
      rewind (file);
13333
      archive_file_size = archive_file_offset = 0;
13334
      ret = process_object (file_name, file);
13335
    }
13336
 
13337
  fclose (file);
13338
 
13339
  return ret;
13340
}
13341
 
13342
#ifdef SUPPORT_DISASSEMBLY
13343
/* Needed by the i386 disassembler.  For extra credit, someone could
13344
   fix this so that we insert symbolic addresses here, esp for GOT/PLT
13345
   symbols.  */
13346
 
13347
void
13348
print_address (unsigned int addr, FILE * outfile)
13349
{
13350
  fprintf (outfile,"0x%8.8x", addr);
13351
}
13352
 
13353
/* Needed by the i386 disassembler.  */
13354
void
13355
db_task_printsym (unsigned int addr)
13356
{
13357
  print_address (addr, stderr);
13358
}
13359
#endif
13360
 
13361
int
13362
main (int argc, char ** argv)
13363
{
13364
  int err;
13365
 
13366
#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
13367
  setlocale (LC_MESSAGES, "");
13368
#endif
13369
#if defined (HAVE_SETLOCALE)
13370
  setlocale (LC_CTYPE, "");
13371
#endif
13372
  bindtextdomain (PACKAGE, LOCALEDIR);
13373
  textdomain (PACKAGE);
13374
 
13375
  expandargv (&argc, &argv);
13376
 
13377
  parse_args (argc, argv);
13378
 
13379
  if (num_dump_sects > 0)
13380
    {
13381
      /* Make a copy of the dump_sects array.  */
13382
      cmdline_dump_sects = (dump_type *)
13383
          malloc (num_dump_sects * sizeof (* dump_sects));
13384
      if (cmdline_dump_sects == NULL)
13385
        error (_("Out of memory allocating dump request table.\n"));
13386
      else
13387
        {
13388
          memcpy (cmdline_dump_sects, dump_sects,
13389
                  num_dump_sects * sizeof (* dump_sects));
13390
          num_cmdline_dump_sects = num_dump_sects;
13391
        }
13392
    }
13393
 
13394
  if (optind < (argc - 1))
13395
    show_name = 1;
13396
 
13397
  err = 0;
13398
  while (optind < argc)
13399
    err |= process_file (argv[optind++]);
13400
 
13401
  if (dump_sects != NULL)
13402
    free (dump_sects);
13403
  if (cmdline_dump_sects != NULL)
13404
    free (cmdline_dump_sects);
13405
 
13406
  return err;
13407
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.