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 161

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

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

powered by: WebSVN 2.1.0

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