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 148

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

powered by: WebSVN 2.1.0

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