]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - binutils/readelf.c
C99 opcodes configury
[thirdparty/binutils-gdb.git] / binutils / readelf.c
CommitLineData
252b5132 1/* readelf.c -- display contents of an ELF format file
250d07de 2 Copyright (C) 1998-2021 Free Software Foundation, Inc.
252b5132
RH
3
4 Originally developed by Eric Youngdale <eric@andante.jic.com>
12ab83a9 5 Modifications by Nick Clifton <nickc@redhat.com>
252b5132
RH
6
7 This file is part of GNU Binutils.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
32866df7 11 the Free Software Foundation; either version 3 of the License, or
252b5132
RH
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
b43b5d5f
NC
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
22 02110-1301, USA. */
252b5132 23\f
9eb20dd8 24/* The difference between readelf and objdump:
252b5132 25
74013231 26 Both programs are capable of displaying the contents of ELF format files,
9eb20dd8 27 so why does the binutils project have two file dumpers ?
0de14b54 28
9eb20dd8
NC
29 The reason is that objdump sees an ELF file through a BFD filter of the
30 world; if BFD has a bug where, say, it disagrees about a machine constant
31 in e_flags, then the odds are good that it will remain internally
32 consistent. The linker sees it the BFD way, objdump sees it the BFD way,
33 GAS sees it the BFD way. There was need for a tool to go find out what
34 the file actually says.
35
36 This is why the readelf program does not link against the BFD library - it
37 exists as an independent program to help verify the correct working of BFD.
38
39 There is also the case that readelf can provide more information about an
40 ELF file than is provided by objdump. In particular it can display DWARF
41 debugging information which (at the moment) objdump cannot. */
42\f
3db64b00 43#include "sysdep.h"
252b5132 44#include <assert.h>
252b5132 45#include <time.h>
1b315056 46#include <zlib.h>
3bfcb652 47#ifdef HAVE_WCHAR_H
7bfd842d 48#include <wchar.h>
3bfcb652 49#endif
252b5132 50
a952a375 51#if __GNUC__ >= 2
19936277 52/* Define BFD64 here, even if our default architecture is 32 bit ELF
a952a375 53 as this will allow us to read in and parse 64bit and 32bit ELF files.
b34976b6 54 Only do this if we believe that the compiler can support a 64 bit
a952a375 55 data type. For now we only rely on GCC being able to do this. */
19936277 56#define BFD64
a952a375
NC
57#endif
58
3db64b00
AM
59#include "bfd.h"
60#include "bucomm.h"
3284fe0c 61#include "elfcomm.h"
19e6b90e 62#include "dwarf.h"
7d9813f1 63#include "ctf-api.h"
79bc120c 64#include "demangle.h"
252b5132
RH
65
66#include "elf/common.h"
67#include "elf/external.h"
68#include "elf/internal.h"
252b5132 69
4b78141a
NC
70
71/* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
72 we can obtain the H8 reloc numbers. We need these for the
73 get_reloc_size() function. We include h8.h again after defining
74 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
75
76#include "elf/h8.h"
77#undef _ELF_H8_H
78
79/* Undo the effects of #including reloc-macros.h. */
80
81#undef START_RELOC_NUMBERS
82#undef RELOC_NUMBER
83#undef FAKE_RELOC
84#undef EMPTY_RELOC
85#undef END_RELOC_NUMBERS
86#undef _RELOC_MACROS_H
87
252b5132
RH
88/* The following headers use the elf/reloc-macros.h file to
89 automatically generate relocation recognition functions
90 such as elf_mips_reloc_type() */
91
92#define RELOC_MACROS_GEN_FUNC
93
a06ea964 94#include "elf/aarch64.h"
252b5132 95#include "elf/alpha.h"
3b16e843 96#include "elf/arc.h"
252b5132 97#include "elf/arm.h"
3b16e843 98#include "elf/avr.h"
1d65ded4 99#include "elf/bfin.h"
60bca95a 100#include "elf/cr16.h"
3b16e843 101#include "elf/cris.h"
1c0d3aa6 102#include "elf/crx.h"
b8891f8d 103#include "elf/csky.h"
252b5132
RH
104#include "elf/d10v.h"
105#include "elf/d30v.h"
d172d4ba 106#include "elf/dlx.h"
aca4efc7 107#include "elf/bpf.h"
cfb8c092 108#include "elf/epiphany.h"
252b5132 109#include "elf/fr30.h"
5c70f934 110#include "elf/frv.h"
3f8107ab 111#include "elf/ft32.h"
3b16e843
NC
112#include "elf/h8.h"
113#include "elf/hppa.h"
114#include "elf/i386.h"
f954747f
AM
115#include "elf/i370.h"
116#include "elf/i860.h"
117#include "elf/i960.h"
3b16e843 118#include "elf/ia64.h"
1e4cf259 119#include "elf/ip2k.h"
84e94c90 120#include "elf/lm32.h"
1c0d3aa6 121#include "elf/iq2000.h"
49f58d10 122#include "elf/m32c.h"
3b16e843
NC
123#include "elf/m32r.h"
124#include "elf/m68k.h"
75751cd9 125#include "elf/m68hc11.h"
7b4ae824 126#include "elf/s12z.h"
252b5132 127#include "elf/mcore.h"
15ab5209 128#include "elf/mep.h"
a3c62988 129#include "elf/metag.h"
7ba29e2a 130#include "elf/microblaze.h"
3b16e843 131#include "elf/mips.h"
3c3bdf30 132#include "elf/mmix.h"
3b16e843
NC
133#include "elf/mn10200.h"
134#include "elf/mn10300.h"
5506d11a 135#include "elf/moxie.h"
4970f871 136#include "elf/mt.h"
2469cfa2 137#include "elf/msp430.h"
35c08157 138#include "elf/nds32.h"
fe944acf 139#include "elf/nfp.h"
13761a11 140#include "elf/nios2.h"
73589c9d 141#include "elf/or1k.h"
7d466069 142#include "elf/pj.h"
3b16e843 143#include "elf/ppc.h"
c833c019 144#include "elf/ppc64.h"
2b100bb5 145#include "elf/pru.h"
03336641 146#include "elf/riscv.h"
99c513f6 147#include "elf/rl78.h"
c7927a3c 148#include "elf/rx.h"
a85d7ed0 149#include "elf/s390.h"
1c0d3aa6 150#include "elf/score.h"
3b16e843
NC
151#include "elf/sh.h"
152#include "elf/sparc.h"
e9f53129 153#include "elf/spu.h"
40b36596 154#include "elf/tic6x.h"
aa137e4d
NC
155#include "elf/tilegx.h"
156#include "elf/tilepro.h"
3b16e843 157#include "elf/v850.h"
179d3252 158#include "elf/vax.h"
619ed720 159#include "elf/visium.h"
f96bd6c2 160#include "elf/wasm32.h"
3b16e843 161#include "elf/x86-64.h"
c29aca4a 162#include "elf/xc16x.h"
f6c1a2d5 163#include "elf/xgate.h"
93fbbb04 164#include "elf/xstormy16.h"
88da6820 165#include "elf/xtensa.h"
6655dba2 166#include "elf/z80.h"
252b5132 167
252b5132 168#include "getopt.h"
566b0d53 169#include "libiberty.h"
09c11c86 170#include "safe-ctype.h"
2cf0635d 171#include "filenames.h"
252b5132 172
15b42fb0
AM
173#ifndef offsetof
174#define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
175#endif
176
6a40cf0c
NC
177typedef struct elf_section_list
178{
dda8d76d
NC
179 Elf_Internal_Shdr * hdr;
180 struct elf_section_list * next;
6a40cf0c
NC
181} elf_section_list;
182
dda8d76d
NC
183/* Flag bits indicating particular types of dump. */
184#define HEX_DUMP (1 << 0) /* The -x command line switch. */
185#define DISASS_DUMP (1 << 1) /* The -i command line switch. */
186#define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
187#define STRING_DUMP (1 << 3) /* The -p command line switch. */
188#define RELOC_DUMP (1 << 4) /* The -R command line switch. */
d344b407 189#define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
dda8d76d
NC
190
191typedef unsigned char dump_type;
192
193/* A linked list of the section names for which dumps were requested. */
194struct dump_list_entry
195{
196 char * name;
197 dump_type type;
198 struct dump_list_entry * next;
199};
200
6431e409
AM
201/* A dynamic array of flags indicating for which sections a dump
202 has been requested via command line switches. */
1b513401
NC
203struct dump_data
204{
6431e409
AM
205 dump_type * dump_sects;
206 unsigned int num_dump_sects;
207};
208
209static struct dump_data cmdline;
210
211static struct dump_list_entry * dump_sects_byname;
212
2cf0635d 213char * program_name = "readelf";
dda8d76d 214
015dc7e1
AM
215static bool show_name = false;
216static bool do_dynamic = false;
217static bool do_syms = false;
218static bool do_dyn_syms = false;
219static bool do_lto_syms = false;
220static bool do_reloc = false;
221static bool do_sections = false;
222static bool do_section_groups = false;
223static bool do_section_details = false;
224static bool do_segments = false;
225static bool do_unwind = false;
226static bool do_using_dynamic = false;
227static bool do_header = false;
228static bool do_dump = false;
229static bool do_version = false;
230static bool do_histogram = false;
231static bool do_debugging = false;
232static bool do_ctf = false;
233static bool do_arch = false;
234static bool do_notes = false;
235static bool do_archive_index = false;
236static bool check_all = false;
237static bool is_32bit_elf = false;
238static bool decompress_dumps = false;
239static bool do_not_show_symbol_truncation = false;
240static bool do_demangle = false; /* Pretty print C++ symbol names. */
241static bool process_links = false;
79bc120c 242static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
252b5132 243
7d9813f1
NA
244static char *dump_ctf_parent_name;
245static char *dump_ctf_symtab_name;
246static char *dump_ctf_strtab_name;
247
e4b17d5c
L
248struct group_list
249{
dda8d76d
NC
250 struct group_list * next;
251 unsigned int section_index;
e4b17d5c
L
252};
253
254struct group
255{
dda8d76d
NC
256 struct group_list * root;
257 unsigned int group_index;
e4b17d5c
L
258};
259
978c4450
AM
260typedef struct filedata
261{
262 const char * file_name;
015dc7e1 263 bool is_separate;
978c4450
AM
264 FILE * handle;
265 bfd_size_type file_size;
266 Elf_Internal_Ehdr file_header;
267 Elf_Internal_Shdr * section_headers;
268 Elf_Internal_Phdr * program_headers;
269 char * string_table;
270 unsigned long string_table_length;
271 unsigned long archive_file_offset;
272 unsigned long archive_file_size;
273 unsigned long dynamic_addr;
274 bfd_size_type dynamic_size;
275 size_t dynamic_nent;
276 Elf_Internal_Dyn * dynamic_section;
8ac10c5b 277 Elf_Internal_Shdr * dynamic_strtab_section;
978c4450
AM
278 char * dynamic_strings;
279 unsigned long dynamic_strings_length;
8ac10c5b 280 Elf_Internal_Shdr * dynamic_symtab_section;
978c4450
AM
281 unsigned long num_dynamic_syms;
282 Elf_Internal_Sym * dynamic_symbols;
283 bfd_vma version_info[16];
284 unsigned int dynamic_syminfo_nent;
285 Elf_Internal_Syminfo * dynamic_syminfo;
286 unsigned long dynamic_syminfo_offset;
287 bfd_size_type nbuckets;
288 bfd_size_type nchains;
289 bfd_vma * buckets;
290 bfd_vma * chains;
291 bfd_size_type ngnubuckets;
292 bfd_size_type ngnuchains;
293 bfd_vma * gnubuckets;
294 bfd_vma * gnuchains;
295 bfd_vma * mipsxlat;
296 bfd_vma gnusymidx;
297 char program_interpreter[PATH_MAX];
298 bfd_vma dynamic_info[DT_ENCODING];
299 bfd_vma dynamic_info_DT_GNU_HASH;
300 bfd_vma dynamic_info_DT_MIPS_XHASH;
301 elf_section_list * symtab_shndx_list;
302 size_t group_count;
303 struct group * section_groups;
304 struct group ** section_headers_groups;
305 /* A dynamic array of flags indicating for which sections a dump of
306 some kind has been requested. It is reset on a per-object file
307 basis and then initialised from the cmdline_dump_sects array,
308 the results of interpreting the -w switch, and the
309 dump_sects_byname list. */
310 struct dump_data dump;
311} Filedata;
aef1f6d0 312
c256ffe7 313/* How to print a vma value. */
843dd992
NC
314typedef enum print_mode
315{
316 HEX,
317 DEC,
318 DEC_5,
319 UNSIGNED,
320 PREFIX_HEX,
321 FULL_HEX,
322 LONG_HEX
323}
324print_mode;
325
bb4d2ac2
L
326/* Versioned symbol info. */
327enum versioned_symbol_info
328{
329 symbol_undefined,
330 symbol_hidden,
331 symbol_public
332};
333
32ec8896 334static const char * get_symbol_version_string
015dc7e1 335 (Filedata *, bool, const char *, unsigned long, unsigned,
32ec8896 336 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
bb4d2ac2 337
9c19a809
NC
338#define UNKNOWN -1
339
b9e920ec
AM
340#define SECTION_NAME(X) \
341 (filedata->string_table + (X)->sh_name)
342
343#define SECTION_NAME_VALID(X) \
344 ((X) != NULL \
345 && filedata->string_table != NULL \
346 && (X)->sh_name < filedata->string_table_length)
347
348#define SECTION_NAME_PRINT(X) \
349 ((X) == NULL ? _("<none>") \
350 : filedata->string_table == NULL ? _("<no-strings>") \
351 : (X)->sh_name >= filedata->string_table_length ? _("<corrupt>") \
352 : filedata->string_table + (X)->sh_name)
252b5132 353
ee42cf8c 354#define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
252b5132 355
ba5cdace
NC
356#define GET_ELF_SYMBOLS(file, section, sym_count) \
357 (is_32bit_elf ? get_32bit_elf_symbols (file, section, sym_count) \
358 : get_64bit_elf_symbols (file, section, sym_count))
9ea033b2 359
10ca4b04
L
360#define VALID_SYMBOL_NAME(strtab, strtab_size, offset) \
361 (strtab != NULL && offset < strtab_size)
978c4450
AM
362#define VALID_DYNAMIC_NAME(filedata, offset) \
363 VALID_SYMBOL_NAME (filedata->dynamic_strings, \
364 filedata->dynamic_strings_length, offset)
d79b3d50
NC
365/* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
366 already been called and verified that the string exists. */
978c4450
AM
367#define GET_DYNAMIC_NAME(filedata, offset) \
368 (filedata->dynamic_strings + offset)
18bd398b 369
61865e30
NC
370#define REMOVE_ARCH_BITS(ADDR) \
371 do \
372 { \
dda8d76d 373 if (filedata->file_header.e_machine == EM_ARM) \
61865e30
NC
374 (ADDR) &= ~1; \
375 } \
376 while (0)
f16a9783
MS
377
378/* Get the correct GNU hash section name. */
978c4450
AM
379#define GNU_HASH_SECTION_NAME(filedata) \
380 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
d79b3d50 381\f
66cfc0fd
AM
382/* Print a BFD_VMA to an internal buffer, for use in error messages.
383 BFD_FMA_FMT can't be used in translated strings. */
384
385static const char *
386bfd_vmatoa (char *fmtch, bfd_vma value)
387{
388 /* bfd_vmatoa is used more then once in a printf call for output.
389 Cycle through an array of buffers. */
390 static int buf_pos = 0;
391 static struct bfd_vmatoa_buf
392 {
393 char place[64];
394 } buf[4];
395 char *ret;
396 char fmt[32];
397
398 ret = buf[buf_pos++].place;
399 buf_pos %= ARRAY_SIZE (buf);
400
401 sprintf (fmt, "%%%s%s", BFD_VMA_FMT, fmtch);
402 snprintf (ret, sizeof (buf[0].place), fmt, value);
403 return ret;
404}
405
dda8d76d
NC
406/* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
407 OFFSET + the offset of the current archive member, if we are examining an
408 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
409 allocate a buffer using malloc and fill that. In either case return the
410 pointer to the start of the retrieved data or NULL if something went wrong.
411 If something does go wrong and REASON is not NULL then emit an error
412 message using REASON as part of the context. */
59245841 413
c256ffe7 414static void *
dda8d76d
NC
415get_data (void * var,
416 Filedata * filedata,
417 unsigned long offset,
418 bfd_size_type size,
419 bfd_size_type nmemb,
420 const char * reason)
a6e9f9df 421{
2cf0635d 422 void * mvar;
57028622 423 bfd_size_type amt = size * nmemb;
a6e9f9df 424
c256ffe7 425 if (size == 0 || nmemb == 0)
a6e9f9df
AM
426 return NULL;
427
57028622
NC
428 /* If the size_t type is smaller than the bfd_size_type, eg because
429 you are building a 32-bit tool on a 64-bit host, then make sure
430 that when the sizes are cast to (size_t) no information is lost. */
7c1c1904
AM
431 if ((size_t) size != size
432 || (size_t) nmemb != nmemb
433 || (size_t) amt != amt)
57028622
NC
434 {
435 if (reason)
66cfc0fd
AM
436 error (_("Size truncation prevents reading %s"
437 " elements of size %s for %s\n"),
438 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
57028622
NC
439 return NULL;
440 }
441
442 /* Check for size overflow. */
7c1c1904 443 if (amt / size != nmemb || (size_t) amt + 1 == 0)
57028622
NC
444 {
445 if (reason)
66cfc0fd
AM
446 error (_("Size overflow prevents reading %s"
447 " elements of size %s for %s\n"),
448 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
57028622
NC
449 return NULL;
450 }
451
c22b42ce 452 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
c9c1d674 453 attempting to allocate memory when the read is bound to fail. */
978c4450
AM
454 if (filedata->archive_file_offset > filedata->file_size
455 || offset > filedata->file_size - filedata->archive_file_offset
456 || amt > filedata->file_size - filedata->archive_file_offset - offset)
a6e9f9df 457 {
049b0c3a 458 if (reason)
66cfc0fd
AM
459 error (_("Reading %s bytes extends past end of file for %s\n"),
460 bfd_vmatoa ("u", amt), reason);
a6e9f9df
AM
461 return NULL;
462 }
463
978c4450
AM
464 if (fseek (filedata->handle, filedata->archive_file_offset + offset,
465 SEEK_SET))
071436c6
NC
466 {
467 if (reason)
c9c1d674 468 error (_("Unable to seek to 0x%lx for %s\n"),
978c4450 469 filedata->archive_file_offset + offset, reason);
071436c6
NC
470 return NULL;
471 }
472
a6e9f9df
AM
473 mvar = var;
474 if (mvar == NULL)
475 {
7c1c1904
AM
476 /* + 1 so that we can '\0' terminate invalid string table sections. */
477 mvar = malloc ((size_t) amt + 1);
a6e9f9df
AM
478
479 if (mvar == NULL)
480 {
049b0c3a 481 if (reason)
66cfc0fd
AM
482 error (_("Out of memory allocating %s bytes for %s\n"),
483 bfd_vmatoa ("u", amt), reason);
a6e9f9df
AM
484 return NULL;
485 }
c256ffe7 486
c9c1d674 487 ((char *) mvar)[amt] = '\0';
a6e9f9df
AM
488 }
489
dda8d76d 490 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
a6e9f9df 491 {
049b0c3a 492 if (reason)
66cfc0fd
AM
493 error (_("Unable to read in %s bytes of %s\n"),
494 bfd_vmatoa ("u", amt), reason);
a6e9f9df
AM
495 if (mvar != var)
496 free (mvar);
497 return NULL;
498 }
499
500 return mvar;
501}
502
32ec8896
NC
503/* Print a VMA value in the MODE specified.
504 Returns the number of characters displayed. */
cb8f3167 505
32ec8896 506static unsigned int
14a91970 507print_vma (bfd_vma vma, print_mode mode)
66543521 508{
32ec8896 509 unsigned int nc = 0;
66543521 510
14a91970 511 switch (mode)
66543521 512 {
14a91970
AM
513 case FULL_HEX:
514 nc = printf ("0x");
1a0670f3 515 /* Fall through. */
14a91970 516 case LONG_HEX:
f7a99963 517#ifdef BFD64
14a91970 518 if (is_32bit_elf)
437c2fb7 519 return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
f7a99963 520#endif
14a91970
AM
521 printf_vma (vma);
522 return nc + 16;
b19aac67 523
14a91970
AM
524 case DEC_5:
525 if (vma <= 99999)
526 return printf ("%5" BFD_VMA_FMT "d", vma);
1a0670f3 527 /* Fall through. */
14a91970
AM
528 case PREFIX_HEX:
529 nc = printf ("0x");
1a0670f3 530 /* Fall through. */
14a91970
AM
531 case HEX:
532 return nc + printf ("%" BFD_VMA_FMT "x", vma);
b19aac67 533
14a91970
AM
534 case DEC:
535 return printf ("%" BFD_VMA_FMT "d", vma);
b19aac67 536
14a91970
AM
537 case UNSIGNED:
538 return printf ("%" BFD_VMA_FMT "u", vma);
32ec8896
NC
539
540 default:
541 /* FIXME: Report unrecognised mode ? */
542 return 0;
f7a99963 543 }
f7a99963
NC
544}
545
7bfd842d 546/* Display a symbol on stdout. Handles the display of control characters and
3bfcb652 547 multibye characters (assuming the host environment supports them).
31104126 548
7bfd842d
NC
549 Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
550
0942c7ab
NC
551 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
552 abs(WIDTH) - 5 characters followed by "[...]".
553
7bfd842d
NC
554 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
555 padding as necessary.
171191ba
NC
556
557 Returns the number of emitted characters. */
558
559static unsigned int
0942c7ab 560print_symbol (signed int width, const char * symbol)
31104126 561{
015dc7e1
AM
562 bool extra_padding = false;
563 bool do_dots = false;
32ec8896 564 signed int num_printed = 0;
3bfcb652 565#ifdef HAVE_MBSTATE_T
7bfd842d 566 mbstate_t state;
3bfcb652 567#endif
32ec8896 568 unsigned int width_remaining;
79bc120c 569 const void * alloced_symbol = NULL;
961c521f 570
7bfd842d 571 if (width < 0)
961c521f 572 {
88305e1b 573 /* Keep the width positive. This helps the code below. */
961c521f 574 width = - width;
015dc7e1 575 extra_padding = true;
0b4362b0 576 }
56d8f8a9
NC
577 else if (width == 0)
578 return 0;
961c521f 579
7bfd842d
NC
580 if (do_wide)
581 /* Set the remaining width to a very large value.
582 This simplifies the code below. */
583 width_remaining = INT_MAX;
584 else
0942c7ab
NC
585 {
586 width_remaining = width;
587 if (! do_not_show_symbol_truncation
588 && (int) strlen (symbol) > width)
589 {
590 width_remaining -= 5;
591 if ((int) width_remaining < 0)
592 width_remaining = 0;
015dc7e1 593 do_dots = true;
0942c7ab
NC
594 }
595 }
cb8f3167 596
3bfcb652 597#ifdef HAVE_MBSTATE_T
7bfd842d
NC
598 /* Initialise the multibyte conversion state. */
599 memset (& state, 0, sizeof (state));
3bfcb652 600#endif
961c521f 601
79bc120c
NC
602 if (do_demangle && *symbol)
603 {
604 const char * res = cplus_demangle (symbol, demangle_flags);
605
606 if (res != NULL)
607 alloced_symbol = symbol = res;
608 }
609
7bfd842d
NC
610 while (width_remaining)
611 {
612 size_t n;
7bfd842d 613 const char c = *symbol++;
961c521f 614
7bfd842d 615 if (c == 0)
961c521f
NC
616 break;
617
7bfd842d
NC
618 /* Do not print control characters directly as they can affect terminal
619 settings. Such characters usually appear in the names generated
620 by the assembler for local labels. */
621 if (ISCNTRL (c))
961c521f 622 {
7bfd842d 623 if (width_remaining < 2)
961c521f
NC
624 break;
625
7bfd842d
NC
626 printf ("^%c", c + 0x40);
627 width_remaining -= 2;
171191ba 628 num_printed += 2;
961c521f 629 }
7bfd842d
NC
630 else if (ISPRINT (c))
631 {
632 putchar (c);
633 width_remaining --;
634 num_printed ++;
635 }
961c521f
NC
636 else
637 {
3bfcb652
NC
638#ifdef HAVE_MBSTATE_T
639 wchar_t w;
640#endif
7bfd842d
NC
641 /* Let printf do the hard work of displaying multibyte characters. */
642 printf ("%.1s", symbol - 1);
643 width_remaining --;
644 num_printed ++;
645
3bfcb652 646#ifdef HAVE_MBSTATE_T
7bfd842d
NC
647 /* Try to find out how many bytes made up the character that was
648 just printed. Advance the symbol pointer past the bytes that
649 were displayed. */
650 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
3bfcb652
NC
651#else
652 n = 1;
653#endif
7bfd842d
NC
654 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
655 symbol += (n - 1);
961c521f 656 }
961c521f 657 }
171191ba 658
0942c7ab
NC
659 if (do_dots)
660 num_printed += printf ("[...]");
661
7bfd842d 662 if (extra_padding && num_printed < width)
171191ba
NC
663 {
664 /* Fill in the remaining spaces. */
7bfd842d
NC
665 printf ("%-*s", width - num_printed, " ");
666 num_printed = width;
171191ba
NC
667 }
668
79bc120c 669 free ((void *) alloced_symbol);
171191ba 670 return num_printed;
31104126
NC
671}
672
1449284b 673/* Returns a pointer to a static buffer containing a printable version of
74e1a04b
NC
674 the given section's name. Like print_symbol, except that it does not try
675 to print multibyte characters, it just interprets them as hex values. */
676
677static const char *
dda8d76d 678printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
74e1a04b 679{
ca0e11aa 680#define MAX_PRINT_SEC_NAME_LEN 256
74e1a04b 681 static char sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
b9e920ec 682 const char * name = SECTION_NAME_PRINT (sec);
74e1a04b
NC
683 char * buf = sec_name_buf;
684 char c;
685 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
686
687 while ((c = * name ++) != 0)
688 {
689 if (ISCNTRL (c))
690 {
691 if (remaining < 2)
692 break;
948f632f 693
74e1a04b
NC
694 * buf ++ = '^';
695 * buf ++ = c + 0x40;
696 remaining -= 2;
697 }
698 else if (ISPRINT (c))
699 {
700 * buf ++ = c;
701 remaining -= 1;
702 }
703 else
704 {
705 static char hex[17] = "0123456789ABCDEF";
706
707 if (remaining < 4)
708 break;
709 * buf ++ = '<';
710 * buf ++ = hex[(c & 0xf0) >> 4];
711 * buf ++ = hex[c & 0x0f];
712 * buf ++ = '>';
713 remaining -= 4;
714 }
715
716 if (remaining == 0)
717 break;
718 }
719
720 * buf = 0;
721 return sec_name_buf;
722}
723
724static const char *
dda8d76d 725printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
74e1a04b 726{
dda8d76d 727 if (ndx >= filedata->file_header.e_shnum)
74e1a04b
NC
728 return _("<corrupt>");
729
dda8d76d 730 return printable_section_name (filedata, filedata->section_headers + ndx);
74e1a04b
NC
731}
732
89fac5e3
RS
733/* Return a pointer to section NAME, or NULL if no such section exists. */
734
735static Elf_Internal_Shdr *
dda8d76d 736find_section (Filedata * filedata, const char * name)
89fac5e3
RS
737{
738 unsigned int i;
739
68807c3c
NC
740 if (filedata->section_headers == NULL)
741 return NULL;
dda8d76d
NC
742
743 for (i = 0; i < filedata->file_header.e_shnum; i++)
b9e920ec
AM
744 if (SECTION_NAME_VALID (filedata->section_headers + i)
745 && streq (SECTION_NAME (filedata->section_headers + i), name))
dda8d76d 746 return filedata->section_headers + i;
89fac5e3
RS
747
748 return NULL;
749}
750
0b6ae522
DJ
751/* Return a pointer to a section containing ADDR, or NULL if no such
752 section exists. */
753
754static Elf_Internal_Shdr *
dda8d76d 755find_section_by_address (Filedata * filedata, bfd_vma addr)
0b6ae522
DJ
756{
757 unsigned int i;
758
68807c3c
NC
759 if (filedata->section_headers == NULL)
760 return NULL;
761
dda8d76d 762 for (i = 0; i < filedata->file_header.e_shnum; i++)
0b6ae522 763 {
dda8d76d
NC
764 Elf_Internal_Shdr *sec = filedata->section_headers + i;
765
0b6ae522
DJ
766 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
767 return sec;
768 }
769
770 return NULL;
771}
772
071436c6 773static Elf_Internal_Shdr *
dda8d76d 774find_section_by_type (Filedata * filedata, unsigned int type)
071436c6
NC
775{
776 unsigned int i;
777
68807c3c
NC
778 if (filedata->section_headers == NULL)
779 return NULL;
780
dda8d76d 781 for (i = 0; i < filedata->file_header.e_shnum; i++)
071436c6 782 {
dda8d76d
NC
783 Elf_Internal_Shdr *sec = filedata->section_headers + i;
784
071436c6
NC
785 if (sec->sh_type == type)
786 return sec;
787 }
788
789 return NULL;
790}
791
657d0d47
CC
792/* Return a pointer to section NAME, or NULL if no such section exists,
793 restricted to the list of sections given in SET. */
794
795static Elf_Internal_Shdr *
dda8d76d 796find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
657d0d47
CC
797{
798 unsigned int i;
799
68807c3c
NC
800 if (filedata->section_headers == NULL)
801 return NULL;
802
657d0d47
CC
803 if (set != NULL)
804 {
805 while ((i = *set++) > 0)
b814a36d
NC
806 {
807 /* See PR 21156 for a reproducer. */
dda8d76d 808 if (i >= filedata->file_header.e_shnum)
b814a36d
NC
809 continue; /* FIXME: Should we issue an error message ? */
810
b9e920ec
AM
811 if (SECTION_NAME_VALID (filedata->section_headers + i)
812 && streq (SECTION_NAME (filedata->section_headers + i), name))
dda8d76d 813 return filedata->section_headers + i;
b814a36d 814 }
657d0d47
CC
815 }
816
dda8d76d 817 return find_section (filedata, name);
657d0d47
CC
818}
819
32ec8896 820/* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
28f997cf
TG
821 This OS has so many departures from the ELF standard that we test it at
822 many places. */
823
015dc7e1 824static inline bool
dda8d76d 825is_ia64_vms (Filedata * filedata)
28f997cf 826{
dda8d76d
NC
827 return filedata->file_header.e_machine == EM_IA_64
828 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
28f997cf
TG
829}
830
bcedfee6 831/* Guess the relocation size commonly used by the specific machines. */
252b5132 832
015dc7e1 833static bool
2dc4cec1 834guess_is_rela (unsigned int e_machine)
252b5132 835{
9c19a809 836 switch (e_machine)
252b5132
RH
837 {
838 /* Targets that use REL relocations. */
252b5132 839 case EM_386:
22abe556 840 case EM_IAMCU:
f954747f 841 case EM_960:
e9f53129 842 case EM_ARM:
2b0337b0 843 case EM_D10V:
252b5132 844 case EM_CYGNUS_D10V:
e9f53129 845 case EM_DLX:
252b5132 846 case EM_MIPS:
4fe85591 847 case EM_MIPS_RS3_LE:
e9f53129 848 case EM_CYGNUS_M32R:
1c0d3aa6 849 case EM_SCORE:
f6c1a2d5 850 case EM_XGATE:
fe944acf 851 case EM_NFP:
aca4efc7 852 case EM_BPF:
015dc7e1 853 return false;
103f02d3 854
252b5132
RH
855 /* Targets that use RELA relocations. */
856 case EM_68K:
f954747f 857 case EM_860:
a06ea964 858 case EM_AARCH64:
cfb8c092 859 case EM_ADAPTEVA_EPIPHANY:
e9f53129
AM
860 case EM_ALPHA:
861 case EM_ALTERA_NIOS2:
886a2506
NC
862 case EM_ARC:
863 case EM_ARC_COMPACT:
864 case EM_ARC_COMPACT2:
e9f53129
AM
865 case EM_AVR:
866 case EM_AVR_OLD:
867 case EM_BLACKFIN:
60bca95a 868 case EM_CR16:
e9f53129
AM
869 case EM_CRIS:
870 case EM_CRX:
b8891f8d 871 case EM_CSKY:
2b0337b0 872 case EM_D30V:
252b5132 873 case EM_CYGNUS_D30V:
2b0337b0 874 case EM_FR30:
3f8107ab 875 case EM_FT32:
252b5132 876 case EM_CYGNUS_FR30:
5c70f934 877 case EM_CYGNUS_FRV:
e9f53129
AM
878 case EM_H8S:
879 case EM_H8_300:
880 case EM_H8_300H:
800eeca4 881 case EM_IA_64:
1e4cf259
NC
882 case EM_IP2K:
883 case EM_IP2K_OLD:
3b36097d 884 case EM_IQ2000:
84e94c90 885 case EM_LATTICEMICO32:
ff7eeb89 886 case EM_M32C_OLD:
49f58d10 887 case EM_M32C:
e9f53129
AM
888 case EM_M32R:
889 case EM_MCORE:
15ab5209 890 case EM_CYGNUS_MEP:
a3c62988 891 case EM_METAG:
e9f53129
AM
892 case EM_MMIX:
893 case EM_MN10200:
894 case EM_CYGNUS_MN10200:
895 case EM_MN10300:
896 case EM_CYGNUS_MN10300:
5506d11a 897 case EM_MOXIE:
e9f53129
AM
898 case EM_MSP430:
899 case EM_MSP430_OLD:
d031aafb 900 case EM_MT:
35c08157 901 case EM_NDS32:
64fd6348 902 case EM_NIOS32:
73589c9d 903 case EM_OR1K:
e9f53129
AM
904 case EM_PPC64:
905 case EM_PPC:
2b100bb5 906 case EM_TI_PRU:
e23eba97 907 case EM_RISCV:
99c513f6 908 case EM_RL78:
c7927a3c 909 case EM_RX:
e9f53129
AM
910 case EM_S390:
911 case EM_S390_OLD:
912 case EM_SH:
913 case EM_SPARC:
914 case EM_SPARC32PLUS:
915 case EM_SPARCV9:
916 case EM_SPU:
40b36596 917 case EM_TI_C6000:
aa137e4d
NC
918 case EM_TILEGX:
919 case EM_TILEPRO:
708e2187 920 case EM_V800:
e9f53129
AM
921 case EM_V850:
922 case EM_CYGNUS_V850:
923 case EM_VAX:
619ed720 924 case EM_VISIUM:
e9f53129 925 case EM_X86_64:
8a9036a4 926 case EM_L1OM:
7a9068fe 927 case EM_K1OM:
e9f53129
AM
928 case EM_XSTORMY16:
929 case EM_XTENSA:
930 case EM_XTENSA_OLD:
7ba29e2a
NC
931 case EM_MICROBLAZE:
932 case EM_MICROBLAZE_OLD:
f96bd6c2 933 case EM_WEBASSEMBLY:
015dc7e1 934 return true;
103f02d3 935
e9f53129
AM
936 case EM_68HC05:
937 case EM_68HC08:
938 case EM_68HC11:
939 case EM_68HC16:
940 case EM_FX66:
941 case EM_ME16:
d1133906 942 case EM_MMA:
d1133906
NC
943 case EM_NCPU:
944 case EM_NDR1:
e9f53129 945 case EM_PCP:
d1133906 946 case EM_ST100:
e9f53129 947 case EM_ST19:
d1133906 948 case EM_ST7:
e9f53129
AM
949 case EM_ST9PLUS:
950 case EM_STARCORE:
d1133906 951 case EM_SVX:
e9f53129 952 case EM_TINYJ:
9c19a809
NC
953 default:
954 warn (_("Don't know about relocations on this machine architecture\n"));
015dc7e1 955 return false;
9c19a809
NC
956 }
957}
252b5132 958
dda8d76d 959/* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
32ec8896
NC
960 Returns TRUE upon success, FALSE otherwise. If successful then a
961 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
962 and the number of relocs loaded is placed in *NRELASP. It is the caller's
963 responsibility to free the allocated buffer. */
964
015dc7e1 965static bool
dda8d76d
NC
966slurp_rela_relocs (Filedata * filedata,
967 unsigned long rel_offset,
968 unsigned long rel_size,
969 Elf_Internal_Rela ** relasp,
970 unsigned long * nrelasp)
9c19a809 971{
2cf0635d 972 Elf_Internal_Rela * relas;
8b73c356 973 size_t nrelas;
4d6ed7c8 974 unsigned int i;
252b5132 975
4d6ed7c8
NC
976 if (is_32bit_elf)
977 {
2cf0635d 978 Elf32_External_Rela * erelas;
103f02d3 979
dda8d76d 980 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
9cf03b7e 981 rel_size, _("32-bit relocation data"));
a6e9f9df 982 if (!erelas)
015dc7e1 983 return false;
252b5132 984
4d6ed7c8 985 nrelas = rel_size / sizeof (Elf32_External_Rela);
103f02d3 986
3f5e193b
NC
987 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
988 sizeof (Elf_Internal_Rela));
103f02d3 989
4d6ed7c8
NC
990 if (relas == NULL)
991 {
c256ffe7 992 free (erelas);
591a748a 993 error (_("out of memory parsing relocs\n"));
015dc7e1 994 return false;
4d6ed7c8 995 }
103f02d3 996
4d6ed7c8
NC
997 for (i = 0; i < nrelas; i++)
998 {
999 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1000 relas[i].r_info = BYTE_GET (erelas[i].r_info);
598aaa76 1001 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
4d6ed7c8 1002 }
103f02d3 1003
4d6ed7c8
NC
1004 free (erelas);
1005 }
1006 else
1007 {
2cf0635d 1008 Elf64_External_Rela * erelas;
103f02d3 1009
dda8d76d 1010 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
9cf03b7e 1011 rel_size, _("64-bit relocation data"));
a6e9f9df 1012 if (!erelas)
015dc7e1 1013 return false;
4d6ed7c8
NC
1014
1015 nrelas = rel_size / sizeof (Elf64_External_Rela);
103f02d3 1016
3f5e193b
NC
1017 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1018 sizeof (Elf_Internal_Rela));
103f02d3 1019
4d6ed7c8
NC
1020 if (relas == NULL)
1021 {
c256ffe7 1022 free (erelas);
591a748a 1023 error (_("out of memory parsing relocs\n"));
015dc7e1 1024 return false;
9c19a809 1025 }
4d6ed7c8
NC
1026
1027 for (i = 0; i < nrelas; i++)
9c19a809 1028 {
66543521
AM
1029 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1030 relas[i].r_info = BYTE_GET (erelas[i].r_info);
598aaa76 1031 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
861fb55a
DJ
1032
1033 /* The #ifdef BFD64 below is to prevent a compile time
1034 warning. We know that if we do not have a 64 bit data
1035 type that we will never execute this code anyway. */
1036#ifdef BFD64
dda8d76d
NC
1037 if (filedata->file_header.e_machine == EM_MIPS
1038 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
861fb55a
DJ
1039 {
1040 /* In little-endian objects, r_info isn't really a
1041 64-bit little-endian value: it has a 32-bit
1042 little-endian symbol index followed by four
1043 individual byte fields. Reorder INFO
1044 accordingly. */
91d6fa6a
NC
1045 bfd_vma inf = relas[i].r_info;
1046 inf = (((inf & 0xffffffff) << 32)
1047 | ((inf >> 56) & 0xff)
1048 | ((inf >> 40) & 0xff00)
1049 | ((inf >> 24) & 0xff0000)
1050 | ((inf >> 8) & 0xff000000));
1051 relas[i].r_info = inf;
861fb55a
DJ
1052 }
1053#endif /* BFD64 */
4d6ed7c8 1054 }
103f02d3 1055
4d6ed7c8
NC
1056 free (erelas);
1057 }
32ec8896 1058
4d6ed7c8
NC
1059 *relasp = relas;
1060 *nrelasp = nrelas;
015dc7e1 1061 return true;
4d6ed7c8 1062}
103f02d3 1063
dda8d76d 1064/* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
32ec8896
NC
1065 Returns TRUE upon success, FALSE otherwise. If successful then a
1066 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1067 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1068 responsibility to free the allocated buffer. */
1069
015dc7e1 1070static bool
dda8d76d
NC
1071slurp_rel_relocs (Filedata * filedata,
1072 unsigned long rel_offset,
1073 unsigned long rel_size,
1074 Elf_Internal_Rela ** relsp,
1075 unsigned long * nrelsp)
4d6ed7c8 1076{
2cf0635d 1077 Elf_Internal_Rela * rels;
8b73c356 1078 size_t nrels;
4d6ed7c8 1079 unsigned int i;
103f02d3 1080
4d6ed7c8
NC
1081 if (is_32bit_elf)
1082 {
2cf0635d 1083 Elf32_External_Rel * erels;
103f02d3 1084
dda8d76d 1085 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
9cf03b7e 1086 rel_size, _("32-bit relocation data"));
a6e9f9df 1087 if (!erels)
015dc7e1 1088 return false;
103f02d3 1089
4d6ed7c8 1090 nrels = rel_size / sizeof (Elf32_External_Rel);
103f02d3 1091
3f5e193b 1092 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
103f02d3 1093
4d6ed7c8
NC
1094 if (rels == NULL)
1095 {
c256ffe7 1096 free (erels);
591a748a 1097 error (_("out of memory parsing relocs\n"));
015dc7e1 1098 return false;
4d6ed7c8
NC
1099 }
1100
1101 for (i = 0; i < nrels; i++)
1102 {
1103 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1104 rels[i].r_info = BYTE_GET (erels[i].r_info);
c8286bd1 1105 rels[i].r_addend = 0;
9ea033b2 1106 }
4d6ed7c8
NC
1107
1108 free (erels);
9c19a809
NC
1109 }
1110 else
1111 {
2cf0635d 1112 Elf64_External_Rel * erels;
9ea033b2 1113
dda8d76d 1114 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
9cf03b7e 1115 rel_size, _("64-bit relocation data"));
a6e9f9df 1116 if (!erels)
015dc7e1 1117 return false;
103f02d3 1118
4d6ed7c8 1119 nrels = rel_size / sizeof (Elf64_External_Rel);
103f02d3 1120
3f5e193b 1121 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
103f02d3 1122
4d6ed7c8 1123 if (rels == NULL)
9c19a809 1124 {
c256ffe7 1125 free (erels);
591a748a 1126 error (_("out of memory parsing relocs\n"));
015dc7e1 1127 return false;
4d6ed7c8 1128 }
103f02d3 1129
4d6ed7c8
NC
1130 for (i = 0; i < nrels; i++)
1131 {
66543521
AM
1132 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1133 rels[i].r_info = BYTE_GET (erels[i].r_info);
c8286bd1 1134 rels[i].r_addend = 0;
861fb55a
DJ
1135
1136 /* The #ifdef BFD64 below is to prevent a compile time
1137 warning. We know that if we do not have a 64 bit data
1138 type that we will never execute this code anyway. */
1139#ifdef BFD64
dda8d76d
NC
1140 if (filedata->file_header.e_machine == EM_MIPS
1141 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
861fb55a
DJ
1142 {
1143 /* In little-endian objects, r_info isn't really a
1144 64-bit little-endian value: it has a 32-bit
1145 little-endian symbol index followed by four
1146 individual byte fields. Reorder INFO
1147 accordingly. */
91d6fa6a
NC
1148 bfd_vma inf = rels[i].r_info;
1149 inf = (((inf & 0xffffffff) << 32)
1150 | ((inf >> 56) & 0xff)
1151 | ((inf >> 40) & 0xff00)
1152 | ((inf >> 24) & 0xff0000)
1153 | ((inf >> 8) & 0xff000000));
1154 rels[i].r_info = inf;
861fb55a
DJ
1155 }
1156#endif /* BFD64 */
4d6ed7c8 1157 }
103f02d3 1158
4d6ed7c8
NC
1159 free (erels);
1160 }
32ec8896 1161
4d6ed7c8
NC
1162 *relsp = rels;
1163 *nrelsp = nrels;
015dc7e1 1164 return true;
4d6ed7c8 1165}
103f02d3 1166
aca88567
NC
1167/* Returns the reloc type extracted from the reloc info field. */
1168
1169static unsigned int
dda8d76d 1170get_reloc_type (Filedata * filedata, bfd_vma reloc_info)
aca88567
NC
1171{
1172 if (is_32bit_elf)
1173 return ELF32_R_TYPE (reloc_info);
1174
dda8d76d 1175 switch (filedata->file_header.e_machine)
aca88567
NC
1176 {
1177 case EM_MIPS:
1178 /* Note: We assume that reloc_info has already been adjusted for us. */
1179 return ELF64_MIPS_R_TYPE (reloc_info);
1180
1181 case EM_SPARCV9:
1182 return ELF64_R_TYPE_ID (reloc_info);
1183
1184 default:
1185 return ELF64_R_TYPE (reloc_info);
1186 }
1187}
1188
1189/* Return the symbol index extracted from the reloc info field. */
1190
1191static bfd_vma
1192get_reloc_symindex (bfd_vma reloc_info)
1193{
1194 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1195}
1196
015dc7e1 1197static inline bool
dda8d76d 1198uses_msp430x_relocs (Filedata * filedata)
13761a11
NC
1199{
1200 return
dda8d76d 1201 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
13761a11 1202 /* GCC uses osabi == ELFOSBI_STANDALONE. */
dda8d76d 1203 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
13761a11 1204 /* TI compiler uses ELFOSABI_NONE. */
dda8d76d 1205 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
13761a11
NC
1206}
1207
d3ba0551
AM
1208/* Display the contents of the relocation data found at the specified
1209 offset. */
ee42cf8c 1210
015dc7e1 1211static bool
dda8d76d
NC
1212dump_relocations (Filedata * filedata,
1213 unsigned long rel_offset,
1214 unsigned long rel_size,
1215 Elf_Internal_Sym * symtab,
1216 unsigned long nsyms,
1217 char * strtab,
1218 unsigned long strtablen,
1219 int is_rela,
015dc7e1 1220 bool is_dynsym)
4d6ed7c8 1221{
32ec8896 1222 unsigned long i;
2cf0635d 1223 Elf_Internal_Rela * rels;
015dc7e1 1224 bool res = true;
103f02d3 1225
4d6ed7c8 1226 if (is_rela == UNKNOWN)
dda8d76d 1227 is_rela = guess_is_rela (filedata->file_header.e_machine);
103f02d3 1228
4d6ed7c8
NC
1229 if (is_rela)
1230 {
dda8d76d 1231 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
015dc7e1 1232 return false;
4d6ed7c8
NC
1233 }
1234 else
1235 {
dda8d76d 1236 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
015dc7e1 1237 return false;
252b5132
RH
1238 }
1239
410f7a12
L
1240 if (is_32bit_elf)
1241 {
1242 if (is_rela)
2c71103e
NC
1243 {
1244 if (do_wide)
1245 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1246 else
1247 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1248 }
410f7a12 1249 else
2c71103e
NC
1250 {
1251 if (do_wide)
1252 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1253 else
1254 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1255 }
410f7a12 1256 }
252b5132 1257 else
410f7a12
L
1258 {
1259 if (is_rela)
2c71103e
NC
1260 {
1261 if (do_wide)
8beeaeb7 1262 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
2c71103e
NC
1263 else
1264 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1265 }
410f7a12 1266 else
2c71103e
NC
1267 {
1268 if (do_wide)
8beeaeb7 1269 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
2c71103e
NC
1270 else
1271 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1272 }
410f7a12 1273 }
252b5132
RH
1274
1275 for (i = 0; i < rel_size; i++)
1276 {
2cf0635d 1277 const char * rtype;
b34976b6 1278 bfd_vma offset;
91d6fa6a 1279 bfd_vma inf;
b34976b6
AM
1280 bfd_vma symtab_index;
1281 bfd_vma type;
103f02d3 1282
b34976b6 1283 offset = rels[i].r_offset;
91d6fa6a 1284 inf = rels[i].r_info;
103f02d3 1285
dda8d76d 1286 type = get_reloc_type (filedata, inf);
91d6fa6a 1287 symtab_index = get_reloc_symindex (inf);
252b5132 1288
410f7a12
L
1289 if (is_32bit_elf)
1290 {
39dbeff8
AM
1291 printf ("%8.8lx %8.8lx ",
1292 (unsigned long) offset & 0xffffffff,
91d6fa6a 1293 (unsigned long) inf & 0xffffffff);
410f7a12
L
1294 }
1295 else
1296 {
39dbeff8 1297 printf (do_wide
d1ce973e
AM
1298 ? "%16.16" BFD_VMA_FMT "x %16.16" BFD_VMA_FMT "x "
1299 : "%12.12" BFD_VMA_FMT "x %12.12" BFD_VMA_FMT "x ",
91d6fa6a 1300 offset, inf);
410f7a12 1301 }
103f02d3 1302
dda8d76d 1303 switch (filedata->file_header.e_machine)
252b5132
RH
1304 {
1305 default:
1306 rtype = NULL;
1307 break;
1308
a06ea964
NC
1309 case EM_AARCH64:
1310 rtype = elf_aarch64_reloc_type (type);
1311 break;
1312
2b0337b0 1313 case EM_M32R:
252b5132 1314 case EM_CYGNUS_M32R:
9ea033b2 1315 rtype = elf_m32r_reloc_type (type);
252b5132
RH
1316 break;
1317
1318 case EM_386:
22abe556 1319 case EM_IAMCU:
9ea033b2 1320 rtype = elf_i386_reloc_type (type);
252b5132
RH
1321 break;
1322
ba2685cc
AM
1323 case EM_68HC11:
1324 case EM_68HC12:
1325 rtype = elf_m68hc11_reloc_type (type);
1326 break;
75751cd9 1327
7b4ae824
JD
1328 case EM_S12Z:
1329 rtype = elf_s12z_reloc_type (type);
1330 break;
1331
252b5132 1332 case EM_68K:
9ea033b2 1333 rtype = elf_m68k_reloc_type (type);
252b5132
RH
1334 break;
1335
f954747f
AM
1336 case EM_960:
1337 rtype = elf_i960_reloc_type (type);
1338 break;
1339
adde6300 1340 case EM_AVR:
2b0337b0 1341 case EM_AVR_OLD:
adde6300
AM
1342 rtype = elf_avr_reloc_type (type);
1343 break;
1344
9ea033b2
NC
1345 case EM_OLD_SPARCV9:
1346 case EM_SPARC32PLUS:
1347 case EM_SPARCV9:
252b5132 1348 case EM_SPARC:
9ea033b2 1349 rtype = elf_sparc_reloc_type (type);
252b5132
RH
1350 break;
1351
e9f53129
AM
1352 case EM_SPU:
1353 rtype = elf_spu_reloc_type (type);
1354 break;
1355
708e2187
NC
1356 case EM_V800:
1357 rtype = v800_reloc_type (type);
1358 break;
2b0337b0 1359 case EM_V850:
252b5132 1360 case EM_CYGNUS_V850:
9ea033b2 1361 rtype = v850_reloc_type (type);
252b5132
RH
1362 break;
1363
2b0337b0 1364 case EM_D10V:
252b5132 1365 case EM_CYGNUS_D10V:
9ea033b2 1366 rtype = elf_d10v_reloc_type (type);
252b5132
RH
1367 break;
1368
2b0337b0 1369 case EM_D30V:
252b5132 1370 case EM_CYGNUS_D30V:
9ea033b2 1371 rtype = elf_d30v_reloc_type (type);
252b5132
RH
1372 break;
1373
d172d4ba
NC
1374 case EM_DLX:
1375 rtype = elf_dlx_reloc_type (type);
1376 break;
1377
252b5132 1378 case EM_SH:
9ea033b2 1379 rtype = elf_sh_reloc_type (type);
252b5132
RH
1380 break;
1381
2b0337b0 1382 case EM_MN10300:
252b5132 1383 case EM_CYGNUS_MN10300:
9ea033b2 1384 rtype = elf_mn10300_reloc_type (type);
252b5132
RH
1385 break;
1386
2b0337b0 1387 case EM_MN10200:
252b5132 1388 case EM_CYGNUS_MN10200:
9ea033b2 1389 rtype = elf_mn10200_reloc_type (type);
252b5132
RH
1390 break;
1391
2b0337b0 1392 case EM_FR30:
252b5132 1393 case EM_CYGNUS_FR30:
9ea033b2 1394 rtype = elf_fr30_reloc_type (type);
252b5132
RH
1395 break;
1396
ba2685cc
AM
1397 case EM_CYGNUS_FRV:
1398 rtype = elf_frv_reloc_type (type);
1399 break;
5c70f934 1400
b8891f8d
AJ
1401 case EM_CSKY:
1402 rtype = elf_csky_reloc_type (type);
1403 break;
1404
3f8107ab
AM
1405 case EM_FT32:
1406 rtype = elf_ft32_reloc_type (type);
1407 break;
1408
252b5132 1409 case EM_MCORE:
9ea033b2 1410 rtype = elf_mcore_reloc_type (type);
252b5132
RH
1411 break;
1412
3c3bdf30
NC
1413 case EM_MMIX:
1414 rtype = elf_mmix_reloc_type (type);
1415 break;
1416
5506d11a
AM
1417 case EM_MOXIE:
1418 rtype = elf_moxie_reloc_type (type);
1419 break;
1420
2469cfa2 1421 case EM_MSP430:
dda8d76d 1422 if (uses_msp430x_relocs (filedata))
13761a11
NC
1423 {
1424 rtype = elf_msp430x_reloc_type (type);
1425 break;
1426 }
1a0670f3 1427 /* Fall through. */
2469cfa2
NC
1428 case EM_MSP430_OLD:
1429 rtype = elf_msp430_reloc_type (type);
1430 break;
1431
35c08157
KLC
1432 case EM_NDS32:
1433 rtype = elf_nds32_reloc_type (type);
1434 break;
1435
252b5132 1436 case EM_PPC:
9ea033b2 1437 rtype = elf_ppc_reloc_type (type);
252b5132
RH
1438 break;
1439
c833c019
AM
1440 case EM_PPC64:
1441 rtype = elf_ppc64_reloc_type (type);
1442 break;
1443
252b5132 1444 case EM_MIPS:
4fe85591 1445 case EM_MIPS_RS3_LE:
9ea033b2 1446 rtype = elf_mips_reloc_type (type);
252b5132
RH
1447 break;
1448
e23eba97
NC
1449 case EM_RISCV:
1450 rtype = elf_riscv_reloc_type (type);
1451 break;
1452
252b5132 1453 case EM_ALPHA:
9ea033b2 1454 rtype = elf_alpha_reloc_type (type);
252b5132
RH
1455 break;
1456
1457 case EM_ARM:
9ea033b2 1458 rtype = elf_arm_reloc_type (type);
252b5132
RH
1459 break;
1460
584da044 1461 case EM_ARC:
886a2506
NC
1462 case EM_ARC_COMPACT:
1463 case EM_ARC_COMPACT2:
9ea033b2 1464 rtype = elf_arc_reloc_type (type);
252b5132
RH
1465 break;
1466
1467 case EM_PARISC:
69e617ca 1468 rtype = elf_hppa_reloc_type (type);
252b5132 1469 break;
7d466069 1470
b8720f9d
JL
1471 case EM_H8_300:
1472 case EM_H8_300H:
1473 case EM_H8S:
1474 rtype = elf_h8_reloc_type (type);
1475 break;
1476
73589c9d
CS
1477 case EM_OR1K:
1478 rtype = elf_or1k_reloc_type (type);
3b16e843
NC
1479 break;
1480
7d466069 1481 case EM_PJ:
2b0337b0 1482 case EM_PJ_OLD:
7d466069
ILT
1483 rtype = elf_pj_reloc_type (type);
1484 break;
800eeca4
JW
1485 case EM_IA_64:
1486 rtype = elf_ia64_reloc_type (type);
1487 break;
1b61cf92
HPN
1488
1489 case EM_CRIS:
1490 rtype = elf_cris_reloc_type (type);
1491 break;
535c37ff 1492
f954747f
AM
1493 case EM_860:
1494 rtype = elf_i860_reloc_type (type);
1495 break;
1496
bcedfee6 1497 case EM_X86_64:
8a9036a4 1498 case EM_L1OM:
7a9068fe 1499 case EM_K1OM:
bcedfee6
NC
1500 rtype = elf_x86_64_reloc_type (type);
1501 break;
a85d7ed0 1502
f954747f
AM
1503 case EM_S370:
1504 rtype = i370_reloc_type (type);
1505 break;
1506
53c7db4b
KH
1507 case EM_S390_OLD:
1508 case EM_S390:
1509 rtype = elf_s390_reloc_type (type);
1510 break;
93fbbb04 1511
1c0d3aa6
NC
1512 case EM_SCORE:
1513 rtype = elf_score_reloc_type (type);
1514 break;
1515
93fbbb04
GK
1516 case EM_XSTORMY16:
1517 rtype = elf_xstormy16_reloc_type (type);
1518 break;
179d3252 1519
1fe1f39c
NC
1520 case EM_CRX:
1521 rtype = elf_crx_reloc_type (type);
1522 break;
1523
179d3252
JT
1524 case EM_VAX:
1525 rtype = elf_vax_reloc_type (type);
1526 break;
1e4cf259 1527
619ed720
EB
1528 case EM_VISIUM:
1529 rtype = elf_visium_reloc_type (type);
1530 break;
1531
aca4efc7
JM
1532 case EM_BPF:
1533 rtype = elf_bpf_reloc_type (type);
1534 break;
1535
cfb8c092
NC
1536 case EM_ADAPTEVA_EPIPHANY:
1537 rtype = elf_epiphany_reloc_type (type);
1538 break;
1539
1e4cf259
NC
1540 case EM_IP2K:
1541 case EM_IP2K_OLD:
1542 rtype = elf_ip2k_reloc_type (type);
1543 break;
3b36097d
SC
1544
1545 case EM_IQ2000:
1546 rtype = elf_iq2000_reloc_type (type);
1547 break;
88da6820
NC
1548
1549 case EM_XTENSA_OLD:
1550 case EM_XTENSA:
1551 rtype = elf_xtensa_reloc_type (type);
1552 break;
a34e3ecb 1553
84e94c90
NC
1554 case EM_LATTICEMICO32:
1555 rtype = elf_lm32_reloc_type (type);
1556 break;
1557
ff7eeb89 1558 case EM_M32C_OLD:
49f58d10
JB
1559 case EM_M32C:
1560 rtype = elf_m32c_reloc_type (type);
1561 break;
1562
d031aafb
NS
1563 case EM_MT:
1564 rtype = elf_mt_reloc_type (type);
a34e3ecb 1565 break;
1d65ded4
CM
1566
1567 case EM_BLACKFIN:
1568 rtype = elf_bfin_reloc_type (type);
1569 break;
15ab5209
DB
1570
1571 case EM_CYGNUS_MEP:
1572 rtype = elf_mep_reloc_type (type);
1573 break;
60bca95a
NC
1574
1575 case EM_CR16:
1576 rtype = elf_cr16_reloc_type (type);
1577 break;
dd24e3da 1578
7ba29e2a
NC
1579 case EM_MICROBLAZE:
1580 case EM_MICROBLAZE_OLD:
1581 rtype = elf_microblaze_reloc_type (type);
1582 break;
c7927a3c 1583
99c513f6
DD
1584 case EM_RL78:
1585 rtype = elf_rl78_reloc_type (type);
1586 break;
1587
c7927a3c
NC
1588 case EM_RX:
1589 rtype = elf_rx_reloc_type (type);
1590 break;
c29aca4a 1591
a3c62988
NC
1592 case EM_METAG:
1593 rtype = elf_metag_reloc_type (type);
1594 break;
1595
c29aca4a
NC
1596 case EM_XC16X:
1597 case EM_C166:
1598 rtype = elf_xc16x_reloc_type (type);
1599 break;
40b36596
JM
1600
1601 case EM_TI_C6000:
1602 rtype = elf_tic6x_reloc_type (type);
1603 break;
aa137e4d
NC
1604
1605 case EM_TILEGX:
1606 rtype = elf_tilegx_reloc_type (type);
1607 break;
1608
1609 case EM_TILEPRO:
1610 rtype = elf_tilepro_reloc_type (type);
1611 break;
f6c1a2d5 1612
f96bd6c2
PC
1613 case EM_WEBASSEMBLY:
1614 rtype = elf_wasm32_reloc_type (type);
1615 break;
1616
f6c1a2d5
NC
1617 case EM_XGATE:
1618 rtype = elf_xgate_reloc_type (type);
1619 break;
36591ba1
SL
1620
1621 case EM_ALTERA_NIOS2:
1622 rtype = elf_nios2_reloc_type (type);
1623 break;
2b100bb5
DD
1624
1625 case EM_TI_PRU:
1626 rtype = elf_pru_reloc_type (type);
1627 break;
fe944acf
FT
1628
1629 case EM_NFP:
1630 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1631 rtype = elf_nfp3200_reloc_type (type);
1632 else
1633 rtype = elf_nfp_reloc_type (type);
1634 break;
6655dba2
SB
1635
1636 case EM_Z80:
1637 rtype = elf_z80_reloc_type (type);
1638 break;
252b5132
RH
1639 }
1640
1641 if (rtype == NULL)
39dbeff8 1642 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
252b5132 1643 else
5c144731 1644 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
252b5132 1645
dda8d76d 1646 if (filedata->file_header.e_machine == EM_ALPHA
157c2599 1647 && rtype != NULL
7ace3541
RH
1648 && streq (rtype, "R_ALPHA_LITUSE")
1649 && is_rela)
1650 {
1651 switch (rels[i].r_addend)
1652 {
1653 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1654 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1655 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1656 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1657 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1658 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1659 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1660 default: rtype = NULL;
1661 }
32ec8896 1662
7ace3541
RH
1663 if (rtype)
1664 printf (" (%s)", rtype);
1665 else
1666 {
1667 putchar (' ');
1668 printf (_("<unknown addend: %lx>"),
1669 (unsigned long) rels[i].r_addend);
015dc7e1 1670 res = false;
7ace3541
RH
1671 }
1672 }
1673 else if (symtab_index)
252b5132 1674 {
af3fc3bc 1675 if (symtab == NULL || symtab_index >= nsyms)
32ec8896 1676 {
27a45f42
AS
1677 error (_(" bad symbol index: %08lx in reloc\n"),
1678 (unsigned long) symtab_index);
015dc7e1 1679 res = false;
32ec8896 1680 }
af3fc3bc 1681 else
19936277 1682 {
2cf0635d 1683 Elf_Internal_Sym * psym;
bb4d2ac2
L
1684 const char * version_string;
1685 enum versioned_symbol_info sym_info;
1686 unsigned short vna_other;
19936277 1687
af3fc3bc 1688 psym = symtab + symtab_index;
103f02d3 1689
bb4d2ac2 1690 version_string
dda8d76d 1691 = get_symbol_version_string (filedata, is_dynsym,
bb4d2ac2
L
1692 strtab, strtablen,
1693 symtab_index,
1694 psym,
1695 &sym_info,
1696 &vna_other);
1697
af3fc3bc 1698 printf (" ");
171191ba 1699
d8045f23
NC
1700 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1701 {
1702 const char * name;
1703 unsigned int len;
1704 unsigned int width = is_32bit_elf ? 8 : 14;
1705
1706 /* Relocations against GNU_IFUNC symbols do not use the value
1707 of the symbol as the address to relocate against. Instead
1708 they invoke the function named by the symbol and use its
1709 result as the address for relocation.
1710
1711 To indicate this to the user, do not display the value of
1712 the symbol in the "Symbols's Value" field. Instead show
1713 its name followed by () as a hint that the symbol is
1714 invoked. */
1715
1716 if (strtab == NULL
1717 || psym->st_name == 0
1718 || psym->st_name >= strtablen)
1719 name = "??";
1720 else
1721 name = strtab + psym->st_name;
1722
1723 len = print_symbol (width, name);
bb4d2ac2
L
1724 if (version_string)
1725 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1726 version_string);
d8045f23
NC
1727 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1728 }
1729 else
1730 {
1731 print_vma (psym->st_value, LONG_HEX);
171191ba 1732
d8045f23
NC
1733 printf (is_32bit_elf ? " " : " ");
1734 }
103f02d3 1735
af3fc3bc 1736 if (psym->st_name == 0)
f1ef08cb 1737 {
2cf0635d 1738 const char * sec_name = "<null>";
f1ef08cb
AM
1739 char name_buf[40];
1740
1741 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1742 {
dda8d76d 1743 if (psym->st_shndx < filedata->file_header.e_shnum)
b9e920ec
AM
1744 sec_name = SECTION_NAME_PRINT (filedata->section_headers
1745 + psym->st_shndx);
f1ef08cb
AM
1746 else if (psym->st_shndx == SHN_ABS)
1747 sec_name = "ABS";
1748 else if (psym->st_shndx == SHN_COMMON)
1749 sec_name = "COMMON";
dda8d76d 1750 else if ((filedata->file_header.e_machine == EM_MIPS
ac145307 1751 && psym->st_shndx == SHN_MIPS_SCOMMON)
dda8d76d 1752 || (filedata->file_header.e_machine == EM_TI_C6000
ac145307 1753 && psym->st_shndx == SHN_TIC6X_SCOMMON))
172553c7 1754 sec_name = "SCOMMON";
dda8d76d 1755 else if (filedata->file_header.e_machine == EM_MIPS
172553c7
TS
1756 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1757 sec_name = "SUNDEF";
dda8d76d
NC
1758 else if ((filedata->file_header.e_machine == EM_X86_64
1759 || filedata->file_header.e_machine == EM_L1OM
1760 || filedata->file_header.e_machine == EM_K1OM)
3b22753a
L
1761 && psym->st_shndx == SHN_X86_64_LCOMMON)
1762 sec_name = "LARGE_COMMON";
dda8d76d
NC
1763 else if (filedata->file_header.e_machine == EM_IA_64
1764 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
9ce701e2
L
1765 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1766 sec_name = "ANSI_COM";
dda8d76d 1767 else if (is_ia64_vms (filedata)
148b93f2
NC
1768 && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1769 sec_name = "VMS_SYMVEC";
f1ef08cb
AM
1770 else
1771 {
1772 sprintf (name_buf, "<section 0x%x>",
1773 (unsigned int) psym->st_shndx);
1774 sec_name = name_buf;
1775 }
1776 }
1777 print_symbol (22, sec_name);
1778 }
af3fc3bc 1779 else if (strtab == NULL)
d79b3d50 1780 printf (_("<string table index: %3ld>"), psym->st_name);
c256ffe7 1781 else if (psym->st_name >= strtablen)
32ec8896 1782 {
27a45f42
AS
1783 error (_("<corrupt string table index: %3ld>\n"),
1784 psym->st_name);
015dc7e1 1785 res = false;
32ec8896 1786 }
af3fc3bc 1787 else
bb4d2ac2
L
1788 {
1789 print_symbol (22, strtab + psym->st_name);
1790 if (version_string)
1791 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1792 version_string);
1793 }
103f02d3 1794
af3fc3bc 1795 if (is_rela)
171191ba 1796 {
7360e63f 1797 bfd_vma off = rels[i].r_addend;
171191ba 1798
7360e63f 1799 if ((bfd_signed_vma) off < 0)
598aaa76 1800 printf (" - %" BFD_VMA_FMT "x", - off);
171191ba 1801 else
598aaa76 1802 printf (" + %" BFD_VMA_FMT "x", off);
171191ba 1803 }
19936277 1804 }
252b5132 1805 }
1b228002 1806 else if (is_rela)
f7a99963 1807 {
7360e63f 1808 bfd_vma off = rels[i].r_addend;
e04d7088
L
1809
1810 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
7360e63f 1811 if ((bfd_signed_vma) off < 0)
e04d7088
L
1812 printf ("-%" BFD_VMA_FMT "x", - off);
1813 else
1814 printf ("%" BFD_VMA_FMT "x", off);
f7a99963 1815 }
252b5132 1816
dda8d76d 1817 if (filedata->file_header.e_machine == EM_SPARCV9
157c2599
NC
1818 && rtype != NULL
1819 && streq (rtype, "R_SPARC_OLO10"))
91d6fa6a 1820 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
351b4b40 1821
252b5132 1822 putchar ('\n');
2c71103e 1823
aca88567 1824#ifdef BFD64
dda8d76d 1825 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
2c71103e 1826 {
91d6fa6a
NC
1827 bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
1828 bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
2cf0635d
NC
1829 const char * rtype2 = elf_mips_reloc_type (type2);
1830 const char * rtype3 = elf_mips_reloc_type (type3);
aca88567 1831
2c71103e
NC
1832 printf (" Type2: ");
1833
1834 if (rtype2 == NULL)
39dbeff8
AM
1835 printf (_("unrecognized: %-7lx"),
1836 (unsigned long) type2 & 0xffffffff);
2c71103e
NC
1837 else
1838 printf ("%-17.17s", rtype2);
1839
18bd398b 1840 printf ("\n Type3: ");
2c71103e
NC
1841
1842 if (rtype3 == NULL)
39dbeff8
AM
1843 printf (_("unrecognized: %-7lx"),
1844 (unsigned long) type3 & 0xffffffff);
2c71103e
NC
1845 else
1846 printf ("%-17.17s", rtype3);
1847
53c7db4b 1848 putchar ('\n');
2c71103e 1849 }
aca88567 1850#endif /* BFD64 */
252b5132
RH
1851 }
1852
c8286bd1 1853 free (rels);
32ec8896
NC
1854
1855 return res;
252b5132
RH
1856}
1857
37c18eed
SD
1858static const char *
1859get_aarch64_dynamic_type (unsigned long type)
1860{
1861 switch (type)
1862 {
1863 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
1dbade74 1864 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
2301ed1c 1865 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
37c18eed
SD
1866 default:
1867 return NULL;
1868 }
1869}
1870
252b5132 1871static const char *
d3ba0551 1872get_mips_dynamic_type (unsigned long type)
252b5132
RH
1873{
1874 switch (type)
1875 {
1876 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1877 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1878 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1879 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1880 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1881 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1882 case DT_MIPS_MSYM: return "MIPS_MSYM";
1883 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1884 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1885 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1886 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1887 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1888 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1889 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1890 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1891 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1892 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
a5499fa4 1893 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
252b5132
RH
1894 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1895 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1896 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1897 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1898 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1899 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1900 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1901 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1902 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1903 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1904 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1905 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1906 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1907 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1908 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1909 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1910 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1911 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1912 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1913 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1914 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1915 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1916 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1917 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1918 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1919 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
861fb55a
DJ
1920 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
1921 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
f16a9783 1922 case DT_MIPS_XHASH: return "MIPS_XHASH";
252b5132
RH
1923 default:
1924 return NULL;
1925 }
1926}
1927
9a097730 1928static const char *
d3ba0551 1929get_sparc64_dynamic_type (unsigned long type)
9a097730
RH
1930{
1931 switch (type)
1932 {
1933 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1934 default:
1935 return NULL;
1936 }
103f02d3
UD
1937}
1938
7490d522
AM
1939static const char *
1940get_ppc_dynamic_type (unsigned long type)
1941{
1942 switch (type)
1943 {
a7f2871e 1944 case DT_PPC_GOT: return "PPC_GOT";
e8910a83 1945 case DT_PPC_OPT: return "PPC_OPT";
7490d522
AM
1946 default:
1947 return NULL;
1948 }
1949}
1950
f1cb7e17 1951static const char *
d3ba0551 1952get_ppc64_dynamic_type (unsigned long type)
f1cb7e17
AM
1953{
1954 switch (type)
1955 {
a7f2871e
AM
1956 case DT_PPC64_GLINK: return "PPC64_GLINK";
1957 case DT_PPC64_OPD: return "PPC64_OPD";
1958 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
e8910a83 1959 case DT_PPC64_OPT: return "PPC64_OPT";
f1cb7e17
AM
1960 default:
1961 return NULL;
1962 }
1963}
1964
103f02d3 1965static const char *
d3ba0551 1966get_parisc_dynamic_type (unsigned long type)
103f02d3
UD
1967{
1968 switch (type)
1969 {
1970 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
1971 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
1972 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
1973 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
1974 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
1975 case DT_HP_PREINIT: return "HP_PREINIT";
1976 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
1977 case DT_HP_NEEDED: return "HP_NEEDED";
1978 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
1979 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
1980 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
1981 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
1982 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
eec8f817
DA
1983 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
1984 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
1985 case DT_HP_FILTERED: return "HP_FILTERED";
1986 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
1987 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
1988 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
1989 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
1990 case DT_PLT: return "PLT";
1991 case DT_PLT_SIZE: return "PLT_SIZE";
1992 case DT_DLT: return "DLT";
1993 case DT_DLT_SIZE: return "DLT_SIZE";
103f02d3
UD
1994 default:
1995 return NULL;
1996 }
1997}
9a097730 1998
ecc51f48 1999static const char *
d3ba0551 2000get_ia64_dynamic_type (unsigned long type)
ecc51f48
NC
2001{
2002 switch (type)
2003 {
148b93f2
NC
2004 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2005 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2006 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2007 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2008 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2009 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2010 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2011 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2012 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2013 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2014 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2015 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2016 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2017 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2018 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2019 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2020 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2021 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2022 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2023 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2024 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2025 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2026 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2027 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2028 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2029 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2030 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2031 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2032 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2033 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2034 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
ecc51f48
NC
2035 default:
2036 return NULL;
2037 }
2038}
2039
fd85a6a1
NC
2040static const char *
2041get_solaris_section_type (unsigned long type)
2042{
2043 switch (type)
2044 {
2045 case 0x6fffffee: return "SUNW_ancillary";
2046 case 0x6fffffef: return "SUNW_capchain";
2047 case 0x6ffffff0: return "SUNW_capinfo";
2048 case 0x6ffffff1: return "SUNW_symsort";
2049 case 0x6ffffff2: return "SUNW_tlssort";
2050 case 0x6ffffff3: return "SUNW_LDYNSYM";
2051 case 0x6ffffff4: return "SUNW_dof";
2052 case 0x6ffffff5: return "SUNW_cap";
2053 case 0x6ffffff6: return "SUNW_SIGNATURE";
2054 case 0x6ffffff7: return "SUNW_ANNOTATE";
2055 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2056 case 0x6ffffff9: return "SUNW_DEBUG";
2057 case 0x6ffffffa: return "SUNW_move";
2058 case 0x6ffffffb: return "SUNW_COMDAT";
2059 case 0x6ffffffc: return "SUNW_syminfo";
2060 case 0x6ffffffd: return "SUNW_verdef";
2061 case 0x6ffffffe: return "SUNW_verneed";
2062 case 0x6fffffff: return "SUNW_versym";
2063 case 0x70000000: return "SPARC_GOTDATA";
2064 default: return NULL;
2065 }
2066}
2067
fabcb361
RH
2068static const char *
2069get_alpha_dynamic_type (unsigned long type)
2070{
2071 switch (type)
2072 {
2073 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
32ec8896 2074 default: return NULL;
fabcb361
RH
2075 }
2076}
2077
1c0d3aa6
NC
2078static const char *
2079get_score_dynamic_type (unsigned long type)
2080{
2081 switch (type)
2082 {
2083 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2084 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2085 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2086 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2087 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2088 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
32ec8896 2089 default: return NULL;
1c0d3aa6
NC
2090 }
2091}
2092
40b36596
JM
2093static const char *
2094get_tic6x_dynamic_type (unsigned long type)
2095{
2096 switch (type)
2097 {
2098 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2099 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2100 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2101 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2102 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2103 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
32ec8896 2104 default: return NULL;
40b36596
JM
2105 }
2106}
1c0d3aa6 2107
36591ba1
SL
2108static const char *
2109get_nios2_dynamic_type (unsigned long type)
2110{
2111 switch (type)
2112 {
2113 case DT_NIOS2_GP: return "NIOS2_GP";
32ec8896 2114 default: return NULL;
36591ba1
SL
2115 }
2116}
2117
fd85a6a1
NC
2118static const char *
2119get_solaris_dynamic_type (unsigned long type)
2120{
2121 switch (type)
2122 {
2123 case 0x6000000d: return "SUNW_AUXILIARY";
2124 case 0x6000000e: return "SUNW_RTLDINF";
2125 case 0x6000000f: return "SUNW_FILTER";
2126 case 0x60000010: return "SUNW_CAP";
2127 case 0x60000011: return "SUNW_SYMTAB";
2128 case 0x60000012: return "SUNW_SYMSZ";
2129 case 0x60000013: return "SUNW_SORTENT";
2130 case 0x60000014: return "SUNW_SYMSORT";
2131 case 0x60000015: return "SUNW_SYMSORTSZ";
2132 case 0x60000016: return "SUNW_TLSSORT";
2133 case 0x60000017: return "SUNW_TLSSORTSZ";
2134 case 0x60000018: return "SUNW_CAPINFO";
2135 case 0x60000019: return "SUNW_STRPAD";
2136 case 0x6000001a: return "SUNW_CAPCHAIN";
2137 case 0x6000001b: return "SUNW_LDMACH";
2138 case 0x6000001d: return "SUNW_CAPCHAINENT";
2139 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2140 case 0x60000021: return "SUNW_PARENT";
2141 case 0x60000023: return "SUNW_ASLR";
2142 case 0x60000025: return "SUNW_RELAX";
2143 case 0x60000029: return "SUNW_NXHEAP";
2144 case 0x6000002b: return "SUNW_NXSTACK";
2145
2146 case 0x70000001: return "SPARC_REGISTER";
2147 case 0x7ffffffd: return "AUXILIARY";
2148 case 0x7ffffffe: return "USED";
2149 case 0x7fffffff: return "FILTER";
2150
15f205b1 2151 default: return NULL;
fd85a6a1
NC
2152 }
2153}
2154
252b5132 2155static const char *
dda8d76d 2156get_dynamic_type (Filedata * filedata, unsigned long type)
252b5132 2157{
e9e44622 2158 static char buff[64];
252b5132
RH
2159
2160 switch (type)
2161 {
2162 case DT_NULL: return "NULL";
2163 case DT_NEEDED: return "NEEDED";
2164 case DT_PLTRELSZ: return "PLTRELSZ";
2165 case DT_PLTGOT: return "PLTGOT";
2166 case DT_HASH: return "HASH";
2167 case DT_STRTAB: return "STRTAB";
2168 case DT_SYMTAB: return "SYMTAB";
2169 case DT_RELA: return "RELA";
2170 case DT_RELASZ: return "RELASZ";
2171 case DT_RELAENT: return "RELAENT";
2172 case DT_STRSZ: return "STRSZ";
2173 case DT_SYMENT: return "SYMENT";
2174 case DT_INIT: return "INIT";
2175 case DT_FINI: return "FINI";
2176 case DT_SONAME: return "SONAME";
2177 case DT_RPATH: return "RPATH";
2178 case DT_SYMBOLIC: return "SYMBOLIC";
2179 case DT_REL: return "REL";
2180 case DT_RELSZ: return "RELSZ";
2181 case DT_RELENT: return "RELENT";
2182 case DT_PLTREL: return "PLTREL";
2183 case DT_DEBUG: return "DEBUG";
2184 case DT_TEXTREL: return "TEXTREL";
2185 case DT_JMPREL: return "JMPREL";
2186 case DT_BIND_NOW: return "BIND_NOW";
2187 case DT_INIT_ARRAY: return "INIT_ARRAY";
2188 case DT_FINI_ARRAY: return "FINI_ARRAY";
2189 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2190 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
d1133906
NC
2191 case DT_RUNPATH: return "RUNPATH";
2192 case DT_FLAGS: return "FLAGS";
2d0e6f43 2193
d1133906
NC
2194 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2195 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
6d913794 2196 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
103f02d3 2197
05107a46 2198 case DT_CHECKSUM: return "CHECKSUM";
252b5132
RH
2199 case DT_PLTPADSZ: return "PLTPADSZ";
2200 case DT_MOVEENT: return "MOVEENT";
2201 case DT_MOVESZ: return "MOVESZ";
dcefbbbd 2202 case DT_FEATURE: return "FEATURE";
252b5132
RH
2203 case DT_POSFLAG_1: return "POSFLAG_1";
2204 case DT_SYMINSZ: return "SYMINSZ";
2205 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
103f02d3 2206
252b5132 2207 case DT_ADDRRNGLO: return "ADDRRNGLO";
dcefbbbd
L
2208 case DT_CONFIG: return "CONFIG";
2209 case DT_DEPAUDIT: return "DEPAUDIT";
2210 case DT_AUDIT: return "AUDIT";
2211 case DT_PLTPAD: return "PLTPAD";
2212 case DT_MOVETAB: return "MOVETAB";
252b5132 2213 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
103f02d3 2214
252b5132 2215 case DT_VERSYM: return "VERSYM";
103f02d3 2216
67a4f2b7
AO
2217 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2218 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
252b5132
RH
2219 case DT_RELACOUNT: return "RELACOUNT";
2220 case DT_RELCOUNT: return "RELCOUNT";
2221 case DT_FLAGS_1: return "FLAGS_1";
2222 case DT_VERDEF: return "VERDEF";
2223 case DT_VERDEFNUM: return "VERDEFNUM";
2224 case DT_VERNEED: return "VERNEED";
2225 case DT_VERNEEDNUM: return "VERNEEDNUM";
103f02d3 2226
019148e4 2227 case DT_AUXILIARY: return "AUXILIARY";
252b5132
RH
2228 case DT_USED: return "USED";
2229 case DT_FILTER: return "FILTER";
103f02d3 2230
047b2264
JJ
2231 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2232 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2233 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2234 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2235 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
fdc90cb4 2236 case DT_GNU_HASH: return "GNU_HASH";
a5da3dee 2237 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
047b2264 2238
252b5132
RH
2239 default:
2240 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2241 {
2cf0635d 2242 const char * result;
103f02d3 2243
dda8d76d 2244 switch (filedata->file_header.e_machine)
252b5132 2245 {
37c18eed
SD
2246 case EM_AARCH64:
2247 result = get_aarch64_dynamic_type (type);
2248 break;
252b5132 2249 case EM_MIPS:
4fe85591 2250 case EM_MIPS_RS3_LE:
252b5132
RH
2251 result = get_mips_dynamic_type (type);
2252 break;
9a097730
RH
2253 case EM_SPARCV9:
2254 result = get_sparc64_dynamic_type (type);
2255 break;
7490d522
AM
2256 case EM_PPC:
2257 result = get_ppc_dynamic_type (type);
2258 break;
f1cb7e17
AM
2259 case EM_PPC64:
2260 result = get_ppc64_dynamic_type (type);
2261 break;
ecc51f48
NC
2262 case EM_IA_64:
2263 result = get_ia64_dynamic_type (type);
2264 break;
fabcb361
RH
2265 case EM_ALPHA:
2266 result = get_alpha_dynamic_type (type);
2267 break;
1c0d3aa6
NC
2268 case EM_SCORE:
2269 result = get_score_dynamic_type (type);
2270 break;
40b36596
JM
2271 case EM_TI_C6000:
2272 result = get_tic6x_dynamic_type (type);
2273 break;
36591ba1
SL
2274 case EM_ALTERA_NIOS2:
2275 result = get_nios2_dynamic_type (type);
2276 break;
252b5132 2277 default:
dda8d76d 2278 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
fd85a6a1
NC
2279 result = get_solaris_dynamic_type (type);
2280 else
2281 result = NULL;
252b5132
RH
2282 break;
2283 }
2284
2285 if (result != NULL)
2286 return result;
2287
e9e44622 2288 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
252b5132 2289 }
eec8f817 2290 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
dda8d76d 2291 || (filedata->file_header.e_machine == EM_PARISC
eec8f817 2292 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
103f02d3 2293 {
2cf0635d 2294 const char * result;
103f02d3 2295
dda8d76d 2296 switch (filedata->file_header.e_machine)
103f02d3
UD
2297 {
2298 case EM_PARISC:
2299 result = get_parisc_dynamic_type (type);
2300 break;
148b93f2
NC
2301 case EM_IA_64:
2302 result = get_ia64_dynamic_type (type);
2303 break;
103f02d3 2304 default:
dda8d76d 2305 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
fd85a6a1
NC
2306 result = get_solaris_dynamic_type (type);
2307 else
2308 result = NULL;
103f02d3
UD
2309 break;
2310 }
2311
2312 if (result != NULL)
2313 return result;
2314
e9e44622
JJ
2315 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2316 type);
103f02d3 2317 }
252b5132 2318 else
e9e44622 2319 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
103f02d3 2320
252b5132
RH
2321 return buff;
2322 }
2323}
2324
2325static char *
d3ba0551 2326get_file_type (unsigned e_type)
252b5132 2327{
89246a0e 2328 static char buff[64];
252b5132
RH
2329
2330 switch (e_type)
2331 {
32ec8896
NC
2332 case ET_NONE: return _("NONE (None)");
2333 case ET_REL: return _("REL (Relocatable file)");
2334 case ET_EXEC: return _("EXEC (Executable file)");
2335 case ET_DYN: return _("DYN (Shared object file)");
2336 case ET_CORE: return _("CORE (Core file)");
252b5132
RH
2337
2338 default:
2339 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
e9e44622 2340 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
252b5132 2341 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
e9e44622 2342 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
252b5132 2343 else
e9e44622 2344 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
252b5132
RH
2345 return buff;
2346 }
2347}
2348
2349static char *
d3ba0551 2350get_machine_name (unsigned e_machine)
252b5132 2351{
b34976b6 2352 static char buff[64]; /* XXX */
252b5132
RH
2353
2354 switch (e_machine)
2355 {
55e22ca8
NC
2356 /* Please keep this switch table sorted by increasing EM_ value. */
2357 /* 0 */
c45021f2
NC
2358 case EM_NONE: return _("None");
2359 case EM_M32: return "WE32100";
2360 case EM_SPARC: return "Sparc";
2361 case EM_386: return "Intel 80386";
2362 case EM_68K: return "MC68000";
2363 case EM_88K: return "MC88000";
22abe556 2364 case EM_IAMCU: return "Intel MCU";
fb70ec17 2365 case EM_860: return "Intel 80860";
c45021f2
NC
2366 case EM_MIPS: return "MIPS R3000";
2367 case EM_S370: return "IBM System/370";
55e22ca8 2368 /* 10 */
7036c0e1 2369 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
252b5132 2370 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
c45021f2 2371 case EM_PARISC: return "HPPA";
55e22ca8 2372 case EM_VPP550: return "Fujitsu VPP500";
7036c0e1 2373 case EM_SPARC32PLUS: return "Sparc v8+" ;
d7867d17 2374 case EM_960: return "Intel 80960";
c45021f2 2375 case EM_PPC: return "PowerPC";
55e22ca8 2376 /* 20 */
285d1771 2377 case EM_PPC64: return "PowerPC64";
55e22ca8
NC
2378 case EM_S390_OLD:
2379 case EM_S390: return "IBM S/390";
2380 case EM_SPU: return "SPU";
2381 /* 30 */
2382 case EM_V800: return "Renesas V850 (using RH850 ABI)";
c45021f2
NC
2383 case EM_FR20: return "Fujitsu FR20";
2384 case EM_RH32: return "TRW RH32";
b34976b6 2385 case EM_MCORE: return "MCORE";
55e22ca8 2386 /* 40 */
7036c0e1
AJ
2387 case EM_ARM: return "ARM";
2388 case EM_OLD_ALPHA: return "Digital Alpha (old)";
ef230218 2389 case EM_SH: return "Renesas / SuperH SH";
c45021f2
NC
2390 case EM_SPARCV9: return "Sparc v9";
2391 case EM_TRICORE: return "Siemens Tricore";
584da044 2392 case EM_ARC: return "ARC";
c2dcd04e
NC
2393 case EM_H8_300: return "Renesas H8/300";
2394 case EM_H8_300H: return "Renesas H8/300H";
2395 case EM_H8S: return "Renesas H8S";
2396 case EM_H8_500: return "Renesas H8/500";
55e22ca8 2397 /* 50 */
30800947 2398 case EM_IA_64: return "Intel IA-64";
252b5132
RH
2399 case EM_MIPS_X: return "Stanford MIPS-X";
2400 case EM_COLDFIRE: return "Motorola Coldfire";
55e22ca8 2401 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
7036c0e1
AJ
2402 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2403 case EM_PCP: return "Siemens PCP";
2404 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2405 case EM_NDR1: return "Denso NDR1 microprocesspr";
2406 case EM_STARCORE: return "Motorola Star*Core processor";
2407 case EM_ME16: return "Toyota ME16 processor";
55e22ca8 2408 /* 60 */
7036c0e1
AJ
2409 case EM_ST100: return "STMicroelectronics ST100 processor";
2410 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
55e22ca8 2411 case EM_X86_64: return "Advanced Micro Devices X86-64";
11636f9e
JM
2412 case EM_PDSP: return "Sony DSP processor";
2413 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2414 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
7036c0e1
AJ
2415 case EM_FX66: return "Siemens FX66 microcontroller";
2416 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2417 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2418 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
55e22ca8 2419 /* 70 */
7036c0e1
AJ
2420 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2421 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2422 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2423 case EM_SVX: return "Silicon Graphics SVx";
2424 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2425 case EM_VAX: return "Digital VAX";
1b61cf92 2426 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
c45021f2
NC
2427 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2428 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2429 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
55e22ca8 2430 /* 80 */
b34976b6 2431 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
c45021f2 2432 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
3b36097d 2433 case EM_PRISM: return "Vitesse Prism";
55e22ca8
NC
2434 case EM_AVR_OLD:
2435 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2436 case EM_CYGNUS_FR30:
2437 case EM_FR30: return "Fujitsu FR30";
2438 case EM_CYGNUS_D10V:
2439 case EM_D10V: return "d10v";
2440 case EM_CYGNUS_D30V:
2441 case EM_D30V: return "d30v";
2442 case EM_CYGNUS_V850:
2443 case EM_V850: return "Renesas V850";
2444 case EM_CYGNUS_M32R:
2445 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2446 case EM_CYGNUS_MN10300:
2447 case EM_MN10300: return "mn10300";
2448 /* 90 */
2449 case EM_CYGNUS_MN10200:
2450 case EM_MN10200: return "mn10200";
2451 case EM_PJ: return "picoJava";
73589c9d 2452 case EM_OR1K: return "OpenRISC 1000";
55e22ca8 2453 case EM_ARC_COMPACT: return "ARCompact";
88da6820
NC
2454 case EM_XTENSA_OLD:
2455 case EM_XTENSA: return "Tensilica Xtensa Processor";
11636f9e
JM
2456 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2457 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2458 case EM_NS32K: return "National Semiconductor 32000 series";
2459 case EM_TPC: return "Tenor Network TPC processor";
55e22ca8
NC
2460 case EM_SNP1K: return "Trebia SNP 1000 processor";
2461 /* 100 */
9abca702 2462 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
55e22ca8
NC
2463 case EM_IP2K_OLD:
2464 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
11636f9e
JM
2465 case EM_MAX: return "MAX Processor";
2466 case EM_CR: return "National Semiconductor CompactRISC";
2467 case EM_F2MC16: return "Fujitsu F2MC16";
2468 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
7bbe5bc5 2469 case EM_BLACKFIN: return "Analog Devices Blackfin";
11636f9e
JM
2470 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2471 case EM_SEP: return "Sharp embedded microprocessor";
2472 case EM_ARCA: return "Arca RISC microprocessor";
55e22ca8 2473 /* 110 */
11636f9e
JM
2474 case EM_UNICORE: return "Unicore";
2475 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2476 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
64fd6348 2477 case EM_ALTERA_NIOS2: return "Altera Nios II";
55e22ca8
NC
2478 case EM_CRX: return "National Semiconductor CRX microprocessor";
2479 case EM_XGATE: return "Motorola XGATE embedded processor";
c29aca4a 2480 case EM_C166:
d70c5fc7 2481 case EM_XC16X: return "Infineon Technologies xc16x";
11636f9e
JM
2482 case EM_M16C: return "Renesas M16C series microprocessors";
2483 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2484 case EM_CE: return "Freescale Communication Engine RISC core";
55e22ca8
NC
2485 /* 120 */
2486 case EM_M32C: return "Renesas M32c";
2487 /* 130 */
11636f9e
JM
2488 case EM_TSK3000: return "Altium TSK3000 core";
2489 case EM_RS08: return "Freescale RS08 embedded processor";
2490 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
55e22ca8 2491 case EM_SCORE: return "SUNPLUS S+Core";
11636f9e
JM
2492 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2493 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
55e22ca8 2494 case EM_LATTICEMICO32: return "Lattice Mico32";
11636f9e 2495 case EM_SE_C17: return "Seiko Epson C17 family";
55e22ca8 2496 /* 140 */
11636f9e
JM
2497 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2498 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2499 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
55e22ca8
NC
2500 case EM_TI_PRU: return "TI PRU I/O processor";
2501 /* 160 */
11636f9e
JM
2502 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2503 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2504 case EM_R32C: return "Renesas R32C series microprocessors";
2505 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2506 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2507 case EM_8051: return "Intel 8051 and variants";
2508 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2509 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2510 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2511 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
55e22ca8 2512 /* 170 */
11636f9e
JM
2513 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2514 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2515 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
c7927a3c 2516 case EM_RX: return "Renesas RX";
a3c62988 2517 case EM_METAG: return "Imagination Technologies Meta processor architecture";
11636f9e
JM
2518 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2519 case EM_ECOG16: return "Cyan Technology eCOG16 family";
55e22ca8
NC
2520 case EM_CR16:
2521 case EM_MICROBLAZE:
2522 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
11636f9e
JM
2523 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2524 case EM_SLE9X: return "Infineon Technologies SLE9X core";
55e22ca8
NC
2525 /* 180 */
2526 case EM_L1OM: return "Intel L1OM";
2527 case EM_K1OM: return "Intel K1OM";
2528 case EM_INTEL182: return "Intel (reserved)";
2529 case EM_AARCH64: return "AArch64";
2530 case EM_ARM184: return "ARM (reserved)";
2531 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
11636f9e
JM
2532 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
2533 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
2534 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
55e22ca8 2535 /* 190 */
11636f9e 2536 case EM_CUDA: return "NVIDIA CUDA architecture";
55e22ca8 2537 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
6d913794
NC
2538 case EM_CLOUDSHIELD: return "CloudShield architecture family";
2539 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
2540 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
55e22ca8 2541 case EM_ARC_COMPACT2: return "ARCv2";
6d913794 2542 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
55e22ca8 2543 case EM_RL78: return "Renesas RL78";
6d913794 2544 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
55e22ca8
NC
2545 case EM_78K0R: return "Renesas 78K0R";
2546 /* 200 */
6d913794 2547 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
15f205b1
NC
2548 case EM_BA1: return "Beyond BA1 CPU architecture";
2549 case EM_BA2: return "Beyond BA2 CPU architecture";
6d913794
NC
2550 case EM_XCORE: return "XMOS xCORE processor family";
2551 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
7b9f9859 2552 case EM_INTELGT: return "Intel Graphics Technology";
55e22ca8 2553 /* 210 */
6d913794
NC
2554 case EM_KM32: return "KM211 KM32 32-bit processor";
2555 case EM_KMX32: return "KM211 KMX32 32-bit processor";
2556 case EM_KMX16: return "KM211 KMX16 16-bit processor";
2557 case EM_KMX8: return "KM211 KMX8 8-bit processor";
2558 case EM_KVARC: return "KM211 KVARC processor";
15f205b1 2559 case EM_CDP: return "Paneve CDP architecture family";
6d913794
NC
2560 case EM_COGE: return "Cognitive Smart Memory Processor";
2561 case EM_COOL: return "Bluechip Systems CoolEngine";
2562 case EM_NORC: return "Nanoradio Optimized RISC";
2563 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
55e22ca8 2564 /* 220 */
15f205b1 2565 case EM_Z80: return "Zilog Z80";
55e22ca8
NC
2566 case EM_VISIUM: return "CDS VISIUMcore processor";
2567 case EM_FT32: return "FTDI Chip FT32";
2568 case EM_MOXIE: return "Moxie";
2569 case EM_AMDGPU: return "AMD GPU";
4cf2ad72
CC
2570 /* 230 (all reserved) */
2571 /* 240 */
55e22ca8
NC
2572 case EM_RISCV: return "RISC-V";
2573 case EM_LANAI: return "Lanai 32-bit processor";
4cf2ad72
CC
2574 case EM_CEVA: return "CEVA Processor Architecture Family";
2575 case EM_CEVA_X2: return "CEVA X2 Processor Family";
55e22ca8 2576 case EM_BPF: return "Linux BPF";
4cf2ad72
CC
2577 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
2578 case EM_IMG1: return "Imagination Technologies";
2579 /* 250 */
fe944acf 2580 case EM_NFP: return "Netronome Flow Processor";
4cf2ad72
CC
2581 case EM_VE: return "NEC Vector Engine";
2582 case EM_CSKY: return "C-SKY";
2583 case EM_ARC_COMPACT3_64: return "Synopsys ARCv2.3 64-bit";
2584 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
2585 case EM_ARC_COMPACT3: return "Synopsys ARCv2.3 32-bit";
2586 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
2587 case EM_65816: return "WDC 65816/65C816";
2588 case EM_LOONGARCH: return "Loongson Loongarch";
2589 case EM_KF32: return "ChipON KungFu32";
55e22ca8
NC
2590
2591 /* Large numbers... */
2592 case EM_MT: return "Morpho Techologies MT processor";
2593 case EM_ALPHA: return "Alpha";
2594 case EM_WEBASSEMBLY: return "Web Assembly";
9abca702 2595 case EM_DLX: return "OpenDLX";
55e22ca8
NC
2596 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
2597 case EM_IQ2000: return "Vitesse IQ2000";
2598 case EM_M32C_OLD:
2599 case EM_NIOS32: return "Altera Nios";
2600 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
2601 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
2602 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
637b1970 2603 case EM_S12Z: return "Freescale S12Z";
55e22ca8 2604
252b5132 2605 default:
35d9dd2f 2606 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
252b5132
RH
2607 return buff;
2608 }
2609}
2610
a9522a21
AB
2611static void
2612decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
2613{
2614 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
6987d5a1 2615 other compilers don't specify an architecture type in the e_flags, and
a9522a21
AB
2616 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
2617 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
2618 architectures.
2619
2620 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
2621 but also sets a specific architecture type in the e_flags field.
2622
2623 However, when decoding the flags we don't worry if we see an
2624 unexpected pairing, for example EM_ARC_COMPACT machine type, with
2625 ARCEM architecture type. */
2626
2627 switch (e_flags & EF_ARC_MACH_MSK)
2628 {
2629 /* We only expect these to occur for EM_ARC_COMPACT2. */
2630 case EF_ARC_CPU_ARCV2EM:
2631 strcat (buf, ", ARC EM");
2632 break;
2633 case EF_ARC_CPU_ARCV2HS:
2634 strcat (buf, ", ARC HS");
2635 break;
2636
2637 /* We only expect these to occur for EM_ARC_COMPACT. */
2638 case E_ARC_MACH_ARC600:
2639 strcat (buf, ", ARC600");
2640 break;
2641 case E_ARC_MACH_ARC601:
2642 strcat (buf, ", ARC601");
2643 break;
2644 case E_ARC_MACH_ARC700:
2645 strcat (buf, ", ARC700");
2646 break;
2647
2648 /* The only times we should end up here are (a) A corrupt ELF, (b) A
2649 new ELF with new architecture being read by an old version of
2650 readelf, or (c) An ELF built with non-GNU compiler that does not
2651 set the architecture in the e_flags. */
2652 default:
2653 if (e_machine == EM_ARC_COMPACT)
2654 strcat (buf, ", Unknown ARCompact");
2655 else
2656 strcat (buf, ", Unknown ARC");
2657 break;
2658 }
2659
2660 switch (e_flags & EF_ARC_OSABI_MSK)
2661 {
2662 case E_ARC_OSABI_ORIG:
2663 strcat (buf, ", (ABI:legacy)");
2664 break;
2665 case E_ARC_OSABI_V2:
2666 strcat (buf, ", (ABI:v2)");
2667 break;
2668 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
2669 case E_ARC_OSABI_V3:
2670 strcat (buf, ", v3 no-legacy-syscalls ABI");
2671 break;
53a346d8
CZ
2672 case E_ARC_OSABI_V4:
2673 strcat (buf, ", v4 ABI");
2674 break;
a9522a21
AB
2675 default:
2676 strcat (buf, ", unrecognised ARC OSABI flag");
2677 break;
2678 }
2679}
2680
f3485b74 2681static void
d3ba0551 2682decode_ARM_machine_flags (unsigned e_flags, char buf[])
f3485b74
NC
2683{
2684 unsigned eabi;
015dc7e1 2685 bool unknown = false;
f3485b74
NC
2686
2687 eabi = EF_ARM_EABI_VERSION (e_flags);
2688 e_flags &= ~ EF_ARM_EABIMASK;
2689
2690 /* Handle "generic" ARM flags. */
2691 if (e_flags & EF_ARM_RELEXEC)
2692 {
2693 strcat (buf, ", relocatable executable");
2694 e_flags &= ~ EF_ARM_RELEXEC;
2695 }
76da6bbe 2696
18a20338
CL
2697 if (e_flags & EF_ARM_PIC)
2698 {
2699 strcat (buf, ", position independent");
2700 e_flags &= ~ EF_ARM_PIC;
2701 }
2702
f3485b74
NC
2703 /* Now handle EABI specific flags. */
2704 switch (eabi)
2705 {
2706 default:
2c71103e 2707 strcat (buf, ", <unrecognized EABI>");
f3485b74 2708 if (e_flags)
015dc7e1 2709 unknown = true;
f3485b74
NC
2710 break;
2711
2712 case EF_ARM_EABI_VER1:
a5bcd848 2713 strcat (buf, ", Version1 EABI");
f3485b74
NC
2714 while (e_flags)
2715 {
2716 unsigned flag;
76da6bbe 2717
f3485b74
NC
2718 /* Process flags one bit at a time. */
2719 flag = e_flags & - e_flags;
2720 e_flags &= ~ flag;
76da6bbe 2721
f3485b74
NC
2722 switch (flag)
2723 {
a5bcd848 2724 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
f3485b74
NC
2725 strcat (buf, ", sorted symbol tables");
2726 break;
76da6bbe 2727
f3485b74 2728 default:
015dc7e1 2729 unknown = true;
f3485b74
NC
2730 break;
2731 }
2732 }
2733 break;
76da6bbe 2734
a5bcd848
PB
2735 case EF_ARM_EABI_VER2:
2736 strcat (buf, ", Version2 EABI");
2737 while (e_flags)
2738 {
2739 unsigned flag;
2740
2741 /* Process flags one bit at a time. */
2742 flag = e_flags & - e_flags;
2743 e_flags &= ~ flag;
2744
2745 switch (flag)
2746 {
2747 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
2748 strcat (buf, ", sorted symbol tables");
2749 break;
2750
2751 case EF_ARM_DYNSYMSUSESEGIDX:
2752 strcat (buf, ", dynamic symbols use segment index");
2753 break;
2754
2755 case EF_ARM_MAPSYMSFIRST:
2756 strcat (buf, ", mapping symbols precede others");
2757 break;
2758
2759 default:
015dc7e1 2760 unknown = true;
a5bcd848
PB
2761 break;
2762 }
2763 }
2764 break;
2765
d507cf36
PB
2766 case EF_ARM_EABI_VER3:
2767 strcat (buf, ", Version3 EABI");
8cb51566
PB
2768 break;
2769
2770 case EF_ARM_EABI_VER4:
2771 strcat (buf, ", Version4 EABI");
3bfcb652
NC
2772 while (e_flags)
2773 {
2774 unsigned flag;
2775
2776 /* Process flags one bit at a time. */
2777 flag = e_flags & - e_flags;
2778 e_flags &= ~ flag;
2779
2780 switch (flag)
2781 {
2782 case EF_ARM_BE8:
2783 strcat (buf, ", BE8");
2784 break;
2785
2786 case EF_ARM_LE8:
2787 strcat (buf, ", LE8");
2788 break;
2789
2790 default:
015dc7e1 2791 unknown = true;
3bfcb652
NC
2792 break;
2793 }
3bfcb652
NC
2794 }
2795 break;
3a4a14e9
PB
2796
2797 case EF_ARM_EABI_VER5:
2798 strcat (buf, ", Version5 EABI");
d507cf36
PB
2799 while (e_flags)
2800 {
2801 unsigned flag;
2802
2803 /* Process flags one bit at a time. */
2804 flag = e_flags & - e_flags;
2805 e_flags &= ~ flag;
2806
2807 switch (flag)
2808 {
2809 case EF_ARM_BE8:
2810 strcat (buf, ", BE8");
2811 break;
2812
2813 case EF_ARM_LE8:
2814 strcat (buf, ", LE8");
2815 break;
2816
3bfcb652
NC
2817 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
2818 strcat (buf, ", soft-float ABI");
2819 break;
2820
2821 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
2822 strcat (buf, ", hard-float ABI");
2823 break;
2824
d507cf36 2825 default:
015dc7e1 2826 unknown = true;
d507cf36
PB
2827 break;
2828 }
2829 }
2830 break;
2831
f3485b74 2832 case EF_ARM_EABI_UNKNOWN:
a5bcd848 2833 strcat (buf, ", GNU EABI");
f3485b74
NC
2834 while (e_flags)
2835 {
2836 unsigned flag;
76da6bbe 2837
f3485b74
NC
2838 /* Process flags one bit at a time. */
2839 flag = e_flags & - e_flags;
2840 e_flags &= ~ flag;
76da6bbe 2841
f3485b74
NC
2842 switch (flag)
2843 {
a5bcd848 2844 case EF_ARM_INTERWORK:
f3485b74
NC
2845 strcat (buf, ", interworking enabled");
2846 break;
76da6bbe 2847
a5bcd848 2848 case EF_ARM_APCS_26:
f3485b74
NC
2849 strcat (buf, ", uses APCS/26");
2850 break;
76da6bbe 2851
a5bcd848 2852 case EF_ARM_APCS_FLOAT:
f3485b74
NC
2853 strcat (buf, ", uses APCS/float");
2854 break;
76da6bbe 2855
a5bcd848 2856 case EF_ARM_PIC:
f3485b74
NC
2857 strcat (buf, ", position independent");
2858 break;
76da6bbe 2859
a5bcd848 2860 case EF_ARM_ALIGN8:
f3485b74
NC
2861 strcat (buf, ", 8 bit structure alignment");
2862 break;
76da6bbe 2863
a5bcd848 2864 case EF_ARM_NEW_ABI:
f3485b74
NC
2865 strcat (buf, ", uses new ABI");
2866 break;
76da6bbe 2867
a5bcd848 2868 case EF_ARM_OLD_ABI:
f3485b74
NC
2869 strcat (buf, ", uses old ABI");
2870 break;
76da6bbe 2871
a5bcd848 2872 case EF_ARM_SOFT_FLOAT:
f3485b74
NC
2873 strcat (buf, ", software FP");
2874 break;
76da6bbe 2875
90e01f86
ILT
2876 case EF_ARM_VFP_FLOAT:
2877 strcat (buf, ", VFP");
2878 break;
2879
fde78edd
NC
2880 case EF_ARM_MAVERICK_FLOAT:
2881 strcat (buf, ", Maverick FP");
2882 break;
2883
f3485b74 2884 default:
015dc7e1 2885 unknown = true;
f3485b74
NC
2886 break;
2887 }
2888 }
2889 }
f3485b74
NC
2890
2891 if (unknown)
2b692964 2892 strcat (buf,_(", <unknown>"));
f3485b74
NC
2893}
2894
343433df
AB
2895static void
2896decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
2897{
2898 --size; /* Leave space for null terminator. */
2899
2900 switch (e_flags & EF_AVR_MACH)
2901 {
2902 case E_AVR_MACH_AVR1:
2903 strncat (buf, ", avr:1", size);
2904 break;
2905 case E_AVR_MACH_AVR2:
2906 strncat (buf, ", avr:2", size);
2907 break;
2908 case E_AVR_MACH_AVR25:
2909 strncat (buf, ", avr:25", size);
2910 break;
2911 case E_AVR_MACH_AVR3:
2912 strncat (buf, ", avr:3", size);
2913 break;
2914 case E_AVR_MACH_AVR31:
2915 strncat (buf, ", avr:31", size);
2916 break;
2917 case E_AVR_MACH_AVR35:
2918 strncat (buf, ", avr:35", size);
2919 break;
2920 case E_AVR_MACH_AVR4:
2921 strncat (buf, ", avr:4", size);
2922 break;
2923 case E_AVR_MACH_AVR5:
2924 strncat (buf, ", avr:5", size);
2925 break;
2926 case E_AVR_MACH_AVR51:
2927 strncat (buf, ", avr:51", size);
2928 break;
2929 case E_AVR_MACH_AVR6:
2930 strncat (buf, ", avr:6", size);
2931 break;
2932 case E_AVR_MACH_AVRTINY:
2933 strncat (buf, ", avr:100", size);
2934 break;
2935 case E_AVR_MACH_XMEGA1:
2936 strncat (buf, ", avr:101", size);
2937 break;
2938 case E_AVR_MACH_XMEGA2:
2939 strncat (buf, ", avr:102", size);
2940 break;
2941 case E_AVR_MACH_XMEGA3:
2942 strncat (buf, ", avr:103", size);
2943 break;
2944 case E_AVR_MACH_XMEGA4:
2945 strncat (buf, ", avr:104", size);
2946 break;
2947 case E_AVR_MACH_XMEGA5:
2948 strncat (buf, ", avr:105", size);
2949 break;
2950 case E_AVR_MACH_XMEGA6:
2951 strncat (buf, ", avr:106", size);
2952 break;
2953 case E_AVR_MACH_XMEGA7:
2954 strncat (buf, ", avr:107", size);
2955 break;
2956 default:
2957 strncat (buf, ", avr:<unknown>", size);
2958 break;
2959 }
2960
2961 size -= strlen (buf);
2962 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
2963 strncat (buf, ", link-relax", size);
2964}
2965
35c08157
KLC
2966static void
2967decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
2968{
2969 unsigned abi;
2970 unsigned arch;
2971 unsigned config;
2972 unsigned version;
015dc7e1 2973 bool has_fpu = false;
32ec8896 2974 unsigned int r = 0;
35c08157
KLC
2975
2976 static const char *ABI_STRINGS[] =
2977 {
2978 "ABI v0", /* use r5 as return register; only used in N1213HC */
2979 "ABI v1", /* use r0 as return register */
2980 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
2981 "ABI v2fp", /* for FPU */
40c7a7cb
KLC
2982 "AABI",
2983 "ABI2 FP+"
35c08157
KLC
2984 };
2985 static const char *VER_STRINGS[] =
2986 {
2987 "Andes ELF V1.3 or older",
2988 "Andes ELF V1.3.1",
2989 "Andes ELF V1.4"
2990 };
2991 static const char *ARCH_STRINGS[] =
2992 {
2993 "",
2994 "Andes Star v1.0",
2995 "Andes Star v2.0",
2996 "Andes Star v3.0",
2997 "Andes Star v3.0m"
2998 };
2999
3000 abi = EF_NDS_ABI & e_flags;
3001 arch = EF_NDS_ARCH & e_flags;
3002 config = EF_NDS_INST & e_flags;
3003 version = EF_NDS32_ELF_VERSION & e_flags;
3004
3005 memset (buf, 0, size);
3006
3007 switch (abi)
3008 {
3009 case E_NDS_ABI_V0:
3010 case E_NDS_ABI_V1:
3011 case E_NDS_ABI_V2:
3012 case E_NDS_ABI_V2FP:
3013 case E_NDS_ABI_AABI:
40c7a7cb 3014 case E_NDS_ABI_V2FP_PLUS:
35c08157
KLC
3015 /* In case there are holes in the array. */
3016 r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3017 break;
3018
3019 default:
3020 r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
3021 break;
3022 }
3023
3024 switch (version)
3025 {
3026 case E_NDS32_ELF_VER_1_2:
3027 case E_NDS32_ELF_VER_1_3:
3028 case E_NDS32_ELF_VER_1_4:
3029 r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
3030 break;
3031
3032 default:
3033 r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
3034 break;
3035 }
3036
3037 if (E_NDS_ABI_V0 == abi)
3038 {
3039 /* OLD ABI; only used in N1213HC, has performance extension 1. */
3040 r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
3041 if (arch == E_NDS_ARCH_STAR_V1_0)
3042 r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
3043 return;
3044 }
3045
3046 switch (arch)
3047 {
3048 case E_NDS_ARCH_STAR_V1_0:
3049 case E_NDS_ARCH_STAR_V2_0:
3050 case E_NDS_ARCH_STAR_V3_0:
3051 case E_NDS_ARCH_STAR_V3_M:
3052 r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
3053 break;
3054
3055 default:
3056 r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3057 /* ARCH version determines how the e_flags are interpreted.
3058 If it is unknown, we cannot proceed. */
3059 return;
3060 }
3061
3062 /* Newer ABI; Now handle architecture specific flags. */
3063 if (arch == E_NDS_ARCH_STAR_V1_0)
3064 {
3065 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3066 r += snprintf (buf + r, size -r, ", MFUSR_PC");
3067
3068 if (!(config & E_NDS32_HAS_NO_MAC_INST))
3069 r += snprintf (buf + r, size -r, ", MAC");
3070
3071 if (config & E_NDS32_HAS_DIV_INST)
3072 r += snprintf (buf + r, size -r, ", DIV");
3073
3074 if (config & E_NDS32_HAS_16BIT_INST)
3075 r += snprintf (buf + r, size -r, ", 16b");
3076 }
3077 else
3078 {
3079 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3080 {
3081 if (version <= E_NDS32_ELF_VER_1_3)
3082 r += snprintf (buf + r, size -r, ", [B8]");
3083 else
3084 r += snprintf (buf + r, size -r, ", EX9");
3085 }
3086
3087 if (config & E_NDS32_HAS_MAC_DX_INST)
3088 r += snprintf (buf + r, size -r, ", MAC_DX");
3089
3090 if (config & E_NDS32_HAS_DIV_DX_INST)
3091 r += snprintf (buf + r, size -r, ", DIV_DX");
3092
3093 if (config & E_NDS32_HAS_16BIT_INST)
3094 {
3095 if (version <= E_NDS32_ELF_VER_1_3)
3096 r += snprintf (buf + r, size -r, ", 16b");
3097 else
3098 r += snprintf (buf + r, size -r, ", IFC");
3099 }
3100 }
3101
3102 if (config & E_NDS32_HAS_EXT_INST)
3103 r += snprintf (buf + r, size -r, ", PERF1");
3104
3105 if (config & E_NDS32_HAS_EXT2_INST)
3106 r += snprintf (buf + r, size -r, ", PERF2");
3107
3108 if (config & E_NDS32_HAS_FPU_INST)
3109 {
015dc7e1 3110 has_fpu = true;
35c08157
KLC
3111 r += snprintf (buf + r, size -r, ", FPU_SP");
3112 }
3113
3114 if (config & E_NDS32_HAS_FPU_DP_INST)
3115 {
015dc7e1 3116 has_fpu = true;
35c08157
KLC
3117 r += snprintf (buf + r, size -r, ", FPU_DP");
3118 }
3119
3120 if (config & E_NDS32_HAS_FPU_MAC_INST)
3121 {
015dc7e1 3122 has_fpu = true;
35c08157
KLC
3123 r += snprintf (buf + r, size -r, ", FPU_MAC");
3124 }
3125
3126 if (has_fpu)
3127 {
3128 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3129 {
3130 case E_NDS32_FPU_REG_8SP_4DP:
3131 r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3132 break;
3133 case E_NDS32_FPU_REG_16SP_8DP:
3134 r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3135 break;
3136 case E_NDS32_FPU_REG_32SP_16DP:
3137 r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3138 break;
3139 case E_NDS32_FPU_REG_32SP_32DP:
3140 r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3141 break;
3142 }
3143 }
3144
3145 if (config & E_NDS32_HAS_AUDIO_INST)
3146 r += snprintf (buf + r, size -r, ", AUDIO");
3147
3148 if (config & E_NDS32_HAS_STRING_INST)
3149 r += snprintf (buf + r, size -r, ", STR");
3150
3151 if (config & E_NDS32_HAS_REDUCED_REGS)
3152 r += snprintf (buf + r, size -r, ", 16REG");
3153
3154 if (config & E_NDS32_HAS_VIDEO_INST)
3155 {
3156 if (version <= E_NDS32_ELF_VER_1_3)
3157 r += snprintf (buf + r, size -r, ", VIDEO");
3158 else
3159 r += snprintf (buf + r, size -r, ", SATURATION");
3160 }
3161
3162 if (config & E_NDS32_HAS_ENCRIPT_INST)
3163 r += snprintf (buf + r, size -r, ", ENCRP");
3164
3165 if (config & E_NDS32_HAS_L2C_INST)
3166 r += snprintf (buf + r, size -r, ", L2C");
3167}
3168
252b5132 3169static char *
dda8d76d 3170get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
252b5132 3171{
b34976b6 3172 static char buf[1024];
252b5132
RH
3173
3174 buf[0] = '\0';
76da6bbe 3175
252b5132
RH
3176 if (e_flags)
3177 {
3178 switch (e_machine)
3179 {
3180 default:
3181 break;
3182
886a2506 3183 case EM_ARC_COMPACT2:
886a2506 3184 case EM_ARC_COMPACT:
a9522a21
AB
3185 decode_ARC_machine_flags (e_flags, e_machine, buf);
3186 break;
886a2506 3187
f3485b74
NC
3188 case EM_ARM:
3189 decode_ARM_machine_flags (e_flags, buf);
3190 break;
76da6bbe 3191
343433df
AB
3192 case EM_AVR:
3193 decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3194 break;
3195
781303ce
MF
3196 case EM_BLACKFIN:
3197 if (e_flags & EF_BFIN_PIC)
3198 strcat (buf, ", PIC");
3199
3200 if (e_flags & EF_BFIN_FDPIC)
3201 strcat (buf, ", FDPIC");
3202
3203 if (e_flags & EF_BFIN_CODE_IN_L1)
3204 strcat (buf, ", code in L1");
3205
3206 if (e_flags & EF_BFIN_DATA_IN_L1)
3207 strcat (buf, ", data in L1");
3208
3209 break;
3210
ec2dfb42
AO
3211 case EM_CYGNUS_FRV:
3212 switch (e_flags & EF_FRV_CPU_MASK)
3213 {
3214 case EF_FRV_CPU_GENERIC:
3215 break;
3216
3217 default:
3218 strcat (buf, ", fr???");
3219 break;
57346661 3220
ec2dfb42
AO
3221 case EF_FRV_CPU_FR300:
3222 strcat (buf, ", fr300");
3223 break;
3224
3225 case EF_FRV_CPU_FR400:
3226 strcat (buf, ", fr400");
3227 break;
3228 case EF_FRV_CPU_FR405:
3229 strcat (buf, ", fr405");
3230 break;
3231
3232 case EF_FRV_CPU_FR450:
3233 strcat (buf, ", fr450");
3234 break;
3235
3236 case EF_FRV_CPU_FR500:
3237 strcat (buf, ", fr500");
3238 break;
3239 case EF_FRV_CPU_FR550:
3240 strcat (buf, ", fr550");
3241 break;
3242
3243 case EF_FRV_CPU_SIMPLE:
3244 strcat (buf, ", simple");
3245 break;
3246 case EF_FRV_CPU_TOMCAT:
3247 strcat (buf, ", tomcat");
3248 break;
3249 }
1c877e87 3250 break;
ec2dfb42 3251
53c7db4b 3252 case EM_68K:
425c6cb0 3253 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
76f57f3a 3254 strcat (buf, ", m68000");
425c6cb0 3255 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3bdcfdf4
KH
3256 strcat (buf, ", cpu32");
3257 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3258 strcat (buf, ", fido_a");
425c6cb0 3259 else
266abb8f 3260 {
2cf0635d
NC
3261 char const * isa = _("unknown");
3262 char const * mac = _("unknown mac");
3263 char const * additional = NULL;
0112cd26 3264
c694fd50 3265 switch (e_flags & EF_M68K_CF_ISA_MASK)
266abb8f 3266 {
c694fd50 3267 case EF_M68K_CF_ISA_A_NODIV:
0b2e31dc
NS
3268 isa = "A";
3269 additional = ", nodiv";
3270 break;
c694fd50 3271 case EF_M68K_CF_ISA_A:
266abb8f
NS
3272 isa = "A";
3273 break;
c694fd50 3274 case EF_M68K_CF_ISA_A_PLUS:
266abb8f
NS
3275 isa = "A+";
3276 break;
c694fd50 3277 case EF_M68K_CF_ISA_B_NOUSP:
0b2e31dc
NS
3278 isa = "B";
3279 additional = ", nousp";
3280 break;
c694fd50 3281 case EF_M68K_CF_ISA_B:
266abb8f
NS
3282 isa = "B";
3283 break;
f608cd77
NS
3284 case EF_M68K_CF_ISA_C:
3285 isa = "C";
3286 break;
3287 case EF_M68K_CF_ISA_C_NODIV:
3288 isa = "C";
3289 additional = ", nodiv";
3290 break;
266abb8f
NS
3291 }
3292 strcat (buf, ", cf, isa ");
3293 strcat (buf, isa);
0b2e31dc
NS
3294 if (additional)
3295 strcat (buf, additional);
c694fd50 3296 if (e_flags & EF_M68K_CF_FLOAT)
0b2e31dc 3297 strcat (buf, ", float");
c694fd50 3298 switch (e_flags & EF_M68K_CF_MAC_MASK)
266abb8f
NS
3299 {
3300 case 0:
3301 mac = NULL;
3302 break;
c694fd50 3303 case EF_M68K_CF_MAC:
266abb8f
NS
3304 mac = "mac";
3305 break;
c694fd50 3306 case EF_M68K_CF_EMAC:
266abb8f
NS
3307 mac = "emac";
3308 break;
f608cd77
NS
3309 case EF_M68K_CF_EMAC_B:
3310 mac = "emac_b";
3311 break;
266abb8f
NS
3312 }
3313 if (mac)
3314 {
3315 strcat (buf, ", ");
3316 strcat (buf, mac);
3317 }
266abb8f 3318 }
53c7db4b 3319 break;
33c63f9d 3320
153a2776
NC
3321 case EM_CYGNUS_MEP:
3322 switch (e_flags & EF_MEP_CPU_MASK)
3323 {
3324 case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3325 case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3326 case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3327 case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3328 case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3329 case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3330 default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3331 }
3332
3333 switch (e_flags & EF_MEP_COP_MASK)
3334 {
3335 case EF_MEP_COP_NONE: break;
3336 case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3337 case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3338 case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3339 case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3340 default: strcat (buf, _("<unknown MeP copro type>")); break;
3341 }
3342
3343 if (e_flags & EF_MEP_LIBRARY)
3344 strcat (buf, ", Built for Library");
3345
3346 if (e_flags & EF_MEP_INDEX_MASK)
3347 sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3348 e_flags & EF_MEP_INDEX_MASK);
3349
3350 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3351 sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3352 e_flags & ~ EF_MEP_ALL_FLAGS);
3353 break;
3354
252b5132
RH
3355 case EM_PPC:
3356 if (e_flags & EF_PPC_EMB)
3357 strcat (buf, ", emb");
3358
3359 if (e_flags & EF_PPC_RELOCATABLE)
2b692964 3360 strcat (buf, _(", relocatable"));
252b5132
RH
3361
3362 if (e_flags & EF_PPC_RELOCATABLE_LIB)
2b692964 3363 strcat (buf, _(", relocatable-lib"));
252b5132
RH
3364 break;
3365
ee67d69a
AM
3366 case EM_PPC64:
3367 if (e_flags & EF_PPC64_ABI)
3368 {
3369 char abi[] = ", abiv0";
3370
3371 abi[6] += e_flags & EF_PPC64_ABI;
3372 strcat (buf, abi);
3373 }
3374 break;
3375
708e2187
NC
3376 case EM_V800:
3377 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3378 strcat (buf, ", RH850 ABI");
0b4362b0 3379
708e2187
NC
3380 if (e_flags & EF_V800_850E3)
3381 strcat (buf, ", V3 architecture");
3382
3383 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3384 strcat (buf, ", FPU not used");
3385
3386 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3387 strcat (buf, ", regmode: COMMON");
3388
3389 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3390 strcat (buf, ", r4 not used");
3391
3392 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3393 strcat (buf, ", r30 not used");
3394
3395 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3396 strcat (buf, ", r5 not used");
3397
3398 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3399 strcat (buf, ", r2 not used");
3400
3401 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3402 {
3403 switch (e_flags & - e_flags)
3404 {
3405 case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3406 case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
708e2187
NC
3407 case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3408 case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
708e2187
NC
3409 case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3410 case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3411 case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3412 case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3413 case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3414 case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3415 case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3416 case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3417 default: break;
3418 }
3419 }
3420 break;
3421
2b0337b0 3422 case EM_V850:
252b5132
RH
3423 case EM_CYGNUS_V850:
3424 switch (e_flags & EF_V850_ARCH)
3425 {
78c8d46c
NC
3426 case E_V850E3V5_ARCH:
3427 strcat (buf, ", v850e3v5");
3428 break;
1cd986c5
NC
3429 case E_V850E2V3_ARCH:
3430 strcat (buf, ", v850e2v3");
3431 break;
3432 case E_V850E2_ARCH:
3433 strcat (buf, ", v850e2");
3434 break;
3435 case E_V850E1_ARCH:
3436 strcat (buf, ", v850e1");
8ad30312 3437 break;
252b5132
RH
3438 case E_V850E_ARCH:
3439 strcat (buf, ", v850e");
3440 break;
252b5132
RH
3441 case E_V850_ARCH:
3442 strcat (buf, ", v850");
3443 break;
3444 default:
2b692964 3445 strcat (buf, _(", unknown v850 architecture variant"));
252b5132
RH
3446 break;
3447 }
3448 break;
3449
2b0337b0 3450 case EM_M32R:
252b5132
RH
3451 case EM_CYGNUS_M32R:
3452 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3453 strcat (buf, ", m32r");
252b5132
RH
3454 break;
3455
3456 case EM_MIPS:
4fe85591 3457 case EM_MIPS_RS3_LE:
252b5132
RH
3458 if (e_flags & EF_MIPS_NOREORDER)
3459 strcat (buf, ", noreorder");
3460
3461 if (e_flags & EF_MIPS_PIC)
3462 strcat (buf, ", pic");
3463
3464 if (e_flags & EF_MIPS_CPIC)
3465 strcat (buf, ", cpic");
3466
d1bdd336
TS
3467 if (e_flags & EF_MIPS_UCODE)
3468 strcat (buf, ", ugen_reserved");
3469
252b5132
RH
3470 if (e_flags & EF_MIPS_ABI2)
3471 strcat (buf, ", abi2");
3472
43521d43
TS
3473 if (e_flags & EF_MIPS_OPTIONS_FIRST)
3474 strcat (buf, ", odk first");
3475
a5d22d2a
TS
3476 if (e_flags & EF_MIPS_32BITMODE)
3477 strcat (buf, ", 32bitmode");
3478
ba92f887
MR
3479 if (e_flags & EF_MIPS_NAN2008)
3480 strcat (buf, ", nan2008");
3481
fef1b0b3
SE
3482 if (e_flags & EF_MIPS_FP64)
3483 strcat (buf, ", fp64");
3484
156c2f8b
NC
3485 switch ((e_flags & EF_MIPS_MACH))
3486 {
3487 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
3488 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
3489 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
156c2f8b 3490 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
810dfa6e
L
3491 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
3492 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
3493 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
3494 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
ef272caa 3495 case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
c6c98b38 3496 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
ebcb91b7 3497 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
350cc38d
MS
3498 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
3499 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
ac8cb70f 3500 case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
bd782c07 3501 case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
9108bc33 3502 case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
05c6f050 3503 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
67c2a3e8 3504 case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
d32e5c54 3505 case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
52b6b6b9 3506 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
38bf472a 3507 case E_MIPS_MACH_IAMR2: strcat (buf, ", interaptiv-mr2"); break;
43521d43
TS
3508 case 0:
3509 /* We simply ignore the field in this case to avoid confusion:
3510 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
3511 extension. */
3512 break;
2b692964 3513 default: strcat (buf, _(", unknown CPU")); break;
156c2f8b 3514 }
43521d43
TS
3515
3516 switch ((e_flags & EF_MIPS_ABI))
3517 {
3518 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
3519 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
3520 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
3521 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
3522 case 0:
3523 /* We simply ignore the field in this case to avoid confusion:
3524 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
3525 This means it is likely to be an o32 file, but not for
3526 sure. */
3527 break;
2b692964 3528 default: strcat (buf, _(", unknown ABI")); break;
43521d43
TS
3529 }
3530
3531 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
3532 strcat (buf, ", mdmx");
3533
3534 if (e_flags & EF_MIPS_ARCH_ASE_M16)
3535 strcat (buf, ", mips16");
3536
df58fc94
RS
3537 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
3538 strcat (buf, ", micromips");
3539
43521d43
TS
3540 switch ((e_flags & EF_MIPS_ARCH))
3541 {
3542 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
3543 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
3544 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
3545 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
3546 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
3547 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
cb44e358 3548 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
7361da2c 3549 case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
43521d43 3550 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
5f74bc13 3551 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
7361da2c 3552 case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
2b692964 3553 default: strcat (buf, _(", unknown ISA")); break;
43521d43 3554 }
252b5132 3555 break;
351b4b40 3556
35c08157
KLC
3557 case EM_NDS32:
3558 decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
3559 break;
3560
fe944acf
FT
3561 case EM_NFP:
3562 switch (EF_NFP_MACH (e_flags))
3563 {
3564 case E_NFP_MACH_3200:
3565 strcat (buf, ", NFP-32xx");
3566 break;
3567 case E_NFP_MACH_6000:
3568 strcat (buf, ", NFP-6xxx");
3569 break;
3570 }
3571 break;
3572
e23eba97
NC
3573 case EM_RISCV:
3574 if (e_flags & EF_RISCV_RVC)
3575 strcat (buf, ", RVC");
2922d21d 3576
7f999549
JW
3577 if (e_flags & EF_RISCV_RVE)
3578 strcat (buf, ", RVE");
3579
2922d21d
AW
3580 switch (e_flags & EF_RISCV_FLOAT_ABI)
3581 {
3582 case EF_RISCV_FLOAT_ABI_SOFT:
3583 strcat (buf, ", soft-float ABI");
3584 break;
3585
3586 case EF_RISCV_FLOAT_ABI_SINGLE:
3587 strcat (buf, ", single-float ABI");
3588 break;
3589
3590 case EF_RISCV_FLOAT_ABI_DOUBLE:
3591 strcat (buf, ", double-float ABI");
3592 break;
3593
3594 case EF_RISCV_FLOAT_ABI_QUAD:
3595 strcat (buf, ", quad-float ABI");
3596 break;
3597 }
e23eba97
NC
3598 break;
3599
ccde1100
AO
3600 case EM_SH:
3601 switch ((e_flags & EF_SH_MACH_MASK))
3602 {
3603 case EF_SH1: strcat (buf, ", sh1"); break;
3604 case EF_SH2: strcat (buf, ", sh2"); break;
3605 case EF_SH3: strcat (buf, ", sh3"); break;
3606 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
3607 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
3608 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
3609 case EF_SH3E: strcat (buf, ", sh3e"); break;
3610 case EF_SH4: strcat (buf, ", sh4"); break;
3611 case EF_SH5: strcat (buf, ", sh5"); break;
3612 case EF_SH2E: strcat (buf, ", sh2e"); break;
3613 case EF_SH4A: strcat (buf, ", sh4a"); break;
1d70c7fb 3614 case EF_SH2A: strcat (buf, ", sh2a"); break;
ccde1100
AO
3615 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
3616 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
1d70c7fb 3617 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
0b92ab21
NH
3618 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
3619 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
3620 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
3621 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
3622 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
3623 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
2b692964 3624 default: strcat (buf, _(", unknown ISA")); break;
ccde1100
AO
3625 }
3626
cec6a5b8
MR
3627 if (e_flags & EF_SH_PIC)
3628 strcat (buf, ", pic");
3629
3630 if (e_flags & EF_SH_FDPIC)
3631 strcat (buf, ", fdpic");
ccde1100 3632 break;
948f632f 3633
73589c9d
CS
3634 case EM_OR1K:
3635 if (e_flags & EF_OR1K_NODELAY)
3636 strcat (buf, ", no delay");
3637 break;
57346661 3638
351b4b40
RH
3639 case EM_SPARCV9:
3640 if (e_flags & EF_SPARC_32PLUS)
3641 strcat (buf, ", v8+");
3642
3643 if (e_flags & EF_SPARC_SUN_US1)
d07faca2
RH
3644 strcat (buf, ", ultrasparcI");
3645
3646 if (e_flags & EF_SPARC_SUN_US3)
3647 strcat (buf, ", ultrasparcIII");
351b4b40
RH
3648
3649 if (e_flags & EF_SPARC_HAL_R1)
3650 strcat (buf, ", halr1");
3651
3652 if (e_flags & EF_SPARC_LEDATA)
3653 strcat (buf, ", ledata");
3654
3655 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
3656 strcat (buf, ", tso");
3657
3658 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
3659 strcat (buf, ", pso");
3660
3661 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
3662 strcat (buf, ", rmo");
3663 break;
7d466069 3664
103f02d3
UD
3665 case EM_PARISC:
3666 switch (e_flags & EF_PARISC_ARCH)
3667 {
3668 case EFA_PARISC_1_0:
3669 strcpy (buf, ", PA-RISC 1.0");
3670 break;
3671 case EFA_PARISC_1_1:
3672 strcpy (buf, ", PA-RISC 1.1");
3673 break;
3674 case EFA_PARISC_2_0:
3675 strcpy (buf, ", PA-RISC 2.0");
3676 break;
3677 default:
3678 break;
3679 }
3680 if (e_flags & EF_PARISC_TRAPNIL)
3681 strcat (buf, ", trapnil");
3682 if (e_flags & EF_PARISC_EXT)
3683 strcat (buf, ", ext");
3684 if (e_flags & EF_PARISC_LSB)
3685 strcat (buf, ", lsb");
3686 if (e_flags & EF_PARISC_WIDE)
3687 strcat (buf, ", wide");
3688 if (e_flags & EF_PARISC_NO_KABP)
3689 strcat (buf, ", no kabp");
3690 if (e_flags & EF_PARISC_LAZYSWAP)
3691 strcat (buf, ", lazyswap");
30800947 3692 break;
76da6bbe 3693
7d466069 3694 case EM_PJ:
2b0337b0 3695 case EM_PJ_OLD:
7d466069
ILT
3696 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
3697 strcat (buf, ", new calling convention");
3698
3699 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
3700 strcat (buf, ", gnu calling convention");
3701 break;
4d6ed7c8
NC
3702
3703 case EM_IA_64:
3704 if ((e_flags & EF_IA_64_ABI64))
3705 strcat (buf, ", 64-bit");
3706 else
3707 strcat (buf, ", 32-bit");
3708 if ((e_flags & EF_IA_64_REDUCEDFP))
3709 strcat (buf, ", reduced fp model");
3710 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3711 strcat (buf, ", no function descriptors, constant gp");
3712 else if ((e_flags & EF_IA_64_CONS_GP))
3713 strcat (buf, ", constant gp");
3714 if ((e_flags & EF_IA_64_ABSOLUTE))
3715 strcat (buf, ", absolute");
dda8d76d 3716 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
28f997cf
TG
3717 {
3718 if ((e_flags & EF_IA_64_VMS_LINKAGES))
3719 strcat (buf, ", vms_linkages");
3720 switch ((e_flags & EF_IA_64_VMS_COMCOD))
3721 {
3722 case EF_IA_64_VMS_COMCOD_SUCCESS:
3723 break;
3724 case EF_IA_64_VMS_COMCOD_WARNING:
3725 strcat (buf, ", warning");
3726 break;
3727 case EF_IA_64_VMS_COMCOD_ERROR:
3728 strcat (buf, ", error");
3729 break;
3730 case EF_IA_64_VMS_COMCOD_ABORT:
3731 strcat (buf, ", abort");
3732 break;
3733 default:
bee0ee85
NC
3734 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
3735 e_flags & EF_IA_64_VMS_COMCOD);
3736 strcat (buf, ", <unknown>");
28f997cf
TG
3737 }
3738 }
4d6ed7c8 3739 break;
179d3252
JT
3740
3741 case EM_VAX:
3742 if ((e_flags & EF_VAX_NONPIC))
3743 strcat (buf, ", non-PIC");
3744 if ((e_flags & EF_VAX_DFLOAT))
3745 strcat (buf, ", D-Float");
3746 if ((e_flags & EF_VAX_GFLOAT))
3747 strcat (buf, ", G-Float");
3748 break;
c7927a3c 3749
619ed720
EB
3750 case EM_VISIUM:
3751 if (e_flags & EF_VISIUM_ARCH_MCM)
3752 strcat (buf, ", mcm");
3753 else if (e_flags & EF_VISIUM_ARCH_MCM24)
3754 strcat (buf, ", mcm24");
3755 if (e_flags & EF_VISIUM_ARCH_GR6)
3756 strcat (buf, ", gr6");
3757 break;
3758
4046d87a 3759 case EM_RL78:
1740ba0c
NC
3760 switch (e_flags & E_FLAG_RL78_CPU_MASK)
3761 {
3762 case E_FLAG_RL78_ANY_CPU: break;
3763 case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
3764 case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
3765 case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
3766 }
856ea05c
KP
3767 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
3768 strcat (buf, ", 64-bit doubles");
4046d87a 3769 break;
0b4362b0 3770
c7927a3c
NC
3771 case EM_RX:
3772 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
3773 strcat (buf, ", 64-bit doubles");
3774 if (e_flags & E_FLAG_RX_DSP)
dd24e3da 3775 strcat (buf, ", dsp");
d4cb0ea0 3776 if (e_flags & E_FLAG_RX_PID)
0b4362b0 3777 strcat (buf, ", pid");
708e2187
NC
3778 if (e_flags & E_FLAG_RX_ABI)
3779 strcat (buf, ", RX ABI");
3525236c
NC
3780 if (e_flags & E_FLAG_RX_SINSNS_SET)
3781 strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
3782 ? ", uses String instructions" : ", bans String instructions");
a117b0a5
YS
3783 if (e_flags & E_FLAG_RX_V2)
3784 strcat (buf, ", V2");
f87673e0
YS
3785 if (e_flags & E_FLAG_RX_V3)
3786 strcat (buf, ", V3");
d4cb0ea0 3787 break;
55786da2
AK
3788
3789 case EM_S390:
3790 if (e_flags & EF_S390_HIGH_GPRS)
3791 strcat (buf, ", highgprs");
d4cb0ea0 3792 break;
40b36596
JM
3793
3794 case EM_TI_C6000:
3795 if ((e_flags & EF_C6000_REL))
3796 strcat (buf, ", relocatable module");
d4cb0ea0 3797 break;
13761a11
NC
3798
3799 case EM_MSP430:
3800 strcat (buf, _(": architecture variant: "));
3801 switch (e_flags & EF_MSP430_MACH)
3802 {
3803 case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
3804 case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
3805 case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
3806 case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
3807 case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
3808 case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
3809 case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
3810 case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
3811 case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
3812 case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
3813 case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
3814 case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
3815 case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
3816 case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
3817 case E_MSP430_MACH_MSP430X : strcat (buf, "MSP430X"); break;
3818 default:
3819 strcat (buf, _(": unknown")); break;
3820 }
3821
3822 if (e_flags & ~ EF_MSP430_MACH)
3823 strcat (buf, _(": unknown extra flag bits also present"));
6655dba2
SB
3824 break;
3825
3826 case EM_Z80:
3827 switch (e_flags & EF_Z80_MACH_MSK)
3828 {
3829 case EF_Z80_MACH_Z80: strcat (buf, ", Z80"); break;
3830 case EF_Z80_MACH_Z180: strcat (buf, ", Z180"); break;
3831 case EF_Z80_MACH_R800: strcat (buf, ", R800"); break;
3832 case EF_Z80_MACH_EZ80_Z80: strcat (buf, ", EZ80"); break;
3833 case EF_Z80_MACH_EZ80_ADL: strcat (buf, ", EZ80, ADL"); break;
3834 case EF_Z80_MACH_GBZ80: strcat (buf, ", GBZ80"); break;
9fc0b501 3835 case EF_Z80_MACH_Z80N: strcat (buf, ", Z80N"); break;
6655dba2
SB
3836 default:
3837 strcat (buf, _(", unknown")); break;
3838 }
3839 break;
252b5132
RH
3840 }
3841 }
3842
3843 return buf;
3844}
3845
252b5132 3846static const char *
dda8d76d 3847get_osabi_name (Filedata * filedata, unsigned int osabi)
d3ba0551
AM
3848{
3849 static char buff[32];
3850
3851 switch (osabi)
3852 {
3853 case ELFOSABI_NONE: return "UNIX - System V";
3854 case ELFOSABI_HPUX: return "UNIX - HP-UX";
3855 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
9c55345c 3856 case ELFOSABI_GNU: return "UNIX - GNU";
d3ba0551
AM
3857 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
3858 case ELFOSABI_AIX: return "UNIX - AIX";
3859 case ELFOSABI_IRIX: return "UNIX - IRIX";
3860 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
3861 case ELFOSABI_TRU64: return "UNIX - TRU64";
3862 case ELFOSABI_MODESTO: return "Novell - Modesto";
3863 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
3864 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
3865 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
3b26c801 3866 case ELFOSABI_AROS: return "AROS";
11636f9e 3867 case ELFOSABI_FENIXOS: return "FenixOS";
6d913794
NC
3868 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
3869 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
d3ba0551 3870 default:
40b36596 3871 if (osabi >= 64)
dda8d76d 3872 switch (filedata->file_header.e_machine)
40b36596
JM
3873 {
3874 case EM_ARM:
3875 switch (osabi)
3876 {
3877 case ELFOSABI_ARM: return "ARM";
18a20338 3878 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
40b36596
JM
3879 default:
3880 break;
3881 }
3882 break;
3883
3884 case EM_MSP430:
3885 case EM_MSP430_OLD:
619ed720 3886 case EM_VISIUM:
40b36596
JM
3887 switch (osabi)
3888 {
3889 case ELFOSABI_STANDALONE: return _("Standalone App");
3890 default:
3891 break;
3892 }
3893 break;
3894
3895 case EM_TI_C6000:
3896 switch (osabi)
3897 {
3898 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
3899 case ELFOSABI_C6000_LINUX: return "Linux C6000";
3900 default:
3901 break;
3902 }
3903 break;
3904
3905 default:
3906 break;
3907 }
e9e44622 3908 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
d3ba0551
AM
3909 return buff;
3910 }
3911}
3912
a06ea964
NC
3913static const char *
3914get_aarch64_segment_type (unsigned long type)
3915{
3916 switch (type)
3917 {
32ec8896
NC
3918 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
3919 default: return NULL;
a06ea964 3920 }
a06ea964
NC
3921}
3922
b294bdf8
MM
3923static const char *
3924get_arm_segment_type (unsigned long type)
3925{
3926 switch (type)
3927 {
32ec8896
NC
3928 case PT_ARM_EXIDX: return "EXIDX";
3929 default: return NULL;
b294bdf8 3930 }
b294bdf8
MM
3931}
3932
b4cbbe8f
AK
3933static const char *
3934get_s390_segment_type (unsigned long type)
3935{
3936 switch (type)
3937 {
3938 case PT_S390_PGSTE: return "S390_PGSTE";
3939 default: return NULL;
3940 }
3941}
3942
d3ba0551
AM
3943static const char *
3944get_mips_segment_type (unsigned long type)
252b5132
RH
3945{
3946 switch (type)
3947 {
32ec8896
NC
3948 case PT_MIPS_REGINFO: return "REGINFO";
3949 case PT_MIPS_RTPROC: return "RTPROC";
3950 case PT_MIPS_OPTIONS: return "OPTIONS";
3951 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
3952 default: return NULL;
252b5132 3953 }
252b5132
RH
3954}
3955
103f02d3 3956static const char *
d3ba0551 3957get_parisc_segment_type (unsigned long type)
103f02d3
UD
3958{
3959 switch (type)
3960 {
103f02d3
UD
3961 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
3962 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
61472819 3963 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
32ec8896 3964 default: return NULL;
103f02d3 3965 }
103f02d3
UD
3966}
3967
4d6ed7c8 3968static const char *
d3ba0551 3969get_ia64_segment_type (unsigned long type)
4d6ed7c8
NC
3970{
3971 switch (type)
3972 {
3973 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
3974 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
32ec8896 3975 default: return NULL;
4d6ed7c8 3976 }
4d6ed7c8
NC
3977}
3978
40b36596
JM
3979static const char *
3980get_tic6x_segment_type (unsigned long type)
3981{
3982 switch (type)
3983 {
32ec8896
NC
3984 case PT_C6000_PHATTR: return "C6000_PHATTR";
3985 default: return NULL;
40b36596 3986 }
40b36596
JM
3987}
3988
df3a023b
AM
3989static const char *
3990get_hpux_segment_type (unsigned long type, unsigned e_machine)
3991{
3992 if (e_machine == EM_PARISC)
3993 switch (type)
3994 {
3995 case PT_HP_TLS: return "HP_TLS";
3996 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
3997 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
3998 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
3999 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
4000 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
4001 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
4002 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
4003 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
4004 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
4005 case PT_HP_PARALLEL: return "HP_PARALLEL";
4006 case PT_HP_FASTBIND: return "HP_FASTBIND";
4007 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
4008 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
4009 case PT_HP_STACK: return "HP_STACK";
4010 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
4011 default: return NULL;
4012 }
4013
4014 if (e_machine == EM_IA_64)
4015 switch (type)
4016 {
4017 case PT_HP_TLS: return "HP_TLS";
4018 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
4019 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
4020 case PT_IA_64_HP_STACK: return "HP_STACK";
4021 default: return NULL;
4022 }
4023
4024 return NULL;
4025}
4026
5522f910
NC
4027static const char *
4028get_solaris_segment_type (unsigned long type)
4029{
4030 switch (type)
4031 {
4032 case 0x6464e550: return "PT_SUNW_UNWIND";
4033 case 0x6474e550: return "PT_SUNW_EH_FRAME";
4034 case 0x6ffffff7: return "PT_LOSUNW";
4035 case 0x6ffffffa: return "PT_SUNWBSS";
4036 case 0x6ffffffb: return "PT_SUNWSTACK";
4037 case 0x6ffffffc: return "PT_SUNWDTRACE";
4038 case 0x6ffffffd: return "PT_SUNWCAP";
4039 case 0x6fffffff: return "PT_HISUNW";
32ec8896 4040 default: return NULL;
5522f910
NC
4041 }
4042}
4043
252b5132 4044static const char *
dda8d76d 4045get_segment_type (Filedata * filedata, unsigned long p_type)
252b5132 4046{
b34976b6 4047 static char buff[32];
252b5132
RH
4048
4049 switch (p_type)
4050 {
b34976b6
AM
4051 case PT_NULL: return "NULL";
4052 case PT_LOAD: return "LOAD";
252b5132 4053 case PT_DYNAMIC: return "DYNAMIC";
b34976b6
AM
4054 case PT_INTERP: return "INTERP";
4055 case PT_NOTE: return "NOTE";
4056 case PT_SHLIB: return "SHLIB";
4057 case PT_PHDR: return "PHDR";
13ae64f3 4058 case PT_TLS: return "TLS";
32ec8896 4059 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
2b05f1b7 4060 case PT_GNU_STACK: return "GNU_STACK";
8c37241b 4061 case PT_GNU_RELRO: return "GNU_RELRO";
0a59decb 4062 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
65765700 4063
3eba3ef3
NC
4064 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
4065 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
4066 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
b9e920ec 4067
252b5132 4068 default:
df3a023b 4069 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
252b5132 4070 {
2cf0635d 4071 const char * result;
103f02d3 4072
dda8d76d 4073 switch (filedata->file_header.e_machine)
252b5132 4074 {
a06ea964
NC
4075 case EM_AARCH64:
4076 result = get_aarch64_segment_type (p_type);
4077 break;
b294bdf8
MM
4078 case EM_ARM:
4079 result = get_arm_segment_type (p_type);
4080 break;
252b5132 4081 case EM_MIPS:
4fe85591 4082 case EM_MIPS_RS3_LE:
252b5132
RH
4083 result = get_mips_segment_type (p_type);
4084 break;
103f02d3
UD
4085 case EM_PARISC:
4086 result = get_parisc_segment_type (p_type);
4087 break;
4d6ed7c8
NC
4088 case EM_IA_64:
4089 result = get_ia64_segment_type (p_type);
4090 break;
40b36596
JM
4091 case EM_TI_C6000:
4092 result = get_tic6x_segment_type (p_type);
4093 break;
b4cbbe8f
AK
4094 case EM_S390:
4095 case EM_S390_OLD:
4096 result = get_s390_segment_type (p_type);
4097 break;
252b5132
RH
4098 default:
4099 result = NULL;
4100 break;
4101 }
103f02d3 4102
252b5132
RH
4103 if (result != NULL)
4104 return result;
103f02d3 4105
1a9ccd70 4106 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
252b5132
RH
4107 }
4108 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
103f02d3 4109 {
df3a023b 4110 const char * result = NULL;
103f02d3 4111
df3a023b 4112 switch (filedata->file_header.e_ident[EI_OSABI])
103f02d3 4113 {
df3a023b
AM
4114 case ELFOSABI_GNU:
4115 case ELFOSABI_FREEBSD:
4116 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
4117 {
4118 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
4119 result = buff;
4120 }
103f02d3 4121 break;
df3a023b
AM
4122 case ELFOSABI_HPUX:
4123 result = get_hpux_segment_type (p_type,
4124 filedata->file_header.e_machine);
4125 break;
4126 case ELFOSABI_SOLARIS:
4127 result = get_solaris_segment_type (p_type);
00428cca 4128 break;
103f02d3 4129 default:
103f02d3
UD
4130 break;
4131 }
103f02d3
UD
4132 if (result != NULL)
4133 return result;
4134
1a9ccd70 4135 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
103f02d3 4136 }
252b5132 4137 else
e9e44622 4138 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
252b5132
RH
4139
4140 return buff;
4141 }
4142}
4143
53a346d8
CZ
4144static const char *
4145get_arc_section_type_name (unsigned int sh_type)
4146{
4147 switch (sh_type)
4148 {
4149 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
4150 default:
4151 break;
4152 }
4153 return NULL;
4154}
4155
252b5132 4156static const char *
d3ba0551 4157get_mips_section_type_name (unsigned int sh_type)
252b5132
RH
4158{
4159 switch (sh_type)
4160 {
b34976b6
AM
4161 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
4162 case SHT_MIPS_MSYM: return "MIPS_MSYM";
4163 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
4164 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
4165 case SHT_MIPS_UCODE: return "MIPS_UCODE";
4166 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
4167 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
4168 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
4169 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
4170 case SHT_MIPS_RELD: return "MIPS_RELD";
4171 case SHT_MIPS_IFACE: return "MIPS_IFACE";
4172 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
4173 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
4174 case SHT_MIPS_SHDR: return "MIPS_SHDR";
4175 case SHT_MIPS_FDESC: return "MIPS_FDESC";
4176 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
4177 case SHT_MIPS_DENSE: return "MIPS_DENSE";
4178 case SHT_MIPS_PDESC: return "MIPS_PDESC";
4179 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
4180 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
4181 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
4182 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
4183 case SHT_MIPS_LINE: return "MIPS_LINE";
4184 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
4185 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
4186 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
4187 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
4188 case SHT_MIPS_DWARF: return "MIPS_DWARF";
4189 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
4190 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
4191 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
4192 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
4193 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
4194 case SHT_MIPS_XLATE: return "MIPS_XLATE";
4195 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
4196 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
4197 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
4198 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
252b5132 4199 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
351cdf24 4200 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
f16a9783 4201 case SHT_MIPS_XHASH: return "MIPS_XHASH";
252b5132
RH
4202 default:
4203 break;
4204 }
4205 return NULL;
4206}
4207
103f02d3 4208static const char *
d3ba0551 4209get_parisc_section_type_name (unsigned int sh_type)
103f02d3
UD
4210{
4211 switch (sh_type)
4212 {
4213 case SHT_PARISC_EXT: return "PARISC_EXT";
4214 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
4215 case SHT_PARISC_DOC: return "PARISC_DOC";
eec8f817
DA
4216 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
4217 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
4218 case SHT_PARISC_STUBS: return "PARISC_STUBS";
61472819 4219 case SHT_PARISC_DLKM: return "PARISC_DLKM";
32ec8896 4220 default: return NULL;
103f02d3 4221 }
103f02d3
UD
4222}
4223
4d6ed7c8 4224static const char *
dda8d76d 4225get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4d6ed7c8 4226{
18bd398b 4227 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
ecc51f48 4228 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
dda8d76d 4229 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
0de14b54 4230
4d6ed7c8
NC
4231 switch (sh_type)
4232 {
148b93f2
NC
4233 case SHT_IA_64_EXT: return "IA_64_EXT";
4234 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
4235 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
4236 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
4237 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4238 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
4239 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
4240 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
4241 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
4242 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
4d6ed7c8
NC
4243 default:
4244 break;
4245 }
4246 return NULL;
4247}
4248
d2b2c203
DJ
4249static const char *
4250get_x86_64_section_type_name (unsigned int sh_type)
4251{
4252 switch (sh_type)
4253 {
4254 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
32ec8896 4255 default: return NULL;
d2b2c203 4256 }
d2b2c203
DJ
4257}
4258
a06ea964
NC
4259static const char *
4260get_aarch64_section_type_name (unsigned int sh_type)
4261{
4262 switch (sh_type)
4263 {
32ec8896
NC
4264 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4265 default: return NULL;
a06ea964 4266 }
a06ea964
NC
4267}
4268
40a18ebd
NC
4269static const char *
4270get_arm_section_type_name (unsigned int sh_type)
4271{
4272 switch (sh_type)
4273 {
7f6fed87
NC
4274 case SHT_ARM_EXIDX: return "ARM_EXIDX";
4275 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
4276 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
4277 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
4278 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
32ec8896 4279 default: return NULL;
40a18ebd 4280 }
40a18ebd
NC
4281}
4282
40b36596
JM
4283static const char *
4284get_tic6x_section_type_name (unsigned int sh_type)
4285{
4286 switch (sh_type)
4287 {
32ec8896
NC
4288 case SHT_C6000_UNWIND: return "C6000_UNWIND";
4289 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
4290 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
4291 case SHT_TI_ICODE: return "TI_ICODE";
4292 case SHT_TI_XREF: return "TI_XREF";
4293 case SHT_TI_HANDLER: return "TI_HANDLER";
4294 case SHT_TI_INITINFO: return "TI_INITINFO";
4295 case SHT_TI_PHATTRS: return "TI_PHATTRS";
4296 default: return NULL;
40b36596 4297 }
40b36596
JM
4298}
4299
13761a11 4300static const char *
b0191216 4301get_msp430_section_type_name (unsigned int sh_type)
13761a11
NC
4302{
4303 switch (sh_type)
4304 {
32ec8896
NC
4305 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
4306 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
4307 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
4308 default: return NULL;
13761a11
NC
4309 }
4310}
4311
fe944acf
FT
4312static const char *
4313get_nfp_section_type_name (unsigned int sh_type)
4314{
4315 switch (sh_type)
4316 {
4317 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
4318 case SHT_NFP_INITREG: return "NFP_INITREG";
4319 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
4320 default: return NULL;
4321 }
4322}
4323
685080f2
NC
4324static const char *
4325get_v850_section_type_name (unsigned int sh_type)
4326{
4327 switch (sh_type)
4328 {
32ec8896
NC
4329 case SHT_V850_SCOMMON: return "V850 Small Common";
4330 case SHT_V850_TCOMMON: return "V850 Tiny Common";
4331 case SHT_V850_ZCOMMON: return "V850 Zero Common";
4332 case SHT_RENESAS_IOP: return "RENESAS IOP";
4333 case SHT_RENESAS_INFO: return "RENESAS INFO";
4334 default: return NULL;
685080f2
NC
4335 }
4336}
4337
2dc8dd17
JW
4338static const char *
4339get_riscv_section_type_name (unsigned int sh_type)
4340{
4341 switch (sh_type)
4342 {
4343 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4344 default: return NULL;
4345 }
4346}
4347
0861f561
CQ
4348static const char *
4349get_csky_section_type_name (unsigned int sh_type)
4350{
4351 switch (sh_type)
4352 {
4353 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
4354 default: return NULL;
4355 }
4356}
4357
252b5132 4358static const char *
dda8d76d 4359get_section_type_name (Filedata * filedata, unsigned int sh_type)
252b5132 4360{
b34976b6 4361 static char buff[32];
9fb71ee4 4362 const char * result;
252b5132
RH
4363
4364 switch (sh_type)
4365 {
4366 case SHT_NULL: return "NULL";
4367 case SHT_PROGBITS: return "PROGBITS";
4368 case SHT_SYMTAB: return "SYMTAB";
4369 case SHT_STRTAB: return "STRTAB";
4370 case SHT_RELA: return "RELA";
4371 case SHT_HASH: return "HASH";
4372 case SHT_DYNAMIC: return "DYNAMIC";
4373 case SHT_NOTE: return "NOTE";
4374 case SHT_NOBITS: return "NOBITS";
4375 case SHT_REL: return "REL";
4376 case SHT_SHLIB: return "SHLIB";
4377 case SHT_DYNSYM: return "DYNSYM";
d1133906
NC
4378 case SHT_INIT_ARRAY: return "INIT_ARRAY";
4379 case SHT_FINI_ARRAY: return "FINI_ARRAY";
4380 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
fdc90cb4 4381 case SHT_GNU_HASH: return "GNU_HASH";
93ebe586 4382 case SHT_GROUP: return "GROUP";
67ce483b 4383 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
252b5132
RH
4384 case SHT_GNU_verdef: return "VERDEF";
4385 case SHT_GNU_verneed: return "VERNEED";
4386 case SHT_GNU_versym: return "VERSYM";
b34976b6
AM
4387 case 0x6ffffff0: return "VERSYM";
4388 case 0x6ffffffc: return "VERDEF";
252b5132
RH
4389 case 0x7ffffffd: return "AUXILIARY";
4390 case 0x7fffffff: return "FILTER";
047b2264 4391 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
252b5132
RH
4392
4393 default:
4394 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4395 {
dda8d76d 4396 switch (filedata->file_header.e_machine)
252b5132 4397 {
53a346d8
CZ
4398 case EM_ARC:
4399 case EM_ARC_COMPACT:
4400 case EM_ARC_COMPACT2:
4401 result = get_arc_section_type_name (sh_type);
4402 break;
252b5132 4403 case EM_MIPS:
4fe85591 4404 case EM_MIPS_RS3_LE:
252b5132
RH
4405 result = get_mips_section_type_name (sh_type);
4406 break;
103f02d3
UD
4407 case EM_PARISC:
4408 result = get_parisc_section_type_name (sh_type);
4409 break;
4d6ed7c8 4410 case EM_IA_64:
dda8d76d 4411 result = get_ia64_section_type_name (filedata, sh_type);
4d6ed7c8 4412 break;
d2b2c203 4413 case EM_X86_64:
8a9036a4 4414 case EM_L1OM:
7a9068fe 4415 case EM_K1OM:
d2b2c203
DJ
4416 result = get_x86_64_section_type_name (sh_type);
4417 break;
a06ea964
NC
4418 case EM_AARCH64:
4419 result = get_aarch64_section_type_name (sh_type);
4420 break;
40a18ebd
NC
4421 case EM_ARM:
4422 result = get_arm_section_type_name (sh_type);
4423 break;
40b36596
JM
4424 case EM_TI_C6000:
4425 result = get_tic6x_section_type_name (sh_type);
4426 break;
13761a11 4427 case EM_MSP430:
b0191216 4428 result = get_msp430_section_type_name (sh_type);
13761a11 4429 break;
fe944acf
FT
4430 case EM_NFP:
4431 result = get_nfp_section_type_name (sh_type);
4432 break;
685080f2
NC
4433 case EM_V800:
4434 case EM_V850:
4435 case EM_CYGNUS_V850:
4436 result = get_v850_section_type_name (sh_type);
4437 break;
2dc8dd17
JW
4438 case EM_RISCV:
4439 result = get_riscv_section_type_name (sh_type);
4440 break;
0861f561
CQ
4441 case EM_CSKY:
4442 result = get_csky_section_type_name (sh_type);
4443 break;
252b5132
RH
4444 default:
4445 result = NULL;
4446 break;
4447 }
4448
4449 if (result != NULL)
4450 return result;
4451
9fb71ee4 4452 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
252b5132
RH
4453 }
4454 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
148b93f2 4455 {
dda8d76d 4456 switch (filedata->file_header.e_machine)
148b93f2
NC
4457 {
4458 case EM_IA_64:
dda8d76d 4459 result = get_ia64_section_type_name (filedata, sh_type);
148b93f2
NC
4460 break;
4461 default:
dda8d76d 4462 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
fd85a6a1
NC
4463 result = get_solaris_section_type (sh_type);
4464 else
1b4b80bf
NC
4465 {
4466 switch (sh_type)
4467 {
4468 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
4469 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
4470 case SHT_GNU_HASH: result = "GNU_HASH"; break;
4471 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
4472 default:
4473 result = NULL;
4474 break;
4475 }
4476 }
148b93f2
NC
4477 break;
4478 }
4479
4480 if (result != NULL)
4481 return result;
4482
9fb71ee4 4483 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
148b93f2 4484 }
252b5132 4485 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
685080f2 4486 {
dda8d76d 4487 switch (filedata->file_header.e_machine)
685080f2
NC
4488 {
4489 case EM_V800:
4490 case EM_V850:
4491 case EM_CYGNUS_V850:
9fb71ee4 4492 result = get_v850_section_type_name (sh_type);
a9fb83be 4493 break;
685080f2 4494 default:
9fb71ee4 4495 result = NULL;
685080f2
NC
4496 break;
4497 }
4498
9fb71ee4
NC
4499 if (result != NULL)
4500 return result;
4501
4502 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
685080f2 4503 }
252b5132 4504 else
a7dbfd1c
NC
4505 /* This message is probably going to be displayed in a 15
4506 character wide field, so put the hex value first. */
4507 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
103f02d3 4508
252b5132
RH
4509 return buff;
4510 }
4511}
4512
79bc120c
NC
4513enum long_option_values
4514{
4515 OPTION_DEBUG_DUMP = 512,
4516 OPTION_DYN_SYMS,
0f03783c 4517 OPTION_LTO_SYMS,
79bc120c
NC
4518 OPTION_DWARF_DEPTH,
4519 OPTION_DWARF_START,
4520 OPTION_DWARF_CHECK,
4521 OPTION_CTF_DUMP,
4522 OPTION_CTF_PARENT,
4523 OPTION_CTF_SYMBOLS,
4524 OPTION_CTF_STRINGS,
4525 OPTION_WITH_SYMBOL_VERSIONS,
4526 OPTION_RECURSE_LIMIT,
4527 OPTION_NO_RECURSE_LIMIT,
4528 OPTION_NO_DEMANGLING
4529};
2979dc34 4530
85b1c36d 4531static struct option options[] =
252b5132 4532{
79bc120c
NC
4533 /* Note - This table is alpha-sorted on the 'val'
4534 field in order to make adding new options easier. */
4535 {"arch-specific", no_argument, 0, 'A'},
b34976b6 4536 {"all", no_argument, 0, 'a'},
79bc120c
NC
4537 {"demangle", optional_argument, 0, 'C'},
4538 {"archive-index", no_argument, 0, 'c'},
4539 {"use-dynamic", no_argument, 0, 'D'},
4540 {"dynamic", no_argument, 0, 'd'},
b34976b6 4541 {"headers", no_argument, 0, 'e'},
79bc120c
NC
4542 {"section-groups", no_argument, 0, 'g'},
4543 {"help", no_argument, 0, 'H'},
4544 {"file-header", no_argument, 0, 'h'},
b34976b6 4545 {"histogram", no_argument, 0, 'I'},
79bc120c
NC
4546 {"lint", no_argument, 0, 'L'},
4547 {"enable-checks", no_argument, 0, 'L'},
4548 {"program-headers", no_argument, 0, 'l'},
b34976b6 4549 {"segments", no_argument, 0, 'l'},
595cf52e 4550 {"full-section-name",no_argument, 0, 'N'},
79bc120c 4551 {"notes", no_argument, 0, 'n'},
ca0e11aa 4552 {"process-links", no_argument, 0, 'P'},
79bc120c
NC
4553 {"string-dump", required_argument, 0, 'p'},
4554 {"relocated-dump", required_argument, 0, 'R'},
4555 {"relocs", no_argument, 0, 'r'},
4556 {"section-headers", no_argument, 0, 'S'},
4557 {"sections", no_argument, 0, 'S'},
b34976b6
AM
4558 {"symbols", no_argument, 0, 's'},
4559 {"syms", no_argument, 0, 's'},
79bc120c
NC
4560 {"silent-truncation",no_argument, 0, 'T'},
4561 {"section-details", no_argument, 0, 't'},
09c11c86 4562 {"unwind", no_argument, 0, 'u'},
79bc120c
NC
4563 {"version-info", no_argument, 0, 'V'},
4564 {"version", no_argument, 0, 'v'},
4565 {"wide", no_argument, 0, 'W'},
b34976b6 4566 {"hex-dump", required_argument, 0, 'x'},
0e602686 4567 {"decompress", no_argument, 0, 'z'},
252b5132 4568
79bc120c
NC
4569 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
4570 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
4571 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4572 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4573 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
0f03783c 4574 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
79bc120c 4575 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
fd2f0033
TT
4576 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
4577 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
4723351a 4578 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
094e34f2 4579#ifdef ENABLE_LIBCTF
d344b407 4580 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
7d9813f1
NA
4581 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
4582 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
4583 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
094e34f2 4584#endif
7d9813f1 4585
b34976b6 4586 {0, no_argument, 0, 0}
252b5132
RH
4587};
4588
4589static void
2cf0635d 4590usage (FILE * stream)
252b5132 4591{
92f01d61
JM
4592 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
4593 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
4594 fprintf (stream, _(" Options are:\n\
8b53311e
NC
4595 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n\
4596 -h --file-header Display the ELF file header\n\
4597 -l --program-headers Display the program headers\n\
4598 --segments An alias for --program-headers\n\
4599 -S --section-headers Display the sections' header\n\
4600 --sections An alias for --section-headers\n\
f5842774 4601 -g --section-groups Display the section groups\n\
5477e8a0 4602 -t --section-details Display the section details\n\
8b53311e
NC
4603 -e --headers Equivalent to: -h -l -S\n\
4604 -s --syms Display the symbol table\n\
3f08eb35 4605 --symbols An alias for --syms\n\
1b513401 4606 --dyn-syms Display the dynamic symbol table\n\
0f03783c 4607 --lto-syms Display LTO symbol tables\n\
79bc120c
NC
4608 -C --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
4609 The STYLE, if specified, can be `auto' (the default),\n\
4610 `gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
4611 or `gnat'\n\
4612 --no-demangle Do not demangle low-level symbol names. (This is the default)\n\
4613 --recurse-limit Enable a demangling recursion limit. (This is the default)\n\
4614 --no-recurse-limit Disable a demangling recursion limit\n\
8b53311e
NC
4615 -n --notes Display the core notes (if present)\n\
4616 -r --relocs Display the relocations (if present)\n\
4617 -u --unwind Display the unwind info (if present)\n\
b2d38a17 4618 -d --dynamic Display the dynamic section (if present)\n\
8b53311e 4619 -V --version-info Display the version sections (if present)\n\
1b31d05e 4620 -A --arch-specific Display architecture specific information (if any)\n\
4145f1d5 4621 -c --archive-index Display the symbol/file index in an archive\n\
8b53311e 4622 -D --use-dynamic Use the dynamic section info when displaying symbols\n\
1b513401 4623 -L --lint|--enable-checks Display warning messages for possible problems\n\
09c11c86
NC
4624 -x --hex-dump=<number|name>\n\
4625 Dump the contents of section <number|name> as bytes\n\
4626 -p --string-dump=<number|name>\n\
4627 Dump the contents of section <number|name> as strings\n\
cf13d699
NC
4628 -R --relocated-dump=<number|name>\n\
4629 Dump the contents of section <number|name> as relocated bytes\n\
0e602686 4630 -z --decompress Decompress section before dumping it\n\
ca0e11aa 4631 -w[lLiaprmfFsoORtUuTgAc] or\n\
1ed06042 4632 --debug-dump[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
e4b7104b 4633 =frames-interp,=str,=str-offsets,=loc,=Ranges,=pubtypes,\n\
657d0d47 4634 =gdb_index,=trace_info,=trace_abbrev,=trace_aranges,\n\
ca0e11aa
NC
4635 =addr,=cu_index]\n\
4636 Display the contents of DWARF debug sections\n\
4637 -wk,--debug-dump=links Display the contents of sections that link to separate debuginfo files\n\
4638 -P,--process-links Display the contents of non-debug sections in separate debuginfo files. (Implies -wK)\n"));
c46b7066
NC
4639#if DEFAULT_FOR_FOLLOW_LINKS
4640 fprintf (stream, _("\
4641 -wK,--debug-dump=follow-links Follow links to separate debug info files (default)\n\
4642 -wN,--debug-dump=no-follow-links Do not follow links to separate debug info files\n\
4643"));
4644#else
4645 fprintf (stream, _("\
4646 -wK,--debug-dump=follow-links Follow links to separate debug info files\n\
4647 -wN,--debug-dump=no-follow-links Do not follow links to separate debug info files (default)\n\
4648"));
4649#endif
fd2f0033
TT
4650 fprintf (stream, _("\
4651 --dwarf-depth=N Do not display DIEs at depth N or greater\n\
4652 --dwarf-start=N Display DIEs starting with N, at the same depth\n\
4653 or deeper\n"));
094e34f2 4654#ifdef ENABLE_LIBCTF
7d9813f1
NA
4655 fprintf (stream, _("\
4656 --ctf=<number|name> Display CTF info from section <number|name>\n\
4657 --ctf-parent=<number|name>\n\
4658 Use section <number|name> as the CTF parent\n\n\
4659 --ctf-symbols=<number|name>\n\
4660 Use section <number|name> as the CTF external symtab\n\n\
4661 --ctf-strings=<number|name>\n\
4662 Use section <number|name> as the CTF external strtab\n\n"));
094e34f2 4663#endif
7d9813f1 4664
252b5132 4665#ifdef SUPPORT_DISASSEMBLY
92f01d61 4666 fprintf (stream, _("\
09c11c86
NC
4667 -i --instruction-dump=<number|name>\n\
4668 Disassemble the contents of section <number|name>\n"));
252b5132 4669#endif
92f01d61 4670 fprintf (stream, _("\
8b53311e
NC
4671 -I --histogram Display histogram of bucket list lengths\n\
4672 -W --wide Allow output width to exceed 80 characters\n\
0942c7ab 4673 -T --silent-truncation If a symbol name is truncated, do not add a suffix [...]\n\
07012eee 4674 @<file> Read options from <file>\n\
8b53311e
NC
4675 -H --help Display this information\n\
4676 -v --version Display the version number of readelf\n"));
1118d252 4677
92f01d61
JM
4678 if (REPORT_BUGS_TO[0] && stream == stdout)
4679 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
252b5132 4680
92f01d61 4681 exit (stream == stdout ? 0 : 1);
252b5132
RH
4682}
4683
18bd398b
NC
4684/* Record the fact that the user wants the contents of section number
4685 SECTION to be displayed using the method(s) encoded as flags bits
4686 in TYPE. Note, TYPE can be zero if we are creating the array for
4687 the first time. */
4688
252b5132 4689static void
6431e409
AM
4690request_dump_bynumber (struct dump_data *dumpdata,
4691 unsigned int section, dump_type type)
252b5132 4692{
6431e409 4693 if (section >= dumpdata->num_dump_sects)
252b5132 4694 {
2cf0635d 4695 dump_type * new_dump_sects;
252b5132 4696
3f5e193b 4697 new_dump_sects = (dump_type *) calloc (section + 1,
dda8d76d 4698 sizeof (* new_dump_sects));
252b5132
RH
4699
4700 if (new_dump_sects == NULL)
591a748a 4701 error (_("Out of memory allocating dump request table.\n"));
252b5132
RH
4702 else
4703 {
6431e409 4704 if (dumpdata->dump_sects)
21b65bac
NC
4705 {
4706 /* Copy current flag settings. */
6431e409
AM
4707 memcpy (new_dump_sects, dumpdata->dump_sects,
4708 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
252b5132 4709
6431e409 4710 free (dumpdata->dump_sects);
21b65bac 4711 }
252b5132 4712
6431e409
AM
4713 dumpdata->dump_sects = new_dump_sects;
4714 dumpdata->num_dump_sects = section + 1;
252b5132
RH
4715 }
4716 }
4717
6431e409
AM
4718 if (dumpdata->dump_sects)
4719 dumpdata->dump_sects[section] |= type;
252b5132
RH
4720}
4721
aef1f6d0
DJ
4722/* Request a dump by section name. */
4723
4724static void
2cf0635d 4725request_dump_byname (const char * section, dump_type type)
aef1f6d0 4726{
2cf0635d 4727 struct dump_list_entry * new_request;
aef1f6d0 4728
3f5e193b
NC
4729 new_request = (struct dump_list_entry *)
4730 malloc (sizeof (struct dump_list_entry));
aef1f6d0 4731 if (!new_request)
591a748a 4732 error (_("Out of memory allocating dump request table.\n"));
aef1f6d0
DJ
4733
4734 new_request->name = strdup (section);
4735 if (!new_request->name)
591a748a 4736 error (_("Out of memory allocating dump request table.\n"));
aef1f6d0
DJ
4737
4738 new_request->type = type;
4739
4740 new_request->next = dump_sects_byname;
4741 dump_sects_byname = new_request;
4742}
4743
cf13d699 4744static inline void
6431e409 4745request_dump (struct dump_data *dumpdata, dump_type type)
cf13d699
NC
4746{
4747 int section;
4748 char * cp;
4749
015dc7e1 4750 do_dump = true;
cf13d699
NC
4751 section = strtoul (optarg, & cp, 0);
4752
4753 if (! *cp && section >= 0)
6431e409 4754 request_dump_bynumber (dumpdata, section, type);
cf13d699
NC
4755 else
4756 request_dump_byname (optarg, type);
4757}
4758
252b5132 4759static void
6431e409 4760parse_args (struct dump_data *dumpdata, int argc, char ** argv)
252b5132
RH
4761{
4762 int c;
4763
4764 if (argc < 2)
92f01d61 4765 usage (stderr);
252b5132
RH
4766
4767 while ((c = getopt_long
ca0e11aa 4768 (argc, argv, "ACDHILNPR:STVWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
252b5132 4769 {
252b5132
RH
4770 switch (c)
4771 {
4772 case 0:
4773 /* Long options. */
4774 break;
4775 case 'H':
92f01d61 4776 usage (stdout);
252b5132
RH
4777 break;
4778
4779 case 'a':
015dc7e1
AM
4780 do_syms = true;
4781 do_reloc = true;
4782 do_unwind = true;
4783 do_dynamic = true;
4784 do_header = true;
4785 do_sections = true;
4786 do_section_groups = true;
4787 do_segments = true;
4788 do_version = true;
4789 do_histogram = true;
4790 do_arch = true;
4791 do_notes = true;
252b5132 4792 break;
79bc120c 4793
f5842774 4794 case 'g':
015dc7e1 4795 do_section_groups = true;
f5842774 4796 break;
5477e8a0 4797 case 't':
595cf52e 4798 case 'N':
015dc7e1
AM
4799 do_sections = true;
4800 do_section_details = true;
595cf52e 4801 break;
252b5132 4802 case 'e':
015dc7e1
AM
4803 do_header = true;
4804 do_sections = true;
4805 do_segments = true;
252b5132 4806 break;
a952a375 4807 case 'A':
015dc7e1 4808 do_arch = true;
a952a375 4809 break;
252b5132 4810 case 'D':
015dc7e1 4811 do_using_dynamic = true;
252b5132
RH
4812 break;
4813 case 'r':
015dc7e1 4814 do_reloc = true;
252b5132 4815 break;
4d6ed7c8 4816 case 'u':
015dc7e1 4817 do_unwind = true;
4d6ed7c8 4818 break;
252b5132 4819 case 'h':
015dc7e1 4820 do_header = true;
252b5132
RH
4821 break;
4822 case 'l':
015dc7e1 4823 do_segments = true;
252b5132
RH
4824 break;
4825 case 's':
015dc7e1 4826 do_syms = true;
252b5132
RH
4827 break;
4828 case 'S':
015dc7e1 4829 do_sections = true;
252b5132
RH
4830 break;
4831 case 'd':
015dc7e1 4832 do_dynamic = true;
252b5132 4833 break;
a952a375 4834 case 'I':
015dc7e1 4835 do_histogram = true;
a952a375 4836 break;
779fe533 4837 case 'n':
015dc7e1 4838 do_notes = true;
779fe533 4839 break;
4145f1d5 4840 case 'c':
015dc7e1 4841 do_archive_index = true;
4145f1d5 4842 break;
1b513401 4843 case 'L':
015dc7e1 4844 do_checks = true;
1b513401 4845 break;
ca0e11aa 4846 case 'P':
015dc7e1
AM
4847 process_links = true;
4848 do_follow_links = true;
ca0e11aa 4849 break;
252b5132 4850 case 'x':
6431e409 4851 request_dump (dumpdata, HEX_DUMP);
aef1f6d0 4852 break;
09c11c86 4853 case 'p':
6431e409 4854 request_dump (dumpdata, STRING_DUMP);
cf13d699
NC
4855 break;
4856 case 'R':
6431e409 4857 request_dump (dumpdata, RELOC_DUMP);
09c11c86 4858 break;
0e602686 4859 case 'z':
015dc7e1 4860 decompress_dumps = true;
0e602686 4861 break;
252b5132 4862 case 'w':
015dc7e1 4863 do_dump = true;
0f03783c 4864 if (optarg == NULL)
613ff48b 4865 {
015dc7e1 4866 do_debugging = true;
613ff48b
CC
4867 dwarf_select_sections_all ();
4868 }
252b5132
RH
4869 else
4870 {
015dc7e1 4871 do_debugging = false;
4cb93e3b 4872 dwarf_select_sections_by_letters (optarg);
252b5132
RH
4873 }
4874 break;
2979dc34 4875 case OPTION_DEBUG_DUMP:
015dc7e1 4876 do_dump = true;
0f03783c 4877 if (optarg == NULL)
015dc7e1 4878 do_debugging = true;
2979dc34
JJ
4879 else
4880 {
015dc7e1 4881 do_debugging = false;
4cb93e3b 4882 dwarf_select_sections_by_names (optarg);
2979dc34
JJ
4883 }
4884 break;
fd2f0033
TT
4885 case OPTION_DWARF_DEPTH:
4886 {
4887 char *cp;
4888
4889 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
4890 }
4891 break;
4892 case OPTION_DWARF_START:
4893 {
4894 char *cp;
4895
4896 dwarf_start_die = strtoul (optarg, & cp, 0);
4897 }
4898 break;
4723351a 4899 case OPTION_DWARF_CHECK:
015dc7e1 4900 dwarf_check = true;
4723351a 4901 break;
7d9813f1 4902 case OPTION_CTF_DUMP:
015dc7e1 4903 do_ctf = true;
6431e409 4904 request_dump (dumpdata, CTF_DUMP);
7d9813f1
NA
4905 break;
4906 case OPTION_CTF_SYMBOLS:
df16e041 4907 free (dump_ctf_symtab_name);
7d9813f1
NA
4908 dump_ctf_symtab_name = strdup (optarg);
4909 break;
4910 case OPTION_CTF_STRINGS:
df16e041 4911 free (dump_ctf_strtab_name);
7d9813f1
NA
4912 dump_ctf_strtab_name = strdup (optarg);
4913 break;
4914 case OPTION_CTF_PARENT:
df16e041 4915 free (dump_ctf_parent_name);
7d9813f1
NA
4916 dump_ctf_parent_name = strdup (optarg);
4917 break;
2c610e4b 4918 case OPTION_DYN_SYMS:
015dc7e1 4919 do_dyn_syms = true;
2c610e4b 4920 break;
0f03783c 4921 case OPTION_LTO_SYMS:
015dc7e1 4922 do_lto_syms = true;
0f03783c 4923 break;
252b5132
RH
4924#ifdef SUPPORT_DISASSEMBLY
4925 case 'i':
6431e409 4926 request_dump (dumpdata, DISASS_DUMP);
cf13d699 4927 break;
252b5132
RH
4928#endif
4929 case 'v':
4930 print_version (program_name);
4931 break;
4932 case 'V':
015dc7e1 4933 do_version = true;
252b5132 4934 break;
d974e256 4935 case 'W':
015dc7e1 4936 do_wide = true;
d974e256 4937 break;
0942c7ab 4938 case 'T':
015dc7e1 4939 do_not_show_symbol_truncation = true;
0942c7ab 4940 break;
79bc120c 4941 case 'C':
015dc7e1 4942 do_demangle = true;
79bc120c
NC
4943 if (optarg != NULL)
4944 {
4945 enum demangling_styles style;
4946
4947 style = cplus_demangle_name_to_style (optarg);
4948 if (style == unknown_demangling)
4949 error (_("unknown demangling style `%s'"), optarg);
4950
4951 cplus_demangle_set_style (style);
4952 }
4953 break;
4954 case OPTION_NO_DEMANGLING:
015dc7e1 4955 do_demangle = false;
79bc120c
NC
4956 break;
4957 case OPTION_RECURSE_LIMIT:
4958 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
4959 break;
4960 case OPTION_NO_RECURSE_LIMIT:
4961 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
4962 break;
4963 case OPTION_WITH_SYMBOL_VERSIONS:
4964 /* Ignored for backward compatibility. */
4965 break;
b9e920ec 4966
252b5132 4967 default:
252b5132
RH
4968 /* xgettext:c-format */
4969 error (_("Invalid option '-%c'\n"), c);
1a0670f3 4970 /* Fall through. */
252b5132 4971 case '?':
92f01d61 4972 usage (stderr);
252b5132
RH
4973 }
4974 }
4975
4d6ed7c8 4976 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
252b5132 4977 && !do_segments && !do_header && !do_dump && !do_version
f5842774 4978 && !do_histogram && !do_debugging && !do_arch && !do_notes
2c610e4b 4979 && !do_section_groups && !do_archive_index
0f03783c 4980 && !do_dyn_syms && !do_lto_syms)
1b513401
NC
4981 {
4982 if (do_checks)
4983 {
015dc7e1
AM
4984 check_all = true;
4985 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
4986 do_segments = do_header = do_dump = do_version = true;
4987 do_histogram = do_debugging = do_arch = do_notes = true;
4988 do_section_groups = do_archive_index = do_dyn_syms = true;
4989 do_lto_syms = true;
1b513401
NC
4990 }
4991 else
4992 usage (stderr);
4993 }
252b5132
RH
4994}
4995
4996static const char *
d3ba0551 4997get_elf_class (unsigned int elf_class)
252b5132 4998{
b34976b6 4999 static char buff[32];
103f02d3 5000
252b5132
RH
5001 switch (elf_class)
5002 {
5003 case ELFCLASSNONE: return _("none");
e3c8793a
NC
5004 case ELFCLASS32: return "ELF32";
5005 case ELFCLASS64: return "ELF64";
ab5e7794 5006 default:
e9e44622 5007 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
ab5e7794 5008 return buff;
252b5132
RH
5009 }
5010}
5011
5012static const char *
d3ba0551 5013get_data_encoding (unsigned int encoding)
252b5132 5014{
b34976b6 5015 static char buff[32];
103f02d3 5016
252b5132
RH
5017 switch (encoding)
5018 {
5019 case ELFDATANONE: return _("none");
33c63f9d
CM
5020 case ELFDATA2LSB: return _("2's complement, little endian");
5021 case ELFDATA2MSB: return _("2's complement, big endian");
103f02d3 5022 default:
e9e44622 5023 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
ab5e7794 5024 return buff;
252b5132
RH
5025 }
5026}
5027
dda8d76d 5028/* Decode the data held in 'filedata->file_header'. */
ee42cf8c 5029
015dc7e1 5030static bool
dda8d76d 5031process_file_header (Filedata * filedata)
252b5132 5032{
dda8d76d
NC
5033 Elf_Internal_Ehdr * header = & filedata->file_header;
5034
5035 if ( header->e_ident[EI_MAG0] != ELFMAG0
5036 || header->e_ident[EI_MAG1] != ELFMAG1
5037 || header->e_ident[EI_MAG2] != ELFMAG2
5038 || header->e_ident[EI_MAG3] != ELFMAG3)
252b5132
RH
5039 {
5040 error
5041 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
015dc7e1 5042 return false;
252b5132
RH
5043 }
5044
ca0e11aa
NC
5045 if (! filedata->is_separate)
5046 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
2dc4cec1 5047
252b5132
RH
5048 if (do_header)
5049 {
32ec8896 5050 unsigned i;
252b5132 5051
ca0e11aa
NC
5052 if (filedata->is_separate)
5053 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
5054 else
5055 printf (_("ELF Header:\n"));
252b5132 5056 printf (_(" Magic: "));
b34976b6 5057 for (i = 0; i < EI_NIDENT; i++)
dda8d76d 5058 printf ("%2.2x ", header->e_ident[i]);
252b5132
RH
5059 printf ("\n");
5060 printf (_(" Class: %s\n"),
dda8d76d 5061 get_elf_class (header->e_ident[EI_CLASS]));
252b5132 5062 printf (_(" Data: %s\n"),
dda8d76d 5063 get_data_encoding (header->e_ident[EI_DATA]));
e8a64888 5064 printf (_(" Version: %d%s\n"),
dda8d76d
NC
5065 header->e_ident[EI_VERSION],
5066 (header->e_ident[EI_VERSION] == EV_CURRENT
e8a64888 5067 ? _(" (current)")
dda8d76d 5068 : (header->e_ident[EI_VERSION] != EV_NONE
e8a64888 5069 ? _(" <unknown>")
789be9f7 5070 : "")));
252b5132 5071 printf (_(" OS/ABI: %s\n"),
dda8d76d 5072 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
252b5132 5073 printf (_(" ABI Version: %d\n"),
dda8d76d 5074 header->e_ident[EI_ABIVERSION]);
252b5132 5075 printf (_(" Type: %s\n"),
dda8d76d 5076 get_file_type (header->e_type));
252b5132 5077 printf (_(" Machine: %s\n"),
dda8d76d 5078 get_machine_name (header->e_machine));
252b5132 5079 printf (_(" Version: 0x%lx\n"),
e8a64888 5080 header->e_version);
76da6bbe 5081
f7a99963 5082 printf (_(" Entry point address: "));
e8a64888 5083 print_vma (header->e_entry, PREFIX_HEX);
f7a99963 5084 printf (_("\n Start of program headers: "));
e8a64888 5085 print_vma (header->e_phoff, DEC);
f7a99963 5086 printf (_(" (bytes into file)\n Start of section headers: "));
e8a64888 5087 print_vma (header->e_shoff, DEC);
f7a99963 5088 printf (_(" (bytes into file)\n"));
76da6bbe 5089
252b5132 5090 printf (_(" Flags: 0x%lx%s\n"),
e8a64888 5091 header->e_flags,
dda8d76d 5092 get_machine_flags (filedata, header->e_flags, header->e_machine));
e8a64888
AM
5093 printf (_(" Size of this header: %u (bytes)\n"),
5094 header->e_ehsize);
5095 printf (_(" Size of program headers: %u (bytes)\n"),
5096 header->e_phentsize);
5097 printf (_(" Number of program headers: %u"),
5098 header->e_phnum);
dda8d76d
NC
5099 if (filedata->section_headers != NULL
5100 && header->e_phnum == PN_XNUM
5101 && filedata->section_headers[0].sh_info != 0)
e8a64888
AM
5102 {
5103 header->e_phnum = filedata->section_headers[0].sh_info;
5104 printf (" (%u)", header->e_phnum);
5105 }
2046a35d 5106 putc ('\n', stdout);
e8a64888
AM
5107 printf (_(" Size of section headers: %u (bytes)\n"),
5108 header->e_shentsize);
5109 printf (_(" Number of section headers: %u"),
5110 header->e_shnum);
dda8d76d 5111 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
e8a64888
AM
5112 {
5113 header->e_shnum = filedata->section_headers[0].sh_size;
5114 printf (" (%u)", header->e_shnum);
5115 }
560f3c1c 5116 putc ('\n', stdout);
e8a64888
AM
5117 printf (_(" Section header string table index: %u"),
5118 header->e_shstrndx);
dda8d76d
NC
5119 if (filedata->section_headers != NULL
5120 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
e8a64888
AM
5121 {
5122 header->e_shstrndx = filedata->section_headers[0].sh_link;
5123 printf (" (%u)", header->e_shstrndx);
5124 }
5125 if (header->e_shstrndx != SHN_UNDEF
5126 && header->e_shstrndx >= header->e_shnum)
5127 {
5128 header->e_shstrndx = SHN_UNDEF;
5129 printf (_(" <corrupt: out of range>"));
5130 }
560f3c1c
AM
5131 putc ('\n', stdout);
5132 }
5133
dda8d76d 5134 if (filedata->section_headers != NULL)
560f3c1c 5135 {
dda8d76d
NC
5136 if (header->e_phnum == PN_XNUM
5137 && filedata->section_headers[0].sh_info != 0)
5138 header->e_phnum = filedata->section_headers[0].sh_info;
5139 if (header->e_shnum == SHN_UNDEF)
5140 header->e_shnum = filedata->section_headers[0].sh_size;
5141 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
5142 header->e_shstrndx = filedata->section_headers[0].sh_link;
9c1ce108 5143 if (header->e_shstrndx >= header->e_shnum)
dda8d76d
NC
5144 header->e_shstrndx = SHN_UNDEF;
5145 free (filedata->section_headers);
5146 filedata->section_headers = NULL;
252b5132 5147 }
103f02d3 5148
015dc7e1 5149 return true;
9ea033b2
NC
5150}
5151
dda8d76d
NC
5152/* Read in the program headers from FILEDATA and store them in PHEADERS.
5153 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
5154
015dc7e1 5155static bool
dda8d76d 5156get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
9ea033b2 5157{
2cf0635d
NC
5158 Elf32_External_Phdr * phdrs;
5159 Elf32_External_Phdr * external;
5160 Elf_Internal_Phdr * internal;
b34976b6 5161 unsigned int i;
dda8d76d
NC
5162 unsigned int size = filedata->file_header.e_phentsize;
5163 unsigned int num = filedata->file_header.e_phnum;
e0a31db1
NC
5164
5165 /* PR binutils/17531: Cope with unexpected section header sizes. */
5166 if (size == 0 || num == 0)
015dc7e1 5167 return false;
e0a31db1
NC
5168 if (size < sizeof * phdrs)
5169 {
5170 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
015dc7e1 5171 return false;
e0a31db1
NC
5172 }
5173 if (size > sizeof * phdrs)
5174 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
103f02d3 5175
dda8d76d 5176 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
e0a31db1
NC
5177 size, num, _("program headers"));
5178 if (phdrs == NULL)
015dc7e1 5179 return false;
9ea033b2 5180
91d6fa6a 5181 for (i = 0, internal = pheaders, external = phdrs;
dda8d76d 5182 i < filedata->file_header.e_phnum;
b34976b6 5183 i++, internal++, external++)
252b5132 5184 {
9ea033b2
NC
5185 internal->p_type = BYTE_GET (external->p_type);
5186 internal->p_offset = BYTE_GET (external->p_offset);
5187 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5188 internal->p_paddr = BYTE_GET (external->p_paddr);
5189 internal->p_filesz = BYTE_GET (external->p_filesz);
5190 internal->p_memsz = BYTE_GET (external->p_memsz);
5191 internal->p_flags = BYTE_GET (external->p_flags);
5192 internal->p_align = BYTE_GET (external->p_align);
252b5132
RH
5193 }
5194
9ea033b2 5195 free (phdrs);
015dc7e1 5196 return true;
252b5132
RH
5197}
5198
dda8d76d
NC
5199/* Read in the program headers from FILEDATA and store them in PHEADERS.
5200 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
5201
015dc7e1 5202static bool
dda8d76d 5203get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
9ea033b2 5204{
2cf0635d
NC
5205 Elf64_External_Phdr * phdrs;
5206 Elf64_External_Phdr * external;
5207 Elf_Internal_Phdr * internal;
b34976b6 5208 unsigned int i;
dda8d76d
NC
5209 unsigned int size = filedata->file_header.e_phentsize;
5210 unsigned int num = filedata->file_header.e_phnum;
e0a31db1
NC
5211
5212 /* PR binutils/17531: Cope with unexpected section header sizes. */
5213 if (size == 0 || num == 0)
015dc7e1 5214 return false;
e0a31db1
NC
5215 if (size < sizeof * phdrs)
5216 {
5217 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
015dc7e1 5218 return false;
e0a31db1
NC
5219 }
5220 if (size > sizeof * phdrs)
5221 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
103f02d3 5222
dda8d76d 5223 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
e0a31db1 5224 size, num, _("program headers"));
a6e9f9df 5225 if (!phdrs)
015dc7e1 5226 return false;
9ea033b2 5227
91d6fa6a 5228 for (i = 0, internal = pheaders, external = phdrs;
dda8d76d 5229 i < filedata->file_header.e_phnum;
b34976b6 5230 i++, internal++, external++)
9ea033b2
NC
5231 {
5232 internal->p_type = BYTE_GET (external->p_type);
5233 internal->p_flags = BYTE_GET (external->p_flags);
66543521
AM
5234 internal->p_offset = BYTE_GET (external->p_offset);
5235 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5236 internal->p_paddr = BYTE_GET (external->p_paddr);
5237 internal->p_filesz = BYTE_GET (external->p_filesz);
5238 internal->p_memsz = BYTE_GET (external->p_memsz);
5239 internal->p_align = BYTE_GET (external->p_align);
9ea033b2
NC
5240 }
5241
5242 free (phdrs);
015dc7e1 5243 return true;
9ea033b2 5244}
252b5132 5245
32ec8896 5246/* Returns TRUE if the program headers were read into `program_headers'. */
d93f0186 5247
015dc7e1 5248static bool
dda8d76d 5249get_program_headers (Filedata * filedata)
d93f0186 5250{
2cf0635d 5251 Elf_Internal_Phdr * phdrs;
d93f0186
NC
5252
5253 /* Check cache of prior read. */
dda8d76d 5254 if (filedata->program_headers != NULL)
015dc7e1 5255 return true;
d93f0186 5256
82156ab7
NC
5257 /* Be kind to memory checkers by looking for
5258 e_phnum values which we know must be invalid. */
dda8d76d 5259 if (filedata->file_header.e_phnum
82156ab7 5260 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
dda8d76d 5261 >= filedata->file_size)
82156ab7
NC
5262 {
5263 error (_("Too many program headers - %#x - the file is not that big\n"),
dda8d76d 5264 filedata->file_header.e_phnum);
015dc7e1 5265 return false;
82156ab7 5266 }
d93f0186 5267
dda8d76d 5268 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
82156ab7 5269 sizeof (Elf_Internal_Phdr));
d93f0186
NC
5270 if (phdrs == NULL)
5271 {
8b73c356 5272 error (_("Out of memory reading %u program headers\n"),
dda8d76d 5273 filedata->file_header.e_phnum);
015dc7e1 5274 return false;
d93f0186
NC
5275 }
5276
5277 if (is_32bit_elf
dda8d76d
NC
5278 ? get_32bit_program_headers (filedata, phdrs)
5279 : get_64bit_program_headers (filedata, phdrs))
d93f0186 5280 {
dda8d76d 5281 filedata->program_headers = phdrs;
015dc7e1 5282 return true;
d93f0186
NC
5283 }
5284
5285 free (phdrs);
015dc7e1 5286 return false;
d93f0186
NC
5287}
5288
32ec8896 5289/* Returns TRUE if the program headers were loaded. */
2f62977e 5290
015dc7e1 5291static bool
dda8d76d 5292process_program_headers (Filedata * filedata)
252b5132 5293{
2cf0635d 5294 Elf_Internal_Phdr * segment;
b34976b6 5295 unsigned int i;
1a9ccd70 5296 Elf_Internal_Phdr * previous_load = NULL;
252b5132 5297
978c4450
AM
5298 filedata->dynamic_addr = 0;
5299 filedata->dynamic_size = 0;
663f67df 5300
dda8d76d 5301 if (filedata->file_header.e_phnum == 0)
252b5132 5302 {
82f2dbf7 5303 /* PR binutils/12467. */
dda8d76d 5304 if (filedata->file_header.e_phoff != 0)
32ec8896
NC
5305 {
5306 warn (_("possibly corrupt ELF header - it has a non-zero program"
5307 " header offset, but no program headers\n"));
015dc7e1 5308 return false;
32ec8896 5309 }
82f2dbf7 5310 else if (do_segments)
ca0e11aa
NC
5311 {
5312 if (filedata->is_separate)
5313 printf (_("\nThere are no program headers in linked file '%s'.\n"),
5314 filedata->file_name);
5315 else
5316 printf (_("\nThere are no program headers in this file.\n"));
5317 }
015dc7e1 5318 return true;
252b5132
RH
5319 }
5320
5321 if (do_segments && !do_header)
5322 {
ca0e11aa
NC
5323 if (filedata->is_separate)
5324 printf ("\nIn linked file '%s' the ELF file type is %s\n",
5325 filedata->file_name,
5326 get_file_type (filedata->file_header.e_type));
5327 else
5328 printf (_("\nElf file type is %s\n"), get_file_type (filedata->file_header.e_type));
dda8d76d 5329 printf (_("Entry point 0x%s\n"), bfd_vmatoa ("x", filedata->file_header.e_entry));
d3a49aa8
AM
5330 printf (ngettext ("There is %d program header, starting at offset %s\n",
5331 "There are %d program headers, starting at offset %s\n",
dda8d76d
NC
5332 filedata->file_header.e_phnum),
5333 filedata->file_header.e_phnum,
5334 bfd_vmatoa ("u", filedata->file_header.e_phoff));
252b5132
RH
5335 }
5336
dda8d76d 5337 if (! get_program_headers (filedata))
015dc7e1 5338 return true;
103f02d3 5339
252b5132
RH
5340 if (do_segments)
5341 {
dda8d76d 5342 if (filedata->file_header.e_phnum > 1)
3a1a2036
NC
5343 printf (_("\nProgram Headers:\n"));
5344 else
5345 printf (_("\nProgram Headers:\n"));
76da6bbe 5346
f7a99963
NC
5347 if (is_32bit_elf)
5348 printf
5349 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
d974e256
JJ
5350 else if (do_wide)
5351 printf
5352 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
f7a99963
NC
5353 else
5354 {
5355 printf
5356 (_(" Type Offset VirtAddr PhysAddr\n"));
5357 printf
5358 (_(" FileSiz MemSiz Flags Align\n"));
5359 }
252b5132
RH
5360 }
5361
dda8d76d
NC
5362 for (i = 0, segment = filedata->program_headers;
5363 i < filedata->file_header.e_phnum;
b34976b6 5364 i++, segment++)
252b5132
RH
5365 {
5366 if (do_segments)
5367 {
dda8d76d 5368 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
f7a99963
NC
5369
5370 if (is_32bit_elf)
5371 {
5372 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5373 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
5374 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
5375 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
5376 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
5377 printf ("%c%c%c ",
5378 (segment->p_flags & PF_R ? 'R' : ' '),
5379 (segment->p_flags & PF_W ? 'W' : ' '),
5380 (segment->p_flags & PF_X ? 'E' : ' '));
5381 printf ("%#lx", (unsigned long) segment->p_align);
5382 }
d974e256
JJ
5383 else if (do_wide)
5384 {
5385 if ((unsigned long) segment->p_offset == segment->p_offset)
5386 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5387 else
5388 {
5389 print_vma (segment->p_offset, FULL_HEX);
5390 putchar (' ');
5391 }
5392
5393 print_vma (segment->p_vaddr, FULL_HEX);
5394 putchar (' ');
5395 print_vma (segment->p_paddr, FULL_HEX);
5396 putchar (' ');
5397
5398 if ((unsigned long) segment->p_filesz == segment->p_filesz)
5399 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
5400 else
5401 {
5402 print_vma (segment->p_filesz, FULL_HEX);
5403 putchar (' ');
5404 }
5405
5406 if ((unsigned long) segment->p_memsz == segment->p_memsz)
5407 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
5408 else
5409 {
f48e6c45 5410 print_vma (segment->p_memsz, FULL_HEX);
d974e256
JJ
5411 }
5412
5413 printf (" %c%c%c ",
5414 (segment->p_flags & PF_R ? 'R' : ' '),
5415 (segment->p_flags & PF_W ? 'W' : ' '),
5416 (segment->p_flags & PF_X ? 'E' : ' '));
5417
5418 if ((unsigned long) segment->p_align == segment->p_align)
5419 printf ("%#lx", (unsigned long) segment->p_align);
5420 else
5421 {
5422 print_vma (segment->p_align, PREFIX_HEX);
5423 }
5424 }
f7a99963
NC
5425 else
5426 {
5427 print_vma (segment->p_offset, FULL_HEX);
5428 putchar (' ');
5429 print_vma (segment->p_vaddr, FULL_HEX);
5430 putchar (' ');
5431 print_vma (segment->p_paddr, FULL_HEX);
5432 printf ("\n ");
5433 print_vma (segment->p_filesz, FULL_HEX);
5434 putchar (' ');
5435 print_vma (segment->p_memsz, FULL_HEX);
5436 printf (" %c%c%c ",
5437 (segment->p_flags & PF_R ? 'R' : ' '),
5438 (segment->p_flags & PF_W ? 'W' : ' '),
5439 (segment->p_flags & PF_X ? 'E' : ' '));
1d262527 5440 print_vma (segment->p_align, PREFIX_HEX);
f7a99963 5441 }
252b5132 5442
1a9ccd70
NC
5443 putc ('\n', stdout);
5444 }
f54498b4 5445
252b5132
RH
5446 switch (segment->p_type)
5447 {
1a9ccd70 5448 case PT_LOAD:
502d895c
NC
5449#if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
5450 required by the ELF standard, several programs, including the Linux
5451 kernel, make use of non-ordered segments. */
1a9ccd70
NC
5452 if (previous_load
5453 && previous_load->p_vaddr > segment->p_vaddr)
5454 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
502d895c 5455#endif
1a9ccd70
NC
5456 if (segment->p_memsz < segment->p_filesz)
5457 error (_("the segment's file size is larger than its memory size\n"));
5458 previous_load = segment;
5459 break;
5460
5461 case PT_PHDR:
5462 /* PR 20815 - Verify that the program header is loaded into memory. */
5463 if (i > 0 && previous_load != NULL)
5464 error (_("the PHDR segment must occur before any LOAD segment\n"));
dda8d76d 5465 if (filedata->file_header.e_machine != EM_PARISC)
1a9ccd70
NC
5466 {
5467 unsigned int j;
5468
dda8d76d 5469 for (j = 1; j < filedata->file_header.e_phnum; j++)
c0c121b0
AM
5470 {
5471 Elf_Internal_Phdr *load = filedata->program_headers + j;
5472 if (load->p_type == PT_LOAD
5473 && load->p_offset <= segment->p_offset
5474 && (load->p_offset + load->p_filesz
5475 >= segment->p_offset + segment->p_filesz)
5476 && load->p_vaddr <= segment->p_vaddr
5477 && (load->p_vaddr + load->p_filesz
5478 >= segment->p_vaddr + segment->p_filesz))
5479 break;
5480 }
dda8d76d 5481 if (j == filedata->file_header.e_phnum)
1a9ccd70
NC
5482 error (_("the PHDR segment is not covered by a LOAD segment\n"));
5483 }
5484 break;
5485
252b5132 5486 case PT_DYNAMIC:
978c4450 5487 if (filedata->dynamic_addr)
252b5132
RH
5488 error (_("more than one dynamic segment\n"));
5489
20737c13
AM
5490 /* By default, assume that the .dynamic section is the first
5491 section in the DYNAMIC segment. */
978c4450
AM
5492 filedata->dynamic_addr = segment->p_offset;
5493 filedata->dynamic_size = segment->p_filesz;
20737c13 5494
b2d38a17
NC
5495 /* Try to locate the .dynamic section. If there is
5496 a section header table, we can easily locate it. */
dda8d76d 5497 if (filedata->section_headers != NULL)
b2d38a17 5498 {
2cf0635d 5499 Elf_Internal_Shdr * sec;
b2d38a17 5500
dda8d76d 5501 sec = find_section (filedata, ".dynamic");
89fac5e3 5502 if (sec == NULL || sec->sh_size == 0)
b2d38a17 5503 {
28f997cf
TG
5504 /* A corresponding .dynamic section is expected, but on
5505 IA-64/OpenVMS it is OK for it to be missing. */
dda8d76d 5506 if (!is_ia64_vms (filedata))
28f997cf 5507 error (_("no .dynamic section in the dynamic segment\n"));
b2d38a17
NC
5508 break;
5509 }
5510
42bb2e33 5511 if (sec->sh_type == SHT_NOBITS)
20737c13 5512 {
978c4450 5513 filedata->dynamic_size = 0;
20737c13
AM
5514 break;
5515 }
42bb2e33 5516
978c4450
AM
5517 filedata->dynamic_addr = sec->sh_offset;
5518 filedata->dynamic_size = sec->sh_size;
b2d38a17 5519
8ac10c5b
L
5520 /* The PT_DYNAMIC segment, which is used by the run-time
5521 loader, should exactly match the .dynamic section. */
5522 if (do_checks
5523 && (filedata->dynamic_addr != segment->p_offset
5524 || filedata->dynamic_size != segment->p_filesz))
5525 warn (_("\
5526the .dynamic section is not the same as the dynamic segment\n"));
b2d38a17 5527 }
39e224f6
MW
5528
5529 /* PR binutils/17512: Avoid corrupt dynamic section info in the
5530 segment. Check this after matching against the section headers
5531 so we don't warn on debuginfo file (which have NOBITS .dynamic
5532 sections). */
978c4450
AM
5533 if (filedata->dynamic_addr > filedata->file_size
5534 || (filedata->dynamic_size
5535 > filedata->file_size - filedata->dynamic_addr))
39e224f6
MW
5536 {
5537 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
978c4450 5538 filedata->dynamic_addr = filedata->dynamic_size = 0;
39e224f6 5539 }
252b5132
RH
5540 break;
5541
5542 case PT_INTERP:
978c4450
AM
5543 if (fseek (filedata->handle,
5544 filedata->archive_file_offset + (long) segment->p_offset,
fb52b2f4 5545 SEEK_SET))
252b5132
RH
5546 error (_("Unable to find program interpreter name\n"));
5547 else
5548 {
f8eae8b2 5549 char fmt [32];
9495b2e6 5550 int ret = snprintf (fmt, sizeof (fmt), "%%%ds", PATH_MAX - 1);
f8eae8b2
L
5551
5552 if (ret >= (int) sizeof (fmt) || ret < 0)
591a748a 5553 error (_("Internal error: failed to create format string to display program interpreter\n"));
f8eae8b2 5554
978c4450
AM
5555 filedata->program_interpreter[0] = 0;
5556 if (fscanf (filedata->handle, fmt,
5557 filedata->program_interpreter) <= 0)
7bd7b3ef 5558 error (_("Unable to read program interpreter name\n"));
252b5132
RH
5559
5560 if (do_segments)
f54498b4 5561 printf (_(" [Requesting program interpreter: %s]\n"),
978c4450 5562 filedata->program_interpreter);
252b5132
RH
5563 }
5564 break;
5565 }
252b5132
RH
5566 }
5567
dda8d76d
NC
5568 if (do_segments
5569 && filedata->section_headers != NULL
5570 && filedata->string_table != NULL)
252b5132
RH
5571 {
5572 printf (_("\n Section to Segment mapping:\n"));
5573 printf (_(" Segment Sections...\n"));
5574
dda8d76d 5575 for (i = 0; i < filedata->file_header.e_phnum; i++)
252b5132 5576 {
9ad5cbcf 5577 unsigned int j;
2cf0635d 5578 Elf_Internal_Shdr * section;
252b5132 5579
dda8d76d
NC
5580 segment = filedata->program_headers + i;
5581 section = filedata->section_headers + 1;
252b5132
RH
5582
5583 printf (" %2.2d ", i);
5584
dda8d76d 5585 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
252b5132 5586 {
f4638467
AM
5587 if (!ELF_TBSS_SPECIAL (section, segment)
5588 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
dda8d76d 5589 printf ("%s ", printable_section_name (filedata, section));
252b5132
RH
5590 }
5591
5592 putc ('\n',stdout);
5593 }
5594 }
5595
015dc7e1 5596 return true;
252b5132
RH
5597}
5598
5599
d93f0186
NC
5600/* Find the file offset corresponding to VMA by using the program headers. */
5601
5602static long
dda8d76d 5603offset_from_vma (Filedata * filedata, bfd_vma vma, bfd_size_type size)
d93f0186 5604{
2cf0635d 5605 Elf_Internal_Phdr * seg;
d93f0186 5606
dda8d76d 5607 if (! get_program_headers (filedata))
d93f0186
NC
5608 {
5609 warn (_("Cannot interpret virtual addresses without program headers.\n"));
5610 return (long) vma;
5611 }
5612
dda8d76d
NC
5613 for (seg = filedata->program_headers;
5614 seg < filedata->program_headers + filedata->file_header.e_phnum;
d93f0186
NC
5615 ++seg)
5616 {
5617 if (seg->p_type != PT_LOAD)
5618 continue;
5619
5620 if (vma >= (seg->p_vaddr & -seg->p_align)
5621 && vma + size <= seg->p_vaddr + seg->p_filesz)
5622 return vma - seg->p_vaddr + seg->p_offset;
5623 }
5624
5625 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
0af1713e 5626 (unsigned long) vma);
d93f0186
NC
5627 return (long) vma;
5628}
5629
5630
dda8d76d
NC
5631/* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
5632 If PROBE is true, this is just a probe and we do not generate any error
5633 messages if the load fails. */
049b0c3a 5634
015dc7e1
AM
5635static bool
5636get_32bit_section_headers (Filedata * filedata, bool probe)
252b5132 5637{
2cf0635d
NC
5638 Elf32_External_Shdr * shdrs;
5639 Elf_Internal_Shdr * internal;
dda8d76d
NC
5640 unsigned int i;
5641 unsigned int size = filedata->file_header.e_shentsize;
5642 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
049b0c3a
NC
5643
5644 /* PR binutils/17531: Cope with unexpected section header sizes. */
5645 if (size == 0 || num == 0)
015dc7e1 5646 return false;
049b0c3a
NC
5647 if (size < sizeof * shdrs)
5648 {
5649 if (! probe)
5650 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
015dc7e1 5651 return false;
049b0c3a
NC
5652 }
5653 if (!probe && size > sizeof * shdrs)
5654 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
252b5132 5655
dda8d76d 5656 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
049b0c3a
NC
5657 size, num,
5658 probe ? NULL : _("section headers"));
5659 if (shdrs == NULL)
015dc7e1 5660 return false;
252b5132 5661
dda8d76d
NC
5662 free (filedata->section_headers);
5663 filedata->section_headers = (Elf_Internal_Shdr *)
5664 cmalloc (num, sizeof (Elf_Internal_Shdr));
5665 if (filedata->section_headers == NULL)
252b5132 5666 {
049b0c3a 5667 if (!probe)
8b73c356 5668 error (_("Out of memory reading %u section headers\n"), num);
e3d39609 5669 free (shdrs);
015dc7e1 5670 return false;
252b5132
RH
5671 }
5672
dda8d76d 5673 for (i = 0, internal = filedata->section_headers;
560f3c1c 5674 i < num;
b34976b6 5675 i++, internal++)
252b5132
RH
5676 {
5677 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
5678 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
5679 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
5680 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
5681 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
5682 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
5683 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
5684 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
5685 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5686 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
315350be
NC
5687 if (!probe && internal->sh_link > num)
5688 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5689 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5690 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
252b5132
RH
5691 }
5692
5693 free (shdrs);
015dc7e1 5694 return true;
252b5132
RH
5695}
5696
dda8d76d
NC
5697/* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
5698
015dc7e1
AM
5699static bool
5700get_64bit_section_headers (Filedata * filedata, bool probe)
9ea033b2 5701{
dda8d76d
NC
5702 Elf64_External_Shdr * shdrs;
5703 Elf_Internal_Shdr * internal;
5704 unsigned int i;
5705 unsigned int size = filedata->file_header.e_shentsize;
5706 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
049b0c3a
NC
5707
5708 /* PR binutils/17531: Cope with unexpected section header sizes. */
5709 if (size == 0 || num == 0)
015dc7e1 5710 return false;
dda8d76d 5711
049b0c3a
NC
5712 if (size < sizeof * shdrs)
5713 {
5714 if (! probe)
5715 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
015dc7e1 5716 return false;
049b0c3a 5717 }
dda8d76d 5718
049b0c3a
NC
5719 if (! probe && size > sizeof * shdrs)
5720 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
9ea033b2 5721
dda8d76d
NC
5722 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
5723 filedata->file_header.e_shoff,
049b0c3a
NC
5724 size, num,
5725 probe ? NULL : _("section headers"));
5726 if (shdrs == NULL)
015dc7e1 5727 return false;
9ea033b2 5728
dda8d76d
NC
5729 free (filedata->section_headers);
5730 filedata->section_headers = (Elf_Internal_Shdr *)
5731 cmalloc (num, sizeof (Elf_Internal_Shdr));
5732 if (filedata->section_headers == NULL)
9ea033b2 5733 {
049b0c3a 5734 if (! probe)
8b73c356 5735 error (_("Out of memory reading %u section headers\n"), num);
e3d39609 5736 free (shdrs);
015dc7e1 5737 return false;
9ea033b2
NC
5738 }
5739
dda8d76d 5740 for (i = 0, internal = filedata->section_headers;
560f3c1c 5741 i < num;
b34976b6 5742 i++, internal++)
9ea033b2
NC
5743 {
5744 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
5745 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
66543521
AM
5746 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
5747 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
5748 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
5749 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
9ea033b2
NC
5750 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
5751 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
5752 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
5753 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
315350be
NC
5754 if (!probe && internal->sh_link > num)
5755 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5756 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5757 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
9ea033b2
NC
5758 }
5759
5760 free (shdrs);
015dc7e1 5761 return true;
9ea033b2
NC
5762}
5763
252b5132 5764static Elf_Internal_Sym *
dda8d76d
NC
5765get_32bit_elf_symbols (Filedata * filedata,
5766 Elf_Internal_Shdr * section,
5767 unsigned long * num_syms_return)
252b5132 5768{
ba5cdace 5769 unsigned long number = 0;
dd24e3da 5770 Elf32_External_Sym * esyms = NULL;
ba5cdace 5771 Elf_External_Sym_Shndx * shndx = NULL;
dd24e3da 5772 Elf_Internal_Sym * isyms = NULL;
2cf0635d 5773 Elf_Internal_Sym * psym;
b34976b6 5774 unsigned int j;
e3d39609 5775 elf_section_list * entry;
252b5132 5776
c9c1d674
EG
5777 if (section->sh_size == 0)
5778 {
5779 if (num_syms_return != NULL)
5780 * num_syms_return = 0;
5781 return NULL;
5782 }
5783
dd24e3da 5784 /* Run some sanity checks first. */
c9c1d674 5785 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
dd24e3da 5786 {
c9c1d674 5787 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
dda8d76d
NC
5788 printable_section_name (filedata, section),
5789 (unsigned long) section->sh_entsize);
ba5cdace 5790 goto exit_point;
dd24e3da
NC
5791 }
5792
dda8d76d 5793 if (section->sh_size > filedata->file_size)
f54498b4
NC
5794 {
5795 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
dda8d76d
NC
5796 printable_section_name (filedata, section),
5797 (unsigned long) section->sh_size);
f54498b4
NC
5798 goto exit_point;
5799 }
5800
dd24e3da
NC
5801 number = section->sh_size / section->sh_entsize;
5802
5803 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
5804 {
c9c1d674 5805 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
8066deb1 5806 (unsigned long) section->sh_size,
dda8d76d 5807 printable_section_name (filedata, section),
8066deb1 5808 (unsigned long) section->sh_entsize);
ba5cdace 5809 goto exit_point;
dd24e3da
NC
5810 }
5811
dda8d76d 5812 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
3f5e193b 5813 section->sh_size, _("symbols"));
dd24e3da 5814 if (esyms == NULL)
ba5cdace 5815 goto exit_point;
252b5132 5816
e3d39609 5817 shndx = NULL;
978c4450 5818 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
e3d39609
NC
5819 {
5820 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
5821 continue;
5822
5823 if (shndx != NULL)
5824 {
5825 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
5826 free (shndx);
5827 }
5828
5829 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
5830 entry->hdr->sh_offset,
5831 1, entry->hdr->sh_size,
5832 _("symbol table section indices"));
5833 if (shndx == NULL)
5834 goto exit_point;
5835
5836 /* PR17531: file: heap-buffer-overflow */
5837 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
5838 {
5839 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
5840 printable_section_name (filedata, entry->hdr),
5841 (unsigned long) entry->hdr->sh_size,
5842 (unsigned long) section->sh_size);
5843 goto exit_point;
c9c1d674 5844 }
e3d39609 5845 }
9ad5cbcf 5846
3f5e193b 5847 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
252b5132
RH
5848
5849 if (isyms == NULL)
5850 {
8b73c356
NC
5851 error (_("Out of memory reading %lu symbols\n"),
5852 (unsigned long) number);
dd24e3da 5853 goto exit_point;
252b5132
RH
5854 }
5855
dd24e3da 5856 for (j = 0, psym = isyms; j < number; j++, psym++)
252b5132
RH
5857 {
5858 psym->st_name = BYTE_GET (esyms[j].st_name);
5859 psym->st_value = BYTE_GET (esyms[j].st_value);
5860 psym->st_size = BYTE_GET (esyms[j].st_size);
5861 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
4fbb74a6 5862 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
9ad5cbcf
AM
5863 psym->st_shndx
5864 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
4fbb74a6
AM
5865 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
5866 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
252b5132
RH
5867 psym->st_info = BYTE_GET (esyms[j].st_info);
5868 psym->st_other = BYTE_GET (esyms[j].st_other);
5869 }
5870
dd24e3da 5871 exit_point:
e3d39609
NC
5872 free (shndx);
5873 free (esyms);
252b5132 5874
ba5cdace
NC
5875 if (num_syms_return != NULL)
5876 * num_syms_return = isyms == NULL ? 0 : number;
5877
252b5132
RH
5878 return isyms;
5879}
5880
9ea033b2 5881static Elf_Internal_Sym *
dda8d76d
NC
5882get_64bit_elf_symbols (Filedata * filedata,
5883 Elf_Internal_Shdr * section,
5884 unsigned long * num_syms_return)
9ea033b2 5885{
ba5cdace
NC
5886 unsigned long number = 0;
5887 Elf64_External_Sym * esyms = NULL;
5888 Elf_External_Sym_Shndx * shndx = NULL;
5889 Elf_Internal_Sym * isyms = NULL;
2cf0635d 5890 Elf_Internal_Sym * psym;
b34976b6 5891 unsigned int j;
e3d39609 5892 elf_section_list * entry;
9ea033b2 5893
c9c1d674
EG
5894 if (section->sh_size == 0)
5895 {
5896 if (num_syms_return != NULL)
5897 * num_syms_return = 0;
5898 return NULL;
5899 }
5900
dd24e3da 5901 /* Run some sanity checks first. */
c9c1d674 5902 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
dd24e3da 5903 {
c9c1d674 5904 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
dda8d76d 5905 printable_section_name (filedata, section),
8066deb1 5906 (unsigned long) section->sh_entsize);
ba5cdace 5907 goto exit_point;
dd24e3da
NC
5908 }
5909
dda8d76d 5910 if (section->sh_size > filedata->file_size)
f54498b4
NC
5911 {
5912 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
dda8d76d 5913 printable_section_name (filedata, section),
8066deb1 5914 (unsigned long) section->sh_size);
f54498b4
NC
5915 goto exit_point;
5916 }
5917
dd24e3da
NC
5918 number = section->sh_size / section->sh_entsize;
5919
5920 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
5921 {
c9c1d674 5922 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
8066deb1 5923 (unsigned long) section->sh_size,
dda8d76d 5924 printable_section_name (filedata, section),
8066deb1 5925 (unsigned long) section->sh_entsize);
ba5cdace 5926 goto exit_point;
dd24e3da
NC
5927 }
5928
dda8d76d 5929 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
3f5e193b 5930 section->sh_size, _("symbols"));
a6e9f9df 5931 if (!esyms)
ba5cdace 5932 goto exit_point;
9ea033b2 5933
e3d39609 5934 shndx = NULL;
978c4450 5935 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
e3d39609
NC
5936 {
5937 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
5938 continue;
5939
5940 if (shndx != NULL)
5941 {
5942 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
5943 free (shndx);
c9c1d674 5944 }
e3d39609
NC
5945
5946 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
5947 entry->hdr->sh_offset,
5948 1, entry->hdr->sh_size,
5949 _("symbol table section indices"));
5950 if (shndx == NULL)
5951 goto exit_point;
5952
5953 /* PR17531: file: heap-buffer-overflow */
5954 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
5955 {
5956 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
5957 printable_section_name (filedata, entry->hdr),
5958 (unsigned long) entry->hdr->sh_size,
5959 (unsigned long) section->sh_size);
5960 goto exit_point;
5961 }
5962 }
9ad5cbcf 5963
3f5e193b 5964 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
9ea033b2
NC
5965
5966 if (isyms == NULL)
5967 {
8b73c356
NC
5968 error (_("Out of memory reading %lu symbols\n"),
5969 (unsigned long) number);
ba5cdace 5970 goto exit_point;
9ea033b2
NC
5971 }
5972
ba5cdace 5973 for (j = 0, psym = isyms; j < number; j++, psym++)
9ea033b2
NC
5974 {
5975 psym->st_name = BYTE_GET (esyms[j].st_name);
5976 psym->st_info = BYTE_GET (esyms[j].st_info);
5977 psym->st_other = BYTE_GET (esyms[j].st_other);
5978 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
ba5cdace 5979
4fbb74a6 5980 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
9ad5cbcf
AM
5981 psym->st_shndx
5982 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
4fbb74a6
AM
5983 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
5984 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
ba5cdace 5985
66543521
AM
5986 psym->st_value = BYTE_GET (esyms[j].st_value);
5987 psym->st_size = BYTE_GET (esyms[j].st_size);
9ea033b2
NC
5988 }
5989
ba5cdace 5990 exit_point:
e3d39609
NC
5991 free (shndx);
5992 free (esyms);
ba5cdace
NC
5993
5994 if (num_syms_return != NULL)
5995 * num_syms_return = isyms == NULL ? 0 : number;
9ea033b2
NC
5996
5997 return isyms;
5998}
5999
d1133906 6000static const char *
dda8d76d 6001get_elf_section_flags (Filedata * filedata, bfd_vma sh_flags)
d1133906 6002{
5477e8a0 6003 static char buff[1024];
2cf0635d 6004 char * p = buff;
32ec8896
NC
6005 unsigned int field_size = is_32bit_elf ? 8 : 16;
6006 signed int sindex;
6007 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
8d5ff12c
L
6008 bfd_vma os_flags = 0;
6009 bfd_vma proc_flags = 0;
6010 bfd_vma unknown_flags = 0;
148b93f2 6011 static const struct
5477e8a0 6012 {
2cf0635d 6013 const char * str;
32ec8896 6014 unsigned int len;
5477e8a0
L
6015 }
6016 flags [] =
6017 {
cfcac11d
NC
6018 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
6019 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
6020 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
6021 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
6022 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
6023 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
6024 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
6025 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
6026 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
6027 /* 9 */ { STRING_COMMA_LEN ("TLS") },
6028 /* IA-64 specific. */
6029 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
6030 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
6031 /* IA-64 OpenVMS specific. */
6032 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
6033 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
6034 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
6035 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
6036 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
6037 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
18ae9cc1 6038 /* Generic. */
cfcac11d 6039 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
18ae9cc1 6040 /* SPARC specific. */
77115a4a 6041 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
ac4c9b04
MG
6042 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
6043 /* ARM specific. */
6044 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
f0728ee3 6045 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
a91e1603
L
6046 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
6047 /* GNU specific. */
6048 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
83eef883
AFB
6049 /* VLE specific. */
6050 /* 25 */ { STRING_COMMA_LEN ("VLE") },
99fabbc9
JL
6051 /* GNU specific. */
6052 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
5477e8a0
L
6053 };
6054
6055 if (do_section_details)
6056 {
8d5ff12c
L
6057 sprintf (buff, "[%*.*lx]: ",
6058 field_size, field_size, (unsigned long) sh_flags);
6059 p += field_size + 4;
5477e8a0 6060 }
76da6bbe 6061
d1133906
NC
6062 while (sh_flags)
6063 {
6064 bfd_vma flag;
6065
6066 flag = sh_flags & - sh_flags;
6067 sh_flags &= ~ flag;
76da6bbe 6068
5477e8a0 6069 if (do_section_details)
d1133906 6070 {
5477e8a0
L
6071 switch (flag)
6072 {
91d6fa6a
NC
6073 case SHF_WRITE: sindex = 0; break;
6074 case SHF_ALLOC: sindex = 1; break;
6075 case SHF_EXECINSTR: sindex = 2; break;
6076 case SHF_MERGE: sindex = 3; break;
6077 case SHF_STRINGS: sindex = 4; break;
6078 case SHF_INFO_LINK: sindex = 5; break;
6079 case SHF_LINK_ORDER: sindex = 6; break;
6080 case SHF_OS_NONCONFORMING: sindex = 7; break;
6081 case SHF_GROUP: sindex = 8; break;
6082 case SHF_TLS: sindex = 9; break;
18ae9cc1 6083 case SHF_EXCLUDE: sindex = 18; break;
77115a4a 6084 case SHF_COMPRESSED: sindex = 20; break;
76da6bbe 6085
5477e8a0 6086 default:
91d6fa6a 6087 sindex = -1;
dda8d76d 6088 switch (filedata->file_header.e_machine)
148b93f2 6089 {
cfcac11d 6090 case EM_IA_64:
148b93f2 6091 if (flag == SHF_IA_64_SHORT)
91d6fa6a 6092 sindex = 10;
148b93f2 6093 else if (flag == SHF_IA_64_NORECOV)
91d6fa6a 6094 sindex = 11;
148b93f2 6095#ifdef BFD64
dda8d76d 6096 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
148b93f2
NC
6097 switch (flag)
6098 {
91d6fa6a
NC
6099 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
6100 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
6101 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
6102 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
6103 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
6104 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
148b93f2
NC
6105 default: break;
6106 }
6107#endif
cfcac11d
NC
6108 break;
6109
caa83f8b 6110 case EM_386:
22abe556 6111 case EM_IAMCU:
caa83f8b 6112 case EM_X86_64:
7f502d6c 6113 case EM_L1OM:
7a9068fe 6114 case EM_K1OM:
cfcac11d
NC
6115 case EM_OLD_SPARCV9:
6116 case EM_SPARC32PLUS:
6117 case EM_SPARCV9:
6118 case EM_SPARC:
18ae9cc1 6119 if (flag == SHF_ORDERED)
91d6fa6a 6120 sindex = 19;
cfcac11d 6121 break;
ac4c9b04
MG
6122
6123 case EM_ARM:
6124 switch (flag)
6125 {
6126 case SHF_ENTRYSECT: sindex = 21; break;
f0728ee3 6127 case SHF_ARM_PURECODE: sindex = 22; break;
ac4c9b04
MG
6128 case SHF_COMDEF: sindex = 23; break;
6129 default: break;
6130 }
6131 break;
83eef883
AFB
6132 case EM_PPC:
6133 if (flag == SHF_PPC_VLE)
6134 sindex = 25;
6135 break;
99fabbc9
JL
6136 default:
6137 break;
6138 }
ac4c9b04 6139
99fabbc9
JL
6140 switch (filedata->file_header.e_ident[EI_OSABI])
6141 {
6142 case ELFOSABI_GNU:
6143 case ELFOSABI_FREEBSD:
6144 if (flag == SHF_GNU_RETAIN)
6145 sindex = 26;
6146 /* Fall through */
6147 case ELFOSABI_NONE:
6148 if (flag == SHF_GNU_MBIND)
6149 /* We should not recognize SHF_GNU_MBIND for
6150 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6151 not set the EI_OSABI header byte. */
6152 sindex = 24;
6153 break;
cfcac11d
NC
6154 default:
6155 break;
148b93f2 6156 }
99fabbc9 6157 break;
5477e8a0
L
6158 }
6159
91d6fa6a 6160 if (sindex != -1)
5477e8a0 6161 {
8d5ff12c
L
6162 if (p != buff + field_size + 4)
6163 {
6164 if (size < (10 + 2))
bee0ee85
NC
6165 {
6166 warn (_("Internal error: not enough buffer room for section flag info"));
6167 return _("<unknown>");
6168 }
8d5ff12c
L
6169 size -= 2;
6170 *p++ = ',';
6171 *p++ = ' ';
6172 }
6173
91d6fa6a
NC
6174 size -= flags [sindex].len;
6175 p = stpcpy (p, flags [sindex].str);
5477e8a0 6176 }
3b22753a 6177 else if (flag & SHF_MASKOS)
8d5ff12c 6178 os_flags |= flag;
d1133906 6179 else if (flag & SHF_MASKPROC)
8d5ff12c 6180 proc_flags |= flag;
d1133906 6181 else
8d5ff12c 6182 unknown_flags |= flag;
5477e8a0
L
6183 }
6184 else
6185 {
6186 switch (flag)
6187 {
6188 case SHF_WRITE: *p = 'W'; break;
6189 case SHF_ALLOC: *p = 'A'; break;
6190 case SHF_EXECINSTR: *p = 'X'; break;
6191 case SHF_MERGE: *p = 'M'; break;
6192 case SHF_STRINGS: *p = 'S'; break;
6193 case SHF_INFO_LINK: *p = 'I'; break;
6194 case SHF_LINK_ORDER: *p = 'L'; break;
6195 case SHF_OS_NONCONFORMING: *p = 'O'; break;
6196 case SHF_GROUP: *p = 'G'; break;
6197 case SHF_TLS: *p = 'T'; break;
18ae9cc1 6198 case SHF_EXCLUDE: *p = 'E'; break;
77115a4a 6199 case SHF_COMPRESSED: *p = 'C'; break;
5477e8a0
L
6200
6201 default:
dda8d76d
NC
6202 if ((filedata->file_header.e_machine == EM_X86_64
6203 || filedata->file_header.e_machine == EM_L1OM
6204 || filedata->file_header.e_machine == EM_K1OM)
5477e8a0
L
6205 && flag == SHF_X86_64_LARGE)
6206 *p = 'l';
dda8d76d 6207 else if (filedata->file_header.e_machine == EM_ARM
f0728ee3 6208 && flag == SHF_ARM_PURECODE)
99fabbc9 6209 *p = 'y';
dda8d76d 6210 else if (filedata->file_header.e_machine == EM_PPC
83eef883 6211 && flag == SHF_PPC_VLE)
99fabbc9 6212 *p = 'v';
5477e8a0
L
6213 else if (flag & SHF_MASKOS)
6214 {
99fabbc9
JL
6215 switch (filedata->file_header.e_ident[EI_OSABI])
6216 {
6217 case ELFOSABI_GNU:
6218 case ELFOSABI_FREEBSD:
6219 if (flag == SHF_GNU_RETAIN)
6220 {
6221 *p = 'R';
6222 break;
6223 }
6224 /* Fall through */
6225 case ELFOSABI_NONE:
6226 if (flag == SHF_GNU_MBIND)
6227 {
6228 /* We should not recognize SHF_GNU_MBIND for
6229 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6230 not set the EI_OSABI header byte. */
6231 *p = 'D';
6232 break;
6233 }
6234 /* Fall through */
6235 default:
6236 *p = 'o';
6237 sh_flags &= ~SHF_MASKOS;
6238 break;
6239 }
5477e8a0
L
6240 }
6241 else if (flag & SHF_MASKPROC)
6242 {
6243 *p = 'p';
6244 sh_flags &= ~ SHF_MASKPROC;
6245 }
6246 else
6247 *p = 'x';
6248 break;
6249 }
6250 p++;
d1133906
NC
6251 }
6252 }
76da6bbe 6253
8d5ff12c
L
6254 if (do_section_details)
6255 {
6256 if (os_flags)
6257 {
6258 size -= 5 + field_size;
6259 if (p != buff + field_size + 4)
6260 {
6261 if (size < (2 + 1))
bee0ee85
NC
6262 {
6263 warn (_("Internal error: not enough buffer room for section flag info"));
6264 return _("<unknown>");
6265 }
8d5ff12c
L
6266 size -= 2;
6267 *p++ = ',';
6268 *p++ = ' ';
6269 }
6270 sprintf (p, "OS (%*.*lx)", field_size, field_size,
6271 (unsigned long) os_flags);
6272 p += 5 + field_size;
6273 }
6274 if (proc_flags)
6275 {
6276 size -= 7 + field_size;
6277 if (p != buff + field_size + 4)
6278 {
6279 if (size < (2 + 1))
bee0ee85
NC
6280 {
6281 warn (_("Internal error: not enough buffer room for section flag info"));
6282 return _("<unknown>");
6283 }
8d5ff12c
L
6284 size -= 2;
6285 *p++ = ',';
6286 *p++ = ' ';
6287 }
6288 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
6289 (unsigned long) proc_flags);
6290 p += 7 + field_size;
6291 }
6292 if (unknown_flags)
6293 {
6294 size -= 10 + field_size;
6295 if (p != buff + field_size + 4)
6296 {
6297 if (size < (2 + 1))
bee0ee85
NC
6298 {
6299 warn (_("Internal error: not enough buffer room for section flag info"));
6300 return _("<unknown>");
6301 }
8d5ff12c
L
6302 size -= 2;
6303 *p++ = ',';
6304 *p++ = ' ';
6305 }
2b692964 6306 sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
8d5ff12c
L
6307 (unsigned long) unknown_flags);
6308 p += 10 + field_size;
6309 }
6310 }
6311
e9e44622 6312 *p = '\0';
d1133906
NC
6313 return buff;
6314}
6315
5844b465 6316static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
ebdf1ebf 6317get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf, bfd_size_type size)
77115a4a
L
6318{
6319 if (is_32bit_elf)
6320 {
6321 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
d8024a91 6322
ebdf1ebf
NC
6323 if (size < sizeof (* echdr))
6324 {
6325 error (_("Compressed section is too small even for a compression header\n"));
6326 return 0;
6327 }
6328
77115a4a
L
6329 chdr->ch_type = BYTE_GET (echdr->ch_type);
6330 chdr->ch_size = BYTE_GET (echdr->ch_size);
6331 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6332 return sizeof (*echdr);
6333 }
6334 else
6335 {
6336 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
d8024a91 6337
ebdf1ebf
NC
6338 if (size < sizeof (* echdr))
6339 {
6340 error (_("Compressed section is too small even for a compression header\n"));
6341 return 0;
6342 }
6343
77115a4a
L
6344 chdr->ch_type = BYTE_GET (echdr->ch_type);
6345 chdr->ch_size = BYTE_GET (echdr->ch_size);
6346 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6347 return sizeof (*echdr);
6348 }
6349}
6350
015dc7e1 6351static bool
dda8d76d 6352process_section_headers (Filedata * filedata)
252b5132 6353{
2cf0635d 6354 Elf_Internal_Shdr * section;
b34976b6 6355 unsigned int i;
252b5132 6356
8fb879cd 6357 free (filedata->section_headers);
dda8d76d 6358 filedata->section_headers = NULL;
978c4450
AM
6359 free (filedata->dynamic_symbols);
6360 filedata->dynamic_symbols = NULL;
6361 filedata->num_dynamic_syms = 0;
6362 free (filedata->dynamic_strings);
6363 filedata->dynamic_strings = NULL;
6364 filedata->dynamic_strings_length = 0;
6365 free (filedata->dynamic_syminfo);
6366 filedata->dynamic_syminfo = NULL;
6367 while (filedata->symtab_shndx_list != NULL)
8ff66993 6368 {
978c4450
AM
6369 elf_section_list *next = filedata->symtab_shndx_list->next;
6370 free (filedata->symtab_shndx_list);
6371 filedata->symtab_shndx_list = next;
8ff66993 6372 }
252b5132 6373
dda8d76d 6374 if (filedata->file_header.e_shnum == 0)
252b5132 6375 {
82f2dbf7 6376 /* PR binutils/12467. */
dda8d76d 6377 if (filedata->file_header.e_shoff != 0)
32ec8896
NC
6378 {
6379 warn (_("possibly corrupt ELF file header - it has a non-zero"
6380 " section header offset, but no section headers\n"));
015dc7e1 6381 return false;
32ec8896 6382 }
82f2dbf7 6383 else if (do_sections)
252b5132
RH
6384 printf (_("\nThere are no sections in this file.\n"));
6385
015dc7e1 6386 return true;
252b5132
RH
6387 }
6388
6389 if (do_sections && !do_header)
ca0e11aa
NC
6390 {
6391 if (filedata->is_separate && process_links)
6392 printf (_("In linked file '%s': "), filedata->file_name);
6393 if (! filedata->is_separate || process_links)
6394 printf (ngettext ("There is %d section header, "
6395 "starting at offset 0x%lx:\n",
6396 "There are %d section headers, "
6397 "starting at offset 0x%lx:\n",
6398 filedata->file_header.e_shnum),
6399 filedata->file_header.e_shnum,
6400 (unsigned long) filedata->file_header.e_shoff);
6401 }
252b5132 6402
9ea033b2
NC
6403 if (is_32bit_elf)
6404 {
015dc7e1
AM
6405 if (! get_32bit_section_headers (filedata, false))
6406 return false;
32ec8896
NC
6407 }
6408 else
6409 {
015dc7e1
AM
6410 if (! get_64bit_section_headers (filedata, false))
6411 return false;
9ea033b2 6412 }
252b5132
RH
6413
6414 /* Read in the string table, so that we have names to display. */
dda8d76d
NC
6415 if (filedata->file_header.e_shstrndx != SHN_UNDEF
6416 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
252b5132 6417 {
dda8d76d 6418 section = filedata->section_headers + filedata->file_header.e_shstrndx;
d40ac9bd 6419
c256ffe7
JJ
6420 if (section->sh_size != 0)
6421 {
dda8d76d
NC
6422 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
6423 1, section->sh_size,
6424 _("string table"));
0de14b54 6425
dda8d76d 6426 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
c256ffe7 6427 }
252b5132
RH
6428 }
6429
6430 /* Scan the sections for the dynamic symbol table
e3c8793a 6431 and dynamic string table and debug sections. */
89fac5e3 6432 eh_addr_size = is_32bit_elf ? 4 : 8;
dda8d76d 6433 switch (filedata->file_header.e_machine)
89fac5e3
RS
6434 {
6435 case EM_MIPS:
6436 case EM_MIPS_RS3_LE:
6437 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
6438 FDE addresses. However, the ABI also has a semi-official ILP32
6439 variant for which the normal FDE address size rules apply.
6440
6441 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
6442 section, where XX is the size of longs in bits. Unfortunately,
6443 earlier compilers provided no way of distinguishing ILP32 objects
6444 from LP64 objects, so if there's any doubt, we should assume that
6445 the official LP64 form is being used. */
dda8d76d
NC
6446 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
6447 && find_section (filedata, ".gcc_compiled_long32") == NULL)
89fac5e3
RS
6448 eh_addr_size = 8;
6449 break;
0f56a26a
DD
6450
6451 case EM_H8_300:
6452 case EM_H8_300H:
dda8d76d 6453 switch (filedata->file_header.e_flags & EF_H8_MACH)
0f56a26a
DD
6454 {
6455 case E_H8_MACH_H8300:
6456 case E_H8_MACH_H8300HN:
6457 case E_H8_MACH_H8300SN:
6458 case E_H8_MACH_H8300SXN:
6459 eh_addr_size = 2;
6460 break;
6461 case E_H8_MACH_H8300H:
6462 case E_H8_MACH_H8300S:
6463 case E_H8_MACH_H8300SX:
6464 eh_addr_size = 4;
6465 break;
6466 }
f4236fe4
DD
6467 break;
6468
ff7eeb89 6469 case EM_M32C_OLD:
f4236fe4 6470 case EM_M32C:
dda8d76d 6471 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
f4236fe4
DD
6472 {
6473 case EF_M32C_CPU_M16C:
6474 eh_addr_size = 2;
6475 break;
6476 }
6477 break;
89fac5e3
RS
6478 }
6479
76ca31c0
NC
6480#define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
6481 do \
6482 { \
6483 bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64; \
6484 if (section->sh_entsize != expected_entsize) \
9dd3a467 6485 { \
76ca31c0
NC
6486 char buf[40]; \
6487 sprintf_vma (buf, section->sh_entsize); \
6488 /* Note: coded this way so that there is a single string for \
6489 translation. */ \
6490 error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
6491 error (_("(Using the expected size of %u for the rest of this dump)\n"), \
6492 (unsigned) expected_entsize); \
9dd3a467 6493 section->sh_entsize = expected_entsize; \
76ca31c0
NC
6494 } \
6495 } \
08d8fa11 6496 while (0)
9dd3a467
NC
6497
6498#define CHECK_ENTSIZE(section, i, type) \
1b513401 6499 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
08d8fa11
JJ
6500 sizeof (Elf64_External_##type))
6501
dda8d76d
NC
6502 for (i = 0, section = filedata->section_headers;
6503 i < filedata->file_header.e_shnum;
b34976b6 6504 i++, section++)
252b5132 6505 {
b9e920ec 6506 char * name = SECTION_NAME_PRINT (section);
252b5132 6507
1b513401
NC
6508 /* Run some sanity checks on the headers and
6509 possibly fill in some file data as well. */
6510 switch (section->sh_type)
252b5132 6511 {
1b513401 6512 case SHT_DYNSYM:
978c4450 6513 if (filedata->dynamic_symbols != NULL)
252b5132
RH
6514 {
6515 error (_("File contains multiple dynamic symbol tables\n"));
6516 continue;
6517 }
6518
08d8fa11 6519 CHECK_ENTSIZE (section, i, Sym);
978c4450
AM
6520 filedata->dynamic_symbols
6521 = GET_ELF_SYMBOLS (filedata, section, &filedata->num_dynamic_syms);
8ac10c5b 6522 filedata->dynamic_symtab_section = section;
1b513401
NC
6523 break;
6524
6525 case SHT_STRTAB:
6526 if (streq (name, ".dynstr"))
252b5132 6527 {
1b513401
NC
6528 if (filedata->dynamic_strings != NULL)
6529 {
6530 error (_("File contains multiple dynamic string tables\n"));
6531 continue;
6532 }
6533
6534 filedata->dynamic_strings
6535 = (char *) get_data (NULL, filedata, section->sh_offset,
6536 1, section->sh_size, _("dynamic strings"));
6537 filedata->dynamic_strings_length
6538 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
8ac10c5b 6539 filedata->dynamic_strtab_section = section;
252b5132 6540 }
1b513401
NC
6541 break;
6542
6543 case SHT_SYMTAB_SHNDX:
6544 {
6545 elf_section_list * entry = xmalloc (sizeof * entry);
6546
6547 entry->hdr = section;
6548 entry->next = filedata->symtab_shndx_list;
6549 filedata->symtab_shndx_list = entry;
6550 }
6551 break;
6552
6553 case SHT_SYMTAB:
6554 CHECK_ENTSIZE (section, i, Sym);
6555 break;
6556
6557 case SHT_GROUP:
6558 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
6559 break;
252b5132 6560
1b513401
NC
6561 case SHT_REL:
6562 CHECK_ENTSIZE (section, i, Rel);
546cb2d8 6563 if (do_checks && section->sh_size == 0)
1b513401
NC
6564 warn (_("Section '%s': zero-sized relocation section\n"), name);
6565 break;
6566
6567 case SHT_RELA:
6568 CHECK_ENTSIZE (section, i, Rela);
546cb2d8 6569 if (do_checks && section->sh_size == 0)
1b513401
NC
6570 warn (_("Section '%s': zero-sized relocation section\n"), name);
6571 break;
6572
6573 case SHT_NOTE:
6574 case SHT_PROGBITS:
546cb2d8
NC
6575 /* Having a zero sized section is not illegal according to the
6576 ELF standard, but it might be an indication that something
6577 is wrong. So issue a warning if we are running in lint mode. */
6578 if (do_checks && section->sh_size == 0)
1b513401
NC
6579 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
6580 break;
6581
6582 default:
6583 break;
6584 }
6585
6586 if ((do_debugging || do_debug_info || do_debug_abbrevs
6587 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
6588 || do_debug_aranges || do_debug_frames || do_debug_macinfo
e38332c2
NC
6589 || do_debug_str || do_debug_str_offsets || do_debug_loc
6590 || do_debug_ranges
1b513401 6591 || do_debug_addr || do_debug_cu_index || do_debug_links)
24d127aa
ML
6592 && (startswith (name, ".debug_")
6593 || startswith (name, ".zdebug_")))
252b5132 6594 {
1b315056
CS
6595 if (name[1] == 'z')
6596 name += sizeof (".zdebug_") - 1;
6597 else
6598 name += sizeof (".debug_") - 1;
252b5132
RH
6599
6600 if (do_debugging
24d127aa
ML
6601 || (do_debug_info && startswith (name, "info"))
6602 || (do_debug_info && startswith (name, "types"))
6603 || (do_debug_abbrevs && startswith (name, "abbrev"))
b40bf0a2 6604 || (do_debug_lines && strcmp (name, "line") == 0)
24d127aa
ML
6605 || (do_debug_lines && startswith (name, "line."))
6606 || (do_debug_pubnames && startswith (name, "pubnames"))
6607 || (do_debug_pubtypes && startswith (name, "pubtypes"))
6608 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
6609 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
6610 || (do_debug_aranges && startswith (name, "aranges"))
6611 || (do_debug_ranges && startswith (name, "ranges"))
6612 || (do_debug_ranges && startswith (name, "rnglists"))
6613 || (do_debug_frames && startswith (name, "frame"))
6614 || (do_debug_macinfo && startswith (name, "macinfo"))
6615 || (do_debug_macinfo && startswith (name, "macro"))
6616 || (do_debug_str && startswith (name, "str"))
6617 || (do_debug_links && startswith (name, "sup"))
6618 || (do_debug_str_offsets && startswith (name, "str_offsets"))
6619 || (do_debug_loc && startswith (name, "loc"))
6620 || (do_debug_loc && startswith (name, "loclists"))
6621 || (do_debug_addr && startswith (name, "addr"))
6622 || (do_debug_cu_index && startswith (name, "cu_index"))
6623 || (do_debug_cu_index && startswith (name, "tu_index"))
252b5132 6624 )
6431e409 6625 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
252b5132 6626 }
a262ae96 6627 /* Linkonce section to be combined with .debug_info at link time. */
09fd7e38 6628 else if ((do_debugging || do_debug_info)
24d127aa 6629 && startswith (name, ".gnu.linkonce.wi."))
6431e409 6630 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
18bd398b 6631 else if (do_debug_frames && streq (name, ".eh_frame"))
6431e409 6632 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
61364358
JK
6633 else if (do_gdb_index && (streq (name, ".gdb_index")
6634 || streq (name, ".debug_names")))
6431e409 6635 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6f875884
TG
6636 /* Trace sections for Itanium VMS. */
6637 else if ((do_debugging || do_trace_info || do_trace_abbrevs
6638 || do_trace_aranges)
24d127aa 6639 && startswith (name, ".trace_"))
6f875884
TG
6640 {
6641 name += sizeof (".trace_") - 1;
6642
6643 if (do_debugging
6644 || (do_trace_info && streq (name, "info"))
6645 || (do_trace_abbrevs && streq (name, "abbrev"))
6646 || (do_trace_aranges && streq (name, "aranges"))
6647 )
6431e409 6648 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6f875884 6649 }
dda8d76d 6650 else if ((do_debugging || do_debug_links)
24d127aa
ML
6651 && (startswith (name, ".gnu_debuglink")
6652 || startswith (name, ".gnu_debugaltlink")))
6431e409 6653 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
252b5132
RH
6654 }
6655
6656 if (! do_sections)
015dc7e1 6657 return true;
252b5132 6658
ca0e11aa 6659 if (filedata->is_separate && ! process_links)
015dc7e1 6660 return true;
ca0e11aa
NC
6661
6662 if (filedata->is_separate)
6663 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
6664 else if (filedata->file_header.e_shnum > 1)
3a1a2036
NC
6665 printf (_("\nSection Headers:\n"));
6666 else
6667 printf (_("\nSection Header:\n"));
76da6bbe 6668
f7a99963 6669 if (is_32bit_elf)
595cf52e 6670 {
5477e8a0 6671 if (do_section_details)
595cf52e
L
6672 {
6673 printf (_(" [Nr] Name\n"));
5477e8a0 6674 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
595cf52e
L
6675 }
6676 else
6677 printf
6678 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
6679 }
d974e256 6680 else if (do_wide)
595cf52e 6681 {
5477e8a0 6682 if (do_section_details)
595cf52e
L
6683 {
6684 printf (_(" [Nr] Name\n"));
5477e8a0 6685 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
595cf52e
L
6686 }
6687 else
6688 printf
6689 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
6690 }
f7a99963
NC
6691 else
6692 {
5477e8a0 6693 if (do_section_details)
595cf52e
L
6694 {
6695 printf (_(" [Nr] Name\n"));
5477e8a0
L
6696 printf (_(" Type Address Offset Link\n"));
6697 printf (_(" Size EntSize Info Align\n"));
595cf52e
L
6698 }
6699 else
6700 {
6701 printf (_(" [Nr] Name Type Address Offset\n"));
6702 printf (_(" Size EntSize Flags Link Info Align\n"));
6703 }
f7a99963 6704 }
252b5132 6705
5477e8a0
L
6706 if (do_section_details)
6707 printf (_(" Flags\n"));
6708
dda8d76d
NC
6709 for (i = 0, section = filedata->section_headers;
6710 i < filedata->file_header.e_shnum;
b34976b6 6711 i++, section++)
252b5132 6712 {
dd905818
NC
6713 /* Run some sanity checks on the section header. */
6714
6715 /* Check the sh_link field. */
6716 switch (section->sh_type)
6717 {
285e3f99
AM
6718 case SHT_REL:
6719 case SHT_RELA:
6720 if (section->sh_link == 0
6721 && (filedata->file_header.e_type == ET_EXEC
6722 || filedata->file_header.e_type == ET_DYN))
6723 /* A dynamic relocation section where all entries use a
6724 zero symbol index need not specify a symtab section. */
6725 break;
6726 /* Fall through. */
dd905818
NC
6727 case SHT_SYMTAB_SHNDX:
6728 case SHT_GROUP:
6729 case SHT_HASH:
6730 case SHT_GNU_HASH:
6731 case SHT_GNU_versym:
285e3f99 6732 if (section->sh_link == 0
dda8d76d
NC
6733 || section->sh_link >= filedata->file_header.e_shnum
6734 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
6735 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
dd905818
NC
6736 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
6737 i, section->sh_link);
6738 break;
6739
6740 case SHT_DYNAMIC:
6741 case SHT_SYMTAB:
6742 case SHT_DYNSYM:
6743 case SHT_GNU_verneed:
6744 case SHT_GNU_verdef:
6745 case SHT_GNU_LIBLIST:
285e3f99 6746 if (section->sh_link == 0
dda8d76d
NC
6747 || section->sh_link >= filedata->file_header.e_shnum
6748 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
dd905818
NC
6749 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
6750 i, section->sh_link);
6751 break;
6752
6753 case SHT_INIT_ARRAY:
6754 case SHT_FINI_ARRAY:
6755 case SHT_PREINIT_ARRAY:
6756 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
6757 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
6758 i, section->sh_link);
6759 break;
6760
6761 default:
6762 /* FIXME: Add support for target specific section types. */
6763#if 0 /* Currently we do not check other section types as there are too
6764 many special cases. Stab sections for example have a type
6765 of SHT_PROGBITS but an sh_link field that links to the .stabstr
6766 section. */
6767 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
6768 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
6769 i, section->sh_link);
6770#endif
6771 break;
6772 }
6773
6774 /* Check the sh_info field. */
6775 switch (section->sh_type)
6776 {
6777 case SHT_REL:
6778 case SHT_RELA:
285e3f99
AM
6779 if (section->sh_info == 0
6780 && (filedata->file_header.e_type == ET_EXEC
6781 || filedata->file_header.e_type == ET_DYN))
6782 /* Dynamic relocations apply to segments, so they do not
6783 need to specify the section they relocate. */
6784 break;
6785 if (section->sh_info == 0
dda8d76d
NC
6786 || section->sh_info >= filedata->file_header.e_shnum
6787 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
6788 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
6789 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
6790 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
385e5b90
L
6791 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
6792 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
dd905818 6793 /* FIXME: Are other section types valid ? */
dda8d76d 6794 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
285e3f99
AM
6795 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
6796 i, section->sh_info);
dd905818
NC
6797 break;
6798
6799 case SHT_DYNAMIC:
6800 case SHT_HASH:
6801 case SHT_SYMTAB_SHNDX:
6802 case SHT_INIT_ARRAY:
6803 case SHT_FINI_ARRAY:
6804 case SHT_PREINIT_ARRAY:
6805 if (section->sh_info != 0)
6806 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
6807 i, section->sh_info);
6808 break;
6809
6810 case SHT_GROUP:
6811 case SHT_SYMTAB:
6812 case SHT_DYNSYM:
6813 /* A symbol index - we assume that it is valid. */
6814 break;
6815
6816 default:
6817 /* FIXME: Add support for target specific section types. */
6818 if (section->sh_type == SHT_NOBITS)
6819 /* NOBITS section headers with non-zero sh_info fields can be
6820 created when a binary is stripped of everything but its debug
1a9ccd70
NC
6821 information. The stripped sections have their headers
6822 preserved but their types set to SHT_NOBITS. So do not check
6823 this type of section. */
dd905818
NC
6824 ;
6825 else if (section->sh_flags & SHF_INFO_LINK)
6826 {
dda8d76d 6827 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
dd905818
NC
6828 warn (_("[%2u]: Expected link to another section in info field"), i);
6829 }
a91e1603
L
6830 else if (section->sh_type < SHT_LOOS
6831 && (section->sh_flags & SHF_GNU_MBIND) == 0
6832 && section->sh_info != 0)
dd905818
NC
6833 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
6834 i, section->sh_info);
6835 break;
6836 }
6837
3e6b6445 6838 /* Check the sh_size field. */
dda8d76d 6839 if (section->sh_size > filedata->file_size
3e6b6445
NC
6840 && section->sh_type != SHT_NOBITS
6841 && section->sh_type != SHT_NULL
6842 && section->sh_type < SHT_LOOS)
6843 warn (_("Size of section %u is larger than the entire file!\n"), i);
6844
7bfd842d 6845 printf (" [%2u] ", i);
5477e8a0 6846 if (do_section_details)
dda8d76d 6847 printf ("%s\n ", printable_section_name (filedata, section));
595cf52e 6848 else
b9e920ec 6849 print_symbol (-17, SECTION_NAME_PRINT (section));
0b4362b0 6850
ea52a088 6851 printf (do_wide ? " %-15s " : " %-15.15s ",
dda8d76d 6852 get_section_type_name (filedata, section->sh_type));
0b4362b0 6853
f7a99963
NC
6854 if (is_32bit_elf)
6855 {
cfcac11d
NC
6856 const char * link_too_big = NULL;
6857
f7a99963 6858 print_vma (section->sh_addr, LONG_HEX);
76da6bbe 6859
f7a99963
NC
6860 printf ( " %6.6lx %6.6lx %2.2lx",
6861 (unsigned long) section->sh_offset,
6862 (unsigned long) section->sh_size,
6863 (unsigned long) section->sh_entsize);
d1133906 6864
5477e8a0
L
6865 if (do_section_details)
6866 fputs (" ", stdout);
6867 else
dda8d76d 6868 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
76da6bbe 6869
dda8d76d 6870 if (section->sh_link >= filedata->file_header.e_shnum)
cfcac11d
NC
6871 {
6872 link_too_big = "";
6873 /* The sh_link value is out of range. Normally this indicates
caa83f8b 6874 an error but it can have special values in Solaris binaries. */
dda8d76d 6875 switch (filedata->file_header.e_machine)
cfcac11d 6876 {
caa83f8b 6877 case EM_386:
22abe556 6878 case EM_IAMCU:
caa83f8b 6879 case EM_X86_64:
7f502d6c 6880 case EM_L1OM:
7a9068fe 6881 case EM_K1OM:
cfcac11d
NC
6882 case EM_OLD_SPARCV9:
6883 case EM_SPARC32PLUS:
6884 case EM_SPARCV9:
6885 case EM_SPARC:
6886 if (section->sh_link == (SHN_BEFORE & 0xffff))
6887 link_too_big = "BEFORE";
6888 else if (section->sh_link == (SHN_AFTER & 0xffff))
6889 link_too_big = "AFTER";
6890 break;
6891 default:
6892 break;
6893 }
6894 }
6895
6896 if (do_section_details)
6897 {
6898 if (link_too_big != NULL && * link_too_big)
6899 printf ("<%s> ", link_too_big);
6900 else
6901 printf ("%2u ", section->sh_link);
6902 printf ("%3u %2lu\n", section->sh_info,
6903 (unsigned long) section->sh_addralign);
6904 }
6905 else
6906 printf ("%2u %3u %2lu\n",
6907 section->sh_link,
6908 section->sh_info,
6909 (unsigned long) section->sh_addralign);
6910
6911 if (link_too_big && ! * link_too_big)
6912 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
6913 i, section->sh_link);
f7a99963 6914 }
d974e256
JJ
6915 else if (do_wide)
6916 {
6917 print_vma (section->sh_addr, LONG_HEX);
6918
6919 if ((long) section->sh_offset == section->sh_offset)
6920 printf (" %6.6lx", (unsigned long) section->sh_offset);
6921 else
6922 {
6923 putchar (' ');
6924 print_vma (section->sh_offset, LONG_HEX);
6925 }
6926
6927 if ((unsigned long) section->sh_size == section->sh_size)
6928 printf (" %6.6lx", (unsigned long) section->sh_size);
6929 else
6930 {
6931 putchar (' ');
6932 print_vma (section->sh_size, LONG_HEX);
6933 }
6934
6935 if ((unsigned long) section->sh_entsize == section->sh_entsize)
6936 printf (" %2.2lx", (unsigned long) section->sh_entsize);
6937 else
6938 {
6939 putchar (' ');
6940 print_vma (section->sh_entsize, LONG_HEX);
6941 }
6942
5477e8a0
L
6943 if (do_section_details)
6944 fputs (" ", stdout);
6945 else
dda8d76d 6946 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
d974e256 6947
72de5009 6948 printf ("%2u %3u ", section->sh_link, section->sh_info);
d974e256
JJ
6949
6950 if ((unsigned long) section->sh_addralign == section->sh_addralign)
72de5009 6951 printf ("%2lu\n", (unsigned long) section->sh_addralign);
d974e256
JJ
6952 else
6953 {
6954 print_vma (section->sh_addralign, DEC);
6955 putchar ('\n');
6956 }
6957 }
5477e8a0 6958 else if (do_section_details)
595cf52e 6959 {
55cc53e9 6960 putchar (' ');
595cf52e
L
6961 print_vma (section->sh_addr, LONG_HEX);
6962 if ((long) section->sh_offset == section->sh_offset)
5477e8a0 6963 printf (" %16.16lx", (unsigned long) section->sh_offset);
595cf52e
L
6964 else
6965 {
6966 printf (" ");
6967 print_vma (section->sh_offset, LONG_HEX);
6968 }
72de5009 6969 printf (" %u\n ", section->sh_link);
595cf52e 6970 print_vma (section->sh_size, LONG_HEX);
5477e8a0 6971 putchar (' ');
595cf52e
L
6972 print_vma (section->sh_entsize, LONG_HEX);
6973
72de5009
AM
6974 printf (" %-16u %lu\n",
6975 section->sh_info,
595cf52e
L
6976 (unsigned long) section->sh_addralign);
6977 }
f7a99963
NC
6978 else
6979 {
6980 putchar (' ');
6981 print_vma (section->sh_addr, LONG_HEX);
53c7db4b
KH
6982 if ((long) section->sh_offset == section->sh_offset)
6983 printf (" %8.8lx", (unsigned long) section->sh_offset);
6984 else
6985 {
6986 printf (" ");
6987 print_vma (section->sh_offset, LONG_HEX);
6988 }
f7a99963
NC
6989 printf ("\n ");
6990 print_vma (section->sh_size, LONG_HEX);
6991 printf (" ");
6992 print_vma (section->sh_entsize, LONG_HEX);
76da6bbe 6993
dda8d76d 6994 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
76da6bbe 6995
72de5009
AM
6996 printf (" %2u %3u %lu\n",
6997 section->sh_link,
6998 section->sh_info,
f7a99963
NC
6999 (unsigned long) section->sh_addralign);
7000 }
5477e8a0
L
7001
7002 if (do_section_details)
77115a4a 7003 {
dda8d76d 7004 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
77115a4a
L
7005 if ((section->sh_flags & SHF_COMPRESSED) != 0)
7006 {
7007 /* Minimum section size is 12 bytes for 32-bit compression
7008 header + 12 bytes for compressed data header. */
7009 unsigned char buf[24];
d8024a91 7010
77115a4a 7011 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
dda8d76d 7012 if (get_data (&buf, filedata, section->sh_offset, 1,
77115a4a
L
7013 sizeof (buf), _("compression header")))
7014 {
7015 Elf_Internal_Chdr chdr;
d8024a91 7016
5844b465
NC
7017 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
7018 printf (_(" [<corrupt>]\n"));
77115a4a 7019 else
5844b465
NC
7020 {
7021 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
7022 printf (" ZLIB, ");
7023 else
7024 printf (_(" [<unknown>: 0x%x], "),
7025 chdr.ch_type);
7026 print_vma (chdr.ch_size, LONG_HEX);
7027 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
7028 }
77115a4a
L
7029 }
7030 }
7031 }
252b5132
RH
7032 }
7033
5477e8a0 7034 if (!do_section_details)
3dbcc61d 7035 {
9fb71ee4
NC
7036 /* The ordering of the letters shown here matches the ordering of the
7037 corresponding SHF_xxx values, and hence the order in which these
7038 letters will be displayed to the user. */
7039 printf (_("Key to Flags:\n\
7040 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
7041 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
fd85a6a1 7042 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
5424d7ed
L
7043 switch (filedata->file_header.e_ident[EI_OSABI])
7044 {
7045 case ELFOSABI_GNU:
7046 case ELFOSABI_FREEBSD:
7047 printf (_("R (retain), "));
7048 /* Fall through */
7049 case ELFOSABI_NONE:
7050 printf (_("D (mbind), "));
7051 break;
7052 default:
7053 break;
7054 }
dda8d76d
NC
7055 if (filedata->file_header.e_machine == EM_X86_64
7056 || filedata->file_header.e_machine == EM_L1OM
7057 || filedata->file_header.e_machine == EM_K1OM)
9fb71ee4 7058 printf (_("l (large), "));
dda8d76d 7059 else if (filedata->file_header.e_machine == EM_ARM)
f0728ee3 7060 printf (_("y (purecode), "));
dda8d76d 7061 else if (filedata->file_header.e_machine == EM_PPC)
83eef883 7062 printf (_("v (VLE), "));
9fb71ee4 7063 printf ("p (processor specific)\n");
0b4362b0 7064 }
d1133906 7065
015dc7e1 7066 return true;
252b5132
RH
7067}
7068
015dc7e1 7069static bool
28d13567
AM
7070get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
7071 Elf_Internal_Sym **symtab, unsigned long *nsyms,
7072 char **strtab, unsigned long *strtablen)
7073{
7074 *strtab = NULL;
7075 *strtablen = 0;
7076 *symtab = GET_ELF_SYMBOLS (filedata, symsec, nsyms);
7077
7078 if (*symtab == NULL)
015dc7e1 7079 return false;
28d13567
AM
7080
7081 if (symsec->sh_link != 0)
7082 {
7083 Elf_Internal_Shdr *strsec;
7084
7085 if (symsec->sh_link >= filedata->file_header.e_shnum)
7086 {
7087 error (_("Bad sh_link in symbol table section\n"));
7088 free (*symtab);
7089 *symtab = NULL;
7090 *nsyms = 0;
015dc7e1 7091 return false;
28d13567
AM
7092 }
7093
7094 strsec = filedata->section_headers + symsec->sh_link;
7095
7096 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7097 1, strsec->sh_size, _("string table"));
7098 if (*strtab == NULL)
7099 {
7100 free (*symtab);
7101 *symtab = NULL;
7102 *nsyms = 0;
015dc7e1 7103 return false;
28d13567
AM
7104 }
7105 *strtablen = strsec->sh_size;
7106 }
015dc7e1 7107 return true;
28d13567
AM
7108}
7109
f5842774
L
7110static const char *
7111get_group_flags (unsigned int flags)
7112{
1449284b 7113 static char buff[128];
220453ec 7114
6d913794
NC
7115 if (flags == 0)
7116 return "";
7117 else if (flags == GRP_COMDAT)
7118 return "COMDAT ";
f5842774 7119
89246a0e
AM
7120 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
7121 flags,
7122 flags & GRP_MASKOS ? _("<OS specific>") : "",
7123 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
7124 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
7125 ? _("<unknown>") : ""));
6d913794 7126
f5842774
L
7127 return buff;
7128}
7129
015dc7e1 7130static bool
dda8d76d 7131process_section_groups (Filedata * filedata)
f5842774 7132{
2cf0635d 7133 Elf_Internal_Shdr * section;
f5842774 7134 unsigned int i;
2cf0635d
NC
7135 struct group * group;
7136 Elf_Internal_Shdr * symtab_sec;
7137 Elf_Internal_Shdr * strtab_sec;
7138 Elf_Internal_Sym * symtab;
ba5cdace 7139 unsigned long num_syms;
2cf0635d 7140 char * strtab;
c256ffe7 7141 size_t strtab_size;
d1f5c6e3
L
7142
7143 /* Don't process section groups unless needed. */
7144 if (!do_unwind && !do_section_groups)
015dc7e1 7145 return true;
f5842774 7146
dda8d76d 7147 if (filedata->file_header.e_shnum == 0)
f5842774
L
7148 {
7149 if (do_section_groups)
ca0e11aa
NC
7150 {
7151 if (filedata->is_separate)
7152 printf (_("\nThere are no sections group in linked file '%s'.\n"),
7153 filedata->file_name);
7154 else
7155 printf (_("\nThere are no section groups in this file.\n"));
7156 }
015dc7e1 7157 return true;
f5842774
L
7158 }
7159
dda8d76d 7160 if (filedata->section_headers == NULL)
f5842774
L
7161 {
7162 error (_("Section headers are not available!\n"));
fa1908fd 7163 /* PR 13622: This can happen with a corrupt ELF header. */
015dc7e1 7164 return false;
f5842774
L
7165 }
7166
978c4450
AM
7167 filedata->section_headers_groups
7168 = (struct group **) calloc (filedata->file_header.e_shnum,
7169 sizeof (struct group *));
e4b17d5c 7170
978c4450 7171 if (filedata->section_headers_groups == NULL)
e4b17d5c 7172 {
8b73c356 7173 error (_("Out of memory reading %u section group headers\n"),
dda8d76d 7174 filedata->file_header.e_shnum);
015dc7e1 7175 return false;
e4b17d5c
L
7176 }
7177
f5842774 7178 /* Scan the sections for the group section. */
978c4450 7179 filedata->group_count = 0;
dda8d76d
NC
7180 for (i = 0, section = filedata->section_headers;
7181 i < filedata->file_header.e_shnum;
f5842774 7182 i++, section++)
e4b17d5c 7183 if (section->sh_type == SHT_GROUP)
978c4450 7184 filedata->group_count++;
e4b17d5c 7185
978c4450 7186 if (filedata->group_count == 0)
d1f5c6e3
L
7187 {
7188 if (do_section_groups)
ca0e11aa
NC
7189 {
7190 if (filedata->is_separate)
7191 printf (_("\nThere are no section groups in linked file '%s'.\n"),
7192 filedata->file_name);
7193 else
7194 printf (_("\nThere are no section groups in this file.\n"));
7195 }
d1f5c6e3 7196
015dc7e1 7197 return true;
d1f5c6e3
L
7198 }
7199
978c4450
AM
7200 filedata->section_groups = (struct group *) calloc (filedata->group_count,
7201 sizeof (struct group));
e4b17d5c 7202
978c4450 7203 if (filedata->section_groups == NULL)
e4b17d5c 7204 {
8b73c356 7205 error (_("Out of memory reading %lu groups\n"),
978c4450 7206 (unsigned long) filedata->group_count);
015dc7e1 7207 return false;
e4b17d5c
L
7208 }
7209
d1f5c6e3
L
7210 symtab_sec = NULL;
7211 strtab_sec = NULL;
7212 symtab = NULL;
ba5cdace 7213 num_syms = 0;
d1f5c6e3 7214 strtab = NULL;
c256ffe7 7215 strtab_size = 0;
ca0e11aa
NC
7216
7217 if (filedata->is_separate)
7218 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
7219
978c4450 7220 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
dda8d76d 7221 i < filedata->file_header.e_shnum;
e4b17d5c 7222 i++, section++)
f5842774
L
7223 {
7224 if (section->sh_type == SHT_GROUP)
7225 {
dda8d76d 7226 const char * name = printable_section_name (filedata, section);
74e1a04b 7227 const char * group_name;
2cf0635d
NC
7228 unsigned char * start;
7229 unsigned char * indices;
f5842774 7230 unsigned int entry, j, size;
2cf0635d
NC
7231 Elf_Internal_Shdr * sec;
7232 Elf_Internal_Sym * sym;
f5842774
L
7233
7234 /* Get the symbol table. */
dda8d76d
NC
7235 if (section->sh_link >= filedata->file_header.e_shnum
7236 || ((sec = filedata->section_headers + section->sh_link)->sh_type
c256ffe7 7237 != SHT_SYMTAB))
f5842774
L
7238 {
7239 error (_("Bad sh_link in group section `%s'\n"), name);
7240 continue;
7241 }
d1f5c6e3
L
7242
7243 if (symtab_sec != sec)
7244 {
7245 symtab_sec = sec;
9db70fc3 7246 free (symtab);
dda8d76d 7247 symtab = GET_ELF_SYMBOLS (filedata, symtab_sec, & num_syms);
d1f5c6e3 7248 }
f5842774 7249
dd24e3da
NC
7250 if (symtab == NULL)
7251 {
7252 error (_("Corrupt header in group section `%s'\n"), name);
7253 continue;
7254 }
7255
ba5cdace
NC
7256 if (section->sh_info >= num_syms)
7257 {
7258 error (_("Bad sh_info in group section `%s'\n"), name);
7259 continue;
7260 }
7261
f5842774
L
7262 sym = symtab + section->sh_info;
7263
7264 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7265 {
4fbb74a6 7266 if (sym->st_shndx == 0
dda8d76d 7267 || sym->st_shndx >= filedata->file_header.e_shnum)
f5842774
L
7268 {
7269 error (_("Bad sh_info in group section `%s'\n"), name);
7270 continue;
7271 }
ba2685cc 7272
b9e920ec
AM
7273 group_name = SECTION_NAME_PRINT (filedata->section_headers
7274 + sym->st_shndx);
c256ffe7 7275 strtab_sec = NULL;
9db70fc3 7276 free (strtab);
f5842774 7277 strtab = NULL;
c256ffe7 7278 strtab_size = 0;
f5842774
L
7279 }
7280 else
7281 {
7282 /* Get the string table. */
dda8d76d 7283 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
c256ffe7
JJ
7284 {
7285 strtab_sec = NULL;
9db70fc3 7286 free (strtab);
c256ffe7
JJ
7287 strtab = NULL;
7288 strtab_size = 0;
7289 }
7290 else if (strtab_sec
dda8d76d 7291 != (sec = filedata->section_headers + symtab_sec->sh_link))
d1f5c6e3
L
7292 {
7293 strtab_sec = sec;
9db70fc3 7294 free (strtab);
071436c6 7295
dda8d76d 7296 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
071436c6
NC
7297 1, strtab_sec->sh_size,
7298 _("string table"));
c256ffe7 7299 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
d1f5c6e3 7300 }
c256ffe7 7301 group_name = sym->st_name < strtab_size
2b692964 7302 ? strtab + sym->st_name : _("<corrupt>");
f5842774
L
7303 }
7304
c9c1d674
EG
7305 /* PR 17531: file: loop. */
7306 if (section->sh_entsize > section->sh_size)
7307 {
7308 error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
dda8d76d 7309 printable_section_name (filedata, section),
8066deb1
AM
7310 (unsigned long) section->sh_entsize,
7311 (unsigned long) section->sh_size);
61dd8e19 7312 continue;
c9c1d674
EG
7313 }
7314
dda8d76d 7315 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
3f5e193b
NC
7316 1, section->sh_size,
7317 _("section data"));
59245841
NC
7318 if (start == NULL)
7319 continue;
f5842774
L
7320
7321 indices = start;
7322 size = (section->sh_size / section->sh_entsize) - 1;
7323 entry = byte_get (indices, 4);
7324 indices += 4;
e4b17d5c
L
7325
7326 if (do_section_groups)
7327 {
2b692964 7328 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
391cb864 7329 get_group_flags (entry), i, name, group_name, size);
ba2685cc 7330
e4b17d5c
L
7331 printf (_(" [Index] Name\n"));
7332 }
7333
7334 group->group_index = i;
7335
f5842774
L
7336 for (j = 0; j < size; j++)
7337 {
2cf0635d 7338 struct group_list * g;
e4b17d5c 7339
f5842774
L
7340 entry = byte_get (indices, 4);
7341 indices += 4;
7342
dda8d76d 7343 if (entry >= filedata->file_header.e_shnum)
391cb864 7344 {
57028622
NC
7345 static unsigned num_group_errors = 0;
7346
7347 if (num_group_errors ++ < 10)
7348 {
7349 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
dda8d76d 7350 entry, i, filedata->file_header.e_shnum - 1);
57028622 7351 if (num_group_errors == 10)
67ce483b 7352 warn (_("Further error messages about overlarge group section indices suppressed\n"));
57028622 7353 }
391cb864
L
7354 continue;
7355 }
391cb864 7356
978c4450 7357 if (filedata->section_headers_groups [entry] != NULL)
e4b17d5c 7358 {
d1f5c6e3
L
7359 if (entry)
7360 {
57028622
NC
7361 static unsigned num_errs = 0;
7362
7363 if (num_errs ++ < 10)
7364 {
7365 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
7366 entry, i,
978c4450 7367 filedata->section_headers_groups [entry]->group_index);
57028622
NC
7368 if (num_errs == 10)
7369 warn (_("Further error messages about already contained group sections suppressed\n"));
7370 }
d1f5c6e3
L
7371 continue;
7372 }
7373 else
7374 {
7375 /* Intel C/C++ compiler may put section 0 in a
32ec8896 7376 section group. We just warn it the first time
d1f5c6e3 7377 and ignore it afterwards. */
015dc7e1 7378 static bool warned = false;
d1f5c6e3
L
7379 if (!warned)
7380 {
7381 error (_("section 0 in group section [%5u]\n"),
978c4450 7382 filedata->section_headers_groups [entry]->group_index);
015dc7e1 7383 warned = true;
d1f5c6e3
L
7384 }
7385 }
e4b17d5c
L
7386 }
7387
978c4450 7388 filedata->section_headers_groups [entry] = group;
e4b17d5c
L
7389
7390 if (do_section_groups)
7391 {
dda8d76d
NC
7392 sec = filedata->section_headers + entry;
7393 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
ba2685cc
AM
7394 }
7395
3f5e193b 7396 g = (struct group_list *) xmalloc (sizeof (struct group_list));
e4b17d5c
L
7397 g->section_index = entry;
7398 g->next = group->root;
7399 group->root = g;
f5842774
L
7400 }
7401
9db70fc3 7402 free (start);
e4b17d5c
L
7403
7404 group++;
f5842774
L
7405 }
7406 }
7407
9db70fc3
AM
7408 free (symtab);
7409 free (strtab);
015dc7e1 7410 return true;
f5842774
L
7411}
7412
28f997cf
TG
7413/* Data used to display dynamic fixups. */
7414
7415struct ia64_vms_dynfixup
7416{
7417 bfd_vma needed_ident; /* Library ident number. */
7418 bfd_vma needed; /* Index in the dstrtab of the library name. */
7419 bfd_vma fixup_needed; /* Index of the library. */
7420 bfd_vma fixup_rela_cnt; /* Number of fixups. */
7421 bfd_vma fixup_rela_off; /* Fixups offset in the dynamic segment. */
7422};
7423
7424/* Data used to display dynamic relocations. */
7425
7426struct ia64_vms_dynimgrela
7427{
7428 bfd_vma img_rela_cnt; /* Number of relocations. */
7429 bfd_vma img_rela_off; /* Reloc offset in the dynamic segment. */
7430};
7431
7432/* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
7433 library). */
7434
015dc7e1 7435static bool
dda8d76d
NC
7436dump_ia64_vms_dynamic_fixups (Filedata * filedata,
7437 struct ia64_vms_dynfixup * fixup,
7438 const char * strtab,
7439 unsigned int strtab_sz)
28f997cf 7440{
32ec8896 7441 Elf64_External_VMS_IMAGE_FIXUP * imfs;
28f997cf 7442 long i;
32ec8896 7443 const char * lib_name;
28f997cf 7444
978c4450
AM
7445 imfs = get_data (NULL, filedata,
7446 filedata->dynamic_addr + fixup->fixup_rela_off,
95099889 7447 sizeof (*imfs), fixup->fixup_rela_cnt,
28f997cf
TG
7448 _("dynamic section image fixups"));
7449 if (!imfs)
015dc7e1 7450 return false;
28f997cf
TG
7451
7452 if (fixup->needed < strtab_sz)
7453 lib_name = strtab + fixup->needed;
7454 else
7455 {
32ec8896 7456 warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
7f01b0c6 7457 (unsigned long) fixup->needed);
28f997cf
TG
7458 lib_name = "???";
7459 }
736990c4 7460
28f997cf
TG
7461 printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
7462 (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
7463 printf
7464 (_("Seg Offset Type SymVec DataType\n"));
7465
7466 for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
7467 {
7468 unsigned int type;
7469 const char *rtype;
7470
7471 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
7472 printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
7473 type = BYTE_GET (imfs [i].type);
7474 rtype = elf_ia64_reloc_type (type);
7475 if (rtype == NULL)
7476 printf (" 0x%08x ", type);
7477 else
7478 printf (" %-32s ", rtype);
7479 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
7480 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
7481 }
7482
7483 free (imfs);
015dc7e1 7484 return true;
28f997cf
TG
7485}
7486
7487/* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
7488
015dc7e1 7489static bool
dda8d76d 7490dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
28f997cf
TG
7491{
7492 Elf64_External_VMS_IMAGE_RELA *imrs;
7493 long i;
7494
978c4450
AM
7495 imrs = get_data (NULL, filedata,
7496 filedata->dynamic_addr + imgrela->img_rela_off,
95099889 7497 sizeof (*imrs), imgrela->img_rela_cnt,
9cf03b7e 7498 _("dynamic section image relocations"));
28f997cf 7499 if (!imrs)
015dc7e1 7500 return false;
28f997cf
TG
7501
7502 printf (_("\nImage relocs\n"));
7503 printf
7504 (_("Seg Offset Type Addend Seg Sym Off\n"));
7505
7506 for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
7507 {
7508 unsigned int type;
7509 const char *rtype;
7510
7511 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
7512 printf ("%08" BFD_VMA_FMT "x ",
7513 (bfd_vma) BYTE_GET (imrs [i].rela_offset));
7514 type = BYTE_GET (imrs [i].type);
7515 rtype = elf_ia64_reloc_type (type);
7516 if (rtype == NULL)
7517 printf ("0x%08x ", type);
7518 else
7519 printf ("%-31s ", rtype);
7520 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
7521 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
7522 printf ("%08" BFD_VMA_FMT "x\n",
7523 (bfd_vma) BYTE_GET (imrs [i].sym_offset));
7524 }
7525
7526 free (imrs);
015dc7e1 7527 return true;
28f997cf
TG
7528}
7529
7530/* Display IA-64 OpenVMS dynamic relocations and fixups. */
7531
015dc7e1 7532static bool
dda8d76d 7533process_ia64_vms_dynamic_relocs (Filedata * filedata)
28f997cf
TG
7534{
7535 struct ia64_vms_dynfixup fixup;
7536 struct ia64_vms_dynimgrela imgrela;
7537 Elf_Internal_Dyn *entry;
28f997cf
TG
7538 bfd_vma strtab_off = 0;
7539 bfd_vma strtab_sz = 0;
7540 char *strtab = NULL;
015dc7e1 7541 bool res = true;
28f997cf
TG
7542
7543 memset (&fixup, 0, sizeof (fixup));
7544 memset (&imgrela, 0, sizeof (imgrela));
7545
7546 /* Note: the order of the entries is specified by the OpenVMS specs. */
978c4450
AM
7547 for (entry = filedata->dynamic_section;
7548 entry < filedata->dynamic_section + filedata->dynamic_nent;
28f997cf
TG
7549 entry++)
7550 {
7551 switch (entry->d_tag)
7552 {
7553 case DT_IA_64_VMS_STRTAB_OFFSET:
7554 strtab_off = entry->d_un.d_val;
7555 break;
7556 case DT_STRSZ:
7557 strtab_sz = entry->d_un.d_val;
7558 if (strtab == NULL)
978c4450
AM
7559 strtab = get_data (NULL, filedata,
7560 filedata->dynamic_addr + strtab_off,
28f997cf 7561 1, strtab_sz, _("dynamic string section"));
736990c4
NC
7562 if (strtab == NULL)
7563 strtab_sz = 0;
28f997cf
TG
7564 break;
7565
7566 case DT_IA_64_VMS_NEEDED_IDENT:
7567 fixup.needed_ident = entry->d_un.d_val;
7568 break;
7569 case DT_NEEDED:
7570 fixup.needed = entry->d_un.d_val;
7571 break;
7572 case DT_IA_64_VMS_FIXUP_NEEDED:
7573 fixup.fixup_needed = entry->d_un.d_val;
7574 break;
7575 case DT_IA_64_VMS_FIXUP_RELA_CNT:
7576 fixup.fixup_rela_cnt = entry->d_un.d_val;
7577 break;
7578 case DT_IA_64_VMS_FIXUP_RELA_OFF:
7579 fixup.fixup_rela_off = entry->d_un.d_val;
dda8d76d 7580 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
015dc7e1 7581 res = false;
28f997cf 7582 break;
28f997cf
TG
7583 case DT_IA_64_VMS_IMG_RELA_CNT:
7584 imgrela.img_rela_cnt = entry->d_un.d_val;
7585 break;
7586 case DT_IA_64_VMS_IMG_RELA_OFF:
7587 imgrela.img_rela_off = entry->d_un.d_val;
dda8d76d 7588 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
015dc7e1 7589 res = false;
28f997cf
TG
7590 break;
7591
7592 default:
7593 break;
7594 }
7595 }
7596
9db70fc3 7597 free (strtab);
28f997cf
TG
7598
7599 return res;
7600}
7601
85b1c36d 7602static struct
566b0d53 7603{
2cf0635d 7604 const char * name;
566b0d53
L
7605 int reloc;
7606 int size;
7607 int rela;
32ec8896
NC
7608}
7609 dynamic_relocations [] =
566b0d53 7610{
015dc7e1
AM
7611 { "REL", DT_REL, DT_RELSZ, false },
7612 { "RELA", DT_RELA, DT_RELASZ, true },
32ec8896 7613 { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
566b0d53
L
7614};
7615
252b5132 7616/* Process the reloc section. */
18bd398b 7617
015dc7e1 7618static bool
dda8d76d 7619process_relocs (Filedata * filedata)
252b5132 7620{
b34976b6
AM
7621 unsigned long rel_size;
7622 unsigned long rel_offset;
252b5132 7623
252b5132 7624 if (!do_reloc)
015dc7e1 7625 return true;
252b5132
RH
7626
7627 if (do_using_dynamic)
7628 {
32ec8896 7629 int is_rela;
2cf0635d 7630 const char * name;
015dc7e1 7631 bool has_dynamic_reloc;
566b0d53 7632 unsigned int i;
0de14b54 7633
015dc7e1 7634 has_dynamic_reloc = false;
252b5132 7635
566b0d53 7636 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
252b5132 7637 {
566b0d53
L
7638 is_rela = dynamic_relocations [i].rela;
7639 name = dynamic_relocations [i].name;
978c4450
AM
7640 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
7641 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
103f02d3 7642
32ec8896 7643 if (rel_size)
015dc7e1 7644 has_dynamic_reloc = true;
566b0d53
L
7645
7646 if (is_rela == UNKNOWN)
aa903cfb 7647 {
566b0d53 7648 if (dynamic_relocations [i].reloc == DT_JMPREL)
978c4450 7649 switch (filedata->dynamic_info[DT_PLTREL])
566b0d53
L
7650 {
7651 case DT_REL:
015dc7e1 7652 is_rela = false;
566b0d53
L
7653 break;
7654 case DT_RELA:
015dc7e1 7655 is_rela = true;
566b0d53
L
7656 break;
7657 }
aa903cfb 7658 }
252b5132 7659
566b0d53
L
7660 if (rel_size)
7661 {
ca0e11aa
NC
7662 if (filedata->is_separate)
7663 printf
7664 (_("\nIn linked file '%s' section '%s' at offset 0x%lx contains %ld bytes:\n"),
7665 filedata->file_name, name, rel_offset, rel_size);
7666 else
7667 printf
7668 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
7669 name, rel_offset, rel_size);
7670
252b5132 7671
dda8d76d
NC
7672 dump_relocations (filedata,
7673 offset_from_vma (filedata, rel_offset, rel_size),
d93f0186 7674 rel_size,
978c4450
AM
7675 filedata->dynamic_symbols,
7676 filedata->num_dynamic_syms,
7677 filedata->dynamic_strings,
7678 filedata->dynamic_strings_length,
015dc7e1 7679 is_rela, true /* is_dynamic */);
566b0d53 7680 }
252b5132 7681 }
566b0d53 7682
dda8d76d
NC
7683 if (is_ia64_vms (filedata))
7684 if (process_ia64_vms_dynamic_relocs (filedata))
015dc7e1 7685 has_dynamic_reloc = true;
28f997cf 7686
566b0d53 7687 if (! has_dynamic_reloc)
ca0e11aa
NC
7688 {
7689 if (filedata->is_separate)
7690 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
7691 filedata->file_name);
7692 else
7693 printf (_("\nThere are no dynamic relocations in this file.\n"));
7694 }
252b5132
RH
7695 }
7696 else
7697 {
2cf0635d 7698 Elf_Internal_Shdr * section;
b34976b6 7699 unsigned long i;
015dc7e1 7700 bool found = false;
252b5132 7701
dda8d76d
NC
7702 for (i = 0, section = filedata->section_headers;
7703 i < filedata->file_header.e_shnum;
b34976b6 7704 i++, section++)
252b5132
RH
7705 {
7706 if ( section->sh_type != SHT_RELA
7707 && section->sh_type != SHT_REL)
7708 continue;
7709
7710 rel_offset = section->sh_offset;
7711 rel_size = section->sh_size;
7712
7713 if (rel_size)
7714 {
b34976b6 7715 int is_rela;
d3a49aa8 7716 unsigned long num_rela;
103f02d3 7717
ca0e11aa
NC
7718 if (filedata->is_separate)
7719 printf (_("\nIn linked file '%s' relocation section "),
7720 filedata->file_name);
7721 else
7722 printf (_("\nRelocation section "));
252b5132 7723
dda8d76d 7724 if (filedata->string_table == NULL)
19936277 7725 printf ("%d", section->sh_name);
252b5132 7726 else
dda8d76d 7727 printf ("'%s'", printable_section_name (filedata, section));
252b5132 7728
d3a49aa8
AM
7729 num_rela = rel_size / section->sh_entsize;
7730 printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
7731 " at offset 0x%lx contains %lu entries:\n",
7732 num_rela),
7733 rel_offset, num_rela);
252b5132 7734
d79b3d50
NC
7735 is_rela = section->sh_type == SHT_RELA;
7736
4fbb74a6 7737 if (section->sh_link != 0
dda8d76d 7738 && section->sh_link < filedata->file_header.e_shnum)
af3fc3bc 7739 {
2cf0635d
NC
7740 Elf_Internal_Shdr * symsec;
7741 Elf_Internal_Sym * symtab;
d79b3d50 7742 unsigned long nsyms;
c256ffe7 7743 unsigned long strtablen = 0;
2cf0635d 7744 char * strtab = NULL;
57346661 7745
dda8d76d 7746 symsec = filedata->section_headers + section->sh_link;
08d8fa11
JJ
7747 if (symsec->sh_type != SHT_SYMTAB
7748 && symsec->sh_type != SHT_DYNSYM)
7749 continue;
7750
28d13567
AM
7751 if (!get_symtab (filedata, symsec,
7752 &symtab, &nsyms, &strtab, &strtablen))
af3fc3bc 7753 continue;
252b5132 7754
dda8d76d 7755 dump_relocations (filedata, rel_offset, rel_size,
bb4d2ac2
L
7756 symtab, nsyms, strtab, strtablen,
7757 is_rela,
7758 symsec->sh_type == SHT_DYNSYM);
9db70fc3 7759 free (strtab);
d79b3d50
NC
7760 free (symtab);
7761 }
7762 else
dda8d76d 7763 dump_relocations (filedata, rel_offset, rel_size,
32ec8896 7764 NULL, 0, NULL, 0, is_rela,
015dc7e1 7765 false /* is_dynamic */);
252b5132 7766
015dc7e1 7767 found = true;
252b5132
RH
7768 }
7769 }
7770
7771 if (! found)
45ac8f4f
NC
7772 {
7773 /* Users sometimes forget the -D option, so try to be helpful. */
7774 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
7775 {
978c4450 7776 if (filedata->dynamic_info[dynamic_relocations [i].size])
45ac8f4f 7777 {
ca0e11aa
NC
7778 if (filedata->is_separate)
7779 printf (_("\nThere are no static relocations in linked file '%s'."),
7780 filedata->file_name);
7781 else
7782 printf (_("\nThere are no static relocations in this file."));
45ac8f4f
NC
7783 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
7784
7785 break;
7786 }
7787 }
7788 if (i == ARRAY_SIZE (dynamic_relocations))
ca0e11aa
NC
7789 {
7790 if (filedata->is_separate)
7791 printf (_("\nThere are no relocations in linked file '%s'.\n"),
7792 filedata->file_name);
7793 else
7794 printf (_("\nThere are no relocations in this file.\n"));
7795 }
45ac8f4f 7796 }
252b5132
RH
7797 }
7798
015dc7e1 7799 return true;
252b5132
RH
7800}
7801
4d6ed7c8
NC
7802/* An absolute address consists of a section and an offset. If the
7803 section is NULL, the offset itself is the address, otherwise, the
7804 address equals to LOAD_ADDRESS(section) + offset. */
7805
7806struct absaddr
948f632f
DA
7807{
7808 unsigned short section;
7809 bfd_vma offset;
7810};
4d6ed7c8 7811
948f632f
DA
7812/* Find the nearest symbol at or below ADDR. Returns the symbol
7813 name, if found, and the offset from the symbol to ADDR. */
4d6ed7c8 7814
4d6ed7c8 7815static void
dda8d76d
NC
7816find_symbol_for_address (Filedata * filedata,
7817 Elf_Internal_Sym * symtab,
7818 unsigned long nsyms,
7819 const char * strtab,
7820 unsigned long strtab_size,
7821 struct absaddr addr,
7822 const char ** symname,
7823 bfd_vma * offset)
4d6ed7c8 7824{
d3ba0551 7825 bfd_vma dist = 0x100000;
2cf0635d 7826 Elf_Internal_Sym * sym;
948f632f
DA
7827 Elf_Internal_Sym * beg;
7828 Elf_Internal_Sym * end;
2cf0635d 7829 Elf_Internal_Sym * best = NULL;
4d6ed7c8 7830
0b6ae522 7831 REMOVE_ARCH_BITS (addr.offset);
948f632f
DA
7832 beg = symtab;
7833 end = symtab + nsyms;
0b6ae522 7834
948f632f 7835 while (beg < end)
4d6ed7c8 7836 {
948f632f
DA
7837 bfd_vma value;
7838
7839 sym = beg + (end - beg) / 2;
0b6ae522 7840
948f632f 7841 value = sym->st_value;
0b6ae522
DJ
7842 REMOVE_ARCH_BITS (value);
7843
948f632f 7844 if (sym->st_name != 0
4d6ed7c8 7845 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
0b6ae522
DJ
7846 && addr.offset >= value
7847 && addr.offset - value < dist)
4d6ed7c8
NC
7848 {
7849 best = sym;
0b6ae522 7850 dist = addr.offset - value;
4d6ed7c8
NC
7851 if (!dist)
7852 break;
7853 }
948f632f
DA
7854
7855 if (addr.offset < value)
7856 end = sym;
7857 else
7858 beg = sym + 1;
4d6ed7c8 7859 }
1b31d05e 7860
4d6ed7c8
NC
7861 if (best)
7862 {
57346661 7863 *symname = (best->st_name >= strtab_size
2b692964 7864 ? _("<corrupt>") : strtab + best->st_name);
4d6ed7c8
NC
7865 *offset = dist;
7866 return;
7867 }
1b31d05e 7868
4d6ed7c8
NC
7869 *symname = NULL;
7870 *offset = addr.offset;
7871}
7872
32ec8896 7873static /* signed */ int
948f632f
DA
7874symcmp (const void *p, const void *q)
7875{
7876 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
7877 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
7878
7879 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
7880}
7881
7882/* Process the unwind section. */
7883
7884#include "unwind-ia64.h"
7885
7886struct ia64_unw_table_entry
7887{
7888 struct absaddr start;
7889 struct absaddr end;
7890 struct absaddr info;
7891};
7892
7893struct ia64_unw_aux_info
7894{
32ec8896
NC
7895 struct ia64_unw_table_entry * table; /* Unwind table. */
7896 unsigned long table_len; /* Length of unwind table. */
7897 unsigned char * info; /* Unwind info. */
7898 unsigned long info_size; /* Size of unwind info. */
7899 bfd_vma info_addr; /* Starting address of unwind info. */
7900 bfd_vma seg_base; /* Starting address of segment. */
7901 Elf_Internal_Sym * symtab; /* The symbol table. */
7902 unsigned long nsyms; /* Number of symbols. */
7903 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
7904 unsigned long nfuns; /* Number of entries in funtab. */
7905 char * strtab; /* The string table. */
7906 unsigned long strtab_size; /* Size of string table. */
948f632f
DA
7907};
7908
015dc7e1 7909static bool
dda8d76d 7910dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
4d6ed7c8 7911{
2cf0635d 7912 struct ia64_unw_table_entry * tp;
948f632f 7913 unsigned long j, nfuns;
4d6ed7c8 7914 int in_body;
015dc7e1 7915 bool res = true;
7036c0e1 7916
948f632f
DA
7917 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
7918 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
7919 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
7920 aux->funtab[nfuns++] = aux->symtab[j];
7921 aux->nfuns = nfuns;
7922 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
7923
4d6ed7c8
NC
7924 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
7925 {
7926 bfd_vma stamp;
7927 bfd_vma offset;
2cf0635d
NC
7928 const unsigned char * dp;
7929 const unsigned char * head;
53774b7e 7930 const unsigned char * end;
2cf0635d 7931 const char * procname;
4d6ed7c8 7932
dda8d76d 7933 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
57346661 7934 aux->strtab_size, tp->start, &procname, &offset);
4d6ed7c8
NC
7935
7936 fputs ("\n<", stdout);
7937
7938 if (procname)
7939 {
7940 fputs (procname, stdout);
7941
7942 if (offset)
7943 printf ("+%lx", (unsigned long) offset);
7944 }
7945
7946 fputs (">: [", stdout);
7947 print_vma (tp->start.offset, PREFIX_HEX);
7948 fputc ('-', stdout);
7949 print_vma (tp->end.offset, PREFIX_HEX);
86f55779 7950 printf ("], info at +0x%lx\n",
4d6ed7c8
NC
7951 (unsigned long) (tp->info.offset - aux->seg_base));
7952
53774b7e
NC
7953 /* PR 17531: file: 86232b32. */
7954 if (aux->info == NULL)
7955 continue;
7956
97c0a079
AM
7957 offset = tp->info.offset;
7958 if (tp->info.section)
7959 {
7960 if (tp->info.section >= filedata->file_header.e_shnum)
7961 {
7962 warn (_("Invalid section %u in table entry %ld\n"),
7963 tp->info.section, (long) (tp - aux->table));
015dc7e1 7964 res = false;
97c0a079
AM
7965 continue;
7966 }
7967 offset += filedata->section_headers[tp->info.section].sh_addr;
7968 }
7969 offset -= aux->info_addr;
53774b7e 7970 /* PR 17531: file: 0997b4d1. */
90679903
AM
7971 if (offset >= aux->info_size
7972 || aux->info_size - offset < 8)
53774b7e
NC
7973 {
7974 warn (_("Invalid offset %lx in table entry %ld\n"),
7975 (long) tp->info.offset, (long) (tp - aux->table));
015dc7e1 7976 res = false;
53774b7e
NC
7977 continue;
7978 }
7979
97c0a079 7980 head = aux->info + offset;
a4a00738 7981 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
4d6ed7c8 7982
86f55779 7983 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
4d6ed7c8
NC
7984 (unsigned) UNW_VER (stamp),
7985 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
7986 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
7987 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
89fac5e3 7988 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
4d6ed7c8
NC
7989
7990 if (UNW_VER (stamp) != 1)
7991 {
2b692964 7992 printf (_("\tUnknown version.\n"));
4d6ed7c8
NC
7993 continue;
7994 }
7995
7996 in_body = 0;
53774b7e
NC
7997 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
7998 /* PR 17531: file: 16ceda89. */
7999 if (end > aux->info + aux->info_size)
8000 end = aux->info + aux->info_size;
8001 for (dp = head + 8; dp < end;)
b4477bc8 8002 dp = unw_decode (dp, in_body, & in_body, end);
4d6ed7c8 8003 }
948f632f
DA
8004
8005 free (aux->funtab);
32ec8896
NC
8006
8007 return res;
4d6ed7c8
NC
8008}
8009
015dc7e1 8010static bool
dda8d76d
NC
8011slurp_ia64_unwind_table (Filedata * filedata,
8012 struct ia64_unw_aux_info * aux,
8013 Elf_Internal_Shdr * sec)
4d6ed7c8 8014{
89fac5e3 8015 unsigned long size, nrelas, i;
2cf0635d
NC
8016 Elf_Internal_Phdr * seg;
8017 struct ia64_unw_table_entry * tep;
8018 Elf_Internal_Shdr * relsec;
8019 Elf_Internal_Rela * rela;
8020 Elf_Internal_Rela * rp;
8021 unsigned char * table;
8022 unsigned char * tp;
8023 Elf_Internal_Sym * sym;
8024 const char * relname;
4d6ed7c8 8025
53774b7e
NC
8026 aux->table_len = 0;
8027
4d6ed7c8
NC
8028 /* First, find the starting address of the segment that includes
8029 this section: */
8030
dda8d76d 8031 if (filedata->file_header.e_phnum)
4d6ed7c8 8032 {
dda8d76d 8033 if (! get_program_headers (filedata))
015dc7e1 8034 return false;
4d6ed7c8 8035
dda8d76d
NC
8036 for (seg = filedata->program_headers;
8037 seg < filedata->program_headers + filedata->file_header.e_phnum;
d93f0186 8038 ++seg)
4d6ed7c8
NC
8039 {
8040 if (seg->p_type != PT_LOAD)
8041 continue;
8042
8043 if (sec->sh_addr >= seg->p_vaddr
8044 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8045 {
8046 aux->seg_base = seg->p_vaddr;
8047 break;
8048 }
8049 }
4d6ed7c8
NC
8050 }
8051
8052 /* Second, build the unwind table from the contents of the unwind section: */
8053 size = sec->sh_size;
dda8d76d 8054 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
3f5e193b 8055 _("unwind table"));
a6e9f9df 8056 if (!table)
015dc7e1 8057 return false;
4d6ed7c8 8058
53774b7e 8059 aux->table_len = size / (3 * eh_addr_size);
3f5e193b 8060 aux->table = (struct ia64_unw_table_entry *)
53774b7e 8061 xcmalloc (aux->table_len, sizeof (aux->table[0]));
89fac5e3 8062 tep = aux->table;
53774b7e
NC
8063
8064 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
4d6ed7c8
NC
8065 {
8066 tep->start.section = SHN_UNDEF;
8067 tep->end.section = SHN_UNDEF;
8068 tep->info.section = SHN_UNDEF;
c6a0c689
AM
8069 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8070 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8071 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
4d6ed7c8
NC
8072 tep->start.offset += aux->seg_base;
8073 tep->end.offset += aux->seg_base;
8074 tep->info.offset += aux->seg_base;
8075 }
8076 free (table);
8077
41e92641 8078 /* Third, apply any relocations to the unwind table: */
dda8d76d
NC
8079 for (relsec = filedata->section_headers;
8080 relsec < filedata->section_headers + filedata->file_header.e_shnum;
4d6ed7c8
NC
8081 ++relsec)
8082 {
8083 if (relsec->sh_type != SHT_RELA
dda8d76d
NC
8084 || relsec->sh_info >= filedata->file_header.e_shnum
8085 || filedata->section_headers + relsec->sh_info != sec)
4d6ed7c8
NC
8086 continue;
8087
dda8d76d 8088 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
4d6ed7c8 8089 & rela, & nrelas))
53774b7e
NC
8090 {
8091 free (aux->table);
8092 aux->table = NULL;
8093 aux->table_len = 0;
015dc7e1 8094 return false;
53774b7e 8095 }
4d6ed7c8
NC
8096
8097 for (rp = rela; rp < rela + nrelas; ++rp)
8098 {
4770fb94 8099 unsigned int sym_ndx;
726bd37d
AM
8100 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8101 relname = elf_ia64_reloc_type (r_type);
4d6ed7c8 8102
82b1b41b
NC
8103 /* PR 17531: file: 9fa67536. */
8104 if (relname == NULL)
8105 {
726bd37d 8106 warn (_("Skipping unknown relocation type: %u\n"), r_type);
82b1b41b
NC
8107 continue;
8108 }
948f632f 8109
24d127aa 8110 if (! startswith (relname, "R_IA64_SEGREL"))
4d6ed7c8 8111 {
82b1b41b 8112 warn (_("Skipping unexpected relocation type: %s\n"), relname);
4d6ed7c8
NC
8113 continue;
8114 }
8115
89fac5e3 8116 i = rp->r_offset / (3 * eh_addr_size);
4d6ed7c8 8117
53774b7e
NC
8118 /* PR 17531: file: 5bc8d9bf. */
8119 if (i >= aux->table_len)
8120 {
8121 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8122 continue;
8123 }
8124
4770fb94
AM
8125 sym_ndx = get_reloc_symindex (rp->r_info);
8126 if (sym_ndx >= aux->nsyms)
8127 {
8128 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8129 sym_ndx);
8130 continue;
8131 }
8132 sym = aux->symtab + sym_ndx;
8133
53774b7e 8134 switch (rp->r_offset / eh_addr_size % 3)
4d6ed7c8
NC
8135 {
8136 case 0:
8137 aux->table[i].start.section = sym->st_shndx;
e466bc6e 8138 aux->table[i].start.offset = rp->r_addend + sym->st_value;
4d6ed7c8
NC
8139 break;
8140 case 1:
8141 aux->table[i].end.section = sym->st_shndx;
e466bc6e 8142 aux->table[i].end.offset = rp->r_addend + sym->st_value;
4d6ed7c8
NC
8143 break;
8144 case 2:
8145 aux->table[i].info.section = sym->st_shndx;
e466bc6e 8146 aux->table[i].info.offset = rp->r_addend + sym->st_value;
4d6ed7c8
NC
8147 break;
8148 default:
8149 break;
8150 }
8151 }
8152
8153 free (rela);
8154 }
8155
015dc7e1 8156 return true;
4d6ed7c8
NC
8157}
8158
015dc7e1 8159static bool
dda8d76d 8160ia64_process_unwind (Filedata * filedata)
4d6ed7c8 8161{
2cf0635d
NC
8162 Elf_Internal_Shdr * sec;
8163 Elf_Internal_Shdr * unwsec = NULL;
89fac5e3 8164 unsigned long i, unwcount = 0, unwstart = 0;
57346661 8165 struct ia64_unw_aux_info aux;
015dc7e1 8166 bool res = true;
f1467e33 8167
4d6ed7c8
NC
8168 memset (& aux, 0, sizeof (aux));
8169
dda8d76d 8170 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
4d6ed7c8 8171 {
28d13567 8172 if (sec->sh_type == SHT_SYMTAB)
4d6ed7c8 8173 {
28d13567 8174 if (aux.symtab)
4082ef84 8175 {
28d13567
AM
8176 error (_("Multiple symbol tables encountered\n"));
8177 free (aux.symtab);
8178 aux.symtab = NULL;
4082ef84 8179 free (aux.strtab);
28d13567 8180 aux.strtab = NULL;
4082ef84 8181 }
28d13567
AM
8182 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8183 &aux.strtab, &aux.strtab_size))
015dc7e1 8184 return false;
4d6ed7c8
NC
8185 }
8186 else if (sec->sh_type == SHT_IA_64_UNWIND)
579f31ac
JJ
8187 unwcount++;
8188 }
8189
8190 if (!unwcount)
8191 printf (_("\nThere are no unwind sections in this file.\n"));
8192
8193 while (unwcount-- > 0)
8194 {
2cf0635d 8195 char * suffix;
579f31ac
JJ
8196 size_t len, len2;
8197
dda8d76d
NC
8198 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
8199 i < filedata->file_header.e_shnum; ++i, ++sec)
579f31ac
JJ
8200 if (sec->sh_type == SHT_IA_64_UNWIND)
8201 {
8202 unwsec = sec;
8203 break;
8204 }
4082ef84
NC
8205 /* We have already counted the number of SHT_IA64_UNWIND
8206 sections so the loop above should never fail. */
8207 assert (unwsec != NULL);
579f31ac
JJ
8208
8209 unwstart = i + 1;
8210 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
8211
e4b17d5c
L
8212 if ((unwsec->sh_flags & SHF_GROUP) != 0)
8213 {
8214 /* We need to find which section group it is in. */
4082ef84 8215 struct group_list * g;
e4b17d5c 8216
978c4450
AM
8217 if (filedata->section_headers_groups == NULL
8218 || filedata->section_headers_groups[i] == NULL)
dda8d76d 8219 i = filedata->file_header.e_shnum;
4082ef84 8220 else
e4b17d5c 8221 {
978c4450 8222 g = filedata->section_headers_groups[i]->root;
18bd398b 8223
4082ef84
NC
8224 for (; g != NULL; g = g->next)
8225 {
dda8d76d 8226 sec = filedata->section_headers + g->section_index;
e4b17d5c 8227
b9e920ec
AM
8228 if (SECTION_NAME_VALID (sec)
8229 && streq (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info))
4082ef84
NC
8230 break;
8231 }
8232
8233 if (g == NULL)
dda8d76d 8234 i = filedata->file_header.e_shnum;
4082ef84 8235 }
e4b17d5c 8236 }
b9e920ec 8237 else if (SECTION_NAME_VALID (unwsec)
e9b095a5
ML
8238 && startswith (SECTION_NAME (unwsec),
8239 ELF_STRING_ia64_unwind_once))
579f31ac 8240 {
18bd398b 8241 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
579f31ac
JJ
8242 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
8243 suffix = SECTION_NAME (unwsec) + len;
b9e920ec
AM
8244 for (i = 0, sec = filedata->section_headers;
8245 i < filedata->file_header.e_shnum;
579f31ac 8246 ++i, ++sec)
b9e920ec 8247 if (SECTION_NAME_VALID (sec)
e9b095a5
ML
8248 && startswith (SECTION_NAME (sec),
8249 ELF_STRING_ia64_unwind_info_once)
18bd398b 8250 && streq (SECTION_NAME (sec) + len2, suffix))
579f31ac
JJ
8251 break;
8252 }
8253 else
8254 {
8255 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
18bd398b 8256 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
579f31ac
JJ
8257 len = sizeof (ELF_STRING_ia64_unwind) - 1;
8258 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
8259 suffix = "";
b9e920ec 8260 if (SECTION_NAME_VALID (unwsec)
e9b095a5 8261 && startswith (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind))
579f31ac 8262 suffix = SECTION_NAME (unwsec) + len;
b9e920ec
AM
8263 for (i = 0, sec = filedata->section_headers;
8264 i < filedata->file_header.e_shnum;
579f31ac 8265 ++i, ++sec)
b9e920ec 8266 if (SECTION_NAME_VALID (sec)
e9b095a5 8267 && startswith (SECTION_NAME (sec), ELF_STRING_ia64_unwind_info)
18bd398b 8268 && streq (SECTION_NAME (sec) + len2, suffix))
579f31ac
JJ
8269 break;
8270 }
8271
dda8d76d 8272 if (i == filedata->file_header.e_shnum)
579f31ac
JJ
8273 {
8274 printf (_("\nCould not find unwind info section for "));
8275
dda8d76d 8276 if (filedata->string_table == NULL)
579f31ac
JJ
8277 printf ("%d", unwsec->sh_name);
8278 else
dda8d76d 8279 printf ("'%s'", printable_section_name (filedata, unwsec));
579f31ac
JJ
8280 }
8281 else
4d6ed7c8 8282 {
4d6ed7c8 8283 aux.info_addr = sec->sh_addr;
dda8d76d 8284 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
4082ef84
NC
8285 sec->sh_size,
8286 _("unwind info"));
59245841 8287 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
4d6ed7c8 8288
579f31ac 8289 printf (_("\nUnwind section "));
4d6ed7c8 8290
dda8d76d 8291 if (filedata->string_table == NULL)
579f31ac
JJ
8292 printf ("%d", unwsec->sh_name);
8293 else
dda8d76d 8294 printf ("'%s'", printable_section_name (filedata, unwsec));
4d6ed7c8 8295
579f31ac 8296 printf (_(" at offset 0x%lx contains %lu entries:\n"),
e59b4dfb 8297 (unsigned long) unwsec->sh_offset,
89fac5e3 8298 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
4d6ed7c8 8299
dda8d76d 8300 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
53774b7e 8301 && aux.table_len > 0)
dda8d76d 8302 dump_ia64_unwind (filedata, & aux);
579f31ac 8303
9db70fc3
AM
8304 free ((char *) aux.table);
8305 free ((char *) aux.info);
579f31ac
JJ
8306 aux.table = NULL;
8307 aux.info = NULL;
8308 }
4d6ed7c8 8309 }
4d6ed7c8 8310
9db70fc3
AM
8311 free (aux.symtab);
8312 free ((char *) aux.strtab);
32ec8896
NC
8313
8314 return res;
4d6ed7c8
NC
8315}
8316
3f5e193b 8317struct hppa_unw_table_entry
32ec8896
NC
8318{
8319 struct absaddr start;
8320 struct absaddr end;
8321 unsigned int Cannot_unwind:1; /* 0 */
8322 unsigned int Millicode:1; /* 1 */
8323 unsigned int Millicode_save_sr0:1; /* 2 */
8324 unsigned int Region_description:2; /* 3..4 */
8325 unsigned int reserved1:1; /* 5 */
8326 unsigned int Entry_SR:1; /* 6 */
8327 unsigned int Entry_FR:4; /* Number saved 7..10 */
8328 unsigned int Entry_GR:5; /* Number saved 11..15 */
8329 unsigned int Args_stored:1; /* 16 */
8330 unsigned int Variable_Frame:1; /* 17 */
8331 unsigned int Separate_Package_Body:1; /* 18 */
8332 unsigned int Frame_Extension_Millicode:1; /* 19 */
8333 unsigned int Stack_Overflow_Check:1; /* 20 */
8334 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
8335 unsigned int Ada_Region:1; /* 22 */
8336 unsigned int cxx_info:1; /* 23 */
8337 unsigned int cxx_try_catch:1; /* 24 */
8338 unsigned int sched_entry_seq:1; /* 25 */
8339 unsigned int reserved2:1; /* 26 */
8340 unsigned int Save_SP:1; /* 27 */
8341 unsigned int Save_RP:1; /* 28 */
8342 unsigned int Save_MRP_in_frame:1; /* 29 */
8343 unsigned int extn_ptr_defined:1; /* 30 */
8344 unsigned int Cleanup_defined:1; /* 31 */
8345
8346 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
8347 unsigned int HP_UX_interrupt_marker:1; /* 1 */
8348 unsigned int Large_frame:1; /* 2 */
8349 unsigned int Pseudo_SP_Set:1; /* 3 */
8350 unsigned int reserved4:1; /* 4 */
8351 unsigned int Total_frame_size:27; /* 5..31 */
8352};
3f5e193b 8353
57346661 8354struct hppa_unw_aux_info
948f632f 8355{
32ec8896
NC
8356 struct hppa_unw_table_entry * table; /* Unwind table. */
8357 unsigned long table_len; /* Length of unwind table. */
8358 bfd_vma seg_base; /* Starting address of segment. */
8359 Elf_Internal_Sym * symtab; /* The symbol table. */
8360 unsigned long nsyms; /* Number of symbols. */
8361 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8362 unsigned long nfuns; /* Number of entries in funtab. */
8363 char * strtab; /* The string table. */
8364 unsigned long strtab_size; /* Size of string table. */
948f632f 8365};
57346661 8366
015dc7e1 8367static bool
dda8d76d 8368dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
57346661 8369{
2cf0635d 8370 struct hppa_unw_table_entry * tp;
948f632f 8371 unsigned long j, nfuns;
015dc7e1 8372 bool res = true;
948f632f
DA
8373
8374 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8375 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8376 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8377 aux->funtab[nfuns++] = aux->symtab[j];
8378 aux->nfuns = nfuns;
8379 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
57346661 8380
57346661
AM
8381 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8382 {
8383 bfd_vma offset;
2cf0635d 8384 const char * procname;
57346661 8385
dda8d76d 8386 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
57346661
AM
8387 aux->strtab_size, tp->start, &procname,
8388 &offset);
8389
8390 fputs ("\n<", stdout);
8391
8392 if (procname)
8393 {
8394 fputs (procname, stdout);
8395
8396 if (offset)
8397 printf ("+%lx", (unsigned long) offset);
8398 }
8399
8400 fputs (">: [", stdout);
8401 print_vma (tp->start.offset, PREFIX_HEX);
8402 fputc ('-', stdout);
8403 print_vma (tp->end.offset, PREFIX_HEX);
8404 printf ("]\n\t");
8405
18bd398b
NC
8406#define PF(_m) if (tp->_m) printf (#_m " ");
8407#define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
57346661
AM
8408 PF(Cannot_unwind);
8409 PF(Millicode);
8410 PF(Millicode_save_sr0);
18bd398b 8411 /* PV(Region_description); */
57346661
AM
8412 PF(Entry_SR);
8413 PV(Entry_FR);
8414 PV(Entry_GR);
8415 PF(Args_stored);
8416 PF(Variable_Frame);
8417 PF(Separate_Package_Body);
8418 PF(Frame_Extension_Millicode);
8419 PF(Stack_Overflow_Check);
8420 PF(Two_Instruction_SP_Increment);
8421 PF(Ada_Region);
8422 PF(cxx_info);
8423 PF(cxx_try_catch);
8424 PF(sched_entry_seq);
8425 PF(Save_SP);
8426 PF(Save_RP);
8427 PF(Save_MRP_in_frame);
8428 PF(extn_ptr_defined);
8429 PF(Cleanup_defined);
8430 PF(MPE_XL_interrupt_marker);
8431 PF(HP_UX_interrupt_marker);
8432 PF(Large_frame);
8433 PF(Pseudo_SP_Set);
8434 PV(Total_frame_size);
8435#undef PF
8436#undef PV
8437 }
8438
18bd398b 8439 printf ("\n");
948f632f
DA
8440
8441 free (aux->funtab);
32ec8896
NC
8442
8443 return res;
57346661
AM
8444}
8445
015dc7e1 8446static bool
dda8d76d
NC
8447slurp_hppa_unwind_table (Filedata * filedata,
8448 struct hppa_unw_aux_info * aux,
8449 Elf_Internal_Shdr * sec)
57346661 8450{
1c0751b2 8451 unsigned long size, unw_ent_size, nentries, nrelas, i;
2cf0635d
NC
8452 Elf_Internal_Phdr * seg;
8453 struct hppa_unw_table_entry * tep;
8454 Elf_Internal_Shdr * relsec;
8455 Elf_Internal_Rela * rela;
8456 Elf_Internal_Rela * rp;
8457 unsigned char * table;
8458 unsigned char * tp;
8459 Elf_Internal_Sym * sym;
8460 const char * relname;
57346661 8461
57346661
AM
8462 /* First, find the starting address of the segment that includes
8463 this section. */
dda8d76d 8464 if (filedata->file_header.e_phnum)
57346661 8465 {
dda8d76d 8466 if (! get_program_headers (filedata))
015dc7e1 8467 return false;
57346661 8468
dda8d76d
NC
8469 for (seg = filedata->program_headers;
8470 seg < filedata->program_headers + filedata->file_header.e_phnum;
57346661
AM
8471 ++seg)
8472 {
8473 if (seg->p_type != PT_LOAD)
8474 continue;
8475
8476 if (sec->sh_addr >= seg->p_vaddr
8477 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8478 {
8479 aux->seg_base = seg->p_vaddr;
8480 break;
8481 }
8482 }
8483 }
8484
8485 /* Second, build the unwind table from the contents of the unwind
8486 section. */
8487 size = sec->sh_size;
dda8d76d 8488 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
3f5e193b 8489 _("unwind table"));
57346661 8490 if (!table)
015dc7e1 8491 return false;
57346661 8492
1c0751b2
DA
8493 unw_ent_size = 16;
8494 nentries = size / unw_ent_size;
8495 size = unw_ent_size * nentries;
57346661 8496
e3fdc001 8497 aux->table_len = nentries;
3f5e193b
NC
8498 tep = aux->table = (struct hppa_unw_table_entry *)
8499 xcmalloc (nentries, sizeof (aux->table[0]));
57346661 8500
1c0751b2 8501 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
57346661
AM
8502 {
8503 unsigned int tmp1, tmp2;
8504
8505 tep->start.section = SHN_UNDEF;
8506 tep->end.section = SHN_UNDEF;
8507
1c0751b2
DA
8508 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
8509 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
8510 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
8511 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
8512
8513 tep->start.offset += aux->seg_base;
8514 tep->end.offset += aux->seg_base;
57346661
AM
8515
8516 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
8517 tep->Millicode = (tmp1 >> 30) & 0x1;
8518 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
8519 tep->Region_description = (tmp1 >> 27) & 0x3;
8520 tep->reserved1 = (tmp1 >> 26) & 0x1;
8521 tep->Entry_SR = (tmp1 >> 25) & 0x1;
8522 tep->Entry_FR = (tmp1 >> 21) & 0xf;
8523 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
8524 tep->Args_stored = (tmp1 >> 15) & 0x1;
8525 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
8526 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
8527 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
8528 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
8529 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
8530 tep->Ada_Region = (tmp1 >> 9) & 0x1;
8531 tep->cxx_info = (tmp1 >> 8) & 0x1;
8532 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
8533 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
8534 tep->reserved2 = (tmp1 >> 5) & 0x1;
8535 tep->Save_SP = (tmp1 >> 4) & 0x1;
8536 tep->Save_RP = (tmp1 >> 3) & 0x1;
8537 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
8538 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
8539 tep->Cleanup_defined = tmp1 & 0x1;
8540
8541 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
8542 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
8543 tep->Large_frame = (tmp2 >> 29) & 0x1;
8544 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
8545 tep->reserved4 = (tmp2 >> 27) & 0x1;
8546 tep->Total_frame_size = tmp2 & 0x7ffffff;
57346661
AM
8547 }
8548 free (table);
8549
8550 /* Third, apply any relocations to the unwind table. */
dda8d76d
NC
8551 for (relsec = filedata->section_headers;
8552 relsec < filedata->section_headers + filedata->file_header.e_shnum;
57346661
AM
8553 ++relsec)
8554 {
8555 if (relsec->sh_type != SHT_RELA
dda8d76d
NC
8556 || relsec->sh_info >= filedata->file_header.e_shnum
8557 || filedata->section_headers + relsec->sh_info != sec)
57346661
AM
8558 continue;
8559
dda8d76d 8560 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
57346661 8561 & rela, & nrelas))
015dc7e1 8562 return false;
57346661
AM
8563
8564 for (rp = rela; rp < rela + nrelas; ++rp)
8565 {
4770fb94 8566 unsigned int sym_ndx;
726bd37d
AM
8567 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8568 relname = elf_hppa_reloc_type (r_type);
57346661 8569
726bd37d
AM
8570 if (relname == NULL)
8571 {
8572 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8573 continue;
8574 }
8575
57346661 8576 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
24d127aa 8577 if (! startswith (relname, "R_PARISC_SEGREL"))
57346661 8578 {
726bd37d 8579 warn (_("Skipping unexpected relocation type: %s\n"), relname);
57346661
AM
8580 continue;
8581 }
8582
8583 i = rp->r_offset / unw_ent_size;
726bd37d
AM
8584 if (i >= aux->table_len)
8585 {
8586 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8587 continue;
8588 }
57346661 8589
4770fb94
AM
8590 sym_ndx = get_reloc_symindex (rp->r_info);
8591 if (sym_ndx >= aux->nsyms)
8592 {
8593 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8594 sym_ndx);
8595 continue;
8596 }
8597 sym = aux->symtab + sym_ndx;
8598
43f6cd05 8599 switch ((rp->r_offset % unw_ent_size) / 4)
57346661
AM
8600 {
8601 case 0:
8602 aux->table[i].start.section = sym->st_shndx;
1e456d54 8603 aux->table[i].start.offset = sym->st_value + rp->r_addend;
57346661
AM
8604 break;
8605 case 1:
8606 aux->table[i].end.section = sym->st_shndx;
1e456d54 8607 aux->table[i].end.offset = sym->st_value + rp->r_addend;
57346661
AM
8608 break;
8609 default:
8610 break;
8611 }
8612 }
8613
8614 free (rela);
8615 }
8616
015dc7e1 8617 return true;
57346661
AM
8618}
8619
015dc7e1 8620static bool
dda8d76d 8621hppa_process_unwind (Filedata * filedata)
57346661 8622{
57346661 8623 struct hppa_unw_aux_info aux;
2cf0635d 8624 Elf_Internal_Shdr * unwsec = NULL;
2cf0635d 8625 Elf_Internal_Shdr * sec;
18bd398b 8626 unsigned long i;
015dc7e1 8627 bool res = true;
57346661 8628
dda8d76d 8629 if (filedata->string_table == NULL)
015dc7e1 8630 return false;
1b31d05e
NC
8631
8632 memset (& aux, 0, sizeof (aux));
57346661 8633
dda8d76d 8634 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
57346661 8635 {
28d13567 8636 if (sec->sh_type == SHT_SYMTAB)
57346661 8637 {
28d13567 8638 if (aux.symtab)
4082ef84 8639 {
28d13567
AM
8640 error (_("Multiple symbol tables encountered\n"));
8641 free (aux.symtab);
8642 aux.symtab = NULL;
4082ef84 8643 free (aux.strtab);
28d13567 8644 aux.strtab = NULL;
4082ef84 8645 }
28d13567
AM
8646 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8647 &aux.strtab, &aux.strtab_size))
015dc7e1 8648 return false;
57346661 8649 }
b9e920ec
AM
8650 else if (SECTION_NAME_VALID (sec)
8651 && streq (SECTION_NAME (sec), ".PARISC.unwind"))
57346661
AM
8652 unwsec = sec;
8653 }
8654
8655 if (!unwsec)
8656 printf (_("\nThere are no unwind sections in this file.\n"));
8657
dda8d76d 8658 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
57346661 8659 {
b9e920ec
AM
8660 if (SECTION_NAME_VALID (sec)
8661 && streq (SECTION_NAME (sec), ".PARISC.unwind"))
57346661 8662 {
43f6cd05 8663 unsigned long num_unwind = sec->sh_size / 16;
dda8d76d 8664
d3a49aa8
AM
8665 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
8666 "contains %lu entry:\n",
8667 "\nUnwind section '%s' at offset 0x%lx "
8668 "contains %lu entries:\n",
8669 num_unwind),
dda8d76d 8670 printable_section_name (filedata, sec),
57346661 8671 (unsigned long) sec->sh_offset,
d3a49aa8 8672 num_unwind);
57346661 8673
dda8d76d 8674 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
015dc7e1 8675 res = false;
66b09c7e
S
8676
8677 if (res && aux.table_len > 0)
32ec8896 8678 {
dda8d76d 8679 if (! dump_hppa_unwind (filedata, &aux))
015dc7e1 8680 res = false;
32ec8896 8681 }
57346661 8682
9db70fc3 8683 free ((char *) aux.table);
57346661
AM
8684 aux.table = NULL;
8685 }
8686 }
8687
9db70fc3
AM
8688 free (aux.symtab);
8689 free ((char *) aux.strtab);
32ec8896
NC
8690
8691 return res;
57346661
AM
8692}
8693
0b6ae522
DJ
8694struct arm_section
8695{
a734115a
NC
8696 unsigned char * data; /* The unwind data. */
8697 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
8698 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
8699 unsigned long nrelas; /* The number of relocations. */
8700 unsigned int rel_type; /* REL or RELA ? */
8701 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
0b6ae522
DJ
8702};
8703
8704struct arm_unw_aux_info
8705{
dda8d76d 8706 Filedata * filedata; /* The file containing the unwind sections. */
a734115a
NC
8707 Elf_Internal_Sym * symtab; /* The file's symbol table. */
8708 unsigned long nsyms; /* Number of symbols. */
948f632f
DA
8709 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8710 unsigned long nfuns; /* Number of these symbols. */
a734115a
NC
8711 char * strtab; /* The file's string table. */
8712 unsigned long strtab_size; /* Size of string table. */
0b6ae522
DJ
8713};
8714
8715static const char *
dda8d76d
NC
8716arm_print_vma_and_name (Filedata * filedata,
8717 struct arm_unw_aux_info * aux,
8718 bfd_vma fn,
8719 struct absaddr addr)
0b6ae522
DJ
8720{
8721 const char *procname;
8722 bfd_vma sym_offset;
8723
8724 if (addr.section == SHN_UNDEF)
8725 addr.offset = fn;
8726
dda8d76d 8727 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
0b6ae522
DJ
8728 aux->strtab_size, addr, &procname,
8729 &sym_offset);
8730
8731 print_vma (fn, PREFIX_HEX);
8732
8733 if (procname)
8734 {
8735 fputs (" <", stdout);
8736 fputs (procname, stdout);
8737
8738 if (sym_offset)
8739 printf ("+0x%lx", (unsigned long) sym_offset);
8740 fputc ('>', stdout);
8741 }
8742
8743 return procname;
8744}
8745
8746static void
8747arm_free_section (struct arm_section *arm_sec)
8748{
9db70fc3
AM
8749 free (arm_sec->data);
8750 free (arm_sec->rela);
0b6ae522
DJ
8751}
8752
a734115a
NC
8753/* 1) If SEC does not match the one cached in ARM_SEC, then free the current
8754 cached section and install SEC instead.
8755 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
8756 and return its valued in * WORDP, relocating if necessary.
1b31d05e 8757 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
a734115a 8758 relocation's offset in ADDR.
1b31d05e
NC
8759 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
8760 into the string table of the symbol associated with the reloc. If no
8761 reloc was applied store -1 there.
8762 5) Return TRUE upon success, FALSE otherwise. */
a734115a 8763
015dc7e1 8764static bool
dda8d76d
NC
8765get_unwind_section_word (Filedata * filedata,
8766 struct arm_unw_aux_info * aux,
1b31d05e
NC
8767 struct arm_section * arm_sec,
8768 Elf_Internal_Shdr * sec,
8769 bfd_vma word_offset,
8770 unsigned int * wordp,
8771 struct absaddr * addr,
8772 bfd_vma * sym_name)
0b6ae522
DJ
8773{
8774 Elf_Internal_Rela *rp;
8775 Elf_Internal_Sym *sym;
8776 const char * relname;
8777 unsigned int word;
015dc7e1 8778 bool wrapped;
0b6ae522 8779
e0a31db1 8780 if (sec == NULL || arm_sec == NULL)
015dc7e1 8781 return false;
e0a31db1 8782
0b6ae522
DJ
8783 addr->section = SHN_UNDEF;
8784 addr->offset = 0;
8785
1b31d05e
NC
8786 if (sym_name != NULL)
8787 *sym_name = (bfd_vma) -1;
8788
a734115a 8789 /* If necessary, update the section cache. */
0b6ae522
DJ
8790 if (sec != arm_sec->sec)
8791 {
8792 Elf_Internal_Shdr *relsec;
8793
8794 arm_free_section (arm_sec);
8795
8796 arm_sec->sec = sec;
dda8d76d 8797 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
0b6ae522 8798 sec->sh_size, _("unwind data"));
0b6ae522
DJ
8799 arm_sec->rela = NULL;
8800 arm_sec->nrelas = 0;
8801
dda8d76d
NC
8802 for (relsec = filedata->section_headers;
8803 relsec < filedata->section_headers + filedata->file_header.e_shnum;
0b6ae522
DJ
8804 ++relsec)
8805 {
dda8d76d
NC
8806 if (relsec->sh_info >= filedata->file_header.e_shnum
8807 || filedata->section_headers + relsec->sh_info != sec
1ae40aa4
NC
8808 /* PR 15745: Check the section type as well. */
8809 || (relsec->sh_type != SHT_REL
8810 && relsec->sh_type != SHT_RELA))
0b6ae522
DJ
8811 continue;
8812
a734115a 8813 arm_sec->rel_type = relsec->sh_type;
0b6ae522
DJ
8814 if (relsec->sh_type == SHT_REL)
8815 {
dda8d76d 8816 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
0b6ae522
DJ
8817 relsec->sh_size,
8818 & arm_sec->rela, & arm_sec->nrelas))
015dc7e1 8819 return false;
0b6ae522 8820 }
1ae40aa4 8821 else /* relsec->sh_type == SHT_RELA */
0b6ae522 8822 {
dda8d76d 8823 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
0b6ae522
DJ
8824 relsec->sh_size,
8825 & arm_sec->rela, & arm_sec->nrelas))
015dc7e1 8826 return false;
0b6ae522 8827 }
1ae40aa4 8828 break;
0b6ae522
DJ
8829 }
8830
8831 arm_sec->next_rela = arm_sec->rela;
8832 }
8833
a734115a 8834 /* If there is no unwind data we can do nothing. */
0b6ae522 8835 if (arm_sec->data == NULL)
015dc7e1 8836 return false;
0b6ae522 8837
e0a31db1 8838 /* If the offset is invalid then fail. */
f32ba729
NC
8839 if (/* PR 21343 *//* PR 18879 */
8840 sec->sh_size < 4
8841 || word_offset > (sec->sh_size - 4)
1a915552 8842 || ((bfd_signed_vma) word_offset) < 0)
015dc7e1 8843 return false;
e0a31db1 8844
a734115a 8845 /* Get the word at the required offset. */
0b6ae522
DJ
8846 word = byte_get (arm_sec->data + word_offset, 4);
8847
0eff7165
NC
8848 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
8849 if (arm_sec->rela == NULL)
8850 {
8851 * wordp = word;
015dc7e1 8852 return true;
0eff7165
NC
8853 }
8854
a734115a 8855 /* Look through the relocs to find the one that applies to the provided offset. */
015dc7e1 8856 wrapped = false;
0b6ae522
DJ
8857 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
8858 {
8859 bfd_vma prelval, offset;
8860
8861 if (rp->r_offset > word_offset && !wrapped)
8862 {
8863 rp = arm_sec->rela;
015dc7e1 8864 wrapped = true;
0b6ae522
DJ
8865 }
8866 if (rp->r_offset > word_offset)
8867 break;
8868
8869 if (rp->r_offset & 3)
8870 {
8871 warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
8872 (unsigned long) rp->r_offset);
8873 continue;
8874 }
8875
8876 if (rp->r_offset < word_offset)
8877 continue;
8878
74e1a04b
NC
8879 /* PR 17531: file: 027-161405-0.004 */
8880 if (aux->symtab == NULL)
8881 continue;
8882
0b6ae522
DJ
8883 if (arm_sec->rel_type == SHT_REL)
8884 {
8885 offset = word & 0x7fffffff;
8886 if (offset & 0x40000000)
8887 offset |= ~ (bfd_vma) 0x7fffffff;
8888 }
a734115a 8889 else if (arm_sec->rel_type == SHT_RELA)
0b6ae522 8890 offset = rp->r_addend;
a734115a 8891 else
74e1a04b
NC
8892 {
8893 error (_("Unknown section relocation type %d encountered\n"),
8894 arm_sec->rel_type);
8895 break;
8896 }
0b6ae522 8897
071436c6
NC
8898 /* PR 17531 file: 027-1241568-0.004. */
8899 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
8900 {
8901 error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
8902 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
8903 break;
8904 }
8905
8906 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
0b6ae522
DJ
8907 offset += sym->st_value;
8908 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
8909
a734115a 8910 /* Check that we are processing the expected reloc type. */
dda8d76d 8911 if (filedata->file_header.e_machine == EM_ARM)
a734115a
NC
8912 {
8913 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
071436c6
NC
8914 if (relname == NULL)
8915 {
8916 warn (_("Skipping unknown ARM relocation type: %d\n"),
8917 (int) ELF32_R_TYPE (rp->r_info));
8918 continue;
8919 }
a734115a
NC
8920
8921 if (streq (relname, "R_ARM_NONE"))
8922 continue;
0b4362b0 8923
a734115a
NC
8924 if (! streq (relname, "R_ARM_PREL31"))
8925 {
071436c6 8926 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
a734115a
NC
8927 continue;
8928 }
8929 }
dda8d76d 8930 else if (filedata->file_header.e_machine == EM_TI_C6000)
a734115a
NC
8931 {
8932 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
071436c6
NC
8933 if (relname == NULL)
8934 {
8935 warn (_("Skipping unknown C6000 relocation type: %d\n"),
8936 (int) ELF32_R_TYPE (rp->r_info));
8937 continue;
8938 }
0b4362b0 8939
a734115a
NC
8940 if (streq (relname, "R_C6000_NONE"))
8941 continue;
8942
8943 if (! streq (relname, "R_C6000_PREL31"))
8944 {
071436c6 8945 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
a734115a
NC
8946 continue;
8947 }
8948
8949 prelval >>= 1;
8950 }
8951 else
74e1a04b
NC
8952 {
8953 /* This function currently only supports ARM and TI unwinders. */
8954 warn (_("Only TI and ARM unwinders are currently supported\n"));
8955 break;
8956 }
fa197c1c 8957
0b6ae522
DJ
8958 word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
8959 addr->section = sym->st_shndx;
8960 addr->offset = offset;
74e1a04b 8961
1b31d05e
NC
8962 if (sym_name)
8963 * sym_name = sym->st_name;
0b6ae522
DJ
8964 break;
8965 }
8966
8967 *wordp = word;
8968 arm_sec->next_rela = rp;
8969
015dc7e1 8970 return true;
0b6ae522
DJ
8971}
8972
a734115a
NC
8973static const char *tic6x_unwind_regnames[16] =
8974{
0b4362b0
RM
8975 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
8976 "A14", "A13", "A12", "A11", "A10",
a734115a
NC
8977 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
8978};
fa197c1c 8979
0b6ae522 8980static void
fa197c1c 8981decode_tic6x_unwind_regmask (unsigned int mask)
0b6ae522 8982{
fa197c1c
PB
8983 int i;
8984
8985 for (i = 12; mask; mask >>= 1, i--)
8986 {
8987 if (mask & 1)
8988 {
8989 fputs (tic6x_unwind_regnames[i], stdout);
8990 if (mask > 1)
8991 fputs (", ", stdout);
8992 }
8993 }
8994}
0b6ae522
DJ
8995
8996#define ADVANCE \
8997 if (remaining == 0 && more_words) \
8998 { \
8999 data_offset += 4; \
dda8d76d 9000 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
1b31d05e 9001 data_offset, & word, & addr, NULL)) \
015dc7e1 9002 return false; \
0b6ae522
DJ
9003 remaining = 4; \
9004 more_words--; \
9005 } \
9006
9007#define GET_OP(OP) \
9008 ADVANCE; \
9009 if (remaining) \
9010 { \
9011 remaining--; \
9012 (OP) = word >> 24; \
9013 word <<= 8; \
9014 } \
9015 else \
9016 { \
2b692964 9017 printf (_("[Truncated opcode]\n")); \
015dc7e1 9018 return false; \
0b6ae522 9019 } \
cc5914eb 9020 printf ("0x%02x ", OP)
0b6ae522 9021
015dc7e1 9022static bool
dda8d76d
NC
9023decode_arm_unwind_bytecode (Filedata * filedata,
9024 struct arm_unw_aux_info * aux,
948f632f
DA
9025 unsigned int word,
9026 unsigned int remaining,
9027 unsigned int more_words,
9028 bfd_vma data_offset,
9029 Elf_Internal_Shdr * data_sec,
9030 struct arm_section * data_arm_sec)
fa197c1c
PB
9031{
9032 struct absaddr addr;
015dc7e1 9033 bool res = true;
0b6ae522
DJ
9034
9035 /* Decode the unwinding instructions. */
9036 while (1)
9037 {
9038 unsigned int op, op2;
9039
9040 ADVANCE;
9041 if (remaining == 0)
9042 break;
9043 remaining--;
9044 op = word >> 24;
9045 word <<= 8;
9046
cc5914eb 9047 printf (" 0x%02x ", op);
0b6ae522
DJ
9048
9049 if ((op & 0xc0) == 0x00)
9050 {
9051 int offset = ((op & 0x3f) << 2) + 4;
61865e30 9052
cc5914eb 9053 printf (" vsp = vsp + %d", offset);
0b6ae522
DJ
9054 }
9055 else if ((op & 0xc0) == 0x40)
9056 {
9057 int offset = ((op & 0x3f) << 2) + 4;
61865e30 9058
cc5914eb 9059 printf (" vsp = vsp - %d", offset);
0b6ae522
DJ
9060 }
9061 else if ((op & 0xf0) == 0x80)
9062 {
9063 GET_OP (op2);
9064 if (op == 0x80 && op2 == 0)
9065 printf (_("Refuse to unwind"));
9066 else
9067 {
9068 unsigned int mask = ((op & 0x0f) << 8) | op2;
015dc7e1 9069 bool first = true;
0b6ae522 9070 int i;
2b692964 9071
0b6ae522
DJ
9072 printf ("pop {");
9073 for (i = 0; i < 12; i++)
9074 if (mask & (1 << i))
9075 {
9076 if (first)
015dc7e1 9077 first = false;
0b6ae522
DJ
9078 else
9079 printf (", ");
9080 printf ("r%d", 4 + i);
9081 }
9082 printf ("}");
9083 }
9084 }
9085 else if ((op & 0xf0) == 0x90)
9086 {
9087 if (op == 0x9d || op == 0x9f)
9088 printf (_(" [Reserved]"));
9089 else
cc5914eb 9090 printf (" vsp = r%d", op & 0x0f);
0b6ae522
DJ
9091 }
9092 else if ((op & 0xf0) == 0xa0)
9093 {
9094 int end = 4 + (op & 0x07);
015dc7e1 9095 bool first = true;
0b6ae522 9096 int i;
61865e30 9097
0b6ae522
DJ
9098 printf (" pop {");
9099 for (i = 4; i <= end; i++)
9100 {
9101 if (first)
015dc7e1 9102 first = false;
0b6ae522
DJ
9103 else
9104 printf (", ");
9105 printf ("r%d", i);
9106 }
9107 if (op & 0x08)
9108 {
1b31d05e 9109 if (!first)
0b6ae522
DJ
9110 printf (", ");
9111 printf ("r14");
9112 }
9113 printf ("}");
9114 }
9115 else if (op == 0xb0)
9116 printf (_(" finish"));
9117 else if (op == 0xb1)
9118 {
9119 GET_OP (op2);
9120 if (op2 == 0 || (op2 & 0xf0) != 0)
9121 printf (_("[Spare]"));
9122 else
9123 {
9124 unsigned int mask = op2 & 0x0f;
015dc7e1 9125 bool first = true;
0b6ae522 9126 int i;
61865e30 9127
0b6ae522
DJ
9128 printf ("pop {");
9129 for (i = 0; i < 12; i++)
9130 if (mask & (1 << i))
9131 {
9132 if (first)
015dc7e1 9133 first = false;
0b6ae522
DJ
9134 else
9135 printf (", ");
9136 printf ("r%d", i);
9137 }
9138 printf ("}");
9139 }
9140 }
9141 else if (op == 0xb2)
9142 {
b115cf96 9143 unsigned char buf[9];
0b6ae522
DJ
9144 unsigned int i, len;
9145 unsigned long offset;
61865e30 9146
b115cf96 9147 for (i = 0; i < sizeof (buf); i++)
0b6ae522
DJ
9148 {
9149 GET_OP (buf[i]);
9150 if ((buf[i] & 0x80) == 0)
9151 break;
9152 }
4082ef84 9153 if (i == sizeof (buf))
32ec8896 9154 {
27a45f42 9155 error (_("corrupt change to vsp\n"));
015dc7e1 9156 res = false;
32ec8896 9157 }
4082ef84
NC
9158 else
9159 {
015dc7e1 9160 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
4082ef84
NC
9161 assert (len == i + 1);
9162 offset = offset * 4 + 0x204;
9163 printf ("vsp = vsp + %ld", offset);
9164 }
0b6ae522 9165 }
61865e30 9166 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
0b6ae522 9167 {
61865e30
NC
9168 unsigned int first, last;
9169
9170 GET_OP (op2);
9171 first = op2 >> 4;
9172 last = op2 & 0x0f;
9173 if (op == 0xc8)
9174 first = first + 16;
9175 printf ("pop {D%d", first);
9176 if (last)
9177 printf ("-D%d", first + last);
9178 printf ("}");
9179 }
9180 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
9181 {
9182 unsigned int count = op & 0x07;
9183
9184 printf ("pop {D8");
9185 if (count)
9186 printf ("-D%d", 8 + count);
9187 printf ("}");
9188 }
9189 else if (op >= 0xc0 && op <= 0xc5)
9190 {
9191 unsigned int count = op & 0x07;
9192
9193 printf (" pop {wR10");
9194 if (count)
9195 printf ("-wR%d", 10 + count);
9196 printf ("}");
9197 }
9198 else if (op == 0xc6)
9199 {
9200 unsigned int first, last;
9201
9202 GET_OP (op2);
9203 first = op2 >> 4;
9204 last = op2 & 0x0f;
9205 printf ("pop {wR%d", first);
9206 if (last)
9207 printf ("-wR%d", first + last);
9208 printf ("}");
9209 }
9210 else if (op == 0xc7)
9211 {
9212 GET_OP (op2);
9213 if (op2 == 0 || (op2 & 0xf0) != 0)
9214 printf (_("[Spare]"));
0b6ae522
DJ
9215 else
9216 {
61865e30 9217 unsigned int mask = op2 & 0x0f;
015dc7e1 9218 bool first = true;
61865e30
NC
9219 int i;
9220
9221 printf ("pop {");
9222 for (i = 0; i < 4; i++)
9223 if (mask & (1 << i))
9224 {
9225 if (first)
015dc7e1 9226 first = false;
61865e30
NC
9227 else
9228 printf (", ");
9229 printf ("wCGR%d", i);
9230 }
9231 printf ("}");
0b6ae522
DJ
9232 }
9233 }
61865e30 9234 else
32ec8896
NC
9235 {
9236 printf (_(" [unsupported opcode]"));
015dc7e1 9237 res = false;
32ec8896
NC
9238 }
9239
0b6ae522
DJ
9240 printf ("\n");
9241 }
32ec8896
NC
9242
9243 return res;
fa197c1c
PB
9244}
9245
015dc7e1 9246static bool
dda8d76d
NC
9247decode_tic6x_unwind_bytecode (Filedata * filedata,
9248 struct arm_unw_aux_info * aux,
948f632f
DA
9249 unsigned int word,
9250 unsigned int remaining,
9251 unsigned int more_words,
9252 bfd_vma data_offset,
9253 Elf_Internal_Shdr * data_sec,
9254 struct arm_section * data_arm_sec)
fa197c1c
PB
9255{
9256 struct absaddr addr;
9257
9258 /* Decode the unwinding instructions. */
9259 while (1)
9260 {
9261 unsigned int op, op2;
9262
9263 ADVANCE;
9264 if (remaining == 0)
9265 break;
9266 remaining--;
9267 op = word >> 24;
9268 word <<= 8;
9269
9cf03b7e 9270 printf (" 0x%02x ", op);
fa197c1c
PB
9271
9272 if ((op & 0xc0) == 0x00)
9273 {
9274 int offset = ((op & 0x3f) << 3) + 8;
9cf03b7e 9275 printf (" sp = sp + %d", offset);
fa197c1c
PB
9276 }
9277 else if ((op & 0xc0) == 0x80)
9278 {
9279 GET_OP (op2);
9280 if (op == 0x80 && op2 == 0)
9281 printf (_("Refuse to unwind"));
9282 else
9283 {
9284 unsigned int mask = ((op & 0x1f) << 8) | op2;
9285 if (op & 0x20)
9286 printf ("pop compact {");
9287 else
9288 printf ("pop {");
9289
9290 decode_tic6x_unwind_regmask (mask);
9291 printf("}");
9292 }
9293 }
9294 else if ((op & 0xf0) == 0xc0)
9295 {
9296 unsigned int reg;
9297 unsigned int nregs;
9298 unsigned int i;
9299 const char *name;
a734115a
NC
9300 struct
9301 {
32ec8896
NC
9302 unsigned int offset;
9303 unsigned int reg;
fa197c1c
PB
9304 } regpos[16];
9305
9306 /* Scan entire instruction first so that GET_OP output is not
9307 interleaved with disassembly. */
9308 nregs = 0;
9309 for (i = 0; nregs < (op & 0xf); i++)
9310 {
9311 GET_OP (op2);
9312 reg = op2 >> 4;
9313 if (reg != 0xf)
9314 {
9315 regpos[nregs].offset = i * 2;
9316 regpos[nregs].reg = reg;
9317 nregs++;
9318 }
9319
9320 reg = op2 & 0xf;
9321 if (reg != 0xf)
9322 {
9323 regpos[nregs].offset = i * 2 + 1;
9324 regpos[nregs].reg = reg;
9325 nregs++;
9326 }
9327 }
9328
9329 printf (_("pop frame {"));
18344509 9330 if (nregs == 0)
fa197c1c 9331 {
18344509
NC
9332 printf (_("*corrupt* - no registers specified"));
9333 }
9334 else
9335 {
9336 reg = nregs - 1;
9337 for (i = i * 2; i > 0; i--)
fa197c1c 9338 {
18344509
NC
9339 if (regpos[reg].offset == i - 1)
9340 {
9341 name = tic6x_unwind_regnames[regpos[reg].reg];
9342 if (reg > 0)
9343 reg--;
9344 }
9345 else
9346 name = _("[pad]");
fa197c1c 9347
18344509
NC
9348 fputs (name, stdout);
9349 if (i > 1)
9350 printf (", ");
9351 }
fa197c1c
PB
9352 }
9353
9354 printf ("}");
9355 }
9356 else if (op == 0xd0)
9357 printf (" MOV FP, SP");
9358 else if (op == 0xd1)
9359 printf (" __c6xabi_pop_rts");
9360 else if (op == 0xd2)
9361 {
9362 unsigned char buf[9];
9363 unsigned int i, len;
9364 unsigned long offset;
a734115a 9365
fa197c1c
PB
9366 for (i = 0; i < sizeof (buf); i++)
9367 {
9368 GET_OP (buf[i]);
9369 if ((buf[i] & 0x80) == 0)
9370 break;
9371 }
0eff7165
NC
9372 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
9373 if (i == sizeof (buf))
9374 {
0eff7165 9375 warn (_("Corrupt stack pointer adjustment detected\n"));
015dc7e1 9376 return false;
0eff7165 9377 }
948f632f 9378
015dc7e1 9379 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
fa197c1c
PB
9380 assert (len == i + 1);
9381 offset = offset * 8 + 0x408;
9382 printf (_("sp = sp + %ld"), offset);
9383 }
9384 else if ((op & 0xf0) == 0xe0)
9385 {
9386 if ((op & 0x0f) == 7)
9387 printf (" RETURN");
9388 else
9389 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
9390 }
9391 else
9392 {
9393 printf (_(" [unsupported opcode]"));
9394 }
9395 putchar ('\n');
9396 }
32ec8896 9397
015dc7e1 9398 return true;
fa197c1c
PB
9399}
9400
9401static bfd_vma
dda8d76d 9402arm_expand_prel31 (Filedata * filedata, bfd_vma word, bfd_vma where)
fa197c1c
PB
9403{
9404 bfd_vma offset;
9405
9406 offset = word & 0x7fffffff;
9407 if (offset & 0x40000000)
9408 offset |= ~ (bfd_vma) 0x7fffffff;
9409
dda8d76d 9410 if (filedata->file_header.e_machine == EM_TI_C6000)
fa197c1c
PB
9411 offset <<= 1;
9412
9413 return offset + where;
9414}
9415
015dc7e1 9416static bool
dda8d76d
NC
9417decode_arm_unwind (Filedata * filedata,
9418 struct arm_unw_aux_info * aux,
1b31d05e
NC
9419 unsigned int word,
9420 unsigned int remaining,
9421 bfd_vma data_offset,
9422 Elf_Internal_Shdr * data_sec,
9423 struct arm_section * data_arm_sec)
fa197c1c
PB
9424{
9425 int per_index;
9426 unsigned int more_words = 0;
37e14bc3 9427 struct absaddr addr;
1b31d05e 9428 bfd_vma sym_name = (bfd_vma) -1;
015dc7e1 9429 bool res = true;
fa197c1c
PB
9430
9431 if (remaining == 0)
9432 {
1b31d05e
NC
9433 /* Fetch the first word.
9434 Note - when decoding an object file the address extracted
9435 here will always be 0. So we also pass in the sym_name
9436 parameter so that we can find the symbol associated with
9437 the personality routine. */
dda8d76d 9438 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
1b31d05e 9439 & word, & addr, & sym_name))
015dc7e1 9440 return false;
1b31d05e 9441
fa197c1c
PB
9442 remaining = 4;
9443 }
c93dbb25
CZ
9444 else
9445 {
9446 addr.section = SHN_UNDEF;
9447 addr.offset = 0;
9448 }
fa197c1c
PB
9449
9450 if ((word & 0x80000000) == 0)
9451 {
9452 /* Expand prel31 for personality routine. */
9453 bfd_vma fn;
9454 const char *procname;
9455
dda8d76d 9456 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
fa197c1c 9457 printf (_(" Personality routine: "));
1b31d05e
NC
9458 if (fn == 0
9459 && addr.section == SHN_UNDEF && addr.offset == 0
9460 && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
9461 {
9462 procname = aux->strtab + sym_name;
9463 print_vma (fn, PREFIX_HEX);
9464 if (procname)
9465 {
9466 fputs (" <", stdout);
9467 fputs (procname, stdout);
9468 fputc ('>', stdout);
9469 }
9470 }
9471 else
dda8d76d 9472 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
fa197c1c
PB
9473 fputc ('\n', stdout);
9474
9475 /* The GCC personality routines use the standard compact
9476 encoding, starting with one byte giving the number of
9477 words. */
9478 if (procname != NULL
24d127aa
ML
9479 && (startswith (procname, "__gcc_personality_v0")
9480 || startswith (procname, "__gxx_personality_v0")
9481 || startswith (procname, "__gcj_personality_v0")
9482 || startswith (procname, "__gnu_objc_personality_v0")))
fa197c1c
PB
9483 {
9484 remaining = 0;
9485 more_words = 1;
9486 ADVANCE;
9487 if (!remaining)
9488 {
9489 printf (_(" [Truncated data]\n"));
015dc7e1 9490 return false;
fa197c1c
PB
9491 }
9492 more_words = word >> 24;
9493 word <<= 8;
9494 remaining--;
9495 per_index = -1;
9496 }
9497 else
015dc7e1 9498 return true;
fa197c1c
PB
9499 }
9500 else
9501 {
1b31d05e 9502 /* ARM EHABI Section 6.3:
0b4362b0 9503
1b31d05e 9504 An exception-handling table entry for the compact model looks like:
0b4362b0 9505
1b31d05e
NC
9506 31 30-28 27-24 23-0
9507 -- ----- ----- ----
9508 1 0 index Data for personalityRoutine[index] */
9509
dda8d76d 9510 if (filedata->file_header.e_machine == EM_ARM
1b31d05e 9511 && (word & 0x70000000))
32ec8896
NC
9512 {
9513 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
015dc7e1 9514 res = false;
32ec8896 9515 }
1b31d05e 9516
fa197c1c 9517 per_index = (word >> 24) & 0x7f;
1b31d05e 9518 printf (_(" Compact model index: %d\n"), per_index);
fa197c1c
PB
9519 if (per_index == 0)
9520 {
9521 more_words = 0;
9522 word <<= 8;
9523 remaining--;
9524 }
9525 else if (per_index < 3)
9526 {
9527 more_words = (word >> 16) & 0xff;
9528 word <<= 16;
9529 remaining -= 2;
9530 }
9531 }
9532
dda8d76d 9533 switch (filedata->file_header.e_machine)
fa197c1c
PB
9534 {
9535 case EM_ARM:
9536 if (per_index < 3)
9537 {
dda8d76d 9538 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
32ec8896 9539 data_offset, data_sec, data_arm_sec))
015dc7e1 9540 res = false;
fa197c1c
PB
9541 }
9542 else
1b31d05e
NC
9543 {
9544 warn (_("Unknown ARM compact model index encountered\n"));
9545 printf (_(" [reserved]\n"));
015dc7e1 9546 res = false;
1b31d05e 9547 }
fa197c1c
PB
9548 break;
9549
9550 case EM_TI_C6000:
9551 if (per_index < 3)
9552 {
dda8d76d 9553 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
32ec8896 9554 data_offset, data_sec, data_arm_sec))
015dc7e1 9555 res = false;
fa197c1c
PB
9556 }
9557 else if (per_index < 5)
9558 {
9559 if (((word >> 17) & 0x7f) == 0x7f)
9560 printf (_(" Restore stack from frame pointer\n"));
9561 else
9562 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
9563 printf (_(" Registers restored: "));
9564 if (per_index == 4)
9565 printf (" (compact) ");
9566 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
9567 putchar ('\n');
9568 printf (_(" Return register: %s\n"),
9569 tic6x_unwind_regnames[word & 0xf]);
9570 }
9571 else
1b31d05e 9572 printf (_(" [reserved (%d)]\n"), per_index);
fa197c1c
PB
9573 break;
9574
9575 default:
74e1a04b 9576 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
dda8d76d 9577 filedata->file_header.e_machine);
015dc7e1 9578 res = false;
fa197c1c 9579 }
0b6ae522
DJ
9580
9581 /* Decode the descriptors. Not implemented. */
32ec8896
NC
9582
9583 return res;
0b6ae522
DJ
9584}
9585
015dc7e1 9586static bool
dda8d76d
NC
9587dump_arm_unwind (Filedata * filedata,
9588 struct arm_unw_aux_info * aux,
9589 Elf_Internal_Shdr * exidx_sec)
0b6ae522
DJ
9590{
9591 struct arm_section exidx_arm_sec, extab_arm_sec;
9592 unsigned int i, exidx_len;
948f632f 9593 unsigned long j, nfuns;
015dc7e1 9594 bool res = true;
0b6ae522
DJ
9595
9596 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
9597 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
9598 exidx_len = exidx_sec->sh_size / 8;
9599
948f632f
DA
9600 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9601 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9602 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9603 aux->funtab[nfuns++] = aux->symtab[j];
9604 aux->nfuns = nfuns;
9605 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9606
0b6ae522
DJ
9607 for (i = 0; i < exidx_len; i++)
9608 {
9609 unsigned int exidx_fn, exidx_entry;
9610 struct absaddr fn_addr, entry_addr;
9611 bfd_vma fn;
9612
9613 fputc ('\n', stdout);
9614
dda8d76d 9615 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
1b31d05e 9616 8 * i, & exidx_fn, & fn_addr, NULL)
dda8d76d 9617 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
1b31d05e 9618 8 * i + 4, & exidx_entry, & entry_addr, NULL))
0b6ae522 9619 {
948f632f 9620 free (aux->funtab);
1b31d05e
NC
9621 arm_free_section (& exidx_arm_sec);
9622 arm_free_section (& extab_arm_sec);
015dc7e1 9623 return false;
0b6ae522
DJ
9624 }
9625
83c257ca
NC
9626 /* ARM EHABI, Section 5:
9627 An index table entry consists of 2 words.
9628 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
9629 if (exidx_fn & 0x80000000)
32ec8896
NC
9630 {
9631 warn (_("corrupt index table entry: %x\n"), exidx_fn);
015dc7e1 9632 res = false;
32ec8896 9633 }
83c257ca 9634
dda8d76d 9635 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
0b6ae522 9636
dda8d76d 9637 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
0b6ae522
DJ
9638 fputs (": ", stdout);
9639
9640 if (exidx_entry == 1)
9641 {
9642 print_vma (exidx_entry, PREFIX_HEX);
9643 fputs (" [cantunwind]\n", stdout);
9644 }
9645 else if (exidx_entry & 0x80000000)
9646 {
9647 print_vma (exidx_entry, PREFIX_HEX);
9648 fputc ('\n', stdout);
dda8d76d 9649 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
0b6ae522
DJ
9650 }
9651 else
9652 {
8f73510c 9653 bfd_vma table, table_offset = 0;
0b6ae522
DJ
9654 Elf_Internal_Shdr *table_sec;
9655
9656 fputs ("@", stdout);
dda8d76d 9657 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
0b6ae522
DJ
9658 print_vma (table, PREFIX_HEX);
9659 printf ("\n");
9660
9661 /* Locate the matching .ARM.extab. */
9662 if (entry_addr.section != SHN_UNDEF
dda8d76d 9663 && entry_addr.section < filedata->file_header.e_shnum)
0b6ae522 9664 {
dda8d76d 9665 table_sec = filedata->section_headers + entry_addr.section;
0b6ae522 9666 table_offset = entry_addr.offset;
1a915552
NC
9667 /* PR 18879 */
9668 if (table_offset > table_sec->sh_size
9669 || ((bfd_signed_vma) table_offset) < 0)
9670 {
9671 warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
9672 (unsigned long) table_offset,
dda8d76d 9673 printable_section_name (filedata, table_sec));
015dc7e1 9674 res = false;
1a915552
NC
9675 continue;
9676 }
0b6ae522
DJ
9677 }
9678 else
9679 {
dda8d76d 9680 table_sec = find_section_by_address (filedata, table);
0b6ae522
DJ
9681 if (table_sec != NULL)
9682 table_offset = table - table_sec->sh_addr;
9683 }
32ec8896 9684
0b6ae522
DJ
9685 if (table_sec == NULL)
9686 {
9687 warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
9688 (unsigned long) table);
015dc7e1 9689 res = false;
0b6ae522
DJ
9690 continue;
9691 }
32ec8896 9692
dda8d76d 9693 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
32ec8896 9694 &extab_arm_sec))
015dc7e1 9695 res = false;
0b6ae522
DJ
9696 }
9697 }
9698
9699 printf ("\n");
9700
948f632f 9701 free (aux->funtab);
0b6ae522
DJ
9702 arm_free_section (&exidx_arm_sec);
9703 arm_free_section (&extab_arm_sec);
32ec8896
NC
9704
9705 return res;
0b6ae522
DJ
9706}
9707
fa197c1c 9708/* Used for both ARM and C6X unwinding tables. */
1b31d05e 9709
015dc7e1 9710static bool
dda8d76d 9711arm_process_unwind (Filedata * filedata)
0b6ae522
DJ
9712{
9713 struct arm_unw_aux_info aux;
9714 Elf_Internal_Shdr *unwsec = NULL;
0b6ae522
DJ
9715 Elf_Internal_Shdr *sec;
9716 unsigned long i;
fa197c1c 9717 unsigned int sec_type;
015dc7e1 9718 bool res = true;
0b6ae522 9719
dda8d76d 9720 switch (filedata->file_header.e_machine)
fa197c1c
PB
9721 {
9722 case EM_ARM:
9723 sec_type = SHT_ARM_EXIDX;
9724 break;
9725
9726 case EM_TI_C6000:
9727 sec_type = SHT_C6000_UNWIND;
9728 break;
9729
0b4362b0 9730 default:
74e1a04b 9731 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
dda8d76d 9732 filedata->file_header.e_machine);
015dc7e1 9733 return false;
fa197c1c
PB
9734 }
9735
dda8d76d 9736 if (filedata->string_table == NULL)
015dc7e1 9737 return false;
1b31d05e
NC
9738
9739 memset (& aux, 0, sizeof (aux));
dda8d76d 9740 aux.filedata = filedata;
0b6ae522 9741
dda8d76d 9742 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
0b6ae522 9743 {
28d13567 9744 if (sec->sh_type == SHT_SYMTAB)
0b6ae522 9745 {
28d13567 9746 if (aux.symtab)
74e1a04b 9747 {
28d13567
AM
9748 error (_("Multiple symbol tables encountered\n"));
9749 free (aux.symtab);
9750 aux.symtab = NULL;
74e1a04b 9751 free (aux.strtab);
28d13567 9752 aux.strtab = NULL;
74e1a04b 9753 }
28d13567
AM
9754 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9755 &aux.strtab, &aux.strtab_size))
015dc7e1 9756 return false;
0b6ae522 9757 }
fa197c1c 9758 else if (sec->sh_type == sec_type)
0b6ae522
DJ
9759 unwsec = sec;
9760 }
9761
1b31d05e 9762 if (unwsec == NULL)
0b6ae522 9763 printf (_("\nThere are no unwind sections in this file.\n"));
1b31d05e 9764 else
dda8d76d 9765 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
1b31d05e
NC
9766 {
9767 if (sec->sh_type == sec_type)
9768 {
d3a49aa8
AM
9769 unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
9770 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
9771 "contains %lu entry:\n",
9772 "\nUnwind section '%s' at offset 0x%lx "
9773 "contains %lu entries:\n",
9774 num_unwind),
dda8d76d 9775 printable_section_name (filedata, sec),
1b31d05e 9776 (unsigned long) sec->sh_offset,
d3a49aa8 9777 num_unwind);
0b6ae522 9778
dda8d76d 9779 if (! dump_arm_unwind (filedata, &aux, sec))
015dc7e1 9780 res = false;
1b31d05e
NC
9781 }
9782 }
0b6ae522 9783
9db70fc3
AM
9784 free (aux.symtab);
9785 free ((char *) aux.strtab);
32ec8896
NC
9786
9787 return res;
0b6ae522
DJ
9788}
9789
015dc7e1 9790static bool
dda8d76d 9791process_unwind (Filedata * filedata)
57346661 9792{
2cf0635d
NC
9793 struct unwind_handler
9794 {
32ec8896 9795 unsigned int machtype;
015dc7e1 9796 bool (* handler)(Filedata *);
2cf0635d
NC
9797 } handlers[] =
9798 {
0b6ae522 9799 { EM_ARM, arm_process_unwind },
57346661
AM
9800 { EM_IA_64, ia64_process_unwind },
9801 { EM_PARISC, hppa_process_unwind },
fa197c1c 9802 { EM_TI_C6000, arm_process_unwind },
32ec8896 9803 { 0, NULL }
57346661
AM
9804 };
9805 int i;
9806
9807 if (!do_unwind)
015dc7e1 9808 return true;
57346661
AM
9809
9810 for (i = 0; handlers[i].handler != NULL; i++)
dda8d76d
NC
9811 if (filedata->file_header.e_machine == handlers[i].machtype)
9812 return handlers[i].handler (filedata);
57346661 9813
1b31d05e 9814 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
dda8d76d 9815 get_machine_name (filedata->file_header.e_machine));
015dc7e1 9816 return true;
57346661
AM
9817}
9818
37c18eed
SD
9819static void
9820dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
9821{
9822 switch (entry->d_tag)
9823 {
9824 case DT_AARCH64_BTI_PLT:
1dbade74 9825 case DT_AARCH64_PAC_PLT:
37c18eed
SD
9826 break;
9827 default:
9828 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9829 break;
9830 }
9831 putchar ('\n');
9832}
9833
252b5132 9834static void
978c4450 9835dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
252b5132
RH
9836{
9837 switch (entry->d_tag)
9838 {
9839 case DT_MIPS_FLAGS:
9840 if (entry->d_un.d_val == 0)
4b68bca3 9841 printf (_("NONE"));
252b5132
RH
9842 else
9843 {
9844 static const char * opts[] =
9845 {
9846 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
9847 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
9848 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
9849 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
9850 "RLD_ORDER_SAFE"
9851 };
9852 unsigned int cnt;
015dc7e1 9853 bool first = true;
2b692964 9854
60bca95a 9855 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
252b5132
RH
9856 if (entry->d_un.d_val & (1 << cnt))
9857 {
9858 printf ("%s%s", first ? "" : " ", opts[cnt]);
015dc7e1 9859 first = false;
252b5132 9860 }
252b5132
RH
9861 }
9862 break;
103f02d3 9863
252b5132 9864 case DT_MIPS_IVERSION:
978c4450
AM
9865 if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
9866 printf (_("Interface Version: %s"),
9867 GET_DYNAMIC_NAME (filedata, entry->d_un.d_val));
252b5132 9868 else
76ca31c0
NC
9869 {
9870 char buf[40];
9871 sprintf_vma (buf, entry->d_un.d_ptr);
9872 /* Note: coded this way so that there is a single string for translation. */
9873 printf (_("<corrupt: %s>"), buf);
9874 }
252b5132 9875 break;
103f02d3 9876
252b5132
RH
9877 case DT_MIPS_TIME_STAMP:
9878 {
d5b07ef4 9879 char timebuf[128];
2cf0635d 9880 struct tm * tmp;
91d6fa6a 9881 time_t atime = entry->d_un.d_val;
82b1b41b 9882
91d6fa6a 9883 tmp = gmtime (&atime);
82b1b41b
NC
9884 /* PR 17531: file: 6accc532. */
9885 if (tmp == NULL)
9886 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
9887 else
9888 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
9889 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9890 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
4b68bca3 9891 printf (_("Time Stamp: %s"), timebuf);
252b5132
RH
9892 }
9893 break;
103f02d3 9894
252b5132
RH
9895 case DT_MIPS_RLD_VERSION:
9896 case DT_MIPS_LOCAL_GOTNO:
9897 case DT_MIPS_CONFLICTNO:
9898 case DT_MIPS_LIBLISTNO:
9899 case DT_MIPS_SYMTABNO:
9900 case DT_MIPS_UNREFEXTNO:
9901 case DT_MIPS_HIPAGENO:
9902 case DT_MIPS_DELTA_CLASS_NO:
9903 case DT_MIPS_DELTA_INSTANCE_NO:
9904 case DT_MIPS_DELTA_RELOC_NO:
9905 case DT_MIPS_DELTA_SYM_NO:
9906 case DT_MIPS_DELTA_CLASSSYM_NO:
9907 case DT_MIPS_COMPACT_SIZE:
c69075ac 9908 print_vma (entry->d_un.d_val, DEC);
252b5132 9909 break;
103f02d3 9910
f16a9783 9911 case DT_MIPS_XHASH:
978c4450
AM
9912 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
9913 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
f16a9783
MS
9914 /* Falls through. */
9915
103f02d3 9916 default:
4b68bca3 9917 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
103f02d3 9918 }
4b68bca3 9919 putchar ('\n');
103f02d3
UD
9920}
9921
103f02d3 9922static void
2cf0635d 9923dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
103f02d3
UD
9924{
9925 switch (entry->d_tag)
9926 {
9927 case DT_HP_DLD_FLAGS:
9928 {
9929 static struct
9930 {
9931 long int bit;
2cf0635d 9932 const char * str;
5e220199
NC
9933 }
9934 flags[] =
9935 {
9936 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
9937 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
9938 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
9939 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
9940 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
9941 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
9942 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
9943 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
9944 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
9945 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
eec8f817
DA
9946 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
9947 { DT_HP_GST, "HP_GST" },
9948 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
9949 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
9950 { DT_HP_NODELETE, "HP_NODELETE" },
9951 { DT_HP_GROUP, "HP_GROUP" },
9952 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
5e220199 9953 };
015dc7e1 9954 bool first = true;
5e220199 9955 size_t cnt;
f7a99963 9956 bfd_vma val = entry->d_un.d_val;
103f02d3 9957
60bca95a 9958 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
103f02d3 9959 if (val & flags[cnt].bit)
30800947
NC
9960 {
9961 if (! first)
9962 putchar (' ');
9963 fputs (flags[cnt].str, stdout);
015dc7e1 9964 first = false;
30800947
NC
9965 val ^= flags[cnt].bit;
9966 }
76da6bbe 9967
103f02d3 9968 if (val != 0 || first)
f7a99963
NC
9969 {
9970 if (! first)
9971 putchar (' ');
9972 print_vma (val, HEX);
9973 }
103f02d3
UD
9974 }
9975 break;
76da6bbe 9976
252b5132 9977 default:
f7a99963
NC
9978 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
9979 break;
252b5132 9980 }
35b1837e 9981 putchar ('\n');
252b5132
RH
9982}
9983
28f997cf
TG
9984#ifdef BFD64
9985
9986/* VMS vs Unix time offset and factor. */
9987
9988#define VMS_EPOCH_OFFSET 35067168000000000LL
9989#define VMS_GRANULARITY_FACTOR 10000000
dccc31de
AM
9990#ifndef INT64_MIN
9991#define INT64_MIN (-9223372036854775807LL - 1)
9992#endif
28f997cf
TG
9993
9994/* Display a VMS time in a human readable format. */
9995
9996static void
9997print_vms_time (bfd_int64_t vmstime)
9998{
dccc31de 9999 struct tm *tm = NULL;
28f997cf
TG
10000 time_t unxtime;
10001
dccc31de
AM
10002 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
10003 {
10004 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
10005 unxtime = vmstime;
10006 if (unxtime == vmstime)
10007 tm = gmtime (&unxtime);
10008 }
10009 if (tm != NULL)
10010 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
10011 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
10012 tm->tm_hour, tm->tm_min, tm->tm_sec);
28f997cf
TG
10013}
10014#endif /* BFD64 */
10015
ecc51f48 10016static void
2cf0635d 10017dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
ecc51f48
NC
10018{
10019 switch (entry->d_tag)
10020 {
0de14b54 10021 case DT_IA_64_PLT_RESERVE:
bdf4d63a 10022 /* First 3 slots reserved. */
ecc51f48
NC
10023 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10024 printf (" -- ");
10025 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
bdf4d63a
JJ
10026 break;
10027
28f997cf
TG
10028 case DT_IA_64_VMS_LINKTIME:
10029#ifdef BFD64
10030 print_vms_time (entry->d_un.d_val);
10031#endif
10032 break;
10033
10034 case DT_IA_64_VMS_LNKFLAGS:
10035 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10036 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
10037 printf (" CALL_DEBUG");
10038 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
10039 printf (" NOP0BUFS");
10040 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
10041 printf (" P0IMAGE");
10042 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
10043 printf (" MKTHREADS");
10044 if (entry->d_un.d_val & VMS_LF_UPCALLS)
10045 printf (" UPCALLS");
10046 if (entry->d_un.d_val & VMS_LF_IMGSTA)
10047 printf (" IMGSTA");
10048 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
10049 printf (" INITIALIZE");
10050 if (entry->d_un.d_val & VMS_LF_MAIN)
10051 printf (" MAIN");
10052 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
10053 printf (" EXE_INIT");
10054 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
10055 printf (" TBK_IN_IMG");
10056 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
10057 printf (" DBG_IN_IMG");
10058 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
10059 printf (" TBK_IN_DSF");
10060 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
10061 printf (" DBG_IN_DSF");
10062 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
10063 printf (" SIGNATURES");
10064 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
10065 printf (" REL_SEG_OFF");
10066 break;
10067
bdf4d63a
JJ
10068 default:
10069 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10070 break;
ecc51f48 10071 }
bdf4d63a 10072 putchar ('\n');
ecc51f48
NC
10073}
10074
015dc7e1 10075static bool
dda8d76d 10076get_32bit_dynamic_section (Filedata * filedata)
252b5132 10077{
2cf0635d
NC
10078 Elf32_External_Dyn * edyn;
10079 Elf32_External_Dyn * ext;
10080 Elf_Internal_Dyn * entry;
103f02d3 10081
978c4450
AM
10082 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
10083 filedata->dynamic_addr, 1,
10084 filedata->dynamic_size,
10085 _("dynamic section"));
a6e9f9df 10086 if (!edyn)
015dc7e1 10087 return false;
103f02d3 10088
071436c6
NC
10089 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10090 might not have the luxury of section headers. Look for the DT_NULL
10091 terminator to determine the number of entries. */
978c4450
AM
10092 for (ext = edyn, filedata->dynamic_nent = 0;
10093 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
ba2685cc
AM
10094 ext++)
10095 {
978c4450 10096 filedata->dynamic_nent++;
ba2685cc
AM
10097 if (BYTE_GET (ext->d_tag) == DT_NULL)
10098 break;
10099 }
252b5132 10100
978c4450
AM
10101 filedata->dynamic_section
10102 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10103 if (filedata->dynamic_section == NULL)
252b5132 10104 {
8b73c356 10105 error (_("Out of memory allocating space for %lu dynamic entries\n"),
978c4450 10106 (unsigned long) filedata->dynamic_nent);
9ea033b2 10107 free (edyn);
015dc7e1 10108 return false;
9ea033b2 10109 }
252b5132 10110
978c4450
AM
10111 for (ext = edyn, entry = filedata->dynamic_section;
10112 entry < filedata->dynamic_section + filedata->dynamic_nent;
fb514b26 10113 ext++, entry++)
9ea033b2 10114 {
fb514b26
AM
10115 entry->d_tag = BYTE_GET (ext->d_tag);
10116 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
252b5132
RH
10117 }
10118
9ea033b2
NC
10119 free (edyn);
10120
015dc7e1 10121 return true;
9ea033b2
NC
10122}
10123
015dc7e1 10124static bool
dda8d76d 10125get_64bit_dynamic_section (Filedata * filedata)
9ea033b2 10126{
2cf0635d
NC
10127 Elf64_External_Dyn * edyn;
10128 Elf64_External_Dyn * ext;
10129 Elf_Internal_Dyn * entry;
103f02d3 10130
071436c6 10131 /* Read in the data. */
978c4450
AM
10132 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
10133 filedata->dynamic_addr, 1,
10134 filedata->dynamic_size,
10135 _("dynamic section"));
a6e9f9df 10136 if (!edyn)
015dc7e1 10137 return false;
103f02d3 10138
071436c6
NC
10139 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10140 might not have the luxury of section headers. Look for the DT_NULL
10141 terminator to determine the number of entries. */
978c4450 10142 for (ext = edyn, filedata->dynamic_nent = 0;
53c3012c 10143 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
978c4450 10144 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
ba2685cc
AM
10145 ext++)
10146 {
978c4450 10147 filedata->dynamic_nent++;
66543521 10148 if (BYTE_GET (ext->d_tag) == DT_NULL)
ba2685cc
AM
10149 break;
10150 }
252b5132 10151
978c4450
AM
10152 filedata->dynamic_section
10153 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10154 if (filedata->dynamic_section == NULL)
252b5132 10155 {
8b73c356 10156 error (_("Out of memory allocating space for %lu dynamic entries\n"),
978c4450 10157 (unsigned long) filedata->dynamic_nent);
252b5132 10158 free (edyn);
015dc7e1 10159 return false;
252b5132
RH
10160 }
10161
071436c6 10162 /* Convert from external to internal formats. */
978c4450
AM
10163 for (ext = edyn, entry = filedata->dynamic_section;
10164 entry < filedata->dynamic_section + filedata->dynamic_nent;
fb514b26 10165 ext++, entry++)
252b5132 10166 {
66543521
AM
10167 entry->d_tag = BYTE_GET (ext->d_tag);
10168 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
252b5132
RH
10169 }
10170
10171 free (edyn);
10172
015dc7e1 10173 return true;
9ea033b2
NC
10174}
10175
e9e44622
JJ
10176static void
10177print_dynamic_flags (bfd_vma flags)
d1133906 10178{
015dc7e1 10179 bool first = true;
13ae64f3 10180
d1133906
NC
10181 while (flags)
10182 {
10183 bfd_vma flag;
10184
10185 flag = flags & - flags;
10186 flags &= ~ flag;
10187
e9e44622 10188 if (first)
015dc7e1 10189 first = false;
e9e44622
JJ
10190 else
10191 putc (' ', stdout);
13ae64f3 10192
d1133906
NC
10193 switch (flag)
10194 {
e9e44622
JJ
10195 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
10196 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
10197 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
10198 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
10199 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
2b692964 10200 default: fputs (_("unknown"), stdout); break;
d1133906
NC
10201 }
10202 }
e9e44622 10203 puts ("");
d1133906
NC
10204}
10205
10ca4b04
L
10206static bfd_vma *
10207get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_size)
10208{
10209 unsigned char * e_data;
10210 bfd_vma * i_data;
10211
10212 /* If the size_t type is smaller than the bfd_size_type, eg because
10213 you are building a 32-bit tool on a 64-bit host, then make sure
10214 that when (number) is cast to (size_t) no information is lost. */
10215 if (sizeof (size_t) < sizeof (bfd_size_type)
10216 && (bfd_size_type) ((size_t) number) != number)
10217 {
10218 error (_("Size truncation prevents reading %s elements of size %u\n"),
10219 bfd_vmatoa ("u", number), ent_size);
10220 return NULL;
10221 }
10222
10223 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
10224 attempting to allocate memory when the read is bound to fail. */
10225 if (ent_size * number > filedata->file_size)
10226 {
10227 error (_("Invalid number of dynamic entries: %s\n"),
10228 bfd_vmatoa ("u", number));
10229 return NULL;
10230 }
10231
10232 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
10233 if (e_data == NULL)
10234 {
10235 error (_("Out of memory reading %s dynamic entries\n"),
10236 bfd_vmatoa ("u", number));
10237 return NULL;
10238 }
10239
10240 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
10241 {
10242 error (_("Unable to read in %s bytes of dynamic data\n"),
10243 bfd_vmatoa ("u", number * ent_size));
10244 free (e_data);
10245 return NULL;
10246 }
10247
10248 i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
10249 if (i_data == NULL)
10250 {
10251 error (_("Out of memory allocating space for %s dynamic entries\n"),
10252 bfd_vmatoa ("u", number));
10253 free (e_data);
10254 return NULL;
10255 }
10256
10257 while (number--)
10258 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
10259
10260 free (e_data);
10261
10262 return i_data;
10263}
10264
10265static unsigned long
10266get_num_dynamic_syms (Filedata * filedata)
10267{
10268 unsigned long num_of_syms = 0;
10269
10270 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
10271 return num_of_syms;
10272
978c4450 10273 if (filedata->dynamic_info[DT_HASH])
10ca4b04
L
10274 {
10275 unsigned char nb[8];
10276 unsigned char nc[8];
10277 unsigned int hash_ent_size = 4;
10278
10279 if ((filedata->file_header.e_machine == EM_ALPHA
10280 || filedata->file_header.e_machine == EM_S390
10281 || filedata->file_header.e_machine == EM_S390_OLD)
10282 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
10283 hash_ent_size = 8;
10284
10285 if (fseek (filedata->handle,
978c4450
AM
10286 (filedata->archive_file_offset
10287 + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
10ca4b04
L
10288 sizeof nb + sizeof nc)),
10289 SEEK_SET))
10290 {
10291 error (_("Unable to seek to start of dynamic information\n"));
10292 goto no_hash;
10293 }
10294
10295 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
10296 {
10297 error (_("Failed to read in number of buckets\n"));
10298 goto no_hash;
10299 }
10300
10301 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
10302 {
10303 error (_("Failed to read in number of chains\n"));
10304 goto no_hash;
10305 }
10306
978c4450
AM
10307 filedata->nbuckets = byte_get (nb, hash_ent_size);
10308 filedata->nchains = byte_get (nc, hash_ent_size);
10ca4b04 10309
2482f306
AM
10310 if (filedata->nbuckets != 0 && filedata->nchains != 0)
10311 {
10312 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
10313 hash_ent_size);
10314 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
10315 hash_ent_size);
001890e1 10316
2482f306
AM
10317 if (filedata->buckets != NULL && filedata->chains != NULL)
10318 num_of_syms = filedata->nchains;
10319 }
ceb9bf11 10320 no_hash:
10ca4b04
L
10321 if (num_of_syms == 0)
10322 {
9db70fc3
AM
10323 free (filedata->buckets);
10324 filedata->buckets = NULL;
10325 free (filedata->chains);
10326 filedata->chains = NULL;
978c4450 10327 filedata->nbuckets = 0;
10ca4b04
L
10328 }
10329 }
10330
978c4450 10331 if (filedata->dynamic_info_DT_GNU_HASH)
10ca4b04
L
10332 {
10333 unsigned char nb[16];
10334 bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
10335 bfd_vma buckets_vma;
10336 unsigned long hn;
10ca4b04
L
10337
10338 if (fseek (filedata->handle,
978c4450
AM
10339 (filedata->archive_file_offset
10340 + offset_from_vma (filedata,
10341 filedata->dynamic_info_DT_GNU_HASH,
10ca4b04
L
10342 sizeof nb)),
10343 SEEK_SET))
10344 {
10345 error (_("Unable to seek to start of dynamic information\n"));
10ca4b04
L
10346 goto no_gnu_hash;
10347 }
10348
10349 if (fread (nb, 16, 1, filedata->handle) != 1)
10350 {
10351 error (_("Failed to read in number of buckets\n"));
10ca4b04
L
10352 goto no_gnu_hash;
10353 }
10354
978c4450
AM
10355 filedata->ngnubuckets = byte_get (nb, 4);
10356 filedata->gnusymidx = byte_get (nb + 4, 4);
10ca4b04 10357 bitmaskwords = byte_get (nb + 8, 4);
978c4450 10358 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
10ca4b04
L
10359 if (is_32bit_elf)
10360 buckets_vma += bitmaskwords * 4;
10361 else
10362 buckets_vma += bitmaskwords * 8;
10363
10364 if (fseek (filedata->handle,
978c4450 10365 (filedata->archive_file_offset
10ca4b04
L
10366 + offset_from_vma (filedata, buckets_vma, 4)),
10367 SEEK_SET))
10368 {
10369 error (_("Unable to seek to start of dynamic information\n"));
10ca4b04
L
10370 goto no_gnu_hash;
10371 }
10372
978c4450
AM
10373 filedata->gnubuckets
10374 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
10ca4b04 10375
978c4450 10376 if (filedata->gnubuckets == NULL)
90837ea7 10377 goto no_gnu_hash;
10ca4b04 10378
978c4450
AM
10379 for (i = 0; i < filedata->ngnubuckets; i++)
10380 if (filedata->gnubuckets[i] != 0)
10ca4b04 10381 {
978c4450 10382 if (filedata->gnubuckets[i] < filedata->gnusymidx)
90837ea7 10383 goto no_gnu_hash;
10ca4b04 10384
978c4450
AM
10385 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
10386 maxchain = filedata->gnubuckets[i];
10ca4b04
L
10387 }
10388
10389 if (maxchain == 0xffffffff)
90837ea7 10390 goto no_gnu_hash;
10ca4b04 10391
978c4450 10392 maxchain -= filedata->gnusymidx;
10ca4b04
L
10393
10394 if (fseek (filedata->handle,
978c4450
AM
10395 (filedata->archive_file_offset
10396 + offset_from_vma (filedata,
10397 buckets_vma + 4 * (filedata->ngnubuckets
10398 + maxchain),
10399 4)),
10ca4b04
L
10400 SEEK_SET))
10401 {
10402 error (_("Unable to seek to start of dynamic information\n"));
10ca4b04
L
10403 goto no_gnu_hash;
10404 }
10405
10406 do
10407 {
10408 if (fread (nb, 4, 1, filedata->handle) != 1)
10409 {
10410 error (_("Failed to determine last chain length\n"));
10ca4b04
L
10411 goto no_gnu_hash;
10412 }
10413
10414 if (maxchain + 1 == 0)
90837ea7 10415 goto no_gnu_hash;
10ca4b04
L
10416
10417 ++maxchain;
10418 }
10419 while ((byte_get (nb, 4) & 1) == 0);
10420
10421 if (fseek (filedata->handle,
978c4450
AM
10422 (filedata->archive_file_offset
10423 + offset_from_vma (filedata, (buckets_vma
10424 + 4 * filedata->ngnubuckets),
10425 4)),
10ca4b04
L
10426 SEEK_SET))
10427 {
10428 error (_("Unable to seek to start of dynamic information\n"));
10ca4b04
L
10429 goto no_gnu_hash;
10430 }
10431
978c4450
AM
10432 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
10433 filedata->ngnuchains = maxchain;
10ca4b04 10434
978c4450 10435 if (filedata->gnuchains == NULL)
90837ea7 10436 goto no_gnu_hash;
10ca4b04 10437
978c4450 10438 if (filedata->dynamic_info_DT_MIPS_XHASH)
10ca4b04
L
10439 {
10440 if (fseek (filedata->handle,
978c4450 10441 (filedata->archive_file_offset
10ca4b04 10442 + offset_from_vma (filedata, (buckets_vma
978c4450 10443 + 4 * (filedata->ngnubuckets
10ca4b04
L
10444 + maxchain)), 4)),
10445 SEEK_SET))
10446 {
10447 error (_("Unable to seek to start of dynamic information\n"));
10ca4b04
L
10448 goto no_gnu_hash;
10449 }
10450
978c4450 10451 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
90837ea7
AM
10452 if (filedata->mipsxlat == NULL)
10453 goto no_gnu_hash;
10ca4b04
L
10454 }
10455
978c4450
AM
10456 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
10457 if (filedata->gnubuckets[hn] != 0)
10ca4b04 10458 {
978c4450
AM
10459 bfd_vma si = filedata->gnubuckets[hn];
10460 bfd_vma off = si - filedata->gnusymidx;
10ca4b04
L
10461
10462 do
10463 {
978c4450 10464 if (filedata->dynamic_info_DT_MIPS_XHASH)
10ca4b04 10465 {
c31ab5a0
AM
10466 if (off < filedata->ngnuchains
10467 && filedata->mipsxlat[off] >= num_of_syms)
978c4450 10468 num_of_syms = filedata->mipsxlat[off] + 1;
10ca4b04
L
10469 }
10470 else
10471 {
10472 if (si >= num_of_syms)
10473 num_of_syms = si + 1;
10474 }
10475 si++;
10476 }
978c4450
AM
10477 while (off < filedata->ngnuchains
10478 && (filedata->gnuchains[off++] & 1) == 0);
10ca4b04
L
10479 }
10480
90837ea7 10481 if (num_of_syms == 0)
10ca4b04 10482 {
90837ea7 10483 no_gnu_hash:
9db70fc3
AM
10484 free (filedata->mipsxlat);
10485 filedata->mipsxlat = NULL;
10486 free (filedata->gnuchains);
10487 filedata->gnuchains = NULL;
10488 free (filedata->gnubuckets);
10489 filedata->gnubuckets = NULL;
978c4450
AM
10490 filedata->ngnubuckets = 0;
10491 filedata->ngnuchains = 0;
10ca4b04
L
10492 }
10493 }
10494
10495 return num_of_syms;
10496}
10497
b2d38a17
NC
10498/* Parse and display the contents of the dynamic section. */
10499
015dc7e1 10500static bool
dda8d76d 10501process_dynamic_section (Filedata * filedata)
9ea033b2 10502{
2cf0635d 10503 Elf_Internal_Dyn * entry;
9ea033b2 10504
978c4450 10505 if (filedata->dynamic_size == 0)
9ea033b2
NC
10506 {
10507 if (do_dynamic)
ca0e11aa
NC
10508 {
10509 if (filedata->is_separate)
10510 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
10511 filedata->file_name);
10512 else
10513 printf (_("\nThere is no dynamic section in this file.\n"));
10514 }
9ea033b2 10515
015dc7e1 10516 return true;
9ea033b2
NC
10517 }
10518
10519 if (is_32bit_elf)
10520 {
dda8d76d 10521 if (! get_32bit_dynamic_section (filedata))
015dc7e1 10522 return false;
32ec8896
NC
10523 }
10524 else
10525 {
dda8d76d 10526 if (! get_64bit_dynamic_section (filedata))
015dc7e1 10527 return false;
9ea033b2 10528 }
9ea033b2 10529
252b5132 10530 /* Find the appropriate symbol table. */
978c4450 10531 if (filedata->dynamic_symbols == NULL || do_histogram)
252b5132 10532 {
2482f306
AM
10533 unsigned long num_of_syms;
10534
978c4450
AM
10535 for (entry = filedata->dynamic_section;
10536 entry < filedata->dynamic_section + filedata->dynamic_nent;
86dba8ee 10537 ++entry)
10ca4b04 10538 if (entry->d_tag == DT_SYMTAB)
978c4450 10539 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
10ca4b04 10540 else if (entry->d_tag == DT_SYMENT)
978c4450 10541 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
10ca4b04 10542 else if (entry->d_tag == DT_HASH)
978c4450 10543 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
10ca4b04 10544 else if (entry->d_tag == DT_GNU_HASH)
978c4450 10545 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10ca4b04
L
10546 else if ((filedata->file_header.e_machine == EM_MIPS
10547 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
10548 && entry->d_tag == DT_MIPS_XHASH)
10549 {
978c4450
AM
10550 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10551 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10ca4b04 10552 }
252b5132 10553
2482f306
AM
10554 num_of_syms = get_num_dynamic_syms (filedata);
10555
10556 if (num_of_syms != 0
10557 && filedata->dynamic_symbols == NULL
10558 && filedata->dynamic_info[DT_SYMTAB]
978c4450 10559 && filedata->dynamic_info[DT_SYMENT])
10ca4b04
L
10560 {
10561 Elf_Internal_Phdr *seg;
2482f306 10562 bfd_vma vma = filedata->dynamic_info[DT_SYMTAB];
252b5132 10563
2482f306
AM
10564 if (! get_program_headers (filedata))
10565 {
10566 error (_("Cannot interpret virtual addresses "
10567 "without program headers.\n"));
015dc7e1 10568 return false;
2482f306 10569 }
252b5132 10570
2482f306
AM
10571 for (seg = filedata->program_headers;
10572 seg < filedata->program_headers + filedata->file_header.e_phnum;
10573 ++seg)
10574 {
10575 if (seg->p_type != PT_LOAD)
10576 continue;
252b5132 10577
2482f306
AM
10578 if (seg->p_offset + seg->p_filesz > filedata->file_size)
10579 {
10580 /* See PR 21379 for a reproducer. */
10581 error (_("Invalid PT_LOAD entry\n"));
015dc7e1 10582 return false;
2482f306 10583 }
252b5132 10584
2482f306
AM
10585 if (vma >= (seg->p_vaddr & -seg->p_align)
10586 && vma < seg->p_vaddr + seg->p_filesz)
10587 {
10588 /* Since we do not know how big the symbol table is,
10589 we default to reading in up to the end of PT_LOAD
10590 segment and processing that. This is overkill, I
10591 know, but it should work. */
10592 Elf_Internal_Shdr section;
10593 section.sh_offset = (vma - seg->p_vaddr
10594 + seg->p_offset);
10595 section.sh_size = (num_of_syms
10596 * filedata->dynamic_info[DT_SYMENT]);
10597 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
8ac10c5b
L
10598
10599 if (do_checks
10600 && filedata->dynamic_symtab_section != NULL
10601 && ((filedata->dynamic_symtab_section->sh_offset
10602 != section.sh_offset)
10603 || (filedata->dynamic_symtab_section->sh_size
10604 != section.sh_size)
10605 || (filedata->dynamic_symtab_section->sh_entsize
10606 != section.sh_entsize)))
10607 warn (_("\
10608the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
10609
2482f306
AM
10610 section.sh_name = filedata->string_table_length;
10611 filedata->dynamic_symbols
10612 = GET_ELF_SYMBOLS (filedata, &section,
10613 &filedata->num_dynamic_syms);
10614 if (filedata->dynamic_symbols == NULL
10615 || filedata->num_dynamic_syms != num_of_syms)
10616 {
10617 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
015dc7e1 10618 return false;
2482f306
AM
10619 }
10620 break;
10621 }
10622 }
10623 }
10624 }
252b5132
RH
10625
10626 /* Similarly find a string table. */
978c4450
AM
10627 if (filedata->dynamic_strings == NULL)
10628 for (entry = filedata->dynamic_section;
10629 entry < filedata->dynamic_section + filedata->dynamic_nent;
10ca4b04
L
10630 ++entry)
10631 {
10632 if (entry->d_tag == DT_STRTAB)
978c4450 10633 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
252b5132 10634
10ca4b04 10635 if (entry->d_tag == DT_STRSZ)
978c4450 10636 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
252b5132 10637
978c4450
AM
10638 if (filedata->dynamic_info[DT_STRTAB]
10639 && filedata->dynamic_info[DT_STRSZ])
10ca4b04
L
10640 {
10641 unsigned long offset;
978c4450 10642 bfd_size_type str_tab_len = filedata->dynamic_info[DT_STRSZ];
10ca4b04
L
10643
10644 offset = offset_from_vma (filedata,
978c4450 10645 filedata->dynamic_info[DT_STRTAB],
10ca4b04 10646 str_tab_len);
8ac10c5b
L
10647 if (do_checks
10648 && filedata->dynamic_strtab_section
10649 && ((filedata->dynamic_strtab_section->sh_offset
10650 != (file_ptr) offset)
10651 || (filedata->dynamic_strtab_section->sh_size
10652 != str_tab_len)))
10653 warn (_("\
10654the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
10655
978c4450
AM
10656 filedata->dynamic_strings
10657 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
10658 _("dynamic string table"));
10659 if (filedata->dynamic_strings == NULL)
10ca4b04
L
10660 {
10661 error (_("Corrupt DT_STRTAB dynamic entry\n"));
10662 break;
10663 }
e3d39609 10664
978c4450 10665 filedata->dynamic_strings_length = str_tab_len;
10ca4b04
L
10666 break;
10667 }
10668 }
252b5132
RH
10669
10670 /* And find the syminfo section if available. */
978c4450 10671 if (filedata->dynamic_syminfo == NULL)
252b5132 10672 {
3e8bba36 10673 unsigned long syminsz = 0;
252b5132 10674
978c4450
AM
10675 for (entry = filedata->dynamic_section;
10676 entry < filedata->dynamic_section + filedata->dynamic_nent;
86dba8ee 10677 ++entry)
252b5132
RH
10678 {
10679 if (entry->d_tag == DT_SYMINENT)
10680 {
10681 /* Note: these braces are necessary to avoid a syntax
10682 error from the SunOS4 C compiler. */
049b0c3a
NC
10683 /* PR binutils/17531: A corrupt file can trigger this test.
10684 So do not use an assert, instead generate an error message. */
10685 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
071436c6 10686 error (_("Bad value (%d) for SYMINENT entry\n"),
049b0c3a 10687 (int) entry->d_un.d_val);
252b5132
RH
10688 }
10689 else if (entry->d_tag == DT_SYMINSZ)
10690 syminsz = entry->d_un.d_val;
10691 else if (entry->d_tag == DT_SYMINFO)
978c4450
AM
10692 filedata->dynamic_syminfo_offset
10693 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
252b5132
RH
10694 }
10695
978c4450 10696 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
252b5132 10697 {
2cf0635d
NC
10698 Elf_External_Syminfo * extsyminfo;
10699 Elf_External_Syminfo * extsym;
10700 Elf_Internal_Syminfo * syminfo;
252b5132
RH
10701
10702 /* There is a syminfo section. Read the data. */
3f5e193b 10703 extsyminfo = (Elf_External_Syminfo *)
978c4450
AM
10704 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
10705 1, syminsz, _("symbol information"));
a6e9f9df 10706 if (!extsyminfo)
015dc7e1 10707 return false;
252b5132 10708
978c4450 10709 if (filedata->dynamic_syminfo != NULL)
e3d39609
NC
10710 {
10711 error (_("Multiple dynamic symbol information sections found\n"));
978c4450 10712 free (filedata->dynamic_syminfo);
e3d39609 10713 }
978c4450
AM
10714 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
10715 if (filedata->dynamic_syminfo == NULL)
252b5132 10716 {
2482f306
AM
10717 error (_("Out of memory allocating %lu bytes "
10718 "for dynamic symbol info\n"),
8b73c356 10719 (unsigned long) syminsz);
015dc7e1 10720 return false;
252b5132
RH
10721 }
10722
2482f306
AM
10723 filedata->dynamic_syminfo_nent
10724 = syminsz / sizeof (Elf_External_Syminfo);
978c4450 10725 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
2482f306
AM
10726 syminfo < (filedata->dynamic_syminfo
10727 + filedata->dynamic_syminfo_nent);
86dba8ee 10728 ++syminfo, ++extsym)
252b5132 10729 {
86dba8ee
AM
10730 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
10731 syminfo->si_flags = BYTE_GET (extsym->si_flags);
252b5132
RH
10732 }
10733
10734 free (extsyminfo);
10735 }
10736 }
10737
978c4450 10738 if (do_dynamic && filedata->dynamic_addr)
ca0e11aa
NC
10739 {
10740 if (filedata->dynamic_nent == 1)
10741 {
10742 if (filedata->is_separate)
10743 printf (_("\nIn linked file '%s' the dynamic section at offset 0x%lx contains 1 entry:\n"),
10744 filedata->file_name,
10745 filedata->dynamic_addr);
10746 else
10747 printf (_("\nDynamic section at offset 0x%lx contains 1 entry:\n"),
10748 filedata->dynamic_addr);
10749 }
10750 else
10751 {
10752 if (filedata->is_separate)
10753 printf (_("\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entries:\n"),
10754 filedata->file_name,
10755 filedata->dynamic_addr,
10756 (unsigned long) filedata->dynamic_nent);
10757 else
10758 printf (_("\nDynamic section at offset 0x%lx contains %lu entries:\n"),
10759 filedata->dynamic_addr,
10760 (unsigned long) filedata->dynamic_nent);
10761 }
10762 }
252b5132
RH
10763 if (do_dynamic)
10764 printf (_(" Tag Type Name/Value\n"));
10765
978c4450
AM
10766 for (entry = filedata->dynamic_section;
10767 entry < filedata->dynamic_section + filedata->dynamic_nent;
86dba8ee 10768 entry++)
252b5132
RH
10769 {
10770 if (do_dynamic)
f7a99963 10771 {
2cf0635d 10772 const char * dtype;
e699b9ff 10773
f7a99963
NC
10774 putchar (' ');
10775 print_vma (entry->d_tag, FULL_HEX);
dda8d76d 10776 dtype = get_dynamic_type (filedata, entry->d_tag);
e699b9ff 10777 printf (" (%s)%*s", dtype,
32ec8896 10778 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
f7a99963 10779 }
252b5132
RH
10780
10781 switch (entry->d_tag)
10782 {
d1133906
NC
10783 case DT_FLAGS:
10784 if (do_dynamic)
e9e44622 10785 print_dynamic_flags (entry->d_un.d_val);
d1133906 10786 break;
76da6bbe 10787
252b5132
RH
10788 case DT_AUXILIARY:
10789 case DT_FILTER:
019148e4
L
10790 case DT_CONFIG:
10791 case DT_DEPAUDIT:
10792 case DT_AUDIT:
252b5132
RH
10793 if (do_dynamic)
10794 {
019148e4 10795 switch (entry->d_tag)
b34976b6 10796 {
019148e4
L
10797 case DT_AUXILIARY:
10798 printf (_("Auxiliary library"));
10799 break;
10800
10801 case DT_FILTER:
10802 printf (_("Filter library"));
10803 break;
10804
b34976b6 10805 case DT_CONFIG:
019148e4
L
10806 printf (_("Configuration file"));
10807 break;
10808
10809 case DT_DEPAUDIT:
10810 printf (_("Dependency audit library"));
10811 break;
10812
10813 case DT_AUDIT:
10814 printf (_("Audit library"));
10815 break;
10816 }
252b5132 10817
978c4450
AM
10818 if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
10819 printf (": [%s]\n",
10820 GET_DYNAMIC_NAME (filedata, entry->d_un.d_val));
252b5132 10821 else
f7a99963
NC
10822 {
10823 printf (": ");
10824 print_vma (entry->d_un.d_val, PREFIX_HEX);
10825 putchar ('\n');
10826 }
252b5132
RH
10827 }
10828 break;
10829
dcefbbbd 10830 case DT_FEATURE:
252b5132
RH
10831 if (do_dynamic)
10832 {
10833 printf (_("Flags:"));
86f55779 10834
252b5132
RH
10835 if (entry->d_un.d_val == 0)
10836 printf (_(" None\n"));
10837 else
10838 {
10839 unsigned long int val = entry->d_un.d_val;
86f55779 10840
252b5132
RH
10841 if (val & DTF_1_PARINIT)
10842 {
10843 printf (" PARINIT");
10844 val ^= DTF_1_PARINIT;
10845 }
dcefbbbd
L
10846 if (val & DTF_1_CONFEXP)
10847 {
10848 printf (" CONFEXP");
10849 val ^= DTF_1_CONFEXP;
10850 }
252b5132
RH
10851 if (val != 0)
10852 printf (" %lx", val);
10853 puts ("");
10854 }
10855 }
10856 break;
10857
10858 case DT_POSFLAG_1:
10859 if (do_dynamic)
10860 {
10861 printf (_("Flags:"));
86f55779 10862
252b5132
RH
10863 if (entry->d_un.d_val == 0)
10864 printf (_(" None\n"));
10865 else
10866 {
10867 unsigned long int val = entry->d_un.d_val;
86f55779 10868
252b5132
RH
10869 if (val & DF_P1_LAZYLOAD)
10870 {
10871 printf (" LAZYLOAD");
10872 val ^= DF_P1_LAZYLOAD;
10873 }
10874 if (val & DF_P1_GROUPPERM)
10875 {
10876 printf (" GROUPPERM");
10877 val ^= DF_P1_GROUPPERM;
10878 }
10879 if (val != 0)
10880 printf (" %lx", val);
10881 puts ("");
10882 }
10883 }
10884 break;
10885
10886 case DT_FLAGS_1:
10887 if (do_dynamic)
10888 {
10889 printf (_("Flags:"));
10890 if (entry->d_un.d_val == 0)
10891 printf (_(" None\n"));
10892 else
10893 {
10894 unsigned long int val = entry->d_un.d_val;
86f55779 10895
252b5132
RH
10896 if (val & DF_1_NOW)
10897 {
10898 printf (" NOW");
10899 val ^= DF_1_NOW;
10900 }
10901 if (val & DF_1_GLOBAL)
10902 {
10903 printf (" GLOBAL");
10904 val ^= DF_1_GLOBAL;
10905 }
10906 if (val & DF_1_GROUP)
10907 {
10908 printf (" GROUP");
10909 val ^= DF_1_GROUP;
10910 }
10911 if (val & DF_1_NODELETE)
10912 {
10913 printf (" NODELETE");
10914 val ^= DF_1_NODELETE;
10915 }
10916 if (val & DF_1_LOADFLTR)
10917 {
10918 printf (" LOADFLTR");
10919 val ^= DF_1_LOADFLTR;
10920 }
10921 if (val & DF_1_INITFIRST)
10922 {
10923 printf (" INITFIRST");
10924 val ^= DF_1_INITFIRST;
10925 }
10926 if (val & DF_1_NOOPEN)
10927 {
10928 printf (" NOOPEN");
10929 val ^= DF_1_NOOPEN;
10930 }
10931 if (val & DF_1_ORIGIN)
10932 {
10933 printf (" ORIGIN");
10934 val ^= DF_1_ORIGIN;
10935 }
10936 if (val & DF_1_DIRECT)
10937 {
10938 printf (" DIRECT");
10939 val ^= DF_1_DIRECT;
10940 }
10941 if (val & DF_1_TRANS)
10942 {
10943 printf (" TRANS");
10944 val ^= DF_1_TRANS;
10945 }
10946 if (val & DF_1_INTERPOSE)
10947 {
10948 printf (" INTERPOSE");
10949 val ^= DF_1_INTERPOSE;
10950 }
f7db6139 10951 if (val & DF_1_NODEFLIB)
dcefbbbd 10952 {
f7db6139
L
10953 printf (" NODEFLIB");
10954 val ^= DF_1_NODEFLIB;
dcefbbbd
L
10955 }
10956 if (val & DF_1_NODUMP)
10957 {
10958 printf (" NODUMP");
10959 val ^= DF_1_NODUMP;
10960 }
34b60028 10961 if (val & DF_1_CONFALT)
dcefbbbd 10962 {
34b60028
L
10963 printf (" CONFALT");
10964 val ^= DF_1_CONFALT;
10965 }
10966 if (val & DF_1_ENDFILTEE)
10967 {
10968 printf (" ENDFILTEE");
10969 val ^= DF_1_ENDFILTEE;
10970 }
10971 if (val & DF_1_DISPRELDNE)
10972 {
10973 printf (" DISPRELDNE");
10974 val ^= DF_1_DISPRELDNE;
10975 }
10976 if (val & DF_1_DISPRELPND)
10977 {
10978 printf (" DISPRELPND");
10979 val ^= DF_1_DISPRELPND;
10980 }
10981 if (val & DF_1_NODIRECT)
10982 {
10983 printf (" NODIRECT");
10984 val ^= DF_1_NODIRECT;
10985 }
10986 if (val & DF_1_IGNMULDEF)
10987 {
10988 printf (" IGNMULDEF");
10989 val ^= DF_1_IGNMULDEF;
10990 }
10991 if (val & DF_1_NOKSYMS)
10992 {
10993 printf (" NOKSYMS");
10994 val ^= DF_1_NOKSYMS;
10995 }
10996 if (val & DF_1_NOHDR)
10997 {
10998 printf (" NOHDR");
10999 val ^= DF_1_NOHDR;
11000 }
11001 if (val & DF_1_EDITED)
11002 {
11003 printf (" EDITED");
11004 val ^= DF_1_EDITED;
11005 }
11006 if (val & DF_1_NORELOC)
11007 {
11008 printf (" NORELOC");
11009 val ^= DF_1_NORELOC;
11010 }
11011 if (val & DF_1_SYMINTPOSE)
11012 {
11013 printf (" SYMINTPOSE");
11014 val ^= DF_1_SYMINTPOSE;
11015 }
11016 if (val & DF_1_GLOBAUDIT)
11017 {
11018 printf (" GLOBAUDIT");
11019 val ^= DF_1_GLOBAUDIT;
11020 }
11021 if (val & DF_1_SINGLETON)
11022 {
11023 printf (" SINGLETON");
11024 val ^= DF_1_SINGLETON;
dcefbbbd 11025 }
5c383f02
RO
11026 if (val & DF_1_STUB)
11027 {
11028 printf (" STUB");
11029 val ^= DF_1_STUB;
11030 }
11031 if (val & DF_1_PIE)
11032 {
11033 printf (" PIE");
11034 val ^= DF_1_PIE;
11035 }
b1202ffa
L
11036 if (val & DF_1_KMOD)
11037 {
11038 printf (" KMOD");
11039 val ^= DF_1_KMOD;
11040 }
11041 if (val & DF_1_WEAKFILTER)
11042 {
11043 printf (" WEAKFILTER");
11044 val ^= DF_1_WEAKFILTER;
11045 }
11046 if (val & DF_1_NOCOMMON)
11047 {
11048 printf (" NOCOMMON");
11049 val ^= DF_1_NOCOMMON;
11050 }
252b5132
RH
11051 if (val != 0)
11052 printf (" %lx", val);
11053 puts ("");
11054 }
11055 }
11056 break;
11057
11058 case DT_PLTREL:
978c4450 11059 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
252b5132 11060 if (do_dynamic)
dda8d76d 11061 puts (get_dynamic_type (filedata, entry->d_un.d_val));
252b5132
RH
11062 break;
11063
11064 case DT_NULL :
11065 case DT_NEEDED :
11066 case DT_PLTGOT :
11067 case DT_HASH :
11068 case DT_STRTAB :
11069 case DT_SYMTAB :
11070 case DT_RELA :
11071 case DT_INIT :
11072 case DT_FINI :
11073 case DT_SONAME :
11074 case DT_RPATH :
11075 case DT_SYMBOLIC:
11076 case DT_REL :
11077 case DT_DEBUG :
11078 case DT_TEXTREL :
11079 case DT_JMPREL :
019148e4 11080 case DT_RUNPATH :
978c4450 11081 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
252b5132
RH
11082
11083 if (do_dynamic)
11084 {
2cf0635d 11085 char * name;
252b5132 11086
978c4450
AM
11087 if (VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
11088 name = GET_DYNAMIC_NAME (filedata, entry->d_un.d_val);
252b5132 11089 else
d79b3d50 11090 name = NULL;
252b5132
RH
11091
11092 if (name)
11093 {
11094 switch (entry->d_tag)
11095 {
11096 case DT_NEEDED:
11097 printf (_("Shared library: [%s]"), name);
11098
978c4450 11099 if (streq (name, filedata->program_interpreter))
f7a99963 11100 printf (_(" program interpreter"));
252b5132
RH
11101 break;
11102
11103 case DT_SONAME:
f7a99963 11104 printf (_("Library soname: [%s]"), name);
252b5132
RH
11105 break;
11106
11107 case DT_RPATH:
f7a99963 11108 printf (_("Library rpath: [%s]"), name);
252b5132
RH
11109 break;
11110
019148e4
L
11111 case DT_RUNPATH:
11112 printf (_("Library runpath: [%s]"), name);
11113 break;
11114
252b5132 11115 default:
f7a99963
NC
11116 print_vma (entry->d_un.d_val, PREFIX_HEX);
11117 break;
252b5132
RH
11118 }
11119 }
11120 else
f7a99963
NC
11121 print_vma (entry->d_un.d_val, PREFIX_HEX);
11122
11123 putchar ('\n');
252b5132
RH
11124 }
11125 break;
11126
11127 case DT_PLTRELSZ:
11128 case DT_RELASZ :
11129 case DT_STRSZ :
11130 case DT_RELSZ :
11131 case DT_RELAENT :
11132 case DT_SYMENT :
11133 case DT_RELENT :
978c4450 11134 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
1a0670f3 11135 /* Fall through. */
252b5132
RH
11136 case DT_PLTPADSZ:
11137 case DT_MOVEENT :
11138 case DT_MOVESZ :
11139 case DT_INIT_ARRAYSZ:
11140 case DT_FINI_ARRAYSZ:
047b2264
JJ
11141 case DT_GNU_CONFLICTSZ:
11142 case DT_GNU_LIBLISTSZ:
252b5132 11143 if (do_dynamic)
f7a99963
NC
11144 {
11145 print_vma (entry->d_un.d_val, UNSIGNED);
2b692964 11146 printf (_(" (bytes)\n"));
f7a99963 11147 }
252b5132
RH
11148 break;
11149
11150 case DT_VERDEFNUM:
11151 case DT_VERNEEDNUM:
11152 case DT_RELACOUNT:
11153 case DT_RELCOUNT:
11154 if (do_dynamic)
f7a99963
NC
11155 {
11156 print_vma (entry->d_un.d_val, UNSIGNED);
11157 putchar ('\n');
11158 }
252b5132
RH
11159 break;
11160
11161 case DT_SYMINSZ:
11162 case DT_SYMINENT:
11163 case DT_SYMINFO:
11164 case DT_USED:
11165 case DT_INIT_ARRAY:
11166 case DT_FINI_ARRAY:
11167 if (do_dynamic)
11168 {
d79b3d50 11169 if (entry->d_tag == DT_USED
978c4450 11170 && VALID_DYNAMIC_NAME (filedata, entry->d_un.d_val))
252b5132 11171 {
978c4450 11172 char * name = GET_DYNAMIC_NAME (filedata, entry->d_un.d_val);
252b5132 11173
b34976b6 11174 if (*name)
252b5132
RH
11175 {
11176 printf (_("Not needed object: [%s]\n"), name);
11177 break;
11178 }
11179 }
103f02d3 11180
f7a99963
NC
11181 print_vma (entry->d_un.d_val, PREFIX_HEX);
11182 putchar ('\n');
252b5132
RH
11183 }
11184 break;
11185
11186 case DT_BIND_NOW:
11187 /* The value of this entry is ignored. */
35b1837e
AM
11188 if (do_dynamic)
11189 putchar ('\n');
252b5132 11190 break;
103f02d3 11191
047b2264
JJ
11192 case DT_GNU_PRELINKED:
11193 if (do_dynamic)
11194 {
2cf0635d 11195 struct tm * tmp;
91d6fa6a 11196 time_t atime = entry->d_un.d_val;
047b2264 11197
91d6fa6a 11198 tmp = gmtime (&atime);
071436c6
NC
11199 /* PR 17533 file: 041-1244816-0.004. */
11200 if (tmp == NULL)
5a2cbcf4
L
11201 printf (_("<corrupt time val: %lx"),
11202 (unsigned long) atime);
071436c6
NC
11203 else
11204 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
11205 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11206 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
047b2264
JJ
11207
11208 }
11209 break;
11210
fdc90cb4 11211 case DT_GNU_HASH:
978c4450 11212 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
fdc90cb4
JJ
11213 if (do_dynamic)
11214 {
11215 print_vma (entry->d_un.d_val, PREFIX_HEX);
11216 putchar ('\n');
11217 }
11218 break;
11219
a5da3dee
VDM
11220 case DT_GNU_FLAGS_1:
11221 if (do_dynamic)
11222 {
11223 printf (_("Flags:"));
11224 if (entry->d_un.d_val == 0)
11225 printf (_(" None\n"));
11226 else
11227 {
11228 unsigned long int val = entry->d_un.d_val;
11229
11230 if (val & DF_GNU_1_UNIQUE)
11231 {
11232 printf (" UNIQUE");
11233 val ^= DF_GNU_1_UNIQUE;
11234 }
11235 if (val != 0)
11236 printf (" %lx", val);
11237 puts ("");
11238 }
11239 }
11240 break;
11241
252b5132
RH
11242 default:
11243 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
978c4450
AM
11244 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
11245 = entry->d_un.d_val;
252b5132
RH
11246
11247 if (do_dynamic)
11248 {
dda8d76d 11249 switch (filedata->file_header.e_machine)
252b5132 11250 {
37c18eed
SD
11251 case EM_AARCH64:
11252 dynamic_section_aarch64_val (entry);
11253 break;
252b5132 11254 case EM_MIPS:
4fe85591 11255 case EM_MIPS_RS3_LE:
978c4450 11256 dynamic_section_mips_val (filedata, entry);
252b5132 11257 break;
103f02d3 11258 case EM_PARISC:
b2d38a17 11259 dynamic_section_parisc_val (entry);
103f02d3 11260 break;
ecc51f48 11261 case EM_IA_64:
b2d38a17 11262 dynamic_section_ia64_val (entry);
ecc51f48 11263 break;
252b5132 11264 default:
f7a99963
NC
11265 print_vma (entry->d_un.d_val, PREFIX_HEX);
11266 putchar ('\n');
252b5132
RH
11267 }
11268 }
11269 break;
11270 }
11271 }
11272
015dc7e1 11273 return true;
252b5132
RH
11274}
11275
11276static char *
d3ba0551 11277get_ver_flags (unsigned int flags)
252b5132 11278{
6d4f21f6 11279 static char buff[128];
252b5132
RH
11280
11281 buff[0] = 0;
11282
11283 if (flags == 0)
11284 return _("none");
11285
11286 if (flags & VER_FLG_BASE)
7bb1ad17 11287 strcat (buff, "BASE");
252b5132
RH
11288
11289 if (flags & VER_FLG_WEAK)
11290 {
11291 if (flags & VER_FLG_BASE)
7bb1ad17 11292 strcat (buff, " | ");
252b5132 11293
7bb1ad17 11294 strcat (buff, "WEAK");
252b5132
RH
11295 }
11296
44ec90b9
RO
11297 if (flags & VER_FLG_INFO)
11298 {
11299 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
7bb1ad17 11300 strcat (buff, " | ");
44ec90b9 11301
7bb1ad17 11302 strcat (buff, "INFO");
44ec90b9
RO
11303 }
11304
11305 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
7bb1ad17
MR
11306 {
11307 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11308 strcat (buff, " | ");
11309
11310 strcat (buff, _("<unknown>"));
11311 }
252b5132
RH
11312
11313 return buff;
11314}
11315
11316/* Display the contents of the version sections. */
98fb390a 11317
015dc7e1 11318static bool
dda8d76d 11319process_version_sections (Filedata * filedata)
252b5132 11320{
2cf0635d 11321 Elf_Internal_Shdr * section;
b34976b6 11322 unsigned i;
015dc7e1 11323 bool found = false;
252b5132
RH
11324
11325 if (! do_version)
015dc7e1 11326 return true;
252b5132 11327
dda8d76d
NC
11328 for (i = 0, section = filedata->section_headers;
11329 i < filedata->file_header.e_shnum;
b34976b6 11330 i++, section++)
252b5132
RH
11331 {
11332 switch (section->sh_type)
11333 {
11334 case SHT_GNU_verdef:
11335 {
2cf0635d 11336 Elf_External_Verdef * edefs;
452bf675
AM
11337 unsigned long idx;
11338 unsigned long cnt;
2cf0635d 11339 char * endbuf;
252b5132 11340
015dc7e1 11341 found = true;
252b5132 11342
ca0e11aa
NC
11343 if (filedata->is_separate)
11344 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
11345 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
11346 section->sh_info),
11347 filedata->file_name,
11348 printable_section_name (filedata, section),
11349 section->sh_info);
11350 else
11351 printf (ngettext ("\nVersion definition section '%s' "
11352 "contains %u entry:\n",
11353 "\nVersion definition section '%s' "
11354 "contains %u entries:\n",
11355 section->sh_info),
11356 printable_section_name (filedata, section),
11357 section->sh_info);
11358
ae9ac79e 11359 printf (_(" Addr: 0x"));
252b5132 11360 printf_vma (section->sh_addr);
233f82cf 11361 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
1b228002 11362 (unsigned long) section->sh_offset, section->sh_link,
dda8d76d 11363 printable_section_name_from_index (filedata, section->sh_link));
252b5132 11364
3f5e193b 11365 edefs = (Elf_External_Verdef *)
dda8d76d 11366 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
3f5e193b 11367 _("version definition section"));
a6e9f9df
AM
11368 if (!edefs)
11369 break;
59245841 11370 endbuf = (char *) edefs + section->sh_size;
252b5132 11371
1445030f 11372 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
252b5132 11373 {
2cf0635d
NC
11374 char * vstart;
11375 Elf_External_Verdef * edef;
b34976b6 11376 Elf_Internal_Verdef ent;
2cf0635d 11377 Elf_External_Verdaux * eaux;
b34976b6 11378 Elf_Internal_Verdaux aux;
452bf675 11379 unsigned long isum;
b34976b6 11380 int j;
103f02d3 11381
252b5132 11382 vstart = ((char *) edefs) + idx;
54806181
AM
11383 if (vstart + sizeof (*edef) > endbuf)
11384 break;
252b5132
RH
11385
11386 edef = (Elf_External_Verdef *) vstart;
11387
11388 ent.vd_version = BYTE_GET (edef->vd_version);
11389 ent.vd_flags = BYTE_GET (edef->vd_flags);
11390 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
11391 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
11392 ent.vd_hash = BYTE_GET (edef->vd_hash);
11393 ent.vd_aux = BYTE_GET (edef->vd_aux);
11394 ent.vd_next = BYTE_GET (edef->vd_next);
11395
452bf675 11396 printf (_(" %#06lx: Rev: %d Flags: %s"),
252b5132
RH
11397 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
11398
11399 printf (_(" Index: %d Cnt: %d "),
11400 ent.vd_ndx, ent.vd_cnt);
11401
452bf675 11402 /* Check for overflow. */
1445030f 11403 if (ent.vd_aux > (size_t) (endbuf - vstart))
dd24e3da
NC
11404 break;
11405
252b5132
RH
11406 vstart += ent.vd_aux;
11407
1445030f
AM
11408 if (vstart + sizeof (*eaux) > endbuf)
11409 break;
252b5132
RH
11410 eaux = (Elf_External_Verdaux *) vstart;
11411
11412 aux.vda_name = BYTE_GET (eaux->vda_name);
11413 aux.vda_next = BYTE_GET (eaux->vda_next);
11414
978c4450
AM
11415 if (VALID_DYNAMIC_NAME (filedata, aux.vda_name))
11416 printf (_("Name: %s\n"),
11417 GET_DYNAMIC_NAME (filedata, aux.vda_name));
252b5132
RH
11418 else
11419 printf (_("Name index: %ld\n"), aux.vda_name);
11420
11421 isum = idx + ent.vd_aux;
11422
b34976b6 11423 for (j = 1; j < ent.vd_cnt; j++)
252b5132 11424 {
1445030f
AM
11425 if (aux.vda_next < sizeof (*eaux)
11426 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
11427 {
11428 warn (_("Invalid vda_next field of %lx\n"),
11429 aux.vda_next);
11430 j = ent.vd_cnt;
11431 break;
11432 }
dd24e3da 11433 /* Check for overflow. */
7e26601c 11434 if (aux.vda_next > (size_t) (endbuf - vstart))
dd24e3da
NC
11435 break;
11436
252b5132
RH
11437 isum += aux.vda_next;
11438 vstart += aux.vda_next;
11439
54806181
AM
11440 if (vstart + sizeof (*eaux) > endbuf)
11441 break;
1445030f 11442 eaux = (Elf_External_Verdaux *) vstart;
252b5132
RH
11443
11444 aux.vda_name = BYTE_GET (eaux->vda_name);
11445 aux.vda_next = BYTE_GET (eaux->vda_next);
11446
978c4450 11447 if (VALID_DYNAMIC_NAME (filedata, aux.vda_name))
452bf675 11448 printf (_(" %#06lx: Parent %d: %s\n"),
978c4450
AM
11449 isum, j,
11450 GET_DYNAMIC_NAME (filedata, aux.vda_name));
252b5132 11451 else
452bf675 11452 printf (_(" %#06lx: Parent %d, name index: %ld\n"),
252b5132
RH
11453 isum, j, aux.vda_name);
11454 }
dd24e3da 11455
54806181
AM
11456 if (j < ent.vd_cnt)
11457 printf (_(" Version def aux past end of section\n"));
252b5132 11458
c9f02c3e
MR
11459 /* PR 17531:
11460 file: id:000001,src:000172+005151,op:splice,rep:2. */
1445030f
AM
11461 if (ent.vd_next < sizeof (*edef)
11462 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
11463 {
11464 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
11465 cnt = section->sh_info;
11466 break;
11467 }
452bf675 11468 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
5d921cbd
NC
11469 break;
11470
252b5132
RH
11471 idx += ent.vd_next;
11472 }
dd24e3da 11473
54806181
AM
11474 if (cnt < section->sh_info)
11475 printf (_(" Version definition past end of section\n"));
252b5132
RH
11476
11477 free (edefs);
11478 }
11479 break;
103f02d3 11480
252b5132
RH
11481 case SHT_GNU_verneed:
11482 {
2cf0635d 11483 Elf_External_Verneed * eneed;
452bf675
AM
11484 unsigned long idx;
11485 unsigned long cnt;
2cf0635d 11486 char * endbuf;
252b5132 11487
015dc7e1 11488 found = true;
252b5132 11489
ca0e11aa
NC
11490 if (filedata->is_separate)
11491 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
11492 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
11493 section->sh_info),
11494 filedata->file_name,
11495 printable_section_name (filedata, section),
11496 section->sh_info);
11497 else
11498 printf (ngettext ("\nVersion needs section '%s' "
11499 "contains %u entry:\n",
11500 "\nVersion needs section '%s' "
11501 "contains %u entries:\n",
11502 section->sh_info),
11503 printable_section_name (filedata, section),
11504 section->sh_info);
11505
252b5132
RH
11506 printf (_(" Addr: 0x"));
11507 printf_vma (section->sh_addr);
72de5009 11508 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
1b228002 11509 (unsigned long) section->sh_offset, section->sh_link,
dda8d76d 11510 printable_section_name_from_index (filedata, section->sh_link));
252b5132 11511
dda8d76d 11512 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
3f5e193b
NC
11513 section->sh_offset, 1,
11514 section->sh_size,
9cf03b7e 11515 _("Version Needs section"));
a6e9f9df
AM
11516 if (!eneed)
11517 break;
59245841 11518 endbuf = (char *) eneed + section->sh_size;
252b5132
RH
11519
11520 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
11521 {
2cf0635d 11522 Elf_External_Verneed * entry;
b34976b6 11523 Elf_Internal_Verneed ent;
452bf675 11524 unsigned long isum;
b34976b6 11525 int j;
2cf0635d 11526 char * vstart;
252b5132
RH
11527
11528 vstart = ((char *) eneed) + idx;
54806181
AM
11529 if (vstart + sizeof (*entry) > endbuf)
11530 break;
252b5132
RH
11531
11532 entry = (Elf_External_Verneed *) vstart;
11533
11534 ent.vn_version = BYTE_GET (entry->vn_version);
11535 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
11536 ent.vn_file = BYTE_GET (entry->vn_file);
11537 ent.vn_aux = BYTE_GET (entry->vn_aux);
11538 ent.vn_next = BYTE_GET (entry->vn_next);
11539
452bf675 11540 printf (_(" %#06lx: Version: %d"), idx, ent.vn_version);
252b5132 11541
978c4450
AM
11542 if (VALID_DYNAMIC_NAME (filedata, ent.vn_file))
11543 printf (_(" File: %s"),
11544 GET_DYNAMIC_NAME (filedata, ent.vn_file));
252b5132
RH
11545 else
11546 printf (_(" File: %lx"), ent.vn_file);
11547
11548 printf (_(" Cnt: %d\n"), ent.vn_cnt);
11549
dd24e3da 11550 /* Check for overflow. */
7e26601c 11551 if (ent.vn_aux > (size_t) (endbuf - vstart))
dd24e3da 11552 break;
252b5132
RH
11553 vstart += ent.vn_aux;
11554
11555 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
11556 {
2cf0635d 11557 Elf_External_Vernaux * eaux;
b34976b6 11558 Elf_Internal_Vernaux aux;
252b5132 11559
54806181
AM
11560 if (vstart + sizeof (*eaux) > endbuf)
11561 break;
252b5132
RH
11562 eaux = (Elf_External_Vernaux *) vstart;
11563
11564 aux.vna_hash = BYTE_GET (eaux->vna_hash);
11565 aux.vna_flags = BYTE_GET (eaux->vna_flags);
11566 aux.vna_other = BYTE_GET (eaux->vna_other);
11567 aux.vna_name = BYTE_GET (eaux->vna_name);
11568 aux.vna_next = BYTE_GET (eaux->vna_next);
11569
978c4450 11570 if (VALID_DYNAMIC_NAME (filedata, aux.vna_name))
452bf675 11571 printf (_(" %#06lx: Name: %s"),
978c4450 11572 isum, GET_DYNAMIC_NAME (filedata, aux.vna_name));
252b5132 11573 else
452bf675 11574 printf (_(" %#06lx: Name index: %lx"),
252b5132
RH
11575 isum, aux.vna_name);
11576
11577 printf (_(" Flags: %s Version: %d\n"),
11578 get_ver_flags (aux.vna_flags), aux.vna_other);
11579
1445030f
AM
11580 if (aux.vna_next < sizeof (*eaux)
11581 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
53774b7e
NC
11582 {
11583 warn (_("Invalid vna_next field of %lx\n"),
11584 aux.vna_next);
11585 j = ent.vn_cnt;
11586 break;
11587 }
1445030f
AM
11588 /* Check for overflow. */
11589 if (aux.vna_next > (size_t) (endbuf - vstart))
11590 break;
252b5132
RH
11591 isum += aux.vna_next;
11592 vstart += aux.vna_next;
11593 }
9cf03b7e 11594
54806181 11595 if (j < ent.vn_cnt)
f9a6a8f0 11596 warn (_("Missing Version Needs auxiliary information\n"));
252b5132 11597
1445030f
AM
11598 if (ent.vn_next < sizeof (*entry)
11599 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
c24cf8b6 11600 {
452bf675 11601 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
c24cf8b6
NC
11602 cnt = section->sh_info;
11603 break;
11604 }
1445030f
AM
11605 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
11606 break;
252b5132
RH
11607 idx += ent.vn_next;
11608 }
9cf03b7e 11609
54806181 11610 if (cnt < section->sh_info)
9cf03b7e 11611 warn (_("Missing Version Needs information\n"));
103f02d3 11612
252b5132
RH
11613 free (eneed);
11614 }
11615 break;
11616
11617 case SHT_GNU_versym:
11618 {
2cf0635d 11619 Elf_Internal_Shdr * link_section;
8b73c356
NC
11620 size_t total;
11621 unsigned int cnt;
2cf0635d
NC
11622 unsigned char * edata;
11623 unsigned short * data;
11624 char * strtab;
11625 Elf_Internal_Sym * symbols;
11626 Elf_Internal_Shdr * string_sec;
ba5cdace 11627 unsigned long num_syms;
d3ba0551 11628 long off;
252b5132 11629
dda8d76d 11630 if (section->sh_link >= filedata->file_header.e_shnum)
c256ffe7
JJ
11631 break;
11632
dda8d76d 11633 link_section = filedata->section_headers + section->sh_link;
08d8fa11 11634 total = section->sh_size / sizeof (Elf_External_Versym);
252b5132 11635
dda8d76d 11636 if (link_section->sh_link >= filedata->file_header.e_shnum)
c256ffe7
JJ
11637 break;
11638
015dc7e1 11639 found = true;
252b5132 11640
dda8d76d 11641 symbols = GET_ELF_SYMBOLS (filedata, link_section, & num_syms);
dd24e3da
NC
11642 if (symbols == NULL)
11643 break;
252b5132 11644
dda8d76d 11645 string_sec = filedata->section_headers + link_section->sh_link;
252b5132 11646
dda8d76d 11647 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
3f5e193b
NC
11648 string_sec->sh_size,
11649 _("version string table"));
a6e9f9df 11650 if (!strtab)
0429c154
MS
11651 {
11652 free (symbols);
11653 break;
11654 }
252b5132 11655
ca0e11aa
NC
11656 if (filedata->is_separate)
11657 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %lu entry:\n",
11658 "\nIn linked file '%s' the version symbols section '%s' contains %lu entries:\n",
11659 total),
11660 filedata->file_name,
11661 printable_section_name (filedata, section),
11662 (unsigned long) total);
11663 else
11664 printf (ngettext ("\nVersion symbols section '%s' "
11665 "contains %lu entry:\n",
11666 "\nVersion symbols section '%s' "
11667 "contains %lu entries:\n",
11668 total),
11669 printable_section_name (filedata, section),
11670 (unsigned long) total);
252b5132 11671
ae9ac79e 11672 printf (_(" Addr: 0x"));
252b5132 11673 printf_vma (section->sh_addr);
72de5009 11674 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
1b228002 11675 (unsigned long) section->sh_offset, section->sh_link,
dda8d76d 11676 printable_section_name (filedata, link_section));
252b5132 11677
dda8d76d 11678 off = offset_from_vma (filedata,
978c4450 11679 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
d3ba0551 11680 total * sizeof (short));
95099889
AM
11681 edata = (unsigned char *) get_data (NULL, filedata, off,
11682 sizeof (short), total,
11683 _("version symbol data"));
a6e9f9df
AM
11684 if (!edata)
11685 {
11686 free (strtab);
0429c154 11687 free (symbols);
a6e9f9df
AM
11688 break;
11689 }
252b5132 11690
3f5e193b 11691 data = (short unsigned int *) cmalloc (total, sizeof (short));
252b5132
RH
11692
11693 for (cnt = total; cnt --;)
b34976b6
AM
11694 data[cnt] = byte_get (edata + cnt * sizeof (short),
11695 sizeof (short));
252b5132
RH
11696
11697 free (edata);
11698
11699 for (cnt = 0; cnt < total; cnt += 4)
11700 {
11701 int j, nn;
ab273396
AM
11702 char *name;
11703 char *invalid = _("*invalid*");
252b5132
RH
11704
11705 printf (" %03x:", cnt);
11706
11707 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
b34976b6 11708 switch (data[cnt + j])
252b5132
RH
11709 {
11710 case 0:
11711 fputs (_(" 0 (*local*) "), stdout);
11712 break;
11713
11714 case 1:
11715 fputs (_(" 1 (*global*) "), stdout);
11716 break;
11717
11718 default:
c244d050
NC
11719 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
11720 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
252b5132 11721
dd24e3da 11722 /* If this index value is greater than the size of the symbols
ba5cdace
NC
11723 array, break to avoid an out-of-bounds read. */
11724 if ((unsigned long)(cnt + j) >= num_syms)
dd24e3da
NC
11725 {
11726 warn (_("invalid index into symbol array\n"));
11727 break;
11728 }
11729
ab273396 11730 name = NULL;
978c4450 11731 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
252b5132 11732 {
b34976b6
AM
11733 Elf_Internal_Verneed ivn;
11734 unsigned long offset;
252b5132 11735
d93f0186 11736 offset = offset_from_vma
978c4450
AM
11737 (filedata,
11738 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
d93f0186 11739 sizeof (Elf_External_Verneed));
252b5132 11740
b34976b6 11741 do
252b5132 11742 {
b34976b6
AM
11743 Elf_Internal_Vernaux ivna;
11744 Elf_External_Verneed evn;
11745 Elf_External_Vernaux evna;
11746 unsigned long a_off;
252b5132 11747
dda8d76d 11748 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
59245841
NC
11749 _("version need")) == NULL)
11750 break;
0b4362b0 11751
252b5132
RH
11752 ivn.vn_aux = BYTE_GET (evn.vn_aux);
11753 ivn.vn_next = BYTE_GET (evn.vn_next);
11754
11755 a_off = offset + ivn.vn_aux;
11756
11757 do
11758 {
dda8d76d 11759 if (get_data (&evna, filedata, a_off, sizeof (evna),
59245841
NC
11760 1, _("version need aux (2)")) == NULL)
11761 {
11762 ivna.vna_next = 0;
11763 ivna.vna_other = 0;
11764 }
11765 else
11766 {
11767 ivna.vna_next = BYTE_GET (evna.vna_next);
11768 ivna.vna_other = BYTE_GET (evna.vna_other);
11769 }
252b5132
RH
11770
11771 a_off += ivna.vna_next;
11772 }
b34976b6 11773 while (ivna.vna_other != data[cnt + j]
252b5132
RH
11774 && ivna.vna_next != 0);
11775
b34976b6 11776 if (ivna.vna_other == data[cnt + j])
252b5132
RH
11777 {
11778 ivna.vna_name = BYTE_GET (evna.vna_name);
11779
54806181 11780 if (ivna.vna_name >= string_sec->sh_size)
ab273396 11781 name = invalid;
54806181
AM
11782 else
11783 name = strtab + ivna.vna_name;
252b5132
RH
11784 break;
11785 }
11786
11787 offset += ivn.vn_next;
11788 }
11789 while (ivn.vn_next);
11790 }
00d93f34 11791
ab273396 11792 if (data[cnt + j] != 0x8001
978c4450 11793 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
252b5132 11794 {
b34976b6
AM
11795 Elf_Internal_Verdef ivd;
11796 Elf_External_Verdef evd;
11797 unsigned long offset;
252b5132 11798
d93f0186 11799 offset = offset_from_vma
978c4450
AM
11800 (filedata,
11801 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
d93f0186 11802 sizeof evd);
252b5132
RH
11803
11804 do
11805 {
dda8d76d 11806 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
59245841
NC
11807 _("version def")) == NULL)
11808 {
11809 ivd.vd_next = 0;
948f632f 11810 /* PR 17531: file: 046-1082287-0.004. */
3102e897
NC
11811 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
11812 break;
59245841
NC
11813 }
11814 else
11815 {
11816 ivd.vd_next = BYTE_GET (evd.vd_next);
11817 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
11818 }
252b5132
RH
11819
11820 offset += ivd.vd_next;
11821 }
c244d050 11822 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
252b5132
RH
11823 && ivd.vd_next != 0);
11824
c244d050 11825 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
252b5132 11826 {
b34976b6
AM
11827 Elf_External_Verdaux evda;
11828 Elf_Internal_Verdaux ivda;
252b5132
RH
11829
11830 ivd.vd_aux = BYTE_GET (evd.vd_aux);
11831
dda8d76d 11832 if (get_data (&evda, filedata,
59245841
NC
11833 offset - ivd.vd_next + ivd.vd_aux,
11834 sizeof (evda), 1,
11835 _("version def aux")) == NULL)
11836 break;
252b5132
RH
11837
11838 ivda.vda_name = BYTE_GET (evda.vda_name);
11839
54806181 11840 if (ivda.vda_name >= string_sec->sh_size)
ab273396
AM
11841 name = invalid;
11842 else if (name != NULL && name != invalid)
11843 name = _("*both*");
54806181
AM
11844 else
11845 name = strtab + ivda.vda_name;
252b5132
RH
11846 }
11847 }
ab273396
AM
11848 if (name != NULL)
11849 nn += printf ("(%s%-*s",
11850 name,
11851 12 - (int) strlen (name),
11852 ")");
252b5132
RH
11853
11854 if (nn < 18)
11855 printf ("%*c", 18 - nn, ' ');
11856 }
11857
11858 putchar ('\n');
11859 }
11860
11861 free (data);
11862 free (strtab);
11863 free (symbols);
11864 }
11865 break;
103f02d3 11866
252b5132
RH
11867 default:
11868 break;
11869 }
11870 }
11871
11872 if (! found)
ca0e11aa
NC
11873 {
11874 if (filedata->is_separate)
11875 printf (_("\nNo version information found in linked file '%s'.\n"),
11876 filedata->file_name);
11877 else
11878 printf (_("\nNo version information found in this file.\n"));
11879 }
252b5132 11880
015dc7e1 11881 return true;
252b5132
RH
11882}
11883
d1133906 11884static const char *
dda8d76d 11885get_symbol_binding (Filedata * filedata, unsigned int binding)
252b5132 11886{
89246a0e 11887 static char buff[64];
252b5132
RH
11888
11889 switch (binding)
11890 {
b34976b6
AM
11891 case STB_LOCAL: return "LOCAL";
11892 case STB_GLOBAL: return "GLOBAL";
11893 case STB_WEAK: return "WEAK";
252b5132
RH
11894 default:
11895 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
e9e44622
JJ
11896 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
11897 binding);
252b5132 11898 else if (binding >= STB_LOOS && binding <= STB_HIOS)
3e7a7d11
NC
11899 {
11900 if (binding == STB_GNU_UNIQUE
df3a023b 11901 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
3e7a7d11
NC
11902 return "UNIQUE";
11903 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
11904 }
252b5132 11905 else
e9e44622 11906 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
252b5132
RH
11907 return buff;
11908 }
11909}
11910
d1133906 11911static const char *
dda8d76d 11912get_symbol_type (Filedata * filedata, unsigned int type)
252b5132 11913{
89246a0e 11914 static char buff[64];
252b5132
RH
11915
11916 switch (type)
11917 {
b34976b6
AM
11918 case STT_NOTYPE: return "NOTYPE";
11919 case STT_OBJECT: return "OBJECT";
11920 case STT_FUNC: return "FUNC";
11921 case STT_SECTION: return "SECTION";
11922 case STT_FILE: return "FILE";
11923 case STT_COMMON: return "COMMON";
11924 case STT_TLS: return "TLS";
15ab5209
DB
11925 case STT_RELC: return "RELC";
11926 case STT_SRELC: return "SRELC";
252b5132
RH
11927 default:
11928 if (type >= STT_LOPROC && type <= STT_HIPROC)
df75f1af 11929 {
dda8d76d 11930 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
3510a7b8 11931 return "THUMB_FUNC";
103f02d3 11932
dda8d76d 11933 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
103f02d3
UD
11934 return "REGISTER";
11935
dda8d76d 11936 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
103f02d3
UD
11937 return "PARISC_MILLI";
11938
e9e44622 11939 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
df75f1af 11940 }
252b5132 11941 else if (type >= STT_LOOS && type <= STT_HIOS)
103f02d3 11942 {
dda8d76d 11943 if (filedata->file_header.e_machine == EM_PARISC)
103f02d3
UD
11944 {
11945 if (type == STT_HP_OPAQUE)
11946 return "HP_OPAQUE";
11947 if (type == STT_HP_STUB)
11948 return "HP_STUB";
11949 }
11950
d8045f23 11951 if (type == STT_GNU_IFUNC
dda8d76d 11952 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
df3a023b 11953 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
d8045f23
NC
11954 return "IFUNC";
11955
e9e44622 11956 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
103f02d3 11957 }
252b5132 11958 else
e9e44622 11959 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
252b5132
RH
11960 return buff;
11961 }
11962}
11963
d1133906 11964static const char *
d3ba0551 11965get_symbol_visibility (unsigned int visibility)
d1133906
NC
11966{
11967 switch (visibility)
11968 {
b34976b6
AM
11969 case STV_DEFAULT: return "DEFAULT";
11970 case STV_INTERNAL: return "INTERNAL";
11971 case STV_HIDDEN: return "HIDDEN";
d1133906 11972 case STV_PROTECTED: return "PROTECTED";
bee0ee85 11973 default:
27a45f42 11974 error (_("Unrecognized visibility value: %u\n"), visibility);
bee0ee85 11975 return _("<unknown>");
d1133906
NC
11976 }
11977}
11978
2057d69d
CZ
11979static const char *
11980get_alpha_symbol_other (unsigned int other)
9abca702 11981{
2057d69d
CZ
11982 switch (other)
11983 {
11984 case STO_ALPHA_NOPV: return "NOPV";
11985 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
11986 default:
27a45f42 11987 error (_("Unrecognized alpha specific other value: %u\n"), other);
2057d69d 11988 return _("<unknown>");
9abca702 11989 }
2057d69d
CZ
11990}
11991
fd85a6a1
NC
11992static const char *
11993get_solaris_symbol_visibility (unsigned int visibility)
11994{
11995 switch (visibility)
11996 {
11997 case 4: return "EXPORTED";
11998 case 5: return "SINGLETON";
11999 case 6: return "ELIMINATE";
12000 default: return get_symbol_visibility (visibility);
12001 }
12002}
12003
2301ed1c
SN
12004static const char *
12005get_aarch64_symbol_other (unsigned int other)
12006{
12007 static char buf[32];
12008
12009 if (other & STO_AARCH64_VARIANT_PCS)
12010 {
12011 other &= ~STO_AARCH64_VARIANT_PCS;
12012 if (other == 0)
12013 return "VARIANT_PCS";
12014 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
12015 return buf;
12016 }
12017 return NULL;
12018}
12019
5e2b0d47
NC
12020static const char *
12021get_mips_symbol_other (unsigned int other)
12022{
12023 switch (other)
12024 {
32ec8896
NC
12025 case STO_OPTIONAL: return "OPTIONAL";
12026 case STO_MIPS_PLT: return "MIPS PLT";
12027 case STO_MIPS_PIC: return "MIPS PIC";
12028 case STO_MICROMIPS: return "MICROMIPS";
12029 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
12030 case STO_MIPS16: return "MIPS16";
12031 default: return NULL;
5e2b0d47
NC
12032 }
12033}
12034
28f997cf 12035static const char *
dda8d76d 12036get_ia64_symbol_other (Filedata * filedata, unsigned int other)
28f997cf 12037{
dda8d76d 12038 if (is_ia64_vms (filedata))
28f997cf
TG
12039 {
12040 static char res[32];
12041
12042 res[0] = 0;
12043
12044 /* Function types is for images and .STB files only. */
dda8d76d 12045 switch (filedata->file_header.e_type)
28f997cf
TG
12046 {
12047 case ET_DYN:
12048 case ET_EXEC:
12049 switch (VMS_ST_FUNC_TYPE (other))
12050 {
12051 case VMS_SFT_CODE_ADDR:
12052 strcat (res, " CA");
12053 break;
12054 case VMS_SFT_SYMV_IDX:
12055 strcat (res, " VEC");
12056 break;
12057 case VMS_SFT_FD:
12058 strcat (res, " FD");
12059 break;
12060 case VMS_SFT_RESERVE:
12061 strcat (res, " RSV");
12062 break;
12063 default:
bee0ee85
NC
12064 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
12065 VMS_ST_FUNC_TYPE (other));
12066 strcat (res, " <unknown>");
12067 break;
28f997cf
TG
12068 }
12069 break;
12070 default:
12071 break;
12072 }
12073 switch (VMS_ST_LINKAGE (other))
12074 {
12075 case VMS_STL_IGNORE:
12076 strcat (res, " IGN");
12077 break;
12078 case VMS_STL_RESERVE:
12079 strcat (res, " RSV");
12080 break;
12081 case VMS_STL_STD:
12082 strcat (res, " STD");
12083 break;
12084 case VMS_STL_LNK:
12085 strcat (res, " LNK");
12086 break;
12087 default:
bee0ee85
NC
12088 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
12089 VMS_ST_LINKAGE (other));
12090 strcat (res, " <unknown>");
12091 break;
28f997cf
TG
12092 }
12093
12094 if (res[0] != 0)
12095 return res + 1;
12096 else
12097 return res;
12098 }
12099 return NULL;
12100}
12101
6911b7dc
AM
12102static const char *
12103get_ppc64_symbol_other (unsigned int other)
12104{
14732552
AM
12105 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
12106 return NULL;
12107
12108 other >>= STO_PPC64_LOCAL_BIT;
12109 if (other <= 6)
6911b7dc 12110 {
89246a0e 12111 static char buf[64];
14732552
AM
12112 if (other >= 2)
12113 other = ppc64_decode_local_entry (other);
12114 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
6911b7dc
AM
12115 return buf;
12116 }
12117 return NULL;
12118}
12119
5e2b0d47 12120static const char *
dda8d76d 12121get_symbol_other (Filedata * filedata, unsigned int other)
5e2b0d47
NC
12122{
12123 const char * result = NULL;
89246a0e 12124 static char buff [64];
5e2b0d47
NC
12125
12126 if (other == 0)
12127 return "";
12128
dda8d76d 12129 switch (filedata->file_header.e_machine)
5e2b0d47 12130 {
2057d69d
CZ
12131 case EM_ALPHA:
12132 result = get_alpha_symbol_other (other);
12133 break;
2301ed1c
SN
12134 case EM_AARCH64:
12135 result = get_aarch64_symbol_other (other);
12136 break;
5e2b0d47
NC
12137 case EM_MIPS:
12138 result = get_mips_symbol_other (other);
28f997cf
TG
12139 break;
12140 case EM_IA_64:
dda8d76d 12141 result = get_ia64_symbol_other (filedata, other);
28f997cf 12142 break;
6911b7dc
AM
12143 case EM_PPC64:
12144 result = get_ppc64_symbol_other (other);
12145 break;
5e2b0d47 12146 default:
fd85a6a1 12147 result = NULL;
5e2b0d47
NC
12148 break;
12149 }
12150
12151 if (result)
12152 return result;
12153
12154 snprintf (buff, sizeof buff, _("<other>: %x"), other);
12155 return buff;
12156}
12157
d1133906 12158static const char *
dda8d76d 12159get_symbol_index_type (Filedata * filedata, unsigned int type)
252b5132 12160{
b34976b6 12161 static char buff[32];
5cf1065c 12162
252b5132
RH
12163 switch (type)
12164 {
b34976b6
AM
12165 case SHN_UNDEF: return "UND";
12166 case SHN_ABS: return "ABS";
12167 case SHN_COMMON: return "COM";
252b5132 12168 default:
9ce701e2 12169 if (type == SHN_IA_64_ANSI_COMMON
10ca4b04
L
12170 && filedata->file_header.e_machine == EM_IA_64
12171 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
12172 return "ANSI_COM";
12173 else if ((filedata->file_header.e_machine == EM_X86_64
12174 || filedata->file_header.e_machine == EM_L1OM
12175 || filedata->file_header.e_machine == EM_K1OM)
12176 && type == SHN_X86_64_LCOMMON)
12177 return "LARGE_COM";
12178 else if ((type == SHN_MIPS_SCOMMON
12179 && filedata->file_header.e_machine == EM_MIPS)
12180 || (type == SHN_TIC6X_SCOMMON
12181 && filedata->file_header.e_machine == EM_TI_C6000))
12182 return "SCOM";
12183 else if (type == SHN_MIPS_SUNDEFINED
12184 && filedata->file_header.e_machine == EM_MIPS)
12185 return "SUND";
12186 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
12187 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
12188 else if (type >= SHN_LOOS && type <= SHN_HIOS)
12189 sprintf (buff, "OS [0x%04x]", type & 0xffff);
12190 else if (type >= SHN_LORESERVE)
12191 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
12192 else if (filedata->file_header.e_shnum != 0
12193 && type >= filedata->file_header.e_shnum)
12194 sprintf (buff, _("bad section index[%3d]"), type);
12195 else
12196 sprintf (buff, "%3d", type);
12197 break;
fd85a6a1
NC
12198 }
12199
10ca4b04 12200 return buff;
6bd1a22c
L
12201}
12202
bb4d2ac2 12203static const char *
dda8d76d 12204get_symbol_version_string (Filedata * filedata,
015dc7e1 12205 bool is_dynsym,
1449284b
NC
12206 const char * strtab,
12207 unsigned long int strtab_size,
12208 unsigned int si,
12209 Elf_Internal_Sym * psym,
12210 enum versioned_symbol_info * sym_info,
12211 unsigned short * vna_other)
bb4d2ac2 12212{
ab273396
AM
12213 unsigned char data[2];
12214 unsigned short vers_data;
12215 unsigned long offset;
7a815dd5 12216 unsigned short max_vd_ndx;
bb4d2ac2 12217
ab273396 12218 if (!is_dynsym
978c4450 12219 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
ab273396 12220 return NULL;
bb4d2ac2 12221
978c4450
AM
12222 offset = offset_from_vma (filedata,
12223 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
ab273396 12224 sizeof data + si * sizeof (vers_data));
bb4d2ac2 12225
dda8d76d 12226 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
ab273396
AM
12227 sizeof (data), 1, _("version data")) == NULL)
12228 return NULL;
12229
12230 vers_data = byte_get (data, 2);
bb4d2ac2 12231
1f6f5dba 12232 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
ab273396 12233 return NULL;
bb4d2ac2 12234
0b8b7609 12235 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
7a815dd5
L
12236 max_vd_ndx = 0;
12237
ab273396
AM
12238 /* Usually we'd only see verdef for defined symbols, and verneed for
12239 undefined symbols. However, symbols defined by the linker in
12240 .dynbss for variables copied from a shared library in order to
12241 avoid text relocations are defined yet have verneed. We could
12242 use a heuristic to detect the special case, for example, check
12243 for verneed first on symbols defined in SHT_NOBITS sections, but
12244 it is simpler and more reliable to just look for both verdef and
12245 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
bb4d2ac2 12246
ab273396
AM
12247 if (psym->st_shndx != SHN_UNDEF
12248 && vers_data != 0x8001
978c4450 12249 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
ab273396
AM
12250 {
12251 Elf_Internal_Verdef ivd;
12252 Elf_Internal_Verdaux ivda;
12253 Elf_External_Verdaux evda;
12254 unsigned long off;
bb4d2ac2 12255
dda8d76d 12256 off = offset_from_vma (filedata,
978c4450 12257 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
ab273396
AM
12258 sizeof (Elf_External_Verdef));
12259
12260 do
bb4d2ac2 12261 {
ab273396
AM
12262 Elf_External_Verdef evd;
12263
dda8d76d 12264 if (get_data (&evd, filedata, off, sizeof (evd), 1,
ab273396
AM
12265 _("version def")) == NULL)
12266 {
12267 ivd.vd_ndx = 0;
12268 ivd.vd_aux = 0;
12269 ivd.vd_next = 0;
1f6f5dba 12270 ivd.vd_flags = 0;
ab273396
AM
12271 }
12272 else
bb4d2ac2 12273 {
ab273396
AM
12274 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12275 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12276 ivd.vd_next = BYTE_GET (evd.vd_next);
1f6f5dba 12277 ivd.vd_flags = BYTE_GET (evd.vd_flags);
ab273396 12278 }
bb4d2ac2 12279
7a815dd5
L
12280 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
12281 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
12282
ab273396
AM
12283 off += ivd.vd_next;
12284 }
12285 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
bb4d2ac2 12286
ab273396
AM
12287 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
12288 {
9abca702 12289 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
1f6f5dba
L
12290 return NULL;
12291
ab273396
AM
12292 off -= ivd.vd_next;
12293 off += ivd.vd_aux;
bb4d2ac2 12294
dda8d76d 12295 if (get_data (&evda, filedata, off, sizeof (evda), 1,
ab273396
AM
12296 _("version def aux")) != NULL)
12297 {
12298 ivda.vda_name = BYTE_GET (evda.vda_name);
bb4d2ac2 12299
ab273396 12300 if (psym->st_name != ivda.vda_name)
0b8b7609
AM
12301 return (ivda.vda_name < strtab_size
12302 ? strtab + ivda.vda_name : _("<corrupt>"));
ab273396
AM
12303 }
12304 }
12305 }
bb4d2ac2 12306
978c4450 12307 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
ab273396
AM
12308 {
12309 Elf_External_Verneed evn;
12310 Elf_Internal_Verneed ivn;
12311 Elf_Internal_Vernaux ivna;
bb4d2ac2 12312
dda8d76d 12313 offset = offset_from_vma (filedata,
978c4450 12314 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
ab273396
AM
12315 sizeof evn);
12316 do
12317 {
12318 unsigned long vna_off;
bb4d2ac2 12319
dda8d76d 12320 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
ab273396
AM
12321 _("version need")) == NULL)
12322 {
12323 ivna.vna_next = 0;
12324 ivna.vna_other = 0;
12325 ivna.vna_name = 0;
12326 break;
12327 }
bb4d2ac2 12328
ab273396
AM
12329 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12330 ivn.vn_next = BYTE_GET (evn.vn_next);
bb4d2ac2 12331
ab273396 12332 vna_off = offset + ivn.vn_aux;
bb4d2ac2 12333
ab273396
AM
12334 do
12335 {
12336 Elf_External_Vernaux evna;
bb4d2ac2 12337
dda8d76d 12338 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
ab273396 12339 _("version need aux (3)")) == NULL)
bb4d2ac2 12340 {
ab273396
AM
12341 ivna.vna_next = 0;
12342 ivna.vna_other = 0;
12343 ivna.vna_name = 0;
bb4d2ac2 12344 }
bb4d2ac2 12345 else
bb4d2ac2 12346 {
ab273396
AM
12347 ivna.vna_other = BYTE_GET (evna.vna_other);
12348 ivna.vna_next = BYTE_GET (evna.vna_next);
12349 ivna.vna_name = BYTE_GET (evna.vna_name);
12350 }
bb4d2ac2 12351
ab273396
AM
12352 vna_off += ivna.vna_next;
12353 }
12354 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
bb4d2ac2 12355
ab273396
AM
12356 if (ivna.vna_other == vers_data)
12357 break;
bb4d2ac2 12358
ab273396
AM
12359 offset += ivn.vn_next;
12360 }
12361 while (ivn.vn_next != 0);
bb4d2ac2 12362
ab273396
AM
12363 if (ivna.vna_other == vers_data)
12364 {
12365 *sym_info = symbol_undefined;
12366 *vna_other = ivna.vna_other;
12367 return (ivna.vna_name < strtab_size
12368 ? strtab + ivna.vna_name : _("<corrupt>"));
bb4d2ac2 12369 }
7a815dd5
L
12370 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
12371 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
12372 return _("<corrupt>");
bb4d2ac2 12373 }
ab273396 12374 return NULL;
bb4d2ac2
L
12375}
12376
10ca4b04
L
12377static void
12378print_dynamic_symbol (Filedata *filedata, unsigned long si,
12379 Elf_Internal_Sym *symtab,
12380 Elf_Internal_Shdr *section,
12381 char *strtab, size_t strtab_size)
252b5132 12382{
10ca4b04
L
12383 const char *version_string;
12384 enum versioned_symbol_info sym_info;
12385 unsigned short vna_other;
12386 Elf_Internal_Sym *psym = symtab + si;
b9e920ec 12387
10ca4b04
L
12388 printf ("%6ld: ", si);
12389 print_vma (psym->st_value, LONG_HEX);
12390 putchar (' ');
12391 print_vma (psym->st_size, DEC_5);
12392 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
12393 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
12394 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
12395 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
12396 else
252b5132 12397 {
10ca4b04 12398 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
252b5132 12399
10ca4b04
L
12400 printf (" %-7s", get_symbol_visibility (vis));
12401 /* Check to see if any other bits in the st_other field are set.
12402 Note - displaying this information disrupts the layout of the
12403 table being generated, but for the moment this case is very rare. */
12404 if (psym->st_other ^ vis)
12405 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
252b5132 12406 }
10ca4b04 12407 printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
0942c7ab 12408
015dc7e1 12409 bool is_valid = VALID_SYMBOL_NAME (strtab, strtab_size, psym->st_name);
0942c7ab 12410 const char * sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
10ca4b04
L
12411
12412 version_string
12413 = get_symbol_version_string (filedata,
12414 (section == NULL
12415 || section->sh_type == SHT_DYNSYM),
12416 strtab, strtab_size, si,
12417 psym, &sym_info, &vna_other);
b9e920ec 12418
0942c7ab
NC
12419 int len_avail = 21;
12420 if (! do_wide && version_string != NULL)
12421 {
ddb43bab 12422 char buffer[16];
0942c7ab 12423
ddb43bab 12424 len_avail -= 1 + strlen (version_string);
0942c7ab
NC
12425
12426 if (sym_info == symbol_undefined)
12427 len_avail -= sprintf (buffer," (%d)", vna_other);
12428 else if (sym_info != symbol_hidden)
12429 len_avail -= 1;
12430 }
12431
12432 print_symbol (len_avail, sstr);
b9e920ec 12433
10ca4b04
L
12434 if (version_string)
12435 {
12436 if (sym_info == symbol_undefined)
12437 printf ("@%s (%d)", version_string, vna_other);
f7a99963 12438 else
10ca4b04
L
12439 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
12440 version_string);
12441 }
6bd1a22c 12442
10ca4b04 12443 putchar ('\n');
6bd1a22c 12444
10ca4b04
L
12445 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
12446 && section != NULL
12447 && si >= section->sh_info
12448 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
12449 && filedata->file_header.e_machine != EM_MIPS
12450 /* Solaris binaries have been found to violate this requirement as
12451 well. Not sure if this is a bug or an ABI requirement. */
12452 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
12453 warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
12454 si, printable_section_name (filedata, section), section->sh_info);
12455}
f16a9783 12456
0f03783c
NC
12457static const char *
12458get_lto_kind (unsigned int kind)
12459{
12460 switch (kind)
12461 {
12462 case 0: return "DEF";
12463 case 1: return "WEAKDEF";
12464 case 2: return "UNDEF";
12465 case 3: return "WEAKUNDEF";
12466 case 4: return "COMMON";
12467 default:
12468 break;
12469 }
12470
12471 static char buffer[30];
12472 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
12473 sprintf (buffer, "<unknown: %u>", kind);
12474 return buffer;
12475}
12476
12477static const char *
12478get_lto_visibility (unsigned int visibility)
12479{
12480 switch (visibility)
12481 {
12482 case 0: return "DEFAULT";
12483 case 1: return "PROTECTED";
12484 case 2: return "INTERNAL";
12485 case 3: return "HIDDEN";
12486 default:
12487 break;
12488 }
12489
12490 static char buffer[30];
12491 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
12492 sprintf (buffer, "<unknown: %u>", visibility);
12493 return buffer;
12494}
12495
12496static const char *
12497get_lto_sym_type (unsigned int sym_type)
12498{
12499 switch (sym_type)
12500 {
12501 case 0: return "UNKNOWN";
12502 case 1: return "FUNCTION";
12503 case 2: return "VARIABLE";
12504 default:
12505 break;
12506 }
12507
12508 static char buffer[30];
12509 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
12510 sprintf (buffer, "<unknown: %u>", sym_type);
12511 return buffer;
12512}
12513
12514/* Display an LTO format symbol table.
12515 FIXME: The format of LTO symbol tables is not formalized.
12516 So this code could need changing in the future. */
12517
015dc7e1 12518static bool
0f03783c
NC
12519display_lto_symtab (Filedata * filedata,
12520 Elf_Internal_Shdr * section)
12521{
12522 if (section->sh_size == 0)
12523 {
ca0e11aa
NC
12524 if (filedata->is_separate)
12525 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
12526 printable_section_name (filedata, section),
12527 filedata->file_name);
12528 else
12529 printf (_("\nLTO Symbol table '%s' is empty!\n"),
12530 printable_section_name (filedata, section));
12531
015dc7e1 12532 return true;
0f03783c
NC
12533 }
12534
12535 if (section->sh_size > filedata->file_size)
12536 {
12537 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
12538 printable_section_name (filedata, section),
12539 (unsigned long) section->sh_size);
015dc7e1 12540 return false;
0f03783c
NC
12541 }
12542
12543 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
12544 section->sh_size, 1, _("LTO symbols"));
12545 if (alloced_data == NULL)
015dc7e1 12546 return false;
0f03783c
NC
12547
12548 /* Look for extended data for the symbol table. */
12549 Elf_Internal_Shdr * ext;
12550 void * ext_data_orig = NULL;
12551 char * ext_data = NULL;
12552 char * ext_data_end = NULL;
12553 char * ext_name = NULL;
12554
12555 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
b9e920ec 12556 SECTION_NAME (section) + sizeof (".gnu.lto_.symtab.") - 1) > 0
0f03783c
NC
12557 && ext_name != NULL /* Paranoia. */
12558 && (ext = find_section (filedata, ext_name)) != NULL)
12559 {
12560 if (ext->sh_size < 3)
12561 error (_("LTO Symbol extension table '%s' is empty!\n"),
12562 printable_section_name (filedata, ext));
12563 else
12564 {
12565 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
12566 ext->sh_size, 1,
12567 _("LTO ext symbol data"));
12568 if (ext_data != NULL)
12569 {
12570 ext_data_end = ext_data + ext->sh_size;
12571 if (* ext_data++ != 1)
12572 error (_("Unexpected version number in symbol extension table\n"));
12573 }
12574 }
12575 }
b9e920ec 12576
0f03783c
NC
12577 const unsigned char * data = (const unsigned char *) alloced_data;
12578 const unsigned char * end = data + section->sh_size;
12579
ca0e11aa
NC
12580 if (filedata->is_separate)
12581 printf (_("\nIn linked file '%s': "), filedata->file_name);
12582 else
12583 printf ("\n");
12584
0f03783c
NC
12585 if (ext_data_orig != NULL)
12586 {
12587 if (do_wide)
ca0e11aa 12588 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
0f03783c
NC
12589 printable_section_name (filedata, section),
12590 printable_section_name (filedata, ext));
12591 else
12592 {
ca0e11aa 12593 printf (_("LTO Symbol table '%s'\n"),
0f03783c
NC
12594 printable_section_name (filedata, section));
12595 printf (_(" and extension table '%s' contain:\n"),
12596 printable_section_name (filedata, ext));
12597 }
12598 }
12599 else
ca0e11aa 12600 printf (_("LTO Symbol table '%s' contains:\n"),
0f03783c 12601 printable_section_name (filedata, section));
b9e920ec 12602
0f03783c 12603 /* FIXME: Add a wide version. */
b9e920ec 12604 if (ext_data_orig != NULL)
0f03783c
NC
12605 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
12606 else
12607 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
12608
12609 /* FIXME: We do not handle style prefixes. */
12610
12611 while (data < end)
12612 {
12613 const unsigned char * sym_name = data;
12614 data += strnlen ((const char *) sym_name, end - data) + 1;
12615 if (data >= end)
12616 goto fail;
12617
12618 const unsigned char * comdat_key = data;
12619 data += strnlen ((const char *) comdat_key, end - data) + 1;
12620 if (data >= end)
12621 goto fail;
12622
12623 if (data + 2 + 8 + 4 > end)
12624 goto fail;
12625
12626 unsigned int kind = *data++;
12627 unsigned int visibility = *data++;
12628
12629 elf_vma size = byte_get (data, 8);
12630 data += 8;
12631
12632 elf_vma slot = byte_get (data, 4);
12633 data += 4;
12634
12635 if (ext_data != NULL)
12636 {
12637 if (ext_data < (ext_data_end - 1))
12638 {
12639 unsigned int sym_type = * ext_data ++;
12640 unsigned int sec_kind = * ext_data ++;
12641
12642 printf (" %10s %10s %11s %08lx %08lx %9s %08lx _",
12643 * comdat_key == 0 ? "-" : (char *) comdat_key,
12644 get_lto_kind (kind),
12645 get_lto_visibility (visibility),
12646 (long) size,
12647 (long) slot,
12648 get_lto_sym_type (sym_type),
12649 (long) sec_kind);
12650 print_symbol (6, (const char *) sym_name);
12651 }
12652 else
12653 {
12654 error (_("Ran out of LTO symbol extension data\n"));
12655 ext_data = NULL;
12656 /* FIXME: return FAIL result ? */
12657 }
12658 }
12659 else
12660 {
12661 printf (" %10s %10s %11s %08lx %08lx _",
12662 * comdat_key == 0 ? "-" : (char *) comdat_key,
12663 get_lto_kind (kind),
12664 get_lto_visibility (visibility),
12665 (long) size,
12666 (long) slot);
12667 print_symbol (21, (const char *) sym_name);
12668 }
12669 putchar ('\n');
12670 }
12671
12672 if (ext_data != NULL && ext_data < ext_data_end)
12673 {
12674 error (_("Data remains in the LTO symbol extension table\n"));
12675 goto fail;
12676 }
12677
12678 free (alloced_data);
12679 free (ext_data_orig);
12680 free (ext_name);
015dc7e1 12681 return true;
b9e920ec 12682
0f03783c
NC
12683 fail:
12684 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
12685 free (alloced_data);
12686 free (ext_data_orig);
12687 free (ext_name);
015dc7e1 12688 return false;
0f03783c
NC
12689}
12690
12691/* Display LTO symbol tables. */
12692
015dc7e1 12693static bool
0f03783c
NC
12694process_lto_symbol_tables (Filedata * filedata)
12695{
12696 Elf_Internal_Shdr * section;
12697 unsigned int i;
015dc7e1 12698 bool res = true;
0f03783c
NC
12699
12700 if (!do_lto_syms)
015dc7e1 12701 return true;
0f03783c
NC
12702
12703 if (filedata->section_headers == NULL)
015dc7e1 12704 return true;
0f03783c
NC
12705
12706 for (i = 0, section = filedata->section_headers;
12707 i < filedata->file_header.e_shnum;
12708 i++, section++)
b9e920ec 12709 if (SECTION_NAME_VALID (section)
08dedd66 12710 && startswith (SECTION_NAME (section), ".gnu.lto_.symtab."))
0f03783c
NC
12711 res &= display_lto_symtab (filedata, section);
12712
b9e920ec 12713 return res;
0f03783c
NC
12714}
12715
10ca4b04 12716/* Dump the symbol table. */
0f03783c 12717
015dc7e1 12718static bool
10ca4b04
L
12719process_symbol_table (Filedata * filedata)
12720{
12721 Elf_Internal_Shdr * section;
f16a9783 12722
10ca4b04 12723 if (!do_syms && !do_dyn_syms && !do_histogram)
015dc7e1 12724 return true;
6bd1a22c 12725
978c4450 12726 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
6bd1a22c
L
12727 && do_syms
12728 && do_using_dynamic
978c4450
AM
12729 && filedata->dynamic_strings != NULL
12730 && filedata->dynamic_symbols != NULL)
6bd1a22c 12731 {
10ca4b04 12732 unsigned long si;
6bd1a22c 12733
ca0e11aa
NC
12734 if (filedata->is_separate)
12735 {
12736 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table contains %lu entry:\n",
12737 "\nIn linked file '%s' the dynamic symbol table contains %lu entries:\n",
12738 filedata->num_dynamic_syms),
12739 filedata->file_name,
12740 filedata->num_dynamic_syms);
12741 }
12742 else
12743 {
12744 printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
12745 "\nSymbol table for image contains %lu entries:\n",
12746 filedata->num_dynamic_syms),
12747 filedata->num_dynamic_syms);
12748 }
10ca4b04
L
12749 if (is_32bit_elf)
12750 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
12751 else
12752 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
6bd1a22c 12753
978c4450
AM
12754 for (si = 0; si < filedata->num_dynamic_syms; si++)
12755 print_dynamic_symbol (filedata, si, filedata->dynamic_symbols, NULL,
12756 filedata->dynamic_strings,
12757 filedata->dynamic_strings_length);
252b5132 12758 }
8b73c356 12759 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
dda8d76d 12760 && filedata->section_headers != NULL)
252b5132 12761 {
b34976b6 12762 unsigned int i;
252b5132 12763
dda8d76d
NC
12764 for (i = 0, section = filedata->section_headers;
12765 i < filedata->file_header.e_shnum;
252b5132
RH
12766 i++, section++)
12767 {
2cf0635d 12768 char * strtab = NULL;
c256ffe7 12769 unsigned long int strtab_size = 0;
2cf0635d 12770 Elf_Internal_Sym * symtab;
ef3df110 12771 unsigned long si, num_syms;
252b5132 12772
2c610e4b
L
12773 if ((section->sh_type != SHT_SYMTAB
12774 && section->sh_type != SHT_DYNSYM)
12775 || (!do_syms
12776 && section->sh_type == SHT_SYMTAB))
252b5132
RH
12777 continue;
12778
dd24e3da
NC
12779 if (section->sh_entsize == 0)
12780 {
12781 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
dda8d76d 12782 printable_section_name (filedata, section));
dd24e3da
NC
12783 continue;
12784 }
12785
d3a49aa8 12786 num_syms = section->sh_size / section->sh_entsize;
ca0e11aa
NC
12787
12788 if (filedata->is_separate)
12789 printf (ngettext ("\nIn linked file '%s' symbol section '%s' contains %lu entry:\n",
12790 "\nIn linked file '%s' symbol section '%s' contains %lu entries:\n",
12791 num_syms),
12792 filedata->file_name,
12793 printable_section_name (filedata, section),
12794 num_syms);
12795 else
12796 printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
12797 "\nSymbol table '%s' contains %lu entries:\n",
12798 num_syms),
12799 printable_section_name (filedata, section),
12800 num_syms);
dd24e3da 12801
f7a99963 12802 if (is_32bit_elf)
ca47b30c 12803 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
f7a99963 12804 else
ca47b30c 12805 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
252b5132 12806
dda8d76d 12807 symtab = GET_ELF_SYMBOLS (filedata, section, & num_syms);
252b5132
RH
12808 if (symtab == NULL)
12809 continue;
12810
dda8d76d 12811 if (section->sh_link == filedata->file_header.e_shstrndx)
c256ffe7 12812 {
dda8d76d
NC
12813 strtab = filedata->string_table;
12814 strtab_size = filedata->string_table_length;
c256ffe7 12815 }
dda8d76d 12816 else if (section->sh_link < filedata->file_header.e_shnum)
252b5132 12817 {
2cf0635d 12818 Elf_Internal_Shdr * string_sec;
252b5132 12819
dda8d76d 12820 string_sec = filedata->section_headers + section->sh_link;
252b5132 12821
dda8d76d 12822 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
3f5e193b
NC
12823 1, string_sec->sh_size,
12824 _("string table"));
c256ffe7 12825 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
252b5132
RH
12826 }
12827
10ca4b04
L
12828 for (si = 0; si < num_syms; si++)
12829 print_dynamic_symbol (filedata, si, symtab, section,
12830 strtab, strtab_size);
252b5132
RH
12831
12832 free (symtab);
dda8d76d 12833 if (strtab != filedata->string_table)
252b5132
RH
12834 free (strtab);
12835 }
12836 }
12837 else if (do_syms)
12838 printf
12839 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
12840
978c4450 12841 if (do_histogram && filedata->buckets != NULL)
252b5132 12842 {
2cf0635d
NC
12843 unsigned long * lengths;
12844 unsigned long * counts;
66543521
AM
12845 unsigned long hn;
12846 bfd_vma si;
12847 unsigned long maxlength = 0;
12848 unsigned long nzero_counts = 0;
12849 unsigned long nsyms = 0;
6bd6a03d 12850 char *visited;
252b5132 12851
d3a49aa8
AM
12852 printf (ngettext ("\nHistogram for bucket list length "
12853 "(total of %lu bucket):\n",
12854 "\nHistogram for bucket list length "
12855 "(total of %lu buckets):\n",
978c4450
AM
12856 (unsigned long) filedata->nbuckets),
12857 (unsigned long) filedata->nbuckets);
252b5132 12858
978c4450
AM
12859 lengths = (unsigned long *) calloc (filedata->nbuckets,
12860 sizeof (*lengths));
252b5132
RH
12861 if (lengths == NULL)
12862 {
8b73c356 12863 error (_("Out of memory allocating space for histogram buckets\n"));
fd486f32 12864 goto err_out;
252b5132 12865 }
978c4450
AM
12866 visited = xcmalloc (filedata->nchains, 1);
12867 memset (visited, 0, filedata->nchains);
8b73c356
NC
12868
12869 printf (_(" Length Number %% of total Coverage\n"));
978c4450 12870 for (hn = 0; hn < filedata->nbuckets; ++hn)
252b5132 12871 {
978c4450 12872 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
252b5132 12873 {
b34976b6 12874 ++nsyms;
252b5132 12875 if (maxlength < ++lengths[hn])
b34976b6 12876 ++maxlength;
978c4450 12877 if (si >= filedata->nchains || visited[si])
6bd6a03d
AM
12878 {
12879 error (_("histogram chain is corrupt\n"));
12880 break;
12881 }
12882 visited[si] = 1;
252b5132
RH
12883 }
12884 }
6bd6a03d 12885 free (visited);
252b5132 12886
3f5e193b 12887 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
252b5132
RH
12888 if (counts == NULL)
12889 {
b2e951ec 12890 free (lengths);
8b73c356 12891 error (_("Out of memory allocating space for histogram counts\n"));
fd486f32 12892 goto err_out;
252b5132
RH
12893 }
12894
978c4450 12895 for (hn = 0; hn < filedata->nbuckets; ++hn)
b34976b6 12896 ++counts[lengths[hn]];
252b5132 12897
978c4450 12898 if (filedata->nbuckets > 0)
252b5132 12899 {
66543521
AM
12900 unsigned long i;
12901 printf (" 0 %-10lu (%5.1f%%)\n",
978c4450 12902 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
66543521 12903 for (i = 1; i <= maxlength; ++i)
103f02d3 12904 {
66543521
AM
12905 nzero_counts += counts[i] * i;
12906 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
978c4450 12907 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
103f02d3
UD
12908 (nzero_counts * 100.0) / nsyms);
12909 }
252b5132
RH
12910 }
12911
12912 free (counts);
12913 free (lengths);
12914 }
12915
978c4450
AM
12916 free (filedata->buckets);
12917 filedata->buckets = NULL;
12918 filedata->nbuckets = 0;
12919 free (filedata->chains);
12920 filedata->chains = NULL;
252b5132 12921
978c4450 12922 if (do_histogram && filedata->gnubuckets != NULL)
fdc90cb4 12923 {
2cf0635d
NC
12924 unsigned long * lengths;
12925 unsigned long * counts;
fdc90cb4
JJ
12926 unsigned long hn;
12927 unsigned long maxlength = 0;
12928 unsigned long nzero_counts = 0;
12929 unsigned long nsyms = 0;
fdc90cb4 12930
f16a9783 12931 printf (ngettext ("\nHistogram for `%s' bucket list length "
d3a49aa8 12932 "(total of %lu bucket):\n",
f16a9783 12933 "\nHistogram for `%s' bucket list length "
d3a49aa8 12934 "(total of %lu buckets):\n",
978c4450
AM
12935 (unsigned long) filedata->ngnubuckets),
12936 GNU_HASH_SECTION_NAME (filedata),
12937 (unsigned long) filedata->ngnubuckets);
8b73c356 12938
978c4450
AM
12939 lengths = (unsigned long *) calloc (filedata->ngnubuckets,
12940 sizeof (*lengths));
fdc90cb4
JJ
12941 if (lengths == NULL)
12942 {
8b73c356 12943 error (_("Out of memory allocating space for gnu histogram buckets\n"));
fd486f32 12944 goto err_out;
fdc90cb4
JJ
12945 }
12946
fdc90cb4
JJ
12947 printf (_(" Length Number %% of total Coverage\n"));
12948
978c4450
AM
12949 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
12950 if (filedata->gnubuckets[hn] != 0)
fdc90cb4
JJ
12951 {
12952 bfd_vma off, length = 1;
12953
978c4450 12954 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
071436c6 12955 /* PR 17531 file: 010-77222-0.004. */
978c4450
AM
12956 off < filedata->ngnuchains
12957 && (filedata->gnuchains[off] & 1) == 0;
071436c6 12958 ++off)
fdc90cb4
JJ
12959 ++length;
12960 lengths[hn] = length;
12961 if (length > maxlength)
12962 maxlength = length;
12963 nsyms += length;
12964 }
12965
3f5e193b 12966 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
fdc90cb4
JJ
12967 if (counts == NULL)
12968 {
b2e951ec 12969 free (lengths);
8b73c356 12970 error (_("Out of memory allocating space for gnu histogram counts\n"));
fd486f32 12971 goto err_out;
fdc90cb4
JJ
12972 }
12973
978c4450 12974 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
fdc90cb4
JJ
12975 ++counts[lengths[hn]];
12976
978c4450 12977 if (filedata->ngnubuckets > 0)
fdc90cb4
JJ
12978 {
12979 unsigned long j;
12980 printf (" 0 %-10lu (%5.1f%%)\n",
978c4450 12981 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
fdc90cb4
JJ
12982 for (j = 1; j <= maxlength; ++j)
12983 {
12984 nzero_counts += counts[j] * j;
12985 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
978c4450 12986 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
fdc90cb4
JJ
12987 (nzero_counts * 100.0) / nsyms);
12988 }
12989 }
12990
12991 free (counts);
12992 free (lengths);
fdc90cb4 12993 }
978c4450
AM
12994 free (filedata->gnubuckets);
12995 filedata->gnubuckets = NULL;
12996 filedata->ngnubuckets = 0;
12997 free (filedata->gnuchains);
12998 filedata->gnuchains = NULL;
12999 filedata->ngnuchains = 0;
13000 free (filedata->mipsxlat);
13001 filedata->mipsxlat = NULL;
015dc7e1 13002 return true;
fd486f32
AM
13003
13004 err_out:
978c4450
AM
13005 free (filedata->gnubuckets);
13006 filedata->gnubuckets = NULL;
13007 filedata->ngnubuckets = 0;
13008 free (filedata->gnuchains);
13009 filedata->gnuchains = NULL;
13010 filedata->ngnuchains = 0;
13011 free (filedata->mipsxlat);
13012 filedata->mipsxlat = NULL;
13013 free (filedata->buckets);
13014 filedata->buckets = NULL;
13015 filedata->nbuckets = 0;
13016 free (filedata->chains);
13017 filedata->chains = NULL;
015dc7e1 13018 return false;
252b5132
RH
13019}
13020
015dc7e1 13021static bool
ca0e11aa 13022process_syminfo (Filedata * filedata)
252b5132 13023{
b4c96d0d 13024 unsigned int i;
252b5132 13025
978c4450 13026 if (filedata->dynamic_syminfo == NULL
252b5132
RH
13027 || !do_dynamic)
13028 /* No syminfo, this is ok. */
015dc7e1 13029 return true;
252b5132
RH
13030
13031 /* There better should be a dynamic symbol section. */
978c4450 13032 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
015dc7e1 13033 return false;
252b5132 13034
ca0e11aa
NC
13035 if (filedata->is_separate)
13036 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entry:\n",
13037 "\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entries:\n",
13038 filedata->dynamic_syminfo_nent),
13039 filedata->file_name,
13040 filedata->dynamic_syminfo_offset,
13041 filedata->dynamic_syminfo_nent);
13042 else
d3a49aa8
AM
13043 printf (ngettext ("\nDynamic info segment at offset 0x%lx "
13044 "contains %d entry:\n",
13045 "\nDynamic info segment at offset 0x%lx "
13046 "contains %d entries:\n",
978c4450 13047 filedata->dynamic_syminfo_nent),
ca0e11aa
NC
13048 filedata->dynamic_syminfo_offset,
13049 filedata->dynamic_syminfo_nent);
252b5132
RH
13050
13051 printf (_(" Num: Name BoundTo Flags\n"));
978c4450 13052 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
252b5132 13053 {
978c4450 13054 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
252b5132 13055
31104126 13056 printf ("%4d: ", i);
978c4450 13057 if (i >= filedata->num_dynamic_syms)
4082ef84 13058 printf (_("<corrupt index>"));
978c4450
AM
13059 else if (VALID_DYNAMIC_NAME (filedata, filedata->dynamic_symbols[i].st_name))
13060 print_symbol (30, GET_DYNAMIC_NAME (filedata,
13061 filedata->dynamic_symbols[i].st_name));
d79b3d50 13062 else
978c4450 13063 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
31104126 13064 putchar (' ');
252b5132 13065
978c4450 13066 switch (filedata->dynamic_syminfo[i].si_boundto)
252b5132
RH
13067 {
13068 case SYMINFO_BT_SELF:
13069 fputs ("SELF ", stdout);
13070 break;
13071 case SYMINFO_BT_PARENT:
13072 fputs ("PARENT ", stdout);
13073 break;
13074 default:
978c4450
AM
13075 if (filedata->dynamic_syminfo[i].si_boundto > 0
13076 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
13077 && VALID_DYNAMIC_NAME (filedata,
13078 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
31104126 13079 {
978c4450
AM
13080 print_symbol (10, GET_DYNAMIC_NAME (filedata,
13081 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
31104126
NC
13082 putchar (' ' );
13083 }
252b5132 13084 else
978c4450 13085 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
252b5132
RH
13086 break;
13087 }
13088
13089 if (flags & SYMINFO_FLG_DIRECT)
13090 printf (" DIRECT");
13091 if (flags & SYMINFO_FLG_PASSTHRU)
13092 printf (" PASSTHRU");
13093 if (flags & SYMINFO_FLG_COPY)
13094 printf (" COPY");
13095 if (flags & SYMINFO_FLG_LAZYLOAD)
13096 printf (" LAZYLOAD");
13097
13098 puts ("");
13099 }
13100
015dc7e1 13101 return true;
252b5132
RH
13102}
13103
75802ccb
CE
13104/* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
13105 is contained by the region START .. END. The types of ADDR, START
13106 and END should all be the same. Note both ADDR + NELEM and END
13107 point to just beyond the end of the regions that are being tested. */
13108#define IN_RANGE(START,END,ADDR,NELEM) \
13109 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
b32e566b 13110
cf13d699
NC
13111/* Check to see if the given reloc needs to be handled in a target specific
13112 manner. If so then process the reloc and return TRUE otherwise return
f84ce13b
NC
13113 FALSE.
13114
13115 If called with reloc == NULL, then this is a signal that reloc processing
13116 for the current section has finished, and any saved state should be
13117 discarded. */
09c11c86 13118
015dc7e1 13119static bool
dda8d76d
NC
13120target_specific_reloc_handling (Filedata * filedata,
13121 Elf_Internal_Rela * reloc,
13122 unsigned char * start,
13123 unsigned char * end,
13124 Elf_Internal_Sym * symtab,
13125 unsigned long num_syms)
252b5132 13126{
f84ce13b
NC
13127 unsigned int reloc_type = 0;
13128 unsigned long sym_index = 0;
13129
13130 if (reloc)
13131 {
dda8d76d 13132 reloc_type = get_reloc_type (filedata, reloc->r_info);
f84ce13b
NC
13133 sym_index = get_reloc_symindex (reloc->r_info);
13134 }
252b5132 13135
dda8d76d 13136 switch (filedata->file_header.e_machine)
252b5132 13137 {
13761a11
NC
13138 case EM_MSP430:
13139 case EM_MSP430_OLD:
13140 {
13141 static Elf_Internal_Sym * saved_sym = NULL;
13142
f84ce13b
NC
13143 if (reloc == NULL)
13144 {
13145 saved_sym = NULL;
015dc7e1 13146 return true;
f84ce13b
NC
13147 }
13148
13761a11
NC
13149 switch (reloc_type)
13150 {
13151 case 10: /* R_MSP430_SYM_DIFF */
7d81bc93 13152 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
dda8d76d 13153 if (uses_msp430x_relocs (filedata))
13761a11 13154 break;
1a0670f3 13155 /* Fall through. */
13761a11 13156 case 21: /* R_MSP430X_SYM_DIFF */
7d81bc93 13157 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
f84ce13b
NC
13158 /* PR 21139. */
13159 if (sym_index >= num_syms)
13160 error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
13161 sym_index);
13162 else
13163 saved_sym = symtab + sym_index;
015dc7e1 13164 return true;
13761a11
NC
13165
13166 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13167 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
13168 goto handle_sym_diff;
0b4362b0 13169
13761a11
NC
13170 case 5: /* R_MSP430_16_BYTE */
13171 case 9: /* R_MSP430_8 */
7d81bc93 13172 case 11: /* R_MSP430_GNU_SET_ULEB128 */
dda8d76d 13173 if (uses_msp430x_relocs (filedata))
13761a11
NC
13174 break;
13175 goto handle_sym_diff;
13176
13177 case 2: /* R_MSP430_ABS16 */
13178 case 15: /* R_MSP430X_ABS16 */
7d81bc93 13179 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
dda8d76d 13180 if (! uses_msp430x_relocs (filedata))
13761a11
NC
13181 break;
13182 goto handle_sym_diff;
0b4362b0 13183
13761a11
NC
13184 handle_sym_diff:
13185 if (saved_sym != NULL)
13186 {
13187 bfd_vma value;
5a805384 13188 unsigned int reloc_size = 0;
7d81bc93
JL
13189 int leb_ret = 0;
13190 switch (reloc_type)
13191 {
13192 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13193 reloc_size = 4;
13194 break;
13195 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13196 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
5a805384 13197 if (reloc->r_offset < (size_t) (end - start))
015dc7e1 13198 read_leb128 (start + reloc->r_offset, end, false,
5a805384 13199 &reloc_size, &leb_ret);
7d81bc93
JL
13200 break;
13201 default:
13202 reloc_size = 2;
13203 break;
13204 }
13761a11 13205
5a805384 13206 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
7d81bc93
JL
13207 error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
13208 "ULEB128 value\n"),
13209 (long) reloc->r_offset);
13210 else if (sym_index >= num_syms)
f84ce13b
NC
13211 error (_("MSP430 reloc contains invalid symbol index %lu\n"),
13212 sym_index);
03f7786e 13213 else
f84ce13b
NC
13214 {
13215 value = reloc->r_addend + (symtab[sym_index].st_value
13216 - saved_sym->st_value);
13217
b32e566b 13218 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
f84ce13b 13219 byte_put (start + reloc->r_offset, value, reloc_size);
b32e566b
NC
13220 else
13221 /* PR 21137 */
13222 error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
13223 (long) reloc->r_offset);
f84ce13b 13224 }
13761a11
NC
13225
13226 saved_sym = NULL;
015dc7e1 13227 return true;
13761a11
NC
13228 }
13229 break;
13230
13231 default:
13232 if (saved_sym != NULL)
071436c6 13233 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
13761a11
NC
13234 break;
13235 }
13236 break;
13237 }
13238
cf13d699
NC
13239 case EM_MN10300:
13240 case EM_CYGNUS_MN10300:
13241 {
13242 static Elf_Internal_Sym * saved_sym = NULL;
252b5132 13243
f84ce13b
NC
13244 if (reloc == NULL)
13245 {
13246 saved_sym = NULL;
015dc7e1 13247 return true;
f84ce13b
NC
13248 }
13249
cf13d699
NC
13250 switch (reloc_type)
13251 {
13252 case 34: /* R_MN10300_ALIGN */
015dc7e1 13253 return true;
cf13d699 13254 case 33: /* R_MN10300_SYM_DIFF */
f84ce13b
NC
13255 if (sym_index >= num_syms)
13256 error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
13257 sym_index);
13258 else
13259 saved_sym = symtab + sym_index;
015dc7e1 13260 return true;
f84ce13b 13261
cf13d699
NC
13262 case 1: /* R_MN10300_32 */
13263 case 2: /* R_MN10300_16 */
13264 if (saved_sym != NULL)
13265 {
03f7786e 13266 int reloc_size = reloc_type == 1 ? 4 : 2;
cf13d699 13267 bfd_vma value;
252b5132 13268
f84ce13b
NC
13269 if (sym_index >= num_syms)
13270 error (_("MN10300 reloc contains invalid symbol index %lu\n"),
13271 sym_index);
03f7786e 13272 else
f84ce13b
NC
13273 {
13274 value = reloc->r_addend + (symtab[sym_index].st_value
13275 - saved_sym->st_value);
13276
b32e566b 13277 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
f84ce13b 13278 byte_put (start + reloc->r_offset, value, reloc_size);
b32e566b
NC
13279 else
13280 error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
13281 (long) reloc->r_offset);
f84ce13b 13282 }
252b5132 13283
cf13d699 13284 saved_sym = NULL;
015dc7e1 13285 return true;
cf13d699
NC
13286 }
13287 break;
13288 default:
13289 if (saved_sym != NULL)
071436c6 13290 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
cf13d699
NC
13291 break;
13292 }
13293 break;
13294 }
6ff71e76
NC
13295
13296 case EM_RL78:
13297 {
13298 static bfd_vma saved_sym1 = 0;
13299 static bfd_vma saved_sym2 = 0;
13300 static bfd_vma value;
13301
f84ce13b
NC
13302 if (reloc == NULL)
13303 {
13304 saved_sym1 = saved_sym2 = 0;
015dc7e1 13305 return true;
f84ce13b
NC
13306 }
13307
6ff71e76
NC
13308 switch (reloc_type)
13309 {
13310 case 0x80: /* R_RL78_SYM. */
13311 saved_sym1 = saved_sym2;
f84ce13b
NC
13312 if (sym_index >= num_syms)
13313 error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
13314 sym_index);
13315 else
13316 {
13317 saved_sym2 = symtab[sym_index].st_value;
13318 saved_sym2 += reloc->r_addend;
13319 }
015dc7e1 13320 return true;
6ff71e76
NC
13321
13322 case 0x83: /* R_RL78_OPsub. */
13323 value = saved_sym1 - saved_sym2;
13324 saved_sym2 = saved_sym1 = 0;
015dc7e1 13325 return true;
6ff71e76
NC
13326 break;
13327
13328 case 0x41: /* R_RL78_ABS32. */
b32e566b 13329 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
03f7786e 13330 byte_put (start + reloc->r_offset, value, 4);
b32e566b
NC
13331 else
13332 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13333 (long) reloc->r_offset);
6ff71e76 13334 value = 0;
015dc7e1 13335 return true;
6ff71e76
NC
13336
13337 case 0x43: /* R_RL78_ABS16. */
b32e566b 13338 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
03f7786e 13339 byte_put (start + reloc->r_offset, value, 2);
b32e566b
NC
13340 else
13341 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13342 (long) reloc->r_offset);
6ff71e76 13343 value = 0;
015dc7e1 13344 return true;
6ff71e76
NC
13345
13346 default:
13347 break;
13348 }
13349 break;
13350 }
252b5132
RH
13351 }
13352
015dc7e1 13353 return false;
252b5132
RH
13354}
13355
aca88567
NC
13356/* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
13357 DWARF debug sections. This is a target specific test. Note - we do not
13358 go through the whole including-target-headers-multiple-times route, (as
13359 we have already done with <elf/h8.h>) because this would become very
13360 messy and even then this function would have to contain target specific
13361 information (the names of the relocs instead of their numeric values).
13362 FIXME: This is not the correct way to solve this problem. The proper way
13363 is to have target specific reloc sizing and typing functions created by
13364 the reloc-macros.h header, in the same way that it already creates the
13365 reloc naming functions. */
13366
015dc7e1 13367static bool
dda8d76d 13368is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
aca88567 13369{
d347c9df 13370 /* Please keep this table alpha-sorted for ease of visual lookup. */
dda8d76d 13371 switch (filedata->file_header.e_machine)
aca88567 13372 {
41e92641 13373 case EM_386:
22abe556 13374 case EM_IAMCU:
41e92641 13375 return reloc_type == 1; /* R_386_32. */
aca88567
NC
13376 case EM_68K:
13377 return reloc_type == 1; /* R_68K_32. */
f954747f
AM
13378 case EM_860:
13379 return reloc_type == 1; /* R_860_32. */
13380 case EM_960:
13381 return reloc_type == 2; /* R_960_32. */
a06ea964 13382 case EM_AARCH64:
9282b95a
JW
13383 return (reloc_type == 258
13384 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
aca4efc7
JM
13385 case EM_BPF:
13386 return reloc_type == 11; /* R_BPF_DATA_32 */
d347c9df
PS
13387 case EM_ADAPTEVA_EPIPHANY:
13388 return reloc_type == 3;
aca88567 13389 case EM_ALPHA:
137b6b5f 13390 return reloc_type == 1; /* R_ALPHA_REFLONG. */
41e92641
NC
13391 case EM_ARC:
13392 return reloc_type == 1; /* R_ARC_32. */
886a2506
NC
13393 case EM_ARC_COMPACT:
13394 case EM_ARC_COMPACT2:
13395 return reloc_type == 4; /* R_ARC_32. */
41e92641
NC
13396 case EM_ARM:
13397 return reloc_type == 2; /* R_ARM_ABS32 */
cb8f3167 13398 case EM_AVR_OLD:
aca88567
NC
13399 case EM_AVR:
13400 return reloc_type == 1;
13401 case EM_BLACKFIN:
13402 return reloc_type == 0x12; /* R_byte4_data. */
13403 case EM_CRIS:
13404 return reloc_type == 3; /* R_CRIS_32. */
13405 case EM_CR16:
13406 return reloc_type == 3; /* R_CR16_NUM32. */
13407 case EM_CRX:
13408 return reloc_type == 15; /* R_CRX_NUM32. */
b8891f8d
AJ
13409 case EM_CSKY:
13410 return reloc_type == 1; /* R_CKCORE_ADDR32. */
aca88567
NC
13411 case EM_CYGNUS_FRV:
13412 return reloc_type == 1;
41e92641
NC
13413 case EM_CYGNUS_D10V:
13414 case EM_D10V:
13415 return reloc_type == 6; /* R_D10V_32. */
aca88567
NC
13416 case EM_CYGNUS_D30V:
13417 case EM_D30V:
13418 return reloc_type == 12; /* R_D30V_32_NORMAL. */
41e92641
NC
13419 case EM_DLX:
13420 return reloc_type == 3; /* R_DLX_RELOC_32. */
aca88567
NC
13421 case EM_CYGNUS_FR30:
13422 case EM_FR30:
13423 return reloc_type == 3; /* R_FR30_32. */
3f8107ab
AM
13424 case EM_FT32:
13425 return reloc_type == 1; /* R_FT32_32. */
aca88567
NC
13426 case EM_H8S:
13427 case EM_H8_300:
13428 case EM_H8_300H:
13429 return reloc_type == 1; /* R_H8_DIR32. */
3730236a 13430 case EM_IA_64:
262cdac7
AM
13431 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
13432 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
13433 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
13434 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
aca88567
NC
13435 case EM_IP2K_OLD:
13436 case EM_IP2K:
13437 return reloc_type == 2; /* R_IP2K_32. */
13438 case EM_IQ2000:
13439 return reloc_type == 2; /* R_IQ2000_32. */
84e94c90
NC
13440 case EM_LATTICEMICO32:
13441 return reloc_type == 3; /* R_LM32_32. */
ff7eeb89 13442 case EM_M32C_OLD:
aca88567
NC
13443 case EM_M32C:
13444 return reloc_type == 3; /* R_M32C_32. */
13445 case EM_M32R:
13446 return reloc_type == 34; /* R_M32R_32_RELA. */
adec12c1
AM
13447 case EM_68HC11:
13448 case EM_68HC12:
13449 return reloc_type == 6; /* R_M68HC11_32. */
7b4ae824 13450 case EM_S12Z:
2849d19f
JD
13451 return reloc_type == 7 || /* R_S12Z_EXT32 */
13452 reloc_type == 6; /* R_S12Z_CW32. */
aca88567
NC
13453 case EM_MCORE:
13454 return reloc_type == 1; /* R_MCORE_ADDR32. */
13455 case EM_CYGNUS_MEP:
13456 return reloc_type == 4; /* R_MEP_32. */
a3c62988
NC
13457 case EM_METAG:
13458 return reloc_type == 2; /* R_METAG_ADDR32. */
137b6b5f
AM
13459 case EM_MICROBLAZE:
13460 return reloc_type == 1; /* R_MICROBLAZE_32. */
aca88567
NC
13461 case EM_MIPS:
13462 return reloc_type == 2; /* R_MIPS_32. */
13463 case EM_MMIX:
13464 return reloc_type == 4; /* R_MMIX_32. */
13465 case EM_CYGNUS_MN10200:
13466 case EM_MN10200:
13467 return reloc_type == 1; /* R_MN10200_32. */
13468 case EM_CYGNUS_MN10300:
13469 case EM_MN10300:
13470 return reloc_type == 1; /* R_MN10300_32. */
5506d11a
AM
13471 case EM_MOXIE:
13472 return reloc_type == 1; /* R_MOXIE_32. */
aca88567
NC
13473 case EM_MSP430_OLD:
13474 case EM_MSP430:
13761a11 13475 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
aca88567
NC
13476 case EM_MT:
13477 return reloc_type == 2; /* R_MT_32. */
35c08157
KLC
13478 case EM_NDS32:
13479 return reloc_type == 20; /* R_NDS32_RELA. */
3e0873ac 13480 case EM_ALTERA_NIOS2:
36591ba1 13481 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
3e0873ac
NC
13482 case EM_NIOS32:
13483 return reloc_type == 1; /* R_NIOS_32. */
73589c9d
CS
13484 case EM_OR1K:
13485 return reloc_type == 1; /* R_OR1K_32. */
aca88567 13486 case EM_PARISC:
9abca702 13487 return (reloc_type == 1 /* R_PARISC_DIR32. */
0df8ad28 13488 || reloc_type == 2 /* R_PARISC_DIR21L. */
5fda8eca 13489 || reloc_type == 41); /* R_PARISC_SECREL32. */
aca88567
NC
13490 case EM_PJ:
13491 case EM_PJ_OLD:
13492 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
13493 case EM_PPC64:
13494 return reloc_type == 1; /* R_PPC64_ADDR32. */
13495 case EM_PPC:
13496 return reloc_type == 1; /* R_PPC_ADDR32. */
2b100bb5
DD
13497 case EM_TI_PRU:
13498 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
e23eba97
NC
13499 case EM_RISCV:
13500 return reloc_type == 1; /* R_RISCV_32. */
99c513f6
DD
13501 case EM_RL78:
13502 return reloc_type == 1; /* R_RL78_DIR32. */
c7927a3c
NC
13503 case EM_RX:
13504 return reloc_type == 1; /* R_RX_DIR32. */
f954747f
AM
13505 case EM_S370:
13506 return reloc_type == 1; /* R_I370_ADDR31. */
aca88567
NC
13507 case EM_S390_OLD:
13508 case EM_S390:
13509 return reloc_type == 4; /* R_S390_32. */
41e92641
NC
13510 case EM_SCORE:
13511 return reloc_type == 8; /* R_SCORE_ABS32. */
aca88567
NC
13512 case EM_SH:
13513 return reloc_type == 1; /* R_SH_DIR32. */
13514 case EM_SPARC32PLUS:
13515 case EM_SPARCV9:
13516 case EM_SPARC:
13517 return reloc_type == 3 /* R_SPARC_32. */
13518 || reloc_type == 23; /* R_SPARC_UA32. */
a7dd7d05
AM
13519 case EM_SPU:
13520 return reloc_type == 6; /* R_SPU_ADDR32 */
40b36596
JM
13521 case EM_TI_C6000:
13522 return reloc_type == 1; /* R_C6000_ABS32. */
aa137e4d
NC
13523 case EM_TILEGX:
13524 return reloc_type == 2; /* R_TILEGX_32. */
13525 case EM_TILEPRO:
13526 return reloc_type == 1; /* R_TILEPRO_32. */
aca88567
NC
13527 case EM_CYGNUS_V850:
13528 case EM_V850:
13529 return reloc_type == 6; /* R_V850_ABS32. */
708e2187
NC
13530 case EM_V800:
13531 return reloc_type == 0x33; /* R_V810_WORD. */
aca88567
NC
13532 case EM_VAX:
13533 return reloc_type == 1; /* R_VAX_32. */
619ed720
EB
13534 case EM_VISIUM:
13535 return reloc_type == 3; /* R_VISIUM_32. */
f96bd6c2
PC
13536 case EM_WEBASSEMBLY:
13537 return reloc_type == 1; /* R_WASM32_32. */
aca88567 13538 case EM_X86_64:
8a9036a4 13539 case EM_L1OM:
7a9068fe 13540 case EM_K1OM:
aca88567 13541 return reloc_type == 10; /* R_X86_64_32. */
c29aca4a
NC
13542 case EM_XC16X:
13543 case EM_C166:
13544 return reloc_type == 3; /* R_XC16C_ABS_32. */
f6c1a2d5
NC
13545 case EM_XGATE:
13546 return reloc_type == 4; /* R_XGATE_32. */
aca88567
NC
13547 case EM_XSTORMY16:
13548 return reloc_type == 1; /* R_XSTROMY16_32. */
13549 case EM_XTENSA_OLD:
13550 case EM_XTENSA:
13551 return reloc_type == 1; /* R_XTENSA_32. */
6655dba2
SB
13552 case EM_Z80:
13553 return reloc_type == 6; /* R_Z80_32. */
aca88567 13554 default:
bee0ee85
NC
13555 {
13556 static unsigned int prev_warn = 0;
13557
13558 /* Avoid repeating the same warning multiple times. */
dda8d76d 13559 if (prev_warn != filedata->file_header.e_machine)
bee0ee85 13560 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
dda8d76d
NC
13561 filedata->file_header.e_machine);
13562 prev_warn = filedata->file_header.e_machine;
015dc7e1 13563 return false;
bee0ee85 13564 }
aca88567
NC
13565 }
13566}
13567
13568/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13569 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
13570
015dc7e1 13571static bool
dda8d76d 13572is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
aca88567 13573{
dda8d76d 13574 switch (filedata->file_header.e_machine)
d347c9df 13575 /* Please keep this table alpha-sorted for ease of visual lookup. */
aca88567 13576 {
41e92641 13577 case EM_386:
22abe556 13578 case EM_IAMCU:
3e0873ac 13579 return reloc_type == 2; /* R_386_PC32. */
aca88567 13580 case EM_68K:
3e0873ac 13581 return reloc_type == 4; /* R_68K_PC32. */
a06ea964
NC
13582 case EM_AARCH64:
13583 return reloc_type == 261; /* R_AARCH64_PREL32 */
cfb8c092
NC
13584 case EM_ADAPTEVA_EPIPHANY:
13585 return reloc_type == 6;
aca88567
NC
13586 case EM_ALPHA:
13587 return reloc_type == 10; /* R_ALPHA_SREL32. */
726c18e1
CZ
13588 case EM_ARC_COMPACT:
13589 case EM_ARC_COMPACT2:
13590 return reloc_type == 49; /* R_ARC_32_PCREL. */
41e92641 13591 case EM_ARM:
3e0873ac 13592 return reloc_type == 3; /* R_ARM_REL32 */
d347c9df
PS
13593 case EM_AVR_OLD:
13594 case EM_AVR:
13595 return reloc_type == 36; /* R_AVR_32_PCREL. */
137b6b5f
AM
13596 case EM_MICROBLAZE:
13597 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
73589c9d
CS
13598 case EM_OR1K:
13599 return reloc_type == 9; /* R_OR1K_32_PCREL. */
aca88567 13600 case EM_PARISC:
85acf597 13601 return reloc_type == 9; /* R_PARISC_PCREL32. */
aca88567
NC
13602 case EM_PPC:
13603 return reloc_type == 26; /* R_PPC_REL32. */
13604 case EM_PPC64:
3e0873ac 13605 return reloc_type == 26; /* R_PPC64_REL32. */
25cbdcbb
AS
13606 case EM_RISCV:
13607 return reloc_type == 57; /* R_RISCV_32_PCREL. */
aca88567
NC
13608 case EM_S390_OLD:
13609 case EM_S390:
3e0873ac 13610 return reloc_type == 5; /* R_390_PC32. */
aca88567 13611 case EM_SH:
3e0873ac 13612 return reloc_type == 2; /* R_SH_REL32. */
aca88567
NC
13613 case EM_SPARC32PLUS:
13614 case EM_SPARCV9:
13615 case EM_SPARC:
3e0873ac 13616 return reloc_type == 6; /* R_SPARC_DISP32. */
a7dd7d05
AM
13617 case EM_SPU:
13618 return reloc_type == 13; /* R_SPU_REL32. */
aa137e4d
NC
13619 case EM_TILEGX:
13620 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
13621 case EM_TILEPRO:
13622 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
619ed720
EB
13623 case EM_VISIUM:
13624 return reloc_type == 6; /* R_VISIUM_32_PCREL */
aca88567 13625 case EM_X86_64:
8a9036a4 13626 case EM_L1OM:
7a9068fe 13627 case EM_K1OM:
3e0873ac 13628 return reloc_type == 2; /* R_X86_64_PC32. */
2057d69d
CZ
13629 case EM_VAX:
13630 return reloc_type == 4; /* R_VAX_PCREL32. */
2fcb9706
BW
13631 case EM_XTENSA_OLD:
13632 case EM_XTENSA:
13633 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
aca88567
NC
13634 default:
13635 /* Do not abort or issue an error message here. Not all targets use
13636 pc-relative 32-bit relocs in their DWARF debug information and we
13637 have already tested for target coverage in is_32bit_abs_reloc. A
cf13d699
NC
13638 more helpful warning message will be generated by apply_relocations
13639 anyway, so just return. */
015dc7e1 13640 return false;
aca88567
NC
13641 }
13642}
13643
13644/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13645 a 64-bit absolute RELA relocation used in DWARF debug sections. */
13646
015dc7e1 13647static bool
dda8d76d 13648is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
aca88567 13649{
dda8d76d 13650 switch (filedata->file_header.e_machine)
aca88567 13651 {
a06ea964
NC
13652 case EM_AARCH64:
13653 return reloc_type == 257; /* R_AARCH64_ABS64. */
aca88567
NC
13654 case EM_ALPHA:
13655 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
3730236a 13656 case EM_IA_64:
262cdac7
AM
13657 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
13658 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
3e0873ac
NC
13659 case EM_PARISC:
13660 return reloc_type == 80; /* R_PARISC_DIR64. */
aca88567
NC
13661 case EM_PPC64:
13662 return reloc_type == 38; /* R_PPC64_ADDR64. */
e23eba97
NC
13663 case EM_RISCV:
13664 return reloc_type == 2; /* R_RISCV_64. */
aca88567
NC
13665 case EM_SPARC32PLUS:
13666 case EM_SPARCV9:
13667 case EM_SPARC:
714da62f
NC
13668 return reloc_type == 32 /* R_SPARC_64. */
13669 || reloc_type == 54; /* R_SPARC_UA64. */
aca88567 13670 case EM_X86_64:
8a9036a4 13671 case EM_L1OM:
7a9068fe 13672 case EM_K1OM:
aca88567 13673 return reloc_type == 1; /* R_X86_64_64. */
e819ade1
AS
13674 case EM_S390_OLD:
13675 case EM_S390:
aa137e4d
NC
13676 return reloc_type == 22; /* R_S390_64. */
13677 case EM_TILEGX:
13678 return reloc_type == 1; /* R_TILEGX_64. */
85a82265 13679 case EM_MIPS:
aa137e4d 13680 return reloc_type == 18; /* R_MIPS_64. */
aca88567 13681 default:
015dc7e1 13682 return false;
aca88567
NC
13683 }
13684}
13685
85acf597
RH
13686/* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
13687 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
13688
015dc7e1 13689static bool
dda8d76d 13690is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
85acf597 13691{
dda8d76d 13692 switch (filedata->file_header.e_machine)
85acf597 13693 {
a06ea964
NC
13694 case EM_AARCH64:
13695 return reloc_type == 260; /* R_AARCH64_PREL64. */
85acf597 13696 case EM_ALPHA:
aa137e4d 13697 return reloc_type == 11; /* R_ALPHA_SREL64. */
85acf597 13698 case EM_IA_64:
262cdac7
AM
13699 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
13700 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
85acf597 13701 case EM_PARISC:
aa137e4d 13702 return reloc_type == 72; /* R_PARISC_PCREL64. */
85acf597 13703 case EM_PPC64:
aa137e4d 13704 return reloc_type == 44; /* R_PPC64_REL64. */
85acf597
RH
13705 case EM_SPARC32PLUS:
13706 case EM_SPARCV9:
13707 case EM_SPARC:
aa137e4d 13708 return reloc_type == 46; /* R_SPARC_DISP64. */
85acf597 13709 case EM_X86_64:
8a9036a4 13710 case EM_L1OM:
7a9068fe 13711 case EM_K1OM:
aa137e4d 13712 return reloc_type == 24; /* R_X86_64_PC64. */
85acf597
RH
13713 case EM_S390_OLD:
13714 case EM_S390:
aa137e4d
NC
13715 return reloc_type == 23; /* R_S390_PC64. */
13716 case EM_TILEGX:
13717 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
85acf597 13718 default:
015dc7e1 13719 return false;
85acf597
RH
13720 }
13721}
13722
4dc3c23d
AM
13723/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13724 a 24-bit absolute RELA relocation used in DWARF debug sections. */
13725
015dc7e1 13726static bool
dda8d76d 13727is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
4dc3c23d 13728{
dda8d76d 13729 switch (filedata->file_header.e_machine)
4dc3c23d
AM
13730 {
13731 case EM_CYGNUS_MN10200:
13732 case EM_MN10200:
13733 return reloc_type == 4; /* R_MN10200_24. */
3ee6e4fb
NC
13734 case EM_FT32:
13735 return reloc_type == 5; /* R_FT32_20. */
6655dba2
SB
13736 case EM_Z80:
13737 return reloc_type == 5; /* R_Z80_24. */
4dc3c23d 13738 default:
015dc7e1 13739 return false;
4dc3c23d
AM
13740 }
13741}
13742
aca88567
NC
13743/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13744 a 16-bit absolute RELA relocation used in DWARF debug sections. */
13745
015dc7e1 13746static bool
dda8d76d 13747is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
4b78141a 13748{
d347c9df 13749 /* Please keep this table alpha-sorted for ease of visual lookup. */
dda8d76d 13750 switch (filedata->file_header.e_machine)
4b78141a 13751 {
886a2506
NC
13752 case EM_ARC:
13753 case EM_ARC_COMPACT:
13754 case EM_ARC_COMPACT2:
13755 return reloc_type == 2; /* R_ARC_16. */
d347c9df
PS
13756 case EM_ADAPTEVA_EPIPHANY:
13757 return reloc_type == 5;
aca88567
NC
13758 case EM_AVR_OLD:
13759 case EM_AVR:
13760 return reloc_type == 4; /* R_AVR_16. */
41e92641
NC
13761 case EM_CYGNUS_D10V:
13762 case EM_D10V:
13763 return reloc_type == 3; /* R_D10V_16. */
81b42bca
JB
13764 case EM_FT32:
13765 return reloc_type == 2; /* R_FT32_16. */
4b78141a
NC
13766 case EM_H8S:
13767 case EM_H8_300:
13768 case EM_H8_300H:
aca88567
NC
13769 return reloc_type == R_H8_DIR16;
13770 case EM_IP2K_OLD:
13771 case EM_IP2K:
13772 return reloc_type == 1; /* R_IP2K_16. */
ff7eeb89 13773 case EM_M32C_OLD:
f4236fe4
DD
13774 case EM_M32C:
13775 return reloc_type == 1; /* R_M32C_16 */
d347c9df
PS
13776 case EM_CYGNUS_MN10200:
13777 case EM_MN10200:
13778 return reloc_type == 2; /* R_MN10200_16. */
13779 case EM_CYGNUS_MN10300:
13780 case EM_MN10300:
13781 return reloc_type == 2; /* R_MN10300_16. */
aca88567 13782 case EM_MSP430:
dda8d76d 13783 if (uses_msp430x_relocs (filedata))
13761a11 13784 return reloc_type == 2; /* R_MSP430_ABS16. */
1a0670f3 13785 /* Fall through. */
78c8d46c 13786 case EM_MSP430_OLD:
aca88567 13787 return reloc_type == 5; /* R_MSP430_16_BYTE. */
35c08157
KLC
13788 case EM_NDS32:
13789 return reloc_type == 19; /* R_NDS32_RELA. */
3e0873ac 13790 case EM_ALTERA_NIOS2:
36591ba1 13791 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
3e0873ac
NC
13792 case EM_NIOS32:
13793 return reloc_type == 9; /* R_NIOS_16. */
73589c9d
CS
13794 case EM_OR1K:
13795 return reloc_type == 2; /* R_OR1K_16. */
39e07931
AS
13796 case EM_RISCV:
13797 return reloc_type == 55; /* R_RISCV_SET16. */
2b100bb5
DD
13798 case EM_TI_PRU:
13799 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
40b36596
JM
13800 case EM_TI_C6000:
13801 return reloc_type == 2; /* R_C6000_ABS16. */
d347c9df
PS
13802 case EM_VISIUM:
13803 return reloc_type == 2; /* R_VISIUM_16. */
c29aca4a
NC
13804 case EM_XC16X:
13805 case EM_C166:
13806 return reloc_type == 2; /* R_XC16C_ABS_16. */
f6c1a2d5
NC
13807 case EM_XGATE:
13808 return reloc_type == 3; /* R_XGATE_16. */
6655dba2
SB
13809 case EM_Z80:
13810 return reloc_type == 4; /* R_Z80_16. */
4b78141a 13811 default:
015dc7e1 13812 return false;
4b78141a
NC
13813 }
13814}
13815
39e07931
AS
13816/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13817 a 8-bit absolute RELA relocation used in DWARF debug sections. */
13818
015dc7e1 13819static bool
39e07931
AS
13820is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13821{
13822 switch (filedata->file_header.e_machine)
13823 {
13824 case EM_RISCV:
13825 return reloc_type == 54; /* R_RISCV_SET8. */
6655dba2
SB
13826 case EM_Z80:
13827 return reloc_type == 1; /* R_Z80_8. */
39e07931 13828 default:
015dc7e1 13829 return false;
39e07931
AS
13830 }
13831}
13832
13833/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13834 a 6-bit absolute RELA relocation used in DWARF debug sections. */
13835
015dc7e1 13836static bool
39e07931
AS
13837is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13838{
13839 switch (filedata->file_header.e_machine)
13840 {
13841 case EM_RISCV:
13842 return reloc_type == 53; /* R_RISCV_SET6. */
13843 default:
015dc7e1 13844 return false;
39e07931
AS
13845 }
13846}
13847
03336641
JW
13848/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13849 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
13850
015dc7e1 13851static bool
03336641
JW
13852is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13853{
13854 /* Please keep this table alpha-sorted for ease of visual lookup. */
13855 switch (filedata->file_header.e_machine)
13856 {
13857 case EM_RISCV:
13858 return reloc_type == 35; /* R_RISCV_ADD32. */
13859 default:
015dc7e1 13860 return false;
03336641
JW
13861 }
13862}
13863
13864/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13865 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
13866
015dc7e1 13867static bool
03336641
JW
13868is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13869{
13870 /* Please keep this table alpha-sorted for ease of visual lookup. */
13871 switch (filedata->file_header.e_machine)
13872 {
13873 case EM_RISCV:
13874 return reloc_type == 39; /* R_RISCV_SUB32. */
13875 default:
015dc7e1 13876 return false;
03336641
JW
13877 }
13878}
13879
13880/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13881 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
13882
015dc7e1 13883static bool
03336641
JW
13884is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13885{
13886 /* Please keep this table alpha-sorted for ease of visual lookup. */
13887 switch (filedata->file_header.e_machine)
13888 {
13889 case EM_RISCV:
13890 return reloc_type == 36; /* R_RISCV_ADD64. */
13891 default:
015dc7e1 13892 return false;
03336641
JW
13893 }
13894}
13895
13896/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13897 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
13898
015dc7e1 13899static bool
03336641
JW
13900is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13901{
13902 /* Please keep this table alpha-sorted for ease of visual lookup. */
13903 switch (filedata->file_header.e_machine)
13904 {
13905 case EM_RISCV:
13906 return reloc_type == 40; /* R_RISCV_SUB64. */
13907 default:
015dc7e1 13908 return false;
03336641
JW
13909 }
13910}
13911
13912/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13913 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
13914
015dc7e1 13915static bool
03336641
JW
13916is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13917{
13918 /* Please keep this table alpha-sorted for ease of visual lookup. */
13919 switch (filedata->file_header.e_machine)
13920 {
13921 case EM_RISCV:
13922 return reloc_type == 34; /* R_RISCV_ADD16. */
13923 default:
015dc7e1 13924 return false;
03336641
JW
13925 }
13926}
13927
13928/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13929 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
13930
015dc7e1 13931static bool
03336641
JW
13932is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13933{
13934 /* Please keep this table alpha-sorted for ease of visual lookup. */
13935 switch (filedata->file_header.e_machine)
13936 {
13937 case EM_RISCV:
13938 return reloc_type == 38; /* R_RISCV_SUB16. */
13939 default:
015dc7e1 13940 return false;
03336641
JW
13941 }
13942}
13943
13944/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13945 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
13946
015dc7e1 13947static bool
03336641
JW
13948is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
13949{
13950 /* Please keep this table alpha-sorted for ease of visual lookup. */
13951 switch (filedata->file_header.e_machine)
13952 {
13953 case EM_RISCV:
13954 return reloc_type == 33; /* R_RISCV_ADD8. */
13955 default:
015dc7e1 13956 return false;
03336641
JW
13957 }
13958}
13959
13960/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13961 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
13962
015dc7e1 13963static bool
03336641
JW
13964is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13965{
13966 /* Please keep this table alpha-sorted for ease of visual lookup. */
13967 switch (filedata->file_header.e_machine)
13968 {
13969 case EM_RISCV:
13970 return reloc_type == 37; /* R_RISCV_SUB8. */
13971 default:
015dc7e1 13972 return false;
03336641
JW
13973 }
13974}
13975
39e07931
AS
13976/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13977 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
13978
015dc7e1 13979static bool
39e07931
AS
13980is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
13981{
13982 switch (filedata->file_header.e_machine)
13983 {
13984 case EM_RISCV:
13985 return reloc_type == 52; /* R_RISCV_SUB6. */
13986 default:
015dc7e1 13987 return false;
39e07931
AS
13988 }
13989}
13990
2a7b2e88
JK
13991/* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
13992 relocation entries (possibly formerly used for SHT_GROUP sections). */
13993
015dc7e1 13994static bool
dda8d76d 13995is_none_reloc (Filedata * filedata, unsigned int reloc_type)
2a7b2e88 13996{
dda8d76d 13997 switch (filedata->file_header.e_machine)
2a7b2e88 13998 {
cb8f3167 13999 case EM_386: /* R_386_NONE. */
d347c9df 14000 case EM_68K: /* R_68K_NONE. */
cfb8c092 14001 case EM_ADAPTEVA_EPIPHANY:
d347c9df
PS
14002 case EM_ALPHA: /* R_ALPHA_NONE. */
14003 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
886a2506 14004 case EM_ARC: /* R_ARC_NONE. */
886a2506 14005 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
d347c9df 14006 case EM_ARC_COMPACT: /* R_ARC_NONE. */
cb8f3167 14007 case EM_ARM: /* R_ARM_NONE. */
d347c9df 14008 case EM_C166: /* R_XC16X_NONE. */
cb8f3167 14009 case EM_CRIS: /* R_CRIS_NONE. */
d347c9df
PS
14010 case EM_FT32: /* R_FT32_NONE. */
14011 case EM_IA_64: /* R_IA64_NONE. */
7a9068fe 14012 case EM_K1OM: /* R_X86_64_NONE. */
d347c9df
PS
14013 case EM_L1OM: /* R_X86_64_NONE. */
14014 case EM_M32R: /* R_M32R_NONE. */
14015 case EM_MIPS: /* R_MIPS_NONE. */
cb8f3167 14016 case EM_MN10300: /* R_MN10300_NONE. */
5506d11a 14017 case EM_MOXIE: /* R_MOXIE_NONE. */
d347c9df
PS
14018 case EM_NIOS32: /* R_NIOS_NONE. */
14019 case EM_OR1K: /* R_OR1K_NONE. */
14020 case EM_PARISC: /* R_PARISC_NONE. */
14021 case EM_PPC64: /* R_PPC64_NONE. */
14022 case EM_PPC: /* R_PPC_NONE. */
e23eba97 14023 case EM_RISCV: /* R_RISCV_NONE. */
d347c9df
PS
14024 case EM_S390: /* R_390_NONE. */
14025 case EM_S390_OLD:
14026 case EM_SH: /* R_SH_NONE. */
14027 case EM_SPARC32PLUS:
14028 case EM_SPARC: /* R_SPARC_NONE. */
14029 case EM_SPARCV9:
aa137e4d
NC
14030 case EM_TILEGX: /* R_TILEGX_NONE. */
14031 case EM_TILEPRO: /* R_TILEPRO_NONE. */
d347c9df
PS
14032 case EM_TI_C6000:/* R_C6000_NONE. */
14033 case EM_X86_64: /* R_X86_64_NONE. */
c29aca4a 14034 case EM_XC16X:
6655dba2 14035 case EM_Z80: /* R_Z80_NONE. */
f96bd6c2 14036 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
cb8f3167 14037 return reloc_type == 0;
d347c9df 14038
a06ea964
NC
14039 case EM_AARCH64:
14040 return reloc_type == 0 || reloc_type == 256;
d347c9df
PS
14041 case EM_AVR_OLD:
14042 case EM_AVR:
14043 return (reloc_type == 0 /* R_AVR_NONE. */
14044 || reloc_type == 30 /* R_AVR_DIFF8. */
14045 || reloc_type == 31 /* R_AVR_DIFF16. */
14046 || reloc_type == 32 /* R_AVR_DIFF32. */);
14047 case EM_METAG:
14048 return reloc_type == 3; /* R_METAG_NONE. */
35c08157
KLC
14049 case EM_NDS32:
14050 return (reloc_type == 0 /* R_XTENSA_NONE. */
14051 || reloc_type == 204 /* R_NDS32_DIFF8. */
14052 || reloc_type == 205 /* R_NDS32_DIFF16. */
14053 || reloc_type == 206 /* R_NDS32_DIFF32. */
14054 || reloc_type == 207 /* R_NDS32_ULEB128. */);
2b100bb5
DD
14055 case EM_TI_PRU:
14056 return (reloc_type == 0 /* R_PRU_NONE. */
14057 || reloc_type == 65 /* R_PRU_DIFF8. */
14058 || reloc_type == 66 /* R_PRU_DIFF16. */
14059 || reloc_type == 67 /* R_PRU_DIFF32. */);
58332dda
JK
14060 case EM_XTENSA_OLD:
14061 case EM_XTENSA:
4dc3c23d
AM
14062 return (reloc_type == 0 /* R_XTENSA_NONE. */
14063 || reloc_type == 17 /* R_XTENSA_DIFF8. */
14064 || reloc_type == 18 /* R_XTENSA_DIFF16. */
30ce8e47
MF
14065 || reloc_type == 19 /* R_XTENSA_DIFF32. */
14066 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
14067 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
14068 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
14069 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
14070 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
14071 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
2a7b2e88 14072 }
015dc7e1 14073 return false;
2a7b2e88
JK
14074}
14075
d1c4b12b
NC
14076/* Returns TRUE if there is a relocation against
14077 section NAME at OFFSET bytes. */
14078
015dc7e1 14079bool
d1c4b12b
NC
14080reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
14081{
14082 Elf_Internal_Rela * relocs;
14083 Elf_Internal_Rela * rp;
14084
14085 if (dsec == NULL || dsec->reloc_info == NULL)
015dc7e1 14086 return false;
d1c4b12b
NC
14087
14088 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
14089
14090 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
14091 if (rp->r_offset == offset)
015dc7e1 14092 return true;
d1c4b12b 14093
015dc7e1 14094 return false;
d1c4b12b
NC
14095}
14096
cf13d699 14097/* Apply relocations to a section.
32ec8896
NC
14098 Returns TRUE upon success, FALSE otherwise.
14099 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
14100 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
14101 will be set to the number of relocs loaded.
14102
cf13d699 14103 Note: So far support has been added only for those relocations
32ec8896
NC
14104 which can be found in debug sections. FIXME: Add support for
14105 more relocations ? */
1b315056 14106
015dc7e1 14107static bool
dda8d76d 14108apply_relocations (Filedata * filedata,
d1c4b12b
NC
14109 const Elf_Internal_Shdr * section,
14110 unsigned char * start,
14111 bfd_size_type size,
1449284b 14112 void ** relocs_return,
d1c4b12b 14113 unsigned long * num_relocs_return)
1b315056 14114{
cf13d699 14115 Elf_Internal_Shdr * relsec;
0d2a7a93 14116 unsigned char * end = start + size;
cb8f3167 14117
d1c4b12b
NC
14118 if (relocs_return != NULL)
14119 {
14120 * (Elf_Internal_Rela **) relocs_return = NULL;
14121 * num_relocs_return = 0;
14122 }
14123
dda8d76d 14124 if (filedata->file_header.e_type != ET_REL)
32ec8896 14125 /* No relocs to apply. */
015dc7e1 14126 return true;
1b315056 14127
cf13d699 14128 /* Find the reloc section associated with the section. */
dda8d76d
NC
14129 for (relsec = filedata->section_headers;
14130 relsec < filedata->section_headers + filedata->file_header.e_shnum;
5b18a4bc 14131 ++relsec)
252b5132 14132 {
015dc7e1 14133 bool is_rela;
41e92641 14134 unsigned long num_relocs;
2cf0635d
NC
14135 Elf_Internal_Rela * relocs;
14136 Elf_Internal_Rela * rp;
14137 Elf_Internal_Shdr * symsec;
14138 Elf_Internal_Sym * symtab;
ba5cdace 14139 unsigned long num_syms;
2cf0635d 14140 Elf_Internal_Sym * sym;
252b5132 14141
41e92641 14142 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
dda8d76d
NC
14143 || relsec->sh_info >= filedata->file_header.e_shnum
14144 || filedata->section_headers + relsec->sh_info != section
c256ffe7 14145 || relsec->sh_size == 0
dda8d76d 14146 || relsec->sh_link >= filedata->file_header.e_shnum)
5b18a4bc 14147 continue;
428409d5 14148
a788aedd
AM
14149 symsec = filedata->section_headers + relsec->sh_link;
14150 if (symsec->sh_type != SHT_SYMTAB
14151 && symsec->sh_type != SHT_DYNSYM)
015dc7e1 14152 return false;
a788aedd 14153
41e92641
NC
14154 is_rela = relsec->sh_type == SHT_RELA;
14155
14156 if (is_rela)
14157 {
dda8d76d 14158 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
3f5e193b 14159 relsec->sh_size, & relocs, & num_relocs))
015dc7e1 14160 return false;
41e92641
NC
14161 }
14162 else
14163 {
dda8d76d 14164 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
3f5e193b 14165 relsec->sh_size, & relocs, & num_relocs))
015dc7e1 14166 return false;
41e92641
NC
14167 }
14168
14169 /* SH uses RELA but uses in place value instead of the addend field. */
dda8d76d 14170 if (filedata->file_header.e_machine == EM_SH)
015dc7e1 14171 is_rela = false;
428409d5 14172
dda8d76d 14173 symtab = GET_ELF_SYMBOLS (filedata, symsec, & num_syms);
103f02d3 14174
41e92641 14175 for (rp = relocs; rp < relocs + num_relocs; ++rp)
252b5132 14176 {
015dc7e1
AM
14177 bfd_vma addend;
14178 unsigned int reloc_type;
14179 unsigned int reloc_size;
14180 bool reloc_inplace = false;
14181 bool reloc_subtract = false;
14182 unsigned char *rloc;
14183 unsigned long sym_index;
4b78141a 14184
dda8d76d 14185 reloc_type = get_reloc_type (filedata, rp->r_info);
41e92641 14186
dda8d76d 14187 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
2a7b2e88 14188 continue;
dda8d76d 14189 else if (is_none_reloc (filedata, reloc_type))
98fb390a 14190 continue;
dda8d76d
NC
14191 else if (is_32bit_abs_reloc (filedata, reloc_type)
14192 || is_32bit_pcrel_reloc (filedata, reloc_type))
aca88567 14193 reloc_size = 4;
dda8d76d
NC
14194 else if (is_64bit_abs_reloc (filedata, reloc_type)
14195 || is_64bit_pcrel_reloc (filedata, reloc_type))
aca88567 14196 reloc_size = 8;
dda8d76d 14197 else if (is_24bit_abs_reloc (filedata, reloc_type))
4dc3c23d 14198 reloc_size = 3;
dda8d76d 14199 else if (is_16bit_abs_reloc (filedata, reloc_type))
aca88567 14200 reloc_size = 2;
39e07931
AS
14201 else if (is_8bit_abs_reloc (filedata, reloc_type)
14202 || is_6bit_abs_reloc (filedata, reloc_type))
14203 reloc_size = 1;
03336641
JW
14204 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
14205 reloc_type))
14206 || is_32bit_inplace_add_reloc (filedata, reloc_type))
14207 {
14208 reloc_size = 4;
015dc7e1 14209 reloc_inplace = true;
03336641
JW
14210 }
14211 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
14212 reloc_type))
14213 || is_64bit_inplace_add_reloc (filedata, reloc_type))
14214 {
14215 reloc_size = 8;
015dc7e1 14216 reloc_inplace = true;
03336641
JW
14217 }
14218 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
14219 reloc_type))
14220 || is_16bit_inplace_add_reloc (filedata, reloc_type))
14221 {
14222 reloc_size = 2;
015dc7e1 14223 reloc_inplace = true;
03336641
JW
14224 }
14225 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
14226 reloc_type))
14227 || is_8bit_inplace_add_reloc (filedata, reloc_type))
14228 {
14229 reloc_size = 1;
015dc7e1 14230 reloc_inplace = true;
03336641 14231 }
39e07931
AS
14232 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
14233 reloc_type)))
14234 {
14235 reloc_size = 1;
015dc7e1 14236 reloc_inplace = true;
39e07931 14237 }
aca88567 14238 else
4b78141a 14239 {
bee0ee85 14240 static unsigned int prev_reloc = 0;
dda8d76d 14241
bee0ee85
NC
14242 if (reloc_type != prev_reloc)
14243 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
dda8d76d 14244 reloc_type, printable_section_name (filedata, section));
bee0ee85 14245 prev_reloc = reloc_type;
4b78141a
NC
14246 continue;
14247 }
103f02d3 14248
91d6fa6a 14249 rloc = start + rp->r_offset;
75802ccb 14250 if (!IN_RANGE (start, end, rloc, reloc_size))
700dd8b7
L
14251 {
14252 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
14253 (unsigned long) rp->r_offset,
dda8d76d 14254 printable_section_name (filedata, section));
700dd8b7
L
14255 continue;
14256 }
103f02d3 14257
ba5cdace
NC
14258 sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
14259 if (sym_index >= num_syms)
14260 {
14261 warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
dda8d76d 14262 sym_index, printable_section_name (filedata, section));
ba5cdace
NC
14263 continue;
14264 }
14265 sym = symtab + sym_index;
41e92641
NC
14266
14267 /* If the reloc has a symbol associated with it,
55f25fc3
L
14268 make sure that it is of an appropriate type.
14269
14270 Relocations against symbols without type can happen.
14271 Gcc -feliminate-dwarf2-dups may generate symbols
14272 without type for debug info.
14273
14274 Icc generates relocations against function symbols
14275 instead of local labels.
14276
14277 Relocations against object symbols can happen, eg when
14278 referencing a global array. For an example of this see
14279 the _clz.o binary in libgcc.a. */
aca88567 14280 if (sym != symtab
b8871f35 14281 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
55f25fc3 14282 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
5b18a4bc 14283 {
d3a49aa8 14284 warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
dda8d76d
NC
14285 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
14286 printable_section_name (filedata, relsec),
d3a49aa8 14287 (long int)(rp - relocs));
aca88567 14288 continue;
5b18a4bc 14289 }
252b5132 14290
4dc3c23d
AM
14291 addend = 0;
14292 if (is_rela)
14293 addend += rp->r_addend;
c47320c3
AM
14294 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
14295 partial_inplace. */
4dc3c23d 14296 if (!is_rela
dda8d76d 14297 || (filedata->file_header.e_machine == EM_XTENSA
4dc3c23d 14298 && reloc_type == 1)
dda8d76d
NC
14299 || ((filedata->file_header.e_machine == EM_PJ
14300 || filedata->file_header.e_machine == EM_PJ_OLD)
c47320c3 14301 && reloc_type == 1)
dda8d76d
NC
14302 || ((filedata->file_header.e_machine == EM_D30V
14303 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
03336641
JW
14304 && reloc_type == 12)
14305 || reloc_inplace)
39e07931
AS
14306 {
14307 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
14308 addend += byte_get (rloc, reloc_size) & 0x3f;
14309 else
14310 addend += byte_get (rloc, reloc_size);
14311 }
cb8f3167 14312
dda8d76d
NC
14313 if (is_32bit_pcrel_reloc (filedata, reloc_type)
14314 || is_64bit_pcrel_reloc (filedata, reloc_type))
85acf597
RH
14315 {
14316 /* On HPPA, all pc-relative relocations are biased by 8. */
dda8d76d 14317 if (filedata->file_header.e_machine == EM_PARISC)
85acf597 14318 addend -= 8;
91d6fa6a 14319 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
85acf597
RH
14320 reloc_size);
14321 }
39e07931
AS
14322 else if (is_6bit_abs_reloc (filedata, reloc_type)
14323 || is_6bit_inplace_sub_reloc (filedata, reloc_type))
14324 {
14325 if (reloc_subtract)
14326 addend -= sym->st_value;
14327 else
14328 addend += sym->st_value;
14329 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
14330 byte_put (rloc, addend, reloc_size);
14331 }
03336641
JW
14332 else if (reloc_subtract)
14333 byte_put (rloc, addend - sym->st_value, reloc_size);
41e92641 14334 else
91d6fa6a 14335 byte_put (rloc, addend + sym->st_value, reloc_size);
5b18a4bc 14336 }
252b5132 14337
5b18a4bc 14338 free (symtab);
f84ce13b
NC
14339 /* Let the target specific reloc processing code know that
14340 we have finished with these relocs. */
dda8d76d 14341 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
d1c4b12b
NC
14342
14343 if (relocs_return)
14344 {
14345 * (Elf_Internal_Rela **) relocs_return = relocs;
14346 * num_relocs_return = num_relocs;
14347 }
14348 else
14349 free (relocs);
14350
5b18a4bc
NC
14351 break;
14352 }
32ec8896 14353
015dc7e1 14354 return true;
5b18a4bc 14355}
103f02d3 14356
cf13d699 14357#ifdef SUPPORT_DISASSEMBLY
015dc7e1 14358static bool
dda8d76d 14359disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
cf13d699 14360{
dda8d76d 14361 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
cf13d699 14362
74e1a04b 14363 /* FIXME: XXX -- to be done --- XXX */
cf13d699 14364
015dc7e1 14365 return true;
cf13d699
NC
14366}
14367#endif
14368
14369/* Reads in the contents of SECTION from FILE, returning a pointer
14370 to a malloc'ed buffer or NULL if something went wrong. */
14371
14372static char *
dda8d76d 14373get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
cf13d699 14374{
dda8d76d 14375 bfd_size_type num_bytes = section->sh_size;
cf13d699
NC
14376
14377 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
14378 {
c6b78c96 14379 printf (_("Section '%s' has no data to dump.\n"),
dda8d76d 14380 printable_section_name (filedata, section));
cf13d699
NC
14381 return NULL;
14382 }
14383
dda8d76d 14384 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
3f5e193b 14385 _("section contents"));
cf13d699
NC
14386}
14387
0e602686
NC
14388/* Uncompresses a section that was compressed using zlib, in place. */
14389
015dc7e1 14390static bool
dda8d76d
NC
14391uncompress_section_contents (unsigned char ** buffer,
14392 dwarf_size_type uncompressed_size,
14393 dwarf_size_type * size)
0e602686
NC
14394{
14395 dwarf_size_type compressed_size = *size;
14396 unsigned char * compressed_buffer = *buffer;
14397 unsigned char * uncompressed_buffer;
14398 z_stream strm;
14399 int rc;
14400
14401 /* It is possible the section consists of several compressed
14402 buffers concatenated together, so we uncompress in a loop. */
14403 /* PR 18313: The state field in the z_stream structure is supposed
14404 to be invisible to the user (ie us), but some compilers will
14405 still complain about it being used without initialisation. So
14406 we first zero the entire z_stream structure and then set the fields
14407 that we need. */
14408 memset (& strm, 0, sizeof strm);
14409 strm.avail_in = compressed_size;
14410 strm.next_in = (Bytef *) compressed_buffer;
14411 strm.avail_out = uncompressed_size;
14412 uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
14413
14414 rc = inflateInit (& strm);
14415 while (strm.avail_in > 0)
14416 {
14417 if (rc != Z_OK)
3624a6c1 14418 break;
0e602686
NC
14419 strm.next_out = ((Bytef *) uncompressed_buffer
14420 + (uncompressed_size - strm.avail_out));
14421 rc = inflate (&strm, Z_FINISH);
14422 if (rc != Z_STREAM_END)
3624a6c1 14423 break;
0e602686
NC
14424 rc = inflateReset (& strm);
14425 }
ad92f33d
AM
14426 if (inflateEnd (& strm) != Z_OK
14427 || rc != Z_OK
0e602686
NC
14428 || strm.avail_out != 0)
14429 goto fail;
14430
14431 *buffer = uncompressed_buffer;
14432 *size = uncompressed_size;
015dc7e1 14433 return true;
0e602686
NC
14434
14435 fail:
14436 free (uncompressed_buffer);
14437 /* Indicate decompression failure. */
14438 *buffer = NULL;
015dc7e1 14439 return false;
0e602686 14440}
dd24e3da 14441
015dc7e1 14442static bool
dda8d76d 14443dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
cf13d699 14444{
015dc7e1
AM
14445 Elf_Internal_Shdr *relsec;
14446 bfd_size_type num_bytes;
14447 unsigned char *data;
14448 unsigned char *end;
14449 unsigned char *real_start;
14450 unsigned char *start;
14451 bool some_strings_shown;
cf13d699 14452
dda8d76d 14453 real_start = start = (unsigned char *) get_section_contents (section, filedata);
cf13d699 14454 if (start == NULL)
c6b78c96 14455 /* PR 21820: Do not fail if the section was empty. */
63b4cc53 14456 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
c6b78c96 14457
0e602686 14458 num_bytes = section->sh_size;
cf13d699 14459
835f2fae
NC
14460 if (filedata->is_separate)
14461 printf (_("\nString dump of section '%s' in linked file %s:\n"),
14462 printable_section_name (filedata, section),
14463 filedata->file_name);
14464 else
14465 printf (_("\nString dump of section '%s':\n"),
14466 printable_section_name (filedata, section));
cf13d699 14467
0e602686
NC
14468 if (decompress_dumps)
14469 {
14470 dwarf_size_type new_size = num_bytes;
14471 dwarf_size_type uncompressed_size = 0;
14472
14473 if ((section->sh_flags & SHF_COMPRESSED) != 0)
14474 {
14475 Elf_Internal_Chdr chdr;
14476 unsigned int compression_header_size
ebdf1ebf
NC
14477 = get_compression_header (& chdr, (unsigned char *) start,
14478 num_bytes);
5844b465
NC
14479 if (compression_header_size == 0)
14480 /* An error message will have already been generated
14481 by get_compression_header. */
14482 goto error_out;
0e602686 14483
813dabb9 14484 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
0e602686 14485 {
813dabb9 14486 warn (_("section '%s' has unsupported compress type: %d\n"),
dda8d76d 14487 printable_section_name (filedata, section), chdr.ch_type);
f761cb13 14488 goto error_out;
813dabb9 14489 }
813dabb9
L
14490 uncompressed_size = chdr.ch_size;
14491 start += compression_header_size;
14492 new_size -= compression_header_size;
0e602686
NC
14493 }
14494 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
14495 {
14496 /* Read the zlib header. In this case, it should be "ZLIB"
14497 followed by the uncompressed section size, 8 bytes in
14498 big-endian order. */
14499 uncompressed_size = start[4]; uncompressed_size <<= 8;
14500 uncompressed_size += start[5]; uncompressed_size <<= 8;
14501 uncompressed_size += start[6]; uncompressed_size <<= 8;
14502 uncompressed_size += start[7]; uncompressed_size <<= 8;
14503 uncompressed_size += start[8]; uncompressed_size <<= 8;
14504 uncompressed_size += start[9]; uncompressed_size <<= 8;
14505 uncompressed_size += start[10]; uncompressed_size <<= 8;
14506 uncompressed_size += start[11];
14507 start += 12;
14508 new_size -= 12;
14509 }
14510
1835f746
NC
14511 if (uncompressed_size)
14512 {
14513 if (uncompress_section_contents (& start,
14514 uncompressed_size, & new_size))
14515 num_bytes = new_size;
14516 else
14517 {
14518 error (_("Unable to decompress section %s\n"),
dda8d76d 14519 printable_section_name (filedata, section));
f761cb13 14520 goto error_out;
1835f746
NC
14521 }
14522 }
bc303e5d
NC
14523 else
14524 start = real_start;
0e602686 14525 }
fd8008d8 14526
cf13d699
NC
14527 /* If the section being dumped has relocations against it the user might
14528 be expecting these relocations to have been applied. Check for this
14529 case and issue a warning message in order to avoid confusion.
14530 FIXME: Maybe we ought to have an option that dumps a section with
14531 relocs applied ? */
dda8d76d
NC
14532 for (relsec = filedata->section_headers;
14533 relsec < filedata->section_headers + filedata->file_header.e_shnum;
cf13d699
NC
14534 ++relsec)
14535 {
14536 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
dda8d76d
NC
14537 || relsec->sh_info >= filedata->file_header.e_shnum
14538 || filedata->section_headers + relsec->sh_info != section
cf13d699 14539 || relsec->sh_size == 0
dda8d76d 14540 || relsec->sh_link >= filedata->file_header.e_shnum)
cf13d699
NC
14541 continue;
14542
14543 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
14544 break;
14545 }
14546
cf13d699
NC
14547 data = start;
14548 end = start + num_bytes;
015dc7e1 14549 some_strings_shown = false;
cf13d699 14550
ba3265d0
NC
14551#ifdef HAVE_MBSTATE_T
14552 mbstate_t state;
14553 /* Initialise the multibyte conversion state. */
14554 memset (& state, 0, sizeof (state));
14555#endif
14556
015dc7e1 14557 bool continuing = false;
ba3265d0 14558
cf13d699
NC
14559 while (data < end)
14560 {
14561 while (!ISPRINT (* data))
14562 if (++ data >= end)
14563 break;
14564
14565 if (data < end)
14566 {
071436c6
NC
14567 size_t maxlen = end - data;
14568
ba3265d0
NC
14569 if (continuing)
14570 {
14571 printf (" ");
015dc7e1 14572 continuing = false;
ba3265d0
NC
14573 }
14574 else
14575 {
d1ce973e 14576 printf (" [%6lx] ", (unsigned long) (data - start));
ba3265d0
NC
14577 }
14578
4082ef84
NC
14579 if (maxlen > 0)
14580 {
f3da8a96 14581 char c = 0;
ba3265d0
NC
14582
14583 while (maxlen)
14584 {
14585 c = *data++;
14586
14587 if (c == 0)
14588 break;
14589
14590 /* PR 25543: Treat new-lines as string-ending characters. */
14591 if (c == '\n')
14592 {
14593 printf ("\\n\n");
14594 if (*data != 0)
015dc7e1 14595 continuing = true;
ba3265d0
NC
14596 break;
14597 }
14598
14599 /* Do not print control characters directly as they can affect terminal
14600 settings. Such characters usually appear in the names generated
14601 by the assembler for local labels. */
14602 if (ISCNTRL (c))
14603 {
14604 printf ("^%c", c + 0x40);
14605 }
14606 else if (ISPRINT (c))
14607 {
14608 putchar (c);
14609 }
14610 else
14611 {
14612 size_t n;
14613#ifdef HAVE_MBSTATE_T
14614 wchar_t w;
14615#endif
14616 /* Let printf do the hard work of displaying multibyte characters. */
14617 printf ("%.1s", data - 1);
14618#ifdef HAVE_MBSTATE_T
14619 /* Try to find out how many bytes made up the character that was
14620 just printed. Advance the symbol pointer past the bytes that
14621 were displayed. */
14622 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
14623#else
14624 n = 1;
14625#endif
14626 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
14627 data += (n - 1);
14628 }
14629 }
14630
14631 if (c != '\n')
14632 putchar ('\n');
4082ef84
NC
14633 }
14634 else
14635 {
14636 printf (_("<corrupt>\n"));
14637 data = end;
14638 }
015dc7e1 14639 some_strings_shown = true;
cf13d699
NC
14640 }
14641 }
14642
14643 if (! some_strings_shown)
14644 printf (_(" No strings found in this section."));
14645
0e602686 14646 free (real_start);
cf13d699
NC
14647
14648 putchar ('\n');
015dc7e1 14649 return true;
f761cb13
AM
14650
14651error_out:
14652 free (real_start);
015dc7e1 14653 return false;
cf13d699
NC
14654}
14655
015dc7e1
AM
14656static bool
14657dump_section_as_bytes (Elf_Internal_Shdr *section,
14658 Filedata *filedata,
14659 bool relocate)
cf13d699
NC
14660{
14661 Elf_Internal_Shdr * relsec;
0e602686
NC
14662 bfd_size_type bytes;
14663 bfd_size_type section_size;
14664 bfd_vma addr;
14665 unsigned char * data;
14666 unsigned char * real_start;
14667 unsigned char * start;
14668
dda8d76d 14669 real_start = start = (unsigned char *) get_section_contents (section, filedata);
cf13d699 14670 if (start == NULL)
c6b78c96 14671 /* PR 21820: Do not fail if the section was empty. */
63b4cc53 14672 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
32ec8896 14673
0e602686 14674 section_size = section->sh_size;
cf13d699 14675
835f2fae
NC
14676 if (filedata->is_separate)
14677 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
14678 printable_section_name (filedata, section),
14679 filedata->file_name);
14680 else
14681 printf (_("\nHex dump of section '%s':\n"),
14682 printable_section_name (filedata, section));
cf13d699 14683
0e602686
NC
14684 if (decompress_dumps)
14685 {
14686 dwarf_size_type new_size = section_size;
14687 dwarf_size_type uncompressed_size = 0;
14688
14689 if ((section->sh_flags & SHF_COMPRESSED) != 0)
14690 {
14691 Elf_Internal_Chdr chdr;
14692 unsigned int compression_header_size
ebdf1ebf 14693 = get_compression_header (& chdr, start, section_size);
0e602686 14694
5844b465
NC
14695 if (compression_header_size == 0)
14696 /* An error message will have already been generated
14697 by get_compression_header. */
14698 goto error_out;
14699
813dabb9 14700 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
0e602686 14701 {
813dabb9 14702 warn (_("section '%s' has unsupported compress type: %d\n"),
dda8d76d 14703 printable_section_name (filedata, section), chdr.ch_type);
f761cb13 14704 goto error_out;
0e602686 14705 }
813dabb9
L
14706 uncompressed_size = chdr.ch_size;
14707 start += compression_header_size;
14708 new_size -= compression_header_size;
0e602686
NC
14709 }
14710 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
14711 {
14712 /* Read the zlib header. In this case, it should be "ZLIB"
14713 followed by the uncompressed section size, 8 bytes in
14714 big-endian order. */
14715 uncompressed_size = start[4]; uncompressed_size <<= 8;
14716 uncompressed_size += start[5]; uncompressed_size <<= 8;
14717 uncompressed_size += start[6]; uncompressed_size <<= 8;
14718 uncompressed_size += start[7]; uncompressed_size <<= 8;
14719 uncompressed_size += start[8]; uncompressed_size <<= 8;
14720 uncompressed_size += start[9]; uncompressed_size <<= 8;
14721 uncompressed_size += start[10]; uncompressed_size <<= 8;
14722 uncompressed_size += start[11];
14723 start += 12;
14724 new_size -= 12;
14725 }
14726
f055032e
NC
14727 if (uncompressed_size)
14728 {
14729 if (uncompress_section_contents (& start, uncompressed_size,
14730 & new_size))
bc303e5d
NC
14731 {
14732 section_size = new_size;
14733 }
f055032e
NC
14734 else
14735 {
14736 error (_("Unable to decompress section %s\n"),
dda8d76d 14737 printable_section_name (filedata, section));
bc303e5d 14738 /* FIXME: Print the section anyway ? */
f761cb13 14739 goto error_out;
f055032e
NC
14740 }
14741 }
bc303e5d
NC
14742 else
14743 start = real_start;
0e602686 14744 }
14ae95f2 14745
cf13d699
NC
14746 if (relocate)
14747 {
dda8d76d 14748 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
f761cb13 14749 goto error_out;
cf13d699
NC
14750 }
14751 else
14752 {
14753 /* If the section being dumped has relocations against it the user might
14754 be expecting these relocations to have been applied. Check for this
14755 case and issue a warning message in order to avoid confusion.
14756 FIXME: Maybe we ought to have an option that dumps a section with
14757 relocs applied ? */
dda8d76d
NC
14758 for (relsec = filedata->section_headers;
14759 relsec < filedata->section_headers + filedata->file_header.e_shnum;
cf13d699
NC
14760 ++relsec)
14761 {
14762 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
dda8d76d
NC
14763 || relsec->sh_info >= filedata->file_header.e_shnum
14764 || filedata->section_headers + relsec->sh_info != section
cf13d699 14765 || relsec->sh_size == 0
dda8d76d 14766 || relsec->sh_link >= filedata->file_header.e_shnum)
cf13d699
NC
14767 continue;
14768
14769 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
14770 break;
14771 }
14772 }
14773
14774 addr = section->sh_addr;
0e602686 14775 bytes = section_size;
cf13d699
NC
14776 data = start;
14777
14778 while (bytes)
14779 {
14780 int j;
14781 int k;
14782 int lbytes;
14783
14784 lbytes = (bytes > 16 ? 16 : bytes);
14785
14786 printf (" 0x%8.8lx ", (unsigned long) addr);
14787
14788 for (j = 0; j < 16; j++)
14789 {
14790 if (j < lbytes)
14791 printf ("%2.2x", data[j]);
14792 else
14793 printf (" ");
14794
14795 if ((j & 3) == 3)
14796 printf (" ");
14797 }
14798
14799 for (j = 0; j < lbytes; j++)
14800 {
14801 k = data[j];
14802 if (k >= ' ' && k < 0x7f)
14803 printf ("%c", k);
14804 else
14805 printf (".");
14806 }
14807
14808 putchar ('\n');
14809
14810 data += lbytes;
14811 addr += lbytes;
14812 bytes -= lbytes;
14813 }
14814
0e602686 14815 free (real_start);
cf13d699
NC
14816
14817 putchar ('\n');
015dc7e1 14818 return true;
f761cb13
AM
14819
14820 error_out:
14821 free (real_start);
015dc7e1 14822 return false;
cf13d699
NC
14823}
14824
094e34f2 14825#ifdef ENABLE_LIBCTF
7d9813f1
NA
14826static ctf_sect_t *
14827shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
14828{
b9e920ec 14829 buf->cts_name = SECTION_NAME_PRINT (shdr);
7d9813f1
NA
14830 buf->cts_size = shdr->sh_size;
14831 buf->cts_entsize = shdr->sh_entsize;
7d9813f1
NA
14832
14833 return buf;
14834}
14835
14836/* Formatting callback function passed to ctf_dump. Returns either the pointer
14837 it is passed, or a pointer to newly-allocated storage, in which case
14838 dump_ctf() will free it when it no longer needs it. */
14839
2f6ecaed
NA
14840static char *
14841dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
14842 char *s, void *arg)
7d9813f1 14843{
3e50a591 14844 const char *blanks = arg;
7d9813f1
NA
14845 char *new_s;
14846
3e50a591 14847 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
7d9813f1
NA
14848 return s;
14849 return new_s;
14850}
14851
926c9e76
NA
14852/* Dump CTF errors/warnings. */
14853static void
139633c3 14854dump_ctf_errs (ctf_dict_t *fp)
926c9e76
NA
14855{
14856 ctf_next_t *it = NULL;
14857 char *errtext;
14858 int is_warning;
14859 int err;
14860
14861 /* Dump accumulated errors and warnings. */
14862 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
14863 {
5e9b84f7 14864 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
926c9e76
NA
14865 errtext);
14866 free (errtext);
14867 }
14868 if (err != ECTF_NEXT_END)
14869 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
14870}
14871
2f6ecaed
NA
14872/* Dump one CTF archive member. */
14873
14874static int
139633c3 14875dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, void *arg)
2f6ecaed 14876{
139633c3 14877 ctf_dict_t *parent = (ctf_dict_t *) arg;
2f6ecaed
NA
14878 const char *things[] = {"Header", "Labels", "Data objects",
14879 "Function objects", "Variables", "Types", "Strings",
14880 ""};
14881 const char **thing;
14882 size_t i;
8b37e7b6 14883 int err = 0;
2f6ecaed
NA
14884
14885 /* Only print out the name of non-default-named archive members.
14886 The name .ctf appears everywhere, even for things that aren't
14887 really archives, so printing it out is liable to be confusing.
14888
14889 The parent, if there is one, is the default-owned archive member:
14890 avoid importing it into itself. (This does no harm, but looks
14891 confusing.) */
14892
14893 if (strcmp (name, ".ctf") != 0)
14894 {
14895 printf (_("\nCTF archive member: %s:\n"), name);
14896 ctf_import (ctf, parent);
14897 }
14898
14899 for (i = 0, thing = things; *thing[0]; thing++, i++)
14900 {
14901 ctf_dump_state_t *s = NULL;
14902 char *item;
14903
14904 printf ("\n %s:\n", *thing);
14905 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
14906 (void *) " ")) != NULL)
14907 {
14908 printf ("%s\n", item);
14909 free (item);
14910 }
14911
14912 if (ctf_errno (ctf))
14913 {
14914 error (_("Iteration failed: %s, %s\n"), *thing,
14915 ctf_errmsg (ctf_errno (ctf)));
8b37e7b6
NA
14916 err = 1;
14917 goto out;
2f6ecaed
NA
14918 }
14919 }
8b37e7b6
NA
14920
14921 out:
926c9e76 14922 dump_ctf_errs (ctf);
8b37e7b6 14923 return err;
2f6ecaed
NA
14924}
14925
015dc7e1 14926static bool
7d9813f1
NA
14927dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
14928{
14929 Elf_Internal_Shdr * parent_sec = NULL;
14930 Elf_Internal_Shdr * symtab_sec = NULL;
14931 Elf_Internal_Shdr * strtab_sec = NULL;
d344b407
NA
14932 void * data = NULL;
14933 void * symdata = NULL;
14934 void * strdata = NULL;
14935 void * parentdata = NULL;
14936 ctf_sect_t ctfsect, symsect, strsect, parentsect;
14937 ctf_sect_t * symsectp = NULL;
14938 ctf_sect_t * strsectp = NULL;
2f6ecaed
NA
14939 ctf_archive_t * ctfa = NULL;
14940 ctf_archive_t * parenta = NULL, *lookparent;
139633c3 14941 ctf_dict_t * parent = NULL;
7d9813f1 14942
7d9813f1 14943 int err;
015dc7e1 14944 bool ret = false;
7d9813f1
NA
14945
14946 shdr_to_ctf_sect (&ctfsect, section, filedata);
14947 data = get_section_contents (section, filedata);
14948 ctfsect.cts_data = data;
14949
616febde 14950 if (!dump_ctf_symtab_name)
3d16b64e 14951 dump_ctf_symtab_name = strdup (".dynsym");
616febde
NA
14952
14953 if (!dump_ctf_strtab_name)
3d16b64e 14954 dump_ctf_strtab_name = strdup (".dynstr");
616febde
NA
14955
14956 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
7d9813f1
NA
14957 {
14958 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
14959 {
14960 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
14961 goto fail;
14962 }
14963 if ((symdata = (void *) get_data (NULL, filedata,
14964 symtab_sec->sh_offset, 1,
14965 symtab_sec->sh_size,
14966 _("symbols"))) == NULL)
14967 goto fail;
14968 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
14969 symsect.cts_data = symdata;
14970 }
835f2fae 14971
df16e041 14972 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
7d9813f1
NA
14973 {
14974 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
14975 {
14976 error (_("No string table section named %s\n"),
14977 dump_ctf_strtab_name);
14978 goto fail;
14979 }
14980 if ((strdata = (void *) get_data (NULL, filedata,
14981 strtab_sec->sh_offset, 1,
14982 strtab_sec->sh_size,
14983 _("strings"))) == NULL)
14984 goto fail;
14985 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
14986 strsect.cts_data = strdata;
14987 }
835f2fae 14988
7d9813f1
NA
14989 if (dump_ctf_parent_name)
14990 {
14991 if ((parent_sec = find_section (filedata, dump_ctf_parent_name)) == NULL)
14992 {
14993 error (_("No CTF parent section named %s\n"), dump_ctf_parent_name);
14994 goto fail;
14995 }
14996 if ((parentdata = (void *) get_data (NULL, filedata,
14997 parent_sec->sh_offset, 1,
14998 parent_sec->sh_size,
14999 _("CTF parent"))) == NULL)
15000 goto fail;
15001 shdr_to_ctf_sect (&parentsect, parent_sec, filedata);
15002 parentsect.cts_data = parentdata;
15003 }
15004
2f6ecaed
NA
15005 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
15006 libctf papers over the difference, so we can pretend it is always an
15007 archive. Possibly open the parent as well, if one was specified. */
7d9813f1 15008
2f6ecaed 15009 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
7d9813f1 15010 {
926c9e76 15011 dump_ctf_errs (NULL);
7d9813f1
NA
15012 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15013 goto fail;
15014 }
15015
96c61be5
NA
15016 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
15017 != ELFDATA2MSB);
15018
7d9813f1
NA
15019 if (parentdata)
15020 {
2f6ecaed
NA
15021 if ((parenta = ctf_arc_bufopen (&parentsect, symsectp, strsectp,
15022 &err)) == NULL)
7d9813f1 15023 {
926c9e76 15024 dump_ctf_errs (NULL);
7d9813f1
NA
15025 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15026 goto fail;
15027 }
2f6ecaed
NA
15028 lookparent = parenta;
15029 }
15030 else
15031 lookparent = ctfa;
7d9813f1 15032
2f6ecaed
NA
15033 /* Assume that the applicable parent archive member is the default one.
15034 (This is what all known implementations are expected to do, if they
15035 put CTFs and their parents in archives together.) */
ae41200b 15036 if ((parent = ctf_dict_open (lookparent, NULL, &err)) == NULL)
2f6ecaed 15037 {
926c9e76 15038 dump_ctf_errs (NULL);
2f6ecaed
NA
15039 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15040 goto fail;
7d9813f1
NA
15041 }
15042
015dc7e1 15043 ret = true;
7d9813f1 15044
835f2fae
NC
15045 if (filedata->is_separate)
15046 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
15047 printable_section_name (filedata, section),
15048 filedata->file_name);
15049 else
15050 printf (_("\nDump of CTF section '%s':\n"),
15051 printable_section_name (filedata, section));
7d9813f1 15052
83d59285
NA
15053 if ((err = ctf_archive_iter (ctfa, dump_ctf_archive_member, parent)) != 0)
15054 {
15055 dump_ctf_errs (NULL);
15056 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
015dc7e1 15057 ret = false;
83d59285 15058 }
7d9813f1
NA
15059
15060 fail:
139633c3 15061 ctf_dict_close (parent);
2f6ecaed
NA
15062 ctf_close (ctfa);
15063 ctf_close (parenta);
7d9813f1
NA
15064 free (parentdata);
15065 free (data);
15066 free (symdata);
15067 free (strdata);
15068 return ret;
15069}
094e34f2 15070#endif
7d9813f1 15071
015dc7e1 15072static bool
dda8d76d
NC
15073load_specific_debug_section (enum dwarf_section_display_enum debug,
15074 const Elf_Internal_Shdr * sec,
15075 void * data)
1007acb3 15076{
2cf0635d 15077 struct dwarf_section * section = &debug_displays [debug].section;
19e6b90e 15078 char buf [64];
dda8d76d 15079 Filedata * filedata = (Filedata *) data;
9abca702 15080
19e6b90e 15081 if (section->start != NULL)
dda8d76d
NC
15082 {
15083 /* If it is already loaded, do nothing. */
15084 if (streq (section->filename, filedata->file_name))
015dc7e1 15085 return true;
dda8d76d
NC
15086 free (section->start);
15087 }
1007acb3 15088
19e6b90e
L
15089 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
15090 section->address = sec->sh_addr;
dda8d76d
NC
15091 section->filename = filedata->file_name;
15092 section->start = (unsigned char *) get_data (NULL, filedata,
3f5e193b
NC
15093 sec->sh_offset, 1,
15094 sec->sh_size, buf);
59245841
NC
15095 if (section->start == NULL)
15096 section->size = 0;
15097 else
15098 {
77115a4a
L
15099 unsigned char *start = section->start;
15100 dwarf_size_type size = sec->sh_size;
dab394de 15101 dwarf_size_type uncompressed_size = 0;
77115a4a
L
15102
15103 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
15104 {
15105 Elf_Internal_Chdr chdr;
d8024a91
NC
15106 unsigned int compression_header_size;
15107
f53be977
L
15108 if (size < (is_32bit_elf
15109 ? sizeof (Elf32_External_Chdr)
15110 : sizeof (Elf64_External_Chdr)))
d8024a91 15111 {
55be8fd0 15112 warn (_("compressed section %s is too small to contain a compression header\n"),
d8024a91 15113 section->name);
015dc7e1 15114 return false;
d8024a91
NC
15115 }
15116
ebdf1ebf 15117 compression_header_size = get_compression_header (&chdr, start, size);
5844b465
NC
15118 if (compression_header_size == 0)
15119 /* An error message will have already been generated
15120 by get_compression_header. */
015dc7e1 15121 return false;
d8024a91 15122
813dabb9
L
15123 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15124 {
15125 warn (_("section '%s' has unsupported compress type: %d\n"),
15126 section->name, chdr.ch_type);
015dc7e1 15127 return false;
813dabb9 15128 }
dab394de 15129 uncompressed_size = chdr.ch_size;
77115a4a
L
15130 start += compression_header_size;
15131 size -= compression_header_size;
15132 }
dab394de
L
15133 else if (size > 12 && streq ((char *) start, "ZLIB"))
15134 {
15135 /* Read the zlib header. In this case, it should be "ZLIB"
15136 followed by the uncompressed section size, 8 bytes in
15137 big-endian order. */
15138 uncompressed_size = start[4]; uncompressed_size <<= 8;
15139 uncompressed_size += start[5]; uncompressed_size <<= 8;
15140 uncompressed_size += start[6]; uncompressed_size <<= 8;
15141 uncompressed_size += start[7]; uncompressed_size <<= 8;
15142 uncompressed_size += start[8]; uncompressed_size <<= 8;
15143 uncompressed_size += start[9]; uncompressed_size <<= 8;
15144 uncompressed_size += start[10]; uncompressed_size <<= 8;
15145 uncompressed_size += start[11];
15146 start += 12;
15147 size -= 12;
15148 }
15149
1835f746 15150 if (uncompressed_size)
77115a4a 15151 {
1835f746
NC
15152 if (uncompress_section_contents (&start, uncompressed_size,
15153 &size))
15154 {
15155 /* Free the compressed buffer, update the section buffer
15156 and the section size if uncompress is successful. */
15157 free (section->start);
15158 section->start = start;
15159 }
15160 else
15161 {
15162 error (_("Unable to decompress section %s\n"),
dda8d76d 15163 printable_section_name (filedata, sec));
015dc7e1 15164 return false;
1835f746 15165 }
77115a4a 15166 }
bc303e5d 15167
77115a4a 15168 section->size = size;
59245841 15169 }
4a114e3e 15170
1b315056 15171 if (section->start == NULL)
015dc7e1 15172 return false;
1b315056 15173
19e6b90e 15174 if (debug_displays [debug].relocate)
32ec8896 15175 {
dda8d76d 15176 if (! apply_relocations (filedata, sec, section->start, section->size,
32ec8896 15177 & section->reloc_info, & section->num_relocs))
015dc7e1 15178 return false;
32ec8896 15179 }
d1c4b12b
NC
15180 else
15181 {
15182 section->reloc_info = NULL;
15183 section->num_relocs = 0;
15184 }
1007acb3 15185
015dc7e1 15186 return true;
1007acb3
L
15187}
15188
301a9420
AM
15189#if HAVE_LIBDEBUGINFOD
15190/* Return a hex string representation of the build-id. */
15191unsigned char *
15192get_build_id (void * data)
15193{
ca0e11aa 15194 Filedata * filedata = (Filedata *) data;
301a9420
AM
15195 Elf_Internal_Shdr * shdr;
15196 unsigned long i;
15197
55be8fd0
NC
15198 /* Iterate through notes to find note.gnu.build-id.
15199 FIXME: Only the first note in any note section is examined. */
301a9420
AM
15200 for (i = 0, shdr = filedata->section_headers;
15201 i < filedata->file_header.e_shnum && shdr != NULL;
15202 i++, shdr++)
15203 {
15204 if (shdr->sh_type != SHT_NOTE)
15205 continue;
15206
15207 char * next;
15208 char * end;
15209 size_t data_remaining;
15210 size_t min_notesz;
15211 Elf_External_Note * enote;
15212 Elf_Internal_Note inote;
15213
15214 bfd_vma offset = shdr->sh_offset;
15215 bfd_vma align = shdr->sh_addralign;
15216 bfd_vma length = shdr->sh_size;
15217
15218 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
15219 if (enote == NULL)
15220 continue;
15221
15222 if (align < 4)
15223 align = 4;
15224 else if (align != 4 && align != 8)
f761cb13
AM
15225 {
15226 free (enote);
15227 continue;
15228 }
301a9420
AM
15229
15230 end = (char *) enote + length;
15231 data_remaining = end - (char *) enote;
15232
15233 if (!is_ia64_vms (filedata))
15234 {
15235 min_notesz = offsetof (Elf_External_Note, name);
15236 if (data_remaining < min_notesz)
15237 {
55be8fd0
NC
15238 warn (_("\
15239malformed note encountered in section %s whilst scanning for build-id note\n"),
15240 printable_section_name (filedata, shdr));
f761cb13 15241 free (enote);
55be8fd0 15242 continue;
301a9420
AM
15243 }
15244 data_remaining -= min_notesz;
15245
15246 inote.type = BYTE_GET (enote->type);
15247 inote.namesz = BYTE_GET (enote->namesz);
15248 inote.namedata = enote->name;
15249 inote.descsz = BYTE_GET (enote->descsz);
15250 inote.descdata = ((char *) enote
15251 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
15252 inote.descpos = offset + (inote.descdata - (char *) enote);
15253 next = ((char *) enote
15254 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
15255 }
15256 else
15257 {
15258 Elf64_External_VMS_Note *vms_enote;
15259
15260 /* PR binutils/15191
15261 Make sure that there is enough data to read. */
15262 min_notesz = offsetof (Elf64_External_VMS_Note, name);
15263 if (data_remaining < min_notesz)
15264 {
55be8fd0
NC
15265 warn (_("\
15266malformed note encountered in section %s whilst scanning for build-id note\n"),
15267 printable_section_name (filedata, shdr));
f761cb13 15268 free (enote);
55be8fd0 15269 continue;
301a9420
AM
15270 }
15271 data_remaining -= min_notesz;
15272
15273 vms_enote = (Elf64_External_VMS_Note *) enote;
15274 inote.type = BYTE_GET (vms_enote->type);
15275 inote.namesz = BYTE_GET (vms_enote->namesz);
15276 inote.namedata = vms_enote->name;
15277 inote.descsz = BYTE_GET (vms_enote->descsz);
15278 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
15279 inote.descpos = offset + (inote.descdata - (char *) enote);
15280 next = inote.descdata + align_power (inote.descsz, 3);
15281 }
15282
15283 /* Skip malformed notes. */
15284 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
15285 || (size_t) (inote.descdata - inote.namedata) > data_remaining
15286 || (size_t) (next - inote.descdata) < inote.descsz
15287 || ((size_t) (next - inote.descdata)
15288 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
15289 {
55be8fd0
NC
15290 warn (_("\
15291malformed note encountered in section %s whilst scanning for build-id note\n"),
15292 printable_section_name (filedata, shdr));
f761cb13 15293 free (enote);
301a9420
AM
15294 continue;
15295 }
15296
15297 /* Check if this is the build-id note. If so then convert the build-id
15298 bytes to a hex string. */
15299 if (inote.namesz > 0
24d127aa 15300 && startswith (inote.namedata, "GNU")
301a9420
AM
15301 && inote.type == NT_GNU_BUILD_ID)
15302 {
15303 unsigned long j;
15304 char * build_id;
15305
15306 build_id = malloc (inote.descsz * 2 + 1);
15307 if (build_id == NULL)
f761cb13
AM
15308 {
15309 free (enote);
15310 return NULL;
15311 }
301a9420
AM
15312
15313 for (j = 0; j < inote.descsz; ++j)
15314 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
15315 build_id[inote.descsz * 2] = '\0';
f761cb13 15316 free (enote);
301a9420 15317
55be8fd0 15318 return (unsigned char *) build_id;
301a9420 15319 }
f761cb13 15320 free (enote);
301a9420
AM
15321 }
15322
15323 return NULL;
15324}
15325#endif /* HAVE_LIBDEBUGINFOD */
15326
657d0d47
CC
15327/* If this is not NULL, load_debug_section will only look for sections
15328 within the list of sections given here. */
32ec8896 15329static unsigned int * section_subset = NULL;
657d0d47 15330
015dc7e1 15331bool
dda8d76d 15332load_debug_section (enum dwarf_section_display_enum debug, void * data)
d966045b 15333{
2cf0635d
NC
15334 struct dwarf_section * section = &debug_displays [debug].section;
15335 Elf_Internal_Shdr * sec;
dda8d76d
NC
15336 Filedata * filedata = (Filedata *) data;
15337
f425ec66
NC
15338 /* Without section headers we cannot find any sections. */
15339 if (filedata->section_headers == NULL)
015dc7e1 15340 return false;
f425ec66 15341
9c1ce108
AM
15342 if (filedata->string_table == NULL
15343 && filedata->file_header.e_shstrndx != SHN_UNDEF
15344 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
dda8d76d
NC
15345 {
15346 Elf_Internal_Shdr * strs;
15347
15348 /* Read in the string table, so that we have section names to scan. */
15349 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
15350
4dff97b2 15351 if (strs != NULL && strs->sh_size != 0)
dda8d76d 15352 {
9c1ce108
AM
15353 filedata->string_table
15354 = (char *) get_data (NULL, filedata, strs->sh_offset,
15355 1, strs->sh_size, _("string table"));
dda8d76d 15356
9c1ce108
AM
15357 filedata->string_table_length
15358 = filedata->string_table != NULL ? strs->sh_size : 0;
dda8d76d
NC
15359 }
15360 }
d966045b
DJ
15361
15362 /* Locate the debug section. */
dda8d76d 15363 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
d966045b
DJ
15364 if (sec != NULL)
15365 section->name = section->uncompressed_name;
15366 else
15367 {
dda8d76d 15368 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
d966045b
DJ
15369 if (sec != NULL)
15370 section->name = section->compressed_name;
15371 }
15372 if (sec == NULL)
015dc7e1 15373 return false;
d966045b 15374
657d0d47
CC
15375 /* If we're loading from a subset of sections, and we've loaded
15376 a section matching this name before, it's likely that it's a
15377 different one. */
15378 if (section_subset != NULL)
15379 free_debug_section (debug);
15380
dda8d76d 15381 return load_specific_debug_section (debug, sec, data);
d966045b
DJ
15382}
15383
19e6b90e
L
15384void
15385free_debug_section (enum dwarf_section_display_enum debug)
1007acb3 15386{
2cf0635d 15387 struct dwarf_section * section = &debug_displays [debug].section;
1007acb3 15388
19e6b90e
L
15389 if (section->start == NULL)
15390 return;
1007acb3 15391
19e6b90e
L
15392 free ((char *) section->start);
15393 section->start = NULL;
15394 section->address = 0;
15395 section->size = 0;
a788aedd 15396
9db70fc3
AM
15397 free (section->reloc_info);
15398 section->reloc_info = NULL;
15399 section->num_relocs = 0;
1007acb3
L
15400}
15401
015dc7e1 15402static bool
dda8d76d 15403display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
1007acb3 15404{
b9e920ec 15405 char * name = SECTION_NAME_VALID (section) ? SECTION_NAME (section) : "";
dda8d76d 15406 const char * print_name = printable_section_name (filedata, section);
19e6b90e 15407 bfd_size_type length;
015dc7e1 15408 bool result = true;
3f5e193b 15409 int i;
1007acb3 15410
19e6b90e
L
15411 length = section->sh_size;
15412 if (length == 0)
1007acb3 15413 {
74e1a04b 15414 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
015dc7e1 15415 return true;
1007acb3 15416 }
5dff79d8
NC
15417 if (section->sh_type == SHT_NOBITS)
15418 {
15419 /* There is no point in dumping the contents of a debugging section
15420 which has the NOBITS type - the bits in the file will be random.
15421 This can happen when a file containing a .eh_frame section is
15422 stripped with the --only-keep-debug command line option. */
74e1a04b
NC
15423 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
15424 print_name);
015dc7e1 15425 return false;
5dff79d8 15426 }
1007acb3 15427
24d127aa 15428 if (startswith (name, ".gnu.linkonce.wi."))
19e6b90e 15429 name = ".debug_info";
1007acb3 15430
19e6b90e
L
15431 /* See if we know how to display the contents of this section. */
15432 for (i = 0; i < max; i++)
d85bf2ba
NC
15433 {
15434 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
15435 struct dwarf_section_display * display = debug_displays + i;
15436 struct dwarf_section * sec = & display->section;
d966045b 15437
d85bf2ba 15438 if (streq (sec->uncompressed_name, name)
24d127aa 15439 || (id == line && startswith (name, ".debug_line."))
d85bf2ba
NC
15440 || streq (sec->compressed_name, name))
15441 {
015dc7e1 15442 bool secondary = (section != find_section (filedata, name));
1007acb3 15443
d85bf2ba
NC
15444 if (secondary)
15445 free_debug_section (id);
dda8d76d 15446
24d127aa 15447 if (i == line && startswith (name, ".debug_line."))
d85bf2ba
NC
15448 sec->name = name;
15449 else if (streq (sec->uncompressed_name, name))
15450 sec->name = sec->uncompressed_name;
15451 else
15452 sec->name = sec->compressed_name;
657d0d47 15453
d85bf2ba
NC
15454 if (load_specific_debug_section (id, section, filedata))
15455 {
15456 /* If this debug section is part of a CU/TU set in a .dwp file,
15457 restrict load_debug_section to the sections in that set. */
15458 section_subset = find_cu_tu_set (filedata, shndx);
1007acb3 15459
d85bf2ba 15460 result &= display->display (sec, filedata);
657d0d47 15461
d85bf2ba 15462 section_subset = NULL;
1007acb3 15463
44266f36 15464 if (secondary || (id != info && id != abbrev && id != debug_addr))
d85bf2ba
NC
15465 free_debug_section (id);
15466 }
15467 break;
15468 }
15469 }
1007acb3 15470
19e6b90e 15471 if (i == max)
1007acb3 15472 {
74e1a04b 15473 printf (_("Unrecognized debug section: %s\n"), print_name);
015dc7e1 15474 result = false;
1007acb3
L
15475 }
15476
19e6b90e 15477 return result;
5b18a4bc 15478}
103f02d3 15479
aef1f6d0
DJ
15480/* Set DUMP_SECTS for all sections where dumps were requested
15481 based on section name. */
15482
15483static void
dda8d76d 15484initialise_dumps_byname (Filedata * filedata)
aef1f6d0 15485{
2cf0635d 15486 struct dump_list_entry * cur;
aef1f6d0
DJ
15487
15488 for (cur = dump_sects_byname; cur; cur = cur->next)
15489 {
15490 unsigned int i;
015dc7e1 15491 bool any = false;
aef1f6d0 15492
dda8d76d 15493 for (i = 0; i < filedata->file_header.e_shnum; i++)
b9e920ec
AM
15494 if (SECTION_NAME_VALID (filedata->section_headers + i)
15495 && streq (SECTION_NAME (filedata->section_headers + i), cur->name))
aef1f6d0 15496 {
6431e409 15497 request_dump_bynumber (&filedata->dump, i, cur->type);
015dc7e1 15498 any = true;
aef1f6d0
DJ
15499 }
15500
835f2fae
NC
15501 if (!any && !filedata->is_separate)
15502 warn (_("Section '%s' was not dumped because it does not exist\n"),
15503 cur->name);
aef1f6d0
DJ
15504 }
15505}
15506
015dc7e1 15507static bool
dda8d76d 15508process_section_contents (Filedata * filedata)
5b18a4bc 15509{
2cf0635d 15510 Elf_Internal_Shdr * section;
19e6b90e 15511 unsigned int i;
015dc7e1 15512 bool res = true;
103f02d3 15513
19e6b90e 15514 if (! do_dump)
015dc7e1 15515 return true;
103f02d3 15516
dda8d76d 15517 initialise_dumps_byname (filedata);
aef1f6d0 15518
dda8d76d 15519 for (i = 0, section = filedata->section_headers;
6431e409 15520 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
19e6b90e
L
15521 i++, section++)
15522 {
6431e409 15523 dump_type dump = filedata->dump.dump_sects[i];
dda8d76d 15524
d6bfbc39
NC
15525 if (filedata->is_separate && ! process_links)
15526 dump &= DEBUG_DUMP;
15527
19e6b90e 15528#ifdef SUPPORT_DISASSEMBLY
dda8d76d
NC
15529 if (dump & DISASS_DUMP)
15530 {
15531 if (! disassemble_section (section, filedata))
015dc7e1 15532 res = false;
dda8d76d 15533 }
19e6b90e 15534#endif
dda8d76d 15535 if (dump & HEX_DUMP)
32ec8896 15536 {
015dc7e1
AM
15537 if (! dump_section_as_bytes (section, filedata, false))
15538 res = false;
32ec8896 15539 }
103f02d3 15540
dda8d76d 15541 if (dump & RELOC_DUMP)
32ec8896 15542 {
015dc7e1
AM
15543 if (! dump_section_as_bytes (section, filedata, true))
15544 res = false;
32ec8896 15545 }
09c11c86 15546
dda8d76d 15547 if (dump & STRING_DUMP)
32ec8896 15548 {
dda8d76d 15549 if (! dump_section_as_strings (section, filedata))
015dc7e1 15550 res = false;
32ec8896 15551 }
cf13d699 15552
dda8d76d 15553 if (dump & DEBUG_DUMP)
32ec8896 15554 {
dda8d76d 15555 if (! display_debug_section (i, section, filedata))
015dc7e1 15556 res = false;
32ec8896 15557 }
7d9813f1 15558
094e34f2 15559#ifdef ENABLE_LIBCTF
7d9813f1
NA
15560 if (dump & CTF_DUMP)
15561 {
15562 if (! dump_section_as_ctf (section, filedata))
015dc7e1 15563 res = false;
7d9813f1 15564 }
094e34f2 15565#endif
5b18a4bc 15566 }
103f02d3 15567
835f2fae 15568 if (! filedata->is_separate)
0ee3043f 15569 {
835f2fae
NC
15570 /* Check to see if the user requested a
15571 dump of a section that does not exist. */
15572 for (; i < filedata->dump.num_dump_sects; i++)
15573 if (filedata->dump.dump_sects[i])
15574 {
ca0e11aa 15575 warn (_("Section %d was not dumped because it does not exist!\n"), i);
015dc7e1 15576 res = false;
835f2fae 15577 }
0ee3043f 15578 }
32ec8896
NC
15579
15580 return res;
5b18a4bc 15581}
103f02d3 15582
5b18a4bc 15583static void
19e6b90e 15584process_mips_fpe_exception (int mask)
5b18a4bc 15585{
19e6b90e
L
15586 if (mask)
15587 {
015dc7e1 15588 bool first = true;
32ec8896 15589
19e6b90e 15590 if (mask & OEX_FPU_INEX)
015dc7e1 15591 fputs ("INEX", stdout), first = false;
19e6b90e 15592 if (mask & OEX_FPU_UFLO)
015dc7e1 15593 printf ("%sUFLO", first ? "" : "|"), first = false;
19e6b90e 15594 if (mask & OEX_FPU_OFLO)
015dc7e1 15595 printf ("%sOFLO", first ? "" : "|"), first = false;
19e6b90e 15596 if (mask & OEX_FPU_DIV0)
015dc7e1 15597 printf ("%sDIV0", first ? "" : "|"), first = false;
19e6b90e
L
15598 if (mask & OEX_FPU_INVAL)
15599 printf ("%sINVAL", first ? "" : "|");
15600 }
5b18a4bc 15601 else
19e6b90e 15602 fputs ("0", stdout);
5b18a4bc 15603}
103f02d3 15604
f6f0e17b
NC
15605/* Display's the value of TAG at location P. If TAG is
15606 greater than 0 it is assumed to be an unknown tag, and
15607 a message is printed to this effect. Otherwise it is
15608 assumed that a message has already been printed.
15609
15610 If the bottom bit of TAG is set it assumed to have a
15611 string value, otherwise it is assumed to have an integer
15612 value.
15613
15614 Returns an updated P pointing to the first unread byte
15615 beyond the end of TAG's value.
15616
15617 Reads at or beyond END will not be made. */
15618
15619static unsigned char *
60abdbed 15620display_tag_value (signed int tag,
f6f0e17b
NC
15621 unsigned char * p,
15622 const unsigned char * const end)
15623{
15624 unsigned long val;
15625
15626 if (tag > 0)
15627 printf (" Tag_unknown_%d: ", tag);
15628
15629 if (p >= end)
15630 {
4082ef84 15631 warn (_("<corrupt tag>\n"));
f6f0e17b
NC
15632 }
15633 else if (tag & 1)
15634 {
071436c6
NC
15635 /* PR 17531 file: 027-19978-0.004. */
15636 size_t maxlen = (end - p) - 1;
15637
15638 putchar ('"');
4082ef84
NC
15639 if (maxlen > 0)
15640 {
15641 print_symbol ((int) maxlen, (const char *) p);
15642 p += strnlen ((char *) p, maxlen) + 1;
15643 }
15644 else
15645 {
15646 printf (_("<corrupt string tag>"));
15647 p = (unsigned char *) end;
15648 }
071436c6 15649 printf ("\"\n");
f6f0e17b
NC
15650 }
15651 else
15652 {
cd30bcef 15653 READ_ULEB (val, p, end);
f6f0e17b
NC
15654 printf ("%ld (0x%lx)\n", val, val);
15655 }
15656
4082ef84 15657 assert (p <= end);
f6f0e17b
NC
15658 return p;
15659}
15660
53a346d8
CZ
15661/* ARC ABI attributes section. */
15662
15663static unsigned char *
15664display_arc_attribute (unsigned char * p,
15665 const unsigned char * const end)
15666{
15667 unsigned int tag;
53a346d8
CZ
15668 unsigned int val;
15669
cd30bcef 15670 READ_ULEB (tag, p, end);
53a346d8
CZ
15671
15672 switch (tag)
15673 {
15674 case Tag_ARC_PCS_config:
cd30bcef 15675 READ_ULEB (val, p, end);
53a346d8
CZ
15676 printf (" Tag_ARC_PCS_config: ");
15677 switch (val)
15678 {
15679 case 0:
15680 printf (_("Absent/Non standard\n"));
15681 break;
15682 case 1:
15683 printf (_("Bare metal/mwdt\n"));
15684 break;
15685 case 2:
15686 printf (_("Bare metal/newlib\n"));
15687 break;
15688 case 3:
15689 printf (_("Linux/uclibc\n"));
15690 break;
15691 case 4:
15692 printf (_("Linux/glibc\n"));
15693 break;
15694 default:
15695 printf (_("Unknown\n"));
15696 break;
15697 }
15698 break;
15699
15700 case Tag_ARC_CPU_base:
cd30bcef 15701 READ_ULEB (val, p, end);
53a346d8
CZ
15702 printf (" Tag_ARC_CPU_base: ");
15703 switch (val)
15704 {
15705 default:
15706 case TAG_CPU_NONE:
15707 printf (_("Absent\n"));
15708 break;
15709 case TAG_CPU_ARC6xx:
15710 printf ("ARC6xx\n");
15711 break;
15712 case TAG_CPU_ARC7xx:
15713 printf ("ARC7xx\n");
15714 break;
15715 case TAG_CPU_ARCEM:
15716 printf ("ARCEM\n");
15717 break;
15718 case TAG_CPU_ARCHS:
15719 printf ("ARCHS\n");
15720 break;
15721 }
15722 break;
15723
15724 case Tag_ARC_CPU_variation:
cd30bcef 15725 READ_ULEB (val, p, end);
53a346d8
CZ
15726 printf (" Tag_ARC_CPU_variation: ");
15727 switch (val)
15728 {
15729 default:
15730 if (val > 0 && val < 16)
53a346d8 15731 printf ("Core%d\n", val);
d8cbc93b
JL
15732 else
15733 printf ("Unknown\n");
15734 break;
15735
53a346d8
CZ
15736 case 0:
15737 printf (_("Absent\n"));
15738 break;
15739 }
15740 break;
15741
15742 case Tag_ARC_CPU_name:
15743 printf (" Tag_ARC_CPU_name: ");
15744 p = display_tag_value (-1, p, end);
15745 break;
15746
15747 case Tag_ARC_ABI_rf16:
cd30bcef 15748 READ_ULEB (val, p, end);
53a346d8
CZ
15749 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
15750 break;
15751
15752 case Tag_ARC_ABI_osver:
cd30bcef 15753 READ_ULEB (val, p, end);
53a346d8
CZ
15754 printf (" Tag_ARC_ABI_osver: v%d\n", val);
15755 break;
15756
15757 case Tag_ARC_ABI_pic:
15758 case Tag_ARC_ABI_sda:
cd30bcef 15759 READ_ULEB (val, p, end);
53a346d8
CZ
15760 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
15761 : " Tag_ARC_ABI_pic: ");
15762 switch (val)
15763 {
15764 case 0:
15765 printf (_("Absent\n"));
15766 break;
15767 case 1:
15768 printf ("MWDT\n");
15769 break;
15770 case 2:
15771 printf ("GNU\n");
15772 break;
15773 default:
15774 printf (_("Unknown\n"));
15775 break;
15776 }
15777 break;
15778
15779 case Tag_ARC_ABI_tls:
cd30bcef 15780 READ_ULEB (val, p, end);
53a346d8
CZ
15781 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
15782 break;
15783
15784 case Tag_ARC_ABI_enumsize:
cd30bcef 15785 READ_ULEB (val, p, end);
53a346d8
CZ
15786 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
15787 _("smallest"));
15788 break;
15789
15790 case Tag_ARC_ABI_exceptions:
cd30bcef 15791 READ_ULEB (val, p, end);
53a346d8
CZ
15792 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
15793 : _("default"));
15794 break;
15795
15796 case Tag_ARC_ABI_double_size:
cd30bcef 15797 READ_ULEB (val, p, end);
53a346d8
CZ
15798 printf (" Tag_ARC_ABI_double_size: %d\n", val);
15799 break;
15800
15801 case Tag_ARC_ISA_config:
15802 printf (" Tag_ARC_ISA_config: ");
15803 p = display_tag_value (-1, p, end);
15804 break;
15805
15806 case Tag_ARC_ISA_apex:
15807 printf (" Tag_ARC_ISA_apex: ");
15808 p = display_tag_value (-1, p, end);
15809 break;
15810
15811 case Tag_ARC_ISA_mpy_option:
cd30bcef 15812 READ_ULEB (val, p, end);
53a346d8
CZ
15813 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
15814 break;
15815
db1e1b45 15816 case Tag_ARC_ATR_version:
cd30bcef 15817 READ_ULEB (val, p, end);
db1e1b45 15818 printf (" Tag_ARC_ATR_version: %d\n", val);
15819 break;
15820
53a346d8
CZ
15821 default:
15822 return display_tag_value (tag & 1, p, end);
15823 }
15824
15825 return p;
15826}
15827
11c1ff18
PB
15828/* ARM EABI attributes section. */
15829typedef struct
15830{
70e99720 15831 unsigned int tag;
2cf0635d 15832 const char * name;
11c1ff18 15833 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
70e99720 15834 unsigned int type;
288f0ba2 15835 const char *const *table;
11c1ff18
PB
15836} arm_attr_public_tag;
15837
288f0ba2 15838static const char *const arm_attr_tag_CPU_arch[] =
11c1ff18 15839 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
ced40572 15840 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
031254f2 15841 "v8-M.mainline", "", "", "", "v8.1-M.mainline"};
288f0ba2
AM
15842static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
15843static const char *const arm_attr_tag_THUMB_ISA_use[] =
4ed7ed8d 15844 {"No", "Thumb-1", "Thumb-2", "Yes"};
288f0ba2 15845static const char *const arm_attr_tag_FP_arch[] =
bca38921 15846 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
a715796b 15847 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
288f0ba2
AM
15848static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
15849static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
9411fd44
MW
15850 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
15851 "NEON for ARMv8.1"};
288f0ba2 15852static const char *const arm_attr_tag_PCS_config[] =
11c1ff18
PB
15853 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
15854 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
288f0ba2 15855static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
11c1ff18 15856 {"V6", "SB", "TLS", "Unused"};
288f0ba2 15857static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
11c1ff18 15858 {"Absolute", "PC-relative", "SB-relative", "None"};
288f0ba2 15859static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
11c1ff18 15860 {"Absolute", "PC-relative", "None"};
288f0ba2 15861static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
11c1ff18 15862 {"None", "direct", "GOT-indirect"};
288f0ba2 15863static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
11c1ff18 15864 {"None", "??? 1", "2", "??? 3", "4"};
288f0ba2
AM
15865static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
15866static const char *const arm_attr_tag_ABI_FP_denormal[] =
f5f53991 15867 {"Unused", "Needed", "Sign only"};
288f0ba2
AM
15868static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
15869static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
15870static const char *const arm_attr_tag_ABI_FP_number_model[] =
11c1ff18 15871 {"Unused", "Finite", "RTABI", "IEEE 754"};
288f0ba2 15872static const char *const arm_attr_tag_ABI_enum_size[] =
11c1ff18 15873 {"Unused", "small", "int", "forced to int"};
288f0ba2 15874static const char *const arm_attr_tag_ABI_HardFP_use[] =
99654aaf 15875 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
288f0ba2 15876static const char *const arm_attr_tag_ABI_VFP_args[] =
5c294fee 15877 {"AAPCS", "VFP registers", "custom", "compatible"};
288f0ba2 15878static const char *const arm_attr_tag_ABI_WMMX_args[] =
11c1ff18 15879 {"AAPCS", "WMMX registers", "custom"};
288f0ba2 15880static const char *const arm_attr_tag_ABI_optimization_goals[] =
11c1ff18
PB
15881 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
15882 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
288f0ba2 15883static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
11c1ff18
PB
15884 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
15885 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
288f0ba2
AM
15886static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
15887static const char *const arm_attr_tag_FP_HP_extension[] =
8e79c3df 15888 {"Not Allowed", "Allowed"};
288f0ba2 15889static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
8e79c3df 15890 {"None", "IEEE 754", "Alternative Format"};
288f0ba2 15891static const char *const arm_attr_tag_DSP_extension[] =
15afaa63 15892 {"Follow architecture", "Allowed"};
288f0ba2 15893static const char *const arm_attr_tag_MPextension_use[] =
cd21e546 15894 {"Not Allowed", "Allowed"};
288f0ba2 15895static const char *const arm_attr_tag_DIV_use[] =
dd24e3da 15896 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
cd21e546 15897 "Allowed in v7-A with integer division extension"};
288f0ba2
AM
15898static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
15899static const char *const arm_attr_tag_Virtualization_use[] =
dd24e3da 15900 {"Not Allowed", "TrustZone", "Virtualization Extensions",
cd21e546 15901 "TrustZone and Virtualization Extensions"};
288f0ba2 15902static const char *const arm_attr_tag_MPextension_use_legacy[] =
f5f53991 15903 {"Not Allowed", "Allowed"};
11c1ff18 15904
288f0ba2 15905static const char *const arm_attr_tag_MVE_arch[] =
a7ad558c
AV
15906 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
15907
11c1ff18
PB
15908#define LOOKUP(id, name) \
15909 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
d70c5fc7 15910static arm_attr_public_tag arm_attr_public_tags[] =
11c1ff18
PB
15911{
15912 {4, "CPU_raw_name", 1, NULL},
15913 {5, "CPU_name", 1, NULL},
15914 LOOKUP(6, CPU_arch),
15915 {7, "CPU_arch_profile", 0, NULL},
15916 LOOKUP(8, ARM_ISA_use),
15917 LOOKUP(9, THUMB_ISA_use),
75375b3e 15918 LOOKUP(10, FP_arch),
11c1ff18 15919 LOOKUP(11, WMMX_arch),
f5f53991
AS
15920 LOOKUP(12, Advanced_SIMD_arch),
15921 LOOKUP(13, PCS_config),
11c1ff18
PB
15922 LOOKUP(14, ABI_PCS_R9_use),
15923 LOOKUP(15, ABI_PCS_RW_data),
f5f53991 15924 LOOKUP(16, ABI_PCS_RO_data),
11c1ff18
PB
15925 LOOKUP(17, ABI_PCS_GOT_use),
15926 LOOKUP(18, ABI_PCS_wchar_t),
15927 LOOKUP(19, ABI_FP_rounding),
15928 LOOKUP(20, ABI_FP_denormal),
15929 LOOKUP(21, ABI_FP_exceptions),
15930 LOOKUP(22, ABI_FP_user_exceptions),
15931 LOOKUP(23, ABI_FP_number_model),
75375b3e
MGD
15932 {24, "ABI_align_needed", 0, NULL},
15933 {25, "ABI_align_preserved", 0, NULL},
11c1ff18
PB
15934 LOOKUP(26, ABI_enum_size),
15935 LOOKUP(27, ABI_HardFP_use),
15936 LOOKUP(28, ABI_VFP_args),
15937 LOOKUP(29, ABI_WMMX_args),
15938 LOOKUP(30, ABI_optimization_goals),
15939 LOOKUP(31, ABI_FP_optimization_goals),
8e79c3df 15940 {32, "compatibility", 0, NULL},
f5f53991 15941 LOOKUP(34, CPU_unaligned_access),
75375b3e 15942 LOOKUP(36, FP_HP_extension),
8e79c3df 15943 LOOKUP(38, ABI_FP_16bit_format),
cd21e546
MGD
15944 LOOKUP(42, MPextension_use),
15945 LOOKUP(44, DIV_use),
15afaa63 15946 LOOKUP(46, DSP_extension),
a7ad558c 15947 LOOKUP(48, MVE_arch),
f5f53991
AS
15948 {64, "nodefaults", 0, NULL},
15949 {65, "also_compatible_with", 0, NULL},
15950 LOOKUP(66, T2EE_use),
15951 {67, "conformance", 1, NULL},
15952 LOOKUP(68, Virtualization_use),
cd21e546 15953 LOOKUP(70, MPextension_use_legacy)
11c1ff18
PB
15954};
15955#undef LOOKUP
15956
11c1ff18 15957static unsigned char *
f6f0e17b
NC
15958display_arm_attribute (unsigned char * p,
15959 const unsigned char * const end)
11c1ff18 15960{
70e99720 15961 unsigned int tag;
70e99720 15962 unsigned int val;
2cf0635d 15963 arm_attr_public_tag * attr;
11c1ff18 15964 unsigned i;
70e99720 15965 unsigned int type;
11c1ff18 15966
cd30bcef 15967 READ_ULEB (tag, p, end);
11c1ff18 15968 attr = NULL;
2cf0635d 15969 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
11c1ff18
PB
15970 {
15971 if (arm_attr_public_tags[i].tag == tag)
15972 {
15973 attr = &arm_attr_public_tags[i];
15974 break;
15975 }
15976 }
15977
15978 if (attr)
15979 {
15980 printf (" Tag_%s: ", attr->name);
15981 switch (attr->type)
15982 {
15983 case 0:
15984 switch (tag)
15985 {
15986 case 7: /* Tag_CPU_arch_profile. */
cd30bcef 15987 READ_ULEB (val, p, end);
11c1ff18
PB
15988 switch (val)
15989 {
2b692964
NC
15990 case 0: printf (_("None\n")); break;
15991 case 'A': printf (_("Application\n")); break;
15992 case 'R': printf (_("Realtime\n")); break;
15993 case 'M': printf (_("Microcontroller\n")); break;
15994 case 'S': printf (_("Application or Realtime\n")); break;
11c1ff18
PB
15995 default: printf ("??? (%d)\n", val); break;
15996 }
15997 break;
15998
75375b3e 15999 case 24: /* Tag_align_needed. */
cd30bcef 16000 READ_ULEB (val, p, end);
75375b3e
MGD
16001 switch (val)
16002 {
2b692964
NC
16003 case 0: printf (_("None\n")); break;
16004 case 1: printf (_("8-byte\n")); break;
16005 case 2: printf (_("4-byte\n")); break;
75375b3e
MGD
16006 case 3: printf ("??? 3\n"); break;
16007 default:
16008 if (val <= 12)
dd24e3da 16009 printf (_("8-byte and up to %d-byte extended\n"),
75375b3e
MGD
16010 1 << val);
16011 else
16012 printf ("??? (%d)\n", val);
16013 break;
16014 }
16015 break;
16016
16017 case 25: /* Tag_align_preserved. */
cd30bcef 16018 READ_ULEB (val, p, end);
75375b3e
MGD
16019 switch (val)
16020 {
2b692964
NC
16021 case 0: printf (_("None\n")); break;
16022 case 1: printf (_("8-byte, except leaf SP\n")); break;
16023 case 2: printf (_("8-byte\n")); break;
75375b3e
MGD
16024 case 3: printf ("??? 3\n"); break;
16025 default:
16026 if (val <= 12)
dd24e3da 16027 printf (_("8-byte and up to %d-byte extended\n"),
75375b3e
MGD
16028 1 << val);
16029 else
16030 printf ("??? (%d)\n", val);
16031 break;
16032 }
16033 break;
16034
11c1ff18 16035 case 32: /* Tag_compatibility. */
071436c6 16036 {
cd30bcef 16037 READ_ULEB (val, p, end);
071436c6 16038 printf (_("flag = %d, vendor = "), val);
4082ef84
NC
16039 if (p < end - 1)
16040 {
16041 size_t maxlen = (end - p) - 1;
16042
16043 print_symbol ((int) maxlen, (const char *) p);
16044 p += strnlen ((char *) p, maxlen) + 1;
16045 }
16046 else
16047 {
16048 printf (_("<corrupt>"));
16049 p = (unsigned char *) end;
16050 }
071436c6 16051 putchar ('\n');
071436c6 16052 }
11c1ff18
PB
16053 break;
16054
f5f53991 16055 case 64: /* Tag_nodefaults. */
541a3cbd
NC
16056 /* PR 17531: file: 001-505008-0.01. */
16057 if (p < end)
16058 p++;
2b692964 16059 printf (_("True\n"));
f5f53991
AS
16060 break;
16061
16062 case 65: /* Tag_also_compatible_with. */
cd30bcef 16063 READ_ULEB (val, p, end);
f5f53991
AS
16064 if (val == 6 /* Tag_CPU_arch. */)
16065 {
cd30bcef 16066 READ_ULEB (val, p, end);
071436c6 16067 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
f5f53991
AS
16068 printf ("??? (%d)\n", val);
16069 else
16070 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
16071 }
16072 else
16073 printf ("???\n");
071436c6
NC
16074 while (p < end && *(p++) != '\0' /* NUL terminator. */)
16075 ;
f5f53991
AS
16076 break;
16077
11c1ff18 16078 default:
bee0ee85
NC
16079 printf (_("<unknown: %d>\n"), tag);
16080 break;
11c1ff18
PB
16081 }
16082 return p;
16083
16084 case 1:
f6f0e17b 16085 return display_tag_value (-1, p, end);
11c1ff18 16086 case 2:
f6f0e17b 16087 return display_tag_value (0, p, end);
11c1ff18
PB
16088
16089 default:
16090 assert (attr->type & 0x80);
cd30bcef 16091 READ_ULEB (val, p, end);
11c1ff18
PB
16092 type = attr->type & 0x7f;
16093 if (val >= type)
16094 printf ("??? (%d)\n", val);
16095 else
16096 printf ("%s\n", attr->table[val]);
16097 return p;
16098 }
16099 }
11c1ff18 16100
f6f0e17b 16101 return display_tag_value (tag, p, end);
11c1ff18
PB
16102}
16103
104d59d1 16104static unsigned char *
60bca95a 16105display_gnu_attribute (unsigned char * p,
60abdbed 16106 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
f6f0e17b 16107 const unsigned char * const end)
104d59d1 16108{
cd30bcef 16109 unsigned int tag;
60abdbed 16110 unsigned int val;
104d59d1 16111
cd30bcef 16112 READ_ULEB (tag, p, end);
104d59d1
JM
16113
16114 /* Tag_compatibility is the only generic GNU attribute defined at
16115 present. */
16116 if (tag == 32)
16117 {
cd30bcef 16118 READ_ULEB (val, p, end);
071436c6
NC
16119
16120 printf (_("flag = %d, vendor = "), val);
f6f0e17b
NC
16121 if (p == end)
16122 {
071436c6 16123 printf (_("<corrupt>\n"));
f6f0e17b
NC
16124 warn (_("corrupt vendor attribute\n"));
16125 }
16126 else
16127 {
4082ef84
NC
16128 if (p < end - 1)
16129 {
16130 size_t maxlen = (end - p) - 1;
071436c6 16131
4082ef84
NC
16132 print_symbol ((int) maxlen, (const char *) p);
16133 p += strnlen ((char *) p, maxlen) + 1;
16134 }
16135 else
16136 {
16137 printf (_("<corrupt>"));
16138 p = (unsigned char *) end;
16139 }
071436c6 16140 putchar ('\n');
f6f0e17b 16141 }
104d59d1
JM
16142 return p;
16143 }
16144
16145 if ((tag & 2) == 0 && display_proc_gnu_attribute)
f6f0e17b 16146 return display_proc_gnu_attribute (p, tag, end);
104d59d1 16147
f6f0e17b 16148 return display_tag_value (tag, p, end);
104d59d1
JM
16149}
16150
85f7484a
PB
16151static unsigned char *
16152display_m68k_gnu_attribute (unsigned char * p,
16153 unsigned int tag,
16154 const unsigned char * const end)
16155{
16156 unsigned int val;
16157
16158 if (tag == Tag_GNU_M68K_ABI_FP)
16159 {
16160 printf (" Tag_GNU_M68K_ABI_FP: ");
16161 if (p == end)
16162 {
16163 printf (_("<corrupt>\n"));
16164 return p;
16165 }
16166 READ_ULEB (val, p, end);
16167
16168 if (val > 3)
16169 printf ("(%#x), ", val);
16170
16171 switch (val & 3)
16172 {
16173 case 0:
16174 printf (_("unspecified hard/soft float\n"));
16175 break;
16176 case 1:
16177 printf (_("hard float\n"));
16178 break;
16179 case 2:
16180 printf (_("soft float\n"));
16181 break;
16182 }
16183 return p;
16184 }
16185
16186 return display_tag_value (tag & 1, p, end);
16187}
16188
34c8bcba 16189static unsigned char *
f6f0e17b 16190display_power_gnu_attribute (unsigned char * p,
60abdbed 16191 unsigned int tag,
f6f0e17b 16192 const unsigned char * const end)
34c8bcba 16193{
005d79fd 16194 unsigned int val;
34c8bcba
JM
16195
16196 if (tag == Tag_GNU_Power_ABI_FP)
16197 {
34c8bcba 16198 printf (" Tag_GNU_Power_ABI_FP: ");
cd30bcef 16199 if (p == end)
005d79fd
AM
16200 {
16201 printf (_("<corrupt>\n"));
16202 return p;
16203 }
cd30bcef 16204 READ_ULEB (val, p, end);
60bca95a 16205
005d79fd
AM
16206 if (val > 15)
16207 printf ("(%#x), ", val);
16208
16209 switch (val & 3)
34c8bcba
JM
16210 {
16211 case 0:
005d79fd 16212 printf (_("unspecified hard/soft float, "));
34c8bcba
JM
16213 break;
16214 case 1:
005d79fd 16215 printf (_("hard float, "));
34c8bcba
JM
16216 break;
16217 case 2:
005d79fd 16218 printf (_("soft float, "));
34c8bcba 16219 break;
3c7b9897 16220 case 3:
005d79fd 16221 printf (_("single-precision hard float, "));
3c7b9897 16222 break;
005d79fd
AM
16223 }
16224
16225 switch (val & 0xC)
16226 {
16227 case 0:
16228 printf (_("unspecified long double\n"));
16229 break;
16230 case 4:
16231 printf (_("128-bit IBM long double\n"));
16232 break;
16233 case 8:
16234 printf (_("64-bit long double\n"));
16235 break;
16236 case 12:
16237 printf (_("128-bit IEEE long double\n"));
34c8bcba
JM
16238 break;
16239 }
16240 return p;
005d79fd 16241 }
34c8bcba 16242
c6e65352
DJ
16243 if (tag == Tag_GNU_Power_ABI_Vector)
16244 {
c6e65352 16245 printf (" Tag_GNU_Power_ABI_Vector: ");
cd30bcef 16246 if (p == end)
005d79fd
AM
16247 {
16248 printf (_("<corrupt>\n"));
16249 return p;
16250 }
cd30bcef 16251 READ_ULEB (val, p, end);
005d79fd
AM
16252
16253 if (val > 3)
16254 printf ("(%#x), ", val);
16255
16256 switch (val & 3)
c6e65352
DJ
16257 {
16258 case 0:
005d79fd 16259 printf (_("unspecified\n"));
c6e65352
DJ
16260 break;
16261 case 1:
005d79fd 16262 printf (_("generic\n"));
c6e65352
DJ
16263 break;
16264 case 2:
16265 printf ("AltiVec\n");
16266 break;
16267 case 3:
16268 printf ("SPE\n");
16269 break;
c6e65352
DJ
16270 }
16271 return p;
005d79fd 16272 }
c6e65352 16273
f82e0623
NF
16274 if (tag == Tag_GNU_Power_ABI_Struct_Return)
16275 {
005d79fd 16276 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
cd30bcef 16277 if (p == end)
f6f0e17b 16278 {
005d79fd 16279 printf (_("<corrupt>\n"));
f6f0e17b
NC
16280 return p;
16281 }
cd30bcef 16282 READ_ULEB (val, p, end);
0b4362b0 16283
005d79fd
AM
16284 if (val > 2)
16285 printf ("(%#x), ", val);
16286
16287 switch (val & 3)
16288 {
16289 case 0:
16290 printf (_("unspecified\n"));
16291 break;
16292 case 1:
16293 printf ("r3/r4\n");
16294 break;
16295 case 2:
16296 printf (_("memory\n"));
16297 break;
16298 case 3:
16299 printf ("???\n");
16300 break;
16301 }
f82e0623
NF
16302 return p;
16303 }
16304
f6f0e17b 16305 return display_tag_value (tag & 1, p, end);
34c8bcba
JM
16306}
16307
643f7afb
AK
16308static unsigned char *
16309display_s390_gnu_attribute (unsigned char * p,
60abdbed 16310 unsigned int tag,
643f7afb
AK
16311 const unsigned char * const end)
16312{
cd30bcef 16313 unsigned int val;
643f7afb
AK
16314
16315 if (tag == Tag_GNU_S390_ABI_Vector)
16316 {
643f7afb 16317 printf (" Tag_GNU_S390_ABI_Vector: ");
cd30bcef 16318 READ_ULEB (val, p, end);
643f7afb
AK
16319
16320 switch (val)
16321 {
16322 case 0:
16323 printf (_("any\n"));
16324 break;
16325 case 1:
16326 printf (_("software\n"));
16327 break;
16328 case 2:
16329 printf (_("hardware\n"));
16330 break;
16331 default:
16332 printf ("??? (%d)\n", val);
16333 break;
16334 }
16335 return p;
16336 }
16337
16338 return display_tag_value (tag & 1, p, end);
16339}
16340
9e8c70f9 16341static void
60abdbed 16342display_sparc_hwcaps (unsigned int mask)
9e8c70f9
DM
16343{
16344 if (mask)
16345 {
015dc7e1 16346 bool first = true;
071436c6 16347
9e8c70f9 16348 if (mask & ELF_SPARC_HWCAP_MUL32)
015dc7e1 16349 fputs ("mul32", stdout), first = false;
9e8c70f9 16350 if (mask & ELF_SPARC_HWCAP_DIV32)
015dc7e1 16351 printf ("%sdiv32", first ? "" : "|"), first = false;
9e8c70f9 16352 if (mask & ELF_SPARC_HWCAP_FSMULD)
015dc7e1 16353 printf ("%sfsmuld", first ? "" : "|"), first = false;
9e8c70f9 16354 if (mask & ELF_SPARC_HWCAP_V8PLUS)
015dc7e1 16355 printf ("%sv8plus", first ? "" : "|"), first = false;
9e8c70f9 16356 if (mask & ELF_SPARC_HWCAP_POPC)
015dc7e1 16357 printf ("%spopc", first ? "" : "|"), first = false;
9e8c70f9 16358 if (mask & ELF_SPARC_HWCAP_VIS)
015dc7e1 16359 printf ("%svis", first ? "" : "|"), first = false;
9e8c70f9 16360 if (mask & ELF_SPARC_HWCAP_VIS2)
015dc7e1 16361 printf ("%svis2", first ? "" : "|"), first = false;
9e8c70f9 16362 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
015dc7e1 16363 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
9e8c70f9 16364 if (mask & ELF_SPARC_HWCAP_FMAF)
015dc7e1 16365 printf ("%sfmaf", first ? "" : "|"), first = false;
9e8c70f9 16366 if (mask & ELF_SPARC_HWCAP_VIS3)
015dc7e1 16367 printf ("%svis3", first ? "" : "|"), first = false;
9e8c70f9 16368 if (mask & ELF_SPARC_HWCAP_HPC)
015dc7e1 16369 printf ("%shpc", first ? "" : "|"), first = false;
9e8c70f9 16370 if (mask & ELF_SPARC_HWCAP_RANDOM)
015dc7e1 16371 printf ("%srandom", first ? "" : "|"), first = false;
9e8c70f9 16372 if (mask & ELF_SPARC_HWCAP_TRANS)
015dc7e1 16373 printf ("%strans", first ? "" : "|"), first = false;
9e8c70f9 16374 if (mask & ELF_SPARC_HWCAP_FJFMAU)
015dc7e1 16375 printf ("%sfjfmau", first ? "" : "|"), first = false;
9e8c70f9 16376 if (mask & ELF_SPARC_HWCAP_IMA)
015dc7e1 16377 printf ("%sima", first ? "" : "|"), first = false;
9e8c70f9 16378 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
015dc7e1 16379 printf ("%scspare", first ? "" : "|"), first = false;
9e8c70f9
DM
16380 }
16381 else
071436c6
NC
16382 fputc ('0', stdout);
16383 fputc ('\n', stdout);
9e8c70f9
DM
16384}
16385
3d68f91c 16386static void
60abdbed 16387display_sparc_hwcaps2 (unsigned int mask)
3d68f91c
JM
16388{
16389 if (mask)
16390 {
015dc7e1 16391 bool first = true;
071436c6 16392
3d68f91c 16393 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
015dc7e1 16394 fputs ("fjathplus", stdout), first = false;
3d68f91c 16395 if (mask & ELF_SPARC_HWCAP2_VIS3B)
015dc7e1 16396 printf ("%svis3b", first ? "" : "|"), first = false;
3d68f91c 16397 if (mask & ELF_SPARC_HWCAP2_ADP)
015dc7e1 16398 printf ("%sadp", first ? "" : "|"), first = false;
3d68f91c 16399 if (mask & ELF_SPARC_HWCAP2_SPARC5)
015dc7e1 16400 printf ("%ssparc5", first ? "" : "|"), first = false;
3d68f91c 16401 if (mask & ELF_SPARC_HWCAP2_MWAIT)
015dc7e1 16402 printf ("%smwait", first ? "" : "|"), first = false;
3d68f91c 16403 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
015dc7e1 16404 printf ("%sxmpmul", first ? "" : "|"), first = false;
3d68f91c 16405 if (mask & ELF_SPARC_HWCAP2_XMONT)
015dc7e1 16406 printf ("%sxmont2", first ? "" : "|"), first = false;
3d68f91c 16407 if (mask & ELF_SPARC_HWCAP2_NSEC)
015dc7e1 16408 printf ("%snsec", first ? "" : "|"), first = false;
3d68f91c 16409 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
015dc7e1 16410 printf ("%sfjathhpc", first ? "" : "|"), first = false;
3d68f91c 16411 if (mask & ELF_SPARC_HWCAP2_FJDES)
015dc7e1 16412 printf ("%sfjdes", first ? "" : "|"), first = false;
3d68f91c 16413 if (mask & ELF_SPARC_HWCAP2_FJAES)
015dc7e1 16414 printf ("%sfjaes", first ? "" : "|"), first = false;
3d68f91c
JM
16415 }
16416 else
071436c6
NC
16417 fputc ('0', stdout);
16418 fputc ('\n', stdout);
3d68f91c
JM
16419}
16420
9e8c70f9 16421static unsigned char *
f6f0e17b 16422display_sparc_gnu_attribute (unsigned char * p,
60abdbed 16423 unsigned int tag,
f6f0e17b 16424 const unsigned char * const end)
9e8c70f9 16425{
cd30bcef 16426 unsigned int val;
3d68f91c 16427
9e8c70f9
DM
16428 if (tag == Tag_GNU_Sparc_HWCAPS)
16429 {
cd30bcef 16430 READ_ULEB (val, p, end);
9e8c70f9 16431 printf (" Tag_GNU_Sparc_HWCAPS: ");
9e8c70f9
DM
16432 display_sparc_hwcaps (val);
16433 return p;
3d68f91c
JM
16434 }
16435 if (tag == Tag_GNU_Sparc_HWCAPS2)
16436 {
cd30bcef 16437 READ_ULEB (val, p, end);
3d68f91c
JM
16438 printf (" Tag_GNU_Sparc_HWCAPS2: ");
16439 display_sparc_hwcaps2 (val);
16440 return p;
16441 }
9e8c70f9 16442
f6f0e17b 16443 return display_tag_value (tag, p, end);
9e8c70f9
DM
16444}
16445
351cdf24 16446static void
32ec8896 16447print_mips_fp_abi_value (unsigned int val)
351cdf24
MF
16448{
16449 switch (val)
16450 {
16451 case Val_GNU_MIPS_ABI_FP_ANY:
16452 printf (_("Hard or soft float\n"));
16453 break;
16454 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16455 printf (_("Hard float (double precision)\n"));
16456 break;
16457 case Val_GNU_MIPS_ABI_FP_SINGLE:
16458 printf (_("Hard float (single precision)\n"));
16459 break;
16460 case Val_GNU_MIPS_ABI_FP_SOFT:
16461 printf (_("Soft float\n"));
16462 break;
16463 case Val_GNU_MIPS_ABI_FP_OLD_64:
16464 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16465 break;
16466 case Val_GNU_MIPS_ABI_FP_XX:
16467 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
16468 break;
16469 case Val_GNU_MIPS_ABI_FP_64:
16470 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
16471 break;
16472 case Val_GNU_MIPS_ABI_FP_64A:
16473 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16474 break;
3350cc01
CM
16475 case Val_GNU_MIPS_ABI_FP_NAN2008:
16476 printf (_("NaN 2008 compatibility\n"));
16477 break;
351cdf24
MF
16478 default:
16479 printf ("??? (%d)\n", val);
16480 break;
16481 }
16482}
16483
2cf19d5c 16484static unsigned char *
f6f0e17b 16485display_mips_gnu_attribute (unsigned char * p,
60abdbed 16486 unsigned int tag,
f6f0e17b 16487 const unsigned char * const end)
2cf19d5c 16488{
2cf19d5c
JM
16489 if (tag == Tag_GNU_MIPS_ABI_FP)
16490 {
32ec8896 16491 unsigned int val;
f6f0e17b 16492
2cf19d5c 16493 printf (" Tag_GNU_MIPS_ABI_FP: ");
cd30bcef 16494 READ_ULEB (val, p, end);
351cdf24 16495 print_mips_fp_abi_value (val);
2cf19d5c
JM
16496 return p;
16497 }
16498
a9f58168
CF
16499 if (tag == Tag_GNU_MIPS_ABI_MSA)
16500 {
32ec8896 16501 unsigned int val;
a9f58168 16502
a9f58168 16503 printf (" Tag_GNU_MIPS_ABI_MSA: ");
cd30bcef 16504 READ_ULEB (val, p, end);
a9f58168
CF
16505
16506 switch (val)
16507 {
16508 case Val_GNU_MIPS_ABI_MSA_ANY:
16509 printf (_("Any MSA or not\n"));
16510 break;
16511 case Val_GNU_MIPS_ABI_MSA_128:
16512 printf (_("128-bit MSA\n"));
16513 break;
16514 default:
16515 printf ("??? (%d)\n", val);
16516 break;
16517 }
16518 return p;
16519 }
16520
f6f0e17b 16521 return display_tag_value (tag & 1, p, end);
2cf19d5c
JM
16522}
16523
59e6276b 16524static unsigned char *
f6f0e17b
NC
16525display_tic6x_attribute (unsigned char * p,
16526 const unsigned char * const end)
59e6276b 16527{
60abdbed 16528 unsigned int tag;
cd30bcef 16529 unsigned int val;
59e6276b 16530
cd30bcef 16531 READ_ULEB (tag, p, end);
59e6276b
JM
16532
16533 switch (tag)
16534 {
75fa6dc1 16535 case Tag_ISA:
75fa6dc1 16536 printf (" Tag_ISA: ");
cd30bcef 16537 READ_ULEB (val, p, end);
59e6276b
JM
16538
16539 switch (val)
16540 {
75fa6dc1 16541 case C6XABI_Tag_ISA_none:
59e6276b
JM
16542 printf (_("None\n"));
16543 break;
75fa6dc1 16544 case C6XABI_Tag_ISA_C62X:
59e6276b
JM
16545 printf ("C62x\n");
16546 break;
75fa6dc1 16547 case C6XABI_Tag_ISA_C67X:
59e6276b
JM
16548 printf ("C67x\n");
16549 break;
75fa6dc1 16550 case C6XABI_Tag_ISA_C67XP:
59e6276b
JM
16551 printf ("C67x+\n");
16552 break;
75fa6dc1 16553 case C6XABI_Tag_ISA_C64X:
59e6276b
JM
16554 printf ("C64x\n");
16555 break;
75fa6dc1 16556 case C6XABI_Tag_ISA_C64XP:
59e6276b
JM
16557 printf ("C64x+\n");
16558 break;
75fa6dc1 16559 case C6XABI_Tag_ISA_C674X:
59e6276b
JM
16560 printf ("C674x\n");
16561 break;
16562 default:
16563 printf ("??? (%d)\n", val);
16564 break;
16565 }
16566 return p;
16567
87779176 16568 case Tag_ABI_wchar_t:
87779176 16569 printf (" Tag_ABI_wchar_t: ");
cd30bcef 16570 READ_ULEB (val, p, end);
87779176
JM
16571 switch (val)
16572 {
16573 case 0:
16574 printf (_("Not used\n"));
16575 break;
16576 case 1:
16577 printf (_("2 bytes\n"));
16578 break;
16579 case 2:
16580 printf (_("4 bytes\n"));
16581 break;
16582 default:
16583 printf ("??? (%d)\n", val);
16584 break;
16585 }
16586 return p;
16587
16588 case Tag_ABI_stack_align_needed:
87779176 16589 printf (" Tag_ABI_stack_align_needed: ");
cd30bcef 16590 READ_ULEB (val, p, end);
87779176
JM
16591 switch (val)
16592 {
16593 case 0:
16594 printf (_("8-byte\n"));
16595 break;
16596 case 1:
16597 printf (_("16-byte\n"));
16598 break;
16599 default:
16600 printf ("??? (%d)\n", val);
16601 break;
16602 }
16603 return p;
16604
16605 case Tag_ABI_stack_align_preserved:
cd30bcef 16606 READ_ULEB (val, p, end);
87779176
JM
16607 printf (" Tag_ABI_stack_align_preserved: ");
16608 switch (val)
16609 {
16610 case 0:
16611 printf (_("8-byte\n"));
16612 break;
16613 case 1:
16614 printf (_("16-byte\n"));
16615 break;
16616 default:
16617 printf ("??? (%d)\n", val);
16618 break;
16619 }
16620 return p;
16621
b5593623 16622 case Tag_ABI_DSBT:
cd30bcef 16623 READ_ULEB (val, p, end);
b5593623
JM
16624 printf (" Tag_ABI_DSBT: ");
16625 switch (val)
16626 {
16627 case 0:
16628 printf (_("DSBT addressing not used\n"));
16629 break;
16630 case 1:
16631 printf (_("DSBT addressing used\n"));
16632 break;
16633 default:
16634 printf ("??? (%d)\n", val);
16635 break;
16636 }
16637 return p;
16638
87779176 16639 case Tag_ABI_PID:
cd30bcef 16640 READ_ULEB (val, p, end);
87779176
JM
16641 printf (" Tag_ABI_PID: ");
16642 switch (val)
16643 {
16644 case 0:
16645 printf (_("Data addressing position-dependent\n"));
16646 break;
16647 case 1:
16648 printf (_("Data addressing position-independent, GOT near DP\n"));
16649 break;
16650 case 2:
16651 printf (_("Data addressing position-independent, GOT far from DP\n"));
16652 break;
16653 default:
16654 printf ("??? (%d)\n", val);
16655 break;
16656 }
16657 return p;
16658
16659 case Tag_ABI_PIC:
cd30bcef 16660 READ_ULEB (val, p, end);
87779176
JM
16661 printf (" Tag_ABI_PIC: ");
16662 switch (val)
16663 {
16664 case 0:
16665 printf (_("Code addressing position-dependent\n"));
16666 break;
16667 case 1:
16668 printf (_("Code addressing position-independent\n"));
16669 break;
16670 default:
16671 printf ("??? (%d)\n", val);
16672 break;
16673 }
16674 return p;
16675
16676 case Tag_ABI_array_object_alignment:
cd30bcef 16677 READ_ULEB (val, p, end);
87779176
JM
16678 printf (" Tag_ABI_array_object_alignment: ");
16679 switch (val)
16680 {
16681 case 0:
16682 printf (_("8-byte\n"));
16683 break;
16684 case 1:
16685 printf (_("4-byte\n"));
16686 break;
16687 case 2:
16688 printf (_("16-byte\n"));
16689 break;
16690 default:
16691 printf ("??? (%d)\n", val);
16692 break;
16693 }
16694 return p;
16695
16696 case Tag_ABI_array_object_align_expected:
cd30bcef 16697 READ_ULEB (val, p, end);
87779176
JM
16698 printf (" Tag_ABI_array_object_align_expected: ");
16699 switch (val)
16700 {
16701 case 0:
16702 printf (_("8-byte\n"));
16703 break;
16704 case 1:
16705 printf (_("4-byte\n"));
16706 break;
16707 case 2:
16708 printf (_("16-byte\n"));
16709 break;
16710 default:
16711 printf ("??? (%d)\n", val);
16712 break;
16713 }
16714 return p;
16715
3cbd1c06 16716 case Tag_ABI_compatibility:
071436c6 16717 {
cd30bcef 16718 READ_ULEB (val, p, end);
071436c6 16719 printf (" Tag_ABI_compatibility: ");
071436c6 16720 printf (_("flag = %d, vendor = "), val);
4082ef84
NC
16721 if (p < end - 1)
16722 {
16723 size_t maxlen = (end - p) - 1;
16724
16725 print_symbol ((int) maxlen, (const char *) p);
16726 p += strnlen ((char *) p, maxlen) + 1;
16727 }
16728 else
16729 {
16730 printf (_("<corrupt>"));
16731 p = (unsigned char *) end;
16732 }
071436c6 16733 putchar ('\n');
071436c6
NC
16734 return p;
16735 }
87779176
JM
16736
16737 case Tag_ABI_conformance:
071436c6 16738 {
4082ef84
NC
16739 printf (" Tag_ABI_conformance: \"");
16740 if (p < end - 1)
16741 {
16742 size_t maxlen = (end - p) - 1;
071436c6 16743
4082ef84
NC
16744 print_symbol ((int) maxlen, (const char *) p);
16745 p += strnlen ((char *) p, maxlen) + 1;
16746 }
16747 else
16748 {
16749 printf (_("<corrupt>"));
16750 p = (unsigned char *) end;
16751 }
071436c6 16752 printf ("\"\n");
071436c6
NC
16753 return p;
16754 }
59e6276b
JM
16755 }
16756
f6f0e17b
NC
16757 return display_tag_value (tag, p, end);
16758}
59e6276b 16759
f6f0e17b 16760static void
60abdbed 16761display_raw_attribute (unsigned char * p, unsigned char const * const end)
f6f0e17b
NC
16762{
16763 unsigned long addr = 0;
16764 size_t bytes = end - p;
16765
feceaa59 16766 assert (end >= p);
f6f0e17b 16767 while (bytes)
87779176 16768 {
f6f0e17b
NC
16769 int j;
16770 int k;
16771 int lbytes = (bytes > 16 ? 16 : bytes);
16772
16773 printf (" 0x%8.8lx ", addr);
16774
16775 for (j = 0; j < 16; j++)
16776 {
16777 if (j < lbytes)
16778 printf ("%2.2x", p[j]);
16779 else
16780 printf (" ");
16781
16782 if ((j & 3) == 3)
16783 printf (" ");
16784 }
16785
16786 for (j = 0; j < lbytes; j++)
16787 {
16788 k = p[j];
16789 if (k >= ' ' && k < 0x7f)
16790 printf ("%c", k);
16791 else
16792 printf (".");
16793 }
16794
16795 putchar ('\n');
16796
16797 p += lbytes;
16798 bytes -= lbytes;
16799 addr += lbytes;
87779176 16800 }
59e6276b 16801
f6f0e17b 16802 putchar ('\n');
59e6276b
JM
16803}
16804
13761a11 16805static unsigned char *
b0191216 16806display_msp430_attribute (unsigned char * p,
13761a11
NC
16807 const unsigned char * const end)
16808{
60abdbed
NC
16809 unsigned int val;
16810 unsigned int tag;
13761a11 16811
cd30bcef 16812 READ_ULEB (tag, p, end);
0b4362b0 16813
13761a11
NC
16814 switch (tag)
16815 {
16816 case OFBA_MSPABI_Tag_ISA:
13761a11 16817 printf (" Tag_ISA: ");
cd30bcef 16818 READ_ULEB (val, p, end);
13761a11
NC
16819 switch (val)
16820 {
16821 case 0: printf (_("None\n")); break;
16822 case 1: printf (_("MSP430\n")); break;
16823 case 2: printf (_("MSP430X\n")); break;
16824 default: printf ("??? (%d)\n", val); break;
16825 }
16826 break;
16827
16828 case OFBA_MSPABI_Tag_Code_Model:
13761a11 16829 printf (" Tag_Code_Model: ");
cd30bcef 16830 READ_ULEB (val, p, end);
13761a11
NC
16831 switch (val)
16832 {
16833 case 0: printf (_("None\n")); break;
16834 case 1: printf (_("Small\n")); break;
16835 case 2: printf (_("Large\n")); break;
16836 default: printf ("??? (%d)\n", val); break;
16837 }
16838 break;
16839
16840 case OFBA_MSPABI_Tag_Data_Model:
13761a11 16841 printf (" Tag_Data_Model: ");
cd30bcef 16842 READ_ULEB (val, p, end);
13761a11
NC
16843 switch (val)
16844 {
16845 case 0: printf (_("None\n")); break;
16846 case 1: printf (_("Small\n")); break;
16847 case 2: printf (_("Large\n")); break;
16848 case 3: printf (_("Restricted Large\n")); break;
16849 default: printf ("??? (%d)\n", val); break;
16850 }
16851 break;
16852
16853 default:
16854 printf (_(" <unknown tag %d>: "), tag);
16855
16856 if (tag & 1)
16857 {
071436c6 16858 putchar ('"');
4082ef84
NC
16859 if (p < end - 1)
16860 {
16861 size_t maxlen = (end - p) - 1;
16862
16863 print_symbol ((int) maxlen, (const char *) p);
16864 p += strnlen ((char *) p, maxlen) + 1;
16865 }
16866 else
16867 {
16868 printf (_("<corrupt>"));
16869 p = (unsigned char *) end;
16870 }
071436c6 16871 printf ("\"\n");
13761a11
NC
16872 }
16873 else
16874 {
cd30bcef 16875 READ_ULEB (val, p, end);
13761a11
NC
16876 printf ("%d (0x%x)\n", val, val);
16877 }
16878 break;
16879 }
16880
4082ef84 16881 assert (p <= end);
13761a11
NC
16882 return p;
16883}
16884
c0ea7c52
JL
16885static unsigned char *
16886display_msp430_gnu_attribute (unsigned char * p,
16887 unsigned int tag,
16888 const unsigned char * const end)
16889{
16890 if (tag == Tag_GNU_MSP430_Data_Region)
16891 {
cd30bcef 16892 unsigned int val;
c0ea7c52 16893
c0ea7c52 16894 printf (" Tag_GNU_MSP430_Data_Region: ");
cd30bcef 16895 READ_ULEB (val, p, end);
c0ea7c52
JL
16896
16897 switch (val)
16898 {
16899 case Val_GNU_MSP430_Data_Region_Any:
16900 printf (_("Any Region\n"));
16901 break;
16902 case Val_GNU_MSP430_Data_Region_Lower:
16903 printf (_("Lower Region Only\n"));
16904 break;
16905 default:
cd30bcef 16906 printf ("??? (%u)\n", val);
c0ea7c52
JL
16907 }
16908 return p;
16909 }
16910 return display_tag_value (tag & 1, p, end);
16911}
16912
2dc8dd17
JW
16913struct riscv_attr_tag_t {
16914 const char *name;
cd30bcef 16915 unsigned int tag;
2dc8dd17
JW
16916};
16917
16918static struct riscv_attr_tag_t riscv_attr_tag[] =
16919{
16920#define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
16921 T(arch),
16922 T(priv_spec),
16923 T(priv_spec_minor),
16924 T(priv_spec_revision),
16925 T(unaligned_access),
16926 T(stack_align),
16927#undef T
16928};
16929
16930static unsigned char *
16931display_riscv_attribute (unsigned char *p,
16932 const unsigned char * const end)
16933{
cd30bcef
AM
16934 unsigned int val;
16935 unsigned int tag;
2dc8dd17
JW
16936 struct riscv_attr_tag_t *attr = NULL;
16937 unsigned i;
16938
cd30bcef 16939 READ_ULEB (tag, p, end);
2dc8dd17
JW
16940
16941 /* Find the name of attribute. */
16942 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
16943 {
16944 if (riscv_attr_tag[i].tag == tag)
16945 {
16946 attr = &riscv_attr_tag[i];
16947 break;
16948 }
16949 }
16950
16951 if (attr)
16952 printf (" %s: ", attr->name);
16953 else
16954 return display_tag_value (tag, p, end);
16955
16956 switch (tag)
16957 {
16958 case Tag_RISCV_priv_spec:
16959 case Tag_RISCV_priv_spec_minor:
16960 case Tag_RISCV_priv_spec_revision:
cd30bcef
AM
16961 READ_ULEB (val, p, end);
16962 printf (_("%u\n"), val);
2dc8dd17
JW
16963 break;
16964 case Tag_RISCV_unaligned_access:
cd30bcef 16965 READ_ULEB (val, p, end);
2dc8dd17
JW
16966 switch (val)
16967 {
16968 case 0:
16969 printf (_("No unaligned access\n"));
16970 break;
16971 case 1:
16972 printf (_("Unaligned access\n"));
16973 break;
16974 }
16975 break;
16976 case Tag_RISCV_stack_align:
cd30bcef
AM
16977 READ_ULEB (val, p, end);
16978 printf (_("%u-bytes\n"), val);
2dc8dd17
JW
16979 break;
16980 case Tag_RISCV_arch:
16981 p = display_tag_value (-1, p, end);
16982 break;
16983 default:
16984 return display_tag_value (tag, p, end);
16985 }
16986
16987 return p;
16988}
16989
0861f561
CQ
16990static unsigned char *
16991display_csky_attribute (unsigned char * p,
16992 const unsigned char * const end)
16993{
16994 unsigned int tag;
16995 unsigned int val;
16996 READ_ULEB (tag, p, end);
16997
16998 if (tag >= Tag_CSKY_MAX)
16999 {
17000 return display_tag_value (-1, p, end);
17001 }
17002
17003 switch (tag)
17004 {
17005 case Tag_CSKY_ARCH_NAME:
17006 printf (" Tag_CSKY_ARCH_NAME:\t\t");
17007 return display_tag_value (-1, p, end);
17008 case Tag_CSKY_CPU_NAME:
17009 printf (" Tag_CSKY_CPU_NAME:\t\t");
17010 return display_tag_value (-1, p, end);
17011
17012 case Tag_CSKY_ISA_FLAGS:
17013 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
17014 return display_tag_value (0, p, end);
17015 case Tag_CSKY_ISA_EXT_FLAGS:
17016 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
17017 return display_tag_value (0, p, end);
17018
17019 case Tag_CSKY_DSP_VERSION:
17020 printf (" Tag_CSKY_DSP_VERSION:\t\t");
17021 READ_ULEB (val, p, end);
17022 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
17023 printf ("DSP Extension\n");
17024 else if (val == VAL_CSKY_DSP_VERSION_2)
17025 printf ("DSP 2.0\n");
17026 break;
17027
17028 case Tag_CSKY_VDSP_VERSION:
17029 printf (" Tag_CSKY_VDSP_VERSION:\t");
17030 READ_ULEB (val, p, end);
17031 printf ("VDSP Version %d\n", val);
17032 break;
17033
17034 case Tag_CSKY_FPU_VERSION:
17035 printf (" Tag_CSKY_FPU_VERSION:\t\t");
17036 READ_ULEB (val, p, end);
17037 if (val == VAL_CSKY_FPU_VERSION_1)
17038 printf ("ABIV1 FPU Version 1\n");
17039 else if (val == VAL_CSKY_FPU_VERSION_2)
17040 printf ("FPU Version 2\n");
17041 break;
17042
17043 case Tag_CSKY_FPU_ABI:
17044 printf (" Tag_CSKY_FPU_ABI:\t\t");
17045 READ_ULEB (val, p, end);
17046 if (val == VAL_CSKY_FPU_ABI_HARD)
17047 printf ("Hard\n");
17048 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
17049 printf ("SoftFP\n");
17050 else if (val == VAL_CSKY_FPU_ABI_SOFT)
17051 printf ("Soft\n");
17052 break;
17053 case Tag_CSKY_FPU_ROUNDING:
17054 READ_ULEB (val, p, end);
17055 if (val == 1) {
17056 printf (" Tag_CSKY_FPU_ROUNDING:\t");
17057 printf ("Needed\n");
17058 }
17059 break;
17060 case Tag_CSKY_FPU_DENORMAL:
17061 READ_ULEB (val, p, end);
17062 if (val == 1) {
17063 printf (" Tag_CSKY_FPU_DENORMAL:\t");
17064 printf ("Needed\n");
17065 }
17066 break;
17067 case Tag_CSKY_FPU_Exception:
17068 READ_ULEB (val, p, end);
17069 if (val == 1) {
17070 printf (" Tag_CSKY_FPU_Exception:\t");
17071 printf ("Needed\n");
17072 }
17073 break;
17074 case Tag_CSKY_FPU_NUMBER_MODULE:
17075 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
17076 return display_tag_value (-1, p, end);
17077 case Tag_CSKY_FPU_HARDFP:
17078 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
17079 READ_ULEB (val, p, end);
17080 if (val & VAL_CSKY_FPU_HARDFP_HALF)
17081 printf (" Half");
17082 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
17083 printf (" Single");
17084 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
17085 printf (" Double");
17086 printf ("\n");
17087 break;
17088 default:
17089 return display_tag_value (tag, p, end);
17090 }
17091 return p;
17092}
17093
015dc7e1 17094static bool
dda8d76d 17095process_attributes (Filedata * filedata,
60bca95a 17096 const char * public_name,
104d59d1 17097 unsigned int proc_type,
f6f0e17b 17098 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
60abdbed 17099 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
11c1ff18 17100{
2cf0635d 17101 Elf_Internal_Shdr * sect;
11c1ff18 17102 unsigned i;
015dc7e1 17103 bool res = true;
11c1ff18
PB
17104
17105 /* Find the section header so that we get the size. */
dda8d76d
NC
17106 for (i = 0, sect = filedata->section_headers;
17107 i < filedata->file_header.e_shnum;
11c1ff18
PB
17108 i++, sect++)
17109 {
071436c6
NC
17110 unsigned char * contents;
17111 unsigned char * p;
17112
104d59d1 17113 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
11c1ff18
PB
17114 continue;
17115
dda8d76d 17116 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
3f5e193b 17117 sect->sh_size, _("attributes"));
60bca95a 17118 if (contents == NULL)
32ec8896 17119 {
015dc7e1 17120 res = false;
32ec8896
NC
17121 continue;
17122 }
60bca95a 17123
11c1ff18 17124 p = contents;
60abdbed
NC
17125 /* The first character is the version of the attributes.
17126 Currently only version 1, (aka 'A') is recognised here. */
17127 if (*p != 'A')
32ec8896
NC
17128 {
17129 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
015dc7e1 17130 res = false;
32ec8896 17131 }
60abdbed 17132 else
11c1ff18 17133 {
071436c6
NC
17134 bfd_vma section_len;
17135
17136 section_len = sect->sh_size - 1;
11c1ff18 17137 p++;
60bca95a 17138
071436c6 17139 while (section_len > 0)
11c1ff18 17140 {
071436c6 17141 bfd_vma attr_len;
e9847026 17142 unsigned int namelen;
015dc7e1
AM
17143 bool public_section;
17144 bool gnu_section;
11c1ff18 17145
071436c6 17146 if (section_len <= 4)
e0a31db1
NC
17147 {
17148 error (_("Tag section ends prematurely\n"));
015dc7e1 17149 res = false;
e0a31db1
NC
17150 break;
17151 }
071436c6 17152 attr_len = byte_get (p, 4);
11c1ff18 17153 p += 4;
60bca95a 17154
071436c6 17155 if (attr_len > section_len)
11c1ff18 17156 {
071436c6
NC
17157 error (_("Bad attribute length (%u > %u)\n"),
17158 (unsigned) attr_len, (unsigned) section_len);
17159 attr_len = section_len;
015dc7e1 17160 res = false;
11c1ff18 17161 }
74e1a04b 17162 /* PR 17531: file: 001-101425-0.004 */
071436c6 17163 else if (attr_len < 5)
74e1a04b 17164 {
071436c6 17165 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
015dc7e1 17166 res = false;
74e1a04b
NC
17167 break;
17168 }
e9847026 17169
071436c6
NC
17170 section_len -= attr_len;
17171 attr_len -= 4;
17172
17173 namelen = strnlen ((char *) p, attr_len) + 1;
17174 if (namelen == 0 || namelen >= attr_len)
e9847026
NC
17175 {
17176 error (_("Corrupt attribute section name\n"));
015dc7e1 17177 res = false;
e9847026
NC
17178 break;
17179 }
17180
071436c6
NC
17181 printf (_("Attribute Section: "));
17182 print_symbol (INT_MAX, (const char *) p);
17183 putchar ('\n');
60bca95a
NC
17184
17185 if (public_name && streq ((char *) p, public_name))
015dc7e1 17186 public_section = true;
11c1ff18 17187 else
015dc7e1 17188 public_section = false;
60bca95a
NC
17189
17190 if (streq ((char *) p, "gnu"))
015dc7e1 17191 gnu_section = true;
104d59d1 17192 else
015dc7e1 17193 gnu_section = false;
60bca95a 17194
11c1ff18 17195 p += namelen;
071436c6 17196 attr_len -= namelen;
e0a31db1 17197
071436c6 17198 while (attr_len > 0 && p < contents + sect->sh_size)
11c1ff18 17199 {
e0a31db1 17200 int tag;
cd30bcef 17201 unsigned int val;
11c1ff18 17202 bfd_vma size;
071436c6 17203 unsigned char * end;
60bca95a 17204
e0a31db1 17205 /* PR binutils/17531: Safe handling of corrupt files. */
071436c6 17206 if (attr_len < 6)
e0a31db1
NC
17207 {
17208 error (_("Unused bytes at end of section\n"));
015dc7e1 17209 res = false;
e0a31db1
NC
17210 section_len = 0;
17211 break;
17212 }
17213
17214 tag = *(p++);
11c1ff18 17215 size = byte_get (p, 4);
071436c6 17216 if (size > attr_len)
11c1ff18 17217 {
e9847026 17218 error (_("Bad subsection length (%u > %u)\n"),
071436c6 17219 (unsigned) size, (unsigned) attr_len);
015dc7e1 17220 res = false;
071436c6 17221 size = attr_len;
11c1ff18 17222 }
e0a31db1
NC
17223 /* PR binutils/17531: Safe handling of corrupt files. */
17224 if (size < 6)
17225 {
17226 error (_("Bad subsection length (%u < 6)\n"),
17227 (unsigned) size);
015dc7e1 17228 res = false;
e0a31db1
NC
17229 section_len = 0;
17230 break;
17231 }
60bca95a 17232
071436c6 17233 attr_len -= size;
11c1ff18 17234 end = p + size - 1;
071436c6 17235 assert (end <= contents + sect->sh_size);
11c1ff18 17236 p += 4;
60bca95a 17237
11c1ff18
PB
17238 switch (tag)
17239 {
17240 case 1:
2b692964 17241 printf (_("File Attributes\n"));
11c1ff18
PB
17242 break;
17243 case 2:
2b692964 17244 printf (_("Section Attributes:"));
11c1ff18
PB
17245 goto do_numlist;
17246 case 3:
2b692964 17247 printf (_("Symbol Attributes:"));
1a0670f3 17248 /* Fall through. */
11c1ff18
PB
17249 do_numlist:
17250 for (;;)
17251 {
cd30bcef 17252 READ_ULEB (val, p, end);
11c1ff18
PB
17253 if (val == 0)
17254 break;
17255 printf (" %d", val);
17256 }
17257 printf ("\n");
17258 break;
17259 default:
2b692964 17260 printf (_("Unknown tag: %d\n"), tag);
015dc7e1 17261 public_section = false;
11c1ff18
PB
17262 break;
17263 }
60bca95a 17264
071436c6 17265 if (public_section && display_pub_attribute != NULL)
11c1ff18
PB
17266 {
17267 while (p < end)
f6f0e17b 17268 p = display_pub_attribute (p, end);
60abdbed 17269 assert (p == end);
104d59d1 17270 }
071436c6 17271 else if (gnu_section && display_proc_gnu_attribute != NULL)
104d59d1
JM
17272 {
17273 while (p < end)
17274 p = display_gnu_attribute (p,
f6f0e17b
NC
17275 display_proc_gnu_attribute,
17276 end);
60abdbed 17277 assert (p == end);
11c1ff18 17278 }
071436c6 17279 else if (p < end)
11c1ff18 17280 {
071436c6 17281 printf (_(" Unknown attribute:\n"));
f6f0e17b 17282 display_raw_attribute (p, end);
11c1ff18
PB
17283 p = end;
17284 }
071436c6
NC
17285 else
17286 attr_len = 0;
11c1ff18
PB
17287 }
17288 }
17289 }
d70c5fc7 17290
60bca95a 17291 free (contents);
11c1ff18 17292 }
32ec8896
NC
17293
17294 return res;
11c1ff18
PB
17295}
17296
ccb4c951
RS
17297/* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
17298 Print the Address, Access and Initial fields of an entry at VMA ADDR
82b1b41b
NC
17299 and return the VMA of the next entry, or -1 if there was a problem.
17300 Does not read from DATA_END or beyond. */
ccb4c951
RS
17301
17302static bfd_vma
82b1b41b
NC
17303print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
17304 unsigned char * data_end)
ccb4c951
RS
17305{
17306 printf (" ");
17307 print_vma (addr, LONG_HEX);
17308 printf (" ");
17309 if (addr < pltgot + 0xfff0)
17310 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
17311 else
17312 printf ("%10s", "");
17313 printf (" ");
17314 if (data == NULL)
2b692964 17315 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
ccb4c951
RS
17316 else
17317 {
17318 bfd_vma entry;
82b1b41b 17319 unsigned char * from = data + addr - pltgot;
ccb4c951 17320
82b1b41b
NC
17321 if (from + (is_32bit_elf ? 4 : 8) > data_end)
17322 {
17323 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
17324 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
17325 return (bfd_vma) -1;
17326 }
17327 else
17328 {
17329 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17330 print_vma (entry, LONG_HEX);
17331 }
ccb4c951
RS
17332 }
17333 return addr + (is_32bit_elf ? 4 : 8);
17334}
17335
861fb55a
DJ
17336/* DATA points to the contents of a MIPS PLT GOT that starts at VMA
17337 PLTGOT. Print the Address and Initial fields of an entry at VMA
17338 ADDR and return the VMA of the next entry. */
17339
17340static bfd_vma
2cf0635d 17341print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
861fb55a
DJ
17342{
17343 printf (" ");
17344 print_vma (addr, LONG_HEX);
17345 printf (" ");
17346 if (data == NULL)
2b692964 17347 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
861fb55a
DJ
17348 else
17349 {
17350 bfd_vma entry;
17351
17352 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17353 print_vma (entry, LONG_HEX);
17354 }
17355 return addr + (is_32bit_elf ? 4 : 8);
17356}
17357
351cdf24
MF
17358static void
17359print_mips_ases (unsigned int mask)
17360{
17361 if (mask & AFL_ASE_DSP)
17362 fputs ("\n\tDSP ASE", stdout);
17363 if (mask & AFL_ASE_DSPR2)
17364 fputs ("\n\tDSP R2 ASE", stdout);
8f4f9071
MF
17365 if (mask & AFL_ASE_DSPR3)
17366 fputs ("\n\tDSP R3 ASE", stdout);
351cdf24
MF
17367 if (mask & AFL_ASE_EVA)
17368 fputs ("\n\tEnhanced VA Scheme", stdout);
17369 if (mask & AFL_ASE_MCU)
17370 fputs ("\n\tMCU (MicroController) ASE", stdout);
17371 if (mask & AFL_ASE_MDMX)
17372 fputs ("\n\tMDMX ASE", stdout);
17373 if (mask & AFL_ASE_MIPS3D)
17374 fputs ("\n\tMIPS-3D ASE", stdout);
17375 if (mask & AFL_ASE_MT)
17376 fputs ("\n\tMT ASE", stdout);
17377 if (mask & AFL_ASE_SMARTMIPS)
17378 fputs ("\n\tSmartMIPS ASE", stdout);
17379 if (mask & AFL_ASE_VIRT)
17380 fputs ("\n\tVZ ASE", stdout);
17381 if (mask & AFL_ASE_MSA)
17382 fputs ("\n\tMSA ASE", stdout);
17383 if (mask & AFL_ASE_MIPS16)
17384 fputs ("\n\tMIPS16 ASE", stdout);
17385 if (mask & AFL_ASE_MICROMIPS)
17386 fputs ("\n\tMICROMIPS ASE", stdout);
17387 if (mask & AFL_ASE_XPA)
17388 fputs ("\n\tXPA ASE", stdout);
25499ac7
MR
17389 if (mask & AFL_ASE_MIPS16E2)
17390 fputs ("\n\tMIPS16e2 ASE", stdout);
730c3174
SE
17391 if (mask & AFL_ASE_CRC)
17392 fputs ("\n\tCRC ASE", stdout);
6f20c942
FS
17393 if (mask & AFL_ASE_GINV)
17394 fputs ("\n\tGINV ASE", stdout);
8095d2f7
CX
17395 if (mask & AFL_ASE_LOONGSON_MMI)
17396 fputs ("\n\tLoongson MMI ASE", stdout);
716c08de
CX
17397 if (mask & AFL_ASE_LOONGSON_CAM)
17398 fputs ("\n\tLoongson CAM ASE", stdout);
bdc6c06e
CX
17399 if (mask & AFL_ASE_LOONGSON_EXT)
17400 fputs ("\n\tLoongson EXT ASE", stdout);
a693765e
CX
17401 if (mask & AFL_ASE_LOONGSON_EXT2)
17402 fputs ("\n\tLoongson EXT2 ASE", stdout);
351cdf24
MF
17403 if (mask == 0)
17404 fprintf (stdout, "\n\t%s", _("None"));
00ac7aa0
MF
17405 else if ((mask & ~AFL_ASE_MASK) != 0)
17406 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
351cdf24
MF
17407}
17408
17409static void
17410print_mips_isa_ext (unsigned int isa_ext)
17411{
17412 switch (isa_ext)
17413 {
17414 case 0:
17415 fputs (_("None"), stdout);
17416 break;
17417 case AFL_EXT_XLR:
17418 fputs ("RMI XLR", stdout);
17419 break;
2c629856
N
17420 case AFL_EXT_OCTEON3:
17421 fputs ("Cavium Networks Octeon3", stdout);
17422 break;
351cdf24
MF
17423 case AFL_EXT_OCTEON2:
17424 fputs ("Cavium Networks Octeon2", stdout);
17425 break;
17426 case AFL_EXT_OCTEONP:
17427 fputs ("Cavium Networks OcteonP", stdout);
17428 break;
351cdf24
MF
17429 case AFL_EXT_OCTEON:
17430 fputs ("Cavium Networks Octeon", stdout);
17431 break;
17432 case AFL_EXT_5900:
17433 fputs ("Toshiba R5900", stdout);
17434 break;
17435 case AFL_EXT_4650:
17436 fputs ("MIPS R4650", stdout);
17437 break;
17438 case AFL_EXT_4010:
17439 fputs ("LSI R4010", stdout);
17440 break;
17441 case AFL_EXT_4100:
17442 fputs ("NEC VR4100", stdout);
17443 break;
17444 case AFL_EXT_3900:
17445 fputs ("Toshiba R3900", stdout);
17446 break;
17447 case AFL_EXT_10000:
17448 fputs ("MIPS R10000", stdout);
17449 break;
17450 case AFL_EXT_SB1:
17451 fputs ("Broadcom SB-1", stdout);
17452 break;
17453 case AFL_EXT_4111:
17454 fputs ("NEC VR4111/VR4181", stdout);
17455 break;
17456 case AFL_EXT_4120:
17457 fputs ("NEC VR4120", stdout);
17458 break;
17459 case AFL_EXT_5400:
17460 fputs ("NEC VR5400", stdout);
17461 break;
17462 case AFL_EXT_5500:
17463 fputs ("NEC VR5500", stdout);
17464 break;
17465 case AFL_EXT_LOONGSON_2E:
17466 fputs ("ST Microelectronics Loongson 2E", stdout);
17467 break;
17468 case AFL_EXT_LOONGSON_2F:
17469 fputs ("ST Microelectronics Loongson 2F", stdout);
17470 break;
38bf472a
MR
17471 case AFL_EXT_INTERAPTIV_MR2:
17472 fputs ("Imagination interAptiv MR2", stdout);
17473 break;
351cdf24 17474 default:
00ac7aa0 17475 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
351cdf24
MF
17476 }
17477}
17478
32ec8896 17479static signed int
351cdf24
MF
17480get_mips_reg_size (int reg_size)
17481{
17482 return (reg_size == AFL_REG_NONE) ? 0
17483 : (reg_size == AFL_REG_32) ? 32
17484 : (reg_size == AFL_REG_64) ? 64
17485 : (reg_size == AFL_REG_128) ? 128
17486 : -1;
17487}
17488
015dc7e1 17489static bool
dda8d76d 17490process_mips_specific (Filedata * filedata)
5b18a4bc 17491{
2cf0635d 17492 Elf_Internal_Dyn * entry;
351cdf24 17493 Elf_Internal_Shdr *sect = NULL;
19e6b90e
L
17494 size_t liblist_offset = 0;
17495 size_t liblistno = 0;
17496 size_t conflictsno = 0;
17497 size_t options_offset = 0;
17498 size_t conflicts_offset = 0;
861fb55a
DJ
17499 size_t pltrelsz = 0;
17500 size_t pltrel = 0;
ccb4c951 17501 bfd_vma pltgot = 0;
861fb55a
DJ
17502 bfd_vma mips_pltgot = 0;
17503 bfd_vma jmprel = 0;
ccb4c951
RS
17504 bfd_vma local_gotno = 0;
17505 bfd_vma gotsym = 0;
17506 bfd_vma symtabno = 0;
015dc7e1 17507 bool res = true;
103f02d3 17508
dda8d76d 17509 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
32ec8896 17510 display_mips_gnu_attribute))
015dc7e1 17511 res = false;
2cf19d5c 17512
dda8d76d 17513 sect = find_section (filedata, ".MIPS.abiflags");
351cdf24
MF
17514
17515 if (sect != NULL)
17516 {
17517 Elf_External_ABIFlags_v0 *abiflags_ext;
17518 Elf_Internal_ABIFlags_v0 abiflags_in;
17519
17520 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
32ec8896
NC
17521 {
17522 error (_("Corrupt MIPS ABI Flags section.\n"));
015dc7e1 17523 res = false;
32ec8896 17524 }
351cdf24
MF
17525 else
17526 {
dda8d76d 17527 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
351cdf24
MF
17528 sect->sh_size, _("MIPS ABI Flags section"));
17529 if (abiflags_ext)
17530 {
17531 abiflags_in.version = BYTE_GET (abiflags_ext->version);
17532 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
17533 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
17534 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
17535 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
17536 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
17537 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
17538 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
17539 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
17540 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
17541 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
17542
17543 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
17544 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
17545 if (abiflags_in.isa_rev > 1)
17546 printf ("r%d", abiflags_in.isa_rev);
17547 printf ("\nGPR size: %d",
17548 get_mips_reg_size (abiflags_in.gpr_size));
17549 printf ("\nCPR1 size: %d",
17550 get_mips_reg_size (abiflags_in.cpr1_size));
17551 printf ("\nCPR2 size: %d",
17552 get_mips_reg_size (abiflags_in.cpr2_size));
17553 fputs ("\nFP ABI: ", stdout);
17554 print_mips_fp_abi_value (abiflags_in.fp_abi);
17555 fputs ("ISA Extension: ", stdout);
17556 print_mips_isa_ext (abiflags_in.isa_ext);
17557 fputs ("\nASEs:", stdout);
17558 print_mips_ases (abiflags_in.ases);
17559 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
17560 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
17561 fputc ('\n', stdout);
17562 free (abiflags_ext);
17563 }
17564 }
17565 }
17566
19e6b90e 17567 /* We have a lot of special sections. Thanks SGI! */
978c4450 17568 if (filedata->dynamic_section == NULL)
bbdd9a68
MR
17569 {
17570 /* No dynamic information available. See if there is static GOT. */
dda8d76d 17571 sect = find_section (filedata, ".got");
bbdd9a68
MR
17572 if (sect != NULL)
17573 {
17574 unsigned char *data_end;
17575 unsigned char *data;
17576 bfd_vma ent, end;
17577 int addr_size;
17578
17579 pltgot = sect->sh_addr;
17580
17581 ent = pltgot;
17582 addr_size = (is_32bit_elf ? 4 : 8);
17583 end = pltgot + sect->sh_size;
17584
dda8d76d 17585 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
bbdd9a68
MR
17586 end - pltgot, 1,
17587 _("Global Offset Table data"));
17588 /* PR 12855: Null data is handled gracefully throughout. */
17589 data_end = data + (end - pltgot);
17590
17591 printf (_("\nStatic GOT:\n"));
17592 printf (_(" Canonical gp value: "));
17593 print_vma (ent + 0x7ff0, LONG_HEX);
17594 printf ("\n\n");
17595
17596 /* In a dynamic binary GOT[0] is reserved for the dynamic
17597 loader to store the lazy resolver pointer, however in
17598 a static binary it may well have been omitted and GOT
17599 reduced to a table of addresses.
17600 PR 21344: Check for the entry being fully available
17601 before fetching it. */
17602 if (data
17603 && data + ent - pltgot + addr_size <= data_end
17604 && byte_get (data + ent - pltgot, addr_size) == 0)
17605 {
17606 printf (_(" Reserved entries:\n"));
17607 printf (_(" %*s %10s %*s\n"),
17608 addr_size * 2, _("Address"), _("Access"),
17609 addr_size * 2, _("Value"));
17610 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17611 printf ("\n");
17612 if (ent == (bfd_vma) -1)
17613 goto sgot_print_fail;
17614
17615 /* Check for the MSB of GOT[1] being set, identifying a
17616 GNU object. This entry will be used by some runtime
17617 loaders, to store the module pointer. Otherwise this
17618 is an ordinary local entry.
17619 PR 21344: Check for the entry being fully available
17620 before fetching it. */
17621 if (data
17622 && data + ent - pltgot + addr_size <= data_end
17623 && (byte_get (data + ent - pltgot, addr_size)
17624 >> (addr_size * 8 - 1)) != 0)
17625 {
17626 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17627 printf ("\n");
17628 if (ent == (bfd_vma) -1)
17629 goto sgot_print_fail;
17630 }
17631 printf ("\n");
17632 }
17633
f17e9d8a 17634 if (data != NULL && ent < end)
bbdd9a68
MR
17635 {
17636 printf (_(" Local entries:\n"));
17637 printf (" %*s %10s %*s\n",
17638 addr_size * 2, _("Address"), _("Access"),
17639 addr_size * 2, _("Value"));
17640 while (ent < end)
17641 {
17642 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17643 printf ("\n");
17644 if (ent == (bfd_vma) -1)
17645 goto sgot_print_fail;
17646 }
17647 printf ("\n");
17648 }
17649
17650 sgot_print_fail:
9db70fc3 17651 free (data);
bbdd9a68
MR
17652 }
17653 return res;
17654 }
252b5132 17655
978c4450 17656 for (entry = filedata->dynamic_section;
071436c6 17657 /* PR 17531 file: 012-50589-0.004. */
978c4450
AM
17658 (entry < filedata->dynamic_section + filedata->dynamic_nent
17659 && entry->d_tag != DT_NULL);
071436c6 17660 ++entry)
252b5132
RH
17661 switch (entry->d_tag)
17662 {
17663 case DT_MIPS_LIBLIST:
d93f0186 17664 liblist_offset
dda8d76d 17665 = offset_from_vma (filedata, entry->d_un.d_val,
d93f0186 17666 liblistno * sizeof (Elf32_External_Lib));
252b5132
RH
17667 break;
17668 case DT_MIPS_LIBLISTNO:
17669 liblistno = entry->d_un.d_val;
17670 break;
17671 case DT_MIPS_OPTIONS:
dda8d76d 17672 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
252b5132
RH
17673 break;
17674 case DT_MIPS_CONFLICT:
d93f0186 17675 conflicts_offset
dda8d76d 17676 = offset_from_vma (filedata, entry->d_un.d_val,
d93f0186 17677 conflictsno * sizeof (Elf32_External_Conflict));
252b5132
RH
17678 break;
17679 case DT_MIPS_CONFLICTNO:
17680 conflictsno = entry->d_un.d_val;
17681 break;
ccb4c951 17682 case DT_PLTGOT:
861fb55a
DJ
17683 pltgot = entry->d_un.d_ptr;
17684 break;
ccb4c951
RS
17685 case DT_MIPS_LOCAL_GOTNO:
17686 local_gotno = entry->d_un.d_val;
17687 break;
17688 case DT_MIPS_GOTSYM:
17689 gotsym = entry->d_un.d_val;
17690 break;
17691 case DT_MIPS_SYMTABNO:
17692 symtabno = entry->d_un.d_val;
17693 break;
861fb55a
DJ
17694 case DT_MIPS_PLTGOT:
17695 mips_pltgot = entry->d_un.d_ptr;
17696 break;
17697 case DT_PLTREL:
17698 pltrel = entry->d_un.d_val;
17699 break;
17700 case DT_PLTRELSZ:
17701 pltrelsz = entry->d_un.d_val;
17702 break;
17703 case DT_JMPREL:
17704 jmprel = entry->d_un.d_ptr;
17705 break;
252b5132
RH
17706 default:
17707 break;
17708 }
17709
17710 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
17711 {
2cf0635d 17712 Elf32_External_Lib * elib;
252b5132
RH
17713 size_t cnt;
17714
dda8d76d 17715 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
95099889
AM
17716 sizeof (Elf32_External_Lib),
17717 liblistno,
17718 _("liblist section data"));
a6e9f9df 17719 if (elib)
252b5132 17720 {
d3a49aa8
AM
17721 printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
17722 "\nSection '.liblist' contains %lu entries:\n",
17723 (unsigned long) liblistno),
a6e9f9df 17724 (unsigned long) liblistno);
2b692964 17725 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
a6e9f9df
AM
17726 stdout);
17727
17728 for (cnt = 0; cnt < liblistno; ++cnt)
252b5132 17729 {
a6e9f9df 17730 Elf32_Lib liblist;
91d6fa6a 17731 time_t atime;
d5b07ef4 17732 char timebuf[128];
2cf0635d 17733 struct tm * tmp;
a6e9f9df
AM
17734
17735 liblist.l_name = BYTE_GET (elib[cnt].l_name);
91d6fa6a 17736 atime = BYTE_GET (elib[cnt].l_time_stamp);
a6e9f9df
AM
17737 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
17738 liblist.l_version = BYTE_GET (elib[cnt].l_version);
17739 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
17740
91d6fa6a 17741 tmp = gmtime (&atime);
e9e44622
JJ
17742 snprintf (timebuf, sizeof (timebuf),
17743 "%04u-%02u-%02uT%02u:%02u:%02u",
17744 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
17745 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
a6e9f9df 17746
31104126 17747 printf ("%3lu: ", (unsigned long) cnt);
978c4450
AM
17748 if (VALID_DYNAMIC_NAME (filedata, liblist.l_name))
17749 print_symbol (20, GET_DYNAMIC_NAME (filedata, liblist.l_name));
d79b3d50 17750 else
2b692964 17751 printf (_("<corrupt: %9ld>"), liblist.l_name);
31104126
NC
17752 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
17753 liblist.l_version);
a6e9f9df
AM
17754
17755 if (liblist.l_flags == 0)
2b692964 17756 puts (_(" NONE"));
a6e9f9df
AM
17757 else
17758 {
17759 static const struct
252b5132 17760 {
2cf0635d 17761 const char * name;
a6e9f9df 17762 int bit;
252b5132 17763 }
a6e9f9df
AM
17764 l_flags_vals[] =
17765 {
17766 { " EXACT_MATCH", LL_EXACT_MATCH },
17767 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
17768 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
17769 { " EXPORTS", LL_EXPORTS },
17770 { " DELAY_LOAD", LL_DELAY_LOAD },
17771 { " DELTA", LL_DELTA }
17772 };
17773 int flags = liblist.l_flags;
17774 size_t fcnt;
17775
60bca95a 17776 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
a6e9f9df
AM
17777 if ((flags & l_flags_vals[fcnt].bit) != 0)
17778 {
17779 fputs (l_flags_vals[fcnt].name, stdout);
17780 flags ^= l_flags_vals[fcnt].bit;
17781 }
17782 if (flags != 0)
17783 printf (" %#x", (unsigned int) flags);
252b5132 17784
a6e9f9df
AM
17785 puts ("");
17786 }
252b5132 17787 }
252b5132 17788
a6e9f9df
AM
17789 free (elib);
17790 }
32ec8896 17791 else
015dc7e1 17792 res = false;
252b5132
RH
17793 }
17794
17795 if (options_offset != 0)
17796 {
2cf0635d 17797 Elf_External_Options * eopt;
252b5132
RH
17798 size_t offset;
17799 int cnt;
dda8d76d 17800 sect = filedata->section_headers;
252b5132
RH
17801
17802 /* Find the section header so that we get the size. */
dda8d76d 17803 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
948f632f 17804 /* PR 17533 file: 012-277276-0.004. */
071436c6
NC
17805 if (sect == NULL)
17806 {
17807 error (_("No MIPS_OPTIONS header found\n"));
015dc7e1 17808 return false;
071436c6 17809 }
7fc0c668
NC
17810 /* PR 24243 */
17811 if (sect->sh_size < sizeof (* eopt))
17812 {
17813 error (_("The MIPS options section is too small.\n"));
015dc7e1 17814 return false;
7fc0c668 17815 }
252b5132 17816
dda8d76d 17817 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
3f5e193b 17818 sect->sh_size, _("options"));
a6e9f9df 17819 if (eopt)
252b5132 17820 {
fd17d1e6 17821 Elf_Internal_Options option;
76da6bbe 17822
a6e9f9df 17823 offset = cnt = 0;
82b1b41b 17824 while (offset <= sect->sh_size - sizeof (* eopt))
a6e9f9df 17825 {
2cf0635d 17826 Elf_External_Options * eoption;
fd17d1e6 17827 unsigned int optsize;
252b5132 17828
a6e9f9df 17829 eoption = (Elf_External_Options *) ((char *) eopt + offset);
252b5132 17830
fd17d1e6 17831 optsize = BYTE_GET (eoption->size);
76da6bbe 17832
82b1b41b 17833 /* PR 17531: file: ffa0fa3b. */
fd17d1e6
AM
17834 if (optsize < sizeof (* eopt)
17835 || optsize > sect->sh_size - offset)
82b1b41b 17836 {
645f43a8 17837 error (_("Invalid size (%u) for MIPS option\n"),
fd17d1e6 17838 optsize);
645f43a8 17839 free (eopt);
015dc7e1 17840 return false;
82b1b41b 17841 }
fd17d1e6 17842 offset += optsize;
a6e9f9df
AM
17843 ++cnt;
17844 }
252b5132 17845
d3a49aa8
AM
17846 printf (ngettext ("\nSection '%s' contains %d entry:\n",
17847 "\nSection '%s' contains %d entries:\n",
17848 cnt),
dda8d76d 17849 printable_section_name (filedata, sect), cnt);
76da6bbe 17850
82b1b41b 17851 offset = 0;
a6e9f9df 17852 while (cnt-- > 0)
252b5132 17853 {
a6e9f9df 17854 size_t len;
fd17d1e6
AM
17855 Elf_External_Options * eoption;
17856
17857 eoption = (Elf_External_Options *) ((char *) eopt + offset);
17858
17859 option.kind = BYTE_GET (eoption->kind);
17860 option.size = BYTE_GET (eoption->size);
17861 option.section = BYTE_GET (eoption->section);
17862 option.info = BYTE_GET (eoption->info);
a6e9f9df 17863
fd17d1e6 17864 switch (option.kind)
252b5132 17865 {
a6e9f9df
AM
17866 case ODK_NULL:
17867 /* This shouldn't happen. */
d0c4e780 17868 printf (" NULL %" PRId16 " %" PRIx32,
fd17d1e6 17869 option.section, option.info);
a6e9f9df 17870 break;
2e6be59c 17871
a6e9f9df
AM
17872 case ODK_REGINFO:
17873 printf (" REGINFO ");
dda8d76d 17874 if (filedata->file_header.e_machine == EM_MIPS)
a6e9f9df 17875 {
2cf0635d 17876 Elf32_External_RegInfo * ereg;
b34976b6 17877 Elf32_RegInfo reginfo;
a6e9f9df 17878
2e6be59c 17879 /* 32bit form. */
fd17d1e6
AM
17880 if (option.size < (sizeof (Elf_External_Options)
17881 + sizeof (Elf32_External_RegInfo)))
2e6be59c
NC
17882 {
17883 printf (_("<corrupt>\n"));
17884 error (_("Truncated MIPS REGINFO option\n"));
17885 cnt = 0;
17886 break;
17887 }
17888
fd17d1e6 17889 ereg = (Elf32_External_RegInfo *) (eoption + 1);
2e6be59c 17890
a6e9f9df
AM
17891 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
17892 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
17893 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
17894 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
17895 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
17896 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
17897
d0c4e780
AM
17898 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
17899 reginfo.ri_gprmask, reginfo.ri_gp_value);
17900 printf (" "
17901 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
17902 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
a6e9f9df
AM
17903 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
17904 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
17905 }
17906 else
17907 {
17908 /* 64 bit form. */
2cf0635d 17909 Elf64_External_RegInfo * ereg;
a6e9f9df
AM
17910 Elf64_Internal_RegInfo reginfo;
17911
fd17d1e6
AM
17912 if (option.size < (sizeof (Elf_External_Options)
17913 + sizeof (Elf64_External_RegInfo)))
2e6be59c
NC
17914 {
17915 printf (_("<corrupt>\n"));
17916 error (_("Truncated MIPS REGINFO option\n"));
17917 cnt = 0;
17918 break;
17919 }
17920
fd17d1e6 17921 ereg = (Elf64_External_RegInfo *) (eoption + 1);
a6e9f9df
AM
17922 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
17923 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
17924 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
17925 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
17926 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
66543521 17927 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
a6e9f9df 17928
d0c4e780
AM
17929 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
17930 reginfo.ri_gprmask, reginfo.ri_gp_value);
17931 printf (" "
17932 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
17933 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
a6e9f9df
AM
17934 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
17935 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
17936 }
fd17d1e6 17937 offset += option.size;
a6e9f9df 17938 continue;
2e6be59c 17939
a6e9f9df
AM
17940 case ODK_EXCEPTIONS:
17941 fputs (" EXCEPTIONS fpe_min(", stdout);
fd17d1e6 17942 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
a6e9f9df 17943 fputs (") fpe_max(", stdout);
fd17d1e6 17944 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
a6e9f9df
AM
17945 fputs (")", stdout);
17946
fd17d1e6 17947 if (option.info & OEX_PAGE0)
a6e9f9df 17948 fputs (" PAGE0", stdout);
fd17d1e6 17949 if (option.info & OEX_SMM)
a6e9f9df 17950 fputs (" SMM", stdout);
fd17d1e6 17951 if (option.info & OEX_FPDBUG)
a6e9f9df 17952 fputs (" FPDBUG", stdout);
fd17d1e6 17953 if (option.info & OEX_DISMISS)
a6e9f9df
AM
17954 fputs (" DISMISS", stdout);
17955 break;
2e6be59c 17956
a6e9f9df
AM
17957 case ODK_PAD:
17958 fputs (" PAD ", stdout);
fd17d1e6 17959 if (option.info & OPAD_PREFIX)
a6e9f9df 17960 fputs (" PREFIX", stdout);
fd17d1e6 17961 if (option.info & OPAD_POSTFIX)
a6e9f9df 17962 fputs (" POSTFIX", stdout);
fd17d1e6 17963 if (option.info & OPAD_SYMBOL)
a6e9f9df
AM
17964 fputs (" SYMBOL", stdout);
17965 break;
2e6be59c 17966
a6e9f9df
AM
17967 case ODK_HWPATCH:
17968 fputs (" HWPATCH ", stdout);
fd17d1e6 17969 if (option.info & OHW_R4KEOP)
a6e9f9df 17970 fputs (" R4KEOP", stdout);
fd17d1e6 17971 if (option.info & OHW_R8KPFETCH)
a6e9f9df 17972 fputs (" R8KPFETCH", stdout);
fd17d1e6 17973 if (option.info & OHW_R5KEOP)
a6e9f9df 17974 fputs (" R5KEOP", stdout);
fd17d1e6 17975 if (option.info & OHW_R5KCVTL)
a6e9f9df
AM
17976 fputs (" R5KCVTL", stdout);
17977 break;
2e6be59c 17978
a6e9f9df
AM
17979 case ODK_FILL:
17980 fputs (" FILL ", stdout);
17981 /* XXX Print content of info word? */
17982 break;
2e6be59c 17983
a6e9f9df
AM
17984 case ODK_TAGS:
17985 fputs (" TAGS ", stdout);
17986 /* XXX Print content of info word? */
17987 break;
2e6be59c 17988
a6e9f9df
AM
17989 case ODK_HWAND:
17990 fputs (" HWAND ", stdout);
fd17d1e6 17991 if (option.info & OHWA0_R4KEOP_CHECKED)
a6e9f9df 17992 fputs (" R4KEOP_CHECKED", stdout);
fd17d1e6 17993 if (option.info & OHWA0_R4KEOP_CLEAN)
a6e9f9df
AM
17994 fputs (" R4KEOP_CLEAN", stdout);
17995 break;
2e6be59c 17996
a6e9f9df
AM
17997 case ODK_HWOR:
17998 fputs (" HWOR ", stdout);
fd17d1e6 17999 if (option.info & OHWA0_R4KEOP_CHECKED)
a6e9f9df 18000 fputs (" R4KEOP_CHECKED", stdout);
fd17d1e6 18001 if (option.info & OHWA0_R4KEOP_CLEAN)
a6e9f9df
AM
18002 fputs (" R4KEOP_CLEAN", stdout);
18003 break;
2e6be59c 18004
a6e9f9df 18005 case ODK_GP_GROUP:
d0c4e780 18006 printf (" GP_GROUP %#06x self-contained %#06x",
fd17d1e6
AM
18007 option.info & OGP_GROUP,
18008 (option.info & OGP_SELF) >> 16);
a6e9f9df 18009 break;
2e6be59c 18010
a6e9f9df 18011 case ODK_IDENT:
d0c4e780 18012 printf (" IDENT %#06x self-contained %#06x",
fd17d1e6
AM
18013 option.info & OGP_GROUP,
18014 (option.info & OGP_SELF) >> 16);
a6e9f9df 18015 break;
2e6be59c 18016
a6e9f9df
AM
18017 default:
18018 /* This shouldn't happen. */
d0c4e780 18019 printf (" %3d ??? %" PRId16 " %" PRIx32,
fd17d1e6 18020 option.kind, option.section, option.info);
a6e9f9df 18021 break;
252b5132 18022 }
a6e9f9df 18023
2cf0635d 18024 len = sizeof (* eopt);
fd17d1e6 18025 while (len < option.size)
82b1b41b 18026 {
fd17d1e6 18027 unsigned char datum = *((unsigned char *) eoption + len);
a6e9f9df 18028
82b1b41b
NC
18029 if (ISPRINT (datum))
18030 printf ("%c", datum);
18031 else
18032 printf ("\\%03o", datum);
18033 len ++;
18034 }
a6e9f9df 18035 fputs ("\n", stdout);
82b1b41b 18036
fd17d1e6 18037 offset += option.size;
252b5132 18038 }
a6e9f9df 18039 free (eopt);
252b5132 18040 }
32ec8896 18041 else
015dc7e1 18042 res = false;
252b5132
RH
18043 }
18044
18045 if (conflicts_offset != 0 && conflictsno != 0)
18046 {
2cf0635d 18047 Elf32_Conflict * iconf;
252b5132
RH
18048 size_t cnt;
18049
978c4450 18050 if (filedata->dynamic_symbols == NULL)
252b5132 18051 {
591a748a 18052 error (_("conflict list found without a dynamic symbol table\n"));
015dc7e1 18053 return false;
252b5132
RH
18054 }
18055
7296a62a
NC
18056 /* PR 21345 - print a slightly more helpful error message
18057 if we are sure that the cmalloc will fail. */
645f43a8 18058 if (conflictsno > filedata->file_size / sizeof (* iconf))
7296a62a
NC
18059 {
18060 error (_("Overlarge number of conflicts detected: %lx\n"),
18061 (long) conflictsno);
015dc7e1 18062 return false;
7296a62a
NC
18063 }
18064
3f5e193b 18065 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
252b5132
RH
18066 if (iconf == NULL)
18067 {
8b73c356 18068 error (_("Out of memory allocating space for dynamic conflicts\n"));
015dc7e1 18069 return false;
252b5132
RH
18070 }
18071
9ea033b2 18072 if (is_32bit_elf)
252b5132 18073 {
2cf0635d 18074 Elf32_External_Conflict * econf32;
a6e9f9df 18075
3f5e193b 18076 econf32 = (Elf32_External_Conflict *)
95099889
AM
18077 get_data (NULL, filedata, conflicts_offset,
18078 sizeof (*econf32), conflictsno, _("conflict"));
a6e9f9df 18079 if (!econf32)
5a814d6d
AM
18080 {
18081 free (iconf);
015dc7e1 18082 return false;
5a814d6d 18083 }
252b5132
RH
18084
18085 for (cnt = 0; cnt < conflictsno; ++cnt)
18086 iconf[cnt] = BYTE_GET (econf32[cnt]);
a6e9f9df
AM
18087
18088 free (econf32);
252b5132
RH
18089 }
18090 else
18091 {
2cf0635d 18092 Elf64_External_Conflict * econf64;
a6e9f9df 18093
3f5e193b 18094 econf64 = (Elf64_External_Conflict *)
95099889
AM
18095 get_data (NULL, filedata, conflicts_offset,
18096 sizeof (*econf64), conflictsno, _("conflict"));
a6e9f9df 18097 if (!econf64)
5a814d6d
AM
18098 {
18099 free (iconf);
015dc7e1 18100 return false;
5a814d6d 18101 }
252b5132
RH
18102
18103 for (cnt = 0; cnt < conflictsno; ++cnt)
18104 iconf[cnt] = BYTE_GET (econf64[cnt]);
a6e9f9df
AM
18105
18106 free (econf64);
252b5132
RH
18107 }
18108
d3a49aa8
AM
18109 printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
18110 "\nSection '.conflict' contains %lu entries:\n",
18111 (unsigned long) conflictsno),
c7e7ca54 18112 (unsigned long) conflictsno);
252b5132
RH
18113 puts (_(" Num: Index Value Name"));
18114
18115 for (cnt = 0; cnt < conflictsno; ++cnt)
18116 {
b34976b6 18117 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
e0a31db1 18118
978c4450 18119 if (iconf[cnt] >= filedata->num_dynamic_syms)
e0a31db1 18120 printf (_("<corrupt symbol index>"));
d79b3d50 18121 else
e0a31db1
NC
18122 {
18123 Elf_Internal_Sym * psym;
18124
978c4450 18125 psym = & filedata->dynamic_symbols[iconf[cnt]];
e0a31db1
NC
18126 print_vma (psym->st_value, FULL_HEX);
18127 putchar (' ');
978c4450
AM
18128 if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
18129 print_symbol (25, GET_DYNAMIC_NAME (filedata, psym->st_name));
e0a31db1
NC
18130 else
18131 printf (_("<corrupt: %14ld>"), psym->st_name);
18132 }
31104126 18133 putchar ('\n');
252b5132
RH
18134 }
18135
252b5132
RH
18136 free (iconf);
18137 }
18138
ccb4c951
RS
18139 if (pltgot != 0 && local_gotno != 0)
18140 {
91d6fa6a 18141 bfd_vma ent, local_end, global_end;
bbeee7ea 18142 size_t i, offset;
2cf0635d 18143 unsigned char * data;
82b1b41b 18144 unsigned char * data_end;
bbeee7ea 18145 int addr_size;
ccb4c951 18146
91d6fa6a 18147 ent = pltgot;
ccb4c951
RS
18148 addr_size = (is_32bit_elf ? 4 : 8);
18149 local_end = pltgot + local_gotno * addr_size;
ccb4c951 18150
74e1a04b
NC
18151 /* PR binutils/17533 file: 012-111227-0.004 */
18152 if (symtabno < gotsym)
18153 {
18154 error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
82b1b41b 18155 (unsigned long) gotsym, (unsigned long) symtabno);
015dc7e1 18156 return false;
74e1a04b 18157 }
82b1b41b 18158
74e1a04b 18159 global_end = local_end + (symtabno - gotsym) * addr_size;
82b1b41b
NC
18160 /* PR 17531: file: 54c91a34. */
18161 if (global_end < local_end)
18162 {
18163 error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
015dc7e1 18164 return false;
82b1b41b 18165 }
948f632f 18166
dda8d76d
NC
18167 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
18168 data = (unsigned char *) get_data (NULL, filedata, offset,
9cf03b7e
NC
18169 global_end - pltgot, 1,
18170 _("Global Offset Table data"));
919383ac 18171 /* PR 12855: Null data is handled gracefully throughout. */
82b1b41b 18172 data_end = data + (global_end - pltgot);
59245841 18173
ccb4c951
RS
18174 printf (_("\nPrimary GOT:\n"));
18175 printf (_(" Canonical gp value: "));
18176 print_vma (pltgot + 0x7ff0, LONG_HEX);
18177 printf ("\n\n");
18178
18179 printf (_(" Reserved entries:\n"));
18180 printf (_(" %*s %10s %*s Purpose\n"),
2b692964
NC
18181 addr_size * 2, _("Address"), _("Access"),
18182 addr_size * 2, _("Initial"));
82b1b41b 18183 ent = print_mips_got_entry (data, pltgot, ent, data_end);
2b692964 18184 printf (_(" Lazy resolver\n"));
82b1b41b
NC
18185 if (ent == (bfd_vma) -1)
18186 goto got_print_fail;
75ec1fdb 18187
c4ab9505
MR
18188 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
18189 This entry will be used by some runtime loaders, to store the
18190 module pointer. Otherwise this is an ordinary local entry.
18191 PR 21344: Check for the entry being fully available before
18192 fetching it. */
18193 if (data
18194 && data + ent - pltgot + addr_size <= data_end
18195 && (byte_get (data + ent - pltgot, addr_size)
18196 >> (addr_size * 8 - 1)) != 0)
18197 {
18198 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18199 printf (_(" Module pointer (GNU extension)\n"));
18200 if (ent == (bfd_vma) -1)
18201 goto got_print_fail;
ccb4c951
RS
18202 }
18203 printf ("\n");
18204
f17e9d8a 18205 if (data != NULL && ent < local_end)
ccb4c951
RS
18206 {
18207 printf (_(" Local entries:\n"));
cc5914eb 18208 printf (" %*s %10s %*s\n",
2b692964
NC
18209 addr_size * 2, _("Address"), _("Access"),
18210 addr_size * 2, _("Initial"));
91d6fa6a 18211 while (ent < local_end)
ccb4c951 18212 {
82b1b41b 18213 ent = print_mips_got_entry (data, pltgot, ent, data_end);
ccb4c951 18214 printf ("\n");
82b1b41b
NC
18215 if (ent == (bfd_vma) -1)
18216 goto got_print_fail;
ccb4c951
RS
18217 }
18218 printf ("\n");
18219 }
18220
f17e9d8a 18221 if (data != NULL && gotsym < symtabno)
ccb4c951
RS
18222 {
18223 int sym_width;
18224
18225 printf (_(" Global entries:\n"));
cc5914eb 18226 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
9cf03b7e
NC
18227 addr_size * 2, _("Address"),
18228 _("Access"),
2b692964 18229 addr_size * 2, _("Initial"),
9cf03b7e
NC
18230 addr_size * 2, _("Sym.Val."),
18231 _("Type"),
18232 /* Note for translators: "Ndx" = abbreviated form of "Index". */
18233 _("Ndx"), _("Name"));
0b4362b0 18234
ccb4c951 18235 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
e0a31db1 18236
ccb4c951
RS
18237 for (i = gotsym; i < symtabno; i++)
18238 {
82b1b41b 18239 ent = print_mips_got_entry (data, pltgot, ent, data_end);
ccb4c951 18240 printf (" ");
e0a31db1 18241
978c4450 18242 if (filedata->dynamic_symbols == NULL)
e0a31db1 18243 printf (_("<no dynamic symbols>"));
978c4450 18244 else if (i < filedata->num_dynamic_syms)
e0a31db1 18245 {
978c4450 18246 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
e0a31db1
NC
18247
18248 print_vma (psym->st_value, LONG_HEX);
18249 printf (" %-7s %3s ",
dda8d76d
NC
18250 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18251 get_symbol_index_type (filedata, psym->st_shndx));
e0a31db1 18252
978c4450
AM
18253 if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
18254 print_symbol (sym_width,
18255 GET_DYNAMIC_NAME (filedata, psym->st_name));
e0a31db1
NC
18256 else
18257 printf (_("<corrupt: %14ld>"), psym->st_name);
18258 }
ccb4c951 18259 else
7fc5ac57
JBG
18260 printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
18261 (unsigned long) i);
e0a31db1 18262
ccb4c951 18263 printf ("\n");
82b1b41b
NC
18264 if (ent == (bfd_vma) -1)
18265 break;
ccb4c951
RS
18266 }
18267 printf ("\n");
18268 }
18269
82b1b41b 18270 got_print_fail:
9db70fc3 18271 free (data);
ccb4c951
RS
18272 }
18273
861fb55a
DJ
18274 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
18275 {
91d6fa6a 18276 bfd_vma ent, end;
861fb55a
DJ
18277 size_t offset, rel_offset;
18278 unsigned long count, i;
2cf0635d 18279 unsigned char * data;
861fb55a 18280 int addr_size, sym_width;
2cf0635d 18281 Elf_Internal_Rela * rels;
861fb55a 18282
dda8d76d 18283 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
861fb55a
DJ
18284 if (pltrel == DT_RELA)
18285 {
dda8d76d 18286 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
015dc7e1 18287 return false;
861fb55a
DJ
18288 }
18289 else
18290 {
dda8d76d 18291 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
015dc7e1 18292 return false;
861fb55a
DJ
18293 }
18294
91d6fa6a 18295 ent = mips_pltgot;
861fb55a
DJ
18296 addr_size = (is_32bit_elf ? 4 : 8);
18297 end = mips_pltgot + (2 + count) * addr_size;
18298
dda8d76d
NC
18299 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
18300 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
9cf03b7e 18301 1, _("Procedure Linkage Table data"));
59245841 18302 if (data == NULL)
288f0ba2
AM
18303 {
18304 free (rels);
015dc7e1 18305 return false;
288f0ba2 18306 }
59245841 18307
9cf03b7e 18308 printf ("\nPLT GOT:\n\n");
861fb55a
DJ
18309 printf (_(" Reserved entries:\n"));
18310 printf (_(" %*s %*s Purpose\n"),
2b692964 18311 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
91d6fa6a 18312 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
2b692964 18313 printf (_(" PLT lazy resolver\n"));
91d6fa6a 18314 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
2b692964 18315 printf (_(" Module pointer\n"));
861fb55a
DJ
18316 printf ("\n");
18317
18318 printf (_(" Entries:\n"));
cc5914eb 18319 printf (" %*s %*s %*s %-7s %3s %s\n",
2b692964
NC
18320 addr_size * 2, _("Address"),
18321 addr_size * 2, _("Initial"),
18322 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
861fb55a
DJ
18323 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
18324 for (i = 0; i < count; i++)
18325 {
df97ab2a 18326 unsigned long idx = get_reloc_symindex (rels[i].r_info);
861fb55a 18327
91d6fa6a 18328 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
861fb55a 18329 printf (" ");
e0a31db1 18330
978c4450 18331 if (idx >= filedata->num_dynamic_syms)
df97ab2a 18332 printf (_("<corrupt symbol index: %lu>"), idx);
861fb55a 18333 else
e0a31db1 18334 {
978c4450 18335 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
e0a31db1
NC
18336
18337 print_vma (psym->st_value, LONG_HEX);
18338 printf (" %-7s %3s ",
dda8d76d
NC
18339 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18340 get_symbol_index_type (filedata, psym->st_shndx));
978c4450
AM
18341 if (VALID_DYNAMIC_NAME (filedata, psym->st_name))
18342 print_symbol (sym_width,
18343 GET_DYNAMIC_NAME (filedata, psym->st_name));
e0a31db1
NC
18344 else
18345 printf (_("<corrupt: %14ld>"), psym->st_name);
18346 }
861fb55a
DJ
18347 printf ("\n");
18348 }
18349 printf ("\n");
18350
9db70fc3 18351 free (data);
861fb55a
DJ
18352 free (rels);
18353 }
18354
32ec8896 18355 return res;
252b5132
RH
18356}
18357
015dc7e1 18358static bool
dda8d76d 18359process_nds32_specific (Filedata * filedata)
35c08157
KLC
18360{
18361 Elf_Internal_Shdr *sect = NULL;
18362
dda8d76d 18363 sect = find_section (filedata, ".nds32_e_flags");
9c7b8e9b 18364 if (sect != NULL && sect->sh_size >= 4)
35c08157 18365 {
9c7b8e9b
AM
18366 unsigned char *buf;
18367 unsigned int flag;
35c08157
KLC
18368
18369 printf ("\nNDS32 elf flags section:\n");
9c7b8e9b
AM
18370 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
18371 _("NDS32 elf flags section"));
35c08157 18372
9c7b8e9b 18373 if (buf == NULL)
015dc7e1 18374 return false;
32ec8896 18375
9c7b8e9b
AM
18376 flag = byte_get (buf, 4);
18377 free (buf);
18378 switch (flag & 0x3)
35c08157
KLC
18379 {
18380 case 0:
18381 printf ("(VEC_SIZE):\tNo entry.\n");
18382 break;
18383 case 1:
18384 printf ("(VEC_SIZE):\t4 bytes\n");
18385 break;
18386 case 2:
18387 printf ("(VEC_SIZE):\t16 bytes\n");
18388 break;
18389 case 3:
18390 printf ("(VEC_SIZE):\treserved\n");
18391 break;
18392 }
18393 }
18394
015dc7e1 18395 return true;
35c08157
KLC
18396}
18397
015dc7e1 18398static bool
dda8d76d 18399process_gnu_liblist (Filedata * filedata)
047b2264 18400{
2cf0635d
NC
18401 Elf_Internal_Shdr * section;
18402 Elf_Internal_Shdr * string_sec;
18403 Elf32_External_Lib * elib;
18404 char * strtab;
c256ffe7 18405 size_t strtab_size;
047b2264 18406 size_t cnt;
d3a49aa8 18407 unsigned long num_liblist;
047b2264 18408 unsigned i;
015dc7e1 18409 bool res = true;
047b2264
JJ
18410
18411 if (! do_arch)
015dc7e1 18412 return true;
047b2264 18413
dda8d76d
NC
18414 for (i = 0, section = filedata->section_headers;
18415 i < filedata->file_header.e_shnum;
b34976b6 18416 i++, section++)
047b2264
JJ
18417 {
18418 switch (section->sh_type)
18419 {
18420 case SHT_GNU_LIBLIST:
dda8d76d 18421 if (section->sh_link >= filedata->file_header.e_shnum)
c256ffe7
JJ
18422 break;
18423
3f5e193b 18424 elib = (Elf32_External_Lib *)
dda8d76d 18425 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
9cf03b7e 18426 _("liblist section data"));
047b2264
JJ
18427
18428 if (elib == NULL)
32ec8896 18429 {
015dc7e1 18430 res = false;
32ec8896
NC
18431 break;
18432 }
047b2264 18433
dda8d76d
NC
18434 string_sec = filedata->section_headers + section->sh_link;
18435 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
3f5e193b
NC
18436 string_sec->sh_size,
18437 _("liblist string table"));
047b2264
JJ
18438 if (strtab == NULL
18439 || section->sh_entsize != sizeof (Elf32_External_Lib))
18440 {
18441 free (elib);
2842702f 18442 free (strtab);
015dc7e1 18443 res = false;
047b2264
JJ
18444 break;
18445 }
59245841 18446 strtab_size = string_sec->sh_size;
047b2264 18447
d3a49aa8
AM
18448 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
18449 printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
18450 "\nLibrary list section '%s' contains %lu entries:\n",
18451 num_liblist),
dda8d76d 18452 printable_section_name (filedata, section),
d3a49aa8 18453 num_liblist);
047b2264 18454
2b692964 18455 puts (_(" Library Time Stamp Checksum Version Flags"));
047b2264
JJ
18456
18457 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
18458 ++cnt)
18459 {
18460 Elf32_Lib liblist;
91d6fa6a 18461 time_t atime;
d5b07ef4 18462 char timebuf[128];
2cf0635d 18463 struct tm * tmp;
047b2264
JJ
18464
18465 liblist.l_name = BYTE_GET (elib[cnt].l_name);
91d6fa6a 18466 atime = BYTE_GET (elib[cnt].l_time_stamp);
047b2264
JJ
18467 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18468 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18469 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18470
91d6fa6a 18471 tmp = gmtime (&atime);
e9e44622
JJ
18472 snprintf (timebuf, sizeof (timebuf),
18473 "%04u-%02u-%02uT%02u:%02u:%02u",
18474 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18475 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
047b2264
JJ
18476
18477 printf ("%3lu: ", (unsigned long) cnt);
18478 if (do_wide)
c256ffe7 18479 printf ("%-20s", liblist.l_name < strtab_size
2b692964 18480 ? strtab + liblist.l_name : _("<corrupt>"));
047b2264 18481 else
c256ffe7 18482 printf ("%-20.20s", liblist.l_name < strtab_size
2b692964 18483 ? strtab + liblist.l_name : _("<corrupt>"));
047b2264
JJ
18484 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
18485 liblist.l_version, liblist.l_flags);
18486 }
18487
18488 free (elib);
2842702f 18489 free (strtab);
047b2264
JJ
18490 }
18491 }
18492
32ec8896 18493 return res;
047b2264
JJ
18494}
18495
9437c45b 18496static const char *
dda8d76d 18497get_note_type (Filedata * filedata, unsigned e_type)
779fe533
NC
18498{
18499 static char buff[64];
103f02d3 18500
dda8d76d 18501 if (filedata->file_header.e_type == ET_CORE)
1ec5cd37
NC
18502 switch (e_type)
18503 {
57346661 18504 case NT_AUXV:
1ec5cd37 18505 return _("NT_AUXV (auxiliary vector)");
57346661 18506 case NT_PRSTATUS:
1ec5cd37 18507 return _("NT_PRSTATUS (prstatus structure)");
57346661 18508 case NT_FPREGSET:
1ec5cd37 18509 return _("NT_FPREGSET (floating point registers)");
57346661 18510 case NT_PRPSINFO:
1ec5cd37 18511 return _("NT_PRPSINFO (prpsinfo structure)");
57346661 18512 case NT_TASKSTRUCT:
1ec5cd37 18513 return _("NT_TASKSTRUCT (task structure)");
b63a5e38
AB
18514 case NT_GDB_TDESC:
18515 return _("NT_GDB_TDESC (GDB XML target description)");
57346661 18516 case NT_PRXFPREG:
1ec5cd37 18517 return _("NT_PRXFPREG (user_xfpregs structure)");
e1e95dec
AM
18518 case NT_PPC_VMX:
18519 return _("NT_PPC_VMX (ppc Altivec registers)");
89eeb0bc
LM
18520 case NT_PPC_VSX:
18521 return _("NT_PPC_VSX (ppc VSX registers)");
66c3b5f8
GR
18522 case NT_PPC_TAR:
18523 return _("NT_PPC_TAR (ppc TAR register)");
18524 case NT_PPC_PPR:
18525 return _("NT_PPC_PPR (ppc PPR register)");
18526 case NT_PPC_DSCR:
18527 return _("NT_PPC_DSCR (ppc DSCR register)");
18528 case NT_PPC_EBB:
18529 return _("NT_PPC_EBB (ppc EBB registers)");
18530 case NT_PPC_PMU:
18531 return _("NT_PPC_PMU (ppc PMU registers)");
18532 case NT_PPC_TM_CGPR:
18533 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
18534 case NT_PPC_TM_CFPR:
18535 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
18536 case NT_PPC_TM_CVMX:
18537 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
18538 case NT_PPC_TM_CVSX:
3fd21718 18539 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
66c3b5f8
GR
18540 case NT_PPC_TM_SPR:
18541 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
18542 case NT_PPC_TM_CTAR:
18543 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
18544 case NT_PPC_TM_CPPR:
18545 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
18546 case NT_PPC_TM_CDSCR:
18547 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
ff826ef3
TT
18548 case NT_386_TLS:
18549 return _("NT_386_TLS (x86 TLS information)");
18550 case NT_386_IOPERM:
18551 return _("NT_386_IOPERM (x86 I/O permissions)");
4339cae0
L
18552 case NT_X86_XSTATE:
18553 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
8d58ed37
L
18554 case NT_X86_CET:
18555 return _("NT_X86_CET (x86 CET state)");
0675e188
UW
18556 case NT_S390_HIGH_GPRS:
18557 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
d7eeb400
MS
18558 case NT_S390_TIMER:
18559 return _("NT_S390_TIMER (s390 timer register)");
18560 case NT_S390_TODCMP:
18561 return _("NT_S390_TODCMP (s390 TOD comparator register)");
18562 case NT_S390_TODPREG:
18563 return _("NT_S390_TODPREG (s390 TOD programmable register)");
18564 case NT_S390_CTRS:
18565 return _("NT_S390_CTRS (s390 control registers)");
18566 case NT_S390_PREFIX:
18567 return _("NT_S390_PREFIX (s390 prefix register)");
a367d729
AK
18568 case NT_S390_LAST_BREAK:
18569 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
18570 case NT_S390_SYSTEM_CALL:
18571 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
abb3f6cc
NC
18572 case NT_S390_TDB:
18573 return _("NT_S390_TDB (s390 transaction diagnostic block)");
4ef9f41a
AA
18574 case NT_S390_VXRS_LOW:
18575 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
18576 case NT_S390_VXRS_HIGH:
18577 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
88ab90e8
AA
18578 case NT_S390_GS_CB:
18579 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
18580 case NT_S390_GS_BC:
18581 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
faa9a424
UW
18582 case NT_ARM_VFP:
18583 return _("NT_ARM_VFP (arm VFP registers)");
652451f8
YZ
18584 case NT_ARM_TLS:
18585 return _("NT_ARM_TLS (AArch TLS registers)");
18586 case NT_ARM_HW_BREAK:
18587 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
18588 case NT_ARM_HW_WATCH:
18589 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
27456742
AK
18590 case NT_ARC_V2:
18591 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
db6092f3
AB
18592 case NT_RISCV_CSR:
18593 return _("NT_RISCV_CSR (RISC-V control and status registers)");
57346661 18594 case NT_PSTATUS:
1ec5cd37 18595 return _("NT_PSTATUS (pstatus structure)");
57346661 18596 case NT_FPREGS:
1ec5cd37 18597 return _("NT_FPREGS (floating point registers)");
57346661 18598 case NT_PSINFO:
1ec5cd37 18599 return _("NT_PSINFO (psinfo structure)");
57346661 18600 case NT_LWPSTATUS:
1ec5cd37 18601 return _("NT_LWPSTATUS (lwpstatus_t structure)");
57346661 18602 case NT_LWPSINFO:
1ec5cd37 18603 return _("NT_LWPSINFO (lwpsinfo_t structure)");
57346661 18604 case NT_WIN32PSTATUS:
1ec5cd37 18605 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
9ece1fa9
TT
18606 case NT_SIGINFO:
18607 return _("NT_SIGINFO (siginfo_t data)");
18608 case NT_FILE:
18609 return _("NT_FILE (mapped files)");
1ec5cd37
NC
18610 default:
18611 break;
18612 }
18613 else
18614 switch (e_type)
18615 {
18616 case NT_VERSION:
18617 return _("NT_VERSION (version)");
18618 case NT_ARCH:
18619 return _("NT_ARCH (architecture)");
9ef920e9 18620 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
6f156d7a 18621 return _("OPEN");
9ef920e9 18622 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
6f156d7a 18623 return _("func");
1ec5cd37
NC
18624 default:
18625 break;
18626 }
18627
e9e44622 18628 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
1ec5cd37 18629 return buff;
779fe533
NC
18630}
18631
015dc7e1 18632static bool
9ece1fa9
TT
18633print_core_note (Elf_Internal_Note *pnote)
18634{
18635 unsigned int addr_size = is_32bit_elf ? 4 : 8;
18636 bfd_vma count, page_size;
18637 unsigned char *descdata, *filenames, *descend;
18638
18639 if (pnote->type != NT_FILE)
04ac15ab
AS
18640 {
18641 if (do_wide)
18642 printf ("\n");
015dc7e1 18643 return true;
04ac15ab 18644 }
9ece1fa9
TT
18645
18646#ifndef BFD64
18647 if (!is_32bit_elf)
18648 {
18649 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
18650 /* Still "successful". */
015dc7e1 18651 return true;
9ece1fa9
TT
18652 }
18653#endif
18654
18655 if (pnote->descsz < 2 * addr_size)
18656 {
32ec8896 18657 error (_(" Malformed note - too short for header\n"));
015dc7e1 18658 return false;
9ece1fa9
TT
18659 }
18660
18661 descdata = (unsigned char *) pnote->descdata;
18662 descend = descdata + pnote->descsz;
18663
18664 if (descdata[pnote->descsz - 1] != '\0')
18665 {
32ec8896 18666 error (_(" Malformed note - does not end with \\0\n"));
015dc7e1 18667 return false;
9ece1fa9
TT
18668 }
18669
18670 count = byte_get (descdata, addr_size);
18671 descdata += addr_size;
18672
18673 page_size = byte_get (descdata, addr_size);
18674 descdata += addr_size;
18675
5396a86e
AM
18676 if (count > ((bfd_vma) -1 - 2 * addr_size) / (3 * addr_size)
18677 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
9ece1fa9 18678 {
32ec8896 18679 error (_(" Malformed note - too short for supplied file count\n"));
015dc7e1 18680 return false;
9ece1fa9
TT
18681 }
18682
18683 printf (_(" Page size: "));
18684 print_vma (page_size, DEC);
18685 printf ("\n");
18686
18687 printf (_(" %*s%*s%*s\n"),
18688 (int) (2 + 2 * addr_size), _("Start"),
18689 (int) (4 + 2 * addr_size), _("End"),
18690 (int) (4 + 2 * addr_size), _("Page Offset"));
18691 filenames = descdata + count * 3 * addr_size;
595712bb 18692 while (count-- > 0)
9ece1fa9
TT
18693 {
18694 bfd_vma start, end, file_ofs;
18695
18696 if (filenames == descend)
18697 {
32ec8896 18698 error (_(" Malformed note - filenames end too early\n"));
015dc7e1 18699 return false;
9ece1fa9
TT
18700 }
18701
18702 start = byte_get (descdata, addr_size);
18703 descdata += addr_size;
18704 end = byte_get (descdata, addr_size);
18705 descdata += addr_size;
18706 file_ofs = byte_get (descdata, addr_size);
18707 descdata += addr_size;
18708
18709 printf (" ");
18710 print_vma (start, FULL_HEX);
18711 printf (" ");
18712 print_vma (end, FULL_HEX);
18713 printf (" ");
18714 print_vma (file_ofs, FULL_HEX);
18715 printf ("\n %s\n", filenames);
18716
18717 filenames += 1 + strlen ((char *) filenames);
18718 }
18719
015dc7e1 18720 return true;
9ece1fa9
TT
18721}
18722
1118d252
RM
18723static const char *
18724get_gnu_elf_note_type (unsigned e_type)
18725{
1449284b 18726 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
1118d252
RM
18727 switch (e_type)
18728 {
18729 case NT_GNU_ABI_TAG:
18730 return _("NT_GNU_ABI_TAG (ABI version tag)");
18731 case NT_GNU_HWCAP:
18732 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
18733 case NT_GNU_BUILD_ID:
18734 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
0297aed6
DM
18735 case NT_GNU_GOLD_VERSION:
18736 return _("NT_GNU_GOLD_VERSION (gold version)");
9ef920e9
NC
18737 case NT_GNU_PROPERTY_TYPE_0:
18738 return _("NT_GNU_PROPERTY_TYPE_0");
18739 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
18740 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
18741 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
18742 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
1118d252 18743 default:
1449284b
NC
18744 {
18745 static char buff[64];
1118d252 18746
1449284b
NC
18747 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
18748 return buff;
18749 }
18750 }
1118d252
RM
18751}
18752
a9eafb08
L
18753static void
18754decode_x86_compat_isa (unsigned int bitmask)
18755{
18756 while (bitmask)
18757 {
18758 unsigned int bit = bitmask & (- bitmask);
18759
18760 bitmask &= ~ bit;
18761 switch (bit)
18762 {
18763 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
18764 printf ("i486");
18765 break;
18766 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
18767 printf ("586");
18768 break;
18769 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
18770 printf ("686");
18771 break;
18772 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
18773 printf ("SSE");
18774 break;
18775 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
18776 printf ("SSE2");
18777 break;
18778 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
18779 printf ("SSE3");
18780 break;
18781 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
18782 printf ("SSSE3");
18783 break;
18784 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
18785 printf ("SSE4_1");
18786 break;
18787 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
18788 printf ("SSE4_2");
18789 break;
18790 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
18791 printf ("AVX");
18792 break;
18793 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
18794 printf ("AVX2");
18795 break;
18796 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
18797 printf ("AVX512F");
18798 break;
18799 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
18800 printf ("AVX512CD");
18801 break;
18802 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
18803 printf ("AVX512ER");
18804 break;
18805 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
18806 printf ("AVX512PF");
18807 break;
18808 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
18809 printf ("AVX512VL");
18810 break;
18811 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
18812 printf ("AVX512DQ");
18813 break;
18814 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
18815 printf ("AVX512BW");
18816 break;
65b3d26e
L
18817 default:
18818 printf (_("<unknown: %x>"), bit);
18819 break;
a9eafb08
L
18820 }
18821 if (bitmask)
18822 printf (", ");
18823 }
18824}
18825
9ef920e9 18826static void
32930e4e 18827decode_x86_compat_2_isa (unsigned int bitmask)
9ef920e9 18828{
0a59decb 18829 if (!bitmask)
90c745dc
L
18830 {
18831 printf (_("<None>"));
18832 return;
18833 }
90c745dc 18834
9ef920e9
NC
18835 while (bitmask)
18836 {
1fc87489 18837 unsigned int bit = bitmask & (- bitmask);
9ef920e9
NC
18838
18839 bitmask &= ~ bit;
18840 switch (bit)
18841 {
32930e4e 18842 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
a9eafb08
L
18843 printf ("CMOV");
18844 break;
32930e4e 18845 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
a9eafb08
L
18846 printf ("SSE");
18847 break;
32930e4e 18848 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
a9eafb08
L
18849 printf ("SSE2");
18850 break;
32930e4e 18851 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
a9eafb08
L
18852 printf ("SSE3");
18853 break;
32930e4e 18854 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
a9eafb08
L
18855 printf ("SSSE3");
18856 break;
32930e4e 18857 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
a9eafb08
L
18858 printf ("SSE4_1");
18859 break;
32930e4e 18860 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
a9eafb08
L
18861 printf ("SSE4_2");
18862 break;
32930e4e 18863 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
a9eafb08
L
18864 printf ("AVX");
18865 break;
32930e4e 18866 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
a9eafb08
L
18867 printf ("AVX2");
18868 break;
32930e4e 18869 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
a9eafb08
L
18870 printf ("FMA");
18871 break;
32930e4e 18872 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
a9eafb08
L
18873 printf ("AVX512F");
18874 break;
32930e4e 18875 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
a9eafb08
L
18876 printf ("AVX512CD");
18877 break;
32930e4e 18878 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
a9eafb08
L
18879 printf ("AVX512ER");
18880 break;
32930e4e 18881 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
a9eafb08
L
18882 printf ("AVX512PF");
18883 break;
32930e4e 18884 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
a9eafb08
L
18885 printf ("AVX512VL");
18886 break;
32930e4e 18887 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
a9eafb08
L
18888 printf ("AVX512DQ");
18889 break;
32930e4e 18890 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
a9eafb08
L
18891 printf ("AVX512BW");
18892 break;
32930e4e 18893 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
a9eafb08
L
18894 printf ("AVX512_4FMAPS");
18895 break;
32930e4e 18896 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
a9eafb08
L
18897 printf ("AVX512_4VNNIW");
18898 break;
32930e4e 18899 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
a9eafb08
L
18900 printf ("AVX512_BITALG");
18901 break;
32930e4e 18902 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
a9eafb08
L
18903 printf ("AVX512_IFMA");
18904 break;
32930e4e 18905 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
a9eafb08
L
18906 printf ("AVX512_VBMI");
18907 break;
32930e4e 18908 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
a9eafb08
L
18909 printf ("AVX512_VBMI2");
18910 break;
32930e4e 18911 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
a9eafb08
L
18912 printf ("AVX512_VNNI");
18913 break;
32930e4e 18914 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
462cac58
L
18915 printf ("AVX512_BF16");
18916 break;
65b3d26e
L
18917 default:
18918 printf (_("<unknown: %x>"), bit);
18919 break;
9ef920e9
NC
18920 }
18921 if (bitmask)
18922 printf (", ");
18923 }
18924}
18925
32930e4e
L
18926static void
18927decode_x86_isa (unsigned int bitmask)
18928{
32930e4e
L
18929 while (bitmask)
18930 {
18931 unsigned int bit = bitmask & (- bitmask);
18932
18933 bitmask &= ~ bit;
18934 switch (bit)
18935 {
b0ab0693
L
18936 case GNU_PROPERTY_X86_ISA_1_BASELINE:
18937 printf ("x86-64-baseline");
18938 break;
32930e4e
L
18939 case GNU_PROPERTY_X86_ISA_1_V2:
18940 printf ("x86-64-v2");
18941 break;
18942 case GNU_PROPERTY_X86_ISA_1_V3:
18943 printf ("x86-64-v3");
18944 break;
18945 case GNU_PROPERTY_X86_ISA_1_V4:
18946 printf ("x86-64-v4");
18947 break;
18948 default:
18949 printf (_("<unknown: %x>"), bit);
18950 break;
18951 }
18952 if (bitmask)
18953 printf (", ");
18954 }
18955}
18956
ee2fdd6f 18957static void
a9eafb08 18958decode_x86_feature_1 (unsigned int bitmask)
ee2fdd6f 18959{
0a59decb 18960 if (!bitmask)
90c745dc
L
18961 {
18962 printf (_("<None>"));
18963 return;
18964 }
90c745dc 18965
ee2fdd6f
L
18966 while (bitmask)
18967 {
18968 unsigned int bit = bitmask & (- bitmask);
18969
18970 bitmask &= ~ bit;
18971 switch (bit)
18972 {
18973 case GNU_PROPERTY_X86_FEATURE_1_IBT:
a9eafb08 18974 printf ("IBT");
ee2fdd6f 18975 break;
48580982 18976 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
a9eafb08 18977 printf ("SHSTK");
48580982 18978 break;
279d901e
L
18979 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
18980 printf ("LAM_U48");
18981 break;
18982 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
18983 printf ("LAM_U57");
18984 break;
ee2fdd6f
L
18985 default:
18986 printf (_("<unknown: %x>"), bit);
18987 break;
18988 }
18989 if (bitmask)
18990 printf (", ");
18991 }
18992}
18993
a9eafb08
L
18994static void
18995decode_x86_feature_2 (unsigned int bitmask)
18996{
0a59decb 18997 if (!bitmask)
90c745dc
L
18998 {
18999 printf (_("<None>"));
19000 return;
19001 }
90c745dc 19002
a9eafb08
L
19003 while (bitmask)
19004 {
19005 unsigned int bit = bitmask & (- bitmask);
19006
19007 bitmask &= ~ bit;
19008 switch (bit)
19009 {
19010 case GNU_PROPERTY_X86_FEATURE_2_X86:
19011 printf ("x86");
19012 break;
19013 case GNU_PROPERTY_X86_FEATURE_2_X87:
19014 printf ("x87");
19015 break;
19016 case GNU_PROPERTY_X86_FEATURE_2_MMX:
19017 printf ("MMX");
19018 break;
19019 case GNU_PROPERTY_X86_FEATURE_2_XMM:
19020 printf ("XMM");
19021 break;
19022 case GNU_PROPERTY_X86_FEATURE_2_YMM:
19023 printf ("YMM");
19024 break;
19025 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
19026 printf ("ZMM");
19027 break;
a308b89d
L
19028 case GNU_PROPERTY_X86_FEATURE_2_TMM:
19029 printf ("TMM");
19030 break;
32930e4e
L
19031 case GNU_PROPERTY_X86_FEATURE_2_MASK:
19032 printf ("MASK");
19033 break;
a9eafb08
L
19034 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
19035 printf ("FXSR");
19036 break;
19037 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
19038 printf ("XSAVE");
19039 break;
19040 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
19041 printf ("XSAVEOPT");
19042 break;
19043 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
19044 printf ("XSAVEC");
19045 break;
65b3d26e
L
19046 default:
19047 printf (_("<unknown: %x>"), bit);
19048 break;
a9eafb08
L
19049 }
19050 if (bitmask)
19051 printf (", ");
19052 }
19053}
19054
cd702818
SD
19055static void
19056decode_aarch64_feature_1_and (unsigned int bitmask)
19057{
19058 while (bitmask)
19059 {
19060 unsigned int bit = bitmask & (- bitmask);
19061
19062 bitmask &= ~ bit;
19063 switch (bit)
19064 {
19065 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
19066 printf ("BTI");
19067 break;
19068
19069 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
19070 printf ("PAC");
19071 break;
19072
19073 default:
19074 printf (_("<unknown: %x>"), bit);
19075 break;
19076 }
19077 if (bitmask)
19078 printf (", ");
19079 }
19080}
19081
9ef920e9 19082static void
dda8d76d 19083print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
9ef920e9
NC
19084{
19085 unsigned char * ptr = (unsigned char *) pnote->descdata;
19086 unsigned char * ptr_end = ptr + pnote->descsz;
19087 unsigned int size = is_32bit_elf ? 4 : 8;
19088
19089 printf (_(" Properties: "));
19090
1fc87489 19091 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
9ef920e9
NC
19092 {
19093 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
19094 return;
19095 }
19096
6ab2c4ed 19097 while (ptr < ptr_end)
9ef920e9 19098 {
1fc87489 19099 unsigned int j;
6ab2c4ed
MC
19100 unsigned int type;
19101 unsigned int datasz;
19102
19103 if ((size_t) (ptr_end - ptr) < 8)
19104 {
19105 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
19106 break;
19107 }
19108
19109 type = byte_get (ptr, 4);
19110 datasz = byte_get (ptr + 4, 4);
9ef920e9 19111
1fc87489 19112 ptr += 8;
9ef920e9 19113
6ab2c4ed 19114 if (datasz > (size_t) (ptr_end - ptr))
9ef920e9 19115 {
1fc87489
L
19116 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
19117 type, datasz);
9ef920e9 19118 break;
1fc87489 19119 }
9ef920e9 19120
1fc87489
L
19121 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
19122 {
dda8d76d
NC
19123 if (filedata->file_header.e_machine == EM_X86_64
19124 || filedata->file_header.e_machine == EM_IAMCU
19125 || filedata->file_header.e_machine == EM_386)
1fc87489 19126 {
aa7bca9b
L
19127 unsigned int bitmask;
19128
19129 if (datasz == 4)
0a59decb 19130 bitmask = byte_get (ptr, 4);
aa7bca9b
L
19131 else
19132 bitmask = 0;
19133
1fc87489
L
19134 switch (type)
19135 {
19136 case GNU_PROPERTY_X86_ISA_1_USED:
1fc87489 19137 if (datasz != 4)
aa7bca9b
L
19138 printf (_("x86 ISA used: <corrupt length: %#x> "),
19139 datasz);
1fc87489 19140 else
aa7bca9b
L
19141 {
19142 printf ("x86 ISA used: ");
19143 decode_x86_isa (bitmask);
19144 }
1fc87489 19145 goto next;
9ef920e9 19146
1fc87489 19147 case GNU_PROPERTY_X86_ISA_1_NEEDED:
1fc87489 19148 if (datasz != 4)
aa7bca9b
L
19149 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19150 datasz);
1fc87489 19151 else
aa7bca9b
L
19152 {
19153 printf ("x86 ISA needed: ");
19154 decode_x86_isa (bitmask);
19155 }
1fc87489 19156 goto next;
9ef920e9 19157
ee2fdd6f 19158 case GNU_PROPERTY_X86_FEATURE_1_AND:
ee2fdd6f 19159 if (datasz != 4)
aa7bca9b
L
19160 printf (_("x86 feature: <corrupt length: %#x> "),
19161 datasz);
ee2fdd6f 19162 else
aa7bca9b
L
19163 {
19164 printf ("x86 feature: ");
a9eafb08
L
19165 decode_x86_feature_1 (bitmask);
19166 }
19167 goto next;
19168
19169 case GNU_PROPERTY_X86_FEATURE_2_USED:
19170 if (datasz != 4)
19171 printf (_("x86 feature used: <corrupt length: %#x> "),
19172 datasz);
19173 else
19174 {
19175 printf ("x86 feature used: ");
19176 decode_x86_feature_2 (bitmask);
19177 }
19178 goto next;
19179
19180 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
19181 if (datasz != 4)
19182 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
19183 else
19184 {
19185 printf ("x86 feature needed: ");
19186 decode_x86_feature_2 (bitmask);
19187 }
19188 goto next;
19189
19190 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
19191 if (datasz != 4)
19192 printf (_("x86 ISA used: <corrupt length: %#x> "),
19193 datasz);
19194 else
19195 {
19196 printf ("x86 ISA used: ");
19197 decode_x86_compat_isa (bitmask);
19198 }
19199 goto next;
19200
19201 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
19202 if (datasz != 4)
19203 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19204 datasz);
19205 else
19206 {
19207 printf ("x86 ISA needed: ");
19208 decode_x86_compat_isa (bitmask);
aa7bca9b 19209 }
ee2fdd6f
L
19210 goto next;
19211
32930e4e
L
19212 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
19213 if (datasz != 4)
19214 printf (_("x86 ISA used: <corrupt length: %#x> "),
19215 datasz);
19216 else
19217 {
19218 printf ("x86 ISA used: ");
19219 decode_x86_compat_2_isa (bitmask);
19220 }
19221 goto next;
19222
19223 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
19224 if (datasz != 4)
19225 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19226 datasz);
19227 else
19228 {
19229 printf ("x86 ISA needed: ");
19230 decode_x86_compat_2_isa (bitmask);
19231 }
19232 goto next;
19233
1fc87489
L
19234 default:
19235 break;
19236 }
19237 }
cd702818
SD
19238 else if (filedata->file_header.e_machine == EM_AARCH64)
19239 {
19240 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
19241 {
19242 printf ("AArch64 feature: ");
19243 if (datasz != 4)
19244 printf (_("<corrupt length: %#x> "), datasz);
19245 else
19246 decode_aarch64_feature_1_and (byte_get (ptr, 4));
19247 goto next;
19248 }
19249 }
1fc87489
L
19250 }
19251 else
19252 {
19253 switch (type)
9ef920e9 19254 {
1fc87489
L
19255 case GNU_PROPERTY_STACK_SIZE:
19256 printf (_("stack size: "));
19257 if (datasz != size)
19258 printf (_("<corrupt length: %#x> "), datasz);
19259 else
19260 printf ("%#lx", (unsigned long) byte_get (ptr, size));
19261 goto next;
19262
19263 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
19264 printf ("no copy on protected ");
19265 if (datasz)
19266 printf (_("<corrupt length: %#x> "), datasz);
19267 goto next;
19268
19269 default:
9ef920e9
NC
19270 break;
19271 }
9ef920e9
NC
19272 }
19273
1fc87489
L
19274 if (type < GNU_PROPERTY_LOPROC)
19275 printf (_("<unknown type %#x data: "), type);
19276 else if (type < GNU_PROPERTY_LOUSER)
8c3853d9 19277 printf (_("<processor-specific type %#x data: "), type);
1fc87489
L
19278 else
19279 printf (_("<application-specific type %#x data: "), type);
19280 for (j = 0; j < datasz; ++j)
19281 printf ("%02x ", ptr[j] & 0xff);
19282 printf (">");
19283
dc1e8a47 19284 next:
9ef920e9 19285 ptr += ((datasz + (size - 1)) & ~ (size - 1));
1fc87489
L
19286 if (ptr == ptr_end)
19287 break;
1fc87489 19288
6ab2c4ed
MC
19289 if (do_wide)
19290 printf (", ");
19291 else
19292 printf ("\n\t");
9ef920e9
NC
19293 }
19294
19295 printf ("\n");
19296}
19297
015dc7e1 19298static bool
dda8d76d 19299print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
664f90a3 19300{
1449284b 19301 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
664f90a3
TT
19302 switch (pnote->type)
19303 {
19304 case NT_GNU_BUILD_ID:
19305 {
19306 unsigned long i;
19307
19308 printf (_(" Build ID: "));
19309 for (i = 0; i < pnote->descsz; ++i)
19310 printf ("%02x", pnote->descdata[i] & 0xff);
9cf03b7e 19311 printf ("\n");
664f90a3
TT
19312 }
19313 break;
19314
19315 case NT_GNU_ABI_TAG:
19316 {
19317 unsigned long os, major, minor, subminor;
19318 const char *osname;
19319
3102e897
NC
19320 /* PR 17531: file: 030-599401-0.004. */
19321 if (pnote->descsz < 16)
19322 {
19323 printf (_(" <corrupt GNU_ABI_TAG>\n"));
19324 break;
19325 }
19326
664f90a3
TT
19327 os = byte_get ((unsigned char *) pnote->descdata, 4);
19328 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
19329 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
19330 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
19331
19332 switch (os)
19333 {
19334 case GNU_ABI_TAG_LINUX:
19335 osname = "Linux";
19336 break;
19337 case GNU_ABI_TAG_HURD:
19338 osname = "Hurd";
19339 break;
19340 case GNU_ABI_TAG_SOLARIS:
19341 osname = "Solaris";
19342 break;
19343 case GNU_ABI_TAG_FREEBSD:
19344 osname = "FreeBSD";
19345 break;
19346 case GNU_ABI_TAG_NETBSD:
19347 osname = "NetBSD";
19348 break;
14ae95f2
RM
19349 case GNU_ABI_TAG_SYLLABLE:
19350 osname = "Syllable";
19351 break;
19352 case GNU_ABI_TAG_NACL:
19353 osname = "NaCl";
19354 break;
664f90a3
TT
19355 default:
19356 osname = "Unknown";
19357 break;
19358 }
19359
19360 printf (_(" OS: %s, ABI: %ld.%ld.%ld\n"), osname,
19361 major, minor, subminor);
19362 }
19363 break;
926c5385
CC
19364
19365 case NT_GNU_GOLD_VERSION:
19366 {
19367 unsigned long i;
19368
19369 printf (_(" Version: "));
19370 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
19371 printf ("%c", pnote->descdata[i]);
19372 printf ("\n");
19373 }
19374 break;
1449284b
NC
19375
19376 case NT_GNU_HWCAP:
19377 {
19378 unsigned long num_entries, mask;
19379
19380 /* Hardware capabilities information. Word 0 is the number of entries.
19381 Word 1 is a bitmask of enabled entries. The rest of the descriptor
19382 is a series of entries, where each entry is a single byte followed
19383 by a nul terminated string. The byte gives the bit number to test
19384 if enabled in the bitmask. */
19385 printf (_(" Hardware Capabilities: "));
19386 if (pnote->descsz < 8)
19387 {
32ec8896 19388 error (_("<corrupt GNU_HWCAP>\n"));
015dc7e1 19389 return false;
1449284b
NC
19390 }
19391 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
19392 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
19393 printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
19394 /* FIXME: Add code to display the entries... */
19395 }
19396 break;
19397
9ef920e9 19398 case NT_GNU_PROPERTY_TYPE_0:
dda8d76d 19399 print_gnu_property_note (filedata, pnote);
9ef920e9 19400 break;
9abca702 19401
1449284b
NC
19402 default:
19403 /* Handle unrecognised types. An error message should have already been
19404 created by get_gnu_elf_note_type(), so all that we need to do is to
19405 display the data. */
19406 {
19407 unsigned long i;
19408
19409 printf (_(" Description data: "));
19410 for (i = 0; i < pnote->descsz; ++i)
19411 printf ("%02x ", pnote->descdata[i] & 0xff);
19412 printf ("\n");
19413 }
19414 break;
664f90a3
TT
19415 }
19416
015dc7e1 19417 return true;
664f90a3
TT
19418}
19419
685080f2
NC
19420static const char *
19421get_v850_elf_note_type (enum v850_notes n_type)
19422{
19423 static char buff[64];
19424
19425 switch (n_type)
19426 {
19427 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
19428 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
19429 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
19430 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
19431 case V850_NOTE_CACHE_INFO: return _("Use of cache");
19432 case V850_NOTE_MMU_INFO: return _("Use of MMU");
19433 default:
19434 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
19435 return buff;
19436 }
19437}
19438
015dc7e1 19439static bool
685080f2
NC
19440print_v850_note (Elf_Internal_Note * pnote)
19441{
19442 unsigned int val;
19443
19444 if (pnote->descsz != 4)
015dc7e1 19445 return false;
32ec8896 19446
685080f2
NC
19447 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
19448
19449 if (val == 0)
19450 {
19451 printf (_("not set\n"));
015dc7e1 19452 return true;
685080f2
NC
19453 }
19454
19455 switch (pnote->type)
19456 {
19457 case V850_NOTE_ALIGNMENT:
19458 switch (val)
19459 {
015dc7e1
AM
19460 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
19461 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
685080f2
NC
19462 }
19463 break;
14ae95f2 19464
685080f2
NC
19465 case V850_NOTE_DATA_SIZE:
19466 switch (val)
19467 {
015dc7e1
AM
19468 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
19469 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
685080f2
NC
19470 }
19471 break;
14ae95f2 19472
685080f2
NC
19473 case V850_NOTE_FPU_INFO:
19474 switch (val)
19475 {
015dc7e1
AM
19476 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
19477 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
685080f2
NC
19478 }
19479 break;
14ae95f2 19480
685080f2
NC
19481 case V850_NOTE_MMU_INFO:
19482 case V850_NOTE_CACHE_INFO:
19483 case V850_NOTE_SIMD_INFO:
19484 if (val == EF_RH850_SIMD)
19485 {
19486 printf (_("yes\n"));
015dc7e1 19487 return true;
685080f2
NC
19488 }
19489 break;
19490
19491 default:
19492 /* An 'unknown note type' message will already have been displayed. */
19493 break;
19494 }
19495
19496 printf (_("unknown value: %x\n"), val);
015dc7e1 19497 return false;
685080f2
NC
19498}
19499
015dc7e1 19500static bool
c6056a74
SF
19501process_netbsd_elf_note (Elf_Internal_Note * pnote)
19502{
19503 unsigned int version;
19504
19505 switch (pnote->type)
19506 {
19507 case NT_NETBSD_IDENT:
b966f55f
AM
19508 if (pnote->descsz < 1)
19509 break;
c6056a74
SF
19510 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
19511 if ((version / 10000) % 100)
b966f55f 19512 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
c6056a74
SF
19513 version, version / 100000000, (version / 1000000) % 100,
19514 (version / 10000) % 100 > 26 ? "Z" : "",
15f205b1 19515 'A' + (version / 10000) % 26);
c6056a74
SF
19516 else
19517 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
b966f55f 19518 version, version / 100000000, (version / 1000000) % 100,
15f205b1 19519 (version / 100) % 100);
015dc7e1 19520 return true;
c6056a74
SF
19521
19522 case NT_NETBSD_MARCH:
9abca702 19523 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
c6056a74 19524 pnote->descdata);
015dc7e1 19525 return true;
c6056a74 19526
9abca702
CZ
19527#ifdef NT_NETBSD_PAX
19528 case NT_NETBSD_PAX:
b966f55f
AM
19529 if (pnote->descsz < 1)
19530 break;
9abca702
CZ
19531 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
19532 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
19533 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
19534 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
19535 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
19536 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
19537 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
19538 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
015dc7e1 19539 return true;
9abca702 19540#endif
c6056a74 19541 }
b966f55f
AM
19542
19543 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
19544 pnote->descsz, pnote->type);
015dc7e1 19545 return false;
c6056a74
SF
19546}
19547
f4ddf30f 19548static const char *
dda8d76d 19549get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
f4ddf30f 19550{
f4ddf30f
JB
19551 switch (e_type)
19552 {
19553 case NT_FREEBSD_THRMISC:
19554 return _("NT_THRMISC (thrmisc structure)");
19555 case NT_FREEBSD_PROCSTAT_PROC:
19556 return _("NT_PROCSTAT_PROC (proc data)");
19557 case NT_FREEBSD_PROCSTAT_FILES:
19558 return _("NT_PROCSTAT_FILES (files data)");
19559 case NT_FREEBSD_PROCSTAT_VMMAP:
19560 return _("NT_PROCSTAT_VMMAP (vmmap data)");
19561 case NT_FREEBSD_PROCSTAT_GROUPS:
19562 return _("NT_PROCSTAT_GROUPS (groups data)");
19563 case NT_FREEBSD_PROCSTAT_UMASK:
19564 return _("NT_PROCSTAT_UMASK (umask data)");
19565 case NT_FREEBSD_PROCSTAT_RLIMIT:
19566 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
19567 case NT_FREEBSD_PROCSTAT_OSREL:
19568 return _("NT_PROCSTAT_OSREL (osreldate data)");
19569 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
19570 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
19571 case NT_FREEBSD_PROCSTAT_AUXV:
19572 return _("NT_PROCSTAT_AUXV (auxv data)");
0b9305ed
JB
19573 case NT_FREEBSD_PTLWPINFO:
19574 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
f4ddf30f 19575 }
dda8d76d 19576 return get_note_type (filedata, e_type);
f4ddf30f
JB
19577}
19578
9437c45b 19579static const char *
dda8d76d 19580get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
9437c45b
JT
19581{
19582 static char buff[64];
19583
540e6170
CZ
19584 switch (e_type)
19585 {
19586 case NT_NETBSDCORE_PROCINFO:
19587 /* NetBSD core "procinfo" structure. */
19588 return _("NetBSD procinfo structure");
9437c45b 19589
540e6170
CZ
19590#ifdef NT_NETBSDCORE_AUXV
19591 case NT_NETBSDCORE_AUXV:
19592 return _("NetBSD ELF auxiliary vector data");
19593#endif
9437c45b 19594
06d949ec
KR
19595#ifdef NT_NETBSDCORE_LWPSTATUS
19596 case NT_NETBSDCORE_LWPSTATUS:
19597 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
19598#endif
19599
540e6170 19600 default:
06d949ec 19601 /* As of Jan 2020 there are no other machine-independent notes
540e6170
CZ
19602 defined for NetBSD core files. If the note type is less
19603 than the start of the machine-dependent note types, we don't
19604 understand it. */
19605
19606 if (e_type < NT_NETBSDCORE_FIRSTMACH)
19607 {
19608 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19609 return buff;
19610 }
19611 break;
9437c45b
JT
19612 }
19613
dda8d76d 19614 switch (filedata->file_header.e_machine)
9437c45b
JT
19615 {
19616 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
19617 and PT_GETFPREGS == mach+2. */
19618
19619 case EM_OLD_ALPHA:
19620 case EM_ALPHA:
19621 case EM_SPARC:
19622 case EM_SPARC32PLUS:
19623 case EM_SPARCV9:
19624 switch (e_type)
19625 {
2b692964 19626 case NT_NETBSDCORE_FIRSTMACH + 0:
b4db1224 19627 return _("PT_GETREGS (reg structure)");
2b692964 19628 case NT_NETBSDCORE_FIRSTMACH + 2:
b4db1224 19629 return _("PT_GETFPREGS (fpreg structure)");
9437c45b
JT
19630 default:
19631 break;
19632 }
19633 break;
19634
c0d38b0e
CZ
19635 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
19636 There's also old PT___GETREGS40 == mach + 1 for old reg
19637 structure which lacks GBR. */
19638 case EM_SH:
19639 switch (e_type)
19640 {
19641 case NT_NETBSDCORE_FIRSTMACH + 1:
19642 return _("PT___GETREGS40 (old reg structure)");
19643 case NT_NETBSDCORE_FIRSTMACH + 3:
19644 return _("PT_GETREGS (reg structure)");
19645 case NT_NETBSDCORE_FIRSTMACH + 5:
19646 return _("PT_GETFPREGS (fpreg structure)");
19647 default:
19648 break;
19649 }
19650 break;
19651
9437c45b
JT
19652 /* On all other arch's, PT_GETREGS == mach+1 and
19653 PT_GETFPREGS == mach+3. */
19654 default:
19655 switch (e_type)
19656 {
2b692964 19657 case NT_NETBSDCORE_FIRSTMACH + 1:
b4db1224 19658 return _("PT_GETREGS (reg structure)");
2b692964 19659 case NT_NETBSDCORE_FIRSTMACH + 3:
b4db1224 19660 return _("PT_GETFPREGS (fpreg structure)");
9437c45b
JT
19661 default:
19662 break;
19663 }
19664 }
19665
9cf03b7e 19666 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
e9e44622 19667 e_type - NT_NETBSDCORE_FIRSTMACH);
9437c45b
JT
19668 return buff;
19669}
19670
70616151
TT
19671static const char *
19672get_stapsdt_note_type (unsigned e_type)
19673{
19674 static char buff[64];
19675
19676 switch (e_type)
19677 {
19678 case NT_STAPSDT:
19679 return _("NT_STAPSDT (SystemTap probe descriptors)");
19680
19681 default:
19682 break;
19683 }
19684
19685 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19686 return buff;
19687}
19688
015dc7e1 19689static bool
c6a9fc58
TT
19690print_stapsdt_note (Elf_Internal_Note *pnote)
19691{
3ca60c57
NC
19692 size_t len, maxlen;
19693 unsigned long addr_size = is_32bit_elf ? 4 : 8;
c6a9fc58
TT
19694 char *data = pnote->descdata;
19695 char *data_end = pnote->descdata + pnote->descsz;
19696 bfd_vma pc, base_addr, semaphore;
19697 char *provider, *probe, *arg_fmt;
19698
3ca60c57
NC
19699 if (pnote->descsz < (addr_size * 3))
19700 goto stapdt_note_too_small;
19701
c6a9fc58
TT
19702 pc = byte_get ((unsigned char *) data, addr_size);
19703 data += addr_size;
3ca60c57 19704
c6a9fc58
TT
19705 base_addr = byte_get ((unsigned char *) data, addr_size);
19706 data += addr_size;
3ca60c57 19707
c6a9fc58
TT
19708 semaphore = byte_get ((unsigned char *) data, addr_size);
19709 data += addr_size;
19710
3ca60c57
NC
19711 if (data >= data_end)
19712 goto stapdt_note_too_small;
19713 maxlen = data_end - data;
19714 len = strnlen (data, maxlen);
19715 if (len < maxlen)
19716 {
19717 provider = data;
19718 data += len + 1;
19719 }
19720 else
19721 goto stapdt_note_too_small;
19722
19723 if (data >= data_end)
19724 goto stapdt_note_too_small;
19725 maxlen = data_end - data;
19726 len = strnlen (data, maxlen);
19727 if (len < maxlen)
19728 {
19729 probe = data;
19730 data += len + 1;
19731 }
19732 else
19733 goto stapdt_note_too_small;
9abca702 19734
3ca60c57
NC
19735 if (data >= data_end)
19736 goto stapdt_note_too_small;
19737 maxlen = data_end - data;
19738 len = strnlen (data, maxlen);
19739 if (len < maxlen)
19740 {
19741 arg_fmt = data;
19742 data += len + 1;
19743 }
19744 else
19745 goto stapdt_note_too_small;
c6a9fc58
TT
19746
19747 printf (_(" Provider: %s\n"), provider);
19748 printf (_(" Name: %s\n"), probe);
19749 printf (_(" Location: "));
19750 print_vma (pc, FULL_HEX);
19751 printf (_(", Base: "));
19752 print_vma (base_addr, FULL_HEX);
19753 printf (_(", Semaphore: "));
19754 print_vma (semaphore, FULL_HEX);
9cf03b7e 19755 printf ("\n");
c6a9fc58
TT
19756 printf (_(" Arguments: %s\n"), arg_fmt);
19757
19758 return data == data_end;
3ca60c57
NC
19759
19760 stapdt_note_too_small:
19761 printf (_(" <corrupt - note is too small>\n"));
19762 error (_("corrupt stapdt note - the data size is too small\n"));
015dc7e1 19763 return false;
c6a9fc58
TT
19764}
19765
00e98fc7
TG
19766static const char *
19767get_ia64_vms_note_type (unsigned e_type)
19768{
19769 static char buff[64];
19770
19771 switch (e_type)
19772 {
19773 case NT_VMS_MHD:
19774 return _("NT_VMS_MHD (module header)");
19775 case NT_VMS_LNM:
19776 return _("NT_VMS_LNM (language name)");
19777 case NT_VMS_SRC:
19778 return _("NT_VMS_SRC (source files)");
19779 case NT_VMS_TITLE:
9cf03b7e 19780 return "NT_VMS_TITLE";
00e98fc7
TG
19781 case NT_VMS_EIDC:
19782 return _("NT_VMS_EIDC (consistency check)");
19783 case NT_VMS_FPMODE:
19784 return _("NT_VMS_FPMODE (FP mode)");
19785 case NT_VMS_LINKTIME:
9cf03b7e 19786 return "NT_VMS_LINKTIME";
00e98fc7
TG
19787 case NT_VMS_IMGNAM:
19788 return _("NT_VMS_IMGNAM (image name)");
19789 case NT_VMS_IMGID:
19790 return _("NT_VMS_IMGID (image id)");
19791 case NT_VMS_LINKID:
19792 return _("NT_VMS_LINKID (link id)");
19793 case NT_VMS_IMGBID:
19794 return _("NT_VMS_IMGBID (build id)");
19795 case NT_VMS_GSTNAM:
19796 return _("NT_VMS_GSTNAM (sym table name)");
19797 case NT_VMS_ORIG_DYN:
9cf03b7e 19798 return "NT_VMS_ORIG_DYN";
00e98fc7 19799 case NT_VMS_PATCHTIME:
9cf03b7e 19800 return "NT_VMS_PATCHTIME";
00e98fc7
TG
19801 default:
19802 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19803 return buff;
19804 }
19805}
19806
015dc7e1 19807static bool
00e98fc7
TG
19808print_ia64_vms_note (Elf_Internal_Note * pnote)
19809{
8d18bf79
NC
19810 int maxlen = pnote->descsz;
19811
19812 if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
19813 goto desc_size_fail;
19814
00e98fc7
TG
19815 switch (pnote->type)
19816 {
19817 case NT_VMS_MHD:
8d18bf79
NC
19818 if (maxlen <= 36)
19819 goto desc_size_fail;
19820
19821 int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
19822
19823 printf (_(" Creation date : %.17s\n"), pnote->descdata);
19824 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
19825 if (l + 34 < maxlen)
19826 {
19827 printf (_(" Module name : %s\n"), pnote->descdata + 34);
19828 if (l + 35 < maxlen)
19829 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
19830 else
19831 printf (_(" Module version : <missing>\n"));
19832 }
00e98fc7 19833 else
8d18bf79
NC
19834 {
19835 printf (_(" Module name : <missing>\n"));
19836 printf (_(" Module version : <missing>\n"));
19837 }
00e98fc7 19838 break;
8d18bf79 19839
00e98fc7 19840 case NT_VMS_LNM:
8d18bf79 19841 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
00e98fc7 19842 break;
8d18bf79 19843
00e98fc7
TG
19844#ifdef BFD64
19845 case NT_VMS_FPMODE:
9cf03b7e 19846 printf (_(" Floating Point mode: "));
8d18bf79
NC
19847 if (maxlen < 8)
19848 goto desc_size_fail;
19849 /* FIXME: Generate an error if descsz > 8 ? */
19850
4a5cb34f 19851 printf ("0x%016" BFD_VMA_FMT "x\n",
8d18bf79 19852 (bfd_vma) byte_get ((unsigned char *)pnote->descdata, 8));
00e98fc7 19853 break;
8d18bf79 19854
00e98fc7
TG
19855 case NT_VMS_LINKTIME:
19856 printf (_(" Link time: "));
8d18bf79
NC
19857 if (maxlen < 8)
19858 goto desc_size_fail;
19859 /* FIXME: Generate an error if descsz > 8 ? */
19860
00e98fc7 19861 print_vms_time
8d18bf79 19862 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
00e98fc7
TG
19863 printf ("\n");
19864 break;
8d18bf79 19865
00e98fc7
TG
19866 case NT_VMS_PATCHTIME:
19867 printf (_(" Patch time: "));
8d18bf79
NC
19868 if (maxlen < 8)
19869 goto desc_size_fail;
19870 /* FIXME: Generate an error if descsz > 8 ? */
19871
00e98fc7 19872 print_vms_time
8d18bf79 19873 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
00e98fc7
TG
19874 printf ("\n");
19875 break;
8d18bf79 19876
00e98fc7 19877 case NT_VMS_ORIG_DYN:
8d18bf79
NC
19878 if (maxlen < 34)
19879 goto desc_size_fail;
19880
00e98fc7
TG
19881 printf (_(" Major id: %u, minor id: %u\n"),
19882 (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
19883 (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
9cf03b7e 19884 printf (_(" Last modified : "));
00e98fc7
TG
19885 print_vms_time
19886 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
9cf03b7e 19887 printf (_("\n Link flags : "));
4a5cb34f 19888 printf ("0x%016" BFD_VMA_FMT "x\n",
948f632f 19889 (bfd_vma) byte_get ((unsigned char *)pnote->descdata + 16, 8));
00e98fc7 19890 printf (_(" Header flags: 0x%08x\n"),
948f632f 19891 (unsigned) byte_get ((unsigned char *)pnote->descdata + 24, 4));
8d18bf79 19892 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
00e98fc7
TG
19893 break;
19894#endif
8d18bf79 19895
00e98fc7 19896 case NT_VMS_IMGNAM:
8d18bf79 19897 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
00e98fc7 19898 break;
8d18bf79 19899
00e98fc7 19900 case NT_VMS_GSTNAM:
8d18bf79 19901 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
00e98fc7 19902 break;
8d18bf79 19903
00e98fc7 19904 case NT_VMS_IMGID:
8d18bf79 19905 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
00e98fc7 19906 break;
8d18bf79 19907
00e98fc7 19908 case NT_VMS_LINKID:
8d18bf79 19909 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
00e98fc7 19910 break;
8d18bf79 19911
00e98fc7 19912 default:
015dc7e1 19913 return false;
00e98fc7 19914 }
8d18bf79 19915
015dc7e1 19916 return true;
8d18bf79
NC
19917
19918 desc_size_fail:
19919 printf (_(" <corrupt - data size is too small>\n"));
19920 error (_("corrupt IA64 note: data size is too small\n"));
015dc7e1 19921 return false;
00e98fc7
TG
19922}
19923
fd486f32
AM
19924struct build_attr_cache {
19925 Filedata *filedata;
19926 char *strtab;
19927 unsigned long strtablen;
19928 Elf_Internal_Sym *symtab;
19929 unsigned long nsyms;
19930} ba_cache;
19931
6f156d7a
NC
19932/* Find the symbol associated with a build attribute that is attached
19933 to address OFFSET. If PNAME is non-NULL then store the name of
19934 the symbol (if found) in the provided pointer, Returns NULL if a
19935 symbol could not be found. */
c799a79d 19936
6f156d7a 19937static Elf_Internal_Sym *
015dc7e1
AM
19938get_symbol_for_build_attribute (Filedata *filedata,
19939 unsigned long offset,
19940 bool is_open_attr,
19941 const char **pname)
9ef920e9 19942{
fd486f32
AM
19943 Elf_Internal_Sym *saved_sym = NULL;
19944 Elf_Internal_Sym *sym;
9ef920e9 19945
dda8d76d 19946 if (filedata->section_headers != NULL
fd486f32 19947 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
9ef920e9 19948 {
c799a79d 19949 Elf_Internal_Shdr * symsec;
9ef920e9 19950
fd486f32
AM
19951 free (ba_cache.strtab);
19952 ba_cache.strtab = NULL;
19953 free (ba_cache.symtab);
19954 ba_cache.symtab = NULL;
19955
c799a79d 19956 /* Load the symbol and string sections. */
dda8d76d
NC
19957 for (symsec = filedata->section_headers;
19958 symsec < filedata->section_headers + filedata->file_header.e_shnum;
c799a79d 19959 symsec ++)
9ef920e9 19960 {
28d13567
AM
19961 if (symsec->sh_type == SHT_SYMTAB
19962 && get_symtab (filedata, symsec,
19963 &ba_cache.symtab, &ba_cache.nsyms,
19964 &ba_cache.strtab, &ba_cache.strtablen))
19965 break;
9ef920e9 19966 }
fd486f32 19967 ba_cache.filedata = filedata;
9ef920e9
NC
19968 }
19969
fd486f32 19970 if (ba_cache.symtab == NULL)
6f156d7a 19971 return NULL;
9ef920e9 19972
c799a79d 19973 /* Find a symbol whose value matches offset. */
fd486f32 19974 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
c799a79d
NC
19975 if (sym->st_value == offset)
19976 {
fd486f32 19977 if (sym->st_name >= ba_cache.strtablen)
c799a79d
NC
19978 /* Huh ? This should not happen. */
19979 continue;
9ef920e9 19980
fd486f32 19981 if (ba_cache.strtab[sym->st_name] == 0)
c799a79d 19982 continue;
9ef920e9 19983
8fd75781
NC
19984 /* The AArch64 and ARM architectures define mapping symbols
19985 (eg $d, $x, $t) which we want to ignore. */
fd486f32
AM
19986 if (ba_cache.strtab[sym->st_name] == '$'
19987 && ba_cache.strtab[sym->st_name + 1] != 0
19988 && ba_cache.strtab[sym->st_name + 2] == 0)
8fd75781
NC
19989 continue;
19990
c799a79d
NC
19991 if (is_open_attr)
19992 {
19993 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
19994 and FILE or OBJECT symbols over NOTYPE symbols. We skip
19995 FUNC symbols entirely. */
19996 switch (ELF_ST_TYPE (sym->st_info))
19997 {
c799a79d 19998 case STT_OBJECT:
6f156d7a 19999 case STT_FILE:
c799a79d 20000 saved_sym = sym;
6f156d7a
NC
20001 if (sym->st_size)
20002 {
20003 /* If the symbol has a size associated
20004 with it then we can stop searching. */
fd486f32 20005 sym = ba_cache.symtab + ba_cache.nsyms;
6f156d7a 20006 }
c799a79d 20007 continue;
9ef920e9 20008
c799a79d
NC
20009 case STT_FUNC:
20010 /* Ignore function symbols. */
20011 continue;
20012
20013 default:
20014 break;
20015 }
20016
20017 switch (ELF_ST_BIND (sym->st_info))
9ef920e9 20018 {
c799a79d
NC
20019 case STB_GLOBAL:
20020 if (saved_sym == NULL
20021 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
20022 saved_sym = sym;
20023 break;
c871dade 20024
c799a79d
NC
20025 case STB_LOCAL:
20026 if (saved_sym == NULL)
20027 saved_sym = sym;
20028 break;
20029
20030 default:
9ef920e9
NC
20031 break;
20032 }
20033 }
c799a79d
NC
20034 else
20035 {
20036 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
20037 continue;
20038
20039 saved_sym = sym;
20040 break;
20041 }
20042 }
20043
6f156d7a 20044 if (saved_sym && pname)
fd486f32 20045 * pname = ba_cache.strtab + saved_sym->st_name;
6f156d7a
NC
20046
20047 return saved_sym;
c799a79d
NC
20048}
20049
d20e98ab
NC
20050/* Returns true iff addr1 and addr2 are in the same section. */
20051
015dc7e1 20052static bool
d20e98ab
NC
20053same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
20054{
20055 Elf_Internal_Shdr * a1;
20056 Elf_Internal_Shdr * a2;
20057
20058 a1 = find_section_by_address (filedata, addr1);
20059 a2 = find_section_by_address (filedata, addr2);
9abca702 20060
d20e98ab
NC
20061 return a1 == a2 && a1 != NULL;
20062}
20063
015dc7e1 20064static bool
dda8d76d
NC
20065print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
20066 Filedata * filedata)
c799a79d 20067{
015dc7e1
AM
20068 static unsigned long global_offset = 0;
20069 static unsigned long global_end = 0;
20070 static unsigned long func_offset = 0;
20071 static unsigned long func_end = 0;
c871dade 20072
015dc7e1
AM
20073 Elf_Internal_Sym *sym;
20074 const char *name;
20075 unsigned long start;
20076 unsigned long end;
20077 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
6f156d7a
NC
20078
20079 switch (pnote->descsz)
c799a79d 20080 {
6f156d7a
NC
20081 case 0:
20082 /* A zero-length description means that the range of
20083 the previous note of the same type should be used. */
c799a79d 20084 if (is_open_attr)
c871dade 20085 {
6f156d7a
NC
20086 if (global_end > global_offset)
20087 printf (_(" Applies to region from %#lx to %#lx\n"),
20088 global_offset, global_end);
20089 else
20090 printf (_(" Applies to region from %#lx\n"), global_offset);
c799a79d
NC
20091 }
20092 else
20093 {
6f156d7a
NC
20094 if (func_end > func_offset)
20095 printf (_(" Applies to region from %#lx to %#lx\n"), func_offset, func_end);
20096 else
20097 printf (_(" Applies to region from %#lx\n"), func_offset);
c871dade 20098 }
015dc7e1 20099 return true;
9ef920e9 20100
6f156d7a
NC
20101 case 4:
20102 start = byte_get ((unsigned char *) pnote->descdata, 4);
20103 end = 0;
20104 break;
20105
20106 case 8:
c74147bb
NC
20107 start = byte_get ((unsigned char *) pnote->descdata, 4);
20108 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
6f156d7a
NC
20109 break;
20110
20111 case 16:
20112 start = byte_get ((unsigned char *) pnote->descdata, 8);
20113 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
20114 break;
9abca702 20115
6f156d7a 20116 default:
c799a79d
NC
20117 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
20118 printf (_(" <invalid descsz>"));
015dc7e1 20119 return false;
c799a79d
NC
20120 }
20121
6f156d7a
NC
20122 name = NULL;
20123 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
8fd75781
NC
20124 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
20125 in order to avoid them being confused with the start address of the
20126 first function in the file... */
20127 if (sym == NULL && is_open_attr)
20128 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
20129 & name);
6f156d7a
NC
20130
20131 if (end == 0 && sym != NULL && sym->st_size > 0)
20132 end = start + sym->st_size;
c799a79d
NC
20133
20134 if (is_open_attr)
20135 {
d20e98ab
NC
20136 /* FIXME: Need to properly allow for section alignment.
20137 16 is just the alignment used on x86_64. */
20138 if (global_end > 0
20139 && start > BFD_ALIGN (global_end, 16)
20140 /* Build notes are not guaranteed to be organised in order of
20141 increasing address, but we should find the all of the notes
20142 for one section in the same place. */
20143 && same_section (filedata, start, global_end))
6f156d7a
NC
20144 warn (_("Gap in build notes detected from %#lx to %#lx\n"),
20145 global_end + 1, start - 1);
20146
20147 printf (_(" Applies to region from %#lx"), start);
20148 global_offset = start;
20149
20150 if (end)
20151 {
20152 printf (_(" to %#lx"), end);
20153 global_end = end;
20154 }
c799a79d
NC
20155 }
20156 else
20157 {
6f156d7a
NC
20158 printf (_(" Applies to region from %#lx"), start);
20159 func_offset = start;
20160
20161 if (end)
20162 {
20163 printf (_(" to %#lx"), end);
20164 func_end = end;
20165 }
c799a79d
NC
20166 }
20167
6f156d7a
NC
20168 if (sym && name)
20169 printf (_(" (%s)"), name);
20170
20171 printf ("\n");
015dc7e1 20172 return true;
9ef920e9
NC
20173}
20174
015dc7e1 20175static bool
9ef920e9
NC
20176print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
20177{
1d15e434
NC
20178 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
20179 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
20180 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
9ef920e9
NC
20181 char name_type;
20182 char name_attribute;
1d15e434 20183 const char * expected_types;
9ef920e9
NC
20184 const char * name = pnote->namedata;
20185 const char * text;
88305e1b 20186 signed int left;
9ef920e9
NC
20187
20188 if (name == NULL || pnote->namesz < 2)
20189 {
20190 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
7296a62a 20191 print_symbol (-20, _(" <corrupt name>"));
015dc7e1 20192 return false;
9ef920e9
NC
20193 }
20194
6f156d7a
NC
20195 if (do_wide)
20196 left = 28;
20197 else
20198 left = 20;
88305e1b
NC
20199
20200 /* Version 2 of the spec adds a "GA" prefix to the name field. */
20201 if (name[0] == 'G' && name[1] == 'A')
20202 {
6f156d7a
NC
20203 if (pnote->namesz < 4)
20204 {
20205 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
20206 print_symbol (-20, _(" <corrupt name>"));
015dc7e1 20207 return false;
6f156d7a
NC
20208 }
20209
88305e1b
NC
20210 printf ("GA");
20211 name += 2;
20212 left -= 2;
20213 }
20214
9ef920e9
NC
20215 switch ((name_type = * name))
20216 {
20217 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
20218 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
20219 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
20220 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
20221 printf ("%c", * name);
88305e1b 20222 left --;
9ef920e9
NC
20223 break;
20224 default:
20225 error (_("unrecognised attribute type in name field: %d\n"), name_type);
20226 print_symbol (-20, _("<unknown name type>"));
015dc7e1 20227 return false;
9ef920e9
NC
20228 }
20229
9ef920e9
NC
20230 ++ name;
20231 text = NULL;
20232
20233 switch ((name_attribute = * name))
20234 {
20235 case GNU_BUILD_ATTRIBUTE_VERSION:
20236 text = _("<version>");
1d15e434 20237 expected_types = string_expected;
9ef920e9
NC
20238 ++ name;
20239 break;
20240 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
20241 text = _("<stack prot>");
75d7d298 20242 expected_types = "!+*";
9ef920e9
NC
20243 ++ name;
20244 break;
20245 case GNU_BUILD_ATTRIBUTE_RELRO:
20246 text = _("<relro>");
1d15e434 20247 expected_types = bool_expected;
9ef920e9
NC
20248 ++ name;
20249 break;
20250 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
20251 text = _("<stack size>");
1d15e434 20252 expected_types = number_expected;
9ef920e9
NC
20253 ++ name;
20254 break;
20255 case GNU_BUILD_ATTRIBUTE_TOOL:
20256 text = _("<tool>");
1d15e434 20257 expected_types = string_expected;
9ef920e9
NC
20258 ++ name;
20259 break;
20260 case GNU_BUILD_ATTRIBUTE_ABI:
20261 text = _("<ABI>");
20262 expected_types = "$*";
20263 ++ name;
20264 break;
20265 case GNU_BUILD_ATTRIBUTE_PIC:
20266 text = _("<PIC>");
1d15e434 20267 expected_types = number_expected;
9ef920e9
NC
20268 ++ name;
20269 break;
a8be5506
NC
20270 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
20271 text = _("<short enum>");
1d15e434 20272 expected_types = bool_expected;
a8be5506
NC
20273 ++ name;
20274 break;
9ef920e9
NC
20275 default:
20276 if (ISPRINT (* name))
20277 {
20278 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
20279
20280 if (len > left && ! do_wide)
20281 len = left;
75d7d298 20282 printf ("%.*s:", len, name);
9ef920e9 20283 left -= len;
0dd6ae21 20284 name += len;
9ef920e9
NC
20285 }
20286 else
20287 {
3e6b6445 20288 static char tmpbuf [128];
88305e1b 20289
3e6b6445
NC
20290 error (_("unrecognised byte in name field: %d\n"), * name);
20291 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
20292 text = tmpbuf;
20293 name ++;
9ef920e9
NC
20294 }
20295 expected_types = "*$!+";
20296 break;
20297 }
20298
20299 if (text)
88305e1b 20300 left -= printf ("%s", text);
9ef920e9
NC
20301
20302 if (strchr (expected_types, name_type) == NULL)
75d7d298 20303 warn (_("attribute does not have an expected type (%c)\n"), name_type);
9ef920e9
NC
20304
20305 if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
20306 {
20307 error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
20308 (unsigned long) pnote->namesz,
20309 (long) (name - pnote->namedata));
015dc7e1 20310 return false;
9ef920e9
NC
20311 }
20312
20313 if (left < 1 && ! do_wide)
015dc7e1 20314 return true;
9ef920e9
NC
20315
20316 switch (name_type)
20317 {
20318 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
20319 {
b06b2c92 20320 unsigned int bytes;
ddef72cd
NC
20321 unsigned long long val = 0;
20322 unsigned int shift = 0;
20323 char * decoded = NULL;
20324
b06b2c92
NC
20325 bytes = pnote->namesz - (name - pnote->namedata);
20326 if (bytes > 0)
20327 /* The -1 is because the name field is always 0 terminated, and we
20328 want to be able to ensure that the shift in the while loop below
20329 will not overflow. */
20330 -- bytes;
20331
ddef72cd
NC
20332 if (bytes > sizeof (val))
20333 {
3e6b6445
NC
20334 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
20335 bytes);
20336 bytes = sizeof (val);
ddef72cd 20337 }
3e6b6445
NC
20338 /* We do not bother to warn if bytes == 0 as this can
20339 happen with some early versions of the gcc plugin. */
9ef920e9
NC
20340
20341 while (bytes --)
20342 {
54b8331d 20343 unsigned long long byte = *name++ & 0xff;
79a964dc
NC
20344
20345 val |= byte << shift;
9ef920e9
NC
20346 shift += 8;
20347 }
20348
75d7d298 20349 switch (name_attribute)
9ef920e9 20350 {
75d7d298 20351 case GNU_BUILD_ATTRIBUTE_PIC:
9ef920e9
NC
20352 switch (val)
20353 {
75d7d298
NC
20354 case 0: decoded = "static"; break;
20355 case 1: decoded = "pic"; break;
20356 case 2: decoded = "PIC"; break;
20357 case 3: decoded = "pie"; break;
20358 case 4: decoded = "PIE"; break;
20359 default: break;
9ef920e9 20360 }
75d7d298
NC
20361 break;
20362 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
20363 switch (val)
9ef920e9 20364 {
75d7d298
NC
20365 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
20366 case 0: decoded = "off"; break;
20367 case 1: decoded = "on"; break;
20368 case 2: decoded = "all"; break;
20369 case 3: decoded = "strong"; break;
20370 case 4: decoded = "explicit"; break;
20371 default: break;
9ef920e9 20372 }
75d7d298
NC
20373 break;
20374 default:
20375 break;
9ef920e9
NC
20376 }
20377
75d7d298 20378 if (decoded != NULL)
3e6b6445
NC
20379 {
20380 print_symbol (-left, decoded);
20381 left = 0;
20382 }
20383 else if (val == 0)
20384 {
20385 printf ("0x0");
20386 left -= 3;
20387 }
9ef920e9 20388 else
75d7d298
NC
20389 {
20390 if (do_wide)
ddef72cd 20391 left -= printf ("0x%llx", val);
75d7d298 20392 else
ddef72cd 20393 left -= printf ("0x%-.*llx", left, val);
75d7d298 20394 }
9ef920e9
NC
20395 }
20396 break;
20397 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
20398 left -= print_symbol (- left, name);
20399 break;
20400 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
20401 left -= print_symbol (- left, "true");
20402 break;
20403 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
20404 left -= print_symbol (- left, "false");
20405 break;
20406 }
20407
20408 if (do_wide && left > 0)
20409 printf ("%-*s", left, " ");
9abca702 20410
015dc7e1 20411 return true;
9ef920e9
NC
20412}
20413
6d118b09
NC
20414/* Note that by the ELF standard, the name field is already null byte
20415 terminated, and namesz includes the terminating null byte.
20416 I.E. the value of namesz for the name "FSF" is 4.
20417
e3c8793a 20418 If the value of namesz is zero, there is no name present. */
9ef920e9 20419
015dc7e1 20420static bool
9ef920e9 20421process_note (Elf_Internal_Note * pnote,
dda8d76d 20422 Filedata * filedata)
779fe533 20423{
2cf0635d
NC
20424 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
20425 const char * nt;
9437c45b
JT
20426
20427 if (pnote->namesz == 0)
1ec5cd37
NC
20428 /* If there is no note name, then use the default set of
20429 note type strings. */
dda8d76d 20430 nt = get_note_type (filedata, pnote->type);
1ec5cd37 20431
24d127aa 20432 else if (startswith (pnote->namedata, "GNU"))
1118d252
RM
20433 /* GNU-specific object file notes. */
20434 nt = get_gnu_elf_note_type (pnote->type);
f4ddf30f 20435
24d127aa 20436 else if (startswith (pnote->namedata, "FreeBSD"))
f4ddf30f 20437 /* FreeBSD-specific core file notes. */
dda8d76d 20438 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
1118d252 20439
24d127aa 20440 else if (startswith (pnote->namedata, "NetBSD-CORE"))
1ec5cd37 20441 /* NetBSD-specific core file notes. */
dda8d76d 20442 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
1ec5cd37 20443
24d127aa 20444 else if (startswith (pnote->namedata, "NetBSD"))
c6056a74
SF
20445 /* NetBSD-specific core file notes. */
20446 return process_netbsd_elf_note (pnote);
20447
24d127aa 20448 else if (startswith (pnote->namedata, "PaX"))
9abca702
CZ
20449 /* NetBSD-specific core file notes. */
20450 return process_netbsd_elf_note (pnote);
20451
e9b095a5 20452 else if (startswith (pnote->namedata, "SPU/"))
b15fa79e
AM
20453 {
20454 /* SPU-specific core file notes. */
20455 nt = pnote->namedata + 4;
20456 name = "SPU";
20457 }
20458
24d127aa 20459 else if (startswith (pnote->namedata, "IPF/VMS"))
00e98fc7
TG
20460 /* VMS/ia64-specific file notes. */
20461 nt = get_ia64_vms_note_type (pnote->type);
20462
24d127aa 20463 else if (startswith (pnote->namedata, "stapsdt"))
70616151
TT
20464 nt = get_stapsdt_note_type (pnote->type);
20465
9437c45b 20466 else
1ec5cd37
NC
20467 /* Don't recognize this note name; just use the default set of
20468 note type strings. */
dda8d76d 20469 nt = get_note_type (filedata, pnote->type);
9437c45b 20470
1449284b 20471 printf (" ");
9ef920e9 20472
24d127aa 20473 if (((startswith (pnote->namedata, "GA")
483767a3
AM
20474 && strchr ("*$!+", pnote->namedata[2]) != NULL)
20475 || strchr ("*$!+", pnote->namedata[0]) != NULL)
20476 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
20477 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
9ef920e9
NC
20478 print_gnu_build_attribute_name (pnote);
20479 else
20480 print_symbol (-20, name);
20481
20482 if (do_wide)
20483 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
20484 else
20485 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
00e98fc7 20486
24d127aa 20487 if (startswith (pnote->namedata, "IPF/VMS"))
00e98fc7 20488 return print_ia64_vms_note (pnote);
24d127aa 20489 else if (startswith (pnote->namedata, "GNU"))
dda8d76d 20490 return print_gnu_note (filedata, pnote);
24d127aa 20491 else if (startswith (pnote->namedata, "stapsdt"))
c6a9fc58 20492 return print_stapsdt_note (pnote);
24d127aa 20493 else if (startswith (pnote->namedata, "CORE"))
9ece1fa9 20494 return print_core_note (pnote);
24d127aa 20495 else if (((startswith (pnote->namedata, "GA")
483767a3
AM
20496 && strchr ("*$!+", pnote->namedata[2]) != NULL)
20497 || strchr ("*$!+", pnote->namedata[0]) != NULL)
20498 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
20499 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
dda8d76d 20500 return print_gnu_build_attribute_description (pnote, filedata);
779fe533 20501
9ef920e9 20502 if (pnote->descsz)
1449284b
NC
20503 {
20504 unsigned long i;
20505
20506 printf (_(" description data: "));
20507 for (i = 0; i < pnote->descsz; i++)
178d8719 20508 printf ("%02x ", pnote->descdata[i] & 0xff);
04ac15ab
AS
20509 if (!do_wide)
20510 printf ("\n");
1449284b
NC
20511 }
20512
9ef920e9
NC
20513 if (do_wide)
20514 printf ("\n");
20515
015dc7e1 20516 return true;
1449284b 20517}
6d118b09 20518
015dc7e1 20519static bool
dda8d76d
NC
20520process_notes_at (Filedata * filedata,
20521 Elf_Internal_Shdr * section,
20522 bfd_vma offset,
82ed9683
L
20523 bfd_vma length,
20524 bfd_vma align)
779fe533 20525{
015dc7e1
AM
20526 Elf_External_Note *pnotes;
20527 Elf_External_Note *external;
20528 char *end;
20529 bool res = true;
103f02d3 20530
779fe533 20531 if (length <= 0)
015dc7e1 20532 return false;
103f02d3 20533
1449284b
NC
20534 if (section)
20535 {
dda8d76d 20536 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
1449284b 20537 if (pnotes)
32ec8896 20538 {
dda8d76d 20539 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
f761cb13
AM
20540 {
20541 free (pnotes);
015dc7e1 20542 return false;
f761cb13 20543 }
32ec8896 20544 }
1449284b
NC
20545 }
20546 else
82ed9683 20547 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
1449284b 20548 _("notes"));
4dff97b2 20549
dd24e3da 20550 if (pnotes == NULL)
015dc7e1 20551 return false;
779fe533 20552
103f02d3 20553 external = pnotes;
103f02d3 20554
ca0e11aa
NC
20555 if (filedata->is_separate)
20556 printf (_("In linked file '%s': "), filedata->file_name);
20557 else
20558 printf ("\n");
1449284b 20559 if (section)
ca0e11aa 20560 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
1449284b 20561 else
ca0e11aa 20562 printf (_("Displaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
1449284b
NC
20563 (unsigned long) offset, (unsigned long) length);
20564
82ed9683
L
20565 /* NB: Some note sections may have alignment value of 0 or 1. gABI
20566 specifies that notes should be aligned to 4 bytes in 32-bit
20567 objects and to 8 bytes in 64-bit objects. As a Linux extension,
20568 we also support 4 byte alignment in 64-bit objects. If section
20569 alignment is less than 4, we treate alignment as 4 bytes. */
20570 if (align < 4)
20571 align = 4;
20572 else if (align != 4 && align != 8)
20573 {
20574 warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
20575 (long) align);
a788aedd 20576 free (pnotes);
015dc7e1 20577 return false;
82ed9683
L
20578 }
20579
dbe15e4e 20580 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
103f02d3 20581
c8071705
NC
20582 end = (char *) pnotes + length;
20583 while ((char *) external < end)
779fe533 20584 {
b34976b6 20585 Elf_Internal_Note inote;
15b42fb0 20586 size_t min_notesz;
4dff97b2 20587 char * next;
2cf0635d 20588 char * temp = NULL;
c8071705 20589 size_t data_remaining = end - (char *) external;
6d118b09 20590
dda8d76d 20591 if (!is_ia64_vms (filedata))
15b42fb0 20592 {
9dd3a467
NC
20593 /* PR binutils/15191
20594 Make sure that there is enough data to read. */
15b42fb0
AM
20595 min_notesz = offsetof (Elf_External_Note, name);
20596 if (data_remaining < min_notesz)
9dd3a467 20597 {
d3a49aa8
AM
20598 warn (ngettext ("Corrupt note: only %ld byte remains, "
20599 "not enough for a full note\n",
20600 "Corrupt note: only %ld bytes remain, "
20601 "not enough for a full note\n",
20602 data_remaining),
20603 (long) data_remaining);
9dd3a467
NC
20604 break;
20605 }
5396a86e
AM
20606 data_remaining -= min_notesz;
20607
15b42fb0
AM
20608 inote.type = BYTE_GET (external->type);
20609 inote.namesz = BYTE_GET (external->namesz);
20610 inote.namedata = external->name;
20611 inote.descsz = BYTE_GET (external->descsz);
276da9b3 20612 inote.descdata = ((char *) external
4dff97b2 20613 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
15b42fb0 20614 inote.descpos = offset + (inote.descdata - (char *) pnotes);
276da9b3 20615 next = ((char *) external
4dff97b2 20616 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
15b42fb0 20617 }
00e98fc7 20618 else
15b42fb0
AM
20619 {
20620 Elf64_External_VMS_Note *vms_external;
00e98fc7 20621
9dd3a467
NC
20622 /* PR binutils/15191
20623 Make sure that there is enough data to read. */
15b42fb0
AM
20624 min_notesz = offsetof (Elf64_External_VMS_Note, name);
20625 if (data_remaining < min_notesz)
9dd3a467 20626 {
d3a49aa8
AM
20627 warn (ngettext ("Corrupt note: only %ld byte remains, "
20628 "not enough for a full note\n",
20629 "Corrupt note: only %ld bytes remain, "
20630 "not enough for a full note\n",
20631 data_remaining),
20632 (long) data_remaining);
9dd3a467
NC
20633 break;
20634 }
5396a86e 20635 data_remaining -= min_notesz;
3e55a963 20636
15b42fb0
AM
20637 vms_external = (Elf64_External_VMS_Note *) external;
20638 inote.type = BYTE_GET (vms_external->type);
20639 inote.namesz = BYTE_GET (vms_external->namesz);
20640 inote.namedata = vms_external->name;
20641 inote.descsz = BYTE_GET (vms_external->descsz);
20642 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
20643 inote.descpos = offset + (inote.descdata - (char *) pnotes);
20644 next = inote.descdata + align_power (inote.descsz, 3);
20645 }
20646
5396a86e
AM
20647 /* PR 17531: file: 3443835e. */
20648 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
20649 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
20650 || (size_t) (inote.descdata - inote.namedata) > data_remaining
20651 || (size_t) (next - inote.descdata) < inote.descsz
20652 || ((size_t) (next - inote.descdata)
20653 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
3e55a963 20654 {
15b42fb0 20655 warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
0af1713e 20656 (unsigned long) ((char *) external - (char *) pnotes));
4dff97b2
NC
20657 warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
20658 inote.type, inote.namesz, inote.descsz, (int) align);
3e55a963
NC
20659 break;
20660 }
20661
15b42fb0 20662 external = (Elf_External_Note *) next;
dd24e3da 20663
6d118b09
NC
20664 /* Verify that name is null terminated. It appears that at least
20665 one version of Linux (RedHat 6.0) generates corefiles that don't
20666 comply with the ELF spec by failing to include the null byte in
20667 namesz. */
18344509 20668 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
6d118b09 20669 {
5396a86e 20670 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
6d118b09 20671 {
5396a86e
AM
20672 temp = (char *) malloc (inote.namesz + 1);
20673 if (temp == NULL)
20674 {
20675 error (_("Out of memory allocating space for inote name\n"));
015dc7e1 20676 res = false;
5396a86e
AM
20677 break;
20678 }
76da6bbe 20679
5396a86e
AM
20680 memcpy (temp, inote.namedata, inote.namesz);
20681 inote.namedata = temp;
20682 }
20683 inote.namedata[inote.namesz] = 0;
6d118b09
NC
20684 }
20685
dda8d76d 20686 if (! process_note (& inote, filedata))
015dc7e1 20687 res = false;
103f02d3 20688
9db70fc3
AM
20689 free (temp);
20690 temp = NULL;
779fe533
NC
20691 }
20692
20693 free (pnotes);
103f02d3 20694
779fe533
NC
20695 return res;
20696}
20697
015dc7e1 20698static bool
dda8d76d 20699process_corefile_note_segments (Filedata * filedata)
779fe533 20700{
015dc7e1 20701 Elf_Internal_Phdr *segment;
b34976b6 20702 unsigned int i;
015dc7e1 20703 bool res = true;
103f02d3 20704
dda8d76d 20705 if (! get_program_headers (filedata))
015dc7e1 20706 return true;
103f02d3 20707
dda8d76d
NC
20708 for (i = 0, segment = filedata->program_headers;
20709 i < filedata->file_header.e_phnum;
b34976b6 20710 i++, segment++)
779fe533
NC
20711 {
20712 if (segment->p_type == PT_NOTE)
dda8d76d 20713 if (! process_notes_at (filedata, NULL,
32ec8896 20714 (bfd_vma) segment->p_offset,
82ed9683
L
20715 (bfd_vma) segment->p_filesz,
20716 (bfd_vma) segment->p_align))
015dc7e1 20717 res = false;
779fe533 20718 }
103f02d3 20719
779fe533
NC
20720 return res;
20721}
20722
015dc7e1 20723static bool
dda8d76d 20724process_v850_notes (Filedata * filedata, bfd_vma offset, bfd_vma length)
685080f2
NC
20725{
20726 Elf_External_Note * pnotes;
20727 Elf_External_Note * external;
c8071705 20728 char * end;
015dc7e1 20729 bool res = true;
685080f2
NC
20730
20731 if (length <= 0)
015dc7e1 20732 return false;
685080f2 20733
dda8d76d 20734 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
685080f2
NC
20735 _("v850 notes"));
20736 if (pnotes == NULL)
015dc7e1 20737 return false;
685080f2
NC
20738
20739 external = pnotes;
c8071705 20740 end = (char*) pnotes + length;
685080f2
NC
20741
20742 printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
20743 (unsigned long) offset, (unsigned long) length);
20744
c8071705 20745 while ((char *) external + sizeof (Elf_External_Note) < end)
685080f2
NC
20746 {
20747 Elf_External_Note * next;
20748 Elf_Internal_Note inote;
20749
20750 inote.type = BYTE_GET (external->type);
20751 inote.namesz = BYTE_GET (external->namesz);
20752 inote.namedata = external->name;
20753 inote.descsz = BYTE_GET (external->descsz);
20754 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
20755 inote.descpos = offset + (inote.descdata - (char *) pnotes);
20756
c8071705
NC
20757 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
20758 {
20759 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
20760 inote.descdata = inote.namedata;
20761 inote.namesz = 0;
20762 }
20763
685080f2
NC
20764 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
20765
c8071705 20766 if ( ((char *) next > end)
685080f2
NC
20767 || ((char *) next < (char *) pnotes))
20768 {
20769 warn (_("corrupt descsz found in note at offset 0x%lx\n"),
20770 (unsigned long) ((char *) external - (char *) pnotes));
20771 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
20772 inote.type, inote.namesz, inote.descsz);
20773 break;
20774 }
20775
20776 external = next;
20777
20778 /* Prevent out-of-bounds indexing. */
c8071705 20779 if ( inote.namedata + inote.namesz > end
685080f2
NC
20780 || inote.namedata + inote.namesz < inote.namedata)
20781 {
20782 warn (_("corrupt namesz found in note at offset 0x%lx\n"),
20783 (unsigned long) ((char *) external - (char *) pnotes));
20784 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
20785 inote.type, inote.namesz, inote.descsz);
20786 break;
20787 }
20788
20789 printf (" %s: ", get_v850_elf_note_type (inote.type));
20790
20791 if (! print_v850_note (& inote))
20792 {
015dc7e1 20793 res = false;
685080f2
NC
20794 printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
20795 inote.namesz, inote.descsz);
20796 }
20797 }
20798
20799 free (pnotes);
20800
20801 return res;
20802}
20803
015dc7e1 20804static bool
dda8d76d 20805process_note_sections (Filedata * filedata)
1ec5cd37 20806{
015dc7e1 20807 Elf_Internal_Shdr *section;
1ec5cd37 20808 unsigned long i;
32ec8896 20809 unsigned int n = 0;
015dc7e1 20810 bool res = true;
1ec5cd37 20811
dda8d76d
NC
20812 for (i = 0, section = filedata->section_headers;
20813 i < filedata->file_header.e_shnum && section != NULL;
1ec5cd37 20814 i++, section++)
685080f2
NC
20815 {
20816 if (section->sh_type == SHT_NOTE)
20817 {
dda8d76d 20818 if (! process_notes_at (filedata, section,
32ec8896 20819 (bfd_vma) section->sh_offset,
82ed9683
L
20820 (bfd_vma) section->sh_size,
20821 (bfd_vma) section->sh_addralign))
015dc7e1 20822 res = false;
685080f2
NC
20823 n++;
20824 }
20825
dda8d76d
NC
20826 if (( filedata->file_header.e_machine == EM_V800
20827 || filedata->file_header.e_machine == EM_V850
20828 || filedata->file_header.e_machine == EM_CYGNUS_V850)
685080f2
NC
20829 && section->sh_type == SHT_RENESAS_INFO)
20830 {
dda8d76d 20831 if (! process_v850_notes (filedata,
32ec8896
NC
20832 (bfd_vma) section->sh_offset,
20833 (bfd_vma) section->sh_size))
015dc7e1 20834 res = false;
685080f2
NC
20835 n++;
20836 }
20837 }
df565f32
NC
20838
20839 if (n == 0)
20840 /* Try processing NOTE segments instead. */
dda8d76d 20841 return process_corefile_note_segments (filedata);
1ec5cd37
NC
20842
20843 return res;
20844}
20845
015dc7e1 20846static bool
dda8d76d 20847process_notes (Filedata * filedata)
779fe533
NC
20848{
20849 /* If we have not been asked to display the notes then do nothing. */
20850 if (! do_notes)
015dc7e1 20851 return true;
103f02d3 20852
dda8d76d
NC
20853 if (filedata->file_header.e_type != ET_CORE)
20854 return process_note_sections (filedata);
103f02d3 20855
779fe533 20856 /* No program headers means no NOTE segment. */
dda8d76d
NC
20857 if (filedata->file_header.e_phnum > 0)
20858 return process_corefile_note_segments (filedata);
779fe533 20859
ca0e11aa
NC
20860 if (filedata->is_separate)
20861 printf (_("No notes found in linked file '%s'.\n"),
20862 filedata->file_name);
20863 else
20864 printf (_("No notes found file.\n"));
20865
015dc7e1 20866 return true;
779fe533
NC
20867}
20868
60abdbed
NC
20869static unsigned char *
20870display_public_gnu_attributes (unsigned char * start,
20871 const unsigned char * const end)
20872{
20873 printf (_(" Unknown GNU attribute: %s\n"), start);
20874
20875 start += strnlen ((char *) start, end - start);
20876 display_raw_attribute (start, end);
20877
20878 return (unsigned char *) end;
20879}
20880
20881static unsigned char *
20882display_generic_attribute (unsigned char * start,
20883 unsigned int tag,
20884 const unsigned char * const end)
20885{
20886 if (tag == 0)
20887 return (unsigned char *) end;
20888
20889 return display_tag_value (tag, start, end);
20890}
20891
015dc7e1 20892static bool
dda8d76d 20893process_arch_specific (Filedata * filedata)
252b5132 20894{
a952a375 20895 if (! do_arch)
015dc7e1 20896 return true;
a952a375 20897
dda8d76d 20898 switch (filedata->file_header.e_machine)
252b5132 20899 {
53a346d8
CZ
20900 case EM_ARC:
20901 case EM_ARC_COMPACT:
20902 case EM_ARC_COMPACT2:
dda8d76d 20903 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
53a346d8
CZ
20904 display_arc_attribute,
20905 display_generic_attribute);
11c1ff18 20906 case EM_ARM:
dda8d76d 20907 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
60abdbed
NC
20908 display_arm_attribute,
20909 display_generic_attribute);
20910
252b5132 20911 case EM_MIPS:
4fe85591 20912 case EM_MIPS_RS3_LE:
dda8d76d 20913 return process_mips_specific (filedata);
60abdbed
NC
20914
20915 case EM_MSP430:
dda8d76d 20916 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
b0191216 20917 display_msp430_attribute,
c0ea7c52 20918 display_msp430_gnu_attribute);
60abdbed 20919
2dc8dd17
JW
20920 case EM_RISCV:
20921 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
20922 display_riscv_attribute,
20923 display_generic_attribute);
20924
35c08157 20925 case EM_NDS32:
dda8d76d 20926 return process_nds32_specific (filedata);
60abdbed 20927
85f7484a
PB
20928 case EM_68K:
20929 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
20930 display_m68k_gnu_attribute);
20931
34c8bcba 20932 case EM_PPC:
b82317dd 20933 case EM_PPC64:
dda8d76d 20934 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
60abdbed
NC
20935 display_power_gnu_attribute);
20936
643f7afb
AK
20937 case EM_S390:
20938 case EM_S390_OLD:
dda8d76d 20939 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
60abdbed
NC
20940 display_s390_gnu_attribute);
20941
9e8c70f9
DM
20942 case EM_SPARC:
20943 case EM_SPARC32PLUS:
20944 case EM_SPARCV9:
dda8d76d 20945 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
60abdbed
NC
20946 display_sparc_gnu_attribute);
20947
59e6276b 20948 case EM_TI_C6000:
dda8d76d 20949 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
60abdbed
NC
20950 display_tic6x_attribute,
20951 display_generic_attribute);
20952
0861f561
CQ
20953 case EM_CSKY:
20954 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
20955 display_csky_attribute, NULL);
20956
252b5132 20957 default:
dda8d76d 20958 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
60abdbed
NC
20959 display_public_gnu_attributes,
20960 display_generic_attribute);
252b5132 20961 }
252b5132
RH
20962}
20963
015dc7e1 20964static bool
dda8d76d 20965get_file_header (Filedata * filedata)
252b5132 20966{
9ea033b2 20967 /* Read in the identity array. */
dda8d76d 20968 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
015dc7e1 20969 return false;
252b5132 20970
9ea033b2 20971 /* Determine how to read the rest of the header. */
dda8d76d 20972 switch (filedata->file_header.e_ident[EI_DATA])
9ea033b2 20973 {
1a0670f3
AM
20974 default:
20975 case ELFDATANONE:
adab8cdc
AO
20976 case ELFDATA2LSB:
20977 byte_get = byte_get_little_endian;
20978 byte_put = byte_put_little_endian;
20979 break;
20980 case ELFDATA2MSB:
20981 byte_get = byte_get_big_endian;
20982 byte_put = byte_put_big_endian;
20983 break;
9ea033b2
NC
20984 }
20985
20986 /* For now we only support 32 bit and 64 bit ELF files. */
dda8d76d 20987 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
9ea033b2
NC
20988
20989 /* Read in the rest of the header. */
20990 if (is_32bit_elf)
20991 {
20992 Elf32_External_Ehdr ehdr32;
252b5132 20993
dda8d76d 20994 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
015dc7e1 20995 return false;
103f02d3 20996
dda8d76d
NC
20997 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
20998 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
20999 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
21000 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
21001 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
21002 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
21003 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
21004 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
21005 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
21006 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
21007 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
21008 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
21009 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
9ea033b2 21010 }
252b5132 21011 else
9ea033b2
NC
21012 {
21013 Elf64_External_Ehdr ehdr64;
a952a375
NC
21014
21015 /* If we have been compiled with sizeof (bfd_vma) == 4, then
21016 we will not be able to cope with the 64bit data found in
21017 64 ELF files. Detect this now and abort before we start
50c2245b 21018 overwriting things. */
a952a375
NC
21019 if (sizeof (bfd_vma) < 8)
21020 {
e3c8793a
NC
21021 error (_("This instance of readelf has been built without support for a\n\
2102264 bit data type and so it cannot read 64 bit ELF files.\n"));
015dc7e1 21023 return false;
a952a375 21024 }
103f02d3 21025
dda8d76d 21026 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
015dc7e1 21027 return false;
103f02d3 21028
dda8d76d
NC
21029 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
21030 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
21031 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
21032 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
21033 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
21034 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
21035 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
21036 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
21037 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
21038 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
21039 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
21040 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
21041 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
9ea033b2 21042 }
252b5132 21043
dda8d76d 21044 if (filedata->file_header.e_shoff)
7ece0d85
JJ
21045 {
21046 /* There may be some extensions in the first section header. Don't
21047 bomb if we can't read it. */
21048 if (is_32bit_elf)
015dc7e1 21049 get_32bit_section_headers (filedata, true);
7ece0d85 21050 else
015dc7e1 21051 get_64bit_section_headers (filedata, true);
7ece0d85 21052 }
560f3c1c 21053
015dc7e1 21054 return true;
252b5132
RH
21055}
21056
dda8d76d
NC
21057static void
21058close_file (Filedata * filedata)
21059{
21060 if (filedata)
21061 {
21062 if (filedata->handle)
21063 fclose (filedata->handle);
21064 free (filedata);
21065 }
21066}
21067
21068void
21069close_debug_file (void * data)
21070{
21071 close_file ((Filedata *) data);
21072}
21073
21074static Filedata *
015dc7e1 21075open_file (const char * pathname, bool is_separate)
dda8d76d
NC
21076{
21077 struct stat statbuf;
21078 Filedata * filedata = NULL;
21079
21080 if (stat (pathname, & statbuf) < 0
21081 || ! S_ISREG (statbuf.st_mode))
21082 goto fail;
21083
21084 filedata = calloc (1, sizeof * filedata);
21085 if (filedata == NULL)
21086 goto fail;
21087
21088 filedata->handle = fopen (pathname, "rb");
21089 if (filedata->handle == NULL)
21090 goto fail;
21091
21092 filedata->file_size = (bfd_size_type) statbuf.st_size;
21093 filedata->file_name = pathname;
ca0e11aa 21094 filedata->is_separate = is_separate;
dda8d76d
NC
21095
21096 if (! get_file_header (filedata))
21097 goto fail;
21098
21099 if (filedata->file_header.e_shoff)
21100 {
015dc7e1 21101 bool res;
dda8d76d
NC
21102
21103 /* Read the section headers again, this time for real. */
21104 if (is_32bit_elf)
015dc7e1 21105 res = get_32bit_section_headers (filedata, false);
dda8d76d 21106 else
015dc7e1 21107 res = get_64bit_section_headers (filedata, false);
dda8d76d
NC
21108
21109 if (!res)
21110 goto fail;
21111 }
21112
21113 return filedata;
21114
21115 fail:
21116 if (filedata)
21117 {
21118 if (filedata->handle)
21119 fclose (filedata->handle);
21120 free (filedata);
21121 }
21122 return NULL;
21123}
21124
21125void *
21126open_debug_file (const char * pathname)
21127{
015dc7e1 21128 return open_file (pathname, true);
dda8d76d
NC
21129}
21130
835f2fae
NC
21131static void
21132initialise_dump_sects (Filedata * filedata)
21133{
21134 /* Initialise the dump_sects array from the cmdline_dump_sects array.
21135 Note we do this even if cmdline_dump_sects is empty because we
21136 must make sure that the dump_sets array is zeroed out before each
21137 object file is processed. */
21138 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
21139 memset (filedata->dump.dump_sects, 0,
21140 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21141
21142 if (cmdline.num_dump_sects > 0)
21143 {
21144 if (filedata->dump.num_dump_sects == 0)
21145 /* A sneaky way of allocating the dump_sects array. */
21146 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
21147
21148 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
21149 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
21150 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21151 }
21152}
21153
fb52b2f4
NC
21154/* Process one ELF object file according to the command line options.
21155 This file may actually be stored in an archive. The file is
32ec8896
NC
21156 positioned at the start of the ELF object. Returns TRUE if no
21157 problems were encountered, FALSE otherwise. */
fb52b2f4 21158
015dc7e1 21159static bool
dda8d76d 21160process_object (Filedata * filedata)
252b5132 21161{
015dc7e1 21162 bool have_separate_files;
252b5132 21163 unsigned int i;
015dc7e1 21164 bool res;
252b5132 21165
dda8d76d 21166 if (! get_file_header (filedata))
252b5132 21167 {
dda8d76d 21168 error (_("%s: Failed to read file header\n"), filedata->file_name);
015dc7e1 21169 return false;
252b5132
RH
21170 }
21171
21172 /* Initialise per file variables. */
978c4450
AM
21173 for (i = ARRAY_SIZE (filedata->version_info); i--;)
21174 filedata->version_info[i] = 0;
252b5132 21175
978c4450
AM
21176 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
21177 filedata->dynamic_info[i] = 0;
21178 filedata->dynamic_info_DT_GNU_HASH = 0;
21179 filedata->dynamic_info_DT_MIPS_XHASH = 0;
252b5132
RH
21180
21181 /* Process the file. */
21182 if (show_name)
dda8d76d 21183 printf (_("\nFile: %s\n"), filedata->file_name);
252b5132 21184
835f2fae 21185 initialise_dump_sects (filedata);
d70c5fc7 21186
dda8d76d 21187 if (! process_file_header (filedata))
015dc7e1 21188 return false;
252b5132 21189
dda8d76d 21190 if (! process_section_headers (filedata))
2f62977e 21191 {
32ec8896 21192 /* Without loaded section headers we cannot process lots of things. */
015dc7e1 21193 do_unwind = do_version = do_dump = do_arch = false;
252b5132 21194
2f62977e 21195 if (! do_using_dynamic)
015dc7e1 21196 do_syms = do_dyn_syms = do_reloc = false;
2f62977e 21197 }
252b5132 21198
dda8d76d 21199 if (! process_section_groups (filedata))
32ec8896 21200 /* Without loaded section groups we cannot process unwind. */
015dc7e1 21201 do_unwind = false;
d1f5c6e3 21202
2482f306
AM
21203 res = process_program_headers (filedata);
21204 if (res)
21205 res = process_dynamic_section (filedata);
252b5132 21206
dda8d76d 21207 if (! process_relocs (filedata))
015dc7e1 21208 res = false;
252b5132 21209
dda8d76d 21210 if (! process_unwind (filedata))
015dc7e1 21211 res = false;
4d6ed7c8 21212
dda8d76d 21213 if (! process_symbol_table (filedata))
015dc7e1 21214 res = false;
252b5132 21215
0f03783c 21216 if (! process_lto_symbol_tables (filedata))
015dc7e1 21217 res = false;
b9e920ec 21218
dda8d76d 21219 if (! process_syminfo (filedata))
015dc7e1 21220 res = false;
252b5132 21221
dda8d76d 21222 if (! process_version_sections (filedata))
015dc7e1 21223 res = false;
252b5132 21224
82ed9683 21225 if (filedata->file_header.e_shstrndx != SHN_UNDEF)
24841daa 21226 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
82ed9683 21227 else
015dc7e1 21228 have_separate_files = false;
dda8d76d
NC
21229
21230 if (! process_section_contents (filedata))
015dc7e1 21231 res = false;
f5842774 21232
24841daa 21233 if (have_separate_files)
dda8d76d 21234 {
24841daa
NC
21235 separate_info * d;
21236
21237 for (d = first_separate_info; d != NULL; d = d->next)
21238 {
835f2fae
NC
21239 initialise_dump_sects (d->handle);
21240
ca0e11aa 21241 if (process_links && ! process_file_header (d->handle))
015dc7e1 21242 res = false;
ca0e11aa 21243 else if (! process_section_headers (d->handle))
015dc7e1 21244 res = false;
d6bfbc39 21245 else if (! process_section_contents (d->handle))
015dc7e1 21246 res = false;
ca0e11aa
NC
21247 else if (process_links)
21248 {
ca0e11aa 21249 if (! process_section_groups (d->handle))
015dc7e1 21250 res = false;
ca0e11aa 21251 if (! process_program_headers (d->handle))
015dc7e1 21252 res = false;
ca0e11aa 21253 if (! process_dynamic_section (d->handle))
015dc7e1 21254 res = false;
ca0e11aa 21255 if (! process_relocs (d->handle))
015dc7e1 21256 res = false;
ca0e11aa 21257 if (! process_unwind (d->handle))
015dc7e1 21258 res = false;
ca0e11aa 21259 if (! process_symbol_table (d->handle))
015dc7e1 21260 res = false;
ca0e11aa 21261 if (! process_lto_symbol_tables (d->handle))
015dc7e1 21262 res = false;
ca0e11aa 21263 if (! process_syminfo (d->handle))
015dc7e1 21264 res = false;
ca0e11aa 21265 if (! process_version_sections (d->handle))
015dc7e1 21266 res = false;
ca0e11aa 21267 if (! process_notes (d->handle))
015dc7e1 21268 res = false;
ca0e11aa 21269 }
24841daa
NC
21270 }
21271
21272 /* The file handles are closed by the call to free_debug_memory() below. */
dda8d76d
NC
21273 }
21274
21275 if (! process_notes (filedata))
015dc7e1 21276 res = false;
103f02d3 21277
dda8d76d 21278 if (! process_gnu_liblist (filedata))
015dc7e1 21279 res = false;
047b2264 21280
dda8d76d 21281 if (! process_arch_specific (filedata))
015dc7e1 21282 res = false;
252b5132 21283
dda8d76d
NC
21284 free (filedata->program_headers);
21285 filedata->program_headers = NULL;
d93f0186 21286
dda8d76d
NC
21287 free (filedata->section_headers);
21288 filedata->section_headers = NULL;
252b5132 21289
dda8d76d
NC
21290 free (filedata->string_table);
21291 filedata->string_table = NULL;
21292 filedata->string_table_length = 0;
252b5132 21293
9db70fc3
AM
21294 free (filedata->dump.dump_sects);
21295 filedata->dump.dump_sects = NULL;
21296 filedata->dump.num_dump_sects = 0;
a788aedd 21297
9db70fc3
AM
21298 free (filedata->dynamic_strings);
21299 filedata->dynamic_strings = NULL;
21300 filedata->dynamic_strings_length = 0;
252b5132 21301
9db70fc3
AM
21302 free (filedata->dynamic_symbols);
21303 filedata->dynamic_symbols = NULL;
21304 filedata->num_dynamic_syms = 0;
252b5132 21305
9db70fc3
AM
21306 free (filedata->dynamic_syminfo);
21307 filedata->dynamic_syminfo = NULL;
ff78d6d6 21308
9db70fc3
AM
21309 free (filedata->dynamic_section);
21310 filedata->dynamic_section = NULL;
293c573e 21311
978c4450 21312 while (filedata->symtab_shndx_list != NULL)
8fb879cd 21313 {
978c4450
AM
21314 elf_section_list *next = filedata->symtab_shndx_list->next;
21315 free (filedata->symtab_shndx_list);
21316 filedata->symtab_shndx_list = next;
8fb879cd
AM
21317 }
21318
9db70fc3
AM
21319 free (filedata->section_headers_groups);
21320 filedata->section_headers_groups = NULL;
e4b17d5c 21321
978c4450 21322 if (filedata->section_groups)
e4b17d5c 21323 {
2cf0635d
NC
21324 struct group_list * g;
21325 struct group_list * next;
e4b17d5c 21326
978c4450 21327 for (i = 0; i < filedata->group_count; i++)
e4b17d5c 21328 {
978c4450 21329 for (g = filedata->section_groups [i].root; g != NULL; g = next)
e4b17d5c
L
21330 {
21331 next = g->next;
21332 free (g);
21333 }
21334 }
21335
978c4450
AM
21336 free (filedata->section_groups);
21337 filedata->section_groups = NULL;
e4b17d5c
L
21338 }
21339
19e6b90e 21340 free_debug_memory ();
18bd398b 21341
32ec8896 21342 return res;
252b5132
RH
21343}
21344
2cf0635d 21345/* Process an ELF archive.
32ec8896
NC
21346 On entry the file is positioned just after the ARMAG string.
21347 Returns TRUE upon success, FALSE otherwise. */
2cf0635d 21348
015dc7e1
AM
21349static bool
21350process_archive (Filedata * filedata, bool is_thin_archive)
2cf0635d
NC
21351{
21352 struct archive_info arch;
21353 struct archive_info nested_arch;
21354 size_t got;
015dc7e1 21355 bool ret = true;
2cf0635d 21356
015dc7e1 21357 show_name = true;
2cf0635d
NC
21358
21359 /* The ARCH structure is used to hold information about this archive. */
21360 arch.file_name = NULL;
21361 arch.file = NULL;
21362 arch.index_array = NULL;
21363 arch.sym_table = NULL;
21364 arch.longnames = NULL;
21365
21366 /* The NESTED_ARCH structure is used as a single-item cache of information
21367 about a nested archive (when members of a thin archive reside within
21368 another regular archive file). */
21369 nested_arch.file_name = NULL;
21370 nested_arch.file = NULL;
21371 nested_arch.index_array = NULL;
21372 nested_arch.sym_table = NULL;
21373 nested_arch.longnames = NULL;
21374
dda8d76d 21375 if (setup_archive (&arch, filedata->file_name, filedata->handle,
780f96ae
AM
21376 filedata->file_size, is_thin_archive,
21377 do_archive_index) != 0)
2cf0635d 21378 {
015dc7e1 21379 ret = false;
2cf0635d 21380 goto out;
4145f1d5 21381 }
fb52b2f4 21382
4145f1d5
NC
21383 if (do_archive_index)
21384 {
2cf0635d 21385 if (arch.sym_table == NULL)
1cb7d8b1
AM
21386 error (_("%s: unable to dump the index as none was found\n"),
21387 filedata->file_name);
4145f1d5
NC
21388 else
21389 {
591f7597 21390 unsigned long i, l;
4145f1d5
NC
21391 unsigned long current_pos;
21392
1cb7d8b1
AM
21393 printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
21394 "in the symbol table)\n"),
21395 filedata->file_name, (unsigned long) arch.index_num,
21396 arch.sym_size);
dda8d76d
NC
21397
21398 current_pos = ftell (filedata->handle);
4145f1d5 21399
2cf0635d 21400 for (i = l = 0; i < arch.index_num; i++)
4145f1d5 21401 {
1cb7d8b1
AM
21402 if (i == 0
21403 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
21404 {
21405 char * member_name
21406 = get_archive_member_name_at (&arch, arch.index_array[i],
21407 &nested_arch);
2cf0635d 21408
1cb7d8b1
AM
21409 if (member_name != NULL)
21410 {
21411 char * qualified_name
21412 = make_qualified_name (&arch, &nested_arch,
21413 member_name);
2cf0635d 21414
1cb7d8b1
AM
21415 if (qualified_name != NULL)
21416 {
21417 printf (_("Contents of binary %s at offset "),
21418 qualified_name);
c2a7d3f5
NC
21419 (void) print_vma (arch.index_array[i], PREFIX_HEX);
21420 putchar ('\n');
1cb7d8b1
AM
21421 free (qualified_name);
21422 }
fd486f32 21423 free (member_name);
4145f1d5
NC
21424 }
21425 }
2cf0635d
NC
21426
21427 if (l >= arch.sym_size)
4145f1d5 21428 {
1cb7d8b1
AM
21429 error (_("%s: end of the symbol table reached "
21430 "before the end of the index\n"),
dda8d76d 21431 filedata->file_name);
015dc7e1 21432 ret = false;
cb8f3167 21433 break;
4145f1d5 21434 }
591f7597 21435 /* PR 17531: file: 0b6630b2. */
1cb7d8b1
AM
21436 printf ("\t%.*s\n",
21437 (int) (arch.sym_size - l), arch.sym_table + l);
591f7597 21438 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
4145f1d5
NC
21439 }
21440
67ce483b 21441 if (arch.uses_64bit_indices)
c2a7d3f5
NC
21442 l = (l + 7) & ~ 7;
21443 else
21444 l += l & 1;
21445
2cf0635d 21446 if (l < arch.sym_size)
32ec8896 21447 {
d3a49aa8
AM
21448 error (ngettext ("%s: %ld byte remains in the symbol table, "
21449 "but without corresponding entries in "
21450 "the index table\n",
21451 "%s: %ld bytes remain in the symbol table, "
21452 "but without corresponding entries in "
21453 "the index table\n",
21454 arch.sym_size - l),
dda8d76d 21455 filedata->file_name, arch.sym_size - l);
015dc7e1 21456 ret = false;
32ec8896 21457 }
4145f1d5 21458
dda8d76d 21459 if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
4145f1d5 21460 {
1cb7d8b1
AM
21461 error (_("%s: failed to seek back to start of object files "
21462 "in the archive\n"),
dda8d76d 21463 filedata->file_name);
015dc7e1 21464 ret = false;
2cf0635d 21465 goto out;
4145f1d5 21466 }
fb52b2f4 21467 }
4145f1d5
NC
21468
21469 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
21470 && !do_segments && !do_header && !do_dump && !do_version
21471 && !do_histogram && !do_debugging && !do_arch && !do_notes
2c610e4b 21472 && !do_section_groups && !do_dyn_syms)
2cf0635d 21473 {
015dc7e1 21474 ret = true; /* Archive index only. */
2cf0635d
NC
21475 goto out;
21476 }
fb52b2f4
NC
21477 }
21478
fb52b2f4
NC
21479 while (1)
21480 {
2cf0635d
NC
21481 char * name;
21482 size_t namelen;
21483 char * qualified_name;
21484
21485 /* Read the next archive header. */
dda8d76d 21486 if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
1cb7d8b1
AM
21487 {
21488 error (_("%s: failed to seek to next archive header\n"),
21489 arch.file_name);
015dc7e1 21490 ret = false;
1cb7d8b1
AM
21491 break;
21492 }
dda8d76d 21493 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
2cf0635d 21494 if (got != sizeof arch.arhdr)
1cb7d8b1
AM
21495 {
21496 if (got == 0)
2cf0635d 21497 break;
28e817cc
NC
21498 /* PR 24049 - we cannot use filedata->file_name as this will
21499 have already been freed. */
21500 error (_("%s: failed to read archive header\n"), arch.file_name);
9abca702 21501
015dc7e1 21502 ret = false;
1cb7d8b1
AM
21503 break;
21504 }
2cf0635d 21505 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
1cb7d8b1
AM
21506 {
21507 error (_("%s: did not find a valid archive header\n"),
21508 arch.file_name);
015dc7e1 21509 ret = false;
1cb7d8b1
AM
21510 break;
21511 }
2cf0635d
NC
21512
21513 arch.next_arhdr_offset += sizeof arch.arhdr;
21514
978c4450
AM
21515 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
21516 if (filedata->archive_file_size & 01)
21517 ++filedata->archive_file_size;
2cf0635d
NC
21518
21519 name = get_archive_member_name (&arch, &nested_arch);
21520 if (name == NULL)
fb52b2f4 21521 {
28e817cc 21522 error (_("%s: bad archive file name\n"), arch.file_name);
015dc7e1 21523 ret = false;
d989285c 21524 break;
fb52b2f4 21525 }
2cf0635d 21526 namelen = strlen (name);
fb52b2f4 21527
2cf0635d
NC
21528 qualified_name = make_qualified_name (&arch, &nested_arch, name);
21529 if (qualified_name == NULL)
fb52b2f4 21530 {
28e817cc 21531 error (_("%s: bad archive file name\n"), arch.file_name);
fd486f32 21532 free (name);
015dc7e1 21533 ret = false;
d989285c 21534 break;
fb52b2f4
NC
21535 }
21536
2cf0635d 21537 if (is_thin_archive && arch.nested_member_origin == 0)
1cb7d8b1
AM
21538 {
21539 /* This is a proxy for an external member of a thin archive. */
21540 Filedata * member_filedata;
21541 char * member_file_name = adjust_relative_path
dda8d76d 21542 (filedata->file_name, name, namelen);
32ec8896 21543
fd486f32 21544 free (name);
1cb7d8b1
AM
21545 if (member_file_name == NULL)
21546 {
fd486f32 21547 free (qualified_name);
015dc7e1 21548 ret = false;
1cb7d8b1
AM
21549 break;
21550 }
2cf0635d 21551
015dc7e1 21552 member_filedata = open_file (member_file_name, false);
1cb7d8b1
AM
21553 if (member_filedata == NULL)
21554 {
21555 error (_("Input file '%s' is not readable.\n"), member_file_name);
21556 free (member_file_name);
fd486f32 21557 free (qualified_name);
015dc7e1 21558 ret = false;
1cb7d8b1
AM
21559 break;
21560 }
2cf0635d 21561
978c4450 21562 filedata->archive_file_offset = arch.nested_member_origin;
dda8d76d 21563 member_filedata->file_name = qualified_name;
2cf0635d 21564
1cb7d8b1 21565 if (! process_object (member_filedata))
015dc7e1 21566 ret = false;
2cf0635d 21567
1cb7d8b1
AM
21568 close_file (member_filedata);
21569 free (member_file_name);
1cb7d8b1 21570 }
2cf0635d 21571 else if (is_thin_archive)
1cb7d8b1
AM
21572 {
21573 Filedata thin_filedata;
eb02c04d 21574
1cb7d8b1 21575 memset (&thin_filedata, 0, sizeof (thin_filedata));
dda8d76d 21576
a043396b
NC
21577 /* PR 15140: Allow for corrupt thin archives. */
21578 if (nested_arch.file == NULL)
21579 {
21580 error (_("%s: contains corrupt thin archive: %s\n"),
28e817cc 21581 qualified_name, name);
fd486f32
AM
21582 free (qualified_name);
21583 free (name);
015dc7e1 21584 ret = false;
a043396b
NC
21585 break;
21586 }
fd486f32 21587 free (name);
a043396b 21588
1cb7d8b1 21589 /* This is a proxy for a member of a nested archive. */
978c4450
AM
21590 filedata->archive_file_offset
21591 = arch.nested_member_origin + sizeof arch.arhdr;
2cf0635d 21592
1cb7d8b1
AM
21593 /* The nested archive file will have been opened and setup by
21594 get_archive_member_name. */
978c4450
AM
21595 if (fseek (nested_arch.file, filedata->archive_file_offset,
21596 SEEK_SET) != 0)
1cb7d8b1
AM
21597 {
21598 error (_("%s: failed to seek to archive member.\n"),
21599 nested_arch.file_name);
fd486f32 21600 free (qualified_name);
015dc7e1 21601 ret = false;
1cb7d8b1
AM
21602 break;
21603 }
2cf0635d 21604
dda8d76d
NC
21605 thin_filedata.handle = nested_arch.file;
21606 thin_filedata.file_name = qualified_name;
9abca702 21607
1cb7d8b1 21608 if (! process_object (& thin_filedata))
015dc7e1 21609 ret = false;
1cb7d8b1 21610 }
2cf0635d 21611 else
1cb7d8b1 21612 {
fd486f32 21613 free (name);
978c4450 21614 filedata->archive_file_offset = arch.next_arhdr_offset;
6a6196fc 21615 filedata->file_name = qualified_name;
1cb7d8b1 21616 if (! process_object (filedata))
015dc7e1 21617 ret = false;
978c4450 21618 arch.next_arhdr_offset += filedata->archive_file_size;
4c836627 21619 /* Stop looping with "negative" archive_file_size. */
978c4450 21620 if (arch.next_arhdr_offset < filedata->archive_file_size)
80e2a3b6 21621 arch.next_arhdr_offset = -1ul;
1cb7d8b1 21622 }
fb52b2f4 21623
2cf0635d 21624 free (qualified_name);
fb52b2f4
NC
21625 }
21626
4145f1d5 21627 out:
2cf0635d
NC
21628 if (nested_arch.file != NULL)
21629 fclose (nested_arch.file);
21630 release_archive (&nested_arch);
21631 release_archive (&arch);
fb52b2f4 21632
d989285c 21633 return ret;
fb52b2f4
NC
21634}
21635
015dc7e1 21636static bool
2cf0635d 21637process_file (char * file_name)
fb52b2f4 21638{
dda8d76d 21639 Filedata * filedata = NULL;
fb52b2f4
NC
21640 struct stat statbuf;
21641 char armag[SARMAG];
015dc7e1 21642 bool ret = true;
fb52b2f4
NC
21643
21644 if (stat (file_name, &statbuf) < 0)
21645 {
f24ddbdd
NC
21646 if (errno == ENOENT)
21647 error (_("'%s': No such file\n"), file_name);
21648 else
21649 error (_("Could not locate '%s'. System error message: %s\n"),
21650 file_name, strerror (errno));
015dc7e1 21651 return false;
f24ddbdd
NC
21652 }
21653
21654 if (! S_ISREG (statbuf.st_mode))
21655 {
21656 error (_("'%s' is not an ordinary file\n"), file_name);
015dc7e1 21657 return false;
fb52b2f4
NC
21658 }
21659
dda8d76d
NC
21660 filedata = calloc (1, sizeof * filedata);
21661 if (filedata == NULL)
21662 {
21663 error (_("Out of memory allocating file data structure\n"));
015dc7e1 21664 return false;
dda8d76d
NC
21665 }
21666
21667 filedata->file_name = file_name;
21668 filedata->handle = fopen (file_name, "rb");
21669 if (filedata->handle == NULL)
fb52b2f4 21670 {
f24ddbdd 21671 error (_("Input file '%s' is not readable.\n"), file_name);
dda8d76d 21672 free (filedata);
015dc7e1 21673 return false;
fb52b2f4
NC
21674 }
21675
dda8d76d 21676 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
fb52b2f4 21677 {
4145f1d5 21678 error (_("%s: Failed to read file's magic number\n"), file_name);
dda8d76d
NC
21679 fclose (filedata->handle);
21680 free (filedata);
015dc7e1 21681 return false;
fb52b2f4
NC
21682 }
21683
dda8d76d 21684 filedata->file_size = (bfd_size_type) statbuf.st_size;
015dc7e1 21685 filedata->is_separate = false;
f54498b4 21686
fb52b2f4 21687 if (memcmp (armag, ARMAG, SARMAG) == 0)
32ec8896 21688 {
015dc7e1
AM
21689 if (! process_archive (filedata, false))
21690 ret = false;
32ec8896 21691 }
2cf0635d 21692 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
32ec8896 21693 {
015dc7e1
AM
21694 if ( ! process_archive (filedata, true))
21695 ret = false;
32ec8896 21696 }
fb52b2f4
NC
21697 else
21698 {
1b513401 21699 if (do_archive_index && !check_all)
4145f1d5
NC
21700 error (_("File %s is not an archive so its index cannot be displayed.\n"),
21701 file_name);
21702
dda8d76d 21703 rewind (filedata->handle);
978c4450 21704 filedata->archive_file_size = filedata->archive_file_offset = 0;
32ec8896 21705
dda8d76d 21706 if (! process_object (filedata))
015dc7e1 21707 ret = false;
fb52b2f4
NC
21708 }
21709
dda8d76d 21710 fclose (filedata->handle);
8fb879cd
AM
21711 free (filedata->section_headers);
21712 free (filedata->program_headers);
21713 free (filedata->string_table);
6431e409 21714 free (filedata->dump.dump_sects);
dda8d76d 21715 free (filedata);
32ec8896 21716
fd486f32 21717 free (ba_cache.strtab);
1bd6175a 21718 ba_cache.strtab = NULL;
fd486f32 21719 free (ba_cache.symtab);
1bd6175a 21720 ba_cache.symtab = NULL;
fd486f32
AM
21721 ba_cache.filedata = NULL;
21722
fb52b2f4
NC
21723 return ret;
21724}
21725
252b5132
RH
21726#ifdef SUPPORT_DISASSEMBLY
21727/* Needed by the i386 disassembler. For extra credit, someone could
9ea033b2 21728 fix this so that we insert symbolic addresses here, esp for GOT/PLT
e3c8793a 21729 symbols. */
252b5132
RH
21730
21731void
2cf0635d 21732print_address (unsigned int addr, FILE * outfile)
252b5132
RH
21733{
21734 fprintf (outfile,"0x%8.8x", addr);
21735}
21736
e3c8793a 21737/* Needed by the i386 disassembler. */
dda8d76d 21738
252b5132
RH
21739void
21740db_task_printsym (unsigned int addr)
21741{
21742 print_address (addr, stderr);
21743}
21744#endif
21745
21746int
2cf0635d 21747main (int argc, char ** argv)
252b5132 21748{
ff78d6d6
L
21749 int err;
21750
252b5132
RH
21751#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
21752 setlocale (LC_MESSAGES, "");
3882b010
L
21753#endif
21754#if defined (HAVE_SETLOCALE)
21755 setlocale (LC_CTYPE, "");
252b5132
RH
21756#endif
21757 bindtextdomain (PACKAGE, LOCALEDIR);
21758 textdomain (PACKAGE);
21759
869b9d07
MM
21760 expandargv (&argc, &argv);
21761
dda8d76d 21762 parse_args (& cmdline, argc, argv);
59f14fc0 21763
18bd398b 21764 if (optind < (argc - 1))
1b513401
NC
21765 /* When displaying information for more than one file,
21766 prefix the information with the file name. */
015dc7e1 21767 show_name = true;
5656ba2c
L
21768 else if (optind >= argc)
21769 {
1b513401 21770 /* Ensure that the warning is always displayed. */
015dc7e1 21771 do_checks = true;
1b513401 21772
5656ba2c
L
21773 warn (_("Nothing to do.\n"));
21774 usage (stderr);
21775 }
18bd398b 21776
015dc7e1 21777 err = false;
252b5132 21778 while (optind < argc)
32ec8896 21779 if (! process_file (argv[optind++]))
015dc7e1 21780 err = true;
252b5132 21781
9db70fc3 21782 free (cmdline.dump_sects);
252b5132 21783
7d9813f1
NA
21784 free (dump_ctf_symtab_name);
21785 free (dump_ctf_strtab_name);
21786 free (dump_ctf_parent_name);
21787
32ec8896 21788 return err ? EXIT_FAILURE : EXIT_SUCCESS;
252b5132 21789}