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