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 163

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

powered by: WebSVN 2.1.0

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