]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - binutils/readelf.c
Automatic date update in version.in
[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>
7bfd842d 47#include <wchar.h>
252b5132 48
a952a375 49#if __GNUC__ >= 2
19936277 50/* Define BFD64 here, even if our default architecture is 32 bit ELF
a952a375 51 as this will allow us to read in and parse 64bit and 32bit ELF files.
b34976b6 52 Only do this if we believe that the compiler can support a 64 bit
a952a375 53 data type. For now we only rely on GCC being able to do this. */
19936277 54#define BFD64
a952a375
NC
55#endif
56
3db64b00
AM
57#include "bfd.h"
58#include "bucomm.h"
3284fe0c 59#include "elfcomm.h"
19e6b90e 60#include "dwarf.h"
7d9813f1 61#include "ctf-api.h"
79bc120c 62#include "demangle.h"
252b5132
RH
63
64#include "elf/common.h"
65#include "elf/external.h"
66#include "elf/internal.h"
252b5132 67
4b78141a
NC
68
69/* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
70 we can obtain the H8 reloc numbers. We need these for the
71 get_reloc_size() function. We include h8.h again after defining
72 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
73
74#include "elf/h8.h"
75#undef _ELF_H8_H
76
77/* Undo the effects of #including reloc-macros.h. */
78
79#undef START_RELOC_NUMBERS
80#undef RELOC_NUMBER
81#undef FAKE_RELOC
82#undef EMPTY_RELOC
83#undef END_RELOC_NUMBERS
84#undef _RELOC_MACROS_H
85
252b5132
RH
86/* The following headers use the elf/reloc-macros.h file to
87 automatically generate relocation recognition functions
88 such as elf_mips_reloc_type() */
89
90#define RELOC_MACROS_GEN_FUNC
91
a06ea964 92#include "elf/aarch64.h"
252b5132 93#include "elf/alpha.h"
3b16e843 94#include "elf/arc.h"
252b5132 95#include "elf/arm.h"
3b16e843 96#include "elf/avr.h"
1d65ded4 97#include "elf/bfin.h"
60bca95a 98#include "elf/cr16.h"
3b16e843 99#include "elf/cris.h"
1c0d3aa6 100#include "elf/crx.h"
b8891f8d 101#include "elf/csky.h"
252b5132
RH
102#include "elf/d10v.h"
103#include "elf/d30v.h"
d172d4ba 104#include "elf/dlx.h"
aca4efc7 105#include "elf/bpf.h"
cfb8c092 106#include "elf/epiphany.h"
252b5132 107#include "elf/fr30.h"
5c70f934 108#include "elf/frv.h"
3f8107ab 109#include "elf/ft32.h"
3b16e843
NC
110#include "elf/h8.h"
111#include "elf/hppa.h"
112#include "elf/i386.h"
f954747f
AM
113#include "elf/i370.h"
114#include "elf/i860.h"
115#include "elf/i960.h"
3b16e843 116#include "elf/ia64.h"
1e4cf259 117#include "elf/ip2k.h"
84e94c90 118#include "elf/lm32.h"
1c0d3aa6 119#include "elf/iq2000.h"
49f58d10 120#include "elf/m32c.h"
3b16e843
NC
121#include "elf/m32r.h"
122#include "elf/m68k.h"
75751cd9 123#include "elf/m68hc11.h"
7b4ae824 124#include "elf/s12z.h"
252b5132 125#include "elf/mcore.h"
15ab5209 126#include "elf/mep.h"
a3c62988 127#include "elf/metag.h"
7ba29e2a 128#include "elf/microblaze.h"
3b16e843 129#include "elf/mips.h"
3c3bdf30 130#include "elf/mmix.h"
3b16e843
NC
131#include "elf/mn10200.h"
132#include "elf/mn10300.h"
5506d11a 133#include "elf/moxie.h"
4970f871 134#include "elf/mt.h"
2469cfa2 135#include "elf/msp430.h"
35c08157 136#include "elf/nds32.h"
fe944acf 137#include "elf/nfp.h"
13761a11 138#include "elf/nios2.h"
73589c9d 139#include "elf/or1k.h"
7d466069 140#include "elf/pj.h"
3b16e843 141#include "elf/ppc.h"
c833c019 142#include "elf/ppc64.h"
2b100bb5 143#include "elf/pru.h"
03336641 144#include "elf/riscv.h"
99c513f6 145#include "elf/rl78.h"
c7927a3c 146#include "elf/rx.h"
a85d7ed0 147#include "elf/s390.h"
1c0d3aa6 148#include "elf/score.h"
3b16e843
NC
149#include "elf/sh.h"
150#include "elf/sparc.h"
e9f53129 151#include "elf/spu.h"
40b36596 152#include "elf/tic6x.h"
aa137e4d
NC
153#include "elf/tilegx.h"
154#include "elf/tilepro.h"
3b16e843 155#include "elf/v850.h"
179d3252 156#include "elf/vax.h"
619ed720 157#include "elf/visium.h"
f96bd6c2 158#include "elf/wasm32.h"
3b16e843 159#include "elf/x86-64.h"
c29aca4a 160#include "elf/xc16x.h"
f6c1a2d5 161#include "elf/xgate.h"
93fbbb04 162#include "elf/xstormy16.h"
88da6820 163#include "elf/xtensa.h"
6655dba2 164#include "elf/z80.h"
e9a0721f 165#include "elf/loongarch.h"
252b5132 166
252b5132 167#include "getopt.h"
566b0d53 168#include "libiberty.h"
09c11c86 169#include "safe-ctype.h"
2cf0635d 170#include "filenames.h"
252b5132 171
15b42fb0
AM
172#ifndef offsetof
173#define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
174#endif
175
6a40cf0c
NC
176typedef struct elf_section_list
177{
dda8d76d
NC
178 Elf_Internal_Shdr * hdr;
179 struct elf_section_list * next;
6a40cf0c
NC
180} elf_section_list;
181
dda8d76d
NC
182/* Flag bits indicating particular types of dump. */
183#define HEX_DUMP (1 << 0) /* The -x command line switch. */
184#define DISASS_DUMP (1 << 1) /* The -i command line switch. */
185#define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
186#define STRING_DUMP (1 << 3) /* The -p command line switch. */
187#define RELOC_DUMP (1 << 4) /* The -R command line switch. */
d344b407 188#define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
dda8d76d
NC
189
190typedef unsigned char dump_type;
191
192/* A linked list of the section names for which dumps were requested. */
193struct dump_list_entry
194{
195 char * name;
196 dump_type type;
197 struct dump_list_entry * next;
198};
199
6431e409
AM
200/* A dynamic array of flags indicating for which sections a dump
201 has been requested via command line switches. */
1b513401
NC
202struct dump_data
203{
6431e409
AM
204 dump_type * dump_sects;
205 unsigned int num_dump_sects;
206};
207
208static struct dump_data cmdline;
209
210static struct dump_list_entry * dump_sects_byname;
211
2cf0635d 212char * program_name = "readelf";
dda8d76d 213
015dc7e1
AM
214static bool show_name = false;
215static bool do_dynamic = false;
216static bool do_syms = false;
217static bool do_dyn_syms = false;
218static bool do_lto_syms = false;
219static bool do_reloc = false;
220static bool do_sections = false;
221static bool do_section_groups = false;
222static bool do_section_details = false;
223static bool do_segments = false;
224static bool do_unwind = false;
225static bool do_using_dynamic = false;
226static bool do_header = false;
227static bool do_dump = false;
228static bool do_version = false;
229static bool do_histogram = false;
230static bool do_debugging = false;
231static bool do_ctf = false;
232static bool do_arch = false;
233static bool do_notes = false;
234static bool do_archive_index = false;
235static bool check_all = false;
236static bool is_32bit_elf = false;
237static bool decompress_dumps = false;
238static bool do_not_show_symbol_truncation = false;
239static bool do_demangle = false; /* Pretty print C++ symbol names. */
240static bool process_links = false;
79bc120c 241static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
047c3dbf 242static int sym_base = 0;
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;
066f8fbe
AM
267 unsigned long archive_file_offset;
268 unsigned long archive_file_size;
269 /* Everything below this point is cleared out by free_filedata. */
978c4450
AM
270 Elf_Internal_Shdr * section_headers;
271 Elf_Internal_Phdr * program_headers;
272 char * string_table;
273 unsigned long string_table_length;
978c4450
AM
274 unsigned long dynamic_addr;
275 bfd_size_type dynamic_size;
276 size_t dynamic_nent;
277 Elf_Internal_Dyn * dynamic_section;
8ac10c5b 278 Elf_Internal_Shdr * dynamic_strtab_section;
978c4450
AM
279 char * dynamic_strings;
280 unsigned long dynamic_strings_length;
8ac10c5b 281 Elf_Internal_Shdr * dynamic_symtab_section;
978c4450
AM
282 unsigned long num_dynamic_syms;
283 Elf_Internal_Sym * dynamic_symbols;
284 bfd_vma version_info[16];
285 unsigned int dynamic_syminfo_nent;
286 Elf_Internal_Syminfo * dynamic_syminfo;
287 unsigned long dynamic_syminfo_offset;
288 bfd_size_type nbuckets;
289 bfd_size_type nchains;
290 bfd_vma * buckets;
291 bfd_vma * chains;
292 bfd_size_type ngnubuckets;
293 bfd_size_type ngnuchains;
294 bfd_vma * gnubuckets;
295 bfd_vma * gnuchains;
296 bfd_vma * mipsxlat;
297 bfd_vma gnusymidx;
13acb58d 298 char * program_interpreter;
978c4450
AM
299 bfd_vma dynamic_info[DT_ENCODING];
300 bfd_vma dynamic_info_DT_GNU_HASH;
301 bfd_vma dynamic_info_DT_MIPS_XHASH;
302 elf_section_list * symtab_shndx_list;
303 size_t group_count;
304 struct group * section_groups;
305 struct group ** section_headers_groups;
306 /* A dynamic array of flags indicating for which sections a dump of
307 some kind has been requested. It is reset on a per-object file
308 basis and then initialised from the cmdline_dump_sects array,
309 the results of interpreting the -w switch, and the
310 dump_sects_byname list. */
311 struct dump_data dump;
312} Filedata;
aef1f6d0 313
c256ffe7 314/* How to print a vma value. */
843dd992
NC
315typedef enum print_mode
316{
317 HEX,
047c3dbf 318 HEX_5,
843dd992
NC
319 DEC,
320 DEC_5,
321 UNSIGNED,
047c3dbf 322 UNSIGNED_5,
843dd992 323 PREFIX_HEX,
047c3dbf 324 PREFIX_HEX_5,
843dd992 325 FULL_HEX,
047c3dbf
NL
326 LONG_HEX,
327 OCTAL,
328 OCTAL_5
843dd992
NC
329}
330print_mode;
331
b3aa80b4
NC
332typedef enum unicode_display_type
333{
334 unicode_default = 0,
335 unicode_locale,
336 unicode_escape,
337 unicode_hex,
338 unicode_highlight,
339 unicode_invalid
340} unicode_display_type;
341
342static unicode_display_type unicode_display = unicode_default;
343
344
bb4d2ac2
L
345/* Versioned symbol info. */
346enum versioned_symbol_info
347{
348 symbol_undefined,
349 symbol_hidden,
350 symbol_public
351};
352
32ec8896 353static const char * get_symbol_version_string
015dc7e1 354 (Filedata *, bool, const char *, unsigned long, unsigned,
32ec8896 355 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
bb4d2ac2 356
9c19a809
NC
357#define UNKNOWN -1
358
84714f86
AM
359static inline const char *
360section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
361{
362 return filedata->string_table + hdr->sh_name;
363}
b9e920ec 364
84714f86
AM
365static inline bool
366section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
367{
368 return (hdr != NULL
369 && filedata->string_table != NULL
370 && hdr->sh_name < filedata->string_table_length);
371}
b9e920ec 372
84714f86
AM
373static inline const char *
374section_name_print (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
375{
376 if (hdr == NULL)
377 return _("<none>");
378 if (filedata->string_table == NULL)
379 return _("<no-strings>");
380 if (hdr->sh_name >= filedata->string_table_length)
381 return _("<corrupt>");
382 return section_name (filedata, hdr);
383}
252b5132 384
ee42cf8c 385#define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
252b5132 386
84714f86
AM
387static inline bool
388valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
389{
390 return strtab != NULL && offset < strtab_size;
391}
392
393static inline bool
394valid_dynamic_name (const Filedata *filedata, uint64_t offset)
395{
396 return valid_symbol_name (filedata->dynamic_strings,
397 filedata->dynamic_strings_length, offset);
398}
399
d79b3d50
NC
400/* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
401 already been called and verified that the string exists. */
84714f86
AM
402static inline const char *
403get_dynamic_name (const Filedata *filedata, size_t offset)
404{
405 return filedata->dynamic_strings + offset;
406}
18bd398b 407
61865e30
NC
408#define REMOVE_ARCH_BITS(ADDR) \
409 do \
410 { \
dda8d76d 411 if (filedata->file_header.e_machine == EM_ARM) \
61865e30
NC
412 (ADDR) &= ~1; \
413 } \
414 while (0)
f16a9783
MS
415
416/* Get the correct GNU hash section name. */
978c4450
AM
417#define GNU_HASH_SECTION_NAME(filedata) \
418 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
d79b3d50 419\f
66cfc0fd
AM
420/* Print a BFD_VMA to an internal buffer, for use in error messages.
421 BFD_FMA_FMT can't be used in translated strings. */
422
423static const char *
424bfd_vmatoa (char *fmtch, bfd_vma value)
425{
426 /* bfd_vmatoa is used more then once in a printf call for output.
427 Cycle through an array of buffers. */
428 static int buf_pos = 0;
429 static struct bfd_vmatoa_buf
430 {
431 char place[64];
432 } buf[4];
433 char *ret;
434 char fmt[32];
435
436 ret = buf[buf_pos++].place;
437 buf_pos %= ARRAY_SIZE (buf);
438
439 sprintf (fmt, "%%%s%s", BFD_VMA_FMT, fmtch);
440 snprintf (ret, sizeof (buf[0].place), fmt, value);
441 return ret;
442}
443
dda8d76d
NC
444/* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
445 OFFSET + the offset of the current archive member, if we are examining an
446 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
447 allocate a buffer using malloc and fill that. In either case return the
448 pointer to the start of the retrieved data or NULL if something went wrong.
449 If something does go wrong and REASON is not NULL then emit an error
450 message using REASON as part of the context. */
59245841 451
c256ffe7 452static void *
dda8d76d
NC
453get_data (void * var,
454 Filedata * filedata,
455 unsigned long offset,
456 bfd_size_type size,
457 bfd_size_type nmemb,
458 const char * reason)
a6e9f9df 459{
2cf0635d 460 void * mvar;
57028622 461 bfd_size_type amt = size * nmemb;
a6e9f9df 462
c256ffe7 463 if (size == 0 || nmemb == 0)
a6e9f9df
AM
464 return NULL;
465
57028622
NC
466 /* If the size_t type is smaller than the bfd_size_type, eg because
467 you are building a 32-bit tool on a 64-bit host, then make sure
468 that when the sizes are cast to (size_t) no information is lost. */
7c1c1904
AM
469 if ((size_t) size != size
470 || (size_t) nmemb != nmemb
471 || (size_t) amt != amt)
57028622
NC
472 {
473 if (reason)
66cfc0fd
AM
474 error (_("Size truncation prevents reading %s"
475 " elements of size %s for %s\n"),
476 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
57028622
NC
477 return NULL;
478 }
479
480 /* Check for size overflow. */
7c1c1904 481 if (amt / size != nmemb || (size_t) amt + 1 == 0)
57028622
NC
482 {
483 if (reason)
66cfc0fd
AM
484 error (_("Size overflow prevents reading %s"
485 " elements of size %s for %s\n"),
486 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
57028622
NC
487 return NULL;
488 }
489
c22b42ce 490 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
c9c1d674 491 attempting to allocate memory when the read is bound to fail. */
978c4450
AM
492 if (filedata->archive_file_offset > filedata->file_size
493 || offset > filedata->file_size - filedata->archive_file_offset
494 || amt > filedata->file_size - filedata->archive_file_offset - offset)
a6e9f9df 495 {
049b0c3a 496 if (reason)
66cfc0fd
AM
497 error (_("Reading %s bytes extends past end of file for %s\n"),
498 bfd_vmatoa ("u", amt), reason);
a6e9f9df
AM
499 return NULL;
500 }
501
978c4450
AM
502 if (fseek (filedata->handle, filedata->archive_file_offset + offset,
503 SEEK_SET))
071436c6
NC
504 {
505 if (reason)
c9c1d674 506 error (_("Unable to seek to 0x%lx for %s\n"),
978c4450 507 filedata->archive_file_offset + offset, reason);
071436c6
NC
508 return NULL;
509 }
510
a6e9f9df
AM
511 mvar = var;
512 if (mvar == NULL)
513 {
7c1c1904
AM
514 /* + 1 so that we can '\0' terminate invalid string table sections. */
515 mvar = malloc ((size_t) amt + 1);
a6e9f9df
AM
516
517 if (mvar == NULL)
518 {
049b0c3a 519 if (reason)
66cfc0fd
AM
520 error (_("Out of memory allocating %s bytes for %s\n"),
521 bfd_vmatoa ("u", amt), reason);
a6e9f9df
AM
522 return NULL;
523 }
c256ffe7 524
c9c1d674 525 ((char *) mvar)[amt] = '\0';
a6e9f9df
AM
526 }
527
dda8d76d 528 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
a6e9f9df 529 {
049b0c3a 530 if (reason)
66cfc0fd
AM
531 error (_("Unable to read in %s bytes of %s\n"),
532 bfd_vmatoa ("u", amt), reason);
a6e9f9df
AM
533 if (mvar != var)
534 free (mvar);
535 return NULL;
536 }
537
538 return mvar;
539}
540
32ec8896
NC
541/* Print a VMA value in the MODE specified.
542 Returns the number of characters displayed. */
cb8f3167 543
32ec8896 544static unsigned int
14a91970 545print_vma (bfd_vma vma, print_mode mode)
66543521 546{
32ec8896 547 unsigned int nc = 0;
66543521 548
14a91970 549 switch (mode)
66543521 550 {
14a91970
AM
551 case FULL_HEX:
552 nc = printf ("0x");
1a0670f3 553 /* Fall through. */
14a91970 554 case LONG_HEX:
f7a99963 555#ifdef BFD64
14a91970 556 if (is_32bit_elf)
437c2fb7 557 return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
f7a99963 558#endif
14a91970
AM
559 printf_vma (vma);
560 return nc + 16;
b19aac67 561
14a91970
AM
562 case DEC_5:
563 if (vma <= 99999)
564 return printf ("%5" BFD_VMA_FMT "d", vma);
1a0670f3 565 /* Fall through. */
14a91970
AM
566 case PREFIX_HEX:
567 nc = printf ("0x");
1a0670f3 568 /* Fall through. */
14a91970
AM
569 case HEX:
570 return nc + printf ("%" BFD_VMA_FMT "x", vma);
b19aac67 571
047c3dbf
NL
572 case PREFIX_HEX_5:
573 nc = printf ("0x");
574 /* Fall through. */
575 case HEX_5:
576 return nc + printf ("%05" BFD_VMA_FMT "x", vma);
577
14a91970
AM
578 case DEC:
579 return printf ("%" BFD_VMA_FMT "d", vma);
b19aac67 580
14a91970
AM
581 case UNSIGNED:
582 return printf ("%" BFD_VMA_FMT "u", vma);
32ec8896 583
047c3dbf
NL
584 case UNSIGNED_5:
585 return printf ("%5" BFD_VMA_FMT "u", vma);
586
587 case OCTAL:
588 return printf ("%" BFD_VMA_FMT "o", vma);
589
590 case OCTAL_5:
591 return printf ("%5" BFD_VMA_FMT "o", vma);
592
32ec8896
NC
593 default:
594 /* FIXME: Report unrecognised mode ? */
595 return 0;
f7a99963 596 }
f7a99963
NC
597}
598
047c3dbf 599
7bfd842d 600/* Display a symbol on stdout. Handles the display of control characters and
3bfcb652 601 multibye characters (assuming the host environment supports them).
31104126 602
7bfd842d
NC
603 Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
604
0942c7ab
NC
605 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
606 abs(WIDTH) - 5 characters followed by "[...]".
607
7bfd842d
NC
608 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
609 padding as necessary.
171191ba
NC
610
611 Returns the number of emitted characters. */
612
613static unsigned int
0942c7ab 614print_symbol (signed int width, const char * symbol)
31104126 615{
015dc7e1
AM
616 bool extra_padding = false;
617 bool do_dots = false;
32ec8896 618 signed int num_printed = 0;
3bfcb652 619#ifdef HAVE_MBSTATE_T
7bfd842d 620 mbstate_t state;
3bfcb652 621#endif
32ec8896 622 unsigned int width_remaining;
79bc120c 623 const void * alloced_symbol = NULL;
961c521f 624
7bfd842d 625 if (width < 0)
961c521f 626 {
88305e1b 627 /* Keep the width positive. This helps the code below. */
961c521f 628 width = - width;
015dc7e1 629 extra_padding = true;
0b4362b0 630 }
56d8f8a9
NC
631 else if (width == 0)
632 return 0;
961c521f 633
7bfd842d
NC
634 if (do_wide)
635 /* Set the remaining width to a very large value.
636 This simplifies the code below. */
637 width_remaining = INT_MAX;
638 else
0942c7ab
NC
639 {
640 width_remaining = width;
641 if (! do_not_show_symbol_truncation
642 && (int) strlen (symbol) > width)
643 {
644 width_remaining -= 5;
645 if ((int) width_remaining < 0)
646 width_remaining = 0;
015dc7e1 647 do_dots = true;
0942c7ab
NC
648 }
649 }
cb8f3167 650
3bfcb652 651#ifdef HAVE_MBSTATE_T
7bfd842d
NC
652 /* Initialise the multibyte conversion state. */
653 memset (& state, 0, sizeof (state));
3bfcb652 654#endif
961c521f 655
79bc120c
NC
656 if (do_demangle && *symbol)
657 {
658 const char * res = cplus_demangle (symbol, demangle_flags);
659
660 if (res != NULL)
661 alloced_symbol = symbol = res;
662 }
663
7bfd842d
NC
664 while (width_remaining)
665 {
666 size_t n;
7bfd842d 667 const char c = *symbol++;
961c521f 668
7bfd842d 669 if (c == 0)
961c521f
NC
670 break;
671
b3aa80b4
NC
672 if (ISPRINT (c))
673 {
674 putchar (c);
675 width_remaining --;
676 num_printed ++;
677 }
678 else if (ISCNTRL (c))
961c521f 679 {
b3aa80b4
NC
680 /* Do not print control characters directly as they can affect terminal
681 settings. Such characters usually appear in the names generated
682 by the assembler for local labels. */
683
7bfd842d 684 if (width_remaining < 2)
961c521f
NC
685 break;
686
7bfd842d
NC
687 printf ("^%c", c + 0x40);
688 width_remaining -= 2;
171191ba 689 num_printed += 2;
961c521f 690 }
b3aa80b4 691 else if (c == 0x7f)
7bfd842d 692 {
b3aa80b4
NC
693 if (width_remaining < 5)
694 break;
695 printf ("<DEL>");
696 width_remaining -= 5;
697 num_printed += 5;
698 }
699 else if (unicode_display != unicode_locale
700 && unicode_display != unicode_default)
701 {
702 /* Display unicode characters as something else. */
703 unsigned char bytes[4];
704 bool is_utf8;
795588ae 705 unsigned int nbytes;
b3aa80b4
NC
706
707 bytes[0] = c;
708
709 if (bytes[0] < 0xc0)
710 {
711 nbytes = 1;
712 is_utf8 = false;
713 }
714 else
715 {
716 bytes[1] = *symbol++;
717
718 if ((bytes[1] & 0xc0) != 0x80)
719 {
720 is_utf8 = false;
721 /* Do not consume this character. It may only
722 be the first byte in the sequence that was
723 corrupt. */
724 --symbol;
725 nbytes = 1;
726 }
727 else if ((bytes[0] & 0x20) == 0)
728 {
729 is_utf8 = true;
730 nbytes = 2;
731 }
732 else
733 {
734 bytes[2] = *symbol++;
735
736 if ((bytes[2] & 0xc0) != 0x80)
737 {
738 is_utf8 = false;
739 symbol -= 2;
740 nbytes = 1;
741 }
742 else if ((bytes[0] & 0x10) == 0)
743 {
744 is_utf8 = true;
745 nbytes = 3;
746 }
747 else
748 {
749 bytes[3] = *symbol++;
750
751 nbytes = 4;
752
753 if ((bytes[3] & 0xc0) != 0x80)
754 {
755 is_utf8 = false;
756 symbol -= 3;
757 nbytes = 1;
758 }
759 else
760 is_utf8 = true;
761 }
762 }
763 }
764
765 if (unicode_display == unicode_invalid)
766 is_utf8 = false;
767
768 if (unicode_display == unicode_hex || ! is_utf8)
769 {
795588ae 770 unsigned int i;
b3aa80b4
NC
771
772 if (width_remaining < (nbytes * 2) + 2)
773 break;
774
775 putchar (is_utf8 ? '<' : '{');
776 printf ("0x");
777 for (i = 0; i < nbytes; i++)
778 printf ("%02x", bytes[i]);
779 putchar (is_utf8 ? '>' : '}');
780 }
781 else
782 {
783 if (unicode_display == unicode_highlight && isatty (1))
784 printf ("\x1B[31;47m"); /* Red. */
785
786 switch (nbytes)
787 {
788 case 2:
789 if (width_remaining < 6)
790 break;
791 printf ("\\u%02x%02x",
792 (bytes[0] & 0x1c) >> 2,
793 ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
794 break;
795 case 3:
796 if (width_remaining < 6)
797 break;
798 printf ("\\u%02x%02x",
799 ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
800 ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
801 break;
802 case 4:
803 if (width_remaining < 8)
804 break;
805 printf ("\\u%02x%02x%02x",
806 ((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
807 ((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
808 ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
809
810 break;
811 default:
812 /* URG. */
813 break;
814 }
815
816 if (unicode_display == unicode_highlight && isatty (1))
817 printf ("\033[0m"); /* Default colour. */
818 }
819
820 if (bytes[nbytes - 1] == 0)
821 break;
7bfd842d 822 }
961c521f
NC
823 else
824 {
3bfcb652
NC
825#ifdef HAVE_MBSTATE_T
826 wchar_t w;
827#endif
7bfd842d
NC
828 /* Let printf do the hard work of displaying multibyte characters. */
829 printf ("%.1s", symbol - 1);
830 width_remaining --;
831 num_printed ++;
832
3bfcb652 833#ifdef HAVE_MBSTATE_T
7bfd842d
NC
834 /* Try to find out how many bytes made up the character that was
835 just printed. Advance the symbol pointer past the bytes that
836 were displayed. */
837 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
3bfcb652
NC
838#else
839 n = 1;
840#endif
7bfd842d
NC
841 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
842 symbol += (n - 1);
961c521f 843 }
961c521f 844 }
171191ba 845
0942c7ab
NC
846 if (do_dots)
847 num_printed += printf ("[...]");
848
7bfd842d 849 if (extra_padding && num_printed < width)
171191ba
NC
850 {
851 /* Fill in the remaining spaces. */
7bfd842d
NC
852 printf ("%-*s", width - num_printed, " ");
853 num_printed = width;
171191ba
NC
854 }
855
79bc120c 856 free ((void *) alloced_symbol);
171191ba 857 return num_printed;
31104126
NC
858}
859
1449284b 860/* Returns a pointer to a static buffer containing a printable version of
74e1a04b
NC
861 the given section's name. Like print_symbol, except that it does not try
862 to print multibyte characters, it just interprets them as hex values. */
863
864static const char *
dda8d76d 865printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
74e1a04b 866{
ca0e11aa 867#define MAX_PRINT_SEC_NAME_LEN 256
74e1a04b 868 static char sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
84714f86 869 const char * name = section_name_print (filedata, sec);
74e1a04b
NC
870 char * buf = sec_name_buf;
871 char c;
872 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
873
874 while ((c = * name ++) != 0)
875 {
876 if (ISCNTRL (c))
877 {
878 if (remaining < 2)
879 break;
948f632f 880
74e1a04b
NC
881 * buf ++ = '^';
882 * buf ++ = c + 0x40;
883 remaining -= 2;
884 }
885 else if (ISPRINT (c))
886 {
887 * buf ++ = c;
888 remaining -= 1;
889 }
890 else
891 {
892 static char hex[17] = "0123456789ABCDEF";
893
894 if (remaining < 4)
895 break;
896 * buf ++ = '<';
897 * buf ++ = hex[(c & 0xf0) >> 4];
898 * buf ++ = hex[c & 0x0f];
899 * buf ++ = '>';
900 remaining -= 4;
901 }
902
903 if (remaining == 0)
904 break;
905 }
906
907 * buf = 0;
908 return sec_name_buf;
909}
910
911static const char *
dda8d76d 912printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
74e1a04b 913{
dda8d76d 914 if (ndx >= filedata->file_header.e_shnum)
74e1a04b
NC
915 return _("<corrupt>");
916
dda8d76d 917 return printable_section_name (filedata, filedata->section_headers + ndx);
74e1a04b
NC
918}
919
89fac5e3
RS
920/* Return a pointer to section NAME, or NULL if no such section exists. */
921
922static Elf_Internal_Shdr *
dda8d76d 923find_section (Filedata * filedata, const char * name)
89fac5e3
RS
924{
925 unsigned int i;
926
68807c3c
NC
927 if (filedata->section_headers == NULL)
928 return NULL;
dda8d76d
NC
929
930 for (i = 0; i < filedata->file_header.e_shnum; i++)
84714f86
AM
931 if (section_name_valid (filedata, filedata->section_headers + i)
932 && streq (section_name (filedata, filedata->section_headers + i),
933 name))
dda8d76d 934 return filedata->section_headers + i;
89fac5e3
RS
935
936 return NULL;
937}
938
0b6ae522
DJ
939/* Return a pointer to a section containing ADDR, or NULL if no such
940 section exists. */
941
942static Elf_Internal_Shdr *
dda8d76d 943find_section_by_address (Filedata * filedata, bfd_vma addr)
0b6ae522
DJ
944{
945 unsigned int i;
946
68807c3c
NC
947 if (filedata->section_headers == NULL)
948 return NULL;
949
dda8d76d 950 for (i = 0; i < filedata->file_header.e_shnum; i++)
0b6ae522 951 {
dda8d76d
NC
952 Elf_Internal_Shdr *sec = filedata->section_headers + i;
953
0b6ae522
DJ
954 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
955 return sec;
956 }
957
958 return NULL;
959}
960
071436c6 961static Elf_Internal_Shdr *
dda8d76d 962find_section_by_type (Filedata * filedata, unsigned int type)
071436c6
NC
963{
964 unsigned int i;
965
68807c3c
NC
966 if (filedata->section_headers == NULL)
967 return NULL;
968
dda8d76d 969 for (i = 0; i < filedata->file_header.e_shnum; i++)
071436c6 970 {
dda8d76d
NC
971 Elf_Internal_Shdr *sec = filedata->section_headers + i;
972
071436c6
NC
973 if (sec->sh_type == type)
974 return sec;
975 }
976
977 return NULL;
978}
979
657d0d47
CC
980/* Return a pointer to section NAME, or NULL if no such section exists,
981 restricted to the list of sections given in SET. */
982
983static Elf_Internal_Shdr *
dda8d76d 984find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
657d0d47
CC
985{
986 unsigned int i;
987
68807c3c
NC
988 if (filedata->section_headers == NULL)
989 return NULL;
990
657d0d47
CC
991 if (set != NULL)
992 {
993 while ((i = *set++) > 0)
b814a36d
NC
994 {
995 /* See PR 21156 for a reproducer. */
dda8d76d 996 if (i >= filedata->file_header.e_shnum)
b814a36d
NC
997 continue; /* FIXME: Should we issue an error message ? */
998
84714f86
AM
999 if (section_name_valid (filedata, filedata->section_headers + i)
1000 && streq (section_name (filedata, filedata->section_headers + i),
1001 name))
dda8d76d 1002 return filedata->section_headers + i;
b814a36d 1003 }
657d0d47
CC
1004 }
1005
dda8d76d 1006 return find_section (filedata, name);
657d0d47
CC
1007}
1008
32ec8896 1009/* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
28f997cf
TG
1010 This OS has so many departures from the ELF standard that we test it at
1011 many places. */
1012
015dc7e1 1013static inline bool
dda8d76d 1014is_ia64_vms (Filedata * filedata)
28f997cf 1015{
dda8d76d
NC
1016 return filedata->file_header.e_machine == EM_IA_64
1017 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
28f997cf
TG
1018}
1019
bcedfee6 1020/* Guess the relocation size commonly used by the specific machines. */
252b5132 1021
015dc7e1 1022static bool
2dc4cec1 1023guess_is_rela (unsigned int e_machine)
252b5132 1024{
9c19a809 1025 switch (e_machine)
252b5132
RH
1026 {
1027 /* Targets that use REL relocations. */
252b5132 1028 case EM_386:
22abe556 1029 case EM_IAMCU:
f954747f 1030 case EM_960:
e9f53129 1031 case EM_ARM:
2b0337b0 1032 case EM_D10V:
252b5132 1033 case EM_CYGNUS_D10V:
e9f53129 1034 case EM_DLX:
252b5132 1035 case EM_MIPS:
4fe85591 1036 case EM_MIPS_RS3_LE:
e9f53129 1037 case EM_CYGNUS_M32R:
1c0d3aa6 1038 case EM_SCORE:
f6c1a2d5 1039 case EM_XGATE:
fe944acf 1040 case EM_NFP:
aca4efc7 1041 case EM_BPF:
015dc7e1 1042 return false;
103f02d3 1043
252b5132
RH
1044 /* Targets that use RELA relocations. */
1045 case EM_68K:
f954747f 1046 case EM_860:
a06ea964 1047 case EM_AARCH64:
cfb8c092 1048 case EM_ADAPTEVA_EPIPHANY:
e9f53129
AM
1049 case EM_ALPHA:
1050 case EM_ALTERA_NIOS2:
886a2506
NC
1051 case EM_ARC:
1052 case EM_ARC_COMPACT:
1053 case EM_ARC_COMPACT2:
e9f53129
AM
1054 case EM_AVR:
1055 case EM_AVR_OLD:
1056 case EM_BLACKFIN:
60bca95a 1057 case EM_CR16:
e9f53129
AM
1058 case EM_CRIS:
1059 case EM_CRX:
b8891f8d 1060 case EM_CSKY:
2b0337b0 1061 case EM_D30V:
252b5132 1062 case EM_CYGNUS_D30V:
2b0337b0 1063 case EM_FR30:
3f8107ab 1064 case EM_FT32:
252b5132 1065 case EM_CYGNUS_FR30:
5c70f934 1066 case EM_CYGNUS_FRV:
e9f53129
AM
1067 case EM_H8S:
1068 case EM_H8_300:
1069 case EM_H8_300H:
800eeca4 1070 case EM_IA_64:
1e4cf259
NC
1071 case EM_IP2K:
1072 case EM_IP2K_OLD:
3b36097d 1073 case EM_IQ2000:
84e94c90 1074 case EM_LATTICEMICO32:
ff7eeb89 1075 case EM_M32C_OLD:
49f58d10 1076 case EM_M32C:
e9f53129
AM
1077 case EM_M32R:
1078 case EM_MCORE:
15ab5209 1079 case EM_CYGNUS_MEP:
a3c62988 1080 case EM_METAG:
e9f53129
AM
1081 case EM_MMIX:
1082 case EM_MN10200:
1083 case EM_CYGNUS_MN10200:
1084 case EM_MN10300:
1085 case EM_CYGNUS_MN10300:
5506d11a 1086 case EM_MOXIE:
e9f53129
AM
1087 case EM_MSP430:
1088 case EM_MSP430_OLD:
d031aafb 1089 case EM_MT:
35c08157 1090 case EM_NDS32:
64fd6348 1091 case EM_NIOS32:
73589c9d 1092 case EM_OR1K:
e9f53129
AM
1093 case EM_PPC64:
1094 case EM_PPC:
2b100bb5 1095 case EM_TI_PRU:
e23eba97 1096 case EM_RISCV:
99c513f6 1097 case EM_RL78:
c7927a3c 1098 case EM_RX:
e9f53129
AM
1099 case EM_S390:
1100 case EM_S390_OLD:
1101 case EM_SH:
1102 case EM_SPARC:
1103 case EM_SPARC32PLUS:
1104 case EM_SPARCV9:
1105 case EM_SPU:
40b36596 1106 case EM_TI_C6000:
aa137e4d
NC
1107 case EM_TILEGX:
1108 case EM_TILEPRO:
708e2187 1109 case EM_V800:
e9f53129
AM
1110 case EM_V850:
1111 case EM_CYGNUS_V850:
1112 case EM_VAX:
619ed720 1113 case EM_VISIUM:
e9f53129 1114 case EM_X86_64:
8a9036a4 1115 case EM_L1OM:
7a9068fe 1116 case EM_K1OM:
e9f53129
AM
1117 case EM_XSTORMY16:
1118 case EM_XTENSA:
1119 case EM_XTENSA_OLD:
7ba29e2a
NC
1120 case EM_MICROBLAZE:
1121 case EM_MICROBLAZE_OLD:
f96bd6c2 1122 case EM_WEBASSEMBLY:
015dc7e1 1123 return true;
103f02d3 1124
e9f53129
AM
1125 case EM_68HC05:
1126 case EM_68HC08:
1127 case EM_68HC11:
1128 case EM_68HC16:
1129 case EM_FX66:
1130 case EM_ME16:
d1133906 1131 case EM_MMA:
d1133906
NC
1132 case EM_NCPU:
1133 case EM_NDR1:
e9f53129 1134 case EM_PCP:
d1133906 1135 case EM_ST100:
e9f53129 1136 case EM_ST19:
d1133906 1137 case EM_ST7:
e9f53129
AM
1138 case EM_ST9PLUS:
1139 case EM_STARCORE:
d1133906 1140 case EM_SVX:
e9f53129 1141 case EM_TINYJ:
9c19a809
NC
1142 default:
1143 warn (_("Don't know about relocations on this machine architecture\n"));
015dc7e1 1144 return false;
9c19a809
NC
1145 }
1146}
252b5132 1147
dda8d76d 1148/* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
32ec8896
NC
1149 Returns TRUE upon success, FALSE otherwise. If successful then a
1150 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
1151 and the number of relocs loaded is placed in *NRELASP. It is the caller's
1152 responsibility to free the allocated buffer. */
1153
015dc7e1 1154static bool
dda8d76d
NC
1155slurp_rela_relocs (Filedata * filedata,
1156 unsigned long rel_offset,
1157 unsigned long rel_size,
1158 Elf_Internal_Rela ** relasp,
1159 unsigned long * nrelasp)
9c19a809 1160{
2cf0635d 1161 Elf_Internal_Rela * relas;
8b73c356 1162 size_t nrelas;
4d6ed7c8 1163 unsigned int i;
252b5132 1164
4d6ed7c8
NC
1165 if (is_32bit_elf)
1166 {
2cf0635d 1167 Elf32_External_Rela * erelas;
103f02d3 1168
dda8d76d 1169 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
9cf03b7e 1170 rel_size, _("32-bit relocation data"));
a6e9f9df 1171 if (!erelas)
015dc7e1 1172 return false;
252b5132 1173
4d6ed7c8 1174 nrelas = rel_size / sizeof (Elf32_External_Rela);
103f02d3 1175
3f5e193b
NC
1176 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1177 sizeof (Elf_Internal_Rela));
103f02d3 1178
4d6ed7c8
NC
1179 if (relas == NULL)
1180 {
c256ffe7 1181 free (erelas);
591a748a 1182 error (_("out of memory parsing relocs\n"));
015dc7e1 1183 return false;
4d6ed7c8 1184 }
103f02d3 1185
4d6ed7c8
NC
1186 for (i = 0; i < nrelas; i++)
1187 {
1188 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1189 relas[i].r_info = BYTE_GET (erelas[i].r_info);
598aaa76 1190 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
4d6ed7c8 1191 }
103f02d3 1192
4d6ed7c8
NC
1193 free (erelas);
1194 }
1195 else
1196 {
2cf0635d 1197 Elf64_External_Rela * erelas;
103f02d3 1198
dda8d76d 1199 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
9cf03b7e 1200 rel_size, _("64-bit relocation data"));
a6e9f9df 1201 if (!erelas)
015dc7e1 1202 return false;
4d6ed7c8
NC
1203
1204 nrelas = rel_size / sizeof (Elf64_External_Rela);
103f02d3 1205
3f5e193b
NC
1206 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1207 sizeof (Elf_Internal_Rela));
103f02d3 1208
4d6ed7c8
NC
1209 if (relas == NULL)
1210 {
c256ffe7 1211 free (erelas);
591a748a 1212 error (_("out of memory parsing relocs\n"));
015dc7e1 1213 return false;
9c19a809 1214 }
4d6ed7c8
NC
1215
1216 for (i = 0; i < nrelas; i++)
9c19a809 1217 {
66543521
AM
1218 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1219 relas[i].r_info = BYTE_GET (erelas[i].r_info);
598aaa76 1220 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
861fb55a
DJ
1221
1222 /* The #ifdef BFD64 below is to prevent a compile time
1223 warning. We know that if we do not have a 64 bit data
1224 type that we will never execute this code anyway. */
1225#ifdef BFD64
dda8d76d
NC
1226 if (filedata->file_header.e_machine == EM_MIPS
1227 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
861fb55a
DJ
1228 {
1229 /* In little-endian objects, r_info isn't really a
1230 64-bit little-endian value: it has a 32-bit
1231 little-endian symbol index followed by four
1232 individual byte fields. Reorder INFO
1233 accordingly. */
91d6fa6a
NC
1234 bfd_vma inf = relas[i].r_info;
1235 inf = (((inf & 0xffffffff) << 32)
1236 | ((inf >> 56) & 0xff)
1237 | ((inf >> 40) & 0xff00)
1238 | ((inf >> 24) & 0xff0000)
1239 | ((inf >> 8) & 0xff000000));
1240 relas[i].r_info = inf;
861fb55a
DJ
1241 }
1242#endif /* BFD64 */
4d6ed7c8 1243 }
103f02d3 1244
4d6ed7c8
NC
1245 free (erelas);
1246 }
32ec8896 1247
4d6ed7c8
NC
1248 *relasp = relas;
1249 *nrelasp = nrelas;
015dc7e1 1250 return true;
4d6ed7c8 1251}
103f02d3 1252
dda8d76d 1253/* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
32ec8896
NC
1254 Returns TRUE upon success, FALSE otherwise. If successful then a
1255 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1256 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1257 responsibility to free the allocated buffer. */
1258
015dc7e1 1259static bool
dda8d76d
NC
1260slurp_rel_relocs (Filedata * filedata,
1261 unsigned long rel_offset,
1262 unsigned long rel_size,
1263 Elf_Internal_Rela ** relsp,
1264 unsigned long * nrelsp)
4d6ed7c8 1265{
2cf0635d 1266 Elf_Internal_Rela * rels;
8b73c356 1267 size_t nrels;
4d6ed7c8 1268 unsigned int i;
103f02d3 1269
4d6ed7c8
NC
1270 if (is_32bit_elf)
1271 {
2cf0635d 1272 Elf32_External_Rel * erels;
103f02d3 1273
dda8d76d 1274 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
9cf03b7e 1275 rel_size, _("32-bit relocation data"));
a6e9f9df 1276 if (!erels)
015dc7e1 1277 return false;
103f02d3 1278
4d6ed7c8 1279 nrels = rel_size / sizeof (Elf32_External_Rel);
103f02d3 1280
3f5e193b 1281 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
103f02d3 1282
4d6ed7c8
NC
1283 if (rels == NULL)
1284 {
c256ffe7 1285 free (erels);
591a748a 1286 error (_("out of memory parsing relocs\n"));
015dc7e1 1287 return false;
4d6ed7c8
NC
1288 }
1289
1290 for (i = 0; i < nrels; i++)
1291 {
1292 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1293 rels[i].r_info = BYTE_GET (erels[i].r_info);
c8286bd1 1294 rels[i].r_addend = 0;
9ea033b2 1295 }
4d6ed7c8
NC
1296
1297 free (erels);
9c19a809
NC
1298 }
1299 else
1300 {
2cf0635d 1301 Elf64_External_Rel * erels;
9ea033b2 1302
dda8d76d 1303 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
9cf03b7e 1304 rel_size, _("64-bit relocation data"));
a6e9f9df 1305 if (!erels)
015dc7e1 1306 return false;
103f02d3 1307
4d6ed7c8 1308 nrels = rel_size / sizeof (Elf64_External_Rel);
103f02d3 1309
3f5e193b 1310 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
103f02d3 1311
4d6ed7c8 1312 if (rels == NULL)
9c19a809 1313 {
c256ffe7 1314 free (erels);
591a748a 1315 error (_("out of memory parsing relocs\n"));
015dc7e1 1316 return false;
4d6ed7c8 1317 }
103f02d3 1318
4d6ed7c8
NC
1319 for (i = 0; i < nrels; i++)
1320 {
66543521
AM
1321 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1322 rels[i].r_info = BYTE_GET (erels[i].r_info);
c8286bd1 1323 rels[i].r_addend = 0;
861fb55a
DJ
1324
1325 /* The #ifdef BFD64 below is to prevent a compile time
1326 warning. We know that if we do not have a 64 bit data
1327 type that we will never execute this code anyway. */
1328#ifdef BFD64
dda8d76d
NC
1329 if (filedata->file_header.e_machine == EM_MIPS
1330 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
861fb55a
DJ
1331 {
1332 /* In little-endian objects, r_info isn't really a
1333 64-bit little-endian value: it has a 32-bit
1334 little-endian symbol index followed by four
1335 individual byte fields. Reorder INFO
1336 accordingly. */
91d6fa6a
NC
1337 bfd_vma inf = rels[i].r_info;
1338 inf = (((inf & 0xffffffff) << 32)
1339 | ((inf >> 56) & 0xff)
1340 | ((inf >> 40) & 0xff00)
1341 | ((inf >> 24) & 0xff0000)
1342 | ((inf >> 8) & 0xff000000));
1343 rels[i].r_info = inf;
861fb55a
DJ
1344 }
1345#endif /* BFD64 */
4d6ed7c8 1346 }
103f02d3 1347
4d6ed7c8
NC
1348 free (erels);
1349 }
32ec8896 1350
4d6ed7c8
NC
1351 *relsp = rels;
1352 *nrelsp = nrels;
015dc7e1 1353 return true;
4d6ed7c8 1354}
103f02d3 1355
aca88567
NC
1356/* Returns the reloc type extracted from the reloc info field. */
1357
1358static unsigned int
dda8d76d 1359get_reloc_type (Filedata * filedata, bfd_vma reloc_info)
aca88567
NC
1360{
1361 if (is_32bit_elf)
1362 return ELF32_R_TYPE (reloc_info);
1363
dda8d76d 1364 switch (filedata->file_header.e_machine)
aca88567
NC
1365 {
1366 case EM_MIPS:
1367 /* Note: We assume that reloc_info has already been adjusted for us. */
1368 return ELF64_MIPS_R_TYPE (reloc_info);
1369
1370 case EM_SPARCV9:
1371 return ELF64_R_TYPE_ID (reloc_info);
1372
1373 default:
1374 return ELF64_R_TYPE (reloc_info);
1375 }
1376}
1377
1378/* Return the symbol index extracted from the reloc info field. */
1379
1380static bfd_vma
1381get_reloc_symindex (bfd_vma reloc_info)
1382{
1383 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1384}
1385
015dc7e1 1386static inline bool
dda8d76d 1387uses_msp430x_relocs (Filedata * filedata)
13761a11
NC
1388{
1389 return
dda8d76d 1390 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
13761a11 1391 /* GCC uses osabi == ELFOSBI_STANDALONE. */
dda8d76d 1392 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
13761a11 1393 /* TI compiler uses ELFOSABI_NONE. */
dda8d76d 1394 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
13761a11
NC
1395}
1396
d3ba0551
AM
1397/* Display the contents of the relocation data found at the specified
1398 offset. */
ee42cf8c 1399
015dc7e1 1400static bool
dda8d76d
NC
1401dump_relocations (Filedata * filedata,
1402 unsigned long rel_offset,
1403 unsigned long rel_size,
1404 Elf_Internal_Sym * symtab,
1405 unsigned long nsyms,
1406 char * strtab,
1407 unsigned long strtablen,
1408 int is_rela,
015dc7e1 1409 bool is_dynsym)
4d6ed7c8 1410{
32ec8896 1411 unsigned long i;
2cf0635d 1412 Elf_Internal_Rela * rels;
015dc7e1 1413 bool res = true;
103f02d3 1414
4d6ed7c8 1415 if (is_rela == UNKNOWN)
dda8d76d 1416 is_rela = guess_is_rela (filedata->file_header.e_machine);
103f02d3 1417
4d6ed7c8
NC
1418 if (is_rela)
1419 {
dda8d76d 1420 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
015dc7e1 1421 return false;
4d6ed7c8
NC
1422 }
1423 else
1424 {
dda8d76d 1425 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
015dc7e1 1426 return false;
252b5132
RH
1427 }
1428
410f7a12
L
1429 if (is_32bit_elf)
1430 {
1431 if (is_rela)
2c71103e
NC
1432 {
1433 if (do_wide)
1434 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1435 else
1436 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1437 }
410f7a12 1438 else
2c71103e
NC
1439 {
1440 if (do_wide)
1441 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1442 else
1443 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1444 }
410f7a12 1445 }
252b5132 1446 else
410f7a12
L
1447 {
1448 if (is_rela)
2c71103e
NC
1449 {
1450 if (do_wide)
8beeaeb7 1451 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
2c71103e
NC
1452 else
1453 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1454 }
410f7a12 1455 else
2c71103e
NC
1456 {
1457 if (do_wide)
8beeaeb7 1458 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
2c71103e
NC
1459 else
1460 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1461 }
410f7a12 1462 }
252b5132
RH
1463
1464 for (i = 0; i < rel_size; i++)
1465 {
2cf0635d 1466 const char * rtype;
b34976b6 1467 bfd_vma offset;
91d6fa6a 1468 bfd_vma inf;
b34976b6
AM
1469 bfd_vma symtab_index;
1470 bfd_vma type;
103f02d3 1471
b34976b6 1472 offset = rels[i].r_offset;
91d6fa6a 1473 inf = rels[i].r_info;
103f02d3 1474
dda8d76d 1475 type = get_reloc_type (filedata, inf);
91d6fa6a 1476 symtab_index = get_reloc_symindex (inf);
252b5132 1477
410f7a12
L
1478 if (is_32bit_elf)
1479 {
39dbeff8
AM
1480 printf ("%8.8lx %8.8lx ",
1481 (unsigned long) offset & 0xffffffff,
91d6fa6a 1482 (unsigned long) inf & 0xffffffff);
410f7a12
L
1483 }
1484 else
1485 {
39dbeff8 1486 printf (do_wide
d1ce973e
AM
1487 ? "%16.16" BFD_VMA_FMT "x %16.16" BFD_VMA_FMT "x "
1488 : "%12.12" BFD_VMA_FMT "x %12.12" BFD_VMA_FMT "x ",
91d6fa6a 1489 offset, inf);
410f7a12 1490 }
103f02d3 1491
dda8d76d 1492 switch (filedata->file_header.e_machine)
252b5132
RH
1493 {
1494 default:
1495 rtype = NULL;
1496 break;
1497
a06ea964
NC
1498 case EM_AARCH64:
1499 rtype = elf_aarch64_reloc_type (type);
1500 break;
1501
2b0337b0 1502 case EM_M32R:
252b5132 1503 case EM_CYGNUS_M32R:
9ea033b2 1504 rtype = elf_m32r_reloc_type (type);
252b5132
RH
1505 break;
1506
1507 case EM_386:
22abe556 1508 case EM_IAMCU:
9ea033b2 1509 rtype = elf_i386_reloc_type (type);
252b5132
RH
1510 break;
1511
ba2685cc
AM
1512 case EM_68HC11:
1513 case EM_68HC12:
1514 rtype = elf_m68hc11_reloc_type (type);
1515 break;
75751cd9 1516
7b4ae824
JD
1517 case EM_S12Z:
1518 rtype = elf_s12z_reloc_type (type);
1519 break;
1520
252b5132 1521 case EM_68K:
9ea033b2 1522 rtype = elf_m68k_reloc_type (type);
252b5132
RH
1523 break;
1524
f954747f
AM
1525 case EM_960:
1526 rtype = elf_i960_reloc_type (type);
1527 break;
1528
adde6300 1529 case EM_AVR:
2b0337b0 1530 case EM_AVR_OLD:
adde6300
AM
1531 rtype = elf_avr_reloc_type (type);
1532 break;
1533
9ea033b2
NC
1534 case EM_OLD_SPARCV9:
1535 case EM_SPARC32PLUS:
1536 case EM_SPARCV9:
252b5132 1537 case EM_SPARC:
9ea033b2 1538 rtype = elf_sparc_reloc_type (type);
252b5132
RH
1539 break;
1540
e9f53129
AM
1541 case EM_SPU:
1542 rtype = elf_spu_reloc_type (type);
1543 break;
1544
708e2187
NC
1545 case EM_V800:
1546 rtype = v800_reloc_type (type);
1547 break;
2b0337b0 1548 case EM_V850:
252b5132 1549 case EM_CYGNUS_V850:
9ea033b2 1550 rtype = v850_reloc_type (type);
252b5132
RH
1551 break;
1552
2b0337b0 1553 case EM_D10V:
252b5132 1554 case EM_CYGNUS_D10V:
9ea033b2 1555 rtype = elf_d10v_reloc_type (type);
252b5132
RH
1556 break;
1557
2b0337b0 1558 case EM_D30V:
252b5132 1559 case EM_CYGNUS_D30V:
9ea033b2 1560 rtype = elf_d30v_reloc_type (type);
252b5132
RH
1561 break;
1562
d172d4ba
NC
1563 case EM_DLX:
1564 rtype = elf_dlx_reloc_type (type);
1565 break;
1566
252b5132 1567 case EM_SH:
9ea033b2 1568 rtype = elf_sh_reloc_type (type);
252b5132
RH
1569 break;
1570
2b0337b0 1571 case EM_MN10300:
252b5132 1572 case EM_CYGNUS_MN10300:
9ea033b2 1573 rtype = elf_mn10300_reloc_type (type);
252b5132
RH
1574 break;
1575
2b0337b0 1576 case EM_MN10200:
252b5132 1577 case EM_CYGNUS_MN10200:
9ea033b2 1578 rtype = elf_mn10200_reloc_type (type);
252b5132
RH
1579 break;
1580
2b0337b0 1581 case EM_FR30:
252b5132 1582 case EM_CYGNUS_FR30:
9ea033b2 1583 rtype = elf_fr30_reloc_type (type);
252b5132
RH
1584 break;
1585
ba2685cc
AM
1586 case EM_CYGNUS_FRV:
1587 rtype = elf_frv_reloc_type (type);
1588 break;
5c70f934 1589
b8891f8d
AJ
1590 case EM_CSKY:
1591 rtype = elf_csky_reloc_type (type);
1592 break;
1593
3f8107ab
AM
1594 case EM_FT32:
1595 rtype = elf_ft32_reloc_type (type);
1596 break;
1597
252b5132 1598 case EM_MCORE:
9ea033b2 1599 rtype = elf_mcore_reloc_type (type);
252b5132
RH
1600 break;
1601
3c3bdf30
NC
1602 case EM_MMIX:
1603 rtype = elf_mmix_reloc_type (type);
1604 break;
1605
5506d11a
AM
1606 case EM_MOXIE:
1607 rtype = elf_moxie_reloc_type (type);
1608 break;
1609
2469cfa2 1610 case EM_MSP430:
dda8d76d 1611 if (uses_msp430x_relocs (filedata))
13761a11
NC
1612 {
1613 rtype = elf_msp430x_reloc_type (type);
1614 break;
1615 }
1a0670f3 1616 /* Fall through. */
2469cfa2
NC
1617 case EM_MSP430_OLD:
1618 rtype = elf_msp430_reloc_type (type);
1619 break;
1620
35c08157
KLC
1621 case EM_NDS32:
1622 rtype = elf_nds32_reloc_type (type);
1623 break;
1624
252b5132 1625 case EM_PPC:
9ea033b2 1626 rtype = elf_ppc_reloc_type (type);
252b5132
RH
1627 break;
1628
c833c019
AM
1629 case EM_PPC64:
1630 rtype = elf_ppc64_reloc_type (type);
1631 break;
1632
252b5132 1633 case EM_MIPS:
4fe85591 1634 case EM_MIPS_RS3_LE:
9ea033b2 1635 rtype = elf_mips_reloc_type (type);
252b5132
RH
1636 break;
1637
e23eba97
NC
1638 case EM_RISCV:
1639 rtype = elf_riscv_reloc_type (type);
1640 break;
1641
252b5132 1642 case EM_ALPHA:
9ea033b2 1643 rtype = elf_alpha_reloc_type (type);
252b5132
RH
1644 break;
1645
1646 case EM_ARM:
9ea033b2 1647 rtype = elf_arm_reloc_type (type);
252b5132
RH
1648 break;
1649
584da044 1650 case EM_ARC:
886a2506
NC
1651 case EM_ARC_COMPACT:
1652 case EM_ARC_COMPACT2:
9ea033b2 1653 rtype = elf_arc_reloc_type (type);
252b5132
RH
1654 break;
1655
1656 case EM_PARISC:
69e617ca 1657 rtype = elf_hppa_reloc_type (type);
252b5132 1658 break;
7d466069 1659
b8720f9d
JL
1660 case EM_H8_300:
1661 case EM_H8_300H:
1662 case EM_H8S:
1663 rtype = elf_h8_reloc_type (type);
1664 break;
1665
73589c9d
CS
1666 case EM_OR1K:
1667 rtype = elf_or1k_reloc_type (type);
3b16e843
NC
1668 break;
1669
7d466069 1670 case EM_PJ:
2b0337b0 1671 case EM_PJ_OLD:
7d466069
ILT
1672 rtype = elf_pj_reloc_type (type);
1673 break;
800eeca4
JW
1674 case EM_IA_64:
1675 rtype = elf_ia64_reloc_type (type);
1676 break;
1b61cf92
HPN
1677
1678 case EM_CRIS:
1679 rtype = elf_cris_reloc_type (type);
1680 break;
535c37ff 1681
f954747f
AM
1682 case EM_860:
1683 rtype = elf_i860_reloc_type (type);
1684 break;
1685
bcedfee6 1686 case EM_X86_64:
8a9036a4 1687 case EM_L1OM:
7a9068fe 1688 case EM_K1OM:
bcedfee6
NC
1689 rtype = elf_x86_64_reloc_type (type);
1690 break;
a85d7ed0 1691
f954747f
AM
1692 case EM_S370:
1693 rtype = i370_reloc_type (type);
1694 break;
1695
53c7db4b
KH
1696 case EM_S390_OLD:
1697 case EM_S390:
1698 rtype = elf_s390_reloc_type (type);
1699 break;
93fbbb04 1700
1c0d3aa6
NC
1701 case EM_SCORE:
1702 rtype = elf_score_reloc_type (type);
1703 break;
1704
93fbbb04
GK
1705 case EM_XSTORMY16:
1706 rtype = elf_xstormy16_reloc_type (type);
1707 break;
179d3252 1708
1fe1f39c
NC
1709 case EM_CRX:
1710 rtype = elf_crx_reloc_type (type);
1711 break;
1712
179d3252
JT
1713 case EM_VAX:
1714 rtype = elf_vax_reloc_type (type);
1715 break;
1e4cf259 1716
619ed720
EB
1717 case EM_VISIUM:
1718 rtype = elf_visium_reloc_type (type);
1719 break;
1720
aca4efc7
JM
1721 case EM_BPF:
1722 rtype = elf_bpf_reloc_type (type);
1723 break;
1724
cfb8c092
NC
1725 case EM_ADAPTEVA_EPIPHANY:
1726 rtype = elf_epiphany_reloc_type (type);
1727 break;
1728
1e4cf259
NC
1729 case EM_IP2K:
1730 case EM_IP2K_OLD:
1731 rtype = elf_ip2k_reloc_type (type);
1732 break;
3b36097d
SC
1733
1734 case EM_IQ2000:
1735 rtype = elf_iq2000_reloc_type (type);
1736 break;
88da6820
NC
1737
1738 case EM_XTENSA_OLD:
1739 case EM_XTENSA:
1740 rtype = elf_xtensa_reloc_type (type);
1741 break;
a34e3ecb 1742
84e94c90
NC
1743 case EM_LATTICEMICO32:
1744 rtype = elf_lm32_reloc_type (type);
1745 break;
1746
ff7eeb89 1747 case EM_M32C_OLD:
49f58d10
JB
1748 case EM_M32C:
1749 rtype = elf_m32c_reloc_type (type);
1750 break;
1751
d031aafb
NS
1752 case EM_MT:
1753 rtype = elf_mt_reloc_type (type);
a34e3ecb 1754 break;
1d65ded4
CM
1755
1756 case EM_BLACKFIN:
1757 rtype = elf_bfin_reloc_type (type);
1758 break;
15ab5209
DB
1759
1760 case EM_CYGNUS_MEP:
1761 rtype = elf_mep_reloc_type (type);
1762 break;
60bca95a
NC
1763
1764 case EM_CR16:
1765 rtype = elf_cr16_reloc_type (type);
1766 break;
dd24e3da 1767
7ba29e2a
NC
1768 case EM_MICROBLAZE:
1769 case EM_MICROBLAZE_OLD:
1770 rtype = elf_microblaze_reloc_type (type);
1771 break;
c7927a3c 1772
99c513f6
DD
1773 case EM_RL78:
1774 rtype = elf_rl78_reloc_type (type);
1775 break;
1776
c7927a3c
NC
1777 case EM_RX:
1778 rtype = elf_rx_reloc_type (type);
1779 break;
c29aca4a 1780
a3c62988
NC
1781 case EM_METAG:
1782 rtype = elf_metag_reloc_type (type);
1783 break;
1784
c29aca4a
NC
1785 case EM_XC16X:
1786 case EM_C166:
1787 rtype = elf_xc16x_reloc_type (type);
1788 break;
40b36596
JM
1789
1790 case EM_TI_C6000:
1791 rtype = elf_tic6x_reloc_type (type);
1792 break;
aa137e4d
NC
1793
1794 case EM_TILEGX:
1795 rtype = elf_tilegx_reloc_type (type);
1796 break;
1797
1798 case EM_TILEPRO:
1799 rtype = elf_tilepro_reloc_type (type);
1800 break;
f6c1a2d5 1801
f96bd6c2
PC
1802 case EM_WEBASSEMBLY:
1803 rtype = elf_wasm32_reloc_type (type);
1804 break;
1805
f6c1a2d5
NC
1806 case EM_XGATE:
1807 rtype = elf_xgate_reloc_type (type);
1808 break;
36591ba1
SL
1809
1810 case EM_ALTERA_NIOS2:
1811 rtype = elf_nios2_reloc_type (type);
1812 break;
2b100bb5
DD
1813
1814 case EM_TI_PRU:
1815 rtype = elf_pru_reloc_type (type);
1816 break;
fe944acf
FT
1817
1818 case EM_NFP:
1819 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1820 rtype = elf_nfp3200_reloc_type (type);
1821 else
1822 rtype = elf_nfp_reloc_type (type);
1823 break;
6655dba2
SB
1824
1825 case EM_Z80:
1826 rtype = elf_z80_reloc_type (type);
1827 break;
e9a0721f 1828
1829 case EM_LOONGARCH:
1830 rtype = elf_loongarch_reloc_type (type);
1831 break;
1832
252b5132
RH
1833 }
1834
1835 if (rtype == NULL)
39dbeff8 1836 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
252b5132 1837 else
5c144731 1838 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
252b5132 1839
dda8d76d 1840 if (filedata->file_header.e_machine == EM_ALPHA
157c2599 1841 && rtype != NULL
7ace3541
RH
1842 && streq (rtype, "R_ALPHA_LITUSE")
1843 && is_rela)
1844 {
1845 switch (rels[i].r_addend)
1846 {
1847 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1848 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1849 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1850 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1851 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1852 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1853 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1854 default: rtype = NULL;
1855 }
32ec8896 1856
7ace3541
RH
1857 if (rtype)
1858 printf (" (%s)", rtype);
1859 else
1860 {
1861 putchar (' ');
1862 printf (_("<unknown addend: %lx>"),
1863 (unsigned long) rels[i].r_addend);
015dc7e1 1864 res = false;
7ace3541
RH
1865 }
1866 }
1867 else if (symtab_index)
252b5132 1868 {
af3fc3bc 1869 if (symtab == NULL || symtab_index >= nsyms)
32ec8896 1870 {
27a45f42
AS
1871 error (_(" bad symbol index: %08lx in reloc\n"),
1872 (unsigned long) symtab_index);
015dc7e1 1873 res = false;
32ec8896 1874 }
af3fc3bc 1875 else
19936277 1876 {
2cf0635d 1877 Elf_Internal_Sym * psym;
bb4d2ac2
L
1878 const char * version_string;
1879 enum versioned_symbol_info sym_info;
1880 unsigned short vna_other;
19936277 1881
af3fc3bc 1882 psym = symtab + symtab_index;
103f02d3 1883
bb4d2ac2 1884 version_string
dda8d76d 1885 = get_symbol_version_string (filedata, is_dynsym,
bb4d2ac2
L
1886 strtab, strtablen,
1887 symtab_index,
1888 psym,
1889 &sym_info,
1890 &vna_other);
1891
af3fc3bc 1892 printf (" ");
171191ba 1893
d8045f23
NC
1894 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1895 {
1896 const char * name;
1897 unsigned int len;
1898 unsigned int width = is_32bit_elf ? 8 : 14;
1899
1900 /* Relocations against GNU_IFUNC symbols do not use the value
1901 of the symbol as the address to relocate against. Instead
1902 they invoke the function named by the symbol and use its
1903 result as the address for relocation.
1904
1905 To indicate this to the user, do not display the value of
1906 the symbol in the "Symbols's Value" field. Instead show
1907 its name followed by () as a hint that the symbol is
1908 invoked. */
1909
1910 if (strtab == NULL
1911 || psym->st_name == 0
1912 || psym->st_name >= strtablen)
1913 name = "??";
1914 else
1915 name = strtab + psym->st_name;
1916
1917 len = print_symbol (width, name);
bb4d2ac2
L
1918 if (version_string)
1919 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1920 version_string);
d8045f23
NC
1921 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1922 }
1923 else
1924 {
1925 print_vma (psym->st_value, LONG_HEX);
171191ba 1926
d8045f23
NC
1927 printf (is_32bit_elf ? " " : " ");
1928 }
103f02d3 1929
af3fc3bc 1930 if (psym->st_name == 0)
f1ef08cb 1931 {
2cf0635d 1932 const char * sec_name = "<null>";
f1ef08cb
AM
1933 char name_buf[40];
1934
1935 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1936 {
b9af6379
AM
1937 if (psym->st_shndx < filedata->file_header.e_shnum
1938 && filedata->section_headers != NULL)
84714f86
AM
1939 sec_name = section_name_print (filedata,
1940 filedata->section_headers
b9e920ec 1941 + psym->st_shndx);
f1ef08cb
AM
1942 else if (psym->st_shndx == SHN_ABS)
1943 sec_name = "ABS";
1944 else if (psym->st_shndx == SHN_COMMON)
1945 sec_name = "COMMON";
dda8d76d 1946 else if ((filedata->file_header.e_machine == EM_MIPS
ac145307 1947 && psym->st_shndx == SHN_MIPS_SCOMMON)
dda8d76d 1948 || (filedata->file_header.e_machine == EM_TI_C6000
ac145307 1949 && psym->st_shndx == SHN_TIC6X_SCOMMON))
172553c7 1950 sec_name = "SCOMMON";
dda8d76d 1951 else if (filedata->file_header.e_machine == EM_MIPS
172553c7
TS
1952 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1953 sec_name = "SUNDEF";
dda8d76d
NC
1954 else if ((filedata->file_header.e_machine == EM_X86_64
1955 || filedata->file_header.e_machine == EM_L1OM
1956 || filedata->file_header.e_machine == EM_K1OM)
3b22753a
L
1957 && psym->st_shndx == SHN_X86_64_LCOMMON)
1958 sec_name = "LARGE_COMMON";
dda8d76d
NC
1959 else if (filedata->file_header.e_machine == EM_IA_64
1960 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
9ce701e2
L
1961 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1962 sec_name = "ANSI_COM";
dda8d76d 1963 else if (is_ia64_vms (filedata)
148b93f2
NC
1964 && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1965 sec_name = "VMS_SYMVEC";
f1ef08cb
AM
1966 else
1967 {
1968 sprintf (name_buf, "<section 0x%x>",
1969 (unsigned int) psym->st_shndx);
1970 sec_name = name_buf;
1971 }
1972 }
1973 print_symbol (22, sec_name);
1974 }
af3fc3bc 1975 else if (strtab == NULL)
d79b3d50 1976 printf (_("<string table index: %3ld>"), psym->st_name);
c256ffe7 1977 else if (psym->st_name >= strtablen)
32ec8896 1978 {
27a45f42
AS
1979 error (_("<corrupt string table index: %3ld>\n"),
1980 psym->st_name);
015dc7e1 1981 res = false;
32ec8896 1982 }
af3fc3bc 1983 else
bb4d2ac2
L
1984 {
1985 print_symbol (22, strtab + psym->st_name);
1986 if (version_string)
1987 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1988 version_string);
1989 }
103f02d3 1990
af3fc3bc 1991 if (is_rela)
171191ba 1992 {
7360e63f 1993 bfd_vma off = rels[i].r_addend;
171191ba 1994
7360e63f 1995 if ((bfd_signed_vma) off < 0)
598aaa76 1996 printf (" - %" BFD_VMA_FMT "x", - off);
171191ba 1997 else
598aaa76 1998 printf (" + %" BFD_VMA_FMT "x", off);
171191ba 1999 }
19936277 2000 }
252b5132 2001 }
1b228002 2002 else if (is_rela)
f7a99963 2003 {
7360e63f 2004 bfd_vma off = rels[i].r_addend;
e04d7088
L
2005
2006 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
7360e63f 2007 if ((bfd_signed_vma) off < 0)
e04d7088
L
2008 printf ("-%" BFD_VMA_FMT "x", - off);
2009 else
2010 printf ("%" BFD_VMA_FMT "x", off);
f7a99963 2011 }
252b5132 2012
dda8d76d 2013 if (filedata->file_header.e_machine == EM_SPARCV9
157c2599
NC
2014 && rtype != NULL
2015 && streq (rtype, "R_SPARC_OLO10"))
91d6fa6a 2016 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
351b4b40 2017
252b5132 2018 putchar ('\n');
2c71103e 2019
aca88567 2020#ifdef BFD64
dda8d76d 2021 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
2c71103e 2022 {
91d6fa6a
NC
2023 bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
2024 bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
2cf0635d
NC
2025 const char * rtype2 = elf_mips_reloc_type (type2);
2026 const char * rtype3 = elf_mips_reloc_type (type3);
aca88567 2027
2c71103e
NC
2028 printf (" Type2: ");
2029
2030 if (rtype2 == NULL)
39dbeff8
AM
2031 printf (_("unrecognized: %-7lx"),
2032 (unsigned long) type2 & 0xffffffff);
2c71103e
NC
2033 else
2034 printf ("%-17.17s", rtype2);
2035
18bd398b 2036 printf ("\n Type3: ");
2c71103e
NC
2037
2038 if (rtype3 == NULL)
39dbeff8
AM
2039 printf (_("unrecognized: %-7lx"),
2040 (unsigned long) type3 & 0xffffffff);
2c71103e
NC
2041 else
2042 printf ("%-17.17s", rtype3);
2043
53c7db4b 2044 putchar ('\n');
2c71103e 2045 }
aca88567 2046#endif /* BFD64 */
252b5132
RH
2047 }
2048
c8286bd1 2049 free (rels);
32ec8896
NC
2050
2051 return res;
252b5132
RH
2052}
2053
37c18eed
SD
2054static const char *
2055get_aarch64_dynamic_type (unsigned long type)
2056{
2057 switch (type)
2058 {
2059 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
1dbade74 2060 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
2301ed1c 2061 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
37c18eed
SD
2062 default:
2063 return NULL;
2064 }
2065}
2066
252b5132 2067static const char *
d3ba0551 2068get_mips_dynamic_type (unsigned long type)
252b5132
RH
2069{
2070 switch (type)
2071 {
2072 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
2073 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
2074 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
2075 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
2076 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
2077 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
2078 case DT_MIPS_MSYM: return "MIPS_MSYM";
2079 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2080 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2081 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
2082 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
2083 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
2084 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
2085 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
2086 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
2087 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
2088 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
a5499fa4 2089 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
252b5132
RH
2090 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
2091 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
2092 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
2093 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
2094 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
2095 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
2096 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
2097 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
2098 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
2099 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
2100 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
2101 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
2102 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2103 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
2104 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
2105 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
2106 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
2107 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2108 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
2109 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
2110 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
2111 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
2112 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
2113 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
2114 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
2115 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
861fb55a
DJ
2116 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
2117 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
f16a9783 2118 case DT_MIPS_XHASH: return "MIPS_XHASH";
252b5132
RH
2119 default:
2120 return NULL;
2121 }
2122}
2123
9a097730 2124static const char *
d3ba0551 2125get_sparc64_dynamic_type (unsigned long type)
9a097730
RH
2126{
2127 switch (type)
2128 {
2129 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
2130 default:
2131 return NULL;
2132 }
103f02d3
UD
2133}
2134
7490d522
AM
2135static const char *
2136get_ppc_dynamic_type (unsigned long type)
2137{
2138 switch (type)
2139 {
a7f2871e 2140 case DT_PPC_GOT: return "PPC_GOT";
e8910a83 2141 case DT_PPC_OPT: return "PPC_OPT";
7490d522
AM
2142 default:
2143 return NULL;
2144 }
2145}
2146
f1cb7e17 2147static const char *
d3ba0551 2148get_ppc64_dynamic_type (unsigned long type)
f1cb7e17
AM
2149{
2150 switch (type)
2151 {
a7f2871e
AM
2152 case DT_PPC64_GLINK: return "PPC64_GLINK";
2153 case DT_PPC64_OPD: return "PPC64_OPD";
2154 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
e8910a83 2155 case DT_PPC64_OPT: return "PPC64_OPT";
f1cb7e17
AM
2156 default:
2157 return NULL;
2158 }
2159}
2160
103f02d3 2161static const char *
d3ba0551 2162get_parisc_dynamic_type (unsigned long type)
103f02d3
UD
2163{
2164 switch (type)
2165 {
2166 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
2167 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
2168 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
2169 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
2170 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
2171 case DT_HP_PREINIT: return "HP_PREINIT";
2172 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
2173 case DT_HP_NEEDED: return "HP_NEEDED";
2174 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
2175 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
2176 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
2177 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
2178 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
eec8f817
DA
2179 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
2180 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
2181 case DT_HP_FILTERED: return "HP_FILTERED";
2182 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
2183 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
2184 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
2185 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
2186 case DT_PLT: return "PLT";
2187 case DT_PLT_SIZE: return "PLT_SIZE";
2188 case DT_DLT: return "DLT";
2189 case DT_DLT_SIZE: return "DLT_SIZE";
103f02d3
UD
2190 default:
2191 return NULL;
2192 }
2193}
9a097730 2194
ecc51f48 2195static const char *
d3ba0551 2196get_ia64_dynamic_type (unsigned long type)
ecc51f48
NC
2197{
2198 switch (type)
2199 {
148b93f2
NC
2200 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2201 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2202 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2203 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2204 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2205 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2206 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2207 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2208 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2209 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2210 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2211 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2212 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2213 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2214 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2215 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2216 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2217 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2218 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2219 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2220 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2221 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2222 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2223 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2224 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2225 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2226 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2227 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2228 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2229 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2230 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
ecc51f48
NC
2231 default:
2232 return NULL;
2233 }
2234}
2235
fd85a6a1
NC
2236static const char *
2237get_solaris_section_type (unsigned long type)
2238{
2239 switch (type)
2240 {
2241 case 0x6fffffee: return "SUNW_ancillary";
2242 case 0x6fffffef: return "SUNW_capchain";
2243 case 0x6ffffff0: return "SUNW_capinfo";
2244 case 0x6ffffff1: return "SUNW_symsort";
2245 case 0x6ffffff2: return "SUNW_tlssort";
2246 case 0x6ffffff3: return "SUNW_LDYNSYM";
2247 case 0x6ffffff4: return "SUNW_dof";
2248 case 0x6ffffff5: return "SUNW_cap";
2249 case 0x6ffffff6: return "SUNW_SIGNATURE";
2250 case 0x6ffffff7: return "SUNW_ANNOTATE";
2251 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2252 case 0x6ffffff9: return "SUNW_DEBUG";
2253 case 0x6ffffffa: return "SUNW_move";
2254 case 0x6ffffffb: return "SUNW_COMDAT";
2255 case 0x6ffffffc: return "SUNW_syminfo";
2256 case 0x6ffffffd: return "SUNW_verdef";
2257 case 0x6ffffffe: return "SUNW_verneed";
2258 case 0x6fffffff: return "SUNW_versym";
2259 case 0x70000000: return "SPARC_GOTDATA";
2260 default: return NULL;
2261 }
2262}
2263
fabcb361
RH
2264static const char *
2265get_alpha_dynamic_type (unsigned long type)
2266{
2267 switch (type)
2268 {
2269 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
32ec8896 2270 default: return NULL;
fabcb361
RH
2271 }
2272}
2273
1c0d3aa6
NC
2274static const char *
2275get_score_dynamic_type (unsigned long type)
2276{
2277 switch (type)
2278 {
2279 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2280 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2281 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2282 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2283 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2284 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
32ec8896 2285 default: return NULL;
1c0d3aa6
NC
2286 }
2287}
2288
40b36596
JM
2289static const char *
2290get_tic6x_dynamic_type (unsigned long type)
2291{
2292 switch (type)
2293 {
2294 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2295 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2296 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2297 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2298 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2299 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
32ec8896 2300 default: return NULL;
40b36596
JM
2301 }
2302}
1c0d3aa6 2303
36591ba1
SL
2304static const char *
2305get_nios2_dynamic_type (unsigned long type)
2306{
2307 switch (type)
2308 {
2309 case DT_NIOS2_GP: return "NIOS2_GP";
32ec8896 2310 default: return NULL;
36591ba1
SL
2311 }
2312}
2313
fd85a6a1
NC
2314static const char *
2315get_solaris_dynamic_type (unsigned long type)
2316{
2317 switch (type)
2318 {
2319 case 0x6000000d: return "SUNW_AUXILIARY";
2320 case 0x6000000e: return "SUNW_RTLDINF";
2321 case 0x6000000f: return "SUNW_FILTER";
2322 case 0x60000010: return "SUNW_CAP";
2323 case 0x60000011: return "SUNW_SYMTAB";
2324 case 0x60000012: return "SUNW_SYMSZ";
2325 case 0x60000013: return "SUNW_SORTENT";
2326 case 0x60000014: return "SUNW_SYMSORT";
2327 case 0x60000015: return "SUNW_SYMSORTSZ";
2328 case 0x60000016: return "SUNW_TLSSORT";
2329 case 0x60000017: return "SUNW_TLSSORTSZ";
2330 case 0x60000018: return "SUNW_CAPINFO";
2331 case 0x60000019: return "SUNW_STRPAD";
2332 case 0x6000001a: return "SUNW_CAPCHAIN";
2333 case 0x6000001b: return "SUNW_LDMACH";
2334 case 0x6000001d: return "SUNW_CAPCHAINENT";
2335 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2336 case 0x60000021: return "SUNW_PARENT";
2337 case 0x60000023: return "SUNW_ASLR";
2338 case 0x60000025: return "SUNW_RELAX";
2339 case 0x60000029: return "SUNW_NXHEAP";
2340 case 0x6000002b: return "SUNW_NXSTACK";
2341
2342 case 0x70000001: return "SPARC_REGISTER";
2343 case 0x7ffffffd: return "AUXILIARY";
2344 case 0x7ffffffe: return "USED";
2345 case 0x7fffffff: return "FILTER";
2346
15f205b1 2347 default: return NULL;
fd85a6a1
NC
2348 }
2349}
2350
252b5132 2351static const char *
dda8d76d 2352get_dynamic_type (Filedata * filedata, unsigned long type)
252b5132 2353{
e9e44622 2354 static char buff[64];
252b5132
RH
2355
2356 switch (type)
2357 {
2358 case DT_NULL: return "NULL";
2359 case DT_NEEDED: return "NEEDED";
2360 case DT_PLTRELSZ: return "PLTRELSZ";
2361 case DT_PLTGOT: return "PLTGOT";
2362 case DT_HASH: return "HASH";
2363 case DT_STRTAB: return "STRTAB";
2364 case DT_SYMTAB: return "SYMTAB";
2365 case DT_RELA: return "RELA";
2366 case DT_RELASZ: return "RELASZ";
2367 case DT_RELAENT: return "RELAENT";
2368 case DT_STRSZ: return "STRSZ";
2369 case DT_SYMENT: return "SYMENT";
2370 case DT_INIT: return "INIT";
2371 case DT_FINI: return "FINI";
2372 case DT_SONAME: return "SONAME";
2373 case DT_RPATH: return "RPATH";
2374 case DT_SYMBOLIC: return "SYMBOLIC";
2375 case DT_REL: return "REL";
2376 case DT_RELSZ: return "RELSZ";
2377 case DT_RELENT: return "RELENT";
dd207c13
FS
2378 case DT_RELR: return "RELR";
2379 case DT_RELRSZ: return "RELRSZ";
2380 case DT_RELRENT: return "RELRENT";
252b5132
RH
2381 case DT_PLTREL: return "PLTREL";
2382 case DT_DEBUG: return "DEBUG";
2383 case DT_TEXTREL: return "TEXTREL";
2384 case DT_JMPREL: return "JMPREL";
2385 case DT_BIND_NOW: return "BIND_NOW";
2386 case DT_INIT_ARRAY: return "INIT_ARRAY";
2387 case DT_FINI_ARRAY: return "FINI_ARRAY";
2388 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2389 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
d1133906
NC
2390 case DT_RUNPATH: return "RUNPATH";
2391 case DT_FLAGS: return "FLAGS";
2d0e6f43 2392
d1133906
NC
2393 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2394 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
6d913794 2395 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
103f02d3 2396
05107a46 2397 case DT_CHECKSUM: return "CHECKSUM";
252b5132
RH
2398 case DT_PLTPADSZ: return "PLTPADSZ";
2399 case DT_MOVEENT: return "MOVEENT";
2400 case DT_MOVESZ: return "MOVESZ";
dcefbbbd 2401 case DT_FEATURE: return "FEATURE";
252b5132
RH
2402 case DT_POSFLAG_1: return "POSFLAG_1";
2403 case DT_SYMINSZ: return "SYMINSZ";
2404 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
103f02d3 2405
252b5132 2406 case DT_ADDRRNGLO: return "ADDRRNGLO";
dcefbbbd
L
2407 case DT_CONFIG: return "CONFIG";
2408 case DT_DEPAUDIT: return "DEPAUDIT";
2409 case DT_AUDIT: return "AUDIT";
2410 case DT_PLTPAD: return "PLTPAD";
2411 case DT_MOVETAB: return "MOVETAB";
252b5132 2412 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
103f02d3 2413
252b5132 2414 case DT_VERSYM: return "VERSYM";
103f02d3 2415
67a4f2b7
AO
2416 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2417 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
252b5132
RH
2418 case DT_RELACOUNT: return "RELACOUNT";
2419 case DT_RELCOUNT: return "RELCOUNT";
2420 case DT_FLAGS_1: return "FLAGS_1";
2421 case DT_VERDEF: return "VERDEF";
2422 case DT_VERDEFNUM: return "VERDEFNUM";
2423 case DT_VERNEED: return "VERNEED";
2424 case DT_VERNEEDNUM: return "VERNEEDNUM";
103f02d3 2425
019148e4 2426 case DT_AUXILIARY: return "AUXILIARY";
252b5132
RH
2427 case DT_USED: return "USED";
2428 case DT_FILTER: return "FILTER";
103f02d3 2429
047b2264
JJ
2430 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2431 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2432 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2433 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2434 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
fdc90cb4 2435 case DT_GNU_HASH: return "GNU_HASH";
a5da3dee 2436 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
047b2264 2437
252b5132
RH
2438 default:
2439 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2440 {
2cf0635d 2441 const char * result;
103f02d3 2442
dda8d76d 2443 switch (filedata->file_header.e_machine)
252b5132 2444 {
37c18eed
SD
2445 case EM_AARCH64:
2446 result = get_aarch64_dynamic_type (type);
2447 break;
252b5132 2448 case EM_MIPS:
4fe85591 2449 case EM_MIPS_RS3_LE:
252b5132
RH
2450 result = get_mips_dynamic_type (type);
2451 break;
9a097730
RH
2452 case EM_SPARCV9:
2453 result = get_sparc64_dynamic_type (type);
2454 break;
7490d522
AM
2455 case EM_PPC:
2456 result = get_ppc_dynamic_type (type);
2457 break;
f1cb7e17
AM
2458 case EM_PPC64:
2459 result = get_ppc64_dynamic_type (type);
2460 break;
ecc51f48
NC
2461 case EM_IA_64:
2462 result = get_ia64_dynamic_type (type);
2463 break;
fabcb361
RH
2464 case EM_ALPHA:
2465 result = get_alpha_dynamic_type (type);
2466 break;
1c0d3aa6
NC
2467 case EM_SCORE:
2468 result = get_score_dynamic_type (type);
2469 break;
40b36596
JM
2470 case EM_TI_C6000:
2471 result = get_tic6x_dynamic_type (type);
2472 break;
36591ba1
SL
2473 case EM_ALTERA_NIOS2:
2474 result = get_nios2_dynamic_type (type);
2475 break;
252b5132 2476 default:
dda8d76d 2477 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
fd85a6a1
NC
2478 result = get_solaris_dynamic_type (type);
2479 else
2480 result = NULL;
252b5132
RH
2481 break;
2482 }
2483
2484 if (result != NULL)
2485 return result;
2486
e9e44622 2487 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
252b5132 2488 }
eec8f817 2489 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
dda8d76d 2490 || (filedata->file_header.e_machine == EM_PARISC
eec8f817 2491 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
103f02d3 2492 {
2cf0635d 2493 const char * result;
103f02d3 2494
dda8d76d 2495 switch (filedata->file_header.e_machine)
103f02d3
UD
2496 {
2497 case EM_PARISC:
2498 result = get_parisc_dynamic_type (type);
2499 break;
148b93f2
NC
2500 case EM_IA_64:
2501 result = get_ia64_dynamic_type (type);
2502 break;
103f02d3 2503 default:
dda8d76d 2504 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
fd85a6a1
NC
2505 result = get_solaris_dynamic_type (type);
2506 else
2507 result = NULL;
103f02d3
UD
2508 break;
2509 }
2510
2511 if (result != NULL)
2512 return result;
2513
e9e44622
JJ
2514 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2515 type);
103f02d3 2516 }
252b5132 2517 else
e9e44622 2518 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
103f02d3 2519
252b5132
RH
2520 return buff;
2521 }
2522}
2523
93df3340
AM
2524static bool get_program_headers (Filedata *);
2525static bool get_dynamic_section (Filedata *);
2526
2527static void
2528locate_dynamic_section (Filedata *filedata)
2529{
2530 unsigned long dynamic_addr = 0;
2531 bfd_size_type dynamic_size = 0;
2532
2533 if (filedata->file_header.e_phnum != 0
2534 && get_program_headers (filedata))
2535 {
2536 Elf_Internal_Phdr *segment;
2537 unsigned int i;
2538
2539 for (i = 0, segment = filedata->program_headers;
2540 i < filedata->file_header.e_phnum;
2541 i++, segment++)
2542 {
2543 if (segment->p_type == PT_DYNAMIC)
2544 {
2545 dynamic_addr = segment->p_offset;
2546 dynamic_size = segment->p_filesz;
2547
2548 if (filedata->section_headers != NULL)
2549 {
2550 Elf_Internal_Shdr *sec;
2551
2552 sec = find_section (filedata, ".dynamic");
2553 if (sec != NULL)
2554 {
2555 if (sec->sh_size == 0
2556 || sec->sh_type == SHT_NOBITS)
2557 {
2558 dynamic_addr = 0;
2559 dynamic_size = 0;
2560 }
2561 else
2562 {
2563 dynamic_addr = sec->sh_offset;
2564 dynamic_size = sec->sh_size;
2565 }
2566 }
2567 }
2568
2569 if (dynamic_addr > filedata->file_size
2570 || (dynamic_size > filedata->file_size - dynamic_addr))
2571 {
2572 dynamic_addr = 0;
2573 dynamic_size = 0;
2574 }
2575 break;
2576 }
2577 }
2578 }
2579 filedata->dynamic_addr = dynamic_addr;
2580 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
2581}
2582
2583static bool
2584is_pie (Filedata *filedata)
2585{
2586 Elf_Internal_Dyn *entry;
2587
2588 if (filedata->dynamic_size == 0)
2589 locate_dynamic_section (filedata);
2590 if (filedata->dynamic_size <= 1)
2591 return false;
2592
2593 if (!get_dynamic_section (filedata))
2594 return false;
2595
2596 for (entry = filedata->dynamic_section;
2597 entry < filedata->dynamic_section + filedata->dynamic_nent;
2598 entry++)
2599 {
2600 if (entry->d_tag == DT_FLAGS_1)
2601 {
2602 if ((entry->d_un.d_val & DF_1_PIE) != 0)
2603 return true;
2604 break;
2605 }
2606 }
2607 return false;
2608}
2609
252b5132 2610static char *
93df3340 2611get_file_type (Filedata *filedata)
252b5132 2612{
93df3340 2613 unsigned e_type = filedata->file_header.e_type;
89246a0e 2614 static char buff[64];
252b5132
RH
2615
2616 switch (e_type)
2617 {
32ec8896
NC
2618 case ET_NONE: return _("NONE (None)");
2619 case ET_REL: return _("REL (Relocatable file)");
2620 case ET_EXEC: return _("EXEC (Executable file)");
93df3340
AM
2621 case ET_DYN:
2622 if (is_pie (filedata))
2623 return _("DYN (Position-Independent Executable file)");
2624 else
2625 return _("DYN (Shared object file)");
32ec8896 2626 case ET_CORE: return _("CORE (Core file)");
252b5132
RH
2627
2628 default:
2629 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
e9e44622 2630 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
252b5132 2631 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
e9e44622 2632 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
252b5132 2633 else
e9e44622 2634 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
252b5132
RH
2635 return buff;
2636 }
2637}
2638
2639static char *
d3ba0551 2640get_machine_name (unsigned e_machine)
252b5132 2641{
b34976b6 2642 static char buff[64]; /* XXX */
252b5132
RH
2643
2644 switch (e_machine)
2645 {
55e22ca8
NC
2646 /* Please keep this switch table sorted by increasing EM_ value. */
2647 /* 0 */
c45021f2
NC
2648 case EM_NONE: return _("None");
2649 case EM_M32: return "WE32100";
2650 case EM_SPARC: return "Sparc";
2651 case EM_386: return "Intel 80386";
2652 case EM_68K: return "MC68000";
2653 case EM_88K: return "MC88000";
22abe556 2654 case EM_IAMCU: return "Intel MCU";
fb70ec17 2655 case EM_860: return "Intel 80860";
c45021f2
NC
2656 case EM_MIPS: return "MIPS R3000";
2657 case EM_S370: return "IBM System/370";
55e22ca8 2658 /* 10 */
7036c0e1 2659 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
252b5132 2660 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
c45021f2 2661 case EM_PARISC: return "HPPA";
55e22ca8 2662 case EM_VPP550: return "Fujitsu VPP500";
7036c0e1 2663 case EM_SPARC32PLUS: return "Sparc v8+" ;
d7867d17 2664 case EM_960: return "Intel 80960";
c45021f2 2665 case EM_PPC: return "PowerPC";
55e22ca8 2666 /* 20 */
285d1771 2667 case EM_PPC64: return "PowerPC64";
55e22ca8
NC
2668 case EM_S390_OLD:
2669 case EM_S390: return "IBM S/390";
2670 case EM_SPU: return "SPU";
2671 /* 30 */
2672 case EM_V800: return "Renesas V850 (using RH850 ABI)";
c45021f2
NC
2673 case EM_FR20: return "Fujitsu FR20";
2674 case EM_RH32: return "TRW RH32";
b34976b6 2675 case EM_MCORE: return "MCORE";
55e22ca8 2676 /* 40 */
7036c0e1
AJ
2677 case EM_ARM: return "ARM";
2678 case EM_OLD_ALPHA: return "Digital Alpha (old)";
ef230218 2679 case EM_SH: return "Renesas / SuperH SH";
c45021f2
NC
2680 case EM_SPARCV9: return "Sparc v9";
2681 case EM_TRICORE: return "Siemens Tricore";
584da044 2682 case EM_ARC: return "ARC";
c2dcd04e
NC
2683 case EM_H8_300: return "Renesas H8/300";
2684 case EM_H8_300H: return "Renesas H8/300H";
2685 case EM_H8S: return "Renesas H8S";
2686 case EM_H8_500: return "Renesas H8/500";
55e22ca8 2687 /* 50 */
30800947 2688 case EM_IA_64: return "Intel IA-64";
252b5132
RH
2689 case EM_MIPS_X: return "Stanford MIPS-X";
2690 case EM_COLDFIRE: return "Motorola Coldfire";
55e22ca8 2691 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
7036c0e1
AJ
2692 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2693 case EM_PCP: return "Siemens PCP";
2694 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2695 case EM_NDR1: return "Denso NDR1 microprocesspr";
2696 case EM_STARCORE: return "Motorola Star*Core processor";
2697 case EM_ME16: return "Toyota ME16 processor";
55e22ca8 2698 /* 60 */
7036c0e1
AJ
2699 case EM_ST100: return "STMicroelectronics ST100 processor";
2700 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
55e22ca8 2701 case EM_X86_64: return "Advanced Micro Devices X86-64";
11636f9e
JM
2702 case EM_PDSP: return "Sony DSP processor";
2703 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2704 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
7036c0e1
AJ
2705 case EM_FX66: return "Siemens FX66 microcontroller";
2706 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2707 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2708 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
55e22ca8 2709 /* 70 */
7036c0e1
AJ
2710 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2711 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2712 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2713 case EM_SVX: return "Silicon Graphics SVx";
2714 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2715 case EM_VAX: return "Digital VAX";
1b61cf92 2716 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
c45021f2
NC
2717 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2718 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2719 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
55e22ca8 2720 /* 80 */
b34976b6 2721 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
c45021f2 2722 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
3b36097d 2723 case EM_PRISM: return "Vitesse Prism";
55e22ca8
NC
2724 case EM_AVR_OLD:
2725 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2726 case EM_CYGNUS_FR30:
2727 case EM_FR30: return "Fujitsu FR30";
2728 case EM_CYGNUS_D10V:
2729 case EM_D10V: return "d10v";
2730 case EM_CYGNUS_D30V:
2731 case EM_D30V: return "d30v";
2732 case EM_CYGNUS_V850:
2733 case EM_V850: return "Renesas V850";
2734 case EM_CYGNUS_M32R:
2735 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2736 case EM_CYGNUS_MN10300:
2737 case EM_MN10300: return "mn10300";
2738 /* 90 */
2739 case EM_CYGNUS_MN10200:
2740 case EM_MN10200: return "mn10200";
2741 case EM_PJ: return "picoJava";
73589c9d 2742 case EM_OR1K: return "OpenRISC 1000";
55e22ca8 2743 case EM_ARC_COMPACT: return "ARCompact";
88da6820
NC
2744 case EM_XTENSA_OLD:
2745 case EM_XTENSA: return "Tensilica Xtensa Processor";
11636f9e
JM
2746 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2747 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2748 case EM_NS32K: return "National Semiconductor 32000 series";
2749 case EM_TPC: return "Tenor Network TPC processor";
55e22ca8
NC
2750 case EM_SNP1K: return "Trebia SNP 1000 processor";
2751 /* 100 */
9abca702 2752 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
55e22ca8
NC
2753 case EM_IP2K_OLD:
2754 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
11636f9e
JM
2755 case EM_MAX: return "MAX Processor";
2756 case EM_CR: return "National Semiconductor CompactRISC";
2757 case EM_F2MC16: return "Fujitsu F2MC16";
2758 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
7bbe5bc5 2759 case EM_BLACKFIN: return "Analog Devices Blackfin";
11636f9e
JM
2760 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2761 case EM_SEP: return "Sharp embedded microprocessor";
2762 case EM_ARCA: return "Arca RISC microprocessor";
55e22ca8 2763 /* 110 */
11636f9e
JM
2764 case EM_UNICORE: return "Unicore";
2765 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2766 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
64fd6348 2767 case EM_ALTERA_NIOS2: return "Altera Nios II";
55e22ca8
NC
2768 case EM_CRX: return "National Semiconductor CRX microprocessor";
2769 case EM_XGATE: return "Motorola XGATE embedded processor";
c29aca4a 2770 case EM_C166:
d70c5fc7 2771 case EM_XC16X: return "Infineon Technologies xc16x";
11636f9e
JM
2772 case EM_M16C: return "Renesas M16C series microprocessors";
2773 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2774 case EM_CE: return "Freescale Communication Engine RISC core";
55e22ca8
NC
2775 /* 120 */
2776 case EM_M32C: return "Renesas M32c";
2777 /* 130 */
11636f9e
JM
2778 case EM_TSK3000: return "Altium TSK3000 core";
2779 case EM_RS08: return "Freescale RS08 embedded processor";
2780 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
55e22ca8 2781 case EM_SCORE: return "SUNPLUS S+Core";
11636f9e
JM
2782 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2783 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
55e22ca8 2784 case EM_LATTICEMICO32: return "Lattice Mico32";
11636f9e 2785 case EM_SE_C17: return "Seiko Epson C17 family";
55e22ca8 2786 /* 140 */
11636f9e
JM
2787 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2788 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2789 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
55e22ca8
NC
2790 case EM_TI_PRU: return "TI PRU I/O processor";
2791 /* 160 */
11636f9e
JM
2792 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2793 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2794 case EM_R32C: return "Renesas R32C series microprocessors";
2795 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2796 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2797 case EM_8051: return "Intel 8051 and variants";
2798 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2799 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2800 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2801 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
55e22ca8 2802 /* 170 */
11636f9e
JM
2803 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2804 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2805 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
c7927a3c 2806 case EM_RX: return "Renesas RX";
a3c62988 2807 case EM_METAG: return "Imagination Technologies Meta processor architecture";
11636f9e
JM
2808 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2809 case EM_ECOG16: return "Cyan Technology eCOG16 family";
55e22ca8
NC
2810 case EM_CR16:
2811 case EM_MICROBLAZE:
2812 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
11636f9e
JM
2813 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2814 case EM_SLE9X: return "Infineon Technologies SLE9X core";
55e22ca8
NC
2815 /* 180 */
2816 case EM_L1OM: return "Intel L1OM";
2817 case EM_K1OM: return "Intel K1OM";
2818 case EM_INTEL182: return "Intel (reserved)";
2819 case EM_AARCH64: return "AArch64";
2820 case EM_ARM184: return "ARM (reserved)";
2821 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
11636f9e
JM
2822 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
2823 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
2824 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
55e22ca8 2825 /* 190 */
11636f9e 2826 case EM_CUDA: return "NVIDIA CUDA architecture";
55e22ca8 2827 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
6d913794
NC
2828 case EM_CLOUDSHIELD: return "CloudShield architecture family";
2829 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
2830 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
55e22ca8 2831 case EM_ARC_COMPACT2: return "ARCv2";
6d913794 2832 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
55e22ca8 2833 case EM_RL78: return "Renesas RL78";
6d913794 2834 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
55e22ca8
NC
2835 case EM_78K0R: return "Renesas 78K0R";
2836 /* 200 */
6d913794 2837 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
15f205b1
NC
2838 case EM_BA1: return "Beyond BA1 CPU architecture";
2839 case EM_BA2: return "Beyond BA2 CPU architecture";
6d913794
NC
2840 case EM_XCORE: return "XMOS xCORE processor family";
2841 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
7b9f9859 2842 case EM_INTELGT: return "Intel Graphics Technology";
55e22ca8 2843 /* 210 */
6d913794
NC
2844 case EM_KM32: return "KM211 KM32 32-bit processor";
2845 case EM_KMX32: return "KM211 KMX32 32-bit processor";
2846 case EM_KMX16: return "KM211 KMX16 16-bit processor";
2847 case EM_KMX8: return "KM211 KMX8 8-bit processor";
2848 case EM_KVARC: return "KM211 KVARC processor";
15f205b1 2849 case EM_CDP: return "Paneve CDP architecture family";
6d913794
NC
2850 case EM_COGE: return "Cognitive Smart Memory Processor";
2851 case EM_COOL: return "Bluechip Systems CoolEngine";
2852 case EM_NORC: return "Nanoradio Optimized RISC";
2853 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
55e22ca8 2854 /* 220 */
15f205b1 2855 case EM_Z80: return "Zilog Z80";
55e22ca8
NC
2856 case EM_VISIUM: return "CDS VISIUMcore processor";
2857 case EM_FT32: return "FTDI Chip FT32";
2858 case EM_MOXIE: return "Moxie";
2859 case EM_AMDGPU: return "AMD GPU";
4cf2ad72
CC
2860 /* 230 (all reserved) */
2861 /* 240 */
55e22ca8
NC
2862 case EM_RISCV: return "RISC-V";
2863 case EM_LANAI: return "Lanai 32-bit processor";
4cf2ad72
CC
2864 case EM_CEVA: return "CEVA Processor Architecture Family";
2865 case EM_CEVA_X2: return "CEVA X2 Processor Family";
55e22ca8 2866 case EM_BPF: return "Linux BPF";
4cf2ad72
CC
2867 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
2868 case EM_IMG1: return "Imagination Technologies";
2869 /* 250 */
fe944acf 2870 case EM_NFP: return "Netronome Flow Processor";
4cf2ad72
CC
2871 case EM_VE: return "NEC Vector Engine";
2872 case EM_CSKY: return "C-SKY";
2873 case EM_ARC_COMPACT3_64: return "Synopsys ARCv2.3 64-bit";
2874 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
2875 case EM_ARC_COMPACT3: return "Synopsys ARCv2.3 32-bit";
2876 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
2877 case EM_65816: return "WDC 65816/65C816";
01a8c731 2878 case EM_LOONGARCH: return "LoongArch";
4cf2ad72 2879 case EM_KF32: return "ChipON KungFu32";
55e22ca8
NC
2880
2881 /* Large numbers... */
2882 case EM_MT: return "Morpho Techologies MT processor";
2883 case EM_ALPHA: return "Alpha";
2884 case EM_WEBASSEMBLY: return "Web Assembly";
9abca702 2885 case EM_DLX: return "OpenDLX";
55e22ca8
NC
2886 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
2887 case EM_IQ2000: return "Vitesse IQ2000";
2888 case EM_M32C_OLD:
2889 case EM_NIOS32: return "Altera Nios";
2890 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
2891 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
2892 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
637b1970 2893 case EM_S12Z: return "Freescale S12Z";
55e22ca8 2894
252b5132 2895 default:
35d9dd2f 2896 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
252b5132
RH
2897 return buff;
2898 }
2899}
2900
a9522a21
AB
2901static void
2902decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
2903{
2904 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
6987d5a1 2905 other compilers don't specify an architecture type in the e_flags, and
a9522a21
AB
2906 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
2907 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
2908 architectures.
2909
2910 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
2911 but also sets a specific architecture type in the e_flags field.
2912
2913 However, when decoding the flags we don't worry if we see an
2914 unexpected pairing, for example EM_ARC_COMPACT machine type, with
2915 ARCEM architecture type. */
2916
2917 switch (e_flags & EF_ARC_MACH_MSK)
2918 {
2919 /* We only expect these to occur for EM_ARC_COMPACT2. */
2920 case EF_ARC_CPU_ARCV2EM:
2921 strcat (buf, ", ARC EM");
2922 break;
2923 case EF_ARC_CPU_ARCV2HS:
2924 strcat (buf, ", ARC HS");
2925 break;
2926
2927 /* We only expect these to occur for EM_ARC_COMPACT. */
2928 case E_ARC_MACH_ARC600:
2929 strcat (buf, ", ARC600");
2930 break;
2931 case E_ARC_MACH_ARC601:
2932 strcat (buf, ", ARC601");
2933 break;
2934 case E_ARC_MACH_ARC700:
2935 strcat (buf, ", ARC700");
2936 break;
2937
2938 /* The only times we should end up here are (a) A corrupt ELF, (b) A
2939 new ELF with new architecture being read by an old version of
2940 readelf, or (c) An ELF built with non-GNU compiler that does not
2941 set the architecture in the e_flags. */
2942 default:
2943 if (e_machine == EM_ARC_COMPACT)
2944 strcat (buf, ", Unknown ARCompact");
2945 else
2946 strcat (buf, ", Unknown ARC");
2947 break;
2948 }
2949
2950 switch (e_flags & EF_ARC_OSABI_MSK)
2951 {
2952 case E_ARC_OSABI_ORIG:
2953 strcat (buf, ", (ABI:legacy)");
2954 break;
2955 case E_ARC_OSABI_V2:
2956 strcat (buf, ", (ABI:v2)");
2957 break;
2958 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
2959 case E_ARC_OSABI_V3:
2960 strcat (buf, ", v3 no-legacy-syscalls ABI");
2961 break;
53a346d8
CZ
2962 case E_ARC_OSABI_V4:
2963 strcat (buf, ", v4 ABI");
2964 break;
a9522a21
AB
2965 default:
2966 strcat (buf, ", unrecognised ARC OSABI flag");
2967 break;
2968 }
2969}
2970
f3485b74 2971static void
d3ba0551 2972decode_ARM_machine_flags (unsigned e_flags, char buf[])
f3485b74
NC
2973{
2974 unsigned eabi;
015dc7e1 2975 bool unknown = false;
f3485b74
NC
2976
2977 eabi = EF_ARM_EABI_VERSION (e_flags);
2978 e_flags &= ~ EF_ARM_EABIMASK;
2979
2980 /* Handle "generic" ARM flags. */
2981 if (e_flags & EF_ARM_RELEXEC)
2982 {
2983 strcat (buf, ", relocatable executable");
2984 e_flags &= ~ EF_ARM_RELEXEC;
2985 }
76da6bbe 2986
18a20338
CL
2987 if (e_flags & EF_ARM_PIC)
2988 {
2989 strcat (buf, ", position independent");
2990 e_flags &= ~ EF_ARM_PIC;
2991 }
2992
f3485b74
NC
2993 /* Now handle EABI specific flags. */
2994 switch (eabi)
2995 {
2996 default:
2c71103e 2997 strcat (buf, ", <unrecognized EABI>");
f3485b74 2998 if (e_flags)
015dc7e1 2999 unknown = true;
f3485b74
NC
3000 break;
3001
3002 case EF_ARM_EABI_VER1:
a5bcd848 3003 strcat (buf, ", Version1 EABI");
f3485b74
NC
3004 while (e_flags)
3005 {
3006 unsigned flag;
76da6bbe 3007
f3485b74
NC
3008 /* Process flags one bit at a time. */
3009 flag = e_flags & - e_flags;
3010 e_flags &= ~ flag;
76da6bbe 3011
f3485b74
NC
3012 switch (flag)
3013 {
a5bcd848 3014 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
f3485b74
NC
3015 strcat (buf, ", sorted symbol tables");
3016 break;
76da6bbe 3017
f3485b74 3018 default:
015dc7e1 3019 unknown = true;
f3485b74
NC
3020 break;
3021 }
3022 }
3023 break;
76da6bbe 3024
a5bcd848
PB
3025 case EF_ARM_EABI_VER2:
3026 strcat (buf, ", Version2 EABI");
3027 while (e_flags)
3028 {
3029 unsigned flag;
3030
3031 /* Process flags one bit at a time. */
3032 flag = e_flags & - e_flags;
3033 e_flags &= ~ flag;
3034
3035 switch (flag)
3036 {
3037 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3038 strcat (buf, ", sorted symbol tables");
3039 break;
3040
3041 case EF_ARM_DYNSYMSUSESEGIDX:
3042 strcat (buf, ", dynamic symbols use segment index");
3043 break;
3044
3045 case EF_ARM_MAPSYMSFIRST:
3046 strcat (buf, ", mapping symbols precede others");
3047 break;
3048
3049 default:
015dc7e1 3050 unknown = true;
a5bcd848
PB
3051 break;
3052 }
3053 }
3054 break;
3055
d507cf36
PB
3056 case EF_ARM_EABI_VER3:
3057 strcat (buf, ", Version3 EABI");
8cb51566
PB
3058 break;
3059
3060 case EF_ARM_EABI_VER4:
3061 strcat (buf, ", Version4 EABI");
3bfcb652
NC
3062 while (e_flags)
3063 {
3064 unsigned flag;
3065
3066 /* Process flags one bit at a time. */
3067 flag = e_flags & - e_flags;
3068 e_flags &= ~ flag;
3069
3070 switch (flag)
3071 {
3072 case EF_ARM_BE8:
3073 strcat (buf, ", BE8");
3074 break;
3075
3076 case EF_ARM_LE8:
3077 strcat (buf, ", LE8");
3078 break;
3079
3080 default:
015dc7e1 3081 unknown = true;
3bfcb652
NC
3082 break;
3083 }
3bfcb652
NC
3084 }
3085 break;
3a4a14e9
PB
3086
3087 case EF_ARM_EABI_VER5:
3088 strcat (buf, ", Version5 EABI");
d507cf36
PB
3089 while (e_flags)
3090 {
3091 unsigned flag;
3092
3093 /* Process flags one bit at a time. */
3094 flag = e_flags & - e_flags;
3095 e_flags &= ~ flag;
3096
3097 switch (flag)
3098 {
3099 case EF_ARM_BE8:
3100 strcat (buf, ", BE8");
3101 break;
3102
3103 case EF_ARM_LE8:
3104 strcat (buf, ", LE8");
3105 break;
3106
3bfcb652
NC
3107 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
3108 strcat (buf, ", soft-float ABI");
3109 break;
3110
3111 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
3112 strcat (buf, ", hard-float ABI");
3113 break;
3114
d507cf36 3115 default:
015dc7e1 3116 unknown = true;
d507cf36
PB
3117 break;
3118 }
3119 }
3120 break;
3121
f3485b74 3122 case EF_ARM_EABI_UNKNOWN:
a5bcd848 3123 strcat (buf, ", GNU EABI");
f3485b74
NC
3124 while (e_flags)
3125 {
3126 unsigned flag;
76da6bbe 3127
f3485b74
NC
3128 /* Process flags one bit at a time. */
3129 flag = e_flags & - e_flags;
3130 e_flags &= ~ flag;
76da6bbe 3131
f3485b74
NC
3132 switch (flag)
3133 {
a5bcd848 3134 case EF_ARM_INTERWORK:
f3485b74
NC
3135 strcat (buf, ", interworking enabled");
3136 break;
76da6bbe 3137
a5bcd848 3138 case EF_ARM_APCS_26:
f3485b74
NC
3139 strcat (buf, ", uses APCS/26");
3140 break;
76da6bbe 3141
a5bcd848 3142 case EF_ARM_APCS_FLOAT:
f3485b74
NC
3143 strcat (buf, ", uses APCS/float");
3144 break;
76da6bbe 3145
a5bcd848 3146 case EF_ARM_PIC:
f3485b74
NC
3147 strcat (buf, ", position independent");
3148 break;
76da6bbe 3149
a5bcd848 3150 case EF_ARM_ALIGN8:
f3485b74
NC
3151 strcat (buf, ", 8 bit structure alignment");
3152 break;
76da6bbe 3153
a5bcd848 3154 case EF_ARM_NEW_ABI:
f3485b74
NC
3155 strcat (buf, ", uses new ABI");
3156 break;
76da6bbe 3157
a5bcd848 3158 case EF_ARM_OLD_ABI:
f3485b74
NC
3159 strcat (buf, ", uses old ABI");
3160 break;
76da6bbe 3161
a5bcd848 3162 case EF_ARM_SOFT_FLOAT:
f3485b74
NC
3163 strcat (buf, ", software FP");
3164 break;
76da6bbe 3165
90e01f86
ILT
3166 case EF_ARM_VFP_FLOAT:
3167 strcat (buf, ", VFP");
3168 break;
3169
fde78edd
NC
3170 case EF_ARM_MAVERICK_FLOAT:
3171 strcat (buf, ", Maverick FP");
3172 break;
3173
f3485b74 3174 default:
015dc7e1 3175 unknown = true;
f3485b74
NC
3176 break;
3177 }
3178 }
3179 }
f3485b74
NC
3180
3181 if (unknown)
2b692964 3182 strcat (buf,_(", <unknown>"));
f3485b74
NC
3183}
3184
343433df
AB
3185static void
3186decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
3187{
3188 --size; /* Leave space for null terminator. */
3189
3190 switch (e_flags & EF_AVR_MACH)
3191 {
3192 case E_AVR_MACH_AVR1:
3193 strncat (buf, ", avr:1", size);
3194 break;
3195 case E_AVR_MACH_AVR2:
3196 strncat (buf, ", avr:2", size);
3197 break;
3198 case E_AVR_MACH_AVR25:
3199 strncat (buf, ", avr:25", size);
3200 break;
3201 case E_AVR_MACH_AVR3:
3202 strncat (buf, ", avr:3", size);
3203 break;
3204 case E_AVR_MACH_AVR31:
3205 strncat (buf, ", avr:31", size);
3206 break;
3207 case E_AVR_MACH_AVR35:
3208 strncat (buf, ", avr:35", size);
3209 break;
3210 case E_AVR_MACH_AVR4:
3211 strncat (buf, ", avr:4", size);
3212 break;
3213 case E_AVR_MACH_AVR5:
3214 strncat (buf, ", avr:5", size);
3215 break;
3216 case E_AVR_MACH_AVR51:
3217 strncat (buf, ", avr:51", size);
3218 break;
3219 case E_AVR_MACH_AVR6:
3220 strncat (buf, ", avr:6", size);
3221 break;
3222 case E_AVR_MACH_AVRTINY:
3223 strncat (buf, ", avr:100", size);
3224 break;
3225 case E_AVR_MACH_XMEGA1:
3226 strncat (buf, ", avr:101", size);
3227 break;
3228 case E_AVR_MACH_XMEGA2:
3229 strncat (buf, ", avr:102", size);
3230 break;
3231 case E_AVR_MACH_XMEGA3:
3232 strncat (buf, ", avr:103", size);
3233 break;
3234 case E_AVR_MACH_XMEGA4:
3235 strncat (buf, ", avr:104", size);
3236 break;
3237 case E_AVR_MACH_XMEGA5:
3238 strncat (buf, ", avr:105", size);
3239 break;
3240 case E_AVR_MACH_XMEGA6:
3241 strncat (buf, ", avr:106", size);
3242 break;
3243 case E_AVR_MACH_XMEGA7:
3244 strncat (buf, ", avr:107", size);
3245 break;
3246 default:
3247 strncat (buf, ", avr:<unknown>", size);
3248 break;
3249 }
3250
3251 size -= strlen (buf);
3252 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
3253 strncat (buf, ", link-relax", size);
3254}
3255
35c08157
KLC
3256static void
3257decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
3258{
3259 unsigned abi;
3260 unsigned arch;
3261 unsigned config;
3262 unsigned version;
015dc7e1 3263 bool has_fpu = false;
32ec8896 3264 unsigned int r = 0;
35c08157
KLC
3265
3266 static const char *ABI_STRINGS[] =
3267 {
3268 "ABI v0", /* use r5 as return register; only used in N1213HC */
3269 "ABI v1", /* use r0 as return register */
3270 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
3271 "ABI v2fp", /* for FPU */
40c7a7cb
KLC
3272 "AABI",
3273 "ABI2 FP+"
35c08157
KLC
3274 };
3275 static const char *VER_STRINGS[] =
3276 {
3277 "Andes ELF V1.3 or older",
3278 "Andes ELF V1.3.1",
3279 "Andes ELF V1.4"
3280 };
3281 static const char *ARCH_STRINGS[] =
3282 {
3283 "",
3284 "Andes Star v1.0",
3285 "Andes Star v2.0",
3286 "Andes Star v3.0",
3287 "Andes Star v3.0m"
3288 };
3289
3290 abi = EF_NDS_ABI & e_flags;
3291 arch = EF_NDS_ARCH & e_flags;
3292 config = EF_NDS_INST & e_flags;
3293 version = EF_NDS32_ELF_VERSION & e_flags;
3294
3295 memset (buf, 0, size);
3296
3297 switch (abi)
3298 {
3299 case E_NDS_ABI_V0:
3300 case E_NDS_ABI_V1:
3301 case E_NDS_ABI_V2:
3302 case E_NDS_ABI_V2FP:
3303 case E_NDS_ABI_AABI:
40c7a7cb 3304 case E_NDS_ABI_V2FP_PLUS:
35c08157
KLC
3305 /* In case there are holes in the array. */
3306 r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3307 break;
3308
3309 default:
3310 r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
3311 break;
3312 }
3313
3314 switch (version)
3315 {
3316 case E_NDS32_ELF_VER_1_2:
3317 case E_NDS32_ELF_VER_1_3:
3318 case E_NDS32_ELF_VER_1_4:
3319 r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
3320 break;
3321
3322 default:
3323 r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
3324 break;
3325 }
3326
3327 if (E_NDS_ABI_V0 == abi)
3328 {
3329 /* OLD ABI; only used in N1213HC, has performance extension 1. */
3330 r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
3331 if (arch == E_NDS_ARCH_STAR_V1_0)
3332 r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
3333 return;
3334 }
3335
3336 switch (arch)
3337 {
3338 case E_NDS_ARCH_STAR_V1_0:
3339 case E_NDS_ARCH_STAR_V2_0:
3340 case E_NDS_ARCH_STAR_V3_0:
3341 case E_NDS_ARCH_STAR_V3_M:
3342 r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
3343 break;
3344
3345 default:
3346 r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3347 /* ARCH version determines how the e_flags are interpreted.
3348 If it is unknown, we cannot proceed. */
3349 return;
3350 }
3351
3352 /* Newer ABI; Now handle architecture specific flags. */
3353 if (arch == E_NDS_ARCH_STAR_V1_0)
3354 {
3355 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3356 r += snprintf (buf + r, size -r, ", MFUSR_PC");
3357
3358 if (!(config & E_NDS32_HAS_NO_MAC_INST))
3359 r += snprintf (buf + r, size -r, ", MAC");
3360
3361 if (config & E_NDS32_HAS_DIV_INST)
3362 r += snprintf (buf + r, size -r, ", DIV");
3363
3364 if (config & E_NDS32_HAS_16BIT_INST)
3365 r += snprintf (buf + r, size -r, ", 16b");
3366 }
3367 else
3368 {
3369 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3370 {
3371 if (version <= E_NDS32_ELF_VER_1_3)
3372 r += snprintf (buf + r, size -r, ", [B8]");
3373 else
3374 r += snprintf (buf + r, size -r, ", EX9");
3375 }
3376
3377 if (config & E_NDS32_HAS_MAC_DX_INST)
3378 r += snprintf (buf + r, size -r, ", MAC_DX");
3379
3380 if (config & E_NDS32_HAS_DIV_DX_INST)
3381 r += snprintf (buf + r, size -r, ", DIV_DX");
3382
3383 if (config & E_NDS32_HAS_16BIT_INST)
3384 {
3385 if (version <= E_NDS32_ELF_VER_1_3)
3386 r += snprintf (buf + r, size -r, ", 16b");
3387 else
3388 r += snprintf (buf + r, size -r, ", IFC");
3389 }
3390 }
3391
3392 if (config & E_NDS32_HAS_EXT_INST)
3393 r += snprintf (buf + r, size -r, ", PERF1");
3394
3395 if (config & E_NDS32_HAS_EXT2_INST)
3396 r += snprintf (buf + r, size -r, ", PERF2");
3397
3398 if (config & E_NDS32_HAS_FPU_INST)
3399 {
015dc7e1 3400 has_fpu = true;
35c08157
KLC
3401 r += snprintf (buf + r, size -r, ", FPU_SP");
3402 }
3403
3404 if (config & E_NDS32_HAS_FPU_DP_INST)
3405 {
015dc7e1 3406 has_fpu = true;
35c08157
KLC
3407 r += snprintf (buf + r, size -r, ", FPU_DP");
3408 }
3409
3410 if (config & E_NDS32_HAS_FPU_MAC_INST)
3411 {
015dc7e1 3412 has_fpu = true;
35c08157
KLC
3413 r += snprintf (buf + r, size -r, ", FPU_MAC");
3414 }
3415
3416 if (has_fpu)
3417 {
3418 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3419 {
3420 case E_NDS32_FPU_REG_8SP_4DP:
3421 r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3422 break;
3423 case E_NDS32_FPU_REG_16SP_8DP:
3424 r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3425 break;
3426 case E_NDS32_FPU_REG_32SP_16DP:
3427 r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3428 break;
3429 case E_NDS32_FPU_REG_32SP_32DP:
3430 r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3431 break;
3432 }
3433 }
3434
3435 if (config & E_NDS32_HAS_AUDIO_INST)
3436 r += snprintf (buf + r, size -r, ", AUDIO");
3437
3438 if (config & E_NDS32_HAS_STRING_INST)
3439 r += snprintf (buf + r, size -r, ", STR");
3440
3441 if (config & E_NDS32_HAS_REDUCED_REGS)
3442 r += snprintf (buf + r, size -r, ", 16REG");
3443
3444 if (config & E_NDS32_HAS_VIDEO_INST)
3445 {
3446 if (version <= E_NDS32_ELF_VER_1_3)
3447 r += snprintf (buf + r, size -r, ", VIDEO");
3448 else
3449 r += snprintf (buf + r, size -r, ", SATURATION");
3450 }
3451
3452 if (config & E_NDS32_HAS_ENCRIPT_INST)
3453 r += snprintf (buf + r, size -r, ", ENCRP");
3454
3455 if (config & E_NDS32_HAS_L2C_INST)
3456 r += snprintf (buf + r, size -r, ", L2C");
3457}
3458
252b5132 3459static char *
dda8d76d 3460get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
252b5132 3461{
b34976b6 3462 static char buf[1024];
252b5132
RH
3463
3464 buf[0] = '\0';
76da6bbe 3465
252b5132
RH
3466 if (e_flags)
3467 {
3468 switch (e_machine)
3469 {
3470 default:
3471 break;
3472
886a2506 3473 case EM_ARC_COMPACT2:
886a2506 3474 case EM_ARC_COMPACT:
a9522a21
AB
3475 decode_ARC_machine_flags (e_flags, e_machine, buf);
3476 break;
886a2506 3477
f3485b74
NC
3478 case EM_ARM:
3479 decode_ARM_machine_flags (e_flags, buf);
3480 break;
76da6bbe 3481
343433df
AB
3482 case EM_AVR:
3483 decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3484 break;
3485
781303ce
MF
3486 case EM_BLACKFIN:
3487 if (e_flags & EF_BFIN_PIC)
3488 strcat (buf, ", PIC");
3489
3490 if (e_flags & EF_BFIN_FDPIC)
3491 strcat (buf, ", FDPIC");
3492
3493 if (e_flags & EF_BFIN_CODE_IN_L1)
3494 strcat (buf, ", code in L1");
3495
3496 if (e_flags & EF_BFIN_DATA_IN_L1)
3497 strcat (buf, ", data in L1");
3498
3499 break;
3500
ec2dfb42
AO
3501 case EM_CYGNUS_FRV:
3502 switch (e_flags & EF_FRV_CPU_MASK)
3503 {
3504 case EF_FRV_CPU_GENERIC:
3505 break;
3506
3507 default:
3508 strcat (buf, ", fr???");
3509 break;
57346661 3510
ec2dfb42
AO
3511 case EF_FRV_CPU_FR300:
3512 strcat (buf, ", fr300");
3513 break;
3514
3515 case EF_FRV_CPU_FR400:
3516 strcat (buf, ", fr400");
3517 break;
3518 case EF_FRV_CPU_FR405:
3519 strcat (buf, ", fr405");
3520 break;
3521
3522 case EF_FRV_CPU_FR450:
3523 strcat (buf, ", fr450");
3524 break;
3525
3526 case EF_FRV_CPU_FR500:
3527 strcat (buf, ", fr500");
3528 break;
3529 case EF_FRV_CPU_FR550:
3530 strcat (buf, ", fr550");
3531 break;
3532
3533 case EF_FRV_CPU_SIMPLE:
3534 strcat (buf, ", simple");
3535 break;
3536 case EF_FRV_CPU_TOMCAT:
3537 strcat (buf, ", tomcat");
3538 break;
3539 }
1c877e87 3540 break;
ec2dfb42 3541
53c7db4b 3542 case EM_68K:
425c6cb0 3543 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
76f57f3a 3544 strcat (buf, ", m68000");
425c6cb0 3545 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3bdcfdf4
KH
3546 strcat (buf, ", cpu32");
3547 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3548 strcat (buf, ", fido_a");
425c6cb0 3549 else
266abb8f 3550 {
2cf0635d
NC
3551 char const * isa = _("unknown");
3552 char const * mac = _("unknown mac");
3553 char const * additional = NULL;
0112cd26 3554
c694fd50 3555 switch (e_flags & EF_M68K_CF_ISA_MASK)
266abb8f 3556 {
c694fd50 3557 case EF_M68K_CF_ISA_A_NODIV:
0b2e31dc
NS
3558 isa = "A";
3559 additional = ", nodiv";
3560 break;
c694fd50 3561 case EF_M68K_CF_ISA_A:
266abb8f
NS
3562 isa = "A";
3563 break;
c694fd50 3564 case EF_M68K_CF_ISA_A_PLUS:
266abb8f
NS
3565 isa = "A+";
3566 break;
c694fd50 3567 case EF_M68K_CF_ISA_B_NOUSP:
0b2e31dc
NS
3568 isa = "B";
3569 additional = ", nousp";
3570 break;
c694fd50 3571 case EF_M68K_CF_ISA_B:
266abb8f
NS
3572 isa = "B";
3573 break;
f608cd77
NS
3574 case EF_M68K_CF_ISA_C:
3575 isa = "C";
3576 break;
3577 case EF_M68K_CF_ISA_C_NODIV:
3578 isa = "C";
3579 additional = ", nodiv";
3580 break;
266abb8f
NS
3581 }
3582 strcat (buf, ", cf, isa ");
3583 strcat (buf, isa);
0b2e31dc
NS
3584 if (additional)
3585 strcat (buf, additional);
c694fd50 3586 if (e_flags & EF_M68K_CF_FLOAT)
0b2e31dc 3587 strcat (buf, ", float");
c694fd50 3588 switch (e_flags & EF_M68K_CF_MAC_MASK)
266abb8f
NS
3589 {
3590 case 0:
3591 mac = NULL;
3592 break;
c694fd50 3593 case EF_M68K_CF_MAC:
266abb8f
NS
3594 mac = "mac";
3595 break;
c694fd50 3596 case EF_M68K_CF_EMAC:
266abb8f
NS
3597 mac = "emac";
3598 break;
f608cd77
NS
3599 case EF_M68K_CF_EMAC_B:
3600 mac = "emac_b";
3601 break;
266abb8f
NS
3602 }
3603 if (mac)
3604 {
3605 strcat (buf, ", ");
3606 strcat (buf, mac);
3607 }
266abb8f 3608 }
53c7db4b 3609 break;
33c63f9d 3610
153a2776
NC
3611 case EM_CYGNUS_MEP:
3612 switch (e_flags & EF_MEP_CPU_MASK)
3613 {
3614 case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3615 case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3616 case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3617 case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3618 case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3619 case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3620 default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3621 }
3622
3623 switch (e_flags & EF_MEP_COP_MASK)
3624 {
3625 case EF_MEP_COP_NONE: break;
3626 case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3627 case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3628 case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3629 case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3630 default: strcat (buf, _("<unknown MeP copro type>")); break;
3631 }
3632
3633 if (e_flags & EF_MEP_LIBRARY)
3634 strcat (buf, ", Built for Library");
3635
3636 if (e_flags & EF_MEP_INDEX_MASK)
3637 sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3638 e_flags & EF_MEP_INDEX_MASK);
3639
3640 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3641 sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3642 e_flags & ~ EF_MEP_ALL_FLAGS);
3643 break;
3644
252b5132
RH
3645 case EM_PPC:
3646 if (e_flags & EF_PPC_EMB)
3647 strcat (buf, ", emb");
3648
3649 if (e_flags & EF_PPC_RELOCATABLE)
2b692964 3650 strcat (buf, _(", relocatable"));
252b5132
RH
3651
3652 if (e_flags & EF_PPC_RELOCATABLE_LIB)
2b692964 3653 strcat (buf, _(", relocatable-lib"));
252b5132
RH
3654 break;
3655
ee67d69a
AM
3656 case EM_PPC64:
3657 if (e_flags & EF_PPC64_ABI)
3658 {
3659 char abi[] = ", abiv0";
3660
3661 abi[6] += e_flags & EF_PPC64_ABI;
3662 strcat (buf, abi);
3663 }
3664 break;
3665
708e2187
NC
3666 case EM_V800:
3667 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3668 strcat (buf, ", RH850 ABI");
0b4362b0 3669
708e2187
NC
3670 if (e_flags & EF_V800_850E3)
3671 strcat (buf, ", V3 architecture");
3672
3673 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3674 strcat (buf, ", FPU not used");
3675
3676 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3677 strcat (buf, ", regmode: COMMON");
3678
3679 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3680 strcat (buf, ", r4 not used");
3681
3682 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3683 strcat (buf, ", r30 not used");
3684
3685 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3686 strcat (buf, ", r5 not used");
3687
3688 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3689 strcat (buf, ", r2 not used");
3690
3691 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3692 {
3693 switch (e_flags & - e_flags)
3694 {
3695 case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3696 case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
708e2187
NC
3697 case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3698 case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
708e2187
NC
3699 case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3700 case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3701 case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3702 case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3703 case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3704 case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3705 case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3706 case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3707 default: break;
3708 }
3709 }
3710 break;
3711
2b0337b0 3712 case EM_V850:
252b5132
RH
3713 case EM_CYGNUS_V850:
3714 switch (e_flags & EF_V850_ARCH)
3715 {
78c8d46c
NC
3716 case E_V850E3V5_ARCH:
3717 strcat (buf, ", v850e3v5");
3718 break;
1cd986c5
NC
3719 case E_V850E2V3_ARCH:
3720 strcat (buf, ", v850e2v3");
3721 break;
3722 case E_V850E2_ARCH:
3723 strcat (buf, ", v850e2");
3724 break;
3725 case E_V850E1_ARCH:
3726 strcat (buf, ", v850e1");
8ad30312 3727 break;
252b5132
RH
3728 case E_V850E_ARCH:
3729 strcat (buf, ", v850e");
3730 break;
252b5132
RH
3731 case E_V850_ARCH:
3732 strcat (buf, ", v850");
3733 break;
3734 default:
2b692964 3735 strcat (buf, _(", unknown v850 architecture variant"));
252b5132
RH
3736 break;
3737 }
3738 break;
3739
2b0337b0 3740 case EM_M32R:
252b5132
RH
3741 case EM_CYGNUS_M32R:
3742 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3743 strcat (buf, ", m32r");
252b5132
RH
3744 break;
3745
3746 case EM_MIPS:
4fe85591 3747 case EM_MIPS_RS3_LE:
252b5132
RH
3748 if (e_flags & EF_MIPS_NOREORDER)
3749 strcat (buf, ", noreorder");
3750
3751 if (e_flags & EF_MIPS_PIC)
3752 strcat (buf, ", pic");
3753
3754 if (e_flags & EF_MIPS_CPIC)
3755 strcat (buf, ", cpic");
3756
d1bdd336
TS
3757 if (e_flags & EF_MIPS_UCODE)
3758 strcat (buf, ", ugen_reserved");
3759
252b5132
RH
3760 if (e_flags & EF_MIPS_ABI2)
3761 strcat (buf, ", abi2");
3762
43521d43
TS
3763 if (e_flags & EF_MIPS_OPTIONS_FIRST)
3764 strcat (buf, ", odk first");
3765
a5d22d2a
TS
3766 if (e_flags & EF_MIPS_32BITMODE)
3767 strcat (buf, ", 32bitmode");
3768
ba92f887
MR
3769 if (e_flags & EF_MIPS_NAN2008)
3770 strcat (buf, ", nan2008");
3771
fef1b0b3
SE
3772 if (e_flags & EF_MIPS_FP64)
3773 strcat (buf, ", fp64");
3774
156c2f8b
NC
3775 switch ((e_flags & EF_MIPS_MACH))
3776 {
3777 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
3778 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
3779 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
156c2f8b 3780 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
810dfa6e
L
3781 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
3782 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
3783 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
3784 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
ef272caa 3785 case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
c6c98b38 3786 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
ebcb91b7 3787 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
350cc38d
MS
3788 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
3789 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
ac8cb70f 3790 case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
bd782c07 3791 case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
9108bc33 3792 case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
05c6f050 3793 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
67c2a3e8 3794 case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
d32e5c54 3795 case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
52b6b6b9 3796 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
38bf472a 3797 case E_MIPS_MACH_IAMR2: strcat (buf, ", interaptiv-mr2"); break;
43521d43
TS
3798 case 0:
3799 /* We simply ignore the field in this case to avoid confusion:
3800 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
3801 extension. */
3802 break;
2b692964 3803 default: strcat (buf, _(", unknown CPU")); break;
156c2f8b 3804 }
43521d43
TS
3805
3806 switch ((e_flags & EF_MIPS_ABI))
3807 {
3808 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
3809 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
3810 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
3811 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
3812 case 0:
3813 /* We simply ignore the field in this case to avoid confusion:
3814 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
3815 This means it is likely to be an o32 file, but not for
3816 sure. */
3817 break;
2b692964 3818 default: strcat (buf, _(", unknown ABI")); break;
43521d43
TS
3819 }
3820
3821 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
3822 strcat (buf, ", mdmx");
3823
3824 if (e_flags & EF_MIPS_ARCH_ASE_M16)
3825 strcat (buf, ", mips16");
3826
df58fc94
RS
3827 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
3828 strcat (buf, ", micromips");
3829
43521d43
TS
3830 switch ((e_flags & EF_MIPS_ARCH))
3831 {
3832 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
3833 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
3834 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
3835 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
3836 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
3837 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
cb44e358 3838 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
7361da2c 3839 case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
43521d43 3840 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
5f74bc13 3841 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
7361da2c 3842 case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
2b692964 3843 default: strcat (buf, _(", unknown ISA")); break;
43521d43 3844 }
252b5132 3845 break;
351b4b40 3846
35c08157
KLC
3847 case EM_NDS32:
3848 decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
3849 break;
3850
fe944acf
FT
3851 case EM_NFP:
3852 switch (EF_NFP_MACH (e_flags))
3853 {
3854 case E_NFP_MACH_3200:
3855 strcat (buf, ", NFP-32xx");
3856 break;
3857 case E_NFP_MACH_6000:
3858 strcat (buf, ", NFP-6xxx");
3859 break;
3860 }
3861 break;
3862
e23eba97
NC
3863 case EM_RISCV:
3864 if (e_flags & EF_RISCV_RVC)
3865 strcat (buf, ", RVC");
2922d21d 3866
7f999549
JW
3867 if (e_flags & EF_RISCV_RVE)
3868 strcat (buf, ", RVE");
3869
2922d21d
AW
3870 switch (e_flags & EF_RISCV_FLOAT_ABI)
3871 {
3872 case EF_RISCV_FLOAT_ABI_SOFT:
3873 strcat (buf, ", soft-float ABI");
3874 break;
3875
3876 case EF_RISCV_FLOAT_ABI_SINGLE:
3877 strcat (buf, ", single-float ABI");
3878 break;
3879
3880 case EF_RISCV_FLOAT_ABI_DOUBLE:
3881 strcat (buf, ", double-float ABI");
3882 break;
3883
3884 case EF_RISCV_FLOAT_ABI_QUAD:
3885 strcat (buf, ", quad-float ABI");
3886 break;
3887 }
e23eba97
NC
3888 break;
3889
ccde1100
AO
3890 case EM_SH:
3891 switch ((e_flags & EF_SH_MACH_MASK))
3892 {
3893 case EF_SH1: strcat (buf, ", sh1"); break;
3894 case EF_SH2: strcat (buf, ", sh2"); break;
3895 case EF_SH3: strcat (buf, ", sh3"); break;
3896 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
3897 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
3898 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
3899 case EF_SH3E: strcat (buf, ", sh3e"); break;
3900 case EF_SH4: strcat (buf, ", sh4"); break;
3901 case EF_SH5: strcat (buf, ", sh5"); break;
3902 case EF_SH2E: strcat (buf, ", sh2e"); break;
3903 case EF_SH4A: strcat (buf, ", sh4a"); break;
1d70c7fb 3904 case EF_SH2A: strcat (buf, ", sh2a"); break;
ccde1100
AO
3905 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
3906 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
1d70c7fb 3907 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
0b92ab21
NH
3908 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
3909 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
3910 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
3911 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
3912 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
3913 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
2b692964 3914 default: strcat (buf, _(", unknown ISA")); break;
ccde1100
AO
3915 }
3916
cec6a5b8
MR
3917 if (e_flags & EF_SH_PIC)
3918 strcat (buf, ", pic");
3919
3920 if (e_flags & EF_SH_FDPIC)
3921 strcat (buf, ", fdpic");
ccde1100 3922 break;
948f632f 3923
73589c9d
CS
3924 case EM_OR1K:
3925 if (e_flags & EF_OR1K_NODELAY)
3926 strcat (buf, ", no delay");
3927 break;
57346661 3928
351b4b40
RH
3929 case EM_SPARCV9:
3930 if (e_flags & EF_SPARC_32PLUS)
3931 strcat (buf, ", v8+");
3932
3933 if (e_flags & EF_SPARC_SUN_US1)
d07faca2
RH
3934 strcat (buf, ", ultrasparcI");
3935
3936 if (e_flags & EF_SPARC_SUN_US3)
3937 strcat (buf, ", ultrasparcIII");
351b4b40
RH
3938
3939 if (e_flags & EF_SPARC_HAL_R1)
3940 strcat (buf, ", halr1");
3941
3942 if (e_flags & EF_SPARC_LEDATA)
3943 strcat (buf, ", ledata");
3944
3945 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
3946 strcat (buf, ", tso");
3947
3948 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
3949 strcat (buf, ", pso");
3950
3951 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
3952 strcat (buf, ", rmo");
3953 break;
7d466069 3954
103f02d3
UD
3955 case EM_PARISC:
3956 switch (e_flags & EF_PARISC_ARCH)
3957 {
3958 case EFA_PARISC_1_0:
3959 strcpy (buf, ", PA-RISC 1.0");
3960 break;
3961 case EFA_PARISC_1_1:
3962 strcpy (buf, ", PA-RISC 1.1");
3963 break;
3964 case EFA_PARISC_2_0:
3965 strcpy (buf, ", PA-RISC 2.0");
3966 break;
3967 default:
3968 break;
3969 }
3970 if (e_flags & EF_PARISC_TRAPNIL)
3971 strcat (buf, ", trapnil");
3972 if (e_flags & EF_PARISC_EXT)
3973 strcat (buf, ", ext");
3974 if (e_flags & EF_PARISC_LSB)
3975 strcat (buf, ", lsb");
3976 if (e_flags & EF_PARISC_WIDE)
3977 strcat (buf, ", wide");
3978 if (e_flags & EF_PARISC_NO_KABP)
3979 strcat (buf, ", no kabp");
3980 if (e_flags & EF_PARISC_LAZYSWAP)
3981 strcat (buf, ", lazyswap");
30800947 3982 break;
76da6bbe 3983
7d466069 3984 case EM_PJ:
2b0337b0 3985 case EM_PJ_OLD:
7d466069
ILT
3986 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
3987 strcat (buf, ", new calling convention");
3988
3989 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
3990 strcat (buf, ", gnu calling convention");
3991 break;
4d6ed7c8
NC
3992
3993 case EM_IA_64:
3994 if ((e_flags & EF_IA_64_ABI64))
3995 strcat (buf, ", 64-bit");
3996 else
3997 strcat (buf, ", 32-bit");
3998 if ((e_flags & EF_IA_64_REDUCEDFP))
3999 strcat (buf, ", reduced fp model");
4000 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4001 strcat (buf, ", no function descriptors, constant gp");
4002 else if ((e_flags & EF_IA_64_CONS_GP))
4003 strcat (buf, ", constant gp");
4004 if ((e_flags & EF_IA_64_ABSOLUTE))
4005 strcat (buf, ", absolute");
dda8d76d 4006 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
28f997cf
TG
4007 {
4008 if ((e_flags & EF_IA_64_VMS_LINKAGES))
4009 strcat (buf, ", vms_linkages");
4010 switch ((e_flags & EF_IA_64_VMS_COMCOD))
4011 {
4012 case EF_IA_64_VMS_COMCOD_SUCCESS:
4013 break;
4014 case EF_IA_64_VMS_COMCOD_WARNING:
4015 strcat (buf, ", warning");
4016 break;
4017 case EF_IA_64_VMS_COMCOD_ERROR:
4018 strcat (buf, ", error");
4019 break;
4020 case EF_IA_64_VMS_COMCOD_ABORT:
4021 strcat (buf, ", abort");
4022 break;
4023 default:
bee0ee85
NC
4024 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
4025 e_flags & EF_IA_64_VMS_COMCOD);
4026 strcat (buf, ", <unknown>");
28f997cf
TG
4027 }
4028 }
4d6ed7c8 4029 break;
179d3252
JT
4030
4031 case EM_VAX:
4032 if ((e_flags & EF_VAX_NONPIC))
4033 strcat (buf, ", non-PIC");
4034 if ((e_flags & EF_VAX_DFLOAT))
4035 strcat (buf, ", D-Float");
4036 if ((e_flags & EF_VAX_GFLOAT))
4037 strcat (buf, ", G-Float");
4038 break;
c7927a3c 4039
619ed720
EB
4040 case EM_VISIUM:
4041 if (e_flags & EF_VISIUM_ARCH_MCM)
4042 strcat (buf, ", mcm");
4043 else if (e_flags & EF_VISIUM_ARCH_MCM24)
4044 strcat (buf, ", mcm24");
4045 if (e_flags & EF_VISIUM_ARCH_GR6)
4046 strcat (buf, ", gr6");
4047 break;
4048
4046d87a 4049 case EM_RL78:
1740ba0c
NC
4050 switch (e_flags & E_FLAG_RL78_CPU_MASK)
4051 {
4052 case E_FLAG_RL78_ANY_CPU: break;
4053 case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
4054 case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
4055 case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
4056 }
856ea05c
KP
4057 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
4058 strcat (buf, ", 64-bit doubles");
4046d87a 4059 break;
0b4362b0 4060
c7927a3c
NC
4061 case EM_RX:
4062 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
4063 strcat (buf, ", 64-bit doubles");
4064 if (e_flags & E_FLAG_RX_DSP)
dd24e3da 4065 strcat (buf, ", dsp");
d4cb0ea0 4066 if (e_flags & E_FLAG_RX_PID)
0b4362b0 4067 strcat (buf, ", pid");
708e2187
NC
4068 if (e_flags & E_FLAG_RX_ABI)
4069 strcat (buf, ", RX ABI");
3525236c
NC
4070 if (e_flags & E_FLAG_RX_SINSNS_SET)
4071 strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
4072 ? ", uses String instructions" : ", bans String instructions");
a117b0a5
YS
4073 if (e_flags & E_FLAG_RX_V2)
4074 strcat (buf, ", V2");
f87673e0
YS
4075 if (e_flags & E_FLAG_RX_V3)
4076 strcat (buf, ", V3");
d4cb0ea0 4077 break;
55786da2
AK
4078
4079 case EM_S390:
4080 if (e_flags & EF_S390_HIGH_GPRS)
4081 strcat (buf, ", highgprs");
d4cb0ea0 4082 break;
40b36596
JM
4083
4084 case EM_TI_C6000:
4085 if ((e_flags & EF_C6000_REL))
4086 strcat (buf, ", relocatable module");
d4cb0ea0 4087 break;
13761a11
NC
4088
4089 case EM_MSP430:
4090 strcat (buf, _(": architecture variant: "));
4091 switch (e_flags & EF_MSP430_MACH)
4092 {
4093 case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
4094 case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
4095 case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
4096 case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
4097 case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
4098 case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
4099 case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
4100 case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
4101 case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
4102 case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
4103 case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
4104 case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
4105 case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
4106 case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
4107 case E_MSP430_MACH_MSP430X : strcat (buf, "MSP430X"); break;
4108 default:
4109 strcat (buf, _(": unknown")); break;
4110 }
4111
4112 if (e_flags & ~ EF_MSP430_MACH)
4113 strcat (buf, _(": unknown extra flag bits also present"));
6655dba2
SB
4114 break;
4115
4116 case EM_Z80:
4117 switch (e_flags & EF_Z80_MACH_MSK)
4118 {
4119 case EF_Z80_MACH_Z80: strcat (buf, ", Z80"); break;
4120 case EF_Z80_MACH_Z180: strcat (buf, ", Z180"); break;
4121 case EF_Z80_MACH_R800: strcat (buf, ", R800"); break;
4122 case EF_Z80_MACH_EZ80_Z80: strcat (buf, ", EZ80"); break;
4123 case EF_Z80_MACH_EZ80_ADL: strcat (buf, ", EZ80, ADL"); break;
4124 case EF_Z80_MACH_GBZ80: strcat (buf, ", GBZ80"); break;
9fc0b501 4125 case EF_Z80_MACH_Z80N: strcat (buf, ", Z80N"); break;
6655dba2
SB
4126 default:
4127 strcat (buf, _(", unknown")); break;
4128 }
4129 break;
e9a0721f 4130 case EM_LOONGARCH:
4131 if (EF_LOONGARCH_IS_LP64 (e_flags))
4132 strcat (buf, ", LP64");
4133 else if (EF_LOONGARCH_IS_ILP32 (e_flags))
4134 strcat (buf, ", ILP32");
4135
4136 if (EF_LOONGARCH_IS_SOFT_FLOAT (e_flags))
4137 strcat (buf, ", SOFT-FLOAT");
4138 else if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
4139 strcat (buf, ", SINGLE-FLOAT");
4140 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
4141 strcat (buf, ", DOUBLE-FLOAT");
4142
4143 break;
252b5132
RH
4144 }
4145 }
4146
4147 return buf;
4148}
4149
252b5132 4150static const char *
dda8d76d 4151get_osabi_name (Filedata * filedata, unsigned int osabi)
d3ba0551
AM
4152{
4153 static char buff[32];
4154
4155 switch (osabi)
4156 {
4157 case ELFOSABI_NONE: return "UNIX - System V";
4158 case ELFOSABI_HPUX: return "UNIX - HP-UX";
4159 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
9c55345c 4160 case ELFOSABI_GNU: return "UNIX - GNU";
d3ba0551
AM
4161 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
4162 case ELFOSABI_AIX: return "UNIX - AIX";
4163 case ELFOSABI_IRIX: return "UNIX - IRIX";
4164 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
4165 case ELFOSABI_TRU64: return "UNIX - TRU64";
4166 case ELFOSABI_MODESTO: return "Novell - Modesto";
4167 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
4168 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
4169 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
3b26c801 4170 case ELFOSABI_AROS: return "AROS";
11636f9e 4171 case ELFOSABI_FENIXOS: return "FenixOS";
6d913794
NC
4172 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
4173 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
d3ba0551 4174 default:
40b36596 4175 if (osabi >= 64)
dda8d76d 4176 switch (filedata->file_header.e_machine)
40b36596
JM
4177 {
4178 case EM_ARM:
4179 switch (osabi)
4180 {
4181 case ELFOSABI_ARM: return "ARM";
18a20338 4182 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
40b36596
JM
4183 default:
4184 break;
4185 }
4186 break;
4187
4188 case EM_MSP430:
4189 case EM_MSP430_OLD:
619ed720 4190 case EM_VISIUM:
40b36596
JM
4191 switch (osabi)
4192 {
4193 case ELFOSABI_STANDALONE: return _("Standalone App");
4194 default:
4195 break;
4196 }
4197 break;
4198
4199 case EM_TI_C6000:
4200 switch (osabi)
4201 {
4202 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
4203 case ELFOSABI_C6000_LINUX: return "Linux C6000";
4204 default:
4205 break;
4206 }
4207 break;
4208
4209 default:
4210 break;
4211 }
e9e44622 4212 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
d3ba0551
AM
4213 return buff;
4214 }
4215}
4216
a06ea964
NC
4217static const char *
4218get_aarch64_segment_type (unsigned long type)
4219{
4220 switch (type)
4221 {
32ec8896
NC
4222 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
4223 default: return NULL;
a06ea964 4224 }
a06ea964
NC
4225}
4226
b294bdf8
MM
4227static const char *
4228get_arm_segment_type (unsigned long type)
4229{
4230 switch (type)
4231 {
32ec8896
NC
4232 case PT_ARM_EXIDX: return "EXIDX";
4233 default: return NULL;
b294bdf8 4234 }
b294bdf8
MM
4235}
4236
b4cbbe8f
AK
4237static const char *
4238get_s390_segment_type (unsigned long type)
4239{
4240 switch (type)
4241 {
4242 case PT_S390_PGSTE: return "S390_PGSTE";
4243 default: return NULL;
4244 }
4245}
4246
d3ba0551
AM
4247static const char *
4248get_mips_segment_type (unsigned long type)
252b5132
RH
4249{
4250 switch (type)
4251 {
32ec8896
NC
4252 case PT_MIPS_REGINFO: return "REGINFO";
4253 case PT_MIPS_RTPROC: return "RTPROC";
4254 case PT_MIPS_OPTIONS: return "OPTIONS";
4255 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
4256 default: return NULL;
252b5132 4257 }
252b5132
RH
4258}
4259
103f02d3 4260static const char *
d3ba0551 4261get_parisc_segment_type (unsigned long type)
103f02d3
UD
4262{
4263 switch (type)
4264 {
103f02d3
UD
4265 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
4266 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
61472819 4267 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
32ec8896 4268 default: return NULL;
103f02d3 4269 }
103f02d3
UD
4270}
4271
4d6ed7c8 4272static const char *
d3ba0551 4273get_ia64_segment_type (unsigned long type)
4d6ed7c8
NC
4274{
4275 switch (type)
4276 {
4277 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
4278 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
32ec8896 4279 default: return NULL;
4d6ed7c8 4280 }
4d6ed7c8
NC
4281}
4282
40b36596
JM
4283static const char *
4284get_tic6x_segment_type (unsigned long type)
4285{
4286 switch (type)
4287 {
32ec8896
NC
4288 case PT_C6000_PHATTR: return "C6000_PHATTR";
4289 default: return NULL;
40b36596 4290 }
40b36596
JM
4291}
4292
fbc95f1e
KC
4293static const char *
4294get_riscv_segment_type (unsigned long type)
4295{
4296 switch (type)
4297 {
4298 case PT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4299 default: return NULL;
4300 }
4301}
4302
df3a023b
AM
4303static const char *
4304get_hpux_segment_type (unsigned long type, unsigned e_machine)
4305{
4306 if (e_machine == EM_PARISC)
4307 switch (type)
4308 {
4309 case PT_HP_TLS: return "HP_TLS";
4310 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
4311 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
4312 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
4313 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
4314 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
4315 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
4316 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
4317 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
4318 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
4319 case PT_HP_PARALLEL: return "HP_PARALLEL";
4320 case PT_HP_FASTBIND: return "HP_FASTBIND";
4321 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
4322 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
4323 case PT_HP_STACK: return "HP_STACK";
4324 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
4325 default: return NULL;
4326 }
4327
4328 if (e_machine == EM_IA_64)
4329 switch (type)
4330 {
4331 case PT_HP_TLS: return "HP_TLS";
4332 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
4333 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
4334 case PT_IA_64_HP_STACK: return "HP_STACK";
4335 default: return NULL;
4336 }
4337
4338 return NULL;
4339}
4340
5522f910
NC
4341static const char *
4342get_solaris_segment_type (unsigned long type)
4343{
4344 switch (type)
4345 {
4346 case 0x6464e550: return "PT_SUNW_UNWIND";
4347 case 0x6474e550: return "PT_SUNW_EH_FRAME";
4348 case 0x6ffffff7: return "PT_LOSUNW";
4349 case 0x6ffffffa: return "PT_SUNWBSS";
4350 case 0x6ffffffb: return "PT_SUNWSTACK";
4351 case 0x6ffffffc: return "PT_SUNWDTRACE";
4352 case 0x6ffffffd: return "PT_SUNWCAP";
4353 case 0x6fffffff: return "PT_HISUNW";
32ec8896 4354 default: return NULL;
5522f910
NC
4355 }
4356}
4357
252b5132 4358static const char *
dda8d76d 4359get_segment_type (Filedata * filedata, unsigned long p_type)
252b5132 4360{
b34976b6 4361 static char buff[32];
252b5132
RH
4362
4363 switch (p_type)
4364 {
b34976b6
AM
4365 case PT_NULL: return "NULL";
4366 case PT_LOAD: return "LOAD";
252b5132 4367 case PT_DYNAMIC: return "DYNAMIC";
b34976b6
AM
4368 case PT_INTERP: return "INTERP";
4369 case PT_NOTE: return "NOTE";
4370 case PT_SHLIB: return "SHLIB";
4371 case PT_PHDR: return "PHDR";
13ae64f3 4372 case PT_TLS: return "TLS";
32ec8896 4373 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
2b05f1b7 4374 case PT_GNU_STACK: return "GNU_STACK";
8c37241b 4375 case PT_GNU_RELRO: return "GNU_RELRO";
0a59decb 4376 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
65765700 4377
3eba3ef3
NC
4378 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
4379 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
4380 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
b9e920ec 4381
252b5132 4382 default:
df3a023b 4383 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
252b5132 4384 {
2cf0635d 4385 const char * result;
103f02d3 4386
dda8d76d 4387 switch (filedata->file_header.e_machine)
252b5132 4388 {
a06ea964
NC
4389 case EM_AARCH64:
4390 result = get_aarch64_segment_type (p_type);
4391 break;
b294bdf8
MM
4392 case EM_ARM:
4393 result = get_arm_segment_type (p_type);
4394 break;
252b5132 4395 case EM_MIPS:
4fe85591 4396 case EM_MIPS_RS3_LE:
252b5132
RH
4397 result = get_mips_segment_type (p_type);
4398 break;
103f02d3
UD
4399 case EM_PARISC:
4400 result = get_parisc_segment_type (p_type);
4401 break;
4d6ed7c8
NC
4402 case EM_IA_64:
4403 result = get_ia64_segment_type (p_type);
4404 break;
40b36596
JM
4405 case EM_TI_C6000:
4406 result = get_tic6x_segment_type (p_type);
4407 break;
b4cbbe8f
AK
4408 case EM_S390:
4409 case EM_S390_OLD:
4410 result = get_s390_segment_type (p_type);
4411 break;
fbc95f1e
KC
4412 case EM_RISCV:
4413 result = get_riscv_segment_type (p_type);
4414 break;
252b5132
RH
4415 default:
4416 result = NULL;
4417 break;
4418 }
103f02d3 4419
252b5132
RH
4420 if (result != NULL)
4421 return result;
103f02d3 4422
1a9ccd70 4423 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
252b5132
RH
4424 }
4425 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
103f02d3 4426 {
df3a023b 4427 const char * result = NULL;
103f02d3 4428
df3a023b 4429 switch (filedata->file_header.e_ident[EI_OSABI])
103f02d3 4430 {
df3a023b
AM
4431 case ELFOSABI_GNU:
4432 case ELFOSABI_FREEBSD:
4433 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
4434 {
4435 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
4436 result = buff;
4437 }
103f02d3 4438 break;
df3a023b
AM
4439 case ELFOSABI_HPUX:
4440 result = get_hpux_segment_type (p_type,
4441 filedata->file_header.e_machine);
4442 break;
4443 case ELFOSABI_SOLARIS:
4444 result = get_solaris_segment_type (p_type);
00428cca 4445 break;
103f02d3 4446 default:
103f02d3
UD
4447 break;
4448 }
103f02d3
UD
4449 if (result != NULL)
4450 return result;
4451
1a9ccd70 4452 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
103f02d3 4453 }
252b5132 4454 else
e9e44622 4455 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
252b5132
RH
4456
4457 return buff;
4458 }
4459}
4460
53a346d8
CZ
4461static const char *
4462get_arc_section_type_name (unsigned int sh_type)
4463{
4464 switch (sh_type)
4465 {
4466 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
4467 default:
4468 break;
4469 }
4470 return NULL;
4471}
4472
252b5132 4473static const char *
d3ba0551 4474get_mips_section_type_name (unsigned int sh_type)
252b5132
RH
4475{
4476 switch (sh_type)
4477 {
b34976b6
AM
4478 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
4479 case SHT_MIPS_MSYM: return "MIPS_MSYM";
4480 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
4481 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
4482 case SHT_MIPS_UCODE: return "MIPS_UCODE";
4483 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
4484 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
4485 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
4486 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
4487 case SHT_MIPS_RELD: return "MIPS_RELD";
4488 case SHT_MIPS_IFACE: return "MIPS_IFACE";
4489 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
4490 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
4491 case SHT_MIPS_SHDR: return "MIPS_SHDR";
4492 case SHT_MIPS_FDESC: return "MIPS_FDESC";
4493 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
4494 case SHT_MIPS_DENSE: return "MIPS_DENSE";
4495 case SHT_MIPS_PDESC: return "MIPS_PDESC";
4496 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
4497 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
4498 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
4499 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
4500 case SHT_MIPS_LINE: return "MIPS_LINE";
4501 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
4502 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
4503 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
4504 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
4505 case SHT_MIPS_DWARF: return "MIPS_DWARF";
4506 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
4507 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
4508 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
4509 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
4510 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
4511 case SHT_MIPS_XLATE: return "MIPS_XLATE";
4512 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
4513 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
4514 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
4515 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
252b5132 4516 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
351cdf24 4517 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
f16a9783 4518 case SHT_MIPS_XHASH: return "MIPS_XHASH";
252b5132
RH
4519 default:
4520 break;
4521 }
4522 return NULL;
4523}
4524
103f02d3 4525static const char *
d3ba0551 4526get_parisc_section_type_name (unsigned int sh_type)
103f02d3
UD
4527{
4528 switch (sh_type)
4529 {
4530 case SHT_PARISC_EXT: return "PARISC_EXT";
4531 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
4532 case SHT_PARISC_DOC: return "PARISC_DOC";
eec8f817
DA
4533 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
4534 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
4535 case SHT_PARISC_STUBS: return "PARISC_STUBS";
61472819 4536 case SHT_PARISC_DLKM: return "PARISC_DLKM";
32ec8896 4537 default: return NULL;
103f02d3 4538 }
103f02d3
UD
4539}
4540
4d6ed7c8 4541static const char *
dda8d76d 4542get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4d6ed7c8 4543{
18bd398b 4544 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
ecc51f48 4545 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
dda8d76d 4546 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
0de14b54 4547
4d6ed7c8
NC
4548 switch (sh_type)
4549 {
148b93f2
NC
4550 case SHT_IA_64_EXT: return "IA_64_EXT";
4551 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
4552 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
4553 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
4554 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4555 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
4556 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
4557 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
4558 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
4559 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
4d6ed7c8
NC
4560 default:
4561 break;
4562 }
4563 return NULL;
4564}
4565
d2b2c203
DJ
4566static const char *
4567get_x86_64_section_type_name (unsigned int sh_type)
4568{
4569 switch (sh_type)
4570 {
4571 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
32ec8896 4572 default: return NULL;
d2b2c203 4573 }
d2b2c203
DJ
4574}
4575
a06ea964
NC
4576static const char *
4577get_aarch64_section_type_name (unsigned int sh_type)
4578{
4579 switch (sh_type)
4580 {
32ec8896
NC
4581 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4582 default: return NULL;
a06ea964 4583 }
a06ea964
NC
4584}
4585
40a18ebd
NC
4586static const char *
4587get_arm_section_type_name (unsigned int sh_type)
4588{
4589 switch (sh_type)
4590 {
7f6fed87
NC
4591 case SHT_ARM_EXIDX: return "ARM_EXIDX";
4592 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
4593 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
4594 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
4595 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
32ec8896 4596 default: return NULL;
40a18ebd 4597 }
40a18ebd
NC
4598}
4599
40b36596
JM
4600static const char *
4601get_tic6x_section_type_name (unsigned int sh_type)
4602{
4603 switch (sh_type)
4604 {
32ec8896
NC
4605 case SHT_C6000_UNWIND: return "C6000_UNWIND";
4606 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
4607 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
4608 case SHT_TI_ICODE: return "TI_ICODE";
4609 case SHT_TI_XREF: return "TI_XREF";
4610 case SHT_TI_HANDLER: return "TI_HANDLER";
4611 case SHT_TI_INITINFO: return "TI_INITINFO";
4612 case SHT_TI_PHATTRS: return "TI_PHATTRS";
4613 default: return NULL;
40b36596 4614 }
40b36596
JM
4615}
4616
13761a11 4617static const char *
b0191216 4618get_msp430_section_type_name (unsigned int sh_type)
13761a11
NC
4619{
4620 switch (sh_type)
4621 {
32ec8896
NC
4622 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
4623 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
4624 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
4625 default: return NULL;
13761a11
NC
4626 }
4627}
4628
fe944acf
FT
4629static const char *
4630get_nfp_section_type_name (unsigned int sh_type)
4631{
4632 switch (sh_type)
4633 {
4634 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
4635 case SHT_NFP_INITREG: return "NFP_INITREG";
4636 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
4637 default: return NULL;
4638 }
4639}
4640
685080f2
NC
4641static const char *
4642get_v850_section_type_name (unsigned int sh_type)
4643{
4644 switch (sh_type)
4645 {
32ec8896
NC
4646 case SHT_V850_SCOMMON: return "V850 Small Common";
4647 case SHT_V850_TCOMMON: return "V850 Tiny Common";
4648 case SHT_V850_ZCOMMON: return "V850 Zero Common";
4649 case SHT_RENESAS_IOP: return "RENESAS IOP";
4650 case SHT_RENESAS_INFO: return "RENESAS INFO";
4651 default: return NULL;
685080f2
NC
4652 }
4653}
4654
2dc8dd17
JW
4655static const char *
4656get_riscv_section_type_name (unsigned int sh_type)
4657{
4658 switch (sh_type)
4659 {
4660 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4661 default: return NULL;
4662 }
4663}
4664
0861f561
CQ
4665static const char *
4666get_csky_section_type_name (unsigned int sh_type)
4667{
4668 switch (sh_type)
4669 {
4670 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
4671 default: return NULL;
4672 }
4673}
4674
252b5132 4675static const char *
dda8d76d 4676get_section_type_name (Filedata * filedata, unsigned int sh_type)
252b5132 4677{
b34976b6 4678 static char buff[32];
9fb71ee4 4679 const char * result;
252b5132
RH
4680
4681 switch (sh_type)
4682 {
4683 case SHT_NULL: return "NULL";
4684 case SHT_PROGBITS: return "PROGBITS";
4685 case SHT_SYMTAB: return "SYMTAB";
4686 case SHT_STRTAB: return "STRTAB";
4687 case SHT_RELA: return "RELA";
dd207c13 4688 case SHT_RELR: return "RELR";
252b5132
RH
4689 case SHT_HASH: return "HASH";
4690 case SHT_DYNAMIC: return "DYNAMIC";
4691 case SHT_NOTE: return "NOTE";
4692 case SHT_NOBITS: return "NOBITS";
4693 case SHT_REL: return "REL";
4694 case SHT_SHLIB: return "SHLIB";
4695 case SHT_DYNSYM: return "DYNSYM";
d1133906
NC
4696 case SHT_INIT_ARRAY: return "INIT_ARRAY";
4697 case SHT_FINI_ARRAY: return "FINI_ARRAY";
4698 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
fdc90cb4 4699 case SHT_GNU_HASH: return "GNU_HASH";
93ebe586 4700 case SHT_GROUP: return "GROUP";
67ce483b 4701 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
252b5132
RH
4702 case SHT_GNU_verdef: return "VERDEF";
4703 case SHT_GNU_verneed: return "VERNEED";
4704 case SHT_GNU_versym: return "VERSYM";
b34976b6
AM
4705 case 0x6ffffff0: return "VERSYM";
4706 case 0x6ffffffc: return "VERDEF";
252b5132
RH
4707 case 0x7ffffffd: return "AUXILIARY";
4708 case 0x7fffffff: return "FILTER";
047b2264 4709 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
252b5132
RH
4710
4711 default:
4712 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4713 {
dda8d76d 4714 switch (filedata->file_header.e_machine)
252b5132 4715 {
53a346d8
CZ
4716 case EM_ARC:
4717 case EM_ARC_COMPACT:
4718 case EM_ARC_COMPACT2:
4719 result = get_arc_section_type_name (sh_type);
4720 break;
252b5132 4721 case EM_MIPS:
4fe85591 4722 case EM_MIPS_RS3_LE:
252b5132
RH
4723 result = get_mips_section_type_name (sh_type);
4724 break;
103f02d3
UD
4725 case EM_PARISC:
4726 result = get_parisc_section_type_name (sh_type);
4727 break;
4d6ed7c8 4728 case EM_IA_64:
dda8d76d 4729 result = get_ia64_section_type_name (filedata, sh_type);
4d6ed7c8 4730 break;
d2b2c203 4731 case EM_X86_64:
8a9036a4 4732 case EM_L1OM:
7a9068fe 4733 case EM_K1OM:
d2b2c203
DJ
4734 result = get_x86_64_section_type_name (sh_type);
4735 break;
a06ea964
NC
4736 case EM_AARCH64:
4737 result = get_aarch64_section_type_name (sh_type);
4738 break;
40a18ebd
NC
4739 case EM_ARM:
4740 result = get_arm_section_type_name (sh_type);
4741 break;
40b36596
JM
4742 case EM_TI_C6000:
4743 result = get_tic6x_section_type_name (sh_type);
4744 break;
13761a11 4745 case EM_MSP430:
b0191216 4746 result = get_msp430_section_type_name (sh_type);
13761a11 4747 break;
fe944acf
FT
4748 case EM_NFP:
4749 result = get_nfp_section_type_name (sh_type);
4750 break;
685080f2
NC
4751 case EM_V800:
4752 case EM_V850:
4753 case EM_CYGNUS_V850:
4754 result = get_v850_section_type_name (sh_type);
4755 break;
2dc8dd17
JW
4756 case EM_RISCV:
4757 result = get_riscv_section_type_name (sh_type);
4758 break;
0861f561
CQ
4759 case EM_CSKY:
4760 result = get_csky_section_type_name (sh_type);
4761 break;
252b5132
RH
4762 default:
4763 result = NULL;
4764 break;
4765 }
4766
4767 if (result != NULL)
4768 return result;
4769
9fb71ee4 4770 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
252b5132
RH
4771 }
4772 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
148b93f2 4773 {
dda8d76d 4774 switch (filedata->file_header.e_machine)
148b93f2
NC
4775 {
4776 case EM_IA_64:
dda8d76d 4777 result = get_ia64_section_type_name (filedata, sh_type);
148b93f2
NC
4778 break;
4779 default:
dda8d76d 4780 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
fd85a6a1
NC
4781 result = get_solaris_section_type (sh_type);
4782 else
1b4b80bf
NC
4783 {
4784 switch (sh_type)
4785 {
4786 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
4787 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
4788 case SHT_GNU_HASH: result = "GNU_HASH"; break;
4789 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
4790 default:
4791 result = NULL;
4792 break;
4793 }
4794 }
148b93f2
NC
4795 break;
4796 }
4797
4798 if (result != NULL)
4799 return result;
4800
9fb71ee4 4801 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
148b93f2 4802 }
252b5132 4803 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
685080f2 4804 {
dda8d76d 4805 switch (filedata->file_header.e_machine)
685080f2
NC
4806 {
4807 case EM_V800:
4808 case EM_V850:
4809 case EM_CYGNUS_V850:
9fb71ee4 4810 result = get_v850_section_type_name (sh_type);
a9fb83be 4811 break;
685080f2 4812 default:
9fb71ee4 4813 result = NULL;
685080f2
NC
4814 break;
4815 }
4816
9fb71ee4
NC
4817 if (result != NULL)
4818 return result;
4819
4820 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
685080f2 4821 }
252b5132 4822 else
a7dbfd1c
NC
4823 /* This message is probably going to be displayed in a 15
4824 character wide field, so put the hex value first. */
4825 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
103f02d3 4826
252b5132
RH
4827 return buff;
4828 }
4829}
4830
79bc120c
NC
4831enum long_option_values
4832{
4833 OPTION_DEBUG_DUMP = 512,
4834 OPTION_DYN_SYMS,
0f03783c 4835 OPTION_LTO_SYMS,
79bc120c
NC
4836 OPTION_DWARF_DEPTH,
4837 OPTION_DWARF_START,
4838 OPTION_DWARF_CHECK,
4839 OPTION_CTF_DUMP,
4840 OPTION_CTF_PARENT,
4841 OPTION_CTF_SYMBOLS,
4842 OPTION_CTF_STRINGS,
4843 OPTION_WITH_SYMBOL_VERSIONS,
4844 OPTION_RECURSE_LIMIT,
4845 OPTION_NO_RECURSE_LIMIT,
047c3dbf
NL
4846 OPTION_NO_DEMANGLING,
4847 OPTION_SYM_BASE
79bc120c 4848};
2979dc34 4849
85b1c36d 4850static struct option options[] =
252b5132 4851{
79bc120c
NC
4852 /* Note - This table is alpha-sorted on the 'val'
4853 field in order to make adding new options easier. */
4854 {"arch-specific", no_argument, 0, 'A'},
b34976b6 4855 {"all", no_argument, 0, 'a'},
79bc120c
NC
4856 {"demangle", optional_argument, 0, 'C'},
4857 {"archive-index", no_argument, 0, 'c'},
4858 {"use-dynamic", no_argument, 0, 'D'},
4859 {"dynamic", no_argument, 0, 'd'},
b34976b6 4860 {"headers", no_argument, 0, 'e'},
79bc120c
NC
4861 {"section-groups", no_argument, 0, 'g'},
4862 {"help", no_argument, 0, 'H'},
4863 {"file-header", no_argument, 0, 'h'},
b34976b6 4864 {"histogram", no_argument, 0, 'I'},
79bc120c
NC
4865 {"lint", no_argument, 0, 'L'},
4866 {"enable-checks", no_argument, 0, 'L'},
4867 {"program-headers", no_argument, 0, 'l'},
b34976b6 4868 {"segments", no_argument, 0, 'l'},
595cf52e 4869 {"full-section-name",no_argument, 0, 'N'},
79bc120c 4870 {"notes", no_argument, 0, 'n'},
ca0e11aa 4871 {"process-links", no_argument, 0, 'P'},
79bc120c
NC
4872 {"string-dump", required_argument, 0, 'p'},
4873 {"relocated-dump", required_argument, 0, 'R'},
4874 {"relocs", no_argument, 0, 'r'},
4875 {"section-headers", no_argument, 0, 'S'},
4876 {"sections", no_argument, 0, 'S'},
b34976b6
AM
4877 {"symbols", no_argument, 0, 's'},
4878 {"syms", no_argument, 0, 's'},
79bc120c
NC
4879 {"silent-truncation",no_argument, 0, 'T'},
4880 {"section-details", no_argument, 0, 't'},
b3aa80b4 4881 {"unicode", required_argument, NULL, 'U'},
09c11c86 4882 {"unwind", no_argument, 0, 'u'},
79bc120c
NC
4883 {"version-info", no_argument, 0, 'V'},
4884 {"version", no_argument, 0, 'v'},
4885 {"wide", no_argument, 0, 'W'},
b34976b6 4886 {"hex-dump", required_argument, 0, 'x'},
0e602686 4887 {"decompress", no_argument, 0, 'z'},
252b5132 4888
79bc120c
NC
4889 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
4890 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
4891 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4892 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4893 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
0f03783c 4894 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
79bc120c 4895 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
fd2f0033
TT
4896 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
4897 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
4723351a 4898 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
094e34f2 4899#ifdef ENABLE_LIBCTF
d344b407 4900 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
7d9813f1
NA
4901 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
4902 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
4903 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
094e34f2 4904#endif
047c3dbf 4905 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
7d9813f1 4906
b34976b6 4907 {0, no_argument, 0, 0}
252b5132
RH
4908};
4909
4910static void
2cf0635d 4911usage (FILE * stream)
252b5132 4912{
92f01d61
JM
4913 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
4914 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
d6249f5f
AM
4915 fprintf (stream, _(" Options are:\n"));
4916 fprintf (stream, _("\
4917 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
4918 fprintf (stream, _("\
4919 -h --file-header Display the ELF file header\n"));
4920 fprintf (stream, _("\
4921 -l --program-headers Display the program headers\n"));
4922 fprintf (stream, _("\
4923 --segments An alias for --program-headers\n"));
4924 fprintf (stream, _("\
4925 -S --section-headers Display the sections' header\n"));
4926 fprintf (stream, _("\
4927 --sections An alias for --section-headers\n"));
4928 fprintf (stream, _("\
4929 -g --section-groups Display the section groups\n"));
4930 fprintf (stream, _("\
4931 -t --section-details Display the section details\n"));
4932 fprintf (stream, _("\
4933 -e --headers Equivalent to: -h -l -S\n"));
4934 fprintf (stream, _("\
4935 -s --syms Display the symbol table\n"));
4936 fprintf (stream, _("\
4937 --symbols An alias for --syms\n"));
4938 fprintf (stream, _("\
4939 --dyn-syms Display the dynamic symbol table\n"));
4940 fprintf (stream, _("\
4941 --lto-syms Display LTO symbol tables\n"));
4942 fprintf (stream, _("\
047c3dbf
NL
4943 --sym-base=[0|8|10|16] \n\
4944 Force base for symbol sizes. The options are \n\
d6249f5f
AM
4945 mixed (the default), octal, decimal, hexadecimal.\n"));
4946 fprintf (stream, _("\
79bc120c
NC
4947 -C --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
4948 The STYLE, if specified, can be `auto' (the default),\n\
4949 `gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
d6249f5f
AM
4950 or `gnat'\n"));
4951 fprintf (stream, _("\
4952 --no-demangle Do not demangle low-level symbol names. (default)\n"));
4953 fprintf (stream, _("\
4954 --recurse-limit Enable a demangling recursion limit. (default)\n"));
4955 fprintf (stream, _("\
4956 --no-recurse-limit Disable a demangling recursion limit\n"));
b3aa80b4
NC
4957 fprintf (stream, _("\
4958 -U[dlexhi] --unicode=[default|locale|escape|hex|highlight|invalid]\n\
4959 Display unicode characters as determined by the current locale\n\
4960 (default), escape sequences, \"<hex sequences>\", highlighted\n\
4961 escape sequences, or treat them as invalid and display as\n\
4962 \"{hex sequences}\"\n"));
d6249f5f
AM
4963 fprintf (stream, _("\
4964 -n --notes Display the core notes (if present)\n"));
4965 fprintf (stream, _("\
4966 -r --relocs Display the relocations (if present)\n"));
4967 fprintf (stream, _("\
4968 -u --unwind Display the unwind info (if present)\n"));
4969 fprintf (stream, _("\
4970 -d --dynamic Display the dynamic section (if present)\n"));
4971 fprintf (stream, _("\
4972 -V --version-info Display the version sections (if present)\n"));
4973 fprintf (stream, _("\
4974 -A --arch-specific Display architecture specific information (if any)\n"));
4975 fprintf (stream, _("\
4976 -c --archive-index Display the symbol/file index in an archive\n"));
4977 fprintf (stream, _("\
4978 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
4979 fprintf (stream, _("\
4980 -L --lint|--enable-checks\n\
4981 Display warning messages for possible problems\n"));
4982 fprintf (stream, _("\
09c11c86 4983 -x --hex-dump=<number|name>\n\
d6249f5f
AM
4984 Dump the contents of section <number|name> as bytes\n"));
4985 fprintf (stream, _("\
09c11c86 4986 -p --string-dump=<number|name>\n\
d6249f5f
AM
4987 Dump the contents of section <number|name> as strings\n"));
4988 fprintf (stream, _("\
cf13d699 4989 -R --relocated-dump=<number|name>\n\
d6249f5f
AM
4990 Dump the relocated contents of section <number|name>\n"));
4991 fprintf (stream, _("\
4992 -z --decompress Decompress section before dumping it\n"));
4993 fprintf (stream, _("\
4994 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
4995 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
4996 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
4997 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
4998 U/=trace_info]\n\
4999 Display the contents of DWARF debug sections\n"));
5000 fprintf (stream, _("\
5001 -wk --debug-dump=links Display the contents of sections that link to separate\n\
5002 debuginfo files\n"));
5003 fprintf (stream, _("\
5004 -P --process-links Display the contents of non-debug sections in separate\n\
5005 debuginfo files. (Implies -wK)\n"));
c46b7066
NC
5006#if DEFAULT_FOR_FOLLOW_LINKS
5007 fprintf (stream, _("\
d6249f5f
AM
5008 -wK --debug-dump=follow-links\n\
5009 Follow links to separate debug info files (default)\n"));
5010 fprintf (stream, _("\
5011 -wN --debug-dump=no-follow-links\n\
5012 Do not follow links to separate debug info files\n"));
c46b7066
NC
5013#else
5014 fprintf (stream, _("\
d6249f5f
AM
5015 -wK --debug-dump=follow-links\n\
5016 Follow links to separate debug info files\n"));
5017 fprintf (stream, _("\
5018 -wN --debug-dump=no-follow-links\n\
5019 Do not follow links to separate debug info files\n\
5020 (default)\n"));
c46b7066 5021#endif
fd2f0033 5022 fprintf (stream, _("\
d6249f5f
AM
5023 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
5024 fprintf (stream, _("\
5025 --dwarf-start=N Display DIEs starting at offset N\n"));
094e34f2 5026#ifdef ENABLE_LIBCTF
7d9813f1 5027 fprintf (stream, _("\
d6249f5f
AM
5028 --ctf=<number|name> Display CTF info from section <number|name>\n"));
5029 fprintf (stream, _("\
80b56fad 5030 --ctf-parent=<name> Use CTF archive member <name> as the CTF parent\n"));
d6249f5f 5031 fprintf (stream, _("\
7d9813f1 5032 --ctf-symbols=<number|name>\n\
d6249f5f
AM
5033 Use section <number|name> as the CTF external symtab\n"));
5034 fprintf (stream, _("\
7d9813f1 5035 --ctf-strings=<number|name>\n\
d6249f5f 5036 Use section <number|name> as the CTF external strtab\n"));
094e34f2 5037#endif
7d9813f1 5038
252b5132 5039#ifdef SUPPORT_DISASSEMBLY
92f01d61 5040 fprintf (stream, _("\
09c11c86
NC
5041 -i --instruction-dump=<number|name>\n\
5042 Disassemble the contents of section <number|name>\n"));
252b5132 5043#endif
92f01d61 5044 fprintf (stream, _("\
d6249f5f
AM
5045 -I --histogram Display histogram of bucket list lengths\n"));
5046 fprintf (stream, _("\
5047 -W --wide Allow output width to exceed 80 characters\n"));
5048 fprintf (stream, _("\
5049 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
5050 fprintf (stream, _("\
5051 @<file> Read options from <file>\n"));
5052 fprintf (stream, _("\
5053 -H --help Display this information\n"));
5054 fprintf (stream, _("\
8b53311e 5055 -v --version Display the version number of readelf\n"));
1118d252 5056
92f01d61
JM
5057 if (REPORT_BUGS_TO[0] && stream == stdout)
5058 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
252b5132 5059
92f01d61 5060 exit (stream == stdout ? 0 : 1);
252b5132
RH
5061}
5062
18bd398b
NC
5063/* Record the fact that the user wants the contents of section number
5064 SECTION to be displayed using the method(s) encoded as flags bits
5065 in TYPE. Note, TYPE can be zero if we are creating the array for
5066 the first time. */
5067
252b5132 5068static void
6431e409
AM
5069request_dump_bynumber (struct dump_data *dumpdata,
5070 unsigned int section, dump_type type)
252b5132 5071{
6431e409 5072 if (section >= dumpdata->num_dump_sects)
252b5132 5073 {
2cf0635d 5074 dump_type * new_dump_sects;
252b5132 5075
3f5e193b 5076 new_dump_sects = (dump_type *) calloc (section + 1,
dda8d76d 5077 sizeof (* new_dump_sects));
252b5132
RH
5078
5079 if (new_dump_sects == NULL)
591a748a 5080 error (_("Out of memory allocating dump request table.\n"));
252b5132
RH
5081 else
5082 {
6431e409 5083 if (dumpdata->dump_sects)
21b65bac
NC
5084 {
5085 /* Copy current flag settings. */
6431e409
AM
5086 memcpy (new_dump_sects, dumpdata->dump_sects,
5087 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
252b5132 5088
6431e409 5089 free (dumpdata->dump_sects);
21b65bac 5090 }
252b5132 5091
6431e409
AM
5092 dumpdata->dump_sects = new_dump_sects;
5093 dumpdata->num_dump_sects = section + 1;
252b5132
RH
5094 }
5095 }
5096
6431e409
AM
5097 if (dumpdata->dump_sects)
5098 dumpdata->dump_sects[section] |= type;
252b5132
RH
5099}
5100
aef1f6d0
DJ
5101/* Request a dump by section name. */
5102
5103static void
2cf0635d 5104request_dump_byname (const char * section, dump_type type)
aef1f6d0 5105{
2cf0635d 5106 struct dump_list_entry * new_request;
aef1f6d0 5107
3f5e193b
NC
5108 new_request = (struct dump_list_entry *)
5109 malloc (sizeof (struct dump_list_entry));
aef1f6d0 5110 if (!new_request)
591a748a 5111 error (_("Out of memory allocating dump request table.\n"));
aef1f6d0
DJ
5112
5113 new_request->name = strdup (section);
5114 if (!new_request->name)
591a748a 5115 error (_("Out of memory allocating dump request table.\n"));
aef1f6d0
DJ
5116
5117 new_request->type = type;
5118
5119 new_request->next = dump_sects_byname;
5120 dump_sects_byname = new_request;
5121}
5122
cf13d699 5123static inline void
6431e409 5124request_dump (struct dump_data *dumpdata, dump_type type)
cf13d699
NC
5125{
5126 int section;
5127 char * cp;
5128
015dc7e1 5129 do_dump = true;
cf13d699
NC
5130 section = strtoul (optarg, & cp, 0);
5131
5132 if (! *cp && section >= 0)
6431e409 5133 request_dump_bynumber (dumpdata, section, type);
cf13d699
NC
5134 else
5135 request_dump_byname (optarg, type);
5136}
5137
252b5132 5138static void
6431e409 5139parse_args (struct dump_data *dumpdata, int argc, char ** argv)
252b5132
RH
5140{
5141 int c;
5142
5143 if (argc < 2)
92f01d61 5144 usage (stderr);
252b5132
RH
5145
5146 while ((c = getopt_long
b3aa80b4 5147 (argc, argv, "ACDHILNPR:STU:VWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
252b5132 5148 {
252b5132
RH
5149 switch (c)
5150 {
5151 case 0:
5152 /* Long options. */
5153 break;
5154 case 'H':
92f01d61 5155 usage (stdout);
252b5132
RH
5156 break;
5157
5158 case 'a':
015dc7e1
AM
5159 do_syms = true;
5160 do_reloc = true;
5161 do_unwind = true;
5162 do_dynamic = true;
5163 do_header = true;
5164 do_sections = true;
5165 do_section_groups = true;
5166 do_segments = true;
5167 do_version = true;
5168 do_histogram = true;
5169 do_arch = true;
5170 do_notes = true;
252b5132 5171 break;
79bc120c 5172
f5842774 5173 case 'g':
015dc7e1 5174 do_section_groups = true;
f5842774 5175 break;
5477e8a0 5176 case 't':
595cf52e 5177 case 'N':
015dc7e1
AM
5178 do_sections = true;
5179 do_section_details = true;
595cf52e 5180 break;
252b5132 5181 case 'e':
015dc7e1
AM
5182 do_header = true;
5183 do_sections = true;
5184 do_segments = true;
252b5132 5185 break;
a952a375 5186 case 'A':
015dc7e1 5187 do_arch = true;
a952a375 5188 break;
252b5132 5189 case 'D':
015dc7e1 5190 do_using_dynamic = true;
252b5132
RH
5191 break;
5192 case 'r':
015dc7e1 5193 do_reloc = true;
252b5132 5194 break;
4d6ed7c8 5195 case 'u':
015dc7e1 5196 do_unwind = true;
4d6ed7c8 5197 break;
252b5132 5198 case 'h':
015dc7e1 5199 do_header = true;
252b5132
RH
5200 break;
5201 case 'l':
015dc7e1 5202 do_segments = true;
252b5132
RH
5203 break;
5204 case 's':
015dc7e1 5205 do_syms = true;
252b5132
RH
5206 break;
5207 case 'S':
015dc7e1 5208 do_sections = true;
252b5132
RH
5209 break;
5210 case 'd':
015dc7e1 5211 do_dynamic = true;
252b5132 5212 break;
a952a375 5213 case 'I':
015dc7e1 5214 do_histogram = true;
a952a375 5215 break;
779fe533 5216 case 'n':
015dc7e1 5217 do_notes = true;
779fe533 5218 break;
4145f1d5 5219 case 'c':
015dc7e1 5220 do_archive_index = true;
4145f1d5 5221 break;
1b513401 5222 case 'L':
015dc7e1 5223 do_checks = true;
1b513401 5224 break;
ca0e11aa 5225 case 'P':
015dc7e1
AM
5226 process_links = true;
5227 do_follow_links = true;
ca0e11aa 5228 break;
252b5132 5229 case 'x':
6431e409 5230 request_dump (dumpdata, HEX_DUMP);
aef1f6d0 5231 break;
09c11c86 5232 case 'p':
6431e409 5233 request_dump (dumpdata, STRING_DUMP);
cf13d699
NC
5234 break;
5235 case 'R':
6431e409 5236 request_dump (dumpdata, RELOC_DUMP);
09c11c86 5237 break;
0e602686 5238 case 'z':
015dc7e1 5239 decompress_dumps = true;
0e602686 5240 break;
252b5132 5241 case 'w':
015dc7e1 5242 do_dump = true;
0f03783c 5243 if (optarg == NULL)
613ff48b 5244 {
015dc7e1 5245 do_debugging = true;
613ff48b
CC
5246 dwarf_select_sections_all ();
5247 }
252b5132
RH
5248 else
5249 {
015dc7e1 5250 do_debugging = false;
4cb93e3b 5251 dwarf_select_sections_by_letters (optarg);
252b5132
RH
5252 }
5253 break;
2979dc34 5254 case OPTION_DEBUG_DUMP:
015dc7e1 5255 do_dump = true;
0f03783c 5256 if (optarg == NULL)
d6249f5f
AM
5257 {
5258 do_debugging = true;
5259 dwarf_select_sections_all ();
5260 }
2979dc34
JJ
5261 else
5262 {
015dc7e1 5263 do_debugging = false;
4cb93e3b 5264 dwarf_select_sections_by_names (optarg);
2979dc34
JJ
5265 }
5266 break;
fd2f0033
TT
5267 case OPTION_DWARF_DEPTH:
5268 {
5269 char *cp;
5270
5271 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
5272 }
5273 break;
5274 case OPTION_DWARF_START:
5275 {
5276 char *cp;
5277
5278 dwarf_start_die = strtoul (optarg, & cp, 0);
5279 }
5280 break;
4723351a 5281 case OPTION_DWARF_CHECK:
015dc7e1 5282 dwarf_check = true;
4723351a 5283 break;
7d9813f1 5284 case OPTION_CTF_DUMP:
015dc7e1 5285 do_ctf = true;
6431e409 5286 request_dump (dumpdata, CTF_DUMP);
7d9813f1
NA
5287 break;
5288 case OPTION_CTF_SYMBOLS:
df16e041 5289 free (dump_ctf_symtab_name);
7d9813f1
NA
5290 dump_ctf_symtab_name = strdup (optarg);
5291 break;
5292 case OPTION_CTF_STRINGS:
df16e041 5293 free (dump_ctf_strtab_name);
7d9813f1
NA
5294 dump_ctf_strtab_name = strdup (optarg);
5295 break;
5296 case OPTION_CTF_PARENT:
df16e041 5297 free (dump_ctf_parent_name);
7d9813f1
NA
5298 dump_ctf_parent_name = strdup (optarg);
5299 break;
2c610e4b 5300 case OPTION_DYN_SYMS:
015dc7e1 5301 do_dyn_syms = true;
2c610e4b 5302 break;
0f03783c 5303 case OPTION_LTO_SYMS:
015dc7e1 5304 do_lto_syms = true;
0f03783c 5305 break;
252b5132
RH
5306#ifdef SUPPORT_DISASSEMBLY
5307 case 'i':
6431e409 5308 request_dump (dumpdata, DISASS_DUMP);
cf13d699 5309 break;
252b5132
RH
5310#endif
5311 case 'v':
5312 print_version (program_name);
5313 break;
5314 case 'V':
015dc7e1 5315 do_version = true;
252b5132 5316 break;
d974e256 5317 case 'W':
015dc7e1 5318 do_wide = true;
d974e256 5319 break;
0942c7ab 5320 case 'T':
015dc7e1 5321 do_not_show_symbol_truncation = true;
0942c7ab 5322 break;
79bc120c 5323 case 'C':
015dc7e1 5324 do_demangle = true;
79bc120c
NC
5325 if (optarg != NULL)
5326 {
5327 enum demangling_styles style;
5328
5329 style = cplus_demangle_name_to_style (optarg);
5330 if (style == unknown_demangling)
5331 error (_("unknown demangling style `%s'"), optarg);
5332
5333 cplus_demangle_set_style (style);
5334 }
5335 break;
5336 case OPTION_NO_DEMANGLING:
015dc7e1 5337 do_demangle = false;
79bc120c
NC
5338 break;
5339 case OPTION_RECURSE_LIMIT:
5340 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
5341 break;
5342 case OPTION_NO_RECURSE_LIMIT:
5343 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
5344 break;
5345 case OPTION_WITH_SYMBOL_VERSIONS:
5346 /* Ignored for backward compatibility. */
5347 break;
b9e920ec 5348
b3aa80b4
NC
5349 case 'U':
5350 if (optarg == NULL)
5351 error (_("Missing arg to -U/--unicode")); /* Can this happen ? */
5352 else if (streq (optarg, "default") || streq (optarg, "d"))
5353 unicode_display = unicode_default;
5354 else if (streq (optarg, "locale") || streq (optarg, "l"))
5355 unicode_display = unicode_locale;
5356 else if (streq (optarg, "escape") || streq (optarg, "e"))
5357 unicode_display = unicode_escape;
5358 else if (streq (optarg, "invalid") || streq (optarg, "i"))
5359 unicode_display = unicode_invalid;
5360 else if (streq (optarg, "hex") || streq (optarg, "x"))
5361 unicode_display = unicode_hex;
5362 else if (streq (optarg, "highlight") || streq (optarg, "h"))
5363 unicode_display = unicode_highlight;
5364 else
5365 error (_("invalid argument to -U/--unicode: %s"), optarg);
5366 break;
5367
047c3dbf
NL
5368 case OPTION_SYM_BASE:
5369 sym_base = 0;
5370 if (optarg != NULL)
5371 {
5372 sym_base = strtoul (optarg, NULL, 0);
5373 switch (sym_base)
5374 {
5375 case 0:
5376 case 8:
5377 case 10:
5378 case 16:
5379 break;
5380
5381 default:
5382 sym_base = 0;
5383 break;
5384 }
5385 }
5386 break;
5387
252b5132 5388 default:
252b5132
RH
5389 /* xgettext:c-format */
5390 error (_("Invalid option '-%c'\n"), c);
1a0670f3 5391 /* Fall through. */
252b5132 5392 case '?':
92f01d61 5393 usage (stderr);
252b5132
RH
5394 }
5395 }
5396
4d6ed7c8 5397 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
252b5132 5398 && !do_segments && !do_header && !do_dump && !do_version
f5842774 5399 && !do_histogram && !do_debugging && !do_arch && !do_notes
2c610e4b 5400 && !do_section_groups && !do_archive_index
0f03783c 5401 && !do_dyn_syms && !do_lto_syms)
1b513401
NC
5402 {
5403 if (do_checks)
5404 {
015dc7e1
AM
5405 check_all = true;
5406 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
5407 do_segments = do_header = do_dump = do_version = true;
5408 do_histogram = do_debugging = do_arch = do_notes = true;
5409 do_section_groups = do_archive_index = do_dyn_syms = true;
5410 do_lto_syms = true;
1b513401
NC
5411 }
5412 else
5413 usage (stderr);
5414 }
252b5132
RH
5415}
5416
5417static const char *
d3ba0551 5418get_elf_class (unsigned int elf_class)
252b5132 5419{
b34976b6 5420 static char buff[32];
103f02d3 5421
252b5132
RH
5422 switch (elf_class)
5423 {
5424 case ELFCLASSNONE: return _("none");
e3c8793a
NC
5425 case ELFCLASS32: return "ELF32";
5426 case ELFCLASS64: return "ELF64";
ab5e7794 5427 default:
e9e44622 5428 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
ab5e7794 5429 return buff;
252b5132
RH
5430 }
5431}
5432
5433static const char *
d3ba0551 5434get_data_encoding (unsigned int encoding)
252b5132 5435{
b34976b6 5436 static char buff[32];
103f02d3 5437
252b5132
RH
5438 switch (encoding)
5439 {
5440 case ELFDATANONE: return _("none");
33c63f9d
CM
5441 case ELFDATA2LSB: return _("2's complement, little endian");
5442 case ELFDATA2MSB: return _("2's complement, big endian");
103f02d3 5443 default:
e9e44622 5444 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
ab5e7794 5445 return buff;
252b5132
RH
5446 }
5447}
5448
dda8d76d 5449/* Decode the data held in 'filedata->file_header'. */
ee42cf8c 5450
015dc7e1 5451static bool
dda8d76d 5452process_file_header (Filedata * filedata)
252b5132 5453{
dda8d76d
NC
5454 Elf_Internal_Ehdr * header = & filedata->file_header;
5455
5456 if ( header->e_ident[EI_MAG0] != ELFMAG0
5457 || header->e_ident[EI_MAG1] != ELFMAG1
5458 || header->e_ident[EI_MAG2] != ELFMAG2
5459 || header->e_ident[EI_MAG3] != ELFMAG3)
252b5132
RH
5460 {
5461 error
5462 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
015dc7e1 5463 return false;
252b5132
RH
5464 }
5465
ca0e11aa
NC
5466 if (! filedata->is_separate)
5467 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
2dc4cec1 5468
252b5132
RH
5469 if (do_header)
5470 {
32ec8896 5471 unsigned i;
252b5132 5472
ca0e11aa
NC
5473 if (filedata->is_separate)
5474 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
5475 else
5476 printf (_("ELF Header:\n"));
252b5132 5477 printf (_(" Magic: "));
b34976b6 5478 for (i = 0; i < EI_NIDENT; i++)
dda8d76d 5479 printf ("%2.2x ", header->e_ident[i]);
252b5132
RH
5480 printf ("\n");
5481 printf (_(" Class: %s\n"),
dda8d76d 5482 get_elf_class (header->e_ident[EI_CLASS]));
252b5132 5483 printf (_(" Data: %s\n"),
dda8d76d 5484 get_data_encoding (header->e_ident[EI_DATA]));
e8a64888 5485 printf (_(" Version: %d%s\n"),
dda8d76d
NC
5486 header->e_ident[EI_VERSION],
5487 (header->e_ident[EI_VERSION] == EV_CURRENT
e8a64888 5488 ? _(" (current)")
dda8d76d 5489 : (header->e_ident[EI_VERSION] != EV_NONE
e8a64888 5490 ? _(" <unknown>")
789be9f7 5491 : "")));
252b5132 5492 printf (_(" OS/ABI: %s\n"),
dda8d76d 5493 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
252b5132 5494 printf (_(" ABI Version: %d\n"),
dda8d76d 5495 header->e_ident[EI_ABIVERSION]);
252b5132 5496 printf (_(" Type: %s\n"),
93df3340 5497 get_file_type (filedata));
252b5132 5498 printf (_(" Machine: %s\n"),
dda8d76d 5499 get_machine_name (header->e_machine));
252b5132 5500 printf (_(" Version: 0x%lx\n"),
e8a64888 5501 header->e_version);
76da6bbe 5502
f7a99963 5503 printf (_(" Entry point address: "));
e8a64888 5504 print_vma (header->e_entry, PREFIX_HEX);
f7a99963 5505 printf (_("\n Start of program headers: "));
e8a64888 5506 print_vma (header->e_phoff, DEC);
f7a99963 5507 printf (_(" (bytes into file)\n Start of section headers: "));
e8a64888 5508 print_vma (header->e_shoff, DEC);
f7a99963 5509 printf (_(" (bytes into file)\n"));
76da6bbe 5510
252b5132 5511 printf (_(" Flags: 0x%lx%s\n"),
e8a64888 5512 header->e_flags,
dda8d76d 5513 get_machine_flags (filedata, header->e_flags, header->e_machine));
e8a64888
AM
5514 printf (_(" Size of this header: %u (bytes)\n"),
5515 header->e_ehsize);
5516 printf (_(" Size of program headers: %u (bytes)\n"),
5517 header->e_phentsize);
5518 printf (_(" Number of program headers: %u"),
5519 header->e_phnum);
dda8d76d
NC
5520 if (filedata->section_headers != NULL
5521 && header->e_phnum == PN_XNUM
5522 && filedata->section_headers[0].sh_info != 0)
e8a64888
AM
5523 {
5524 header->e_phnum = filedata->section_headers[0].sh_info;
5525 printf (" (%u)", header->e_phnum);
5526 }
2046a35d 5527 putc ('\n', stdout);
e8a64888
AM
5528 printf (_(" Size of section headers: %u (bytes)\n"),
5529 header->e_shentsize);
5530 printf (_(" Number of section headers: %u"),
5531 header->e_shnum);
dda8d76d 5532 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
e8a64888
AM
5533 {
5534 header->e_shnum = filedata->section_headers[0].sh_size;
5535 printf (" (%u)", header->e_shnum);
5536 }
560f3c1c 5537 putc ('\n', stdout);
e8a64888
AM
5538 printf (_(" Section header string table index: %u"),
5539 header->e_shstrndx);
dda8d76d
NC
5540 if (filedata->section_headers != NULL
5541 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
e8a64888
AM
5542 {
5543 header->e_shstrndx = filedata->section_headers[0].sh_link;
5544 printf (" (%u)", header->e_shstrndx);
5545 }
5546 if (header->e_shstrndx != SHN_UNDEF
5547 && header->e_shstrndx >= header->e_shnum)
5548 {
5549 header->e_shstrndx = SHN_UNDEF;
5550 printf (_(" <corrupt: out of range>"));
5551 }
560f3c1c
AM
5552 putc ('\n', stdout);
5553 }
5554
dda8d76d 5555 if (filedata->section_headers != NULL)
560f3c1c 5556 {
dda8d76d
NC
5557 if (header->e_phnum == PN_XNUM
5558 && filedata->section_headers[0].sh_info != 0)
5559 header->e_phnum = filedata->section_headers[0].sh_info;
5560 if (header->e_shnum == SHN_UNDEF)
5561 header->e_shnum = filedata->section_headers[0].sh_size;
5562 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
5563 header->e_shstrndx = filedata->section_headers[0].sh_link;
9c1ce108 5564 if (header->e_shstrndx >= header->e_shnum)
dda8d76d 5565 header->e_shstrndx = SHN_UNDEF;
252b5132 5566 }
103f02d3 5567
015dc7e1 5568 return true;
9ea033b2
NC
5569}
5570
dda8d76d
NC
5571/* Read in the program headers from FILEDATA and store them in PHEADERS.
5572 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
5573
015dc7e1 5574static bool
dda8d76d 5575get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
9ea033b2 5576{
2cf0635d
NC
5577 Elf32_External_Phdr * phdrs;
5578 Elf32_External_Phdr * external;
5579 Elf_Internal_Phdr * internal;
b34976b6 5580 unsigned int i;
dda8d76d
NC
5581 unsigned int size = filedata->file_header.e_phentsize;
5582 unsigned int num = filedata->file_header.e_phnum;
e0a31db1
NC
5583
5584 /* PR binutils/17531: Cope with unexpected section header sizes. */
5585 if (size == 0 || num == 0)
015dc7e1 5586 return false;
e0a31db1
NC
5587 if (size < sizeof * phdrs)
5588 {
5589 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
015dc7e1 5590 return false;
e0a31db1
NC
5591 }
5592 if (size > sizeof * phdrs)
5593 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
103f02d3 5594
dda8d76d 5595 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
e0a31db1
NC
5596 size, num, _("program headers"));
5597 if (phdrs == NULL)
015dc7e1 5598 return false;
9ea033b2 5599
91d6fa6a 5600 for (i = 0, internal = pheaders, external = phdrs;
dda8d76d 5601 i < filedata->file_header.e_phnum;
b34976b6 5602 i++, internal++, external++)
252b5132 5603 {
9ea033b2
NC
5604 internal->p_type = BYTE_GET (external->p_type);
5605 internal->p_offset = BYTE_GET (external->p_offset);
5606 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5607 internal->p_paddr = BYTE_GET (external->p_paddr);
5608 internal->p_filesz = BYTE_GET (external->p_filesz);
5609 internal->p_memsz = BYTE_GET (external->p_memsz);
5610 internal->p_flags = BYTE_GET (external->p_flags);
5611 internal->p_align = BYTE_GET (external->p_align);
252b5132
RH
5612 }
5613
9ea033b2 5614 free (phdrs);
015dc7e1 5615 return true;
252b5132
RH
5616}
5617
dda8d76d
NC
5618/* Read in the program headers from FILEDATA and store them in PHEADERS.
5619 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
5620
015dc7e1 5621static bool
dda8d76d 5622get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
9ea033b2 5623{
2cf0635d
NC
5624 Elf64_External_Phdr * phdrs;
5625 Elf64_External_Phdr * external;
5626 Elf_Internal_Phdr * internal;
b34976b6 5627 unsigned int i;
dda8d76d
NC
5628 unsigned int size = filedata->file_header.e_phentsize;
5629 unsigned int num = filedata->file_header.e_phnum;
e0a31db1
NC
5630
5631 /* PR binutils/17531: Cope with unexpected section header sizes. */
5632 if (size == 0 || num == 0)
015dc7e1 5633 return false;
e0a31db1
NC
5634 if (size < sizeof * phdrs)
5635 {
5636 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
015dc7e1 5637 return false;
e0a31db1
NC
5638 }
5639 if (size > sizeof * phdrs)
5640 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
103f02d3 5641
dda8d76d 5642 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
e0a31db1 5643 size, num, _("program headers"));
a6e9f9df 5644 if (!phdrs)
015dc7e1 5645 return false;
9ea033b2 5646
91d6fa6a 5647 for (i = 0, internal = pheaders, external = phdrs;
dda8d76d 5648 i < filedata->file_header.e_phnum;
b34976b6 5649 i++, internal++, external++)
9ea033b2
NC
5650 {
5651 internal->p_type = BYTE_GET (external->p_type);
5652 internal->p_flags = BYTE_GET (external->p_flags);
66543521
AM
5653 internal->p_offset = BYTE_GET (external->p_offset);
5654 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5655 internal->p_paddr = BYTE_GET (external->p_paddr);
5656 internal->p_filesz = BYTE_GET (external->p_filesz);
5657 internal->p_memsz = BYTE_GET (external->p_memsz);
5658 internal->p_align = BYTE_GET (external->p_align);
9ea033b2
NC
5659 }
5660
5661 free (phdrs);
015dc7e1 5662 return true;
9ea033b2 5663}
252b5132 5664
32ec8896 5665/* Returns TRUE if the program headers were read into `program_headers'. */
d93f0186 5666
015dc7e1 5667static bool
dda8d76d 5668get_program_headers (Filedata * filedata)
d93f0186 5669{
2cf0635d 5670 Elf_Internal_Phdr * phdrs;
d93f0186
NC
5671
5672 /* Check cache of prior read. */
dda8d76d 5673 if (filedata->program_headers != NULL)
015dc7e1 5674 return true;
d93f0186 5675
82156ab7
NC
5676 /* Be kind to memory checkers by looking for
5677 e_phnum values which we know must be invalid. */
dda8d76d 5678 if (filedata->file_header.e_phnum
82156ab7 5679 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
dda8d76d 5680 >= filedata->file_size)
82156ab7
NC
5681 {
5682 error (_("Too many program headers - %#x - the file is not that big\n"),
dda8d76d 5683 filedata->file_header.e_phnum);
015dc7e1 5684 return false;
82156ab7 5685 }
d93f0186 5686
dda8d76d 5687 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
82156ab7 5688 sizeof (Elf_Internal_Phdr));
d93f0186
NC
5689 if (phdrs == NULL)
5690 {
8b73c356 5691 error (_("Out of memory reading %u program headers\n"),
dda8d76d 5692 filedata->file_header.e_phnum);
015dc7e1 5693 return false;
d93f0186
NC
5694 }
5695
5696 if (is_32bit_elf
dda8d76d
NC
5697 ? get_32bit_program_headers (filedata, phdrs)
5698 : get_64bit_program_headers (filedata, phdrs))
d93f0186 5699 {
dda8d76d 5700 filedata->program_headers = phdrs;
015dc7e1 5701 return true;
d93f0186
NC
5702 }
5703
5704 free (phdrs);
015dc7e1 5705 return false;
d93f0186
NC
5706}
5707
93df3340 5708/* Print program header info and locate dynamic section. */
2f62977e 5709
93df3340 5710static void
dda8d76d 5711process_program_headers (Filedata * filedata)
252b5132 5712{
2cf0635d 5713 Elf_Internal_Phdr * segment;
b34976b6 5714 unsigned int i;
1a9ccd70 5715 Elf_Internal_Phdr * previous_load = NULL;
252b5132 5716
dda8d76d 5717 if (filedata->file_header.e_phnum == 0)
252b5132 5718 {
82f2dbf7 5719 /* PR binutils/12467. */
dda8d76d 5720 if (filedata->file_header.e_phoff != 0)
93df3340
AM
5721 warn (_("possibly corrupt ELF header - it has a non-zero program"
5722 " header offset, but no program headers\n"));
82f2dbf7 5723 else if (do_segments)
ca0e11aa
NC
5724 {
5725 if (filedata->is_separate)
5726 printf (_("\nThere are no program headers in linked file '%s'.\n"),
5727 filedata->file_name);
5728 else
5729 printf (_("\nThere are no program headers in this file.\n"));
5730 }
93df3340 5731 goto no_headers;
252b5132
RH
5732 }
5733
5734 if (do_segments && !do_header)
5735 {
ca0e11aa
NC
5736 if (filedata->is_separate)
5737 printf ("\nIn linked file '%s' the ELF file type is %s\n",
93df3340 5738 filedata->file_name, get_file_type (filedata));
ca0e11aa 5739 else
93df3340 5740 printf (_("\nElf file type is %s\n"), get_file_type (filedata));
dda8d76d 5741 printf (_("Entry point 0x%s\n"), bfd_vmatoa ("x", filedata->file_header.e_entry));
d3a49aa8
AM
5742 printf (ngettext ("There is %d program header, starting at offset %s\n",
5743 "There are %d program headers, starting at offset %s\n",
dda8d76d
NC
5744 filedata->file_header.e_phnum),
5745 filedata->file_header.e_phnum,
5746 bfd_vmatoa ("u", filedata->file_header.e_phoff));
252b5132
RH
5747 }
5748
dda8d76d 5749 if (! get_program_headers (filedata))
93df3340 5750 goto no_headers;
103f02d3 5751
252b5132
RH
5752 if (do_segments)
5753 {
dda8d76d 5754 if (filedata->file_header.e_phnum > 1)
3a1a2036
NC
5755 printf (_("\nProgram Headers:\n"));
5756 else
5757 printf (_("\nProgram Headers:\n"));
76da6bbe 5758
f7a99963
NC
5759 if (is_32bit_elf)
5760 printf
5761 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
d974e256
JJ
5762 else if (do_wide)
5763 printf
5764 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
f7a99963
NC
5765 else
5766 {
5767 printf
5768 (_(" Type Offset VirtAddr PhysAddr\n"));
5769 printf
5770 (_(" FileSiz MemSiz Flags Align\n"));
5771 }
252b5132
RH
5772 }
5773
93df3340
AM
5774 unsigned long dynamic_addr = 0;
5775 bfd_size_type dynamic_size = 0;
dda8d76d
NC
5776 for (i = 0, segment = filedata->program_headers;
5777 i < filedata->file_header.e_phnum;
b34976b6 5778 i++, segment++)
252b5132
RH
5779 {
5780 if (do_segments)
5781 {
dda8d76d 5782 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
f7a99963
NC
5783
5784 if (is_32bit_elf)
5785 {
5786 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5787 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
5788 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
5789 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
5790 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
5791 printf ("%c%c%c ",
5792 (segment->p_flags & PF_R ? 'R' : ' '),
5793 (segment->p_flags & PF_W ? 'W' : ' '),
5794 (segment->p_flags & PF_X ? 'E' : ' '));
5795 printf ("%#lx", (unsigned long) segment->p_align);
5796 }
d974e256
JJ
5797 else if (do_wide)
5798 {
5799 if ((unsigned long) segment->p_offset == segment->p_offset)
5800 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5801 else
5802 {
5803 print_vma (segment->p_offset, FULL_HEX);
5804 putchar (' ');
5805 }
5806
5807 print_vma (segment->p_vaddr, FULL_HEX);
5808 putchar (' ');
5809 print_vma (segment->p_paddr, FULL_HEX);
5810 putchar (' ');
5811
5812 if ((unsigned long) segment->p_filesz == segment->p_filesz)
5813 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
5814 else
5815 {
5816 print_vma (segment->p_filesz, FULL_HEX);
5817 putchar (' ');
5818 }
5819
5820 if ((unsigned long) segment->p_memsz == segment->p_memsz)
5821 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
5822 else
5823 {
f48e6c45 5824 print_vma (segment->p_memsz, FULL_HEX);
d974e256
JJ
5825 }
5826
5827 printf (" %c%c%c ",
5828 (segment->p_flags & PF_R ? 'R' : ' '),
5829 (segment->p_flags & PF_W ? 'W' : ' '),
5830 (segment->p_flags & PF_X ? 'E' : ' '));
5831
5832 if ((unsigned long) segment->p_align == segment->p_align)
5833 printf ("%#lx", (unsigned long) segment->p_align);
5834 else
5835 {
5836 print_vma (segment->p_align, PREFIX_HEX);
5837 }
5838 }
f7a99963
NC
5839 else
5840 {
5841 print_vma (segment->p_offset, FULL_HEX);
5842 putchar (' ');
5843 print_vma (segment->p_vaddr, FULL_HEX);
5844 putchar (' ');
5845 print_vma (segment->p_paddr, FULL_HEX);
5846 printf ("\n ");
5847 print_vma (segment->p_filesz, FULL_HEX);
5848 putchar (' ');
5849 print_vma (segment->p_memsz, FULL_HEX);
5850 printf (" %c%c%c ",
5851 (segment->p_flags & PF_R ? 'R' : ' '),
5852 (segment->p_flags & PF_W ? 'W' : ' '),
5853 (segment->p_flags & PF_X ? 'E' : ' '));
1d262527 5854 print_vma (segment->p_align, PREFIX_HEX);
f7a99963 5855 }
252b5132 5856
1a9ccd70
NC
5857 putc ('\n', stdout);
5858 }
f54498b4 5859
252b5132
RH
5860 switch (segment->p_type)
5861 {
1a9ccd70 5862 case PT_LOAD:
502d895c
NC
5863#if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
5864 required by the ELF standard, several programs, including the Linux
5865 kernel, make use of non-ordered segments. */
1a9ccd70
NC
5866 if (previous_load
5867 && previous_load->p_vaddr > segment->p_vaddr)
5868 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
502d895c 5869#endif
1a9ccd70
NC
5870 if (segment->p_memsz < segment->p_filesz)
5871 error (_("the segment's file size is larger than its memory size\n"));
5872 previous_load = segment;
5873 break;
5874
5875 case PT_PHDR:
5876 /* PR 20815 - Verify that the program header is loaded into memory. */
5877 if (i > 0 && previous_load != NULL)
5878 error (_("the PHDR segment must occur before any LOAD segment\n"));
dda8d76d 5879 if (filedata->file_header.e_machine != EM_PARISC)
1a9ccd70
NC
5880 {
5881 unsigned int j;
5882
dda8d76d 5883 for (j = 1; j < filedata->file_header.e_phnum; j++)
c0c121b0
AM
5884 {
5885 Elf_Internal_Phdr *load = filedata->program_headers + j;
5886 if (load->p_type == PT_LOAD
5887 && load->p_offset <= segment->p_offset
5888 && (load->p_offset + load->p_filesz
5889 >= segment->p_offset + segment->p_filesz)
5890 && load->p_vaddr <= segment->p_vaddr
5891 && (load->p_vaddr + load->p_filesz
5892 >= segment->p_vaddr + segment->p_filesz))
5893 break;
5894 }
dda8d76d 5895 if (j == filedata->file_header.e_phnum)
1a9ccd70
NC
5896 error (_("the PHDR segment is not covered by a LOAD segment\n"));
5897 }
5898 break;
5899
252b5132 5900 case PT_DYNAMIC:
93df3340 5901 if (dynamic_addr)
252b5132
RH
5902 error (_("more than one dynamic segment\n"));
5903
20737c13
AM
5904 /* By default, assume that the .dynamic section is the first
5905 section in the DYNAMIC segment. */
93df3340
AM
5906 dynamic_addr = segment->p_offset;
5907 dynamic_size = segment->p_filesz;
20737c13 5908
b2d38a17
NC
5909 /* Try to locate the .dynamic section. If there is
5910 a section header table, we can easily locate it. */
dda8d76d 5911 if (filedata->section_headers != NULL)
b2d38a17 5912 {
2cf0635d 5913 Elf_Internal_Shdr * sec;
b2d38a17 5914
dda8d76d 5915 sec = find_section (filedata, ".dynamic");
89fac5e3 5916 if (sec == NULL || sec->sh_size == 0)
b2d38a17 5917 {
93df3340
AM
5918 /* A corresponding .dynamic section is expected, but on
5919 IA-64/OpenVMS it is OK for it to be missing. */
5920 if (!is_ia64_vms (filedata))
5921 error (_("no .dynamic section in the dynamic segment\n"));
b2d38a17
NC
5922 break;
5923 }
5924
42bb2e33 5925 if (sec->sh_type == SHT_NOBITS)
20737c13 5926 {
93df3340
AM
5927 dynamic_addr = 0;
5928 dynamic_size = 0;
20737c13
AM
5929 break;
5930 }
42bb2e33 5931
93df3340
AM
5932 dynamic_addr = sec->sh_offset;
5933 dynamic_size = sec->sh_size;
b2d38a17 5934
8ac10c5b
L
5935 /* The PT_DYNAMIC segment, which is used by the run-time
5936 loader, should exactly match the .dynamic section. */
5937 if (do_checks
93df3340
AM
5938 && (dynamic_addr != segment->p_offset
5939 || dynamic_size != segment->p_filesz))
8ac10c5b
L
5940 warn (_("\
5941the .dynamic section is not the same as the dynamic segment\n"));
b2d38a17 5942 }
39e224f6
MW
5943
5944 /* PR binutils/17512: Avoid corrupt dynamic section info in the
5945 segment. Check this after matching against the section headers
5946 so we don't warn on debuginfo file (which have NOBITS .dynamic
5947 sections). */
93df3340
AM
5948 if (dynamic_addr > filedata->file_size
5949 || (dynamic_size > filedata->file_size - dynamic_addr))
39e224f6
MW
5950 {
5951 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
93df3340
AM
5952 dynamic_addr = 0;
5953 dynamic_size = 0;
39e224f6 5954 }
252b5132
RH
5955 break;
5956
5957 case PT_INTERP:
13acb58d
AM
5958 if (segment->p_offset >= filedata->file_size
5959 || segment->p_filesz > filedata->file_size - segment->p_offset
5960 || segment->p_filesz - 1 >= (size_t) -2
5961 || fseek (filedata->handle,
5962 filedata->archive_file_offset + (long) segment->p_offset,
5963 SEEK_SET))
252b5132
RH
5964 error (_("Unable to find program interpreter name\n"));
5965 else
5966 {
13acb58d
AM
5967 size_t len = segment->p_filesz;
5968 free (filedata->program_interpreter);
5969 filedata->program_interpreter = xmalloc (len + 1);
5970 len = fread (filedata->program_interpreter, 1, len,
5971 filedata->handle);
5972 filedata->program_interpreter[len] = 0;
252b5132
RH
5973
5974 if (do_segments)
f54498b4 5975 printf (_(" [Requesting program interpreter: %s]\n"),
978c4450 5976 filedata->program_interpreter);
252b5132
RH
5977 }
5978 break;
5979 }
252b5132
RH
5980 }
5981
dda8d76d
NC
5982 if (do_segments
5983 && filedata->section_headers != NULL
5984 && filedata->string_table != NULL)
252b5132
RH
5985 {
5986 printf (_("\n Section to Segment mapping:\n"));
5987 printf (_(" Segment Sections...\n"));
5988
dda8d76d 5989 for (i = 0; i < filedata->file_header.e_phnum; i++)
252b5132 5990 {
9ad5cbcf 5991 unsigned int j;
2cf0635d 5992 Elf_Internal_Shdr * section;
252b5132 5993
dda8d76d
NC
5994 segment = filedata->program_headers + i;
5995 section = filedata->section_headers + 1;
252b5132
RH
5996
5997 printf (" %2.2d ", i);
5998
dda8d76d 5999 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
252b5132 6000 {
f4638467
AM
6001 if (!ELF_TBSS_SPECIAL (section, segment)
6002 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
dda8d76d 6003 printf ("%s ", printable_section_name (filedata, section));
252b5132
RH
6004 }
6005
6006 putc ('\n',stdout);
6007 }
6008 }
6009
93df3340
AM
6010 filedata->dynamic_addr = dynamic_addr;
6011 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
6012 return;
6013
6014 no_headers:
6015 filedata->dynamic_addr = 0;
6016 filedata->dynamic_size = 1;
252b5132
RH
6017}
6018
6019
d93f0186
NC
6020/* Find the file offset corresponding to VMA by using the program headers. */
6021
6022static long
dda8d76d 6023offset_from_vma (Filedata * filedata, bfd_vma vma, bfd_size_type size)
d93f0186 6024{
2cf0635d 6025 Elf_Internal_Phdr * seg;
d93f0186 6026
dda8d76d 6027 if (! get_program_headers (filedata))
d93f0186
NC
6028 {
6029 warn (_("Cannot interpret virtual addresses without program headers.\n"));
6030 return (long) vma;
6031 }
6032
dda8d76d
NC
6033 for (seg = filedata->program_headers;
6034 seg < filedata->program_headers + filedata->file_header.e_phnum;
d93f0186
NC
6035 ++seg)
6036 {
6037 if (seg->p_type != PT_LOAD)
6038 continue;
6039
6040 if (vma >= (seg->p_vaddr & -seg->p_align)
6041 && vma + size <= seg->p_vaddr + seg->p_filesz)
6042 return vma - seg->p_vaddr + seg->p_offset;
6043 }
6044
6045 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
0af1713e 6046 (unsigned long) vma);
d93f0186
NC
6047 return (long) vma;
6048}
6049
6050
dda8d76d
NC
6051/* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
6052 If PROBE is true, this is just a probe and we do not generate any error
6053 messages if the load fails. */
049b0c3a 6054
015dc7e1
AM
6055static bool
6056get_32bit_section_headers (Filedata * filedata, bool probe)
252b5132 6057{
2cf0635d
NC
6058 Elf32_External_Shdr * shdrs;
6059 Elf_Internal_Shdr * internal;
dda8d76d
NC
6060 unsigned int i;
6061 unsigned int size = filedata->file_header.e_shentsize;
6062 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
049b0c3a
NC
6063
6064 /* PR binutils/17531: Cope with unexpected section header sizes. */
6065 if (size == 0 || num == 0)
015dc7e1 6066 return false;
049b0c3a
NC
6067 if (size < sizeof * shdrs)
6068 {
6069 if (! probe)
6070 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
015dc7e1 6071 return false;
049b0c3a
NC
6072 }
6073 if (!probe && size > sizeof * shdrs)
6074 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
252b5132 6075
dda8d76d 6076 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
049b0c3a
NC
6077 size, num,
6078 probe ? NULL : _("section headers"));
6079 if (shdrs == NULL)
015dc7e1 6080 return false;
252b5132 6081
dda8d76d
NC
6082 filedata->section_headers = (Elf_Internal_Shdr *)
6083 cmalloc (num, sizeof (Elf_Internal_Shdr));
6084 if (filedata->section_headers == NULL)
252b5132 6085 {
049b0c3a 6086 if (!probe)
8b73c356 6087 error (_("Out of memory reading %u section headers\n"), num);
e3d39609 6088 free (shdrs);
015dc7e1 6089 return false;
252b5132
RH
6090 }
6091
dda8d76d 6092 for (i = 0, internal = filedata->section_headers;
560f3c1c 6093 i < num;
b34976b6 6094 i++, internal++)
252b5132
RH
6095 {
6096 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6097 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6098 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6099 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6100 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6101 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6102 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6103 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6104 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6105 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
315350be
NC
6106 if (!probe && internal->sh_link > num)
6107 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6108 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6109 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
252b5132
RH
6110 }
6111
6112 free (shdrs);
015dc7e1 6113 return true;
252b5132
RH
6114}
6115
dda8d76d
NC
6116/* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
6117
015dc7e1
AM
6118static bool
6119get_64bit_section_headers (Filedata * filedata, bool probe)
9ea033b2 6120{
dda8d76d
NC
6121 Elf64_External_Shdr * shdrs;
6122 Elf_Internal_Shdr * internal;
6123 unsigned int i;
6124 unsigned int size = filedata->file_header.e_shentsize;
6125 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
049b0c3a
NC
6126
6127 /* PR binutils/17531: Cope with unexpected section header sizes. */
6128 if (size == 0 || num == 0)
015dc7e1 6129 return false;
dda8d76d 6130
049b0c3a
NC
6131 if (size < sizeof * shdrs)
6132 {
6133 if (! probe)
6134 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
015dc7e1 6135 return false;
049b0c3a 6136 }
dda8d76d 6137
049b0c3a
NC
6138 if (! probe && size > sizeof * shdrs)
6139 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
9ea033b2 6140
dda8d76d
NC
6141 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
6142 filedata->file_header.e_shoff,
049b0c3a
NC
6143 size, num,
6144 probe ? NULL : _("section headers"));
6145 if (shdrs == NULL)
015dc7e1 6146 return false;
9ea033b2 6147
dda8d76d
NC
6148 filedata->section_headers = (Elf_Internal_Shdr *)
6149 cmalloc (num, sizeof (Elf_Internal_Shdr));
6150 if (filedata->section_headers == NULL)
9ea033b2 6151 {
049b0c3a 6152 if (! probe)
8b73c356 6153 error (_("Out of memory reading %u section headers\n"), num);
e3d39609 6154 free (shdrs);
015dc7e1 6155 return false;
9ea033b2
NC
6156 }
6157
dda8d76d 6158 for (i = 0, internal = filedata->section_headers;
560f3c1c 6159 i < num;
b34976b6 6160 i++, internal++)
9ea033b2
NC
6161 {
6162 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6163 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
66543521
AM
6164 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6165 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6166 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6167 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
9ea033b2
NC
6168 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6169 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6170 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6171 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
315350be
NC
6172 if (!probe && internal->sh_link > num)
6173 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6174 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6175 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
9ea033b2
NC
6176 }
6177
6178 free (shdrs);
015dc7e1 6179 return true;
9ea033b2
NC
6180}
6181
4de91c10
AM
6182static bool
6183get_section_headers (Filedata *filedata, bool probe)
6184{
6185 if (filedata->section_headers != NULL)
6186 return true;
6187
4de91c10
AM
6188 if (is_32bit_elf)
6189 return get_32bit_section_headers (filedata, probe);
6190 else
6191 return get_64bit_section_headers (filedata, probe);
6192}
6193
252b5132 6194static Elf_Internal_Sym *
dda8d76d
NC
6195get_32bit_elf_symbols (Filedata * filedata,
6196 Elf_Internal_Shdr * section,
6197 unsigned long * num_syms_return)
252b5132 6198{
ba5cdace 6199 unsigned long number = 0;
dd24e3da 6200 Elf32_External_Sym * esyms = NULL;
ba5cdace 6201 Elf_External_Sym_Shndx * shndx = NULL;
dd24e3da 6202 Elf_Internal_Sym * isyms = NULL;
2cf0635d 6203 Elf_Internal_Sym * psym;
b34976b6 6204 unsigned int j;
e3d39609 6205 elf_section_list * entry;
252b5132 6206
c9c1d674
EG
6207 if (section->sh_size == 0)
6208 {
6209 if (num_syms_return != NULL)
6210 * num_syms_return = 0;
6211 return NULL;
6212 }
6213
dd24e3da 6214 /* Run some sanity checks first. */
c9c1d674 6215 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
dd24e3da 6216 {
c9c1d674 6217 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
dda8d76d
NC
6218 printable_section_name (filedata, section),
6219 (unsigned long) section->sh_entsize);
ba5cdace 6220 goto exit_point;
dd24e3da
NC
6221 }
6222
dda8d76d 6223 if (section->sh_size > filedata->file_size)
f54498b4
NC
6224 {
6225 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
dda8d76d
NC
6226 printable_section_name (filedata, section),
6227 (unsigned long) section->sh_size);
f54498b4
NC
6228 goto exit_point;
6229 }
6230
dd24e3da
NC
6231 number = section->sh_size / section->sh_entsize;
6232
6233 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
6234 {
c9c1d674 6235 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
8066deb1 6236 (unsigned long) section->sh_size,
dda8d76d 6237 printable_section_name (filedata, section),
8066deb1 6238 (unsigned long) section->sh_entsize);
ba5cdace 6239 goto exit_point;
dd24e3da
NC
6240 }
6241
dda8d76d 6242 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
3f5e193b 6243 section->sh_size, _("symbols"));
dd24e3da 6244 if (esyms == NULL)
ba5cdace 6245 goto exit_point;
252b5132 6246
e3d39609 6247 shndx = NULL;
978c4450 6248 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
e3d39609
NC
6249 {
6250 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6251 continue;
6252
6253 if (shndx != NULL)
6254 {
6255 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6256 free (shndx);
6257 }
6258
6259 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6260 entry->hdr->sh_offset,
6261 1, entry->hdr->sh_size,
6262 _("symbol table section indices"));
6263 if (shndx == NULL)
6264 goto exit_point;
6265
6266 /* PR17531: file: heap-buffer-overflow */
6267 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6268 {
6269 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6270 printable_section_name (filedata, entry->hdr),
6271 (unsigned long) entry->hdr->sh_size,
6272 (unsigned long) section->sh_size);
6273 goto exit_point;
c9c1d674 6274 }
e3d39609 6275 }
9ad5cbcf 6276
3f5e193b 6277 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
252b5132
RH
6278
6279 if (isyms == NULL)
6280 {
8b73c356
NC
6281 error (_("Out of memory reading %lu symbols\n"),
6282 (unsigned long) number);
dd24e3da 6283 goto exit_point;
252b5132
RH
6284 }
6285
dd24e3da 6286 for (j = 0, psym = isyms; j < number; j++, psym++)
252b5132
RH
6287 {
6288 psym->st_name = BYTE_GET (esyms[j].st_name);
6289 psym->st_value = BYTE_GET (esyms[j].st_value);
6290 psym->st_size = BYTE_GET (esyms[j].st_size);
6291 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
4fbb74a6 6292 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
9ad5cbcf
AM
6293 psym->st_shndx
6294 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
4fbb74a6
AM
6295 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6296 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
252b5132
RH
6297 psym->st_info = BYTE_GET (esyms[j].st_info);
6298 psym->st_other = BYTE_GET (esyms[j].st_other);
6299 }
6300
dd24e3da 6301 exit_point:
e3d39609
NC
6302 free (shndx);
6303 free (esyms);
252b5132 6304
ba5cdace
NC
6305 if (num_syms_return != NULL)
6306 * num_syms_return = isyms == NULL ? 0 : number;
6307
252b5132
RH
6308 return isyms;
6309}
6310
9ea033b2 6311static Elf_Internal_Sym *
dda8d76d
NC
6312get_64bit_elf_symbols (Filedata * filedata,
6313 Elf_Internal_Shdr * section,
6314 unsigned long * num_syms_return)
9ea033b2 6315{
ba5cdace
NC
6316 unsigned long number = 0;
6317 Elf64_External_Sym * esyms = NULL;
6318 Elf_External_Sym_Shndx * shndx = NULL;
6319 Elf_Internal_Sym * isyms = NULL;
2cf0635d 6320 Elf_Internal_Sym * psym;
b34976b6 6321 unsigned int j;
e3d39609 6322 elf_section_list * entry;
9ea033b2 6323
c9c1d674
EG
6324 if (section->sh_size == 0)
6325 {
6326 if (num_syms_return != NULL)
6327 * num_syms_return = 0;
6328 return NULL;
6329 }
6330
dd24e3da 6331 /* Run some sanity checks first. */
c9c1d674 6332 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
dd24e3da 6333 {
c9c1d674 6334 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
dda8d76d 6335 printable_section_name (filedata, section),
8066deb1 6336 (unsigned long) section->sh_entsize);
ba5cdace 6337 goto exit_point;
dd24e3da
NC
6338 }
6339
dda8d76d 6340 if (section->sh_size > filedata->file_size)
f54498b4
NC
6341 {
6342 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
dda8d76d 6343 printable_section_name (filedata, section),
8066deb1 6344 (unsigned long) section->sh_size);
f54498b4
NC
6345 goto exit_point;
6346 }
6347
dd24e3da
NC
6348 number = section->sh_size / section->sh_entsize;
6349
6350 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
6351 {
c9c1d674 6352 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
8066deb1 6353 (unsigned long) section->sh_size,
dda8d76d 6354 printable_section_name (filedata, section),
8066deb1 6355 (unsigned long) section->sh_entsize);
ba5cdace 6356 goto exit_point;
dd24e3da
NC
6357 }
6358
dda8d76d 6359 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
3f5e193b 6360 section->sh_size, _("symbols"));
a6e9f9df 6361 if (!esyms)
ba5cdace 6362 goto exit_point;
9ea033b2 6363
e3d39609 6364 shndx = NULL;
978c4450 6365 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
e3d39609
NC
6366 {
6367 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6368 continue;
6369
6370 if (shndx != NULL)
6371 {
6372 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6373 free (shndx);
c9c1d674 6374 }
e3d39609
NC
6375
6376 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6377 entry->hdr->sh_offset,
6378 1, entry->hdr->sh_size,
6379 _("symbol table section indices"));
6380 if (shndx == NULL)
6381 goto exit_point;
6382
6383 /* PR17531: file: heap-buffer-overflow */
6384 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6385 {
6386 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6387 printable_section_name (filedata, entry->hdr),
6388 (unsigned long) entry->hdr->sh_size,
6389 (unsigned long) section->sh_size);
6390 goto exit_point;
6391 }
6392 }
9ad5cbcf 6393
3f5e193b 6394 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
9ea033b2
NC
6395
6396 if (isyms == NULL)
6397 {
8b73c356
NC
6398 error (_("Out of memory reading %lu symbols\n"),
6399 (unsigned long) number);
ba5cdace 6400 goto exit_point;
9ea033b2
NC
6401 }
6402
ba5cdace 6403 for (j = 0, psym = isyms; j < number; j++, psym++)
9ea033b2
NC
6404 {
6405 psym->st_name = BYTE_GET (esyms[j].st_name);
6406 psym->st_info = BYTE_GET (esyms[j].st_info);
6407 psym->st_other = BYTE_GET (esyms[j].st_other);
6408 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
ba5cdace 6409
4fbb74a6 6410 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
9ad5cbcf
AM
6411 psym->st_shndx
6412 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
4fbb74a6
AM
6413 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6414 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
ba5cdace 6415
66543521
AM
6416 psym->st_value = BYTE_GET (esyms[j].st_value);
6417 psym->st_size = BYTE_GET (esyms[j].st_size);
9ea033b2
NC
6418 }
6419
ba5cdace 6420 exit_point:
e3d39609
NC
6421 free (shndx);
6422 free (esyms);
ba5cdace
NC
6423
6424 if (num_syms_return != NULL)
6425 * num_syms_return = isyms == NULL ? 0 : number;
9ea033b2
NC
6426
6427 return isyms;
6428}
6429
4de91c10
AM
6430static Elf_Internal_Sym *
6431get_elf_symbols (Filedata *filedata,
6432 Elf_Internal_Shdr *section,
6433 unsigned long *num_syms_return)
6434{
6435 if (is_32bit_elf)
6436 return get_32bit_elf_symbols (filedata, section, num_syms_return);
6437 else
6438 return get_64bit_elf_symbols (filedata, section, num_syms_return);
6439}
6440
d1133906 6441static const char *
dda8d76d 6442get_elf_section_flags (Filedata * filedata, bfd_vma sh_flags)
d1133906 6443{
5477e8a0 6444 static char buff[1024];
2cf0635d 6445 char * p = buff;
32ec8896
NC
6446 unsigned int field_size = is_32bit_elf ? 8 : 16;
6447 signed int sindex;
6448 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
8d5ff12c
L
6449 bfd_vma os_flags = 0;
6450 bfd_vma proc_flags = 0;
6451 bfd_vma unknown_flags = 0;
148b93f2 6452 static const struct
5477e8a0 6453 {
2cf0635d 6454 const char * str;
32ec8896 6455 unsigned int len;
5477e8a0
L
6456 }
6457 flags [] =
6458 {
cfcac11d
NC
6459 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
6460 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
6461 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
6462 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
6463 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
6464 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
6465 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
6466 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
6467 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
6468 /* 9 */ { STRING_COMMA_LEN ("TLS") },
6469 /* IA-64 specific. */
6470 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
6471 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
6472 /* IA-64 OpenVMS specific. */
6473 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
6474 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
6475 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
6476 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
6477 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
6478 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
18ae9cc1 6479 /* Generic. */
cfcac11d 6480 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
18ae9cc1 6481 /* SPARC specific. */
77115a4a 6482 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
ac4c9b04
MG
6483 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
6484 /* ARM specific. */
6485 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
f0728ee3 6486 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
a91e1603
L
6487 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
6488 /* GNU specific. */
6489 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
83eef883
AFB
6490 /* VLE specific. */
6491 /* 25 */ { STRING_COMMA_LEN ("VLE") },
99fabbc9
JL
6492 /* GNU specific. */
6493 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
5477e8a0
L
6494 };
6495
6496 if (do_section_details)
6497 {
8d5ff12c
L
6498 sprintf (buff, "[%*.*lx]: ",
6499 field_size, field_size, (unsigned long) sh_flags);
6500 p += field_size + 4;
5477e8a0 6501 }
76da6bbe 6502
d1133906
NC
6503 while (sh_flags)
6504 {
6505 bfd_vma flag;
6506
6507 flag = sh_flags & - sh_flags;
6508 sh_flags &= ~ flag;
76da6bbe 6509
5477e8a0 6510 if (do_section_details)
d1133906 6511 {
5477e8a0
L
6512 switch (flag)
6513 {
91d6fa6a
NC
6514 case SHF_WRITE: sindex = 0; break;
6515 case SHF_ALLOC: sindex = 1; break;
6516 case SHF_EXECINSTR: sindex = 2; break;
6517 case SHF_MERGE: sindex = 3; break;
6518 case SHF_STRINGS: sindex = 4; break;
6519 case SHF_INFO_LINK: sindex = 5; break;
6520 case SHF_LINK_ORDER: sindex = 6; break;
6521 case SHF_OS_NONCONFORMING: sindex = 7; break;
6522 case SHF_GROUP: sindex = 8; break;
6523 case SHF_TLS: sindex = 9; break;
18ae9cc1 6524 case SHF_EXCLUDE: sindex = 18; break;
77115a4a 6525 case SHF_COMPRESSED: sindex = 20; break;
76da6bbe 6526
5477e8a0 6527 default:
91d6fa6a 6528 sindex = -1;
dda8d76d 6529 switch (filedata->file_header.e_machine)
148b93f2 6530 {
cfcac11d 6531 case EM_IA_64:
148b93f2 6532 if (flag == SHF_IA_64_SHORT)
91d6fa6a 6533 sindex = 10;
148b93f2 6534 else if (flag == SHF_IA_64_NORECOV)
91d6fa6a 6535 sindex = 11;
148b93f2 6536#ifdef BFD64
dda8d76d 6537 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
148b93f2
NC
6538 switch (flag)
6539 {
91d6fa6a
NC
6540 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
6541 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
6542 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
6543 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
6544 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
6545 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
148b93f2
NC
6546 default: break;
6547 }
6548#endif
cfcac11d
NC
6549 break;
6550
caa83f8b 6551 case EM_386:
22abe556 6552 case EM_IAMCU:
caa83f8b 6553 case EM_X86_64:
7f502d6c 6554 case EM_L1OM:
7a9068fe 6555 case EM_K1OM:
cfcac11d
NC
6556 case EM_OLD_SPARCV9:
6557 case EM_SPARC32PLUS:
6558 case EM_SPARCV9:
6559 case EM_SPARC:
18ae9cc1 6560 if (flag == SHF_ORDERED)
91d6fa6a 6561 sindex = 19;
cfcac11d 6562 break;
ac4c9b04
MG
6563
6564 case EM_ARM:
6565 switch (flag)
6566 {
6567 case SHF_ENTRYSECT: sindex = 21; break;
f0728ee3 6568 case SHF_ARM_PURECODE: sindex = 22; break;
ac4c9b04
MG
6569 case SHF_COMDEF: sindex = 23; break;
6570 default: break;
6571 }
6572 break;
83eef883
AFB
6573 case EM_PPC:
6574 if (flag == SHF_PPC_VLE)
6575 sindex = 25;
6576 break;
99fabbc9
JL
6577 default:
6578 break;
6579 }
ac4c9b04 6580
99fabbc9
JL
6581 switch (filedata->file_header.e_ident[EI_OSABI])
6582 {
6583 case ELFOSABI_GNU:
6584 case ELFOSABI_FREEBSD:
6585 if (flag == SHF_GNU_RETAIN)
6586 sindex = 26;
6587 /* Fall through */
6588 case ELFOSABI_NONE:
6589 if (flag == SHF_GNU_MBIND)
6590 /* We should not recognize SHF_GNU_MBIND for
6591 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6592 not set the EI_OSABI header byte. */
6593 sindex = 24;
6594 break;
cfcac11d
NC
6595 default:
6596 break;
148b93f2 6597 }
99fabbc9 6598 break;
5477e8a0
L
6599 }
6600
91d6fa6a 6601 if (sindex != -1)
5477e8a0 6602 {
8d5ff12c
L
6603 if (p != buff + field_size + 4)
6604 {
6605 if (size < (10 + 2))
bee0ee85
NC
6606 {
6607 warn (_("Internal error: not enough buffer room for section flag info"));
6608 return _("<unknown>");
6609 }
8d5ff12c
L
6610 size -= 2;
6611 *p++ = ',';
6612 *p++ = ' ';
6613 }
6614
91d6fa6a
NC
6615 size -= flags [sindex].len;
6616 p = stpcpy (p, flags [sindex].str);
5477e8a0 6617 }
3b22753a 6618 else if (flag & SHF_MASKOS)
8d5ff12c 6619 os_flags |= flag;
d1133906 6620 else if (flag & SHF_MASKPROC)
8d5ff12c 6621 proc_flags |= flag;
d1133906 6622 else
8d5ff12c 6623 unknown_flags |= flag;
5477e8a0
L
6624 }
6625 else
6626 {
6627 switch (flag)
6628 {
6629 case SHF_WRITE: *p = 'W'; break;
6630 case SHF_ALLOC: *p = 'A'; break;
6631 case SHF_EXECINSTR: *p = 'X'; break;
6632 case SHF_MERGE: *p = 'M'; break;
6633 case SHF_STRINGS: *p = 'S'; break;
6634 case SHF_INFO_LINK: *p = 'I'; break;
6635 case SHF_LINK_ORDER: *p = 'L'; break;
6636 case SHF_OS_NONCONFORMING: *p = 'O'; break;
6637 case SHF_GROUP: *p = 'G'; break;
6638 case SHF_TLS: *p = 'T'; break;
18ae9cc1 6639 case SHF_EXCLUDE: *p = 'E'; break;
77115a4a 6640 case SHF_COMPRESSED: *p = 'C'; break;
5477e8a0
L
6641
6642 default:
dda8d76d
NC
6643 if ((filedata->file_header.e_machine == EM_X86_64
6644 || filedata->file_header.e_machine == EM_L1OM
6645 || filedata->file_header.e_machine == EM_K1OM)
5477e8a0
L
6646 && flag == SHF_X86_64_LARGE)
6647 *p = 'l';
dda8d76d 6648 else if (filedata->file_header.e_machine == EM_ARM
f0728ee3 6649 && flag == SHF_ARM_PURECODE)
99fabbc9 6650 *p = 'y';
dda8d76d 6651 else if (filedata->file_header.e_machine == EM_PPC
83eef883 6652 && flag == SHF_PPC_VLE)
99fabbc9 6653 *p = 'v';
5477e8a0
L
6654 else if (flag & SHF_MASKOS)
6655 {
99fabbc9
JL
6656 switch (filedata->file_header.e_ident[EI_OSABI])
6657 {
6658 case ELFOSABI_GNU:
6659 case ELFOSABI_FREEBSD:
6660 if (flag == SHF_GNU_RETAIN)
6661 {
6662 *p = 'R';
6663 break;
6664 }
6665 /* Fall through */
6666 case ELFOSABI_NONE:
6667 if (flag == SHF_GNU_MBIND)
6668 {
6669 /* We should not recognize SHF_GNU_MBIND for
6670 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6671 not set the EI_OSABI header byte. */
6672 *p = 'D';
6673 break;
6674 }
6675 /* Fall through */
6676 default:
6677 *p = 'o';
6678 sh_flags &= ~SHF_MASKOS;
6679 break;
6680 }
5477e8a0
L
6681 }
6682 else if (flag & SHF_MASKPROC)
6683 {
6684 *p = 'p';
6685 sh_flags &= ~ SHF_MASKPROC;
6686 }
6687 else
6688 *p = 'x';
6689 break;
6690 }
6691 p++;
d1133906
NC
6692 }
6693 }
76da6bbe 6694
8d5ff12c
L
6695 if (do_section_details)
6696 {
6697 if (os_flags)
6698 {
6699 size -= 5 + field_size;
6700 if (p != buff + field_size + 4)
6701 {
6702 if (size < (2 + 1))
bee0ee85
NC
6703 {
6704 warn (_("Internal error: not enough buffer room for section flag info"));
6705 return _("<unknown>");
6706 }
8d5ff12c
L
6707 size -= 2;
6708 *p++ = ',';
6709 *p++ = ' ';
6710 }
6711 sprintf (p, "OS (%*.*lx)", field_size, field_size,
6712 (unsigned long) os_flags);
6713 p += 5 + field_size;
6714 }
6715 if (proc_flags)
6716 {
6717 size -= 7 + field_size;
6718 if (p != buff + field_size + 4)
6719 {
6720 if (size < (2 + 1))
bee0ee85
NC
6721 {
6722 warn (_("Internal error: not enough buffer room for section flag info"));
6723 return _("<unknown>");
6724 }
8d5ff12c
L
6725 size -= 2;
6726 *p++ = ',';
6727 *p++ = ' ';
6728 }
6729 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
6730 (unsigned long) proc_flags);
6731 p += 7 + field_size;
6732 }
6733 if (unknown_flags)
6734 {
6735 size -= 10 + field_size;
6736 if (p != buff + field_size + 4)
6737 {
6738 if (size < (2 + 1))
bee0ee85
NC
6739 {
6740 warn (_("Internal error: not enough buffer room for section flag info"));
6741 return _("<unknown>");
6742 }
8d5ff12c
L
6743 size -= 2;
6744 *p++ = ',';
6745 *p++ = ' ';
6746 }
2b692964 6747 sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
8d5ff12c
L
6748 (unsigned long) unknown_flags);
6749 p += 10 + field_size;
6750 }
6751 }
6752
e9e44622 6753 *p = '\0';
d1133906
NC
6754 return buff;
6755}
6756
5844b465 6757static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
ebdf1ebf 6758get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf, bfd_size_type size)
77115a4a
L
6759{
6760 if (is_32bit_elf)
6761 {
6762 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
d8024a91 6763
ebdf1ebf
NC
6764 if (size < sizeof (* echdr))
6765 {
6766 error (_("Compressed section is too small even for a compression header\n"));
6767 return 0;
6768 }
6769
77115a4a
L
6770 chdr->ch_type = BYTE_GET (echdr->ch_type);
6771 chdr->ch_size = BYTE_GET (echdr->ch_size);
6772 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6773 return sizeof (*echdr);
6774 }
6775 else
6776 {
6777 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
d8024a91 6778
ebdf1ebf
NC
6779 if (size < sizeof (* echdr))
6780 {
6781 error (_("Compressed section is too small even for a compression header\n"));
6782 return 0;
6783 }
6784
77115a4a
L
6785 chdr->ch_type = BYTE_GET (echdr->ch_type);
6786 chdr->ch_size = BYTE_GET (echdr->ch_size);
6787 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6788 return sizeof (*echdr);
6789 }
6790}
6791
015dc7e1 6792static bool
dda8d76d 6793process_section_headers (Filedata * filedata)
252b5132 6794{
2cf0635d 6795 Elf_Internal_Shdr * section;
b34976b6 6796 unsigned int i;
252b5132 6797
dda8d76d 6798 if (filedata->file_header.e_shnum == 0)
252b5132 6799 {
82f2dbf7 6800 /* PR binutils/12467. */
dda8d76d 6801 if (filedata->file_header.e_shoff != 0)
32ec8896
NC
6802 {
6803 warn (_("possibly corrupt ELF file header - it has a non-zero"
6804 " section header offset, but no section headers\n"));
015dc7e1 6805 return false;
32ec8896 6806 }
82f2dbf7 6807 else if (do_sections)
252b5132
RH
6808 printf (_("\nThere are no sections in this file.\n"));
6809
015dc7e1 6810 return true;
252b5132
RH
6811 }
6812
6813 if (do_sections && !do_header)
ca0e11aa
NC
6814 {
6815 if (filedata->is_separate && process_links)
6816 printf (_("In linked file '%s': "), filedata->file_name);
6817 if (! filedata->is_separate || process_links)
6818 printf (ngettext ("There is %d section header, "
6819 "starting at offset 0x%lx:\n",
6820 "There are %d section headers, "
6821 "starting at offset 0x%lx:\n",
6822 filedata->file_header.e_shnum),
6823 filedata->file_header.e_shnum,
6824 (unsigned long) filedata->file_header.e_shoff);
6825 }
252b5132 6826
4de91c10
AM
6827 if (!get_section_headers (filedata, false))
6828 return false;
252b5132
RH
6829
6830 /* Read in the string table, so that we have names to display. */
dda8d76d
NC
6831 if (filedata->file_header.e_shstrndx != SHN_UNDEF
6832 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
252b5132 6833 {
dda8d76d 6834 section = filedata->section_headers + filedata->file_header.e_shstrndx;
d40ac9bd 6835
c256ffe7
JJ
6836 if (section->sh_size != 0)
6837 {
dda8d76d
NC
6838 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
6839 1, section->sh_size,
6840 _("string table"));
0de14b54 6841
dda8d76d 6842 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
c256ffe7 6843 }
252b5132
RH
6844 }
6845
6846 /* Scan the sections for the dynamic symbol table
e3c8793a 6847 and dynamic string table and debug sections. */
89fac5e3 6848 eh_addr_size = is_32bit_elf ? 4 : 8;
dda8d76d 6849 switch (filedata->file_header.e_machine)
89fac5e3
RS
6850 {
6851 case EM_MIPS:
6852 case EM_MIPS_RS3_LE:
6853 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
6854 FDE addresses. However, the ABI also has a semi-official ILP32
6855 variant for which the normal FDE address size rules apply.
6856
6857 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
6858 section, where XX is the size of longs in bits. Unfortunately,
6859 earlier compilers provided no way of distinguishing ILP32 objects
6860 from LP64 objects, so if there's any doubt, we should assume that
6861 the official LP64 form is being used. */
dda8d76d
NC
6862 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
6863 && find_section (filedata, ".gcc_compiled_long32") == NULL)
89fac5e3
RS
6864 eh_addr_size = 8;
6865 break;
0f56a26a
DD
6866
6867 case EM_H8_300:
6868 case EM_H8_300H:
dda8d76d 6869 switch (filedata->file_header.e_flags & EF_H8_MACH)
0f56a26a
DD
6870 {
6871 case E_H8_MACH_H8300:
6872 case E_H8_MACH_H8300HN:
6873 case E_H8_MACH_H8300SN:
6874 case E_H8_MACH_H8300SXN:
6875 eh_addr_size = 2;
6876 break;
6877 case E_H8_MACH_H8300H:
6878 case E_H8_MACH_H8300S:
6879 case E_H8_MACH_H8300SX:
6880 eh_addr_size = 4;
6881 break;
6882 }
f4236fe4
DD
6883 break;
6884
ff7eeb89 6885 case EM_M32C_OLD:
f4236fe4 6886 case EM_M32C:
dda8d76d 6887 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
f4236fe4
DD
6888 {
6889 case EF_M32C_CPU_M16C:
6890 eh_addr_size = 2;
6891 break;
6892 }
6893 break;
89fac5e3
RS
6894 }
6895
76ca31c0
NC
6896#define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
6897 do \
6898 { \
6899 bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64; \
6900 if (section->sh_entsize != expected_entsize) \
9dd3a467 6901 { \
76ca31c0
NC
6902 char buf[40]; \
6903 sprintf_vma (buf, section->sh_entsize); \
6904 /* Note: coded this way so that there is a single string for \
6905 translation. */ \
6906 error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
6907 error (_("(Using the expected size of %u for the rest of this dump)\n"), \
6908 (unsigned) expected_entsize); \
9dd3a467 6909 section->sh_entsize = expected_entsize; \
76ca31c0
NC
6910 } \
6911 } \
08d8fa11 6912 while (0)
9dd3a467
NC
6913
6914#define CHECK_ENTSIZE(section, i, type) \
1b513401 6915 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
08d8fa11
JJ
6916 sizeof (Elf64_External_##type))
6917
dda8d76d
NC
6918 for (i = 0, section = filedata->section_headers;
6919 i < filedata->file_header.e_shnum;
b34976b6 6920 i++, section++)
252b5132 6921 {
84714f86 6922 const char *name = section_name_print (filedata, section);
252b5132 6923
1b513401
NC
6924 /* Run some sanity checks on the headers and
6925 possibly fill in some file data as well. */
6926 switch (section->sh_type)
252b5132 6927 {
1b513401 6928 case SHT_DYNSYM:
978c4450 6929 if (filedata->dynamic_symbols != NULL)
252b5132
RH
6930 {
6931 error (_("File contains multiple dynamic symbol tables\n"));
6932 continue;
6933 }
6934
08d8fa11 6935 CHECK_ENTSIZE (section, i, Sym);
978c4450 6936 filedata->dynamic_symbols
4de91c10 6937 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
8ac10c5b 6938 filedata->dynamic_symtab_section = section;
1b513401
NC
6939 break;
6940
6941 case SHT_STRTAB:
6942 if (streq (name, ".dynstr"))
252b5132 6943 {
1b513401
NC
6944 if (filedata->dynamic_strings != NULL)
6945 {
6946 error (_("File contains multiple dynamic string tables\n"));
6947 continue;
6948 }
6949
6950 filedata->dynamic_strings
6951 = (char *) get_data (NULL, filedata, section->sh_offset,
6952 1, section->sh_size, _("dynamic strings"));
6953 filedata->dynamic_strings_length
6954 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
8ac10c5b 6955 filedata->dynamic_strtab_section = section;
252b5132 6956 }
1b513401
NC
6957 break;
6958
6959 case SHT_SYMTAB_SHNDX:
6960 {
6961 elf_section_list * entry = xmalloc (sizeof * entry);
6962
6963 entry->hdr = section;
6964 entry->next = filedata->symtab_shndx_list;
6965 filedata->symtab_shndx_list = entry;
6966 }
6967 break;
6968
6969 case SHT_SYMTAB:
6970 CHECK_ENTSIZE (section, i, Sym);
6971 break;
6972
6973 case SHT_GROUP:
6974 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
6975 break;
252b5132 6976
1b513401
NC
6977 case SHT_REL:
6978 CHECK_ENTSIZE (section, i, Rel);
546cb2d8 6979 if (do_checks && section->sh_size == 0)
1b513401
NC
6980 warn (_("Section '%s': zero-sized relocation section\n"), name);
6981 break;
6982
6983 case SHT_RELA:
6984 CHECK_ENTSIZE (section, i, Rela);
546cb2d8 6985 if (do_checks && section->sh_size == 0)
1b513401
NC
6986 warn (_("Section '%s': zero-sized relocation section\n"), name);
6987 break;
6988
6989 case SHT_NOTE:
6990 case SHT_PROGBITS:
546cb2d8
NC
6991 /* Having a zero sized section is not illegal according to the
6992 ELF standard, but it might be an indication that something
6993 is wrong. So issue a warning if we are running in lint mode. */
6994 if (do_checks && section->sh_size == 0)
1b513401
NC
6995 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
6996 break;
6997
6998 default:
6999 break;
7000 }
7001
7002 if ((do_debugging || do_debug_info || do_debug_abbrevs
7003 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
7004 || do_debug_aranges || do_debug_frames || do_debug_macinfo
e38332c2
NC
7005 || do_debug_str || do_debug_str_offsets || do_debug_loc
7006 || do_debug_ranges
1b513401 7007 || do_debug_addr || do_debug_cu_index || do_debug_links)
24d127aa
ML
7008 && (startswith (name, ".debug_")
7009 || startswith (name, ".zdebug_")))
252b5132 7010 {
1b315056
CS
7011 if (name[1] == 'z')
7012 name += sizeof (".zdebug_") - 1;
7013 else
7014 name += sizeof (".debug_") - 1;
252b5132
RH
7015
7016 if (do_debugging
24d127aa
ML
7017 || (do_debug_info && startswith (name, "info"))
7018 || (do_debug_info && startswith (name, "types"))
7019 || (do_debug_abbrevs && startswith (name, "abbrev"))
b40bf0a2 7020 || (do_debug_lines && strcmp (name, "line") == 0)
24d127aa
ML
7021 || (do_debug_lines && startswith (name, "line."))
7022 || (do_debug_pubnames && startswith (name, "pubnames"))
7023 || (do_debug_pubtypes && startswith (name, "pubtypes"))
7024 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
7025 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
7026 || (do_debug_aranges && startswith (name, "aranges"))
7027 || (do_debug_ranges && startswith (name, "ranges"))
7028 || (do_debug_ranges && startswith (name, "rnglists"))
7029 || (do_debug_frames && startswith (name, "frame"))
7030 || (do_debug_macinfo && startswith (name, "macinfo"))
7031 || (do_debug_macinfo && startswith (name, "macro"))
7032 || (do_debug_str && startswith (name, "str"))
7033 || (do_debug_links && startswith (name, "sup"))
7034 || (do_debug_str_offsets && startswith (name, "str_offsets"))
7035 || (do_debug_loc && startswith (name, "loc"))
7036 || (do_debug_loc && startswith (name, "loclists"))
7037 || (do_debug_addr && startswith (name, "addr"))
7038 || (do_debug_cu_index && startswith (name, "cu_index"))
7039 || (do_debug_cu_index && startswith (name, "tu_index"))
252b5132 7040 )
6431e409 7041 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
252b5132 7042 }
a262ae96 7043 /* Linkonce section to be combined with .debug_info at link time. */
09fd7e38 7044 else if ((do_debugging || do_debug_info)
24d127aa 7045 && startswith (name, ".gnu.linkonce.wi."))
6431e409 7046 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
18bd398b 7047 else if (do_debug_frames && streq (name, ".eh_frame"))
6431e409 7048 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
61364358
JK
7049 else if (do_gdb_index && (streq (name, ".gdb_index")
7050 || streq (name, ".debug_names")))
6431e409 7051 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6f875884
TG
7052 /* Trace sections for Itanium VMS. */
7053 else if ((do_debugging || do_trace_info || do_trace_abbrevs
7054 || do_trace_aranges)
24d127aa 7055 && startswith (name, ".trace_"))
6f875884
TG
7056 {
7057 name += sizeof (".trace_") - 1;
7058
7059 if (do_debugging
7060 || (do_trace_info && streq (name, "info"))
7061 || (do_trace_abbrevs && streq (name, "abbrev"))
7062 || (do_trace_aranges && streq (name, "aranges"))
7063 )
6431e409 7064 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6f875884 7065 }
dda8d76d 7066 else if ((do_debugging || do_debug_links)
24d127aa
ML
7067 && (startswith (name, ".gnu_debuglink")
7068 || startswith (name, ".gnu_debugaltlink")))
6431e409 7069 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
252b5132
RH
7070 }
7071
7072 if (! do_sections)
015dc7e1 7073 return true;
252b5132 7074
ca0e11aa 7075 if (filedata->is_separate && ! process_links)
015dc7e1 7076 return true;
ca0e11aa
NC
7077
7078 if (filedata->is_separate)
7079 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
7080 else if (filedata->file_header.e_shnum > 1)
3a1a2036
NC
7081 printf (_("\nSection Headers:\n"));
7082 else
7083 printf (_("\nSection Header:\n"));
76da6bbe 7084
f7a99963 7085 if (is_32bit_elf)
595cf52e 7086 {
5477e8a0 7087 if (do_section_details)
595cf52e
L
7088 {
7089 printf (_(" [Nr] Name\n"));
5477e8a0 7090 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
595cf52e
L
7091 }
7092 else
7093 printf
7094 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
7095 }
d974e256 7096 else if (do_wide)
595cf52e 7097 {
5477e8a0 7098 if (do_section_details)
595cf52e
L
7099 {
7100 printf (_(" [Nr] Name\n"));
5477e8a0 7101 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
595cf52e
L
7102 }
7103 else
7104 printf
7105 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
7106 }
f7a99963
NC
7107 else
7108 {
5477e8a0 7109 if (do_section_details)
595cf52e
L
7110 {
7111 printf (_(" [Nr] Name\n"));
5477e8a0
L
7112 printf (_(" Type Address Offset Link\n"));
7113 printf (_(" Size EntSize Info Align\n"));
595cf52e
L
7114 }
7115 else
7116 {
7117 printf (_(" [Nr] Name Type Address Offset\n"));
7118 printf (_(" Size EntSize Flags Link Info Align\n"));
7119 }
f7a99963 7120 }
252b5132 7121
5477e8a0
L
7122 if (do_section_details)
7123 printf (_(" Flags\n"));
7124
dda8d76d
NC
7125 for (i = 0, section = filedata->section_headers;
7126 i < filedata->file_header.e_shnum;
b34976b6 7127 i++, section++)
252b5132 7128 {
dd905818
NC
7129 /* Run some sanity checks on the section header. */
7130
7131 /* Check the sh_link field. */
7132 switch (section->sh_type)
7133 {
285e3f99
AM
7134 case SHT_REL:
7135 case SHT_RELA:
7136 if (section->sh_link == 0
7137 && (filedata->file_header.e_type == ET_EXEC
7138 || filedata->file_header.e_type == ET_DYN))
7139 /* A dynamic relocation section where all entries use a
7140 zero symbol index need not specify a symtab section. */
7141 break;
7142 /* Fall through. */
dd905818
NC
7143 case SHT_SYMTAB_SHNDX:
7144 case SHT_GROUP:
7145 case SHT_HASH:
7146 case SHT_GNU_HASH:
7147 case SHT_GNU_versym:
285e3f99 7148 if (section->sh_link == 0
dda8d76d
NC
7149 || section->sh_link >= filedata->file_header.e_shnum
7150 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
7151 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
dd905818
NC
7152 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
7153 i, section->sh_link);
7154 break;
7155
7156 case SHT_DYNAMIC:
7157 case SHT_SYMTAB:
7158 case SHT_DYNSYM:
7159 case SHT_GNU_verneed:
7160 case SHT_GNU_verdef:
7161 case SHT_GNU_LIBLIST:
285e3f99 7162 if (section->sh_link == 0
dda8d76d
NC
7163 || section->sh_link >= filedata->file_header.e_shnum
7164 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
dd905818
NC
7165 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
7166 i, section->sh_link);
7167 break;
7168
7169 case SHT_INIT_ARRAY:
7170 case SHT_FINI_ARRAY:
7171 case SHT_PREINIT_ARRAY:
7172 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7173 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7174 i, section->sh_link);
7175 break;
7176
7177 default:
7178 /* FIXME: Add support for target specific section types. */
7179#if 0 /* Currently we do not check other section types as there are too
7180 many special cases. Stab sections for example have a type
7181 of SHT_PROGBITS but an sh_link field that links to the .stabstr
7182 section. */
7183 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7184 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7185 i, section->sh_link);
7186#endif
7187 break;
7188 }
7189
7190 /* Check the sh_info field. */
7191 switch (section->sh_type)
7192 {
7193 case SHT_REL:
7194 case SHT_RELA:
285e3f99
AM
7195 if (section->sh_info == 0
7196 && (filedata->file_header.e_type == ET_EXEC
7197 || filedata->file_header.e_type == ET_DYN))
7198 /* Dynamic relocations apply to segments, so they do not
7199 need to specify the section they relocate. */
7200 break;
7201 if (section->sh_info == 0
dda8d76d
NC
7202 || section->sh_info >= filedata->file_header.e_shnum
7203 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
7204 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
7205 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
7206 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
385e5b90
L
7207 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
7208 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
dd905818 7209 /* FIXME: Are other section types valid ? */
dda8d76d 7210 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
285e3f99
AM
7211 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
7212 i, section->sh_info);
dd905818
NC
7213 break;
7214
7215 case SHT_DYNAMIC:
7216 case SHT_HASH:
7217 case SHT_SYMTAB_SHNDX:
7218 case SHT_INIT_ARRAY:
7219 case SHT_FINI_ARRAY:
7220 case SHT_PREINIT_ARRAY:
7221 if (section->sh_info != 0)
7222 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7223 i, section->sh_info);
7224 break;
7225
7226 case SHT_GROUP:
7227 case SHT_SYMTAB:
7228 case SHT_DYNSYM:
7229 /* A symbol index - we assume that it is valid. */
7230 break;
7231
7232 default:
7233 /* FIXME: Add support for target specific section types. */
7234 if (section->sh_type == SHT_NOBITS)
7235 /* NOBITS section headers with non-zero sh_info fields can be
7236 created when a binary is stripped of everything but its debug
1a9ccd70
NC
7237 information. The stripped sections have their headers
7238 preserved but their types set to SHT_NOBITS. So do not check
7239 this type of section. */
dd905818
NC
7240 ;
7241 else if (section->sh_flags & SHF_INFO_LINK)
7242 {
dda8d76d 7243 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
dd905818
NC
7244 warn (_("[%2u]: Expected link to another section in info field"), i);
7245 }
a91e1603
L
7246 else if (section->sh_type < SHT_LOOS
7247 && (section->sh_flags & SHF_GNU_MBIND) == 0
7248 && section->sh_info != 0)
dd905818
NC
7249 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7250 i, section->sh_info);
7251 break;
7252 }
7253
3e6b6445 7254 /* Check the sh_size field. */
dda8d76d 7255 if (section->sh_size > filedata->file_size
3e6b6445
NC
7256 && section->sh_type != SHT_NOBITS
7257 && section->sh_type != SHT_NULL
7258 && section->sh_type < SHT_LOOS)
7259 warn (_("Size of section %u is larger than the entire file!\n"), i);
7260
7bfd842d 7261 printf (" [%2u] ", i);
5477e8a0 7262 if (do_section_details)
dda8d76d 7263 printf ("%s\n ", printable_section_name (filedata, section));
595cf52e 7264 else
84714f86 7265 print_symbol (-17, section_name_print (filedata, section));
0b4362b0 7266
ea52a088 7267 printf (do_wide ? " %-15s " : " %-15.15s ",
dda8d76d 7268 get_section_type_name (filedata, section->sh_type));
0b4362b0 7269
f7a99963
NC
7270 if (is_32bit_elf)
7271 {
cfcac11d
NC
7272 const char * link_too_big = NULL;
7273
f7a99963 7274 print_vma (section->sh_addr, LONG_HEX);
76da6bbe 7275
f7a99963
NC
7276 printf ( " %6.6lx %6.6lx %2.2lx",
7277 (unsigned long) section->sh_offset,
7278 (unsigned long) section->sh_size,
7279 (unsigned long) section->sh_entsize);
d1133906 7280
5477e8a0
L
7281 if (do_section_details)
7282 fputs (" ", stdout);
7283 else
dda8d76d 7284 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
76da6bbe 7285
dda8d76d 7286 if (section->sh_link >= filedata->file_header.e_shnum)
cfcac11d
NC
7287 {
7288 link_too_big = "";
7289 /* The sh_link value is out of range. Normally this indicates
caa83f8b 7290 an error but it can have special values in Solaris binaries. */
dda8d76d 7291 switch (filedata->file_header.e_machine)
cfcac11d 7292 {
caa83f8b 7293 case EM_386:
22abe556 7294 case EM_IAMCU:
caa83f8b 7295 case EM_X86_64:
7f502d6c 7296 case EM_L1OM:
7a9068fe 7297 case EM_K1OM:
cfcac11d
NC
7298 case EM_OLD_SPARCV9:
7299 case EM_SPARC32PLUS:
7300 case EM_SPARCV9:
7301 case EM_SPARC:
7302 if (section->sh_link == (SHN_BEFORE & 0xffff))
7303 link_too_big = "BEFORE";
7304 else if (section->sh_link == (SHN_AFTER & 0xffff))
7305 link_too_big = "AFTER";
7306 break;
7307 default:
7308 break;
7309 }
7310 }
7311
7312 if (do_section_details)
7313 {
7314 if (link_too_big != NULL && * link_too_big)
7315 printf ("<%s> ", link_too_big);
7316 else
7317 printf ("%2u ", section->sh_link);
7318 printf ("%3u %2lu\n", section->sh_info,
7319 (unsigned long) section->sh_addralign);
7320 }
7321 else
7322 printf ("%2u %3u %2lu\n",
7323 section->sh_link,
7324 section->sh_info,
7325 (unsigned long) section->sh_addralign);
7326
7327 if (link_too_big && ! * link_too_big)
7328 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
7329 i, section->sh_link);
f7a99963 7330 }
d974e256
JJ
7331 else if (do_wide)
7332 {
7333 print_vma (section->sh_addr, LONG_HEX);
7334
7335 if ((long) section->sh_offset == section->sh_offset)
7336 printf (" %6.6lx", (unsigned long) section->sh_offset);
7337 else
7338 {
7339 putchar (' ');
7340 print_vma (section->sh_offset, LONG_HEX);
7341 }
7342
7343 if ((unsigned long) section->sh_size == section->sh_size)
7344 printf (" %6.6lx", (unsigned long) section->sh_size);
7345 else
7346 {
7347 putchar (' ');
7348 print_vma (section->sh_size, LONG_HEX);
7349 }
7350
7351 if ((unsigned long) section->sh_entsize == section->sh_entsize)
7352 printf (" %2.2lx", (unsigned long) section->sh_entsize);
7353 else
7354 {
7355 putchar (' ');
7356 print_vma (section->sh_entsize, LONG_HEX);
7357 }
7358
5477e8a0
L
7359 if (do_section_details)
7360 fputs (" ", stdout);
7361 else
dda8d76d 7362 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
d974e256 7363
72de5009 7364 printf ("%2u %3u ", section->sh_link, section->sh_info);
d974e256
JJ
7365
7366 if ((unsigned long) section->sh_addralign == section->sh_addralign)
72de5009 7367 printf ("%2lu\n", (unsigned long) section->sh_addralign);
d974e256
JJ
7368 else
7369 {
7370 print_vma (section->sh_addralign, DEC);
7371 putchar ('\n');
7372 }
7373 }
5477e8a0 7374 else if (do_section_details)
595cf52e 7375 {
55cc53e9 7376 putchar (' ');
595cf52e
L
7377 print_vma (section->sh_addr, LONG_HEX);
7378 if ((long) section->sh_offset == section->sh_offset)
5477e8a0 7379 printf (" %16.16lx", (unsigned long) section->sh_offset);
595cf52e
L
7380 else
7381 {
7382 printf (" ");
7383 print_vma (section->sh_offset, LONG_HEX);
7384 }
72de5009 7385 printf (" %u\n ", section->sh_link);
595cf52e 7386 print_vma (section->sh_size, LONG_HEX);
5477e8a0 7387 putchar (' ');
595cf52e
L
7388 print_vma (section->sh_entsize, LONG_HEX);
7389
72de5009
AM
7390 printf (" %-16u %lu\n",
7391 section->sh_info,
595cf52e
L
7392 (unsigned long) section->sh_addralign);
7393 }
f7a99963
NC
7394 else
7395 {
7396 putchar (' ');
7397 print_vma (section->sh_addr, LONG_HEX);
53c7db4b
KH
7398 if ((long) section->sh_offset == section->sh_offset)
7399 printf (" %8.8lx", (unsigned long) section->sh_offset);
7400 else
7401 {
7402 printf (" ");
7403 print_vma (section->sh_offset, LONG_HEX);
7404 }
f7a99963
NC
7405 printf ("\n ");
7406 print_vma (section->sh_size, LONG_HEX);
7407 printf (" ");
7408 print_vma (section->sh_entsize, LONG_HEX);
76da6bbe 7409
dda8d76d 7410 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
76da6bbe 7411
72de5009
AM
7412 printf (" %2u %3u %lu\n",
7413 section->sh_link,
7414 section->sh_info,
f7a99963
NC
7415 (unsigned long) section->sh_addralign);
7416 }
5477e8a0
L
7417
7418 if (do_section_details)
77115a4a 7419 {
dda8d76d 7420 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
77115a4a
L
7421 if ((section->sh_flags & SHF_COMPRESSED) != 0)
7422 {
7423 /* Minimum section size is 12 bytes for 32-bit compression
7424 header + 12 bytes for compressed data header. */
7425 unsigned char buf[24];
d8024a91 7426
77115a4a 7427 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
dda8d76d 7428 if (get_data (&buf, filedata, section->sh_offset, 1,
77115a4a
L
7429 sizeof (buf), _("compression header")))
7430 {
7431 Elf_Internal_Chdr chdr;
d8024a91 7432
5844b465
NC
7433 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
7434 printf (_(" [<corrupt>]\n"));
77115a4a 7435 else
5844b465
NC
7436 {
7437 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
7438 printf (" ZLIB, ");
7439 else
7440 printf (_(" [<unknown>: 0x%x], "),
7441 chdr.ch_type);
7442 print_vma (chdr.ch_size, LONG_HEX);
7443 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
7444 }
77115a4a
L
7445 }
7446 }
7447 }
252b5132
RH
7448 }
7449
5477e8a0 7450 if (!do_section_details)
3dbcc61d 7451 {
9fb71ee4
NC
7452 /* The ordering of the letters shown here matches the ordering of the
7453 corresponding SHF_xxx values, and hence the order in which these
7454 letters will be displayed to the user. */
7455 printf (_("Key to Flags:\n\
7456 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
7457 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
fd85a6a1 7458 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
5424d7ed
L
7459 switch (filedata->file_header.e_ident[EI_OSABI])
7460 {
7461 case ELFOSABI_GNU:
7462 case ELFOSABI_FREEBSD:
7463 printf (_("R (retain), "));
7464 /* Fall through */
7465 case ELFOSABI_NONE:
7466 printf (_("D (mbind), "));
7467 break;
7468 default:
7469 break;
7470 }
dda8d76d
NC
7471 if (filedata->file_header.e_machine == EM_X86_64
7472 || filedata->file_header.e_machine == EM_L1OM
7473 || filedata->file_header.e_machine == EM_K1OM)
9fb71ee4 7474 printf (_("l (large), "));
dda8d76d 7475 else if (filedata->file_header.e_machine == EM_ARM)
f0728ee3 7476 printf (_("y (purecode), "));
dda8d76d 7477 else if (filedata->file_header.e_machine == EM_PPC)
83eef883 7478 printf (_("v (VLE), "));
9fb71ee4 7479 printf ("p (processor specific)\n");
0b4362b0 7480 }
d1133906 7481
015dc7e1 7482 return true;
252b5132
RH
7483}
7484
015dc7e1 7485static bool
28d13567
AM
7486get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
7487 Elf_Internal_Sym **symtab, unsigned long *nsyms,
7488 char **strtab, unsigned long *strtablen)
7489{
7490 *strtab = NULL;
7491 *strtablen = 0;
4de91c10 7492 *symtab = get_elf_symbols (filedata, symsec, nsyms);
28d13567
AM
7493
7494 if (*symtab == NULL)
015dc7e1 7495 return false;
28d13567
AM
7496
7497 if (symsec->sh_link != 0)
7498 {
7499 Elf_Internal_Shdr *strsec;
7500
7501 if (symsec->sh_link >= filedata->file_header.e_shnum)
7502 {
7503 error (_("Bad sh_link in symbol table section\n"));
7504 free (*symtab);
7505 *symtab = NULL;
7506 *nsyms = 0;
015dc7e1 7507 return false;
28d13567
AM
7508 }
7509
7510 strsec = filedata->section_headers + symsec->sh_link;
7511
7512 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7513 1, strsec->sh_size, _("string table"));
7514 if (*strtab == NULL)
7515 {
7516 free (*symtab);
7517 *symtab = NULL;
7518 *nsyms = 0;
015dc7e1 7519 return false;
28d13567
AM
7520 }
7521 *strtablen = strsec->sh_size;
7522 }
015dc7e1 7523 return true;
28d13567
AM
7524}
7525
f5842774
L
7526static const char *
7527get_group_flags (unsigned int flags)
7528{
1449284b 7529 static char buff[128];
220453ec 7530
6d913794
NC
7531 if (flags == 0)
7532 return "";
7533 else if (flags == GRP_COMDAT)
7534 return "COMDAT ";
f5842774 7535
89246a0e
AM
7536 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
7537 flags,
7538 flags & GRP_MASKOS ? _("<OS specific>") : "",
7539 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
7540 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
7541 ? _("<unknown>") : ""));
6d913794 7542
f5842774
L
7543 return buff;
7544}
7545
015dc7e1 7546static bool
dda8d76d 7547process_section_groups (Filedata * filedata)
f5842774 7548{
2cf0635d 7549 Elf_Internal_Shdr * section;
f5842774 7550 unsigned int i;
2cf0635d
NC
7551 struct group * group;
7552 Elf_Internal_Shdr * symtab_sec;
7553 Elf_Internal_Shdr * strtab_sec;
7554 Elf_Internal_Sym * symtab;
ba5cdace 7555 unsigned long num_syms;
2cf0635d 7556 char * strtab;
c256ffe7 7557 size_t strtab_size;
d1f5c6e3
L
7558
7559 /* Don't process section groups unless needed. */
7560 if (!do_unwind && !do_section_groups)
015dc7e1 7561 return true;
f5842774 7562
dda8d76d 7563 if (filedata->file_header.e_shnum == 0)
f5842774
L
7564 {
7565 if (do_section_groups)
ca0e11aa
NC
7566 {
7567 if (filedata->is_separate)
7568 printf (_("\nThere are no sections group in linked file '%s'.\n"),
7569 filedata->file_name);
7570 else
7571 printf (_("\nThere are no section groups in this file.\n"));
7572 }
015dc7e1 7573 return true;
f5842774
L
7574 }
7575
dda8d76d 7576 if (filedata->section_headers == NULL)
f5842774
L
7577 {
7578 error (_("Section headers are not available!\n"));
fa1908fd 7579 /* PR 13622: This can happen with a corrupt ELF header. */
015dc7e1 7580 return false;
f5842774
L
7581 }
7582
978c4450
AM
7583 filedata->section_headers_groups
7584 = (struct group **) calloc (filedata->file_header.e_shnum,
7585 sizeof (struct group *));
e4b17d5c 7586
978c4450 7587 if (filedata->section_headers_groups == NULL)
e4b17d5c 7588 {
8b73c356 7589 error (_("Out of memory reading %u section group headers\n"),
dda8d76d 7590 filedata->file_header.e_shnum);
015dc7e1 7591 return false;
e4b17d5c
L
7592 }
7593
f5842774 7594 /* Scan the sections for the group section. */
978c4450 7595 filedata->group_count = 0;
dda8d76d
NC
7596 for (i = 0, section = filedata->section_headers;
7597 i < filedata->file_header.e_shnum;
f5842774 7598 i++, section++)
e4b17d5c 7599 if (section->sh_type == SHT_GROUP)
978c4450 7600 filedata->group_count++;
e4b17d5c 7601
978c4450 7602 if (filedata->group_count == 0)
d1f5c6e3
L
7603 {
7604 if (do_section_groups)
ca0e11aa
NC
7605 {
7606 if (filedata->is_separate)
7607 printf (_("\nThere are no section groups in linked file '%s'.\n"),
7608 filedata->file_name);
7609 else
7610 printf (_("\nThere are no section groups in this file.\n"));
7611 }
d1f5c6e3 7612
015dc7e1 7613 return true;
d1f5c6e3
L
7614 }
7615
978c4450
AM
7616 filedata->section_groups = (struct group *) calloc (filedata->group_count,
7617 sizeof (struct group));
e4b17d5c 7618
978c4450 7619 if (filedata->section_groups == NULL)
e4b17d5c 7620 {
8b73c356 7621 error (_("Out of memory reading %lu groups\n"),
978c4450 7622 (unsigned long) filedata->group_count);
015dc7e1 7623 return false;
e4b17d5c
L
7624 }
7625
d1f5c6e3
L
7626 symtab_sec = NULL;
7627 strtab_sec = NULL;
7628 symtab = NULL;
ba5cdace 7629 num_syms = 0;
d1f5c6e3 7630 strtab = NULL;
c256ffe7 7631 strtab_size = 0;
ca0e11aa
NC
7632
7633 if (filedata->is_separate)
7634 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
047c3dbf 7635
978c4450 7636 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
dda8d76d 7637 i < filedata->file_header.e_shnum;
e4b17d5c 7638 i++, section++)
f5842774
L
7639 {
7640 if (section->sh_type == SHT_GROUP)
7641 {
dda8d76d 7642 const char * name = printable_section_name (filedata, section);
74e1a04b 7643 const char * group_name;
2cf0635d
NC
7644 unsigned char * start;
7645 unsigned char * indices;
f5842774 7646 unsigned int entry, j, size;
2cf0635d
NC
7647 Elf_Internal_Shdr * sec;
7648 Elf_Internal_Sym * sym;
f5842774
L
7649
7650 /* Get the symbol table. */
dda8d76d
NC
7651 if (section->sh_link >= filedata->file_header.e_shnum
7652 || ((sec = filedata->section_headers + section->sh_link)->sh_type
c256ffe7 7653 != SHT_SYMTAB))
f5842774
L
7654 {
7655 error (_("Bad sh_link in group section `%s'\n"), name);
7656 continue;
7657 }
d1f5c6e3
L
7658
7659 if (symtab_sec != sec)
7660 {
7661 symtab_sec = sec;
9db70fc3 7662 free (symtab);
4de91c10 7663 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
d1f5c6e3 7664 }
f5842774 7665
dd24e3da
NC
7666 if (symtab == NULL)
7667 {
7668 error (_("Corrupt header in group section `%s'\n"), name);
7669 continue;
7670 }
7671
ba5cdace
NC
7672 if (section->sh_info >= num_syms)
7673 {
7674 error (_("Bad sh_info in group section `%s'\n"), name);
7675 continue;
7676 }
7677
f5842774
L
7678 sym = symtab + section->sh_info;
7679
7680 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7681 {
4fbb74a6 7682 if (sym->st_shndx == 0
dda8d76d 7683 || sym->st_shndx >= filedata->file_header.e_shnum)
f5842774
L
7684 {
7685 error (_("Bad sh_info in group section `%s'\n"), name);
7686 continue;
7687 }
ba2685cc 7688
84714f86
AM
7689 group_name = section_name_print (filedata,
7690 filedata->section_headers
b9e920ec 7691 + sym->st_shndx);
c256ffe7 7692 strtab_sec = NULL;
9db70fc3 7693 free (strtab);
f5842774 7694 strtab = NULL;
c256ffe7 7695 strtab_size = 0;
f5842774
L
7696 }
7697 else
7698 {
7699 /* Get the string table. */
dda8d76d 7700 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
c256ffe7
JJ
7701 {
7702 strtab_sec = NULL;
9db70fc3 7703 free (strtab);
c256ffe7
JJ
7704 strtab = NULL;
7705 strtab_size = 0;
7706 }
7707 else if (strtab_sec
dda8d76d 7708 != (sec = filedata->section_headers + symtab_sec->sh_link))
d1f5c6e3
L
7709 {
7710 strtab_sec = sec;
9db70fc3 7711 free (strtab);
071436c6 7712
dda8d76d 7713 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
071436c6
NC
7714 1, strtab_sec->sh_size,
7715 _("string table"));
c256ffe7 7716 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
d1f5c6e3 7717 }
c256ffe7 7718 group_name = sym->st_name < strtab_size
2b692964 7719 ? strtab + sym->st_name : _("<corrupt>");
f5842774
L
7720 }
7721
c9c1d674
EG
7722 /* PR 17531: file: loop. */
7723 if (section->sh_entsize > section->sh_size)
7724 {
7725 error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
dda8d76d 7726 printable_section_name (filedata, section),
8066deb1
AM
7727 (unsigned long) section->sh_entsize,
7728 (unsigned long) section->sh_size);
61dd8e19 7729 continue;
c9c1d674
EG
7730 }
7731
dda8d76d 7732 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
3f5e193b
NC
7733 1, section->sh_size,
7734 _("section data"));
59245841
NC
7735 if (start == NULL)
7736 continue;
f5842774
L
7737
7738 indices = start;
7739 size = (section->sh_size / section->sh_entsize) - 1;
7740 entry = byte_get (indices, 4);
7741 indices += 4;
e4b17d5c
L
7742
7743 if (do_section_groups)
7744 {
2b692964 7745 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
391cb864 7746 get_group_flags (entry), i, name, group_name, size);
ba2685cc 7747
e4b17d5c
L
7748 printf (_(" [Index] Name\n"));
7749 }
7750
7751 group->group_index = i;
7752
f5842774
L
7753 for (j = 0; j < size; j++)
7754 {
2cf0635d 7755 struct group_list * g;
e4b17d5c 7756
f5842774
L
7757 entry = byte_get (indices, 4);
7758 indices += 4;
7759
dda8d76d 7760 if (entry >= filedata->file_header.e_shnum)
391cb864 7761 {
57028622
NC
7762 static unsigned num_group_errors = 0;
7763
7764 if (num_group_errors ++ < 10)
7765 {
7766 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
dda8d76d 7767 entry, i, filedata->file_header.e_shnum - 1);
57028622 7768 if (num_group_errors == 10)
67ce483b 7769 warn (_("Further error messages about overlarge group section indices suppressed\n"));
57028622 7770 }
391cb864
L
7771 continue;
7772 }
391cb864 7773
978c4450 7774 if (filedata->section_headers_groups [entry] != NULL)
e4b17d5c 7775 {
d1f5c6e3
L
7776 if (entry)
7777 {
57028622
NC
7778 static unsigned num_errs = 0;
7779
7780 if (num_errs ++ < 10)
7781 {
7782 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
7783 entry, i,
978c4450 7784 filedata->section_headers_groups [entry]->group_index);
57028622
NC
7785 if (num_errs == 10)
7786 warn (_("Further error messages about already contained group sections suppressed\n"));
7787 }
d1f5c6e3
L
7788 continue;
7789 }
7790 else
7791 {
7792 /* Intel C/C++ compiler may put section 0 in a
32ec8896 7793 section group. We just warn it the first time
d1f5c6e3 7794 and ignore it afterwards. */
015dc7e1 7795 static bool warned = false;
d1f5c6e3
L
7796 if (!warned)
7797 {
7798 error (_("section 0 in group section [%5u]\n"),
978c4450 7799 filedata->section_headers_groups [entry]->group_index);
015dc7e1 7800 warned = true;
d1f5c6e3
L
7801 }
7802 }
e4b17d5c
L
7803 }
7804
978c4450 7805 filedata->section_headers_groups [entry] = group;
e4b17d5c
L
7806
7807 if (do_section_groups)
7808 {
dda8d76d
NC
7809 sec = filedata->section_headers + entry;
7810 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
ba2685cc
AM
7811 }
7812
3f5e193b 7813 g = (struct group_list *) xmalloc (sizeof (struct group_list));
e4b17d5c
L
7814 g->section_index = entry;
7815 g->next = group->root;
7816 group->root = g;
f5842774
L
7817 }
7818
9db70fc3 7819 free (start);
e4b17d5c
L
7820
7821 group++;
f5842774
L
7822 }
7823 }
7824
9db70fc3
AM
7825 free (symtab);
7826 free (strtab);
015dc7e1 7827 return true;
f5842774
L
7828}
7829
28f997cf
TG
7830/* Data used to display dynamic fixups. */
7831
7832struct ia64_vms_dynfixup
7833{
7834 bfd_vma needed_ident; /* Library ident number. */
7835 bfd_vma needed; /* Index in the dstrtab of the library name. */
7836 bfd_vma fixup_needed; /* Index of the library. */
7837 bfd_vma fixup_rela_cnt; /* Number of fixups. */
7838 bfd_vma fixup_rela_off; /* Fixups offset in the dynamic segment. */
7839};
7840
7841/* Data used to display dynamic relocations. */
7842
7843struct ia64_vms_dynimgrela
7844{
7845 bfd_vma img_rela_cnt; /* Number of relocations. */
7846 bfd_vma img_rela_off; /* Reloc offset in the dynamic segment. */
7847};
7848
7849/* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
7850 library). */
7851
015dc7e1 7852static bool
dda8d76d
NC
7853dump_ia64_vms_dynamic_fixups (Filedata * filedata,
7854 struct ia64_vms_dynfixup * fixup,
7855 const char * strtab,
7856 unsigned int strtab_sz)
28f997cf 7857{
32ec8896 7858 Elf64_External_VMS_IMAGE_FIXUP * imfs;
28f997cf 7859 long i;
32ec8896 7860 const char * lib_name;
28f997cf 7861
978c4450
AM
7862 imfs = get_data (NULL, filedata,
7863 filedata->dynamic_addr + fixup->fixup_rela_off,
95099889 7864 sizeof (*imfs), fixup->fixup_rela_cnt,
28f997cf
TG
7865 _("dynamic section image fixups"));
7866 if (!imfs)
015dc7e1 7867 return false;
28f997cf
TG
7868
7869 if (fixup->needed < strtab_sz)
7870 lib_name = strtab + fixup->needed;
7871 else
7872 {
32ec8896 7873 warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
7f01b0c6 7874 (unsigned long) fixup->needed);
28f997cf
TG
7875 lib_name = "???";
7876 }
736990c4 7877
28f997cf
TG
7878 printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
7879 (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
7880 printf
7881 (_("Seg Offset Type SymVec DataType\n"));
7882
7883 for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
7884 {
7885 unsigned int type;
7886 const char *rtype;
7887
7888 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
7889 printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
7890 type = BYTE_GET (imfs [i].type);
7891 rtype = elf_ia64_reloc_type (type);
7892 if (rtype == NULL)
7893 printf (" 0x%08x ", type);
7894 else
7895 printf (" %-32s ", rtype);
7896 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
7897 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
7898 }
7899
7900 free (imfs);
015dc7e1 7901 return true;
28f997cf
TG
7902}
7903
7904/* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
7905
015dc7e1 7906static bool
dda8d76d 7907dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
28f997cf
TG
7908{
7909 Elf64_External_VMS_IMAGE_RELA *imrs;
7910 long i;
7911
978c4450
AM
7912 imrs = get_data (NULL, filedata,
7913 filedata->dynamic_addr + imgrela->img_rela_off,
95099889 7914 sizeof (*imrs), imgrela->img_rela_cnt,
9cf03b7e 7915 _("dynamic section image relocations"));
28f997cf 7916 if (!imrs)
015dc7e1 7917 return false;
28f997cf
TG
7918
7919 printf (_("\nImage relocs\n"));
7920 printf
7921 (_("Seg Offset Type Addend Seg Sym Off\n"));
7922
7923 for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
7924 {
7925 unsigned int type;
7926 const char *rtype;
7927
7928 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
7929 printf ("%08" BFD_VMA_FMT "x ",
7930 (bfd_vma) BYTE_GET (imrs [i].rela_offset));
7931 type = BYTE_GET (imrs [i].type);
7932 rtype = elf_ia64_reloc_type (type);
7933 if (rtype == NULL)
7934 printf ("0x%08x ", type);
7935 else
7936 printf ("%-31s ", rtype);
7937 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
7938 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
7939 printf ("%08" BFD_VMA_FMT "x\n",
7940 (bfd_vma) BYTE_GET (imrs [i].sym_offset));
7941 }
7942
7943 free (imrs);
015dc7e1 7944 return true;
28f997cf
TG
7945}
7946
7947/* Display IA-64 OpenVMS dynamic relocations and fixups. */
7948
015dc7e1 7949static bool
dda8d76d 7950process_ia64_vms_dynamic_relocs (Filedata * filedata)
28f997cf
TG
7951{
7952 struct ia64_vms_dynfixup fixup;
7953 struct ia64_vms_dynimgrela imgrela;
7954 Elf_Internal_Dyn *entry;
28f997cf
TG
7955 bfd_vma strtab_off = 0;
7956 bfd_vma strtab_sz = 0;
7957 char *strtab = NULL;
015dc7e1 7958 bool res = true;
28f997cf
TG
7959
7960 memset (&fixup, 0, sizeof (fixup));
7961 memset (&imgrela, 0, sizeof (imgrela));
7962
7963 /* Note: the order of the entries is specified by the OpenVMS specs. */
978c4450
AM
7964 for (entry = filedata->dynamic_section;
7965 entry < filedata->dynamic_section + filedata->dynamic_nent;
28f997cf
TG
7966 entry++)
7967 {
7968 switch (entry->d_tag)
7969 {
7970 case DT_IA_64_VMS_STRTAB_OFFSET:
7971 strtab_off = entry->d_un.d_val;
7972 break;
7973 case DT_STRSZ:
7974 strtab_sz = entry->d_un.d_val;
7975 if (strtab == NULL)
978c4450
AM
7976 strtab = get_data (NULL, filedata,
7977 filedata->dynamic_addr + strtab_off,
28f997cf 7978 1, strtab_sz, _("dynamic string section"));
736990c4
NC
7979 if (strtab == NULL)
7980 strtab_sz = 0;
28f997cf
TG
7981 break;
7982
7983 case DT_IA_64_VMS_NEEDED_IDENT:
7984 fixup.needed_ident = entry->d_un.d_val;
7985 break;
7986 case DT_NEEDED:
7987 fixup.needed = entry->d_un.d_val;
7988 break;
7989 case DT_IA_64_VMS_FIXUP_NEEDED:
7990 fixup.fixup_needed = entry->d_un.d_val;
7991 break;
7992 case DT_IA_64_VMS_FIXUP_RELA_CNT:
7993 fixup.fixup_rela_cnt = entry->d_un.d_val;
7994 break;
7995 case DT_IA_64_VMS_FIXUP_RELA_OFF:
7996 fixup.fixup_rela_off = entry->d_un.d_val;
dda8d76d 7997 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
015dc7e1 7998 res = false;
28f997cf 7999 break;
28f997cf
TG
8000 case DT_IA_64_VMS_IMG_RELA_CNT:
8001 imgrela.img_rela_cnt = entry->d_un.d_val;
8002 break;
8003 case DT_IA_64_VMS_IMG_RELA_OFF:
8004 imgrela.img_rela_off = entry->d_un.d_val;
dda8d76d 8005 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
015dc7e1 8006 res = false;
28f997cf
TG
8007 break;
8008
8009 default:
8010 break;
8011 }
8012 }
8013
9db70fc3 8014 free (strtab);
28f997cf
TG
8015
8016 return res;
8017}
8018
85b1c36d 8019static struct
566b0d53 8020{
2cf0635d 8021 const char * name;
566b0d53
L
8022 int reloc;
8023 int size;
8024 int rela;
32ec8896
NC
8025}
8026 dynamic_relocations [] =
566b0d53 8027{
015dc7e1
AM
8028 { "REL", DT_REL, DT_RELSZ, false },
8029 { "RELA", DT_RELA, DT_RELASZ, true },
32ec8896 8030 { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
566b0d53
L
8031};
8032
252b5132 8033/* Process the reloc section. */
18bd398b 8034
015dc7e1 8035static bool
dda8d76d 8036process_relocs (Filedata * filedata)
252b5132 8037{
b34976b6
AM
8038 unsigned long rel_size;
8039 unsigned long rel_offset;
252b5132 8040
252b5132 8041 if (!do_reloc)
015dc7e1 8042 return true;
252b5132
RH
8043
8044 if (do_using_dynamic)
8045 {
32ec8896 8046 int is_rela;
2cf0635d 8047 const char * name;
015dc7e1 8048 bool has_dynamic_reloc;
566b0d53 8049 unsigned int i;
0de14b54 8050
015dc7e1 8051 has_dynamic_reloc = false;
252b5132 8052
566b0d53 8053 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
252b5132 8054 {
566b0d53
L
8055 is_rela = dynamic_relocations [i].rela;
8056 name = dynamic_relocations [i].name;
978c4450
AM
8057 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
8058 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
103f02d3 8059
32ec8896 8060 if (rel_size)
015dc7e1 8061 has_dynamic_reloc = true;
566b0d53
L
8062
8063 if (is_rela == UNKNOWN)
aa903cfb 8064 {
566b0d53 8065 if (dynamic_relocations [i].reloc == DT_JMPREL)
978c4450 8066 switch (filedata->dynamic_info[DT_PLTREL])
566b0d53
L
8067 {
8068 case DT_REL:
015dc7e1 8069 is_rela = false;
566b0d53
L
8070 break;
8071 case DT_RELA:
015dc7e1 8072 is_rela = true;
566b0d53
L
8073 break;
8074 }
aa903cfb 8075 }
252b5132 8076
566b0d53
L
8077 if (rel_size)
8078 {
ca0e11aa
NC
8079 if (filedata->is_separate)
8080 printf
8081 (_("\nIn linked file '%s' section '%s' at offset 0x%lx contains %ld bytes:\n"),
8082 filedata->file_name, name, rel_offset, rel_size);
8083 else
8084 printf
8085 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
8086 name, rel_offset, rel_size);
252b5132 8087
dda8d76d
NC
8088 dump_relocations (filedata,
8089 offset_from_vma (filedata, rel_offset, rel_size),
d93f0186 8090 rel_size,
978c4450
AM
8091 filedata->dynamic_symbols,
8092 filedata->num_dynamic_syms,
8093 filedata->dynamic_strings,
8094 filedata->dynamic_strings_length,
015dc7e1 8095 is_rela, true /* is_dynamic */);
566b0d53 8096 }
252b5132 8097 }
566b0d53 8098
dda8d76d
NC
8099 if (is_ia64_vms (filedata))
8100 if (process_ia64_vms_dynamic_relocs (filedata))
015dc7e1 8101 has_dynamic_reloc = true;
28f997cf 8102
566b0d53 8103 if (! has_dynamic_reloc)
ca0e11aa
NC
8104 {
8105 if (filedata->is_separate)
8106 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
8107 filedata->file_name);
8108 else
8109 printf (_("\nThere are no dynamic relocations in this file.\n"));
8110 }
252b5132
RH
8111 }
8112 else
8113 {
2cf0635d 8114 Elf_Internal_Shdr * section;
b34976b6 8115 unsigned long i;
015dc7e1 8116 bool found = false;
252b5132 8117
dda8d76d
NC
8118 for (i = 0, section = filedata->section_headers;
8119 i < filedata->file_header.e_shnum;
b34976b6 8120 i++, section++)
252b5132
RH
8121 {
8122 if ( section->sh_type != SHT_RELA
8123 && section->sh_type != SHT_REL)
8124 continue;
8125
8126 rel_offset = section->sh_offset;
8127 rel_size = section->sh_size;
8128
8129 if (rel_size)
8130 {
b34976b6 8131 int is_rela;
d3a49aa8 8132 unsigned long num_rela;
103f02d3 8133
ca0e11aa
NC
8134 if (filedata->is_separate)
8135 printf (_("\nIn linked file '%s' relocation section "),
8136 filedata->file_name);
8137 else
8138 printf (_("\nRelocation section "));
252b5132 8139
dda8d76d 8140 if (filedata->string_table == NULL)
19936277 8141 printf ("%d", section->sh_name);
252b5132 8142 else
dda8d76d 8143 printf ("'%s'", printable_section_name (filedata, section));
252b5132 8144
d3a49aa8
AM
8145 num_rela = rel_size / section->sh_entsize;
8146 printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
8147 " at offset 0x%lx contains %lu entries:\n",
8148 num_rela),
8149 rel_offset, num_rela);
252b5132 8150
d79b3d50
NC
8151 is_rela = section->sh_type == SHT_RELA;
8152
4fbb74a6 8153 if (section->sh_link != 0
dda8d76d 8154 && section->sh_link < filedata->file_header.e_shnum)
af3fc3bc 8155 {
2cf0635d
NC
8156 Elf_Internal_Shdr * symsec;
8157 Elf_Internal_Sym * symtab;
d79b3d50 8158 unsigned long nsyms;
c256ffe7 8159 unsigned long strtablen = 0;
2cf0635d 8160 char * strtab = NULL;
57346661 8161
dda8d76d 8162 symsec = filedata->section_headers + section->sh_link;
08d8fa11
JJ
8163 if (symsec->sh_type != SHT_SYMTAB
8164 && symsec->sh_type != SHT_DYNSYM)
8165 continue;
8166
28d13567
AM
8167 if (!get_symtab (filedata, symsec,
8168 &symtab, &nsyms, &strtab, &strtablen))
af3fc3bc 8169 continue;
252b5132 8170
dda8d76d 8171 dump_relocations (filedata, rel_offset, rel_size,
bb4d2ac2
L
8172 symtab, nsyms, strtab, strtablen,
8173 is_rela,
8174 symsec->sh_type == SHT_DYNSYM);
9db70fc3 8175 free (strtab);
d79b3d50
NC
8176 free (symtab);
8177 }
8178 else
dda8d76d 8179 dump_relocations (filedata, rel_offset, rel_size,
32ec8896 8180 NULL, 0, NULL, 0, is_rela,
015dc7e1 8181 false /* is_dynamic */);
252b5132 8182
015dc7e1 8183 found = true;
252b5132
RH
8184 }
8185 }
8186
8187 if (! found)
45ac8f4f
NC
8188 {
8189 /* Users sometimes forget the -D option, so try to be helpful. */
8190 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8191 {
978c4450 8192 if (filedata->dynamic_info[dynamic_relocations [i].size])
45ac8f4f 8193 {
ca0e11aa
NC
8194 if (filedata->is_separate)
8195 printf (_("\nThere are no static relocations in linked file '%s'."),
8196 filedata->file_name);
8197 else
8198 printf (_("\nThere are no static relocations in this file."));
45ac8f4f
NC
8199 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
8200
8201 break;
8202 }
8203 }
8204 if (i == ARRAY_SIZE (dynamic_relocations))
ca0e11aa
NC
8205 {
8206 if (filedata->is_separate)
8207 printf (_("\nThere are no relocations in linked file '%s'.\n"),
8208 filedata->file_name);
8209 else
8210 printf (_("\nThere are no relocations in this file.\n"));
8211 }
45ac8f4f 8212 }
252b5132
RH
8213 }
8214
015dc7e1 8215 return true;
252b5132
RH
8216}
8217
4d6ed7c8
NC
8218/* An absolute address consists of a section and an offset. If the
8219 section is NULL, the offset itself is the address, otherwise, the
8220 address equals to LOAD_ADDRESS(section) + offset. */
8221
8222struct absaddr
948f632f
DA
8223{
8224 unsigned short section;
8225 bfd_vma offset;
8226};
4d6ed7c8 8227
948f632f
DA
8228/* Find the nearest symbol at or below ADDR. Returns the symbol
8229 name, if found, and the offset from the symbol to ADDR. */
4d6ed7c8 8230
4d6ed7c8 8231static void
dda8d76d
NC
8232find_symbol_for_address (Filedata * filedata,
8233 Elf_Internal_Sym * symtab,
8234 unsigned long nsyms,
8235 const char * strtab,
8236 unsigned long strtab_size,
8237 struct absaddr addr,
8238 const char ** symname,
8239 bfd_vma * offset)
4d6ed7c8 8240{
d3ba0551 8241 bfd_vma dist = 0x100000;
2cf0635d 8242 Elf_Internal_Sym * sym;
948f632f
DA
8243 Elf_Internal_Sym * beg;
8244 Elf_Internal_Sym * end;
2cf0635d 8245 Elf_Internal_Sym * best = NULL;
4d6ed7c8 8246
0b6ae522 8247 REMOVE_ARCH_BITS (addr.offset);
948f632f
DA
8248 beg = symtab;
8249 end = symtab + nsyms;
0b6ae522 8250
948f632f 8251 while (beg < end)
4d6ed7c8 8252 {
948f632f
DA
8253 bfd_vma value;
8254
8255 sym = beg + (end - beg) / 2;
0b6ae522 8256
948f632f 8257 value = sym->st_value;
0b6ae522
DJ
8258 REMOVE_ARCH_BITS (value);
8259
948f632f 8260 if (sym->st_name != 0
4d6ed7c8 8261 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
0b6ae522
DJ
8262 && addr.offset >= value
8263 && addr.offset - value < dist)
4d6ed7c8
NC
8264 {
8265 best = sym;
0b6ae522 8266 dist = addr.offset - value;
4d6ed7c8
NC
8267 if (!dist)
8268 break;
8269 }
948f632f
DA
8270
8271 if (addr.offset < value)
8272 end = sym;
8273 else
8274 beg = sym + 1;
4d6ed7c8 8275 }
1b31d05e 8276
4d6ed7c8
NC
8277 if (best)
8278 {
57346661 8279 *symname = (best->st_name >= strtab_size
2b692964 8280 ? _("<corrupt>") : strtab + best->st_name);
4d6ed7c8
NC
8281 *offset = dist;
8282 return;
8283 }
1b31d05e 8284
4d6ed7c8
NC
8285 *symname = NULL;
8286 *offset = addr.offset;
8287}
8288
32ec8896 8289static /* signed */ int
948f632f
DA
8290symcmp (const void *p, const void *q)
8291{
8292 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
8293 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
8294
8295 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
8296}
8297
8298/* Process the unwind section. */
8299
8300#include "unwind-ia64.h"
8301
8302struct ia64_unw_table_entry
8303{
8304 struct absaddr start;
8305 struct absaddr end;
8306 struct absaddr info;
8307};
8308
8309struct ia64_unw_aux_info
8310{
32ec8896
NC
8311 struct ia64_unw_table_entry * table; /* Unwind table. */
8312 unsigned long table_len; /* Length of unwind table. */
8313 unsigned char * info; /* Unwind info. */
8314 unsigned long info_size; /* Size of unwind info. */
8315 bfd_vma info_addr; /* Starting address of unwind info. */
8316 bfd_vma seg_base; /* Starting address of segment. */
8317 Elf_Internal_Sym * symtab; /* The symbol table. */
8318 unsigned long nsyms; /* Number of symbols. */
8319 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8320 unsigned long nfuns; /* Number of entries in funtab. */
8321 char * strtab; /* The string table. */
8322 unsigned long strtab_size; /* Size of string table. */
948f632f
DA
8323};
8324
015dc7e1 8325static bool
dda8d76d 8326dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
4d6ed7c8 8327{
2cf0635d 8328 struct ia64_unw_table_entry * tp;
948f632f 8329 unsigned long j, nfuns;
4d6ed7c8 8330 int in_body;
015dc7e1 8331 bool res = true;
7036c0e1 8332
948f632f
DA
8333 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8334 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8335 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8336 aux->funtab[nfuns++] = aux->symtab[j];
8337 aux->nfuns = nfuns;
8338 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8339
4d6ed7c8
NC
8340 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8341 {
8342 bfd_vma stamp;
8343 bfd_vma offset;
2cf0635d
NC
8344 const unsigned char * dp;
8345 const unsigned char * head;
53774b7e 8346 const unsigned char * end;
2cf0635d 8347 const char * procname;
4d6ed7c8 8348
dda8d76d 8349 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
57346661 8350 aux->strtab_size, tp->start, &procname, &offset);
4d6ed7c8
NC
8351
8352 fputs ("\n<", stdout);
8353
8354 if (procname)
8355 {
8356 fputs (procname, stdout);
8357
8358 if (offset)
8359 printf ("+%lx", (unsigned long) offset);
8360 }
8361
8362 fputs (">: [", stdout);
8363 print_vma (tp->start.offset, PREFIX_HEX);
8364 fputc ('-', stdout);
8365 print_vma (tp->end.offset, PREFIX_HEX);
86f55779 8366 printf ("], info at +0x%lx\n",
4d6ed7c8
NC
8367 (unsigned long) (tp->info.offset - aux->seg_base));
8368
53774b7e
NC
8369 /* PR 17531: file: 86232b32. */
8370 if (aux->info == NULL)
8371 continue;
8372
97c0a079
AM
8373 offset = tp->info.offset;
8374 if (tp->info.section)
8375 {
8376 if (tp->info.section >= filedata->file_header.e_shnum)
8377 {
8378 warn (_("Invalid section %u in table entry %ld\n"),
8379 tp->info.section, (long) (tp - aux->table));
015dc7e1 8380 res = false;
97c0a079
AM
8381 continue;
8382 }
8383 offset += filedata->section_headers[tp->info.section].sh_addr;
8384 }
8385 offset -= aux->info_addr;
53774b7e 8386 /* PR 17531: file: 0997b4d1. */
90679903
AM
8387 if (offset >= aux->info_size
8388 || aux->info_size - offset < 8)
53774b7e
NC
8389 {
8390 warn (_("Invalid offset %lx in table entry %ld\n"),
8391 (long) tp->info.offset, (long) (tp - aux->table));
015dc7e1 8392 res = false;
53774b7e
NC
8393 continue;
8394 }
8395
97c0a079 8396 head = aux->info + offset;
a4a00738 8397 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
4d6ed7c8 8398
86f55779 8399 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
4d6ed7c8
NC
8400 (unsigned) UNW_VER (stamp),
8401 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
8402 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
8403 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
89fac5e3 8404 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
4d6ed7c8
NC
8405
8406 if (UNW_VER (stamp) != 1)
8407 {
2b692964 8408 printf (_("\tUnknown version.\n"));
4d6ed7c8
NC
8409 continue;
8410 }
8411
8412 in_body = 0;
53774b7e
NC
8413 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
8414 /* PR 17531: file: 16ceda89. */
8415 if (end > aux->info + aux->info_size)
8416 end = aux->info + aux->info_size;
8417 for (dp = head + 8; dp < end;)
b4477bc8 8418 dp = unw_decode (dp, in_body, & in_body, end);
4d6ed7c8 8419 }
948f632f
DA
8420
8421 free (aux->funtab);
32ec8896
NC
8422
8423 return res;
4d6ed7c8
NC
8424}
8425
015dc7e1 8426static bool
dda8d76d
NC
8427slurp_ia64_unwind_table (Filedata * filedata,
8428 struct ia64_unw_aux_info * aux,
8429 Elf_Internal_Shdr * sec)
4d6ed7c8 8430{
89fac5e3 8431 unsigned long size, nrelas, i;
2cf0635d
NC
8432 Elf_Internal_Phdr * seg;
8433 struct ia64_unw_table_entry * tep;
8434 Elf_Internal_Shdr * relsec;
8435 Elf_Internal_Rela * rela;
8436 Elf_Internal_Rela * rp;
8437 unsigned char * table;
8438 unsigned char * tp;
8439 Elf_Internal_Sym * sym;
8440 const char * relname;
4d6ed7c8 8441
53774b7e
NC
8442 aux->table_len = 0;
8443
4d6ed7c8
NC
8444 /* First, find the starting address of the segment that includes
8445 this section: */
8446
dda8d76d 8447 if (filedata->file_header.e_phnum)
4d6ed7c8 8448 {
dda8d76d 8449 if (! get_program_headers (filedata))
015dc7e1 8450 return false;
4d6ed7c8 8451
dda8d76d
NC
8452 for (seg = filedata->program_headers;
8453 seg < filedata->program_headers + filedata->file_header.e_phnum;
d93f0186 8454 ++seg)
4d6ed7c8
NC
8455 {
8456 if (seg->p_type != PT_LOAD)
8457 continue;
8458
8459 if (sec->sh_addr >= seg->p_vaddr
8460 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8461 {
8462 aux->seg_base = seg->p_vaddr;
8463 break;
8464 }
8465 }
4d6ed7c8
NC
8466 }
8467
8468 /* Second, build the unwind table from the contents of the unwind section: */
8469 size = sec->sh_size;
dda8d76d 8470 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
3f5e193b 8471 _("unwind table"));
a6e9f9df 8472 if (!table)
015dc7e1 8473 return false;
4d6ed7c8 8474
53774b7e 8475 aux->table_len = size / (3 * eh_addr_size);
3f5e193b 8476 aux->table = (struct ia64_unw_table_entry *)
53774b7e 8477 xcmalloc (aux->table_len, sizeof (aux->table[0]));
89fac5e3 8478 tep = aux->table;
53774b7e
NC
8479
8480 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
4d6ed7c8
NC
8481 {
8482 tep->start.section = SHN_UNDEF;
8483 tep->end.section = SHN_UNDEF;
8484 tep->info.section = SHN_UNDEF;
c6a0c689
AM
8485 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8486 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8487 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
4d6ed7c8
NC
8488 tep->start.offset += aux->seg_base;
8489 tep->end.offset += aux->seg_base;
8490 tep->info.offset += aux->seg_base;
8491 }
8492 free (table);
8493
41e92641 8494 /* Third, apply any relocations to the unwind table: */
dda8d76d
NC
8495 for (relsec = filedata->section_headers;
8496 relsec < filedata->section_headers + filedata->file_header.e_shnum;
4d6ed7c8
NC
8497 ++relsec)
8498 {
8499 if (relsec->sh_type != SHT_RELA
dda8d76d
NC
8500 || relsec->sh_info >= filedata->file_header.e_shnum
8501 || filedata->section_headers + relsec->sh_info != sec)
4d6ed7c8
NC
8502 continue;
8503
dda8d76d 8504 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
4d6ed7c8 8505 & rela, & nrelas))
53774b7e
NC
8506 {
8507 free (aux->table);
8508 aux->table = NULL;
8509 aux->table_len = 0;
015dc7e1 8510 return false;
53774b7e 8511 }
4d6ed7c8
NC
8512
8513 for (rp = rela; rp < rela + nrelas; ++rp)
8514 {
4770fb94 8515 unsigned int sym_ndx;
726bd37d
AM
8516 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8517 relname = elf_ia64_reloc_type (r_type);
4d6ed7c8 8518
82b1b41b
NC
8519 /* PR 17531: file: 9fa67536. */
8520 if (relname == NULL)
8521 {
726bd37d 8522 warn (_("Skipping unknown relocation type: %u\n"), r_type);
82b1b41b
NC
8523 continue;
8524 }
948f632f 8525
24d127aa 8526 if (! startswith (relname, "R_IA64_SEGREL"))
4d6ed7c8 8527 {
82b1b41b 8528 warn (_("Skipping unexpected relocation type: %s\n"), relname);
4d6ed7c8
NC
8529 continue;
8530 }
8531
89fac5e3 8532 i = rp->r_offset / (3 * eh_addr_size);
4d6ed7c8 8533
53774b7e
NC
8534 /* PR 17531: file: 5bc8d9bf. */
8535 if (i >= aux->table_len)
8536 {
8537 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8538 continue;
8539 }
8540
4770fb94
AM
8541 sym_ndx = get_reloc_symindex (rp->r_info);
8542 if (sym_ndx >= aux->nsyms)
8543 {
8544 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8545 sym_ndx);
8546 continue;
8547 }
8548 sym = aux->symtab + sym_ndx;
8549
53774b7e 8550 switch (rp->r_offset / eh_addr_size % 3)
4d6ed7c8
NC
8551 {
8552 case 0:
8553 aux->table[i].start.section = sym->st_shndx;
e466bc6e 8554 aux->table[i].start.offset = rp->r_addend + sym->st_value;
4d6ed7c8
NC
8555 break;
8556 case 1:
8557 aux->table[i].end.section = sym->st_shndx;
e466bc6e 8558 aux->table[i].end.offset = rp->r_addend + sym->st_value;
4d6ed7c8
NC
8559 break;
8560 case 2:
8561 aux->table[i].info.section = sym->st_shndx;
e466bc6e 8562 aux->table[i].info.offset = rp->r_addend + sym->st_value;
4d6ed7c8
NC
8563 break;
8564 default:
8565 break;
8566 }
8567 }
8568
8569 free (rela);
8570 }
8571
015dc7e1 8572 return true;
4d6ed7c8
NC
8573}
8574
015dc7e1 8575static bool
dda8d76d 8576ia64_process_unwind (Filedata * filedata)
4d6ed7c8 8577{
2cf0635d
NC
8578 Elf_Internal_Shdr * sec;
8579 Elf_Internal_Shdr * unwsec = NULL;
89fac5e3 8580 unsigned long i, unwcount = 0, unwstart = 0;
57346661 8581 struct ia64_unw_aux_info aux;
015dc7e1 8582 bool res = true;
f1467e33 8583
4d6ed7c8
NC
8584 memset (& aux, 0, sizeof (aux));
8585
dda8d76d 8586 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
4d6ed7c8 8587 {
28d13567 8588 if (sec->sh_type == SHT_SYMTAB)
4d6ed7c8 8589 {
28d13567 8590 if (aux.symtab)
4082ef84 8591 {
28d13567
AM
8592 error (_("Multiple symbol tables encountered\n"));
8593 free (aux.symtab);
8594 aux.symtab = NULL;
4082ef84 8595 free (aux.strtab);
28d13567 8596 aux.strtab = NULL;
4082ef84 8597 }
28d13567
AM
8598 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8599 &aux.strtab, &aux.strtab_size))
015dc7e1 8600 return false;
4d6ed7c8
NC
8601 }
8602 else if (sec->sh_type == SHT_IA_64_UNWIND)
579f31ac
JJ
8603 unwcount++;
8604 }
8605
8606 if (!unwcount)
8607 printf (_("\nThere are no unwind sections in this file.\n"));
8608
8609 while (unwcount-- > 0)
8610 {
84714f86 8611 const char *suffix;
579f31ac
JJ
8612 size_t len, len2;
8613
dda8d76d
NC
8614 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
8615 i < filedata->file_header.e_shnum; ++i, ++sec)
579f31ac
JJ
8616 if (sec->sh_type == SHT_IA_64_UNWIND)
8617 {
8618 unwsec = sec;
8619 break;
8620 }
4082ef84
NC
8621 /* We have already counted the number of SHT_IA64_UNWIND
8622 sections so the loop above should never fail. */
8623 assert (unwsec != NULL);
579f31ac
JJ
8624
8625 unwstart = i + 1;
8626 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
8627
e4b17d5c
L
8628 if ((unwsec->sh_flags & SHF_GROUP) != 0)
8629 {
8630 /* We need to find which section group it is in. */
4082ef84 8631 struct group_list * g;
e4b17d5c 8632
978c4450
AM
8633 if (filedata->section_headers_groups == NULL
8634 || filedata->section_headers_groups[i] == NULL)
dda8d76d 8635 i = filedata->file_header.e_shnum;
4082ef84 8636 else
e4b17d5c 8637 {
978c4450 8638 g = filedata->section_headers_groups[i]->root;
18bd398b 8639
4082ef84
NC
8640 for (; g != NULL; g = g->next)
8641 {
dda8d76d 8642 sec = filedata->section_headers + g->section_index;
e4b17d5c 8643
84714f86
AM
8644 if (section_name_valid (filedata, sec)
8645 && streq (section_name (filedata, sec),
8646 ELF_STRING_ia64_unwind_info))
4082ef84
NC
8647 break;
8648 }
8649
8650 if (g == NULL)
dda8d76d 8651 i = filedata->file_header.e_shnum;
4082ef84 8652 }
e4b17d5c 8653 }
84714f86
AM
8654 else if (section_name_valid (filedata, unwsec)
8655 && startswith (section_name (filedata, unwsec),
e9b095a5 8656 ELF_STRING_ia64_unwind_once))
579f31ac 8657 {
18bd398b 8658 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
579f31ac 8659 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
84714f86 8660 suffix = section_name (filedata, unwsec) + len;
b9e920ec
AM
8661 for (i = 0, sec = filedata->section_headers;
8662 i < filedata->file_header.e_shnum;
579f31ac 8663 ++i, ++sec)
84714f86
AM
8664 if (section_name_valid (filedata, sec)
8665 && startswith (section_name (filedata, sec),
e9b095a5 8666 ELF_STRING_ia64_unwind_info_once)
84714f86 8667 && streq (section_name (filedata, sec) + len2, suffix))
579f31ac
JJ
8668 break;
8669 }
8670 else
8671 {
8672 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
18bd398b 8673 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
579f31ac
JJ
8674 len = sizeof (ELF_STRING_ia64_unwind) - 1;
8675 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
8676 suffix = "";
84714f86
AM
8677 if (section_name_valid (filedata, unwsec)
8678 && startswith (section_name (filedata, unwsec),
8679 ELF_STRING_ia64_unwind))
8680 suffix = section_name (filedata, unwsec) + len;
b9e920ec
AM
8681 for (i = 0, sec = filedata->section_headers;
8682 i < filedata->file_header.e_shnum;
579f31ac 8683 ++i, ++sec)
84714f86
AM
8684 if (section_name_valid (filedata, sec)
8685 && startswith (section_name (filedata, sec),
8686 ELF_STRING_ia64_unwind_info)
8687 && streq (section_name (filedata, sec) + len2, suffix))
579f31ac
JJ
8688 break;
8689 }
8690
dda8d76d 8691 if (i == filedata->file_header.e_shnum)
579f31ac
JJ
8692 {
8693 printf (_("\nCould not find unwind info section for "));
8694
dda8d76d 8695 if (filedata->string_table == NULL)
579f31ac
JJ
8696 printf ("%d", unwsec->sh_name);
8697 else
dda8d76d 8698 printf ("'%s'", printable_section_name (filedata, unwsec));
579f31ac
JJ
8699 }
8700 else
4d6ed7c8 8701 {
4d6ed7c8 8702 aux.info_addr = sec->sh_addr;
dda8d76d 8703 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
4082ef84
NC
8704 sec->sh_size,
8705 _("unwind info"));
59245841 8706 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
4d6ed7c8 8707
579f31ac 8708 printf (_("\nUnwind section "));
4d6ed7c8 8709
dda8d76d 8710 if (filedata->string_table == NULL)
579f31ac
JJ
8711 printf ("%d", unwsec->sh_name);
8712 else
dda8d76d 8713 printf ("'%s'", printable_section_name (filedata, unwsec));
4d6ed7c8 8714
579f31ac 8715 printf (_(" at offset 0x%lx contains %lu entries:\n"),
e59b4dfb 8716 (unsigned long) unwsec->sh_offset,
89fac5e3 8717 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
4d6ed7c8 8718
dda8d76d 8719 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
53774b7e 8720 && aux.table_len > 0)
dda8d76d 8721 dump_ia64_unwind (filedata, & aux);
579f31ac 8722
9db70fc3
AM
8723 free ((char *) aux.table);
8724 free ((char *) aux.info);
579f31ac
JJ
8725 aux.table = NULL;
8726 aux.info = NULL;
8727 }
4d6ed7c8 8728 }
4d6ed7c8 8729
9db70fc3
AM
8730 free (aux.symtab);
8731 free ((char *) aux.strtab);
32ec8896
NC
8732
8733 return res;
4d6ed7c8
NC
8734}
8735
3f5e193b 8736struct hppa_unw_table_entry
32ec8896
NC
8737{
8738 struct absaddr start;
8739 struct absaddr end;
8740 unsigned int Cannot_unwind:1; /* 0 */
8741 unsigned int Millicode:1; /* 1 */
8742 unsigned int Millicode_save_sr0:1; /* 2 */
8743 unsigned int Region_description:2; /* 3..4 */
8744 unsigned int reserved1:1; /* 5 */
8745 unsigned int Entry_SR:1; /* 6 */
8746 unsigned int Entry_FR:4; /* Number saved 7..10 */
8747 unsigned int Entry_GR:5; /* Number saved 11..15 */
8748 unsigned int Args_stored:1; /* 16 */
8749 unsigned int Variable_Frame:1; /* 17 */
8750 unsigned int Separate_Package_Body:1; /* 18 */
8751 unsigned int Frame_Extension_Millicode:1; /* 19 */
8752 unsigned int Stack_Overflow_Check:1; /* 20 */
8753 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
8754 unsigned int Ada_Region:1; /* 22 */
8755 unsigned int cxx_info:1; /* 23 */
8756 unsigned int cxx_try_catch:1; /* 24 */
8757 unsigned int sched_entry_seq:1; /* 25 */
8758 unsigned int reserved2:1; /* 26 */
8759 unsigned int Save_SP:1; /* 27 */
8760 unsigned int Save_RP:1; /* 28 */
8761 unsigned int Save_MRP_in_frame:1; /* 29 */
8762 unsigned int extn_ptr_defined:1; /* 30 */
8763 unsigned int Cleanup_defined:1; /* 31 */
8764
8765 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
8766 unsigned int HP_UX_interrupt_marker:1; /* 1 */
8767 unsigned int Large_frame:1; /* 2 */
8768 unsigned int Pseudo_SP_Set:1; /* 3 */
8769 unsigned int reserved4:1; /* 4 */
8770 unsigned int Total_frame_size:27; /* 5..31 */
8771};
3f5e193b 8772
57346661 8773struct hppa_unw_aux_info
948f632f 8774{
32ec8896
NC
8775 struct hppa_unw_table_entry * table; /* Unwind table. */
8776 unsigned long table_len; /* Length of unwind table. */
8777 bfd_vma seg_base; /* Starting address of segment. */
8778 Elf_Internal_Sym * symtab; /* The symbol table. */
8779 unsigned long nsyms; /* Number of symbols. */
8780 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8781 unsigned long nfuns; /* Number of entries in funtab. */
8782 char * strtab; /* The string table. */
8783 unsigned long strtab_size; /* Size of string table. */
948f632f 8784};
57346661 8785
015dc7e1 8786static bool
dda8d76d 8787dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
57346661 8788{
2cf0635d 8789 struct hppa_unw_table_entry * tp;
948f632f 8790 unsigned long j, nfuns;
015dc7e1 8791 bool res = true;
948f632f
DA
8792
8793 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8794 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8795 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8796 aux->funtab[nfuns++] = aux->symtab[j];
8797 aux->nfuns = nfuns;
8798 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
57346661 8799
57346661
AM
8800 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8801 {
8802 bfd_vma offset;
2cf0635d 8803 const char * procname;
57346661 8804
dda8d76d 8805 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
57346661
AM
8806 aux->strtab_size, tp->start, &procname,
8807 &offset);
8808
8809 fputs ("\n<", stdout);
8810
8811 if (procname)
8812 {
8813 fputs (procname, stdout);
8814
8815 if (offset)
8816 printf ("+%lx", (unsigned long) offset);
8817 }
8818
8819 fputs (">: [", stdout);
8820 print_vma (tp->start.offset, PREFIX_HEX);
8821 fputc ('-', stdout);
8822 print_vma (tp->end.offset, PREFIX_HEX);
8823 printf ("]\n\t");
8824
18bd398b
NC
8825#define PF(_m) if (tp->_m) printf (#_m " ");
8826#define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
57346661
AM
8827 PF(Cannot_unwind);
8828 PF(Millicode);
8829 PF(Millicode_save_sr0);
18bd398b 8830 /* PV(Region_description); */
57346661
AM
8831 PF(Entry_SR);
8832 PV(Entry_FR);
8833 PV(Entry_GR);
8834 PF(Args_stored);
8835 PF(Variable_Frame);
8836 PF(Separate_Package_Body);
8837 PF(Frame_Extension_Millicode);
8838 PF(Stack_Overflow_Check);
8839 PF(Two_Instruction_SP_Increment);
8840 PF(Ada_Region);
8841 PF(cxx_info);
8842 PF(cxx_try_catch);
8843 PF(sched_entry_seq);
8844 PF(Save_SP);
8845 PF(Save_RP);
8846 PF(Save_MRP_in_frame);
8847 PF(extn_ptr_defined);
8848 PF(Cleanup_defined);
8849 PF(MPE_XL_interrupt_marker);
8850 PF(HP_UX_interrupt_marker);
8851 PF(Large_frame);
8852 PF(Pseudo_SP_Set);
8853 PV(Total_frame_size);
8854#undef PF
8855#undef PV
8856 }
8857
18bd398b 8858 printf ("\n");
948f632f
DA
8859
8860 free (aux->funtab);
32ec8896
NC
8861
8862 return res;
57346661
AM
8863}
8864
015dc7e1 8865static bool
dda8d76d
NC
8866slurp_hppa_unwind_table (Filedata * filedata,
8867 struct hppa_unw_aux_info * aux,
8868 Elf_Internal_Shdr * sec)
57346661 8869{
1c0751b2 8870 unsigned long size, unw_ent_size, nentries, nrelas, i;
2cf0635d
NC
8871 Elf_Internal_Phdr * seg;
8872 struct hppa_unw_table_entry * tep;
8873 Elf_Internal_Shdr * relsec;
8874 Elf_Internal_Rela * rela;
8875 Elf_Internal_Rela * rp;
8876 unsigned char * table;
8877 unsigned char * tp;
8878 Elf_Internal_Sym * sym;
8879 const char * relname;
57346661 8880
57346661
AM
8881 /* First, find the starting address of the segment that includes
8882 this section. */
dda8d76d 8883 if (filedata->file_header.e_phnum)
57346661 8884 {
dda8d76d 8885 if (! get_program_headers (filedata))
015dc7e1 8886 return false;
57346661 8887
dda8d76d
NC
8888 for (seg = filedata->program_headers;
8889 seg < filedata->program_headers + filedata->file_header.e_phnum;
57346661
AM
8890 ++seg)
8891 {
8892 if (seg->p_type != PT_LOAD)
8893 continue;
8894
8895 if (sec->sh_addr >= seg->p_vaddr
8896 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8897 {
8898 aux->seg_base = seg->p_vaddr;
8899 break;
8900 }
8901 }
8902 }
8903
8904 /* Second, build the unwind table from the contents of the unwind
8905 section. */
8906 size = sec->sh_size;
dda8d76d 8907 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
3f5e193b 8908 _("unwind table"));
57346661 8909 if (!table)
015dc7e1 8910 return false;
57346661 8911
1c0751b2
DA
8912 unw_ent_size = 16;
8913 nentries = size / unw_ent_size;
8914 size = unw_ent_size * nentries;
57346661 8915
e3fdc001 8916 aux->table_len = nentries;
3f5e193b
NC
8917 tep = aux->table = (struct hppa_unw_table_entry *)
8918 xcmalloc (nentries, sizeof (aux->table[0]));
57346661 8919
1c0751b2 8920 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
57346661
AM
8921 {
8922 unsigned int tmp1, tmp2;
8923
8924 tep->start.section = SHN_UNDEF;
8925 tep->end.section = SHN_UNDEF;
8926
1c0751b2
DA
8927 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
8928 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
8929 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
8930 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
8931
8932 tep->start.offset += aux->seg_base;
8933 tep->end.offset += aux->seg_base;
57346661
AM
8934
8935 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
8936 tep->Millicode = (tmp1 >> 30) & 0x1;
8937 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
8938 tep->Region_description = (tmp1 >> 27) & 0x3;
8939 tep->reserved1 = (tmp1 >> 26) & 0x1;
8940 tep->Entry_SR = (tmp1 >> 25) & 0x1;
8941 tep->Entry_FR = (tmp1 >> 21) & 0xf;
8942 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
8943 tep->Args_stored = (tmp1 >> 15) & 0x1;
8944 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
8945 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
8946 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
8947 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
8948 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
8949 tep->Ada_Region = (tmp1 >> 9) & 0x1;
8950 tep->cxx_info = (tmp1 >> 8) & 0x1;
8951 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
8952 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
8953 tep->reserved2 = (tmp1 >> 5) & 0x1;
8954 tep->Save_SP = (tmp1 >> 4) & 0x1;
8955 tep->Save_RP = (tmp1 >> 3) & 0x1;
8956 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
8957 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
8958 tep->Cleanup_defined = tmp1 & 0x1;
8959
8960 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
8961 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
8962 tep->Large_frame = (tmp2 >> 29) & 0x1;
8963 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
8964 tep->reserved4 = (tmp2 >> 27) & 0x1;
8965 tep->Total_frame_size = tmp2 & 0x7ffffff;
57346661
AM
8966 }
8967 free (table);
8968
8969 /* Third, apply any relocations to the unwind table. */
dda8d76d
NC
8970 for (relsec = filedata->section_headers;
8971 relsec < filedata->section_headers + filedata->file_header.e_shnum;
57346661
AM
8972 ++relsec)
8973 {
8974 if (relsec->sh_type != SHT_RELA
dda8d76d
NC
8975 || relsec->sh_info >= filedata->file_header.e_shnum
8976 || filedata->section_headers + relsec->sh_info != sec)
57346661
AM
8977 continue;
8978
dda8d76d 8979 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
57346661 8980 & rela, & nrelas))
015dc7e1 8981 return false;
57346661
AM
8982
8983 for (rp = rela; rp < rela + nrelas; ++rp)
8984 {
4770fb94 8985 unsigned int sym_ndx;
726bd37d
AM
8986 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8987 relname = elf_hppa_reloc_type (r_type);
57346661 8988
726bd37d
AM
8989 if (relname == NULL)
8990 {
8991 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8992 continue;
8993 }
8994
57346661 8995 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
24d127aa 8996 if (! startswith (relname, "R_PARISC_SEGREL"))
57346661 8997 {
726bd37d 8998 warn (_("Skipping unexpected relocation type: %s\n"), relname);
57346661
AM
8999 continue;
9000 }
9001
9002 i = rp->r_offset / unw_ent_size;
726bd37d
AM
9003 if (i >= aux->table_len)
9004 {
9005 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
9006 continue;
9007 }
57346661 9008
4770fb94
AM
9009 sym_ndx = get_reloc_symindex (rp->r_info);
9010 if (sym_ndx >= aux->nsyms)
9011 {
9012 warn (_("Skipping reloc with invalid symbol index: %u\n"),
9013 sym_ndx);
9014 continue;
9015 }
9016 sym = aux->symtab + sym_ndx;
9017
43f6cd05 9018 switch ((rp->r_offset % unw_ent_size) / 4)
57346661
AM
9019 {
9020 case 0:
9021 aux->table[i].start.section = sym->st_shndx;
1e456d54 9022 aux->table[i].start.offset = sym->st_value + rp->r_addend;
57346661
AM
9023 break;
9024 case 1:
9025 aux->table[i].end.section = sym->st_shndx;
1e456d54 9026 aux->table[i].end.offset = sym->st_value + rp->r_addend;
57346661
AM
9027 break;
9028 default:
9029 break;
9030 }
9031 }
9032
9033 free (rela);
9034 }
9035
015dc7e1 9036 return true;
57346661
AM
9037}
9038
015dc7e1 9039static bool
dda8d76d 9040hppa_process_unwind (Filedata * filedata)
57346661 9041{
57346661 9042 struct hppa_unw_aux_info aux;
2cf0635d 9043 Elf_Internal_Shdr * unwsec = NULL;
2cf0635d 9044 Elf_Internal_Shdr * sec;
18bd398b 9045 unsigned long i;
015dc7e1 9046 bool res = true;
57346661 9047
dda8d76d 9048 if (filedata->string_table == NULL)
015dc7e1 9049 return false;
1b31d05e
NC
9050
9051 memset (& aux, 0, sizeof (aux));
57346661 9052
dda8d76d 9053 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
57346661 9054 {
28d13567 9055 if (sec->sh_type == SHT_SYMTAB)
57346661 9056 {
28d13567 9057 if (aux.symtab)
4082ef84 9058 {
28d13567
AM
9059 error (_("Multiple symbol tables encountered\n"));
9060 free (aux.symtab);
9061 aux.symtab = NULL;
4082ef84 9062 free (aux.strtab);
28d13567 9063 aux.strtab = NULL;
4082ef84 9064 }
28d13567
AM
9065 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9066 &aux.strtab, &aux.strtab_size))
015dc7e1 9067 return false;
57346661 9068 }
84714f86
AM
9069 else if (section_name_valid (filedata, sec)
9070 && streq (section_name (filedata, sec), ".PARISC.unwind"))
57346661
AM
9071 unwsec = sec;
9072 }
9073
9074 if (!unwsec)
9075 printf (_("\nThere are no unwind sections in this file.\n"));
9076
dda8d76d 9077 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
57346661 9078 {
84714f86
AM
9079 if (section_name_valid (filedata, sec)
9080 && streq (section_name (filedata, sec), ".PARISC.unwind"))
57346661 9081 {
43f6cd05 9082 unsigned long num_unwind = sec->sh_size / 16;
dda8d76d 9083
d3a49aa8
AM
9084 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
9085 "contains %lu entry:\n",
9086 "\nUnwind section '%s' at offset 0x%lx "
9087 "contains %lu entries:\n",
9088 num_unwind),
dda8d76d 9089 printable_section_name (filedata, sec),
57346661 9090 (unsigned long) sec->sh_offset,
d3a49aa8 9091 num_unwind);
57346661 9092
dda8d76d 9093 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
015dc7e1 9094 res = false;
66b09c7e
S
9095
9096 if (res && aux.table_len > 0)
32ec8896 9097 {
dda8d76d 9098 if (! dump_hppa_unwind (filedata, &aux))
015dc7e1 9099 res = false;
32ec8896 9100 }
57346661 9101
9db70fc3 9102 free ((char *) aux.table);
57346661
AM
9103 aux.table = NULL;
9104 }
9105 }
9106
9db70fc3
AM
9107 free (aux.symtab);
9108 free ((char *) aux.strtab);
32ec8896
NC
9109
9110 return res;
57346661
AM
9111}
9112
0b6ae522
DJ
9113struct arm_section
9114{
a734115a
NC
9115 unsigned char * data; /* The unwind data. */
9116 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
9117 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
9118 unsigned long nrelas; /* The number of relocations. */
9119 unsigned int rel_type; /* REL or RELA ? */
9120 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
0b6ae522
DJ
9121};
9122
9123struct arm_unw_aux_info
9124{
dda8d76d 9125 Filedata * filedata; /* The file containing the unwind sections. */
a734115a
NC
9126 Elf_Internal_Sym * symtab; /* The file's symbol table. */
9127 unsigned long nsyms; /* Number of symbols. */
948f632f
DA
9128 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9129 unsigned long nfuns; /* Number of these symbols. */
a734115a
NC
9130 char * strtab; /* The file's string table. */
9131 unsigned long strtab_size; /* Size of string table. */
0b6ae522
DJ
9132};
9133
9134static const char *
dda8d76d
NC
9135arm_print_vma_and_name (Filedata * filedata,
9136 struct arm_unw_aux_info * aux,
9137 bfd_vma fn,
9138 struct absaddr addr)
0b6ae522
DJ
9139{
9140 const char *procname;
9141 bfd_vma sym_offset;
9142
9143 if (addr.section == SHN_UNDEF)
9144 addr.offset = fn;
9145
dda8d76d 9146 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
0b6ae522
DJ
9147 aux->strtab_size, addr, &procname,
9148 &sym_offset);
9149
9150 print_vma (fn, PREFIX_HEX);
9151
9152 if (procname)
9153 {
9154 fputs (" <", stdout);
9155 fputs (procname, stdout);
9156
9157 if (sym_offset)
9158 printf ("+0x%lx", (unsigned long) sym_offset);
9159 fputc ('>', stdout);
9160 }
9161
9162 return procname;
9163}
9164
9165static void
9166arm_free_section (struct arm_section *arm_sec)
9167{
9db70fc3
AM
9168 free (arm_sec->data);
9169 free (arm_sec->rela);
0b6ae522
DJ
9170}
9171
a734115a
NC
9172/* 1) If SEC does not match the one cached in ARM_SEC, then free the current
9173 cached section and install SEC instead.
9174 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
9175 and return its valued in * WORDP, relocating if necessary.
1b31d05e 9176 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
a734115a 9177 relocation's offset in ADDR.
1b31d05e
NC
9178 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
9179 into the string table of the symbol associated with the reloc. If no
9180 reloc was applied store -1 there.
9181 5) Return TRUE upon success, FALSE otherwise. */
a734115a 9182
015dc7e1 9183static bool
dda8d76d
NC
9184get_unwind_section_word (Filedata * filedata,
9185 struct arm_unw_aux_info * aux,
1b31d05e
NC
9186 struct arm_section * arm_sec,
9187 Elf_Internal_Shdr * sec,
9188 bfd_vma word_offset,
9189 unsigned int * wordp,
9190 struct absaddr * addr,
9191 bfd_vma * sym_name)
0b6ae522
DJ
9192{
9193 Elf_Internal_Rela *rp;
9194 Elf_Internal_Sym *sym;
9195 const char * relname;
9196 unsigned int word;
015dc7e1 9197 bool wrapped;
0b6ae522 9198
e0a31db1 9199 if (sec == NULL || arm_sec == NULL)
015dc7e1 9200 return false;
e0a31db1 9201
0b6ae522
DJ
9202 addr->section = SHN_UNDEF;
9203 addr->offset = 0;
9204
1b31d05e
NC
9205 if (sym_name != NULL)
9206 *sym_name = (bfd_vma) -1;
9207
a734115a 9208 /* If necessary, update the section cache. */
0b6ae522
DJ
9209 if (sec != arm_sec->sec)
9210 {
9211 Elf_Internal_Shdr *relsec;
9212
9213 arm_free_section (arm_sec);
9214
9215 arm_sec->sec = sec;
dda8d76d 9216 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
0b6ae522 9217 sec->sh_size, _("unwind data"));
0b6ae522
DJ
9218 arm_sec->rela = NULL;
9219 arm_sec->nrelas = 0;
9220
dda8d76d
NC
9221 for (relsec = filedata->section_headers;
9222 relsec < filedata->section_headers + filedata->file_header.e_shnum;
0b6ae522
DJ
9223 ++relsec)
9224 {
dda8d76d
NC
9225 if (relsec->sh_info >= filedata->file_header.e_shnum
9226 || filedata->section_headers + relsec->sh_info != sec
1ae40aa4
NC
9227 /* PR 15745: Check the section type as well. */
9228 || (relsec->sh_type != SHT_REL
9229 && relsec->sh_type != SHT_RELA))
0b6ae522
DJ
9230 continue;
9231
a734115a 9232 arm_sec->rel_type = relsec->sh_type;
0b6ae522
DJ
9233 if (relsec->sh_type == SHT_REL)
9234 {
dda8d76d 9235 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
0b6ae522
DJ
9236 relsec->sh_size,
9237 & arm_sec->rela, & arm_sec->nrelas))
015dc7e1 9238 return false;
0b6ae522 9239 }
1ae40aa4 9240 else /* relsec->sh_type == SHT_RELA */
0b6ae522 9241 {
dda8d76d 9242 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
0b6ae522
DJ
9243 relsec->sh_size,
9244 & arm_sec->rela, & arm_sec->nrelas))
015dc7e1 9245 return false;
0b6ae522 9246 }
1ae40aa4 9247 break;
0b6ae522
DJ
9248 }
9249
9250 arm_sec->next_rela = arm_sec->rela;
9251 }
9252
a734115a 9253 /* If there is no unwind data we can do nothing. */
0b6ae522 9254 if (arm_sec->data == NULL)
015dc7e1 9255 return false;
0b6ae522 9256
e0a31db1 9257 /* If the offset is invalid then fail. */
f32ba729
NC
9258 if (/* PR 21343 *//* PR 18879 */
9259 sec->sh_size < 4
9260 || word_offset > (sec->sh_size - 4)
1a915552 9261 || ((bfd_signed_vma) word_offset) < 0)
015dc7e1 9262 return false;
e0a31db1 9263
a734115a 9264 /* Get the word at the required offset. */
0b6ae522
DJ
9265 word = byte_get (arm_sec->data + word_offset, 4);
9266
0eff7165
NC
9267 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
9268 if (arm_sec->rela == NULL)
9269 {
9270 * wordp = word;
015dc7e1 9271 return true;
0eff7165
NC
9272 }
9273
a734115a 9274 /* Look through the relocs to find the one that applies to the provided offset. */
015dc7e1 9275 wrapped = false;
0b6ae522
DJ
9276 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
9277 {
9278 bfd_vma prelval, offset;
9279
9280 if (rp->r_offset > word_offset && !wrapped)
9281 {
9282 rp = arm_sec->rela;
015dc7e1 9283 wrapped = true;
0b6ae522
DJ
9284 }
9285 if (rp->r_offset > word_offset)
9286 break;
9287
9288 if (rp->r_offset & 3)
9289 {
9290 warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
9291 (unsigned long) rp->r_offset);
9292 continue;
9293 }
9294
9295 if (rp->r_offset < word_offset)
9296 continue;
9297
74e1a04b
NC
9298 /* PR 17531: file: 027-161405-0.004 */
9299 if (aux->symtab == NULL)
9300 continue;
9301
0b6ae522
DJ
9302 if (arm_sec->rel_type == SHT_REL)
9303 {
9304 offset = word & 0x7fffffff;
9305 if (offset & 0x40000000)
9306 offset |= ~ (bfd_vma) 0x7fffffff;
9307 }
a734115a 9308 else if (arm_sec->rel_type == SHT_RELA)
0b6ae522 9309 offset = rp->r_addend;
a734115a 9310 else
74e1a04b
NC
9311 {
9312 error (_("Unknown section relocation type %d encountered\n"),
9313 arm_sec->rel_type);
9314 break;
9315 }
0b6ae522 9316
071436c6
NC
9317 /* PR 17531 file: 027-1241568-0.004. */
9318 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
9319 {
9320 error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
9321 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
9322 break;
9323 }
9324
9325 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
0b6ae522
DJ
9326 offset += sym->st_value;
9327 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
9328
a734115a 9329 /* Check that we are processing the expected reloc type. */
dda8d76d 9330 if (filedata->file_header.e_machine == EM_ARM)
a734115a
NC
9331 {
9332 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
071436c6
NC
9333 if (relname == NULL)
9334 {
9335 warn (_("Skipping unknown ARM relocation type: %d\n"),
9336 (int) ELF32_R_TYPE (rp->r_info));
9337 continue;
9338 }
a734115a
NC
9339
9340 if (streq (relname, "R_ARM_NONE"))
9341 continue;
0b4362b0 9342
a734115a
NC
9343 if (! streq (relname, "R_ARM_PREL31"))
9344 {
071436c6 9345 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
a734115a
NC
9346 continue;
9347 }
9348 }
dda8d76d 9349 else if (filedata->file_header.e_machine == EM_TI_C6000)
a734115a
NC
9350 {
9351 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
071436c6
NC
9352 if (relname == NULL)
9353 {
9354 warn (_("Skipping unknown C6000 relocation type: %d\n"),
9355 (int) ELF32_R_TYPE (rp->r_info));
9356 continue;
9357 }
0b4362b0 9358
a734115a
NC
9359 if (streq (relname, "R_C6000_NONE"))
9360 continue;
9361
9362 if (! streq (relname, "R_C6000_PREL31"))
9363 {
071436c6 9364 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
a734115a
NC
9365 continue;
9366 }
9367
9368 prelval >>= 1;
9369 }
9370 else
74e1a04b
NC
9371 {
9372 /* This function currently only supports ARM and TI unwinders. */
9373 warn (_("Only TI and ARM unwinders are currently supported\n"));
9374 break;
9375 }
fa197c1c 9376
0b6ae522
DJ
9377 word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
9378 addr->section = sym->st_shndx;
9379 addr->offset = offset;
74e1a04b 9380
1b31d05e
NC
9381 if (sym_name)
9382 * sym_name = sym->st_name;
0b6ae522
DJ
9383 break;
9384 }
9385
9386 *wordp = word;
9387 arm_sec->next_rela = rp;
9388
015dc7e1 9389 return true;
0b6ae522
DJ
9390}
9391
a734115a
NC
9392static const char *tic6x_unwind_regnames[16] =
9393{
0b4362b0
RM
9394 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
9395 "A14", "A13", "A12", "A11", "A10",
a734115a
NC
9396 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
9397};
fa197c1c 9398
0b6ae522 9399static void
fa197c1c 9400decode_tic6x_unwind_regmask (unsigned int mask)
0b6ae522 9401{
fa197c1c
PB
9402 int i;
9403
9404 for (i = 12; mask; mask >>= 1, i--)
9405 {
9406 if (mask & 1)
9407 {
9408 fputs (tic6x_unwind_regnames[i], stdout);
9409 if (mask > 1)
9410 fputs (", ", stdout);
9411 }
9412 }
9413}
0b6ae522
DJ
9414
9415#define ADVANCE \
9416 if (remaining == 0 && more_words) \
9417 { \
9418 data_offset += 4; \
dda8d76d 9419 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
1b31d05e 9420 data_offset, & word, & addr, NULL)) \
015dc7e1 9421 return false; \
0b6ae522
DJ
9422 remaining = 4; \
9423 more_words--; \
9424 } \
9425
9426#define GET_OP(OP) \
9427 ADVANCE; \
9428 if (remaining) \
9429 { \
9430 remaining--; \
9431 (OP) = word >> 24; \
9432 word <<= 8; \
9433 } \
9434 else \
9435 { \
2b692964 9436 printf (_("[Truncated opcode]\n")); \
015dc7e1 9437 return false; \
0b6ae522 9438 } \
cc5914eb 9439 printf ("0x%02x ", OP)
0b6ae522 9440
015dc7e1 9441static bool
dda8d76d
NC
9442decode_arm_unwind_bytecode (Filedata * filedata,
9443 struct arm_unw_aux_info * aux,
948f632f
DA
9444 unsigned int word,
9445 unsigned int remaining,
9446 unsigned int more_words,
9447 bfd_vma data_offset,
9448 Elf_Internal_Shdr * data_sec,
9449 struct arm_section * data_arm_sec)
fa197c1c
PB
9450{
9451 struct absaddr addr;
015dc7e1 9452 bool res = true;
0b6ae522
DJ
9453
9454 /* Decode the unwinding instructions. */
9455 while (1)
9456 {
9457 unsigned int op, op2;
9458
9459 ADVANCE;
9460 if (remaining == 0)
9461 break;
9462 remaining--;
9463 op = word >> 24;
9464 word <<= 8;
9465
cc5914eb 9466 printf (" 0x%02x ", op);
0b6ae522
DJ
9467
9468 if ((op & 0xc0) == 0x00)
9469 {
9470 int offset = ((op & 0x3f) << 2) + 4;
61865e30 9471
cc5914eb 9472 printf (" vsp = vsp + %d", offset);
0b6ae522
DJ
9473 }
9474 else if ((op & 0xc0) == 0x40)
9475 {
9476 int offset = ((op & 0x3f) << 2) + 4;
61865e30 9477
cc5914eb 9478 printf (" vsp = vsp - %d", offset);
0b6ae522
DJ
9479 }
9480 else if ((op & 0xf0) == 0x80)
9481 {
9482 GET_OP (op2);
9483 if (op == 0x80 && op2 == 0)
9484 printf (_("Refuse to unwind"));
9485 else
9486 {
9487 unsigned int mask = ((op & 0x0f) << 8) | op2;
015dc7e1 9488 bool first = true;
0b6ae522 9489 int i;
2b692964 9490
0b6ae522
DJ
9491 printf ("pop {");
9492 for (i = 0; i < 12; i++)
9493 if (mask & (1 << i))
9494 {
9495 if (first)
015dc7e1 9496 first = false;
0b6ae522
DJ
9497 else
9498 printf (", ");
9499 printf ("r%d", 4 + i);
9500 }
9501 printf ("}");
9502 }
9503 }
9504 else if ((op & 0xf0) == 0x90)
9505 {
9506 if (op == 0x9d || op == 0x9f)
9507 printf (_(" [Reserved]"));
9508 else
cc5914eb 9509 printf (" vsp = r%d", op & 0x0f);
0b6ae522
DJ
9510 }
9511 else if ((op & 0xf0) == 0xa0)
9512 {
9513 int end = 4 + (op & 0x07);
015dc7e1 9514 bool first = true;
0b6ae522 9515 int i;
61865e30 9516
0b6ae522
DJ
9517 printf (" pop {");
9518 for (i = 4; i <= end; i++)
9519 {
9520 if (first)
015dc7e1 9521 first = false;
0b6ae522
DJ
9522 else
9523 printf (", ");
9524 printf ("r%d", i);
9525 }
9526 if (op & 0x08)
9527 {
1b31d05e 9528 if (!first)
0b6ae522
DJ
9529 printf (", ");
9530 printf ("r14");
9531 }
9532 printf ("}");
9533 }
9534 else if (op == 0xb0)
9535 printf (_(" finish"));
9536 else if (op == 0xb1)
9537 {
9538 GET_OP (op2);
9539 if (op2 == 0 || (op2 & 0xf0) != 0)
9540 printf (_("[Spare]"));
9541 else
9542 {
9543 unsigned int mask = op2 & 0x0f;
015dc7e1 9544 bool first = true;
0b6ae522 9545 int i;
61865e30 9546
0b6ae522
DJ
9547 printf ("pop {");
9548 for (i = 0; i < 12; i++)
9549 if (mask & (1 << i))
9550 {
9551 if (first)
015dc7e1 9552 first = false;
0b6ae522
DJ
9553 else
9554 printf (", ");
9555 printf ("r%d", i);
9556 }
9557 printf ("}");
9558 }
9559 }
9560 else if (op == 0xb2)
9561 {
b115cf96 9562 unsigned char buf[9];
0b6ae522
DJ
9563 unsigned int i, len;
9564 unsigned long offset;
61865e30 9565
b115cf96 9566 for (i = 0; i < sizeof (buf); i++)
0b6ae522
DJ
9567 {
9568 GET_OP (buf[i]);
9569 if ((buf[i] & 0x80) == 0)
9570 break;
9571 }
4082ef84 9572 if (i == sizeof (buf))
32ec8896 9573 {
27a45f42 9574 error (_("corrupt change to vsp\n"));
015dc7e1 9575 res = false;
32ec8896 9576 }
4082ef84
NC
9577 else
9578 {
015dc7e1 9579 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
4082ef84
NC
9580 assert (len == i + 1);
9581 offset = offset * 4 + 0x204;
9582 printf ("vsp = vsp + %ld", offset);
9583 }
0b6ae522 9584 }
61865e30 9585 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
0b6ae522 9586 {
61865e30
NC
9587 unsigned int first, last;
9588
9589 GET_OP (op2);
9590 first = op2 >> 4;
9591 last = op2 & 0x0f;
9592 if (op == 0xc8)
9593 first = first + 16;
9594 printf ("pop {D%d", first);
9595 if (last)
9596 printf ("-D%d", first + last);
9597 printf ("}");
9598 }
09854a88
TB
9599 else if (op == 0xb4)
9600 printf (_(" pop {ra_auth_code}"));
61865e30
NC
9601 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
9602 {
9603 unsigned int count = op & 0x07;
9604
9605 printf ("pop {D8");
9606 if (count)
9607 printf ("-D%d", 8 + count);
9608 printf ("}");
9609 }
9610 else if (op >= 0xc0 && op <= 0xc5)
9611 {
9612 unsigned int count = op & 0x07;
9613
9614 printf (" pop {wR10");
9615 if (count)
9616 printf ("-wR%d", 10 + count);
9617 printf ("}");
9618 }
9619 else if (op == 0xc6)
9620 {
9621 unsigned int first, last;
9622
9623 GET_OP (op2);
9624 first = op2 >> 4;
9625 last = op2 & 0x0f;
9626 printf ("pop {wR%d", first);
9627 if (last)
9628 printf ("-wR%d", first + last);
9629 printf ("}");
9630 }
9631 else if (op == 0xc7)
9632 {
9633 GET_OP (op2);
9634 if (op2 == 0 || (op2 & 0xf0) != 0)
9635 printf (_("[Spare]"));
0b6ae522
DJ
9636 else
9637 {
61865e30 9638 unsigned int mask = op2 & 0x0f;
015dc7e1 9639 bool first = true;
61865e30
NC
9640 int i;
9641
9642 printf ("pop {");
9643 for (i = 0; i < 4; i++)
9644 if (mask & (1 << i))
9645 {
9646 if (first)
015dc7e1 9647 first = false;
61865e30
NC
9648 else
9649 printf (", ");
9650 printf ("wCGR%d", i);
9651 }
9652 printf ("}");
0b6ae522
DJ
9653 }
9654 }
61865e30 9655 else
32ec8896
NC
9656 {
9657 printf (_(" [unsupported opcode]"));
015dc7e1 9658 res = false;
32ec8896
NC
9659 }
9660
0b6ae522
DJ
9661 printf ("\n");
9662 }
32ec8896
NC
9663
9664 return res;
fa197c1c
PB
9665}
9666
015dc7e1 9667static bool
dda8d76d
NC
9668decode_tic6x_unwind_bytecode (Filedata * filedata,
9669 struct arm_unw_aux_info * aux,
948f632f
DA
9670 unsigned int word,
9671 unsigned int remaining,
9672 unsigned int more_words,
9673 bfd_vma data_offset,
9674 Elf_Internal_Shdr * data_sec,
9675 struct arm_section * data_arm_sec)
fa197c1c
PB
9676{
9677 struct absaddr addr;
9678
9679 /* Decode the unwinding instructions. */
9680 while (1)
9681 {
9682 unsigned int op, op2;
9683
9684 ADVANCE;
9685 if (remaining == 0)
9686 break;
9687 remaining--;
9688 op = word >> 24;
9689 word <<= 8;
9690
9cf03b7e 9691 printf (" 0x%02x ", op);
fa197c1c
PB
9692
9693 if ((op & 0xc0) == 0x00)
9694 {
9695 int offset = ((op & 0x3f) << 3) + 8;
9cf03b7e 9696 printf (" sp = sp + %d", offset);
fa197c1c
PB
9697 }
9698 else if ((op & 0xc0) == 0x80)
9699 {
9700 GET_OP (op2);
9701 if (op == 0x80 && op2 == 0)
9702 printf (_("Refuse to unwind"));
9703 else
9704 {
9705 unsigned int mask = ((op & 0x1f) << 8) | op2;
9706 if (op & 0x20)
9707 printf ("pop compact {");
9708 else
9709 printf ("pop {");
9710
9711 decode_tic6x_unwind_regmask (mask);
9712 printf("}");
9713 }
9714 }
9715 else if ((op & 0xf0) == 0xc0)
9716 {
9717 unsigned int reg;
9718 unsigned int nregs;
9719 unsigned int i;
9720 const char *name;
a734115a
NC
9721 struct
9722 {
32ec8896
NC
9723 unsigned int offset;
9724 unsigned int reg;
fa197c1c
PB
9725 } regpos[16];
9726
9727 /* Scan entire instruction first so that GET_OP output is not
9728 interleaved with disassembly. */
9729 nregs = 0;
9730 for (i = 0; nregs < (op & 0xf); i++)
9731 {
9732 GET_OP (op2);
9733 reg = op2 >> 4;
9734 if (reg != 0xf)
9735 {
9736 regpos[nregs].offset = i * 2;
9737 regpos[nregs].reg = reg;
9738 nregs++;
9739 }
9740
9741 reg = op2 & 0xf;
9742 if (reg != 0xf)
9743 {
9744 regpos[nregs].offset = i * 2 + 1;
9745 regpos[nregs].reg = reg;
9746 nregs++;
9747 }
9748 }
9749
9750 printf (_("pop frame {"));
18344509 9751 if (nregs == 0)
fa197c1c 9752 {
18344509
NC
9753 printf (_("*corrupt* - no registers specified"));
9754 }
9755 else
9756 {
9757 reg = nregs - 1;
9758 for (i = i * 2; i > 0; i--)
fa197c1c 9759 {
18344509
NC
9760 if (regpos[reg].offset == i - 1)
9761 {
9762 name = tic6x_unwind_regnames[regpos[reg].reg];
9763 if (reg > 0)
9764 reg--;
9765 }
9766 else
9767 name = _("[pad]");
fa197c1c 9768
18344509
NC
9769 fputs (name, stdout);
9770 if (i > 1)
9771 printf (", ");
9772 }
fa197c1c
PB
9773 }
9774
9775 printf ("}");
9776 }
9777 else if (op == 0xd0)
9778 printf (" MOV FP, SP");
9779 else if (op == 0xd1)
9780 printf (" __c6xabi_pop_rts");
9781 else if (op == 0xd2)
9782 {
9783 unsigned char buf[9];
9784 unsigned int i, len;
9785 unsigned long offset;
a734115a 9786
fa197c1c
PB
9787 for (i = 0; i < sizeof (buf); i++)
9788 {
9789 GET_OP (buf[i]);
9790 if ((buf[i] & 0x80) == 0)
9791 break;
9792 }
0eff7165
NC
9793 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
9794 if (i == sizeof (buf))
9795 {
0eff7165 9796 warn (_("Corrupt stack pointer adjustment detected\n"));
015dc7e1 9797 return false;
0eff7165 9798 }
948f632f 9799
015dc7e1 9800 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
fa197c1c
PB
9801 assert (len == i + 1);
9802 offset = offset * 8 + 0x408;
9803 printf (_("sp = sp + %ld"), offset);
9804 }
9805 else if ((op & 0xf0) == 0xe0)
9806 {
9807 if ((op & 0x0f) == 7)
9808 printf (" RETURN");
9809 else
9810 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
9811 }
9812 else
9813 {
9814 printf (_(" [unsupported opcode]"));
9815 }
9816 putchar ('\n');
9817 }
32ec8896 9818
015dc7e1 9819 return true;
fa197c1c
PB
9820}
9821
9822static bfd_vma
dda8d76d 9823arm_expand_prel31 (Filedata * filedata, bfd_vma word, bfd_vma where)
fa197c1c
PB
9824{
9825 bfd_vma offset;
9826
9827 offset = word & 0x7fffffff;
9828 if (offset & 0x40000000)
9829 offset |= ~ (bfd_vma) 0x7fffffff;
9830
dda8d76d 9831 if (filedata->file_header.e_machine == EM_TI_C6000)
fa197c1c
PB
9832 offset <<= 1;
9833
9834 return offset + where;
9835}
9836
015dc7e1 9837static bool
dda8d76d
NC
9838decode_arm_unwind (Filedata * filedata,
9839 struct arm_unw_aux_info * aux,
1b31d05e
NC
9840 unsigned int word,
9841 unsigned int remaining,
9842 bfd_vma data_offset,
9843 Elf_Internal_Shdr * data_sec,
9844 struct arm_section * data_arm_sec)
fa197c1c
PB
9845{
9846 int per_index;
9847 unsigned int more_words = 0;
37e14bc3 9848 struct absaddr addr;
1b31d05e 9849 bfd_vma sym_name = (bfd_vma) -1;
015dc7e1 9850 bool res = true;
fa197c1c
PB
9851
9852 if (remaining == 0)
9853 {
1b31d05e
NC
9854 /* Fetch the first word.
9855 Note - when decoding an object file the address extracted
9856 here will always be 0. So we also pass in the sym_name
9857 parameter so that we can find the symbol associated with
9858 the personality routine. */
dda8d76d 9859 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
1b31d05e 9860 & word, & addr, & sym_name))
015dc7e1 9861 return false;
1b31d05e 9862
fa197c1c
PB
9863 remaining = 4;
9864 }
c93dbb25
CZ
9865 else
9866 {
9867 addr.section = SHN_UNDEF;
9868 addr.offset = 0;
9869 }
fa197c1c
PB
9870
9871 if ((word & 0x80000000) == 0)
9872 {
9873 /* Expand prel31 for personality routine. */
9874 bfd_vma fn;
9875 const char *procname;
9876
dda8d76d 9877 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
fa197c1c 9878 printf (_(" Personality routine: "));
1b31d05e
NC
9879 if (fn == 0
9880 && addr.section == SHN_UNDEF && addr.offset == 0
9881 && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
9882 {
9883 procname = aux->strtab + sym_name;
9884 print_vma (fn, PREFIX_HEX);
9885 if (procname)
9886 {
9887 fputs (" <", stdout);
9888 fputs (procname, stdout);
9889 fputc ('>', stdout);
9890 }
9891 }
9892 else
dda8d76d 9893 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
fa197c1c
PB
9894 fputc ('\n', stdout);
9895
9896 /* The GCC personality routines use the standard compact
9897 encoding, starting with one byte giving the number of
9898 words. */
9899 if (procname != NULL
24d127aa
ML
9900 && (startswith (procname, "__gcc_personality_v0")
9901 || startswith (procname, "__gxx_personality_v0")
9902 || startswith (procname, "__gcj_personality_v0")
9903 || startswith (procname, "__gnu_objc_personality_v0")))
fa197c1c
PB
9904 {
9905 remaining = 0;
9906 more_words = 1;
9907 ADVANCE;
9908 if (!remaining)
9909 {
9910 printf (_(" [Truncated data]\n"));
015dc7e1 9911 return false;
fa197c1c
PB
9912 }
9913 more_words = word >> 24;
9914 word <<= 8;
9915 remaining--;
9916 per_index = -1;
9917 }
9918 else
015dc7e1 9919 return true;
fa197c1c
PB
9920 }
9921 else
9922 {
1b31d05e 9923 /* ARM EHABI Section 6.3:
0b4362b0 9924
1b31d05e 9925 An exception-handling table entry for the compact model looks like:
0b4362b0 9926
1b31d05e
NC
9927 31 30-28 27-24 23-0
9928 -- ----- ----- ----
9929 1 0 index Data for personalityRoutine[index] */
9930
dda8d76d 9931 if (filedata->file_header.e_machine == EM_ARM
1b31d05e 9932 && (word & 0x70000000))
32ec8896
NC
9933 {
9934 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
015dc7e1 9935 res = false;
32ec8896 9936 }
1b31d05e 9937
fa197c1c 9938 per_index = (word >> 24) & 0x7f;
1b31d05e 9939 printf (_(" Compact model index: %d\n"), per_index);
fa197c1c
PB
9940 if (per_index == 0)
9941 {
9942 more_words = 0;
9943 word <<= 8;
9944 remaining--;
9945 }
9946 else if (per_index < 3)
9947 {
9948 more_words = (word >> 16) & 0xff;
9949 word <<= 16;
9950 remaining -= 2;
9951 }
9952 }
9953
dda8d76d 9954 switch (filedata->file_header.e_machine)
fa197c1c
PB
9955 {
9956 case EM_ARM:
9957 if (per_index < 3)
9958 {
dda8d76d 9959 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
32ec8896 9960 data_offset, data_sec, data_arm_sec))
015dc7e1 9961 res = false;
fa197c1c
PB
9962 }
9963 else
1b31d05e
NC
9964 {
9965 warn (_("Unknown ARM compact model index encountered\n"));
9966 printf (_(" [reserved]\n"));
015dc7e1 9967 res = false;
1b31d05e 9968 }
fa197c1c
PB
9969 break;
9970
9971 case EM_TI_C6000:
9972 if (per_index < 3)
9973 {
dda8d76d 9974 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
32ec8896 9975 data_offset, data_sec, data_arm_sec))
015dc7e1 9976 res = false;
fa197c1c
PB
9977 }
9978 else if (per_index < 5)
9979 {
9980 if (((word >> 17) & 0x7f) == 0x7f)
9981 printf (_(" Restore stack from frame pointer\n"));
9982 else
9983 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
9984 printf (_(" Registers restored: "));
9985 if (per_index == 4)
9986 printf (" (compact) ");
9987 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
9988 putchar ('\n');
9989 printf (_(" Return register: %s\n"),
9990 tic6x_unwind_regnames[word & 0xf]);
9991 }
9992 else
1b31d05e 9993 printf (_(" [reserved (%d)]\n"), per_index);
fa197c1c
PB
9994 break;
9995
9996 default:
74e1a04b 9997 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
dda8d76d 9998 filedata->file_header.e_machine);
015dc7e1 9999 res = false;
fa197c1c 10000 }
0b6ae522
DJ
10001
10002 /* Decode the descriptors. Not implemented. */
32ec8896
NC
10003
10004 return res;
0b6ae522
DJ
10005}
10006
015dc7e1 10007static bool
dda8d76d
NC
10008dump_arm_unwind (Filedata * filedata,
10009 struct arm_unw_aux_info * aux,
10010 Elf_Internal_Shdr * exidx_sec)
0b6ae522
DJ
10011{
10012 struct arm_section exidx_arm_sec, extab_arm_sec;
10013 unsigned int i, exidx_len;
948f632f 10014 unsigned long j, nfuns;
015dc7e1 10015 bool res = true;
0b6ae522
DJ
10016
10017 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
10018 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
10019 exidx_len = exidx_sec->sh_size / 8;
10020
948f632f
DA
10021 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
10022 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
10023 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
10024 aux->funtab[nfuns++] = aux->symtab[j];
10025 aux->nfuns = nfuns;
10026 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
10027
0b6ae522
DJ
10028 for (i = 0; i < exidx_len; i++)
10029 {
10030 unsigned int exidx_fn, exidx_entry;
10031 struct absaddr fn_addr, entry_addr;
10032 bfd_vma fn;
10033
10034 fputc ('\n', stdout);
10035
dda8d76d 10036 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
1b31d05e 10037 8 * i, & exidx_fn, & fn_addr, NULL)
dda8d76d 10038 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
1b31d05e 10039 8 * i + 4, & exidx_entry, & entry_addr, NULL))
0b6ae522 10040 {
948f632f 10041 free (aux->funtab);
1b31d05e
NC
10042 arm_free_section (& exidx_arm_sec);
10043 arm_free_section (& extab_arm_sec);
015dc7e1 10044 return false;
0b6ae522
DJ
10045 }
10046
83c257ca
NC
10047 /* ARM EHABI, Section 5:
10048 An index table entry consists of 2 words.
10049 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
10050 if (exidx_fn & 0x80000000)
32ec8896
NC
10051 {
10052 warn (_("corrupt index table entry: %x\n"), exidx_fn);
015dc7e1 10053 res = false;
32ec8896 10054 }
83c257ca 10055
dda8d76d 10056 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
0b6ae522 10057
dda8d76d 10058 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
0b6ae522
DJ
10059 fputs (": ", stdout);
10060
10061 if (exidx_entry == 1)
10062 {
10063 print_vma (exidx_entry, PREFIX_HEX);
10064 fputs (" [cantunwind]\n", stdout);
10065 }
10066 else if (exidx_entry & 0x80000000)
10067 {
10068 print_vma (exidx_entry, PREFIX_HEX);
10069 fputc ('\n', stdout);
dda8d76d 10070 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
0b6ae522
DJ
10071 }
10072 else
10073 {
8f73510c 10074 bfd_vma table, table_offset = 0;
0b6ae522
DJ
10075 Elf_Internal_Shdr *table_sec;
10076
10077 fputs ("@", stdout);
dda8d76d 10078 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
0b6ae522
DJ
10079 print_vma (table, PREFIX_HEX);
10080 printf ("\n");
10081
10082 /* Locate the matching .ARM.extab. */
10083 if (entry_addr.section != SHN_UNDEF
dda8d76d 10084 && entry_addr.section < filedata->file_header.e_shnum)
0b6ae522 10085 {
dda8d76d 10086 table_sec = filedata->section_headers + entry_addr.section;
0b6ae522 10087 table_offset = entry_addr.offset;
1a915552
NC
10088 /* PR 18879 */
10089 if (table_offset > table_sec->sh_size
10090 || ((bfd_signed_vma) table_offset) < 0)
10091 {
10092 warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
10093 (unsigned long) table_offset,
dda8d76d 10094 printable_section_name (filedata, table_sec));
015dc7e1 10095 res = false;
1a915552
NC
10096 continue;
10097 }
0b6ae522
DJ
10098 }
10099 else
10100 {
dda8d76d 10101 table_sec = find_section_by_address (filedata, table);
0b6ae522
DJ
10102 if (table_sec != NULL)
10103 table_offset = table - table_sec->sh_addr;
10104 }
32ec8896 10105
0b6ae522
DJ
10106 if (table_sec == NULL)
10107 {
10108 warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
10109 (unsigned long) table);
015dc7e1 10110 res = false;
0b6ae522
DJ
10111 continue;
10112 }
32ec8896 10113
dda8d76d 10114 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
32ec8896 10115 &extab_arm_sec))
015dc7e1 10116 res = false;
0b6ae522
DJ
10117 }
10118 }
10119
10120 printf ("\n");
10121
948f632f 10122 free (aux->funtab);
0b6ae522
DJ
10123 arm_free_section (&exidx_arm_sec);
10124 arm_free_section (&extab_arm_sec);
32ec8896
NC
10125
10126 return res;
0b6ae522
DJ
10127}
10128
fa197c1c 10129/* Used for both ARM and C6X unwinding tables. */
1b31d05e 10130
015dc7e1 10131static bool
dda8d76d 10132arm_process_unwind (Filedata * filedata)
0b6ae522
DJ
10133{
10134 struct arm_unw_aux_info aux;
10135 Elf_Internal_Shdr *unwsec = NULL;
0b6ae522
DJ
10136 Elf_Internal_Shdr *sec;
10137 unsigned long i;
fa197c1c 10138 unsigned int sec_type;
015dc7e1 10139 bool res = true;
0b6ae522 10140
dda8d76d 10141 switch (filedata->file_header.e_machine)
fa197c1c
PB
10142 {
10143 case EM_ARM:
10144 sec_type = SHT_ARM_EXIDX;
10145 break;
10146
10147 case EM_TI_C6000:
10148 sec_type = SHT_C6000_UNWIND;
10149 break;
10150
0b4362b0 10151 default:
74e1a04b 10152 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
dda8d76d 10153 filedata->file_header.e_machine);
015dc7e1 10154 return false;
fa197c1c
PB
10155 }
10156
dda8d76d 10157 if (filedata->string_table == NULL)
015dc7e1 10158 return false;
1b31d05e
NC
10159
10160 memset (& aux, 0, sizeof (aux));
dda8d76d 10161 aux.filedata = filedata;
0b6ae522 10162
dda8d76d 10163 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
0b6ae522 10164 {
28d13567 10165 if (sec->sh_type == SHT_SYMTAB)
0b6ae522 10166 {
28d13567 10167 if (aux.symtab)
74e1a04b 10168 {
28d13567
AM
10169 error (_("Multiple symbol tables encountered\n"));
10170 free (aux.symtab);
10171 aux.symtab = NULL;
74e1a04b 10172 free (aux.strtab);
28d13567 10173 aux.strtab = NULL;
74e1a04b 10174 }
28d13567
AM
10175 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
10176 &aux.strtab, &aux.strtab_size))
015dc7e1 10177 return false;
0b6ae522 10178 }
fa197c1c 10179 else if (sec->sh_type == sec_type)
0b6ae522
DJ
10180 unwsec = sec;
10181 }
10182
1b31d05e 10183 if (unwsec == NULL)
0b6ae522 10184 printf (_("\nThere are no unwind sections in this file.\n"));
1b31d05e 10185 else
dda8d76d 10186 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
1b31d05e
NC
10187 {
10188 if (sec->sh_type == sec_type)
10189 {
d3a49aa8
AM
10190 unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
10191 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
10192 "contains %lu entry:\n",
10193 "\nUnwind section '%s' at offset 0x%lx "
10194 "contains %lu entries:\n",
10195 num_unwind),
dda8d76d 10196 printable_section_name (filedata, sec),
1b31d05e 10197 (unsigned long) sec->sh_offset,
d3a49aa8 10198 num_unwind);
0b6ae522 10199
dda8d76d 10200 if (! dump_arm_unwind (filedata, &aux, sec))
015dc7e1 10201 res = false;
1b31d05e
NC
10202 }
10203 }
0b6ae522 10204
9db70fc3
AM
10205 free (aux.symtab);
10206 free ((char *) aux.strtab);
32ec8896
NC
10207
10208 return res;
0b6ae522
DJ
10209}
10210
3ecc00ec
NC
10211static bool
10212no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
10213{
10214 printf (_("No processor specific unwind information to decode\n"));
10215 return true;
10216}
10217
015dc7e1 10218static bool
dda8d76d 10219process_unwind (Filedata * filedata)
57346661 10220{
2cf0635d
NC
10221 struct unwind_handler
10222 {
32ec8896 10223 unsigned int machtype;
015dc7e1 10224 bool (* handler)(Filedata *);
2cf0635d
NC
10225 } handlers[] =
10226 {
0b6ae522 10227 { EM_ARM, arm_process_unwind },
57346661
AM
10228 { EM_IA_64, ia64_process_unwind },
10229 { EM_PARISC, hppa_process_unwind },
fa197c1c 10230 { EM_TI_C6000, arm_process_unwind },
3ecc00ec
NC
10231 { EM_386, no_processor_specific_unwind },
10232 { EM_X86_64, no_processor_specific_unwind },
32ec8896 10233 { 0, NULL }
57346661
AM
10234 };
10235 int i;
10236
10237 if (!do_unwind)
015dc7e1 10238 return true;
57346661
AM
10239
10240 for (i = 0; handlers[i].handler != NULL; i++)
dda8d76d
NC
10241 if (filedata->file_header.e_machine == handlers[i].machtype)
10242 return handlers[i].handler (filedata);
57346661 10243
1b31d05e 10244 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
dda8d76d 10245 get_machine_name (filedata->file_header.e_machine));
015dc7e1 10246 return true;
57346661
AM
10247}
10248
37c18eed
SD
10249static void
10250dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
10251{
10252 switch (entry->d_tag)
10253 {
10254 case DT_AARCH64_BTI_PLT:
1dbade74 10255 case DT_AARCH64_PAC_PLT:
37c18eed
SD
10256 break;
10257 default:
10258 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10259 break;
10260 }
10261 putchar ('\n');
10262}
10263
252b5132 10264static void
978c4450 10265dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
252b5132
RH
10266{
10267 switch (entry->d_tag)
10268 {
10269 case DT_MIPS_FLAGS:
10270 if (entry->d_un.d_val == 0)
4b68bca3 10271 printf (_("NONE"));
252b5132
RH
10272 else
10273 {
10274 static const char * opts[] =
10275 {
10276 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
10277 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
10278 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
10279 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
10280 "RLD_ORDER_SAFE"
10281 };
10282 unsigned int cnt;
015dc7e1 10283 bool first = true;
2b692964 10284
60bca95a 10285 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
252b5132
RH
10286 if (entry->d_un.d_val & (1 << cnt))
10287 {
10288 printf ("%s%s", first ? "" : " ", opts[cnt]);
015dc7e1 10289 first = false;
252b5132 10290 }
252b5132
RH
10291 }
10292 break;
103f02d3 10293
252b5132 10294 case DT_MIPS_IVERSION:
84714f86 10295 if (valid_dynamic_name (filedata, entry->d_un.d_val))
978c4450 10296 printf (_("Interface Version: %s"),
84714f86 10297 get_dynamic_name (filedata, entry->d_un.d_val));
252b5132 10298 else
76ca31c0
NC
10299 {
10300 char buf[40];
10301 sprintf_vma (buf, entry->d_un.d_ptr);
10302 /* Note: coded this way so that there is a single string for translation. */
10303 printf (_("<corrupt: %s>"), buf);
10304 }
252b5132 10305 break;
103f02d3 10306
252b5132
RH
10307 case DT_MIPS_TIME_STAMP:
10308 {
d5b07ef4 10309 char timebuf[128];
2cf0635d 10310 struct tm * tmp;
91d6fa6a 10311 time_t atime = entry->d_un.d_val;
82b1b41b 10312
91d6fa6a 10313 tmp = gmtime (&atime);
82b1b41b
NC
10314 /* PR 17531: file: 6accc532. */
10315 if (tmp == NULL)
10316 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
10317 else
10318 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
10319 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10320 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
4b68bca3 10321 printf (_("Time Stamp: %s"), timebuf);
252b5132
RH
10322 }
10323 break;
103f02d3 10324
252b5132
RH
10325 case DT_MIPS_RLD_VERSION:
10326 case DT_MIPS_LOCAL_GOTNO:
10327 case DT_MIPS_CONFLICTNO:
10328 case DT_MIPS_LIBLISTNO:
10329 case DT_MIPS_SYMTABNO:
10330 case DT_MIPS_UNREFEXTNO:
10331 case DT_MIPS_HIPAGENO:
10332 case DT_MIPS_DELTA_CLASS_NO:
10333 case DT_MIPS_DELTA_INSTANCE_NO:
10334 case DT_MIPS_DELTA_RELOC_NO:
10335 case DT_MIPS_DELTA_SYM_NO:
10336 case DT_MIPS_DELTA_CLASSSYM_NO:
10337 case DT_MIPS_COMPACT_SIZE:
c69075ac 10338 print_vma (entry->d_un.d_val, DEC);
252b5132 10339 break;
103f02d3 10340
f16a9783 10341 case DT_MIPS_XHASH:
978c4450
AM
10342 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10343 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
f16a9783
MS
10344 /* Falls through. */
10345
103f02d3 10346 default:
4b68bca3 10347 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
103f02d3 10348 }
4b68bca3 10349 putchar ('\n');
103f02d3
UD
10350}
10351
103f02d3 10352static void
2cf0635d 10353dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
103f02d3
UD
10354{
10355 switch (entry->d_tag)
10356 {
10357 case DT_HP_DLD_FLAGS:
10358 {
10359 static struct
10360 {
10361 long int bit;
2cf0635d 10362 const char * str;
5e220199
NC
10363 }
10364 flags[] =
10365 {
10366 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
10367 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
10368 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
10369 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
10370 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
10371 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
10372 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
10373 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
10374 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
10375 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
eec8f817
DA
10376 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
10377 { DT_HP_GST, "HP_GST" },
10378 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
10379 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
10380 { DT_HP_NODELETE, "HP_NODELETE" },
10381 { DT_HP_GROUP, "HP_GROUP" },
10382 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
5e220199 10383 };
015dc7e1 10384 bool first = true;
5e220199 10385 size_t cnt;
f7a99963 10386 bfd_vma val = entry->d_un.d_val;
103f02d3 10387
60bca95a 10388 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
103f02d3 10389 if (val & flags[cnt].bit)
30800947
NC
10390 {
10391 if (! first)
10392 putchar (' ');
10393 fputs (flags[cnt].str, stdout);
015dc7e1 10394 first = false;
30800947
NC
10395 val ^= flags[cnt].bit;
10396 }
76da6bbe 10397
103f02d3 10398 if (val != 0 || first)
f7a99963
NC
10399 {
10400 if (! first)
10401 putchar (' ');
10402 print_vma (val, HEX);
10403 }
103f02d3
UD
10404 }
10405 break;
76da6bbe 10406
252b5132 10407 default:
f7a99963
NC
10408 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10409 break;
252b5132 10410 }
35b1837e 10411 putchar ('\n');
252b5132
RH
10412}
10413
28f997cf
TG
10414#ifdef BFD64
10415
10416/* VMS vs Unix time offset and factor. */
10417
10418#define VMS_EPOCH_OFFSET 35067168000000000LL
10419#define VMS_GRANULARITY_FACTOR 10000000
dccc31de
AM
10420#ifndef INT64_MIN
10421#define INT64_MIN (-9223372036854775807LL - 1)
10422#endif
28f997cf
TG
10423
10424/* Display a VMS time in a human readable format. */
10425
10426static void
10427print_vms_time (bfd_int64_t vmstime)
10428{
dccc31de 10429 struct tm *tm = NULL;
28f997cf
TG
10430 time_t unxtime;
10431
dccc31de
AM
10432 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
10433 {
10434 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
10435 unxtime = vmstime;
10436 if (unxtime == vmstime)
10437 tm = gmtime (&unxtime);
10438 }
10439 if (tm != NULL)
10440 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
10441 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
10442 tm->tm_hour, tm->tm_min, tm->tm_sec);
28f997cf
TG
10443}
10444#endif /* BFD64 */
10445
ecc51f48 10446static void
2cf0635d 10447dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
ecc51f48
NC
10448{
10449 switch (entry->d_tag)
10450 {
0de14b54 10451 case DT_IA_64_PLT_RESERVE:
bdf4d63a 10452 /* First 3 slots reserved. */
ecc51f48
NC
10453 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10454 printf (" -- ");
10455 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
bdf4d63a
JJ
10456 break;
10457
28f997cf
TG
10458 case DT_IA_64_VMS_LINKTIME:
10459#ifdef BFD64
10460 print_vms_time (entry->d_un.d_val);
10461#endif
10462 break;
10463
10464 case DT_IA_64_VMS_LNKFLAGS:
10465 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10466 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
10467 printf (" CALL_DEBUG");
10468 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
10469 printf (" NOP0BUFS");
10470 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
10471 printf (" P0IMAGE");
10472 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
10473 printf (" MKTHREADS");
10474 if (entry->d_un.d_val & VMS_LF_UPCALLS)
10475 printf (" UPCALLS");
10476 if (entry->d_un.d_val & VMS_LF_IMGSTA)
10477 printf (" IMGSTA");
10478 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
10479 printf (" INITIALIZE");
10480 if (entry->d_un.d_val & VMS_LF_MAIN)
10481 printf (" MAIN");
10482 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
10483 printf (" EXE_INIT");
10484 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
10485 printf (" TBK_IN_IMG");
10486 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
10487 printf (" DBG_IN_IMG");
10488 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
10489 printf (" TBK_IN_DSF");
10490 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
10491 printf (" DBG_IN_DSF");
10492 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
10493 printf (" SIGNATURES");
10494 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
10495 printf (" REL_SEG_OFF");
10496 break;
10497
bdf4d63a
JJ
10498 default:
10499 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10500 break;
ecc51f48 10501 }
bdf4d63a 10502 putchar ('\n');
ecc51f48
NC
10503}
10504
015dc7e1 10505static bool
dda8d76d 10506get_32bit_dynamic_section (Filedata * filedata)
252b5132 10507{
2cf0635d
NC
10508 Elf32_External_Dyn * edyn;
10509 Elf32_External_Dyn * ext;
10510 Elf_Internal_Dyn * entry;
103f02d3 10511
978c4450
AM
10512 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
10513 filedata->dynamic_addr, 1,
10514 filedata->dynamic_size,
10515 _("dynamic section"));
a6e9f9df 10516 if (!edyn)
015dc7e1 10517 return false;
103f02d3 10518
071436c6
NC
10519 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10520 might not have the luxury of section headers. Look for the DT_NULL
10521 terminator to determine the number of entries. */
978c4450
AM
10522 for (ext = edyn, filedata->dynamic_nent = 0;
10523 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
ba2685cc
AM
10524 ext++)
10525 {
978c4450 10526 filedata->dynamic_nent++;
ba2685cc
AM
10527 if (BYTE_GET (ext->d_tag) == DT_NULL)
10528 break;
10529 }
252b5132 10530
978c4450
AM
10531 filedata->dynamic_section
10532 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10533 if (filedata->dynamic_section == NULL)
252b5132 10534 {
8b73c356 10535 error (_("Out of memory allocating space for %lu dynamic entries\n"),
978c4450 10536 (unsigned long) filedata->dynamic_nent);
9ea033b2 10537 free (edyn);
015dc7e1 10538 return false;
9ea033b2 10539 }
252b5132 10540
978c4450
AM
10541 for (ext = edyn, entry = filedata->dynamic_section;
10542 entry < filedata->dynamic_section + filedata->dynamic_nent;
fb514b26 10543 ext++, entry++)
9ea033b2 10544 {
fb514b26
AM
10545 entry->d_tag = BYTE_GET (ext->d_tag);
10546 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
252b5132
RH
10547 }
10548
9ea033b2
NC
10549 free (edyn);
10550
015dc7e1 10551 return true;
9ea033b2
NC
10552}
10553
015dc7e1 10554static bool
dda8d76d 10555get_64bit_dynamic_section (Filedata * filedata)
9ea033b2 10556{
2cf0635d
NC
10557 Elf64_External_Dyn * edyn;
10558 Elf64_External_Dyn * ext;
10559 Elf_Internal_Dyn * entry;
103f02d3 10560
071436c6 10561 /* Read in the data. */
978c4450
AM
10562 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
10563 filedata->dynamic_addr, 1,
10564 filedata->dynamic_size,
10565 _("dynamic section"));
a6e9f9df 10566 if (!edyn)
015dc7e1 10567 return false;
103f02d3 10568
071436c6
NC
10569 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10570 might not have the luxury of section headers. Look for the DT_NULL
10571 terminator to determine the number of entries. */
978c4450 10572 for (ext = edyn, filedata->dynamic_nent = 0;
53c3012c 10573 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
978c4450 10574 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
ba2685cc
AM
10575 ext++)
10576 {
978c4450 10577 filedata->dynamic_nent++;
66543521 10578 if (BYTE_GET (ext->d_tag) == DT_NULL)
ba2685cc
AM
10579 break;
10580 }
252b5132 10581
978c4450
AM
10582 filedata->dynamic_section
10583 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10584 if (filedata->dynamic_section == NULL)
252b5132 10585 {
8b73c356 10586 error (_("Out of memory allocating space for %lu dynamic entries\n"),
978c4450 10587 (unsigned long) filedata->dynamic_nent);
252b5132 10588 free (edyn);
015dc7e1 10589 return false;
252b5132
RH
10590 }
10591
071436c6 10592 /* Convert from external to internal formats. */
978c4450
AM
10593 for (ext = edyn, entry = filedata->dynamic_section;
10594 entry < filedata->dynamic_section + filedata->dynamic_nent;
fb514b26 10595 ext++, entry++)
252b5132 10596 {
66543521
AM
10597 entry->d_tag = BYTE_GET (ext->d_tag);
10598 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
252b5132
RH
10599 }
10600
10601 free (edyn);
10602
015dc7e1 10603 return true;
9ea033b2
NC
10604}
10605
4de91c10
AM
10606static bool
10607get_dynamic_section (Filedata *filedata)
10608{
10609 if (filedata->dynamic_section)
10610 return true;
10611
10612 if (is_32bit_elf)
10613 return get_32bit_dynamic_section (filedata);
10614 else
10615 return get_64bit_dynamic_section (filedata);
10616}
10617
e9e44622
JJ
10618static void
10619print_dynamic_flags (bfd_vma flags)
d1133906 10620{
015dc7e1 10621 bool first = true;
13ae64f3 10622
d1133906
NC
10623 while (flags)
10624 {
10625 bfd_vma flag;
10626
10627 flag = flags & - flags;
10628 flags &= ~ flag;
10629
e9e44622 10630 if (first)
015dc7e1 10631 first = false;
e9e44622
JJ
10632 else
10633 putc (' ', stdout);
13ae64f3 10634
d1133906
NC
10635 switch (flag)
10636 {
e9e44622
JJ
10637 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
10638 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
10639 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
10640 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
10641 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
2b692964 10642 default: fputs (_("unknown"), stdout); break;
d1133906
NC
10643 }
10644 }
e9e44622 10645 puts ("");
d1133906
NC
10646}
10647
10ca4b04
L
10648static bfd_vma *
10649get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_size)
10650{
10651 unsigned char * e_data;
10652 bfd_vma * i_data;
10653
10654 /* If the size_t type is smaller than the bfd_size_type, eg because
10655 you are building a 32-bit tool on a 64-bit host, then make sure
10656 that when (number) is cast to (size_t) no information is lost. */
10657 if (sizeof (size_t) < sizeof (bfd_size_type)
10658 && (bfd_size_type) ((size_t) number) != number)
10659 {
10660 error (_("Size truncation prevents reading %s elements of size %u\n"),
10661 bfd_vmatoa ("u", number), ent_size);
10662 return NULL;
10663 }
10664
10665 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
10666 attempting to allocate memory when the read is bound to fail. */
10667 if (ent_size * number > filedata->file_size)
10668 {
10669 error (_("Invalid number of dynamic entries: %s\n"),
10670 bfd_vmatoa ("u", number));
10671 return NULL;
10672 }
10673
10674 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
10675 if (e_data == NULL)
10676 {
10677 error (_("Out of memory reading %s dynamic entries\n"),
10678 bfd_vmatoa ("u", number));
10679 return NULL;
10680 }
10681
10682 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
10683 {
10684 error (_("Unable to read in %s bytes of dynamic data\n"),
10685 bfd_vmatoa ("u", number * ent_size));
10686 free (e_data);
10687 return NULL;
10688 }
10689
10690 i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
10691 if (i_data == NULL)
10692 {
10693 error (_("Out of memory allocating space for %s dynamic entries\n"),
10694 bfd_vmatoa ("u", number));
10695 free (e_data);
10696 return NULL;
10697 }
10698
10699 while (number--)
10700 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
10701
10702 free (e_data);
10703
10704 return i_data;
10705}
10706
10707static unsigned long
10708get_num_dynamic_syms (Filedata * filedata)
10709{
10710 unsigned long num_of_syms = 0;
10711
10712 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
10713 return num_of_syms;
10714
978c4450 10715 if (filedata->dynamic_info[DT_HASH])
10ca4b04
L
10716 {
10717 unsigned char nb[8];
10718 unsigned char nc[8];
10719 unsigned int hash_ent_size = 4;
10720
10721 if ((filedata->file_header.e_machine == EM_ALPHA
10722 || filedata->file_header.e_machine == EM_S390
10723 || filedata->file_header.e_machine == EM_S390_OLD)
10724 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
10725 hash_ent_size = 8;
10726
10727 if (fseek (filedata->handle,
978c4450
AM
10728 (filedata->archive_file_offset
10729 + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
10ca4b04
L
10730 sizeof nb + sizeof nc)),
10731 SEEK_SET))
10732 {
10733 error (_("Unable to seek to start of dynamic information\n"));
10734 goto no_hash;
10735 }
10736
10737 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
10738 {
10739 error (_("Failed to read in number of buckets\n"));
10740 goto no_hash;
10741 }
10742
10743 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
10744 {
10745 error (_("Failed to read in number of chains\n"));
10746 goto no_hash;
10747 }
10748
978c4450
AM
10749 filedata->nbuckets = byte_get (nb, hash_ent_size);
10750 filedata->nchains = byte_get (nc, hash_ent_size);
10ca4b04 10751
2482f306
AM
10752 if (filedata->nbuckets != 0 && filedata->nchains != 0)
10753 {
10754 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
10755 hash_ent_size);
10756 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
10757 hash_ent_size);
001890e1 10758
2482f306
AM
10759 if (filedata->buckets != NULL && filedata->chains != NULL)
10760 num_of_syms = filedata->nchains;
10761 }
ceb9bf11 10762 no_hash:
10ca4b04
L
10763 if (num_of_syms == 0)
10764 {
9db70fc3
AM
10765 free (filedata->buckets);
10766 filedata->buckets = NULL;
10767 free (filedata->chains);
10768 filedata->chains = NULL;
978c4450 10769 filedata->nbuckets = 0;
10ca4b04
L
10770 }
10771 }
10772
978c4450 10773 if (filedata->dynamic_info_DT_GNU_HASH)
10ca4b04
L
10774 {
10775 unsigned char nb[16];
10776 bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
10777 bfd_vma buckets_vma;
10778 unsigned long hn;
10ca4b04
L
10779
10780 if (fseek (filedata->handle,
978c4450
AM
10781 (filedata->archive_file_offset
10782 + offset_from_vma (filedata,
10783 filedata->dynamic_info_DT_GNU_HASH,
10ca4b04
L
10784 sizeof nb)),
10785 SEEK_SET))
10786 {
10787 error (_("Unable to seek to start of dynamic information\n"));
10ca4b04
L
10788 goto no_gnu_hash;
10789 }
10790
10791 if (fread (nb, 16, 1, filedata->handle) != 1)
10792 {
10793 error (_("Failed to read in number of buckets\n"));
10ca4b04
L
10794 goto no_gnu_hash;
10795 }
10796
978c4450
AM
10797 filedata->ngnubuckets = byte_get (nb, 4);
10798 filedata->gnusymidx = byte_get (nb + 4, 4);
10ca4b04 10799 bitmaskwords = byte_get (nb + 8, 4);
978c4450 10800 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
10ca4b04
L
10801 if (is_32bit_elf)
10802 buckets_vma += bitmaskwords * 4;
10803 else
10804 buckets_vma += bitmaskwords * 8;
10805
10806 if (fseek (filedata->handle,
978c4450 10807 (filedata->archive_file_offset
10ca4b04
L
10808 + offset_from_vma (filedata, buckets_vma, 4)),
10809 SEEK_SET))
10810 {
10811 error (_("Unable to seek to start of dynamic information\n"));
10ca4b04
L
10812 goto no_gnu_hash;
10813 }
10814
978c4450
AM
10815 filedata->gnubuckets
10816 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
10ca4b04 10817
978c4450 10818 if (filedata->gnubuckets == NULL)
90837ea7 10819 goto no_gnu_hash;
10ca4b04 10820
978c4450
AM
10821 for (i = 0; i < filedata->ngnubuckets; i++)
10822 if (filedata->gnubuckets[i] != 0)
10ca4b04 10823 {
978c4450 10824 if (filedata->gnubuckets[i] < filedata->gnusymidx)
90837ea7 10825 goto no_gnu_hash;
10ca4b04 10826
978c4450
AM
10827 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
10828 maxchain = filedata->gnubuckets[i];
10ca4b04
L
10829 }
10830
10831 if (maxchain == 0xffffffff)
90837ea7 10832 goto no_gnu_hash;
10ca4b04 10833
978c4450 10834 maxchain -= filedata->gnusymidx;
10ca4b04
L
10835
10836 if (fseek (filedata->handle,
978c4450
AM
10837 (filedata->archive_file_offset
10838 + offset_from_vma (filedata,
10839 buckets_vma + 4 * (filedata->ngnubuckets
10840 + maxchain),
10841 4)),
10ca4b04
L
10842 SEEK_SET))
10843 {
10844 error (_("Unable to seek to start of dynamic information\n"));
10ca4b04
L
10845 goto no_gnu_hash;
10846 }
10847
10848 do
10849 {
10850 if (fread (nb, 4, 1, filedata->handle) != 1)
10851 {
10852 error (_("Failed to determine last chain length\n"));
10ca4b04
L
10853 goto no_gnu_hash;
10854 }
10855
10856 if (maxchain + 1 == 0)
90837ea7 10857 goto no_gnu_hash;
10ca4b04
L
10858
10859 ++maxchain;
10860 }
10861 while ((byte_get (nb, 4) & 1) == 0);
10862
10863 if (fseek (filedata->handle,
978c4450
AM
10864 (filedata->archive_file_offset
10865 + offset_from_vma (filedata, (buckets_vma
10866 + 4 * filedata->ngnubuckets),
10867 4)),
10ca4b04
L
10868 SEEK_SET))
10869 {
10870 error (_("Unable to seek to start of dynamic information\n"));
10ca4b04
L
10871 goto no_gnu_hash;
10872 }
10873
978c4450
AM
10874 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
10875 filedata->ngnuchains = maxchain;
10ca4b04 10876
978c4450 10877 if (filedata->gnuchains == NULL)
90837ea7 10878 goto no_gnu_hash;
10ca4b04 10879
978c4450 10880 if (filedata->dynamic_info_DT_MIPS_XHASH)
10ca4b04
L
10881 {
10882 if (fseek (filedata->handle,
978c4450 10883 (filedata->archive_file_offset
10ca4b04 10884 + offset_from_vma (filedata, (buckets_vma
978c4450 10885 + 4 * (filedata->ngnubuckets
10ca4b04
L
10886 + maxchain)), 4)),
10887 SEEK_SET))
10888 {
10889 error (_("Unable to seek to start of dynamic information\n"));
10ca4b04
L
10890 goto no_gnu_hash;
10891 }
10892
978c4450 10893 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
90837ea7
AM
10894 if (filedata->mipsxlat == NULL)
10895 goto no_gnu_hash;
10ca4b04
L
10896 }
10897
978c4450
AM
10898 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
10899 if (filedata->gnubuckets[hn] != 0)
10ca4b04 10900 {
978c4450
AM
10901 bfd_vma si = filedata->gnubuckets[hn];
10902 bfd_vma off = si - filedata->gnusymidx;
10ca4b04
L
10903
10904 do
10905 {
978c4450 10906 if (filedata->dynamic_info_DT_MIPS_XHASH)
10ca4b04 10907 {
c31ab5a0
AM
10908 if (off < filedata->ngnuchains
10909 && filedata->mipsxlat[off] >= num_of_syms)
978c4450 10910 num_of_syms = filedata->mipsxlat[off] + 1;
10ca4b04
L
10911 }
10912 else
10913 {
10914 if (si >= num_of_syms)
10915 num_of_syms = si + 1;
10916 }
10917 si++;
10918 }
978c4450
AM
10919 while (off < filedata->ngnuchains
10920 && (filedata->gnuchains[off++] & 1) == 0);
10ca4b04
L
10921 }
10922
90837ea7 10923 if (num_of_syms == 0)
10ca4b04 10924 {
90837ea7 10925 no_gnu_hash:
9db70fc3
AM
10926 free (filedata->mipsxlat);
10927 filedata->mipsxlat = NULL;
10928 free (filedata->gnuchains);
10929 filedata->gnuchains = NULL;
10930 free (filedata->gnubuckets);
10931 filedata->gnubuckets = NULL;
978c4450
AM
10932 filedata->ngnubuckets = 0;
10933 filedata->ngnuchains = 0;
10ca4b04
L
10934 }
10935 }
10936
10937 return num_of_syms;
10938}
10939
b2d38a17
NC
10940/* Parse and display the contents of the dynamic section. */
10941
015dc7e1 10942static bool
dda8d76d 10943process_dynamic_section (Filedata * filedata)
9ea033b2 10944{
2cf0635d 10945 Elf_Internal_Dyn * entry;
9ea033b2 10946
93df3340 10947 if (filedata->dynamic_size <= 1)
9ea033b2
NC
10948 {
10949 if (do_dynamic)
ca0e11aa
NC
10950 {
10951 if (filedata->is_separate)
10952 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
10953 filedata->file_name);
10954 else
10955 printf (_("\nThere is no dynamic section in this file.\n"));
10956 }
9ea033b2 10957
015dc7e1 10958 return true;
9ea033b2
NC
10959 }
10960
4de91c10
AM
10961 if (!get_dynamic_section (filedata))
10962 return false;
9ea033b2 10963
252b5132 10964 /* Find the appropriate symbol table. */
978c4450 10965 if (filedata->dynamic_symbols == NULL || do_histogram)
252b5132 10966 {
2482f306
AM
10967 unsigned long num_of_syms;
10968
978c4450
AM
10969 for (entry = filedata->dynamic_section;
10970 entry < filedata->dynamic_section + filedata->dynamic_nent;
86dba8ee 10971 ++entry)
10ca4b04 10972 if (entry->d_tag == DT_SYMTAB)
978c4450 10973 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
10ca4b04 10974 else if (entry->d_tag == DT_SYMENT)
978c4450 10975 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
10ca4b04 10976 else if (entry->d_tag == DT_HASH)
978c4450 10977 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
10ca4b04 10978 else if (entry->d_tag == DT_GNU_HASH)
978c4450 10979 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10ca4b04
L
10980 else if ((filedata->file_header.e_machine == EM_MIPS
10981 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
10982 && entry->d_tag == DT_MIPS_XHASH)
10983 {
978c4450
AM
10984 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10985 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10ca4b04 10986 }
252b5132 10987
2482f306
AM
10988 num_of_syms = get_num_dynamic_syms (filedata);
10989
10990 if (num_of_syms != 0
10991 && filedata->dynamic_symbols == NULL
10992 && filedata->dynamic_info[DT_SYMTAB]
978c4450 10993 && filedata->dynamic_info[DT_SYMENT])
10ca4b04
L
10994 {
10995 Elf_Internal_Phdr *seg;
2482f306 10996 bfd_vma vma = filedata->dynamic_info[DT_SYMTAB];
252b5132 10997
2482f306
AM
10998 if (! get_program_headers (filedata))
10999 {
11000 error (_("Cannot interpret virtual addresses "
11001 "without program headers.\n"));
015dc7e1 11002 return false;
2482f306 11003 }
252b5132 11004
2482f306
AM
11005 for (seg = filedata->program_headers;
11006 seg < filedata->program_headers + filedata->file_header.e_phnum;
11007 ++seg)
11008 {
11009 if (seg->p_type != PT_LOAD)
11010 continue;
252b5132 11011
2482f306
AM
11012 if (seg->p_offset + seg->p_filesz > filedata->file_size)
11013 {
11014 /* See PR 21379 for a reproducer. */
11015 error (_("Invalid PT_LOAD entry\n"));
015dc7e1 11016 return false;
2482f306 11017 }
252b5132 11018
2482f306
AM
11019 if (vma >= (seg->p_vaddr & -seg->p_align)
11020 && vma < seg->p_vaddr + seg->p_filesz)
11021 {
11022 /* Since we do not know how big the symbol table is,
11023 we default to reading in up to the end of PT_LOAD
11024 segment and processing that. This is overkill, I
11025 know, but it should work. */
11026 Elf_Internal_Shdr section;
11027 section.sh_offset = (vma - seg->p_vaddr
11028 + seg->p_offset);
11029 section.sh_size = (num_of_syms
11030 * filedata->dynamic_info[DT_SYMENT]);
11031 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
8ac10c5b
L
11032
11033 if (do_checks
11034 && filedata->dynamic_symtab_section != NULL
11035 && ((filedata->dynamic_symtab_section->sh_offset
11036 != section.sh_offset)
11037 || (filedata->dynamic_symtab_section->sh_size
11038 != section.sh_size)
11039 || (filedata->dynamic_symtab_section->sh_entsize
11040 != section.sh_entsize)))
11041 warn (_("\
11042the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
11043
2482f306
AM
11044 section.sh_name = filedata->string_table_length;
11045 filedata->dynamic_symbols
4de91c10 11046 = get_elf_symbols (filedata, &section,
2482f306
AM
11047 &filedata->num_dynamic_syms);
11048 if (filedata->dynamic_symbols == NULL
11049 || filedata->num_dynamic_syms != num_of_syms)
11050 {
11051 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
015dc7e1 11052 return false;
2482f306
AM
11053 }
11054 break;
11055 }
11056 }
11057 }
11058 }
252b5132
RH
11059
11060 /* Similarly find a string table. */
978c4450
AM
11061 if (filedata->dynamic_strings == NULL)
11062 for (entry = filedata->dynamic_section;
11063 entry < filedata->dynamic_section + filedata->dynamic_nent;
10ca4b04
L
11064 ++entry)
11065 {
11066 if (entry->d_tag == DT_STRTAB)
978c4450 11067 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
252b5132 11068
10ca4b04 11069 if (entry->d_tag == DT_STRSZ)
978c4450 11070 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
252b5132 11071
978c4450
AM
11072 if (filedata->dynamic_info[DT_STRTAB]
11073 && filedata->dynamic_info[DT_STRSZ])
10ca4b04
L
11074 {
11075 unsigned long offset;
978c4450 11076 bfd_size_type str_tab_len = filedata->dynamic_info[DT_STRSZ];
10ca4b04
L
11077
11078 offset = offset_from_vma (filedata,
978c4450 11079 filedata->dynamic_info[DT_STRTAB],
10ca4b04 11080 str_tab_len);
8ac10c5b
L
11081 if (do_checks
11082 && filedata->dynamic_strtab_section
11083 && ((filedata->dynamic_strtab_section->sh_offset
11084 != (file_ptr) offset)
11085 || (filedata->dynamic_strtab_section->sh_size
11086 != str_tab_len)))
11087 warn (_("\
11088the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
11089
978c4450
AM
11090 filedata->dynamic_strings
11091 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
11092 _("dynamic string table"));
11093 if (filedata->dynamic_strings == NULL)
10ca4b04
L
11094 {
11095 error (_("Corrupt DT_STRTAB dynamic entry\n"));
11096 break;
11097 }
e3d39609 11098
978c4450 11099 filedata->dynamic_strings_length = str_tab_len;
10ca4b04
L
11100 break;
11101 }
11102 }
252b5132
RH
11103
11104 /* And find the syminfo section if available. */
978c4450 11105 if (filedata->dynamic_syminfo == NULL)
252b5132 11106 {
3e8bba36 11107 unsigned long syminsz = 0;
252b5132 11108
978c4450
AM
11109 for (entry = filedata->dynamic_section;
11110 entry < filedata->dynamic_section + filedata->dynamic_nent;
86dba8ee 11111 ++entry)
252b5132
RH
11112 {
11113 if (entry->d_tag == DT_SYMINENT)
11114 {
11115 /* Note: these braces are necessary to avoid a syntax
11116 error from the SunOS4 C compiler. */
049b0c3a
NC
11117 /* PR binutils/17531: A corrupt file can trigger this test.
11118 So do not use an assert, instead generate an error message. */
11119 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
071436c6 11120 error (_("Bad value (%d) for SYMINENT entry\n"),
049b0c3a 11121 (int) entry->d_un.d_val);
252b5132
RH
11122 }
11123 else if (entry->d_tag == DT_SYMINSZ)
11124 syminsz = entry->d_un.d_val;
11125 else if (entry->d_tag == DT_SYMINFO)
978c4450
AM
11126 filedata->dynamic_syminfo_offset
11127 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
252b5132
RH
11128 }
11129
978c4450 11130 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
252b5132 11131 {
2cf0635d
NC
11132 Elf_External_Syminfo * extsyminfo;
11133 Elf_External_Syminfo * extsym;
11134 Elf_Internal_Syminfo * syminfo;
252b5132
RH
11135
11136 /* There is a syminfo section. Read the data. */
3f5e193b 11137 extsyminfo = (Elf_External_Syminfo *)
978c4450
AM
11138 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
11139 1, syminsz, _("symbol information"));
a6e9f9df 11140 if (!extsyminfo)
015dc7e1 11141 return false;
252b5132 11142
978c4450 11143 if (filedata->dynamic_syminfo != NULL)
e3d39609
NC
11144 {
11145 error (_("Multiple dynamic symbol information sections found\n"));
978c4450 11146 free (filedata->dynamic_syminfo);
e3d39609 11147 }
978c4450
AM
11148 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
11149 if (filedata->dynamic_syminfo == NULL)
252b5132 11150 {
2482f306
AM
11151 error (_("Out of memory allocating %lu bytes "
11152 "for dynamic symbol info\n"),
8b73c356 11153 (unsigned long) syminsz);
015dc7e1 11154 return false;
252b5132
RH
11155 }
11156
2482f306
AM
11157 filedata->dynamic_syminfo_nent
11158 = syminsz / sizeof (Elf_External_Syminfo);
978c4450 11159 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
2482f306
AM
11160 syminfo < (filedata->dynamic_syminfo
11161 + filedata->dynamic_syminfo_nent);
86dba8ee 11162 ++syminfo, ++extsym)
252b5132 11163 {
86dba8ee
AM
11164 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
11165 syminfo->si_flags = BYTE_GET (extsym->si_flags);
252b5132
RH
11166 }
11167
11168 free (extsyminfo);
11169 }
11170 }
11171
978c4450 11172 if (do_dynamic && filedata->dynamic_addr)
ca0e11aa 11173 {
f253158f
NC
11174 if (filedata->is_separate)
11175 printf (ngettext ("\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entry:\n",
11176 "\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entries:\n",
11177 (unsigned long) filedata->dynamic_nent),
11178 filedata->file_name,
11179 filedata->dynamic_addr,
11180 (unsigned long) filedata->dynamic_nent);
ca0e11aa 11181 else
f253158f
NC
11182 printf (ngettext ("\nDynamic section at offset 0x%lx contains %lu entry:\n",
11183 "\nDynamic section at offset 0x%lx contains %lu entries:\n",
11184 (unsigned long) filedata->dynamic_nent),
ca0e11aa
NC
11185 filedata->dynamic_addr,
11186 (unsigned long) filedata->dynamic_nent);
ca0e11aa 11187 }
252b5132
RH
11188 if (do_dynamic)
11189 printf (_(" Tag Type Name/Value\n"));
11190
978c4450
AM
11191 for (entry = filedata->dynamic_section;
11192 entry < filedata->dynamic_section + filedata->dynamic_nent;
86dba8ee 11193 entry++)
252b5132
RH
11194 {
11195 if (do_dynamic)
f7a99963 11196 {
2cf0635d 11197 const char * dtype;
e699b9ff 11198
f7a99963
NC
11199 putchar (' ');
11200 print_vma (entry->d_tag, FULL_HEX);
dda8d76d 11201 dtype = get_dynamic_type (filedata, entry->d_tag);
e699b9ff 11202 printf (" (%s)%*s", dtype,
32ec8896 11203 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
f7a99963 11204 }
252b5132
RH
11205
11206 switch (entry->d_tag)
11207 {
d1133906
NC
11208 case DT_FLAGS:
11209 if (do_dynamic)
e9e44622 11210 print_dynamic_flags (entry->d_un.d_val);
d1133906 11211 break;
76da6bbe 11212
252b5132
RH
11213 case DT_AUXILIARY:
11214 case DT_FILTER:
019148e4
L
11215 case DT_CONFIG:
11216 case DT_DEPAUDIT:
11217 case DT_AUDIT:
252b5132
RH
11218 if (do_dynamic)
11219 {
019148e4 11220 switch (entry->d_tag)
b34976b6 11221 {
019148e4
L
11222 case DT_AUXILIARY:
11223 printf (_("Auxiliary library"));
11224 break;
11225
11226 case DT_FILTER:
11227 printf (_("Filter library"));
11228 break;
11229
b34976b6 11230 case DT_CONFIG:
019148e4
L
11231 printf (_("Configuration file"));
11232 break;
11233
11234 case DT_DEPAUDIT:
11235 printf (_("Dependency audit library"));
11236 break;
11237
11238 case DT_AUDIT:
11239 printf (_("Audit library"));
11240 break;
11241 }
252b5132 11242
84714f86 11243 if (valid_dynamic_name (filedata, entry->d_un.d_val))
978c4450 11244 printf (": [%s]\n",
84714f86 11245 get_dynamic_name (filedata, entry->d_un.d_val));
252b5132 11246 else
f7a99963
NC
11247 {
11248 printf (": ");
11249 print_vma (entry->d_un.d_val, PREFIX_HEX);
11250 putchar ('\n');
11251 }
252b5132
RH
11252 }
11253 break;
11254
dcefbbbd 11255 case DT_FEATURE:
252b5132
RH
11256 if (do_dynamic)
11257 {
11258 printf (_("Flags:"));
86f55779 11259
252b5132
RH
11260 if (entry->d_un.d_val == 0)
11261 printf (_(" None\n"));
11262 else
11263 {
11264 unsigned long int val = entry->d_un.d_val;
86f55779 11265
252b5132
RH
11266 if (val & DTF_1_PARINIT)
11267 {
11268 printf (" PARINIT");
11269 val ^= DTF_1_PARINIT;
11270 }
dcefbbbd
L
11271 if (val & DTF_1_CONFEXP)
11272 {
11273 printf (" CONFEXP");
11274 val ^= DTF_1_CONFEXP;
11275 }
252b5132
RH
11276 if (val != 0)
11277 printf (" %lx", val);
11278 puts ("");
11279 }
11280 }
11281 break;
11282
11283 case DT_POSFLAG_1:
11284 if (do_dynamic)
11285 {
11286 printf (_("Flags:"));
86f55779 11287
252b5132
RH
11288 if (entry->d_un.d_val == 0)
11289 printf (_(" None\n"));
11290 else
11291 {
11292 unsigned long int val = entry->d_un.d_val;
86f55779 11293
252b5132
RH
11294 if (val & DF_P1_LAZYLOAD)
11295 {
11296 printf (" LAZYLOAD");
11297 val ^= DF_P1_LAZYLOAD;
11298 }
11299 if (val & DF_P1_GROUPPERM)
11300 {
11301 printf (" GROUPPERM");
11302 val ^= DF_P1_GROUPPERM;
11303 }
11304 if (val != 0)
11305 printf (" %lx", val);
11306 puts ("");
11307 }
11308 }
11309 break;
11310
11311 case DT_FLAGS_1:
11312 if (do_dynamic)
11313 {
11314 printf (_("Flags:"));
11315 if (entry->d_un.d_val == 0)
11316 printf (_(" None\n"));
11317 else
11318 {
11319 unsigned long int val = entry->d_un.d_val;
86f55779 11320
252b5132
RH
11321 if (val & DF_1_NOW)
11322 {
11323 printf (" NOW");
11324 val ^= DF_1_NOW;
11325 }
11326 if (val & DF_1_GLOBAL)
11327 {
11328 printf (" GLOBAL");
11329 val ^= DF_1_GLOBAL;
11330 }
11331 if (val & DF_1_GROUP)
11332 {
11333 printf (" GROUP");
11334 val ^= DF_1_GROUP;
11335 }
11336 if (val & DF_1_NODELETE)
11337 {
11338 printf (" NODELETE");
11339 val ^= DF_1_NODELETE;
11340 }
11341 if (val & DF_1_LOADFLTR)
11342 {
11343 printf (" LOADFLTR");
11344 val ^= DF_1_LOADFLTR;
11345 }
11346 if (val & DF_1_INITFIRST)
11347 {
11348 printf (" INITFIRST");
11349 val ^= DF_1_INITFIRST;
11350 }
11351 if (val & DF_1_NOOPEN)
11352 {
11353 printf (" NOOPEN");
11354 val ^= DF_1_NOOPEN;
11355 }
11356 if (val & DF_1_ORIGIN)
11357 {
11358 printf (" ORIGIN");
11359 val ^= DF_1_ORIGIN;
11360 }
11361 if (val & DF_1_DIRECT)
11362 {
11363 printf (" DIRECT");
11364 val ^= DF_1_DIRECT;
11365 }
11366 if (val & DF_1_TRANS)
11367 {
11368 printf (" TRANS");
11369 val ^= DF_1_TRANS;
11370 }
11371 if (val & DF_1_INTERPOSE)
11372 {
11373 printf (" INTERPOSE");
11374 val ^= DF_1_INTERPOSE;
11375 }
f7db6139 11376 if (val & DF_1_NODEFLIB)
dcefbbbd 11377 {
f7db6139
L
11378 printf (" NODEFLIB");
11379 val ^= DF_1_NODEFLIB;
dcefbbbd
L
11380 }
11381 if (val & DF_1_NODUMP)
11382 {
11383 printf (" NODUMP");
11384 val ^= DF_1_NODUMP;
11385 }
34b60028 11386 if (val & DF_1_CONFALT)
dcefbbbd 11387 {
34b60028
L
11388 printf (" CONFALT");
11389 val ^= DF_1_CONFALT;
11390 }
11391 if (val & DF_1_ENDFILTEE)
11392 {
11393 printf (" ENDFILTEE");
11394 val ^= DF_1_ENDFILTEE;
11395 }
11396 if (val & DF_1_DISPRELDNE)
11397 {
11398 printf (" DISPRELDNE");
11399 val ^= DF_1_DISPRELDNE;
11400 }
11401 if (val & DF_1_DISPRELPND)
11402 {
11403 printf (" DISPRELPND");
11404 val ^= DF_1_DISPRELPND;
11405 }
11406 if (val & DF_1_NODIRECT)
11407 {
11408 printf (" NODIRECT");
11409 val ^= DF_1_NODIRECT;
11410 }
11411 if (val & DF_1_IGNMULDEF)
11412 {
11413 printf (" IGNMULDEF");
11414 val ^= DF_1_IGNMULDEF;
11415 }
11416 if (val & DF_1_NOKSYMS)
11417 {
11418 printf (" NOKSYMS");
11419 val ^= DF_1_NOKSYMS;
11420 }
11421 if (val & DF_1_NOHDR)
11422 {
11423 printf (" NOHDR");
11424 val ^= DF_1_NOHDR;
11425 }
11426 if (val & DF_1_EDITED)
11427 {
11428 printf (" EDITED");
11429 val ^= DF_1_EDITED;
11430 }
11431 if (val & DF_1_NORELOC)
11432 {
11433 printf (" NORELOC");
11434 val ^= DF_1_NORELOC;
11435 }
11436 if (val & DF_1_SYMINTPOSE)
11437 {
11438 printf (" SYMINTPOSE");
11439 val ^= DF_1_SYMINTPOSE;
11440 }
11441 if (val & DF_1_GLOBAUDIT)
11442 {
11443 printf (" GLOBAUDIT");
11444 val ^= DF_1_GLOBAUDIT;
11445 }
11446 if (val & DF_1_SINGLETON)
11447 {
11448 printf (" SINGLETON");
11449 val ^= DF_1_SINGLETON;
dcefbbbd 11450 }
5c383f02
RO
11451 if (val & DF_1_STUB)
11452 {
11453 printf (" STUB");
11454 val ^= DF_1_STUB;
11455 }
11456 if (val & DF_1_PIE)
11457 {
11458 printf (" PIE");
11459 val ^= DF_1_PIE;
11460 }
b1202ffa
L
11461 if (val & DF_1_KMOD)
11462 {
11463 printf (" KMOD");
11464 val ^= DF_1_KMOD;
11465 }
11466 if (val & DF_1_WEAKFILTER)
11467 {
11468 printf (" WEAKFILTER");
11469 val ^= DF_1_WEAKFILTER;
11470 }
11471 if (val & DF_1_NOCOMMON)
11472 {
11473 printf (" NOCOMMON");
11474 val ^= DF_1_NOCOMMON;
11475 }
252b5132
RH
11476 if (val != 0)
11477 printf (" %lx", val);
11478 puts ("");
11479 }
11480 }
11481 break;
11482
11483 case DT_PLTREL:
978c4450 11484 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
252b5132 11485 if (do_dynamic)
dda8d76d 11486 puts (get_dynamic_type (filedata, entry->d_un.d_val));
252b5132
RH
11487 break;
11488
11489 case DT_NULL :
11490 case DT_NEEDED :
11491 case DT_PLTGOT :
11492 case DT_HASH :
11493 case DT_STRTAB :
11494 case DT_SYMTAB :
11495 case DT_RELA :
11496 case DT_INIT :
11497 case DT_FINI :
11498 case DT_SONAME :
11499 case DT_RPATH :
11500 case DT_SYMBOLIC:
11501 case DT_REL :
11502 case DT_DEBUG :
11503 case DT_TEXTREL :
11504 case DT_JMPREL :
019148e4 11505 case DT_RUNPATH :
978c4450 11506 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
252b5132
RH
11507
11508 if (do_dynamic)
11509 {
84714f86 11510 const char *name;
252b5132 11511
84714f86
AM
11512 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11513 name = get_dynamic_name (filedata, entry->d_un.d_val);
252b5132 11514 else
d79b3d50 11515 name = NULL;
252b5132
RH
11516
11517 if (name)
11518 {
11519 switch (entry->d_tag)
11520 {
11521 case DT_NEEDED:
11522 printf (_("Shared library: [%s]"), name);
11523
13acb58d
AM
11524 if (filedata->program_interpreter
11525 && streq (name, filedata->program_interpreter))
f7a99963 11526 printf (_(" program interpreter"));
252b5132
RH
11527 break;
11528
11529 case DT_SONAME:
f7a99963 11530 printf (_("Library soname: [%s]"), name);
252b5132
RH
11531 break;
11532
11533 case DT_RPATH:
f7a99963 11534 printf (_("Library rpath: [%s]"), name);
252b5132
RH
11535 break;
11536
019148e4
L
11537 case DT_RUNPATH:
11538 printf (_("Library runpath: [%s]"), name);
11539 break;
11540
252b5132 11541 default:
f7a99963
NC
11542 print_vma (entry->d_un.d_val, PREFIX_HEX);
11543 break;
252b5132
RH
11544 }
11545 }
11546 else
f7a99963
NC
11547 print_vma (entry->d_un.d_val, PREFIX_HEX);
11548
11549 putchar ('\n');
252b5132
RH
11550 }
11551 break;
11552
11553 case DT_PLTRELSZ:
11554 case DT_RELASZ :
11555 case DT_STRSZ :
11556 case DT_RELSZ :
11557 case DT_RELAENT :
11558 case DT_SYMENT :
11559 case DT_RELENT :
978c4450 11560 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
1a0670f3 11561 /* Fall through. */
252b5132
RH
11562 case DT_PLTPADSZ:
11563 case DT_MOVEENT :
11564 case DT_MOVESZ :
dd207c13
FS
11565 case DT_RELRENT :
11566 case DT_RELRSZ :
04d8355a 11567 case DT_PREINIT_ARRAYSZ:
252b5132
RH
11568 case DT_INIT_ARRAYSZ:
11569 case DT_FINI_ARRAYSZ:
047b2264
JJ
11570 case DT_GNU_CONFLICTSZ:
11571 case DT_GNU_LIBLISTSZ:
252b5132 11572 if (do_dynamic)
f7a99963
NC
11573 {
11574 print_vma (entry->d_un.d_val, UNSIGNED);
2b692964 11575 printf (_(" (bytes)\n"));
f7a99963 11576 }
252b5132
RH
11577 break;
11578
11579 case DT_VERDEFNUM:
11580 case DT_VERNEEDNUM:
11581 case DT_RELACOUNT:
11582 case DT_RELCOUNT:
11583 if (do_dynamic)
f7a99963
NC
11584 {
11585 print_vma (entry->d_un.d_val, UNSIGNED);
11586 putchar ('\n');
11587 }
252b5132
RH
11588 break;
11589
11590 case DT_SYMINSZ:
11591 case DT_SYMINENT:
11592 case DT_SYMINFO:
11593 case DT_USED:
11594 case DT_INIT_ARRAY:
11595 case DT_FINI_ARRAY:
11596 if (do_dynamic)
11597 {
d79b3d50 11598 if (entry->d_tag == DT_USED
84714f86 11599 && valid_dynamic_name (filedata, entry->d_un.d_val))
252b5132 11600 {
84714f86
AM
11601 const char *name
11602 = get_dynamic_name (filedata, entry->d_un.d_val);
252b5132 11603
b34976b6 11604 if (*name)
252b5132
RH
11605 {
11606 printf (_("Not needed object: [%s]\n"), name);
11607 break;
11608 }
11609 }
103f02d3 11610
f7a99963
NC
11611 print_vma (entry->d_un.d_val, PREFIX_HEX);
11612 putchar ('\n');
252b5132
RH
11613 }
11614 break;
11615
11616 case DT_BIND_NOW:
11617 /* The value of this entry is ignored. */
35b1837e
AM
11618 if (do_dynamic)
11619 putchar ('\n');
252b5132 11620 break;
103f02d3 11621
047b2264
JJ
11622 case DT_GNU_PRELINKED:
11623 if (do_dynamic)
11624 {
2cf0635d 11625 struct tm * tmp;
91d6fa6a 11626 time_t atime = entry->d_un.d_val;
047b2264 11627
91d6fa6a 11628 tmp = gmtime (&atime);
071436c6
NC
11629 /* PR 17533 file: 041-1244816-0.004. */
11630 if (tmp == NULL)
5a2cbcf4
L
11631 printf (_("<corrupt time val: %lx"),
11632 (unsigned long) atime);
071436c6
NC
11633 else
11634 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
11635 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11636 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
047b2264
JJ
11637
11638 }
11639 break;
11640
fdc90cb4 11641 case DT_GNU_HASH:
978c4450 11642 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
fdc90cb4
JJ
11643 if (do_dynamic)
11644 {
11645 print_vma (entry->d_un.d_val, PREFIX_HEX);
11646 putchar ('\n');
11647 }
11648 break;
11649
a5da3dee
VDM
11650 case DT_GNU_FLAGS_1:
11651 if (do_dynamic)
11652 {
11653 printf (_("Flags:"));
11654 if (entry->d_un.d_val == 0)
11655 printf (_(" None\n"));
11656 else
11657 {
11658 unsigned long int val = entry->d_un.d_val;
11659
11660 if (val & DF_GNU_1_UNIQUE)
11661 {
11662 printf (" UNIQUE");
11663 val ^= DF_GNU_1_UNIQUE;
11664 }
11665 if (val != 0)
11666 printf (" %lx", val);
11667 puts ("");
11668 }
11669 }
11670 break;
11671
252b5132
RH
11672 default:
11673 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
978c4450
AM
11674 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
11675 = entry->d_un.d_val;
252b5132
RH
11676
11677 if (do_dynamic)
11678 {
dda8d76d 11679 switch (filedata->file_header.e_machine)
252b5132 11680 {
37c18eed
SD
11681 case EM_AARCH64:
11682 dynamic_section_aarch64_val (entry);
11683 break;
252b5132 11684 case EM_MIPS:
4fe85591 11685 case EM_MIPS_RS3_LE:
978c4450 11686 dynamic_section_mips_val (filedata, entry);
252b5132 11687 break;
103f02d3 11688 case EM_PARISC:
b2d38a17 11689 dynamic_section_parisc_val (entry);
103f02d3 11690 break;
ecc51f48 11691 case EM_IA_64:
b2d38a17 11692 dynamic_section_ia64_val (entry);
ecc51f48 11693 break;
252b5132 11694 default:
f7a99963
NC
11695 print_vma (entry->d_un.d_val, PREFIX_HEX);
11696 putchar ('\n');
252b5132
RH
11697 }
11698 }
11699 break;
11700 }
11701 }
11702
015dc7e1 11703 return true;
252b5132
RH
11704}
11705
11706static char *
d3ba0551 11707get_ver_flags (unsigned int flags)
252b5132 11708{
6d4f21f6 11709 static char buff[128];
252b5132
RH
11710
11711 buff[0] = 0;
11712
11713 if (flags == 0)
11714 return _("none");
11715
11716 if (flags & VER_FLG_BASE)
7bb1ad17 11717 strcat (buff, "BASE");
252b5132
RH
11718
11719 if (flags & VER_FLG_WEAK)
11720 {
11721 if (flags & VER_FLG_BASE)
7bb1ad17 11722 strcat (buff, " | ");
252b5132 11723
7bb1ad17 11724 strcat (buff, "WEAK");
252b5132
RH
11725 }
11726
44ec90b9
RO
11727 if (flags & VER_FLG_INFO)
11728 {
11729 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
7bb1ad17 11730 strcat (buff, " | ");
44ec90b9 11731
7bb1ad17 11732 strcat (buff, "INFO");
44ec90b9
RO
11733 }
11734
11735 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
7bb1ad17
MR
11736 {
11737 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11738 strcat (buff, " | ");
11739
11740 strcat (buff, _("<unknown>"));
11741 }
252b5132
RH
11742
11743 return buff;
11744}
11745
11746/* Display the contents of the version sections. */
98fb390a 11747
015dc7e1 11748static bool
dda8d76d 11749process_version_sections (Filedata * filedata)
252b5132 11750{
2cf0635d 11751 Elf_Internal_Shdr * section;
b34976b6 11752 unsigned i;
015dc7e1 11753 bool found = false;
252b5132
RH
11754
11755 if (! do_version)
015dc7e1 11756 return true;
252b5132 11757
dda8d76d
NC
11758 for (i = 0, section = filedata->section_headers;
11759 i < filedata->file_header.e_shnum;
b34976b6 11760 i++, section++)
252b5132
RH
11761 {
11762 switch (section->sh_type)
11763 {
11764 case SHT_GNU_verdef:
11765 {
2cf0635d 11766 Elf_External_Verdef * edefs;
452bf675
AM
11767 unsigned long idx;
11768 unsigned long cnt;
2cf0635d 11769 char * endbuf;
252b5132 11770
015dc7e1 11771 found = true;
252b5132 11772
ca0e11aa
NC
11773 if (filedata->is_separate)
11774 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
11775 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
11776 section->sh_info),
11777 filedata->file_name,
11778 printable_section_name (filedata, section),
11779 section->sh_info);
11780 else
11781 printf (ngettext ("\nVersion definition section '%s' "
11782 "contains %u entry:\n",
11783 "\nVersion definition section '%s' "
11784 "contains %u entries:\n",
11785 section->sh_info),
11786 printable_section_name (filedata, section),
11787 section->sh_info);
047c3dbf 11788
ae9ac79e 11789 printf (_(" Addr: 0x"));
252b5132 11790 printf_vma (section->sh_addr);
233f82cf 11791 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
1b228002 11792 (unsigned long) section->sh_offset, section->sh_link,
dda8d76d 11793 printable_section_name_from_index (filedata, section->sh_link));
252b5132 11794
3f5e193b 11795 edefs = (Elf_External_Verdef *)
dda8d76d 11796 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
3f5e193b 11797 _("version definition section"));
a6e9f9df
AM
11798 if (!edefs)
11799 break;
59245841 11800 endbuf = (char *) edefs + section->sh_size;
252b5132 11801
1445030f 11802 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
252b5132 11803 {
2cf0635d
NC
11804 char * vstart;
11805 Elf_External_Verdef * edef;
b34976b6 11806 Elf_Internal_Verdef ent;
2cf0635d 11807 Elf_External_Verdaux * eaux;
b34976b6 11808 Elf_Internal_Verdaux aux;
452bf675 11809 unsigned long isum;
b34976b6 11810 int j;
103f02d3 11811
252b5132 11812 vstart = ((char *) edefs) + idx;
54806181
AM
11813 if (vstart + sizeof (*edef) > endbuf)
11814 break;
252b5132
RH
11815
11816 edef = (Elf_External_Verdef *) vstart;
11817
11818 ent.vd_version = BYTE_GET (edef->vd_version);
11819 ent.vd_flags = BYTE_GET (edef->vd_flags);
11820 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
11821 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
11822 ent.vd_hash = BYTE_GET (edef->vd_hash);
11823 ent.vd_aux = BYTE_GET (edef->vd_aux);
11824 ent.vd_next = BYTE_GET (edef->vd_next);
11825
452bf675 11826 printf (_(" %#06lx: Rev: %d Flags: %s"),
252b5132
RH
11827 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
11828
11829 printf (_(" Index: %d Cnt: %d "),
11830 ent.vd_ndx, ent.vd_cnt);
11831
452bf675 11832 /* Check for overflow. */
1445030f 11833 if (ent.vd_aux > (size_t) (endbuf - vstart))
dd24e3da
NC
11834 break;
11835
252b5132
RH
11836 vstart += ent.vd_aux;
11837
1445030f
AM
11838 if (vstart + sizeof (*eaux) > endbuf)
11839 break;
252b5132
RH
11840 eaux = (Elf_External_Verdaux *) vstart;
11841
11842 aux.vda_name = BYTE_GET (eaux->vda_name);
11843 aux.vda_next = BYTE_GET (eaux->vda_next);
11844
84714f86 11845 if (valid_dynamic_name (filedata, aux.vda_name))
978c4450 11846 printf (_("Name: %s\n"),
84714f86 11847 get_dynamic_name (filedata, aux.vda_name));
252b5132
RH
11848 else
11849 printf (_("Name index: %ld\n"), aux.vda_name);
11850
11851 isum = idx + ent.vd_aux;
11852
b34976b6 11853 for (j = 1; j < ent.vd_cnt; j++)
252b5132 11854 {
1445030f
AM
11855 if (aux.vda_next < sizeof (*eaux)
11856 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
11857 {
11858 warn (_("Invalid vda_next field of %lx\n"),
11859 aux.vda_next);
11860 j = ent.vd_cnt;
11861 break;
11862 }
dd24e3da 11863 /* Check for overflow. */
7e26601c 11864 if (aux.vda_next > (size_t) (endbuf - vstart))
dd24e3da
NC
11865 break;
11866
252b5132
RH
11867 isum += aux.vda_next;
11868 vstart += aux.vda_next;
11869
54806181
AM
11870 if (vstart + sizeof (*eaux) > endbuf)
11871 break;
1445030f 11872 eaux = (Elf_External_Verdaux *) vstart;
252b5132
RH
11873
11874 aux.vda_name = BYTE_GET (eaux->vda_name);
11875 aux.vda_next = BYTE_GET (eaux->vda_next);
11876
84714f86 11877 if (valid_dynamic_name (filedata, aux.vda_name))
452bf675 11878 printf (_(" %#06lx: Parent %d: %s\n"),
978c4450 11879 isum, j,
84714f86 11880 get_dynamic_name (filedata, aux.vda_name));
252b5132 11881 else
452bf675 11882 printf (_(" %#06lx: Parent %d, name index: %ld\n"),
252b5132
RH
11883 isum, j, aux.vda_name);
11884 }
dd24e3da 11885
54806181
AM
11886 if (j < ent.vd_cnt)
11887 printf (_(" Version def aux past end of section\n"));
252b5132 11888
c9f02c3e
MR
11889 /* PR 17531:
11890 file: id:000001,src:000172+005151,op:splice,rep:2. */
1445030f
AM
11891 if (ent.vd_next < sizeof (*edef)
11892 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
11893 {
11894 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
11895 cnt = section->sh_info;
11896 break;
11897 }
452bf675 11898 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
5d921cbd
NC
11899 break;
11900
252b5132
RH
11901 idx += ent.vd_next;
11902 }
dd24e3da 11903
54806181
AM
11904 if (cnt < section->sh_info)
11905 printf (_(" Version definition past end of section\n"));
252b5132
RH
11906
11907 free (edefs);
11908 }
11909 break;
103f02d3 11910
252b5132
RH
11911 case SHT_GNU_verneed:
11912 {
2cf0635d 11913 Elf_External_Verneed * eneed;
452bf675
AM
11914 unsigned long idx;
11915 unsigned long cnt;
2cf0635d 11916 char * endbuf;
252b5132 11917
015dc7e1 11918 found = true;
252b5132 11919
ca0e11aa
NC
11920 if (filedata->is_separate)
11921 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
11922 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
11923 section->sh_info),
11924 filedata->file_name,
11925 printable_section_name (filedata, section),
11926 section->sh_info);
11927 else
11928 printf (ngettext ("\nVersion needs section '%s' "
11929 "contains %u entry:\n",
11930 "\nVersion needs section '%s' "
11931 "contains %u entries:\n",
11932 section->sh_info),
11933 printable_section_name (filedata, section),
11934 section->sh_info);
047c3dbf 11935
252b5132
RH
11936 printf (_(" Addr: 0x"));
11937 printf_vma (section->sh_addr);
72de5009 11938 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
1b228002 11939 (unsigned long) section->sh_offset, section->sh_link,
dda8d76d 11940 printable_section_name_from_index (filedata, section->sh_link));
252b5132 11941
dda8d76d 11942 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
3f5e193b
NC
11943 section->sh_offset, 1,
11944 section->sh_size,
9cf03b7e 11945 _("Version Needs section"));
a6e9f9df
AM
11946 if (!eneed)
11947 break;
59245841 11948 endbuf = (char *) eneed + section->sh_size;
252b5132
RH
11949
11950 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
11951 {
2cf0635d 11952 Elf_External_Verneed * entry;
b34976b6 11953 Elf_Internal_Verneed ent;
452bf675 11954 unsigned long isum;
b34976b6 11955 int j;
2cf0635d 11956 char * vstart;
252b5132
RH
11957
11958 vstart = ((char *) eneed) + idx;
54806181
AM
11959 if (vstart + sizeof (*entry) > endbuf)
11960 break;
252b5132
RH
11961
11962 entry = (Elf_External_Verneed *) vstart;
11963
11964 ent.vn_version = BYTE_GET (entry->vn_version);
11965 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
11966 ent.vn_file = BYTE_GET (entry->vn_file);
11967 ent.vn_aux = BYTE_GET (entry->vn_aux);
11968 ent.vn_next = BYTE_GET (entry->vn_next);
11969
452bf675 11970 printf (_(" %#06lx: Version: %d"), idx, ent.vn_version);
252b5132 11971
84714f86 11972 if (valid_dynamic_name (filedata, ent.vn_file))
978c4450 11973 printf (_(" File: %s"),
84714f86 11974 get_dynamic_name (filedata, ent.vn_file));
252b5132
RH
11975 else
11976 printf (_(" File: %lx"), ent.vn_file);
11977
11978 printf (_(" Cnt: %d\n"), ent.vn_cnt);
11979
dd24e3da 11980 /* Check for overflow. */
7e26601c 11981 if (ent.vn_aux > (size_t) (endbuf - vstart))
dd24e3da 11982 break;
252b5132
RH
11983 vstart += ent.vn_aux;
11984
11985 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
11986 {
2cf0635d 11987 Elf_External_Vernaux * eaux;
b34976b6 11988 Elf_Internal_Vernaux aux;
252b5132 11989
54806181
AM
11990 if (vstart + sizeof (*eaux) > endbuf)
11991 break;
252b5132
RH
11992 eaux = (Elf_External_Vernaux *) vstart;
11993
11994 aux.vna_hash = BYTE_GET (eaux->vna_hash);
11995 aux.vna_flags = BYTE_GET (eaux->vna_flags);
11996 aux.vna_other = BYTE_GET (eaux->vna_other);
11997 aux.vna_name = BYTE_GET (eaux->vna_name);
11998 aux.vna_next = BYTE_GET (eaux->vna_next);
11999
84714f86 12000 if (valid_dynamic_name (filedata, aux.vna_name))
452bf675 12001 printf (_(" %#06lx: Name: %s"),
84714f86 12002 isum, get_dynamic_name (filedata, aux.vna_name));
252b5132 12003 else
452bf675 12004 printf (_(" %#06lx: Name index: %lx"),
252b5132
RH
12005 isum, aux.vna_name);
12006
12007 printf (_(" Flags: %s Version: %d\n"),
12008 get_ver_flags (aux.vna_flags), aux.vna_other);
12009
1445030f
AM
12010 if (aux.vna_next < sizeof (*eaux)
12011 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
53774b7e
NC
12012 {
12013 warn (_("Invalid vna_next field of %lx\n"),
12014 aux.vna_next);
12015 j = ent.vn_cnt;
12016 break;
12017 }
1445030f
AM
12018 /* Check for overflow. */
12019 if (aux.vna_next > (size_t) (endbuf - vstart))
12020 break;
252b5132
RH
12021 isum += aux.vna_next;
12022 vstart += aux.vna_next;
12023 }
9cf03b7e 12024
54806181 12025 if (j < ent.vn_cnt)
f9a6a8f0 12026 warn (_("Missing Version Needs auxiliary information\n"));
252b5132 12027
1445030f
AM
12028 if (ent.vn_next < sizeof (*entry)
12029 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
c24cf8b6 12030 {
452bf675 12031 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
c24cf8b6
NC
12032 cnt = section->sh_info;
12033 break;
12034 }
1445030f
AM
12035 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
12036 break;
252b5132
RH
12037 idx += ent.vn_next;
12038 }
9cf03b7e 12039
54806181 12040 if (cnt < section->sh_info)
9cf03b7e 12041 warn (_("Missing Version Needs information\n"));
103f02d3 12042
252b5132
RH
12043 free (eneed);
12044 }
12045 break;
12046
12047 case SHT_GNU_versym:
12048 {
2cf0635d 12049 Elf_Internal_Shdr * link_section;
8b73c356
NC
12050 size_t total;
12051 unsigned int cnt;
2cf0635d
NC
12052 unsigned char * edata;
12053 unsigned short * data;
12054 char * strtab;
12055 Elf_Internal_Sym * symbols;
12056 Elf_Internal_Shdr * string_sec;
ba5cdace 12057 unsigned long num_syms;
d3ba0551 12058 long off;
252b5132 12059
dda8d76d 12060 if (section->sh_link >= filedata->file_header.e_shnum)
c256ffe7
JJ
12061 break;
12062
dda8d76d 12063 link_section = filedata->section_headers + section->sh_link;
08d8fa11 12064 total = section->sh_size / sizeof (Elf_External_Versym);
252b5132 12065
dda8d76d 12066 if (link_section->sh_link >= filedata->file_header.e_shnum)
c256ffe7
JJ
12067 break;
12068
015dc7e1 12069 found = true;
252b5132 12070
4de91c10 12071 symbols = get_elf_symbols (filedata, link_section, & num_syms);
dd24e3da
NC
12072 if (symbols == NULL)
12073 break;
252b5132 12074
dda8d76d 12075 string_sec = filedata->section_headers + link_section->sh_link;
252b5132 12076
dda8d76d 12077 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
3f5e193b
NC
12078 string_sec->sh_size,
12079 _("version string table"));
a6e9f9df 12080 if (!strtab)
0429c154
MS
12081 {
12082 free (symbols);
12083 break;
12084 }
252b5132 12085
ca0e11aa
NC
12086 if (filedata->is_separate)
12087 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %lu entry:\n",
12088 "\nIn linked file '%s' the version symbols section '%s' contains %lu entries:\n",
12089 total),
12090 filedata->file_name,
12091 printable_section_name (filedata, section),
12092 (unsigned long) total);
12093 else
12094 printf (ngettext ("\nVersion symbols section '%s' "
12095 "contains %lu entry:\n",
12096 "\nVersion symbols section '%s' "
12097 "contains %lu entries:\n",
12098 total),
12099 printable_section_name (filedata, section),
12100 (unsigned long) total);
252b5132 12101
ae9ac79e 12102 printf (_(" Addr: 0x"));
252b5132 12103 printf_vma (section->sh_addr);
72de5009 12104 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
1b228002 12105 (unsigned long) section->sh_offset, section->sh_link,
dda8d76d 12106 printable_section_name (filedata, link_section));
252b5132 12107
dda8d76d 12108 off = offset_from_vma (filedata,
978c4450 12109 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
d3ba0551 12110 total * sizeof (short));
95099889
AM
12111 edata = (unsigned char *) get_data (NULL, filedata, off,
12112 sizeof (short), total,
12113 _("version symbol data"));
a6e9f9df
AM
12114 if (!edata)
12115 {
12116 free (strtab);
0429c154 12117 free (symbols);
a6e9f9df
AM
12118 break;
12119 }
252b5132 12120
3f5e193b 12121 data = (short unsigned int *) cmalloc (total, sizeof (short));
252b5132
RH
12122
12123 for (cnt = total; cnt --;)
b34976b6
AM
12124 data[cnt] = byte_get (edata + cnt * sizeof (short),
12125 sizeof (short));
252b5132
RH
12126
12127 free (edata);
12128
12129 for (cnt = 0; cnt < total; cnt += 4)
12130 {
12131 int j, nn;
ab273396
AM
12132 char *name;
12133 char *invalid = _("*invalid*");
252b5132
RH
12134
12135 printf (" %03x:", cnt);
12136
12137 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
b34976b6 12138 switch (data[cnt + j])
252b5132
RH
12139 {
12140 case 0:
12141 fputs (_(" 0 (*local*) "), stdout);
12142 break;
12143
12144 case 1:
12145 fputs (_(" 1 (*global*) "), stdout);
12146 break;
12147
12148 default:
c244d050
NC
12149 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
12150 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
252b5132 12151
dd24e3da 12152 /* If this index value is greater than the size of the symbols
ba5cdace
NC
12153 array, break to avoid an out-of-bounds read. */
12154 if ((unsigned long)(cnt + j) >= num_syms)
dd24e3da
NC
12155 {
12156 warn (_("invalid index into symbol array\n"));
12157 break;
12158 }
12159
ab273396 12160 name = NULL;
978c4450 12161 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
252b5132 12162 {
b34976b6
AM
12163 Elf_Internal_Verneed ivn;
12164 unsigned long offset;
252b5132 12165
d93f0186 12166 offset = offset_from_vma
978c4450
AM
12167 (filedata,
12168 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
d93f0186 12169 sizeof (Elf_External_Verneed));
252b5132 12170
b34976b6 12171 do
252b5132 12172 {
b34976b6
AM
12173 Elf_Internal_Vernaux ivna;
12174 Elf_External_Verneed evn;
12175 Elf_External_Vernaux evna;
12176 unsigned long a_off;
252b5132 12177
dda8d76d 12178 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
59245841
NC
12179 _("version need")) == NULL)
12180 break;
0b4362b0 12181
252b5132
RH
12182 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12183 ivn.vn_next = BYTE_GET (evn.vn_next);
12184
12185 a_off = offset + ivn.vn_aux;
12186
12187 do
12188 {
dda8d76d 12189 if (get_data (&evna, filedata, a_off, sizeof (evna),
59245841
NC
12190 1, _("version need aux (2)")) == NULL)
12191 {
12192 ivna.vna_next = 0;
12193 ivna.vna_other = 0;
12194 }
12195 else
12196 {
12197 ivna.vna_next = BYTE_GET (evna.vna_next);
12198 ivna.vna_other = BYTE_GET (evna.vna_other);
12199 }
252b5132
RH
12200
12201 a_off += ivna.vna_next;
12202 }
b34976b6 12203 while (ivna.vna_other != data[cnt + j]
252b5132
RH
12204 && ivna.vna_next != 0);
12205
b34976b6 12206 if (ivna.vna_other == data[cnt + j])
252b5132
RH
12207 {
12208 ivna.vna_name = BYTE_GET (evna.vna_name);
12209
54806181 12210 if (ivna.vna_name >= string_sec->sh_size)
ab273396 12211 name = invalid;
54806181
AM
12212 else
12213 name = strtab + ivna.vna_name;
252b5132
RH
12214 break;
12215 }
12216
12217 offset += ivn.vn_next;
12218 }
12219 while (ivn.vn_next);
12220 }
00d93f34 12221
ab273396 12222 if (data[cnt + j] != 0x8001
978c4450 12223 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
252b5132 12224 {
b34976b6
AM
12225 Elf_Internal_Verdef ivd;
12226 Elf_External_Verdef evd;
12227 unsigned long offset;
252b5132 12228
d93f0186 12229 offset = offset_from_vma
978c4450
AM
12230 (filedata,
12231 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
d93f0186 12232 sizeof evd);
252b5132
RH
12233
12234 do
12235 {
dda8d76d 12236 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
59245841
NC
12237 _("version def")) == NULL)
12238 {
12239 ivd.vd_next = 0;
948f632f 12240 /* PR 17531: file: 046-1082287-0.004. */
3102e897
NC
12241 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
12242 break;
59245841
NC
12243 }
12244 else
12245 {
12246 ivd.vd_next = BYTE_GET (evd.vd_next);
12247 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12248 }
252b5132
RH
12249
12250 offset += ivd.vd_next;
12251 }
c244d050 12252 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
252b5132
RH
12253 && ivd.vd_next != 0);
12254
c244d050 12255 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
252b5132 12256 {
b34976b6
AM
12257 Elf_External_Verdaux evda;
12258 Elf_Internal_Verdaux ivda;
252b5132
RH
12259
12260 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12261
dda8d76d 12262 if (get_data (&evda, filedata,
59245841
NC
12263 offset - ivd.vd_next + ivd.vd_aux,
12264 sizeof (evda), 1,
12265 _("version def aux")) == NULL)
12266 break;
252b5132
RH
12267
12268 ivda.vda_name = BYTE_GET (evda.vda_name);
12269
54806181 12270 if (ivda.vda_name >= string_sec->sh_size)
ab273396
AM
12271 name = invalid;
12272 else if (name != NULL && name != invalid)
12273 name = _("*both*");
54806181
AM
12274 else
12275 name = strtab + ivda.vda_name;
252b5132
RH
12276 }
12277 }
ab273396
AM
12278 if (name != NULL)
12279 nn += printf ("(%s%-*s",
12280 name,
12281 12 - (int) strlen (name),
12282 ")");
252b5132
RH
12283
12284 if (nn < 18)
12285 printf ("%*c", 18 - nn, ' ');
12286 }
12287
12288 putchar ('\n');
12289 }
12290
12291 free (data);
12292 free (strtab);
12293 free (symbols);
12294 }
12295 break;
103f02d3 12296
252b5132
RH
12297 default:
12298 break;
12299 }
12300 }
12301
12302 if (! found)
ca0e11aa
NC
12303 {
12304 if (filedata->is_separate)
12305 printf (_("\nNo version information found in linked file '%s'.\n"),
12306 filedata->file_name);
12307 else
12308 printf (_("\nNo version information found in this file.\n"));
12309 }
252b5132 12310
015dc7e1 12311 return true;
252b5132
RH
12312}
12313
d1133906 12314static const char *
dda8d76d 12315get_symbol_binding (Filedata * filedata, unsigned int binding)
252b5132 12316{
89246a0e 12317 static char buff[64];
252b5132
RH
12318
12319 switch (binding)
12320 {
b34976b6
AM
12321 case STB_LOCAL: return "LOCAL";
12322 case STB_GLOBAL: return "GLOBAL";
12323 case STB_WEAK: return "WEAK";
252b5132
RH
12324 default:
12325 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
e9e44622
JJ
12326 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
12327 binding);
252b5132 12328 else if (binding >= STB_LOOS && binding <= STB_HIOS)
3e7a7d11
NC
12329 {
12330 if (binding == STB_GNU_UNIQUE
df3a023b 12331 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
3e7a7d11
NC
12332 return "UNIQUE";
12333 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
12334 }
252b5132 12335 else
e9e44622 12336 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
252b5132
RH
12337 return buff;
12338 }
12339}
12340
d1133906 12341static const char *
dda8d76d 12342get_symbol_type (Filedata * filedata, unsigned int type)
252b5132 12343{
89246a0e 12344 static char buff[64];
252b5132
RH
12345
12346 switch (type)
12347 {
b34976b6
AM
12348 case STT_NOTYPE: return "NOTYPE";
12349 case STT_OBJECT: return "OBJECT";
12350 case STT_FUNC: return "FUNC";
12351 case STT_SECTION: return "SECTION";
12352 case STT_FILE: return "FILE";
12353 case STT_COMMON: return "COMMON";
12354 case STT_TLS: return "TLS";
15ab5209
DB
12355 case STT_RELC: return "RELC";
12356 case STT_SRELC: return "SRELC";
252b5132
RH
12357 default:
12358 if (type >= STT_LOPROC && type <= STT_HIPROC)
df75f1af 12359 {
dda8d76d 12360 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
3510a7b8 12361 return "THUMB_FUNC";
103f02d3 12362
dda8d76d 12363 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
103f02d3
UD
12364 return "REGISTER";
12365
dda8d76d 12366 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
103f02d3
UD
12367 return "PARISC_MILLI";
12368
e9e44622 12369 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
df75f1af 12370 }
252b5132 12371 else if (type >= STT_LOOS && type <= STT_HIOS)
103f02d3 12372 {
dda8d76d 12373 if (filedata->file_header.e_machine == EM_PARISC)
103f02d3
UD
12374 {
12375 if (type == STT_HP_OPAQUE)
12376 return "HP_OPAQUE";
12377 if (type == STT_HP_STUB)
12378 return "HP_STUB";
12379 }
12380
d8045f23 12381 if (type == STT_GNU_IFUNC
dda8d76d 12382 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
df3a023b 12383 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
d8045f23
NC
12384 return "IFUNC";
12385
e9e44622 12386 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
103f02d3 12387 }
252b5132 12388 else
e9e44622 12389 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
252b5132
RH
12390 return buff;
12391 }
12392}
12393
d1133906 12394static const char *
d3ba0551 12395get_symbol_visibility (unsigned int visibility)
d1133906
NC
12396{
12397 switch (visibility)
12398 {
b34976b6
AM
12399 case STV_DEFAULT: return "DEFAULT";
12400 case STV_INTERNAL: return "INTERNAL";
12401 case STV_HIDDEN: return "HIDDEN";
d1133906 12402 case STV_PROTECTED: return "PROTECTED";
bee0ee85 12403 default:
27a45f42 12404 error (_("Unrecognized visibility value: %u\n"), visibility);
bee0ee85 12405 return _("<unknown>");
d1133906
NC
12406 }
12407}
12408
2057d69d
CZ
12409static const char *
12410get_alpha_symbol_other (unsigned int other)
9abca702 12411{
2057d69d
CZ
12412 switch (other)
12413 {
12414 case STO_ALPHA_NOPV: return "NOPV";
12415 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
12416 default:
27a45f42 12417 error (_("Unrecognized alpha specific other value: %u\n"), other);
2057d69d 12418 return _("<unknown>");
9abca702 12419 }
2057d69d
CZ
12420}
12421
fd85a6a1
NC
12422static const char *
12423get_solaris_symbol_visibility (unsigned int visibility)
12424{
12425 switch (visibility)
12426 {
12427 case 4: return "EXPORTED";
12428 case 5: return "SINGLETON";
12429 case 6: return "ELIMINATE";
12430 default: return get_symbol_visibility (visibility);
12431 }
12432}
12433
2301ed1c
SN
12434static const char *
12435get_aarch64_symbol_other (unsigned int other)
12436{
12437 static char buf[32];
12438
12439 if (other & STO_AARCH64_VARIANT_PCS)
12440 {
12441 other &= ~STO_AARCH64_VARIANT_PCS;
12442 if (other == 0)
12443 return "VARIANT_PCS";
12444 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
12445 return buf;
12446 }
12447 return NULL;
12448}
12449
5e2b0d47
NC
12450static const char *
12451get_mips_symbol_other (unsigned int other)
12452{
12453 switch (other)
12454 {
32ec8896
NC
12455 case STO_OPTIONAL: return "OPTIONAL";
12456 case STO_MIPS_PLT: return "MIPS PLT";
12457 case STO_MIPS_PIC: return "MIPS PIC";
12458 case STO_MICROMIPS: return "MICROMIPS";
12459 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
12460 case STO_MIPS16: return "MIPS16";
12461 default: return NULL;
5e2b0d47
NC
12462 }
12463}
12464
28f997cf 12465static const char *
dda8d76d 12466get_ia64_symbol_other (Filedata * filedata, unsigned int other)
28f997cf 12467{
dda8d76d 12468 if (is_ia64_vms (filedata))
28f997cf
TG
12469 {
12470 static char res[32];
12471
12472 res[0] = 0;
12473
12474 /* Function types is for images and .STB files only. */
dda8d76d 12475 switch (filedata->file_header.e_type)
28f997cf
TG
12476 {
12477 case ET_DYN:
12478 case ET_EXEC:
12479 switch (VMS_ST_FUNC_TYPE (other))
12480 {
12481 case VMS_SFT_CODE_ADDR:
12482 strcat (res, " CA");
12483 break;
12484 case VMS_SFT_SYMV_IDX:
12485 strcat (res, " VEC");
12486 break;
12487 case VMS_SFT_FD:
12488 strcat (res, " FD");
12489 break;
12490 case VMS_SFT_RESERVE:
12491 strcat (res, " RSV");
12492 break;
12493 default:
bee0ee85
NC
12494 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
12495 VMS_ST_FUNC_TYPE (other));
12496 strcat (res, " <unknown>");
12497 break;
28f997cf
TG
12498 }
12499 break;
12500 default:
12501 break;
12502 }
12503 switch (VMS_ST_LINKAGE (other))
12504 {
12505 case VMS_STL_IGNORE:
12506 strcat (res, " IGN");
12507 break;
12508 case VMS_STL_RESERVE:
12509 strcat (res, " RSV");
12510 break;
12511 case VMS_STL_STD:
12512 strcat (res, " STD");
12513 break;
12514 case VMS_STL_LNK:
12515 strcat (res, " LNK");
12516 break;
12517 default:
bee0ee85
NC
12518 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
12519 VMS_ST_LINKAGE (other));
12520 strcat (res, " <unknown>");
12521 break;
28f997cf
TG
12522 }
12523
12524 if (res[0] != 0)
12525 return res + 1;
12526 else
12527 return res;
12528 }
12529 return NULL;
12530}
12531
6911b7dc
AM
12532static const char *
12533get_ppc64_symbol_other (unsigned int other)
12534{
14732552
AM
12535 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
12536 return NULL;
12537
12538 other >>= STO_PPC64_LOCAL_BIT;
12539 if (other <= 6)
6911b7dc 12540 {
89246a0e 12541 static char buf[64];
14732552
AM
12542 if (other >= 2)
12543 other = ppc64_decode_local_entry (other);
12544 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
6911b7dc
AM
12545 return buf;
12546 }
12547 return NULL;
12548}
12549
5e2b0d47 12550static const char *
dda8d76d 12551get_symbol_other (Filedata * filedata, unsigned int other)
5e2b0d47
NC
12552{
12553 const char * result = NULL;
89246a0e 12554 static char buff [64];
5e2b0d47
NC
12555
12556 if (other == 0)
12557 return "";
12558
dda8d76d 12559 switch (filedata->file_header.e_machine)
5e2b0d47 12560 {
2057d69d
CZ
12561 case EM_ALPHA:
12562 result = get_alpha_symbol_other (other);
12563 break;
2301ed1c
SN
12564 case EM_AARCH64:
12565 result = get_aarch64_symbol_other (other);
12566 break;
5e2b0d47
NC
12567 case EM_MIPS:
12568 result = get_mips_symbol_other (other);
28f997cf
TG
12569 break;
12570 case EM_IA_64:
dda8d76d 12571 result = get_ia64_symbol_other (filedata, other);
28f997cf 12572 break;
6911b7dc
AM
12573 case EM_PPC64:
12574 result = get_ppc64_symbol_other (other);
12575 break;
5e2b0d47 12576 default:
fd85a6a1 12577 result = NULL;
5e2b0d47
NC
12578 break;
12579 }
12580
12581 if (result)
12582 return result;
12583
12584 snprintf (buff, sizeof buff, _("<other>: %x"), other);
12585 return buff;
12586}
12587
d1133906 12588static const char *
dda8d76d 12589get_symbol_index_type (Filedata * filedata, unsigned int type)
252b5132 12590{
b34976b6 12591 static char buff[32];
5cf1065c 12592
252b5132
RH
12593 switch (type)
12594 {
b34976b6
AM
12595 case SHN_UNDEF: return "UND";
12596 case SHN_ABS: return "ABS";
12597 case SHN_COMMON: return "COM";
252b5132 12598 default:
9ce701e2 12599 if (type == SHN_IA_64_ANSI_COMMON
10ca4b04
L
12600 && filedata->file_header.e_machine == EM_IA_64
12601 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
12602 return "ANSI_COM";
12603 else if ((filedata->file_header.e_machine == EM_X86_64
12604 || filedata->file_header.e_machine == EM_L1OM
12605 || filedata->file_header.e_machine == EM_K1OM)
12606 && type == SHN_X86_64_LCOMMON)
12607 return "LARGE_COM";
12608 else if ((type == SHN_MIPS_SCOMMON
12609 && filedata->file_header.e_machine == EM_MIPS)
12610 || (type == SHN_TIC6X_SCOMMON
12611 && filedata->file_header.e_machine == EM_TI_C6000))
12612 return "SCOM";
12613 else if (type == SHN_MIPS_SUNDEFINED
12614 && filedata->file_header.e_machine == EM_MIPS)
12615 return "SUND";
12616 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
12617 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
12618 else if (type >= SHN_LOOS && type <= SHN_HIOS)
12619 sprintf (buff, "OS [0x%04x]", type & 0xffff);
12620 else if (type >= SHN_LORESERVE)
12621 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
12622 else if (filedata->file_header.e_shnum != 0
12623 && type >= filedata->file_header.e_shnum)
12624 sprintf (buff, _("bad section index[%3d]"), type);
12625 else
12626 sprintf (buff, "%3d", type);
12627 break;
fd85a6a1
NC
12628 }
12629
10ca4b04 12630 return buff;
6bd1a22c
L
12631}
12632
bb4d2ac2 12633static const char *
dda8d76d 12634get_symbol_version_string (Filedata * filedata,
015dc7e1 12635 bool is_dynsym,
1449284b
NC
12636 const char * strtab,
12637 unsigned long int strtab_size,
12638 unsigned int si,
12639 Elf_Internal_Sym * psym,
12640 enum versioned_symbol_info * sym_info,
12641 unsigned short * vna_other)
bb4d2ac2 12642{
ab273396
AM
12643 unsigned char data[2];
12644 unsigned short vers_data;
12645 unsigned long offset;
7a815dd5 12646 unsigned short max_vd_ndx;
bb4d2ac2 12647
ab273396 12648 if (!is_dynsym
978c4450 12649 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
ab273396 12650 return NULL;
bb4d2ac2 12651
978c4450
AM
12652 offset = offset_from_vma (filedata,
12653 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
ab273396 12654 sizeof data + si * sizeof (vers_data));
bb4d2ac2 12655
dda8d76d 12656 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
ab273396
AM
12657 sizeof (data), 1, _("version data")) == NULL)
12658 return NULL;
12659
12660 vers_data = byte_get (data, 2);
bb4d2ac2 12661
1f6f5dba 12662 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
ab273396 12663 return NULL;
bb4d2ac2 12664
0b8b7609 12665 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
7a815dd5
L
12666 max_vd_ndx = 0;
12667
ab273396
AM
12668 /* Usually we'd only see verdef for defined symbols, and verneed for
12669 undefined symbols. However, symbols defined by the linker in
12670 .dynbss for variables copied from a shared library in order to
12671 avoid text relocations are defined yet have verneed. We could
12672 use a heuristic to detect the special case, for example, check
12673 for verneed first on symbols defined in SHT_NOBITS sections, but
12674 it is simpler and more reliable to just look for both verdef and
12675 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
bb4d2ac2 12676
ab273396
AM
12677 if (psym->st_shndx != SHN_UNDEF
12678 && vers_data != 0x8001
978c4450 12679 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
ab273396
AM
12680 {
12681 Elf_Internal_Verdef ivd;
12682 Elf_Internal_Verdaux ivda;
12683 Elf_External_Verdaux evda;
12684 unsigned long off;
bb4d2ac2 12685
dda8d76d 12686 off = offset_from_vma (filedata,
978c4450 12687 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
ab273396
AM
12688 sizeof (Elf_External_Verdef));
12689
12690 do
bb4d2ac2 12691 {
ab273396
AM
12692 Elf_External_Verdef evd;
12693
dda8d76d 12694 if (get_data (&evd, filedata, off, sizeof (evd), 1,
ab273396
AM
12695 _("version def")) == NULL)
12696 {
12697 ivd.vd_ndx = 0;
12698 ivd.vd_aux = 0;
12699 ivd.vd_next = 0;
1f6f5dba 12700 ivd.vd_flags = 0;
ab273396
AM
12701 }
12702 else
bb4d2ac2 12703 {
ab273396
AM
12704 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12705 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12706 ivd.vd_next = BYTE_GET (evd.vd_next);
1f6f5dba 12707 ivd.vd_flags = BYTE_GET (evd.vd_flags);
ab273396 12708 }
bb4d2ac2 12709
7a815dd5
L
12710 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
12711 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
12712
ab273396
AM
12713 off += ivd.vd_next;
12714 }
12715 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
bb4d2ac2 12716
ab273396
AM
12717 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
12718 {
9abca702 12719 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
1f6f5dba
L
12720 return NULL;
12721
ab273396
AM
12722 off -= ivd.vd_next;
12723 off += ivd.vd_aux;
bb4d2ac2 12724
dda8d76d 12725 if (get_data (&evda, filedata, off, sizeof (evda), 1,
ab273396
AM
12726 _("version def aux")) != NULL)
12727 {
12728 ivda.vda_name = BYTE_GET (evda.vda_name);
bb4d2ac2 12729
ab273396 12730 if (psym->st_name != ivda.vda_name)
0b8b7609
AM
12731 return (ivda.vda_name < strtab_size
12732 ? strtab + ivda.vda_name : _("<corrupt>"));
ab273396
AM
12733 }
12734 }
12735 }
bb4d2ac2 12736
978c4450 12737 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
ab273396
AM
12738 {
12739 Elf_External_Verneed evn;
12740 Elf_Internal_Verneed ivn;
12741 Elf_Internal_Vernaux ivna;
bb4d2ac2 12742
dda8d76d 12743 offset = offset_from_vma (filedata,
978c4450 12744 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
ab273396
AM
12745 sizeof evn);
12746 do
12747 {
12748 unsigned long vna_off;
bb4d2ac2 12749
dda8d76d 12750 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
ab273396
AM
12751 _("version need")) == NULL)
12752 {
12753 ivna.vna_next = 0;
12754 ivna.vna_other = 0;
12755 ivna.vna_name = 0;
12756 break;
12757 }
bb4d2ac2 12758
ab273396
AM
12759 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12760 ivn.vn_next = BYTE_GET (evn.vn_next);
bb4d2ac2 12761
ab273396 12762 vna_off = offset + ivn.vn_aux;
bb4d2ac2 12763
ab273396
AM
12764 do
12765 {
12766 Elf_External_Vernaux evna;
bb4d2ac2 12767
dda8d76d 12768 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
ab273396 12769 _("version need aux (3)")) == NULL)
bb4d2ac2 12770 {
ab273396
AM
12771 ivna.vna_next = 0;
12772 ivna.vna_other = 0;
12773 ivna.vna_name = 0;
bb4d2ac2 12774 }
bb4d2ac2 12775 else
bb4d2ac2 12776 {
ab273396
AM
12777 ivna.vna_other = BYTE_GET (evna.vna_other);
12778 ivna.vna_next = BYTE_GET (evna.vna_next);
12779 ivna.vna_name = BYTE_GET (evna.vna_name);
12780 }
bb4d2ac2 12781
ab273396
AM
12782 vna_off += ivna.vna_next;
12783 }
12784 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
bb4d2ac2 12785
ab273396
AM
12786 if (ivna.vna_other == vers_data)
12787 break;
bb4d2ac2 12788
ab273396
AM
12789 offset += ivn.vn_next;
12790 }
12791 while (ivn.vn_next != 0);
bb4d2ac2 12792
ab273396
AM
12793 if (ivna.vna_other == vers_data)
12794 {
12795 *sym_info = symbol_undefined;
12796 *vna_other = ivna.vna_other;
12797 return (ivna.vna_name < strtab_size
12798 ? strtab + ivna.vna_name : _("<corrupt>"));
bb4d2ac2 12799 }
7a815dd5
L
12800 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
12801 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
12802 return _("<corrupt>");
bb4d2ac2 12803 }
ab273396 12804 return NULL;
bb4d2ac2
L
12805}
12806
047c3dbf
NL
12807/* Display a symbol size on stdout. Format is based on --sym-base setting. */
12808
12809static unsigned int
12810print_dynamic_symbol_size (bfd_vma vma, int base)
12811{
12812 switch (base)
12813 {
12814 case 8:
12815 return print_vma (vma, OCTAL_5);
12816
12817 case 10:
12818 return print_vma (vma, UNSIGNED_5);
12819
12820 case 16:
12821 return print_vma (vma, PREFIX_HEX_5);
12822
12823 case 0:
12824 default:
12825 return print_vma (vma, DEC_5);
12826 }
12827}
12828
10ca4b04
L
12829static void
12830print_dynamic_symbol (Filedata *filedata, unsigned long si,
12831 Elf_Internal_Sym *symtab,
12832 Elf_Internal_Shdr *section,
12833 char *strtab, size_t strtab_size)
252b5132 12834{
10ca4b04
L
12835 const char *version_string;
12836 enum versioned_symbol_info sym_info;
12837 unsigned short vna_other;
23356397
NC
12838 bool is_valid;
12839 const char * sstr;
10ca4b04 12840 Elf_Internal_Sym *psym = symtab + si;
b9e920ec 12841
10ca4b04
L
12842 printf ("%6ld: ", si);
12843 print_vma (psym->st_value, LONG_HEX);
12844 putchar (' ');
047c3dbf 12845 print_dynamic_symbol_size (psym->st_size, sym_base);
10ca4b04
L
12846 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
12847 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
12848 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
12849 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
12850 else
252b5132 12851 {
10ca4b04 12852 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
252b5132 12853
10ca4b04
L
12854 printf (" %-7s", get_symbol_visibility (vis));
12855 /* Check to see if any other bits in the st_other field are set.
12856 Note - displaying this information disrupts the layout of the
12857 table being generated, but for the moment this case is very rare. */
12858 if (psym->st_other ^ vis)
12859 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
252b5132 12860 }
10ca4b04 12861 printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
0942c7ab 12862
23356397
NC
12863 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
12864 && psym->st_shndx < filedata->file_header.e_shnum
b9af6379 12865 && filedata->section_headers != NULL
23356397
NC
12866 && psym->st_name == 0)
12867 {
84714f86
AM
12868 is_valid
12869 = section_name_valid (filedata,
12870 filedata->section_headers + psym->st_shndx);
23356397 12871 sstr = is_valid ?
84714f86
AM
12872 section_name_print (filedata,
12873 filedata->section_headers + psym->st_shndx)
23356397
NC
12874 : _("<corrupt>");
12875 }
12876 else
12877 {
84714f86 12878 is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
23356397
NC
12879 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
12880 }
10ca4b04
L
12881
12882 version_string
12883 = get_symbol_version_string (filedata,
12884 (section == NULL
12885 || section->sh_type == SHT_DYNSYM),
12886 strtab, strtab_size, si,
12887 psym, &sym_info, &vna_other);
b9e920ec 12888
0942c7ab
NC
12889 int len_avail = 21;
12890 if (! do_wide && version_string != NULL)
12891 {
ddb43bab 12892 char buffer[16];
0942c7ab 12893
ddb43bab 12894 len_avail -= 1 + strlen (version_string);
0942c7ab
NC
12895
12896 if (sym_info == symbol_undefined)
12897 len_avail -= sprintf (buffer," (%d)", vna_other);
12898 else if (sym_info != symbol_hidden)
12899 len_avail -= 1;
12900 }
12901
12902 print_symbol (len_avail, sstr);
b9e920ec 12903
10ca4b04
L
12904 if (version_string)
12905 {
12906 if (sym_info == symbol_undefined)
12907 printf ("@%s (%d)", version_string, vna_other);
f7a99963 12908 else
10ca4b04
L
12909 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
12910 version_string);
12911 }
6bd1a22c 12912
10ca4b04 12913 putchar ('\n');
6bd1a22c 12914
10ca4b04
L
12915 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
12916 && section != NULL
12917 && si >= section->sh_info
12918 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
12919 && filedata->file_header.e_machine != EM_MIPS
12920 /* Solaris binaries have been found to violate this requirement as
12921 well. Not sure if this is a bug or an ABI requirement. */
12922 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
12923 warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
12924 si, printable_section_name (filedata, section), section->sh_info);
12925}
f16a9783 12926
0f03783c
NC
12927static const char *
12928get_lto_kind (unsigned int kind)
12929{
12930 switch (kind)
12931 {
12932 case 0: return "DEF";
12933 case 1: return "WEAKDEF";
12934 case 2: return "UNDEF";
12935 case 3: return "WEAKUNDEF";
12936 case 4: return "COMMON";
12937 default:
12938 break;
12939 }
12940
12941 static char buffer[30];
12942 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
12943 sprintf (buffer, "<unknown: %u>", kind);
12944 return buffer;
12945}
12946
12947static const char *
12948get_lto_visibility (unsigned int visibility)
12949{
12950 switch (visibility)
12951 {
12952 case 0: return "DEFAULT";
12953 case 1: return "PROTECTED";
12954 case 2: return "INTERNAL";
12955 case 3: return "HIDDEN";
12956 default:
12957 break;
12958 }
12959
12960 static char buffer[30];
12961 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
12962 sprintf (buffer, "<unknown: %u>", visibility);
12963 return buffer;
12964}
12965
12966static const char *
12967get_lto_sym_type (unsigned int sym_type)
12968{
12969 switch (sym_type)
12970 {
12971 case 0: return "UNKNOWN";
12972 case 1: return "FUNCTION";
12973 case 2: return "VARIABLE";
12974 default:
12975 break;
12976 }
12977
12978 static char buffer[30];
12979 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
12980 sprintf (buffer, "<unknown: %u>", sym_type);
12981 return buffer;
12982}
12983
12984/* Display an LTO format symbol table.
12985 FIXME: The format of LTO symbol tables is not formalized.
12986 So this code could need changing in the future. */
12987
015dc7e1 12988static bool
0f03783c
NC
12989display_lto_symtab (Filedata * filedata,
12990 Elf_Internal_Shdr * section)
12991{
12992 if (section->sh_size == 0)
12993 {
ca0e11aa
NC
12994 if (filedata->is_separate)
12995 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
12996 printable_section_name (filedata, section),
12997 filedata->file_name);
12998 else
12999 printf (_("\nLTO Symbol table '%s' is empty!\n"),
13000 printable_section_name (filedata, section));
047c3dbf 13001
015dc7e1 13002 return true;
0f03783c
NC
13003 }
13004
13005 if (section->sh_size > filedata->file_size)
13006 {
13007 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
13008 printable_section_name (filedata, section),
13009 (unsigned long) section->sh_size);
015dc7e1 13010 return false;
0f03783c
NC
13011 }
13012
13013 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
13014 section->sh_size, 1, _("LTO symbols"));
13015 if (alloced_data == NULL)
015dc7e1 13016 return false;
0f03783c
NC
13017
13018 /* Look for extended data for the symbol table. */
13019 Elf_Internal_Shdr * ext;
13020 void * ext_data_orig = NULL;
13021 char * ext_data = NULL;
13022 char * ext_data_end = NULL;
13023 char * ext_name = NULL;
13024
13025 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
84714f86
AM
13026 (section_name (filedata, section)
13027 + sizeof (".gnu.lto_.symtab.") - 1)) > 0
0f03783c
NC
13028 && ext_name != NULL /* Paranoia. */
13029 && (ext = find_section (filedata, ext_name)) != NULL)
13030 {
13031 if (ext->sh_size < 3)
13032 error (_("LTO Symbol extension table '%s' is empty!\n"),
13033 printable_section_name (filedata, ext));
13034 else
13035 {
13036 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
13037 ext->sh_size, 1,
13038 _("LTO ext symbol data"));
13039 if (ext_data != NULL)
13040 {
13041 ext_data_end = ext_data + ext->sh_size;
13042 if (* ext_data++ != 1)
13043 error (_("Unexpected version number in symbol extension table\n"));
13044 }
13045 }
13046 }
b9e920ec 13047
0f03783c
NC
13048 const unsigned char * data = (const unsigned char *) alloced_data;
13049 const unsigned char * end = data + section->sh_size;
13050
ca0e11aa
NC
13051 if (filedata->is_separate)
13052 printf (_("\nIn linked file '%s': "), filedata->file_name);
13053 else
13054 printf ("\n");
13055
0f03783c
NC
13056 if (ext_data_orig != NULL)
13057 {
13058 if (do_wide)
ca0e11aa 13059 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
0f03783c
NC
13060 printable_section_name (filedata, section),
13061 printable_section_name (filedata, ext));
13062 else
13063 {
ca0e11aa 13064 printf (_("LTO Symbol table '%s'\n"),
0f03783c
NC
13065 printable_section_name (filedata, section));
13066 printf (_(" and extension table '%s' contain:\n"),
13067 printable_section_name (filedata, ext));
13068 }
13069 }
13070 else
ca0e11aa 13071 printf (_("LTO Symbol table '%s' contains:\n"),
0f03783c 13072 printable_section_name (filedata, section));
b9e920ec 13073
0f03783c 13074 /* FIXME: Add a wide version. */
b9e920ec 13075 if (ext_data_orig != NULL)
0f03783c
NC
13076 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
13077 else
13078 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
13079
13080 /* FIXME: We do not handle style prefixes. */
13081
13082 while (data < end)
13083 {
13084 const unsigned char * sym_name = data;
13085 data += strnlen ((const char *) sym_name, end - data) + 1;
13086 if (data >= end)
13087 goto fail;
13088
13089 const unsigned char * comdat_key = data;
13090 data += strnlen ((const char *) comdat_key, end - data) + 1;
13091 if (data >= end)
13092 goto fail;
13093
13094 if (data + 2 + 8 + 4 > end)
13095 goto fail;
13096
13097 unsigned int kind = *data++;
13098 unsigned int visibility = *data++;
13099
13100 elf_vma size = byte_get (data, 8);
13101 data += 8;
13102
13103 elf_vma slot = byte_get (data, 4);
13104 data += 4;
13105
13106 if (ext_data != NULL)
13107 {
13108 if (ext_data < (ext_data_end - 1))
13109 {
13110 unsigned int sym_type = * ext_data ++;
13111 unsigned int sec_kind = * ext_data ++;
13112
13113 printf (" %10s %10s %11s %08lx %08lx %9s %08lx _",
13114 * comdat_key == 0 ? "-" : (char *) comdat_key,
13115 get_lto_kind (kind),
13116 get_lto_visibility (visibility),
13117 (long) size,
13118 (long) slot,
13119 get_lto_sym_type (sym_type),
13120 (long) sec_kind);
13121 print_symbol (6, (const char *) sym_name);
13122 }
13123 else
13124 {
13125 error (_("Ran out of LTO symbol extension data\n"));
13126 ext_data = NULL;
13127 /* FIXME: return FAIL result ? */
13128 }
13129 }
13130 else
13131 {
13132 printf (" %10s %10s %11s %08lx %08lx _",
13133 * comdat_key == 0 ? "-" : (char *) comdat_key,
13134 get_lto_kind (kind),
13135 get_lto_visibility (visibility),
13136 (long) size,
13137 (long) slot);
13138 print_symbol (21, (const char *) sym_name);
13139 }
13140 putchar ('\n');
13141 }
13142
13143 if (ext_data != NULL && ext_data < ext_data_end)
13144 {
13145 error (_("Data remains in the LTO symbol extension table\n"));
13146 goto fail;
13147 }
13148
13149 free (alloced_data);
13150 free (ext_data_orig);
13151 free (ext_name);
015dc7e1 13152 return true;
b9e920ec 13153
0f03783c
NC
13154 fail:
13155 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
13156 free (alloced_data);
13157 free (ext_data_orig);
13158 free (ext_name);
015dc7e1 13159 return false;
0f03783c
NC
13160}
13161
13162/* Display LTO symbol tables. */
13163
015dc7e1 13164static bool
0f03783c
NC
13165process_lto_symbol_tables (Filedata * filedata)
13166{
13167 Elf_Internal_Shdr * section;
13168 unsigned int i;
015dc7e1 13169 bool res = true;
0f03783c
NC
13170
13171 if (!do_lto_syms)
015dc7e1 13172 return true;
0f03783c
NC
13173
13174 if (filedata->section_headers == NULL)
015dc7e1 13175 return true;
0f03783c
NC
13176
13177 for (i = 0, section = filedata->section_headers;
13178 i < filedata->file_header.e_shnum;
13179 i++, section++)
84714f86
AM
13180 if (section_name_valid (filedata, section)
13181 && startswith (section_name (filedata, section), ".gnu.lto_.symtab."))
0f03783c
NC
13182 res &= display_lto_symtab (filedata, section);
13183
b9e920ec 13184 return res;
0f03783c
NC
13185}
13186
10ca4b04 13187/* Dump the symbol table. */
0f03783c 13188
015dc7e1 13189static bool
10ca4b04
L
13190process_symbol_table (Filedata * filedata)
13191{
13192 Elf_Internal_Shdr * section;
f16a9783 13193
10ca4b04 13194 if (!do_syms && !do_dyn_syms && !do_histogram)
015dc7e1 13195 return true;
6bd1a22c 13196
978c4450 13197 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
6bd1a22c
L
13198 && do_syms
13199 && do_using_dynamic
978c4450
AM
13200 && filedata->dynamic_strings != NULL
13201 && filedata->dynamic_symbols != NULL)
6bd1a22c 13202 {
10ca4b04 13203 unsigned long si;
6bd1a22c 13204
ca0e11aa
NC
13205 if (filedata->is_separate)
13206 {
13207 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table contains %lu entry:\n",
13208 "\nIn linked file '%s' the dynamic symbol table contains %lu entries:\n",
13209 filedata->num_dynamic_syms),
13210 filedata->file_name,
13211 filedata->num_dynamic_syms);
13212 }
13213 else
13214 {
13215 printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
13216 "\nSymbol table for image contains %lu entries:\n",
13217 filedata->num_dynamic_syms),
13218 filedata->num_dynamic_syms);
13219 }
10ca4b04
L
13220 if (is_32bit_elf)
13221 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13222 else
13223 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
6bd1a22c 13224
978c4450
AM
13225 for (si = 0; si < filedata->num_dynamic_syms; si++)
13226 print_dynamic_symbol (filedata, si, filedata->dynamic_symbols, NULL,
13227 filedata->dynamic_strings,
13228 filedata->dynamic_strings_length);
252b5132 13229 }
8b73c356 13230 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
dda8d76d 13231 && filedata->section_headers != NULL)
252b5132 13232 {
b34976b6 13233 unsigned int i;
252b5132 13234
dda8d76d
NC
13235 for (i = 0, section = filedata->section_headers;
13236 i < filedata->file_header.e_shnum;
252b5132
RH
13237 i++, section++)
13238 {
2cf0635d 13239 char * strtab = NULL;
c256ffe7 13240 unsigned long int strtab_size = 0;
2cf0635d 13241 Elf_Internal_Sym * symtab;
ef3df110 13242 unsigned long si, num_syms;
252b5132 13243
2c610e4b
L
13244 if ((section->sh_type != SHT_SYMTAB
13245 && section->sh_type != SHT_DYNSYM)
13246 || (!do_syms
13247 && section->sh_type == SHT_SYMTAB))
252b5132
RH
13248 continue;
13249
dd24e3da
NC
13250 if (section->sh_entsize == 0)
13251 {
13252 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
dda8d76d 13253 printable_section_name (filedata, section));
dd24e3da
NC
13254 continue;
13255 }
13256
d3a49aa8 13257 num_syms = section->sh_size / section->sh_entsize;
ca0e11aa
NC
13258
13259 if (filedata->is_separate)
13260 printf (ngettext ("\nIn linked file '%s' symbol section '%s' contains %lu entry:\n",
13261 "\nIn linked file '%s' symbol section '%s' contains %lu entries:\n",
13262 num_syms),
13263 filedata->file_name,
13264 printable_section_name (filedata, section),
13265 num_syms);
13266 else
13267 printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
13268 "\nSymbol table '%s' contains %lu entries:\n",
13269 num_syms),
13270 printable_section_name (filedata, section),
13271 num_syms);
dd24e3da 13272
f7a99963 13273 if (is_32bit_elf)
ca47b30c 13274 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
f7a99963 13275 else
ca47b30c 13276 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
252b5132 13277
4de91c10 13278 symtab = get_elf_symbols (filedata, section, & num_syms);
252b5132
RH
13279 if (symtab == NULL)
13280 continue;
13281
dda8d76d 13282 if (section->sh_link == filedata->file_header.e_shstrndx)
c256ffe7 13283 {
dda8d76d
NC
13284 strtab = filedata->string_table;
13285 strtab_size = filedata->string_table_length;
c256ffe7 13286 }
dda8d76d 13287 else if (section->sh_link < filedata->file_header.e_shnum)
252b5132 13288 {
2cf0635d 13289 Elf_Internal_Shdr * string_sec;
252b5132 13290
dda8d76d 13291 string_sec = filedata->section_headers + section->sh_link;
252b5132 13292
dda8d76d 13293 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
3f5e193b
NC
13294 1, string_sec->sh_size,
13295 _("string table"));
c256ffe7 13296 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
252b5132
RH
13297 }
13298
10ca4b04
L
13299 for (si = 0; si < num_syms; si++)
13300 print_dynamic_symbol (filedata, si, symtab, section,
13301 strtab, strtab_size);
252b5132
RH
13302
13303 free (symtab);
dda8d76d 13304 if (strtab != filedata->string_table)
252b5132
RH
13305 free (strtab);
13306 }
13307 }
13308 else if (do_syms)
13309 printf
13310 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
13311
978c4450 13312 if (do_histogram && filedata->buckets != NULL)
252b5132 13313 {
2cf0635d
NC
13314 unsigned long * lengths;
13315 unsigned long * counts;
66543521
AM
13316 unsigned long hn;
13317 bfd_vma si;
13318 unsigned long maxlength = 0;
13319 unsigned long nzero_counts = 0;
13320 unsigned long nsyms = 0;
6bd6a03d 13321 char *visited;
252b5132 13322
d3a49aa8
AM
13323 printf (ngettext ("\nHistogram for bucket list length "
13324 "(total of %lu bucket):\n",
13325 "\nHistogram for bucket list length "
13326 "(total of %lu buckets):\n",
978c4450
AM
13327 (unsigned long) filedata->nbuckets),
13328 (unsigned long) filedata->nbuckets);
252b5132 13329
978c4450
AM
13330 lengths = (unsigned long *) calloc (filedata->nbuckets,
13331 sizeof (*lengths));
252b5132
RH
13332 if (lengths == NULL)
13333 {
8b73c356 13334 error (_("Out of memory allocating space for histogram buckets\n"));
fd486f32 13335 goto err_out;
252b5132 13336 }
978c4450
AM
13337 visited = xcmalloc (filedata->nchains, 1);
13338 memset (visited, 0, filedata->nchains);
8b73c356
NC
13339
13340 printf (_(" Length Number %% of total Coverage\n"));
978c4450 13341 for (hn = 0; hn < filedata->nbuckets; ++hn)
252b5132 13342 {
978c4450 13343 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
252b5132 13344 {
b34976b6 13345 ++nsyms;
252b5132 13346 if (maxlength < ++lengths[hn])
b34976b6 13347 ++maxlength;
978c4450 13348 if (si >= filedata->nchains || visited[si])
6bd6a03d
AM
13349 {
13350 error (_("histogram chain is corrupt\n"));
13351 break;
13352 }
13353 visited[si] = 1;
252b5132
RH
13354 }
13355 }
6bd6a03d 13356 free (visited);
252b5132 13357
3f5e193b 13358 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
252b5132
RH
13359 if (counts == NULL)
13360 {
b2e951ec 13361 free (lengths);
8b73c356 13362 error (_("Out of memory allocating space for histogram counts\n"));
fd486f32 13363 goto err_out;
252b5132
RH
13364 }
13365
978c4450 13366 for (hn = 0; hn < filedata->nbuckets; ++hn)
b34976b6 13367 ++counts[lengths[hn]];
252b5132 13368
978c4450 13369 if (filedata->nbuckets > 0)
252b5132 13370 {
66543521
AM
13371 unsigned long i;
13372 printf (" 0 %-10lu (%5.1f%%)\n",
978c4450 13373 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
66543521 13374 for (i = 1; i <= maxlength; ++i)
103f02d3 13375 {
66543521
AM
13376 nzero_counts += counts[i] * i;
13377 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
978c4450 13378 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
103f02d3
UD
13379 (nzero_counts * 100.0) / nsyms);
13380 }
252b5132
RH
13381 }
13382
13383 free (counts);
13384 free (lengths);
13385 }
13386
978c4450
AM
13387 free (filedata->buckets);
13388 filedata->buckets = NULL;
13389 filedata->nbuckets = 0;
13390 free (filedata->chains);
13391 filedata->chains = NULL;
252b5132 13392
978c4450 13393 if (do_histogram && filedata->gnubuckets != NULL)
fdc90cb4 13394 {
2cf0635d
NC
13395 unsigned long * lengths;
13396 unsigned long * counts;
fdc90cb4
JJ
13397 unsigned long hn;
13398 unsigned long maxlength = 0;
13399 unsigned long nzero_counts = 0;
13400 unsigned long nsyms = 0;
fdc90cb4 13401
f16a9783 13402 printf (ngettext ("\nHistogram for `%s' bucket list length "
d3a49aa8 13403 "(total of %lu bucket):\n",
f16a9783 13404 "\nHistogram for `%s' bucket list length "
d3a49aa8 13405 "(total of %lu buckets):\n",
978c4450
AM
13406 (unsigned long) filedata->ngnubuckets),
13407 GNU_HASH_SECTION_NAME (filedata),
13408 (unsigned long) filedata->ngnubuckets);
8b73c356 13409
978c4450
AM
13410 lengths = (unsigned long *) calloc (filedata->ngnubuckets,
13411 sizeof (*lengths));
fdc90cb4
JJ
13412 if (lengths == NULL)
13413 {
8b73c356 13414 error (_("Out of memory allocating space for gnu histogram buckets\n"));
fd486f32 13415 goto err_out;
fdc90cb4
JJ
13416 }
13417
fdc90cb4
JJ
13418 printf (_(" Length Number %% of total Coverage\n"));
13419
978c4450
AM
13420 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13421 if (filedata->gnubuckets[hn] != 0)
fdc90cb4
JJ
13422 {
13423 bfd_vma off, length = 1;
13424
978c4450 13425 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
071436c6 13426 /* PR 17531 file: 010-77222-0.004. */
978c4450
AM
13427 off < filedata->ngnuchains
13428 && (filedata->gnuchains[off] & 1) == 0;
071436c6 13429 ++off)
fdc90cb4
JJ
13430 ++length;
13431 lengths[hn] = length;
13432 if (length > maxlength)
13433 maxlength = length;
13434 nsyms += length;
13435 }
13436
3f5e193b 13437 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
fdc90cb4
JJ
13438 if (counts == NULL)
13439 {
b2e951ec 13440 free (lengths);
8b73c356 13441 error (_("Out of memory allocating space for gnu histogram counts\n"));
fd486f32 13442 goto err_out;
fdc90cb4
JJ
13443 }
13444
978c4450 13445 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
fdc90cb4
JJ
13446 ++counts[lengths[hn]];
13447
978c4450 13448 if (filedata->ngnubuckets > 0)
fdc90cb4
JJ
13449 {
13450 unsigned long j;
13451 printf (" 0 %-10lu (%5.1f%%)\n",
978c4450 13452 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
fdc90cb4
JJ
13453 for (j = 1; j <= maxlength; ++j)
13454 {
13455 nzero_counts += counts[j] * j;
13456 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
978c4450 13457 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
fdc90cb4
JJ
13458 (nzero_counts * 100.0) / nsyms);
13459 }
13460 }
13461
13462 free (counts);
13463 free (lengths);
fdc90cb4 13464 }
978c4450
AM
13465 free (filedata->gnubuckets);
13466 filedata->gnubuckets = NULL;
13467 filedata->ngnubuckets = 0;
13468 free (filedata->gnuchains);
13469 filedata->gnuchains = NULL;
13470 filedata->ngnuchains = 0;
13471 free (filedata->mipsxlat);
13472 filedata->mipsxlat = NULL;
015dc7e1 13473 return true;
fd486f32
AM
13474
13475 err_out:
978c4450
AM
13476 free (filedata->gnubuckets);
13477 filedata->gnubuckets = NULL;
13478 filedata->ngnubuckets = 0;
13479 free (filedata->gnuchains);
13480 filedata->gnuchains = NULL;
13481 filedata->ngnuchains = 0;
13482 free (filedata->mipsxlat);
13483 filedata->mipsxlat = NULL;
13484 free (filedata->buckets);
13485 filedata->buckets = NULL;
13486 filedata->nbuckets = 0;
13487 free (filedata->chains);
13488 filedata->chains = NULL;
015dc7e1 13489 return false;
252b5132
RH
13490}
13491
015dc7e1 13492static bool
ca0e11aa 13493process_syminfo (Filedata * filedata)
252b5132 13494{
b4c96d0d 13495 unsigned int i;
252b5132 13496
978c4450 13497 if (filedata->dynamic_syminfo == NULL
252b5132
RH
13498 || !do_dynamic)
13499 /* No syminfo, this is ok. */
015dc7e1 13500 return true;
252b5132
RH
13501
13502 /* There better should be a dynamic symbol section. */
978c4450 13503 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
015dc7e1 13504 return false;
252b5132 13505
ca0e11aa
NC
13506 if (filedata->is_separate)
13507 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entry:\n",
13508 "\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entries:\n",
13509 filedata->dynamic_syminfo_nent),
13510 filedata->file_name,
13511 filedata->dynamic_syminfo_offset,
13512 filedata->dynamic_syminfo_nent);
13513 else
d3a49aa8
AM
13514 printf (ngettext ("\nDynamic info segment at offset 0x%lx "
13515 "contains %d entry:\n",
13516 "\nDynamic info segment at offset 0x%lx "
13517 "contains %d entries:\n",
978c4450 13518 filedata->dynamic_syminfo_nent),
ca0e11aa
NC
13519 filedata->dynamic_syminfo_offset,
13520 filedata->dynamic_syminfo_nent);
252b5132
RH
13521
13522 printf (_(" Num: Name BoundTo Flags\n"));
978c4450 13523 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
252b5132 13524 {
978c4450 13525 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
252b5132 13526
31104126 13527 printf ("%4d: ", i);
978c4450 13528 if (i >= filedata->num_dynamic_syms)
4082ef84 13529 printf (_("<corrupt index>"));
84714f86
AM
13530 else if (valid_dynamic_name (filedata, filedata->dynamic_symbols[i].st_name))
13531 print_symbol (30, get_dynamic_name (filedata,
978c4450 13532 filedata->dynamic_symbols[i].st_name));
d79b3d50 13533 else
978c4450 13534 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
31104126 13535 putchar (' ');
252b5132 13536
978c4450 13537 switch (filedata->dynamic_syminfo[i].si_boundto)
252b5132
RH
13538 {
13539 case SYMINFO_BT_SELF:
13540 fputs ("SELF ", stdout);
13541 break;
13542 case SYMINFO_BT_PARENT:
13543 fputs ("PARENT ", stdout);
13544 break;
13545 default:
978c4450
AM
13546 if (filedata->dynamic_syminfo[i].si_boundto > 0
13547 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
84714f86 13548 && valid_dynamic_name (filedata,
978c4450 13549 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
31104126 13550 {
84714f86 13551 print_symbol (10, get_dynamic_name (filedata,
978c4450 13552 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
31104126
NC
13553 putchar (' ' );
13554 }
252b5132 13555 else
978c4450 13556 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
252b5132
RH
13557 break;
13558 }
13559
13560 if (flags & SYMINFO_FLG_DIRECT)
13561 printf (" DIRECT");
13562 if (flags & SYMINFO_FLG_PASSTHRU)
13563 printf (" PASSTHRU");
13564 if (flags & SYMINFO_FLG_COPY)
13565 printf (" COPY");
13566 if (flags & SYMINFO_FLG_LAZYLOAD)
13567 printf (" LAZYLOAD");
13568
13569 puts ("");
13570 }
13571
015dc7e1 13572 return true;
252b5132
RH
13573}
13574
75802ccb
CE
13575/* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
13576 is contained by the region START .. END. The types of ADDR, START
13577 and END should all be the same. Note both ADDR + NELEM and END
13578 point to just beyond the end of the regions that are being tested. */
13579#define IN_RANGE(START,END,ADDR,NELEM) \
13580 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
b32e566b 13581
cf13d699
NC
13582/* Check to see if the given reloc needs to be handled in a target specific
13583 manner. If so then process the reloc and return TRUE otherwise return
f84ce13b
NC
13584 FALSE.
13585
13586 If called with reloc == NULL, then this is a signal that reloc processing
13587 for the current section has finished, and any saved state should be
13588 discarded. */
09c11c86 13589
015dc7e1 13590static bool
dda8d76d
NC
13591target_specific_reloc_handling (Filedata * filedata,
13592 Elf_Internal_Rela * reloc,
13593 unsigned char * start,
13594 unsigned char * end,
13595 Elf_Internal_Sym * symtab,
13596 unsigned long num_syms)
252b5132 13597{
f84ce13b
NC
13598 unsigned int reloc_type = 0;
13599 unsigned long sym_index = 0;
13600
13601 if (reloc)
13602 {
dda8d76d 13603 reloc_type = get_reloc_type (filedata, reloc->r_info);
f84ce13b
NC
13604 sym_index = get_reloc_symindex (reloc->r_info);
13605 }
252b5132 13606
dda8d76d 13607 switch (filedata->file_header.e_machine)
252b5132 13608 {
13761a11
NC
13609 case EM_MSP430:
13610 case EM_MSP430_OLD:
13611 {
13612 static Elf_Internal_Sym * saved_sym = NULL;
13613
f84ce13b
NC
13614 if (reloc == NULL)
13615 {
13616 saved_sym = NULL;
015dc7e1 13617 return true;
f84ce13b
NC
13618 }
13619
13761a11
NC
13620 switch (reloc_type)
13621 {
13622 case 10: /* R_MSP430_SYM_DIFF */
7d81bc93 13623 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
dda8d76d 13624 if (uses_msp430x_relocs (filedata))
13761a11 13625 break;
1a0670f3 13626 /* Fall through. */
13761a11 13627 case 21: /* R_MSP430X_SYM_DIFF */
7d81bc93 13628 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
f84ce13b
NC
13629 /* PR 21139. */
13630 if (sym_index >= num_syms)
13631 error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
13632 sym_index);
13633 else
13634 saved_sym = symtab + sym_index;
015dc7e1 13635 return true;
13761a11
NC
13636
13637 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13638 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
13639 goto handle_sym_diff;
0b4362b0 13640
13761a11
NC
13641 case 5: /* R_MSP430_16_BYTE */
13642 case 9: /* R_MSP430_8 */
7d81bc93 13643 case 11: /* R_MSP430_GNU_SET_ULEB128 */
dda8d76d 13644 if (uses_msp430x_relocs (filedata))
13761a11
NC
13645 break;
13646 goto handle_sym_diff;
13647
13648 case 2: /* R_MSP430_ABS16 */
13649 case 15: /* R_MSP430X_ABS16 */
7d81bc93 13650 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
dda8d76d 13651 if (! uses_msp430x_relocs (filedata))
13761a11
NC
13652 break;
13653 goto handle_sym_diff;
0b4362b0 13654
13761a11
NC
13655 handle_sym_diff:
13656 if (saved_sym != NULL)
13657 {
13658 bfd_vma value;
5a805384 13659 unsigned int reloc_size = 0;
7d81bc93
JL
13660 int leb_ret = 0;
13661 switch (reloc_type)
13662 {
13663 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13664 reloc_size = 4;
13665 break;
13666 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13667 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
5a805384 13668 if (reloc->r_offset < (size_t) (end - start))
015dc7e1 13669 read_leb128 (start + reloc->r_offset, end, false,
5a805384 13670 &reloc_size, &leb_ret);
7d81bc93
JL
13671 break;
13672 default:
13673 reloc_size = 2;
13674 break;
13675 }
13761a11 13676
5a805384 13677 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
7d81bc93
JL
13678 error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
13679 "ULEB128 value\n"),
13680 (long) reloc->r_offset);
13681 else if (sym_index >= num_syms)
f84ce13b
NC
13682 error (_("MSP430 reloc contains invalid symbol index %lu\n"),
13683 sym_index);
03f7786e 13684 else
f84ce13b
NC
13685 {
13686 value = reloc->r_addend + (symtab[sym_index].st_value
13687 - saved_sym->st_value);
13688
b32e566b 13689 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
f84ce13b 13690 byte_put (start + reloc->r_offset, value, reloc_size);
b32e566b
NC
13691 else
13692 /* PR 21137 */
13693 error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
13694 (long) reloc->r_offset);
f84ce13b 13695 }
13761a11
NC
13696
13697 saved_sym = NULL;
015dc7e1 13698 return true;
13761a11
NC
13699 }
13700 break;
13701
13702 default:
13703 if (saved_sym != NULL)
071436c6 13704 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
13761a11
NC
13705 break;
13706 }
13707 break;
13708 }
13709
cf13d699
NC
13710 case EM_MN10300:
13711 case EM_CYGNUS_MN10300:
13712 {
13713 static Elf_Internal_Sym * saved_sym = NULL;
252b5132 13714
f84ce13b
NC
13715 if (reloc == NULL)
13716 {
13717 saved_sym = NULL;
015dc7e1 13718 return true;
f84ce13b
NC
13719 }
13720
cf13d699
NC
13721 switch (reloc_type)
13722 {
13723 case 34: /* R_MN10300_ALIGN */
015dc7e1 13724 return true;
cf13d699 13725 case 33: /* R_MN10300_SYM_DIFF */
f84ce13b
NC
13726 if (sym_index >= num_syms)
13727 error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
13728 sym_index);
13729 else
13730 saved_sym = symtab + sym_index;
015dc7e1 13731 return true;
f84ce13b 13732
cf13d699
NC
13733 case 1: /* R_MN10300_32 */
13734 case 2: /* R_MN10300_16 */
13735 if (saved_sym != NULL)
13736 {
03f7786e 13737 int reloc_size = reloc_type == 1 ? 4 : 2;
cf13d699 13738 bfd_vma value;
252b5132 13739
f84ce13b
NC
13740 if (sym_index >= num_syms)
13741 error (_("MN10300 reloc contains invalid symbol index %lu\n"),
13742 sym_index);
03f7786e 13743 else
f84ce13b
NC
13744 {
13745 value = reloc->r_addend + (symtab[sym_index].st_value
13746 - saved_sym->st_value);
13747
b32e566b 13748 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
f84ce13b 13749 byte_put (start + reloc->r_offset, value, reloc_size);
b32e566b
NC
13750 else
13751 error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
13752 (long) reloc->r_offset);
f84ce13b 13753 }
252b5132 13754
cf13d699 13755 saved_sym = NULL;
015dc7e1 13756 return true;
cf13d699
NC
13757 }
13758 break;
13759 default:
13760 if (saved_sym != NULL)
071436c6 13761 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
cf13d699
NC
13762 break;
13763 }
13764 break;
13765 }
6ff71e76
NC
13766
13767 case EM_RL78:
13768 {
13769 static bfd_vma saved_sym1 = 0;
13770 static bfd_vma saved_sym2 = 0;
13771 static bfd_vma value;
13772
f84ce13b
NC
13773 if (reloc == NULL)
13774 {
13775 saved_sym1 = saved_sym2 = 0;
015dc7e1 13776 return true;
f84ce13b
NC
13777 }
13778
6ff71e76
NC
13779 switch (reloc_type)
13780 {
13781 case 0x80: /* R_RL78_SYM. */
13782 saved_sym1 = saved_sym2;
f84ce13b
NC
13783 if (sym_index >= num_syms)
13784 error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
13785 sym_index);
13786 else
13787 {
13788 saved_sym2 = symtab[sym_index].st_value;
13789 saved_sym2 += reloc->r_addend;
13790 }
015dc7e1 13791 return true;
6ff71e76
NC
13792
13793 case 0x83: /* R_RL78_OPsub. */
13794 value = saved_sym1 - saved_sym2;
13795 saved_sym2 = saved_sym1 = 0;
015dc7e1 13796 return true;
6ff71e76
NC
13797 break;
13798
13799 case 0x41: /* R_RL78_ABS32. */
b32e566b 13800 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
03f7786e 13801 byte_put (start + reloc->r_offset, value, 4);
b32e566b
NC
13802 else
13803 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13804 (long) reloc->r_offset);
6ff71e76 13805 value = 0;
015dc7e1 13806 return true;
6ff71e76
NC
13807
13808 case 0x43: /* R_RL78_ABS16. */
b32e566b 13809 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
03f7786e 13810 byte_put (start + reloc->r_offset, value, 2);
b32e566b
NC
13811 else
13812 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13813 (long) reloc->r_offset);
6ff71e76 13814 value = 0;
015dc7e1 13815 return true;
6ff71e76
NC
13816
13817 default:
13818 break;
13819 }
13820 break;
13821 }
252b5132
RH
13822 }
13823
015dc7e1 13824 return false;
252b5132
RH
13825}
13826
aca88567
NC
13827/* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
13828 DWARF debug sections. This is a target specific test. Note - we do not
13829 go through the whole including-target-headers-multiple-times route, (as
13830 we have already done with <elf/h8.h>) because this would become very
13831 messy and even then this function would have to contain target specific
13832 information (the names of the relocs instead of their numeric values).
13833 FIXME: This is not the correct way to solve this problem. The proper way
13834 is to have target specific reloc sizing and typing functions created by
13835 the reloc-macros.h header, in the same way that it already creates the
13836 reloc naming functions. */
13837
015dc7e1 13838static bool
dda8d76d 13839is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
aca88567 13840{
d347c9df 13841 /* Please keep this table alpha-sorted for ease of visual lookup. */
dda8d76d 13842 switch (filedata->file_header.e_machine)
aca88567 13843 {
41e92641 13844 case EM_386:
22abe556 13845 case EM_IAMCU:
41e92641 13846 return reloc_type == 1; /* R_386_32. */
aca88567
NC
13847 case EM_68K:
13848 return reloc_type == 1; /* R_68K_32. */
f954747f
AM
13849 case EM_860:
13850 return reloc_type == 1; /* R_860_32. */
13851 case EM_960:
13852 return reloc_type == 2; /* R_960_32. */
a06ea964 13853 case EM_AARCH64:
9282b95a
JW
13854 return (reloc_type == 258
13855 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
aca4efc7
JM
13856 case EM_BPF:
13857 return reloc_type == 11; /* R_BPF_DATA_32 */
d347c9df
PS
13858 case EM_ADAPTEVA_EPIPHANY:
13859 return reloc_type == 3;
aca88567 13860 case EM_ALPHA:
137b6b5f 13861 return reloc_type == 1; /* R_ALPHA_REFLONG. */
41e92641
NC
13862 case EM_ARC:
13863 return reloc_type == 1; /* R_ARC_32. */
886a2506
NC
13864 case EM_ARC_COMPACT:
13865 case EM_ARC_COMPACT2:
13866 return reloc_type == 4; /* R_ARC_32. */
41e92641
NC
13867 case EM_ARM:
13868 return reloc_type == 2; /* R_ARM_ABS32 */
cb8f3167 13869 case EM_AVR_OLD:
aca88567
NC
13870 case EM_AVR:
13871 return reloc_type == 1;
13872 case EM_BLACKFIN:
13873 return reloc_type == 0x12; /* R_byte4_data. */
13874 case EM_CRIS:
13875 return reloc_type == 3; /* R_CRIS_32. */
13876 case EM_CR16:
13877 return reloc_type == 3; /* R_CR16_NUM32. */
13878 case EM_CRX:
13879 return reloc_type == 15; /* R_CRX_NUM32. */
b8891f8d
AJ
13880 case EM_CSKY:
13881 return reloc_type == 1; /* R_CKCORE_ADDR32. */
aca88567
NC
13882 case EM_CYGNUS_FRV:
13883 return reloc_type == 1;
41e92641
NC
13884 case EM_CYGNUS_D10V:
13885 case EM_D10V:
13886 return reloc_type == 6; /* R_D10V_32. */
aca88567
NC
13887 case EM_CYGNUS_D30V:
13888 case EM_D30V:
13889 return reloc_type == 12; /* R_D30V_32_NORMAL. */
41e92641
NC
13890 case EM_DLX:
13891 return reloc_type == 3; /* R_DLX_RELOC_32. */
aca88567
NC
13892 case EM_CYGNUS_FR30:
13893 case EM_FR30:
13894 return reloc_type == 3; /* R_FR30_32. */
3f8107ab
AM
13895 case EM_FT32:
13896 return reloc_type == 1; /* R_FT32_32. */
aca88567
NC
13897 case EM_H8S:
13898 case EM_H8_300:
13899 case EM_H8_300H:
13900 return reloc_type == 1; /* R_H8_DIR32. */
3730236a 13901 case EM_IA_64:
262cdac7
AM
13902 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
13903 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
13904 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
13905 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
aca88567
NC
13906 case EM_IP2K_OLD:
13907 case EM_IP2K:
13908 return reloc_type == 2; /* R_IP2K_32. */
13909 case EM_IQ2000:
13910 return reloc_type == 2; /* R_IQ2000_32. */
84e94c90
NC
13911 case EM_LATTICEMICO32:
13912 return reloc_type == 3; /* R_LM32_32. */
e9a0721f 13913 case EM_LOONGARCH:
13914 return reloc_type == 1; /* R_LARCH_32. */
ff7eeb89 13915 case EM_M32C_OLD:
aca88567
NC
13916 case EM_M32C:
13917 return reloc_type == 3; /* R_M32C_32. */
13918 case EM_M32R:
13919 return reloc_type == 34; /* R_M32R_32_RELA. */
adec12c1
AM
13920 case EM_68HC11:
13921 case EM_68HC12:
13922 return reloc_type == 6; /* R_M68HC11_32. */
7b4ae824 13923 case EM_S12Z:
2849d19f
JD
13924 return reloc_type == 7 || /* R_S12Z_EXT32 */
13925 reloc_type == 6; /* R_S12Z_CW32. */
aca88567
NC
13926 case EM_MCORE:
13927 return reloc_type == 1; /* R_MCORE_ADDR32. */
13928 case EM_CYGNUS_MEP:
13929 return reloc_type == 4; /* R_MEP_32. */
a3c62988
NC
13930 case EM_METAG:
13931 return reloc_type == 2; /* R_METAG_ADDR32. */
137b6b5f
AM
13932 case EM_MICROBLAZE:
13933 return reloc_type == 1; /* R_MICROBLAZE_32. */
aca88567
NC
13934 case EM_MIPS:
13935 return reloc_type == 2; /* R_MIPS_32. */
13936 case EM_MMIX:
13937 return reloc_type == 4; /* R_MMIX_32. */
13938 case EM_CYGNUS_MN10200:
13939 case EM_MN10200:
13940 return reloc_type == 1; /* R_MN10200_32. */
13941 case EM_CYGNUS_MN10300:
13942 case EM_MN10300:
13943 return reloc_type == 1; /* R_MN10300_32. */
5506d11a
AM
13944 case EM_MOXIE:
13945 return reloc_type == 1; /* R_MOXIE_32. */
aca88567
NC
13946 case EM_MSP430_OLD:
13947 case EM_MSP430:
13761a11 13948 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
aca88567
NC
13949 case EM_MT:
13950 return reloc_type == 2; /* R_MT_32. */
35c08157
KLC
13951 case EM_NDS32:
13952 return reloc_type == 20; /* R_NDS32_RELA. */
3e0873ac 13953 case EM_ALTERA_NIOS2:
36591ba1 13954 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
3e0873ac
NC
13955 case EM_NIOS32:
13956 return reloc_type == 1; /* R_NIOS_32. */
73589c9d
CS
13957 case EM_OR1K:
13958 return reloc_type == 1; /* R_OR1K_32. */
aca88567 13959 case EM_PARISC:
9abca702 13960 return (reloc_type == 1 /* R_PARISC_DIR32. */
0df8ad28 13961 || reloc_type == 2 /* R_PARISC_DIR21L. */
5fda8eca 13962 || reloc_type == 41); /* R_PARISC_SECREL32. */
aca88567
NC
13963 case EM_PJ:
13964 case EM_PJ_OLD:
13965 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
13966 case EM_PPC64:
13967 return reloc_type == 1; /* R_PPC64_ADDR32. */
13968 case EM_PPC:
13969 return reloc_type == 1; /* R_PPC_ADDR32. */
2b100bb5
DD
13970 case EM_TI_PRU:
13971 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
e23eba97
NC
13972 case EM_RISCV:
13973 return reloc_type == 1; /* R_RISCV_32. */
99c513f6
DD
13974 case EM_RL78:
13975 return reloc_type == 1; /* R_RL78_DIR32. */
c7927a3c
NC
13976 case EM_RX:
13977 return reloc_type == 1; /* R_RX_DIR32. */
f954747f
AM
13978 case EM_S370:
13979 return reloc_type == 1; /* R_I370_ADDR31. */
aca88567
NC
13980 case EM_S390_OLD:
13981 case EM_S390:
13982 return reloc_type == 4; /* R_S390_32. */
41e92641
NC
13983 case EM_SCORE:
13984 return reloc_type == 8; /* R_SCORE_ABS32. */
aca88567
NC
13985 case EM_SH:
13986 return reloc_type == 1; /* R_SH_DIR32. */
13987 case EM_SPARC32PLUS:
13988 case EM_SPARCV9:
13989 case EM_SPARC:
13990 return reloc_type == 3 /* R_SPARC_32. */
13991 || reloc_type == 23; /* R_SPARC_UA32. */
a7dd7d05
AM
13992 case EM_SPU:
13993 return reloc_type == 6; /* R_SPU_ADDR32 */
40b36596
JM
13994 case EM_TI_C6000:
13995 return reloc_type == 1; /* R_C6000_ABS32. */
aa137e4d
NC
13996 case EM_TILEGX:
13997 return reloc_type == 2; /* R_TILEGX_32. */
13998 case EM_TILEPRO:
13999 return reloc_type == 1; /* R_TILEPRO_32. */
aca88567
NC
14000 case EM_CYGNUS_V850:
14001 case EM_V850:
14002 return reloc_type == 6; /* R_V850_ABS32. */
708e2187
NC
14003 case EM_V800:
14004 return reloc_type == 0x33; /* R_V810_WORD. */
aca88567
NC
14005 case EM_VAX:
14006 return reloc_type == 1; /* R_VAX_32. */
619ed720
EB
14007 case EM_VISIUM:
14008 return reloc_type == 3; /* R_VISIUM_32. */
f96bd6c2
PC
14009 case EM_WEBASSEMBLY:
14010 return reloc_type == 1; /* R_WASM32_32. */
aca88567 14011 case EM_X86_64:
8a9036a4 14012 case EM_L1OM:
7a9068fe 14013 case EM_K1OM:
aca88567 14014 return reloc_type == 10; /* R_X86_64_32. */
c29aca4a
NC
14015 case EM_XC16X:
14016 case EM_C166:
14017 return reloc_type == 3; /* R_XC16C_ABS_32. */
f6c1a2d5
NC
14018 case EM_XGATE:
14019 return reloc_type == 4; /* R_XGATE_32. */
aca88567
NC
14020 case EM_XSTORMY16:
14021 return reloc_type == 1; /* R_XSTROMY16_32. */
14022 case EM_XTENSA_OLD:
14023 case EM_XTENSA:
14024 return reloc_type == 1; /* R_XTENSA_32. */
6655dba2
SB
14025 case EM_Z80:
14026 return reloc_type == 6; /* R_Z80_32. */
aca88567 14027 default:
bee0ee85
NC
14028 {
14029 static unsigned int prev_warn = 0;
14030
14031 /* Avoid repeating the same warning multiple times. */
dda8d76d 14032 if (prev_warn != filedata->file_header.e_machine)
bee0ee85 14033 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
dda8d76d
NC
14034 filedata->file_header.e_machine);
14035 prev_warn = filedata->file_header.e_machine;
015dc7e1 14036 return false;
bee0ee85 14037 }
aca88567
NC
14038 }
14039}
14040
14041/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14042 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
14043
015dc7e1 14044static bool
dda8d76d 14045is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
aca88567 14046{
dda8d76d 14047 switch (filedata->file_header.e_machine)
d347c9df 14048 /* Please keep this table alpha-sorted for ease of visual lookup. */
aca88567 14049 {
41e92641 14050 case EM_386:
22abe556 14051 case EM_IAMCU:
3e0873ac 14052 return reloc_type == 2; /* R_386_PC32. */
aca88567 14053 case EM_68K:
3e0873ac 14054 return reloc_type == 4; /* R_68K_PC32. */
a06ea964
NC
14055 case EM_AARCH64:
14056 return reloc_type == 261; /* R_AARCH64_PREL32 */
cfb8c092
NC
14057 case EM_ADAPTEVA_EPIPHANY:
14058 return reloc_type == 6;
aca88567
NC
14059 case EM_ALPHA:
14060 return reloc_type == 10; /* R_ALPHA_SREL32. */
726c18e1
CZ
14061 case EM_ARC_COMPACT:
14062 case EM_ARC_COMPACT2:
14063 return reloc_type == 49; /* R_ARC_32_PCREL. */
41e92641 14064 case EM_ARM:
3e0873ac 14065 return reloc_type == 3; /* R_ARM_REL32 */
d347c9df
PS
14066 case EM_AVR_OLD:
14067 case EM_AVR:
14068 return reloc_type == 36; /* R_AVR_32_PCREL. */
137b6b5f
AM
14069 case EM_MICROBLAZE:
14070 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
73589c9d
CS
14071 case EM_OR1K:
14072 return reloc_type == 9; /* R_OR1K_32_PCREL. */
aca88567 14073 case EM_PARISC:
85acf597 14074 return reloc_type == 9; /* R_PARISC_PCREL32. */
aca88567
NC
14075 case EM_PPC:
14076 return reloc_type == 26; /* R_PPC_REL32. */
14077 case EM_PPC64:
3e0873ac 14078 return reloc_type == 26; /* R_PPC64_REL32. */
25cbdcbb
AS
14079 case EM_RISCV:
14080 return reloc_type == 57; /* R_RISCV_32_PCREL. */
aca88567
NC
14081 case EM_S390_OLD:
14082 case EM_S390:
3e0873ac 14083 return reloc_type == 5; /* R_390_PC32. */
aca88567 14084 case EM_SH:
3e0873ac 14085 return reloc_type == 2; /* R_SH_REL32. */
aca88567
NC
14086 case EM_SPARC32PLUS:
14087 case EM_SPARCV9:
14088 case EM_SPARC:
3e0873ac 14089 return reloc_type == 6; /* R_SPARC_DISP32. */
a7dd7d05
AM
14090 case EM_SPU:
14091 return reloc_type == 13; /* R_SPU_REL32. */
aa137e4d
NC
14092 case EM_TILEGX:
14093 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
14094 case EM_TILEPRO:
14095 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
619ed720
EB
14096 case EM_VISIUM:
14097 return reloc_type == 6; /* R_VISIUM_32_PCREL */
aca88567 14098 case EM_X86_64:
8a9036a4 14099 case EM_L1OM:
7a9068fe 14100 case EM_K1OM:
3e0873ac 14101 return reloc_type == 2; /* R_X86_64_PC32. */
2057d69d
CZ
14102 case EM_VAX:
14103 return reloc_type == 4; /* R_VAX_PCREL32. */
2fcb9706
BW
14104 case EM_XTENSA_OLD:
14105 case EM_XTENSA:
14106 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
aca88567
NC
14107 default:
14108 /* Do not abort or issue an error message here. Not all targets use
14109 pc-relative 32-bit relocs in their DWARF debug information and we
14110 have already tested for target coverage in is_32bit_abs_reloc. A
cf13d699
NC
14111 more helpful warning message will be generated by apply_relocations
14112 anyway, so just return. */
015dc7e1 14113 return false;
aca88567
NC
14114 }
14115}
14116
14117/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14118 a 64-bit absolute RELA relocation used in DWARF debug sections. */
14119
015dc7e1 14120static bool
dda8d76d 14121is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
aca88567 14122{
dda8d76d 14123 switch (filedata->file_header.e_machine)
aca88567 14124 {
a06ea964
NC
14125 case EM_AARCH64:
14126 return reloc_type == 257; /* R_AARCH64_ABS64. */
aca88567
NC
14127 case EM_ALPHA:
14128 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
3730236a 14129 case EM_IA_64:
262cdac7
AM
14130 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
14131 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
e9a0721f 14132 case EM_LOONGARCH:
14133 return reloc_type == 2; /* R_LARCH_64 */
3e0873ac
NC
14134 case EM_PARISC:
14135 return reloc_type == 80; /* R_PARISC_DIR64. */
aca88567
NC
14136 case EM_PPC64:
14137 return reloc_type == 38; /* R_PPC64_ADDR64. */
e23eba97
NC
14138 case EM_RISCV:
14139 return reloc_type == 2; /* R_RISCV_64. */
aca88567
NC
14140 case EM_SPARC32PLUS:
14141 case EM_SPARCV9:
14142 case EM_SPARC:
714da62f
NC
14143 return reloc_type == 32 /* R_SPARC_64. */
14144 || reloc_type == 54; /* R_SPARC_UA64. */
aca88567 14145 case EM_X86_64:
8a9036a4 14146 case EM_L1OM:
7a9068fe 14147 case EM_K1OM:
aca88567 14148 return reloc_type == 1; /* R_X86_64_64. */
e819ade1
AS
14149 case EM_S390_OLD:
14150 case EM_S390:
aa137e4d
NC
14151 return reloc_type == 22; /* R_S390_64. */
14152 case EM_TILEGX:
14153 return reloc_type == 1; /* R_TILEGX_64. */
85a82265 14154 case EM_MIPS:
aa137e4d 14155 return reloc_type == 18; /* R_MIPS_64. */
aca88567 14156 default:
015dc7e1 14157 return false;
aca88567
NC
14158 }
14159}
14160
85acf597
RH
14161/* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
14162 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
14163
015dc7e1 14164static bool
dda8d76d 14165is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
85acf597 14166{
dda8d76d 14167 switch (filedata->file_header.e_machine)
85acf597 14168 {
a06ea964
NC
14169 case EM_AARCH64:
14170 return reloc_type == 260; /* R_AARCH64_PREL64. */
85acf597 14171 case EM_ALPHA:
aa137e4d 14172 return reloc_type == 11; /* R_ALPHA_SREL64. */
85acf597 14173 case EM_IA_64:
262cdac7
AM
14174 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
14175 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
85acf597 14176 case EM_PARISC:
aa137e4d 14177 return reloc_type == 72; /* R_PARISC_PCREL64. */
85acf597 14178 case EM_PPC64:
aa137e4d 14179 return reloc_type == 44; /* R_PPC64_REL64. */
85acf597
RH
14180 case EM_SPARC32PLUS:
14181 case EM_SPARCV9:
14182 case EM_SPARC:
aa137e4d 14183 return reloc_type == 46; /* R_SPARC_DISP64. */
85acf597 14184 case EM_X86_64:
8a9036a4 14185 case EM_L1OM:
7a9068fe 14186 case EM_K1OM:
aa137e4d 14187 return reloc_type == 24; /* R_X86_64_PC64. */
85acf597
RH
14188 case EM_S390_OLD:
14189 case EM_S390:
aa137e4d
NC
14190 return reloc_type == 23; /* R_S390_PC64. */
14191 case EM_TILEGX:
14192 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
85acf597 14193 default:
015dc7e1 14194 return false;
85acf597
RH
14195 }
14196}
14197
4dc3c23d
AM
14198/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14199 a 24-bit absolute RELA relocation used in DWARF debug sections. */
14200
015dc7e1 14201static bool
dda8d76d 14202is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
4dc3c23d 14203{
dda8d76d 14204 switch (filedata->file_header.e_machine)
4dc3c23d
AM
14205 {
14206 case EM_CYGNUS_MN10200:
14207 case EM_MN10200:
14208 return reloc_type == 4; /* R_MN10200_24. */
3ee6e4fb
NC
14209 case EM_FT32:
14210 return reloc_type == 5; /* R_FT32_20. */
6655dba2
SB
14211 case EM_Z80:
14212 return reloc_type == 5; /* R_Z80_24. */
4dc3c23d 14213 default:
015dc7e1 14214 return false;
4dc3c23d
AM
14215 }
14216}
14217
aca88567
NC
14218/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14219 a 16-bit absolute RELA relocation used in DWARF debug sections. */
14220
015dc7e1 14221static bool
dda8d76d 14222is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
4b78141a 14223{
d347c9df 14224 /* Please keep this table alpha-sorted for ease of visual lookup. */
dda8d76d 14225 switch (filedata->file_header.e_machine)
4b78141a 14226 {
886a2506
NC
14227 case EM_ARC:
14228 case EM_ARC_COMPACT:
14229 case EM_ARC_COMPACT2:
14230 return reloc_type == 2; /* R_ARC_16. */
d347c9df
PS
14231 case EM_ADAPTEVA_EPIPHANY:
14232 return reloc_type == 5;
aca88567
NC
14233 case EM_AVR_OLD:
14234 case EM_AVR:
14235 return reloc_type == 4; /* R_AVR_16. */
41e92641
NC
14236 case EM_CYGNUS_D10V:
14237 case EM_D10V:
14238 return reloc_type == 3; /* R_D10V_16. */
81b42bca
JB
14239 case EM_FT32:
14240 return reloc_type == 2; /* R_FT32_16. */
4b78141a
NC
14241 case EM_H8S:
14242 case EM_H8_300:
14243 case EM_H8_300H:
aca88567
NC
14244 return reloc_type == R_H8_DIR16;
14245 case EM_IP2K_OLD:
14246 case EM_IP2K:
14247 return reloc_type == 1; /* R_IP2K_16. */
ff7eeb89 14248 case EM_M32C_OLD:
f4236fe4
DD
14249 case EM_M32C:
14250 return reloc_type == 1; /* R_M32C_16 */
d347c9df
PS
14251 case EM_CYGNUS_MN10200:
14252 case EM_MN10200:
14253 return reloc_type == 2; /* R_MN10200_16. */
14254 case EM_CYGNUS_MN10300:
14255 case EM_MN10300:
14256 return reloc_type == 2; /* R_MN10300_16. */
aca88567 14257 case EM_MSP430:
dda8d76d 14258 if (uses_msp430x_relocs (filedata))
13761a11 14259 return reloc_type == 2; /* R_MSP430_ABS16. */
1a0670f3 14260 /* Fall through. */
78c8d46c 14261 case EM_MSP430_OLD:
aca88567 14262 return reloc_type == 5; /* R_MSP430_16_BYTE. */
35c08157
KLC
14263 case EM_NDS32:
14264 return reloc_type == 19; /* R_NDS32_RELA. */
3e0873ac 14265 case EM_ALTERA_NIOS2:
36591ba1 14266 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
3e0873ac
NC
14267 case EM_NIOS32:
14268 return reloc_type == 9; /* R_NIOS_16. */
73589c9d
CS
14269 case EM_OR1K:
14270 return reloc_type == 2; /* R_OR1K_16. */
39e07931
AS
14271 case EM_RISCV:
14272 return reloc_type == 55; /* R_RISCV_SET16. */
2b100bb5
DD
14273 case EM_TI_PRU:
14274 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
40b36596
JM
14275 case EM_TI_C6000:
14276 return reloc_type == 2; /* R_C6000_ABS16. */
d347c9df
PS
14277 case EM_VISIUM:
14278 return reloc_type == 2; /* R_VISIUM_16. */
c29aca4a
NC
14279 case EM_XC16X:
14280 case EM_C166:
14281 return reloc_type == 2; /* R_XC16C_ABS_16. */
f6c1a2d5
NC
14282 case EM_XGATE:
14283 return reloc_type == 3; /* R_XGATE_16. */
6655dba2
SB
14284 case EM_Z80:
14285 return reloc_type == 4; /* R_Z80_16. */
4b78141a 14286 default:
015dc7e1 14287 return false;
4b78141a
NC
14288 }
14289}
14290
39e07931
AS
14291/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14292 a 8-bit absolute RELA relocation used in DWARF debug sections. */
14293
015dc7e1 14294static bool
39e07931
AS
14295is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14296{
14297 switch (filedata->file_header.e_machine)
14298 {
14299 case EM_RISCV:
14300 return reloc_type == 54; /* R_RISCV_SET8. */
6655dba2
SB
14301 case EM_Z80:
14302 return reloc_type == 1; /* R_Z80_8. */
39e07931 14303 default:
015dc7e1 14304 return false;
39e07931
AS
14305 }
14306}
14307
14308/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14309 a 6-bit absolute RELA relocation used in DWARF debug sections. */
14310
015dc7e1 14311static bool
39e07931
AS
14312is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14313{
14314 switch (filedata->file_header.e_machine)
14315 {
14316 case EM_RISCV:
14317 return reloc_type == 53; /* R_RISCV_SET6. */
14318 default:
015dc7e1 14319 return false;
39e07931
AS
14320 }
14321}
14322
03336641
JW
14323/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14324 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
14325
015dc7e1 14326static bool
03336641
JW
14327is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14328{
14329 /* Please keep this table alpha-sorted for ease of visual lookup. */
14330 switch (filedata->file_header.e_machine)
14331 {
14332 case EM_RISCV:
14333 return reloc_type == 35; /* R_RISCV_ADD32. */
14334 default:
015dc7e1 14335 return false;
03336641
JW
14336 }
14337}
14338
14339/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14340 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
14341
015dc7e1 14342static bool
03336641
JW
14343is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14344{
14345 /* Please keep this table alpha-sorted for ease of visual lookup. */
14346 switch (filedata->file_header.e_machine)
14347 {
14348 case EM_RISCV:
14349 return reloc_type == 39; /* R_RISCV_SUB32. */
14350 default:
015dc7e1 14351 return false;
03336641
JW
14352 }
14353}
14354
14355/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14356 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
14357
015dc7e1 14358static bool
03336641
JW
14359is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14360{
14361 /* Please keep this table alpha-sorted for ease of visual lookup. */
14362 switch (filedata->file_header.e_machine)
14363 {
14364 case EM_RISCV:
14365 return reloc_type == 36; /* R_RISCV_ADD64. */
14366 default:
015dc7e1 14367 return false;
03336641
JW
14368 }
14369}
14370
14371/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14372 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
14373
015dc7e1 14374static bool
03336641
JW
14375is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14376{
14377 /* Please keep this table alpha-sorted for ease of visual lookup. */
14378 switch (filedata->file_header.e_machine)
14379 {
14380 case EM_RISCV:
14381 return reloc_type == 40; /* R_RISCV_SUB64. */
14382 default:
015dc7e1 14383 return false;
03336641
JW
14384 }
14385}
14386
14387/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14388 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
14389
015dc7e1 14390static bool
03336641
JW
14391is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14392{
14393 /* Please keep this table alpha-sorted for ease of visual lookup. */
14394 switch (filedata->file_header.e_machine)
14395 {
14396 case EM_RISCV:
14397 return reloc_type == 34; /* R_RISCV_ADD16. */
14398 default:
015dc7e1 14399 return false;
03336641
JW
14400 }
14401}
14402
14403/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14404 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
14405
015dc7e1 14406static bool
03336641
JW
14407is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14408{
14409 /* Please keep this table alpha-sorted for ease of visual lookup. */
14410 switch (filedata->file_header.e_machine)
14411 {
14412 case EM_RISCV:
14413 return reloc_type == 38; /* R_RISCV_SUB16. */
14414 default:
015dc7e1 14415 return false;
03336641
JW
14416 }
14417}
14418
14419/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14420 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
14421
015dc7e1 14422static bool
03336641
JW
14423is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14424{
14425 /* Please keep this table alpha-sorted for ease of visual lookup. */
14426 switch (filedata->file_header.e_machine)
14427 {
14428 case EM_RISCV:
14429 return reloc_type == 33; /* R_RISCV_ADD8. */
14430 default:
015dc7e1 14431 return false;
03336641
JW
14432 }
14433}
14434
14435/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14436 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
14437
015dc7e1 14438static bool
03336641
JW
14439is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14440{
14441 /* Please keep this table alpha-sorted for ease of visual lookup. */
14442 switch (filedata->file_header.e_machine)
14443 {
14444 case EM_RISCV:
14445 return reloc_type == 37; /* R_RISCV_SUB8. */
14446 default:
015dc7e1 14447 return false;
03336641
JW
14448 }
14449}
14450
39e07931
AS
14451/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14452 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
14453
015dc7e1 14454static bool
39e07931
AS
14455is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14456{
14457 switch (filedata->file_header.e_machine)
14458 {
14459 case EM_RISCV:
14460 return reloc_type == 52; /* R_RISCV_SUB6. */
14461 default:
015dc7e1 14462 return false;
39e07931
AS
14463 }
14464}
14465
2a7b2e88
JK
14466/* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
14467 relocation entries (possibly formerly used for SHT_GROUP sections). */
14468
015dc7e1 14469static bool
dda8d76d 14470is_none_reloc (Filedata * filedata, unsigned int reloc_type)
2a7b2e88 14471{
dda8d76d 14472 switch (filedata->file_header.e_machine)
2a7b2e88 14473 {
cb8f3167 14474 case EM_386: /* R_386_NONE. */
d347c9df 14475 case EM_68K: /* R_68K_NONE. */
cfb8c092 14476 case EM_ADAPTEVA_EPIPHANY:
d347c9df
PS
14477 case EM_ALPHA: /* R_ALPHA_NONE. */
14478 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
886a2506 14479 case EM_ARC: /* R_ARC_NONE. */
886a2506 14480 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
d347c9df 14481 case EM_ARC_COMPACT: /* R_ARC_NONE. */
cb8f3167 14482 case EM_ARM: /* R_ARM_NONE. */
d347c9df 14483 case EM_C166: /* R_XC16X_NONE. */
cb8f3167 14484 case EM_CRIS: /* R_CRIS_NONE. */
d347c9df
PS
14485 case EM_FT32: /* R_FT32_NONE. */
14486 case EM_IA_64: /* R_IA64_NONE. */
7a9068fe 14487 case EM_K1OM: /* R_X86_64_NONE. */
d347c9df
PS
14488 case EM_L1OM: /* R_X86_64_NONE. */
14489 case EM_M32R: /* R_M32R_NONE. */
14490 case EM_MIPS: /* R_MIPS_NONE. */
cb8f3167 14491 case EM_MN10300: /* R_MN10300_NONE. */
5506d11a 14492 case EM_MOXIE: /* R_MOXIE_NONE. */
d347c9df
PS
14493 case EM_NIOS32: /* R_NIOS_NONE. */
14494 case EM_OR1K: /* R_OR1K_NONE. */
14495 case EM_PARISC: /* R_PARISC_NONE. */
14496 case EM_PPC64: /* R_PPC64_NONE. */
14497 case EM_PPC: /* R_PPC_NONE. */
e23eba97 14498 case EM_RISCV: /* R_RISCV_NONE. */
d347c9df
PS
14499 case EM_S390: /* R_390_NONE. */
14500 case EM_S390_OLD:
14501 case EM_SH: /* R_SH_NONE. */
14502 case EM_SPARC32PLUS:
14503 case EM_SPARC: /* R_SPARC_NONE. */
14504 case EM_SPARCV9:
aa137e4d
NC
14505 case EM_TILEGX: /* R_TILEGX_NONE. */
14506 case EM_TILEPRO: /* R_TILEPRO_NONE. */
d347c9df
PS
14507 case EM_TI_C6000:/* R_C6000_NONE. */
14508 case EM_X86_64: /* R_X86_64_NONE. */
c29aca4a 14509 case EM_XC16X:
6655dba2 14510 case EM_Z80: /* R_Z80_NONE. */
f96bd6c2 14511 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
cb8f3167 14512 return reloc_type == 0;
d347c9df 14513
a06ea964
NC
14514 case EM_AARCH64:
14515 return reloc_type == 0 || reloc_type == 256;
d347c9df
PS
14516 case EM_AVR_OLD:
14517 case EM_AVR:
14518 return (reloc_type == 0 /* R_AVR_NONE. */
14519 || reloc_type == 30 /* R_AVR_DIFF8. */
14520 || reloc_type == 31 /* R_AVR_DIFF16. */
14521 || reloc_type == 32 /* R_AVR_DIFF32. */);
14522 case EM_METAG:
14523 return reloc_type == 3; /* R_METAG_NONE. */
35c08157
KLC
14524 case EM_NDS32:
14525 return (reloc_type == 0 /* R_XTENSA_NONE. */
14526 || reloc_type == 204 /* R_NDS32_DIFF8. */
14527 || reloc_type == 205 /* R_NDS32_DIFF16. */
14528 || reloc_type == 206 /* R_NDS32_DIFF32. */
14529 || reloc_type == 207 /* R_NDS32_ULEB128. */);
2b100bb5
DD
14530 case EM_TI_PRU:
14531 return (reloc_type == 0 /* R_PRU_NONE. */
14532 || reloc_type == 65 /* R_PRU_DIFF8. */
14533 || reloc_type == 66 /* R_PRU_DIFF16. */
14534 || reloc_type == 67 /* R_PRU_DIFF32. */);
58332dda
JK
14535 case EM_XTENSA_OLD:
14536 case EM_XTENSA:
4dc3c23d
AM
14537 return (reloc_type == 0 /* R_XTENSA_NONE. */
14538 || reloc_type == 17 /* R_XTENSA_DIFF8. */
14539 || reloc_type == 18 /* R_XTENSA_DIFF16. */
30ce8e47
MF
14540 || reloc_type == 19 /* R_XTENSA_DIFF32. */
14541 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
14542 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
14543 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
14544 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
14545 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
14546 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
2a7b2e88 14547 }
015dc7e1 14548 return false;
2a7b2e88
JK
14549}
14550
d1c4b12b
NC
14551/* Returns TRUE if there is a relocation against
14552 section NAME at OFFSET bytes. */
14553
015dc7e1 14554bool
d1c4b12b
NC
14555reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
14556{
14557 Elf_Internal_Rela * relocs;
14558 Elf_Internal_Rela * rp;
14559
14560 if (dsec == NULL || dsec->reloc_info == NULL)
015dc7e1 14561 return false;
d1c4b12b
NC
14562
14563 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
14564
14565 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
14566 if (rp->r_offset == offset)
015dc7e1 14567 return true;
d1c4b12b 14568
015dc7e1 14569 return false;
d1c4b12b
NC
14570}
14571
cf13d699 14572/* Apply relocations to a section.
32ec8896
NC
14573 Returns TRUE upon success, FALSE otherwise.
14574 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
14575 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
14576 will be set to the number of relocs loaded.
14577
cf13d699 14578 Note: So far support has been added only for those relocations
32ec8896
NC
14579 which can be found in debug sections. FIXME: Add support for
14580 more relocations ? */
1b315056 14581
015dc7e1 14582static bool
dda8d76d 14583apply_relocations (Filedata * filedata,
d1c4b12b
NC
14584 const Elf_Internal_Shdr * section,
14585 unsigned char * start,
14586 bfd_size_type size,
1449284b 14587 void ** relocs_return,
d1c4b12b 14588 unsigned long * num_relocs_return)
1b315056 14589{
cf13d699 14590 Elf_Internal_Shdr * relsec;
0d2a7a93 14591 unsigned char * end = start + size;
cb8f3167 14592
d1c4b12b
NC
14593 if (relocs_return != NULL)
14594 {
14595 * (Elf_Internal_Rela **) relocs_return = NULL;
14596 * num_relocs_return = 0;
14597 }
14598
dda8d76d 14599 if (filedata->file_header.e_type != ET_REL)
32ec8896 14600 /* No relocs to apply. */
015dc7e1 14601 return true;
1b315056 14602
cf13d699 14603 /* Find the reloc section associated with the section. */
dda8d76d
NC
14604 for (relsec = filedata->section_headers;
14605 relsec < filedata->section_headers + filedata->file_header.e_shnum;
5b18a4bc 14606 ++relsec)
252b5132 14607 {
015dc7e1 14608 bool is_rela;
41e92641 14609 unsigned long num_relocs;
2cf0635d
NC
14610 Elf_Internal_Rela * relocs;
14611 Elf_Internal_Rela * rp;
14612 Elf_Internal_Shdr * symsec;
14613 Elf_Internal_Sym * symtab;
ba5cdace 14614 unsigned long num_syms;
2cf0635d 14615 Elf_Internal_Sym * sym;
252b5132 14616
41e92641 14617 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
dda8d76d
NC
14618 || relsec->sh_info >= filedata->file_header.e_shnum
14619 || filedata->section_headers + relsec->sh_info != section
c256ffe7 14620 || relsec->sh_size == 0
dda8d76d 14621 || relsec->sh_link >= filedata->file_header.e_shnum)
5b18a4bc 14622 continue;
428409d5 14623
a788aedd
AM
14624 symsec = filedata->section_headers + relsec->sh_link;
14625 if (symsec->sh_type != SHT_SYMTAB
14626 && symsec->sh_type != SHT_DYNSYM)
015dc7e1 14627 return false;
a788aedd 14628
41e92641
NC
14629 is_rela = relsec->sh_type == SHT_RELA;
14630
14631 if (is_rela)
14632 {
dda8d76d 14633 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
3f5e193b 14634 relsec->sh_size, & relocs, & num_relocs))
015dc7e1 14635 return false;
41e92641
NC
14636 }
14637 else
14638 {
dda8d76d 14639 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
3f5e193b 14640 relsec->sh_size, & relocs, & num_relocs))
015dc7e1 14641 return false;
41e92641
NC
14642 }
14643
14644 /* SH uses RELA but uses in place value instead of the addend field. */
dda8d76d 14645 if (filedata->file_header.e_machine == EM_SH)
015dc7e1 14646 is_rela = false;
428409d5 14647
4de91c10 14648 symtab = get_elf_symbols (filedata, symsec, & num_syms);
103f02d3 14649
41e92641 14650 for (rp = relocs; rp < relocs + num_relocs; ++rp)
252b5132 14651 {
015dc7e1
AM
14652 bfd_vma addend;
14653 unsigned int reloc_type;
14654 unsigned int reloc_size;
14655 bool reloc_inplace = false;
14656 bool reloc_subtract = false;
14657 unsigned char *rloc;
14658 unsigned long sym_index;
4b78141a 14659
dda8d76d 14660 reloc_type = get_reloc_type (filedata, rp->r_info);
41e92641 14661
dda8d76d 14662 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
2a7b2e88 14663 continue;
dda8d76d 14664 else if (is_none_reloc (filedata, reloc_type))
98fb390a 14665 continue;
dda8d76d
NC
14666 else if (is_32bit_abs_reloc (filedata, reloc_type)
14667 || is_32bit_pcrel_reloc (filedata, reloc_type))
aca88567 14668 reloc_size = 4;
dda8d76d
NC
14669 else if (is_64bit_abs_reloc (filedata, reloc_type)
14670 || is_64bit_pcrel_reloc (filedata, reloc_type))
aca88567 14671 reloc_size = 8;
dda8d76d 14672 else if (is_24bit_abs_reloc (filedata, reloc_type))
4dc3c23d 14673 reloc_size = 3;
dda8d76d 14674 else if (is_16bit_abs_reloc (filedata, reloc_type))
aca88567 14675 reloc_size = 2;
39e07931
AS
14676 else if (is_8bit_abs_reloc (filedata, reloc_type)
14677 || is_6bit_abs_reloc (filedata, reloc_type))
14678 reloc_size = 1;
03336641
JW
14679 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
14680 reloc_type))
14681 || is_32bit_inplace_add_reloc (filedata, reloc_type))
14682 {
14683 reloc_size = 4;
015dc7e1 14684 reloc_inplace = true;
03336641
JW
14685 }
14686 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
14687 reloc_type))
14688 || is_64bit_inplace_add_reloc (filedata, reloc_type))
14689 {
14690 reloc_size = 8;
015dc7e1 14691 reloc_inplace = true;
03336641
JW
14692 }
14693 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
14694 reloc_type))
14695 || is_16bit_inplace_add_reloc (filedata, reloc_type))
14696 {
14697 reloc_size = 2;
015dc7e1 14698 reloc_inplace = true;
03336641
JW
14699 }
14700 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
14701 reloc_type))
14702 || is_8bit_inplace_add_reloc (filedata, reloc_type))
14703 {
14704 reloc_size = 1;
015dc7e1 14705 reloc_inplace = true;
03336641 14706 }
39e07931
AS
14707 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
14708 reloc_type)))
14709 {
14710 reloc_size = 1;
015dc7e1 14711 reloc_inplace = true;
39e07931 14712 }
aca88567 14713 else
4b78141a 14714 {
bee0ee85 14715 static unsigned int prev_reloc = 0;
dda8d76d 14716
bee0ee85
NC
14717 if (reloc_type != prev_reloc)
14718 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
dda8d76d 14719 reloc_type, printable_section_name (filedata, section));
bee0ee85 14720 prev_reloc = reloc_type;
4b78141a
NC
14721 continue;
14722 }
103f02d3 14723
91d6fa6a 14724 rloc = start + rp->r_offset;
75802ccb 14725 if (!IN_RANGE (start, end, rloc, reloc_size))
700dd8b7
L
14726 {
14727 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
14728 (unsigned long) rp->r_offset,
dda8d76d 14729 printable_section_name (filedata, section));
700dd8b7
L
14730 continue;
14731 }
103f02d3 14732
ba5cdace
NC
14733 sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
14734 if (sym_index >= num_syms)
14735 {
14736 warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
dda8d76d 14737 sym_index, printable_section_name (filedata, section));
ba5cdace
NC
14738 continue;
14739 }
14740 sym = symtab + sym_index;
41e92641
NC
14741
14742 /* If the reloc has a symbol associated with it,
55f25fc3
L
14743 make sure that it is of an appropriate type.
14744
14745 Relocations against symbols without type can happen.
14746 Gcc -feliminate-dwarf2-dups may generate symbols
14747 without type for debug info.
14748
14749 Icc generates relocations against function symbols
14750 instead of local labels.
14751
14752 Relocations against object symbols can happen, eg when
14753 referencing a global array. For an example of this see
14754 the _clz.o binary in libgcc.a. */
aca88567 14755 if (sym != symtab
b8871f35 14756 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
55f25fc3 14757 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
5b18a4bc 14758 {
d3a49aa8 14759 warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
dda8d76d
NC
14760 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
14761 printable_section_name (filedata, relsec),
d3a49aa8 14762 (long int)(rp - relocs));
aca88567 14763 continue;
5b18a4bc 14764 }
252b5132 14765
4dc3c23d
AM
14766 addend = 0;
14767 if (is_rela)
14768 addend += rp->r_addend;
c47320c3
AM
14769 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
14770 partial_inplace. */
4dc3c23d 14771 if (!is_rela
dda8d76d 14772 || (filedata->file_header.e_machine == EM_XTENSA
4dc3c23d 14773 && reloc_type == 1)
dda8d76d
NC
14774 || ((filedata->file_header.e_machine == EM_PJ
14775 || filedata->file_header.e_machine == EM_PJ_OLD)
c47320c3 14776 && reloc_type == 1)
dda8d76d
NC
14777 || ((filedata->file_header.e_machine == EM_D30V
14778 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
03336641
JW
14779 && reloc_type == 12)
14780 || reloc_inplace)
39e07931
AS
14781 {
14782 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
14783 addend += byte_get (rloc, reloc_size) & 0x3f;
14784 else
14785 addend += byte_get (rloc, reloc_size);
14786 }
cb8f3167 14787
dda8d76d
NC
14788 if (is_32bit_pcrel_reloc (filedata, reloc_type)
14789 || is_64bit_pcrel_reloc (filedata, reloc_type))
85acf597
RH
14790 {
14791 /* On HPPA, all pc-relative relocations are biased by 8. */
dda8d76d 14792 if (filedata->file_header.e_machine == EM_PARISC)
85acf597 14793 addend -= 8;
91d6fa6a 14794 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
85acf597
RH
14795 reloc_size);
14796 }
39e07931
AS
14797 else if (is_6bit_abs_reloc (filedata, reloc_type)
14798 || is_6bit_inplace_sub_reloc (filedata, reloc_type))
14799 {
14800 if (reloc_subtract)
14801 addend -= sym->st_value;
14802 else
14803 addend += sym->st_value;
14804 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
14805 byte_put (rloc, addend, reloc_size);
14806 }
03336641
JW
14807 else if (reloc_subtract)
14808 byte_put (rloc, addend - sym->st_value, reloc_size);
41e92641 14809 else
91d6fa6a 14810 byte_put (rloc, addend + sym->st_value, reloc_size);
5b18a4bc 14811 }
252b5132 14812
5b18a4bc 14813 free (symtab);
f84ce13b
NC
14814 /* Let the target specific reloc processing code know that
14815 we have finished with these relocs. */
dda8d76d 14816 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
d1c4b12b
NC
14817
14818 if (relocs_return)
14819 {
14820 * (Elf_Internal_Rela **) relocs_return = relocs;
14821 * num_relocs_return = num_relocs;
14822 }
14823 else
14824 free (relocs);
14825
5b18a4bc
NC
14826 break;
14827 }
32ec8896 14828
015dc7e1 14829 return true;
5b18a4bc 14830}
103f02d3 14831
cf13d699 14832#ifdef SUPPORT_DISASSEMBLY
015dc7e1 14833static bool
dda8d76d 14834disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
cf13d699 14835{
dda8d76d 14836 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
cf13d699 14837
74e1a04b 14838 /* FIXME: XXX -- to be done --- XXX */
cf13d699 14839
015dc7e1 14840 return true;
cf13d699
NC
14841}
14842#endif
14843
14844/* Reads in the contents of SECTION from FILE, returning a pointer
14845 to a malloc'ed buffer or NULL if something went wrong. */
14846
14847static char *
dda8d76d 14848get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
cf13d699 14849{
dda8d76d 14850 bfd_size_type num_bytes = section->sh_size;
cf13d699
NC
14851
14852 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
14853 {
c6b78c96 14854 printf (_("Section '%s' has no data to dump.\n"),
dda8d76d 14855 printable_section_name (filedata, section));
cf13d699
NC
14856 return NULL;
14857 }
14858
dda8d76d 14859 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
3f5e193b 14860 _("section contents"));
cf13d699
NC
14861}
14862
0e602686
NC
14863/* Uncompresses a section that was compressed using zlib, in place. */
14864
015dc7e1 14865static bool
dda8d76d
NC
14866uncompress_section_contents (unsigned char ** buffer,
14867 dwarf_size_type uncompressed_size,
14868 dwarf_size_type * size)
0e602686
NC
14869{
14870 dwarf_size_type compressed_size = *size;
14871 unsigned char * compressed_buffer = *buffer;
14872 unsigned char * uncompressed_buffer;
14873 z_stream strm;
14874 int rc;
14875
14876 /* It is possible the section consists of several compressed
14877 buffers concatenated together, so we uncompress in a loop. */
14878 /* PR 18313: The state field in the z_stream structure is supposed
14879 to be invisible to the user (ie us), but some compilers will
14880 still complain about it being used without initialisation. So
14881 we first zero the entire z_stream structure and then set the fields
14882 that we need. */
14883 memset (& strm, 0, sizeof strm);
14884 strm.avail_in = compressed_size;
14885 strm.next_in = (Bytef *) compressed_buffer;
14886 strm.avail_out = uncompressed_size;
14887 uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
14888
14889 rc = inflateInit (& strm);
14890 while (strm.avail_in > 0)
14891 {
14892 if (rc != Z_OK)
3624a6c1 14893 break;
0e602686
NC
14894 strm.next_out = ((Bytef *) uncompressed_buffer
14895 + (uncompressed_size - strm.avail_out));
14896 rc = inflate (&strm, Z_FINISH);
14897 if (rc != Z_STREAM_END)
3624a6c1 14898 break;
0e602686
NC
14899 rc = inflateReset (& strm);
14900 }
ad92f33d
AM
14901 if (inflateEnd (& strm) != Z_OK
14902 || rc != Z_OK
0e602686
NC
14903 || strm.avail_out != 0)
14904 goto fail;
14905
14906 *buffer = uncompressed_buffer;
14907 *size = uncompressed_size;
015dc7e1 14908 return true;
0e602686
NC
14909
14910 fail:
14911 free (uncompressed_buffer);
14912 /* Indicate decompression failure. */
14913 *buffer = NULL;
015dc7e1 14914 return false;
0e602686 14915}
dd24e3da 14916
015dc7e1 14917static bool
dda8d76d 14918dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
cf13d699 14919{
015dc7e1
AM
14920 Elf_Internal_Shdr *relsec;
14921 bfd_size_type num_bytes;
14922 unsigned char *data;
14923 unsigned char *end;
14924 unsigned char *real_start;
14925 unsigned char *start;
14926 bool some_strings_shown;
cf13d699 14927
dda8d76d 14928 real_start = start = (unsigned char *) get_section_contents (section, filedata);
cf13d699 14929 if (start == NULL)
c6b78c96 14930 /* PR 21820: Do not fail if the section was empty. */
63b4cc53 14931 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
c6b78c96 14932
0e602686 14933 num_bytes = section->sh_size;
cf13d699 14934
835f2fae
NC
14935 if (filedata->is_separate)
14936 printf (_("\nString dump of section '%s' in linked file %s:\n"),
14937 printable_section_name (filedata, section),
14938 filedata->file_name);
14939 else
14940 printf (_("\nString dump of section '%s':\n"),
14941 printable_section_name (filedata, section));
cf13d699 14942
0e602686
NC
14943 if (decompress_dumps)
14944 {
14945 dwarf_size_type new_size = num_bytes;
14946 dwarf_size_type uncompressed_size = 0;
14947
14948 if ((section->sh_flags & SHF_COMPRESSED) != 0)
14949 {
14950 Elf_Internal_Chdr chdr;
14951 unsigned int compression_header_size
ebdf1ebf
NC
14952 = get_compression_header (& chdr, (unsigned char *) start,
14953 num_bytes);
5844b465
NC
14954 if (compression_header_size == 0)
14955 /* An error message will have already been generated
14956 by get_compression_header. */
14957 goto error_out;
0e602686 14958
813dabb9 14959 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
0e602686 14960 {
813dabb9 14961 warn (_("section '%s' has unsupported compress type: %d\n"),
dda8d76d 14962 printable_section_name (filedata, section), chdr.ch_type);
f761cb13 14963 goto error_out;
813dabb9 14964 }
813dabb9
L
14965 uncompressed_size = chdr.ch_size;
14966 start += compression_header_size;
14967 new_size -= compression_header_size;
0e602686
NC
14968 }
14969 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
14970 {
14971 /* Read the zlib header. In this case, it should be "ZLIB"
14972 followed by the uncompressed section size, 8 bytes in
14973 big-endian order. */
14974 uncompressed_size = start[4]; uncompressed_size <<= 8;
14975 uncompressed_size += start[5]; uncompressed_size <<= 8;
14976 uncompressed_size += start[6]; uncompressed_size <<= 8;
14977 uncompressed_size += start[7]; uncompressed_size <<= 8;
14978 uncompressed_size += start[8]; uncompressed_size <<= 8;
14979 uncompressed_size += start[9]; uncompressed_size <<= 8;
14980 uncompressed_size += start[10]; uncompressed_size <<= 8;
14981 uncompressed_size += start[11];
14982 start += 12;
14983 new_size -= 12;
14984 }
14985
1835f746
NC
14986 if (uncompressed_size)
14987 {
14988 if (uncompress_section_contents (& start,
14989 uncompressed_size, & new_size))
14990 num_bytes = new_size;
14991 else
14992 {
14993 error (_("Unable to decompress section %s\n"),
dda8d76d 14994 printable_section_name (filedata, section));
f761cb13 14995 goto error_out;
1835f746
NC
14996 }
14997 }
bc303e5d
NC
14998 else
14999 start = real_start;
0e602686 15000 }
fd8008d8 15001
cf13d699
NC
15002 /* If the section being dumped has relocations against it the user might
15003 be expecting these relocations to have been applied. Check for this
15004 case and issue a warning message in order to avoid confusion.
15005 FIXME: Maybe we ought to have an option that dumps a section with
15006 relocs applied ? */
dda8d76d
NC
15007 for (relsec = filedata->section_headers;
15008 relsec < filedata->section_headers + filedata->file_header.e_shnum;
cf13d699
NC
15009 ++relsec)
15010 {
15011 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
dda8d76d
NC
15012 || relsec->sh_info >= filedata->file_header.e_shnum
15013 || filedata->section_headers + relsec->sh_info != section
cf13d699 15014 || relsec->sh_size == 0
dda8d76d 15015 || relsec->sh_link >= filedata->file_header.e_shnum)
cf13d699
NC
15016 continue;
15017
15018 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15019 break;
15020 }
15021
cf13d699
NC
15022 data = start;
15023 end = start + num_bytes;
015dc7e1 15024 some_strings_shown = false;
cf13d699 15025
ba3265d0
NC
15026#ifdef HAVE_MBSTATE_T
15027 mbstate_t state;
15028 /* Initialise the multibyte conversion state. */
15029 memset (& state, 0, sizeof (state));
15030#endif
15031
015dc7e1 15032 bool continuing = false;
ba3265d0 15033
cf13d699
NC
15034 while (data < end)
15035 {
15036 while (!ISPRINT (* data))
15037 if (++ data >= end)
15038 break;
15039
15040 if (data < end)
15041 {
071436c6
NC
15042 size_t maxlen = end - data;
15043
ba3265d0
NC
15044 if (continuing)
15045 {
15046 printf (" ");
015dc7e1 15047 continuing = false;
ba3265d0
NC
15048 }
15049 else
15050 {
d1ce973e 15051 printf (" [%6lx] ", (unsigned long) (data - start));
ba3265d0
NC
15052 }
15053
4082ef84
NC
15054 if (maxlen > 0)
15055 {
f3da8a96 15056 char c = 0;
ba3265d0
NC
15057
15058 while (maxlen)
15059 {
15060 c = *data++;
15061
15062 if (c == 0)
15063 break;
15064
15065 /* PR 25543: Treat new-lines as string-ending characters. */
15066 if (c == '\n')
15067 {
15068 printf ("\\n\n");
15069 if (*data != 0)
015dc7e1 15070 continuing = true;
ba3265d0
NC
15071 break;
15072 }
15073
15074 /* Do not print control characters directly as they can affect terminal
15075 settings. Such characters usually appear in the names generated
15076 by the assembler for local labels. */
15077 if (ISCNTRL (c))
15078 {
15079 printf ("^%c", c + 0x40);
15080 }
15081 else if (ISPRINT (c))
15082 {
15083 putchar (c);
15084 }
15085 else
15086 {
15087 size_t n;
15088#ifdef HAVE_MBSTATE_T
15089 wchar_t w;
15090#endif
15091 /* Let printf do the hard work of displaying multibyte characters. */
15092 printf ("%.1s", data - 1);
15093#ifdef HAVE_MBSTATE_T
15094 /* Try to find out how many bytes made up the character that was
15095 just printed. Advance the symbol pointer past the bytes that
15096 were displayed. */
15097 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
15098#else
15099 n = 1;
15100#endif
15101 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
15102 data += (n - 1);
15103 }
15104 }
15105
15106 if (c != '\n')
15107 putchar ('\n');
4082ef84
NC
15108 }
15109 else
15110 {
15111 printf (_("<corrupt>\n"));
15112 data = end;
15113 }
015dc7e1 15114 some_strings_shown = true;
cf13d699
NC
15115 }
15116 }
15117
15118 if (! some_strings_shown)
15119 printf (_(" No strings found in this section."));
15120
0e602686 15121 free (real_start);
cf13d699
NC
15122
15123 putchar ('\n');
015dc7e1 15124 return true;
f761cb13
AM
15125
15126error_out:
15127 free (real_start);
015dc7e1 15128 return false;
cf13d699
NC
15129}
15130
015dc7e1
AM
15131static bool
15132dump_section_as_bytes (Elf_Internal_Shdr *section,
15133 Filedata *filedata,
15134 bool relocate)
cf13d699
NC
15135{
15136 Elf_Internal_Shdr * relsec;
0e602686
NC
15137 bfd_size_type bytes;
15138 bfd_size_type section_size;
15139 bfd_vma addr;
15140 unsigned char * data;
15141 unsigned char * real_start;
15142 unsigned char * start;
15143
dda8d76d 15144 real_start = start = (unsigned char *) get_section_contents (section, filedata);
cf13d699 15145 if (start == NULL)
c6b78c96 15146 /* PR 21820: Do not fail if the section was empty. */
63b4cc53 15147 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
32ec8896 15148
0e602686 15149 section_size = section->sh_size;
cf13d699 15150
835f2fae
NC
15151 if (filedata->is_separate)
15152 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
15153 printable_section_name (filedata, section),
15154 filedata->file_name);
15155 else
15156 printf (_("\nHex dump of section '%s':\n"),
15157 printable_section_name (filedata, section));
cf13d699 15158
0e602686
NC
15159 if (decompress_dumps)
15160 {
15161 dwarf_size_type new_size = section_size;
15162 dwarf_size_type uncompressed_size = 0;
15163
15164 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15165 {
15166 Elf_Internal_Chdr chdr;
15167 unsigned int compression_header_size
ebdf1ebf 15168 = get_compression_header (& chdr, start, section_size);
0e602686 15169
5844b465
NC
15170 if (compression_header_size == 0)
15171 /* An error message will have already been generated
15172 by get_compression_header. */
15173 goto error_out;
15174
813dabb9 15175 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
0e602686 15176 {
813dabb9 15177 warn (_("section '%s' has unsupported compress type: %d\n"),
dda8d76d 15178 printable_section_name (filedata, section), chdr.ch_type);
f761cb13 15179 goto error_out;
0e602686 15180 }
813dabb9
L
15181 uncompressed_size = chdr.ch_size;
15182 start += compression_header_size;
15183 new_size -= compression_header_size;
0e602686
NC
15184 }
15185 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15186 {
15187 /* Read the zlib header. In this case, it should be "ZLIB"
15188 followed by the uncompressed section size, 8 bytes in
15189 big-endian order. */
15190 uncompressed_size = start[4]; uncompressed_size <<= 8;
15191 uncompressed_size += start[5]; uncompressed_size <<= 8;
15192 uncompressed_size += start[6]; uncompressed_size <<= 8;
15193 uncompressed_size += start[7]; uncompressed_size <<= 8;
15194 uncompressed_size += start[8]; uncompressed_size <<= 8;
15195 uncompressed_size += start[9]; uncompressed_size <<= 8;
15196 uncompressed_size += start[10]; uncompressed_size <<= 8;
15197 uncompressed_size += start[11];
15198 start += 12;
15199 new_size -= 12;
15200 }
15201
f055032e
NC
15202 if (uncompressed_size)
15203 {
15204 if (uncompress_section_contents (& start, uncompressed_size,
15205 & new_size))
bc303e5d
NC
15206 {
15207 section_size = new_size;
15208 }
f055032e
NC
15209 else
15210 {
15211 error (_("Unable to decompress section %s\n"),
dda8d76d 15212 printable_section_name (filedata, section));
bc303e5d 15213 /* FIXME: Print the section anyway ? */
f761cb13 15214 goto error_out;
f055032e
NC
15215 }
15216 }
bc303e5d
NC
15217 else
15218 start = real_start;
0e602686 15219 }
14ae95f2 15220
cf13d699
NC
15221 if (relocate)
15222 {
dda8d76d 15223 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
f761cb13 15224 goto error_out;
cf13d699
NC
15225 }
15226 else
15227 {
15228 /* If the section being dumped has relocations against it the user might
15229 be expecting these relocations to have been applied. Check for this
15230 case and issue a warning message in order to avoid confusion.
15231 FIXME: Maybe we ought to have an option that dumps a section with
15232 relocs applied ? */
dda8d76d
NC
15233 for (relsec = filedata->section_headers;
15234 relsec < filedata->section_headers + filedata->file_header.e_shnum;
cf13d699
NC
15235 ++relsec)
15236 {
15237 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
dda8d76d
NC
15238 || relsec->sh_info >= filedata->file_header.e_shnum
15239 || filedata->section_headers + relsec->sh_info != section
cf13d699 15240 || relsec->sh_size == 0
dda8d76d 15241 || relsec->sh_link >= filedata->file_header.e_shnum)
cf13d699
NC
15242 continue;
15243
15244 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15245 break;
15246 }
15247 }
15248
15249 addr = section->sh_addr;
0e602686 15250 bytes = section_size;
cf13d699
NC
15251 data = start;
15252
15253 while (bytes)
15254 {
15255 int j;
15256 int k;
15257 int lbytes;
15258
15259 lbytes = (bytes > 16 ? 16 : bytes);
15260
15261 printf (" 0x%8.8lx ", (unsigned long) addr);
15262
15263 for (j = 0; j < 16; j++)
15264 {
15265 if (j < lbytes)
15266 printf ("%2.2x", data[j]);
15267 else
15268 printf (" ");
15269
15270 if ((j & 3) == 3)
15271 printf (" ");
15272 }
15273
15274 for (j = 0; j < lbytes; j++)
15275 {
15276 k = data[j];
15277 if (k >= ' ' && k < 0x7f)
15278 printf ("%c", k);
15279 else
15280 printf (".");
15281 }
15282
15283 putchar ('\n');
15284
15285 data += lbytes;
15286 addr += lbytes;
15287 bytes -= lbytes;
15288 }
15289
0e602686 15290 free (real_start);
cf13d699
NC
15291
15292 putchar ('\n');
015dc7e1 15293 return true;
f761cb13
AM
15294
15295 error_out:
15296 free (real_start);
015dc7e1 15297 return false;
cf13d699
NC
15298}
15299
094e34f2 15300#ifdef ENABLE_LIBCTF
7d9813f1
NA
15301static ctf_sect_t *
15302shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
15303{
84714f86 15304 buf->cts_name = section_name_print (filedata, shdr);
7d9813f1
NA
15305 buf->cts_size = shdr->sh_size;
15306 buf->cts_entsize = shdr->sh_entsize;
7d9813f1
NA
15307
15308 return buf;
15309}
15310
15311/* Formatting callback function passed to ctf_dump. Returns either the pointer
15312 it is passed, or a pointer to newly-allocated storage, in which case
15313 dump_ctf() will free it when it no longer needs it. */
15314
2f6ecaed
NA
15315static char *
15316dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
15317 char *s, void *arg)
7d9813f1 15318{
3e50a591 15319 const char *blanks = arg;
7d9813f1
NA
15320 char *new_s;
15321
3e50a591 15322 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
7d9813f1
NA
15323 return s;
15324 return new_s;
15325}
15326
926c9e76
NA
15327/* Dump CTF errors/warnings. */
15328static void
139633c3 15329dump_ctf_errs (ctf_dict_t *fp)
926c9e76
NA
15330{
15331 ctf_next_t *it = NULL;
15332 char *errtext;
15333 int is_warning;
15334 int err;
15335
15336 /* Dump accumulated errors and warnings. */
15337 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
15338 {
5e9b84f7 15339 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
926c9e76
NA
15340 errtext);
15341 free (errtext);
15342 }
15343 if (err != ECTF_NEXT_END)
15344 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
15345}
15346
2f6ecaed
NA
15347/* Dump one CTF archive member. */
15348
80b56fad
NA
15349static void
15350dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
15351 size_t member)
2f6ecaed 15352{
2f6ecaed
NA
15353 const char *things[] = {"Header", "Labels", "Data objects",
15354 "Function objects", "Variables", "Types", "Strings",
15355 ""};
15356 const char **thing;
15357 size_t i;
15358
80b56fad
NA
15359 /* Don't print out the name of the default-named archive member if it appears
15360 first in the list. The name .ctf appears everywhere, even for things that
15361 aren't really archives, so printing it out is liable to be confusing; also,
15362 the common case by far is for only one archive member to exist, and hiding
15363 it in that case seems worthwhile. */
2f6ecaed 15364
80b56fad
NA
15365 if (strcmp (name, ".ctf") != 0 || member != 0)
15366 printf (_("\nCTF archive member: %s:\n"), name);
2f6ecaed 15367
80b56fad
NA
15368 if (ctf_parent_name (ctf) != NULL)
15369 ctf_import (ctf, parent);
2f6ecaed
NA
15370
15371 for (i = 0, thing = things; *thing[0]; thing++, i++)
15372 {
15373 ctf_dump_state_t *s = NULL;
15374 char *item;
15375
15376 printf ("\n %s:\n", *thing);
15377 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
15378 (void *) " ")) != NULL)
15379 {
15380 printf ("%s\n", item);
15381 free (item);
15382 }
15383
15384 if (ctf_errno (ctf))
15385 {
15386 error (_("Iteration failed: %s, %s\n"), *thing,
15387 ctf_errmsg (ctf_errno (ctf)));
80b56fad 15388 break;
2f6ecaed
NA
15389 }
15390 }
8b37e7b6 15391
926c9e76 15392 dump_ctf_errs (ctf);
2f6ecaed
NA
15393}
15394
015dc7e1 15395static bool
7d9813f1
NA
15396dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
15397{
7d9813f1
NA
15398 Elf_Internal_Shdr * symtab_sec = NULL;
15399 Elf_Internal_Shdr * strtab_sec = NULL;
d344b407
NA
15400 void * data = NULL;
15401 void * symdata = NULL;
15402 void * strdata = NULL;
80b56fad 15403 ctf_sect_t ctfsect, symsect, strsect;
d344b407
NA
15404 ctf_sect_t * symsectp = NULL;
15405 ctf_sect_t * strsectp = NULL;
2f6ecaed 15406 ctf_archive_t * ctfa = NULL;
139633c3 15407 ctf_dict_t * parent = NULL;
80b56fad 15408 ctf_dict_t * fp;
7d9813f1 15409
80b56fad
NA
15410 ctf_next_t *i = NULL;
15411 const char *name;
15412 size_t member = 0;
7d9813f1 15413 int err;
015dc7e1 15414 bool ret = false;
7d9813f1
NA
15415
15416 shdr_to_ctf_sect (&ctfsect, section, filedata);
15417 data = get_section_contents (section, filedata);
15418 ctfsect.cts_data = data;
15419
616febde 15420 if (!dump_ctf_symtab_name)
3d16b64e 15421 dump_ctf_symtab_name = strdup (".dynsym");
616febde
NA
15422
15423 if (!dump_ctf_strtab_name)
3d16b64e 15424 dump_ctf_strtab_name = strdup (".dynstr");
616febde
NA
15425
15426 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
7d9813f1
NA
15427 {
15428 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
15429 {
15430 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
15431 goto fail;
15432 }
15433 if ((symdata = (void *) get_data (NULL, filedata,
15434 symtab_sec->sh_offset, 1,
15435 symtab_sec->sh_size,
15436 _("symbols"))) == NULL)
15437 goto fail;
15438 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
15439 symsect.cts_data = symdata;
15440 }
835f2fae 15441
df16e041 15442 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
7d9813f1
NA
15443 {
15444 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
15445 {
15446 error (_("No string table section named %s\n"),
15447 dump_ctf_strtab_name);
15448 goto fail;
15449 }
15450 if ((strdata = (void *) get_data (NULL, filedata,
15451 strtab_sec->sh_offset, 1,
15452 strtab_sec->sh_size,
15453 _("strings"))) == NULL)
15454 goto fail;
15455 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
15456 strsect.cts_data = strdata;
15457 }
835f2fae 15458
2f6ecaed
NA
15459 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
15460 libctf papers over the difference, so we can pretend it is always an
80b56fad 15461 archive. */
7d9813f1 15462
2f6ecaed 15463 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
7d9813f1 15464 {
926c9e76 15465 dump_ctf_errs (NULL);
7d9813f1
NA
15466 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15467 goto fail;
15468 }
15469
96c61be5
NA
15470 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
15471 != ELFDATA2MSB);
15472
80b56fad
NA
15473 /* Preload the parent dict, since it will need to be imported into every
15474 child in turn. */
15475 if ((parent = ctf_dict_open (ctfa, dump_ctf_parent_name, &err)) == NULL)
2f6ecaed 15476 {
926c9e76 15477 dump_ctf_errs (NULL);
2f6ecaed
NA
15478 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15479 goto fail;
7d9813f1
NA
15480 }
15481
015dc7e1 15482 ret = true;
7d9813f1 15483
835f2fae
NC
15484 if (filedata->is_separate)
15485 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
15486 printable_section_name (filedata, section),
15487 filedata->file_name);
15488 else
15489 printf (_("\nDump of CTF section '%s':\n"),
15490 printable_section_name (filedata, section));
7d9813f1 15491
80b56fad
NA
15492 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
15493 dump_ctf_archive_member (fp, name, parent, member++);
15494 if (err != ECTF_NEXT_END)
15495 {
15496 dump_ctf_errs (NULL);
15497 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
15498 ret = false;
15499 }
7d9813f1
NA
15500
15501 fail:
139633c3 15502 ctf_dict_close (parent);
2f6ecaed 15503 ctf_close (ctfa);
7d9813f1
NA
15504 free (data);
15505 free (symdata);
15506 free (strdata);
15507 return ret;
15508}
094e34f2 15509#endif
7d9813f1 15510
015dc7e1 15511static bool
dda8d76d
NC
15512load_specific_debug_section (enum dwarf_section_display_enum debug,
15513 const Elf_Internal_Shdr * sec,
15514 void * data)
1007acb3 15515{
2cf0635d 15516 struct dwarf_section * section = &debug_displays [debug].section;
19e6b90e 15517 char buf [64];
dda8d76d 15518 Filedata * filedata = (Filedata *) data;
9abca702 15519
19e6b90e 15520 if (section->start != NULL)
dda8d76d
NC
15521 {
15522 /* If it is already loaded, do nothing. */
15523 if (streq (section->filename, filedata->file_name))
015dc7e1 15524 return true;
dda8d76d
NC
15525 free (section->start);
15526 }
1007acb3 15527
19e6b90e
L
15528 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
15529 section->address = sec->sh_addr;
dda8d76d
NC
15530 section->filename = filedata->file_name;
15531 section->start = (unsigned char *) get_data (NULL, filedata,
3f5e193b
NC
15532 sec->sh_offset, 1,
15533 sec->sh_size, buf);
59245841
NC
15534 if (section->start == NULL)
15535 section->size = 0;
15536 else
15537 {
77115a4a
L
15538 unsigned char *start = section->start;
15539 dwarf_size_type size = sec->sh_size;
dab394de 15540 dwarf_size_type uncompressed_size = 0;
77115a4a
L
15541
15542 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
15543 {
15544 Elf_Internal_Chdr chdr;
d8024a91
NC
15545 unsigned int compression_header_size;
15546
f53be977
L
15547 if (size < (is_32bit_elf
15548 ? sizeof (Elf32_External_Chdr)
15549 : sizeof (Elf64_External_Chdr)))
d8024a91 15550 {
55be8fd0 15551 warn (_("compressed section %s is too small to contain a compression header\n"),
d8024a91 15552 section->name);
015dc7e1 15553 return false;
d8024a91
NC
15554 }
15555
ebdf1ebf 15556 compression_header_size = get_compression_header (&chdr, start, size);
5844b465
NC
15557 if (compression_header_size == 0)
15558 /* An error message will have already been generated
15559 by get_compression_header. */
015dc7e1 15560 return false;
d8024a91 15561
813dabb9
L
15562 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15563 {
15564 warn (_("section '%s' has unsupported compress type: %d\n"),
15565 section->name, chdr.ch_type);
015dc7e1 15566 return false;
813dabb9 15567 }
dab394de 15568 uncompressed_size = chdr.ch_size;
77115a4a
L
15569 start += compression_header_size;
15570 size -= compression_header_size;
15571 }
dab394de
L
15572 else if (size > 12 && streq ((char *) start, "ZLIB"))
15573 {
15574 /* Read the zlib header. In this case, it should be "ZLIB"
15575 followed by the uncompressed section size, 8 bytes in
15576 big-endian order. */
15577 uncompressed_size = start[4]; uncompressed_size <<= 8;
15578 uncompressed_size += start[5]; uncompressed_size <<= 8;
15579 uncompressed_size += start[6]; uncompressed_size <<= 8;
15580 uncompressed_size += start[7]; uncompressed_size <<= 8;
15581 uncompressed_size += start[8]; uncompressed_size <<= 8;
15582 uncompressed_size += start[9]; uncompressed_size <<= 8;
15583 uncompressed_size += start[10]; uncompressed_size <<= 8;
15584 uncompressed_size += start[11];
15585 start += 12;
15586 size -= 12;
15587 }
15588
1835f746 15589 if (uncompressed_size)
77115a4a 15590 {
1835f746
NC
15591 if (uncompress_section_contents (&start, uncompressed_size,
15592 &size))
15593 {
15594 /* Free the compressed buffer, update the section buffer
15595 and the section size if uncompress is successful. */
15596 free (section->start);
15597 section->start = start;
15598 }
15599 else
15600 {
15601 error (_("Unable to decompress section %s\n"),
dda8d76d 15602 printable_section_name (filedata, sec));
015dc7e1 15603 return false;
1835f746 15604 }
77115a4a 15605 }
bc303e5d 15606
77115a4a 15607 section->size = size;
59245841 15608 }
4a114e3e 15609
1b315056 15610 if (section->start == NULL)
015dc7e1 15611 return false;
1b315056 15612
19e6b90e 15613 if (debug_displays [debug].relocate)
32ec8896 15614 {
dda8d76d 15615 if (! apply_relocations (filedata, sec, section->start, section->size,
32ec8896 15616 & section->reloc_info, & section->num_relocs))
015dc7e1 15617 return false;
32ec8896 15618 }
d1c4b12b
NC
15619 else
15620 {
15621 section->reloc_info = NULL;
15622 section->num_relocs = 0;
15623 }
1007acb3 15624
015dc7e1 15625 return true;
1007acb3
L
15626}
15627
301a9420
AM
15628#if HAVE_LIBDEBUGINFOD
15629/* Return a hex string representation of the build-id. */
15630unsigned char *
15631get_build_id (void * data)
15632{
ca0e11aa 15633 Filedata * filedata = (Filedata *) data;
301a9420
AM
15634 Elf_Internal_Shdr * shdr;
15635 unsigned long i;
15636
55be8fd0
NC
15637 /* Iterate through notes to find note.gnu.build-id.
15638 FIXME: Only the first note in any note section is examined. */
301a9420
AM
15639 for (i = 0, shdr = filedata->section_headers;
15640 i < filedata->file_header.e_shnum && shdr != NULL;
15641 i++, shdr++)
15642 {
15643 if (shdr->sh_type != SHT_NOTE)
15644 continue;
15645
15646 char * next;
15647 char * end;
15648 size_t data_remaining;
15649 size_t min_notesz;
15650 Elf_External_Note * enote;
15651 Elf_Internal_Note inote;
15652
15653 bfd_vma offset = shdr->sh_offset;
15654 bfd_vma align = shdr->sh_addralign;
15655 bfd_vma length = shdr->sh_size;
15656
15657 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
15658 if (enote == NULL)
15659 continue;
15660
15661 if (align < 4)
15662 align = 4;
15663 else if (align != 4 && align != 8)
f761cb13
AM
15664 {
15665 free (enote);
15666 continue;
15667 }
301a9420
AM
15668
15669 end = (char *) enote + length;
15670 data_remaining = end - (char *) enote;
15671
15672 if (!is_ia64_vms (filedata))
15673 {
15674 min_notesz = offsetof (Elf_External_Note, name);
15675 if (data_remaining < min_notesz)
15676 {
55be8fd0
NC
15677 warn (_("\
15678malformed note encountered in section %s whilst scanning for build-id note\n"),
15679 printable_section_name (filedata, shdr));
f761cb13 15680 free (enote);
55be8fd0 15681 continue;
301a9420
AM
15682 }
15683 data_remaining -= min_notesz;
15684
15685 inote.type = BYTE_GET (enote->type);
15686 inote.namesz = BYTE_GET (enote->namesz);
15687 inote.namedata = enote->name;
15688 inote.descsz = BYTE_GET (enote->descsz);
15689 inote.descdata = ((char *) enote
15690 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
15691 inote.descpos = offset + (inote.descdata - (char *) enote);
15692 next = ((char *) enote
15693 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
15694 }
15695 else
15696 {
15697 Elf64_External_VMS_Note *vms_enote;
15698
15699 /* PR binutils/15191
15700 Make sure that there is enough data to read. */
15701 min_notesz = offsetof (Elf64_External_VMS_Note, name);
15702 if (data_remaining < min_notesz)
15703 {
55be8fd0
NC
15704 warn (_("\
15705malformed note encountered in section %s whilst scanning for build-id note\n"),
15706 printable_section_name (filedata, shdr));
f761cb13 15707 free (enote);
55be8fd0 15708 continue;
301a9420
AM
15709 }
15710 data_remaining -= min_notesz;
15711
15712 vms_enote = (Elf64_External_VMS_Note *) enote;
15713 inote.type = BYTE_GET (vms_enote->type);
15714 inote.namesz = BYTE_GET (vms_enote->namesz);
15715 inote.namedata = vms_enote->name;
15716 inote.descsz = BYTE_GET (vms_enote->descsz);
15717 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
15718 inote.descpos = offset + (inote.descdata - (char *) enote);
15719 next = inote.descdata + align_power (inote.descsz, 3);
15720 }
15721
15722 /* Skip malformed notes. */
15723 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
15724 || (size_t) (inote.descdata - inote.namedata) > data_remaining
15725 || (size_t) (next - inote.descdata) < inote.descsz
15726 || ((size_t) (next - inote.descdata)
15727 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
15728 {
55be8fd0
NC
15729 warn (_("\
15730malformed note encountered in section %s whilst scanning for build-id note\n"),
15731 printable_section_name (filedata, shdr));
f761cb13 15732 free (enote);
301a9420
AM
15733 continue;
15734 }
15735
15736 /* Check if this is the build-id note. If so then convert the build-id
15737 bytes to a hex string. */
15738 if (inote.namesz > 0
24d127aa 15739 && startswith (inote.namedata, "GNU")
301a9420
AM
15740 && inote.type == NT_GNU_BUILD_ID)
15741 {
15742 unsigned long j;
15743 char * build_id;
15744
15745 build_id = malloc (inote.descsz * 2 + 1);
15746 if (build_id == NULL)
f761cb13
AM
15747 {
15748 free (enote);
15749 return NULL;
15750 }
301a9420
AM
15751
15752 for (j = 0; j < inote.descsz; ++j)
15753 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
15754 build_id[inote.descsz * 2] = '\0';
f761cb13 15755 free (enote);
301a9420 15756
55be8fd0 15757 return (unsigned char *) build_id;
301a9420 15758 }
f761cb13 15759 free (enote);
301a9420
AM
15760 }
15761
15762 return NULL;
15763}
15764#endif /* HAVE_LIBDEBUGINFOD */
15765
657d0d47
CC
15766/* If this is not NULL, load_debug_section will only look for sections
15767 within the list of sections given here. */
32ec8896 15768static unsigned int * section_subset = NULL;
657d0d47 15769
015dc7e1 15770bool
dda8d76d 15771load_debug_section (enum dwarf_section_display_enum debug, void * data)
d966045b 15772{
2cf0635d
NC
15773 struct dwarf_section * section = &debug_displays [debug].section;
15774 Elf_Internal_Shdr * sec;
dda8d76d
NC
15775 Filedata * filedata = (Filedata *) data;
15776
f425ec66
NC
15777 /* Without section headers we cannot find any sections. */
15778 if (filedata->section_headers == NULL)
015dc7e1 15779 return false;
f425ec66 15780
9c1ce108
AM
15781 if (filedata->string_table == NULL
15782 && filedata->file_header.e_shstrndx != SHN_UNDEF
15783 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
dda8d76d
NC
15784 {
15785 Elf_Internal_Shdr * strs;
15786
15787 /* Read in the string table, so that we have section names to scan. */
15788 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
15789
4dff97b2 15790 if (strs != NULL && strs->sh_size != 0)
dda8d76d 15791 {
9c1ce108
AM
15792 filedata->string_table
15793 = (char *) get_data (NULL, filedata, strs->sh_offset,
15794 1, strs->sh_size, _("string table"));
dda8d76d 15795
9c1ce108
AM
15796 filedata->string_table_length
15797 = filedata->string_table != NULL ? strs->sh_size : 0;
dda8d76d
NC
15798 }
15799 }
d966045b
DJ
15800
15801 /* Locate the debug section. */
dda8d76d 15802 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
d966045b
DJ
15803 if (sec != NULL)
15804 section->name = section->uncompressed_name;
15805 else
15806 {
dda8d76d 15807 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
d966045b
DJ
15808 if (sec != NULL)
15809 section->name = section->compressed_name;
15810 }
15811 if (sec == NULL)
015dc7e1 15812 return false;
d966045b 15813
657d0d47
CC
15814 /* If we're loading from a subset of sections, and we've loaded
15815 a section matching this name before, it's likely that it's a
15816 different one. */
15817 if (section_subset != NULL)
15818 free_debug_section (debug);
15819
dda8d76d 15820 return load_specific_debug_section (debug, sec, data);
d966045b
DJ
15821}
15822
19e6b90e
L
15823void
15824free_debug_section (enum dwarf_section_display_enum debug)
1007acb3 15825{
2cf0635d 15826 struct dwarf_section * section = &debug_displays [debug].section;
1007acb3 15827
19e6b90e
L
15828 if (section->start == NULL)
15829 return;
1007acb3 15830
19e6b90e
L
15831 free ((char *) section->start);
15832 section->start = NULL;
15833 section->address = 0;
15834 section->size = 0;
a788aedd 15835
9db70fc3
AM
15836 free (section->reloc_info);
15837 section->reloc_info = NULL;
15838 section->num_relocs = 0;
1007acb3
L
15839}
15840
015dc7e1 15841static bool
dda8d76d 15842display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
1007acb3 15843{
84714f86
AM
15844 const char *name = (section_name_valid (filedata, section)
15845 ? section_name (filedata, section) : "");
15846 const char *print_name = printable_section_name (filedata, section);
19e6b90e 15847 bfd_size_type length;
015dc7e1 15848 bool result = true;
3f5e193b 15849 int i;
1007acb3 15850
19e6b90e
L
15851 length = section->sh_size;
15852 if (length == 0)
1007acb3 15853 {
74e1a04b 15854 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
015dc7e1 15855 return true;
1007acb3 15856 }
5dff79d8
NC
15857 if (section->sh_type == SHT_NOBITS)
15858 {
15859 /* There is no point in dumping the contents of a debugging section
15860 which has the NOBITS type - the bits in the file will be random.
15861 This can happen when a file containing a .eh_frame section is
15862 stripped with the --only-keep-debug command line option. */
74e1a04b
NC
15863 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
15864 print_name);
015dc7e1 15865 return false;
5dff79d8 15866 }
1007acb3 15867
24d127aa 15868 if (startswith (name, ".gnu.linkonce.wi."))
19e6b90e 15869 name = ".debug_info";
1007acb3 15870
19e6b90e
L
15871 /* See if we know how to display the contents of this section. */
15872 for (i = 0; i < max; i++)
d85bf2ba
NC
15873 {
15874 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
15875 struct dwarf_section_display * display = debug_displays + i;
15876 struct dwarf_section * sec = & display->section;
d966045b 15877
d85bf2ba 15878 if (streq (sec->uncompressed_name, name)
24d127aa 15879 || (id == line && startswith (name, ".debug_line."))
d85bf2ba
NC
15880 || streq (sec->compressed_name, name))
15881 {
015dc7e1 15882 bool secondary = (section != find_section (filedata, name));
1007acb3 15883
d85bf2ba
NC
15884 if (secondary)
15885 free_debug_section (id);
dda8d76d 15886
24d127aa 15887 if (i == line && startswith (name, ".debug_line."))
d85bf2ba
NC
15888 sec->name = name;
15889 else if (streq (sec->uncompressed_name, name))
15890 sec->name = sec->uncompressed_name;
15891 else
15892 sec->name = sec->compressed_name;
657d0d47 15893
d85bf2ba
NC
15894 if (load_specific_debug_section (id, section, filedata))
15895 {
15896 /* If this debug section is part of a CU/TU set in a .dwp file,
15897 restrict load_debug_section to the sections in that set. */
15898 section_subset = find_cu_tu_set (filedata, shndx);
1007acb3 15899
d85bf2ba 15900 result &= display->display (sec, filedata);
657d0d47 15901
d85bf2ba 15902 section_subset = NULL;
1007acb3 15903
44266f36 15904 if (secondary || (id != info && id != abbrev && id != debug_addr))
d85bf2ba
NC
15905 free_debug_section (id);
15906 }
15907 break;
15908 }
15909 }
1007acb3 15910
19e6b90e 15911 if (i == max)
1007acb3 15912 {
74e1a04b 15913 printf (_("Unrecognized debug section: %s\n"), print_name);
015dc7e1 15914 result = false;
1007acb3
L
15915 }
15916
19e6b90e 15917 return result;
5b18a4bc 15918}
103f02d3 15919
aef1f6d0
DJ
15920/* Set DUMP_SECTS for all sections where dumps were requested
15921 based on section name. */
15922
15923static void
dda8d76d 15924initialise_dumps_byname (Filedata * filedata)
aef1f6d0 15925{
2cf0635d 15926 struct dump_list_entry * cur;
aef1f6d0
DJ
15927
15928 for (cur = dump_sects_byname; cur; cur = cur->next)
15929 {
15930 unsigned int i;
015dc7e1 15931 bool any = false;
aef1f6d0 15932
dda8d76d 15933 for (i = 0; i < filedata->file_header.e_shnum; i++)
84714f86
AM
15934 if (section_name_valid (filedata, filedata->section_headers + i)
15935 && streq (section_name (filedata, filedata->section_headers + i),
15936 cur->name))
aef1f6d0 15937 {
6431e409 15938 request_dump_bynumber (&filedata->dump, i, cur->type);
015dc7e1 15939 any = true;
aef1f6d0
DJ
15940 }
15941
835f2fae
NC
15942 if (!any && !filedata->is_separate)
15943 warn (_("Section '%s' was not dumped because it does not exist\n"),
15944 cur->name);
aef1f6d0
DJ
15945 }
15946}
15947
015dc7e1 15948static bool
dda8d76d 15949process_section_contents (Filedata * filedata)
5b18a4bc 15950{
2cf0635d 15951 Elf_Internal_Shdr * section;
19e6b90e 15952 unsigned int i;
015dc7e1 15953 bool res = true;
103f02d3 15954
19e6b90e 15955 if (! do_dump)
015dc7e1 15956 return true;
103f02d3 15957
dda8d76d 15958 initialise_dumps_byname (filedata);
aef1f6d0 15959
dda8d76d 15960 for (i = 0, section = filedata->section_headers;
6431e409 15961 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
19e6b90e
L
15962 i++, section++)
15963 {
6431e409 15964 dump_type dump = filedata->dump.dump_sects[i];
dda8d76d 15965
d6bfbc39
NC
15966 if (filedata->is_separate && ! process_links)
15967 dump &= DEBUG_DUMP;
047c3dbf 15968
19e6b90e 15969#ifdef SUPPORT_DISASSEMBLY
dda8d76d
NC
15970 if (dump & DISASS_DUMP)
15971 {
15972 if (! disassemble_section (section, filedata))
015dc7e1 15973 res = false;
dda8d76d 15974 }
19e6b90e 15975#endif
dda8d76d 15976 if (dump & HEX_DUMP)
32ec8896 15977 {
015dc7e1
AM
15978 if (! dump_section_as_bytes (section, filedata, false))
15979 res = false;
32ec8896 15980 }
103f02d3 15981
dda8d76d 15982 if (dump & RELOC_DUMP)
32ec8896 15983 {
015dc7e1
AM
15984 if (! dump_section_as_bytes (section, filedata, true))
15985 res = false;
32ec8896 15986 }
09c11c86 15987
dda8d76d 15988 if (dump & STRING_DUMP)
32ec8896 15989 {
dda8d76d 15990 if (! dump_section_as_strings (section, filedata))
015dc7e1 15991 res = false;
32ec8896 15992 }
cf13d699 15993
dda8d76d 15994 if (dump & DEBUG_DUMP)
32ec8896 15995 {
dda8d76d 15996 if (! display_debug_section (i, section, filedata))
015dc7e1 15997 res = false;
32ec8896 15998 }
7d9813f1 15999
094e34f2 16000#ifdef ENABLE_LIBCTF
7d9813f1
NA
16001 if (dump & CTF_DUMP)
16002 {
16003 if (! dump_section_as_ctf (section, filedata))
015dc7e1 16004 res = false;
7d9813f1 16005 }
094e34f2 16006#endif
5b18a4bc 16007 }
103f02d3 16008
835f2fae 16009 if (! filedata->is_separate)
0ee3043f 16010 {
835f2fae
NC
16011 /* Check to see if the user requested a
16012 dump of a section that does not exist. */
16013 for (; i < filedata->dump.num_dump_sects; i++)
16014 if (filedata->dump.dump_sects[i])
16015 {
ca0e11aa 16016 warn (_("Section %d was not dumped because it does not exist!\n"), i);
015dc7e1 16017 res = false;
835f2fae 16018 }
0ee3043f 16019 }
32ec8896
NC
16020
16021 return res;
5b18a4bc 16022}
103f02d3 16023
5b18a4bc 16024static void
19e6b90e 16025process_mips_fpe_exception (int mask)
5b18a4bc 16026{
19e6b90e
L
16027 if (mask)
16028 {
015dc7e1 16029 bool first = true;
32ec8896 16030
19e6b90e 16031 if (mask & OEX_FPU_INEX)
015dc7e1 16032 fputs ("INEX", stdout), first = false;
19e6b90e 16033 if (mask & OEX_FPU_UFLO)
015dc7e1 16034 printf ("%sUFLO", first ? "" : "|"), first = false;
19e6b90e 16035 if (mask & OEX_FPU_OFLO)
015dc7e1 16036 printf ("%sOFLO", first ? "" : "|"), first = false;
19e6b90e 16037 if (mask & OEX_FPU_DIV0)
015dc7e1 16038 printf ("%sDIV0", first ? "" : "|"), first = false;
19e6b90e
L
16039 if (mask & OEX_FPU_INVAL)
16040 printf ("%sINVAL", first ? "" : "|");
16041 }
5b18a4bc 16042 else
19e6b90e 16043 fputs ("0", stdout);
5b18a4bc 16044}
103f02d3 16045
f6f0e17b
NC
16046/* Display's the value of TAG at location P. If TAG is
16047 greater than 0 it is assumed to be an unknown tag, and
16048 a message is printed to this effect. Otherwise it is
16049 assumed that a message has already been printed.
16050
16051 If the bottom bit of TAG is set it assumed to have a
16052 string value, otherwise it is assumed to have an integer
16053 value.
16054
16055 Returns an updated P pointing to the first unread byte
16056 beyond the end of TAG's value.
16057
16058 Reads at or beyond END will not be made. */
16059
16060static unsigned char *
60abdbed 16061display_tag_value (signed int tag,
f6f0e17b
NC
16062 unsigned char * p,
16063 const unsigned char * const end)
16064{
16065 unsigned long val;
16066
16067 if (tag > 0)
16068 printf (" Tag_unknown_%d: ", tag);
16069
16070 if (p >= end)
16071 {
4082ef84 16072 warn (_("<corrupt tag>\n"));
f6f0e17b
NC
16073 }
16074 else if (tag & 1)
16075 {
071436c6
NC
16076 /* PR 17531 file: 027-19978-0.004. */
16077 size_t maxlen = (end - p) - 1;
16078
16079 putchar ('"');
4082ef84
NC
16080 if (maxlen > 0)
16081 {
16082 print_symbol ((int) maxlen, (const char *) p);
16083 p += strnlen ((char *) p, maxlen) + 1;
16084 }
16085 else
16086 {
16087 printf (_("<corrupt string tag>"));
16088 p = (unsigned char *) end;
16089 }
071436c6 16090 printf ("\"\n");
f6f0e17b
NC
16091 }
16092 else
16093 {
cd30bcef 16094 READ_ULEB (val, p, end);
f6f0e17b
NC
16095 printf ("%ld (0x%lx)\n", val, val);
16096 }
16097
4082ef84 16098 assert (p <= end);
f6f0e17b
NC
16099 return p;
16100}
16101
53a346d8
CZ
16102/* ARC ABI attributes section. */
16103
16104static unsigned char *
16105display_arc_attribute (unsigned char * p,
16106 const unsigned char * const end)
16107{
16108 unsigned int tag;
53a346d8
CZ
16109 unsigned int val;
16110
cd30bcef 16111 READ_ULEB (tag, p, end);
53a346d8
CZ
16112
16113 switch (tag)
16114 {
16115 case Tag_ARC_PCS_config:
cd30bcef 16116 READ_ULEB (val, p, end);
53a346d8
CZ
16117 printf (" Tag_ARC_PCS_config: ");
16118 switch (val)
16119 {
16120 case 0:
16121 printf (_("Absent/Non standard\n"));
16122 break;
16123 case 1:
16124 printf (_("Bare metal/mwdt\n"));
16125 break;
16126 case 2:
16127 printf (_("Bare metal/newlib\n"));
16128 break;
16129 case 3:
16130 printf (_("Linux/uclibc\n"));
16131 break;
16132 case 4:
16133 printf (_("Linux/glibc\n"));
16134 break;
16135 default:
16136 printf (_("Unknown\n"));
16137 break;
16138 }
16139 break;
16140
16141 case Tag_ARC_CPU_base:
cd30bcef 16142 READ_ULEB (val, p, end);
53a346d8
CZ
16143 printf (" Tag_ARC_CPU_base: ");
16144 switch (val)
16145 {
16146 default:
16147 case TAG_CPU_NONE:
16148 printf (_("Absent\n"));
16149 break;
16150 case TAG_CPU_ARC6xx:
16151 printf ("ARC6xx\n");
16152 break;
16153 case TAG_CPU_ARC7xx:
16154 printf ("ARC7xx\n");
16155 break;
16156 case TAG_CPU_ARCEM:
16157 printf ("ARCEM\n");
16158 break;
16159 case TAG_CPU_ARCHS:
16160 printf ("ARCHS\n");
16161 break;
16162 }
16163 break;
16164
16165 case Tag_ARC_CPU_variation:
cd30bcef 16166 READ_ULEB (val, p, end);
53a346d8
CZ
16167 printf (" Tag_ARC_CPU_variation: ");
16168 switch (val)
16169 {
16170 default:
16171 if (val > 0 && val < 16)
53a346d8 16172 printf ("Core%d\n", val);
d8cbc93b
JL
16173 else
16174 printf ("Unknown\n");
16175 break;
16176
53a346d8
CZ
16177 case 0:
16178 printf (_("Absent\n"));
16179 break;
16180 }
16181 break;
16182
16183 case Tag_ARC_CPU_name:
16184 printf (" Tag_ARC_CPU_name: ");
16185 p = display_tag_value (-1, p, end);
16186 break;
16187
16188 case Tag_ARC_ABI_rf16:
cd30bcef 16189 READ_ULEB (val, p, end);
53a346d8
CZ
16190 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
16191 break;
16192
16193 case Tag_ARC_ABI_osver:
cd30bcef 16194 READ_ULEB (val, p, end);
53a346d8
CZ
16195 printf (" Tag_ARC_ABI_osver: v%d\n", val);
16196 break;
16197
16198 case Tag_ARC_ABI_pic:
16199 case Tag_ARC_ABI_sda:
cd30bcef 16200 READ_ULEB (val, p, end);
53a346d8
CZ
16201 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
16202 : " Tag_ARC_ABI_pic: ");
16203 switch (val)
16204 {
16205 case 0:
16206 printf (_("Absent\n"));
16207 break;
16208 case 1:
16209 printf ("MWDT\n");
16210 break;
16211 case 2:
16212 printf ("GNU\n");
16213 break;
16214 default:
16215 printf (_("Unknown\n"));
16216 break;
16217 }
16218 break;
16219
16220 case Tag_ARC_ABI_tls:
cd30bcef 16221 READ_ULEB (val, p, end);
53a346d8
CZ
16222 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
16223 break;
16224
16225 case Tag_ARC_ABI_enumsize:
cd30bcef 16226 READ_ULEB (val, p, end);
53a346d8
CZ
16227 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
16228 _("smallest"));
16229 break;
16230
16231 case Tag_ARC_ABI_exceptions:
cd30bcef 16232 READ_ULEB (val, p, end);
53a346d8
CZ
16233 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
16234 : _("default"));
16235 break;
16236
16237 case Tag_ARC_ABI_double_size:
cd30bcef 16238 READ_ULEB (val, p, end);
53a346d8
CZ
16239 printf (" Tag_ARC_ABI_double_size: %d\n", val);
16240 break;
16241
16242 case Tag_ARC_ISA_config:
16243 printf (" Tag_ARC_ISA_config: ");
16244 p = display_tag_value (-1, p, end);
16245 break;
16246
16247 case Tag_ARC_ISA_apex:
16248 printf (" Tag_ARC_ISA_apex: ");
16249 p = display_tag_value (-1, p, end);
16250 break;
16251
16252 case Tag_ARC_ISA_mpy_option:
cd30bcef 16253 READ_ULEB (val, p, end);
53a346d8
CZ
16254 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
16255 break;
16256
db1e1b45 16257 case Tag_ARC_ATR_version:
cd30bcef 16258 READ_ULEB (val, p, end);
db1e1b45 16259 printf (" Tag_ARC_ATR_version: %d\n", val);
16260 break;
16261
53a346d8
CZ
16262 default:
16263 return display_tag_value (tag & 1, p, end);
16264 }
16265
16266 return p;
16267}
16268
11c1ff18
PB
16269/* ARM EABI attributes section. */
16270typedef struct
16271{
70e99720 16272 unsigned int tag;
2cf0635d 16273 const char * name;
11c1ff18 16274 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
70e99720 16275 unsigned int type;
288f0ba2 16276 const char *const *table;
11c1ff18
PB
16277} arm_attr_public_tag;
16278
288f0ba2 16279static const char *const arm_attr_tag_CPU_arch[] =
11c1ff18 16280 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
ced40572 16281 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
3197e593
PW
16282 "v8-M.mainline", "v8.1-A", "v8.2-A", "v8.3-A",
16283 "v8.1-M.mainline", "v9"};
288f0ba2
AM
16284static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
16285static const char *const arm_attr_tag_THUMB_ISA_use[] =
4ed7ed8d 16286 {"No", "Thumb-1", "Thumb-2", "Yes"};
288f0ba2 16287static const char *const arm_attr_tag_FP_arch[] =
bca38921 16288 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
a715796b 16289 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
288f0ba2
AM
16290static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
16291static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
9411fd44
MW
16292 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
16293 "NEON for ARMv8.1"};
288f0ba2 16294static const char *const arm_attr_tag_PCS_config[] =
11c1ff18
PB
16295 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
16296 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
288f0ba2 16297static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
11c1ff18 16298 {"V6", "SB", "TLS", "Unused"};
288f0ba2 16299static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
11c1ff18 16300 {"Absolute", "PC-relative", "SB-relative", "None"};
288f0ba2 16301static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
11c1ff18 16302 {"Absolute", "PC-relative", "None"};
288f0ba2 16303static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
11c1ff18 16304 {"None", "direct", "GOT-indirect"};
288f0ba2 16305static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
11c1ff18 16306 {"None", "??? 1", "2", "??? 3", "4"};
288f0ba2
AM
16307static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
16308static const char *const arm_attr_tag_ABI_FP_denormal[] =
f5f53991 16309 {"Unused", "Needed", "Sign only"};
288f0ba2
AM
16310static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
16311static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
16312static const char *const arm_attr_tag_ABI_FP_number_model[] =
11c1ff18 16313 {"Unused", "Finite", "RTABI", "IEEE 754"};
288f0ba2 16314static const char *const arm_attr_tag_ABI_enum_size[] =
11c1ff18 16315 {"Unused", "small", "int", "forced to int"};
288f0ba2 16316static const char *const arm_attr_tag_ABI_HardFP_use[] =
99654aaf 16317 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
288f0ba2 16318static const char *const arm_attr_tag_ABI_VFP_args[] =
5c294fee 16319 {"AAPCS", "VFP registers", "custom", "compatible"};
288f0ba2 16320static const char *const arm_attr_tag_ABI_WMMX_args[] =
11c1ff18 16321 {"AAPCS", "WMMX registers", "custom"};
288f0ba2 16322static const char *const arm_attr_tag_ABI_optimization_goals[] =
11c1ff18
PB
16323 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16324 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
288f0ba2 16325static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
11c1ff18
PB
16326 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16327 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
288f0ba2
AM
16328static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
16329static const char *const arm_attr_tag_FP_HP_extension[] =
8e79c3df 16330 {"Not Allowed", "Allowed"};
288f0ba2 16331static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
8e79c3df 16332 {"None", "IEEE 754", "Alternative Format"};
288f0ba2 16333static const char *const arm_attr_tag_DSP_extension[] =
15afaa63 16334 {"Follow architecture", "Allowed"};
288f0ba2 16335static const char *const arm_attr_tag_MPextension_use[] =
cd21e546 16336 {"Not Allowed", "Allowed"};
288f0ba2 16337static const char *const arm_attr_tag_DIV_use[] =
dd24e3da 16338 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
cd21e546 16339 "Allowed in v7-A with integer division extension"};
288f0ba2
AM
16340static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
16341static const char *const arm_attr_tag_Virtualization_use[] =
dd24e3da 16342 {"Not Allowed", "TrustZone", "Virtualization Extensions",
cd21e546 16343 "TrustZone and Virtualization Extensions"};
288f0ba2 16344static const char *const arm_attr_tag_MPextension_use_legacy[] =
f5f53991 16345 {"Not Allowed", "Allowed"};
11c1ff18 16346
288f0ba2 16347static const char *const arm_attr_tag_MVE_arch[] =
a7ad558c
AV
16348 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
16349
99db83d0
AC
16350static const char * arm_attr_tag_PAC_extension[] =
16351 {"No PAC/AUT instructions",
16352 "PAC/AUT instructions permitted in the NOP space",
16353 "PAC/AUT instructions permitted in the NOP and in the non-NOP space"};
16354
4b535030
AC
16355static const char * arm_attr_tag_BTI_extension[] =
16356 {"BTI instructions not permitted",
16357 "BTI instructions permitted in the NOP space",
16358 "BTI instructions permitted in the NOP and in the non-NOP space"};
16359
b81ee92f
AC
16360static const char * arm_attr_tag_BTI_use[] =
16361 {"Compiled without branch target enforcement",
16362 "Compiled with branch target enforcement"};
16363
c9fed665
AC
16364static const char * arm_attr_tag_PACRET_use[] =
16365 {"Compiled without return address signing and authentication",
16366 "Compiled with return address signing and authentication"};
16367
11c1ff18
PB
16368#define LOOKUP(id, name) \
16369 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
d70c5fc7 16370static arm_attr_public_tag arm_attr_public_tags[] =
11c1ff18
PB
16371{
16372 {4, "CPU_raw_name", 1, NULL},
16373 {5, "CPU_name", 1, NULL},
16374 LOOKUP(6, CPU_arch),
16375 {7, "CPU_arch_profile", 0, NULL},
16376 LOOKUP(8, ARM_ISA_use),
16377 LOOKUP(9, THUMB_ISA_use),
75375b3e 16378 LOOKUP(10, FP_arch),
11c1ff18 16379 LOOKUP(11, WMMX_arch),
f5f53991
AS
16380 LOOKUP(12, Advanced_SIMD_arch),
16381 LOOKUP(13, PCS_config),
11c1ff18
PB
16382 LOOKUP(14, ABI_PCS_R9_use),
16383 LOOKUP(15, ABI_PCS_RW_data),
f5f53991 16384 LOOKUP(16, ABI_PCS_RO_data),
11c1ff18
PB
16385 LOOKUP(17, ABI_PCS_GOT_use),
16386 LOOKUP(18, ABI_PCS_wchar_t),
16387 LOOKUP(19, ABI_FP_rounding),
16388 LOOKUP(20, ABI_FP_denormal),
16389 LOOKUP(21, ABI_FP_exceptions),
16390 LOOKUP(22, ABI_FP_user_exceptions),
16391 LOOKUP(23, ABI_FP_number_model),
75375b3e
MGD
16392 {24, "ABI_align_needed", 0, NULL},
16393 {25, "ABI_align_preserved", 0, NULL},
11c1ff18
PB
16394 LOOKUP(26, ABI_enum_size),
16395 LOOKUP(27, ABI_HardFP_use),
16396 LOOKUP(28, ABI_VFP_args),
16397 LOOKUP(29, ABI_WMMX_args),
16398 LOOKUP(30, ABI_optimization_goals),
16399 LOOKUP(31, ABI_FP_optimization_goals),
8e79c3df 16400 {32, "compatibility", 0, NULL},
f5f53991 16401 LOOKUP(34, CPU_unaligned_access),
75375b3e 16402 LOOKUP(36, FP_HP_extension),
8e79c3df 16403 LOOKUP(38, ABI_FP_16bit_format),
cd21e546
MGD
16404 LOOKUP(42, MPextension_use),
16405 LOOKUP(44, DIV_use),
15afaa63 16406 LOOKUP(46, DSP_extension),
a7ad558c 16407 LOOKUP(48, MVE_arch),
99db83d0 16408 LOOKUP(50, PAC_extension),
4b535030 16409 LOOKUP(52, BTI_extension),
b81ee92f 16410 LOOKUP(74, BTI_use),
c9fed665 16411 LOOKUP(76, PACRET_use),
f5f53991
AS
16412 {64, "nodefaults", 0, NULL},
16413 {65, "also_compatible_with", 0, NULL},
16414 LOOKUP(66, T2EE_use),
16415 {67, "conformance", 1, NULL},
16416 LOOKUP(68, Virtualization_use),
cd21e546 16417 LOOKUP(70, MPextension_use_legacy)
11c1ff18
PB
16418};
16419#undef LOOKUP
16420
11c1ff18 16421static unsigned char *
f6f0e17b
NC
16422display_arm_attribute (unsigned char * p,
16423 const unsigned char * const end)
11c1ff18 16424{
70e99720 16425 unsigned int tag;
70e99720 16426 unsigned int val;
2cf0635d 16427 arm_attr_public_tag * attr;
11c1ff18 16428 unsigned i;
70e99720 16429 unsigned int type;
11c1ff18 16430
cd30bcef 16431 READ_ULEB (tag, p, end);
11c1ff18 16432 attr = NULL;
2cf0635d 16433 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
11c1ff18
PB
16434 {
16435 if (arm_attr_public_tags[i].tag == tag)
16436 {
16437 attr = &arm_attr_public_tags[i];
16438 break;
16439 }
16440 }
16441
16442 if (attr)
16443 {
16444 printf (" Tag_%s: ", attr->name);
16445 switch (attr->type)
16446 {
16447 case 0:
16448 switch (tag)
16449 {
16450 case 7: /* Tag_CPU_arch_profile. */
cd30bcef 16451 READ_ULEB (val, p, end);
11c1ff18
PB
16452 switch (val)
16453 {
2b692964
NC
16454 case 0: printf (_("None\n")); break;
16455 case 'A': printf (_("Application\n")); break;
16456 case 'R': printf (_("Realtime\n")); break;
16457 case 'M': printf (_("Microcontroller\n")); break;
16458 case 'S': printf (_("Application or Realtime\n")); break;
11c1ff18
PB
16459 default: printf ("??? (%d)\n", val); break;
16460 }
16461 break;
16462
75375b3e 16463 case 24: /* Tag_align_needed. */
cd30bcef 16464 READ_ULEB (val, p, end);
75375b3e
MGD
16465 switch (val)
16466 {
2b692964
NC
16467 case 0: printf (_("None\n")); break;
16468 case 1: printf (_("8-byte\n")); break;
16469 case 2: printf (_("4-byte\n")); break;
75375b3e
MGD
16470 case 3: printf ("??? 3\n"); break;
16471 default:
16472 if (val <= 12)
dd24e3da 16473 printf (_("8-byte and up to %d-byte extended\n"),
75375b3e
MGD
16474 1 << val);
16475 else
16476 printf ("??? (%d)\n", val);
16477 break;
16478 }
16479 break;
16480
16481 case 25: /* Tag_align_preserved. */
cd30bcef 16482 READ_ULEB (val, p, end);
75375b3e
MGD
16483 switch (val)
16484 {
2b692964
NC
16485 case 0: printf (_("None\n")); break;
16486 case 1: printf (_("8-byte, except leaf SP\n")); break;
16487 case 2: printf (_("8-byte\n")); break;
75375b3e
MGD
16488 case 3: printf ("??? 3\n"); break;
16489 default:
16490 if (val <= 12)
dd24e3da 16491 printf (_("8-byte and up to %d-byte extended\n"),
75375b3e
MGD
16492 1 << val);
16493 else
16494 printf ("??? (%d)\n", val);
16495 break;
16496 }
16497 break;
16498
11c1ff18 16499 case 32: /* Tag_compatibility. */
071436c6 16500 {
cd30bcef 16501 READ_ULEB (val, p, end);
071436c6 16502 printf (_("flag = %d, vendor = "), val);
4082ef84
NC
16503 if (p < end - 1)
16504 {
16505 size_t maxlen = (end - p) - 1;
16506
16507 print_symbol ((int) maxlen, (const char *) p);
16508 p += strnlen ((char *) p, maxlen) + 1;
16509 }
16510 else
16511 {
16512 printf (_("<corrupt>"));
16513 p = (unsigned char *) end;
16514 }
071436c6 16515 putchar ('\n');
071436c6 16516 }
11c1ff18
PB
16517 break;
16518
f5f53991 16519 case 64: /* Tag_nodefaults. */
541a3cbd
NC
16520 /* PR 17531: file: 001-505008-0.01. */
16521 if (p < end)
16522 p++;
2b692964 16523 printf (_("True\n"));
f5f53991
AS
16524 break;
16525
16526 case 65: /* Tag_also_compatible_with. */
cd30bcef 16527 READ_ULEB (val, p, end);
f5f53991
AS
16528 if (val == 6 /* Tag_CPU_arch. */)
16529 {
cd30bcef 16530 READ_ULEB (val, p, end);
071436c6 16531 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
f5f53991
AS
16532 printf ("??? (%d)\n", val);
16533 else
16534 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
16535 }
16536 else
16537 printf ("???\n");
071436c6
NC
16538 while (p < end && *(p++) != '\0' /* NUL terminator. */)
16539 ;
f5f53991
AS
16540 break;
16541
11c1ff18 16542 default:
bee0ee85
NC
16543 printf (_("<unknown: %d>\n"), tag);
16544 break;
11c1ff18
PB
16545 }
16546 return p;
16547
16548 case 1:
f6f0e17b 16549 return display_tag_value (-1, p, end);
11c1ff18 16550 case 2:
f6f0e17b 16551 return display_tag_value (0, p, end);
11c1ff18
PB
16552
16553 default:
16554 assert (attr->type & 0x80);
cd30bcef 16555 READ_ULEB (val, p, end);
11c1ff18
PB
16556 type = attr->type & 0x7f;
16557 if (val >= type)
16558 printf ("??? (%d)\n", val);
16559 else
16560 printf ("%s\n", attr->table[val]);
16561 return p;
16562 }
16563 }
11c1ff18 16564
f6f0e17b 16565 return display_tag_value (tag, p, end);
11c1ff18
PB
16566}
16567
104d59d1 16568static unsigned char *
60bca95a 16569display_gnu_attribute (unsigned char * p,
60abdbed 16570 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
f6f0e17b 16571 const unsigned char * const end)
104d59d1 16572{
cd30bcef 16573 unsigned int tag;
60abdbed 16574 unsigned int val;
104d59d1 16575
cd30bcef 16576 READ_ULEB (tag, p, end);
104d59d1
JM
16577
16578 /* Tag_compatibility is the only generic GNU attribute defined at
16579 present. */
16580 if (tag == 32)
16581 {
cd30bcef 16582 READ_ULEB (val, p, end);
071436c6
NC
16583
16584 printf (_("flag = %d, vendor = "), val);
f6f0e17b
NC
16585 if (p == end)
16586 {
071436c6 16587 printf (_("<corrupt>\n"));
f6f0e17b
NC
16588 warn (_("corrupt vendor attribute\n"));
16589 }
16590 else
16591 {
4082ef84
NC
16592 if (p < end - 1)
16593 {
16594 size_t maxlen = (end - p) - 1;
071436c6 16595
4082ef84
NC
16596 print_symbol ((int) maxlen, (const char *) p);
16597 p += strnlen ((char *) p, maxlen) + 1;
16598 }
16599 else
16600 {
16601 printf (_("<corrupt>"));
16602 p = (unsigned char *) end;
16603 }
071436c6 16604 putchar ('\n');
f6f0e17b 16605 }
104d59d1
JM
16606 return p;
16607 }
16608
16609 if ((tag & 2) == 0 && display_proc_gnu_attribute)
f6f0e17b 16610 return display_proc_gnu_attribute (p, tag, end);
104d59d1 16611
f6f0e17b 16612 return display_tag_value (tag, p, end);
104d59d1
JM
16613}
16614
85f7484a
PB
16615static unsigned char *
16616display_m68k_gnu_attribute (unsigned char * p,
16617 unsigned int tag,
16618 const unsigned char * const end)
16619{
16620 unsigned int val;
16621
16622 if (tag == Tag_GNU_M68K_ABI_FP)
16623 {
16624 printf (" Tag_GNU_M68K_ABI_FP: ");
16625 if (p == end)
16626 {
16627 printf (_("<corrupt>\n"));
16628 return p;
16629 }
16630 READ_ULEB (val, p, end);
16631
16632 if (val > 3)
16633 printf ("(%#x), ", val);
16634
16635 switch (val & 3)
16636 {
16637 case 0:
16638 printf (_("unspecified hard/soft float\n"));
16639 break;
16640 case 1:
16641 printf (_("hard float\n"));
16642 break;
16643 case 2:
16644 printf (_("soft float\n"));
16645 break;
16646 }
16647 return p;
16648 }
16649
16650 return display_tag_value (tag & 1, p, end);
16651}
16652
34c8bcba 16653static unsigned char *
f6f0e17b 16654display_power_gnu_attribute (unsigned char * p,
60abdbed 16655 unsigned int tag,
f6f0e17b 16656 const unsigned char * const end)
34c8bcba 16657{
005d79fd 16658 unsigned int val;
34c8bcba
JM
16659
16660 if (tag == Tag_GNU_Power_ABI_FP)
16661 {
34c8bcba 16662 printf (" Tag_GNU_Power_ABI_FP: ");
cd30bcef 16663 if (p == end)
005d79fd
AM
16664 {
16665 printf (_("<corrupt>\n"));
16666 return p;
16667 }
cd30bcef 16668 READ_ULEB (val, p, end);
60bca95a 16669
005d79fd
AM
16670 if (val > 15)
16671 printf ("(%#x), ", val);
16672
16673 switch (val & 3)
34c8bcba
JM
16674 {
16675 case 0:
005d79fd 16676 printf (_("unspecified hard/soft float, "));
34c8bcba
JM
16677 break;
16678 case 1:
005d79fd 16679 printf (_("hard float, "));
34c8bcba
JM
16680 break;
16681 case 2:
005d79fd 16682 printf (_("soft float, "));
34c8bcba 16683 break;
3c7b9897 16684 case 3:
005d79fd 16685 printf (_("single-precision hard float, "));
3c7b9897 16686 break;
005d79fd
AM
16687 }
16688
16689 switch (val & 0xC)
16690 {
16691 case 0:
16692 printf (_("unspecified long double\n"));
16693 break;
16694 case 4:
16695 printf (_("128-bit IBM long double\n"));
16696 break;
16697 case 8:
16698 printf (_("64-bit long double\n"));
16699 break;
16700 case 12:
16701 printf (_("128-bit IEEE long double\n"));
34c8bcba
JM
16702 break;
16703 }
16704 return p;
005d79fd 16705 }
34c8bcba 16706
c6e65352
DJ
16707 if (tag == Tag_GNU_Power_ABI_Vector)
16708 {
c6e65352 16709 printf (" Tag_GNU_Power_ABI_Vector: ");
cd30bcef 16710 if (p == end)
005d79fd
AM
16711 {
16712 printf (_("<corrupt>\n"));
16713 return p;
16714 }
cd30bcef 16715 READ_ULEB (val, p, end);
005d79fd
AM
16716
16717 if (val > 3)
16718 printf ("(%#x), ", val);
16719
16720 switch (val & 3)
c6e65352
DJ
16721 {
16722 case 0:
005d79fd 16723 printf (_("unspecified\n"));
c6e65352
DJ
16724 break;
16725 case 1:
005d79fd 16726 printf (_("generic\n"));
c6e65352
DJ
16727 break;
16728 case 2:
16729 printf ("AltiVec\n");
16730 break;
16731 case 3:
16732 printf ("SPE\n");
16733 break;
c6e65352
DJ
16734 }
16735 return p;
005d79fd 16736 }
c6e65352 16737
f82e0623
NF
16738 if (tag == Tag_GNU_Power_ABI_Struct_Return)
16739 {
005d79fd 16740 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
cd30bcef 16741 if (p == end)
f6f0e17b 16742 {
005d79fd 16743 printf (_("<corrupt>\n"));
f6f0e17b
NC
16744 return p;
16745 }
cd30bcef 16746 READ_ULEB (val, p, end);
0b4362b0 16747
005d79fd
AM
16748 if (val > 2)
16749 printf ("(%#x), ", val);
16750
16751 switch (val & 3)
16752 {
16753 case 0:
16754 printf (_("unspecified\n"));
16755 break;
16756 case 1:
16757 printf ("r3/r4\n");
16758 break;
16759 case 2:
16760 printf (_("memory\n"));
16761 break;
16762 case 3:
16763 printf ("???\n");
16764 break;
16765 }
f82e0623
NF
16766 return p;
16767 }
16768
f6f0e17b 16769 return display_tag_value (tag & 1, p, end);
34c8bcba
JM
16770}
16771
643f7afb
AK
16772static unsigned char *
16773display_s390_gnu_attribute (unsigned char * p,
60abdbed 16774 unsigned int tag,
643f7afb
AK
16775 const unsigned char * const end)
16776{
cd30bcef 16777 unsigned int val;
643f7afb
AK
16778
16779 if (tag == Tag_GNU_S390_ABI_Vector)
16780 {
643f7afb 16781 printf (" Tag_GNU_S390_ABI_Vector: ");
cd30bcef 16782 READ_ULEB (val, p, end);
643f7afb
AK
16783
16784 switch (val)
16785 {
16786 case 0:
16787 printf (_("any\n"));
16788 break;
16789 case 1:
16790 printf (_("software\n"));
16791 break;
16792 case 2:
16793 printf (_("hardware\n"));
16794 break;
16795 default:
16796 printf ("??? (%d)\n", val);
16797 break;
16798 }
16799 return p;
16800 }
16801
16802 return display_tag_value (tag & 1, p, end);
16803}
16804
9e8c70f9 16805static void
60abdbed 16806display_sparc_hwcaps (unsigned int mask)
9e8c70f9
DM
16807{
16808 if (mask)
16809 {
015dc7e1 16810 bool first = true;
071436c6 16811
9e8c70f9 16812 if (mask & ELF_SPARC_HWCAP_MUL32)
015dc7e1 16813 fputs ("mul32", stdout), first = false;
9e8c70f9 16814 if (mask & ELF_SPARC_HWCAP_DIV32)
015dc7e1 16815 printf ("%sdiv32", first ? "" : "|"), first = false;
9e8c70f9 16816 if (mask & ELF_SPARC_HWCAP_FSMULD)
015dc7e1 16817 printf ("%sfsmuld", first ? "" : "|"), first = false;
9e8c70f9 16818 if (mask & ELF_SPARC_HWCAP_V8PLUS)
015dc7e1 16819 printf ("%sv8plus", first ? "" : "|"), first = false;
9e8c70f9 16820 if (mask & ELF_SPARC_HWCAP_POPC)
015dc7e1 16821 printf ("%spopc", first ? "" : "|"), first = false;
9e8c70f9 16822 if (mask & ELF_SPARC_HWCAP_VIS)
015dc7e1 16823 printf ("%svis", first ? "" : "|"), first = false;
9e8c70f9 16824 if (mask & ELF_SPARC_HWCAP_VIS2)
015dc7e1 16825 printf ("%svis2", first ? "" : "|"), first = false;
9e8c70f9 16826 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
015dc7e1 16827 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
9e8c70f9 16828 if (mask & ELF_SPARC_HWCAP_FMAF)
015dc7e1 16829 printf ("%sfmaf", first ? "" : "|"), first = false;
9e8c70f9 16830 if (mask & ELF_SPARC_HWCAP_VIS3)
015dc7e1 16831 printf ("%svis3", first ? "" : "|"), first = false;
9e8c70f9 16832 if (mask & ELF_SPARC_HWCAP_HPC)
015dc7e1 16833 printf ("%shpc", first ? "" : "|"), first = false;
9e8c70f9 16834 if (mask & ELF_SPARC_HWCAP_RANDOM)
015dc7e1 16835 printf ("%srandom", first ? "" : "|"), first = false;
9e8c70f9 16836 if (mask & ELF_SPARC_HWCAP_TRANS)
015dc7e1 16837 printf ("%strans", first ? "" : "|"), first = false;
9e8c70f9 16838 if (mask & ELF_SPARC_HWCAP_FJFMAU)
015dc7e1 16839 printf ("%sfjfmau", first ? "" : "|"), first = false;
9e8c70f9 16840 if (mask & ELF_SPARC_HWCAP_IMA)
015dc7e1 16841 printf ("%sima", first ? "" : "|"), first = false;
9e8c70f9 16842 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
015dc7e1 16843 printf ("%scspare", first ? "" : "|"), first = false;
9e8c70f9
DM
16844 }
16845 else
071436c6
NC
16846 fputc ('0', stdout);
16847 fputc ('\n', stdout);
9e8c70f9
DM
16848}
16849
3d68f91c 16850static void
60abdbed 16851display_sparc_hwcaps2 (unsigned int mask)
3d68f91c
JM
16852{
16853 if (mask)
16854 {
015dc7e1 16855 bool first = true;
071436c6 16856
3d68f91c 16857 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
015dc7e1 16858 fputs ("fjathplus", stdout), first = false;
3d68f91c 16859 if (mask & ELF_SPARC_HWCAP2_VIS3B)
015dc7e1 16860 printf ("%svis3b", first ? "" : "|"), first = false;
3d68f91c 16861 if (mask & ELF_SPARC_HWCAP2_ADP)
015dc7e1 16862 printf ("%sadp", first ? "" : "|"), first = false;
3d68f91c 16863 if (mask & ELF_SPARC_HWCAP2_SPARC5)
015dc7e1 16864 printf ("%ssparc5", first ? "" : "|"), first = false;
3d68f91c 16865 if (mask & ELF_SPARC_HWCAP2_MWAIT)
015dc7e1 16866 printf ("%smwait", first ? "" : "|"), first = false;
3d68f91c 16867 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
015dc7e1 16868 printf ("%sxmpmul", first ? "" : "|"), first = false;
3d68f91c 16869 if (mask & ELF_SPARC_HWCAP2_XMONT)
015dc7e1 16870 printf ("%sxmont2", first ? "" : "|"), first = false;
3d68f91c 16871 if (mask & ELF_SPARC_HWCAP2_NSEC)
015dc7e1 16872 printf ("%snsec", first ? "" : "|"), first = false;
3d68f91c 16873 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
015dc7e1 16874 printf ("%sfjathhpc", first ? "" : "|"), first = false;
3d68f91c 16875 if (mask & ELF_SPARC_HWCAP2_FJDES)
015dc7e1 16876 printf ("%sfjdes", first ? "" : "|"), first = false;
3d68f91c 16877 if (mask & ELF_SPARC_HWCAP2_FJAES)
015dc7e1 16878 printf ("%sfjaes", first ? "" : "|"), first = false;
3d68f91c
JM
16879 }
16880 else
071436c6
NC
16881 fputc ('0', stdout);
16882 fputc ('\n', stdout);
3d68f91c
JM
16883}
16884
9e8c70f9 16885static unsigned char *
f6f0e17b 16886display_sparc_gnu_attribute (unsigned char * p,
60abdbed 16887 unsigned int tag,
f6f0e17b 16888 const unsigned char * const end)
9e8c70f9 16889{
cd30bcef 16890 unsigned int val;
3d68f91c 16891
9e8c70f9
DM
16892 if (tag == Tag_GNU_Sparc_HWCAPS)
16893 {
cd30bcef 16894 READ_ULEB (val, p, end);
9e8c70f9 16895 printf (" Tag_GNU_Sparc_HWCAPS: ");
9e8c70f9
DM
16896 display_sparc_hwcaps (val);
16897 return p;
3d68f91c
JM
16898 }
16899 if (tag == Tag_GNU_Sparc_HWCAPS2)
16900 {
cd30bcef 16901 READ_ULEB (val, p, end);
3d68f91c
JM
16902 printf (" Tag_GNU_Sparc_HWCAPS2: ");
16903 display_sparc_hwcaps2 (val);
16904 return p;
16905 }
9e8c70f9 16906
f6f0e17b 16907 return display_tag_value (tag, p, end);
9e8c70f9
DM
16908}
16909
351cdf24 16910static void
32ec8896 16911print_mips_fp_abi_value (unsigned int val)
351cdf24
MF
16912{
16913 switch (val)
16914 {
16915 case Val_GNU_MIPS_ABI_FP_ANY:
16916 printf (_("Hard or soft float\n"));
16917 break;
16918 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16919 printf (_("Hard float (double precision)\n"));
16920 break;
16921 case Val_GNU_MIPS_ABI_FP_SINGLE:
16922 printf (_("Hard float (single precision)\n"));
16923 break;
16924 case Val_GNU_MIPS_ABI_FP_SOFT:
16925 printf (_("Soft float\n"));
16926 break;
16927 case Val_GNU_MIPS_ABI_FP_OLD_64:
16928 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16929 break;
16930 case Val_GNU_MIPS_ABI_FP_XX:
16931 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
16932 break;
16933 case Val_GNU_MIPS_ABI_FP_64:
16934 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
16935 break;
16936 case Val_GNU_MIPS_ABI_FP_64A:
16937 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16938 break;
3350cc01
CM
16939 case Val_GNU_MIPS_ABI_FP_NAN2008:
16940 printf (_("NaN 2008 compatibility\n"));
16941 break;
351cdf24
MF
16942 default:
16943 printf ("??? (%d)\n", val);
16944 break;
16945 }
16946}
16947
2cf19d5c 16948static unsigned char *
f6f0e17b 16949display_mips_gnu_attribute (unsigned char * p,
60abdbed 16950 unsigned int tag,
f6f0e17b 16951 const unsigned char * const end)
2cf19d5c 16952{
2cf19d5c
JM
16953 if (tag == Tag_GNU_MIPS_ABI_FP)
16954 {
32ec8896 16955 unsigned int val;
f6f0e17b 16956
2cf19d5c 16957 printf (" Tag_GNU_MIPS_ABI_FP: ");
cd30bcef 16958 READ_ULEB (val, p, end);
351cdf24 16959 print_mips_fp_abi_value (val);
2cf19d5c
JM
16960 return p;
16961 }
16962
a9f58168
CF
16963 if (tag == Tag_GNU_MIPS_ABI_MSA)
16964 {
32ec8896 16965 unsigned int val;
a9f58168 16966
a9f58168 16967 printf (" Tag_GNU_MIPS_ABI_MSA: ");
cd30bcef 16968 READ_ULEB (val, p, end);
a9f58168
CF
16969
16970 switch (val)
16971 {
16972 case Val_GNU_MIPS_ABI_MSA_ANY:
16973 printf (_("Any MSA or not\n"));
16974 break;
16975 case Val_GNU_MIPS_ABI_MSA_128:
16976 printf (_("128-bit MSA\n"));
16977 break;
16978 default:
16979 printf ("??? (%d)\n", val);
16980 break;
16981 }
16982 return p;
16983 }
16984
f6f0e17b 16985 return display_tag_value (tag & 1, p, end);
2cf19d5c
JM
16986}
16987
59e6276b 16988static unsigned char *
f6f0e17b
NC
16989display_tic6x_attribute (unsigned char * p,
16990 const unsigned char * const end)
59e6276b 16991{
60abdbed 16992 unsigned int tag;
cd30bcef 16993 unsigned int val;
59e6276b 16994
cd30bcef 16995 READ_ULEB (tag, p, end);
59e6276b
JM
16996
16997 switch (tag)
16998 {
75fa6dc1 16999 case Tag_ISA:
75fa6dc1 17000 printf (" Tag_ISA: ");
cd30bcef 17001 READ_ULEB (val, p, end);
59e6276b
JM
17002
17003 switch (val)
17004 {
75fa6dc1 17005 case C6XABI_Tag_ISA_none:
59e6276b
JM
17006 printf (_("None\n"));
17007 break;
75fa6dc1 17008 case C6XABI_Tag_ISA_C62X:
59e6276b
JM
17009 printf ("C62x\n");
17010 break;
75fa6dc1 17011 case C6XABI_Tag_ISA_C67X:
59e6276b
JM
17012 printf ("C67x\n");
17013 break;
75fa6dc1 17014 case C6XABI_Tag_ISA_C67XP:
59e6276b
JM
17015 printf ("C67x+\n");
17016 break;
75fa6dc1 17017 case C6XABI_Tag_ISA_C64X:
59e6276b
JM
17018 printf ("C64x\n");
17019 break;
75fa6dc1 17020 case C6XABI_Tag_ISA_C64XP:
59e6276b
JM
17021 printf ("C64x+\n");
17022 break;
75fa6dc1 17023 case C6XABI_Tag_ISA_C674X:
59e6276b
JM
17024 printf ("C674x\n");
17025 break;
17026 default:
17027 printf ("??? (%d)\n", val);
17028 break;
17029 }
17030 return p;
17031
87779176 17032 case Tag_ABI_wchar_t:
87779176 17033 printf (" Tag_ABI_wchar_t: ");
cd30bcef 17034 READ_ULEB (val, p, end);
87779176
JM
17035 switch (val)
17036 {
17037 case 0:
17038 printf (_("Not used\n"));
17039 break;
17040 case 1:
17041 printf (_("2 bytes\n"));
17042 break;
17043 case 2:
17044 printf (_("4 bytes\n"));
17045 break;
17046 default:
17047 printf ("??? (%d)\n", val);
17048 break;
17049 }
17050 return p;
17051
17052 case Tag_ABI_stack_align_needed:
87779176 17053 printf (" Tag_ABI_stack_align_needed: ");
cd30bcef 17054 READ_ULEB (val, p, end);
87779176
JM
17055 switch (val)
17056 {
17057 case 0:
17058 printf (_("8-byte\n"));
17059 break;
17060 case 1:
17061 printf (_("16-byte\n"));
17062 break;
17063 default:
17064 printf ("??? (%d)\n", val);
17065 break;
17066 }
17067 return p;
17068
17069 case Tag_ABI_stack_align_preserved:
cd30bcef 17070 READ_ULEB (val, p, end);
87779176
JM
17071 printf (" Tag_ABI_stack_align_preserved: ");
17072 switch (val)
17073 {
17074 case 0:
17075 printf (_("8-byte\n"));
17076 break;
17077 case 1:
17078 printf (_("16-byte\n"));
17079 break;
17080 default:
17081 printf ("??? (%d)\n", val);
17082 break;
17083 }
17084 return p;
17085
b5593623 17086 case Tag_ABI_DSBT:
cd30bcef 17087 READ_ULEB (val, p, end);
b5593623
JM
17088 printf (" Tag_ABI_DSBT: ");
17089 switch (val)
17090 {
17091 case 0:
17092 printf (_("DSBT addressing not used\n"));
17093 break;
17094 case 1:
17095 printf (_("DSBT addressing used\n"));
17096 break;
17097 default:
17098 printf ("??? (%d)\n", val);
17099 break;
17100 }
17101 return p;
17102
87779176 17103 case Tag_ABI_PID:
cd30bcef 17104 READ_ULEB (val, p, end);
87779176
JM
17105 printf (" Tag_ABI_PID: ");
17106 switch (val)
17107 {
17108 case 0:
17109 printf (_("Data addressing position-dependent\n"));
17110 break;
17111 case 1:
17112 printf (_("Data addressing position-independent, GOT near DP\n"));
17113 break;
17114 case 2:
17115 printf (_("Data addressing position-independent, GOT far from DP\n"));
17116 break;
17117 default:
17118 printf ("??? (%d)\n", val);
17119 break;
17120 }
17121 return p;
17122
17123 case Tag_ABI_PIC:
cd30bcef 17124 READ_ULEB (val, p, end);
87779176
JM
17125 printf (" Tag_ABI_PIC: ");
17126 switch (val)
17127 {
17128 case 0:
17129 printf (_("Code addressing position-dependent\n"));
17130 break;
17131 case 1:
17132 printf (_("Code addressing position-independent\n"));
17133 break;
17134 default:
17135 printf ("??? (%d)\n", val);
17136 break;
17137 }
17138 return p;
17139
17140 case Tag_ABI_array_object_alignment:
cd30bcef 17141 READ_ULEB (val, p, end);
87779176
JM
17142 printf (" Tag_ABI_array_object_alignment: ");
17143 switch (val)
17144 {
17145 case 0:
17146 printf (_("8-byte\n"));
17147 break;
17148 case 1:
17149 printf (_("4-byte\n"));
17150 break;
17151 case 2:
17152 printf (_("16-byte\n"));
17153 break;
17154 default:
17155 printf ("??? (%d)\n", val);
17156 break;
17157 }
17158 return p;
17159
17160 case Tag_ABI_array_object_align_expected:
cd30bcef 17161 READ_ULEB (val, p, end);
87779176
JM
17162 printf (" Tag_ABI_array_object_align_expected: ");
17163 switch (val)
17164 {
17165 case 0:
17166 printf (_("8-byte\n"));
17167 break;
17168 case 1:
17169 printf (_("4-byte\n"));
17170 break;
17171 case 2:
17172 printf (_("16-byte\n"));
17173 break;
17174 default:
17175 printf ("??? (%d)\n", val);
17176 break;
17177 }
17178 return p;
17179
3cbd1c06 17180 case Tag_ABI_compatibility:
071436c6 17181 {
cd30bcef 17182 READ_ULEB (val, p, end);
071436c6 17183 printf (" Tag_ABI_compatibility: ");
071436c6 17184 printf (_("flag = %d, vendor = "), val);
4082ef84
NC
17185 if (p < end - 1)
17186 {
17187 size_t maxlen = (end - p) - 1;
17188
17189 print_symbol ((int) maxlen, (const char *) p);
17190 p += strnlen ((char *) p, maxlen) + 1;
17191 }
17192 else
17193 {
17194 printf (_("<corrupt>"));
17195 p = (unsigned char *) end;
17196 }
071436c6 17197 putchar ('\n');
071436c6
NC
17198 return p;
17199 }
87779176
JM
17200
17201 case Tag_ABI_conformance:
071436c6 17202 {
4082ef84
NC
17203 printf (" Tag_ABI_conformance: \"");
17204 if (p < end - 1)
17205 {
17206 size_t maxlen = (end - p) - 1;
071436c6 17207
4082ef84
NC
17208 print_symbol ((int) maxlen, (const char *) p);
17209 p += strnlen ((char *) p, maxlen) + 1;
17210 }
17211 else
17212 {
17213 printf (_("<corrupt>"));
17214 p = (unsigned char *) end;
17215 }
071436c6 17216 printf ("\"\n");
071436c6
NC
17217 return p;
17218 }
59e6276b
JM
17219 }
17220
f6f0e17b
NC
17221 return display_tag_value (tag, p, end);
17222}
59e6276b 17223
f6f0e17b 17224static void
60abdbed 17225display_raw_attribute (unsigned char * p, unsigned char const * const end)
f6f0e17b
NC
17226{
17227 unsigned long addr = 0;
17228 size_t bytes = end - p;
17229
feceaa59 17230 assert (end >= p);
f6f0e17b 17231 while (bytes)
87779176 17232 {
f6f0e17b
NC
17233 int j;
17234 int k;
17235 int lbytes = (bytes > 16 ? 16 : bytes);
17236
17237 printf (" 0x%8.8lx ", addr);
17238
17239 for (j = 0; j < 16; j++)
17240 {
17241 if (j < lbytes)
17242 printf ("%2.2x", p[j]);
17243 else
17244 printf (" ");
17245
17246 if ((j & 3) == 3)
17247 printf (" ");
17248 }
17249
17250 for (j = 0; j < lbytes; j++)
17251 {
17252 k = p[j];
17253 if (k >= ' ' && k < 0x7f)
17254 printf ("%c", k);
17255 else
17256 printf (".");
17257 }
17258
17259 putchar ('\n');
17260
17261 p += lbytes;
17262 bytes -= lbytes;
17263 addr += lbytes;
87779176 17264 }
59e6276b 17265
f6f0e17b 17266 putchar ('\n');
59e6276b
JM
17267}
17268
13761a11 17269static unsigned char *
b0191216 17270display_msp430_attribute (unsigned char * p,
13761a11
NC
17271 const unsigned char * const end)
17272{
60abdbed
NC
17273 unsigned int val;
17274 unsigned int tag;
13761a11 17275
cd30bcef 17276 READ_ULEB (tag, p, end);
0b4362b0 17277
13761a11
NC
17278 switch (tag)
17279 {
17280 case OFBA_MSPABI_Tag_ISA:
13761a11 17281 printf (" Tag_ISA: ");
cd30bcef 17282 READ_ULEB (val, p, end);
13761a11
NC
17283 switch (val)
17284 {
17285 case 0: printf (_("None\n")); break;
17286 case 1: printf (_("MSP430\n")); break;
17287 case 2: printf (_("MSP430X\n")); break;
17288 default: printf ("??? (%d)\n", val); break;
17289 }
17290 break;
17291
17292 case OFBA_MSPABI_Tag_Code_Model:
13761a11 17293 printf (" Tag_Code_Model: ");
cd30bcef 17294 READ_ULEB (val, p, end);
13761a11
NC
17295 switch (val)
17296 {
17297 case 0: printf (_("None\n")); break;
17298 case 1: printf (_("Small\n")); break;
17299 case 2: printf (_("Large\n")); break;
17300 default: printf ("??? (%d)\n", val); break;
17301 }
17302 break;
17303
17304 case OFBA_MSPABI_Tag_Data_Model:
13761a11 17305 printf (" Tag_Data_Model: ");
cd30bcef 17306 READ_ULEB (val, p, end);
13761a11
NC
17307 switch (val)
17308 {
17309 case 0: printf (_("None\n")); break;
17310 case 1: printf (_("Small\n")); break;
17311 case 2: printf (_("Large\n")); break;
17312 case 3: printf (_("Restricted Large\n")); break;
17313 default: printf ("??? (%d)\n", val); break;
17314 }
17315 break;
17316
17317 default:
17318 printf (_(" <unknown tag %d>: "), tag);
17319
17320 if (tag & 1)
17321 {
071436c6 17322 putchar ('"');
4082ef84
NC
17323 if (p < end - 1)
17324 {
17325 size_t maxlen = (end - p) - 1;
17326
17327 print_symbol ((int) maxlen, (const char *) p);
17328 p += strnlen ((char *) p, maxlen) + 1;
17329 }
17330 else
17331 {
17332 printf (_("<corrupt>"));
17333 p = (unsigned char *) end;
17334 }
071436c6 17335 printf ("\"\n");
13761a11
NC
17336 }
17337 else
17338 {
cd30bcef 17339 READ_ULEB (val, p, end);
13761a11
NC
17340 printf ("%d (0x%x)\n", val, val);
17341 }
17342 break;
17343 }
17344
4082ef84 17345 assert (p <= end);
13761a11
NC
17346 return p;
17347}
17348
c0ea7c52
JL
17349static unsigned char *
17350display_msp430_gnu_attribute (unsigned char * p,
17351 unsigned int tag,
17352 const unsigned char * const end)
17353{
17354 if (tag == Tag_GNU_MSP430_Data_Region)
17355 {
cd30bcef 17356 unsigned int val;
c0ea7c52 17357
c0ea7c52 17358 printf (" Tag_GNU_MSP430_Data_Region: ");
cd30bcef 17359 READ_ULEB (val, p, end);
c0ea7c52
JL
17360
17361 switch (val)
17362 {
17363 case Val_GNU_MSP430_Data_Region_Any:
17364 printf (_("Any Region\n"));
17365 break;
17366 case Val_GNU_MSP430_Data_Region_Lower:
17367 printf (_("Lower Region Only\n"));
17368 break;
17369 default:
cd30bcef 17370 printf ("??? (%u)\n", val);
c0ea7c52
JL
17371 }
17372 return p;
17373 }
17374 return display_tag_value (tag & 1, p, end);
17375}
17376
2dc8dd17
JW
17377struct riscv_attr_tag_t {
17378 const char *name;
cd30bcef 17379 unsigned int tag;
2dc8dd17
JW
17380};
17381
17382static struct riscv_attr_tag_t riscv_attr_tag[] =
17383{
17384#define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
17385 T(arch),
17386 T(priv_spec),
17387 T(priv_spec_minor),
17388 T(priv_spec_revision),
17389 T(unaligned_access),
17390 T(stack_align),
17391#undef T
17392};
17393
17394static unsigned char *
17395display_riscv_attribute (unsigned char *p,
17396 const unsigned char * const end)
17397{
cd30bcef
AM
17398 unsigned int val;
17399 unsigned int tag;
2dc8dd17
JW
17400 struct riscv_attr_tag_t *attr = NULL;
17401 unsigned i;
17402
cd30bcef 17403 READ_ULEB (tag, p, end);
2dc8dd17
JW
17404
17405 /* Find the name of attribute. */
17406 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
17407 {
17408 if (riscv_attr_tag[i].tag == tag)
17409 {
17410 attr = &riscv_attr_tag[i];
17411 break;
17412 }
17413 }
17414
17415 if (attr)
17416 printf (" %s: ", attr->name);
17417 else
17418 return display_tag_value (tag, p, end);
17419
17420 switch (tag)
17421 {
17422 case Tag_RISCV_priv_spec:
17423 case Tag_RISCV_priv_spec_minor:
17424 case Tag_RISCV_priv_spec_revision:
cd30bcef
AM
17425 READ_ULEB (val, p, end);
17426 printf (_("%u\n"), val);
2dc8dd17
JW
17427 break;
17428 case Tag_RISCV_unaligned_access:
cd30bcef 17429 READ_ULEB (val, p, end);
2dc8dd17
JW
17430 switch (val)
17431 {
17432 case 0:
17433 printf (_("No unaligned access\n"));
17434 break;
17435 case 1:
17436 printf (_("Unaligned access\n"));
17437 break;
17438 }
17439 break;
17440 case Tag_RISCV_stack_align:
cd30bcef
AM
17441 READ_ULEB (val, p, end);
17442 printf (_("%u-bytes\n"), val);
2dc8dd17
JW
17443 break;
17444 case Tag_RISCV_arch:
17445 p = display_tag_value (-1, p, end);
17446 break;
17447 default:
17448 return display_tag_value (tag, p, end);
17449 }
17450
17451 return p;
17452}
17453
0861f561
CQ
17454static unsigned char *
17455display_csky_attribute (unsigned char * p,
17456 const unsigned char * const end)
17457{
17458 unsigned int tag;
17459 unsigned int val;
17460 READ_ULEB (tag, p, end);
17461
17462 if (tag >= Tag_CSKY_MAX)
17463 {
17464 return display_tag_value (-1, p, end);
17465 }
17466
17467 switch (tag)
17468 {
17469 case Tag_CSKY_ARCH_NAME:
17470 printf (" Tag_CSKY_ARCH_NAME:\t\t");
17471 return display_tag_value (-1, p, end);
17472 case Tag_CSKY_CPU_NAME:
17473 printf (" Tag_CSKY_CPU_NAME:\t\t");
17474 return display_tag_value (-1, p, end);
17475
17476 case Tag_CSKY_ISA_FLAGS:
17477 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
17478 return display_tag_value (0, p, end);
17479 case Tag_CSKY_ISA_EXT_FLAGS:
17480 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
17481 return display_tag_value (0, p, end);
17482
17483 case Tag_CSKY_DSP_VERSION:
17484 printf (" Tag_CSKY_DSP_VERSION:\t\t");
17485 READ_ULEB (val, p, end);
17486 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
17487 printf ("DSP Extension\n");
17488 else if (val == VAL_CSKY_DSP_VERSION_2)
17489 printf ("DSP 2.0\n");
17490 break;
17491
17492 case Tag_CSKY_VDSP_VERSION:
17493 printf (" Tag_CSKY_VDSP_VERSION:\t");
17494 READ_ULEB (val, p, end);
17495 printf ("VDSP Version %d\n", val);
17496 break;
17497
17498 case Tag_CSKY_FPU_VERSION:
17499 printf (" Tag_CSKY_FPU_VERSION:\t\t");
17500 READ_ULEB (val, p, end);
17501 if (val == VAL_CSKY_FPU_VERSION_1)
17502 printf ("ABIV1 FPU Version 1\n");
17503 else if (val == VAL_CSKY_FPU_VERSION_2)
17504 printf ("FPU Version 2\n");
17505 break;
17506
17507 case Tag_CSKY_FPU_ABI:
17508 printf (" Tag_CSKY_FPU_ABI:\t\t");
17509 READ_ULEB (val, p, end);
17510 if (val == VAL_CSKY_FPU_ABI_HARD)
17511 printf ("Hard\n");
17512 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
17513 printf ("SoftFP\n");
17514 else if (val == VAL_CSKY_FPU_ABI_SOFT)
17515 printf ("Soft\n");
17516 break;
17517 case Tag_CSKY_FPU_ROUNDING:
17518 READ_ULEB (val, p, end);
f253158f
NC
17519 if (val == 1)
17520 {
17521 printf (" Tag_CSKY_FPU_ROUNDING:\t");
17522 printf ("Needed\n");
17523 }
0861f561
CQ
17524 break;
17525 case Tag_CSKY_FPU_DENORMAL:
17526 READ_ULEB (val, p, end);
f253158f
NC
17527 if (val == 1)
17528 {
17529 printf (" Tag_CSKY_FPU_DENORMAL:\t");
17530 printf ("Needed\n");
17531 }
0861f561
CQ
17532 break;
17533 case Tag_CSKY_FPU_Exception:
17534 READ_ULEB (val, p, end);
f253158f
NC
17535 if (val == 1)
17536 {
17537 printf (" Tag_CSKY_FPU_Exception:\t");
17538 printf ("Needed\n");
17539 }
0861f561
CQ
17540 break;
17541 case Tag_CSKY_FPU_NUMBER_MODULE:
17542 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
17543 return display_tag_value (-1, p, end);
17544 case Tag_CSKY_FPU_HARDFP:
17545 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
17546 READ_ULEB (val, p, end);
17547 if (val & VAL_CSKY_FPU_HARDFP_HALF)
17548 printf (" Half");
17549 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
17550 printf (" Single");
17551 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
17552 printf (" Double");
17553 printf ("\n");
17554 break;
17555 default:
17556 return display_tag_value (tag, p, end);
17557 }
17558 return p;
17559}
17560
015dc7e1 17561static bool
dda8d76d 17562process_attributes (Filedata * filedata,
60bca95a 17563 const char * public_name,
104d59d1 17564 unsigned int proc_type,
f6f0e17b 17565 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
60abdbed 17566 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
11c1ff18 17567{
2cf0635d 17568 Elf_Internal_Shdr * sect;
11c1ff18 17569 unsigned i;
015dc7e1 17570 bool res = true;
11c1ff18
PB
17571
17572 /* Find the section header so that we get the size. */
dda8d76d
NC
17573 for (i = 0, sect = filedata->section_headers;
17574 i < filedata->file_header.e_shnum;
11c1ff18
PB
17575 i++, sect++)
17576 {
071436c6
NC
17577 unsigned char * contents;
17578 unsigned char * p;
17579
104d59d1 17580 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
11c1ff18
PB
17581 continue;
17582
dda8d76d 17583 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
3f5e193b 17584 sect->sh_size, _("attributes"));
60bca95a 17585 if (contents == NULL)
32ec8896 17586 {
015dc7e1 17587 res = false;
32ec8896
NC
17588 continue;
17589 }
60bca95a 17590
11c1ff18 17591 p = contents;
60abdbed
NC
17592 /* The first character is the version of the attributes.
17593 Currently only version 1, (aka 'A') is recognised here. */
17594 if (*p != 'A')
32ec8896
NC
17595 {
17596 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
015dc7e1 17597 res = false;
32ec8896 17598 }
60abdbed 17599 else
11c1ff18 17600 {
071436c6
NC
17601 bfd_vma section_len;
17602
17603 section_len = sect->sh_size - 1;
11c1ff18 17604 p++;
60bca95a 17605
071436c6 17606 while (section_len > 0)
11c1ff18 17607 {
071436c6 17608 bfd_vma attr_len;
e9847026 17609 unsigned int namelen;
015dc7e1
AM
17610 bool public_section;
17611 bool gnu_section;
11c1ff18 17612
071436c6 17613 if (section_len <= 4)
e0a31db1
NC
17614 {
17615 error (_("Tag section ends prematurely\n"));
015dc7e1 17616 res = false;
e0a31db1
NC
17617 break;
17618 }
071436c6 17619 attr_len = byte_get (p, 4);
11c1ff18 17620 p += 4;
60bca95a 17621
071436c6 17622 if (attr_len > section_len)
11c1ff18 17623 {
071436c6
NC
17624 error (_("Bad attribute length (%u > %u)\n"),
17625 (unsigned) attr_len, (unsigned) section_len);
17626 attr_len = section_len;
015dc7e1 17627 res = false;
11c1ff18 17628 }
74e1a04b 17629 /* PR 17531: file: 001-101425-0.004 */
071436c6 17630 else if (attr_len < 5)
74e1a04b 17631 {
071436c6 17632 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
015dc7e1 17633 res = false;
74e1a04b
NC
17634 break;
17635 }
e9847026 17636
071436c6
NC
17637 section_len -= attr_len;
17638 attr_len -= 4;
17639
17640 namelen = strnlen ((char *) p, attr_len) + 1;
17641 if (namelen == 0 || namelen >= attr_len)
e9847026
NC
17642 {
17643 error (_("Corrupt attribute section name\n"));
015dc7e1 17644 res = false;
e9847026
NC
17645 break;
17646 }
17647
071436c6
NC
17648 printf (_("Attribute Section: "));
17649 print_symbol (INT_MAX, (const char *) p);
17650 putchar ('\n');
60bca95a
NC
17651
17652 if (public_name && streq ((char *) p, public_name))
015dc7e1 17653 public_section = true;
11c1ff18 17654 else
015dc7e1 17655 public_section = false;
60bca95a
NC
17656
17657 if (streq ((char *) p, "gnu"))
015dc7e1 17658 gnu_section = true;
104d59d1 17659 else
015dc7e1 17660 gnu_section = false;
60bca95a 17661
11c1ff18 17662 p += namelen;
071436c6 17663 attr_len -= namelen;
e0a31db1 17664
071436c6 17665 while (attr_len > 0 && p < contents + sect->sh_size)
11c1ff18 17666 {
e0a31db1 17667 int tag;
cd30bcef 17668 unsigned int val;
11c1ff18 17669 bfd_vma size;
071436c6 17670 unsigned char * end;
60bca95a 17671
e0a31db1 17672 /* PR binutils/17531: Safe handling of corrupt files. */
071436c6 17673 if (attr_len < 6)
e0a31db1
NC
17674 {
17675 error (_("Unused bytes at end of section\n"));
015dc7e1 17676 res = false;
e0a31db1
NC
17677 section_len = 0;
17678 break;
17679 }
17680
17681 tag = *(p++);
11c1ff18 17682 size = byte_get (p, 4);
071436c6 17683 if (size > attr_len)
11c1ff18 17684 {
e9847026 17685 error (_("Bad subsection length (%u > %u)\n"),
071436c6 17686 (unsigned) size, (unsigned) attr_len);
015dc7e1 17687 res = false;
071436c6 17688 size = attr_len;
11c1ff18 17689 }
e0a31db1
NC
17690 /* PR binutils/17531: Safe handling of corrupt files. */
17691 if (size < 6)
17692 {
17693 error (_("Bad subsection length (%u < 6)\n"),
17694 (unsigned) size);
015dc7e1 17695 res = false;
e0a31db1
NC
17696 section_len = 0;
17697 break;
17698 }
60bca95a 17699
071436c6 17700 attr_len -= size;
11c1ff18 17701 end = p + size - 1;
071436c6 17702 assert (end <= contents + sect->sh_size);
11c1ff18 17703 p += 4;
60bca95a 17704
11c1ff18
PB
17705 switch (tag)
17706 {
17707 case 1:
2b692964 17708 printf (_("File Attributes\n"));
11c1ff18
PB
17709 break;
17710 case 2:
2b692964 17711 printf (_("Section Attributes:"));
11c1ff18
PB
17712 goto do_numlist;
17713 case 3:
2b692964 17714 printf (_("Symbol Attributes:"));
1a0670f3 17715 /* Fall through. */
11c1ff18
PB
17716 do_numlist:
17717 for (;;)
17718 {
cd30bcef 17719 READ_ULEB (val, p, end);
11c1ff18
PB
17720 if (val == 0)
17721 break;
17722 printf (" %d", val);
17723 }
17724 printf ("\n");
17725 break;
17726 default:
2b692964 17727 printf (_("Unknown tag: %d\n"), tag);
015dc7e1 17728 public_section = false;
11c1ff18
PB
17729 break;
17730 }
60bca95a 17731
071436c6 17732 if (public_section && display_pub_attribute != NULL)
11c1ff18
PB
17733 {
17734 while (p < end)
f6f0e17b 17735 p = display_pub_attribute (p, end);
60abdbed 17736 assert (p == end);
104d59d1 17737 }
071436c6 17738 else if (gnu_section && display_proc_gnu_attribute != NULL)
104d59d1
JM
17739 {
17740 while (p < end)
17741 p = display_gnu_attribute (p,
f6f0e17b
NC
17742 display_proc_gnu_attribute,
17743 end);
60abdbed 17744 assert (p == end);
11c1ff18 17745 }
071436c6 17746 else if (p < end)
11c1ff18 17747 {
071436c6 17748 printf (_(" Unknown attribute:\n"));
f6f0e17b 17749 display_raw_attribute (p, end);
11c1ff18
PB
17750 p = end;
17751 }
071436c6
NC
17752 else
17753 attr_len = 0;
11c1ff18
PB
17754 }
17755 }
17756 }
d70c5fc7 17757
60bca95a 17758 free (contents);
11c1ff18 17759 }
32ec8896
NC
17760
17761 return res;
11c1ff18
PB
17762}
17763
ccb4c951
RS
17764/* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
17765 Print the Address, Access and Initial fields of an entry at VMA ADDR
82b1b41b
NC
17766 and return the VMA of the next entry, or -1 if there was a problem.
17767 Does not read from DATA_END or beyond. */
ccb4c951
RS
17768
17769static bfd_vma
82b1b41b
NC
17770print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
17771 unsigned char * data_end)
ccb4c951
RS
17772{
17773 printf (" ");
17774 print_vma (addr, LONG_HEX);
17775 printf (" ");
17776 if (addr < pltgot + 0xfff0)
17777 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
17778 else
17779 printf ("%10s", "");
17780 printf (" ");
17781 if (data == NULL)
2b692964 17782 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
ccb4c951
RS
17783 else
17784 {
17785 bfd_vma entry;
82b1b41b 17786 unsigned char * from = data + addr - pltgot;
ccb4c951 17787
82b1b41b
NC
17788 if (from + (is_32bit_elf ? 4 : 8) > data_end)
17789 {
17790 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
17791 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
17792 return (bfd_vma) -1;
17793 }
17794 else
17795 {
17796 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17797 print_vma (entry, LONG_HEX);
17798 }
ccb4c951
RS
17799 }
17800 return addr + (is_32bit_elf ? 4 : 8);
17801}
17802
861fb55a
DJ
17803/* DATA points to the contents of a MIPS PLT GOT that starts at VMA
17804 PLTGOT. Print the Address and Initial fields of an entry at VMA
17805 ADDR and return the VMA of the next entry. */
17806
17807static bfd_vma
2cf0635d 17808print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
861fb55a
DJ
17809{
17810 printf (" ");
17811 print_vma (addr, LONG_HEX);
17812 printf (" ");
17813 if (data == NULL)
2b692964 17814 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
861fb55a
DJ
17815 else
17816 {
17817 bfd_vma entry;
17818
17819 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17820 print_vma (entry, LONG_HEX);
17821 }
17822 return addr + (is_32bit_elf ? 4 : 8);
17823}
17824
351cdf24
MF
17825static void
17826print_mips_ases (unsigned int mask)
17827{
17828 if (mask & AFL_ASE_DSP)
17829 fputs ("\n\tDSP ASE", stdout);
17830 if (mask & AFL_ASE_DSPR2)
17831 fputs ("\n\tDSP R2 ASE", stdout);
8f4f9071
MF
17832 if (mask & AFL_ASE_DSPR3)
17833 fputs ("\n\tDSP R3 ASE", stdout);
351cdf24
MF
17834 if (mask & AFL_ASE_EVA)
17835 fputs ("\n\tEnhanced VA Scheme", stdout);
17836 if (mask & AFL_ASE_MCU)
17837 fputs ("\n\tMCU (MicroController) ASE", stdout);
17838 if (mask & AFL_ASE_MDMX)
17839 fputs ("\n\tMDMX ASE", stdout);
17840 if (mask & AFL_ASE_MIPS3D)
17841 fputs ("\n\tMIPS-3D ASE", stdout);
17842 if (mask & AFL_ASE_MT)
17843 fputs ("\n\tMT ASE", stdout);
17844 if (mask & AFL_ASE_SMARTMIPS)
17845 fputs ("\n\tSmartMIPS ASE", stdout);
17846 if (mask & AFL_ASE_VIRT)
17847 fputs ("\n\tVZ ASE", stdout);
17848 if (mask & AFL_ASE_MSA)
17849 fputs ("\n\tMSA ASE", stdout);
17850 if (mask & AFL_ASE_MIPS16)
17851 fputs ("\n\tMIPS16 ASE", stdout);
17852 if (mask & AFL_ASE_MICROMIPS)
17853 fputs ("\n\tMICROMIPS ASE", stdout);
17854 if (mask & AFL_ASE_XPA)
17855 fputs ("\n\tXPA ASE", stdout);
25499ac7
MR
17856 if (mask & AFL_ASE_MIPS16E2)
17857 fputs ("\n\tMIPS16e2 ASE", stdout);
730c3174
SE
17858 if (mask & AFL_ASE_CRC)
17859 fputs ("\n\tCRC ASE", stdout);
6f20c942
FS
17860 if (mask & AFL_ASE_GINV)
17861 fputs ("\n\tGINV ASE", stdout);
8095d2f7
CX
17862 if (mask & AFL_ASE_LOONGSON_MMI)
17863 fputs ("\n\tLoongson MMI ASE", stdout);
716c08de
CX
17864 if (mask & AFL_ASE_LOONGSON_CAM)
17865 fputs ("\n\tLoongson CAM ASE", stdout);
bdc6c06e
CX
17866 if (mask & AFL_ASE_LOONGSON_EXT)
17867 fputs ("\n\tLoongson EXT ASE", stdout);
a693765e
CX
17868 if (mask & AFL_ASE_LOONGSON_EXT2)
17869 fputs ("\n\tLoongson EXT2 ASE", stdout);
351cdf24
MF
17870 if (mask == 0)
17871 fprintf (stdout, "\n\t%s", _("None"));
00ac7aa0
MF
17872 else if ((mask & ~AFL_ASE_MASK) != 0)
17873 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
351cdf24
MF
17874}
17875
17876static void
17877print_mips_isa_ext (unsigned int isa_ext)
17878{
17879 switch (isa_ext)
17880 {
17881 case 0:
17882 fputs (_("None"), stdout);
17883 break;
17884 case AFL_EXT_XLR:
17885 fputs ("RMI XLR", stdout);
17886 break;
2c629856
N
17887 case AFL_EXT_OCTEON3:
17888 fputs ("Cavium Networks Octeon3", stdout);
17889 break;
351cdf24
MF
17890 case AFL_EXT_OCTEON2:
17891 fputs ("Cavium Networks Octeon2", stdout);
17892 break;
17893 case AFL_EXT_OCTEONP:
17894 fputs ("Cavium Networks OcteonP", stdout);
17895 break;
351cdf24
MF
17896 case AFL_EXT_OCTEON:
17897 fputs ("Cavium Networks Octeon", stdout);
17898 break;
17899 case AFL_EXT_5900:
17900 fputs ("Toshiba R5900", stdout);
17901 break;
17902 case AFL_EXT_4650:
17903 fputs ("MIPS R4650", stdout);
17904 break;
17905 case AFL_EXT_4010:
17906 fputs ("LSI R4010", stdout);
17907 break;
17908 case AFL_EXT_4100:
17909 fputs ("NEC VR4100", stdout);
17910 break;
17911 case AFL_EXT_3900:
17912 fputs ("Toshiba R3900", stdout);
17913 break;
17914 case AFL_EXT_10000:
17915 fputs ("MIPS R10000", stdout);
17916 break;
17917 case AFL_EXT_SB1:
17918 fputs ("Broadcom SB-1", stdout);
17919 break;
17920 case AFL_EXT_4111:
17921 fputs ("NEC VR4111/VR4181", stdout);
17922 break;
17923 case AFL_EXT_4120:
17924 fputs ("NEC VR4120", stdout);
17925 break;
17926 case AFL_EXT_5400:
17927 fputs ("NEC VR5400", stdout);
17928 break;
17929 case AFL_EXT_5500:
17930 fputs ("NEC VR5500", stdout);
17931 break;
17932 case AFL_EXT_LOONGSON_2E:
17933 fputs ("ST Microelectronics Loongson 2E", stdout);
17934 break;
17935 case AFL_EXT_LOONGSON_2F:
17936 fputs ("ST Microelectronics Loongson 2F", stdout);
17937 break;
38bf472a
MR
17938 case AFL_EXT_INTERAPTIV_MR2:
17939 fputs ("Imagination interAptiv MR2", stdout);
17940 break;
351cdf24 17941 default:
00ac7aa0 17942 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
351cdf24
MF
17943 }
17944}
17945
32ec8896 17946static signed int
351cdf24
MF
17947get_mips_reg_size (int reg_size)
17948{
17949 return (reg_size == AFL_REG_NONE) ? 0
17950 : (reg_size == AFL_REG_32) ? 32
17951 : (reg_size == AFL_REG_64) ? 64
17952 : (reg_size == AFL_REG_128) ? 128
17953 : -1;
17954}
17955
015dc7e1 17956static bool
dda8d76d 17957process_mips_specific (Filedata * filedata)
5b18a4bc 17958{
2cf0635d 17959 Elf_Internal_Dyn * entry;
351cdf24 17960 Elf_Internal_Shdr *sect = NULL;
19e6b90e
L
17961 size_t liblist_offset = 0;
17962 size_t liblistno = 0;
17963 size_t conflictsno = 0;
17964 size_t options_offset = 0;
17965 size_t conflicts_offset = 0;
861fb55a
DJ
17966 size_t pltrelsz = 0;
17967 size_t pltrel = 0;
ccb4c951 17968 bfd_vma pltgot = 0;
861fb55a
DJ
17969 bfd_vma mips_pltgot = 0;
17970 bfd_vma jmprel = 0;
ccb4c951
RS
17971 bfd_vma local_gotno = 0;
17972 bfd_vma gotsym = 0;
17973 bfd_vma symtabno = 0;
015dc7e1 17974 bool res = true;
103f02d3 17975
dda8d76d 17976 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
32ec8896 17977 display_mips_gnu_attribute))
015dc7e1 17978 res = false;
2cf19d5c 17979
dda8d76d 17980 sect = find_section (filedata, ".MIPS.abiflags");
351cdf24
MF
17981
17982 if (sect != NULL)
17983 {
17984 Elf_External_ABIFlags_v0 *abiflags_ext;
17985 Elf_Internal_ABIFlags_v0 abiflags_in;
17986
17987 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
32ec8896
NC
17988 {
17989 error (_("Corrupt MIPS ABI Flags section.\n"));
015dc7e1 17990 res = false;
32ec8896 17991 }
351cdf24
MF
17992 else
17993 {
dda8d76d 17994 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
351cdf24
MF
17995 sect->sh_size, _("MIPS ABI Flags section"));
17996 if (abiflags_ext)
17997 {
17998 abiflags_in.version = BYTE_GET (abiflags_ext->version);
17999 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
18000 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
18001 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
18002 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
18003 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
18004 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
18005 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
18006 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
18007 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
18008 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
18009
18010 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
18011 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
18012 if (abiflags_in.isa_rev > 1)
18013 printf ("r%d", abiflags_in.isa_rev);
18014 printf ("\nGPR size: %d",
18015 get_mips_reg_size (abiflags_in.gpr_size));
18016 printf ("\nCPR1 size: %d",
18017 get_mips_reg_size (abiflags_in.cpr1_size));
18018 printf ("\nCPR2 size: %d",
18019 get_mips_reg_size (abiflags_in.cpr2_size));
18020 fputs ("\nFP ABI: ", stdout);
18021 print_mips_fp_abi_value (abiflags_in.fp_abi);
18022 fputs ("ISA Extension: ", stdout);
18023 print_mips_isa_ext (abiflags_in.isa_ext);
18024 fputs ("\nASEs:", stdout);
18025 print_mips_ases (abiflags_in.ases);
18026 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
18027 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
18028 fputc ('\n', stdout);
18029 free (abiflags_ext);
18030 }
18031 }
18032 }
18033
19e6b90e 18034 /* We have a lot of special sections. Thanks SGI! */
978c4450 18035 if (filedata->dynamic_section == NULL)
bbdd9a68
MR
18036 {
18037 /* No dynamic information available. See if there is static GOT. */
dda8d76d 18038 sect = find_section (filedata, ".got");
bbdd9a68
MR
18039 if (sect != NULL)
18040 {
18041 unsigned char *data_end;
18042 unsigned char *data;
18043 bfd_vma ent, end;
18044 int addr_size;
18045
18046 pltgot = sect->sh_addr;
18047
18048 ent = pltgot;
18049 addr_size = (is_32bit_elf ? 4 : 8);
18050 end = pltgot + sect->sh_size;
18051
dda8d76d 18052 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
bbdd9a68
MR
18053 end - pltgot, 1,
18054 _("Global Offset Table data"));
18055 /* PR 12855: Null data is handled gracefully throughout. */
18056 data_end = data + (end - pltgot);
18057
18058 printf (_("\nStatic GOT:\n"));
18059 printf (_(" Canonical gp value: "));
18060 print_vma (ent + 0x7ff0, LONG_HEX);
18061 printf ("\n\n");
18062
18063 /* In a dynamic binary GOT[0] is reserved for the dynamic
18064 loader to store the lazy resolver pointer, however in
18065 a static binary it may well have been omitted and GOT
18066 reduced to a table of addresses.
18067 PR 21344: Check for the entry being fully available
18068 before fetching it. */
18069 if (data
18070 && data + ent - pltgot + addr_size <= data_end
18071 && byte_get (data + ent - pltgot, addr_size) == 0)
18072 {
18073 printf (_(" Reserved entries:\n"));
18074 printf (_(" %*s %10s %*s\n"),
18075 addr_size * 2, _("Address"), _("Access"),
18076 addr_size * 2, _("Value"));
18077 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18078 printf ("\n");
18079 if (ent == (bfd_vma) -1)
18080 goto sgot_print_fail;
18081
18082 /* Check for the MSB of GOT[1] being set, identifying a
18083 GNU object. This entry will be used by some runtime
18084 loaders, to store the module pointer. Otherwise this
18085 is an ordinary local entry.
18086 PR 21344: Check for the entry being fully available
18087 before fetching it. */
18088 if (data
18089 && data + ent - pltgot + addr_size <= data_end
18090 && (byte_get (data + ent - pltgot, addr_size)
18091 >> (addr_size * 8 - 1)) != 0)
18092 {
18093 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18094 printf ("\n");
18095 if (ent == (bfd_vma) -1)
18096 goto sgot_print_fail;
18097 }
18098 printf ("\n");
18099 }
18100
f17e9d8a 18101 if (data != NULL && ent < end)
bbdd9a68
MR
18102 {
18103 printf (_(" Local entries:\n"));
18104 printf (" %*s %10s %*s\n",
18105 addr_size * 2, _("Address"), _("Access"),
18106 addr_size * 2, _("Value"));
18107 while (ent < end)
18108 {
18109 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18110 printf ("\n");
18111 if (ent == (bfd_vma) -1)
18112 goto sgot_print_fail;
18113 }
18114 printf ("\n");
18115 }
18116
18117 sgot_print_fail:
9db70fc3 18118 free (data);
bbdd9a68
MR
18119 }
18120 return res;
18121 }
252b5132 18122
978c4450 18123 for (entry = filedata->dynamic_section;
071436c6 18124 /* PR 17531 file: 012-50589-0.004. */
978c4450
AM
18125 (entry < filedata->dynamic_section + filedata->dynamic_nent
18126 && entry->d_tag != DT_NULL);
071436c6 18127 ++entry)
252b5132
RH
18128 switch (entry->d_tag)
18129 {
18130 case DT_MIPS_LIBLIST:
d93f0186 18131 liblist_offset
dda8d76d 18132 = offset_from_vma (filedata, entry->d_un.d_val,
d93f0186 18133 liblistno * sizeof (Elf32_External_Lib));
252b5132
RH
18134 break;
18135 case DT_MIPS_LIBLISTNO:
18136 liblistno = entry->d_un.d_val;
18137 break;
18138 case DT_MIPS_OPTIONS:
dda8d76d 18139 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
252b5132
RH
18140 break;
18141 case DT_MIPS_CONFLICT:
d93f0186 18142 conflicts_offset
dda8d76d 18143 = offset_from_vma (filedata, entry->d_un.d_val,
d93f0186 18144 conflictsno * sizeof (Elf32_External_Conflict));
252b5132
RH
18145 break;
18146 case DT_MIPS_CONFLICTNO:
18147 conflictsno = entry->d_un.d_val;
18148 break;
ccb4c951 18149 case DT_PLTGOT:
861fb55a
DJ
18150 pltgot = entry->d_un.d_ptr;
18151 break;
ccb4c951
RS
18152 case DT_MIPS_LOCAL_GOTNO:
18153 local_gotno = entry->d_un.d_val;
18154 break;
18155 case DT_MIPS_GOTSYM:
18156 gotsym = entry->d_un.d_val;
18157 break;
18158 case DT_MIPS_SYMTABNO:
18159 symtabno = entry->d_un.d_val;
18160 break;
861fb55a
DJ
18161 case DT_MIPS_PLTGOT:
18162 mips_pltgot = entry->d_un.d_ptr;
18163 break;
18164 case DT_PLTREL:
18165 pltrel = entry->d_un.d_val;
18166 break;
18167 case DT_PLTRELSZ:
18168 pltrelsz = entry->d_un.d_val;
18169 break;
18170 case DT_JMPREL:
18171 jmprel = entry->d_un.d_ptr;
18172 break;
252b5132
RH
18173 default:
18174 break;
18175 }
18176
18177 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
18178 {
2cf0635d 18179 Elf32_External_Lib * elib;
252b5132
RH
18180 size_t cnt;
18181
dda8d76d 18182 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
95099889
AM
18183 sizeof (Elf32_External_Lib),
18184 liblistno,
18185 _("liblist section data"));
a6e9f9df 18186 if (elib)
252b5132 18187 {
d3a49aa8
AM
18188 printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
18189 "\nSection '.liblist' contains %lu entries:\n",
18190 (unsigned long) liblistno),
a6e9f9df 18191 (unsigned long) liblistno);
2b692964 18192 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
a6e9f9df
AM
18193 stdout);
18194
18195 for (cnt = 0; cnt < liblistno; ++cnt)
252b5132 18196 {
a6e9f9df 18197 Elf32_Lib liblist;
91d6fa6a 18198 time_t atime;
d5b07ef4 18199 char timebuf[128];
2cf0635d 18200 struct tm * tmp;
a6e9f9df
AM
18201
18202 liblist.l_name = BYTE_GET (elib[cnt].l_name);
91d6fa6a 18203 atime = BYTE_GET (elib[cnt].l_time_stamp);
a6e9f9df
AM
18204 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18205 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18206 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18207
91d6fa6a 18208 tmp = gmtime (&atime);
e9e44622
JJ
18209 snprintf (timebuf, sizeof (timebuf),
18210 "%04u-%02u-%02uT%02u:%02u:%02u",
18211 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18212 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
a6e9f9df 18213
31104126 18214 printf ("%3lu: ", (unsigned long) cnt);
84714f86
AM
18215 if (valid_dynamic_name (filedata, liblist.l_name))
18216 print_symbol (20, get_dynamic_name (filedata, liblist.l_name));
d79b3d50 18217 else
2b692964 18218 printf (_("<corrupt: %9ld>"), liblist.l_name);
31104126
NC
18219 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
18220 liblist.l_version);
a6e9f9df
AM
18221
18222 if (liblist.l_flags == 0)
2b692964 18223 puts (_(" NONE"));
a6e9f9df
AM
18224 else
18225 {
18226 static const struct
252b5132 18227 {
2cf0635d 18228 const char * name;
a6e9f9df 18229 int bit;
252b5132 18230 }
a6e9f9df
AM
18231 l_flags_vals[] =
18232 {
18233 { " EXACT_MATCH", LL_EXACT_MATCH },
18234 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
18235 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
18236 { " EXPORTS", LL_EXPORTS },
18237 { " DELAY_LOAD", LL_DELAY_LOAD },
18238 { " DELTA", LL_DELTA }
18239 };
18240 int flags = liblist.l_flags;
18241 size_t fcnt;
18242
60bca95a 18243 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
a6e9f9df
AM
18244 if ((flags & l_flags_vals[fcnt].bit) != 0)
18245 {
18246 fputs (l_flags_vals[fcnt].name, stdout);
18247 flags ^= l_flags_vals[fcnt].bit;
18248 }
18249 if (flags != 0)
18250 printf (" %#x", (unsigned int) flags);
252b5132 18251
a6e9f9df
AM
18252 puts ("");
18253 }
252b5132 18254 }
252b5132 18255
a6e9f9df
AM
18256 free (elib);
18257 }
32ec8896 18258 else
015dc7e1 18259 res = false;
252b5132
RH
18260 }
18261
18262 if (options_offset != 0)
18263 {
2cf0635d 18264 Elf_External_Options * eopt;
252b5132
RH
18265 size_t offset;
18266 int cnt;
18267
18268 /* Find the section header so that we get the size. */
dda8d76d 18269 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
948f632f 18270 /* PR 17533 file: 012-277276-0.004. */
071436c6
NC
18271 if (sect == NULL)
18272 {
18273 error (_("No MIPS_OPTIONS header found\n"));
015dc7e1 18274 return false;
071436c6 18275 }
7fc0c668
NC
18276 /* PR 24243 */
18277 if (sect->sh_size < sizeof (* eopt))
18278 {
18279 error (_("The MIPS options section is too small.\n"));
015dc7e1 18280 return false;
7fc0c668 18281 }
252b5132 18282
dda8d76d 18283 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
3f5e193b 18284 sect->sh_size, _("options"));
a6e9f9df 18285 if (eopt)
252b5132 18286 {
fd17d1e6 18287 Elf_Internal_Options option;
76da6bbe 18288
a6e9f9df 18289 offset = cnt = 0;
82b1b41b 18290 while (offset <= sect->sh_size - sizeof (* eopt))
a6e9f9df 18291 {
2cf0635d 18292 Elf_External_Options * eoption;
fd17d1e6 18293 unsigned int optsize;
252b5132 18294
a6e9f9df 18295 eoption = (Elf_External_Options *) ((char *) eopt + offset);
252b5132 18296
fd17d1e6 18297 optsize = BYTE_GET (eoption->size);
76da6bbe 18298
82b1b41b 18299 /* PR 17531: file: ffa0fa3b. */
fd17d1e6
AM
18300 if (optsize < sizeof (* eopt)
18301 || optsize > sect->sh_size - offset)
82b1b41b 18302 {
645f43a8 18303 error (_("Invalid size (%u) for MIPS option\n"),
fd17d1e6 18304 optsize);
645f43a8 18305 free (eopt);
015dc7e1 18306 return false;
82b1b41b 18307 }
fd17d1e6 18308 offset += optsize;
a6e9f9df
AM
18309 ++cnt;
18310 }
252b5132 18311
d3a49aa8
AM
18312 printf (ngettext ("\nSection '%s' contains %d entry:\n",
18313 "\nSection '%s' contains %d entries:\n",
18314 cnt),
dda8d76d 18315 printable_section_name (filedata, sect), cnt);
76da6bbe 18316
82b1b41b 18317 offset = 0;
a6e9f9df 18318 while (cnt-- > 0)
252b5132 18319 {
a6e9f9df 18320 size_t len;
fd17d1e6
AM
18321 Elf_External_Options * eoption;
18322
18323 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18324
18325 option.kind = BYTE_GET (eoption->kind);
18326 option.size = BYTE_GET (eoption->size);
18327 option.section = BYTE_GET (eoption->section);
18328 option.info = BYTE_GET (eoption->info);
a6e9f9df 18329
fd17d1e6 18330 switch (option.kind)
252b5132 18331 {
a6e9f9df
AM
18332 case ODK_NULL:
18333 /* This shouldn't happen. */
d0c4e780 18334 printf (" NULL %" PRId16 " %" PRIx32,
fd17d1e6 18335 option.section, option.info);
a6e9f9df 18336 break;
2e6be59c 18337
a6e9f9df
AM
18338 case ODK_REGINFO:
18339 printf (" REGINFO ");
dda8d76d 18340 if (filedata->file_header.e_machine == EM_MIPS)
a6e9f9df 18341 {
2cf0635d 18342 Elf32_External_RegInfo * ereg;
b34976b6 18343 Elf32_RegInfo reginfo;
a6e9f9df 18344
2e6be59c 18345 /* 32bit form. */
fd17d1e6
AM
18346 if (option.size < (sizeof (Elf_External_Options)
18347 + sizeof (Elf32_External_RegInfo)))
2e6be59c
NC
18348 {
18349 printf (_("<corrupt>\n"));
18350 error (_("Truncated MIPS REGINFO option\n"));
18351 cnt = 0;
18352 break;
18353 }
18354
fd17d1e6 18355 ereg = (Elf32_External_RegInfo *) (eoption + 1);
2e6be59c 18356
a6e9f9df
AM
18357 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18358 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18359 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18360 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18361 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18362 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18363
d0c4e780
AM
18364 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
18365 reginfo.ri_gprmask, reginfo.ri_gp_value);
18366 printf (" "
18367 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18368 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
a6e9f9df
AM
18369 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18370 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18371 }
18372 else
18373 {
18374 /* 64 bit form. */
2cf0635d 18375 Elf64_External_RegInfo * ereg;
a6e9f9df
AM
18376 Elf64_Internal_RegInfo reginfo;
18377
fd17d1e6
AM
18378 if (option.size < (sizeof (Elf_External_Options)
18379 + sizeof (Elf64_External_RegInfo)))
2e6be59c
NC
18380 {
18381 printf (_("<corrupt>\n"));
18382 error (_("Truncated MIPS REGINFO option\n"));
18383 cnt = 0;
18384 break;
18385 }
18386
fd17d1e6 18387 ereg = (Elf64_External_RegInfo *) (eoption + 1);
a6e9f9df
AM
18388 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18389 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18390 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18391 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18392 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
66543521 18393 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
a6e9f9df 18394
d0c4e780
AM
18395 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
18396 reginfo.ri_gprmask, reginfo.ri_gp_value);
18397 printf (" "
18398 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18399 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
a6e9f9df
AM
18400 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18401 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18402 }
fd17d1e6 18403 offset += option.size;
a6e9f9df 18404 continue;
2e6be59c 18405
a6e9f9df
AM
18406 case ODK_EXCEPTIONS:
18407 fputs (" EXCEPTIONS fpe_min(", stdout);
fd17d1e6 18408 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
a6e9f9df 18409 fputs (") fpe_max(", stdout);
fd17d1e6 18410 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
a6e9f9df
AM
18411 fputs (")", stdout);
18412
fd17d1e6 18413 if (option.info & OEX_PAGE0)
a6e9f9df 18414 fputs (" PAGE0", stdout);
fd17d1e6 18415 if (option.info & OEX_SMM)
a6e9f9df 18416 fputs (" SMM", stdout);
fd17d1e6 18417 if (option.info & OEX_FPDBUG)
a6e9f9df 18418 fputs (" FPDBUG", stdout);
fd17d1e6 18419 if (option.info & OEX_DISMISS)
a6e9f9df
AM
18420 fputs (" DISMISS", stdout);
18421 break;
2e6be59c 18422
a6e9f9df
AM
18423 case ODK_PAD:
18424 fputs (" PAD ", stdout);
fd17d1e6 18425 if (option.info & OPAD_PREFIX)
a6e9f9df 18426 fputs (" PREFIX", stdout);
fd17d1e6 18427 if (option.info & OPAD_POSTFIX)
a6e9f9df 18428 fputs (" POSTFIX", stdout);
fd17d1e6 18429 if (option.info & OPAD_SYMBOL)
a6e9f9df
AM
18430 fputs (" SYMBOL", stdout);
18431 break;
2e6be59c 18432
a6e9f9df
AM
18433 case ODK_HWPATCH:
18434 fputs (" HWPATCH ", stdout);
fd17d1e6 18435 if (option.info & OHW_R4KEOP)
a6e9f9df 18436 fputs (" R4KEOP", stdout);
fd17d1e6 18437 if (option.info & OHW_R8KPFETCH)
a6e9f9df 18438 fputs (" R8KPFETCH", stdout);
fd17d1e6 18439 if (option.info & OHW_R5KEOP)
a6e9f9df 18440 fputs (" R5KEOP", stdout);
fd17d1e6 18441 if (option.info & OHW_R5KCVTL)
a6e9f9df
AM
18442 fputs (" R5KCVTL", stdout);
18443 break;
2e6be59c 18444
a6e9f9df
AM
18445 case ODK_FILL:
18446 fputs (" FILL ", stdout);
18447 /* XXX Print content of info word? */
18448 break;
2e6be59c 18449
a6e9f9df
AM
18450 case ODK_TAGS:
18451 fputs (" TAGS ", stdout);
18452 /* XXX Print content of info word? */
18453 break;
2e6be59c 18454
a6e9f9df
AM
18455 case ODK_HWAND:
18456 fputs (" HWAND ", stdout);
fd17d1e6 18457 if (option.info & OHWA0_R4KEOP_CHECKED)
a6e9f9df 18458 fputs (" R4KEOP_CHECKED", stdout);
fd17d1e6 18459 if (option.info & OHWA0_R4KEOP_CLEAN)
a6e9f9df
AM
18460 fputs (" R4KEOP_CLEAN", stdout);
18461 break;
2e6be59c 18462
a6e9f9df
AM
18463 case ODK_HWOR:
18464 fputs (" HWOR ", stdout);
fd17d1e6 18465 if (option.info & OHWA0_R4KEOP_CHECKED)
a6e9f9df 18466 fputs (" R4KEOP_CHECKED", stdout);
fd17d1e6 18467 if (option.info & OHWA0_R4KEOP_CLEAN)
a6e9f9df
AM
18468 fputs (" R4KEOP_CLEAN", stdout);
18469 break;
2e6be59c 18470
a6e9f9df 18471 case ODK_GP_GROUP:
d0c4e780 18472 printf (" GP_GROUP %#06x self-contained %#06x",
fd17d1e6
AM
18473 option.info & OGP_GROUP,
18474 (option.info & OGP_SELF) >> 16);
a6e9f9df 18475 break;
2e6be59c 18476
a6e9f9df 18477 case ODK_IDENT:
d0c4e780 18478 printf (" IDENT %#06x self-contained %#06x",
fd17d1e6
AM
18479 option.info & OGP_GROUP,
18480 (option.info & OGP_SELF) >> 16);
a6e9f9df 18481 break;
2e6be59c 18482
a6e9f9df
AM
18483 default:
18484 /* This shouldn't happen. */
d0c4e780 18485 printf (" %3d ??? %" PRId16 " %" PRIx32,
fd17d1e6 18486 option.kind, option.section, option.info);
a6e9f9df 18487 break;
252b5132 18488 }
a6e9f9df 18489
2cf0635d 18490 len = sizeof (* eopt);
fd17d1e6 18491 while (len < option.size)
82b1b41b 18492 {
fd17d1e6 18493 unsigned char datum = *((unsigned char *) eoption + len);
a6e9f9df 18494
82b1b41b
NC
18495 if (ISPRINT (datum))
18496 printf ("%c", datum);
18497 else
18498 printf ("\\%03o", datum);
18499 len ++;
18500 }
a6e9f9df 18501 fputs ("\n", stdout);
82b1b41b 18502
fd17d1e6 18503 offset += option.size;
252b5132 18504 }
a6e9f9df 18505 free (eopt);
252b5132 18506 }
32ec8896 18507 else
015dc7e1 18508 res = false;
252b5132
RH
18509 }
18510
18511 if (conflicts_offset != 0 && conflictsno != 0)
18512 {
2cf0635d 18513 Elf32_Conflict * iconf;
252b5132
RH
18514 size_t cnt;
18515
978c4450 18516 if (filedata->dynamic_symbols == NULL)
252b5132 18517 {
591a748a 18518 error (_("conflict list found without a dynamic symbol table\n"));
015dc7e1 18519 return false;
252b5132
RH
18520 }
18521
7296a62a
NC
18522 /* PR 21345 - print a slightly more helpful error message
18523 if we are sure that the cmalloc will fail. */
645f43a8 18524 if (conflictsno > filedata->file_size / sizeof (* iconf))
7296a62a
NC
18525 {
18526 error (_("Overlarge number of conflicts detected: %lx\n"),
18527 (long) conflictsno);
015dc7e1 18528 return false;
7296a62a
NC
18529 }
18530
3f5e193b 18531 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
252b5132
RH
18532 if (iconf == NULL)
18533 {
8b73c356 18534 error (_("Out of memory allocating space for dynamic conflicts\n"));
015dc7e1 18535 return false;
252b5132
RH
18536 }
18537
9ea033b2 18538 if (is_32bit_elf)
252b5132 18539 {
2cf0635d 18540 Elf32_External_Conflict * econf32;
a6e9f9df 18541
3f5e193b 18542 econf32 = (Elf32_External_Conflict *)
95099889
AM
18543 get_data (NULL, filedata, conflicts_offset,
18544 sizeof (*econf32), conflictsno, _("conflict"));
a6e9f9df 18545 if (!econf32)
5a814d6d
AM
18546 {
18547 free (iconf);
015dc7e1 18548 return false;
5a814d6d 18549 }
252b5132
RH
18550
18551 for (cnt = 0; cnt < conflictsno; ++cnt)
18552 iconf[cnt] = BYTE_GET (econf32[cnt]);
a6e9f9df
AM
18553
18554 free (econf32);
252b5132
RH
18555 }
18556 else
18557 {
2cf0635d 18558 Elf64_External_Conflict * econf64;
a6e9f9df 18559
3f5e193b 18560 econf64 = (Elf64_External_Conflict *)
95099889
AM
18561 get_data (NULL, filedata, conflicts_offset,
18562 sizeof (*econf64), conflictsno, _("conflict"));
a6e9f9df 18563 if (!econf64)
5a814d6d
AM
18564 {
18565 free (iconf);
015dc7e1 18566 return false;
5a814d6d 18567 }
252b5132
RH
18568
18569 for (cnt = 0; cnt < conflictsno; ++cnt)
18570 iconf[cnt] = BYTE_GET (econf64[cnt]);
a6e9f9df
AM
18571
18572 free (econf64);
252b5132
RH
18573 }
18574
d3a49aa8
AM
18575 printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
18576 "\nSection '.conflict' contains %lu entries:\n",
18577 (unsigned long) conflictsno),
c7e7ca54 18578 (unsigned long) conflictsno);
252b5132
RH
18579 puts (_(" Num: Index Value Name"));
18580
18581 for (cnt = 0; cnt < conflictsno; ++cnt)
18582 {
b34976b6 18583 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
e0a31db1 18584
978c4450 18585 if (iconf[cnt] >= filedata->num_dynamic_syms)
e0a31db1 18586 printf (_("<corrupt symbol index>"));
d79b3d50 18587 else
e0a31db1
NC
18588 {
18589 Elf_Internal_Sym * psym;
18590
978c4450 18591 psym = & filedata->dynamic_symbols[iconf[cnt]];
e0a31db1
NC
18592 print_vma (psym->st_value, FULL_HEX);
18593 putchar (' ');
84714f86
AM
18594 if (valid_dynamic_name (filedata, psym->st_name))
18595 print_symbol (25, get_dynamic_name (filedata, psym->st_name));
e0a31db1
NC
18596 else
18597 printf (_("<corrupt: %14ld>"), psym->st_name);
18598 }
31104126 18599 putchar ('\n');
252b5132
RH
18600 }
18601
252b5132
RH
18602 free (iconf);
18603 }
18604
ccb4c951
RS
18605 if (pltgot != 0 && local_gotno != 0)
18606 {
91d6fa6a 18607 bfd_vma ent, local_end, global_end;
bbeee7ea 18608 size_t i, offset;
2cf0635d 18609 unsigned char * data;
82b1b41b 18610 unsigned char * data_end;
bbeee7ea 18611 int addr_size;
ccb4c951 18612
91d6fa6a 18613 ent = pltgot;
ccb4c951
RS
18614 addr_size = (is_32bit_elf ? 4 : 8);
18615 local_end = pltgot + local_gotno * addr_size;
ccb4c951 18616
74e1a04b
NC
18617 /* PR binutils/17533 file: 012-111227-0.004 */
18618 if (symtabno < gotsym)
18619 {
18620 error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
82b1b41b 18621 (unsigned long) gotsym, (unsigned long) symtabno);
015dc7e1 18622 return false;
74e1a04b 18623 }
82b1b41b 18624
74e1a04b 18625 global_end = local_end + (symtabno - gotsym) * addr_size;
82b1b41b
NC
18626 /* PR 17531: file: 54c91a34. */
18627 if (global_end < local_end)
18628 {
18629 error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
015dc7e1 18630 return false;
82b1b41b 18631 }
948f632f 18632
dda8d76d
NC
18633 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
18634 data = (unsigned char *) get_data (NULL, filedata, offset,
9cf03b7e
NC
18635 global_end - pltgot, 1,
18636 _("Global Offset Table data"));
919383ac 18637 /* PR 12855: Null data is handled gracefully throughout. */
82b1b41b 18638 data_end = data + (global_end - pltgot);
59245841 18639
ccb4c951
RS
18640 printf (_("\nPrimary GOT:\n"));
18641 printf (_(" Canonical gp value: "));
18642 print_vma (pltgot + 0x7ff0, LONG_HEX);
18643 printf ("\n\n");
18644
18645 printf (_(" Reserved entries:\n"));
18646 printf (_(" %*s %10s %*s Purpose\n"),
2b692964
NC
18647 addr_size * 2, _("Address"), _("Access"),
18648 addr_size * 2, _("Initial"));
82b1b41b 18649 ent = print_mips_got_entry (data, pltgot, ent, data_end);
2b692964 18650 printf (_(" Lazy resolver\n"));
82b1b41b
NC
18651 if (ent == (bfd_vma) -1)
18652 goto got_print_fail;
75ec1fdb 18653
c4ab9505
MR
18654 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
18655 This entry will be used by some runtime loaders, to store the
18656 module pointer. Otherwise this is an ordinary local entry.
18657 PR 21344: Check for the entry being fully available before
18658 fetching it. */
18659 if (data
18660 && data + ent - pltgot + addr_size <= data_end
18661 && (byte_get (data + ent - pltgot, addr_size)
18662 >> (addr_size * 8 - 1)) != 0)
18663 {
18664 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18665 printf (_(" Module pointer (GNU extension)\n"));
18666 if (ent == (bfd_vma) -1)
18667 goto got_print_fail;
ccb4c951
RS
18668 }
18669 printf ("\n");
18670
f17e9d8a 18671 if (data != NULL && ent < local_end)
ccb4c951
RS
18672 {
18673 printf (_(" Local entries:\n"));
cc5914eb 18674 printf (" %*s %10s %*s\n",
2b692964
NC
18675 addr_size * 2, _("Address"), _("Access"),
18676 addr_size * 2, _("Initial"));
91d6fa6a 18677 while (ent < local_end)
ccb4c951 18678 {
82b1b41b 18679 ent = print_mips_got_entry (data, pltgot, ent, data_end);
ccb4c951 18680 printf ("\n");
82b1b41b
NC
18681 if (ent == (bfd_vma) -1)
18682 goto got_print_fail;
ccb4c951
RS
18683 }
18684 printf ("\n");
18685 }
18686
f17e9d8a 18687 if (data != NULL && gotsym < symtabno)
ccb4c951
RS
18688 {
18689 int sym_width;
18690
18691 printf (_(" Global entries:\n"));
cc5914eb 18692 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
9cf03b7e
NC
18693 addr_size * 2, _("Address"),
18694 _("Access"),
2b692964 18695 addr_size * 2, _("Initial"),
9cf03b7e
NC
18696 addr_size * 2, _("Sym.Val."),
18697 _("Type"),
18698 /* Note for translators: "Ndx" = abbreviated form of "Index". */
18699 _("Ndx"), _("Name"));
0b4362b0 18700
ccb4c951 18701 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
e0a31db1 18702
ccb4c951
RS
18703 for (i = gotsym; i < symtabno; i++)
18704 {
82b1b41b 18705 ent = print_mips_got_entry (data, pltgot, ent, data_end);
ccb4c951 18706 printf (" ");
e0a31db1 18707
978c4450 18708 if (filedata->dynamic_symbols == NULL)
e0a31db1 18709 printf (_("<no dynamic symbols>"));
978c4450 18710 else if (i < filedata->num_dynamic_syms)
e0a31db1 18711 {
978c4450 18712 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
e0a31db1
NC
18713
18714 print_vma (psym->st_value, LONG_HEX);
18715 printf (" %-7s %3s ",
dda8d76d
NC
18716 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18717 get_symbol_index_type (filedata, psym->st_shndx));
e0a31db1 18718
84714f86 18719 if (valid_dynamic_name (filedata, psym->st_name))
978c4450 18720 print_symbol (sym_width,
84714f86 18721 get_dynamic_name (filedata, psym->st_name));
e0a31db1
NC
18722 else
18723 printf (_("<corrupt: %14ld>"), psym->st_name);
18724 }
ccb4c951 18725 else
7fc5ac57
JBG
18726 printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
18727 (unsigned long) i);
e0a31db1 18728
ccb4c951 18729 printf ("\n");
82b1b41b
NC
18730 if (ent == (bfd_vma) -1)
18731 break;
ccb4c951
RS
18732 }
18733 printf ("\n");
18734 }
18735
82b1b41b 18736 got_print_fail:
9db70fc3 18737 free (data);
ccb4c951
RS
18738 }
18739
861fb55a
DJ
18740 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
18741 {
91d6fa6a 18742 bfd_vma ent, end;
861fb55a
DJ
18743 size_t offset, rel_offset;
18744 unsigned long count, i;
2cf0635d 18745 unsigned char * data;
861fb55a 18746 int addr_size, sym_width;
2cf0635d 18747 Elf_Internal_Rela * rels;
861fb55a 18748
dda8d76d 18749 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
861fb55a
DJ
18750 if (pltrel == DT_RELA)
18751 {
dda8d76d 18752 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
015dc7e1 18753 return false;
861fb55a
DJ
18754 }
18755 else
18756 {
dda8d76d 18757 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
015dc7e1 18758 return false;
861fb55a
DJ
18759 }
18760
91d6fa6a 18761 ent = mips_pltgot;
861fb55a
DJ
18762 addr_size = (is_32bit_elf ? 4 : 8);
18763 end = mips_pltgot + (2 + count) * addr_size;
18764
dda8d76d
NC
18765 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
18766 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
9cf03b7e 18767 1, _("Procedure Linkage Table data"));
59245841 18768 if (data == NULL)
288f0ba2
AM
18769 {
18770 free (rels);
015dc7e1 18771 return false;
288f0ba2 18772 }
59245841 18773
9cf03b7e 18774 printf ("\nPLT GOT:\n\n");
861fb55a
DJ
18775 printf (_(" Reserved entries:\n"));
18776 printf (_(" %*s %*s Purpose\n"),
2b692964 18777 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
91d6fa6a 18778 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
2b692964 18779 printf (_(" PLT lazy resolver\n"));
91d6fa6a 18780 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
2b692964 18781 printf (_(" Module pointer\n"));
861fb55a
DJ
18782 printf ("\n");
18783
18784 printf (_(" Entries:\n"));
cc5914eb 18785 printf (" %*s %*s %*s %-7s %3s %s\n",
2b692964
NC
18786 addr_size * 2, _("Address"),
18787 addr_size * 2, _("Initial"),
18788 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
861fb55a
DJ
18789 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
18790 for (i = 0; i < count; i++)
18791 {
df97ab2a 18792 unsigned long idx = get_reloc_symindex (rels[i].r_info);
861fb55a 18793
91d6fa6a 18794 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
861fb55a 18795 printf (" ");
e0a31db1 18796
978c4450 18797 if (idx >= filedata->num_dynamic_syms)
df97ab2a 18798 printf (_("<corrupt symbol index: %lu>"), idx);
861fb55a 18799 else
e0a31db1 18800 {
978c4450 18801 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
e0a31db1
NC
18802
18803 print_vma (psym->st_value, LONG_HEX);
18804 printf (" %-7s %3s ",
dda8d76d
NC
18805 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18806 get_symbol_index_type (filedata, psym->st_shndx));
84714f86 18807 if (valid_dynamic_name (filedata, psym->st_name))
978c4450 18808 print_symbol (sym_width,
84714f86 18809 get_dynamic_name (filedata, psym->st_name));
e0a31db1
NC
18810 else
18811 printf (_("<corrupt: %14ld>"), psym->st_name);
18812 }
861fb55a
DJ
18813 printf ("\n");
18814 }
18815 printf ("\n");
18816
9db70fc3 18817 free (data);
861fb55a
DJ
18818 free (rels);
18819 }
18820
32ec8896 18821 return res;
252b5132
RH
18822}
18823
015dc7e1 18824static bool
dda8d76d 18825process_nds32_specific (Filedata * filedata)
35c08157
KLC
18826{
18827 Elf_Internal_Shdr *sect = NULL;
18828
dda8d76d 18829 sect = find_section (filedata, ".nds32_e_flags");
9c7b8e9b 18830 if (sect != NULL && sect->sh_size >= 4)
35c08157 18831 {
9c7b8e9b
AM
18832 unsigned char *buf;
18833 unsigned int flag;
35c08157
KLC
18834
18835 printf ("\nNDS32 elf flags section:\n");
9c7b8e9b
AM
18836 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
18837 _("NDS32 elf flags section"));
35c08157 18838
9c7b8e9b 18839 if (buf == NULL)
015dc7e1 18840 return false;
32ec8896 18841
9c7b8e9b
AM
18842 flag = byte_get (buf, 4);
18843 free (buf);
18844 switch (flag & 0x3)
35c08157
KLC
18845 {
18846 case 0:
18847 printf ("(VEC_SIZE):\tNo entry.\n");
18848 break;
18849 case 1:
18850 printf ("(VEC_SIZE):\t4 bytes\n");
18851 break;
18852 case 2:
18853 printf ("(VEC_SIZE):\t16 bytes\n");
18854 break;
18855 case 3:
18856 printf ("(VEC_SIZE):\treserved\n");
18857 break;
18858 }
18859 }
18860
015dc7e1 18861 return true;
35c08157
KLC
18862}
18863
015dc7e1 18864static bool
dda8d76d 18865process_gnu_liblist (Filedata * filedata)
047b2264 18866{
2cf0635d
NC
18867 Elf_Internal_Shdr * section;
18868 Elf_Internal_Shdr * string_sec;
18869 Elf32_External_Lib * elib;
18870 char * strtab;
c256ffe7 18871 size_t strtab_size;
047b2264 18872 size_t cnt;
d3a49aa8 18873 unsigned long num_liblist;
047b2264 18874 unsigned i;
015dc7e1 18875 bool res = true;
047b2264
JJ
18876
18877 if (! do_arch)
015dc7e1 18878 return true;
047b2264 18879
dda8d76d
NC
18880 for (i = 0, section = filedata->section_headers;
18881 i < filedata->file_header.e_shnum;
b34976b6 18882 i++, section++)
047b2264
JJ
18883 {
18884 switch (section->sh_type)
18885 {
18886 case SHT_GNU_LIBLIST:
dda8d76d 18887 if (section->sh_link >= filedata->file_header.e_shnum)
c256ffe7
JJ
18888 break;
18889
3f5e193b 18890 elib = (Elf32_External_Lib *)
dda8d76d 18891 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
9cf03b7e 18892 _("liblist section data"));
047b2264
JJ
18893
18894 if (elib == NULL)
32ec8896 18895 {
015dc7e1 18896 res = false;
32ec8896
NC
18897 break;
18898 }
047b2264 18899
dda8d76d
NC
18900 string_sec = filedata->section_headers + section->sh_link;
18901 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
3f5e193b
NC
18902 string_sec->sh_size,
18903 _("liblist string table"));
047b2264
JJ
18904 if (strtab == NULL
18905 || section->sh_entsize != sizeof (Elf32_External_Lib))
18906 {
18907 free (elib);
2842702f 18908 free (strtab);
015dc7e1 18909 res = false;
047b2264
JJ
18910 break;
18911 }
59245841 18912 strtab_size = string_sec->sh_size;
047b2264 18913
d3a49aa8
AM
18914 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
18915 printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
18916 "\nLibrary list section '%s' contains %lu entries:\n",
18917 num_liblist),
dda8d76d 18918 printable_section_name (filedata, section),
d3a49aa8 18919 num_liblist);
047b2264 18920
2b692964 18921 puts (_(" Library Time Stamp Checksum Version Flags"));
047b2264
JJ
18922
18923 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
18924 ++cnt)
18925 {
18926 Elf32_Lib liblist;
91d6fa6a 18927 time_t atime;
d5b07ef4 18928 char timebuf[128];
2cf0635d 18929 struct tm * tmp;
047b2264
JJ
18930
18931 liblist.l_name = BYTE_GET (elib[cnt].l_name);
91d6fa6a 18932 atime = BYTE_GET (elib[cnt].l_time_stamp);
047b2264
JJ
18933 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18934 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18935 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18936
91d6fa6a 18937 tmp = gmtime (&atime);
e9e44622
JJ
18938 snprintf (timebuf, sizeof (timebuf),
18939 "%04u-%02u-%02uT%02u:%02u:%02u",
18940 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18941 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
047b2264
JJ
18942
18943 printf ("%3lu: ", (unsigned long) cnt);
18944 if (do_wide)
c256ffe7 18945 printf ("%-20s", liblist.l_name < strtab_size
2b692964 18946 ? strtab + liblist.l_name : _("<corrupt>"));
047b2264 18947 else
c256ffe7 18948 printf ("%-20.20s", liblist.l_name < strtab_size
2b692964 18949 ? strtab + liblist.l_name : _("<corrupt>"));
047b2264
JJ
18950 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
18951 liblist.l_version, liblist.l_flags);
18952 }
18953
18954 free (elib);
2842702f 18955 free (strtab);
047b2264
JJ
18956 }
18957 }
18958
32ec8896 18959 return res;
047b2264
JJ
18960}
18961
9437c45b 18962static const char *
dda8d76d 18963get_note_type (Filedata * filedata, unsigned e_type)
779fe533
NC
18964{
18965 static char buff[64];
103f02d3 18966
dda8d76d 18967 if (filedata->file_header.e_type == ET_CORE)
1ec5cd37
NC
18968 switch (e_type)
18969 {
57346661 18970 case NT_AUXV:
1ec5cd37 18971 return _("NT_AUXV (auxiliary vector)");
57346661 18972 case NT_PRSTATUS:
1ec5cd37 18973 return _("NT_PRSTATUS (prstatus structure)");
57346661 18974 case NT_FPREGSET:
1ec5cd37 18975 return _("NT_FPREGSET (floating point registers)");
57346661 18976 case NT_PRPSINFO:
1ec5cd37 18977 return _("NT_PRPSINFO (prpsinfo structure)");
57346661 18978 case NT_TASKSTRUCT:
1ec5cd37 18979 return _("NT_TASKSTRUCT (task structure)");
b63a5e38
AB
18980 case NT_GDB_TDESC:
18981 return _("NT_GDB_TDESC (GDB XML target description)");
57346661 18982 case NT_PRXFPREG:
1ec5cd37 18983 return _("NT_PRXFPREG (user_xfpregs structure)");
e1e95dec
AM
18984 case NT_PPC_VMX:
18985 return _("NT_PPC_VMX (ppc Altivec registers)");
89eeb0bc
LM
18986 case NT_PPC_VSX:
18987 return _("NT_PPC_VSX (ppc VSX registers)");
66c3b5f8
GR
18988 case NT_PPC_TAR:
18989 return _("NT_PPC_TAR (ppc TAR register)");
18990 case NT_PPC_PPR:
18991 return _("NT_PPC_PPR (ppc PPR register)");
18992 case NT_PPC_DSCR:
18993 return _("NT_PPC_DSCR (ppc DSCR register)");
18994 case NT_PPC_EBB:
18995 return _("NT_PPC_EBB (ppc EBB registers)");
18996 case NT_PPC_PMU:
18997 return _("NT_PPC_PMU (ppc PMU registers)");
18998 case NT_PPC_TM_CGPR:
18999 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
19000 case NT_PPC_TM_CFPR:
19001 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
19002 case NT_PPC_TM_CVMX:
19003 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
19004 case NT_PPC_TM_CVSX:
3fd21718 19005 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
66c3b5f8
GR
19006 case NT_PPC_TM_SPR:
19007 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
19008 case NT_PPC_TM_CTAR:
19009 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
19010 case NT_PPC_TM_CPPR:
19011 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
19012 case NT_PPC_TM_CDSCR:
19013 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
ff826ef3
TT
19014 case NT_386_TLS:
19015 return _("NT_386_TLS (x86 TLS information)");
19016 case NT_386_IOPERM:
19017 return _("NT_386_IOPERM (x86 I/O permissions)");
4339cae0
L
19018 case NT_X86_XSTATE:
19019 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
8d58ed37
L
19020 case NT_X86_CET:
19021 return _("NT_X86_CET (x86 CET state)");
0675e188
UW
19022 case NT_S390_HIGH_GPRS:
19023 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
d7eeb400
MS
19024 case NT_S390_TIMER:
19025 return _("NT_S390_TIMER (s390 timer register)");
19026 case NT_S390_TODCMP:
19027 return _("NT_S390_TODCMP (s390 TOD comparator register)");
19028 case NT_S390_TODPREG:
19029 return _("NT_S390_TODPREG (s390 TOD programmable register)");
19030 case NT_S390_CTRS:
19031 return _("NT_S390_CTRS (s390 control registers)");
19032 case NT_S390_PREFIX:
19033 return _("NT_S390_PREFIX (s390 prefix register)");
a367d729
AK
19034 case NT_S390_LAST_BREAK:
19035 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
19036 case NT_S390_SYSTEM_CALL:
19037 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
abb3f6cc
NC
19038 case NT_S390_TDB:
19039 return _("NT_S390_TDB (s390 transaction diagnostic block)");
4ef9f41a
AA
19040 case NT_S390_VXRS_LOW:
19041 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
19042 case NT_S390_VXRS_HIGH:
19043 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
88ab90e8
AA
19044 case NT_S390_GS_CB:
19045 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
19046 case NT_S390_GS_BC:
19047 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
faa9a424
UW
19048 case NT_ARM_VFP:
19049 return _("NT_ARM_VFP (arm VFP registers)");
652451f8
YZ
19050 case NT_ARM_TLS:
19051 return _("NT_ARM_TLS (AArch TLS registers)");
19052 case NT_ARM_HW_BREAK:
19053 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
19054 case NT_ARM_HW_WATCH:
19055 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
3b2bef8b
LM
19056 case NT_ARM_SVE:
19057 return _("NT_ARM_SVE (AArch SVE registers)");
19058 case NT_ARM_PAC_MASK:
19059 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
3af2785c
LM
19060 case NT_ARM_PACA_KEYS:
19061 return _("NT_ARM_PACA_KEYS (ARM pointer authentication address keys)");
19062 case NT_ARM_PACG_KEYS:
19063 return _("NT_ARM_PACG_KEYS (ARM pointer authentication generic keys)");
3b2bef8b
LM
19064 case NT_ARM_TAGGED_ADDR_CTRL:
19065 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
3af2785c
LM
19066 case NT_ARM_PAC_ENABLED_KEYS:
19067 return _("NT_ARM_PAC_ENABLED_KEYS (AArch64 pointer authentication enabled keys)");
27456742
AK
19068 case NT_ARC_V2:
19069 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
db6092f3
AB
19070 case NT_RISCV_CSR:
19071 return _("NT_RISCV_CSR (RISC-V control and status registers)");
57346661 19072 case NT_PSTATUS:
1ec5cd37 19073 return _("NT_PSTATUS (pstatus structure)");
57346661 19074 case NT_FPREGS:
1ec5cd37 19075 return _("NT_FPREGS (floating point registers)");
57346661 19076 case NT_PSINFO:
1ec5cd37 19077 return _("NT_PSINFO (psinfo structure)");
57346661 19078 case NT_LWPSTATUS:
1ec5cd37 19079 return _("NT_LWPSTATUS (lwpstatus_t structure)");
57346661 19080 case NT_LWPSINFO:
1ec5cd37 19081 return _("NT_LWPSINFO (lwpsinfo_t structure)");
57346661 19082 case NT_WIN32PSTATUS:
1ec5cd37 19083 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
9ece1fa9
TT
19084 case NT_SIGINFO:
19085 return _("NT_SIGINFO (siginfo_t data)");
19086 case NT_FILE:
19087 return _("NT_FILE (mapped files)");
1ec5cd37
NC
19088 default:
19089 break;
19090 }
19091 else
19092 switch (e_type)
19093 {
19094 case NT_VERSION:
19095 return _("NT_VERSION (version)");
19096 case NT_ARCH:
19097 return _("NT_ARCH (architecture)");
9ef920e9 19098 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
6f156d7a 19099 return _("OPEN");
9ef920e9 19100 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
6f156d7a 19101 return _("func");
c8795e1f
NC
19102 case NT_GO_BUILDID:
19103 return _("GO BUILDID");
1ec5cd37
NC
19104 default:
19105 break;
19106 }
19107
e9e44622 19108 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
1ec5cd37 19109 return buff;
779fe533
NC
19110}
19111
015dc7e1 19112static bool
9ece1fa9
TT
19113print_core_note (Elf_Internal_Note *pnote)
19114{
19115 unsigned int addr_size = is_32bit_elf ? 4 : 8;
19116 bfd_vma count, page_size;
19117 unsigned char *descdata, *filenames, *descend;
19118
19119 if (pnote->type != NT_FILE)
04ac15ab
AS
19120 {
19121 if (do_wide)
19122 printf ("\n");
015dc7e1 19123 return true;
04ac15ab 19124 }
9ece1fa9
TT
19125
19126#ifndef BFD64
19127 if (!is_32bit_elf)
19128 {
19129 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
19130 /* Still "successful". */
015dc7e1 19131 return true;
9ece1fa9
TT
19132 }
19133#endif
19134
19135 if (pnote->descsz < 2 * addr_size)
19136 {
32ec8896 19137 error (_(" Malformed note - too short for header\n"));
015dc7e1 19138 return false;
9ece1fa9
TT
19139 }
19140
19141 descdata = (unsigned char *) pnote->descdata;
19142 descend = descdata + pnote->descsz;
19143
19144 if (descdata[pnote->descsz - 1] != '\0')
19145 {
32ec8896 19146 error (_(" Malformed note - does not end with \\0\n"));
015dc7e1 19147 return false;
9ece1fa9
TT
19148 }
19149
19150 count = byte_get (descdata, addr_size);
19151 descdata += addr_size;
19152
19153 page_size = byte_get (descdata, addr_size);
19154 descdata += addr_size;
19155
5396a86e
AM
19156 if (count > ((bfd_vma) -1 - 2 * addr_size) / (3 * addr_size)
19157 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
9ece1fa9 19158 {
32ec8896 19159 error (_(" Malformed note - too short for supplied file count\n"));
015dc7e1 19160 return false;
9ece1fa9
TT
19161 }
19162
19163 printf (_(" Page size: "));
19164 print_vma (page_size, DEC);
19165 printf ("\n");
19166
19167 printf (_(" %*s%*s%*s\n"),
19168 (int) (2 + 2 * addr_size), _("Start"),
19169 (int) (4 + 2 * addr_size), _("End"),
19170 (int) (4 + 2 * addr_size), _("Page Offset"));
19171 filenames = descdata + count * 3 * addr_size;
595712bb 19172 while (count-- > 0)
9ece1fa9
TT
19173 {
19174 bfd_vma start, end, file_ofs;
19175
19176 if (filenames == descend)
19177 {
32ec8896 19178 error (_(" Malformed note - filenames end too early\n"));
015dc7e1 19179 return false;
9ece1fa9
TT
19180 }
19181
19182 start = byte_get (descdata, addr_size);
19183 descdata += addr_size;
19184 end = byte_get (descdata, addr_size);
19185 descdata += addr_size;
19186 file_ofs = byte_get (descdata, addr_size);
19187 descdata += addr_size;
19188
19189 printf (" ");
19190 print_vma (start, FULL_HEX);
19191 printf (" ");
19192 print_vma (end, FULL_HEX);
19193 printf (" ");
19194 print_vma (file_ofs, FULL_HEX);
19195 printf ("\n %s\n", filenames);
19196
19197 filenames += 1 + strlen ((char *) filenames);
19198 }
19199
015dc7e1 19200 return true;
9ece1fa9
TT
19201}
19202
1118d252
RM
19203static const char *
19204get_gnu_elf_note_type (unsigned e_type)
19205{
1449284b 19206 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
1118d252
RM
19207 switch (e_type)
19208 {
19209 case NT_GNU_ABI_TAG:
19210 return _("NT_GNU_ABI_TAG (ABI version tag)");
19211 case NT_GNU_HWCAP:
19212 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
19213 case NT_GNU_BUILD_ID:
19214 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
0297aed6
DM
19215 case NT_GNU_GOLD_VERSION:
19216 return _("NT_GNU_GOLD_VERSION (gold version)");
9ef920e9
NC
19217 case NT_GNU_PROPERTY_TYPE_0:
19218 return _("NT_GNU_PROPERTY_TYPE_0");
19219 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19220 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
19221 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19222 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
1118d252 19223 default:
1449284b
NC
19224 {
19225 static char buff[64];
1118d252 19226
1449284b
NC
19227 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19228 return buff;
19229 }
19230 }
1118d252
RM
19231}
19232
a9eafb08
L
19233static void
19234decode_x86_compat_isa (unsigned int bitmask)
19235{
19236 while (bitmask)
19237 {
19238 unsigned int bit = bitmask & (- bitmask);
19239
19240 bitmask &= ~ bit;
19241 switch (bit)
19242 {
19243 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
19244 printf ("i486");
19245 break;
19246 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
19247 printf ("586");
19248 break;
19249 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
19250 printf ("686");
19251 break;
19252 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
19253 printf ("SSE");
19254 break;
19255 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
19256 printf ("SSE2");
19257 break;
19258 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
19259 printf ("SSE3");
19260 break;
19261 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
19262 printf ("SSSE3");
19263 break;
19264 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
19265 printf ("SSE4_1");
19266 break;
19267 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
19268 printf ("SSE4_2");
19269 break;
19270 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
19271 printf ("AVX");
19272 break;
19273 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
19274 printf ("AVX2");
19275 break;
19276 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
19277 printf ("AVX512F");
19278 break;
19279 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
19280 printf ("AVX512CD");
19281 break;
19282 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
19283 printf ("AVX512ER");
19284 break;
19285 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
19286 printf ("AVX512PF");
19287 break;
19288 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
19289 printf ("AVX512VL");
19290 break;
19291 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
19292 printf ("AVX512DQ");
19293 break;
19294 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
19295 printf ("AVX512BW");
19296 break;
65b3d26e
L
19297 default:
19298 printf (_("<unknown: %x>"), bit);
19299 break;
a9eafb08
L
19300 }
19301 if (bitmask)
19302 printf (", ");
19303 }
19304}
19305
9ef920e9 19306static void
32930e4e 19307decode_x86_compat_2_isa (unsigned int bitmask)
9ef920e9 19308{
0a59decb 19309 if (!bitmask)
90c745dc
L
19310 {
19311 printf (_("<None>"));
19312 return;
19313 }
90c745dc 19314
9ef920e9
NC
19315 while (bitmask)
19316 {
1fc87489 19317 unsigned int bit = bitmask & (- bitmask);
9ef920e9
NC
19318
19319 bitmask &= ~ bit;
19320 switch (bit)
19321 {
32930e4e 19322 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
a9eafb08
L
19323 printf ("CMOV");
19324 break;
32930e4e 19325 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
a9eafb08
L
19326 printf ("SSE");
19327 break;
32930e4e 19328 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
a9eafb08
L
19329 printf ("SSE2");
19330 break;
32930e4e 19331 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
a9eafb08
L
19332 printf ("SSE3");
19333 break;
32930e4e 19334 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
a9eafb08
L
19335 printf ("SSSE3");
19336 break;
32930e4e 19337 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
a9eafb08
L
19338 printf ("SSE4_1");
19339 break;
32930e4e 19340 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
a9eafb08
L
19341 printf ("SSE4_2");
19342 break;
32930e4e 19343 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
a9eafb08
L
19344 printf ("AVX");
19345 break;
32930e4e 19346 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
a9eafb08
L
19347 printf ("AVX2");
19348 break;
32930e4e 19349 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
a9eafb08
L
19350 printf ("FMA");
19351 break;
32930e4e 19352 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
a9eafb08
L
19353 printf ("AVX512F");
19354 break;
32930e4e 19355 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
a9eafb08
L
19356 printf ("AVX512CD");
19357 break;
32930e4e 19358 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
a9eafb08
L
19359 printf ("AVX512ER");
19360 break;
32930e4e 19361 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
a9eafb08
L
19362 printf ("AVX512PF");
19363 break;
32930e4e 19364 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
a9eafb08
L
19365 printf ("AVX512VL");
19366 break;
32930e4e 19367 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
a9eafb08
L
19368 printf ("AVX512DQ");
19369 break;
32930e4e 19370 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
a9eafb08
L
19371 printf ("AVX512BW");
19372 break;
32930e4e 19373 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
a9eafb08
L
19374 printf ("AVX512_4FMAPS");
19375 break;
32930e4e 19376 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
a9eafb08
L
19377 printf ("AVX512_4VNNIW");
19378 break;
32930e4e 19379 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
a9eafb08
L
19380 printf ("AVX512_BITALG");
19381 break;
32930e4e 19382 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
a9eafb08
L
19383 printf ("AVX512_IFMA");
19384 break;
32930e4e 19385 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
a9eafb08
L
19386 printf ("AVX512_VBMI");
19387 break;
32930e4e 19388 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
a9eafb08
L
19389 printf ("AVX512_VBMI2");
19390 break;
32930e4e 19391 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
a9eafb08
L
19392 printf ("AVX512_VNNI");
19393 break;
32930e4e 19394 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
462cac58
L
19395 printf ("AVX512_BF16");
19396 break;
65b3d26e
L
19397 default:
19398 printf (_("<unknown: %x>"), bit);
19399 break;
9ef920e9
NC
19400 }
19401 if (bitmask)
19402 printf (", ");
19403 }
19404}
19405
32930e4e
L
19406static void
19407decode_x86_isa (unsigned int bitmask)
19408{
32930e4e
L
19409 while (bitmask)
19410 {
19411 unsigned int bit = bitmask & (- bitmask);
19412
19413 bitmask &= ~ bit;
19414 switch (bit)
19415 {
b0ab0693
L
19416 case GNU_PROPERTY_X86_ISA_1_BASELINE:
19417 printf ("x86-64-baseline");
19418 break;
32930e4e
L
19419 case GNU_PROPERTY_X86_ISA_1_V2:
19420 printf ("x86-64-v2");
19421 break;
19422 case GNU_PROPERTY_X86_ISA_1_V3:
19423 printf ("x86-64-v3");
19424 break;
19425 case GNU_PROPERTY_X86_ISA_1_V4:
19426 printf ("x86-64-v4");
19427 break;
19428 default:
19429 printf (_("<unknown: %x>"), bit);
19430 break;
19431 }
19432 if (bitmask)
19433 printf (", ");
19434 }
19435}
19436
ee2fdd6f 19437static void
a9eafb08 19438decode_x86_feature_1 (unsigned int bitmask)
ee2fdd6f 19439{
0a59decb 19440 if (!bitmask)
90c745dc
L
19441 {
19442 printf (_("<None>"));
19443 return;
19444 }
90c745dc 19445
ee2fdd6f
L
19446 while (bitmask)
19447 {
19448 unsigned int bit = bitmask & (- bitmask);
19449
19450 bitmask &= ~ bit;
19451 switch (bit)
19452 {
19453 case GNU_PROPERTY_X86_FEATURE_1_IBT:
a9eafb08 19454 printf ("IBT");
ee2fdd6f 19455 break;
48580982 19456 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
a9eafb08 19457 printf ("SHSTK");
48580982 19458 break;
279d901e
L
19459 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
19460 printf ("LAM_U48");
19461 break;
19462 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
19463 printf ("LAM_U57");
19464 break;
ee2fdd6f
L
19465 default:
19466 printf (_("<unknown: %x>"), bit);
19467 break;
19468 }
19469 if (bitmask)
19470 printf (", ");
19471 }
19472}
19473
a9eafb08
L
19474static void
19475decode_x86_feature_2 (unsigned int bitmask)
19476{
0a59decb 19477 if (!bitmask)
90c745dc
L
19478 {
19479 printf (_("<None>"));
19480 return;
19481 }
90c745dc 19482
a9eafb08
L
19483 while (bitmask)
19484 {
19485 unsigned int bit = bitmask & (- bitmask);
19486
19487 bitmask &= ~ bit;
19488 switch (bit)
19489 {
19490 case GNU_PROPERTY_X86_FEATURE_2_X86:
19491 printf ("x86");
19492 break;
19493 case GNU_PROPERTY_X86_FEATURE_2_X87:
19494 printf ("x87");
19495 break;
19496 case GNU_PROPERTY_X86_FEATURE_2_MMX:
19497 printf ("MMX");
19498 break;
19499 case GNU_PROPERTY_X86_FEATURE_2_XMM:
19500 printf ("XMM");
19501 break;
19502 case GNU_PROPERTY_X86_FEATURE_2_YMM:
19503 printf ("YMM");
19504 break;
19505 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
19506 printf ("ZMM");
19507 break;
a308b89d
L
19508 case GNU_PROPERTY_X86_FEATURE_2_TMM:
19509 printf ("TMM");
19510 break;
32930e4e
L
19511 case GNU_PROPERTY_X86_FEATURE_2_MASK:
19512 printf ("MASK");
19513 break;
a9eafb08
L
19514 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
19515 printf ("FXSR");
19516 break;
19517 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
19518 printf ("XSAVE");
19519 break;
19520 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
19521 printf ("XSAVEOPT");
19522 break;
19523 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
19524 printf ("XSAVEC");
19525 break;
65b3d26e
L
19526 default:
19527 printf (_("<unknown: %x>"), bit);
19528 break;
a9eafb08
L
19529 }
19530 if (bitmask)
19531 printf (", ");
19532 }
19533}
19534
cd702818
SD
19535static void
19536decode_aarch64_feature_1_and (unsigned int bitmask)
19537{
19538 while (bitmask)
19539 {
19540 unsigned int bit = bitmask & (- bitmask);
19541
19542 bitmask &= ~ bit;
19543 switch (bit)
19544 {
19545 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
19546 printf ("BTI");
19547 break;
19548
19549 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
19550 printf ("PAC");
19551 break;
19552
19553 default:
19554 printf (_("<unknown: %x>"), bit);
19555 break;
19556 }
19557 if (bitmask)
19558 printf (", ");
19559 }
19560}
19561
6320fd00
L
19562static void
19563decode_1_needed (unsigned int bitmask)
19564{
19565 while (bitmask)
19566 {
19567 unsigned int bit = bitmask & (- bitmask);
19568
19569 bitmask &= ~ bit;
19570 switch (bit)
19571 {
19572 case GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
19573 printf ("indirect external access");
19574 break;
19575 default:
19576 printf (_("<unknown: %x>"), bit);
19577 break;
19578 }
19579 if (bitmask)
19580 printf (", ");
19581 }
19582}
19583
9ef920e9 19584static void
dda8d76d 19585print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
9ef920e9
NC
19586{
19587 unsigned char * ptr = (unsigned char *) pnote->descdata;
19588 unsigned char * ptr_end = ptr + pnote->descsz;
19589 unsigned int size = is_32bit_elf ? 4 : 8;
19590
19591 printf (_(" Properties: "));
19592
1fc87489 19593 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
9ef920e9
NC
19594 {
19595 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
19596 return;
19597 }
19598
6ab2c4ed 19599 while (ptr < ptr_end)
9ef920e9 19600 {
1fc87489 19601 unsigned int j;
6ab2c4ed
MC
19602 unsigned int type;
19603 unsigned int datasz;
19604
19605 if ((size_t) (ptr_end - ptr) < 8)
19606 {
19607 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
19608 break;
19609 }
19610
19611 type = byte_get (ptr, 4);
19612 datasz = byte_get (ptr + 4, 4);
9ef920e9 19613
1fc87489 19614 ptr += 8;
9ef920e9 19615
6ab2c4ed 19616 if (datasz > (size_t) (ptr_end - ptr))
9ef920e9 19617 {
1fc87489
L
19618 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
19619 type, datasz);
9ef920e9 19620 break;
1fc87489 19621 }
9ef920e9 19622
1fc87489
L
19623 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
19624 {
dda8d76d
NC
19625 if (filedata->file_header.e_machine == EM_X86_64
19626 || filedata->file_header.e_machine == EM_IAMCU
19627 || filedata->file_header.e_machine == EM_386)
1fc87489 19628 {
aa7bca9b
L
19629 unsigned int bitmask;
19630
19631 if (datasz == 4)
0a59decb 19632 bitmask = byte_get (ptr, 4);
aa7bca9b
L
19633 else
19634 bitmask = 0;
19635
1fc87489
L
19636 switch (type)
19637 {
19638 case GNU_PROPERTY_X86_ISA_1_USED:
1fc87489 19639 if (datasz != 4)
aa7bca9b
L
19640 printf (_("x86 ISA used: <corrupt length: %#x> "),
19641 datasz);
1fc87489 19642 else
aa7bca9b
L
19643 {
19644 printf ("x86 ISA used: ");
19645 decode_x86_isa (bitmask);
19646 }
1fc87489 19647 goto next;
9ef920e9 19648
1fc87489 19649 case GNU_PROPERTY_X86_ISA_1_NEEDED:
1fc87489 19650 if (datasz != 4)
aa7bca9b
L
19651 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19652 datasz);
1fc87489 19653 else
aa7bca9b
L
19654 {
19655 printf ("x86 ISA needed: ");
19656 decode_x86_isa (bitmask);
19657 }
1fc87489 19658 goto next;
9ef920e9 19659
ee2fdd6f 19660 case GNU_PROPERTY_X86_FEATURE_1_AND:
ee2fdd6f 19661 if (datasz != 4)
aa7bca9b
L
19662 printf (_("x86 feature: <corrupt length: %#x> "),
19663 datasz);
ee2fdd6f 19664 else
aa7bca9b
L
19665 {
19666 printf ("x86 feature: ");
a9eafb08
L
19667 decode_x86_feature_1 (bitmask);
19668 }
19669 goto next;
19670
19671 case GNU_PROPERTY_X86_FEATURE_2_USED:
19672 if (datasz != 4)
19673 printf (_("x86 feature used: <corrupt length: %#x> "),
19674 datasz);
19675 else
19676 {
19677 printf ("x86 feature used: ");
19678 decode_x86_feature_2 (bitmask);
19679 }
19680 goto next;
19681
19682 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
19683 if (datasz != 4)
19684 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
19685 else
19686 {
19687 printf ("x86 feature needed: ");
19688 decode_x86_feature_2 (bitmask);
19689 }
19690 goto next;
19691
19692 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
19693 if (datasz != 4)
19694 printf (_("x86 ISA used: <corrupt length: %#x> "),
19695 datasz);
19696 else
19697 {
19698 printf ("x86 ISA used: ");
19699 decode_x86_compat_isa (bitmask);
19700 }
19701 goto next;
19702
19703 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
19704 if (datasz != 4)
19705 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19706 datasz);
19707 else
19708 {
19709 printf ("x86 ISA needed: ");
19710 decode_x86_compat_isa (bitmask);
aa7bca9b 19711 }
ee2fdd6f
L
19712 goto next;
19713
32930e4e
L
19714 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
19715 if (datasz != 4)
19716 printf (_("x86 ISA used: <corrupt length: %#x> "),
19717 datasz);
19718 else
19719 {
19720 printf ("x86 ISA used: ");
19721 decode_x86_compat_2_isa (bitmask);
19722 }
19723 goto next;
19724
19725 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
19726 if (datasz != 4)
19727 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19728 datasz);
19729 else
19730 {
19731 printf ("x86 ISA needed: ");
19732 decode_x86_compat_2_isa (bitmask);
19733 }
19734 goto next;
19735
1fc87489
L
19736 default:
19737 break;
19738 }
19739 }
cd702818
SD
19740 else if (filedata->file_header.e_machine == EM_AARCH64)
19741 {
19742 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
19743 {
19744 printf ("AArch64 feature: ");
19745 if (datasz != 4)
19746 printf (_("<corrupt length: %#x> "), datasz);
19747 else
19748 decode_aarch64_feature_1_and (byte_get (ptr, 4));
19749 goto next;
19750 }
19751 }
1fc87489
L
19752 }
19753 else
19754 {
19755 switch (type)
9ef920e9 19756 {
1fc87489
L
19757 case GNU_PROPERTY_STACK_SIZE:
19758 printf (_("stack size: "));
19759 if (datasz != size)
19760 printf (_("<corrupt length: %#x> "), datasz);
19761 else
19762 printf ("%#lx", (unsigned long) byte_get (ptr, size));
19763 goto next;
19764
19765 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
19766 printf ("no copy on protected ");
19767 if (datasz)
19768 printf (_("<corrupt length: %#x> "), datasz);
19769 goto next;
19770
19771 default:
5a767724
L
19772 if ((type >= GNU_PROPERTY_UINT32_AND_LO
19773 && type <= GNU_PROPERTY_UINT32_AND_HI)
19774 || (type >= GNU_PROPERTY_UINT32_OR_LO
19775 && type <= GNU_PROPERTY_UINT32_OR_HI))
19776 {
6320fd00
L
19777 switch (type)
19778 {
19779 case GNU_PROPERTY_1_NEEDED:
19780 if (datasz != 4)
19781 printf (_("1_needed: <corrupt length: %#x> "),
19782 datasz);
19783 else
19784 {
19785 unsigned int bitmask = byte_get (ptr, 4);
19786 printf ("1_needed: ");
19787 decode_1_needed (bitmask);
19788 }
19789 goto next;
19790
19791 default:
19792 break;
19793 }
5a767724
L
19794 if (type <= GNU_PROPERTY_UINT32_AND_HI)
19795 printf (_("UINT32_AND (%#x): "), type);
19796 else
19797 printf (_("UINT32_OR (%#x): "), type);
19798 if (datasz != 4)
19799 printf (_("<corrupt length: %#x> "), datasz);
19800 else
19801 printf ("%#x", (unsigned int) byte_get (ptr, 4));
19802 goto next;
19803 }
9ef920e9
NC
19804 break;
19805 }
9ef920e9
NC
19806 }
19807
1fc87489
L
19808 if (type < GNU_PROPERTY_LOPROC)
19809 printf (_("<unknown type %#x data: "), type);
19810 else if (type < GNU_PROPERTY_LOUSER)
8c3853d9 19811 printf (_("<processor-specific type %#x data: "), type);
1fc87489
L
19812 else
19813 printf (_("<application-specific type %#x data: "), type);
19814 for (j = 0; j < datasz; ++j)
19815 printf ("%02x ", ptr[j] & 0xff);
19816 printf (">");
19817
dc1e8a47 19818 next:
9ef920e9 19819 ptr += ((datasz + (size - 1)) & ~ (size - 1));
1fc87489
L
19820 if (ptr == ptr_end)
19821 break;
1fc87489 19822
6ab2c4ed
MC
19823 if (do_wide)
19824 printf (", ");
19825 else
19826 printf ("\n\t");
9ef920e9
NC
19827 }
19828
19829 printf ("\n");
19830}
19831
015dc7e1 19832static bool
dda8d76d 19833print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
664f90a3 19834{
1449284b 19835 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
664f90a3
TT
19836 switch (pnote->type)
19837 {
19838 case NT_GNU_BUILD_ID:
19839 {
19840 unsigned long i;
19841
19842 printf (_(" Build ID: "));
19843 for (i = 0; i < pnote->descsz; ++i)
19844 printf ("%02x", pnote->descdata[i] & 0xff);
9cf03b7e 19845 printf ("\n");
664f90a3
TT
19846 }
19847 break;
19848
19849 case NT_GNU_ABI_TAG:
19850 {
19851 unsigned long os, major, minor, subminor;
19852 const char *osname;
19853
3102e897
NC
19854 /* PR 17531: file: 030-599401-0.004. */
19855 if (pnote->descsz < 16)
19856 {
19857 printf (_(" <corrupt GNU_ABI_TAG>\n"));
19858 break;
19859 }
19860
664f90a3
TT
19861 os = byte_get ((unsigned char *) pnote->descdata, 4);
19862 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
19863 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
19864 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
19865
19866 switch (os)
19867 {
19868 case GNU_ABI_TAG_LINUX:
19869 osname = "Linux";
19870 break;
19871 case GNU_ABI_TAG_HURD:
19872 osname = "Hurd";
19873 break;
19874 case GNU_ABI_TAG_SOLARIS:
19875 osname = "Solaris";
19876 break;
19877 case GNU_ABI_TAG_FREEBSD:
19878 osname = "FreeBSD";
19879 break;
19880 case GNU_ABI_TAG_NETBSD:
19881 osname = "NetBSD";
19882 break;
14ae95f2
RM
19883 case GNU_ABI_TAG_SYLLABLE:
19884 osname = "Syllable";
19885 break;
19886 case GNU_ABI_TAG_NACL:
19887 osname = "NaCl";
19888 break;
664f90a3
TT
19889 default:
19890 osname = "Unknown";
19891 break;
19892 }
19893
19894 printf (_(" OS: %s, ABI: %ld.%ld.%ld\n"), osname,
19895 major, minor, subminor);
19896 }
19897 break;
926c5385
CC
19898
19899 case NT_GNU_GOLD_VERSION:
19900 {
19901 unsigned long i;
19902
19903 printf (_(" Version: "));
19904 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
19905 printf ("%c", pnote->descdata[i]);
19906 printf ("\n");
19907 }
19908 break;
1449284b
NC
19909
19910 case NT_GNU_HWCAP:
19911 {
19912 unsigned long num_entries, mask;
19913
19914 /* Hardware capabilities information. Word 0 is the number of entries.
19915 Word 1 is a bitmask of enabled entries. The rest of the descriptor
19916 is a series of entries, where each entry is a single byte followed
19917 by a nul terminated string. The byte gives the bit number to test
19918 if enabled in the bitmask. */
19919 printf (_(" Hardware Capabilities: "));
19920 if (pnote->descsz < 8)
19921 {
32ec8896 19922 error (_("<corrupt GNU_HWCAP>\n"));
015dc7e1 19923 return false;
1449284b
NC
19924 }
19925 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
19926 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
19927 printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
19928 /* FIXME: Add code to display the entries... */
19929 }
19930 break;
19931
9ef920e9 19932 case NT_GNU_PROPERTY_TYPE_0:
dda8d76d 19933 print_gnu_property_note (filedata, pnote);
9ef920e9 19934 break;
9abca702 19935
1449284b
NC
19936 default:
19937 /* Handle unrecognised types. An error message should have already been
19938 created by get_gnu_elf_note_type(), so all that we need to do is to
19939 display the data. */
19940 {
19941 unsigned long i;
19942
19943 printf (_(" Description data: "));
19944 for (i = 0; i < pnote->descsz; ++i)
19945 printf ("%02x ", pnote->descdata[i] & 0xff);
19946 printf ("\n");
19947 }
19948 break;
664f90a3
TT
19949 }
19950
015dc7e1 19951 return true;
664f90a3
TT
19952}
19953
685080f2
NC
19954static const char *
19955get_v850_elf_note_type (enum v850_notes n_type)
19956{
19957 static char buff[64];
19958
19959 switch (n_type)
19960 {
19961 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
19962 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
19963 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
19964 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
19965 case V850_NOTE_CACHE_INFO: return _("Use of cache");
19966 case V850_NOTE_MMU_INFO: return _("Use of MMU");
19967 default:
19968 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
19969 return buff;
19970 }
19971}
19972
015dc7e1 19973static bool
685080f2
NC
19974print_v850_note (Elf_Internal_Note * pnote)
19975{
19976 unsigned int val;
19977
19978 if (pnote->descsz != 4)
015dc7e1 19979 return false;
32ec8896 19980
685080f2
NC
19981 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
19982
19983 if (val == 0)
19984 {
19985 printf (_("not set\n"));
015dc7e1 19986 return true;
685080f2
NC
19987 }
19988
19989 switch (pnote->type)
19990 {
19991 case V850_NOTE_ALIGNMENT:
19992 switch (val)
19993 {
015dc7e1
AM
19994 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
19995 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
685080f2
NC
19996 }
19997 break;
14ae95f2 19998
685080f2
NC
19999 case V850_NOTE_DATA_SIZE:
20000 switch (val)
20001 {
015dc7e1
AM
20002 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
20003 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
685080f2
NC
20004 }
20005 break;
14ae95f2 20006
685080f2
NC
20007 case V850_NOTE_FPU_INFO:
20008 switch (val)
20009 {
015dc7e1
AM
20010 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
20011 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
685080f2
NC
20012 }
20013 break;
14ae95f2 20014
685080f2
NC
20015 case V850_NOTE_MMU_INFO:
20016 case V850_NOTE_CACHE_INFO:
20017 case V850_NOTE_SIMD_INFO:
20018 if (val == EF_RH850_SIMD)
20019 {
20020 printf (_("yes\n"));
015dc7e1 20021 return true;
685080f2
NC
20022 }
20023 break;
20024
20025 default:
20026 /* An 'unknown note type' message will already have been displayed. */
20027 break;
20028 }
20029
20030 printf (_("unknown value: %x\n"), val);
015dc7e1 20031 return false;
685080f2
NC
20032}
20033
015dc7e1 20034static bool
c6056a74
SF
20035process_netbsd_elf_note (Elf_Internal_Note * pnote)
20036{
20037 unsigned int version;
20038
20039 switch (pnote->type)
20040 {
20041 case NT_NETBSD_IDENT:
b966f55f
AM
20042 if (pnote->descsz < 1)
20043 break;
c6056a74
SF
20044 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20045 if ((version / 10000) % 100)
b966f55f 20046 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
c6056a74
SF
20047 version, version / 100000000, (version / 1000000) % 100,
20048 (version / 10000) % 100 > 26 ? "Z" : "",
15f205b1 20049 'A' + (version / 10000) % 26);
c6056a74
SF
20050 else
20051 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
b966f55f 20052 version, version / 100000000, (version / 1000000) % 100,
15f205b1 20053 (version / 100) % 100);
015dc7e1 20054 return true;
c6056a74
SF
20055
20056 case NT_NETBSD_MARCH:
9abca702 20057 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
c6056a74 20058 pnote->descdata);
015dc7e1 20059 return true;
c6056a74 20060
9abca702 20061 case NT_NETBSD_PAX:
b966f55f
AM
20062 if (pnote->descsz < 1)
20063 break;
9abca702
CZ
20064 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
20065 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
20066 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
20067 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
20068 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
20069 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
20070 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
20071 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
015dc7e1 20072 return true;
c6056a74 20073 }
b966f55f
AM
20074
20075 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
20076 pnote->descsz, pnote->type);
015dc7e1 20077 return false;
c6056a74
SF
20078}
20079
f4ddf30f 20080static const char *
dda8d76d 20081get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
f4ddf30f 20082{
f4ddf30f
JB
20083 switch (e_type)
20084 {
20085 case NT_FREEBSD_THRMISC:
20086 return _("NT_THRMISC (thrmisc structure)");
20087 case NT_FREEBSD_PROCSTAT_PROC:
20088 return _("NT_PROCSTAT_PROC (proc data)");
20089 case NT_FREEBSD_PROCSTAT_FILES:
20090 return _("NT_PROCSTAT_FILES (files data)");
20091 case NT_FREEBSD_PROCSTAT_VMMAP:
20092 return _("NT_PROCSTAT_VMMAP (vmmap data)");
20093 case NT_FREEBSD_PROCSTAT_GROUPS:
20094 return _("NT_PROCSTAT_GROUPS (groups data)");
20095 case NT_FREEBSD_PROCSTAT_UMASK:
20096 return _("NT_PROCSTAT_UMASK (umask data)");
20097 case NT_FREEBSD_PROCSTAT_RLIMIT:
20098 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
20099 case NT_FREEBSD_PROCSTAT_OSREL:
20100 return _("NT_PROCSTAT_OSREL (osreldate data)");
20101 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
20102 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
20103 case NT_FREEBSD_PROCSTAT_AUXV:
20104 return _("NT_PROCSTAT_AUXV (auxv data)");
0b9305ed
JB
20105 case NT_FREEBSD_PTLWPINFO:
20106 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
f4ddf30f 20107 }
dda8d76d 20108 return get_note_type (filedata, e_type);
f4ddf30f
JB
20109}
20110
9437c45b 20111static const char *
dda8d76d 20112get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
9437c45b
JT
20113{
20114 static char buff[64];
20115
540e6170
CZ
20116 switch (e_type)
20117 {
20118 case NT_NETBSDCORE_PROCINFO:
20119 /* NetBSD core "procinfo" structure. */
20120 return _("NetBSD procinfo structure");
9437c45b 20121
540e6170
CZ
20122 case NT_NETBSDCORE_AUXV:
20123 return _("NetBSD ELF auxiliary vector data");
9437c45b 20124
06d949ec
KR
20125 case NT_NETBSDCORE_LWPSTATUS:
20126 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
06d949ec 20127
540e6170 20128 default:
06d949ec 20129 /* As of Jan 2020 there are no other machine-independent notes
540e6170
CZ
20130 defined for NetBSD core files. If the note type is less
20131 than the start of the machine-dependent note types, we don't
20132 understand it. */
20133
20134 if (e_type < NT_NETBSDCORE_FIRSTMACH)
20135 {
20136 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20137 return buff;
20138 }
20139 break;
9437c45b
JT
20140 }
20141
dda8d76d 20142 switch (filedata->file_header.e_machine)
9437c45b
JT
20143 {
20144 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
20145 and PT_GETFPREGS == mach+2. */
20146
20147 case EM_OLD_ALPHA:
20148 case EM_ALPHA:
20149 case EM_SPARC:
20150 case EM_SPARC32PLUS:
20151 case EM_SPARCV9:
20152 switch (e_type)
20153 {
2b692964 20154 case NT_NETBSDCORE_FIRSTMACH + 0:
b4db1224 20155 return _("PT_GETREGS (reg structure)");
2b692964 20156 case NT_NETBSDCORE_FIRSTMACH + 2:
b4db1224 20157 return _("PT_GETFPREGS (fpreg structure)");
9437c45b
JT
20158 default:
20159 break;
20160 }
20161 break;
20162
c0d38b0e
CZ
20163 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
20164 There's also old PT___GETREGS40 == mach + 1 for old reg
20165 structure which lacks GBR. */
20166 case EM_SH:
20167 switch (e_type)
20168 {
20169 case NT_NETBSDCORE_FIRSTMACH + 1:
20170 return _("PT___GETREGS40 (old reg structure)");
20171 case NT_NETBSDCORE_FIRSTMACH + 3:
20172 return _("PT_GETREGS (reg structure)");
20173 case NT_NETBSDCORE_FIRSTMACH + 5:
20174 return _("PT_GETFPREGS (fpreg structure)");
20175 default:
20176 break;
20177 }
20178 break;
20179
9437c45b
JT
20180 /* On all other arch's, PT_GETREGS == mach+1 and
20181 PT_GETFPREGS == mach+3. */
20182 default:
20183 switch (e_type)
20184 {
2b692964 20185 case NT_NETBSDCORE_FIRSTMACH + 1:
b4db1224 20186 return _("PT_GETREGS (reg structure)");
2b692964 20187 case NT_NETBSDCORE_FIRSTMACH + 3:
b4db1224 20188 return _("PT_GETFPREGS (fpreg structure)");
9437c45b
JT
20189 default:
20190 break;
20191 }
20192 }
20193
9cf03b7e 20194 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
e9e44622 20195 e_type - NT_NETBSDCORE_FIRSTMACH);
9437c45b
JT
20196 return buff;
20197}
20198
98ca73af
FC
20199static const char *
20200get_openbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20201{
20202 switch (e_type)
20203 {
20204 case NT_OPENBSD_PROCINFO:
20205 return _("OpenBSD procinfo structure");
20206 case NT_OPENBSD_AUXV:
20207 return _("OpenBSD ELF auxiliary vector data");
20208 case NT_OPENBSD_REGS:
20209 return _("OpenBSD regular registers");
20210 case NT_OPENBSD_FPREGS:
20211 return _("OpenBSD floating point registers");
20212 case NT_OPENBSD_WCOOKIE:
20213 return _("OpenBSD window cookie");
20214 }
20215
20216 return get_note_type (filedata, e_type);
20217}
20218
70616151
TT
20219static const char *
20220get_stapsdt_note_type (unsigned e_type)
20221{
20222 static char buff[64];
20223
20224 switch (e_type)
20225 {
20226 case NT_STAPSDT:
20227 return _("NT_STAPSDT (SystemTap probe descriptors)");
20228
20229 default:
20230 break;
20231 }
20232
20233 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20234 return buff;
20235}
20236
015dc7e1 20237static bool
c6a9fc58
TT
20238print_stapsdt_note (Elf_Internal_Note *pnote)
20239{
3ca60c57
NC
20240 size_t len, maxlen;
20241 unsigned long addr_size = is_32bit_elf ? 4 : 8;
c6a9fc58
TT
20242 char *data = pnote->descdata;
20243 char *data_end = pnote->descdata + pnote->descsz;
20244 bfd_vma pc, base_addr, semaphore;
20245 char *provider, *probe, *arg_fmt;
20246
3ca60c57
NC
20247 if (pnote->descsz < (addr_size * 3))
20248 goto stapdt_note_too_small;
20249
c6a9fc58
TT
20250 pc = byte_get ((unsigned char *) data, addr_size);
20251 data += addr_size;
3ca60c57 20252
c6a9fc58
TT
20253 base_addr = byte_get ((unsigned char *) data, addr_size);
20254 data += addr_size;
3ca60c57 20255
c6a9fc58
TT
20256 semaphore = byte_get ((unsigned char *) data, addr_size);
20257 data += addr_size;
20258
3ca60c57
NC
20259 if (data >= data_end)
20260 goto stapdt_note_too_small;
20261 maxlen = data_end - data;
20262 len = strnlen (data, maxlen);
20263 if (len < maxlen)
20264 {
20265 provider = data;
20266 data += len + 1;
20267 }
20268 else
20269 goto stapdt_note_too_small;
20270
20271 if (data >= data_end)
20272 goto stapdt_note_too_small;
20273 maxlen = data_end - data;
20274 len = strnlen (data, maxlen);
20275 if (len < maxlen)
20276 {
20277 probe = data;
20278 data += len + 1;
20279 }
20280 else
20281 goto stapdt_note_too_small;
9abca702 20282
3ca60c57
NC
20283 if (data >= data_end)
20284 goto stapdt_note_too_small;
20285 maxlen = data_end - data;
20286 len = strnlen (data, maxlen);
20287 if (len < maxlen)
20288 {
20289 arg_fmt = data;
20290 data += len + 1;
20291 }
20292 else
20293 goto stapdt_note_too_small;
c6a9fc58
TT
20294
20295 printf (_(" Provider: %s\n"), provider);
20296 printf (_(" Name: %s\n"), probe);
20297 printf (_(" Location: "));
20298 print_vma (pc, FULL_HEX);
20299 printf (_(", Base: "));
20300 print_vma (base_addr, FULL_HEX);
20301 printf (_(", Semaphore: "));
20302 print_vma (semaphore, FULL_HEX);
9cf03b7e 20303 printf ("\n");
c6a9fc58
TT
20304 printf (_(" Arguments: %s\n"), arg_fmt);
20305
20306 return data == data_end;
3ca60c57
NC
20307
20308 stapdt_note_too_small:
20309 printf (_(" <corrupt - note is too small>\n"));
20310 error (_("corrupt stapdt note - the data size is too small\n"));
015dc7e1 20311 return false;
c6a9fc58
TT
20312}
20313
00e98fc7
TG
20314static const char *
20315get_ia64_vms_note_type (unsigned e_type)
20316{
20317 static char buff[64];
20318
20319 switch (e_type)
20320 {
20321 case NT_VMS_MHD:
20322 return _("NT_VMS_MHD (module header)");
20323 case NT_VMS_LNM:
20324 return _("NT_VMS_LNM (language name)");
20325 case NT_VMS_SRC:
20326 return _("NT_VMS_SRC (source files)");
20327 case NT_VMS_TITLE:
9cf03b7e 20328 return "NT_VMS_TITLE";
00e98fc7
TG
20329 case NT_VMS_EIDC:
20330 return _("NT_VMS_EIDC (consistency check)");
20331 case NT_VMS_FPMODE:
20332 return _("NT_VMS_FPMODE (FP mode)");
20333 case NT_VMS_LINKTIME:
9cf03b7e 20334 return "NT_VMS_LINKTIME";
00e98fc7
TG
20335 case NT_VMS_IMGNAM:
20336 return _("NT_VMS_IMGNAM (image name)");
20337 case NT_VMS_IMGID:
20338 return _("NT_VMS_IMGID (image id)");
20339 case NT_VMS_LINKID:
20340 return _("NT_VMS_LINKID (link id)");
20341 case NT_VMS_IMGBID:
20342 return _("NT_VMS_IMGBID (build id)");
20343 case NT_VMS_GSTNAM:
20344 return _("NT_VMS_GSTNAM (sym table name)");
20345 case NT_VMS_ORIG_DYN:
9cf03b7e 20346 return "NT_VMS_ORIG_DYN";
00e98fc7 20347 case NT_VMS_PATCHTIME:
9cf03b7e 20348 return "NT_VMS_PATCHTIME";
00e98fc7
TG
20349 default:
20350 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20351 return buff;
20352 }
20353}
20354
015dc7e1 20355static bool
00e98fc7
TG
20356print_ia64_vms_note (Elf_Internal_Note * pnote)
20357{
8d18bf79
NC
20358 int maxlen = pnote->descsz;
20359
20360 if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
20361 goto desc_size_fail;
20362
00e98fc7
TG
20363 switch (pnote->type)
20364 {
20365 case NT_VMS_MHD:
8d18bf79
NC
20366 if (maxlen <= 36)
20367 goto desc_size_fail;
20368
20369 int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
20370
20371 printf (_(" Creation date : %.17s\n"), pnote->descdata);
20372 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
20373 if (l + 34 < maxlen)
20374 {
20375 printf (_(" Module name : %s\n"), pnote->descdata + 34);
20376 if (l + 35 < maxlen)
20377 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
20378 else
20379 printf (_(" Module version : <missing>\n"));
20380 }
00e98fc7 20381 else
8d18bf79
NC
20382 {
20383 printf (_(" Module name : <missing>\n"));
20384 printf (_(" Module version : <missing>\n"));
20385 }
00e98fc7 20386 break;
8d18bf79 20387
00e98fc7 20388 case NT_VMS_LNM:
8d18bf79 20389 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
00e98fc7 20390 break;
8d18bf79 20391
00e98fc7
TG
20392#ifdef BFD64
20393 case NT_VMS_FPMODE:
9cf03b7e 20394 printf (_(" Floating Point mode: "));
8d18bf79
NC
20395 if (maxlen < 8)
20396 goto desc_size_fail;
20397 /* FIXME: Generate an error if descsz > 8 ? */
20398
4a5cb34f 20399 printf ("0x%016" BFD_VMA_FMT "x\n",
8d18bf79 20400 (bfd_vma) byte_get ((unsigned char *)pnote->descdata, 8));
00e98fc7 20401 break;
8d18bf79 20402
00e98fc7
TG
20403 case NT_VMS_LINKTIME:
20404 printf (_(" Link time: "));
8d18bf79
NC
20405 if (maxlen < 8)
20406 goto desc_size_fail;
20407 /* FIXME: Generate an error if descsz > 8 ? */
20408
00e98fc7 20409 print_vms_time
8d18bf79 20410 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
00e98fc7
TG
20411 printf ("\n");
20412 break;
8d18bf79 20413
00e98fc7
TG
20414 case NT_VMS_PATCHTIME:
20415 printf (_(" Patch time: "));
8d18bf79
NC
20416 if (maxlen < 8)
20417 goto desc_size_fail;
20418 /* FIXME: Generate an error if descsz > 8 ? */
20419
00e98fc7 20420 print_vms_time
8d18bf79 20421 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
00e98fc7
TG
20422 printf ("\n");
20423 break;
8d18bf79 20424
00e98fc7 20425 case NT_VMS_ORIG_DYN:
8d18bf79
NC
20426 if (maxlen < 34)
20427 goto desc_size_fail;
20428
00e98fc7
TG
20429 printf (_(" Major id: %u, minor id: %u\n"),
20430 (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
20431 (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
9cf03b7e 20432 printf (_(" Last modified : "));
00e98fc7
TG
20433 print_vms_time
20434 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
9cf03b7e 20435 printf (_("\n Link flags : "));
4a5cb34f 20436 printf ("0x%016" BFD_VMA_FMT "x\n",
948f632f 20437 (bfd_vma) byte_get ((unsigned char *)pnote->descdata + 16, 8));
00e98fc7 20438 printf (_(" Header flags: 0x%08x\n"),
948f632f 20439 (unsigned) byte_get ((unsigned char *)pnote->descdata + 24, 4));
8d18bf79 20440 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
00e98fc7
TG
20441 break;
20442#endif
8d18bf79 20443
00e98fc7 20444 case NT_VMS_IMGNAM:
8d18bf79 20445 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
00e98fc7 20446 break;
8d18bf79 20447
00e98fc7 20448 case NT_VMS_GSTNAM:
8d18bf79 20449 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
00e98fc7 20450 break;
8d18bf79 20451
00e98fc7 20452 case NT_VMS_IMGID:
8d18bf79 20453 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
00e98fc7 20454 break;
8d18bf79 20455
00e98fc7 20456 case NT_VMS_LINKID:
8d18bf79 20457 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
00e98fc7 20458 break;
8d18bf79 20459
00e98fc7 20460 default:
015dc7e1 20461 return false;
00e98fc7 20462 }
8d18bf79 20463
015dc7e1 20464 return true;
8d18bf79
NC
20465
20466 desc_size_fail:
20467 printf (_(" <corrupt - data size is too small>\n"));
20468 error (_("corrupt IA64 note: data size is too small\n"));
015dc7e1 20469 return false;
00e98fc7
TG
20470}
20471
fd486f32
AM
20472struct build_attr_cache {
20473 Filedata *filedata;
20474 char *strtab;
20475 unsigned long strtablen;
20476 Elf_Internal_Sym *symtab;
20477 unsigned long nsyms;
20478} ba_cache;
20479
6f156d7a
NC
20480/* Find the symbol associated with a build attribute that is attached
20481 to address OFFSET. If PNAME is non-NULL then store the name of
20482 the symbol (if found) in the provided pointer, Returns NULL if a
20483 symbol could not be found. */
c799a79d 20484
6f156d7a 20485static Elf_Internal_Sym *
015dc7e1
AM
20486get_symbol_for_build_attribute (Filedata *filedata,
20487 unsigned long offset,
20488 bool is_open_attr,
20489 const char **pname)
9ef920e9 20490{
fd486f32
AM
20491 Elf_Internal_Sym *saved_sym = NULL;
20492 Elf_Internal_Sym *sym;
9ef920e9 20493
dda8d76d 20494 if (filedata->section_headers != NULL
fd486f32 20495 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
9ef920e9 20496 {
c799a79d 20497 Elf_Internal_Shdr * symsec;
9ef920e9 20498
fd486f32
AM
20499 free (ba_cache.strtab);
20500 ba_cache.strtab = NULL;
20501 free (ba_cache.symtab);
20502 ba_cache.symtab = NULL;
20503
c799a79d 20504 /* Load the symbol and string sections. */
dda8d76d
NC
20505 for (symsec = filedata->section_headers;
20506 symsec < filedata->section_headers + filedata->file_header.e_shnum;
c799a79d 20507 symsec ++)
9ef920e9 20508 {
28d13567
AM
20509 if (symsec->sh_type == SHT_SYMTAB
20510 && get_symtab (filedata, symsec,
20511 &ba_cache.symtab, &ba_cache.nsyms,
20512 &ba_cache.strtab, &ba_cache.strtablen))
20513 break;
9ef920e9 20514 }
fd486f32 20515 ba_cache.filedata = filedata;
9ef920e9
NC
20516 }
20517
fd486f32 20518 if (ba_cache.symtab == NULL)
6f156d7a 20519 return NULL;
9ef920e9 20520
c799a79d 20521 /* Find a symbol whose value matches offset. */
fd486f32 20522 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
c799a79d
NC
20523 if (sym->st_value == offset)
20524 {
fd486f32 20525 if (sym->st_name >= ba_cache.strtablen)
c799a79d
NC
20526 /* Huh ? This should not happen. */
20527 continue;
9ef920e9 20528
fd486f32 20529 if (ba_cache.strtab[sym->st_name] == 0)
c799a79d 20530 continue;
9ef920e9 20531
9b9b1092 20532 /* The AArch64, ARM and RISC-V architectures define mapping symbols
8fd75781 20533 (eg $d, $x, $t) which we want to ignore. */
fd486f32
AM
20534 if (ba_cache.strtab[sym->st_name] == '$'
20535 && ba_cache.strtab[sym->st_name + 1] != 0
20536 && ba_cache.strtab[sym->st_name + 2] == 0)
8fd75781
NC
20537 continue;
20538
c799a79d
NC
20539 if (is_open_attr)
20540 {
20541 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
20542 and FILE or OBJECT symbols over NOTYPE symbols. We skip
20543 FUNC symbols entirely. */
20544 switch (ELF_ST_TYPE (sym->st_info))
20545 {
c799a79d 20546 case STT_OBJECT:
6f156d7a 20547 case STT_FILE:
c799a79d 20548 saved_sym = sym;
6f156d7a
NC
20549 if (sym->st_size)
20550 {
20551 /* If the symbol has a size associated
20552 with it then we can stop searching. */
fd486f32 20553 sym = ba_cache.symtab + ba_cache.nsyms;
6f156d7a 20554 }
c799a79d 20555 continue;
9ef920e9 20556
c799a79d
NC
20557 case STT_FUNC:
20558 /* Ignore function symbols. */
20559 continue;
20560
20561 default:
20562 break;
20563 }
20564
20565 switch (ELF_ST_BIND (sym->st_info))
9ef920e9 20566 {
c799a79d
NC
20567 case STB_GLOBAL:
20568 if (saved_sym == NULL
20569 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
20570 saved_sym = sym;
20571 break;
c871dade 20572
c799a79d
NC
20573 case STB_LOCAL:
20574 if (saved_sym == NULL)
20575 saved_sym = sym;
20576 break;
20577
20578 default:
9ef920e9
NC
20579 break;
20580 }
20581 }
c799a79d
NC
20582 else
20583 {
20584 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
20585 continue;
20586
20587 saved_sym = sym;
20588 break;
20589 }
20590 }
20591
6f156d7a 20592 if (saved_sym && pname)
fd486f32 20593 * pname = ba_cache.strtab + saved_sym->st_name;
6f156d7a
NC
20594
20595 return saved_sym;
c799a79d
NC
20596}
20597
d20e98ab
NC
20598/* Returns true iff addr1 and addr2 are in the same section. */
20599
015dc7e1 20600static bool
d20e98ab
NC
20601same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
20602{
20603 Elf_Internal_Shdr * a1;
20604 Elf_Internal_Shdr * a2;
20605
20606 a1 = find_section_by_address (filedata, addr1);
20607 a2 = find_section_by_address (filedata, addr2);
9abca702 20608
d20e98ab
NC
20609 return a1 == a2 && a1 != NULL;
20610}
20611
015dc7e1 20612static bool
dda8d76d
NC
20613print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
20614 Filedata * filedata)
c799a79d 20615{
015dc7e1
AM
20616 static unsigned long global_offset = 0;
20617 static unsigned long global_end = 0;
20618 static unsigned long func_offset = 0;
20619 static unsigned long func_end = 0;
c871dade 20620
015dc7e1
AM
20621 Elf_Internal_Sym *sym;
20622 const char *name;
20623 unsigned long start;
20624 unsigned long end;
20625 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
6f156d7a
NC
20626
20627 switch (pnote->descsz)
c799a79d 20628 {
6f156d7a
NC
20629 case 0:
20630 /* A zero-length description means that the range of
20631 the previous note of the same type should be used. */
c799a79d 20632 if (is_open_attr)
c871dade 20633 {
6f156d7a
NC
20634 if (global_end > global_offset)
20635 printf (_(" Applies to region from %#lx to %#lx\n"),
20636 global_offset, global_end);
20637 else
20638 printf (_(" Applies to region from %#lx\n"), global_offset);
c799a79d
NC
20639 }
20640 else
20641 {
6f156d7a
NC
20642 if (func_end > func_offset)
20643 printf (_(" Applies to region from %#lx to %#lx\n"), func_offset, func_end);
20644 else
20645 printf (_(" Applies to region from %#lx\n"), func_offset);
c871dade 20646 }
015dc7e1 20647 return true;
9ef920e9 20648
6f156d7a
NC
20649 case 4:
20650 start = byte_get ((unsigned char *) pnote->descdata, 4);
20651 end = 0;
20652 break;
20653
20654 case 8:
c74147bb
NC
20655 start = byte_get ((unsigned char *) pnote->descdata, 4);
20656 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
6f156d7a
NC
20657 break;
20658
20659 case 16:
20660 start = byte_get ((unsigned char *) pnote->descdata, 8);
20661 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
20662 break;
9abca702 20663
6f156d7a 20664 default:
c799a79d
NC
20665 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
20666 printf (_(" <invalid descsz>"));
015dc7e1 20667 return false;
c799a79d
NC
20668 }
20669
6f156d7a
NC
20670 name = NULL;
20671 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
8fd75781
NC
20672 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
20673 in order to avoid them being confused with the start address of the
20674 first function in the file... */
20675 if (sym == NULL && is_open_attr)
20676 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
20677 & name);
6f156d7a
NC
20678
20679 if (end == 0 && sym != NULL && sym->st_size > 0)
20680 end = start + sym->st_size;
c799a79d
NC
20681
20682 if (is_open_attr)
20683 {
d20e98ab
NC
20684 /* FIXME: Need to properly allow for section alignment.
20685 16 is just the alignment used on x86_64. */
20686 if (global_end > 0
20687 && start > BFD_ALIGN (global_end, 16)
20688 /* Build notes are not guaranteed to be organised in order of
20689 increasing address, but we should find the all of the notes
20690 for one section in the same place. */
20691 && same_section (filedata, start, global_end))
6f156d7a
NC
20692 warn (_("Gap in build notes detected from %#lx to %#lx\n"),
20693 global_end + 1, start - 1);
20694
20695 printf (_(" Applies to region from %#lx"), start);
20696 global_offset = start;
20697
20698 if (end)
20699 {
20700 printf (_(" to %#lx"), end);
20701 global_end = end;
20702 }
c799a79d
NC
20703 }
20704 else
20705 {
6f156d7a
NC
20706 printf (_(" Applies to region from %#lx"), start);
20707 func_offset = start;
20708
20709 if (end)
20710 {
20711 printf (_(" to %#lx"), end);
20712 func_end = end;
20713 }
c799a79d
NC
20714 }
20715
6f156d7a
NC
20716 if (sym && name)
20717 printf (_(" (%s)"), name);
20718
20719 printf ("\n");
015dc7e1 20720 return true;
9ef920e9
NC
20721}
20722
015dc7e1 20723static bool
9ef920e9
NC
20724print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
20725{
1d15e434
NC
20726 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
20727 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
20728 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
9ef920e9
NC
20729 char name_type;
20730 char name_attribute;
1d15e434 20731 const char * expected_types;
9ef920e9
NC
20732 const char * name = pnote->namedata;
20733 const char * text;
88305e1b 20734 signed int left;
9ef920e9
NC
20735
20736 if (name == NULL || pnote->namesz < 2)
20737 {
20738 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
7296a62a 20739 print_symbol (-20, _(" <corrupt name>"));
015dc7e1 20740 return false;
9ef920e9
NC
20741 }
20742
6f156d7a
NC
20743 if (do_wide)
20744 left = 28;
20745 else
20746 left = 20;
88305e1b
NC
20747
20748 /* Version 2 of the spec adds a "GA" prefix to the name field. */
20749 if (name[0] == 'G' && name[1] == 'A')
20750 {
6f156d7a
NC
20751 if (pnote->namesz < 4)
20752 {
20753 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
20754 print_symbol (-20, _(" <corrupt name>"));
015dc7e1 20755 return false;
6f156d7a
NC
20756 }
20757
88305e1b
NC
20758 printf ("GA");
20759 name += 2;
20760 left -= 2;
20761 }
20762
9ef920e9
NC
20763 switch ((name_type = * name))
20764 {
20765 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
20766 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
20767 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
20768 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
20769 printf ("%c", * name);
88305e1b 20770 left --;
9ef920e9
NC
20771 break;
20772 default:
20773 error (_("unrecognised attribute type in name field: %d\n"), name_type);
20774 print_symbol (-20, _("<unknown name type>"));
015dc7e1 20775 return false;
9ef920e9
NC
20776 }
20777
9ef920e9
NC
20778 ++ name;
20779 text = NULL;
20780
20781 switch ((name_attribute = * name))
20782 {
20783 case GNU_BUILD_ATTRIBUTE_VERSION:
20784 text = _("<version>");
1d15e434 20785 expected_types = string_expected;
9ef920e9
NC
20786 ++ name;
20787 break;
20788 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
20789 text = _("<stack prot>");
75d7d298 20790 expected_types = "!+*";
9ef920e9
NC
20791 ++ name;
20792 break;
20793 case GNU_BUILD_ATTRIBUTE_RELRO:
20794 text = _("<relro>");
1d15e434 20795 expected_types = bool_expected;
9ef920e9
NC
20796 ++ name;
20797 break;
20798 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
20799 text = _("<stack size>");
1d15e434 20800 expected_types = number_expected;
9ef920e9
NC
20801 ++ name;
20802 break;
20803 case GNU_BUILD_ATTRIBUTE_TOOL:
20804 text = _("<tool>");
1d15e434 20805 expected_types = string_expected;
9ef920e9
NC
20806 ++ name;
20807 break;
20808 case GNU_BUILD_ATTRIBUTE_ABI:
20809 text = _("<ABI>");
20810 expected_types = "$*";
20811 ++ name;
20812 break;
20813 case GNU_BUILD_ATTRIBUTE_PIC:
20814 text = _("<PIC>");
1d15e434 20815 expected_types = number_expected;
9ef920e9
NC
20816 ++ name;
20817 break;
a8be5506
NC
20818 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
20819 text = _("<short enum>");
1d15e434 20820 expected_types = bool_expected;
a8be5506
NC
20821 ++ name;
20822 break;
9ef920e9
NC
20823 default:
20824 if (ISPRINT (* name))
20825 {
20826 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
20827
20828 if (len > left && ! do_wide)
20829 len = left;
75d7d298 20830 printf ("%.*s:", len, name);
9ef920e9 20831 left -= len;
0dd6ae21 20832 name += len;
9ef920e9
NC
20833 }
20834 else
20835 {
3e6b6445 20836 static char tmpbuf [128];
88305e1b 20837
3e6b6445
NC
20838 error (_("unrecognised byte in name field: %d\n"), * name);
20839 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
20840 text = tmpbuf;
20841 name ++;
9ef920e9
NC
20842 }
20843 expected_types = "*$!+";
20844 break;
20845 }
20846
20847 if (text)
88305e1b 20848 left -= printf ("%s", text);
9ef920e9
NC
20849
20850 if (strchr (expected_types, name_type) == NULL)
75d7d298 20851 warn (_("attribute does not have an expected type (%c)\n"), name_type);
9ef920e9
NC
20852
20853 if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
20854 {
20855 error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
20856 (unsigned long) pnote->namesz,
20857 (long) (name - pnote->namedata));
015dc7e1 20858 return false;
9ef920e9
NC
20859 }
20860
20861 if (left < 1 && ! do_wide)
015dc7e1 20862 return true;
9ef920e9
NC
20863
20864 switch (name_type)
20865 {
20866 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
20867 {
b06b2c92 20868 unsigned int bytes;
ddef72cd
NC
20869 unsigned long long val = 0;
20870 unsigned int shift = 0;
20871 char * decoded = NULL;
20872
b06b2c92
NC
20873 bytes = pnote->namesz - (name - pnote->namedata);
20874 if (bytes > 0)
20875 /* The -1 is because the name field is always 0 terminated, and we
20876 want to be able to ensure that the shift in the while loop below
20877 will not overflow. */
20878 -- bytes;
20879
ddef72cd
NC
20880 if (bytes > sizeof (val))
20881 {
3e6b6445
NC
20882 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
20883 bytes);
20884 bytes = sizeof (val);
ddef72cd 20885 }
3e6b6445
NC
20886 /* We do not bother to warn if bytes == 0 as this can
20887 happen with some early versions of the gcc plugin. */
9ef920e9
NC
20888
20889 while (bytes --)
20890 {
54b8331d 20891 unsigned long long byte = *name++ & 0xff;
79a964dc
NC
20892
20893 val |= byte << shift;
9ef920e9
NC
20894 shift += 8;
20895 }
20896
75d7d298 20897 switch (name_attribute)
9ef920e9 20898 {
75d7d298 20899 case GNU_BUILD_ATTRIBUTE_PIC:
9ef920e9
NC
20900 switch (val)
20901 {
75d7d298
NC
20902 case 0: decoded = "static"; break;
20903 case 1: decoded = "pic"; break;
20904 case 2: decoded = "PIC"; break;
20905 case 3: decoded = "pie"; break;
20906 case 4: decoded = "PIE"; break;
20907 default: break;
9ef920e9 20908 }
75d7d298
NC
20909 break;
20910 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
20911 switch (val)
9ef920e9 20912 {
75d7d298
NC
20913 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
20914 case 0: decoded = "off"; break;
20915 case 1: decoded = "on"; break;
20916 case 2: decoded = "all"; break;
20917 case 3: decoded = "strong"; break;
20918 case 4: decoded = "explicit"; break;
20919 default: break;
9ef920e9 20920 }
75d7d298
NC
20921 break;
20922 default:
20923 break;
9ef920e9
NC
20924 }
20925
75d7d298 20926 if (decoded != NULL)
3e6b6445
NC
20927 {
20928 print_symbol (-left, decoded);
20929 left = 0;
20930 }
20931 else if (val == 0)
20932 {
20933 printf ("0x0");
20934 left -= 3;
20935 }
9ef920e9 20936 else
75d7d298
NC
20937 {
20938 if (do_wide)
ddef72cd 20939 left -= printf ("0x%llx", val);
75d7d298 20940 else
ddef72cd 20941 left -= printf ("0x%-.*llx", left, val);
75d7d298 20942 }
9ef920e9
NC
20943 }
20944 break;
20945 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
20946 left -= print_symbol (- left, name);
20947 break;
20948 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
20949 left -= print_symbol (- left, "true");
20950 break;
20951 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
20952 left -= print_symbol (- left, "false");
20953 break;
20954 }
20955
20956 if (do_wide && left > 0)
20957 printf ("%-*s", left, " ");
9abca702 20958
015dc7e1 20959 return true;
9ef920e9
NC
20960}
20961
6d118b09
NC
20962/* Note that by the ELF standard, the name field is already null byte
20963 terminated, and namesz includes the terminating null byte.
20964 I.E. the value of namesz for the name "FSF" is 4.
20965
e3c8793a 20966 If the value of namesz is zero, there is no name present. */
9ef920e9 20967
015dc7e1 20968static bool
9ef920e9 20969process_note (Elf_Internal_Note * pnote,
dda8d76d 20970 Filedata * filedata)
779fe533 20971{
2cf0635d
NC
20972 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
20973 const char * nt;
9437c45b
JT
20974
20975 if (pnote->namesz == 0)
1ec5cd37
NC
20976 /* If there is no note name, then use the default set of
20977 note type strings. */
dda8d76d 20978 nt = get_note_type (filedata, pnote->type);
1ec5cd37 20979
24d127aa 20980 else if (startswith (pnote->namedata, "GNU"))
1118d252
RM
20981 /* GNU-specific object file notes. */
20982 nt = get_gnu_elf_note_type (pnote->type);
f4ddf30f 20983
24d127aa 20984 else if (startswith (pnote->namedata, "FreeBSD"))
f4ddf30f 20985 /* FreeBSD-specific core file notes. */
dda8d76d 20986 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
1118d252 20987
24d127aa 20988 else if (startswith (pnote->namedata, "NetBSD-CORE"))
1ec5cd37 20989 /* NetBSD-specific core file notes. */
dda8d76d 20990 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
1ec5cd37 20991
24d127aa 20992 else if (startswith (pnote->namedata, "NetBSD"))
c6056a74
SF
20993 /* NetBSD-specific core file notes. */
20994 return process_netbsd_elf_note (pnote);
20995
24d127aa 20996 else if (startswith (pnote->namedata, "PaX"))
9abca702
CZ
20997 /* NetBSD-specific core file notes. */
20998 return process_netbsd_elf_note (pnote);
20999
98ca73af
FC
21000 else if (startswith (pnote->namedata, "OpenBSD"))
21001 /* OpenBSD-specific core file notes. */
21002 nt = get_openbsd_elfcore_note_type (filedata, pnote->type);
21003
e9b095a5 21004 else if (startswith (pnote->namedata, "SPU/"))
b15fa79e
AM
21005 {
21006 /* SPU-specific core file notes. */
21007 nt = pnote->namedata + 4;
21008 name = "SPU";
21009 }
21010
24d127aa 21011 else if (startswith (pnote->namedata, "IPF/VMS"))
00e98fc7
TG
21012 /* VMS/ia64-specific file notes. */
21013 nt = get_ia64_vms_note_type (pnote->type);
21014
24d127aa 21015 else if (startswith (pnote->namedata, "stapsdt"))
70616151
TT
21016 nt = get_stapsdt_note_type (pnote->type);
21017
9437c45b 21018 else
1ec5cd37
NC
21019 /* Don't recognize this note name; just use the default set of
21020 note type strings. */
dda8d76d 21021 nt = get_note_type (filedata, pnote->type);
9437c45b 21022
1449284b 21023 printf (" ");
9ef920e9 21024
24d127aa 21025 if (((startswith (pnote->namedata, "GA")
483767a3
AM
21026 && strchr ("*$!+", pnote->namedata[2]) != NULL)
21027 || strchr ("*$!+", pnote->namedata[0]) != NULL)
21028 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21029 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
9ef920e9
NC
21030 print_gnu_build_attribute_name (pnote);
21031 else
21032 print_symbol (-20, name);
21033
21034 if (do_wide)
21035 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
21036 else
21037 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
00e98fc7 21038
24d127aa 21039 if (startswith (pnote->namedata, "IPF/VMS"))
00e98fc7 21040 return print_ia64_vms_note (pnote);
24d127aa 21041 else if (startswith (pnote->namedata, "GNU"))
dda8d76d 21042 return print_gnu_note (filedata, pnote);
24d127aa 21043 else if (startswith (pnote->namedata, "stapsdt"))
c6a9fc58 21044 return print_stapsdt_note (pnote);
24d127aa 21045 else if (startswith (pnote->namedata, "CORE"))
9ece1fa9 21046 return print_core_note (pnote);
24d127aa 21047 else if (((startswith (pnote->namedata, "GA")
483767a3
AM
21048 && strchr ("*$!+", pnote->namedata[2]) != NULL)
21049 || strchr ("*$!+", pnote->namedata[0]) != NULL)
21050 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
21051 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
dda8d76d 21052 return print_gnu_build_attribute_description (pnote, filedata);
779fe533 21053
9ef920e9 21054 if (pnote->descsz)
1449284b
NC
21055 {
21056 unsigned long i;
21057
21058 printf (_(" description data: "));
21059 for (i = 0; i < pnote->descsz; i++)
178d8719 21060 printf ("%02x ", pnote->descdata[i] & 0xff);
04ac15ab
AS
21061 if (!do_wide)
21062 printf ("\n");
1449284b
NC
21063 }
21064
9ef920e9
NC
21065 if (do_wide)
21066 printf ("\n");
21067
015dc7e1 21068 return true;
1449284b 21069}
6d118b09 21070
015dc7e1 21071static bool
dda8d76d
NC
21072process_notes_at (Filedata * filedata,
21073 Elf_Internal_Shdr * section,
21074 bfd_vma offset,
82ed9683
L
21075 bfd_vma length,
21076 bfd_vma align)
779fe533 21077{
015dc7e1
AM
21078 Elf_External_Note *pnotes;
21079 Elf_External_Note *external;
21080 char *end;
21081 bool res = true;
103f02d3 21082
779fe533 21083 if (length <= 0)
015dc7e1 21084 return false;
103f02d3 21085
1449284b
NC
21086 if (section)
21087 {
dda8d76d 21088 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
1449284b 21089 if (pnotes)
32ec8896 21090 {
dda8d76d 21091 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
f761cb13
AM
21092 {
21093 free (pnotes);
015dc7e1 21094 return false;
f761cb13 21095 }
32ec8896 21096 }
1449284b
NC
21097 }
21098 else
82ed9683 21099 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
1449284b 21100 _("notes"));
4dff97b2 21101
dd24e3da 21102 if (pnotes == NULL)
015dc7e1 21103 return false;
779fe533 21104
103f02d3 21105 external = pnotes;
103f02d3 21106
ca0e11aa
NC
21107 if (filedata->is_separate)
21108 printf (_("In linked file '%s': "), filedata->file_name);
21109 else
21110 printf ("\n");
1449284b 21111 if (section)
ca0e11aa 21112 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
1449284b 21113 else
ca0e11aa 21114 printf (_("Displaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
1449284b
NC
21115 (unsigned long) offset, (unsigned long) length);
21116
82ed9683
L
21117 /* NB: Some note sections may have alignment value of 0 or 1. gABI
21118 specifies that notes should be aligned to 4 bytes in 32-bit
21119 objects and to 8 bytes in 64-bit objects. As a Linux extension,
21120 we also support 4 byte alignment in 64-bit objects. If section
21121 alignment is less than 4, we treate alignment as 4 bytes. */
21122 if (align < 4)
21123 align = 4;
21124 else if (align != 4 && align != 8)
21125 {
21126 warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
21127 (long) align);
a788aedd 21128 free (pnotes);
015dc7e1 21129 return false;
82ed9683
L
21130 }
21131
dbe15e4e 21132 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
103f02d3 21133
c8071705
NC
21134 end = (char *) pnotes + length;
21135 while ((char *) external < end)
779fe533 21136 {
b34976b6 21137 Elf_Internal_Note inote;
15b42fb0 21138 size_t min_notesz;
4dff97b2 21139 char * next;
2cf0635d 21140 char * temp = NULL;
c8071705 21141 size_t data_remaining = end - (char *) external;
6d118b09 21142
dda8d76d 21143 if (!is_ia64_vms (filedata))
15b42fb0 21144 {
9dd3a467
NC
21145 /* PR binutils/15191
21146 Make sure that there is enough data to read. */
15b42fb0
AM
21147 min_notesz = offsetof (Elf_External_Note, name);
21148 if (data_remaining < min_notesz)
9dd3a467 21149 {
d3a49aa8
AM
21150 warn (ngettext ("Corrupt note: only %ld byte remains, "
21151 "not enough for a full note\n",
21152 "Corrupt note: only %ld bytes remain, "
21153 "not enough for a full note\n",
21154 data_remaining),
21155 (long) data_remaining);
9dd3a467
NC
21156 break;
21157 }
5396a86e
AM
21158 data_remaining -= min_notesz;
21159
15b42fb0
AM
21160 inote.type = BYTE_GET (external->type);
21161 inote.namesz = BYTE_GET (external->namesz);
21162 inote.namedata = external->name;
21163 inote.descsz = BYTE_GET (external->descsz);
276da9b3 21164 inote.descdata = ((char *) external
4dff97b2 21165 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
15b42fb0 21166 inote.descpos = offset + (inote.descdata - (char *) pnotes);
276da9b3 21167 next = ((char *) external
4dff97b2 21168 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
15b42fb0 21169 }
00e98fc7 21170 else
15b42fb0
AM
21171 {
21172 Elf64_External_VMS_Note *vms_external;
00e98fc7 21173
9dd3a467
NC
21174 /* PR binutils/15191
21175 Make sure that there is enough data to read. */
15b42fb0
AM
21176 min_notesz = offsetof (Elf64_External_VMS_Note, name);
21177 if (data_remaining < min_notesz)
9dd3a467 21178 {
d3a49aa8
AM
21179 warn (ngettext ("Corrupt note: only %ld byte remains, "
21180 "not enough for a full note\n",
21181 "Corrupt note: only %ld bytes remain, "
21182 "not enough for a full note\n",
21183 data_remaining),
21184 (long) data_remaining);
9dd3a467
NC
21185 break;
21186 }
5396a86e 21187 data_remaining -= min_notesz;
3e55a963 21188
15b42fb0
AM
21189 vms_external = (Elf64_External_VMS_Note *) external;
21190 inote.type = BYTE_GET (vms_external->type);
21191 inote.namesz = BYTE_GET (vms_external->namesz);
21192 inote.namedata = vms_external->name;
21193 inote.descsz = BYTE_GET (vms_external->descsz);
21194 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
21195 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21196 next = inote.descdata + align_power (inote.descsz, 3);
21197 }
21198
5396a86e
AM
21199 /* PR 17531: file: 3443835e. */
21200 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
21201 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
21202 || (size_t) (inote.descdata - inote.namedata) > data_remaining
21203 || (size_t) (next - inote.descdata) < inote.descsz
21204 || ((size_t) (next - inote.descdata)
21205 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
3e55a963 21206 {
15b42fb0 21207 warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
0af1713e 21208 (unsigned long) ((char *) external - (char *) pnotes));
4dff97b2
NC
21209 warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
21210 inote.type, inote.namesz, inote.descsz, (int) align);
3e55a963
NC
21211 break;
21212 }
21213
15b42fb0 21214 external = (Elf_External_Note *) next;
dd24e3da 21215
6d118b09
NC
21216 /* Verify that name is null terminated. It appears that at least
21217 one version of Linux (RedHat 6.0) generates corefiles that don't
21218 comply with the ELF spec by failing to include the null byte in
21219 namesz. */
18344509 21220 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
6d118b09 21221 {
5396a86e 21222 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
6d118b09 21223 {
5396a86e
AM
21224 temp = (char *) malloc (inote.namesz + 1);
21225 if (temp == NULL)
21226 {
21227 error (_("Out of memory allocating space for inote name\n"));
015dc7e1 21228 res = false;
5396a86e
AM
21229 break;
21230 }
76da6bbe 21231
5396a86e
AM
21232 memcpy (temp, inote.namedata, inote.namesz);
21233 inote.namedata = temp;
21234 }
21235 inote.namedata[inote.namesz] = 0;
6d118b09
NC
21236 }
21237
dda8d76d 21238 if (! process_note (& inote, filedata))
015dc7e1 21239 res = false;
103f02d3 21240
9db70fc3
AM
21241 free (temp);
21242 temp = NULL;
779fe533
NC
21243 }
21244
21245 free (pnotes);
103f02d3 21246
779fe533
NC
21247 return res;
21248}
21249
015dc7e1 21250static bool
dda8d76d 21251process_corefile_note_segments (Filedata * filedata)
779fe533 21252{
015dc7e1 21253 Elf_Internal_Phdr *segment;
b34976b6 21254 unsigned int i;
015dc7e1 21255 bool res = true;
103f02d3 21256
dda8d76d 21257 if (! get_program_headers (filedata))
015dc7e1 21258 return true;
103f02d3 21259
dda8d76d
NC
21260 for (i = 0, segment = filedata->program_headers;
21261 i < filedata->file_header.e_phnum;
b34976b6 21262 i++, segment++)
779fe533
NC
21263 {
21264 if (segment->p_type == PT_NOTE)
dda8d76d 21265 if (! process_notes_at (filedata, NULL,
32ec8896 21266 (bfd_vma) segment->p_offset,
82ed9683
L
21267 (bfd_vma) segment->p_filesz,
21268 (bfd_vma) segment->p_align))
015dc7e1 21269 res = false;
779fe533 21270 }
103f02d3 21271
779fe533
NC
21272 return res;
21273}
21274
015dc7e1 21275static bool
dda8d76d 21276process_v850_notes (Filedata * filedata, bfd_vma offset, bfd_vma length)
685080f2
NC
21277{
21278 Elf_External_Note * pnotes;
21279 Elf_External_Note * external;
c8071705 21280 char * end;
015dc7e1 21281 bool res = true;
685080f2
NC
21282
21283 if (length <= 0)
015dc7e1 21284 return false;
685080f2 21285
dda8d76d 21286 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
685080f2
NC
21287 _("v850 notes"));
21288 if (pnotes == NULL)
015dc7e1 21289 return false;
685080f2
NC
21290
21291 external = pnotes;
c8071705 21292 end = (char*) pnotes + length;
685080f2
NC
21293
21294 printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
21295 (unsigned long) offset, (unsigned long) length);
21296
c8071705 21297 while ((char *) external + sizeof (Elf_External_Note) < end)
685080f2
NC
21298 {
21299 Elf_External_Note * next;
21300 Elf_Internal_Note inote;
21301
21302 inote.type = BYTE_GET (external->type);
21303 inote.namesz = BYTE_GET (external->namesz);
21304 inote.namedata = external->name;
21305 inote.descsz = BYTE_GET (external->descsz);
21306 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
21307 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21308
c8071705
NC
21309 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
21310 {
21311 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
21312 inote.descdata = inote.namedata;
21313 inote.namesz = 0;
21314 }
21315
685080f2
NC
21316 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
21317
c8071705 21318 if ( ((char *) next > end)
685080f2
NC
21319 || ((char *) next < (char *) pnotes))
21320 {
21321 warn (_("corrupt descsz found in note at offset 0x%lx\n"),
21322 (unsigned long) ((char *) external - (char *) pnotes));
21323 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21324 inote.type, inote.namesz, inote.descsz);
21325 break;
21326 }
21327
21328 external = next;
21329
21330 /* Prevent out-of-bounds indexing. */
c8071705 21331 if ( inote.namedata + inote.namesz > end
685080f2
NC
21332 || inote.namedata + inote.namesz < inote.namedata)
21333 {
21334 warn (_("corrupt namesz found in note at offset 0x%lx\n"),
21335 (unsigned long) ((char *) external - (char *) pnotes));
21336 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21337 inote.type, inote.namesz, inote.descsz);
21338 break;
21339 }
21340
21341 printf (" %s: ", get_v850_elf_note_type (inote.type));
21342
21343 if (! print_v850_note (& inote))
21344 {
015dc7e1 21345 res = false;
685080f2
NC
21346 printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
21347 inote.namesz, inote.descsz);
21348 }
21349 }
21350
21351 free (pnotes);
21352
21353 return res;
21354}
21355
015dc7e1 21356static bool
dda8d76d 21357process_note_sections (Filedata * filedata)
1ec5cd37 21358{
015dc7e1 21359 Elf_Internal_Shdr *section;
1ec5cd37 21360 unsigned long i;
32ec8896 21361 unsigned int n = 0;
015dc7e1 21362 bool res = true;
1ec5cd37 21363
dda8d76d
NC
21364 for (i = 0, section = filedata->section_headers;
21365 i < filedata->file_header.e_shnum && section != NULL;
1ec5cd37 21366 i++, section++)
685080f2
NC
21367 {
21368 if (section->sh_type == SHT_NOTE)
21369 {
dda8d76d 21370 if (! process_notes_at (filedata, section,
32ec8896 21371 (bfd_vma) section->sh_offset,
82ed9683
L
21372 (bfd_vma) section->sh_size,
21373 (bfd_vma) section->sh_addralign))
015dc7e1 21374 res = false;
685080f2
NC
21375 n++;
21376 }
21377
dda8d76d
NC
21378 if (( filedata->file_header.e_machine == EM_V800
21379 || filedata->file_header.e_machine == EM_V850
21380 || filedata->file_header.e_machine == EM_CYGNUS_V850)
685080f2
NC
21381 && section->sh_type == SHT_RENESAS_INFO)
21382 {
dda8d76d 21383 if (! process_v850_notes (filedata,
32ec8896
NC
21384 (bfd_vma) section->sh_offset,
21385 (bfd_vma) section->sh_size))
015dc7e1 21386 res = false;
685080f2
NC
21387 n++;
21388 }
21389 }
df565f32
NC
21390
21391 if (n == 0)
21392 /* Try processing NOTE segments instead. */
dda8d76d 21393 return process_corefile_note_segments (filedata);
1ec5cd37
NC
21394
21395 return res;
21396}
21397
015dc7e1 21398static bool
dda8d76d 21399process_notes (Filedata * filedata)
779fe533
NC
21400{
21401 /* If we have not been asked to display the notes then do nothing. */
21402 if (! do_notes)
015dc7e1 21403 return true;
103f02d3 21404
dda8d76d
NC
21405 if (filedata->file_header.e_type != ET_CORE)
21406 return process_note_sections (filedata);
103f02d3 21407
779fe533 21408 /* No program headers means no NOTE segment. */
dda8d76d
NC
21409 if (filedata->file_header.e_phnum > 0)
21410 return process_corefile_note_segments (filedata);
779fe533 21411
ca0e11aa
NC
21412 if (filedata->is_separate)
21413 printf (_("No notes found in linked file '%s'.\n"),
21414 filedata->file_name);
21415 else
21416 printf (_("No notes found file.\n"));
21417
015dc7e1 21418 return true;
779fe533
NC
21419}
21420
60abdbed
NC
21421static unsigned char *
21422display_public_gnu_attributes (unsigned char * start,
21423 const unsigned char * const end)
21424{
21425 printf (_(" Unknown GNU attribute: %s\n"), start);
21426
21427 start += strnlen ((char *) start, end - start);
21428 display_raw_attribute (start, end);
21429
21430 return (unsigned char *) end;
21431}
21432
21433static unsigned char *
21434display_generic_attribute (unsigned char * start,
21435 unsigned int tag,
21436 const unsigned char * const end)
21437{
21438 if (tag == 0)
21439 return (unsigned char *) end;
21440
21441 return display_tag_value (tag, start, end);
21442}
21443
015dc7e1 21444static bool
dda8d76d 21445process_arch_specific (Filedata * filedata)
252b5132 21446{
a952a375 21447 if (! do_arch)
015dc7e1 21448 return true;
a952a375 21449
dda8d76d 21450 switch (filedata->file_header.e_machine)
252b5132 21451 {
53a346d8
CZ
21452 case EM_ARC:
21453 case EM_ARC_COMPACT:
21454 case EM_ARC_COMPACT2:
dda8d76d 21455 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
53a346d8
CZ
21456 display_arc_attribute,
21457 display_generic_attribute);
11c1ff18 21458 case EM_ARM:
dda8d76d 21459 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
60abdbed
NC
21460 display_arm_attribute,
21461 display_generic_attribute);
21462
252b5132 21463 case EM_MIPS:
4fe85591 21464 case EM_MIPS_RS3_LE:
dda8d76d 21465 return process_mips_specific (filedata);
60abdbed
NC
21466
21467 case EM_MSP430:
dda8d76d 21468 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
b0191216 21469 display_msp430_attribute,
c0ea7c52 21470 display_msp430_gnu_attribute);
60abdbed 21471
2dc8dd17
JW
21472 case EM_RISCV:
21473 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
21474 display_riscv_attribute,
21475 display_generic_attribute);
21476
35c08157 21477 case EM_NDS32:
dda8d76d 21478 return process_nds32_specific (filedata);
60abdbed 21479
85f7484a
PB
21480 case EM_68K:
21481 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21482 display_m68k_gnu_attribute);
21483
34c8bcba 21484 case EM_PPC:
b82317dd 21485 case EM_PPC64:
dda8d76d 21486 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
60abdbed
NC
21487 display_power_gnu_attribute);
21488
643f7afb
AK
21489 case EM_S390:
21490 case EM_S390_OLD:
dda8d76d 21491 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
60abdbed
NC
21492 display_s390_gnu_attribute);
21493
9e8c70f9
DM
21494 case EM_SPARC:
21495 case EM_SPARC32PLUS:
21496 case EM_SPARCV9:
dda8d76d 21497 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
60abdbed
NC
21498 display_sparc_gnu_attribute);
21499
59e6276b 21500 case EM_TI_C6000:
dda8d76d 21501 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
60abdbed
NC
21502 display_tic6x_attribute,
21503 display_generic_attribute);
21504
0861f561
CQ
21505 case EM_CSKY:
21506 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
21507 display_csky_attribute, NULL);
21508
252b5132 21509 default:
dda8d76d 21510 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
60abdbed
NC
21511 display_public_gnu_attributes,
21512 display_generic_attribute);
252b5132 21513 }
252b5132
RH
21514}
21515
015dc7e1 21516static bool
dda8d76d 21517get_file_header (Filedata * filedata)
252b5132 21518{
9ea033b2 21519 /* Read in the identity array. */
dda8d76d 21520 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
015dc7e1 21521 return false;
252b5132 21522
9ea033b2 21523 /* Determine how to read the rest of the header. */
dda8d76d 21524 switch (filedata->file_header.e_ident[EI_DATA])
9ea033b2 21525 {
1a0670f3
AM
21526 default:
21527 case ELFDATANONE:
adab8cdc
AO
21528 case ELFDATA2LSB:
21529 byte_get = byte_get_little_endian;
21530 byte_put = byte_put_little_endian;
21531 break;
21532 case ELFDATA2MSB:
21533 byte_get = byte_get_big_endian;
21534 byte_put = byte_put_big_endian;
21535 break;
9ea033b2
NC
21536 }
21537
21538 /* For now we only support 32 bit and 64 bit ELF files. */
dda8d76d 21539 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
9ea033b2
NC
21540
21541 /* Read in the rest of the header. */
21542 if (is_32bit_elf)
21543 {
21544 Elf32_External_Ehdr ehdr32;
252b5132 21545
dda8d76d 21546 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
015dc7e1 21547 return false;
103f02d3 21548
dda8d76d
NC
21549 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
21550 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
21551 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
21552 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
21553 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
21554 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
21555 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
21556 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
21557 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
21558 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
21559 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
21560 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
21561 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
9ea033b2 21562 }
252b5132 21563 else
9ea033b2
NC
21564 {
21565 Elf64_External_Ehdr ehdr64;
a952a375
NC
21566
21567 /* If we have been compiled with sizeof (bfd_vma) == 4, then
21568 we will not be able to cope with the 64bit data found in
21569 64 ELF files. Detect this now and abort before we start
50c2245b 21570 overwriting things. */
a952a375
NC
21571 if (sizeof (bfd_vma) < 8)
21572 {
e3c8793a
NC
21573 error (_("This instance of readelf has been built without support for a\n\
2157464 bit data type and so it cannot read 64 bit ELF files.\n"));
015dc7e1 21575 return false;
a952a375 21576 }
103f02d3 21577
dda8d76d 21578 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
015dc7e1 21579 return false;
103f02d3 21580
dda8d76d
NC
21581 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
21582 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
21583 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
21584 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
21585 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
21586 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
21587 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
21588 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
21589 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
21590 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
21591 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
21592 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
21593 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
9ea033b2 21594 }
252b5132 21595
015dc7e1 21596 return true;
252b5132
RH
21597}
21598
13acb58d
AM
21599static void
21600free_filedata (Filedata *filedata)
21601{
21602 free (filedata->program_interpreter);
13acb58d 21603 free (filedata->program_headers);
13acb58d 21604 free (filedata->section_headers);
13acb58d 21605 free (filedata->string_table);
13acb58d 21606 free (filedata->dump.dump_sects);
13acb58d 21607 free (filedata->dynamic_strings);
13acb58d 21608 free (filedata->dynamic_symbols);
13acb58d 21609 free (filedata->dynamic_syminfo);
13acb58d 21610 free (filedata->dynamic_section);
13acb58d
AM
21611
21612 while (filedata->symtab_shndx_list != NULL)
21613 {
21614 elf_section_list *next = filedata->symtab_shndx_list->next;
21615 free (filedata->symtab_shndx_list);
21616 filedata->symtab_shndx_list = next;
21617 }
21618
21619 free (filedata->section_headers_groups);
13acb58d
AM
21620
21621 if (filedata->section_groups)
21622 {
21623 size_t i;
21624 struct group_list * g;
21625 struct group_list * next;
21626
21627 for (i = 0; i < filedata->group_count; i++)
21628 {
21629 for (g = filedata->section_groups [i].root; g != NULL; g = next)
21630 {
21631 next = g->next;
21632 free (g);
21633 }
21634 }
21635
21636 free (filedata->section_groups);
13acb58d 21637 }
066f8fbe
AM
21638 memset (&filedata->section_headers, 0,
21639 sizeof (Filedata) - offsetof (Filedata, section_headers));
13acb58d
AM
21640}
21641
dda8d76d
NC
21642static void
21643close_file (Filedata * filedata)
21644{
21645 if (filedata)
21646 {
21647 if (filedata->handle)
21648 fclose (filedata->handle);
21649 free (filedata);
21650 }
21651}
21652
21653void
21654close_debug_file (void * data)
21655{
13acb58d 21656 free_filedata ((Filedata *) data);
dda8d76d
NC
21657 close_file ((Filedata *) data);
21658}
21659
21660static Filedata *
015dc7e1 21661open_file (const char * pathname, bool is_separate)
dda8d76d
NC
21662{
21663 struct stat statbuf;
21664 Filedata * filedata = NULL;
21665
21666 if (stat (pathname, & statbuf) < 0
21667 || ! S_ISREG (statbuf.st_mode))
21668 goto fail;
21669
21670 filedata = calloc (1, sizeof * filedata);
21671 if (filedata == NULL)
21672 goto fail;
21673
21674 filedata->handle = fopen (pathname, "rb");
21675 if (filedata->handle == NULL)
21676 goto fail;
21677
21678 filedata->file_size = (bfd_size_type) statbuf.st_size;
21679 filedata->file_name = pathname;
ca0e11aa 21680 filedata->is_separate = is_separate;
dda8d76d
NC
21681
21682 if (! get_file_header (filedata))
21683 goto fail;
21684
4de91c10
AM
21685 if (!get_section_headers (filedata, false))
21686 goto fail;
dda8d76d
NC
21687
21688 return filedata;
21689
21690 fail:
21691 if (filedata)
21692 {
21693 if (filedata->handle)
21694 fclose (filedata->handle);
21695 free (filedata);
21696 }
21697 return NULL;
21698}
21699
21700void *
21701open_debug_file (const char * pathname)
21702{
015dc7e1 21703 return open_file (pathname, true);
dda8d76d
NC
21704}
21705
835f2fae
NC
21706static void
21707initialise_dump_sects (Filedata * filedata)
21708{
21709 /* Initialise the dump_sects array from the cmdline_dump_sects array.
21710 Note we do this even if cmdline_dump_sects is empty because we
21711 must make sure that the dump_sets array is zeroed out before each
21712 object file is processed. */
21713 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
21714 memset (filedata->dump.dump_sects, 0,
21715 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21716
21717 if (cmdline.num_dump_sects > 0)
21718 {
21719 if (filedata->dump.num_dump_sects == 0)
21720 /* A sneaky way of allocating the dump_sects array. */
21721 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
21722
21723 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
21724 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
21725 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21726 }
21727}
21728
fb52b2f4
NC
21729/* Process one ELF object file according to the command line options.
21730 This file may actually be stored in an archive. The file is
32ec8896
NC
21731 positioned at the start of the ELF object. Returns TRUE if no
21732 problems were encountered, FALSE otherwise. */
fb52b2f4 21733
015dc7e1 21734static bool
dda8d76d 21735process_object (Filedata * filedata)
252b5132 21736{
015dc7e1 21737 bool have_separate_files;
252b5132 21738 unsigned int i;
015dc7e1 21739 bool res;
252b5132 21740
dda8d76d 21741 if (! get_file_header (filedata))
252b5132 21742 {
dda8d76d 21743 error (_("%s: Failed to read file header\n"), filedata->file_name);
015dc7e1 21744 return false;
252b5132
RH
21745 }
21746
21747 /* Initialise per file variables. */
978c4450
AM
21748 for (i = ARRAY_SIZE (filedata->version_info); i--;)
21749 filedata->version_info[i] = 0;
252b5132 21750
978c4450
AM
21751 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
21752 filedata->dynamic_info[i] = 0;
21753 filedata->dynamic_info_DT_GNU_HASH = 0;
21754 filedata->dynamic_info_DT_MIPS_XHASH = 0;
252b5132
RH
21755
21756 /* Process the file. */
21757 if (show_name)
dda8d76d 21758 printf (_("\nFile: %s\n"), filedata->file_name);
252b5132 21759
835f2fae 21760 initialise_dump_sects (filedata);
d70c5fc7 21761
4de91c10
AM
21762 /* There may be some extensions in the first section header. Don't
21763 bomb if we can't read it. */
21764 get_section_headers (filedata, true);
21765
dda8d76d 21766 if (! process_file_header (filedata))
4de91c10
AM
21767 {
21768 res = false;
21769 goto out;
21770 }
252b5132 21771
e331b18d
AM
21772 /* Throw away the single section header read above, so that we
21773 re-read the entire set. */
21774 free (filedata->section_headers);
21775 filedata->section_headers = NULL;
21776
dda8d76d 21777 if (! process_section_headers (filedata))
2f62977e 21778 {
32ec8896 21779 /* Without loaded section headers we cannot process lots of things. */
015dc7e1 21780 do_unwind = do_version = do_dump = do_arch = false;
252b5132 21781
2f62977e 21782 if (! do_using_dynamic)
015dc7e1 21783 do_syms = do_dyn_syms = do_reloc = false;
2f62977e 21784 }
252b5132 21785
dda8d76d 21786 if (! process_section_groups (filedata))
32ec8896 21787 /* Without loaded section groups we cannot process unwind. */
015dc7e1 21788 do_unwind = false;
d1f5c6e3 21789
93df3340
AM
21790 process_program_headers (filedata);
21791
21792 res = process_dynamic_section (filedata);
252b5132 21793
dda8d76d 21794 if (! process_relocs (filedata))
015dc7e1 21795 res = false;
252b5132 21796
dda8d76d 21797 if (! process_unwind (filedata))
015dc7e1 21798 res = false;
4d6ed7c8 21799
dda8d76d 21800 if (! process_symbol_table (filedata))
015dc7e1 21801 res = false;
252b5132 21802
0f03783c 21803 if (! process_lto_symbol_tables (filedata))
015dc7e1 21804 res = false;
b9e920ec 21805
dda8d76d 21806 if (! process_syminfo (filedata))
015dc7e1 21807 res = false;
252b5132 21808
dda8d76d 21809 if (! process_version_sections (filedata))
015dc7e1 21810 res = false;
252b5132 21811
82ed9683 21812 if (filedata->file_header.e_shstrndx != SHN_UNDEF)
24841daa 21813 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
82ed9683 21814 else
015dc7e1 21815 have_separate_files = false;
dda8d76d
NC
21816
21817 if (! process_section_contents (filedata))
015dc7e1 21818 res = false;
f5842774 21819
24841daa 21820 if (have_separate_files)
dda8d76d 21821 {
24841daa
NC
21822 separate_info * d;
21823
21824 for (d = first_separate_info; d != NULL; d = d->next)
21825 {
835f2fae
NC
21826 initialise_dump_sects (d->handle);
21827
ca0e11aa 21828 if (process_links && ! process_file_header (d->handle))
015dc7e1 21829 res = false;
ca0e11aa 21830 else if (! process_section_headers (d->handle))
015dc7e1 21831 res = false;
d6bfbc39 21832 else if (! process_section_contents (d->handle))
015dc7e1 21833 res = false;
ca0e11aa
NC
21834 else if (process_links)
21835 {
ca0e11aa 21836 if (! process_section_groups (d->handle))
015dc7e1 21837 res = false;
93df3340 21838 process_program_headers (d->handle);
ca0e11aa 21839 if (! process_dynamic_section (d->handle))
015dc7e1 21840 res = false;
ca0e11aa 21841 if (! process_relocs (d->handle))
015dc7e1 21842 res = false;
ca0e11aa 21843 if (! process_unwind (d->handle))
015dc7e1 21844 res = false;
ca0e11aa 21845 if (! process_symbol_table (d->handle))
015dc7e1 21846 res = false;
ca0e11aa 21847 if (! process_lto_symbol_tables (d->handle))
015dc7e1 21848 res = false;
ca0e11aa 21849 if (! process_syminfo (d->handle))
015dc7e1 21850 res = false;
ca0e11aa 21851 if (! process_version_sections (d->handle))
015dc7e1 21852 res = false;
ca0e11aa 21853 if (! process_notes (d->handle))
015dc7e1 21854 res = false;
ca0e11aa 21855 }
24841daa
NC
21856 }
21857
21858 /* The file handles are closed by the call to free_debug_memory() below. */
dda8d76d
NC
21859 }
21860
21861 if (! process_notes (filedata))
015dc7e1 21862 res = false;
103f02d3 21863
dda8d76d 21864 if (! process_gnu_liblist (filedata))
015dc7e1 21865 res = false;
047b2264 21866
dda8d76d 21867 if (! process_arch_specific (filedata))
015dc7e1 21868 res = false;
252b5132 21869
4de91c10 21870 out:
13acb58d 21871 free_filedata (filedata);
e4b17d5c 21872
19e6b90e 21873 free_debug_memory ();
18bd398b 21874
32ec8896 21875 return res;
252b5132
RH
21876}
21877
2cf0635d 21878/* Process an ELF archive.
32ec8896
NC
21879 On entry the file is positioned just after the ARMAG string.
21880 Returns TRUE upon success, FALSE otherwise. */
2cf0635d 21881
015dc7e1
AM
21882static bool
21883process_archive (Filedata * filedata, bool is_thin_archive)
2cf0635d
NC
21884{
21885 struct archive_info arch;
21886 struct archive_info nested_arch;
21887 size_t got;
015dc7e1 21888 bool ret = true;
2cf0635d 21889
015dc7e1 21890 show_name = true;
2cf0635d
NC
21891
21892 /* The ARCH structure is used to hold information about this archive. */
21893 arch.file_name = NULL;
21894 arch.file = NULL;
21895 arch.index_array = NULL;
21896 arch.sym_table = NULL;
21897 arch.longnames = NULL;
21898
21899 /* The NESTED_ARCH structure is used as a single-item cache of information
21900 about a nested archive (when members of a thin archive reside within
21901 another regular archive file). */
21902 nested_arch.file_name = NULL;
21903 nested_arch.file = NULL;
21904 nested_arch.index_array = NULL;
21905 nested_arch.sym_table = NULL;
21906 nested_arch.longnames = NULL;
21907
dda8d76d 21908 if (setup_archive (&arch, filedata->file_name, filedata->handle,
780f96ae
AM
21909 filedata->file_size, is_thin_archive,
21910 do_archive_index) != 0)
2cf0635d 21911 {
015dc7e1 21912 ret = false;
2cf0635d 21913 goto out;
4145f1d5 21914 }
fb52b2f4 21915
4145f1d5
NC
21916 if (do_archive_index)
21917 {
2cf0635d 21918 if (arch.sym_table == NULL)
1cb7d8b1
AM
21919 error (_("%s: unable to dump the index as none was found\n"),
21920 filedata->file_name);
4145f1d5
NC
21921 else
21922 {
591f7597 21923 unsigned long i, l;
4145f1d5
NC
21924 unsigned long current_pos;
21925
1cb7d8b1
AM
21926 printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
21927 "in the symbol table)\n"),
21928 filedata->file_name, (unsigned long) arch.index_num,
21929 arch.sym_size);
dda8d76d
NC
21930
21931 current_pos = ftell (filedata->handle);
4145f1d5 21932
2cf0635d 21933 for (i = l = 0; i < arch.index_num; i++)
4145f1d5 21934 {
1cb7d8b1
AM
21935 if (i == 0
21936 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
21937 {
21938 char * member_name
21939 = get_archive_member_name_at (&arch, arch.index_array[i],
21940 &nested_arch);
2cf0635d 21941
1cb7d8b1
AM
21942 if (member_name != NULL)
21943 {
21944 char * qualified_name
21945 = make_qualified_name (&arch, &nested_arch,
21946 member_name);
2cf0635d 21947
1cb7d8b1
AM
21948 if (qualified_name != NULL)
21949 {
21950 printf (_("Contents of binary %s at offset "),
21951 qualified_name);
c2a7d3f5
NC
21952 (void) print_vma (arch.index_array[i], PREFIX_HEX);
21953 putchar ('\n');
1cb7d8b1
AM
21954 free (qualified_name);
21955 }
fd486f32 21956 free (member_name);
4145f1d5
NC
21957 }
21958 }
2cf0635d
NC
21959
21960 if (l >= arch.sym_size)
4145f1d5 21961 {
1cb7d8b1
AM
21962 error (_("%s: end of the symbol table reached "
21963 "before the end of the index\n"),
dda8d76d 21964 filedata->file_name);
015dc7e1 21965 ret = false;
cb8f3167 21966 break;
4145f1d5 21967 }
591f7597 21968 /* PR 17531: file: 0b6630b2. */
1cb7d8b1
AM
21969 printf ("\t%.*s\n",
21970 (int) (arch.sym_size - l), arch.sym_table + l);
591f7597 21971 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
4145f1d5
NC
21972 }
21973
67ce483b 21974 if (arch.uses_64bit_indices)
c2a7d3f5
NC
21975 l = (l + 7) & ~ 7;
21976 else
21977 l += l & 1;
21978
2cf0635d 21979 if (l < arch.sym_size)
32ec8896 21980 {
d3a49aa8
AM
21981 error (ngettext ("%s: %ld byte remains in the symbol table, "
21982 "but without corresponding entries in "
21983 "the index table\n",
21984 "%s: %ld bytes remain in the symbol table, "
21985 "but without corresponding entries in "
21986 "the index table\n",
21987 arch.sym_size - l),
dda8d76d 21988 filedata->file_name, arch.sym_size - l);
015dc7e1 21989 ret = false;
32ec8896 21990 }
4145f1d5 21991
dda8d76d 21992 if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
4145f1d5 21993 {
1cb7d8b1
AM
21994 error (_("%s: failed to seek back to start of object files "
21995 "in the archive\n"),
dda8d76d 21996 filedata->file_name);
015dc7e1 21997 ret = false;
2cf0635d 21998 goto out;
4145f1d5 21999 }
fb52b2f4 22000 }
4145f1d5
NC
22001
22002 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
22003 && !do_segments && !do_header && !do_dump && !do_version
22004 && !do_histogram && !do_debugging && !do_arch && !do_notes
2c610e4b 22005 && !do_section_groups && !do_dyn_syms)
2cf0635d 22006 {
015dc7e1 22007 ret = true; /* Archive index only. */
2cf0635d
NC
22008 goto out;
22009 }
fb52b2f4
NC
22010 }
22011
fb52b2f4
NC
22012 while (1)
22013 {
2cf0635d
NC
22014 char * name;
22015 size_t namelen;
22016 char * qualified_name;
22017
22018 /* Read the next archive header. */
dda8d76d 22019 if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
1cb7d8b1
AM
22020 {
22021 error (_("%s: failed to seek to next archive header\n"),
22022 arch.file_name);
015dc7e1 22023 ret = false;
1cb7d8b1
AM
22024 break;
22025 }
dda8d76d 22026 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
2cf0635d 22027 if (got != sizeof arch.arhdr)
1cb7d8b1
AM
22028 {
22029 if (got == 0)
2cf0635d 22030 break;
28e817cc
NC
22031 /* PR 24049 - we cannot use filedata->file_name as this will
22032 have already been freed. */
22033 error (_("%s: failed to read archive header\n"), arch.file_name);
9abca702 22034
015dc7e1 22035 ret = false;
1cb7d8b1
AM
22036 break;
22037 }
2cf0635d 22038 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
1cb7d8b1
AM
22039 {
22040 error (_("%s: did not find a valid archive header\n"),
22041 arch.file_name);
015dc7e1 22042 ret = false;
1cb7d8b1
AM
22043 break;
22044 }
2cf0635d
NC
22045
22046 arch.next_arhdr_offset += sizeof arch.arhdr;
22047
978c4450 22048 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
2cf0635d
NC
22049
22050 name = get_archive_member_name (&arch, &nested_arch);
22051 if (name == NULL)
fb52b2f4 22052 {
28e817cc 22053 error (_("%s: bad archive file name\n"), arch.file_name);
015dc7e1 22054 ret = false;
d989285c 22055 break;
fb52b2f4 22056 }
2cf0635d 22057 namelen = strlen (name);
fb52b2f4 22058
2cf0635d
NC
22059 qualified_name = make_qualified_name (&arch, &nested_arch, name);
22060 if (qualified_name == NULL)
fb52b2f4 22061 {
28e817cc 22062 error (_("%s: bad archive file name\n"), arch.file_name);
fd486f32 22063 free (name);
015dc7e1 22064 ret = false;
d989285c 22065 break;
fb52b2f4
NC
22066 }
22067
2cf0635d 22068 if (is_thin_archive && arch.nested_member_origin == 0)
1cb7d8b1
AM
22069 {
22070 /* This is a proxy for an external member of a thin archive. */
22071 Filedata * member_filedata;
22072 char * member_file_name = adjust_relative_path
dda8d76d 22073 (filedata->file_name, name, namelen);
32ec8896 22074
fd486f32 22075 free (name);
1cb7d8b1
AM
22076 if (member_file_name == NULL)
22077 {
fd486f32 22078 free (qualified_name);
015dc7e1 22079 ret = false;
1cb7d8b1
AM
22080 break;
22081 }
2cf0635d 22082
015dc7e1 22083 member_filedata = open_file (member_file_name, false);
1cb7d8b1
AM
22084 if (member_filedata == NULL)
22085 {
22086 error (_("Input file '%s' is not readable.\n"), member_file_name);
22087 free (member_file_name);
fd486f32 22088 free (qualified_name);
015dc7e1 22089 ret = false;
1cb7d8b1
AM
22090 break;
22091 }
2cf0635d 22092
978c4450 22093 filedata->archive_file_offset = arch.nested_member_origin;
dda8d76d 22094 member_filedata->file_name = qualified_name;
2cf0635d 22095
75a2da57
AH
22096 /* The call to process_object() expects the file to be at the beginning. */
22097 rewind (member_filedata->handle);
22098
1cb7d8b1 22099 if (! process_object (member_filedata))
015dc7e1 22100 ret = false;
2cf0635d 22101
1cb7d8b1
AM
22102 close_file (member_filedata);
22103 free (member_file_name);
1cb7d8b1 22104 }
2cf0635d 22105 else if (is_thin_archive)
1cb7d8b1
AM
22106 {
22107 Filedata thin_filedata;
eb02c04d 22108
1cb7d8b1 22109 memset (&thin_filedata, 0, sizeof (thin_filedata));
dda8d76d 22110
a043396b
NC
22111 /* PR 15140: Allow for corrupt thin archives. */
22112 if (nested_arch.file == NULL)
22113 {
22114 error (_("%s: contains corrupt thin archive: %s\n"),
28e817cc 22115 qualified_name, name);
fd486f32
AM
22116 free (qualified_name);
22117 free (name);
015dc7e1 22118 ret = false;
a043396b
NC
22119 break;
22120 }
fd486f32 22121 free (name);
a043396b 22122
1cb7d8b1 22123 /* This is a proxy for a member of a nested archive. */
978c4450
AM
22124 filedata->archive_file_offset
22125 = arch.nested_member_origin + sizeof arch.arhdr;
2cf0635d 22126
1cb7d8b1
AM
22127 /* The nested archive file will have been opened and setup by
22128 get_archive_member_name. */
978c4450
AM
22129 if (fseek (nested_arch.file, filedata->archive_file_offset,
22130 SEEK_SET) != 0)
1cb7d8b1
AM
22131 {
22132 error (_("%s: failed to seek to archive member.\n"),
22133 nested_arch.file_name);
fd486f32 22134 free (qualified_name);
015dc7e1 22135 ret = false;
1cb7d8b1
AM
22136 break;
22137 }
2cf0635d 22138
dda8d76d
NC
22139 thin_filedata.handle = nested_arch.file;
22140 thin_filedata.file_name = qualified_name;
9abca702 22141
1cb7d8b1 22142 if (! process_object (& thin_filedata))
015dc7e1 22143 ret = false;
1cb7d8b1 22144 }
2cf0635d 22145 else
1cb7d8b1 22146 {
fd486f32 22147 free (name);
978c4450 22148 filedata->archive_file_offset = arch.next_arhdr_offset;
6a6196fc 22149 filedata->file_name = qualified_name;
1cb7d8b1 22150 if (! process_object (filedata))
015dc7e1 22151 ret = false;
237877b8 22152 arch.next_arhdr_offset += (filedata->archive_file_size + 1) & -2;
4c836627 22153 /* Stop looping with "negative" archive_file_size. */
978c4450 22154 if (arch.next_arhdr_offset < filedata->archive_file_size)
80e2a3b6 22155 arch.next_arhdr_offset = -1ul;
1cb7d8b1 22156 }
fb52b2f4 22157
2cf0635d 22158 free (qualified_name);
fb52b2f4
NC
22159 }
22160
4145f1d5 22161 out:
2cf0635d
NC
22162 if (nested_arch.file != NULL)
22163 fclose (nested_arch.file);
22164 release_archive (&nested_arch);
22165 release_archive (&arch);
fb52b2f4 22166
d989285c 22167 return ret;
fb52b2f4
NC
22168}
22169
015dc7e1 22170static bool
2cf0635d 22171process_file (char * file_name)
fb52b2f4 22172{
dda8d76d 22173 Filedata * filedata = NULL;
fb52b2f4
NC
22174 struct stat statbuf;
22175 char armag[SARMAG];
015dc7e1 22176 bool ret = true;
fb52b2f4
NC
22177
22178 if (stat (file_name, &statbuf) < 0)
22179 {
f24ddbdd
NC
22180 if (errno == ENOENT)
22181 error (_("'%s': No such file\n"), file_name);
22182 else
22183 error (_("Could not locate '%s'. System error message: %s\n"),
22184 file_name, strerror (errno));
015dc7e1 22185 return false;
f24ddbdd
NC
22186 }
22187
22188 if (! S_ISREG (statbuf.st_mode))
22189 {
22190 error (_("'%s' is not an ordinary file\n"), file_name);
015dc7e1 22191 return false;
fb52b2f4
NC
22192 }
22193
dda8d76d
NC
22194 filedata = calloc (1, sizeof * filedata);
22195 if (filedata == NULL)
22196 {
22197 error (_("Out of memory allocating file data structure\n"));
015dc7e1 22198 return false;
dda8d76d
NC
22199 }
22200
22201 filedata->file_name = file_name;
22202 filedata->handle = fopen (file_name, "rb");
22203 if (filedata->handle == NULL)
fb52b2f4 22204 {
f24ddbdd 22205 error (_("Input file '%s' is not readable.\n"), file_name);
dda8d76d 22206 free (filedata);
015dc7e1 22207 return false;
fb52b2f4
NC
22208 }
22209
dda8d76d 22210 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
fb52b2f4 22211 {
4145f1d5 22212 error (_("%s: Failed to read file's magic number\n"), file_name);
dda8d76d
NC
22213 fclose (filedata->handle);
22214 free (filedata);
015dc7e1 22215 return false;
fb52b2f4
NC
22216 }
22217
dda8d76d 22218 filedata->file_size = (bfd_size_type) statbuf.st_size;
015dc7e1 22219 filedata->is_separate = false;
f54498b4 22220
fb52b2f4 22221 if (memcmp (armag, ARMAG, SARMAG) == 0)
32ec8896 22222 {
015dc7e1
AM
22223 if (! process_archive (filedata, false))
22224 ret = false;
32ec8896 22225 }
2cf0635d 22226 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
32ec8896 22227 {
015dc7e1
AM
22228 if ( ! process_archive (filedata, true))
22229 ret = false;
32ec8896 22230 }
fb52b2f4
NC
22231 else
22232 {
1b513401 22233 if (do_archive_index && !check_all)
4145f1d5
NC
22234 error (_("File %s is not an archive so its index cannot be displayed.\n"),
22235 file_name);
22236
dda8d76d 22237 rewind (filedata->handle);
978c4450 22238 filedata->archive_file_size = filedata->archive_file_offset = 0;
32ec8896 22239
dda8d76d 22240 if (! process_object (filedata))
015dc7e1 22241 ret = false;
fb52b2f4
NC
22242 }
22243
dda8d76d 22244 fclose (filedata->handle);
8fb879cd
AM
22245 free (filedata->section_headers);
22246 free (filedata->program_headers);
22247 free (filedata->string_table);
6431e409 22248 free (filedata->dump.dump_sects);
dda8d76d 22249 free (filedata);
32ec8896 22250
fd486f32 22251 free (ba_cache.strtab);
1bd6175a 22252 ba_cache.strtab = NULL;
fd486f32 22253 free (ba_cache.symtab);
1bd6175a 22254 ba_cache.symtab = NULL;
fd486f32
AM
22255 ba_cache.filedata = NULL;
22256
fb52b2f4
NC
22257 return ret;
22258}
22259
252b5132
RH
22260#ifdef SUPPORT_DISASSEMBLY
22261/* Needed by the i386 disassembler. For extra credit, someone could
9ea033b2 22262 fix this so that we insert symbolic addresses here, esp for GOT/PLT
e3c8793a 22263 symbols. */
252b5132
RH
22264
22265void
2cf0635d 22266print_address (unsigned int addr, FILE * outfile)
252b5132
RH
22267{
22268 fprintf (outfile,"0x%8.8x", addr);
22269}
22270
e3c8793a 22271/* Needed by the i386 disassembler. */
dda8d76d 22272
252b5132
RH
22273void
22274db_task_printsym (unsigned int addr)
22275{
22276 print_address (addr, stderr);
22277}
22278#endif
22279
22280int
2cf0635d 22281main (int argc, char ** argv)
252b5132 22282{
ff78d6d6
L
22283 int err;
22284
87b9f255 22285#ifdef HAVE_LC_MESSAGES
252b5132 22286 setlocale (LC_MESSAGES, "");
3882b010 22287#endif
3882b010 22288 setlocale (LC_CTYPE, "");
252b5132
RH
22289 bindtextdomain (PACKAGE, LOCALEDIR);
22290 textdomain (PACKAGE);
22291
869b9d07
MM
22292 expandargv (&argc, &argv);
22293
dda8d76d 22294 parse_args (& cmdline, argc, argv);
59f14fc0 22295
18bd398b 22296 if (optind < (argc - 1))
1b513401
NC
22297 /* When displaying information for more than one file,
22298 prefix the information with the file name. */
015dc7e1 22299 show_name = true;
5656ba2c
L
22300 else if (optind >= argc)
22301 {
1b513401 22302 /* Ensure that the warning is always displayed. */
015dc7e1 22303 do_checks = true;
1b513401 22304
5656ba2c
L
22305 warn (_("Nothing to do.\n"));
22306 usage (stderr);
22307 }
18bd398b 22308
015dc7e1 22309 err = false;
252b5132 22310 while (optind < argc)
32ec8896 22311 if (! process_file (argv[optind++]))
015dc7e1 22312 err = true;
252b5132 22313
9db70fc3 22314 free (cmdline.dump_sects);
252b5132 22315
7d9813f1
NA
22316 free (dump_ctf_symtab_name);
22317 free (dump_ctf_strtab_name);
22318 free (dump_ctf_parent_name);
22319
32ec8896 22320 return err ? EXIT_FAILURE : EXIT_SUCCESS;
252b5132 22321}