]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - binutils/readelf.c
readelf: Add option to display the names of sections referenced by symbols.
[thirdparty/binutils-gdb.git] / binutils / readelf.c
CommitLineData
252b5132 1/* readelf.c -- display contents of an ELF format file
d87bef3a 2 Copyright (C) 1998-2023 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>
1f5a3546
FS
47#ifdef HAVE_ZSTD
48#include <zstd.h>
49#endif
7bfd842d 50#include <wchar.h>
252b5132 51
2952f10c
SM
52#if defined HAVE_MSGPACK
53#include <msgpack.h>
54#endif
55
19936277 56/* Define BFD64 here, even if our default architecture is 32 bit ELF
625d49fc 57 as this will allow us to read in and parse 64bit and 32bit ELF files. */
19936277 58#define BFD64
a952a375 59
3db64b00
AM
60#include "bfd.h"
61#include "bucomm.h"
3284fe0c 62#include "elfcomm.h"
0d646226 63#include "demanguse.h"
19e6b90e 64#include "dwarf.h"
7d9813f1 65#include "ctf-api.h"
42b6953b 66#include "sframe-api.h"
79bc120c 67#include "demangle.h"
252b5132
RH
68
69#include "elf/common.h"
70#include "elf/external.h"
71#include "elf/internal.h"
252b5132 72
4b78141a
NC
73
74/* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
75 we can obtain the H8 reloc numbers. We need these for the
76 get_reloc_size() function. We include h8.h again after defining
77 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
78
79#include "elf/h8.h"
80#undef _ELF_H8_H
81
82/* Undo the effects of #including reloc-macros.h. */
83
84#undef START_RELOC_NUMBERS
85#undef RELOC_NUMBER
86#undef FAKE_RELOC
87#undef EMPTY_RELOC
88#undef END_RELOC_NUMBERS
89#undef _RELOC_MACROS_H
90
252b5132
RH
91/* The following headers use the elf/reloc-macros.h file to
92 automatically generate relocation recognition functions
93 such as elf_mips_reloc_type() */
94
95#define RELOC_MACROS_GEN_FUNC
96
a06ea964 97#include "elf/aarch64.h"
252b5132 98#include "elf/alpha.h"
c077c580 99#include "elf/amdgpu.h"
3b16e843 100#include "elf/arc.h"
252b5132 101#include "elf/arm.h"
3b16e843 102#include "elf/avr.h"
1d65ded4 103#include "elf/bfin.h"
60bca95a 104#include "elf/cr16.h"
3b16e843 105#include "elf/cris.h"
1c0d3aa6 106#include "elf/crx.h"
b8891f8d 107#include "elf/csky.h"
252b5132
RH
108#include "elf/d10v.h"
109#include "elf/d30v.h"
d172d4ba 110#include "elf/dlx.h"
aca4efc7 111#include "elf/bpf.h"
cfb8c092 112#include "elf/epiphany.h"
252b5132 113#include "elf/fr30.h"
5c70f934 114#include "elf/frv.h"
3f8107ab 115#include "elf/ft32.h"
3b16e843
NC
116#include "elf/h8.h"
117#include "elf/hppa.h"
118#include "elf/i386.h"
f954747f
AM
119#include "elf/i370.h"
120#include "elf/i860.h"
121#include "elf/i960.h"
3b16e843 122#include "elf/ia64.h"
1e4cf259 123#include "elf/ip2k.h"
6e712424 124#include "elf/kvx.h"
84e94c90 125#include "elf/lm32.h"
1c0d3aa6 126#include "elf/iq2000.h"
49f58d10 127#include "elf/m32c.h"
3b16e843
NC
128#include "elf/m32r.h"
129#include "elf/m68k.h"
75751cd9 130#include "elf/m68hc11.h"
7b4ae824 131#include "elf/s12z.h"
252b5132 132#include "elf/mcore.h"
15ab5209 133#include "elf/mep.h"
a3c62988 134#include "elf/metag.h"
7ba29e2a 135#include "elf/microblaze.h"
3b16e843 136#include "elf/mips.h"
3c3bdf30 137#include "elf/mmix.h"
3b16e843
NC
138#include "elf/mn10200.h"
139#include "elf/mn10300.h"
5506d11a 140#include "elf/moxie.h"
4970f871 141#include "elf/mt.h"
2469cfa2 142#include "elf/msp430.h"
35c08157 143#include "elf/nds32.h"
fe944acf 144#include "elf/nfp.h"
13761a11 145#include "elf/nios2.h"
73589c9d 146#include "elf/or1k.h"
7d466069 147#include "elf/pj.h"
3b16e843 148#include "elf/ppc.h"
c833c019 149#include "elf/ppc64.h"
2b100bb5 150#include "elf/pru.h"
03336641 151#include "elf/riscv.h"
99c513f6 152#include "elf/rl78.h"
c7927a3c 153#include "elf/rx.h"
a85d7ed0 154#include "elf/s390.h"
1c0d3aa6 155#include "elf/score.h"
3b16e843
NC
156#include "elf/sh.h"
157#include "elf/sparc.h"
e9f53129 158#include "elf/spu.h"
40b36596 159#include "elf/tic6x.h"
aa137e4d
NC
160#include "elf/tilegx.h"
161#include "elf/tilepro.h"
3b16e843 162#include "elf/v850.h"
179d3252 163#include "elf/vax.h"
619ed720 164#include "elf/visium.h"
f96bd6c2 165#include "elf/wasm32.h"
3b16e843 166#include "elf/x86-64.h"
f6c1a2d5 167#include "elf/xgate.h"
93fbbb04 168#include "elf/xstormy16.h"
88da6820 169#include "elf/xtensa.h"
6655dba2 170#include "elf/z80.h"
e9a0721f 171#include "elf/loongarch.h"
b5c37946 172#include "elf/bpf.h"
252b5132 173
252b5132 174#include "getopt.h"
566b0d53 175#include "libiberty.h"
09c11c86 176#include "safe-ctype.h"
2cf0635d 177#include "filenames.h"
252b5132 178
15b42fb0
AM
179#ifndef offsetof
180#define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
181#endif
182
6a40cf0c
NC
183typedef struct elf_section_list
184{
dda8d76d
NC
185 Elf_Internal_Shdr * hdr;
186 struct elf_section_list * next;
6a40cf0c
NC
187} elf_section_list;
188
dda8d76d
NC
189/* Flag bits indicating particular types of dump. */
190#define HEX_DUMP (1 << 0) /* The -x command line switch. */
191#define DISASS_DUMP (1 << 1) /* The -i command line switch. */
192#define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
193#define STRING_DUMP (1 << 3) /* The -p command line switch. */
194#define RELOC_DUMP (1 << 4) /* The -R command line switch. */
d344b407 195#define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
42b6953b 196#define SFRAME_DUMP (1 << 6) /* The --sframe command line switch. */
dda8d76d
NC
197
198typedef unsigned char dump_type;
199
200/* A linked list of the section names for which dumps were requested. */
201struct dump_list_entry
202{
203 char * name;
204 dump_type type;
205 struct dump_list_entry * next;
206};
207
6431e409
AM
208/* A dynamic array of flags indicating for which sections a dump
209 has been requested via command line switches. */
1b513401
NC
210struct dump_data
211{
6431e409
AM
212 dump_type * dump_sects;
213 unsigned int num_dump_sects;
214};
215
216static struct dump_data cmdline;
217
218static struct dump_list_entry * dump_sects_byname;
219
2cf0635d 220char * program_name = "readelf";
dda8d76d 221
015dc7e1
AM
222static bool show_name = false;
223static bool do_dynamic = false;
224static bool do_syms = false;
225static bool do_dyn_syms = false;
226static bool do_lto_syms = false;
227static bool do_reloc = false;
228static bool do_sections = false;
229static bool do_section_groups = false;
230static bool do_section_details = false;
231static bool do_segments = false;
232static bool do_unwind = false;
233static bool do_using_dynamic = false;
234static bool do_header = false;
235static bool do_dump = false;
236static bool do_version = false;
237static bool do_histogram = false;
238static bool do_debugging = false;
239static bool do_ctf = false;
42b6953b 240static bool do_sframe = false;
015dc7e1
AM
241static bool do_arch = false;
242static bool do_notes = false;
243static bool do_archive_index = false;
244static bool check_all = false;
245static bool is_32bit_elf = false;
246static bool decompress_dumps = false;
247static bool do_not_show_symbol_truncation = false;
248static bool do_demangle = false; /* Pretty print C++ symbol names. */
249static bool process_links = false;
e1dbfc17 250static bool dump_any_debugging = false;
b6ac461a 251static bool extra_sym_info = false;
79bc120c 252static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
047c3dbf 253static int sym_base = 0;
252b5132 254
7d9813f1
NA
255static char *dump_ctf_parent_name;
256static char *dump_ctf_symtab_name;
257static char *dump_ctf_strtab_name;
258
e4b17d5c
L
259struct group_list
260{
dda8d76d
NC
261 struct group_list * next;
262 unsigned int section_index;
e4b17d5c
L
263};
264
265struct group
266{
dda8d76d
NC
267 struct group_list * root;
268 unsigned int group_index;
e4b17d5c
L
269};
270
978c4450
AM
271typedef struct filedata
272{
273 const char * file_name;
015dc7e1 274 bool is_separate;
978c4450 275 FILE * handle;
be7d229a 276 uint64_t file_size;
978c4450 277 Elf_Internal_Ehdr file_header;
26c527e6
AM
278 uint64_t archive_file_offset;
279 uint64_t archive_file_size;
066f8fbe 280 /* Everything below this point is cleared out by free_filedata. */
978c4450
AM
281 Elf_Internal_Shdr * section_headers;
282 Elf_Internal_Phdr * program_headers;
283 char * string_table;
26c527e6
AM
284 uint64_t string_table_length;
285 uint64_t dynamic_addr;
be7d229a 286 uint64_t dynamic_size;
26c527e6 287 uint64_t dynamic_nent;
978c4450 288 Elf_Internal_Dyn * dynamic_section;
8ac10c5b 289 Elf_Internal_Shdr * dynamic_strtab_section;
978c4450 290 char * dynamic_strings;
26c527e6 291 uint64_t dynamic_strings_length;
8ac10c5b 292 Elf_Internal_Shdr * dynamic_symtab_section;
26c527e6 293 uint64_t num_dynamic_syms;
978c4450 294 Elf_Internal_Sym * dynamic_symbols;
26c527e6 295 uint64_t version_info[16];
978c4450
AM
296 unsigned int dynamic_syminfo_nent;
297 Elf_Internal_Syminfo * dynamic_syminfo;
26c527e6 298 uint64_t dynamic_syminfo_offset;
be7d229a
AM
299 uint64_t nbuckets;
300 uint64_t nchains;
625d49fc
AM
301 uint64_t * buckets;
302 uint64_t * chains;
be7d229a
AM
303 uint64_t ngnubuckets;
304 uint64_t ngnuchains;
625d49fc
AM
305 uint64_t * gnubuckets;
306 uint64_t * gnuchains;
307 uint64_t * mipsxlat;
308 uint64_t gnusymidx;
13acb58d 309 char * program_interpreter;
bc227f4c 310 uint64_t dynamic_info[DT_RELRENT + 1];
625d49fc
AM
311 uint64_t dynamic_info_DT_GNU_HASH;
312 uint64_t dynamic_info_DT_MIPS_XHASH;
978c4450
AM
313 elf_section_list * symtab_shndx_list;
314 size_t group_count;
315 struct group * section_groups;
316 struct group ** section_headers_groups;
317 /* A dynamic array of flags indicating for which sections a dump of
318 some kind has been requested. It is reset on a per-object file
319 basis and then initialised from the cmdline_dump_sects array,
320 the results of interpreting the -w switch, and the
321 dump_sects_byname list. */
322 struct dump_data dump;
323} Filedata;
aef1f6d0 324
c256ffe7 325/* How to print a vma value. */
843dd992
NC
326typedef enum print_mode
327{
328 HEX,
047c3dbf 329 HEX_5,
843dd992
NC
330 DEC,
331 DEC_5,
332 UNSIGNED,
047c3dbf 333 UNSIGNED_5,
843dd992 334 PREFIX_HEX,
047c3dbf 335 PREFIX_HEX_5,
843dd992 336 FULL_HEX,
047c3dbf
NL
337 LONG_HEX,
338 OCTAL,
339 OCTAL_5
843dd992
NC
340}
341print_mode;
342
b3aa80b4
NC
343typedef enum unicode_display_type
344{
345 unicode_default = 0,
346 unicode_locale,
347 unicode_escape,
348 unicode_hex,
349 unicode_highlight,
350 unicode_invalid
351} unicode_display_type;
352
353static unicode_display_type unicode_display = unicode_default;
354
a7fd1186
FS
355typedef enum
356{
357 reltype_unknown,
358 reltype_rel,
359 reltype_rela,
360 reltype_relr
361} relocation_type;
362
bb4d2ac2
L
363/* Versioned symbol info. */
364enum versioned_symbol_info
365{
366 symbol_undefined,
367 symbol_hidden,
368 symbol_public
369};
370
63cf857e
AM
371static int
372fseek64 (FILE *stream, int64_t offset, int whence)
373{
374#if defined (HAVE_FSEEKO64)
375 off64_t o = offset;
376 if (o != offset)
377 {
378 errno = EINVAL;
379 return -1;
380 }
381 return fseeko64 (stream, o, whence);
382#elif defined (HAVE_FSEEKO)
383 off_t o = offset;
384 if (o != offset)
385 {
386 errno = EINVAL;
387 return -1;
388 }
389 return fseeko (stream, o, whence);
390#else
391 long o = offset;
392 if (o != offset)
393 {
394 errno = EINVAL;
395 return -1;
396 }
397 return fseek (stream, o, whence);
398#endif
399}
400
32ec8896 401static const char * get_symbol_version_string
26c527e6 402 (Filedata *, bool, const char *, size_t, unsigned,
32ec8896 403 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
bb4d2ac2 404
9c19a809
NC
405#define UNKNOWN -1
406
84714f86
AM
407static inline const char *
408section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
409{
410 return filedata->string_table + hdr->sh_name;
411}
b9e920ec 412
84714f86
AM
413static inline bool
414section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
415{
b6ac461a
NC
416 return (filedata != NULL
417 && hdr != NULL
84714f86
AM
418 && filedata->string_table != NULL
419 && hdr->sh_name < filedata->string_table_length);
420}
b9e920ec 421
b6ac461a
NC
422/* Returns true if the given index is real/valid. Note: "real" here
423 means "references a real section in the section header" and not
424 "is a valid section index as per the ELF standard". */
425
426static inline bool
427section_index_real (const Filedata *filedata, unsigned int ndx)
84714f86 428{
b6ac461a
NC
429 return (filedata != NULL
430 && filedata->section_headers != NULL
431 && ndx < filedata->file_header.e_shnum
432 && ndx > 0);
84714f86 433}
b6ac461a 434
ee42cf8c 435#define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
252b5132 436
84714f86
AM
437static inline bool
438valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
439{
440 return strtab != NULL && offset < strtab_size;
441}
442
443static inline bool
444valid_dynamic_name (const Filedata *filedata, uint64_t offset)
445{
446 return valid_symbol_name (filedata->dynamic_strings,
447 filedata->dynamic_strings_length, offset);
448}
449
d79b3d50
NC
450/* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
451 already been called and verified that the string exists. */
84714f86
AM
452static inline const char *
453get_dynamic_name (const Filedata *filedata, size_t offset)
454{
455 return filedata->dynamic_strings + offset;
456}
18bd398b 457
61865e30
NC
458#define REMOVE_ARCH_BITS(ADDR) \
459 do \
460 { \
dda8d76d 461 if (filedata->file_header.e_machine == EM_ARM) \
61865e30
NC
462 (ADDR) &= ~1; \
463 } \
464 while (0)
f16a9783
MS
465
466/* Get the correct GNU hash section name. */
978c4450
AM
467#define GNU_HASH_SECTION_NAME(filedata) \
468 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
d79b3d50 469\f
dda8d76d
NC
470/* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
471 OFFSET + the offset of the current archive member, if we are examining an
472 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
473 allocate a buffer using malloc and fill that. In either case return the
474 pointer to the start of the retrieved data or NULL if something went wrong.
475 If something does go wrong and REASON is not NULL then emit an error
476 message using REASON as part of the context. */
59245841 477
c256ffe7 478static void *
be7d229a
AM
479get_data (void *var,
480 Filedata *filedata,
26c527e6 481 uint64_t offset,
be7d229a
AM
482 uint64_t size,
483 uint64_t nmemb,
484 const char *reason)
a6e9f9df 485{
2cf0635d 486 void * mvar;
be7d229a 487 uint64_t amt = size * nmemb;
a6e9f9df 488
c256ffe7 489 if (size == 0 || nmemb == 0)
a6e9f9df
AM
490 return NULL;
491
be7d229a
AM
492 /* If size_t is smaller than uint64_t, eg because you are building
493 on a 32-bit host, then make sure that when the sizes are cast to
494 size_t no information is lost. */
7c1c1904
AM
495 if ((size_t) size != size
496 || (size_t) nmemb != nmemb
be7d229a
AM
497 || (size_t) amt != amt
498 || amt / size != nmemb
499 || (size_t) amt + 1 == 0)
57028622
NC
500 {
501 if (reason)
b8281767
AM
502 error (_("Size overflow prevents reading %" PRIu64
503 " elements of size %" PRIu64 " for %s\n"),
be7d229a 504 nmemb, size, reason);
57028622
NC
505 return NULL;
506 }
507
c22b42ce 508 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
c9c1d674 509 attempting to allocate memory when the read is bound to fail. */
978c4450
AM
510 if (filedata->archive_file_offset > filedata->file_size
511 || offset > filedata->file_size - filedata->archive_file_offset
512 || amt > filedata->file_size - filedata->archive_file_offset - offset)
a6e9f9df 513 {
049b0c3a 514 if (reason)
b8281767 515 error (_("Reading %" PRIu64 " bytes extends past end of file for %s\n"),
be7d229a 516 amt, reason);
a6e9f9df
AM
517 return NULL;
518 }
519
63cf857e
AM
520 if (fseek64 (filedata->handle, filedata->archive_file_offset + offset,
521 SEEK_SET))
071436c6
NC
522 {
523 if (reason)
26c527e6 524 error (_("Unable to seek to %#" PRIx64 " for %s\n"),
978c4450 525 filedata->archive_file_offset + offset, reason);
071436c6
NC
526 return NULL;
527 }
528
a6e9f9df
AM
529 mvar = var;
530 if (mvar == NULL)
531 {
7c1c1904
AM
532 /* + 1 so that we can '\0' terminate invalid string table sections. */
533 mvar = malloc ((size_t) amt + 1);
a6e9f9df
AM
534
535 if (mvar == NULL)
536 {
049b0c3a 537 if (reason)
b8281767 538 error (_("Out of memory allocating %" PRIu64 " bytes for %s\n"),
be7d229a 539 amt, reason);
a6e9f9df
AM
540 return NULL;
541 }
c256ffe7 542
c9c1d674 543 ((char *) mvar)[amt] = '\0';
a6e9f9df
AM
544 }
545
dda8d76d 546 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
a6e9f9df 547 {
049b0c3a 548 if (reason)
b8281767 549 error (_("Unable to read in %" PRIu64 " bytes of %s\n"),
be7d229a 550 amt, reason);
a6e9f9df
AM
551 if (mvar != var)
552 free (mvar);
553 return NULL;
554 }
555
556 return mvar;
557}
558
32ec8896
NC
559/* Print a VMA value in the MODE specified.
560 Returns the number of characters displayed. */
cb8f3167 561
32ec8896 562static unsigned int
625d49fc 563print_vma (uint64_t vma, print_mode mode)
66543521 564{
32ec8896 565 unsigned int nc = 0;
66543521 566
14a91970 567 switch (mode)
66543521 568 {
14a91970
AM
569 case FULL_HEX:
570 nc = printf ("0x");
1a0670f3 571 /* Fall through. */
14a91970 572 case LONG_HEX:
f493c217 573 if (!is_32bit_elf)
625d49fc
AM
574 return nc + printf ("%16.16" PRIx64, vma);
575 return nc + printf ("%8.8" PRIx64, vma);
b19aac67 576
14a91970
AM
577 case DEC_5:
578 if (vma <= 99999)
625d49fc 579 return printf ("%5" PRId64, vma);
1a0670f3 580 /* Fall through. */
14a91970
AM
581 case PREFIX_HEX:
582 nc = printf ("0x");
1a0670f3 583 /* Fall through. */
14a91970 584 case HEX:
625d49fc 585 return nc + printf ("%" PRIx64, vma);
b19aac67 586
047c3dbf
NL
587 case PREFIX_HEX_5:
588 nc = printf ("0x");
589 /* Fall through. */
590 case HEX_5:
625d49fc 591 return nc + printf ("%05" PRIx64, vma);
047c3dbf 592
14a91970 593 case DEC:
625d49fc 594 return printf ("%" PRId64, vma);
b19aac67 595
14a91970 596 case UNSIGNED:
625d49fc 597 return printf ("%" PRIu64, vma);
32ec8896 598
047c3dbf 599 case UNSIGNED_5:
625d49fc 600 return printf ("%5" PRIu64, vma);
047c3dbf
NL
601
602 case OCTAL:
625d49fc 603 return printf ("%" PRIo64, vma);
047c3dbf
NL
604
605 case OCTAL_5:
625d49fc 606 return printf ("%5" PRIo64, vma);
047c3dbf 607
32ec8896
NC
608 default:
609 /* FIXME: Report unrecognised mode ? */
610 return 0;
f7a99963 611 }
f7a99963
NC
612}
613
047c3dbf 614
7bfd842d 615/* Display a symbol on stdout. Handles the display of control characters and
3bfcb652 616 multibye characters (assuming the host environment supports them).
31104126 617
b6ac461a
NC
618 Display at most abs(WIDTH) characters, truncating as necessary,
619 unless do_wide or extra_sym_info is true.
7bfd842d 620
0942c7ab
NC
621 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
622 abs(WIDTH) - 5 characters followed by "[...]".
623
7bfd842d
NC
624 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
625 padding as necessary.
171191ba
NC
626
627 Returns the number of emitted characters. */
628
629static unsigned int
b6ac461a 630print_symbol_name (signed int width, const char * symbol)
31104126 631{
015dc7e1
AM
632 bool extra_padding = false;
633 bool do_dots = false;
32ec8896 634 signed int num_printed = 0;
3bfcb652 635#ifdef HAVE_MBSTATE_T
7bfd842d 636 mbstate_t state;
3bfcb652 637#endif
32ec8896 638 unsigned int width_remaining;
79bc120c 639 const void * alloced_symbol = NULL;
961c521f 640
7bfd842d 641 if (width < 0)
961c521f 642 {
88305e1b 643 /* Keep the width positive. This helps the code below. */
961c521f 644 width = - width;
015dc7e1 645 extra_padding = true;
0b4362b0 646 }
56d8f8a9
NC
647 else if (width == 0)
648 return 0;
961c521f 649
b6ac461a 650 if (do_wide || extra_sym_info)
7bfd842d
NC
651 /* Set the remaining width to a very large value.
652 This simplifies the code below. */
653 width_remaining = INT_MAX;
654 else
0942c7ab
NC
655 {
656 width_remaining = width;
b6ac461a 657
0942c7ab
NC
658 if (! do_not_show_symbol_truncation
659 && (int) strlen (symbol) > width)
660 {
661 width_remaining -= 5;
662 if ((int) width_remaining < 0)
663 width_remaining = 0;
015dc7e1 664 do_dots = true;
0942c7ab
NC
665 }
666 }
cb8f3167 667
3bfcb652 668#ifdef HAVE_MBSTATE_T
7bfd842d
NC
669 /* Initialise the multibyte conversion state. */
670 memset (& state, 0, sizeof (state));
3bfcb652 671#endif
961c521f 672
79bc120c
NC
673 if (do_demangle && *symbol)
674 {
675 const char * res = cplus_demangle (symbol, demangle_flags);
676
677 if (res != NULL)
678 alloced_symbol = symbol = res;
679 }
680
7bfd842d
NC
681 while (width_remaining)
682 {
683 size_t n;
7bfd842d 684 const char c = *symbol++;
961c521f 685
7bfd842d 686 if (c == 0)
961c521f
NC
687 break;
688
b3aa80b4
NC
689 if (ISPRINT (c))
690 {
691 putchar (c);
692 width_remaining --;
693 num_printed ++;
694 }
695 else if (ISCNTRL (c))
961c521f 696 {
b3aa80b4
NC
697 /* Do not print control characters directly as they can affect terminal
698 settings. Such characters usually appear in the names generated
699 by the assembler for local labels. */
700
7bfd842d 701 if (width_remaining < 2)
961c521f
NC
702 break;
703
7bfd842d
NC
704 printf ("^%c", c + 0x40);
705 width_remaining -= 2;
171191ba 706 num_printed += 2;
961c521f 707 }
b3aa80b4 708 else if (c == 0x7f)
7bfd842d 709 {
b3aa80b4
NC
710 if (width_remaining < 5)
711 break;
712 printf ("<DEL>");
713 width_remaining -= 5;
714 num_printed += 5;
715 }
716 else if (unicode_display != unicode_locale
717 && unicode_display != unicode_default)
718 {
719 /* Display unicode characters as something else. */
720 unsigned char bytes[4];
721 bool is_utf8;
795588ae 722 unsigned int nbytes;
b3aa80b4
NC
723
724 bytes[0] = c;
725
726 if (bytes[0] < 0xc0)
727 {
728 nbytes = 1;
729 is_utf8 = false;
730 }
731 else
732 {
733 bytes[1] = *symbol++;
734
735 if ((bytes[1] & 0xc0) != 0x80)
736 {
737 is_utf8 = false;
738 /* Do not consume this character. It may only
739 be the first byte in the sequence that was
740 corrupt. */
741 --symbol;
742 nbytes = 1;
743 }
744 else if ((bytes[0] & 0x20) == 0)
745 {
746 is_utf8 = true;
747 nbytes = 2;
748 }
749 else
750 {
751 bytes[2] = *symbol++;
752
753 if ((bytes[2] & 0xc0) != 0x80)
754 {
755 is_utf8 = false;
756 symbol -= 2;
757 nbytes = 1;
758 }
759 else if ((bytes[0] & 0x10) == 0)
760 {
761 is_utf8 = true;
762 nbytes = 3;
763 }
764 else
765 {
766 bytes[3] = *symbol++;
767
768 nbytes = 4;
769
770 if ((bytes[3] & 0xc0) != 0x80)
771 {
772 is_utf8 = false;
773 symbol -= 3;
774 nbytes = 1;
775 }
776 else
777 is_utf8 = true;
778 }
779 }
780 }
781
782 if (unicode_display == unicode_invalid)
783 is_utf8 = false;
784
785 if (unicode_display == unicode_hex || ! is_utf8)
786 {
795588ae 787 unsigned int i;
b3aa80b4
NC
788
789 if (width_remaining < (nbytes * 2) + 2)
790 break;
791
792 putchar (is_utf8 ? '<' : '{');
793 printf ("0x");
794 for (i = 0; i < nbytes; i++)
795 printf ("%02x", bytes[i]);
796 putchar (is_utf8 ? '>' : '}');
797 }
798 else
799 {
800 if (unicode_display == unicode_highlight && isatty (1))
801 printf ("\x1B[31;47m"); /* Red. */
802
803 switch (nbytes)
804 {
805 case 2:
806 if (width_remaining < 6)
807 break;
808 printf ("\\u%02x%02x",
809 (bytes[0] & 0x1c) >> 2,
810 ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
811 break;
812 case 3:
813 if (width_remaining < 6)
814 break;
815 printf ("\\u%02x%02x",
816 ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
817 ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
818 break;
819 case 4:
820 if (width_remaining < 8)
821 break;
822 printf ("\\u%02x%02x%02x",
823 ((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
824 ((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
825 ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
826
827 break;
828 default:
829 /* URG. */
830 break;
831 }
832
833 if (unicode_display == unicode_highlight && isatty (1))
834 printf ("\033[0m"); /* Default colour. */
835 }
836
837 if (bytes[nbytes - 1] == 0)
838 break;
7bfd842d 839 }
961c521f
NC
840 else
841 {
3bfcb652
NC
842#ifdef HAVE_MBSTATE_T
843 wchar_t w;
844#endif
7bfd842d
NC
845 /* Let printf do the hard work of displaying multibyte characters. */
846 printf ("%.1s", symbol - 1);
847 width_remaining --;
848 num_printed ++;
849
3bfcb652 850#ifdef HAVE_MBSTATE_T
7bfd842d
NC
851 /* Try to find out how many bytes made up the character that was
852 just printed. Advance the symbol pointer past the bytes that
853 were displayed. */
854 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
3bfcb652
NC
855#else
856 n = 1;
857#endif
7bfd842d
NC
858 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
859 symbol += (n - 1);
961c521f 860 }
961c521f 861 }
171191ba 862
0942c7ab
NC
863 if (do_dots)
864 num_printed += printf ("[...]");
865
7bfd842d 866 if (extra_padding && num_printed < width)
171191ba
NC
867 {
868 /* Fill in the remaining spaces. */
7bfd842d
NC
869 printf ("%-*s", width - num_printed, " ");
870 num_printed = width;
171191ba
NC
871 }
872
79bc120c 873 free ((void *) alloced_symbol);
171191ba 874 return num_printed;
31104126
NC
875}
876
1449284b 877/* Returns a pointer to a static buffer containing a printable version of
74e1a04b
NC
878 the given section's name. Like print_symbol, except that it does not try
879 to print multibyte characters, it just interprets them as hex values. */
880
881static const char *
dda8d76d 882printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
74e1a04b 883{
b6ac461a
NC
884#define NUM_SEC_NAME_BUFS 5
885#define MAX_PRINT_SEC_NAME_LEN 256
886
887 static int sec_name_buf_index = 0;
888 /* We use a rotating array of static buffers, so that multiple successive calls
889 to printable_section_name() will still work. eg when used in a printf. */
890 static char sec_name_buf [NUM_SEC_NAME_BUFS][MAX_PRINT_SEC_NAME_LEN + 1];
891
892 const char * name;
893 char * buf;
894 char * buf_start;
74e1a04b
NC
895 char c;
896 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
897
b6ac461a
NC
898 /* Validate the input parameters. */
899 if (filedata == NULL)
900 return _("<internal error>");
901 if (sec == NULL)
902 return _("<none>");
903 if (filedata->string_table == NULL)
904 return _("<no-strings>");
905 if (sec->sh_name >= filedata->string_table_length)
906 return _("<corrupt>");
907
908 /* Select a buffer to use. */
909 buf_start = buf = sec_name_buf[sec_name_buf_index];
910 if (++sec_name_buf_index >= NUM_SEC_NAME_BUFS)
911 sec_name_buf_index = 0;
912
913 name = section_name (filedata, sec);
914
74e1a04b
NC
915 while ((c = * name ++) != 0)
916 {
917 if (ISCNTRL (c))
918 {
919 if (remaining < 2)
920 break;
948f632f 921
74e1a04b
NC
922 * buf ++ = '^';
923 * buf ++ = c + 0x40;
924 remaining -= 2;
925 }
926 else if (ISPRINT (c))
927 {
928 * buf ++ = c;
929 remaining -= 1;
930 }
931 else
932 {
933 static char hex[17] = "0123456789ABCDEF";
934
935 if (remaining < 4)
936 break;
937 * buf ++ = '<';
938 * buf ++ = hex[(c & 0xf0) >> 4];
939 * buf ++ = hex[c & 0x0f];
940 * buf ++ = '>';
941 remaining -= 4;
942 }
943
944 if (remaining == 0)
945 break;
946 }
947
948 * buf = 0;
b6ac461a
NC
949 return buf_start;
950}
951
952/* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
953 This OS has so many departures from the ELF standard that we test it at
954 many places. */
955
956static inline bool
957is_ia64_vms (Filedata * filedata)
958{
959 return filedata->file_header.e_machine == EM_IA_64
960 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
74e1a04b
NC
961}
962
963static const char *
b6ac461a
NC
964printable_section_name_from_index (Filedata * filedata,
965 size_t ndx,
966 bool * is_special)
74e1a04b 967{
b6ac461a
NC
968 if (is_special != NULL)
969 * is_special = true;
970
971 switch (ndx)
972 {
973 case SHN_UNDEF: return "UND";
974 case SHN_ABS: return "ABS";
975 case SHN_COMMON: return "COM";
976 break;
977 }
978
979 if (filedata != NULL)
980 {
981 switch (filedata->file_header.e_machine)
982 {
983 case EM_MIPS:
984 if (ndx == SHN_MIPS_SCOMMON)
985 return "SCOMMON";
986 if (ndx == SHN_MIPS_SUNDEFINED)
987 return "SUNDEF";
988 break;
989
990 case EM_TI_C6000:
991 if (ndx == SHN_TIC6X_SCOMMON)
992 return "SCOM";
993 break;
994
995 case EM_X86_64:
996 case EM_L1OM:
997 case EM_K1OM:
998 if (ndx == SHN_X86_64_LCOMMON)
999 return "LARGE_COM";
1000 break;
1001
1002 case EM_IA_64:
1003 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1004 && ndx == SHN_IA_64_ANSI_COMMON)
1005 return "ANSI_COM";
1006
1007 if (is_ia64_vms (filedata) && ndx == SHN_IA_64_VMS_SYMVEC)
1008 return "VMS_SYMVEC";
1009 break;
1010
1011 default:
1012 break;
1013 }
74e1a04b 1014
b6ac461a
NC
1015 if (filedata->section_headers != NULL
1016 && ndx < filedata->file_header.e_shnum)
1017 {
1018 const char * res;
1019
1020 res = printable_section_name (filedata, filedata->section_headers + ndx);
1021 if (is_special != NULL)
1022 * is_special = (res[0] == '<');
1023
1024 return res;
1025 }
1026 }
1027
1028 static char name_buf[40];
1029 unsigned int short_ndx = (unsigned int) (ndx & 0xffff);
1030
1031 if (ndx >= SHN_LOPROC && ndx <= SHN_HIPROC)
1032 sprintf (name_buf, "PRC[0x%04x]", short_ndx);
1033 else if (ndx >= SHN_LOOS && ndx <= SHN_HIOS)
1034 sprintf (name_buf, "OS [0x%04x]", short_ndx);
1035 else if (ndx >= SHN_LORESERVE)
1036 sprintf (name_buf, "RSV[0x%04x]", short_ndx);
1037 else if (filedata->file_header.e_shnum != 0
1038 && ndx >= filedata->file_header.e_shnum)
1039 sprintf (name_buf, _("BAD[0x%lx]"), (long) ndx);
1040 else
1041 sprintf (name_buf, "<section 0x%lx>", (long) ndx);
1042
1043 return name_buf;
74e1a04b
NC
1044}
1045
89fac5e3
RS
1046/* Return a pointer to section NAME, or NULL if no such section exists. */
1047
1048static Elf_Internal_Shdr *
dda8d76d 1049find_section (Filedata * filedata, const char * name)
89fac5e3
RS
1050{
1051 unsigned int i;
1052
68807c3c
NC
1053 if (filedata->section_headers == NULL)
1054 return NULL;
dda8d76d
NC
1055
1056 for (i = 0; i < filedata->file_header.e_shnum; i++)
84714f86
AM
1057 if (section_name_valid (filedata, filedata->section_headers + i)
1058 && streq (section_name (filedata, filedata->section_headers + i),
1059 name))
dda8d76d 1060 return filedata->section_headers + i;
89fac5e3
RS
1061
1062 return NULL;
1063}
1064
0b6ae522
DJ
1065/* Return a pointer to a section containing ADDR, or NULL if no such
1066 section exists. */
1067
1068static Elf_Internal_Shdr *
625d49fc 1069find_section_by_address (Filedata * filedata, uint64_t addr)
0b6ae522
DJ
1070{
1071 unsigned int i;
1072
68807c3c
NC
1073 if (filedata->section_headers == NULL)
1074 return NULL;
1075
dda8d76d 1076 for (i = 0; i < filedata->file_header.e_shnum; i++)
0b6ae522 1077 {
dda8d76d
NC
1078 Elf_Internal_Shdr *sec = filedata->section_headers + i;
1079
0b6ae522
DJ
1080 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
1081 return sec;
1082 }
1083
1084 return NULL;
1085}
1086
071436c6 1087static Elf_Internal_Shdr *
dda8d76d 1088find_section_by_type (Filedata * filedata, unsigned int type)
071436c6
NC
1089{
1090 unsigned int i;
1091
68807c3c
NC
1092 if (filedata->section_headers == NULL)
1093 return NULL;
1094
dda8d76d 1095 for (i = 0; i < filedata->file_header.e_shnum; i++)
071436c6 1096 {
dda8d76d
NC
1097 Elf_Internal_Shdr *sec = filedata->section_headers + i;
1098
071436c6
NC
1099 if (sec->sh_type == type)
1100 return sec;
1101 }
1102
1103 return NULL;
1104}
1105
657d0d47
CC
1106/* Return a pointer to section NAME, or NULL if no such section exists,
1107 restricted to the list of sections given in SET. */
1108
1109static Elf_Internal_Shdr *
dda8d76d 1110find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
657d0d47
CC
1111{
1112 unsigned int i;
1113
68807c3c
NC
1114 if (filedata->section_headers == NULL)
1115 return NULL;
1116
657d0d47
CC
1117 if (set != NULL)
1118 {
1119 while ((i = *set++) > 0)
b814a36d
NC
1120 {
1121 /* See PR 21156 for a reproducer. */
dda8d76d 1122 if (i >= filedata->file_header.e_shnum)
b814a36d
NC
1123 continue; /* FIXME: Should we issue an error message ? */
1124
84714f86
AM
1125 if (section_name_valid (filedata, filedata->section_headers + i)
1126 && streq (section_name (filedata, filedata->section_headers + i),
1127 name))
dda8d76d 1128 return filedata->section_headers + i;
b814a36d 1129 }
657d0d47
CC
1130 }
1131
dda8d76d 1132 return find_section (filedata, name);
657d0d47
CC
1133}
1134
bcedfee6 1135/* Guess the relocation size commonly used by the specific machines. */
252b5132 1136
015dc7e1 1137static bool
2dc4cec1 1138guess_is_rela (unsigned int e_machine)
252b5132 1139{
9c19a809 1140 switch (e_machine)
252b5132
RH
1141 {
1142 /* Targets that use REL relocations. */
252b5132 1143 case EM_386:
22abe556 1144 case EM_IAMCU:
f954747f 1145 case EM_960:
e9f53129 1146 case EM_ARM:
2b0337b0 1147 case EM_D10V:
252b5132 1148 case EM_CYGNUS_D10V:
e9f53129 1149 case EM_DLX:
252b5132 1150 case EM_MIPS:
4fe85591 1151 case EM_MIPS_RS3_LE:
e9f53129 1152 case EM_CYGNUS_M32R:
1c0d3aa6 1153 case EM_SCORE:
f6c1a2d5 1154 case EM_XGATE:
fe944acf 1155 case EM_NFP:
aca4efc7 1156 case EM_BPF:
015dc7e1 1157 return false;
103f02d3 1158
252b5132
RH
1159 /* Targets that use RELA relocations. */
1160 case EM_68K:
f954747f 1161 case EM_860:
a06ea964 1162 case EM_AARCH64:
cfb8c092 1163 case EM_ADAPTEVA_EPIPHANY:
e9f53129
AM
1164 case EM_ALPHA:
1165 case EM_ALTERA_NIOS2:
886a2506
NC
1166 case EM_ARC:
1167 case EM_ARC_COMPACT:
1168 case EM_ARC_COMPACT2:
b5c37946
SJ
1169 case EM_ARC_COMPACT3:
1170 case EM_ARC_COMPACT3_64:
e9f53129
AM
1171 case EM_AVR:
1172 case EM_AVR_OLD:
1173 case EM_BLACKFIN:
60bca95a 1174 case EM_CR16:
e9f53129
AM
1175 case EM_CRIS:
1176 case EM_CRX:
b8891f8d 1177 case EM_CSKY:
2b0337b0 1178 case EM_D30V:
252b5132 1179 case EM_CYGNUS_D30V:
2b0337b0 1180 case EM_FR30:
3f8107ab 1181 case EM_FT32:
252b5132 1182 case EM_CYGNUS_FR30:
5c70f934 1183 case EM_CYGNUS_FRV:
e9f53129
AM
1184 case EM_H8S:
1185 case EM_H8_300:
1186 case EM_H8_300H:
800eeca4 1187 case EM_IA_64:
1e4cf259
NC
1188 case EM_IP2K:
1189 case EM_IP2K_OLD:
3b36097d 1190 case EM_IQ2000:
6e712424 1191 case EM_KVX:
84e94c90 1192 case EM_LATTICEMICO32:
ff7eeb89 1193 case EM_M32C_OLD:
49f58d10 1194 case EM_M32C:
e9f53129
AM
1195 case EM_M32R:
1196 case EM_MCORE:
15ab5209 1197 case EM_CYGNUS_MEP:
a3c62988 1198 case EM_METAG:
e9f53129
AM
1199 case EM_MMIX:
1200 case EM_MN10200:
1201 case EM_CYGNUS_MN10200:
1202 case EM_MN10300:
1203 case EM_CYGNUS_MN10300:
5506d11a 1204 case EM_MOXIE:
e9f53129
AM
1205 case EM_MSP430:
1206 case EM_MSP430_OLD:
d031aafb 1207 case EM_MT:
35c08157 1208 case EM_NDS32:
64fd6348 1209 case EM_NIOS32:
73589c9d 1210 case EM_OR1K:
e9f53129
AM
1211 case EM_PPC64:
1212 case EM_PPC:
2b100bb5 1213 case EM_TI_PRU:
e23eba97 1214 case EM_RISCV:
99c513f6 1215 case EM_RL78:
c7927a3c 1216 case EM_RX:
e9f53129
AM
1217 case EM_S390:
1218 case EM_S390_OLD:
1219 case EM_SH:
1220 case EM_SPARC:
1221 case EM_SPARC32PLUS:
1222 case EM_SPARCV9:
1223 case EM_SPU:
40b36596 1224 case EM_TI_C6000:
aa137e4d
NC
1225 case EM_TILEGX:
1226 case EM_TILEPRO:
708e2187 1227 case EM_V800:
e9f53129
AM
1228 case EM_V850:
1229 case EM_CYGNUS_V850:
1230 case EM_VAX:
619ed720 1231 case EM_VISIUM:
e9f53129 1232 case EM_X86_64:
8a9036a4 1233 case EM_L1OM:
7a9068fe 1234 case EM_K1OM:
e9f53129
AM
1235 case EM_XSTORMY16:
1236 case EM_XTENSA:
1237 case EM_XTENSA_OLD:
7ba29e2a
NC
1238 case EM_MICROBLAZE:
1239 case EM_MICROBLAZE_OLD:
f96bd6c2 1240 case EM_WEBASSEMBLY:
015dc7e1 1241 return true;
103f02d3 1242
e9f53129
AM
1243 case EM_68HC05:
1244 case EM_68HC08:
1245 case EM_68HC11:
1246 case EM_68HC16:
1247 case EM_FX66:
1248 case EM_ME16:
d1133906 1249 case EM_MMA:
d1133906
NC
1250 case EM_NCPU:
1251 case EM_NDR1:
e9f53129 1252 case EM_PCP:
d1133906 1253 case EM_ST100:
e9f53129 1254 case EM_ST19:
d1133906 1255 case EM_ST7:
e9f53129
AM
1256 case EM_ST9PLUS:
1257 case EM_STARCORE:
d1133906 1258 case EM_SVX:
e9f53129 1259 case EM_TINYJ:
9c19a809
NC
1260 default:
1261 warn (_("Don't know about relocations on this machine architecture\n"));
015dc7e1 1262 return false;
9c19a809
NC
1263 }
1264}
252b5132 1265
dda8d76d 1266/* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
32ec8896
NC
1267 Returns TRUE upon success, FALSE otherwise. If successful then a
1268 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
1269 and the number of relocs loaded is placed in *NRELASP. It is the caller's
1270 responsibility to free the allocated buffer. */
1271
015dc7e1 1272static bool
26c527e6
AM
1273slurp_rela_relocs (Filedata *filedata,
1274 uint64_t rel_offset,
1275 uint64_t rel_size,
1276 Elf_Internal_Rela **relasp,
1277 uint64_t *nrelasp)
9c19a809 1278{
2cf0635d 1279 Elf_Internal_Rela * relas;
26c527e6 1280 uint64_t nrelas;
4d6ed7c8 1281 unsigned int i;
252b5132 1282
4d6ed7c8
NC
1283 if (is_32bit_elf)
1284 {
2cf0635d 1285 Elf32_External_Rela * erelas;
103f02d3 1286
dda8d76d 1287 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
9cf03b7e 1288 rel_size, _("32-bit relocation data"));
a6e9f9df 1289 if (!erelas)
015dc7e1 1290 return false;
252b5132 1291
4d6ed7c8 1292 nrelas = rel_size / sizeof (Elf32_External_Rela);
103f02d3 1293
3f5e193b
NC
1294 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1295 sizeof (Elf_Internal_Rela));
103f02d3 1296
4d6ed7c8
NC
1297 if (relas == NULL)
1298 {
c256ffe7 1299 free (erelas);
591a748a 1300 error (_("out of memory parsing relocs\n"));
015dc7e1 1301 return false;
4d6ed7c8 1302 }
103f02d3 1303
4d6ed7c8
NC
1304 for (i = 0; i < nrelas; i++)
1305 {
1306 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1307 relas[i].r_info = BYTE_GET (erelas[i].r_info);
598aaa76 1308 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
4d6ed7c8 1309 }
103f02d3 1310
4d6ed7c8
NC
1311 free (erelas);
1312 }
1313 else
1314 {
2cf0635d 1315 Elf64_External_Rela * erelas;
103f02d3 1316
dda8d76d 1317 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
9cf03b7e 1318 rel_size, _("64-bit relocation data"));
a6e9f9df 1319 if (!erelas)
015dc7e1 1320 return false;
4d6ed7c8
NC
1321
1322 nrelas = rel_size / sizeof (Elf64_External_Rela);
103f02d3 1323
3f5e193b
NC
1324 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1325 sizeof (Elf_Internal_Rela));
103f02d3 1326
4d6ed7c8
NC
1327 if (relas == NULL)
1328 {
c256ffe7 1329 free (erelas);
591a748a 1330 error (_("out of memory parsing relocs\n"));
015dc7e1 1331 return false;
9c19a809 1332 }
4d6ed7c8
NC
1333
1334 for (i = 0; i < nrelas; i++)
9c19a809 1335 {
66543521
AM
1336 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1337 relas[i].r_info = BYTE_GET (erelas[i].r_info);
598aaa76 1338 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
861fb55a 1339
dda8d76d
NC
1340 if (filedata->file_header.e_machine == EM_MIPS
1341 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
861fb55a
DJ
1342 {
1343 /* In little-endian objects, r_info isn't really a
1344 64-bit little-endian value: it has a 32-bit
1345 little-endian symbol index followed by four
1346 individual byte fields. Reorder INFO
1347 accordingly. */
625d49fc 1348 uint64_t inf = relas[i].r_info;
91d6fa6a
NC
1349 inf = (((inf & 0xffffffff) << 32)
1350 | ((inf >> 56) & 0xff)
1351 | ((inf >> 40) & 0xff00)
1352 | ((inf >> 24) & 0xff0000)
1353 | ((inf >> 8) & 0xff000000));
1354 relas[i].r_info = inf;
861fb55a 1355 }
4d6ed7c8 1356 }
103f02d3 1357
4d6ed7c8
NC
1358 free (erelas);
1359 }
32ec8896 1360
4d6ed7c8
NC
1361 *relasp = relas;
1362 *nrelasp = nrelas;
015dc7e1 1363 return true;
4d6ed7c8 1364}
103f02d3 1365
dda8d76d 1366/* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
32ec8896
NC
1367 Returns TRUE upon success, FALSE otherwise. If successful then a
1368 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1369 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1370 responsibility to free the allocated buffer. */
1371
015dc7e1 1372static bool
26c527e6
AM
1373slurp_rel_relocs (Filedata *filedata,
1374 uint64_t rel_offset,
1375 uint64_t rel_size,
1376 Elf_Internal_Rela **relsp,
1377 uint64_t *nrelsp)
4d6ed7c8 1378{
2cf0635d 1379 Elf_Internal_Rela * rels;
26c527e6 1380 uint64_t nrels;
4d6ed7c8 1381 unsigned int i;
103f02d3 1382
4d6ed7c8
NC
1383 if (is_32bit_elf)
1384 {
2cf0635d 1385 Elf32_External_Rel * erels;
103f02d3 1386
dda8d76d 1387 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
9cf03b7e 1388 rel_size, _("32-bit relocation data"));
a6e9f9df 1389 if (!erels)
015dc7e1 1390 return false;
103f02d3 1391
4d6ed7c8 1392 nrels = rel_size / sizeof (Elf32_External_Rel);
103f02d3 1393
3f5e193b 1394 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
103f02d3 1395
4d6ed7c8
NC
1396 if (rels == NULL)
1397 {
c256ffe7 1398 free (erels);
591a748a 1399 error (_("out of memory parsing relocs\n"));
015dc7e1 1400 return false;
4d6ed7c8
NC
1401 }
1402
1403 for (i = 0; i < nrels; i++)
1404 {
1405 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1406 rels[i].r_info = BYTE_GET (erels[i].r_info);
c8286bd1 1407 rels[i].r_addend = 0;
9ea033b2 1408 }
4d6ed7c8
NC
1409
1410 free (erels);
9c19a809
NC
1411 }
1412 else
1413 {
2cf0635d 1414 Elf64_External_Rel * erels;
9ea033b2 1415
dda8d76d 1416 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
9cf03b7e 1417 rel_size, _("64-bit relocation data"));
a6e9f9df 1418 if (!erels)
015dc7e1 1419 return false;
103f02d3 1420
4d6ed7c8 1421 nrels = rel_size / sizeof (Elf64_External_Rel);
103f02d3 1422
3f5e193b 1423 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
103f02d3 1424
4d6ed7c8 1425 if (rels == NULL)
9c19a809 1426 {
c256ffe7 1427 free (erels);
591a748a 1428 error (_("out of memory parsing relocs\n"));
015dc7e1 1429 return false;
4d6ed7c8 1430 }
103f02d3 1431
4d6ed7c8
NC
1432 for (i = 0; i < nrels; i++)
1433 {
66543521
AM
1434 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1435 rels[i].r_info = BYTE_GET (erels[i].r_info);
c8286bd1 1436 rels[i].r_addend = 0;
861fb55a 1437
dda8d76d
NC
1438 if (filedata->file_header.e_machine == EM_MIPS
1439 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
861fb55a
DJ
1440 {
1441 /* In little-endian objects, r_info isn't really a
1442 64-bit little-endian value: it has a 32-bit
1443 little-endian symbol index followed by four
1444 individual byte fields. Reorder INFO
1445 accordingly. */
625d49fc 1446 uint64_t inf = rels[i].r_info;
91d6fa6a
NC
1447 inf = (((inf & 0xffffffff) << 32)
1448 | ((inf >> 56) & 0xff)
1449 | ((inf >> 40) & 0xff00)
1450 | ((inf >> 24) & 0xff0000)
1451 | ((inf >> 8) & 0xff000000));
1452 rels[i].r_info = inf;
861fb55a 1453 }
4d6ed7c8 1454 }
103f02d3 1455
4d6ed7c8
NC
1456 free (erels);
1457 }
32ec8896 1458
4d6ed7c8
NC
1459 *relsp = rels;
1460 *nrelsp = nrels;
015dc7e1 1461 return true;
4d6ed7c8 1462}
103f02d3 1463
a7fd1186 1464static bool
26c527e6
AM
1465slurp_relr_relocs (Filedata *filedata,
1466 uint64_t relr_offset,
1467 uint64_t relr_size,
1468 uint64_t **relrsp,
1469 uint64_t *nrelrsp)
a7fd1186
FS
1470{
1471 void *relrs;
1472 size_t size = 0, nentries, i;
625d49fc 1473 uint64_t base = 0, addr, entry;
a7fd1186
FS
1474
1475 relrs = get_data (NULL, filedata, relr_offset, 1, relr_size,
1476 _("RELR relocation data"));
1477 if (!relrs)
1478 return false;
1479
1480 if (is_32bit_elf)
1481 nentries = relr_size / sizeof (Elf32_External_Relr);
1482 else
1483 nentries = relr_size / sizeof (Elf64_External_Relr);
1484 for (i = 0; i < nentries; i++)
1485 {
1486 if (is_32bit_elf)
1487 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1488 else
1489 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1490 if ((entry & 1) == 0)
1491 size++;
1492 else
1493 while ((entry >>= 1) != 0)
1494 if ((entry & 1) == 1)
1495 size++;
1496 }
1497
625d49fc 1498 *relrsp = malloc (size * sizeof (**relrsp));
a7fd1186
FS
1499 if (*relrsp == NULL)
1500 {
1501 free (relrs);
1502 error (_("out of memory parsing relocs\n"));
1503 return false;
1504 }
1505
1506 size = 0;
1507 for (i = 0; i < nentries; i++)
1508 {
625d49fc 1509 const uint64_t entry_bytes = is_32bit_elf ? 4 : 8;
a7fd1186
FS
1510
1511 if (is_32bit_elf)
1512 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1513 else
1514 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1515 if ((entry & 1) == 0)
1516 {
1517 (*relrsp)[size++] = entry;
1518 base = entry + entry_bytes;
1519 }
1520 else
1521 {
1522 for (addr = base; (entry >>= 1) != 0; addr += entry_bytes)
1523 if ((entry & 1) != 0)
1524 (*relrsp)[size++] = addr;
1525 base += entry_bytes * (entry_bytes * CHAR_BIT - 1);
1526 }
1527 }
1528
1529 *nrelrsp = size;
1530 free (relrs);
1531 return true;
1532}
1533
aca88567
NC
1534/* Returns the reloc type extracted from the reloc info field. */
1535
1536static unsigned int
625d49fc 1537get_reloc_type (Filedata * filedata, uint64_t reloc_info)
aca88567
NC
1538{
1539 if (is_32bit_elf)
1540 return ELF32_R_TYPE (reloc_info);
1541
dda8d76d 1542 switch (filedata->file_header.e_machine)
aca88567
NC
1543 {
1544 case EM_MIPS:
1545 /* Note: We assume that reloc_info has already been adjusted for us. */
1546 return ELF64_MIPS_R_TYPE (reloc_info);
1547
1548 case EM_SPARCV9:
1549 return ELF64_R_TYPE_ID (reloc_info);
1550
1551 default:
1552 return ELF64_R_TYPE (reloc_info);
1553 }
1554}
1555
1556/* Return the symbol index extracted from the reloc info field. */
1557
625d49fc
AM
1558static uint64_t
1559get_reloc_symindex (uint64_t reloc_info)
aca88567
NC
1560{
1561 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1562}
1563
015dc7e1 1564static inline bool
dda8d76d 1565uses_msp430x_relocs (Filedata * filedata)
13761a11
NC
1566{
1567 return
dda8d76d 1568 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
13761a11 1569 /* GCC uses osabi == ELFOSBI_STANDALONE. */
dda8d76d 1570 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
13761a11 1571 /* TI compiler uses ELFOSABI_NONE. */
dda8d76d 1572 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
13761a11
NC
1573}
1574
d3ba0551
AM
1575/* Display the contents of the relocation data found at the specified
1576 offset. */
ee42cf8c 1577
015dc7e1 1578static bool
26c527e6
AM
1579dump_relocations (Filedata *filedata,
1580 uint64_t rel_offset,
1581 uint64_t rel_size,
1582 Elf_Internal_Sym *symtab,
1583 uint64_t nsyms,
1584 char *strtab,
1585 uint64_t strtablen,
1586 relocation_type rel_type,
1587 bool is_dynsym)
1588{
1589 size_t i;
2cf0635d 1590 Elf_Internal_Rela * rels;
015dc7e1 1591 bool res = true;
103f02d3 1592
a7fd1186
FS
1593 if (rel_type == reltype_unknown)
1594 rel_type = guess_is_rela (filedata->file_header.e_machine) ? reltype_rela : reltype_rel;
103f02d3 1595
a7fd1186 1596 if (rel_type == reltype_rela)
4d6ed7c8 1597 {
dda8d76d 1598 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
015dc7e1 1599 return false;
4d6ed7c8 1600 }
a7fd1186 1601 else if (rel_type == reltype_rel)
4d6ed7c8 1602 {
dda8d76d 1603 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
015dc7e1 1604 return false;
252b5132 1605 }
a7fd1186
FS
1606 else if (rel_type == reltype_relr)
1607 {
625d49fc 1608 uint64_t * relrs;
a7fd1186 1609 const char *format
b8281767 1610 = is_32bit_elf ? "%08" PRIx64 "\n" : "%016" PRIx64 "\n";
a7fd1186
FS
1611
1612 if (!slurp_relr_relocs (filedata, rel_offset, rel_size, &relrs,
1613 &rel_size))
1614 return false;
1615
26c527e6
AM
1616 printf (ngettext (" %" PRIu64 " offset\n",
1617 " %" PRIu64 " offsets\n", rel_size),
b8281767 1618 rel_size);
a7fd1186 1619 for (i = 0; i < rel_size; i++)
625d49fc 1620 printf (format, relrs[i]);
a7fd1186
FS
1621 free (relrs);
1622 return true;
1623 }
252b5132 1624
410f7a12
L
1625 if (is_32bit_elf)
1626 {
a7fd1186 1627 if (rel_type == reltype_rela)
2c71103e
NC
1628 {
1629 if (do_wide)
1630 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1631 else
1632 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1633 }
410f7a12 1634 else
2c71103e
NC
1635 {
1636 if (do_wide)
1637 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1638 else
1639 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1640 }
410f7a12 1641 }
252b5132 1642 else
410f7a12 1643 {
a7fd1186 1644 if (rel_type == reltype_rela)
2c71103e
NC
1645 {
1646 if (do_wide)
8beeaeb7 1647 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
2c71103e
NC
1648 else
1649 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1650 }
410f7a12 1651 else
2c71103e
NC
1652 {
1653 if (do_wide)
8beeaeb7 1654 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
2c71103e
NC
1655 else
1656 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1657 }
410f7a12 1658 }
252b5132
RH
1659
1660 for (i = 0; i < rel_size; i++)
1661 {
2cf0635d 1662 const char * rtype;
625d49fc
AM
1663 uint64_t offset;
1664 uint64_t inf;
1665 uint64_t symtab_index;
1666 uint64_t type;
103f02d3 1667
b34976b6 1668 offset = rels[i].r_offset;
91d6fa6a 1669 inf = rels[i].r_info;
103f02d3 1670
dda8d76d 1671 type = get_reloc_type (filedata, inf);
91d6fa6a 1672 symtab_index = get_reloc_symindex (inf);
252b5132 1673
410f7a12
L
1674 if (is_32bit_elf)
1675 {
39dbeff8
AM
1676 printf ("%8.8lx %8.8lx ",
1677 (unsigned long) offset & 0xffffffff,
91d6fa6a 1678 (unsigned long) inf & 0xffffffff);
410f7a12
L
1679 }
1680 else
1681 {
39dbeff8 1682 printf (do_wide
b8281767
AM
1683 ? "%16.16" PRIx64 " %16.16" PRIx64 " "
1684 : "%12.12" PRIx64 " %12.12" PRIx64 " ",
625d49fc 1685 offset, inf);
410f7a12 1686 }
103f02d3 1687
dda8d76d 1688 switch (filedata->file_header.e_machine)
252b5132
RH
1689 {
1690 default:
1691 rtype = NULL;
1692 break;
1693
a06ea964
NC
1694 case EM_AARCH64:
1695 rtype = elf_aarch64_reloc_type (type);
1696 break;
1697
2b0337b0 1698 case EM_M32R:
252b5132 1699 case EM_CYGNUS_M32R:
9ea033b2 1700 rtype = elf_m32r_reloc_type (type);
252b5132
RH
1701 break;
1702
1703 case EM_386:
22abe556 1704 case EM_IAMCU:
9ea033b2 1705 rtype = elf_i386_reloc_type (type);
252b5132
RH
1706 break;
1707
ba2685cc
AM
1708 case EM_68HC11:
1709 case EM_68HC12:
1710 rtype = elf_m68hc11_reloc_type (type);
1711 break;
75751cd9 1712
7b4ae824
JD
1713 case EM_S12Z:
1714 rtype = elf_s12z_reloc_type (type);
1715 break;
1716
252b5132 1717 case EM_68K:
9ea033b2 1718 rtype = elf_m68k_reloc_type (type);
252b5132
RH
1719 break;
1720
f954747f
AM
1721 case EM_960:
1722 rtype = elf_i960_reloc_type (type);
1723 break;
1724
adde6300 1725 case EM_AVR:
2b0337b0 1726 case EM_AVR_OLD:
adde6300
AM
1727 rtype = elf_avr_reloc_type (type);
1728 break;
1729
9ea033b2
NC
1730 case EM_OLD_SPARCV9:
1731 case EM_SPARC32PLUS:
1732 case EM_SPARCV9:
252b5132 1733 case EM_SPARC:
9ea033b2 1734 rtype = elf_sparc_reloc_type (type);
252b5132
RH
1735 break;
1736
e9f53129
AM
1737 case EM_SPU:
1738 rtype = elf_spu_reloc_type (type);
1739 break;
1740
708e2187
NC
1741 case EM_V800:
1742 rtype = v800_reloc_type (type);
1743 break;
2b0337b0 1744 case EM_V850:
252b5132 1745 case EM_CYGNUS_V850:
9ea033b2 1746 rtype = v850_reloc_type (type);
252b5132
RH
1747 break;
1748
2b0337b0 1749 case EM_D10V:
252b5132 1750 case EM_CYGNUS_D10V:
9ea033b2 1751 rtype = elf_d10v_reloc_type (type);
252b5132
RH
1752 break;
1753
2b0337b0 1754 case EM_D30V:
252b5132 1755 case EM_CYGNUS_D30V:
9ea033b2 1756 rtype = elf_d30v_reloc_type (type);
252b5132
RH
1757 break;
1758
d172d4ba
NC
1759 case EM_DLX:
1760 rtype = elf_dlx_reloc_type (type);
1761 break;
1762
252b5132 1763 case EM_SH:
9ea033b2 1764 rtype = elf_sh_reloc_type (type);
252b5132
RH
1765 break;
1766
2b0337b0 1767 case EM_MN10300:
252b5132 1768 case EM_CYGNUS_MN10300:
9ea033b2 1769 rtype = elf_mn10300_reloc_type (type);
252b5132
RH
1770 break;
1771
2b0337b0 1772 case EM_MN10200:
252b5132 1773 case EM_CYGNUS_MN10200:
9ea033b2 1774 rtype = elf_mn10200_reloc_type (type);
252b5132
RH
1775 break;
1776
2b0337b0 1777 case EM_FR30:
252b5132 1778 case EM_CYGNUS_FR30:
9ea033b2 1779 rtype = elf_fr30_reloc_type (type);
252b5132
RH
1780 break;
1781
ba2685cc
AM
1782 case EM_CYGNUS_FRV:
1783 rtype = elf_frv_reloc_type (type);
1784 break;
5c70f934 1785
b8891f8d
AJ
1786 case EM_CSKY:
1787 rtype = elf_csky_reloc_type (type);
1788 break;
1789
3f8107ab
AM
1790 case EM_FT32:
1791 rtype = elf_ft32_reloc_type (type);
1792 break;
1793
252b5132 1794 case EM_MCORE:
9ea033b2 1795 rtype = elf_mcore_reloc_type (type);
252b5132
RH
1796 break;
1797
3c3bdf30
NC
1798 case EM_MMIX:
1799 rtype = elf_mmix_reloc_type (type);
1800 break;
1801
5506d11a
AM
1802 case EM_MOXIE:
1803 rtype = elf_moxie_reloc_type (type);
1804 break;
1805
2469cfa2 1806 case EM_MSP430:
dda8d76d 1807 if (uses_msp430x_relocs (filedata))
13761a11
NC
1808 {
1809 rtype = elf_msp430x_reloc_type (type);
1810 break;
1811 }
1a0670f3 1812 /* Fall through. */
2469cfa2
NC
1813 case EM_MSP430_OLD:
1814 rtype = elf_msp430_reloc_type (type);
1815 break;
1816
35c08157
KLC
1817 case EM_NDS32:
1818 rtype = elf_nds32_reloc_type (type);
1819 break;
1820
252b5132 1821 case EM_PPC:
9ea033b2 1822 rtype = elf_ppc_reloc_type (type);
252b5132
RH
1823 break;
1824
c833c019
AM
1825 case EM_PPC64:
1826 rtype = elf_ppc64_reloc_type (type);
1827 break;
1828
252b5132 1829 case EM_MIPS:
4fe85591 1830 case EM_MIPS_RS3_LE:
9ea033b2 1831 rtype = elf_mips_reloc_type (type);
252b5132
RH
1832 break;
1833
e23eba97
NC
1834 case EM_RISCV:
1835 rtype = elf_riscv_reloc_type (type);
1836 break;
1837
252b5132 1838 case EM_ALPHA:
9ea033b2 1839 rtype = elf_alpha_reloc_type (type);
252b5132
RH
1840 break;
1841
1842 case EM_ARM:
9ea033b2 1843 rtype = elf_arm_reloc_type (type);
252b5132
RH
1844 break;
1845
584da044 1846 case EM_ARC:
886a2506
NC
1847 case EM_ARC_COMPACT:
1848 case EM_ARC_COMPACT2:
b5c37946
SJ
1849 case EM_ARC_COMPACT3:
1850 case EM_ARC_COMPACT3_64:
9ea033b2 1851 rtype = elf_arc_reloc_type (type);
252b5132
RH
1852 break;
1853
1854 case EM_PARISC:
69e617ca 1855 rtype = elf_hppa_reloc_type (type);
252b5132 1856 break;
7d466069 1857
b8720f9d
JL
1858 case EM_H8_300:
1859 case EM_H8_300H:
1860 case EM_H8S:
1861 rtype = elf_h8_reloc_type (type);
1862 break;
1863
73589c9d
CS
1864 case EM_OR1K:
1865 rtype = elf_or1k_reloc_type (type);
3b16e843
NC
1866 break;
1867
7d466069 1868 case EM_PJ:
2b0337b0 1869 case EM_PJ_OLD:
7d466069
ILT
1870 rtype = elf_pj_reloc_type (type);
1871 break;
800eeca4
JW
1872 case EM_IA_64:
1873 rtype = elf_ia64_reloc_type (type);
1874 break;
1b61cf92 1875
6e712424
PI
1876 case EM_KVX:
1877 rtype = elf_kvx_reloc_type (type);
1878 break;
1879
1b61cf92
HPN
1880 case EM_CRIS:
1881 rtype = elf_cris_reloc_type (type);
1882 break;
535c37ff 1883
f954747f
AM
1884 case EM_860:
1885 rtype = elf_i860_reloc_type (type);
1886 break;
1887
bcedfee6 1888 case EM_X86_64:
8a9036a4 1889 case EM_L1OM:
7a9068fe 1890 case EM_K1OM:
bcedfee6
NC
1891 rtype = elf_x86_64_reloc_type (type);
1892 break;
a85d7ed0 1893
f954747f
AM
1894 case EM_S370:
1895 rtype = i370_reloc_type (type);
1896 break;
1897
53c7db4b
KH
1898 case EM_S390_OLD:
1899 case EM_S390:
1900 rtype = elf_s390_reloc_type (type);
1901 break;
93fbbb04 1902
1c0d3aa6
NC
1903 case EM_SCORE:
1904 rtype = elf_score_reloc_type (type);
1905 break;
1906
93fbbb04
GK
1907 case EM_XSTORMY16:
1908 rtype = elf_xstormy16_reloc_type (type);
1909 break;
179d3252 1910
1fe1f39c
NC
1911 case EM_CRX:
1912 rtype = elf_crx_reloc_type (type);
1913 break;
1914
179d3252
JT
1915 case EM_VAX:
1916 rtype = elf_vax_reloc_type (type);
1917 break;
1e4cf259 1918
619ed720
EB
1919 case EM_VISIUM:
1920 rtype = elf_visium_reloc_type (type);
1921 break;
1922
aca4efc7
JM
1923 case EM_BPF:
1924 rtype = elf_bpf_reloc_type (type);
1925 break;
1926
cfb8c092
NC
1927 case EM_ADAPTEVA_EPIPHANY:
1928 rtype = elf_epiphany_reloc_type (type);
1929 break;
1930
1e4cf259
NC
1931 case EM_IP2K:
1932 case EM_IP2K_OLD:
1933 rtype = elf_ip2k_reloc_type (type);
1934 break;
3b36097d
SC
1935
1936 case EM_IQ2000:
1937 rtype = elf_iq2000_reloc_type (type);
1938 break;
88da6820
NC
1939
1940 case EM_XTENSA_OLD:
1941 case EM_XTENSA:
1942 rtype = elf_xtensa_reloc_type (type);
1943 break;
a34e3ecb 1944
84e94c90
NC
1945 case EM_LATTICEMICO32:
1946 rtype = elf_lm32_reloc_type (type);
1947 break;
1948
ff7eeb89 1949 case EM_M32C_OLD:
49f58d10
JB
1950 case EM_M32C:
1951 rtype = elf_m32c_reloc_type (type);
1952 break;
1953
d031aafb
NS
1954 case EM_MT:
1955 rtype = elf_mt_reloc_type (type);
a34e3ecb 1956 break;
1d65ded4
CM
1957
1958 case EM_BLACKFIN:
1959 rtype = elf_bfin_reloc_type (type);
1960 break;
15ab5209
DB
1961
1962 case EM_CYGNUS_MEP:
1963 rtype = elf_mep_reloc_type (type);
1964 break;
60bca95a
NC
1965
1966 case EM_CR16:
1967 rtype = elf_cr16_reloc_type (type);
1968 break;
dd24e3da 1969
7ba29e2a
NC
1970 case EM_MICROBLAZE:
1971 case EM_MICROBLAZE_OLD:
1972 rtype = elf_microblaze_reloc_type (type);
1973 break;
c7927a3c 1974
99c513f6
DD
1975 case EM_RL78:
1976 rtype = elf_rl78_reloc_type (type);
1977 break;
1978
c7927a3c
NC
1979 case EM_RX:
1980 rtype = elf_rx_reloc_type (type);
1981 break;
c29aca4a 1982
a3c62988
NC
1983 case EM_METAG:
1984 rtype = elf_metag_reloc_type (type);
1985 break;
1986
40b36596
JM
1987 case EM_TI_C6000:
1988 rtype = elf_tic6x_reloc_type (type);
1989 break;
aa137e4d
NC
1990
1991 case EM_TILEGX:
1992 rtype = elf_tilegx_reloc_type (type);
1993 break;
1994
1995 case EM_TILEPRO:
1996 rtype = elf_tilepro_reloc_type (type);
1997 break;
f6c1a2d5 1998
f96bd6c2
PC
1999 case EM_WEBASSEMBLY:
2000 rtype = elf_wasm32_reloc_type (type);
2001 break;
2002
f6c1a2d5
NC
2003 case EM_XGATE:
2004 rtype = elf_xgate_reloc_type (type);
2005 break;
36591ba1
SL
2006
2007 case EM_ALTERA_NIOS2:
2008 rtype = elf_nios2_reloc_type (type);
2009 break;
2b100bb5
DD
2010
2011 case EM_TI_PRU:
2012 rtype = elf_pru_reloc_type (type);
2013 break;
fe944acf
FT
2014
2015 case EM_NFP:
2016 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
2017 rtype = elf_nfp3200_reloc_type (type);
2018 else
2019 rtype = elf_nfp_reloc_type (type);
2020 break;
6655dba2
SB
2021
2022 case EM_Z80:
2023 rtype = elf_z80_reloc_type (type);
2024 break;
e9a0721f 2025
2026 case EM_LOONGARCH:
2027 rtype = elf_loongarch_reloc_type (type);
2028 break;
2029
0c857ef4
SM
2030 case EM_AMDGPU:
2031 rtype = elf_amdgpu_reloc_type (type);
2032 break;
252b5132
RH
2033 }
2034
2035 if (rtype == NULL)
39dbeff8 2036 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
252b5132 2037 else
5c144731 2038 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
252b5132 2039
dda8d76d 2040 if (filedata->file_header.e_machine == EM_ALPHA
157c2599 2041 && rtype != NULL
7ace3541 2042 && streq (rtype, "R_ALPHA_LITUSE")
a7fd1186 2043 && rel_type == reltype_rela)
7ace3541
RH
2044 {
2045 switch (rels[i].r_addend)
2046 {
2047 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
2048 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
2049 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
2050 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
2051 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
2052 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
2053 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
2054 default: rtype = NULL;
2055 }
32ec8896 2056
7ace3541
RH
2057 if (rtype)
2058 printf (" (%s)", rtype);
2059 else
2060 {
2061 putchar (' ');
26c527e6
AM
2062 printf (_("<unknown addend: %" PRIx64 ">"),
2063 rels[i].r_addend);
015dc7e1 2064 res = false;
7ace3541
RH
2065 }
2066 }
2067 else if (symtab_index)
252b5132 2068 {
af3fc3bc 2069 if (symtab == NULL || symtab_index >= nsyms)
32ec8896 2070 {
27a45f42
AS
2071 error (_(" bad symbol index: %08lx in reloc\n"),
2072 (unsigned long) symtab_index);
015dc7e1 2073 res = false;
32ec8896 2074 }
af3fc3bc 2075 else
19936277 2076 {
2cf0635d 2077 Elf_Internal_Sym * psym;
bb4d2ac2
L
2078 const char * version_string;
2079 enum versioned_symbol_info sym_info;
2080 unsigned short vna_other;
19936277 2081
af3fc3bc 2082 psym = symtab + symtab_index;
103f02d3 2083
bb4d2ac2 2084 version_string
dda8d76d 2085 = get_symbol_version_string (filedata, is_dynsym,
bb4d2ac2
L
2086 strtab, strtablen,
2087 symtab_index,
2088 psym,
2089 &sym_info,
2090 &vna_other);
2091
af3fc3bc 2092 printf (" ");
171191ba 2093
d8045f23
NC
2094 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
2095 {
2096 const char * name;
2097 unsigned int len;
2098 unsigned int width = is_32bit_elf ? 8 : 14;
2099
2100 /* Relocations against GNU_IFUNC symbols do not use the value
2101 of the symbol as the address to relocate against. Instead
2102 they invoke the function named by the symbol and use its
2103 result as the address for relocation.
2104
2105 To indicate this to the user, do not display the value of
2106 the symbol in the "Symbols's Value" field. Instead show
2107 its name followed by () as a hint that the symbol is
2108 invoked. */
2109
2110 if (strtab == NULL
2111 || psym->st_name == 0
2112 || psym->st_name >= strtablen)
2113 name = "??";
2114 else
2115 name = strtab + psym->st_name;
2116
b6ac461a 2117 len = print_symbol_name (width, name);
bb4d2ac2
L
2118 if (version_string)
2119 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2120 version_string);
d8045f23
NC
2121 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
2122 }
2123 else
2124 {
2125 print_vma (psym->st_value, LONG_HEX);
171191ba 2126
d8045f23
NC
2127 printf (is_32bit_elf ? " " : " ");
2128 }
103f02d3 2129
af3fc3bc 2130 if (psym->st_name == 0)
f1ef08cb 2131 {
2cf0635d 2132 const char * sec_name = "<null>";
f1ef08cb
AM
2133
2134 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
b6ac461a
NC
2135 sec_name = printable_section_name_from_index
2136 (filedata, psym->st_shndx, NULL);
2137
2138 print_symbol_name (22, sec_name);
f1ef08cb 2139 }
af3fc3bc 2140 else if (strtab == NULL)
d79b3d50 2141 printf (_("<string table index: %3ld>"), psym->st_name);
c256ffe7 2142 else if (psym->st_name >= strtablen)
32ec8896 2143 {
27a45f42
AS
2144 error (_("<corrupt string table index: %3ld>\n"),
2145 psym->st_name);
015dc7e1 2146 res = false;
32ec8896 2147 }
af3fc3bc 2148 else
bb4d2ac2 2149 {
b6ac461a 2150 print_symbol_name (22, strtab + psym->st_name);
bb4d2ac2
L
2151 if (version_string)
2152 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2153 version_string);
2154 }
103f02d3 2155
a7fd1186 2156 if (rel_type == reltype_rela)
171191ba 2157 {
625d49fc 2158 uint64_t off = rels[i].r_addend;
171191ba 2159
625d49fc
AM
2160 if ((int64_t) off < 0)
2161 printf (" - %" PRIx64, -off);
171191ba 2162 else
625d49fc 2163 printf (" + %" PRIx64, off);
171191ba 2164 }
19936277 2165 }
252b5132 2166 }
a7fd1186 2167 else if (rel_type == reltype_rela)
f7a99963 2168 {
625d49fc 2169 uint64_t off = rels[i].r_addend;
e04d7088
L
2170
2171 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
625d49fc
AM
2172 if ((int64_t) off < 0)
2173 printf ("-%" PRIx64, -off);
e04d7088 2174 else
625d49fc 2175 printf ("%" PRIx64, off);
f7a99963 2176 }
252b5132 2177
dda8d76d 2178 if (filedata->file_header.e_machine == EM_SPARCV9
157c2599
NC
2179 && rtype != NULL
2180 && streq (rtype, "R_SPARC_OLO10"))
26c527e6 2181 printf (" + %" PRIx64, ELF64_R_TYPE_DATA (inf));
351b4b40 2182
252b5132 2183 putchar ('\n');
2c71103e 2184
dda8d76d 2185 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
2c71103e 2186 {
625d49fc
AM
2187 uint64_t type2 = ELF64_MIPS_R_TYPE2 (inf);
2188 uint64_t type3 = ELF64_MIPS_R_TYPE3 (inf);
2cf0635d
NC
2189 const char * rtype2 = elf_mips_reloc_type (type2);
2190 const char * rtype3 = elf_mips_reloc_type (type3);
aca88567 2191
2c71103e
NC
2192 printf (" Type2: ");
2193
2194 if (rtype2 == NULL)
39dbeff8
AM
2195 printf (_("unrecognized: %-7lx"),
2196 (unsigned long) type2 & 0xffffffff);
2c71103e
NC
2197 else
2198 printf ("%-17.17s", rtype2);
2199
18bd398b 2200 printf ("\n Type3: ");
2c71103e
NC
2201
2202 if (rtype3 == NULL)
39dbeff8
AM
2203 printf (_("unrecognized: %-7lx"),
2204 (unsigned long) type3 & 0xffffffff);
2c71103e
NC
2205 else
2206 printf ("%-17.17s", rtype3);
2207
53c7db4b 2208 putchar ('\n');
2c71103e 2209 }
252b5132
RH
2210 }
2211
c8286bd1 2212 free (rels);
32ec8896
NC
2213
2214 return res;
252b5132
RH
2215}
2216
37c18eed
SD
2217static const char *
2218get_aarch64_dynamic_type (unsigned long type)
2219{
2220 switch (type)
2221 {
2222 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
1dbade74 2223 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
2301ed1c 2224 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
37c18eed
SD
2225 default:
2226 return NULL;
2227 }
2228}
2229
252b5132 2230static const char *
d3ba0551 2231get_mips_dynamic_type (unsigned long type)
252b5132
RH
2232{
2233 switch (type)
2234 {
2235 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
2236 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
2237 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
2238 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
2239 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
2240 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
2241 case DT_MIPS_MSYM: return "MIPS_MSYM";
2242 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2243 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2244 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
2245 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
2246 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
2247 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
2248 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
2249 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
2250 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
2251 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
a5499fa4 2252 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
252b5132
RH
2253 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
2254 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
2255 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
2256 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
2257 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
2258 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
2259 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
2260 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
2261 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
2262 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
2263 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
2264 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
2265 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2266 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
2267 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
2268 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
2269 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
2270 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2271 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
2272 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
2273 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
2274 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
2275 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
2276 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
2277 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
2278 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
861fb55a
DJ
2279 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
2280 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
f16a9783 2281 case DT_MIPS_XHASH: return "MIPS_XHASH";
252b5132
RH
2282 default:
2283 return NULL;
2284 }
2285}
2286
9a097730 2287static const char *
d3ba0551 2288get_sparc64_dynamic_type (unsigned long type)
9a097730
RH
2289{
2290 switch (type)
2291 {
2292 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
2293 default:
2294 return NULL;
2295 }
103f02d3
UD
2296}
2297
7490d522
AM
2298static const char *
2299get_ppc_dynamic_type (unsigned long type)
2300{
2301 switch (type)
2302 {
a7f2871e 2303 case DT_PPC_GOT: return "PPC_GOT";
e8910a83 2304 case DT_PPC_OPT: return "PPC_OPT";
7490d522
AM
2305 default:
2306 return NULL;
2307 }
2308}
2309
f1cb7e17 2310static const char *
d3ba0551 2311get_ppc64_dynamic_type (unsigned long type)
f1cb7e17
AM
2312{
2313 switch (type)
2314 {
a7f2871e
AM
2315 case DT_PPC64_GLINK: return "PPC64_GLINK";
2316 case DT_PPC64_OPD: return "PPC64_OPD";
2317 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
e8910a83 2318 case DT_PPC64_OPT: return "PPC64_OPT";
f1cb7e17
AM
2319 default:
2320 return NULL;
2321 }
2322}
2323
103f02d3 2324static const char *
d3ba0551 2325get_parisc_dynamic_type (unsigned long type)
103f02d3
UD
2326{
2327 switch (type)
2328 {
2329 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
2330 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
2331 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
2332 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
2333 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
2334 case DT_HP_PREINIT: return "HP_PREINIT";
2335 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
2336 case DT_HP_NEEDED: return "HP_NEEDED";
2337 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
2338 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
2339 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
2340 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
2341 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
eec8f817
DA
2342 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
2343 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
2344 case DT_HP_FILTERED: return "HP_FILTERED";
2345 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
2346 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
2347 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
2348 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
2349 case DT_PLT: return "PLT";
2350 case DT_PLT_SIZE: return "PLT_SIZE";
2351 case DT_DLT: return "DLT";
2352 case DT_DLT_SIZE: return "DLT_SIZE";
103f02d3
UD
2353 default:
2354 return NULL;
2355 }
2356}
9a097730 2357
ecc51f48 2358static const char *
d3ba0551 2359get_ia64_dynamic_type (unsigned long type)
ecc51f48
NC
2360{
2361 switch (type)
2362 {
148b93f2
NC
2363 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2364 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2365 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2366 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2367 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2368 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2369 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2370 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2371 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2372 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2373 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2374 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2375 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2376 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2377 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2378 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2379 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2380 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2381 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2382 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2383 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2384 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2385 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2386 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2387 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2388 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2389 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2390 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2391 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2392 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2393 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
ecc51f48
NC
2394 default:
2395 return NULL;
2396 }
2397}
2398
fd85a6a1
NC
2399static const char *
2400get_solaris_section_type (unsigned long type)
2401{
2402 switch (type)
2403 {
2404 case 0x6fffffee: return "SUNW_ancillary";
2405 case 0x6fffffef: return "SUNW_capchain";
2406 case 0x6ffffff0: return "SUNW_capinfo";
2407 case 0x6ffffff1: return "SUNW_symsort";
2408 case 0x6ffffff2: return "SUNW_tlssort";
2409 case 0x6ffffff3: return "SUNW_LDYNSYM";
2410 case 0x6ffffff4: return "SUNW_dof";
2411 case 0x6ffffff5: return "SUNW_cap";
2412 case 0x6ffffff6: return "SUNW_SIGNATURE";
2413 case 0x6ffffff7: return "SUNW_ANNOTATE";
2414 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2415 case 0x6ffffff9: return "SUNW_DEBUG";
2416 case 0x6ffffffa: return "SUNW_move";
2417 case 0x6ffffffb: return "SUNW_COMDAT";
2418 case 0x6ffffffc: return "SUNW_syminfo";
2419 case 0x6ffffffd: return "SUNW_verdef";
2420 case 0x6ffffffe: return "SUNW_verneed";
2421 case 0x6fffffff: return "SUNW_versym";
2422 case 0x70000000: return "SPARC_GOTDATA";
2423 default: return NULL;
2424 }
2425}
2426
fabcb361
RH
2427static const char *
2428get_alpha_dynamic_type (unsigned long type)
2429{
2430 switch (type)
2431 {
2432 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
32ec8896 2433 default: return NULL;
fabcb361
RH
2434 }
2435}
2436
1c0d3aa6
NC
2437static const char *
2438get_score_dynamic_type (unsigned long type)
2439{
2440 switch (type)
2441 {
2442 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2443 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2444 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2445 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2446 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2447 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
32ec8896 2448 default: return NULL;
1c0d3aa6
NC
2449 }
2450}
2451
40b36596
JM
2452static const char *
2453get_tic6x_dynamic_type (unsigned long type)
2454{
2455 switch (type)
2456 {
2457 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2458 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2459 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2460 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2461 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2462 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
32ec8896 2463 default: return NULL;
40b36596
JM
2464 }
2465}
1c0d3aa6 2466
36591ba1
SL
2467static const char *
2468get_nios2_dynamic_type (unsigned long type)
2469{
2470 switch (type)
2471 {
2472 case DT_NIOS2_GP: return "NIOS2_GP";
32ec8896 2473 default: return NULL;
36591ba1
SL
2474 }
2475}
2476
fd85a6a1
NC
2477static const char *
2478get_solaris_dynamic_type (unsigned long type)
2479{
2480 switch (type)
2481 {
2482 case 0x6000000d: return "SUNW_AUXILIARY";
2483 case 0x6000000e: return "SUNW_RTLDINF";
2484 case 0x6000000f: return "SUNW_FILTER";
2485 case 0x60000010: return "SUNW_CAP";
2486 case 0x60000011: return "SUNW_SYMTAB";
2487 case 0x60000012: return "SUNW_SYMSZ";
2488 case 0x60000013: return "SUNW_SORTENT";
2489 case 0x60000014: return "SUNW_SYMSORT";
2490 case 0x60000015: return "SUNW_SYMSORTSZ";
2491 case 0x60000016: return "SUNW_TLSSORT";
2492 case 0x60000017: return "SUNW_TLSSORTSZ";
2493 case 0x60000018: return "SUNW_CAPINFO";
2494 case 0x60000019: return "SUNW_STRPAD";
2495 case 0x6000001a: return "SUNW_CAPCHAIN";
2496 case 0x6000001b: return "SUNW_LDMACH";
2497 case 0x6000001d: return "SUNW_CAPCHAINENT";
2498 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2499 case 0x60000021: return "SUNW_PARENT";
2500 case 0x60000023: return "SUNW_ASLR";
2501 case 0x60000025: return "SUNW_RELAX";
2502 case 0x60000029: return "SUNW_NXHEAP";
2503 case 0x6000002b: return "SUNW_NXSTACK";
2504
2505 case 0x70000001: return "SPARC_REGISTER";
2506 case 0x7ffffffd: return "AUXILIARY";
2507 case 0x7ffffffe: return "USED";
2508 case 0x7fffffff: return "FILTER";
2509
15f205b1 2510 default: return NULL;
fd85a6a1
NC
2511 }
2512}
2513
8155b853
NC
2514static const char *
2515get_riscv_dynamic_type (unsigned long type)
2516{
2517 switch (type)
2518 {
2519 case DT_RISCV_VARIANT_CC: return "RISCV_VARIANT_CC";
2520 default:
2521 return NULL;
2522 }
2523}
2524
252b5132 2525static const char *
dda8d76d 2526get_dynamic_type (Filedata * filedata, unsigned long type)
252b5132 2527{
e9e44622 2528 static char buff[64];
252b5132
RH
2529
2530 switch (type)
2531 {
2532 case DT_NULL: return "NULL";
2533 case DT_NEEDED: return "NEEDED";
2534 case DT_PLTRELSZ: return "PLTRELSZ";
2535 case DT_PLTGOT: return "PLTGOT";
2536 case DT_HASH: return "HASH";
2537 case DT_STRTAB: return "STRTAB";
2538 case DT_SYMTAB: return "SYMTAB";
2539 case DT_RELA: return "RELA";
2540 case DT_RELASZ: return "RELASZ";
2541 case DT_RELAENT: return "RELAENT";
2542 case DT_STRSZ: return "STRSZ";
2543 case DT_SYMENT: return "SYMENT";
2544 case DT_INIT: return "INIT";
2545 case DT_FINI: return "FINI";
2546 case DT_SONAME: return "SONAME";
2547 case DT_RPATH: return "RPATH";
2548 case DT_SYMBOLIC: return "SYMBOLIC";
2549 case DT_REL: return "REL";
2550 case DT_RELSZ: return "RELSZ";
2551 case DT_RELENT: return "RELENT";
dd207c13
FS
2552 case DT_RELR: return "RELR";
2553 case DT_RELRSZ: return "RELRSZ";
2554 case DT_RELRENT: return "RELRENT";
252b5132
RH
2555 case DT_PLTREL: return "PLTREL";
2556 case DT_DEBUG: return "DEBUG";
2557 case DT_TEXTREL: return "TEXTREL";
2558 case DT_JMPREL: return "JMPREL";
2559 case DT_BIND_NOW: return "BIND_NOW";
2560 case DT_INIT_ARRAY: return "INIT_ARRAY";
2561 case DT_FINI_ARRAY: return "FINI_ARRAY";
2562 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2563 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
d1133906
NC
2564 case DT_RUNPATH: return "RUNPATH";
2565 case DT_FLAGS: return "FLAGS";
2d0e6f43 2566
d1133906
NC
2567 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2568 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
6d913794 2569 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
103f02d3 2570
05107a46 2571 case DT_CHECKSUM: return "CHECKSUM";
252b5132
RH
2572 case DT_PLTPADSZ: return "PLTPADSZ";
2573 case DT_MOVEENT: return "MOVEENT";
2574 case DT_MOVESZ: return "MOVESZ";
dcefbbbd 2575 case DT_FEATURE: return "FEATURE";
252b5132
RH
2576 case DT_POSFLAG_1: return "POSFLAG_1";
2577 case DT_SYMINSZ: return "SYMINSZ";
2578 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
103f02d3 2579
252b5132 2580 case DT_ADDRRNGLO: return "ADDRRNGLO";
dcefbbbd
L
2581 case DT_CONFIG: return "CONFIG";
2582 case DT_DEPAUDIT: return "DEPAUDIT";
2583 case DT_AUDIT: return "AUDIT";
2584 case DT_PLTPAD: return "PLTPAD";
2585 case DT_MOVETAB: return "MOVETAB";
252b5132 2586 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
103f02d3 2587
252b5132 2588 case DT_VERSYM: return "VERSYM";
103f02d3 2589
67a4f2b7
AO
2590 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2591 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
252b5132
RH
2592 case DT_RELACOUNT: return "RELACOUNT";
2593 case DT_RELCOUNT: return "RELCOUNT";
2594 case DT_FLAGS_1: return "FLAGS_1";
2595 case DT_VERDEF: return "VERDEF";
2596 case DT_VERDEFNUM: return "VERDEFNUM";
2597 case DT_VERNEED: return "VERNEED";
2598 case DT_VERNEEDNUM: return "VERNEEDNUM";
103f02d3 2599
019148e4 2600 case DT_AUXILIARY: return "AUXILIARY";
252b5132
RH
2601 case DT_USED: return "USED";
2602 case DT_FILTER: return "FILTER";
103f02d3 2603
047b2264
JJ
2604 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2605 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2606 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2607 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2608 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
fdc90cb4 2609 case DT_GNU_HASH: return "GNU_HASH";
a5da3dee 2610 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
047b2264 2611
252b5132
RH
2612 default:
2613 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2614 {
2cf0635d 2615 const char * result;
103f02d3 2616
dda8d76d 2617 switch (filedata->file_header.e_machine)
252b5132 2618 {
37c18eed
SD
2619 case EM_AARCH64:
2620 result = get_aarch64_dynamic_type (type);
2621 break;
252b5132 2622 case EM_MIPS:
4fe85591 2623 case EM_MIPS_RS3_LE:
252b5132
RH
2624 result = get_mips_dynamic_type (type);
2625 break;
9a097730
RH
2626 case EM_SPARCV9:
2627 result = get_sparc64_dynamic_type (type);
2628 break;
7490d522
AM
2629 case EM_PPC:
2630 result = get_ppc_dynamic_type (type);
2631 break;
f1cb7e17
AM
2632 case EM_PPC64:
2633 result = get_ppc64_dynamic_type (type);
2634 break;
ecc51f48
NC
2635 case EM_IA_64:
2636 result = get_ia64_dynamic_type (type);
2637 break;
fabcb361
RH
2638 case EM_ALPHA:
2639 result = get_alpha_dynamic_type (type);
2640 break;
1c0d3aa6
NC
2641 case EM_SCORE:
2642 result = get_score_dynamic_type (type);
2643 break;
40b36596
JM
2644 case EM_TI_C6000:
2645 result = get_tic6x_dynamic_type (type);
2646 break;
36591ba1
SL
2647 case EM_ALTERA_NIOS2:
2648 result = get_nios2_dynamic_type (type);
2649 break;
8155b853
NC
2650 case EM_RISCV:
2651 result = get_riscv_dynamic_type (type);
2652 break;
252b5132 2653 default:
dda8d76d 2654 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
fd85a6a1
NC
2655 result = get_solaris_dynamic_type (type);
2656 else
2657 result = NULL;
252b5132
RH
2658 break;
2659 }
2660
2661 if (result != NULL)
2662 return result;
2663
e9e44622 2664 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
252b5132 2665 }
eec8f817 2666 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
dda8d76d 2667 || (filedata->file_header.e_machine == EM_PARISC
eec8f817 2668 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
103f02d3 2669 {
2cf0635d 2670 const char * result;
103f02d3 2671
dda8d76d 2672 switch (filedata->file_header.e_machine)
103f02d3
UD
2673 {
2674 case EM_PARISC:
2675 result = get_parisc_dynamic_type (type);
2676 break;
148b93f2
NC
2677 case EM_IA_64:
2678 result = get_ia64_dynamic_type (type);
2679 break;
103f02d3 2680 default:
dda8d76d 2681 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
fd85a6a1
NC
2682 result = get_solaris_dynamic_type (type);
2683 else
2684 result = NULL;
103f02d3
UD
2685 break;
2686 }
2687
2688 if (result != NULL)
2689 return result;
2690
e9e44622
JJ
2691 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2692 type);
103f02d3 2693 }
252b5132 2694 else
e9e44622 2695 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
103f02d3 2696
252b5132
RH
2697 return buff;
2698 }
2699}
2700
93df3340
AM
2701static bool get_program_headers (Filedata *);
2702static bool get_dynamic_section (Filedata *);
2703
2704static void
2705locate_dynamic_section (Filedata *filedata)
2706{
26c527e6 2707 uint64_t dynamic_addr = 0;
be7d229a 2708 uint64_t dynamic_size = 0;
93df3340
AM
2709
2710 if (filedata->file_header.e_phnum != 0
2711 && get_program_headers (filedata))
2712 {
2713 Elf_Internal_Phdr *segment;
2714 unsigned int i;
2715
2716 for (i = 0, segment = filedata->program_headers;
2717 i < filedata->file_header.e_phnum;
2718 i++, segment++)
2719 {
2720 if (segment->p_type == PT_DYNAMIC)
2721 {
2722 dynamic_addr = segment->p_offset;
2723 dynamic_size = segment->p_filesz;
2724
2725 if (filedata->section_headers != NULL)
2726 {
2727 Elf_Internal_Shdr *sec;
2728
2729 sec = find_section (filedata, ".dynamic");
2730 if (sec != NULL)
2731 {
2732 if (sec->sh_size == 0
2733 || sec->sh_type == SHT_NOBITS)
2734 {
2735 dynamic_addr = 0;
2736 dynamic_size = 0;
2737 }
2738 else
2739 {
2740 dynamic_addr = sec->sh_offset;
2741 dynamic_size = sec->sh_size;
2742 }
2743 }
2744 }
2745
2746 if (dynamic_addr > filedata->file_size
2747 || (dynamic_size > filedata->file_size - dynamic_addr))
2748 {
2749 dynamic_addr = 0;
2750 dynamic_size = 0;
2751 }
2752 break;
2753 }
2754 }
2755 }
2756 filedata->dynamic_addr = dynamic_addr;
2757 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
2758}
2759
2760static bool
2761is_pie (Filedata *filedata)
2762{
2763 Elf_Internal_Dyn *entry;
2764
2765 if (filedata->dynamic_size == 0)
2766 locate_dynamic_section (filedata);
2767 if (filedata->dynamic_size <= 1)
2768 return false;
2769
2770 if (!get_dynamic_section (filedata))
2771 return false;
2772
2773 for (entry = filedata->dynamic_section;
2774 entry < filedata->dynamic_section + filedata->dynamic_nent;
2775 entry++)
2776 {
2777 if (entry->d_tag == DT_FLAGS_1)
2778 {
2779 if ((entry->d_un.d_val & DF_1_PIE) != 0)
2780 return true;
2781 break;
2782 }
2783 }
2784 return false;
2785}
2786
252b5132 2787static char *
93df3340 2788get_file_type (Filedata *filedata)
252b5132 2789{
93df3340 2790 unsigned e_type = filedata->file_header.e_type;
89246a0e 2791 static char buff[64];
252b5132
RH
2792
2793 switch (e_type)
2794 {
32ec8896
NC
2795 case ET_NONE: return _("NONE (None)");
2796 case ET_REL: return _("REL (Relocatable file)");
2797 case ET_EXEC: return _("EXEC (Executable file)");
93df3340
AM
2798 case ET_DYN:
2799 if (is_pie (filedata))
2800 return _("DYN (Position-Independent Executable file)");
2801 else
2802 return _("DYN (Shared object file)");
32ec8896 2803 case ET_CORE: return _("CORE (Core file)");
252b5132
RH
2804
2805 default:
2806 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
e9e44622 2807 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
252b5132 2808 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
e9e44622 2809 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
252b5132 2810 else
e9e44622 2811 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
252b5132
RH
2812 return buff;
2813 }
2814}
2815
2816static char *
d3ba0551 2817get_machine_name (unsigned e_machine)
252b5132 2818{
b34976b6 2819 static char buff[64]; /* XXX */
252b5132
RH
2820
2821 switch (e_machine)
2822 {
55e22ca8
NC
2823 /* Please keep this switch table sorted by increasing EM_ value. */
2824 /* 0 */
c45021f2
NC
2825 case EM_NONE: return _("None");
2826 case EM_M32: return "WE32100";
2827 case EM_SPARC: return "Sparc";
2828 case EM_386: return "Intel 80386";
2829 case EM_68K: return "MC68000";
2830 case EM_88K: return "MC88000";
22abe556 2831 case EM_IAMCU: return "Intel MCU";
fb70ec17 2832 case EM_860: return "Intel 80860";
c45021f2
NC
2833 case EM_MIPS: return "MIPS R3000";
2834 case EM_S370: return "IBM System/370";
55e22ca8 2835 /* 10 */
7036c0e1 2836 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
252b5132 2837 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
c45021f2 2838 case EM_PARISC: return "HPPA";
55e22ca8 2839 case EM_VPP550: return "Fujitsu VPP500";
7036c0e1 2840 case EM_SPARC32PLUS: return "Sparc v8+" ;
d7867d17 2841 case EM_960: return "Intel 80960";
c45021f2 2842 case EM_PPC: return "PowerPC";
55e22ca8 2843 /* 20 */
285d1771 2844 case EM_PPC64: return "PowerPC64";
55e22ca8
NC
2845 case EM_S390_OLD:
2846 case EM_S390: return "IBM S/390";
2847 case EM_SPU: return "SPU";
2848 /* 30 */
2849 case EM_V800: return "Renesas V850 (using RH850 ABI)";
c45021f2
NC
2850 case EM_FR20: return "Fujitsu FR20";
2851 case EM_RH32: return "TRW RH32";
b34976b6 2852 case EM_MCORE: return "MCORE";
55e22ca8 2853 /* 40 */
7036c0e1
AJ
2854 case EM_ARM: return "ARM";
2855 case EM_OLD_ALPHA: return "Digital Alpha (old)";
ef230218 2856 case EM_SH: return "Renesas / SuperH SH";
c45021f2
NC
2857 case EM_SPARCV9: return "Sparc v9";
2858 case EM_TRICORE: return "Siemens Tricore";
584da044 2859 case EM_ARC: return "ARC";
c2dcd04e
NC
2860 case EM_H8_300: return "Renesas H8/300";
2861 case EM_H8_300H: return "Renesas H8/300H";
2862 case EM_H8S: return "Renesas H8S";
2863 case EM_H8_500: return "Renesas H8/500";
55e22ca8 2864 /* 50 */
30800947 2865 case EM_IA_64: return "Intel IA-64";
252b5132
RH
2866 case EM_MIPS_X: return "Stanford MIPS-X";
2867 case EM_COLDFIRE: return "Motorola Coldfire";
55e22ca8 2868 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
7036c0e1
AJ
2869 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2870 case EM_PCP: return "Siemens PCP";
2871 case EM_NCPU: return "Sony nCPU embedded RISC processor";
90de8f9c 2872 case EM_NDR1: return "Denso NDR1 microprocessor";
7036c0e1
AJ
2873 case EM_STARCORE: return "Motorola Star*Core processor";
2874 case EM_ME16: return "Toyota ME16 processor";
55e22ca8 2875 /* 60 */
7036c0e1
AJ
2876 case EM_ST100: return "STMicroelectronics ST100 processor";
2877 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
55e22ca8 2878 case EM_X86_64: return "Advanced Micro Devices X86-64";
11636f9e
JM
2879 case EM_PDSP: return "Sony DSP processor";
2880 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2881 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
7036c0e1
AJ
2882 case EM_FX66: return "Siemens FX66 microcontroller";
2883 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2884 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2885 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
55e22ca8 2886 /* 70 */
7036c0e1
AJ
2887 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2888 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2889 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2890 case EM_SVX: return "Silicon Graphics SVx";
2891 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2892 case EM_VAX: return "Digital VAX";
1b61cf92 2893 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
c45021f2
NC
2894 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2895 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2896 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
55e22ca8 2897 /* 80 */
b34976b6 2898 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
c45021f2 2899 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
3b36097d 2900 case EM_PRISM: return "Vitesse Prism";
55e22ca8
NC
2901 case EM_AVR_OLD:
2902 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2903 case EM_CYGNUS_FR30:
2904 case EM_FR30: return "Fujitsu FR30";
2905 case EM_CYGNUS_D10V:
2906 case EM_D10V: return "d10v";
2907 case EM_CYGNUS_D30V:
2908 case EM_D30V: return "d30v";
2909 case EM_CYGNUS_V850:
2910 case EM_V850: return "Renesas V850";
2911 case EM_CYGNUS_M32R:
2912 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2913 case EM_CYGNUS_MN10300:
2914 case EM_MN10300: return "mn10300";
2915 /* 90 */
2916 case EM_CYGNUS_MN10200:
2917 case EM_MN10200: return "mn10200";
2918 case EM_PJ: return "picoJava";
73589c9d 2919 case EM_OR1K: return "OpenRISC 1000";
55e22ca8 2920 case EM_ARC_COMPACT: return "ARCompact";
88da6820
NC
2921 case EM_XTENSA_OLD:
2922 case EM_XTENSA: return "Tensilica Xtensa Processor";
11636f9e
JM
2923 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2924 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2925 case EM_NS32K: return "National Semiconductor 32000 series";
2926 case EM_TPC: return "Tenor Network TPC processor";
55e22ca8
NC
2927 case EM_SNP1K: return "Trebia SNP 1000 processor";
2928 /* 100 */
9abca702 2929 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
55e22ca8
NC
2930 case EM_IP2K_OLD:
2931 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
11636f9e
JM
2932 case EM_MAX: return "MAX Processor";
2933 case EM_CR: return "National Semiconductor CompactRISC";
2934 case EM_F2MC16: return "Fujitsu F2MC16";
2935 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
7bbe5bc5 2936 case EM_BLACKFIN: return "Analog Devices Blackfin";
11636f9e
JM
2937 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2938 case EM_SEP: return "Sharp embedded microprocessor";
2939 case EM_ARCA: return "Arca RISC microprocessor";
55e22ca8 2940 /* 110 */
11636f9e
JM
2941 case EM_UNICORE: return "Unicore";
2942 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2943 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
64fd6348 2944 case EM_ALTERA_NIOS2: return "Altera Nios II";
55e22ca8
NC
2945 case EM_CRX: return "National Semiconductor CRX microprocessor";
2946 case EM_XGATE: return "Motorola XGATE embedded processor";
c29aca4a 2947 case EM_C166:
d70c5fc7 2948 case EM_XC16X: return "Infineon Technologies xc16x";
11636f9e
JM
2949 case EM_M16C: return "Renesas M16C series microprocessors";
2950 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2951 case EM_CE: return "Freescale Communication Engine RISC core";
55e22ca8
NC
2952 /* 120 */
2953 case EM_M32C: return "Renesas M32c";
2954 /* 130 */
11636f9e
JM
2955 case EM_TSK3000: return "Altium TSK3000 core";
2956 case EM_RS08: return "Freescale RS08 embedded processor";
2957 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
55e22ca8 2958 case EM_SCORE: return "SUNPLUS S+Core";
11636f9e
JM
2959 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2960 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
55e22ca8 2961 case EM_LATTICEMICO32: return "Lattice Mico32";
11636f9e 2962 case EM_SE_C17: return "Seiko Epson C17 family";
55e22ca8 2963 /* 140 */
11636f9e
JM
2964 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2965 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2966 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
55e22ca8
NC
2967 case EM_TI_PRU: return "TI PRU I/O processor";
2968 /* 160 */
11636f9e
JM
2969 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2970 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2971 case EM_R32C: return "Renesas R32C series microprocessors";
2972 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2973 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2974 case EM_8051: return "Intel 8051 and variants";
2975 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2976 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2977 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2978 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
55e22ca8 2979 /* 170 */
11636f9e
JM
2980 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2981 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2982 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
c7927a3c 2983 case EM_RX: return "Renesas RX";
a3c62988 2984 case EM_METAG: return "Imagination Technologies Meta processor architecture";
11636f9e
JM
2985 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2986 case EM_ECOG16: return "Cyan Technology eCOG16 family";
55e22ca8
NC
2987 case EM_CR16:
2988 case EM_MICROBLAZE:
2989 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
11636f9e
JM
2990 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2991 case EM_SLE9X: return "Infineon Technologies SLE9X core";
55e22ca8
NC
2992 /* 180 */
2993 case EM_L1OM: return "Intel L1OM";
2994 case EM_K1OM: return "Intel K1OM";
2995 case EM_INTEL182: return "Intel (reserved)";
2996 case EM_AARCH64: return "AArch64";
2997 case EM_ARM184: return "ARM (reserved)";
2998 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
11636f9e
JM
2999 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
3000 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
3001 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
55e22ca8 3002 /* 190 */
11636f9e 3003 case EM_CUDA: return "NVIDIA CUDA architecture";
55e22ca8 3004 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
6d913794
NC
3005 case EM_CLOUDSHIELD: return "CloudShield architecture family";
3006 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
3007 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
55e22ca8 3008 case EM_ARC_COMPACT2: return "ARCv2";
6d913794 3009 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
55e22ca8 3010 case EM_RL78: return "Renesas RL78";
6d913794 3011 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
55e22ca8
NC
3012 case EM_78K0R: return "Renesas 78K0R";
3013 /* 200 */
6d913794 3014 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
15f205b1
NC
3015 case EM_BA1: return "Beyond BA1 CPU architecture";
3016 case EM_BA2: return "Beyond BA2 CPU architecture";
6d913794
NC
3017 case EM_XCORE: return "XMOS xCORE processor family";
3018 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
7b9f9859 3019 case EM_INTELGT: return "Intel Graphics Technology";
55e22ca8 3020 /* 210 */
6d913794
NC
3021 case EM_KM32: return "KM211 KM32 32-bit processor";
3022 case EM_KMX32: return "KM211 KMX32 32-bit processor";
3023 case EM_KMX16: return "KM211 KMX16 16-bit processor";
3024 case EM_KMX8: return "KM211 KMX8 8-bit processor";
3025 case EM_KVARC: return "KM211 KVARC processor";
15f205b1 3026 case EM_CDP: return "Paneve CDP architecture family";
6d913794
NC
3027 case EM_COGE: return "Cognitive Smart Memory Processor";
3028 case EM_COOL: return "Bluechip Systems CoolEngine";
3029 case EM_NORC: return "Nanoradio Optimized RISC";
3030 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
55e22ca8 3031 /* 220 */
15f205b1 3032 case EM_Z80: return "Zilog Z80";
55e22ca8
NC
3033 case EM_VISIUM: return "CDS VISIUMcore processor";
3034 case EM_FT32: return "FTDI Chip FT32";
3035 case EM_MOXIE: return "Moxie";
3036 case EM_AMDGPU: return "AMD GPU";
4cf2ad72
CC
3037 /* 230 (all reserved) */
3038 /* 240 */
55e22ca8
NC
3039 case EM_RISCV: return "RISC-V";
3040 case EM_LANAI: return "Lanai 32-bit processor";
4cf2ad72
CC
3041 case EM_CEVA: return "CEVA Processor Architecture Family";
3042 case EM_CEVA_X2: return "CEVA X2 Processor Family";
55e22ca8 3043 case EM_BPF: return "Linux BPF";
4cf2ad72
CC
3044 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
3045 case EM_IMG1: return "Imagination Technologies";
3046 /* 250 */
fe944acf 3047 case EM_NFP: return "Netronome Flow Processor";
4cf2ad72
CC
3048 case EM_VE: return "NEC Vector Engine";
3049 case EM_CSKY: return "C-SKY";
b5c37946 3050 case EM_ARC_COMPACT3_64: return "Synopsys ARCv3 64-bit processor";
4cf2ad72 3051 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
b5c37946 3052 case EM_ARC_COMPACT3: return "Synopsys ARCv3 32-bit processor";
4cf2ad72
CC
3053 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
3054 case EM_65816: return "WDC 65816/65C816";
01a8c731 3055 case EM_LOONGARCH: return "LoongArch";
4cf2ad72 3056 case EM_KF32: return "ChipON KungFu32";
55e22ca8
NC
3057
3058 /* Large numbers... */
3059 case EM_MT: return "Morpho Techologies MT processor";
3060 case EM_ALPHA: return "Alpha";
3061 case EM_WEBASSEMBLY: return "Web Assembly";
9abca702 3062 case EM_DLX: return "OpenDLX";
55e22ca8
NC
3063 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
3064 case EM_IQ2000: return "Vitesse IQ2000";
3065 case EM_M32C_OLD:
3066 case EM_NIOS32: return "Altera Nios";
3067 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
3068 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
3069 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
637b1970 3070 case EM_S12Z: return "Freescale S12Z";
55e22ca8 3071
252b5132 3072 default:
35d9dd2f 3073 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
252b5132
RH
3074 return buff;
3075 }
3076}
3077
f8c4789c
AM
3078static char *
3079decode_ARC_machine_flags (char *out, unsigned e_flags, unsigned e_machine)
a9522a21
AB
3080{
3081 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
6987d5a1 3082 other compilers don't specify an architecture type in the e_flags, and
a9522a21
AB
3083 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
3084 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
3085 architectures.
3086
3087 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
3088 but also sets a specific architecture type in the e_flags field.
3089
3090 However, when decoding the flags we don't worry if we see an
3091 unexpected pairing, for example EM_ARC_COMPACT machine type, with
3092 ARCEM architecture type. */
3093
3094 switch (e_flags & EF_ARC_MACH_MSK)
3095 {
3096 /* We only expect these to occur for EM_ARC_COMPACT2. */
3097 case EF_ARC_CPU_ARCV2EM:
f8c4789c 3098 out = stpcpy (out, ", ARC EM");
a9522a21
AB
3099 break;
3100 case EF_ARC_CPU_ARCV2HS:
f8c4789c 3101 out = stpcpy (out, ", ARC HS");
a9522a21
AB
3102 break;
3103
3104 /* We only expect these to occur for EM_ARC_COMPACT. */
3105 case E_ARC_MACH_ARC600:
f8c4789c 3106 out = stpcpy (out, ", ARC600");
a9522a21
AB
3107 break;
3108 case E_ARC_MACH_ARC601:
f8c4789c 3109 out = stpcpy (out, ", ARC601");
a9522a21
AB
3110 break;
3111 case E_ARC_MACH_ARC700:
f8c4789c 3112 out = stpcpy (out, ", ARC700");
a9522a21
AB
3113 break;
3114
3115 /* The only times we should end up here are (a) A corrupt ELF, (b) A
3116 new ELF with new architecture being read by an old version of
3117 readelf, or (c) An ELF built with non-GNU compiler that does not
3118 set the architecture in the e_flags. */
3119 default:
3120 if (e_machine == EM_ARC_COMPACT)
f8c4789c 3121 out = stpcpy (out, ", Unknown ARCompact");
a9522a21 3122 else
f8c4789c 3123 out = stpcpy (out, ", Unknown ARC");
a9522a21
AB
3124 break;
3125 }
3126
3127 switch (e_flags & EF_ARC_OSABI_MSK)
3128 {
3129 case E_ARC_OSABI_ORIG:
f8c4789c 3130 out = stpcpy (out, ", (ABI:legacy)");
a9522a21
AB
3131 break;
3132 case E_ARC_OSABI_V2:
f8c4789c 3133 out = stpcpy (out, ", (ABI:v2)");
a9522a21
AB
3134 break;
3135 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
3136 case E_ARC_OSABI_V3:
f8c4789c 3137 out = stpcpy (out, ", v3 no-legacy-syscalls ABI");
a9522a21 3138 break;
53a346d8 3139 case E_ARC_OSABI_V4:
f8c4789c 3140 out = stpcpy (out, ", v4 ABI");
53a346d8 3141 break;
a9522a21 3142 default:
f8c4789c 3143 out = stpcpy (out, ", unrecognised ARC OSABI flag");
a9522a21
AB
3144 break;
3145 }
f8c4789c 3146 return out;
a9522a21
AB
3147}
3148
f8c4789c
AM
3149static char *
3150decode_ARM_machine_flags (char *out, unsigned e_flags)
f3485b74
NC
3151{
3152 unsigned eabi;
015dc7e1 3153 bool unknown = false;
f3485b74
NC
3154
3155 eabi = EF_ARM_EABI_VERSION (e_flags);
3156 e_flags &= ~ EF_ARM_EABIMASK;
3157
3158 /* Handle "generic" ARM flags. */
3159 if (e_flags & EF_ARM_RELEXEC)
3160 {
f8c4789c 3161 out = stpcpy (out, ", relocatable executable");
f3485b74
NC
3162 e_flags &= ~ EF_ARM_RELEXEC;
3163 }
76da6bbe 3164
18a20338
CL
3165 if (e_flags & EF_ARM_PIC)
3166 {
f8c4789c 3167 out = stpcpy (out, ", position independent");
18a20338
CL
3168 e_flags &= ~ EF_ARM_PIC;
3169 }
3170
f3485b74
NC
3171 /* Now handle EABI specific flags. */
3172 switch (eabi)
3173 {
3174 default:
f8c4789c 3175 out = stpcpy (out, ", <unrecognized EABI>");
f3485b74 3176 if (e_flags)
015dc7e1 3177 unknown = true;
f3485b74
NC
3178 break;
3179
3180 case EF_ARM_EABI_VER1:
f8c4789c 3181 out = stpcpy (out, ", Version1 EABI");
f3485b74
NC
3182 while (e_flags)
3183 {
3184 unsigned flag;
76da6bbe 3185
f3485b74
NC
3186 /* Process flags one bit at a time. */
3187 flag = e_flags & - e_flags;
3188 e_flags &= ~ flag;
76da6bbe 3189
f3485b74
NC
3190 switch (flag)
3191 {
a5bcd848 3192 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
f8c4789c 3193 out = stpcpy (out, ", sorted symbol tables");
f3485b74 3194 break;
76da6bbe 3195
f3485b74 3196 default:
015dc7e1 3197 unknown = true;
f3485b74
NC
3198 break;
3199 }
3200 }
3201 break;
76da6bbe 3202
a5bcd848 3203 case EF_ARM_EABI_VER2:
f8c4789c 3204 out = stpcpy (out, ", Version2 EABI");
a5bcd848
PB
3205 while (e_flags)
3206 {
3207 unsigned flag;
3208
3209 /* Process flags one bit at a time. */
3210 flag = e_flags & - e_flags;
3211 e_flags &= ~ flag;
3212
3213 switch (flag)
3214 {
3215 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
f8c4789c 3216 out = stpcpy (out, ", sorted symbol tables");
a5bcd848
PB
3217 break;
3218
3219 case EF_ARM_DYNSYMSUSESEGIDX:
f8c4789c 3220 out = stpcpy (out, ", dynamic symbols use segment index");
a5bcd848
PB
3221 break;
3222
3223 case EF_ARM_MAPSYMSFIRST:
f8c4789c 3224 out = stpcpy (out, ", mapping symbols precede others");
a5bcd848
PB
3225 break;
3226
3227 default:
015dc7e1 3228 unknown = true;
a5bcd848
PB
3229 break;
3230 }
3231 }
3232 break;
3233
d507cf36 3234 case EF_ARM_EABI_VER3:
f8c4789c 3235 out = stpcpy (out, ", Version3 EABI");
8cb51566
PB
3236 break;
3237
3238 case EF_ARM_EABI_VER4:
f8c4789c 3239 out = stpcpy (out, ", Version4 EABI");
3bfcb652
NC
3240 while (e_flags)
3241 {
3242 unsigned flag;
3243
3244 /* Process flags one bit at a time. */
3245 flag = e_flags & - e_flags;
3246 e_flags &= ~ flag;
3247
3248 switch (flag)
3249 {
3250 case EF_ARM_BE8:
f8c4789c 3251 out = stpcpy (out, ", BE8");
3bfcb652
NC
3252 break;
3253
3254 case EF_ARM_LE8:
f8c4789c 3255 out = stpcpy (out, ", LE8");
3bfcb652
NC
3256 break;
3257
3258 default:
015dc7e1 3259 unknown = true;
3bfcb652
NC
3260 break;
3261 }
3bfcb652
NC
3262 }
3263 break;
3a4a14e9
PB
3264
3265 case EF_ARM_EABI_VER5:
f8c4789c 3266 out = stpcpy (out, ", Version5 EABI");
d507cf36
PB
3267 while (e_flags)
3268 {
3269 unsigned flag;
3270
3271 /* Process flags one bit at a time. */
3272 flag = e_flags & - e_flags;
3273 e_flags &= ~ flag;
3274
3275 switch (flag)
3276 {
3277 case EF_ARM_BE8:
f8c4789c 3278 out = stpcpy (out, ", BE8");
d507cf36
PB
3279 break;
3280
3281 case EF_ARM_LE8:
f8c4789c 3282 out = stpcpy (out, ", LE8");
d507cf36
PB
3283 break;
3284
3bfcb652 3285 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
f8c4789c 3286 out = stpcpy (out, ", soft-float ABI");
3bfcb652
NC
3287 break;
3288
3289 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
f8c4789c 3290 out = stpcpy (out, ", hard-float ABI");
3bfcb652
NC
3291 break;
3292
d507cf36 3293 default:
015dc7e1 3294 unknown = true;
d507cf36
PB
3295 break;
3296 }
3297 }
3298 break;
3299
f3485b74 3300 case EF_ARM_EABI_UNKNOWN:
f8c4789c 3301 out = stpcpy (out, ", GNU EABI");
f3485b74
NC
3302 while (e_flags)
3303 {
3304 unsigned flag;
76da6bbe 3305
f3485b74
NC
3306 /* Process flags one bit at a time. */
3307 flag = e_flags & - e_flags;
3308 e_flags &= ~ flag;
76da6bbe 3309
f3485b74
NC
3310 switch (flag)
3311 {
a5bcd848 3312 case EF_ARM_INTERWORK:
f8c4789c 3313 out = stpcpy (out, ", interworking enabled");
f3485b74 3314 break;
76da6bbe 3315
a5bcd848 3316 case EF_ARM_APCS_26:
f8c4789c 3317 out = stpcpy (out, ", uses APCS/26");
f3485b74 3318 break;
76da6bbe 3319
a5bcd848 3320 case EF_ARM_APCS_FLOAT:
f8c4789c 3321 out = stpcpy (out, ", uses APCS/float");
f3485b74 3322 break;
76da6bbe 3323
a5bcd848 3324 case EF_ARM_PIC:
f8c4789c 3325 out = stpcpy (out, ", position independent");
f3485b74 3326 break;
76da6bbe 3327
a5bcd848 3328 case EF_ARM_ALIGN8:
f8c4789c 3329 out = stpcpy (out, ", 8 bit structure alignment");
f3485b74 3330 break;
76da6bbe 3331
a5bcd848 3332 case EF_ARM_NEW_ABI:
f8c4789c 3333 out = stpcpy (out, ", uses new ABI");
f3485b74 3334 break;
76da6bbe 3335
a5bcd848 3336 case EF_ARM_OLD_ABI:
f8c4789c 3337 out = stpcpy (out, ", uses old ABI");
f3485b74 3338 break;
76da6bbe 3339
a5bcd848 3340 case EF_ARM_SOFT_FLOAT:
f8c4789c 3341 out = stpcpy (out, ", software FP");
f3485b74 3342 break;
76da6bbe 3343
90e01f86 3344 case EF_ARM_VFP_FLOAT:
f8c4789c 3345 out = stpcpy (out, ", VFP");
90e01f86
ILT
3346 break;
3347
fde78edd 3348 case EF_ARM_MAVERICK_FLOAT:
f8c4789c 3349 out = stpcpy (out, ", Maverick FP");
fde78edd
NC
3350 break;
3351
f3485b74 3352 default:
015dc7e1 3353 unknown = true;
f3485b74
NC
3354 break;
3355 }
3356 }
3357 }
f3485b74
NC
3358
3359 if (unknown)
f8c4789c
AM
3360 out = stpcpy (out,_(", <unknown>"));
3361 return out;
f3485b74
NC
3362}
3363
f8c4789c
AM
3364static char *
3365decode_AVR_machine_flags (char *out, unsigned e_flags)
343433df 3366{
343433df
AB
3367 switch (e_flags & EF_AVR_MACH)
3368 {
3369 case E_AVR_MACH_AVR1:
f8c4789c 3370 out = stpcpy (out, ", avr:1");
343433df
AB
3371 break;
3372 case E_AVR_MACH_AVR2:
f8c4789c 3373 out = stpcpy (out, ", avr:2");
343433df
AB
3374 break;
3375 case E_AVR_MACH_AVR25:
f8c4789c 3376 out = stpcpy (out, ", avr:25");
343433df
AB
3377 break;
3378 case E_AVR_MACH_AVR3:
f8c4789c 3379 out = stpcpy (out, ", avr:3");
343433df
AB
3380 break;
3381 case E_AVR_MACH_AVR31:
f8c4789c 3382 out = stpcpy (out, ", avr:31");
343433df
AB
3383 break;
3384 case E_AVR_MACH_AVR35:
f8c4789c 3385 out = stpcpy (out, ", avr:35");
343433df
AB
3386 break;
3387 case E_AVR_MACH_AVR4:
f8c4789c 3388 out = stpcpy (out, ", avr:4");
343433df
AB
3389 break;
3390 case E_AVR_MACH_AVR5:
f8c4789c 3391 out = stpcpy (out, ", avr:5");
343433df
AB
3392 break;
3393 case E_AVR_MACH_AVR51:
f8c4789c 3394 out = stpcpy (out, ", avr:51");
343433df
AB
3395 break;
3396 case E_AVR_MACH_AVR6:
f8c4789c 3397 out = stpcpy (out, ", avr:6");
343433df
AB
3398 break;
3399 case E_AVR_MACH_AVRTINY:
f8c4789c 3400 out = stpcpy (out, ", avr:100");
343433df
AB
3401 break;
3402 case E_AVR_MACH_XMEGA1:
f8c4789c 3403 out = stpcpy (out, ", avr:101");
343433df
AB
3404 break;
3405 case E_AVR_MACH_XMEGA2:
f8c4789c 3406 out = stpcpy (out, ", avr:102");
343433df
AB
3407 break;
3408 case E_AVR_MACH_XMEGA3:
f8c4789c 3409 out = stpcpy (out, ", avr:103");
343433df
AB
3410 break;
3411 case E_AVR_MACH_XMEGA4:
f8c4789c 3412 out = stpcpy (out, ", avr:104");
343433df
AB
3413 break;
3414 case E_AVR_MACH_XMEGA5:
f8c4789c 3415 out = stpcpy (out, ", avr:105");
343433df
AB
3416 break;
3417 case E_AVR_MACH_XMEGA6:
f8c4789c 3418 out = stpcpy (out, ", avr:106");
343433df
AB
3419 break;
3420 case E_AVR_MACH_XMEGA7:
f8c4789c 3421 out = stpcpy (out, ", avr:107");
343433df
AB
3422 break;
3423 default:
f8c4789c 3424 out = stpcpy (out, ", avr:<unknown>");
343433df
AB
3425 break;
3426 }
3427
343433df 3428 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
f8c4789c
AM
3429 out = stpcpy (out, ", link-relax");
3430 return out;
343433df
AB
3431}
3432
f8c4789c
AM
3433static char *
3434decode_BLACKFIN_machine_flags (char *out, unsigned e_flags)
3435{
3436 if (e_flags & EF_BFIN_PIC)
3437 out = stpcpy (out, ", PIC");
3438
3439 if (e_flags & EF_BFIN_FDPIC)
3440 out = stpcpy (out, ", FDPIC");
3441
3442 if (e_flags & EF_BFIN_CODE_IN_L1)
3443 out = stpcpy (out, ", code in L1");
3444
3445 if (e_flags & EF_BFIN_DATA_IN_L1)
3446 out = stpcpy (out, ", data in L1");
3447 return out;
3448}
3449
3450static char *
3451decode_FRV_machine_flags (char *out, unsigned e_flags)
3452{
3453 switch (e_flags & EF_FRV_CPU_MASK)
3454 {
3455 case EF_FRV_CPU_GENERIC:
3456 break;
3457
3458 default:
3459 out = stpcpy (out, ", fr???");
3460 break;
3461
3462 case EF_FRV_CPU_FR300:
3463 out = stpcpy (out, ", fr300");
3464 break;
3465
3466 case EF_FRV_CPU_FR400:
3467 out = stpcpy (out, ", fr400");
3468 break;
3469 case EF_FRV_CPU_FR405:
3470 out = stpcpy (out, ", fr405");
3471 break;
3472
3473 case EF_FRV_CPU_FR450:
3474 out = stpcpy (out, ", fr450");
3475 break;
3476
3477 case EF_FRV_CPU_FR500:
3478 out = stpcpy (out, ", fr500");
3479 break;
3480 case EF_FRV_CPU_FR550:
3481 out = stpcpy (out, ", fr550");
3482 break;
3483
3484 case EF_FRV_CPU_SIMPLE:
3485 out = stpcpy (out, ", simple");
3486 break;
3487 case EF_FRV_CPU_TOMCAT:
3488 out = stpcpy (out, ", tomcat");
3489 break;
3490 }
3491 return out;
3492}
3493
3494static char *
3495decode_IA64_machine_flags (char *out, unsigned e_flags, Filedata *filedata)
3496{
3497 if ((e_flags & EF_IA_64_ABI64))
3498 out = stpcpy (out, ", 64-bit");
3499 else
3500 out = stpcpy (out, ", 32-bit");
3501 if ((e_flags & EF_IA_64_REDUCEDFP))
3502 out = stpcpy (out, ", reduced fp model");
3503 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3504 out = stpcpy (out, ", no function descriptors, constant gp");
3505 else if ((e_flags & EF_IA_64_CONS_GP))
3506 out = stpcpy (out, ", constant gp");
3507 if ((e_flags & EF_IA_64_ABSOLUTE))
3508 out = stpcpy (out, ", absolute");
3509 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3510 {
3511 if ((e_flags & EF_IA_64_VMS_LINKAGES))
3512 out = stpcpy (out, ", vms_linkages");
3513 switch ((e_flags & EF_IA_64_VMS_COMCOD))
3514 {
3515 case EF_IA_64_VMS_COMCOD_SUCCESS:
3516 break;
3517 case EF_IA_64_VMS_COMCOD_WARNING:
3518 out = stpcpy (out, ", warning");
3519 break;
3520 case EF_IA_64_VMS_COMCOD_ERROR:
3521 out = stpcpy (out, ", error");
3522 break;
3523 case EF_IA_64_VMS_COMCOD_ABORT:
3524 out = stpcpy (out, ", abort");
3525 break;
3526 default:
3527 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
3528 e_flags & EF_IA_64_VMS_COMCOD);
3529 out = stpcpy (out, ", <unknown>");
3530 }
3531 }
3532 return out;
3533}
3534
3535static char *
3536decode_LOONGARCH_machine_flags (char *out, unsigned int e_flags)
3537{
3538 if (EF_LOONGARCH_IS_SOFT_FLOAT (e_flags))
3539 out = stpcpy (out, ", SOFT-FLOAT");
3540 else if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
3541 out = stpcpy (out, ", SINGLE-FLOAT");
3542 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
3543 out = stpcpy (out, ", DOUBLE-FLOAT");
3544
3545 if (EF_LOONGARCH_IS_OBJ_V0 (e_flags))
3546 out = stpcpy (out, ", OBJ-v0");
3547 else if (EF_LOONGARCH_IS_OBJ_V1 (e_flags))
3548 out = stpcpy (out, ", OBJ-v1");
3549 return out;
3550}
3551
3552static char *
3553decode_M68K_machine_flags (char *out, unsigned int e_flags)
3554{
3555 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3556 out = stpcpy (out, ", m68000");
3557 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3558 out = stpcpy (out, ", cpu32");
3559 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3560 out = stpcpy (out, ", fido_a");
3561 else
3562 {
3563 char const *isa = _("unknown");
3564 char const *mac = _("unknown mac");
3565 char const *additional = NULL;
3566
3567 switch (e_flags & EF_M68K_CF_ISA_MASK)
3568 {
3569 case EF_M68K_CF_ISA_A_NODIV:
3570 isa = "A";
3571 additional = ", nodiv";
3572 break;
3573 case EF_M68K_CF_ISA_A:
3574 isa = "A";
3575 break;
3576 case EF_M68K_CF_ISA_A_PLUS:
3577 isa = "A+";
3578 break;
3579 case EF_M68K_CF_ISA_B_NOUSP:
3580 isa = "B";
3581 additional = ", nousp";
3582 break;
3583 case EF_M68K_CF_ISA_B:
3584 isa = "B";
3585 break;
3586 case EF_M68K_CF_ISA_C:
3587 isa = "C";
3588 break;
3589 case EF_M68K_CF_ISA_C_NODIV:
3590 isa = "C";
3591 additional = ", nodiv";
3592 break;
3593 }
3594 out = stpcpy (out, ", cf, isa ");
3595 out = stpcpy (out, isa);
3596 if (additional)
3597 out = stpcpy (out, additional);
3598 if (e_flags & EF_M68K_CF_FLOAT)
3599 out = stpcpy (out, ", float");
3600 switch (e_flags & EF_M68K_CF_MAC_MASK)
3601 {
3602 case 0:
3603 mac = NULL;
3604 break;
3605 case EF_M68K_CF_MAC:
3606 mac = "mac";
3607 break;
3608 case EF_M68K_CF_EMAC:
3609 mac = "emac";
3610 break;
3611 case EF_M68K_CF_EMAC_B:
3612 mac = "emac_b";
3613 break;
3614 }
3615 if (mac)
3616 {
3617 out = stpcpy (out, ", ");
3618 out = stpcpy (out, mac);
3619 }
3620 }
3621 return out;
3622}
3623
3624static char *
3625decode_MeP_machine_flags (char *out, unsigned int e_flags)
3626{
3627 switch (e_flags & EF_MEP_CPU_MASK)
3628 {
3629 case EF_MEP_CPU_MEP:
3630 out = stpcpy (out, ", generic MeP");
3631 break;
3632 case EF_MEP_CPU_C2:
3633 out = stpcpy (out, ", MeP C2");
3634 break;
3635 case EF_MEP_CPU_C3:
3636 out = stpcpy (out, ", MeP C3");
3637 break;
3638 case EF_MEP_CPU_C4:
3639 out = stpcpy (out, ", MeP C4");
3640 break;
3641 case EF_MEP_CPU_C5:
3642 out = stpcpy (out, ", MeP C5");
3643 break;
3644 case EF_MEP_CPU_H1:
3645 out = stpcpy (out, ", MeP H1");
3646 break;
3647 default:
3648 out = stpcpy (out, _(", <unknown MeP cpu type>"));
3649 break;
3650 }
3651
3652 switch (e_flags & EF_MEP_COP_MASK)
3653 {
3654 case EF_MEP_COP_NONE:
3655 break;
3656 case EF_MEP_COP_AVC:
3657 out = stpcpy (out, ", AVC coprocessor");
3658 break;
3659 case EF_MEP_COP_AVC2:
3660 out = stpcpy (out, ", AVC2 coprocessor");
3661 break;
3662 case EF_MEP_COP_FMAX:
3663 out = stpcpy (out, ", FMAX coprocessor");
3664 break;
3665 case EF_MEP_COP_IVC2:
3666 out = stpcpy (out, ", IVC2 coprocessor");
3667 break;
3668 default:
3669 out = stpcpy (out, _("<unknown MeP copro type>"));
3670 break;
3671 }
3672
3673 if (e_flags & EF_MEP_LIBRARY)
3674 out = stpcpy (out, ", Built for Library");
3675
3676 if (e_flags & EF_MEP_INDEX_MASK)
3677 out += sprintf (out, ", Configuration Index: %#x",
3678 e_flags & EF_MEP_INDEX_MASK);
3679
3680 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3681 out += sprintf (out, _(", unknown flags bits: %#x"),
3682 e_flags & ~ EF_MEP_ALL_FLAGS);
3683 return out;
3684}
3685
3686static char *
3687decode_MIPS_machine_flags (char *out, unsigned int e_flags)
3688{
3689 if (e_flags & EF_MIPS_NOREORDER)
3690 out = stpcpy (out, ", noreorder");
3691
3692 if (e_flags & EF_MIPS_PIC)
3693 out = stpcpy (out, ", pic");
3694
3695 if (e_flags & EF_MIPS_CPIC)
3696 out = stpcpy (out, ", cpic");
3697
3698 if (e_flags & EF_MIPS_UCODE)
3699 out = stpcpy (out, ", ugen_reserved");
3700
3701 if (e_flags & EF_MIPS_ABI2)
3702 out = stpcpy (out, ", abi2");
3703
3704 if (e_flags & EF_MIPS_OPTIONS_FIRST)
3705 out = stpcpy (out, ", odk first");
3706
3707 if (e_flags & EF_MIPS_32BITMODE)
3708 out = stpcpy (out, ", 32bitmode");
3709
3710 if (e_flags & EF_MIPS_NAN2008)
3711 out = stpcpy (out, ", nan2008");
3712
3713 if (e_flags & EF_MIPS_FP64)
3714 out = stpcpy (out, ", fp64");
3715
3716 switch ((e_flags & EF_MIPS_MACH))
3717 {
3718 case E_MIPS_MACH_3900:
3719 out = stpcpy (out, ", 3900");
3720 break;
3721 case E_MIPS_MACH_4010:
3722 out = stpcpy (out, ", 4010");
3723 break;
3724 case E_MIPS_MACH_4100:
3725 out = stpcpy (out, ", 4100");
3726 break;
3727 case E_MIPS_MACH_4111:
3728 out = stpcpy (out, ", 4111");
3729 break;
3730 case E_MIPS_MACH_4120:
3731 out = stpcpy (out, ", 4120");
3732 break;
3733 case E_MIPS_MACH_4650:
3734 out = stpcpy (out, ", 4650");
3735 break;
3736 case E_MIPS_MACH_5400:
3737 out = stpcpy (out, ", 5400");
3738 break;
3739 case E_MIPS_MACH_5500:
3740 out = stpcpy (out, ", 5500");
3741 break;
3742 case E_MIPS_MACH_5900:
3743 out = stpcpy (out, ", 5900");
3744 break;
3745 case E_MIPS_MACH_SB1:
3746 out = stpcpy (out, ", sb1");
3747 break;
3748 case E_MIPS_MACH_9000:
3749 out = stpcpy (out, ", 9000");
3750 break;
3751 case E_MIPS_MACH_LS2E:
3752 out = stpcpy (out, ", loongson-2e");
3753 break;
3754 case E_MIPS_MACH_LS2F:
3755 out = stpcpy (out, ", loongson-2f");
3756 break;
3757 case E_MIPS_MACH_GS464:
3758 out = stpcpy (out, ", gs464");
3759 break;
3760 case E_MIPS_MACH_GS464E:
3761 out = stpcpy (out, ", gs464e");
3762 break;
3763 case E_MIPS_MACH_GS264E:
3764 out = stpcpy (out, ", gs264e");
3765 break;
3766 case E_MIPS_MACH_OCTEON:
3767 out = stpcpy (out, ", octeon");
3768 break;
3769 case E_MIPS_MACH_OCTEON2:
3770 out = stpcpy (out, ", octeon2");
3771 break;
3772 case E_MIPS_MACH_OCTEON3:
3773 out = stpcpy (out, ", octeon3");
3774 break;
3775 case E_MIPS_MACH_XLR:
3776 out = stpcpy (out, ", xlr");
3777 break;
3778 case E_MIPS_MACH_IAMR2:
3779 out = stpcpy (out, ", interaptiv-mr2");
3780 break;
3781 case E_MIPS_MACH_ALLEGREX:
3782 out = stpcpy (out, ", allegrex");
3783 break;
3784 case 0:
3785 /* We simply ignore the field in this case to avoid confusion:
3786 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
3787 extension. */
3788 break;
3789 default:
3790 out = stpcpy (out, _(", unknown CPU"));
3791 break;
3792 }
3793
3794 switch ((e_flags & EF_MIPS_ABI))
3795 {
3796 case E_MIPS_ABI_O32:
3797 out = stpcpy (out, ", o32");
3798 break;
3799 case E_MIPS_ABI_O64:
3800 out = stpcpy (out, ", o64");
3801 break;
3802 case E_MIPS_ABI_EABI32:
3803 out = stpcpy (out, ", eabi32");
3804 break;
3805 case E_MIPS_ABI_EABI64:
3806 out = stpcpy (out, ", eabi64");
3807 break;
3808 case 0:
3809 /* We simply ignore the field in this case to avoid confusion:
3810 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
3811 This means it is likely to be an o32 file, but not for
3812 sure. */
3813 break;
3814 default:
3815 out = stpcpy (out, _(", unknown ABI"));
3816 break;
3817 }
3818
3819 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
3820 out = stpcpy (out, ", mdmx");
3821
3822 if (e_flags & EF_MIPS_ARCH_ASE_M16)
3823 out = stpcpy (out, ", mips16");
3824
3825 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
3826 out = stpcpy (out, ", micromips");
3827
3828 switch ((e_flags & EF_MIPS_ARCH))
3829 {
3830 case E_MIPS_ARCH_1:
3831 out = stpcpy (out, ", mips1");
3832 break;
3833 case E_MIPS_ARCH_2:
3834 out = stpcpy (out, ", mips2");
3835 break;
3836 case E_MIPS_ARCH_3:
3837 out = stpcpy (out, ", mips3");
3838 break;
3839 case E_MIPS_ARCH_4:
3840 out = stpcpy (out, ", mips4");
3841 break;
3842 case E_MIPS_ARCH_5:
3843 out = stpcpy (out, ", mips5");
3844 break;
3845 case E_MIPS_ARCH_32:
3846 out = stpcpy (out, ", mips32");
3847 break;
3848 case E_MIPS_ARCH_32R2:
3849 out = stpcpy (out, ", mips32r2");
3850 break;
3851 case E_MIPS_ARCH_32R6:
3852 out = stpcpy (out, ", mips32r6");
3853 break;
3854 case E_MIPS_ARCH_64:
3855 out = stpcpy (out, ", mips64");
3856 break;
3857 case E_MIPS_ARCH_64R2:
3858 out = stpcpy (out, ", mips64r2");
3859 break;
3860 case E_MIPS_ARCH_64R6:
3861 out = stpcpy (out, ", mips64r6");
3862 break;
3863 default:
3864 out = stpcpy (out, _(", unknown ISA"));
3865 break;
3866 }
3867 return out;
3868}
3869
3870static char *
3871decode_MSP430_machine_flags (char *out, unsigned e_flags)
3872{
3873 out = stpcpy (out, _(": architecture variant: "));
3874 switch (e_flags & EF_MSP430_MACH)
3875 {
3876 case E_MSP430_MACH_MSP430x11:
3877 out = stpcpy (out, "MSP430x11");
3878 break;
3879 case E_MSP430_MACH_MSP430x11x1:
3880 out = stpcpy (out, "MSP430x11x1 ");
3881 break;
3882 case E_MSP430_MACH_MSP430x12:
3883 out = stpcpy (out, "MSP430x12");
3884 break;
3885 case E_MSP430_MACH_MSP430x13:
3886 out = stpcpy (out, "MSP430x13");
3887 break;
3888 case E_MSP430_MACH_MSP430x14:
3889 out = stpcpy (out, "MSP430x14");
3890 break;
3891 case E_MSP430_MACH_MSP430x15:
3892 out = stpcpy (out, "MSP430x15");
3893 break;
3894 case E_MSP430_MACH_MSP430x16:
3895 out = stpcpy (out, "MSP430x16");
3896 break;
3897 case E_MSP430_MACH_MSP430x31:
3898 out = stpcpy (out, "MSP430x31");
3899 break;
3900 case E_MSP430_MACH_MSP430x32:
3901 out = stpcpy (out, "MSP430x32");
3902 break;
3903 case E_MSP430_MACH_MSP430x33:
3904 out = stpcpy (out, "MSP430x33");
3905 break;
3906 case E_MSP430_MACH_MSP430x41:
3907 out = stpcpy (out, "MSP430x41");
3908 break;
3909 case E_MSP430_MACH_MSP430x42:
3910 out = stpcpy (out, "MSP430x42");
3911 break;
3912 case E_MSP430_MACH_MSP430x43:
3913 out = stpcpy (out, "MSP430x43");
3914 break;
3915 case E_MSP430_MACH_MSP430x44:
3916 out = stpcpy (out, "MSP430x44");
3917 break;
3918 case E_MSP430_MACH_MSP430X :
3919 out = stpcpy (out, "MSP430X");
3920 break;
3921 default:
3922 out = stpcpy (out, _(": unknown"));
3923 break;
3924 }
3925
3926 if (e_flags & ~ EF_MSP430_MACH)
3927 out = stpcpy (out, _(": unknown extra flag bits also present"));
3928 return out;
3929}
3930
3931static char *
3932decode_NDS32_machine_flags (char *out, unsigned e_flags)
35c08157
KLC
3933{
3934 unsigned abi;
3935 unsigned arch;
3936 unsigned config;
3937 unsigned version;
015dc7e1 3938 bool has_fpu = false;
35c08157
KLC
3939
3940 static const char *ABI_STRINGS[] =
3941 {
3942 "ABI v0", /* use r5 as return register; only used in N1213HC */
3943 "ABI v1", /* use r0 as return register */
3944 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
3945 "ABI v2fp", /* for FPU */
40c7a7cb
KLC
3946 "AABI",
3947 "ABI2 FP+"
35c08157
KLC
3948 };
3949 static const char *VER_STRINGS[] =
3950 {
3951 "Andes ELF V1.3 or older",
3952 "Andes ELF V1.3.1",
3953 "Andes ELF V1.4"
3954 };
3955 static const char *ARCH_STRINGS[] =
3956 {
3957 "",
3958 "Andes Star v1.0",
3959 "Andes Star v2.0",
3960 "Andes Star v3.0",
3961 "Andes Star v3.0m"
3962 };
3963
3964 abi = EF_NDS_ABI & e_flags;
3965 arch = EF_NDS_ARCH & e_flags;
3966 config = EF_NDS_INST & e_flags;
3967 version = EF_NDS32_ELF_VERSION & e_flags;
3968
35c08157
KLC
3969 switch (abi)
3970 {
3971 case E_NDS_ABI_V0:
3972 case E_NDS_ABI_V1:
3973 case E_NDS_ABI_V2:
3974 case E_NDS_ABI_V2FP:
3975 case E_NDS_ABI_AABI:
40c7a7cb 3976 case E_NDS_ABI_V2FP_PLUS:
35c08157 3977 /* In case there are holes in the array. */
f8c4789c 3978 out += sprintf (out, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
35c08157
KLC
3979 break;
3980
3981 default:
f8c4789c 3982 out = stpcpy (out, ", <unrecognized ABI>");
35c08157
KLC
3983 break;
3984 }
3985
3986 switch (version)
3987 {
3988 case E_NDS32_ELF_VER_1_2:
3989 case E_NDS32_ELF_VER_1_3:
3990 case E_NDS32_ELF_VER_1_4:
f8c4789c 3991 out += sprintf (out, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
35c08157
KLC
3992 break;
3993
3994 default:
f8c4789c 3995 out = stpcpy (out, ", <unrecognized ELF version number>");
35c08157
KLC
3996 break;
3997 }
3998
3999 if (E_NDS_ABI_V0 == abi)
4000 {
4001 /* OLD ABI; only used in N1213HC, has performance extension 1. */
f8c4789c 4002 out = stpcpy (out, ", Andes Star v1.0, N1213HC, MAC, PERF1");
35c08157 4003 if (arch == E_NDS_ARCH_STAR_V1_0)
f8c4789c
AM
4004 out = stpcpy (out, ", 16b"); /* has 16-bit instructions */
4005 return out;
35c08157
KLC
4006 }
4007
4008 switch (arch)
4009 {
4010 case E_NDS_ARCH_STAR_V1_0:
4011 case E_NDS_ARCH_STAR_V2_0:
4012 case E_NDS_ARCH_STAR_V3_0:
4013 case E_NDS_ARCH_STAR_V3_M:
f8c4789c 4014 out += sprintf (out, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
35c08157
KLC
4015 break;
4016
4017 default:
f8c4789c 4018 out = stpcpy (out, ", <unrecognized architecture>");
35c08157
KLC
4019 /* ARCH version determines how the e_flags are interpreted.
4020 If it is unknown, we cannot proceed. */
f8c4789c 4021 return out;
35c08157
KLC
4022 }
4023
4024 /* Newer ABI; Now handle architecture specific flags. */
4025 if (arch == E_NDS_ARCH_STAR_V1_0)
4026 {
4027 if (config & E_NDS32_HAS_MFUSR_PC_INST)
f8c4789c 4028 out = stpcpy (out, ", MFUSR_PC");
35c08157
KLC
4029
4030 if (!(config & E_NDS32_HAS_NO_MAC_INST))
f8c4789c 4031 out = stpcpy (out, ", MAC");
35c08157
KLC
4032
4033 if (config & E_NDS32_HAS_DIV_INST)
f8c4789c 4034 out = stpcpy (out, ", DIV");
35c08157
KLC
4035
4036 if (config & E_NDS32_HAS_16BIT_INST)
f8c4789c 4037 out = stpcpy (out, ", 16b");
35c08157
KLC
4038 }
4039 else
4040 {
4041 if (config & E_NDS32_HAS_MFUSR_PC_INST)
4042 {
4043 if (version <= E_NDS32_ELF_VER_1_3)
f8c4789c 4044 out = stpcpy (out, ", [B8]");
35c08157 4045 else
f8c4789c 4046 out = stpcpy (out, ", EX9");
35c08157
KLC
4047 }
4048
4049 if (config & E_NDS32_HAS_MAC_DX_INST)
f8c4789c 4050 out = stpcpy (out, ", MAC_DX");
35c08157
KLC
4051
4052 if (config & E_NDS32_HAS_DIV_DX_INST)
f8c4789c 4053 out = stpcpy (out, ", DIV_DX");
35c08157
KLC
4054
4055 if (config & E_NDS32_HAS_16BIT_INST)
4056 {
4057 if (version <= E_NDS32_ELF_VER_1_3)
f8c4789c 4058 out = stpcpy (out, ", 16b");
35c08157 4059 else
f8c4789c 4060 out = stpcpy (out, ", IFC");
35c08157
KLC
4061 }
4062 }
4063
4064 if (config & E_NDS32_HAS_EXT_INST)
f8c4789c 4065 out = stpcpy (out, ", PERF1");
35c08157
KLC
4066
4067 if (config & E_NDS32_HAS_EXT2_INST)
f8c4789c 4068 out = stpcpy (out, ", PERF2");
35c08157
KLC
4069
4070 if (config & E_NDS32_HAS_FPU_INST)
4071 {
015dc7e1 4072 has_fpu = true;
f8c4789c 4073 out = stpcpy (out, ", FPU_SP");
35c08157
KLC
4074 }
4075
4076 if (config & E_NDS32_HAS_FPU_DP_INST)
4077 {
015dc7e1 4078 has_fpu = true;
f8c4789c 4079 out = stpcpy (out, ", FPU_DP");
35c08157
KLC
4080 }
4081
4082 if (config & E_NDS32_HAS_FPU_MAC_INST)
4083 {
015dc7e1 4084 has_fpu = true;
f8c4789c 4085 out = stpcpy (out, ", FPU_MAC");
35c08157
KLC
4086 }
4087
4088 if (has_fpu)
4089 {
4090 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
4091 {
4092 case E_NDS32_FPU_REG_8SP_4DP:
f8c4789c 4093 out = stpcpy (out, ", FPU_REG:8/4");
35c08157
KLC
4094 break;
4095 case E_NDS32_FPU_REG_16SP_8DP:
f8c4789c 4096 out = stpcpy (out, ", FPU_REG:16/8");
35c08157
KLC
4097 break;
4098 case E_NDS32_FPU_REG_32SP_16DP:
f8c4789c 4099 out = stpcpy (out, ", FPU_REG:32/16");
35c08157
KLC
4100 break;
4101 case E_NDS32_FPU_REG_32SP_32DP:
f8c4789c 4102 out = stpcpy (out, ", FPU_REG:32/32");
35c08157
KLC
4103 break;
4104 }
4105 }
4106
4107 if (config & E_NDS32_HAS_AUDIO_INST)
f8c4789c 4108 out = stpcpy (out, ", AUDIO");
35c08157
KLC
4109
4110 if (config & E_NDS32_HAS_STRING_INST)
f8c4789c 4111 out = stpcpy (out, ", STR");
35c08157
KLC
4112
4113 if (config & E_NDS32_HAS_REDUCED_REGS)
f8c4789c 4114 out = stpcpy (out, ", 16REG");
35c08157
KLC
4115
4116 if (config & E_NDS32_HAS_VIDEO_INST)
4117 {
4118 if (version <= E_NDS32_ELF_VER_1_3)
f8c4789c 4119 out = stpcpy (out, ", VIDEO");
35c08157 4120 else
f8c4789c 4121 out = stpcpy (out, ", SATURATION");
35c08157
KLC
4122 }
4123
4124 if (config & E_NDS32_HAS_ENCRIPT_INST)
f8c4789c 4125 out = stpcpy (out, ", ENCRP");
35c08157
KLC
4126
4127 if (config & E_NDS32_HAS_L2C_INST)
f8c4789c
AM
4128 out = stpcpy (out, ", L2C");
4129
4130 return out;
35c08157
KLC
4131}
4132
f8c4789c
AM
4133static char *
4134decode_PARISC_machine_flags (char *out, unsigned e_flags)
4135{
4136 switch (e_flags & EF_PARISC_ARCH)
4137 {
4138 case EFA_PARISC_1_0:
4139 out = stpcpy (out, ", PA-RISC 1.0");
4140 break;
4141 case EFA_PARISC_1_1:
4142 out = stpcpy (out, ", PA-RISC 1.1");
4143 break;
4144 case EFA_PARISC_2_0:
4145 out = stpcpy (out, ", PA-RISC 2.0");
4146 break;
4147 default:
4148 break;
4149 }
4150 if (e_flags & EF_PARISC_TRAPNIL)
4151 out = stpcpy (out, ", trapnil");
4152 if (e_flags & EF_PARISC_EXT)
4153 out = stpcpy (out, ", ext");
4154 if (e_flags & EF_PARISC_LSB)
4155 out = stpcpy (out, ", lsb");
4156 if (e_flags & EF_PARISC_WIDE)
4157 out = stpcpy (out, ", wide");
4158 if (e_flags & EF_PARISC_NO_KABP)
4159 out = stpcpy (out, ", no kabp");
4160 if (e_flags & EF_PARISC_LAZYSWAP)
4161 out = stpcpy (out, ", lazyswap");
4162 return out;
4163}
4164
4165static char *
4166decode_RISCV_machine_flags (char *out, unsigned e_flags)
4167{
4168 if (e_flags & EF_RISCV_RVC)
4169 out = stpcpy (out, ", RVC");
4170
4171 if (e_flags & EF_RISCV_RVE)
4172 out = stpcpy (out, ", RVE");
4173
4174 if (e_flags & EF_RISCV_TSO)
4175 out = stpcpy (out, ", TSO");
4176
4177 switch (e_flags & EF_RISCV_FLOAT_ABI)
4178 {
4179 case EF_RISCV_FLOAT_ABI_SOFT:
4180 out = stpcpy (out, ", soft-float ABI");
4181 break;
4182
4183 case EF_RISCV_FLOAT_ABI_SINGLE:
4184 out = stpcpy (out, ", single-float ABI");
4185 break;
4186
4187 case EF_RISCV_FLOAT_ABI_DOUBLE:
4188 out = stpcpy (out, ", double-float ABI");
4189 break;
4190
4191 case EF_RISCV_FLOAT_ABI_QUAD:
4192 out = stpcpy (out, ", quad-float ABI");
4193 break;
4194 }
4195 return out;
4196}
4197
4198static char *
4199decode_RL78_machine_flags (char *out, unsigned e_flags)
4200{
4201 switch (e_flags & E_FLAG_RL78_CPU_MASK)
4202 {
4203 case E_FLAG_RL78_ANY_CPU:
4204 break;
4205 case E_FLAG_RL78_G10:
4206 out = stpcpy (out, ", G10");
4207 break;
4208 case E_FLAG_RL78_G13:
4209 out = stpcpy (out, ", G13");
4210 break;
4211 case E_FLAG_RL78_G14:
4212 out = stpcpy (out, ", G14");
4213 break;
4214 }
4215 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
4216 out = stpcpy (out, ", 64-bit doubles");
4217 return out;
4218}
4219
4220static char *
4221decode_RX_machine_flags (char *out, unsigned e_flags)
4222{
4223 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
4224 out = stpcpy (out, ", 64-bit doubles");
4225 if (e_flags & E_FLAG_RX_DSP)
4226 out = stpcpy (out, ", dsp");
4227 if (e_flags & E_FLAG_RX_PID)
4228 out = stpcpy (out, ", pid");
4229 if (e_flags & E_FLAG_RX_ABI)
4230 out = stpcpy (out, ", RX ABI");
4231 if (e_flags & E_FLAG_RX_SINSNS_SET)
4232 out = stpcpy (out, (e_flags & E_FLAG_RX_SINSNS_YES
4233 ? ", uses String instructions"
4234 : ", bans String instructions"));
4235 if (e_flags & E_FLAG_RX_V2)
4236 out = stpcpy (out, ", V2");
4237 if (e_flags & E_FLAG_RX_V3)
4238 out = stpcpy (out, ", V3");
4239 return out;
4240}
4241
4242static char *
4243decode_SH_machine_flags (char *out, unsigned e_flags)
4244{
4245 switch ((e_flags & EF_SH_MACH_MASK))
4246 {
4247 case EF_SH1:
4248 out = stpcpy (out, ", sh1");
4249 break;
4250 case EF_SH2:
4251 out = stpcpy (out, ", sh2");
4252 break;
4253 case EF_SH3:
4254 out = stpcpy (out, ", sh3");
4255 break;
4256 case EF_SH_DSP:
4257 out = stpcpy (out, ", sh-dsp");
4258 break;
4259 case EF_SH3_DSP:
4260 out = stpcpy (out, ", sh3-dsp");
4261 break;
4262 case EF_SH4AL_DSP:
4263 out = stpcpy (out, ", sh4al-dsp");
4264 break;
4265 case EF_SH3E:
4266 out = stpcpy (out, ", sh3e");
4267 break;
4268 case EF_SH4:
4269 out = stpcpy (out, ", sh4");
4270 break;
4271 case EF_SH5:
4272 out = stpcpy (out, ", sh5");
4273 break;
4274 case EF_SH2E:
4275 out = stpcpy (out, ", sh2e");
4276 break;
4277 case EF_SH4A:
4278 out = stpcpy (out, ", sh4a");
4279 break;
4280 case EF_SH2A:
4281 out = stpcpy (out, ", sh2a");
4282 break;
4283 case EF_SH4_NOFPU:
4284 out = stpcpy (out, ", sh4-nofpu");
4285 break;
4286 case EF_SH4A_NOFPU:
4287 out = stpcpy (out, ", sh4a-nofpu");
4288 break;
4289 case EF_SH2A_NOFPU:
4290 out = stpcpy (out, ", sh2a-nofpu");
4291 break;
4292 case EF_SH3_NOMMU:
4293 out = stpcpy (out, ", sh3-nommu");
4294 break;
4295 case EF_SH4_NOMMU_NOFPU:
4296 out = stpcpy (out, ", sh4-nommu-nofpu");
4297 break;
4298 case EF_SH2A_SH4_NOFPU:
4299 out = stpcpy (out, ", sh2a-nofpu-or-sh4-nommu-nofpu");
4300 break;
4301 case EF_SH2A_SH3_NOFPU:
4302 out = stpcpy (out, ", sh2a-nofpu-or-sh3-nommu");
4303 break;
4304 case EF_SH2A_SH4:
4305 out = stpcpy (out, ", sh2a-or-sh4");
4306 break;
4307 case EF_SH2A_SH3E:
4308 out = stpcpy (out, ", sh2a-or-sh3e");
4309 break;
4310 default:
4311 out = stpcpy (out, _(", unknown ISA"));
4312 break;
4313 }
4314
4315 if (e_flags & EF_SH_PIC)
4316 out = stpcpy (out, ", pic");
4317
4318 if (e_flags & EF_SH_FDPIC)
4319 out = stpcpy (out, ", fdpic");
4320 return out;
4321}
4322
4323static char *
4324decode_SPARC_machine_flags (char *out, unsigned e_flags)
4325{
4326 if (e_flags & EF_SPARC_32PLUS)
4327 out = stpcpy (out, ", v8+");
4328
4329 if (e_flags & EF_SPARC_SUN_US1)
4330 out = stpcpy (out, ", ultrasparcI");
4331
4332 if (e_flags & EF_SPARC_SUN_US3)
4333 out = stpcpy (out, ", ultrasparcIII");
4334
4335 if (e_flags & EF_SPARC_HAL_R1)
4336 out = stpcpy (out, ", halr1");
4337
4338 if (e_flags & EF_SPARC_LEDATA)
4339 out = stpcpy (out, ", ledata");
4340
4341 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
4342 out = stpcpy (out, ", tso");
4343
4344 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
4345 out = stpcpy (out, ", pso");
4346
4347 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
4348 out = stpcpy (out, ", rmo");
4349 return out;
4350}
4351
4352static char *
4353decode_V800_machine_flags (char *out, unsigned int e_flags)
4354{
4355 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
4356 out = stpcpy (out, ", RH850 ABI");
4357
4358 if (e_flags & EF_V800_850E3)
4359 out = stpcpy (out, ", V3 architecture");
4360
4361 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
4362 out = stpcpy (out, ", FPU not used");
4363
4364 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
4365 out = stpcpy (out, ", regmode: COMMON");
4366
4367 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
4368 out = stpcpy (out, ", r4 not used");
4369
4370 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
4371 out = stpcpy (out, ", r30 not used");
4372
4373 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
4374 out = stpcpy (out, ", r5 not used");
4375
4376 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
4377 out = stpcpy (out, ", r2 not used");
4378
4379 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
4380 {
4381 switch (e_flags & - e_flags)
4382 {
4383 case EF_RH850_FPU_DOUBLE:
4384 out = stpcpy (out, ", double precision FPU");
4385 break;
4386 case EF_RH850_FPU_SINGLE:
4387 out = stpcpy (out, ", single precision FPU");
4388 break;
4389 case EF_RH850_REGMODE22:
4390 out = stpcpy (out, ", regmode:22");
4391 break;
4392 case EF_RH850_REGMODE32:
4393 out = stpcpy (out, ", regmode:23");
4394 break;
4395 case EF_RH850_GP_FIX:
4396 out = stpcpy (out, ", r4 fixed");
4397 break;
4398 case EF_RH850_GP_NOFIX:
4399 out = stpcpy (out, ", r4 free");
4400 break;
4401 case EF_RH850_EP_FIX:
4402 out = stpcpy (out, ", r30 fixed");
4403 break;
4404 case EF_RH850_EP_NOFIX:
4405 out = stpcpy (out, ", r30 free");
4406 break;
4407 case EF_RH850_TP_FIX:
4408 out = stpcpy (out, ", r5 fixed");
4409 break;
4410 case EF_RH850_TP_NOFIX:
4411 out = stpcpy (out, ", r5 free");
4412 break;
4413 case EF_RH850_REG2_RESERVE:
4414 out = stpcpy (out, ", r2 fixed");
4415 break;
4416 case EF_RH850_REG2_NORESERVE:
4417 out = stpcpy (out, ", r2 free");
4418 break;
4419 default:
4420 break;
4421 }
4422 }
4423 return out;
4424}
4425
4426static char *
4427decode_V850_machine_flags (char *out, unsigned int e_flags)
4428{
4429 switch (e_flags & EF_V850_ARCH)
4430 {
4431 case E_V850E3V5_ARCH:
4432 out = stpcpy (out, ", v850e3v5");
4433 break;
4434 case E_V850E2V3_ARCH:
4435 out = stpcpy (out, ", v850e2v3");
4436 break;
4437 case E_V850E2_ARCH:
4438 out = stpcpy (out, ", v850e2");
4439 break;
4440 case E_V850E1_ARCH:
4441 out = stpcpy (out, ", v850e1");
4442 break;
4443 case E_V850E_ARCH:
4444 out = stpcpy (out, ", v850e");
4445 break;
4446 case E_V850_ARCH:
4447 out = stpcpy (out, ", v850");
4448 break;
4449 default:
4450 out = stpcpy (out, _(", unknown v850 architecture variant"));
4451 break;
4452 }
4453 return out;
4454}
4455
4456static char *
4457decode_Z80_machine_flags (char *out, unsigned int e_flags)
4458{
4459 switch (e_flags & EF_Z80_MACH_MSK)
4460 {
4461 case EF_Z80_MACH_Z80:
4462 out = stpcpy (out, ", Z80");
4463 break;
4464 case EF_Z80_MACH_Z180:
4465 out = stpcpy (out, ", Z180");
4466 break;
4467 case EF_Z80_MACH_R800:
4468 out = stpcpy (out, ", R800");
4469 break;
4470 case EF_Z80_MACH_EZ80_Z80:
4471 out = stpcpy (out, ", EZ80");
4472 break;
4473 case EF_Z80_MACH_EZ80_ADL:
4474 out = stpcpy (out, ", EZ80, ADL");
4475 break;
4476 case EF_Z80_MACH_GBZ80:
4477 out = stpcpy (out, ", GBZ80");
4478 break;
4479 case EF_Z80_MACH_Z80N:
4480 out = stpcpy (out, ", Z80N");
4481 break;
4482 default:
4483 out = stpcpy (out, _(", unknown"));
4484 break;
4485 }
4486 return out;
4487}
4488
4489static char *
4490decode_AMDGPU_machine_flags (char *out, unsigned int e_flags, Filedata *filedata)
c077c580
SM
4491{
4492 unsigned char *e_ident = filedata->file_header.e_ident;
4493 unsigned char osabi = e_ident[EI_OSABI];
4494 unsigned char abiversion = e_ident[EI_ABIVERSION];
4495 unsigned int mach;
4496
4497 /* HSA OS ABI v2 used a different encoding, but we don't need to support it,
4498 it has been deprecated for a while.
4499
4500 The PAL, MESA3D and NONE OS ABIs are not properly versioned, at the time
4501 of writing, they use the same flags as HSA v3, so the code below uses that
4502 assumption. */
4503 if (osabi == ELFOSABI_AMDGPU_HSA && abiversion < ELFABIVERSION_AMDGPU_HSA_V3)
f8c4789c 4504 return out;
c077c580
SM
4505
4506 mach = e_flags & EF_AMDGPU_MACH;
4507 switch (mach)
4508 {
4509#define AMDGPU_CASE(code, string) \
f8c4789c 4510 case code: out = stpcpy (out, ", " string); break;
c077c580
SM
4511 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX600, "gfx600")
4512 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX601, "gfx601")
4513 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX700, "gfx700")
4514 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX701, "gfx701")
4515 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX702, "gfx702")
4516 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX703, "gfx703")
4517 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX704, "gfx704")
4518 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX801, "gfx801")
4519 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX802, "gfx802")
4520 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX803, "gfx803")
4521 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX810, "gfx810")
4522 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX900, "gfx900")
4523 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX902, "gfx902")
4524 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX904, "gfx904")
4525 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX906, "gfx906")
4526 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX908, "gfx908")
4527 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX909, "gfx909")
4528 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90C, "gfx90c")
4529 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1010, "gfx1010")
4530 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1011, "gfx1011")
4531 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1012, "gfx1012")
4532 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1030, "gfx1030")
4533 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1031, "gfx1031")
4534 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1032, "gfx1032")
4535 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1033, "gfx1033")
4536 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX602, "gfx602")
4537 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX705, "gfx705")
4538 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX805, "gfx805")
4539 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1035, "gfx1035")
4540 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1034, "gfx1034")
4541 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90A, "gfx90a")
4542 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX940, "gfx940")
4543 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1013, "gfx1013")
4544 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1036, "gfx1036")
4545 default:
f8c4789c 4546 out += sprintf (out, _(", <unknown AMDGPU GPU type: %#x>"), mach);
c077c580
SM
4547 break;
4548#undef AMDGPU_CASE
4549 }
4550
c077c580
SM
4551 e_flags &= ~EF_AMDGPU_MACH;
4552
4553 if ((osabi == ELFOSABI_AMDGPU_HSA
4554 && abiversion == ELFABIVERSION_AMDGPU_HSA_V3)
4555 || osabi != ELFOSABI_AMDGPU_HSA)
4556 {
4557 /* For HSA v3 and other OS ABIs. */
4558 if (e_flags & EF_AMDGPU_FEATURE_XNACK_V3)
4559 {
f8c4789c 4560 out = stpcpy (out, ", xnack on");
c077c580
SM
4561 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V3;
4562 }
4563
4564 if (e_flags & EF_AMDGPU_FEATURE_SRAMECC_V3)
4565 {
f8c4789c 4566 out = stpcpy (out, ", sramecc on");
c077c580
SM
4567 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V3;
4568 }
4569 }
4570 else
4571 {
4572 /* For HSA v4+. */
4573 int xnack, sramecc;
4574
4575 xnack = e_flags & EF_AMDGPU_FEATURE_XNACK_V4;
4576 switch (xnack)
4577 {
4578 case EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4:
4579 break;
4580
4581 case EF_AMDGPU_FEATURE_XNACK_ANY_V4:
f8c4789c 4582 out = stpcpy (out, ", xnack any");
c077c580
SM
4583 break;
4584
4585 case EF_AMDGPU_FEATURE_XNACK_OFF_V4:
f8c4789c 4586 out = stpcpy (out, ", xnack off");
c077c580
SM
4587 break;
4588
4589 case EF_AMDGPU_FEATURE_XNACK_ON_V4:
f8c4789c 4590 out = stpcpy (out, ", xnack on");
c077c580
SM
4591 break;
4592
4593 default:
f8c4789c 4594 out += sprintf (out, _(", <unknown xnack value: %#x>"), xnack);
c077c580
SM
4595 break;
4596 }
4597
c077c580
SM
4598 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V4;
4599
4600 sramecc = e_flags & EF_AMDGPU_FEATURE_SRAMECC_V4;
4601 switch (sramecc)
4602 {
4603 case EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4:
4604 break;
4605
4606 case EF_AMDGPU_FEATURE_SRAMECC_ANY_V4:
f8c4789c 4607 out = stpcpy (out, ", sramecc any");
c077c580
SM
4608 break;
4609
4610 case EF_AMDGPU_FEATURE_SRAMECC_OFF_V4:
f8c4789c 4611 out = stpcpy (out, ", sramecc off");
c077c580
SM
4612 break;
4613
4614 case EF_AMDGPU_FEATURE_SRAMECC_ON_V4:
f8c4789c 4615 out = stpcpy (out, ", sramecc on");
c077c580
SM
4616 break;
4617
4618 default:
f8c4789c 4619 out += sprintf (out, _(", <unknown sramecc value: %#x>"), sramecc);
c077c580
SM
4620 break;
4621 }
4622
c077c580
SM
4623 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V4;
4624 }
4625
4626 if (e_flags != 0)
f8c4789c
AM
4627 out += sprintf (out, _(", unknown flags bits: %#x"), e_flags);
4628 return out;
c077c580
SM
4629}
4630
252b5132 4631static char *
dda8d76d 4632get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
252b5132 4633{
b34976b6 4634 static char buf[1024];
f8c4789c 4635 char *out = buf;
252b5132
RH
4636
4637 buf[0] = '\0';
76da6bbe 4638
252b5132
RH
4639 if (e_flags)
4640 {
4641 switch (e_machine)
4642 {
4643 default:
4644 break;
4645
b5c37946 4646 case EM_ARC_COMPACT3:
f8c4789c 4647 out = stpcpy (out, ", HS5x");
b5c37946
SJ
4648 break;
4649
4650 case EM_ARC_COMPACT3_64:
f8c4789c 4651 out = stpcpy (out, ", HS6x");
b5c37946
SJ
4652 break;
4653
886a2506 4654 case EM_ARC_COMPACT2:
886a2506 4655 case EM_ARC_COMPACT:
f8c4789c
AM
4656 out = decode_ARC_machine_flags (out, e_flags, e_machine);
4657 break;
886a2506 4658
f3485b74 4659 case EM_ARM:
f8c4789c 4660 out = decode_ARM_machine_flags (out, e_flags);
f3485b74 4661 break;
76da6bbe 4662
f8c4789c
AM
4663 case EM_AVR:
4664 out = decode_AVR_machine_flags (out, e_flags);
4665 break;
343433df 4666
781303ce 4667 case EM_BLACKFIN:
f8c4789c 4668 out = decode_BLACKFIN_machine_flags (out, e_flags);
781303ce
MF
4669 break;
4670
ec2dfb42 4671 case EM_CYGNUS_FRV:
f8c4789c 4672 out = decode_FRV_machine_flags (out, e_flags);
1c877e87 4673 break;
ec2dfb42 4674
53c7db4b 4675 case EM_68K:
f8c4789c 4676 out = decode_M68K_machine_flags (out, e_flags);
53c7db4b 4677 break;
33c63f9d 4678
c077c580 4679 case EM_AMDGPU:
f8c4789c 4680 out = decode_AMDGPU_machine_flags (out, e_flags, filedata);
c077c580
SM
4681 break;
4682
153a2776 4683 case EM_CYGNUS_MEP:
f8c4789c 4684 out = decode_MeP_machine_flags (out, e_flags);
153a2776
NC
4685 break;
4686
252b5132
RH
4687 case EM_PPC:
4688 if (e_flags & EF_PPC_EMB)
f8c4789c 4689 out = stpcpy (out, ", emb");
252b5132
RH
4690
4691 if (e_flags & EF_PPC_RELOCATABLE)
f8c4789c 4692 out = stpcpy (out, _(", relocatable"));
252b5132
RH
4693
4694 if (e_flags & EF_PPC_RELOCATABLE_LIB)
f8c4789c 4695 out = stpcpy (out, _(", relocatable-lib"));
252b5132
RH
4696 break;
4697
ee67d69a
AM
4698 case EM_PPC64:
4699 if (e_flags & EF_PPC64_ABI)
f8c4789c 4700 out += sprintf (out, ", abiv%d", e_flags & EF_PPC64_ABI);
ee67d69a
AM
4701 break;
4702
708e2187 4703 case EM_V800:
f8c4789c 4704 out = decode_V800_machine_flags (out, e_flags);
708e2187
NC
4705 break;
4706
2b0337b0 4707 case EM_V850:
252b5132 4708 case EM_CYGNUS_V850:
f8c4789c 4709 out = decode_V850_machine_flags (out, e_flags);
252b5132
RH
4710 break;
4711
2b0337b0 4712 case EM_M32R:
252b5132
RH
4713 case EM_CYGNUS_M32R:
4714 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
f8c4789c 4715 out = stpcpy (out, ", m32r");
252b5132
RH
4716 break;
4717
4718 case EM_MIPS:
4fe85591 4719 case EM_MIPS_RS3_LE:
f8c4789c 4720 out = decode_MIPS_machine_flags (out, e_flags);
252b5132 4721 break;
351b4b40 4722
35c08157 4723 case EM_NDS32:
f8c4789c 4724 out = decode_NDS32_machine_flags (out, e_flags);
35c08157
KLC
4725 break;
4726
fe944acf
FT
4727 case EM_NFP:
4728 switch (EF_NFP_MACH (e_flags))
4729 {
4730 case E_NFP_MACH_3200:
f8c4789c 4731 out = stpcpy (out, ", NFP-32xx");
fe944acf
FT
4732 break;
4733 case E_NFP_MACH_6000:
f8c4789c 4734 out = stpcpy (out, ", NFP-6xxx");
fe944acf
FT
4735 break;
4736 }
4737 break;
4738
e23eba97 4739 case EM_RISCV:
f8c4789c 4740 out = decode_RISCV_machine_flags (out, e_flags);
e23eba97
NC
4741 break;
4742
ccde1100 4743 case EM_SH:
f8c4789c 4744 out = decode_SH_machine_flags (out, e_flags);
ccde1100 4745 break;
948f632f 4746
f8c4789c
AM
4747 case EM_OR1K:
4748 if (e_flags & EF_OR1K_NODELAY)
4749 out = stpcpy (out, ", no delay");
4750 break;
57346661 4751
f8c4789c
AM
4752 case EM_BPF:
4753 out += sprintf (out, ", CPU Version: %u", e_flags & EF_BPF_CPUVER);
4754 break;
b5c37946 4755
351b4b40 4756 case EM_SPARCV9:
f8c4789c 4757 out = decode_SPARC_machine_flags (out, e_flags);
351b4b40 4758 break;
7d466069 4759
103f02d3 4760 case EM_PARISC:
f8c4789c 4761 out = decode_PARISC_machine_flags (out, e_flags);
30800947 4762 break;
76da6bbe 4763
7d466069 4764 case EM_PJ:
2b0337b0 4765 case EM_PJ_OLD:
7d466069 4766 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
f8c4789c 4767 out = stpcpy (out, ", new calling convention");
7d466069
ILT
4768
4769 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
f8c4789c 4770 out = stpcpy (out, ", gnu calling convention");
7d466069 4771 break;
4d6ed7c8
NC
4772
4773 case EM_IA_64:
f8c4789c 4774 out = decode_IA64_machine_flags (out, e_flags, filedata);
4d6ed7c8 4775 break;
179d3252
JT
4776
4777 case EM_VAX:
4778 if ((e_flags & EF_VAX_NONPIC))
f8c4789c 4779 out = stpcpy (out, ", non-PIC");
179d3252 4780 if ((e_flags & EF_VAX_DFLOAT))
f8c4789c 4781 out = stpcpy (out, ", D-Float");
179d3252 4782 if ((e_flags & EF_VAX_GFLOAT))
f8c4789c 4783 out = stpcpy (out, ", G-Float");
179d3252 4784 break;
c7927a3c 4785
f8c4789c 4786 case EM_VISIUM:
619ed720 4787 if (e_flags & EF_VISIUM_ARCH_MCM)
f8c4789c 4788 out = stpcpy (out, ", mcm");
619ed720 4789 else if (e_flags & EF_VISIUM_ARCH_MCM24)
f8c4789c 4790 out = stpcpy (out, ", mcm24");
619ed720 4791 if (e_flags & EF_VISIUM_ARCH_GR6)
f8c4789c 4792 out = stpcpy (out, ", gr6");
619ed720
EB
4793 break;
4794
4046d87a 4795 case EM_RL78:
f8c4789c 4796 out = decode_RL78_machine_flags (out, e_flags);
4046d87a 4797 break;
0b4362b0 4798
c7927a3c 4799 case EM_RX:
f8c4789c 4800 out = decode_RX_machine_flags (out, e_flags);
d4cb0ea0 4801 break;
55786da2
AK
4802
4803 case EM_S390:
4804 if (e_flags & EF_S390_HIGH_GPRS)
f8c4789c 4805 out = stpcpy (out, ", highgprs");
d4cb0ea0 4806 break;
40b36596
JM
4807
4808 case EM_TI_C6000:
4809 if ((e_flags & EF_C6000_REL))
f8c4789c 4810 out = stpcpy (out, ", relocatable module");
d4cb0ea0 4811 break;
13761a11 4812
6e712424
PI
4813 case EM_KVX:
4814 if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV3_1)
4815 strcat (buf, ", Kalray VLIW kv3-1");
4816 else if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV3_2)
4817 strcat (buf, ", Kalray VLIW kv3-2");
4818 else if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV4_1)
4819 strcat (buf, ", Kalray VLIW kv4-1");
4820 else
4821 strcat (buf, ", unknown KVX MPPA");
4822 break;
4823
13761a11 4824 case EM_MSP430:
f8c4789c 4825 out = decode_MSP430_machine_flags (out, e_flags);
6655dba2
SB
4826 break;
4827
4828 case EM_Z80:
f8c4789c 4829 out = decode_Z80_machine_flags (out, e_flags);
6655dba2 4830 break;
c4a7e6b5 4831
f8c4789c
AM
4832 case EM_LOONGARCH:
4833 out = decode_LOONGARCH_machine_flags (out, e_flags);
e9a0721f 4834 break;
252b5132
RH
4835 }
4836 }
4837
4838 return buf;
4839}
4840
252b5132 4841static const char *
dda8d76d 4842get_osabi_name (Filedata * filedata, unsigned int osabi)
d3ba0551
AM
4843{
4844 static char buff[32];
4845
4846 switch (osabi)
4847 {
4848 case ELFOSABI_NONE: return "UNIX - System V";
4849 case ELFOSABI_HPUX: return "UNIX - HP-UX";
4850 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
9c55345c 4851 case ELFOSABI_GNU: return "UNIX - GNU";
d3ba0551
AM
4852 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
4853 case ELFOSABI_AIX: return "UNIX - AIX";
4854 case ELFOSABI_IRIX: return "UNIX - IRIX";
4855 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
4856 case ELFOSABI_TRU64: return "UNIX - TRU64";
4857 case ELFOSABI_MODESTO: return "Novell - Modesto";
4858 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
4859 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
4860 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
3b26c801 4861 case ELFOSABI_AROS: return "AROS";
11636f9e 4862 case ELFOSABI_FENIXOS: return "FenixOS";
6d913794
NC
4863 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
4864 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
d3ba0551 4865 default:
40b36596 4866 if (osabi >= 64)
dda8d76d 4867 switch (filedata->file_header.e_machine)
40b36596 4868 {
37870be8
SM
4869 case EM_AMDGPU:
4870 switch (osabi)
4871 {
4872 case ELFOSABI_AMDGPU_HSA: return "AMD HSA";
4873 case ELFOSABI_AMDGPU_PAL: return "AMD PAL";
4874 case ELFOSABI_AMDGPU_MESA3D: return "AMD Mesa3D";
4875 default:
4876 break;
4877 }
4878 break;
4879
40b36596
JM
4880 case EM_ARM:
4881 switch (osabi)
4882 {
4883 case ELFOSABI_ARM: return "ARM";
18a20338 4884 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
40b36596
JM
4885 default:
4886 break;
4887 }
4888 break;
4889
4890 case EM_MSP430:
4891 case EM_MSP430_OLD:
619ed720 4892 case EM_VISIUM:
40b36596
JM
4893 switch (osabi)
4894 {
4895 case ELFOSABI_STANDALONE: return _("Standalone App");
4896 default:
4897 break;
4898 }
4899 break;
4900
4901 case EM_TI_C6000:
4902 switch (osabi)
4903 {
4904 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
4905 case ELFOSABI_C6000_LINUX: return "Linux C6000";
4906 default:
4907 break;
4908 }
4909 break;
4910
4911 default:
4912 break;
4913 }
e9e44622 4914 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
d3ba0551
AM
4915 return buff;
4916 }
4917}
4918
a06ea964
NC
4919static const char *
4920get_aarch64_segment_type (unsigned long type)
4921{
4922 switch (type)
4923 {
32ec8896 4924 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
d0ff5ca9 4925 case PT_AARCH64_MEMTAG_MTE: return "AARCH64_MEMTAG_MTE";
32ec8896 4926 default: return NULL;
a06ea964 4927 }
a06ea964
NC
4928}
4929
b294bdf8
MM
4930static const char *
4931get_arm_segment_type (unsigned long type)
4932{
4933 switch (type)
4934 {
32ec8896
NC
4935 case PT_ARM_EXIDX: return "EXIDX";
4936 default: return NULL;
b294bdf8 4937 }
b294bdf8
MM
4938}
4939
b4cbbe8f
AK
4940static const char *
4941get_s390_segment_type (unsigned long type)
4942{
4943 switch (type)
4944 {
4945 case PT_S390_PGSTE: return "S390_PGSTE";
4946 default: return NULL;
4947 }
4948}
4949
d3ba0551
AM
4950static const char *
4951get_mips_segment_type (unsigned long type)
252b5132
RH
4952{
4953 switch (type)
4954 {
32ec8896
NC
4955 case PT_MIPS_REGINFO: return "REGINFO";
4956 case PT_MIPS_RTPROC: return "RTPROC";
4957 case PT_MIPS_OPTIONS: return "OPTIONS";
4958 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
4959 default: return NULL;
252b5132 4960 }
252b5132
RH
4961}
4962
103f02d3 4963static const char *
d3ba0551 4964get_parisc_segment_type (unsigned long type)
103f02d3
UD
4965{
4966 switch (type)
4967 {
103f02d3
UD
4968 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
4969 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
61472819 4970 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
32ec8896 4971 default: return NULL;
103f02d3 4972 }
103f02d3
UD
4973}
4974
4d6ed7c8 4975static const char *
d3ba0551 4976get_ia64_segment_type (unsigned long type)
4d6ed7c8
NC
4977{
4978 switch (type)
4979 {
4980 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
4981 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
32ec8896 4982 default: return NULL;
4d6ed7c8 4983 }
4d6ed7c8
NC
4984}
4985
40b36596
JM
4986static const char *
4987get_tic6x_segment_type (unsigned long type)
4988{
4989 switch (type)
4990 {
32ec8896
NC
4991 case PT_C6000_PHATTR: return "C6000_PHATTR";
4992 default: return NULL;
40b36596 4993 }
40b36596
JM
4994}
4995
fbc95f1e
KC
4996static const char *
4997get_riscv_segment_type (unsigned long type)
4998{
4999 switch (type)
5000 {
5001 case PT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
5002 default: return NULL;
5003 }
5004}
5005
df3a023b
AM
5006static const char *
5007get_hpux_segment_type (unsigned long type, unsigned e_machine)
5008{
5009 if (e_machine == EM_PARISC)
5010 switch (type)
5011 {
5012 case PT_HP_TLS: return "HP_TLS";
5013 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
5014 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
5015 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
5016 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
5017 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
5018 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
5019 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
5020 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
5021 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
5022 case PT_HP_PARALLEL: return "HP_PARALLEL";
5023 case PT_HP_FASTBIND: return "HP_FASTBIND";
5024 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
5025 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
5026 case PT_HP_STACK: return "HP_STACK";
5027 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
5028 default: return NULL;
5029 }
5030
5031 if (e_machine == EM_IA_64)
5032 switch (type)
5033 {
5034 case PT_HP_TLS: return "HP_TLS";
5035 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
5036 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
5037 case PT_IA_64_HP_STACK: return "HP_STACK";
5038 default: return NULL;
5039 }
5040
5041 return NULL;
5042}
5043
5522f910
NC
5044static const char *
5045get_solaris_segment_type (unsigned long type)
5046{
5047 switch (type)
5048 {
5049 case 0x6464e550: return "PT_SUNW_UNWIND";
5050 case 0x6474e550: return "PT_SUNW_EH_FRAME";
5051 case 0x6ffffff7: return "PT_LOSUNW";
5052 case 0x6ffffffa: return "PT_SUNWBSS";
5053 case 0x6ffffffb: return "PT_SUNWSTACK";
5054 case 0x6ffffffc: return "PT_SUNWDTRACE";
5055 case 0x6ffffffd: return "PT_SUNWCAP";
5056 case 0x6fffffff: return "PT_HISUNW";
32ec8896 5057 default: return NULL;
5522f910
NC
5058 }
5059}
5060
252b5132 5061static const char *
dda8d76d 5062get_segment_type (Filedata * filedata, unsigned long p_type)
252b5132 5063{
b34976b6 5064 static char buff[32];
252b5132
RH
5065
5066 switch (p_type)
5067 {
b34976b6
AM
5068 case PT_NULL: return "NULL";
5069 case PT_LOAD: return "LOAD";
252b5132 5070 case PT_DYNAMIC: return "DYNAMIC";
b34976b6
AM
5071 case PT_INTERP: return "INTERP";
5072 case PT_NOTE: return "NOTE";
5073 case PT_SHLIB: return "SHLIB";
5074 case PT_PHDR: return "PHDR";
13ae64f3 5075 case PT_TLS: return "TLS";
32ec8896 5076 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
2b05f1b7 5077 case PT_GNU_STACK: return "GNU_STACK";
8c37241b 5078 case PT_GNU_RELRO: return "GNU_RELRO";
0a59decb 5079 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
cf0e0a0b 5080 case PT_GNU_SFRAME: return "GNU_SFRAME";
65765700 5081
80251d41 5082 case PT_OPENBSD_MUTABLE: return "OPENBSD_MUTABLE";
3eba3ef3
NC
5083 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
5084 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
5085 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
b9e920ec 5086
252b5132 5087 default:
df3a023b 5088 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
252b5132 5089 {
2cf0635d 5090 const char * result;
103f02d3 5091
dda8d76d 5092 switch (filedata->file_header.e_machine)
252b5132 5093 {
a06ea964
NC
5094 case EM_AARCH64:
5095 result = get_aarch64_segment_type (p_type);
5096 break;
b294bdf8
MM
5097 case EM_ARM:
5098 result = get_arm_segment_type (p_type);
5099 break;
252b5132 5100 case EM_MIPS:
4fe85591 5101 case EM_MIPS_RS3_LE:
252b5132
RH
5102 result = get_mips_segment_type (p_type);
5103 break;
103f02d3
UD
5104 case EM_PARISC:
5105 result = get_parisc_segment_type (p_type);
5106 break;
4d6ed7c8
NC
5107 case EM_IA_64:
5108 result = get_ia64_segment_type (p_type);
5109 break;
40b36596
JM
5110 case EM_TI_C6000:
5111 result = get_tic6x_segment_type (p_type);
5112 break;
b4cbbe8f
AK
5113 case EM_S390:
5114 case EM_S390_OLD:
5115 result = get_s390_segment_type (p_type);
5116 break;
fbc95f1e
KC
5117 case EM_RISCV:
5118 result = get_riscv_segment_type (p_type);
5119 break;
252b5132
RH
5120 default:
5121 result = NULL;
5122 break;
5123 }
103f02d3 5124
252b5132
RH
5125 if (result != NULL)
5126 return result;
103f02d3 5127
1a9ccd70 5128 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
252b5132
RH
5129 }
5130 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
103f02d3 5131 {
df3a023b 5132 const char * result = NULL;
103f02d3 5133
df3a023b 5134 switch (filedata->file_header.e_ident[EI_OSABI])
103f02d3 5135 {
df3a023b
AM
5136 case ELFOSABI_GNU:
5137 case ELFOSABI_FREEBSD:
5138 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
5139 {
5140 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
5141 result = buff;
5142 }
103f02d3 5143 break;
df3a023b
AM
5144 case ELFOSABI_HPUX:
5145 result = get_hpux_segment_type (p_type,
5146 filedata->file_header.e_machine);
5147 break;
5148 case ELFOSABI_SOLARIS:
5149 result = get_solaris_segment_type (p_type);
00428cca 5150 break;
103f02d3 5151 default:
103f02d3
UD
5152 break;
5153 }
103f02d3
UD
5154 if (result != NULL)
5155 return result;
5156
1a9ccd70 5157 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
103f02d3 5158 }
252b5132 5159 else
e9e44622 5160 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
252b5132
RH
5161
5162 return buff;
5163 }
5164}
5165
53a346d8
CZ
5166static const char *
5167get_arc_section_type_name (unsigned int sh_type)
5168{
5169 switch (sh_type)
5170 {
5171 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
5172 default:
5173 break;
5174 }
5175 return NULL;
5176}
5177
252b5132 5178static const char *
d3ba0551 5179get_mips_section_type_name (unsigned int sh_type)
252b5132
RH
5180{
5181 switch (sh_type)
5182 {
b34976b6
AM
5183 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
5184 case SHT_MIPS_MSYM: return "MIPS_MSYM";
5185 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
5186 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
5187 case SHT_MIPS_UCODE: return "MIPS_UCODE";
5188 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
5189 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
5190 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
5191 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
5192 case SHT_MIPS_RELD: return "MIPS_RELD";
5193 case SHT_MIPS_IFACE: return "MIPS_IFACE";
5194 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
5195 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
5196 case SHT_MIPS_SHDR: return "MIPS_SHDR";
5197 case SHT_MIPS_FDESC: return "MIPS_FDESC";
5198 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
5199 case SHT_MIPS_DENSE: return "MIPS_DENSE";
5200 case SHT_MIPS_PDESC: return "MIPS_PDESC";
5201 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
5202 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
5203 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
5204 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
5205 case SHT_MIPS_LINE: return "MIPS_LINE";
5206 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
5207 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
5208 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
5209 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
5210 case SHT_MIPS_DWARF: return "MIPS_DWARF";
5211 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
5212 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
5213 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
5214 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
5215 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
5216 case SHT_MIPS_XLATE: return "MIPS_XLATE";
5217 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
5218 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
5219 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
5220 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
252b5132 5221 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
351cdf24 5222 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
f16a9783 5223 case SHT_MIPS_XHASH: return "MIPS_XHASH";
252b5132
RH
5224 default:
5225 break;
5226 }
5227 return NULL;
5228}
5229
103f02d3 5230static const char *
d3ba0551 5231get_parisc_section_type_name (unsigned int sh_type)
103f02d3
UD
5232{
5233 switch (sh_type)
5234 {
5235 case SHT_PARISC_EXT: return "PARISC_EXT";
5236 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
5237 case SHT_PARISC_DOC: return "PARISC_DOC";
eec8f817
DA
5238 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
5239 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
5240 case SHT_PARISC_STUBS: return "PARISC_STUBS";
61472819 5241 case SHT_PARISC_DLKM: return "PARISC_DLKM";
32ec8896 5242 default: return NULL;
103f02d3 5243 }
103f02d3
UD
5244}
5245
4d6ed7c8 5246static const char *
dda8d76d 5247get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4d6ed7c8 5248{
18bd398b 5249 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
ecc51f48 5250 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
dda8d76d 5251 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
0de14b54 5252
4d6ed7c8
NC
5253 switch (sh_type)
5254 {
148b93f2
NC
5255 case SHT_IA_64_EXT: return "IA_64_EXT";
5256 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
5257 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
5258 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
5259 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
5260 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
5261 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
5262 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
5263 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
5264 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
4d6ed7c8
NC
5265 default:
5266 break;
5267 }
5268 return NULL;
5269}
5270
d2b2c203
DJ
5271static const char *
5272get_x86_64_section_type_name (unsigned int sh_type)
5273{
5274 switch (sh_type)
5275 {
5276 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
32ec8896 5277 default: return NULL;
d2b2c203 5278 }
d2b2c203
DJ
5279}
5280
a06ea964
NC
5281static const char *
5282get_aarch64_section_type_name (unsigned int sh_type)
5283{
5284 switch (sh_type)
5285 {
32ec8896
NC
5286 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
5287 default: return NULL;
a06ea964 5288 }
a06ea964
NC
5289}
5290
40a18ebd
NC
5291static const char *
5292get_arm_section_type_name (unsigned int sh_type)
5293{
5294 switch (sh_type)
5295 {
7f6fed87
NC
5296 case SHT_ARM_EXIDX: return "ARM_EXIDX";
5297 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
5298 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
5299 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
5300 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
32ec8896 5301 default: return NULL;
40a18ebd 5302 }
40a18ebd
NC
5303}
5304
40b36596
JM
5305static const char *
5306get_tic6x_section_type_name (unsigned int sh_type)
5307{
5308 switch (sh_type)
5309 {
32ec8896
NC
5310 case SHT_C6000_UNWIND: return "C6000_UNWIND";
5311 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
5312 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
5313 case SHT_TI_ICODE: return "TI_ICODE";
5314 case SHT_TI_XREF: return "TI_XREF";
5315 case SHT_TI_HANDLER: return "TI_HANDLER";
5316 case SHT_TI_INITINFO: return "TI_INITINFO";
5317 case SHT_TI_PHATTRS: return "TI_PHATTRS";
5318 default: return NULL;
40b36596 5319 }
40b36596
JM
5320}
5321
13761a11 5322static const char *
b0191216 5323get_msp430_section_type_name (unsigned int sh_type)
13761a11
NC
5324{
5325 switch (sh_type)
5326 {
32ec8896
NC
5327 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
5328 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
5329 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
5330 default: return NULL;
13761a11
NC
5331 }
5332}
5333
fe944acf
FT
5334static const char *
5335get_nfp_section_type_name (unsigned int sh_type)
5336{
5337 switch (sh_type)
5338 {
5339 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
5340 case SHT_NFP_INITREG: return "NFP_INITREG";
5341 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
5342 default: return NULL;
5343 }
5344}
5345
685080f2
NC
5346static const char *
5347get_v850_section_type_name (unsigned int sh_type)
5348{
5349 switch (sh_type)
5350 {
32ec8896
NC
5351 case SHT_V850_SCOMMON: return "V850 Small Common";
5352 case SHT_V850_TCOMMON: return "V850 Tiny Common";
5353 case SHT_V850_ZCOMMON: return "V850 Zero Common";
5354 case SHT_RENESAS_IOP: return "RENESAS IOP";
5355 case SHT_RENESAS_INFO: return "RENESAS INFO";
5356 default: return NULL;
685080f2
NC
5357 }
5358}
5359
2dc8dd17
JW
5360static const char *
5361get_riscv_section_type_name (unsigned int sh_type)
5362{
5363 switch (sh_type)
5364 {
5365 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
5366 default: return NULL;
5367 }
5368}
5369
0861f561
CQ
5370static const char *
5371get_csky_section_type_name (unsigned int sh_type)
5372{
5373 switch (sh_type)
5374 {
5375 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
5376 default: return NULL;
5377 }
5378}
5379
252b5132 5380static const char *
dda8d76d 5381get_section_type_name (Filedata * filedata, unsigned int sh_type)
252b5132 5382{
b34976b6 5383 static char buff[32];
9fb71ee4 5384 const char * result;
252b5132
RH
5385
5386 switch (sh_type)
5387 {
5388 case SHT_NULL: return "NULL";
5389 case SHT_PROGBITS: return "PROGBITS";
5390 case SHT_SYMTAB: return "SYMTAB";
5391 case SHT_STRTAB: return "STRTAB";
5392 case SHT_RELA: return "RELA";
dd207c13 5393 case SHT_RELR: return "RELR";
252b5132
RH
5394 case SHT_HASH: return "HASH";
5395 case SHT_DYNAMIC: return "DYNAMIC";
5396 case SHT_NOTE: return "NOTE";
5397 case SHT_NOBITS: return "NOBITS";
5398 case SHT_REL: return "REL";
5399 case SHT_SHLIB: return "SHLIB";
5400 case SHT_DYNSYM: return "DYNSYM";
d1133906
NC
5401 case SHT_INIT_ARRAY: return "INIT_ARRAY";
5402 case SHT_FINI_ARRAY: return "FINI_ARRAY";
5403 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
fdc90cb4 5404 case SHT_GNU_HASH: return "GNU_HASH";
93ebe586 5405 case SHT_GROUP: return "GROUP";
67ce483b 5406 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
252b5132
RH
5407 case SHT_GNU_verdef: return "VERDEF";
5408 case SHT_GNU_verneed: return "VERNEED";
5409 case SHT_GNU_versym: return "VERSYM";
b34976b6
AM
5410 case 0x6ffffff0: return "VERSYM";
5411 case 0x6ffffffc: return "VERDEF";
252b5132
RH
5412 case 0x7ffffffd: return "AUXILIARY";
5413 case 0x7fffffff: return "FILTER";
047b2264 5414 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
252b5132
RH
5415
5416 default:
5417 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
5418 {
dda8d76d 5419 switch (filedata->file_header.e_machine)
252b5132 5420 {
53a346d8
CZ
5421 case EM_ARC:
5422 case EM_ARC_COMPACT:
5423 case EM_ARC_COMPACT2:
b5c37946
SJ
5424 case EM_ARC_COMPACT3:
5425 case EM_ARC_COMPACT3_64:
53a346d8
CZ
5426 result = get_arc_section_type_name (sh_type);
5427 break;
252b5132 5428 case EM_MIPS:
4fe85591 5429 case EM_MIPS_RS3_LE:
252b5132
RH
5430 result = get_mips_section_type_name (sh_type);
5431 break;
103f02d3
UD
5432 case EM_PARISC:
5433 result = get_parisc_section_type_name (sh_type);
5434 break;
4d6ed7c8 5435 case EM_IA_64:
dda8d76d 5436 result = get_ia64_section_type_name (filedata, sh_type);
4d6ed7c8 5437 break;
d2b2c203 5438 case EM_X86_64:
8a9036a4 5439 case EM_L1OM:
7a9068fe 5440 case EM_K1OM:
d2b2c203
DJ
5441 result = get_x86_64_section_type_name (sh_type);
5442 break;
a06ea964
NC
5443 case EM_AARCH64:
5444 result = get_aarch64_section_type_name (sh_type);
5445 break;
40a18ebd
NC
5446 case EM_ARM:
5447 result = get_arm_section_type_name (sh_type);
5448 break;
40b36596
JM
5449 case EM_TI_C6000:
5450 result = get_tic6x_section_type_name (sh_type);
5451 break;
13761a11 5452 case EM_MSP430:
b0191216 5453 result = get_msp430_section_type_name (sh_type);
13761a11 5454 break;
fe944acf
FT
5455 case EM_NFP:
5456 result = get_nfp_section_type_name (sh_type);
5457 break;
685080f2
NC
5458 case EM_V800:
5459 case EM_V850:
5460 case EM_CYGNUS_V850:
5461 result = get_v850_section_type_name (sh_type);
5462 break;
2dc8dd17
JW
5463 case EM_RISCV:
5464 result = get_riscv_section_type_name (sh_type);
5465 break;
0861f561
CQ
5466 case EM_CSKY:
5467 result = get_csky_section_type_name (sh_type);
5468 break;
252b5132
RH
5469 default:
5470 result = NULL;
5471 break;
5472 }
5473
5474 if (result != NULL)
5475 return result;
5476
9fb71ee4 5477 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
252b5132
RH
5478 }
5479 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
148b93f2 5480 {
dda8d76d 5481 switch (filedata->file_header.e_machine)
148b93f2
NC
5482 {
5483 case EM_IA_64:
dda8d76d 5484 result = get_ia64_section_type_name (filedata, sh_type);
148b93f2
NC
5485 break;
5486 default:
dda8d76d 5487 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
fd85a6a1
NC
5488 result = get_solaris_section_type (sh_type);
5489 else
1b4b80bf
NC
5490 {
5491 switch (sh_type)
5492 {
5493 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
5494 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
5495 case SHT_GNU_HASH: result = "GNU_HASH"; break;
5496 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
5497 default:
5498 result = NULL;
5499 break;
5500 }
5501 }
148b93f2
NC
5502 break;
5503 }
5504
5505 if (result != NULL)
5506 return result;
5507
9fb71ee4 5508 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
148b93f2 5509 }
252b5132 5510 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
685080f2 5511 {
dda8d76d 5512 switch (filedata->file_header.e_machine)
685080f2
NC
5513 {
5514 case EM_V800:
5515 case EM_V850:
5516 case EM_CYGNUS_V850:
9fb71ee4 5517 result = get_v850_section_type_name (sh_type);
a9fb83be 5518 break;
685080f2 5519 default:
9fb71ee4 5520 result = NULL;
685080f2
NC
5521 break;
5522 }
5523
9fb71ee4
NC
5524 if (result != NULL)
5525 return result;
5526
5527 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
685080f2 5528 }
252b5132 5529 else
a7dbfd1c
NC
5530 /* This message is probably going to be displayed in a 15
5531 character wide field, so put the hex value first. */
5532 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
103f02d3 5533
252b5132
RH
5534 return buff;
5535 }
5536}
5537
79bc120c
NC
5538enum long_option_values
5539{
5540 OPTION_DEBUG_DUMP = 512,
5541 OPTION_DYN_SYMS,
0f03783c 5542 OPTION_LTO_SYMS,
79bc120c
NC
5543 OPTION_DWARF_DEPTH,
5544 OPTION_DWARF_START,
5545 OPTION_DWARF_CHECK,
5546 OPTION_CTF_DUMP,
5547 OPTION_CTF_PARENT,
5548 OPTION_CTF_SYMBOLS,
5549 OPTION_CTF_STRINGS,
42b6953b 5550 OPTION_SFRAME_DUMP,
79bc120c
NC
5551 OPTION_WITH_SYMBOL_VERSIONS,
5552 OPTION_RECURSE_LIMIT,
5553 OPTION_NO_RECURSE_LIMIT,
047c3dbf 5554 OPTION_NO_DEMANGLING,
b6ac461a 5555 OPTION_NO_EXTRA_SYM_INFO,
047c3dbf 5556 OPTION_SYM_BASE
79bc120c 5557};
2979dc34 5558
85b1c36d 5559static struct option options[] =
252b5132 5560{
79bc120c
NC
5561 /* Note - This table is alpha-sorted on the 'val'
5562 field in order to make adding new options easier. */
5563 {"arch-specific", no_argument, 0, 'A'},
b34976b6 5564 {"all", no_argument, 0, 'a'},
79bc120c
NC
5565 {"demangle", optional_argument, 0, 'C'},
5566 {"archive-index", no_argument, 0, 'c'},
5567 {"use-dynamic", no_argument, 0, 'D'},
5568 {"dynamic", no_argument, 0, 'd'},
b34976b6 5569 {"headers", no_argument, 0, 'e'},
79bc120c
NC
5570 {"section-groups", no_argument, 0, 'g'},
5571 {"help", no_argument, 0, 'H'},
5572 {"file-header", no_argument, 0, 'h'},
b34976b6 5573 {"histogram", no_argument, 0, 'I'},
79bc120c
NC
5574 {"lint", no_argument, 0, 'L'},
5575 {"enable-checks", no_argument, 0, 'L'},
5576 {"program-headers", no_argument, 0, 'l'},
b34976b6 5577 {"segments", no_argument, 0, 'l'},
595cf52e 5578 {"full-section-name",no_argument, 0, 'N'},
79bc120c 5579 {"notes", no_argument, 0, 'n'},
ca0e11aa 5580 {"process-links", no_argument, 0, 'P'},
79bc120c
NC
5581 {"string-dump", required_argument, 0, 'p'},
5582 {"relocated-dump", required_argument, 0, 'R'},
5583 {"relocs", no_argument, 0, 'r'},
5584 {"section-headers", no_argument, 0, 'S'},
5585 {"sections", no_argument, 0, 'S'},
b34976b6
AM
5586 {"symbols", no_argument, 0, 's'},
5587 {"syms", no_argument, 0, 's'},
79bc120c
NC
5588 {"silent-truncation",no_argument, 0, 'T'},
5589 {"section-details", no_argument, 0, 't'},
b3aa80b4 5590 {"unicode", required_argument, NULL, 'U'},
09c11c86 5591 {"unwind", no_argument, 0, 'u'},
79bc120c
NC
5592 {"version-info", no_argument, 0, 'V'},
5593 {"version", no_argument, 0, 'v'},
5594 {"wide", no_argument, 0, 'W'},
b6ac461a 5595 {"extra-sym-info", no_argument, 0, 'X'},
b34976b6 5596 {"hex-dump", required_argument, 0, 'x'},
0e602686 5597 {"decompress", no_argument, 0, 'z'},
252b5132 5598
79bc120c 5599 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
b6ac461a 5600 {"no-extra-sym-info",no_argument, 0, OPTION_NO_EXTRA_SYM_INFO},
79bc120c
NC
5601 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
5602 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5603 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5604 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
0f03783c 5605 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
79bc120c 5606 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
fd2f0033
TT
5607 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
5608 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
4723351a 5609 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
094e34f2 5610#ifdef ENABLE_LIBCTF
d344b407 5611 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
7d9813f1
NA
5612 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
5613 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
5614 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
094e34f2 5615#endif
42b6953b 5616 {"sframe", optional_argument, 0, OPTION_SFRAME_DUMP},
047c3dbf 5617 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
7d9813f1 5618
b34976b6 5619 {0, no_argument, 0, 0}
252b5132
RH
5620};
5621
5622static void
2cf0635d 5623usage (FILE * stream)
252b5132 5624{
92f01d61
JM
5625 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
5626 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
d6249f5f
AM
5627 fprintf (stream, _(" Options are:\n"));
5628 fprintf (stream, _("\
5629 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
5630 fprintf (stream, _("\
5631 -h --file-header Display the ELF file header\n"));
5632 fprintf (stream, _("\
5633 -l --program-headers Display the program headers\n"));
5634 fprintf (stream, _("\
5635 --segments An alias for --program-headers\n"));
5636 fprintf (stream, _("\
5637 -S --section-headers Display the sections' header\n"));
5638 fprintf (stream, _("\
5639 --sections An alias for --section-headers\n"));
5640 fprintf (stream, _("\
5641 -g --section-groups Display the section groups\n"));
5642 fprintf (stream, _("\
5643 -t --section-details Display the section details\n"));
5644 fprintf (stream, _("\
5645 -e --headers Equivalent to: -h -l -S\n"));
5646 fprintf (stream, _("\
5647 -s --syms Display the symbol table\n"));
5648 fprintf (stream, _("\
5649 --symbols An alias for --syms\n"));
5650 fprintf (stream, _("\
5651 --dyn-syms Display the dynamic symbol table\n"));
5652 fprintf (stream, _("\
5653 --lto-syms Display LTO symbol tables\n"));
5654 fprintf (stream, _("\
047c3dbf
NL
5655 --sym-base=[0|8|10|16] \n\
5656 Force base for symbol sizes. The options are \n\
d6249f5f
AM
5657 mixed (the default), octal, decimal, hexadecimal.\n"));
5658 fprintf (stream, _("\
0d646226
AM
5659 -C --demangle[=STYLE] Decode mangled/processed symbol names\n"));
5660 display_demangler_styles (stream, _("\
5661 STYLE can be "));
d6249f5f
AM
5662 fprintf (stream, _("\
5663 --no-demangle Do not demangle low-level symbol names. (default)\n"));
5664 fprintf (stream, _("\
5665 --recurse-limit Enable a demangling recursion limit. (default)\n"));
5666 fprintf (stream, _("\
5667 --no-recurse-limit Disable a demangling recursion limit\n"));
b3aa80b4
NC
5668 fprintf (stream, _("\
5669 -U[dlexhi] --unicode=[default|locale|escape|hex|highlight|invalid]\n\
5670 Display unicode characters as determined by the current locale\n\
5671 (default), escape sequences, \"<hex sequences>\", highlighted\n\
5672 escape sequences, or treat them as invalid and display as\n\
5673 \"{hex sequences}\"\n"));
d6249f5f 5674 fprintf (stream, _("\
b6ac461a
NC
5675 -X --extra-sym-info Display extra information when showing symbols\n"));
5676 fprintf (stream, _("\
5677 --no-extra-sym-info Do not display extra information when showing symbols (default)\n"));
5678 fprintf (stream, _("\
5679 -n --notes Display the contents of note sections (if present)\n"));
d6249f5f
AM
5680 fprintf (stream, _("\
5681 -r --relocs Display the relocations (if present)\n"));
5682 fprintf (stream, _("\
5683 -u --unwind Display the unwind info (if present)\n"));
5684 fprintf (stream, _("\
5685 -d --dynamic Display the dynamic section (if present)\n"));
5686 fprintf (stream, _("\
5687 -V --version-info Display the version sections (if present)\n"));
5688 fprintf (stream, _("\
5689 -A --arch-specific Display architecture specific information (if any)\n"));
5690 fprintf (stream, _("\
5691 -c --archive-index Display the symbol/file index in an archive\n"));
5692 fprintf (stream, _("\
5693 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
5694 fprintf (stream, _("\
5695 -L --lint|--enable-checks\n\
5696 Display warning messages for possible problems\n"));
5697 fprintf (stream, _("\
09c11c86 5698 -x --hex-dump=<number|name>\n\
d6249f5f
AM
5699 Dump the contents of section <number|name> as bytes\n"));
5700 fprintf (stream, _("\
09c11c86 5701 -p --string-dump=<number|name>\n\
d6249f5f
AM
5702 Dump the contents of section <number|name> as strings\n"));
5703 fprintf (stream, _("\
cf13d699 5704 -R --relocated-dump=<number|name>\n\
d6249f5f
AM
5705 Dump the relocated contents of section <number|name>\n"));
5706 fprintf (stream, _("\
5707 -z --decompress Decompress section before dumping it\n"));
5708 fprintf (stream, _("\
5709 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
5710 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
5711 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
5712 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
5713 U/=trace_info]\n\
5714 Display the contents of DWARF debug sections\n"));
5715 fprintf (stream, _("\
5716 -wk --debug-dump=links Display the contents of sections that link to separate\n\
5717 debuginfo files\n"));
5718 fprintf (stream, _("\
5719 -P --process-links Display the contents of non-debug sections in separate\n\
5720 debuginfo files. (Implies -wK)\n"));
c46b7066
NC
5721#if DEFAULT_FOR_FOLLOW_LINKS
5722 fprintf (stream, _("\
d6249f5f
AM
5723 -wK --debug-dump=follow-links\n\
5724 Follow links to separate debug info files (default)\n"));
5725 fprintf (stream, _("\
5726 -wN --debug-dump=no-follow-links\n\
5727 Do not follow links to separate debug info files\n"));
c46b7066
NC
5728#else
5729 fprintf (stream, _("\
d6249f5f
AM
5730 -wK --debug-dump=follow-links\n\
5731 Follow links to separate debug info files\n"));
5732 fprintf (stream, _("\
5733 -wN --debug-dump=no-follow-links\n\
5734 Do not follow links to separate debug info files\n\
5735 (default)\n"));
bed566bb
NC
5736#endif
5737#if HAVE_LIBDEBUGINFOD
5738 fprintf (stream, _("\
5739 -wD --debug-dump=use-debuginfod\n\
5740 When following links, also query debuginfod servers (default)\n"));
5741 fprintf (stream, _("\
5742 -wE --debug-dump=do-not-use-debuginfod\n\
5743 When following links, do not query debuginfod servers\n"));
c46b7066 5744#endif
fd2f0033 5745 fprintf (stream, _("\
d6249f5f
AM
5746 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
5747 fprintf (stream, _("\
5748 --dwarf-start=N Display DIEs starting at offset N\n"));
094e34f2 5749#ifdef ENABLE_LIBCTF
7d9813f1 5750 fprintf (stream, _("\
d6249f5f
AM
5751 --ctf=<number|name> Display CTF info from section <number|name>\n"));
5752 fprintf (stream, _("\
80b56fad 5753 --ctf-parent=<name> Use CTF archive member <name> as the CTF parent\n"));
d6249f5f 5754 fprintf (stream, _("\
7d9813f1 5755 --ctf-symbols=<number|name>\n\
d6249f5f
AM
5756 Use section <number|name> as the CTF external symtab\n"));
5757 fprintf (stream, _("\
7d9813f1 5758 --ctf-strings=<number|name>\n\
d6249f5f 5759 Use section <number|name> as the CTF external strtab\n"));
094e34f2 5760#endif
42b6953b
IB
5761 fprintf (stream, _("\
5762 --sframe[=NAME] Display SFrame info from section NAME, (default '.sframe')\n"));
7d9813f1 5763
252b5132 5764#ifdef SUPPORT_DISASSEMBLY
92f01d61 5765 fprintf (stream, _("\
09c11c86
NC
5766 -i --instruction-dump=<number|name>\n\
5767 Disassemble the contents of section <number|name>\n"));
252b5132 5768#endif
92f01d61 5769 fprintf (stream, _("\
d6249f5f
AM
5770 -I --histogram Display histogram of bucket list lengths\n"));
5771 fprintf (stream, _("\
5772 -W --wide Allow output width to exceed 80 characters\n"));
5773 fprintf (stream, _("\
5774 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
5775 fprintf (stream, _("\
5776 @<file> Read options from <file>\n"));
5777 fprintf (stream, _("\
5778 -H --help Display this information\n"));
5779 fprintf (stream, _("\
8b53311e 5780 -v --version Display the version number of readelf\n"));
1118d252 5781
92f01d61
JM
5782 if (REPORT_BUGS_TO[0] && stream == stdout)
5783 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
252b5132 5784
92f01d61 5785 exit (stream == stdout ? 0 : 1);
252b5132
RH
5786}
5787
18bd398b
NC
5788/* Record the fact that the user wants the contents of section number
5789 SECTION to be displayed using the method(s) encoded as flags bits
5790 in TYPE. Note, TYPE can be zero if we are creating the array for
5791 the first time. */
5792
252b5132 5793static void
6431e409
AM
5794request_dump_bynumber (struct dump_data *dumpdata,
5795 unsigned int section, dump_type type)
252b5132 5796{
6431e409 5797 if (section >= dumpdata->num_dump_sects)
252b5132 5798 {
2cf0635d 5799 dump_type * new_dump_sects;
252b5132 5800
3f5e193b 5801 new_dump_sects = (dump_type *) calloc (section + 1,
dda8d76d 5802 sizeof (* new_dump_sects));
252b5132
RH
5803
5804 if (new_dump_sects == NULL)
591a748a 5805 error (_("Out of memory allocating dump request table.\n"));
252b5132
RH
5806 else
5807 {
6431e409 5808 if (dumpdata->dump_sects)
21b65bac
NC
5809 {
5810 /* Copy current flag settings. */
6431e409
AM
5811 memcpy (new_dump_sects, dumpdata->dump_sects,
5812 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
252b5132 5813
6431e409 5814 free (dumpdata->dump_sects);
21b65bac 5815 }
252b5132 5816
6431e409
AM
5817 dumpdata->dump_sects = new_dump_sects;
5818 dumpdata->num_dump_sects = section + 1;
252b5132
RH
5819 }
5820 }
5821
6431e409
AM
5822 if (dumpdata->dump_sects)
5823 dumpdata->dump_sects[section] |= type;
252b5132
RH
5824}
5825
aef1f6d0
DJ
5826/* Request a dump by section name. */
5827
5828static void
2cf0635d 5829request_dump_byname (const char * section, dump_type type)
aef1f6d0 5830{
2cf0635d 5831 struct dump_list_entry * new_request;
aef1f6d0 5832
3f5e193b
NC
5833 new_request = (struct dump_list_entry *)
5834 malloc (sizeof (struct dump_list_entry));
aef1f6d0 5835 if (!new_request)
591a748a 5836 error (_("Out of memory allocating dump request table.\n"));
aef1f6d0
DJ
5837
5838 new_request->name = strdup (section);
5839 if (!new_request->name)
591a748a 5840 error (_("Out of memory allocating dump request table.\n"));
aef1f6d0
DJ
5841
5842 new_request->type = type;
5843
5844 new_request->next = dump_sects_byname;
5845 dump_sects_byname = new_request;
5846}
5847
cf13d699 5848static inline void
6431e409 5849request_dump (struct dump_data *dumpdata, dump_type type)
cf13d699
NC
5850{
5851 int section;
5852 char * cp;
5853
015dc7e1 5854 do_dump = true;
cf13d699
NC
5855 section = strtoul (optarg, & cp, 0);
5856
5857 if (! *cp && section >= 0)
6431e409 5858 request_dump_bynumber (dumpdata, section, type);
cf13d699
NC
5859 else
5860 request_dump_byname (optarg, type);
5861}
5862
252b5132 5863static void
6431e409 5864parse_args (struct dump_data *dumpdata, int argc, char ** argv)
252b5132
RH
5865{
5866 int c;
5867
5868 if (argc < 2)
92f01d61 5869 usage (stderr);
252b5132
RH
5870
5871 while ((c = getopt_long
b6ac461a 5872 (argc, argv, "ACDHILNPR:STU:VWXacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
252b5132 5873 {
252b5132
RH
5874 switch (c)
5875 {
5876 case 0:
5877 /* Long options. */
5878 break;
5879 case 'H':
92f01d61 5880 usage (stdout);
252b5132
RH
5881 break;
5882
5883 case 'a':
015dc7e1
AM
5884 do_syms = true;
5885 do_reloc = true;
5886 do_unwind = true;
5887 do_dynamic = true;
5888 do_header = true;
5889 do_sections = true;
5890 do_section_groups = true;
5891 do_segments = true;
5892 do_version = true;
5893 do_histogram = true;
5894 do_arch = true;
5895 do_notes = true;
252b5132 5896 break;
79bc120c 5897
f5842774 5898 case 'g':
015dc7e1 5899 do_section_groups = true;
f5842774 5900 break;
5477e8a0 5901 case 't':
595cf52e 5902 case 'N':
015dc7e1
AM
5903 do_sections = true;
5904 do_section_details = true;
595cf52e 5905 break;
252b5132 5906 case 'e':
015dc7e1
AM
5907 do_header = true;
5908 do_sections = true;
5909 do_segments = true;
252b5132 5910 break;
a952a375 5911 case 'A':
015dc7e1 5912 do_arch = true;
a952a375 5913 break;
252b5132 5914 case 'D':
015dc7e1 5915 do_using_dynamic = true;
252b5132
RH
5916 break;
5917 case 'r':
015dc7e1 5918 do_reloc = true;
252b5132 5919 break;
4d6ed7c8 5920 case 'u':
015dc7e1 5921 do_unwind = true;
4d6ed7c8 5922 break;
252b5132 5923 case 'h':
015dc7e1 5924 do_header = true;
252b5132
RH
5925 break;
5926 case 'l':
015dc7e1 5927 do_segments = true;
252b5132
RH
5928 break;
5929 case 's':
015dc7e1 5930 do_syms = true;
252b5132
RH
5931 break;
5932 case 'S':
015dc7e1 5933 do_sections = true;
252b5132
RH
5934 break;
5935 case 'd':
015dc7e1 5936 do_dynamic = true;
252b5132 5937 break;
a952a375 5938 case 'I':
015dc7e1 5939 do_histogram = true;
a952a375 5940 break;
779fe533 5941 case 'n':
015dc7e1 5942 do_notes = true;
779fe533 5943 break;
4145f1d5 5944 case 'c':
015dc7e1 5945 do_archive_index = true;
4145f1d5 5946 break;
1b513401 5947 case 'L':
015dc7e1 5948 do_checks = true;
1b513401 5949 break;
ca0e11aa 5950 case 'P':
015dc7e1
AM
5951 process_links = true;
5952 do_follow_links = true;
e1dbfc17 5953 dump_any_debugging = true;
ca0e11aa 5954 break;
252b5132 5955 case 'x':
6431e409 5956 request_dump (dumpdata, HEX_DUMP);
aef1f6d0 5957 break;
09c11c86 5958 case 'p':
6431e409 5959 request_dump (dumpdata, STRING_DUMP);
cf13d699
NC
5960 break;
5961 case 'R':
6431e409 5962 request_dump (dumpdata, RELOC_DUMP);
09c11c86 5963 break;
0e602686 5964 case 'z':
015dc7e1 5965 decompress_dumps = true;
0e602686 5966 break;
252b5132 5967 case 'w':
0f03783c 5968 if (optarg == NULL)
613ff48b 5969 {
015dc7e1 5970 do_debugging = true;
94585d6d
NC
5971 do_dump = true;
5972 dump_any_debugging = true;
613ff48b
CC
5973 dwarf_select_sections_all ();
5974 }
252b5132
RH
5975 else
5976 {
015dc7e1 5977 do_debugging = false;
94585d6d
NC
5978 if (dwarf_select_sections_by_letters (optarg))
5979 {
5980 do_dump = true;
5981 dump_any_debugging = true;
5982 }
252b5132
RH
5983 }
5984 break;
2979dc34 5985 case OPTION_DEBUG_DUMP:
0f03783c 5986 if (optarg == NULL)
d6249f5f 5987 {
94585d6d 5988 do_dump = true;
d6249f5f 5989 do_debugging = true;
94585d6d 5990 dump_any_debugging = true;
d6249f5f
AM
5991 dwarf_select_sections_all ();
5992 }
2979dc34
JJ
5993 else
5994 {
015dc7e1 5995 do_debugging = false;
94585d6d
NC
5996 if (dwarf_select_sections_by_names (optarg))
5997 {
5998 do_dump = true;
5999 dump_any_debugging = true;
6000 }
2979dc34
JJ
6001 }
6002 break;
fd2f0033
TT
6003 case OPTION_DWARF_DEPTH:
6004 {
6005 char *cp;
6006
6007 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
6008 }
6009 break;
6010 case OPTION_DWARF_START:
6011 {
6012 char *cp;
6013
6014 dwarf_start_die = strtoul (optarg, & cp, 0);
6015 }
6016 break;
4723351a 6017 case OPTION_DWARF_CHECK:
015dc7e1 6018 dwarf_check = true;
4723351a 6019 break;
7d9813f1 6020 case OPTION_CTF_DUMP:
015dc7e1 6021 do_ctf = true;
6431e409 6022 request_dump (dumpdata, CTF_DUMP);
7d9813f1
NA
6023 break;
6024 case OPTION_CTF_SYMBOLS:
df16e041 6025 free (dump_ctf_symtab_name);
7d9813f1
NA
6026 dump_ctf_symtab_name = strdup (optarg);
6027 break;
6028 case OPTION_CTF_STRINGS:
df16e041 6029 free (dump_ctf_strtab_name);
7d9813f1
NA
6030 dump_ctf_strtab_name = strdup (optarg);
6031 break;
6032 case OPTION_CTF_PARENT:
df16e041 6033 free (dump_ctf_parent_name);
7d9813f1
NA
6034 dump_ctf_parent_name = strdup (optarg);
6035 break;
42b6953b
IB
6036 case OPTION_SFRAME_DUMP:
6037 do_sframe = true;
6038 /* Providing section name is optional. request_dump (), however,
6039 thrives on non NULL optarg. Handle it explicitly here. */
6040 if (optarg != NULL)
6041 request_dump (dumpdata, SFRAME_DUMP);
6042 else
6043 {
6044 do_dump = true;
6045 const char *sframe_sec_name = strdup (".sframe");
6046 request_dump_byname (sframe_sec_name, SFRAME_DUMP);
6047 }
6048 break;
2c610e4b 6049 case OPTION_DYN_SYMS:
015dc7e1 6050 do_dyn_syms = true;
2c610e4b 6051 break;
0f03783c 6052 case OPTION_LTO_SYMS:
015dc7e1 6053 do_lto_syms = true;
0f03783c 6054 break;
b6ac461a
NC
6055 case 'X':
6056 extra_sym_info = true;
6057 break;
6058 case OPTION_NO_EXTRA_SYM_INFO:
6059 extra_sym_info = false;
6060 break;
6061
252b5132
RH
6062#ifdef SUPPORT_DISASSEMBLY
6063 case 'i':
6431e409 6064 request_dump (dumpdata, DISASS_DUMP);
cf13d699 6065 break;
252b5132
RH
6066#endif
6067 case 'v':
6068 print_version (program_name);
6069 break;
6070 case 'V':
015dc7e1 6071 do_version = true;
252b5132 6072 break;
d974e256 6073 case 'W':
015dc7e1 6074 do_wide = true;
d974e256 6075 break;
0942c7ab 6076 case 'T':
015dc7e1 6077 do_not_show_symbol_truncation = true;
0942c7ab 6078 break;
79bc120c 6079 case 'C':
015dc7e1 6080 do_demangle = true;
79bc120c
NC
6081 if (optarg != NULL)
6082 {
6083 enum demangling_styles style;
6084
6085 style = cplus_demangle_name_to_style (optarg);
6086 if (style == unknown_demangling)
6087 error (_("unknown demangling style `%s'"), optarg);
6088
6089 cplus_demangle_set_style (style);
6090 }
6091 break;
6092 case OPTION_NO_DEMANGLING:
015dc7e1 6093 do_demangle = false;
79bc120c
NC
6094 break;
6095 case OPTION_RECURSE_LIMIT:
6096 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
6097 break;
6098 case OPTION_NO_RECURSE_LIMIT:
6099 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
6100 break;
6101 case OPTION_WITH_SYMBOL_VERSIONS:
6102 /* Ignored for backward compatibility. */
6103 break;
b9e920ec 6104
b3aa80b4
NC
6105 case 'U':
6106 if (optarg == NULL)
6107 error (_("Missing arg to -U/--unicode")); /* Can this happen ? */
6108 else if (streq (optarg, "default") || streq (optarg, "d"))
6109 unicode_display = unicode_default;
6110 else if (streq (optarg, "locale") || streq (optarg, "l"))
6111 unicode_display = unicode_locale;
6112 else if (streq (optarg, "escape") || streq (optarg, "e"))
6113 unicode_display = unicode_escape;
6114 else if (streq (optarg, "invalid") || streq (optarg, "i"))
6115 unicode_display = unicode_invalid;
6116 else if (streq (optarg, "hex") || streq (optarg, "x"))
6117 unicode_display = unicode_hex;
6118 else if (streq (optarg, "highlight") || streq (optarg, "h"))
6119 unicode_display = unicode_highlight;
6120 else
6121 error (_("invalid argument to -U/--unicode: %s"), optarg);
6122 break;
6123
047c3dbf
NL
6124 case OPTION_SYM_BASE:
6125 sym_base = 0;
6126 if (optarg != NULL)
6127 {
6128 sym_base = strtoul (optarg, NULL, 0);
6129 switch (sym_base)
6130 {
6131 case 0:
6132 case 8:
6133 case 10:
6134 case 16:
6135 break;
6136
6137 default:
6138 sym_base = 0;
6139 break;
6140 }
6141 }
6142 break;
6143
252b5132 6144 default:
252b5132
RH
6145 /* xgettext:c-format */
6146 error (_("Invalid option '-%c'\n"), c);
1a0670f3 6147 /* Fall through. */
252b5132 6148 case '?':
92f01d61 6149 usage (stderr);
252b5132
RH
6150 }
6151 }
6152
4d6ed7c8 6153 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
252b5132 6154 && !do_segments && !do_header && !do_dump && !do_version
f5842774 6155 && !do_histogram && !do_debugging && !do_arch && !do_notes
2c610e4b 6156 && !do_section_groups && !do_archive_index
0f03783c 6157 && !do_dyn_syms && !do_lto_syms)
1b513401
NC
6158 {
6159 if (do_checks)
6160 {
015dc7e1
AM
6161 check_all = true;
6162 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
6163 do_segments = do_header = do_dump = do_version = true;
6164 do_histogram = do_debugging = do_arch = do_notes = true;
6165 do_section_groups = do_archive_index = do_dyn_syms = true;
6166 do_lto_syms = true;
1b513401
NC
6167 }
6168 else
6169 usage (stderr);
6170 }
252b5132
RH
6171}
6172
6173static const char *
d3ba0551 6174get_elf_class (unsigned int elf_class)
252b5132 6175{
b34976b6 6176 static char buff[32];
103f02d3 6177
252b5132
RH
6178 switch (elf_class)
6179 {
6180 case ELFCLASSNONE: return _("none");
e3c8793a
NC
6181 case ELFCLASS32: return "ELF32";
6182 case ELFCLASS64: return "ELF64";
ab5e7794 6183 default:
e9e44622 6184 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
ab5e7794 6185 return buff;
252b5132
RH
6186 }
6187}
6188
6189static const char *
d3ba0551 6190get_data_encoding (unsigned int encoding)
252b5132 6191{
b34976b6 6192 static char buff[32];
103f02d3 6193
252b5132
RH
6194 switch (encoding)
6195 {
6196 case ELFDATANONE: return _("none");
33c63f9d
CM
6197 case ELFDATA2LSB: return _("2's complement, little endian");
6198 case ELFDATA2MSB: return _("2's complement, big endian");
103f02d3 6199 default:
e9e44622 6200 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
ab5e7794 6201 return buff;
252b5132
RH
6202 }
6203}
6204
521f7268
NC
6205static bool
6206check_magic_number (Filedata * filedata, Elf_Internal_Ehdr * header)
6207{
6208 if (header->e_ident[EI_MAG0] == ELFMAG0
6209 && header->e_ident[EI_MAG1] == ELFMAG1
6210 && header->e_ident[EI_MAG2] == ELFMAG2
6211 && header->e_ident[EI_MAG3] == ELFMAG3)
6212 return true;
6213
6214 /* Some compilers produce object files that are not in the ELF file format.
6215 As an aid to users of readelf, try to identify these cases and suggest
6216 alternative tools.
6217
6218 FIXME: It is not clear if all four bytes are used as constant magic
6219 valus by all compilers. It may be necessary to recode this function if
6220 different tools use different length sequences. */
6221
6222 static struct
6223 {
6224 unsigned char magic[4];
6225 const char * obj_message;
6226 const char * ar_message;
6227 }
6228 known_magic[] =
6229 {
6230 { { 'B', 'C', 0xc0, 0xde },
6231 N_("This is a LLVM bitcode file - try using llvm-bcanalyzer\n"),
90de8f9c 6232 N_("This is a LLVM bitcode file - try extracting and then using llvm-bcanalyzer\n")
521f7268
NC
6233 },
6234 { { 'g', 'o', ' ', 'o' },
6235 N_("This is a GO binary file - try using 'go tool objdump' or 'go tool nm'\n"),
6236 NULL
6237 }
6238 };
6239 int i;
6240
6241 for (i = ARRAY_SIZE (known_magic); i--;)
6242 {
6243 if (header->e_ident[EI_MAG0] == known_magic[i].magic[0]
6244 && header->e_ident[EI_MAG1] == known_magic[i].magic[1]
6245 && header->e_ident[EI_MAG2] == known_magic[i].magic[2]
6246 && header->e_ident[EI_MAG3] == known_magic[i].magic[3])
6247 {
6248 /* Some compiler's analyzer tools do not handle archives,
6249 so we provide two different kinds of error message. */
6250 if (filedata->archive_file_size > 0
6251 && known_magic[i].ar_message != NULL)
b3ea2010 6252 error ("%s", known_magic[i].ar_message);
521f7268 6253 else
b3ea2010 6254 error ("%s", known_magic[i].obj_message);
521f7268
NC
6255 return false;
6256 }
6257 }
6258
6259 error (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
6260 return false;
6261}
6262
dda8d76d 6263/* Decode the data held in 'filedata->file_header'. */
ee42cf8c 6264
015dc7e1 6265static bool
dda8d76d 6266process_file_header (Filedata * filedata)
252b5132 6267{
dda8d76d
NC
6268 Elf_Internal_Ehdr * header = & filedata->file_header;
6269
521f7268
NC
6270 if (! check_magic_number (filedata, header))
6271 return false;
252b5132 6272
ca0e11aa
NC
6273 if (! filedata->is_separate)
6274 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
2dc4cec1 6275
252b5132
RH
6276 if (do_header)
6277 {
32ec8896 6278 unsigned i;
252b5132 6279
ca0e11aa
NC
6280 if (filedata->is_separate)
6281 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
6282 else
6283 printf (_("ELF Header:\n"));
252b5132 6284 printf (_(" Magic: "));
b34976b6 6285 for (i = 0; i < EI_NIDENT; i++)
dda8d76d 6286 printf ("%2.2x ", header->e_ident[i]);
252b5132
RH
6287 printf ("\n");
6288 printf (_(" Class: %s\n"),
dda8d76d 6289 get_elf_class (header->e_ident[EI_CLASS]));
252b5132 6290 printf (_(" Data: %s\n"),
dda8d76d 6291 get_data_encoding (header->e_ident[EI_DATA]));
e8a64888 6292 printf (_(" Version: %d%s\n"),
dda8d76d
NC
6293 header->e_ident[EI_VERSION],
6294 (header->e_ident[EI_VERSION] == EV_CURRENT
e8a64888 6295 ? _(" (current)")
dda8d76d 6296 : (header->e_ident[EI_VERSION] != EV_NONE
e8a64888 6297 ? _(" <unknown>")
789be9f7 6298 : "")));
252b5132 6299 printf (_(" OS/ABI: %s\n"),
dda8d76d 6300 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
252b5132 6301 printf (_(" ABI Version: %d\n"),
dda8d76d 6302 header->e_ident[EI_ABIVERSION]);
252b5132 6303 printf (_(" Type: %s\n"),
93df3340 6304 get_file_type (filedata));
252b5132 6305 printf (_(" Machine: %s\n"),
dda8d76d 6306 get_machine_name (header->e_machine));
252b5132 6307 printf (_(" Version: 0x%lx\n"),
e8a64888 6308 header->e_version);
76da6bbe 6309
f7a99963 6310 printf (_(" Entry point address: "));
e8a64888 6311 print_vma (header->e_entry, PREFIX_HEX);
f7a99963 6312 printf (_("\n Start of program headers: "));
e8a64888 6313 print_vma (header->e_phoff, DEC);
f7a99963 6314 printf (_(" (bytes into file)\n Start of section headers: "));
e8a64888 6315 print_vma (header->e_shoff, DEC);
f7a99963 6316 printf (_(" (bytes into file)\n"));
76da6bbe 6317
252b5132 6318 printf (_(" Flags: 0x%lx%s\n"),
e8a64888 6319 header->e_flags,
dda8d76d 6320 get_machine_flags (filedata, header->e_flags, header->e_machine));
e8a64888
AM
6321 printf (_(" Size of this header: %u (bytes)\n"),
6322 header->e_ehsize);
6323 printf (_(" Size of program headers: %u (bytes)\n"),
6324 header->e_phentsize);
6325 printf (_(" Number of program headers: %u"),
6326 header->e_phnum);
dda8d76d
NC
6327 if (filedata->section_headers != NULL
6328 && header->e_phnum == PN_XNUM
6329 && filedata->section_headers[0].sh_info != 0)
2969c3b3 6330 printf (" (%u)", filedata->section_headers[0].sh_info);
2046a35d 6331 putc ('\n', stdout);
e8a64888
AM
6332 printf (_(" Size of section headers: %u (bytes)\n"),
6333 header->e_shentsize);
6334 printf (_(" Number of section headers: %u"),
6335 header->e_shnum);
dda8d76d 6336 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
e8a64888
AM
6337 {
6338 header->e_shnum = filedata->section_headers[0].sh_size;
6339 printf (" (%u)", header->e_shnum);
6340 }
560f3c1c 6341 putc ('\n', stdout);
e8a64888
AM
6342 printf (_(" Section header string table index: %u"),
6343 header->e_shstrndx);
dda8d76d
NC
6344 if (filedata->section_headers != NULL
6345 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
e8a64888
AM
6346 {
6347 header->e_shstrndx = filedata->section_headers[0].sh_link;
6348 printf (" (%u)", header->e_shstrndx);
6349 }
6350 if (header->e_shstrndx != SHN_UNDEF
6351 && header->e_shstrndx >= header->e_shnum)
6352 {
6353 header->e_shstrndx = SHN_UNDEF;
6354 printf (_(" <corrupt: out of range>"));
6355 }
560f3c1c
AM
6356 putc ('\n', stdout);
6357 }
6358
dda8d76d 6359 if (filedata->section_headers != NULL)
560f3c1c 6360 {
dda8d76d
NC
6361 if (header->e_phnum == PN_XNUM
6362 && filedata->section_headers[0].sh_info != 0)
2969c3b3
AM
6363 {
6364 /* Throw away any cached read of PN_XNUM headers. */
6365 free (filedata->program_headers);
6366 filedata->program_headers = NULL;
6367 header->e_phnum = filedata->section_headers[0].sh_info;
6368 }
dda8d76d
NC
6369 if (header->e_shnum == SHN_UNDEF)
6370 header->e_shnum = filedata->section_headers[0].sh_size;
6371 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
6372 header->e_shstrndx = filedata->section_headers[0].sh_link;
9c1ce108 6373 if (header->e_shstrndx >= header->e_shnum)
dda8d76d 6374 header->e_shstrndx = SHN_UNDEF;
252b5132 6375 }
103f02d3 6376
015dc7e1 6377 return true;
9ea033b2
NC
6378}
6379
dda8d76d
NC
6380/* Read in the program headers from FILEDATA and store them in PHEADERS.
6381 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
6382
015dc7e1 6383static bool
dda8d76d 6384get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
9ea033b2 6385{
2cf0635d
NC
6386 Elf32_External_Phdr * phdrs;
6387 Elf32_External_Phdr * external;
6388 Elf_Internal_Phdr * internal;
b34976b6 6389 unsigned int i;
dda8d76d
NC
6390 unsigned int size = filedata->file_header.e_phentsize;
6391 unsigned int num = filedata->file_header.e_phnum;
e0a31db1
NC
6392
6393 /* PR binutils/17531: Cope with unexpected section header sizes. */
6394 if (size == 0 || num == 0)
015dc7e1 6395 return false;
e0a31db1
NC
6396 if (size < sizeof * phdrs)
6397 {
6398 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
015dc7e1 6399 return false;
e0a31db1
NC
6400 }
6401 if (size > sizeof * phdrs)
6402 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
103f02d3 6403
dda8d76d 6404 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
e0a31db1
NC
6405 size, num, _("program headers"));
6406 if (phdrs == NULL)
015dc7e1 6407 return false;
9ea033b2 6408
91d6fa6a 6409 for (i = 0, internal = pheaders, external = phdrs;
dda8d76d 6410 i < filedata->file_header.e_phnum;
b34976b6 6411 i++, internal++, external++)
252b5132 6412 {
9ea033b2
NC
6413 internal->p_type = BYTE_GET (external->p_type);
6414 internal->p_offset = BYTE_GET (external->p_offset);
6415 internal->p_vaddr = BYTE_GET (external->p_vaddr);
6416 internal->p_paddr = BYTE_GET (external->p_paddr);
6417 internal->p_filesz = BYTE_GET (external->p_filesz);
6418 internal->p_memsz = BYTE_GET (external->p_memsz);
6419 internal->p_flags = BYTE_GET (external->p_flags);
6420 internal->p_align = BYTE_GET (external->p_align);
252b5132
RH
6421 }
6422
9ea033b2 6423 free (phdrs);
015dc7e1 6424 return true;
252b5132
RH
6425}
6426
dda8d76d
NC
6427/* Read in the program headers from FILEDATA and store them in PHEADERS.
6428 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
6429
015dc7e1 6430static bool
dda8d76d 6431get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
9ea033b2 6432{
2cf0635d
NC
6433 Elf64_External_Phdr * phdrs;
6434 Elf64_External_Phdr * external;
6435 Elf_Internal_Phdr * internal;
b34976b6 6436 unsigned int i;
dda8d76d
NC
6437 unsigned int size = filedata->file_header.e_phentsize;
6438 unsigned int num = filedata->file_header.e_phnum;
e0a31db1
NC
6439
6440 /* PR binutils/17531: Cope with unexpected section header sizes. */
6441 if (size == 0 || num == 0)
015dc7e1 6442 return false;
e0a31db1
NC
6443 if (size < sizeof * phdrs)
6444 {
6445 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
015dc7e1 6446 return false;
e0a31db1
NC
6447 }
6448 if (size > sizeof * phdrs)
6449 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
103f02d3 6450
dda8d76d 6451 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
e0a31db1 6452 size, num, _("program headers"));
a6e9f9df 6453 if (!phdrs)
015dc7e1 6454 return false;
9ea033b2 6455
91d6fa6a 6456 for (i = 0, internal = pheaders, external = phdrs;
dda8d76d 6457 i < filedata->file_header.e_phnum;
b34976b6 6458 i++, internal++, external++)
9ea033b2
NC
6459 {
6460 internal->p_type = BYTE_GET (external->p_type);
6461 internal->p_flags = BYTE_GET (external->p_flags);
66543521
AM
6462 internal->p_offset = BYTE_GET (external->p_offset);
6463 internal->p_vaddr = BYTE_GET (external->p_vaddr);
6464 internal->p_paddr = BYTE_GET (external->p_paddr);
6465 internal->p_filesz = BYTE_GET (external->p_filesz);
6466 internal->p_memsz = BYTE_GET (external->p_memsz);
6467 internal->p_align = BYTE_GET (external->p_align);
9ea033b2
NC
6468 }
6469
6470 free (phdrs);
015dc7e1 6471 return true;
9ea033b2 6472}
252b5132 6473
32ec8896 6474/* Returns TRUE if the program headers were read into `program_headers'. */
d93f0186 6475
015dc7e1 6476static bool
dda8d76d 6477get_program_headers (Filedata * filedata)
d93f0186 6478{
2cf0635d 6479 Elf_Internal_Phdr * phdrs;
d93f0186
NC
6480
6481 /* Check cache of prior read. */
dda8d76d 6482 if (filedata->program_headers != NULL)
015dc7e1 6483 return true;
d93f0186 6484
82156ab7
NC
6485 /* Be kind to memory checkers by looking for
6486 e_phnum values which we know must be invalid. */
dda8d76d 6487 if (filedata->file_header.e_phnum
82156ab7 6488 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
dda8d76d 6489 >= filedata->file_size)
82156ab7
NC
6490 {
6491 error (_("Too many program headers - %#x - the file is not that big\n"),
dda8d76d 6492 filedata->file_header.e_phnum);
015dc7e1 6493 return false;
82156ab7 6494 }
d93f0186 6495
dda8d76d 6496 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
82156ab7 6497 sizeof (Elf_Internal_Phdr));
d93f0186
NC
6498 if (phdrs == NULL)
6499 {
8b73c356 6500 error (_("Out of memory reading %u program headers\n"),
dda8d76d 6501 filedata->file_header.e_phnum);
015dc7e1 6502 return false;
d93f0186
NC
6503 }
6504
6505 if (is_32bit_elf
dda8d76d
NC
6506 ? get_32bit_program_headers (filedata, phdrs)
6507 : get_64bit_program_headers (filedata, phdrs))
d93f0186 6508 {
dda8d76d 6509 filedata->program_headers = phdrs;
015dc7e1 6510 return true;
d93f0186
NC
6511 }
6512
6513 free (phdrs);
015dc7e1 6514 return false;
d93f0186
NC
6515}
6516
93df3340 6517/* Print program header info and locate dynamic section. */
2f62977e 6518
93df3340 6519static void
dda8d76d 6520process_program_headers (Filedata * filedata)
252b5132 6521{
2cf0635d 6522 Elf_Internal_Phdr * segment;
b34976b6 6523 unsigned int i;
1a9ccd70 6524 Elf_Internal_Phdr * previous_load = NULL;
252b5132 6525
dda8d76d 6526 if (filedata->file_header.e_phnum == 0)
252b5132 6527 {
82f2dbf7 6528 /* PR binutils/12467. */
dda8d76d 6529 if (filedata->file_header.e_phoff != 0)
93df3340
AM
6530 warn (_("possibly corrupt ELF header - it has a non-zero program"
6531 " header offset, but no program headers\n"));
82f2dbf7 6532 else if (do_segments)
ca0e11aa
NC
6533 {
6534 if (filedata->is_separate)
6535 printf (_("\nThere are no program headers in linked file '%s'.\n"),
6536 filedata->file_name);
6537 else
6538 printf (_("\nThere are no program headers in this file.\n"));
6539 }
93df3340 6540 goto no_headers;
252b5132
RH
6541 }
6542
6543 if (do_segments && !do_header)
6544 {
ca0e11aa
NC
6545 if (filedata->is_separate)
6546 printf ("\nIn linked file '%s' the ELF file type is %s\n",
93df3340 6547 filedata->file_name, get_file_type (filedata));
ca0e11aa 6548 else
93df3340 6549 printf (_("\nElf file type is %s\n"), get_file_type (filedata));
b8281767 6550 printf (_("Entry point 0x%" PRIx64 "\n"),
625d49fc 6551 filedata->file_header.e_entry);
b8281767
AM
6552 printf (ngettext ("There is %d program header,"
6553 " starting at offset %" PRIu64 "\n",
6554 "There are %d program headers,"
6555 " starting at offset %" PRIu64 "\n",
dda8d76d
NC
6556 filedata->file_header.e_phnum),
6557 filedata->file_header.e_phnum,
625d49fc 6558 filedata->file_header.e_phoff);
252b5132
RH
6559 }
6560
dda8d76d 6561 if (! get_program_headers (filedata))
93df3340 6562 goto no_headers;
103f02d3 6563
252b5132
RH
6564 if (do_segments)
6565 {
dda8d76d 6566 if (filedata->file_header.e_phnum > 1)
3a1a2036
NC
6567 printf (_("\nProgram Headers:\n"));
6568 else
6569 printf (_("\nProgram Headers:\n"));
76da6bbe 6570
f7a99963
NC
6571 if (is_32bit_elf)
6572 printf
6573 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
d974e256
JJ
6574 else if (do_wide)
6575 printf
6576 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
f7a99963
NC
6577 else
6578 {
6579 printf
6580 (_(" Type Offset VirtAddr PhysAddr\n"));
6581 printf
6582 (_(" FileSiz MemSiz Flags Align\n"));
6583 }
252b5132
RH
6584 }
6585
26c527e6 6586 uint64_t dynamic_addr = 0;
be7d229a 6587 uint64_t dynamic_size = 0;
dda8d76d
NC
6588 for (i = 0, segment = filedata->program_headers;
6589 i < filedata->file_header.e_phnum;
b34976b6 6590 i++, segment++)
252b5132
RH
6591 {
6592 if (do_segments)
6593 {
dda8d76d 6594 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
f7a99963
NC
6595
6596 if (is_32bit_elf)
6597 {
6598 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6599 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
6600 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
6601 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
6602 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
6603 printf ("%c%c%c ",
6604 (segment->p_flags & PF_R ? 'R' : ' '),
6605 (segment->p_flags & PF_W ? 'W' : ' '),
6606 (segment->p_flags & PF_X ? 'E' : ' '));
6607 printf ("%#lx", (unsigned long) segment->p_align);
6608 }
d974e256
JJ
6609 else if (do_wide)
6610 {
6611 if ((unsigned long) segment->p_offset == segment->p_offset)
6612 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6613 else
6614 {
6615 print_vma (segment->p_offset, FULL_HEX);
6616 putchar (' ');
6617 }
6618
6619 print_vma (segment->p_vaddr, FULL_HEX);
6620 putchar (' ');
6621 print_vma (segment->p_paddr, FULL_HEX);
6622 putchar (' ');
6623
6624 if ((unsigned long) segment->p_filesz == segment->p_filesz)
6625 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
6626 else
6627 {
6628 print_vma (segment->p_filesz, FULL_HEX);
6629 putchar (' ');
6630 }
6631
6632 if ((unsigned long) segment->p_memsz == segment->p_memsz)
6633 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
6634 else
6635 {
f48e6c45 6636 print_vma (segment->p_memsz, FULL_HEX);
d974e256
JJ
6637 }
6638
6639 printf (" %c%c%c ",
6640 (segment->p_flags & PF_R ? 'R' : ' '),
6641 (segment->p_flags & PF_W ? 'W' : ' '),
6642 (segment->p_flags & PF_X ? 'E' : ' '));
6643
6644 if ((unsigned long) segment->p_align == segment->p_align)
6645 printf ("%#lx", (unsigned long) segment->p_align);
6646 else
6647 {
6648 print_vma (segment->p_align, PREFIX_HEX);
6649 }
6650 }
f7a99963
NC
6651 else
6652 {
6653 print_vma (segment->p_offset, FULL_HEX);
6654 putchar (' ');
6655 print_vma (segment->p_vaddr, FULL_HEX);
6656 putchar (' ');
6657 print_vma (segment->p_paddr, FULL_HEX);
6658 printf ("\n ");
6659 print_vma (segment->p_filesz, FULL_HEX);
6660 putchar (' ');
6661 print_vma (segment->p_memsz, FULL_HEX);
6662 printf (" %c%c%c ",
6663 (segment->p_flags & PF_R ? 'R' : ' '),
6664 (segment->p_flags & PF_W ? 'W' : ' '),
6665 (segment->p_flags & PF_X ? 'E' : ' '));
1d262527 6666 print_vma (segment->p_align, PREFIX_HEX);
f7a99963 6667 }
252b5132 6668
1a9ccd70
NC
6669 putc ('\n', stdout);
6670 }
f54498b4 6671
252b5132
RH
6672 switch (segment->p_type)
6673 {
1a9ccd70 6674 case PT_LOAD:
502d895c
NC
6675#if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
6676 required by the ELF standard, several programs, including the Linux
6677 kernel, make use of non-ordered segments. */
1a9ccd70
NC
6678 if (previous_load
6679 && previous_load->p_vaddr > segment->p_vaddr)
6680 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
502d895c 6681#endif
1a9ccd70
NC
6682 if (segment->p_memsz < segment->p_filesz)
6683 error (_("the segment's file size is larger than its memory size\n"));
6684 previous_load = segment;
6685 break;
6686
6687 case PT_PHDR:
6688 /* PR 20815 - Verify that the program header is loaded into memory. */
6689 if (i > 0 && previous_load != NULL)
6690 error (_("the PHDR segment must occur before any LOAD segment\n"));
dda8d76d 6691 if (filedata->file_header.e_machine != EM_PARISC)
1a9ccd70
NC
6692 {
6693 unsigned int j;
6694
dda8d76d 6695 for (j = 1; j < filedata->file_header.e_phnum; j++)
c0c121b0
AM
6696 {
6697 Elf_Internal_Phdr *load = filedata->program_headers + j;
6698 if (load->p_type == PT_LOAD
6699 && load->p_offset <= segment->p_offset
6700 && (load->p_offset + load->p_filesz
6701 >= segment->p_offset + segment->p_filesz)
6702 && load->p_vaddr <= segment->p_vaddr
6703 && (load->p_vaddr + load->p_filesz
6704 >= segment->p_vaddr + segment->p_filesz))
6705 break;
6706 }
dda8d76d 6707 if (j == filedata->file_header.e_phnum)
1a9ccd70
NC
6708 error (_("the PHDR segment is not covered by a LOAD segment\n"));
6709 }
6710 break;
6711
252b5132 6712 case PT_DYNAMIC:
93df3340 6713 if (dynamic_addr)
252b5132
RH
6714 error (_("more than one dynamic segment\n"));
6715
20737c13
AM
6716 /* By default, assume that the .dynamic section is the first
6717 section in the DYNAMIC segment. */
93df3340
AM
6718 dynamic_addr = segment->p_offset;
6719 dynamic_size = segment->p_filesz;
20737c13 6720
b2d38a17
NC
6721 /* Try to locate the .dynamic section. If there is
6722 a section header table, we can easily locate it. */
dda8d76d 6723 if (filedata->section_headers != NULL)
b2d38a17 6724 {
2cf0635d 6725 Elf_Internal_Shdr * sec;
b2d38a17 6726
dda8d76d 6727 sec = find_section (filedata, ".dynamic");
89fac5e3 6728 if (sec == NULL || sec->sh_size == 0)
b2d38a17 6729 {
93df3340
AM
6730 /* A corresponding .dynamic section is expected, but on
6731 IA-64/OpenVMS it is OK for it to be missing. */
6732 if (!is_ia64_vms (filedata))
6733 error (_("no .dynamic section in the dynamic segment\n"));
b2d38a17
NC
6734 break;
6735 }
6736
42bb2e33 6737 if (sec->sh_type == SHT_NOBITS)
20737c13 6738 {
93df3340
AM
6739 dynamic_addr = 0;
6740 dynamic_size = 0;
20737c13
AM
6741 break;
6742 }
42bb2e33 6743
93df3340
AM
6744 dynamic_addr = sec->sh_offset;
6745 dynamic_size = sec->sh_size;
b2d38a17 6746
8ac10c5b
L
6747 /* The PT_DYNAMIC segment, which is used by the run-time
6748 loader, should exactly match the .dynamic section. */
6749 if (do_checks
93df3340
AM
6750 && (dynamic_addr != segment->p_offset
6751 || dynamic_size != segment->p_filesz))
8ac10c5b
L
6752 warn (_("\
6753the .dynamic section is not the same as the dynamic segment\n"));
b2d38a17 6754 }
39e224f6
MW
6755
6756 /* PR binutils/17512: Avoid corrupt dynamic section info in the
6757 segment. Check this after matching against the section headers
6758 so we don't warn on debuginfo file (which have NOBITS .dynamic
6759 sections). */
93df3340
AM
6760 if (dynamic_addr > filedata->file_size
6761 || (dynamic_size > filedata->file_size - dynamic_addr))
39e224f6
MW
6762 {
6763 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
93df3340
AM
6764 dynamic_addr = 0;
6765 dynamic_size = 0;
39e224f6 6766 }
252b5132
RH
6767 break;
6768
6769 case PT_INTERP:
13acb58d
AM
6770 if (segment->p_offset >= filedata->file_size
6771 || segment->p_filesz > filedata->file_size - segment->p_offset
6772 || segment->p_filesz - 1 >= (size_t) -2
63cf857e
AM
6773 || fseek64 (filedata->handle,
6774 filedata->archive_file_offset + segment->p_offset,
6775 SEEK_SET))
252b5132
RH
6776 error (_("Unable to find program interpreter name\n"));
6777 else
6778 {
13acb58d
AM
6779 size_t len = segment->p_filesz;
6780 free (filedata->program_interpreter);
6781 filedata->program_interpreter = xmalloc (len + 1);
6782 len = fread (filedata->program_interpreter, 1, len,
6783 filedata->handle);
6784 filedata->program_interpreter[len] = 0;
252b5132
RH
6785
6786 if (do_segments)
f54498b4 6787 printf (_(" [Requesting program interpreter: %s]\n"),
978c4450 6788 filedata->program_interpreter);
252b5132
RH
6789 }
6790 break;
6791 }
252b5132
RH
6792 }
6793
dda8d76d
NC
6794 if (do_segments
6795 && filedata->section_headers != NULL
6796 && filedata->string_table != NULL)
252b5132
RH
6797 {
6798 printf (_("\n Section to Segment mapping:\n"));
6799 printf (_(" Segment Sections...\n"));
6800
dda8d76d 6801 for (i = 0; i < filedata->file_header.e_phnum; i++)
252b5132 6802 {
9ad5cbcf 6803 unsigned int j;
2cf0635d 6804 Elf_Internal_Shdr * section;
252b5132 6805
dda8d76d
NC
6806 segment = filedata->program_headers + i;
6807 section = filedata->section_headers + 1;
252b5132
RH
6808
6809 printf (" %2.2d ", i);
6810
dda8d76d 6811 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
252b5132 6812 {
f4638467
AM
6813 if (!ELF_TBSS_SPECIAL (section, segment)
6814 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
dda8d76d 6815 printf ("%s ", printable_section_name (filedata, section));
252b5132
RH
6816 }
6817
6818 putc ('\n',stdout);
6819 }
6820 }
6821
93df3340
AM
6822 filedata->dynamic_addr = dynamic_addr;
6823 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
6824 return;
6825
6826 no_headers:
6827 filedata->dynamic_addr = 0;
6828 filedata->dynamic_size = 1;
252b5132
RH
6829}
6830
6831
d93f0186
NC
6832/* Find the file offset corresponding to VMA by using the program headers. */
6833
26c527e6 6834static int64_t
625d49fc 6835offset_from_vma (Filedata * filedata, uint64_t vma, uint64_t size)
d93f0186 6836{
2cf0635d 6837 Elf_Internal_Phdr * seg;
d93f0186 6838
dda8d76d 6839 if (! get_program_headers (filedata))
d93f0186
NC
6840 {
6841 warn (_("Cannot interpret virtual addresses without program headers.\n"));
6842 return (long) vma;
6843 }
6844
dda8d76d
NC
6845 for (seg = filedata->program_headers;
6846 seg < filedata->program_headers + filedata->file_header.e_phnum;
d93f0186
NC
6847 ++seg)
6848 {
6849 if (seg->p_type != PT_LOAD)
6850 continue;
6851
6852 if (vma >= (seg->p_vaddr & -seg->p_align)
6853 && vma + size <= seg->p_vaddr + seg->p_filesz)
6854 return vma - seg->p_vaddr + seg->p_offset;
6855 }
6856
26c527e6
AM
6857 warn (_("Virtual address %#" PRIx64
6858 " not located in any PT_LOAD segment.\n"), vma);
6859 return vma;
d93f0186
NC
6860}
6861
6862
dda8d76d
NC
6863/* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
6864 If PROBE is true, this is just a probe and we do not generate any error
6865 messages if the load fails. */
049b0c3a 6866
015dc7e1
AM
6867static bool
6868get_32bit_section_headers (Filedata * filedata, bool probe)
252b5132 6869{
2cf0635d
NC
6870 Elf32_External_Shdr * shdrs;
6871 Elf_Internal_Shdr * internal;
dda8d76d
NC
6872 unsigned int i;
6873 unsigned int size = filedata->file_header.e_shentsize;
6874 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
049b0c3a
NC
6875
6876 /* PR binutils/17531: Cope with unexpected section header sizes. */
6877 if (size == 0 || num == 0)
015dc7e1 6878 return false;
907b52f4
NC
6879
6880 /* The section header cannot be at the start of the file - that is
6881 where the ELF file header is located. A file with absolutely no
6882 sections in it will use a shoff of 0. */
6883 if (filedata->file_header.e_shoff == 0)
6884 return false;
6885
049b0c3a
NC
6886 if (size < sizeof * shdrs)
6887 {
6888 if (! probe)
6889 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
015dc7e1 6890 return false;
049b0c3a
NC
6891 }
6892 if (!probe && size > sizeof * shdrs)
6893 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
252b5132 6894
dda8d76d 6895 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
049b0c3a
NC
6896 size, num,
6897 probe ? NULL : _("section headers"));
6898 if (shdrs == NULL)
015dc7e1 6899 return false;
252b5132 6900
dda8d76d
NC
6901 filedata->section_headers = (Elf_Internal_Shdr *)
6902 cmalloc (num, sizeof (Elf_Internal_Shdr));
6903 if (filedata->section_headers == NULL)
252b5132 6904 {
049b0c3a 6905 if (!probe)
8b73c356 6906 error (_("Out of memory reading %u section headers\n"), num);
e3d39609 6907 free (shdrs);
015dc7e1 6908 return false;
252b5132
RH
6909 }
6910
dda8d76d 6911 for (i = 0, internal = filedata->section_headers;
560f3c1c 6912 i < num;
b34976b6 6913 i++, internal++)
252b5132
RH
6914 {
6915 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6916 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6917 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6918 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6919 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6920 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6921 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6922 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6923 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6924 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
315350be
NC
6925 if (!probe && internal->sh_link > num)
6926 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6927 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6928 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
252b5132
RH
6929 }
6930
6931 free (shdrs);
015dc7e1 6932 return true;
252b5132
RH
6933}
6934
dda8d76d
NC
6935/* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
6936
015dc7e1
AM
6937static bool
6938get_64bit_section_headers (Filedata * filedata, bool probe)
9ea033b2 6939{
dda8d76d
NC
6940 Elf64_External_Shdr * shdrs;
6941 Elf_Internal_Shdr * internal;
6942 unsigned int i;
6943 unsigned int size = filedata->file_header.e_shentsize;
6944 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
049b0c3a
NC
6945
6946 /* PR binutils/17531: Cope with unexpected section header sizes. */
6947 if (size == 0 || num == 0)
015dc7e1 6948 return false;
dda8d76d 6949
907b52f4
NC
6950 /* The section header cannot be at the start of the file - that is
6951 where the ELF file header is located. A file with absolutely no
6952 sections in it will use a shoff of 0. */
6953 if (filedata->file_header.e_shoff == 0)
6954 return false;
6955
049b0c3a
NC
6956 if (size < sizeof * shdrs)
6957 {
6958 if (! probe)
6959 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
015dc7e1 6960 return false;
049b0c3a 6961 }
dda8d76d 6962
049b0c3a
NC
6963 if (! probe && size > sizeof * shdrs)
6964 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
9ea033b2 6965
dda8d76d
NC
6966 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
6967 filedata->file_header.e_shoff,
049b0c3a
NC
6968 size, num,
6969 probe ? NULL : _("section headers"));
6970 if (shdrs == NULL)
015dc7e1 6971 return false;
9ea033b2 6972
dda8d76d
NC
6973 filedata->section_headers = (Elf_Internal_Shdr *)
6974 cmalloc (num, sizeof (Elf_Internal_Shdr));
6975 if (filedata->section_headers == NULL)
9ea033b2 6976 {
049b0c3a 6977 if (! probe)
8b73c356 6978 error (_("Out of memory reading %u section headers\n"), num);
e3d39609 6979 free (shdrs);
015dc7e1 6980 return false;
9ea033b2
NC
6981 }
6982
dda8d76d 6983 for (i = 0, internal = filedata->section_headers;
560f3c1c 6984 i < num;
b34976b6 6985 i++, internal++)
9ea033b2
NC
6986 {
6987 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6988 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
66543521
AM
6989 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6990 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6991 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6992 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
9ea033b2
NC
6993 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6994 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6995 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6996 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
315350be
NC
6997 if (!probe && internal->sh_link > num)
6998 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6999 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
7000 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
9ea033b2
NC
7001 }
7002
7003 free (shdrs);
015dc7e1 7004 return true;
9ea033b2
NC
7005}
7006
4de91c10
AM
7007static bool
7008get_section_headers (Filedata *filedata, bool probe)
7009{
7010 if (filedata->section_headers != NULL)
7011 return true;
7012
4de91c10
AM
7013 if (is_32bit_elf)
7014 return get_32bit_section_headers (filedata, probe);
7015 else
7016 return get_64bit_section_headers (filedata, probe);
7017}
7018
252b5132 7019static Elf_Internal_Sym *
26c527e6
AM
7020get_32bit_elf_symbols (Filedata *filedata,
7021 Elf_Internal_Shdr *section,
7022 uint64_t *num_syms_return)
252b5132 7023{
26c527e6 7024 uint64_t number = 0;
dd24e3da 7025 Elf32_External_Sym * esyms = NULL;
ba5cdace 7026 Elf_External_Sym_Shndx * shndx = NULL;
dd24e3da 7027 Elf_Internal_Sym * isyms = NULL;
2cf0635d 7028 Elf_Internal_Sym * psym;
b34976b6 7029 unsigned int j;
e3d39609 7030 elf_section_list * entry;
252b5132 7031
c9c1d674
EG
7032 if (section->sh_size == 0)
7033 {
7034 if (num_syms_return != NULL)
7035 * num_syms_return = 0;
7036 return NULL;
7037 }
7038
dd24e3da 7039 /* Run some sanity checks first. */
c9c1d674 7040 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
dd24e3da 7041 {
26c527e6 7042 error (_("Section %s has an invalid sh_entsize of %#" PRIx64 "\n"),
dda8d76d 7043 printable_section_name (filedata, section),
26c527e6 7044 section->sh_entsize);
ba5cdace 7045 goto exit_point;
dd24e3da
NC
7046 }
7047
dda8d76d 7048 if (section->sh_size > filedata->file_size)
f54498b4 7049 {
26c527e6 7050 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
dda8d76d 7051 printable_section_name (filedata, section),
26c527e6 7052 section->sh_size);
f54498b4
NC
7053 goto exit_point;
7054 }
7055
dd24e3da
NC
7056 number = section->sh_size / section->sh_entsize;
7057
7058 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
7059 {
26c527e6
AM
7060 error (_("Size (%#" PRIx64 ") of section %s "
7061 "is not a multiple of its sh_entsize (%#" PRIx64 ")\n"),
7062 section->sh_size,
dda8d76d 7063 printable_section_name (filedata, section),
26c527e6 7064 section->sh_entsize);
ba5cdace 7065 goto exit_point;
dd24e3da
NC
7066 }
7067
dda8d76d 7068 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
3f5e193b 7069 section->sh_size, _("symbols"));
dd24e3da 7070 if (esyms == NULL)
ba5cdace 7071 goto exit_point;
252b5132 7072
e3d39609 7073 shndx = NULL;
978c4450 7074 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
e3d39609 7075 {
26c527e6 7076 if (entry->hdr->sh_link != (size_t) (section - filedata->section_headers))
e3d39609
NC
7077 continue;
7078
7079 if (shndx != NULL)
7080 {
7081 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
7082 free (shndx);
7083 }
7084
7085 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
7086 entry->hdr->sh_offset,
7087 1, entry->hdr->sh_size,
7088 _("symbol table section indices"));
7089 if (shndx == NULL)
7090 goto exit_point;
7091
7092 /* PR17531: file: heap-buffer-overflow */
7093 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
7094 {
26c527e6 7095 error (_("Index section %s has an sh_size of %#" PRIx64 " - expected %#" PRIx64 "\n"),
e3d39609 7096 printable_section_name (filedata, entry->hdr),
26c527e6
AM
7097 entry->hdr->sh_size,
7098 section->sh_size);
e3d39609 7099 goto exit_point;
c9c1d674 7100 }
e3d39609 7101 }
9ad5cbcf 7102
3f5e193b 7103 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
252b5132
RH
7104
7105 if (isyms == NULL)
7106 {
26c527e6 7107 error (_("Out of memory reading %" PRIu64 " symbols\n"), number);
dd24e3da 7108 goto exit_point;
252b5132
RH
7109 }
7110
dd24e3da 7111 for (j = 0, psym = isyms; j < number; j++, psym++)
252b5132
RH
7112 {
7113 psym->st_name = BYTE_GET (esyms[j].st_name);
7114 psym->st_value = BYTE_GET (esyms[j].st_value);
7115 psym->st_size = BYTE_GET (esyms[j].st_size);
7116 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
4fbb74a6 7117 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
9ad5cbcf
AM
7118 psym->st_shndx
7119 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
4fbb74a6
AM
7120 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
7121 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
252b5132
RH
7122 psym->st_info = BYTE_GET (esyms[j].st_info);
7123 psym->st_other = BYTE_GET (esyms[j].st_other);
7124 }
7125
dd24e3da 7126 exit_point:
e3d39609
NC
7127 free (shndx);
7128 free (esyms);
252b5132 7129
ba5cdace
NC
7130 if (num_syms_return != NULL)
7131 * num_syms_return = isyms == NULL ? 0 : number;
7132
252b5132
RH
7133 return isyms;
7134}
7135
9ea033b2 7136static Elf_Internal_Sym *
26c527e6
AM
7137get_64bit_elf_symbols (Filedata *filedata,
7138 Elf_Internal_Shdr *section,
7139 uint64_t *num_syms_return)
9ea033b2 7140{
26c527e6 7141 uint64_t number = 0;
ba5cdace
NC
7142 Elf64_External_Sym * esyms = NULL;
7143 Elf_External_Sym_Shndx * shndx = NULL;
7144 Elf_Internal_Sym * isyms = NULL;
2cf0635d 7145 Elf_Internal_Sym * psym;
b34976b6 7146 unsigned int j;
e3d39609 7147 elf_section_list * entry;
9ea033b2 7148
c9c1d674
EG
7149 if (section->sh_size == 0)
7150 {
7151 if (num_syms_return != NULL)
7152 * num_syms_return = 0;
7153 return NULL;
7154 }
7155
dd24e3da 7156 /* Run some sanity checks first. */
c9c1d674 7157 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
dd24e3da 7158 {
26c527e6 7159 error (_("Section %s has an invalid sh_entsize of %#" PRIx64 "\n"),
dda8d76d 7160 printable_section_name (filedata, section),
26c527e6 7161 section->sh_entsize);
ba5cdace 7162 goto exit_point;
dd24e3da
NC
7163 }
7164
dda8d76d 7165 if (section->sh_size > filedata->file_size)
f54498b4 7166 {
26c527e6 7167 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
dda8d76d 7168 printable_section_name (filedata, section),
26c527e6 7169 section->sh_size);
f54498b4
NC
7170 goto exit_point;
7171 }
7172
dd24e3da
NC
7173 number = section->sh_size / section->sh_entsize;
7174
7175 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
7176 {
26c527e6
AM
7177 error (_("Size (%#" PRIx64 ") of section %s "
7178 "is not a multiple of its sh_entsize (%#" PRIx64 ")\n"),
7179 section->sh_size,
dda8d76d 7180 printable_section_name (filedata, section),
26c527e6 7181 section->sh_entsize);
ba5cdace 7182 goto exit_point;
dd24e3da
NC
7183 }
7184
dda8d76d 7185 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
3f5e193b 7186 section->sh_size, _("symbols"));
a6e9f9df 7187 if (!esyms)
ba5cdace 7188 goto exit_point;
9ea033b2 7189
e3d39609 7190 shndx = NULL;
978c4450 7191 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
e3d39609 7192 {
26c527e6 7193 if (entry->hdr->sh_link != (size_t) (section - filedata->section_headers))
e3d39609
NC
7194 continue;
7195
7196 if (shndx != NULL)
7197 {
7198 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
7199 free (shndx);
c9c1d674 7200 }
e3d39609
NC
7201
7202 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
7203 entry->hdr->sh_offset,
7204 1, entry->hdr->sh_size,
7205 _("symbol table section indices"));
7206 if (shndx == NULL)
7207 goto exit_point;
7208
7209 /* PR17531: file: heap-buffer-overflow */
7210 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
7211 {
26c527e6 7212 error (_("Index section %s has an sh_size of %#" PRIx64 " - expected %#" PRIx64 "\n"),
e3d39609 7213 printable_section_name (filedata, entry->hdr),
26c527e6
AM
7214 entry->hdr->sh_size,
7215 section->sh_size);
e3d39609
NC
7216 goto exit_point;
7217 }
7218 }
9ad5cbcf 7219
3f5e193b 7220 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
9ea033b2
NC
7221
7222 if (isyms == NULL)
7223 {
26c527e6 7224 error (_("Out of memory reading %" PRIu64 " symbols\n"), number);
ba5cdace 7225 goto exit_point;
9ea033b2
NC
7226 }
7227
ba5cdace 7228 for (j = 0, psym = isyms; j < number; j++, psym++)
9ea033b2
NC
7229 {
7230 psym->st_name = BYTE_GET (esyms[j].st_name);
7231 psym->st_info = BYTE_GET (esyms[j].st_info);
7232 psym->st_other = BYTE_GET (esyms[j].st_other);
7233 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
ba5cdace 7234
4fbb74a6 7235 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
9ad5cbcf
AM
7236 psym->st_shndx
7237 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
4fbb74a6
AM
7238 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
7239 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
ba5cdace 7240
66543521
AM
7241 psym->st_value = BYTE_GET (esyms[j].st_value);
7242 psym->st_size = BYTE_GET (esyms[j].st_size);
9ea033b2
NC
7243 }
7244
ba5cdace 7245 exit_point:
e3d39609
NC
7246 free (shndx);
7247 free (esyms);
ba5cdace
NC
7248
7249 if (num_syms_return != NULL)
7250 * num_syms_return = isyms == NULL ? 0 : number;
9ea033b2
NC
7251
7252 return isyms;
7253}
7254
4de91c10
AM
7255static Elf_Internal_Sym *
7256get_elf_symbols (Filedata *filedata,
7257 Elf_Internal_Shdr *section,
26c527e6 7258 uint64_t *num_syms_return)
4de91c10
AM
7259{
7260 if (is_32bit_elf)
7261 return get_32bit_elf_symbols (filedata, section, num_syms_return);
7262 else
7263 return get_64bit_elf_symbols (filedata, section, num_syms_return);
7264}
7265
d1133906 7266static const char *
625d49fc 7267get_elf_section_flags (Filedata * filedata, uint64_t sh_flags)
d1133906 7268{
5477e8a0 7269 static char buff[1024];
2cf0635d 7270 char * p = buff;
32ec8896
NC
7271 unsigned int field_size = is_32bit_elf ? 8 : 16;
7272 signed int sindex;
7273 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
625d49fc
AM
7274 uint64_t os_flags = 0;
7275 uint64_t proc_flags = 0;
7276 uint64_t unknown_flags = 0;
148b93f2 7277 static const struct
5477e8a0 7278 {
2cf0635d 7279 const char * str;
32ec8896 7280 unsigned int len;
5477e8a0
L
7281 }
7282 flags [] =
7283 {
cfcac11d
NC
7284 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
7285 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
7286 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
7287 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
7288 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
7289 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
7290 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
7291 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
7292 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
7293 /* 9 */ { STRING_COMMA_LEN ("TLS") },
7294 /* IA-64 specific. */
7295 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
7296 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
7297 /* IA-64 OpenVMS specific. */
7298 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
7299 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
7300 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
7301 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
7302 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
7303 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
18ae9cc1 7304 /* Generic. */
cfcac11d 7305 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
18ae9cc1 7306 /* SPARC specific. */
77115a4a 7307 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
ac4c9b04
MG
7308 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
7309 /* ARM specific. */
7310 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
f0728ee3 7311 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
a91e1603
L
7312 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
7313 /* GNU specific. */
7314 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
83eef883
AFB
7315 /* VLE specific. */
7316 /* 25 */ { STRING_COMMA_LEN ("VLE") },
99fabbc9
JL
7317 /* GNU specific. */
7318 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
5477e8a0
L
7319 };
7320
7321 if (do_section_details)
f8c4789c
AM
7322 p += sprintf (p, "[%*.*lx]: ",
7323 field_size, field_size, (unsigned long) sh_flags);
76da6bbe 7324
d1133906
NC
7325 while (sh_flags)
7326 {
625d49fc 7327 uint64_t flag;
d1133906
NC
7328
7329 flag = sh_flags & - sh_flags;
7330 sh_flags &= ~ flag;
76da6bbe 7331
5477e8a0 7332 if (do_section_details)
d1133906 7333 {
5477e8a0
L
7334 switch (flag)
7335 {
91d6fa6a
NC
7336 case SHF_WRITE: sindex = 0; break;
7337 case SHF_ALLOC: sindex = 1; break;
7338 case SHF_EXECINSTR: sindex = 2; break;
7339 case SHF_MERGE: sindex = 3; break;
7340 case SHF_STRINGS: sindex = 4; break;
7341 case SHF_INFO_LINK: sindex = 5; break;
7342 case SHF_LINK_ORDER: sindex = 6; break;
7343 case SHF_OS_NONCONFORMING: sindex = 7; break;
7344 case SHF_GROUP: sindex = 8; break;
7345 case SHF_TLS: sindex = 9; break;
18ae9cc1 7346 case SHF_EXCLUDE: sindex = 18; break;
77115a4a 7347 case SHF_COMPRESSED: sindex = 20; break;
76da6bbe 7348
5477e8a0 7349 default:
91d6fa6a 7350 sindex = -1;
dda8d76d 7351 switch (filedata->file_header.e_machine)
148b93f2 7352 {
cfcac11d 7353 case EM_IA_64:
148b93f2 7354 if (flag == SHF_IA_64_SHORT)
91d6fa6a 7355 sindex = 10;
148b93f2 7356 else if (flag == SHF_IA_64_NORECOV)
91d6fa6a 7357 sindex = 11;
dda8d76d 7358 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
148b93f2
NC
7359 switch (flag)
7360 {
91d6fa6a
NC
7361 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
7362 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
7363 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
7364 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
7365 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
7366 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
148b93f2
NC
7367 default: break;
7368 }
cfcac11d
NC
7369 break;
7370
caa83f8b 7371 case EM_386:
22abe556 7372 case EM_IAMCU:
caa83f8b 7373 case EM_X86_64:
7f502d6c 7374 case EM_L1OM:
7a9068fe 7375 case EM_K1OM:
cfcac11d
NC
7376 case EM_OLD_SPARCV9:
7377 case EM_SPARC32PLUS:
7378 case EM_SPARCV9:
7379 case EM_SPARC:
18ae9cc1 7380 if (flag == SHF_ORDERED)
91d6fa6a 7381 sindex = 19;
cfcac11d 7382 break;
ac4c9b04
MG
7383
7384 case EM_ARM:
7385 switch (flag)
7386 {
7387 case SHF_ENTRYSECT: sindex = 21; break;
f0728ee3 7388 case SHF_ARM_PURECODE: sindex = 22; break;
ac4c9b04
MG
7389 case SHF_COMDEF: sindex = 23; break;
7390 default: break;
7391 }
7392 break;
83eef883
AFB
7393 case EM_PPC:
7394 if (flag == SHF_PPC_VLE)
7395 sindex = 25;
7396 break;
99fabbc9
JL
7397 default:
7398 break;
7399 }
ac4c9b04 7400
99fabbc9
JL
7401 switch (filedata->file_header.e_ident[EI_OSABI])
7402 {
7403 case ELFOSABI_GNU:
7404 case ELFOSABI_FREEBSD:
7405 if (flag == SHF_GNU_RETAIN)
7406 sindex = 26;
7407 /* Fall through */
7408 case ELFOSABI_NONE:
7409 if (flag == SHF_GNU_MBIND)
7410 /* We should not recognize SHF_GNU_MBIND for
7411 ELFOSABI_NONE, but binutils as of 2019-07-23 did
7412 not set the EI_OSABI header byte. */
7413 sindex = 24;
7414 break;
cfcac11d
NC
7415 default:
7416 break;
148b93f2 7417 }
99fabbc9 7418 break;
5477e8a0
L
7419 }
7420
91d6fa6a 7421 if (sindex != -1)
5477e8a0 7422 {
8d5ff12c
L
7423 if (p != buff + field_size + 4)
7424 {
7425 if (size < (10 + 2))
bee0ee85
NC
7426 {
7427 warn (_("Internal error: not enough buffer room for section flag info"));
7428 return _("<unknown>");
7429 }
8d5ff12c
L
7430 size -= 2;
7431 *p++ = ',';
7432 *p++ = ' ';
7433 }
7434
91d6fa6a
NC
7435 size -= flags [sindex].len;
7436 p = stpcpy (p, flags [sindex].str);
5477e8a0 7437 }
3b22753a 7438 else if (flag & SHF_MASKOS)
8d5ff12c 7439 os_flags |= flag;
d1133906 7440 else if (flag & SHF_MASKPROC)
8d5ff12c 7441 proc_flags |= flag;
d1133906 7442 else
8d5ff12c 7443 unknown_flags |= flag;
5477e8a0
L
7444 }
7445 else
7446 {
7447 switch (flag)
7448 {
7449 case SHF_WRITE: *p = 'W'; break;
7450 case SHF_ALLOC: *p = 'A'; break;
7451 case SHF_EXECINSTR: *p = 'X'; break;
7452 case SHF_MERGE: *p = 'M'; break;
7453 case SHF_STRINGS: *p = 'S'; break;
7454 case SHF_INFO_LINK: *p = 'I'; break;
7455 case SHF_LINK_ORDER: *p = 'L'; break;
7456 case SHF_OS_NONCONFORMING: *p = 'O'; break;
7457 case SHF_GROUP: *p = 'G'; break;
7458 case SHF_TLS: *p = 'T'; break;
18ae9cc1 7459 case SHF_EXCLUDE: *p = 'E'; break;
77115a4a 7460 case SHF_COMPRESSED: *p = 'C'; break;
5477e8a0
L
7461
7462 default:
dda8d76d
NC
7463 if ((filedata->file_header.e_machine == EM_X86_64
7464 || filedata->file_header.e_machine == EM_L1OM
7465 || filedata->file_header.e_machine == EM_K1OM)
5477e8a0
L
7466 && flag == SHF_X86_64_LARGE)
7467 *p = 'l';
dda8d76d 7468 else if (filedata->file_header.e_machine == EM_ARM
f0728ee3 7469 && flag == SHF_ARM_PURECODE)
99fabbc9 7470 *p = 'y';
dda8d76d 7471 else if (filedata->file_header.e_machine == EM_PPC
83eef883 7472 && flag == SHF_PPC_VLE)
99fabbc9 7473 *p = 'v';
5477e8a0
L
7474 else if (flag & SHF_MASKOS)
7475 {
99fabbc9
JL
7476 switch (filedata->file_header.e_ident[EI_OSABI])
7477 {
7478 case ELFOSABI_GNU:
7479 case ELFOSABI_FREEBSD:
7480 if (flag == SHF_GNU_RETAIN)
7481 {
7482 *p = 'R';
7483 break;
7484 }
7485 /* Fall through */
7486 case ELFOSABI_NONE:
7487 if (flag == SHF_GNU_MBIND)
7488 {
7489 /* We should not recognize SHF_GNU_MBIND for
7490 ELFOSABI_NONE, but binutils as of 2019-07-23 did
7491 not set the EI_OSABI header byte. */
7492 *p = 'D';
7493 break;
7494 }
7495 /* Fall through */
7496 default:
7497 *p = 'o';
7498 sh_flags &= ~SHF_MASKOS;
7499 break;
7500 }
5477e8a0
L
7501 }
7502 else if (flag & SHF_MASKPROC)
7503 {
7504 *p = 'p';
7505 sh_flags &= ~ SHF_MASKPROC;
7506 }
7507 else
7508 *p = 'x';
7509 break;
7510 }
7511 p++;
d1133906
NC
7512 }
7513 }
76da6bbe 7514
8d5ff12c
L
7515 if (do_section_details)
7516 {
7517 if (os_flags)
7518 {
8d5ff12c
L
7519 if (p != buff + field_size + 4)
7520 {
f8c4789c 7521 if (size < 2 + 5 + field_size + 1)
bee0ee85
NC
7522 {
7523 warn (_("Internal error: not enough buffer room for section flag info"));
7524 return _("<unknown>");
7525 }
8d5ff12c
L
7526 size -= 2;
7527 *p++ = ',';
7528 *p++ = ' ';
7529 }
f8c4789c
AM
7530 size -= 5 + field_size;
7531 p += sprintf (p, "OS (%*.*lx)", field_size, field_size,
7532 (unsigned long) os_flags);
8d5ff12c
L
7533 }
7534 if (proc_flags)
7535 {
8d5ff12c
L
7536 if (p != buff + field_size + 4)
7537 {
f8c4789c 7538 if (size < 2 + 7 + field_size + 1)
bee0ee85
NC
7539 {
7540 warn (_("Internal error: not enough buffer room for section flag info"));
7541 return _("<unknown>");
7542 }
8d5ff12c
L
7543 size -= 2;
7544 *p++ = ',';
7545 *p++ = ' ';
7546 }
f8c4789c
AM
7547 size -= 7 + field_size;
7548 p += sprintf (p, "PROC (%*.*lx)", field_size, field_size,
7549 (unsigned long) proc_flags);
8d5ff12c
L
7550 }
7551 if (unknown_flags)
7552 {
8d5ff12c
L
7553 if (p != buff + field_size + 4)
7554 {
f8c4789c 7555 if (size < 2 + 10 + field_size + 1)
bee0ee85
NC
7556 {
7557 warn (_("Internal error: not enough buffer room for section flag info"));
7558 return _("<unknown>");
7559 }
8d5ff12c
L
7560 size -= 2;
7561 *p++ = ',';
7562 *p++ = ' ';
7563 }
f8c4789c
AM
7564 size -= 10 + field_size;
7565 p += sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
7566 (unsigned long) unknown_flags);
8d5ff12c
L
7567 }
7568 }
7569
e9e44622 7570 *p = '\0';
d1133906
NC
7571 return buff;
7572}
7573
5844b465 7574static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
be7d229a
AM
7575get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf,
7576 uint64_t size)
77115a4a
L
7577{
7578 if (is_32bit_elf)
7579 {
7580 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
d8024a91 7581
ebdf1ebf
NC
7582 if (size < sizeof (* echdr))
7583 {
7584 error (_("Compressed section is too small even for a compression header\n"));
7585 return 0;
7586 }
7587
77115a4a
L
7588 chdr->ch_type = BYTE_GET (echdr->ch_type);
7589 chdr->ch_size = BYTE_GET (echdr->ch_size);
7590 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7591 return sizeof (*echdr);
7592 }
7593 else
7594 {
7595 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
d8024a91 7596
ebdf1ebf
NC
7597 if (size < sizeof (* echdr))
7598 {
7599 error (_("Compressed section is too small even for a compression header\n"));
7600 return 0;
7601 }
7602
77115a4a
L
7603 chdr->ch_type = BYTE_GET (echdr->ch_type);
7604 chdr->ch_size = BYTE_GET (echdr->ch_size);
7605 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7606 return sizeof (*echdr);
7607 }
7608}
7609
015dc7e1 7610static bool
dda8d76d 7611process_section_headers (Filedata * filedata)
252b5132 7612{
2cf0635d 7613 Elf_Internal_Shdr * section;
b34976b6 7614 unsigned int i;
252b5132 7615
dda8d76d 7616 if (filedata->file_header.e_shnum == 0)
252b5132 7617 {
82f2dbf7 7618 /* PR binutils/12467. */
dda8d76d 7619 if (filedata->file_header.e_shoff != 0)
32ec8896
NC
7620 {
7621 warn (_("possibly corrupt ELF file header - it has a non-zero"
7622 " section header offset, but no section headers\n"));
015dc7e1 7623 return false;
32ec8896 7624 }
82f2dbf7 7625 else if (do_sections)
252b5132
RH
7626 printf (_("\nThere are no sections in this file.\n"));
7627
015dc7e1 7628 return true;
252b5132
RH
7629 }
7630
7631 if (do_sections && !do_header)
ca0e11aa
NC
7632 {
7633 if (filedata->is_separate && process_links)
7634 printf (_("In linked file '%s': "), filedata->file_name);
7635 if (! filedata->is_separate || process_links)
7636 printf (ngettext ("There is %d section header, "
26c527e6 7637 "starting at offset %#" PRIx64 ":\n",
ca0e11aa 7638 "There are %d section headers, "
26c527e6 7639 "starting at offset %#" PRIx64 ":\n",
ca0e11aa
NC
7640 filedata->file_header.e_shnum),
7641 filedata->file_header.e_shnum,
26c527e6 7642 filedata->file_header.e_shoff);
ca0e11aa 7643 }
252b5132 7644
4de91c10
AM
7645 if (!get_section_headers (filedata, false))
7646 return false;
252b5132
RH
7647
7648 /* Read in the string table, so that we have names to display. */
dda8d76d
NC
7649 if (filedata->file_header.e_shstrndx != SHN_UNDEF
7650 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
252b5132 7651 {
dda8d76d 7652 section = filedata->section_headers + filedata->file_header.e_shstrndx;
d40ac9bd 7653
c256ffe7
JJ
7654 if (section->sh_size != 0)
7655 {
dda8d76d
NC
7656 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
7657 1, section->sh_size,
7658 _("string table"));
0de14b54 7659
dda8d76d 7660 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
c256ffe7 7661 }
252b5132
RH
7662 }
7663
7664 /* Scan the sections for the dynamic symbol table
e3c8793a 7665 and dynamic string table and debug sections. */
89fac5e3 7666 eh_addr_size = is_32bit_elf ? 4 : 8;
dda8d76d 7667 switch (filedata->file_header.e_machine)
89fac5e3
RS
7668 {
7669 case EM_MIPS:
7670 case EM_MIPS_RS3_LE:
7671 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
7672 FDE addresses. However, the ABI also has a semi-official ILP32
7673 variant for which the normal FDE address size rules apply.
7674
7675 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
7676 section, where XX is the size of longs in bits. Unfortunately,
7677 earlier compilers provided no way of distinguishing ILP32 objects
7678 from LP64 objects, so if there's any doubt, we should assume that
7679 the official LP64 form is being used. */
dda8d76d
NC
7680 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
7681 && find_section (filedata, ".gcc_compiled_long32") == NULL)
89fac5e3
RS
7682 eh_addr_size = 8;
7683 break;
0f56a26a
DD
7684
7685 case EM_H8_300:
7686 case EM_H8_300H:
dda8d76d 7687 switch (filedata->file_header.e_flags & EF_H8_MACH)
0f56a26a
DD
7688 {
7689 case E_H8_MACH_H8300:
7690 case E_H8_MACH_H8300HN:
7691 case E_H8_MACH_H8300SN:
7692 case E_H8_MACH_H8300SXN:
7693 eh_addr_size = 2;
7694 break;
7695 case E_H8_MACH_H8300H:
7696 case E_H8_MACH_H8300S:
7697 case E_H8_MACH_H8300SX:
7698 eh_addr_size = 4;
7699 break;
7700 }
f4236fe4
DD
7701 break;
7702
ff7eeb89 7703 case EM_M32C_OLD:
f4236fe4 7704 case EM_M32C:
dda8d76d 7705 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
f4236fe4
DD
7706 {
7707 case EF_M32C_CPU_M16C:
7708 eh_addr_size = 2;
7709 break;
7710 }
7711 break;
89fac5e3
RS
7712 }
7713
76ca31c0
NC
7714#define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
7715 do \
7716 { \
be7d229a 7717 uint64_t expected_entsize = is_32bit_elf ? size32 : size64; \
76ca31c0 7718 if (section->sh_entsize != expected_entsize) \
9dd3a467 7719 { \
f493c217 7720 error (_("Section %d has invalid sh_entsize of %" PRIx64 "\n"), \
625d49fc 7721 i, section->sh_entsize); \
f493c217 7722 error (_("(Using the expected size of %" PRIx64 " for the rest of this dump)\n"), \
be7d229a 7723 expected_entsize); \
9dd3a467 7724 section->sh_entsize = expected_entsize; \
76ca31c0
NC
7725 } \
7726 } \
08d8fa11 7727 while (0)
9dd3a467
NC
7728
7729#define CHECK_ENTSIZE(section, i, type) \
1b513401 7730 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
08d8fa11
JJ
7731 sizeof (Elf64_External_##type))
7732
dda8d76d
NC
7733 for (i = 0, section = filedata->section_headers;
7734 i < filedata->file_header.e_shnum;
b34976b6 7735 i++, section++)
252b5132 7736 {
b6ac461a 7737 const char *name = printable_section_name (filedata, section);
252b5132 7738
1b513401
NC
7739 /* Run some sanity checks on the headers and
7740 possibly fill in some file data as well. */
7741 switch (section->sh_type)
252b5132 7742 {
1b513401 7743 case SHT_DYNSYM:
978c4450 7744 if (filedata->dynamic_symbols != NULL)
252b5132
RH
7745 {
7746 error (_("File contains multiple dynamic symbol tables\n"));
7747 continue;
7748 }
7749
08d8fa11 7750 CHECK_ENTSIZE (section, i, Sym);
978c4450 7751 filedata->dynamic_symbols
4de91c10 7752 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
8ac10c5b 7753 filedata->dynamic_symtab_section = section;
1b513401
NC
7754 break;
7755
7756 case SHT_STRTAB:
7757 if (streq (name, ".dynstr"))
252b5132 7758 {
1b513401
NC
7759 if (filedata->dynamic_strings != NULL)
7760 {
7761 error (_("File contains multiple dynamic string tables\n"));
7762 continue;
7763 }
7764
7765 filedata->dynamic_strings
7766 = (char *) get_data (NULL, filedata, section->sh_offset,
7767 1, section->sh_size, _("dynamic strings"));
7768 filedata->dynamic_strings_length
7769 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
8ac10c5b 7770 filedata->dynamic_strtab_section = section;
252b5132 7771 }
1b513401
NC
7772 break;
7773
7774 case SHT_SYMTAB_SHNDX:
7775 {
7776 elf_section_list * entry = xmalloc (sizeof * entry);
7777
7778 entry->hdr = section;
7779 entry->next = filedata->symtab_shndx_list;
7780 filedata->symtab_shndx_list = entry;
7781 }
7782 break;
7783
7784 case SHT_SYMTAB:
7785 CHECK_ENTSIZE (section, i, Sym);
7786 break;
7787
7788 case SHT_GROUP:
7789 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
7790 break;
252b5132 7791
1b513401
NC
7792 case SHT_REL:
7793 CHECK_ENTSIZE (section, i, Rel);
546cb2d8 7794 if (do_checks && section->sh_size == 0)
1b513401
NC
7795 warn (_("Section '%s': zero-sized relocation section\n"), name);
7796 break;
7797
7798 case SHT_RELA:
7799 CHECK_ENTSIZE (section, i, Rela);
546cb2d8 7800 if (do_checks && section->sh_size == 0)
1b513401
NC
7801 warn (_("Section '%s': zero-sized relocation section\n"), name);
7802 break;
7803
682351b9
AM
7804 case SHT_RELR:
7805 CHECK_ENTSIZE (section, i, Relr);
7806 break;
7807
1b513401
NC
7808 case SHT_NOTE:
7809 case SHT_PROGBITS:
546cb2d8
NC
7810 /* Having a zero sized section is not illegal according to the
7811 ELF standard, but it might be an indication that something
7812 is wrong. So issue a warning if we are running in lint mode. */
7813 if (do_checks && section->sh_size == 0)
1b513401
NC
7814 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
7815 break;
7816
7817 default:
7818 break;
7819 }
7820
7821 if ((do_debugging || do_debug_info || do_debug_abbrevs
7822 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
7823 || do_debug_aranges || do_debug_frames || do_debug_macinfo
e38332c2
NC
7824 || do_debug_str || do_debug_str_offsets || do_debug_loc
7825 || do_debug_ranges
1b513401 7826 || do_debug_addr || do_debug_cu_index || do_debug_links)
24d127aa
ML
7827 && (startswith (name, ".debug_")
7828 || startswith (name, ".zdebug_")))
252b5132 7829 {
1b315056
CS
7830 if (name[1] == 'z')
7831 name += sizeof (".zdebug_") - 1;
7832 else
7833 name += sizeof (".debug_") - 1;
252b5132
RH
7834
7835 if (do_debugging
24d127aa
ML
7836 || (do_debug_info && startswith (name, "info"))
7837 || (do_debug_info && startswith (name, "types"))
7838 || (do_debug_abbrevs && startswith (name, "abbrev"))
b40bf0a2 7839 || (do_debug_lines && strcmp (name, "line") == 0)
24d127aa
ML
7840 || (do_debug_lines && startswith (name, "line."))
7841 || (do_debug_pubnames && startswith (name, "pubnames"))
7842 || (do_debug_pubtypes && startswith (name, "pubtypes"))
7843 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
7844 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
7845 || (do_debug_aranges && startswith (name, "aranges"))
7846 || (do_debug_ranges && startswith (name, "ranges"))
7847 || (do_debug_ranges && startswith (name, "rnglists"))
7848 || (do_debug_frames && startswith (name, "frame"))
7849 || (do_debug_macinfo && startswith (name, "macinfo"))
7850 || (do_debug_macinfo && startswith (name, "macro"))
7851 || (do_debug_str && startswith (name, "str"))
7852 || (do_debug_links && startswith (name, "sup"))
7853 || (do_debug_str_offsets && startswith (name, "str_offsets"))
7854 || (do_debug_loc && startswith (name, "loc"))
7855 || (do_debug_loc && startswith (name, "loclists"))
7856 || (do_debug_addr && startswith (name, "addr"))
7857 || (do_debug_cu_index && startswith (name, "cu_index"))
7858 || (do_debug_cu_index && startswith (name, "tu_index"))
252b5132 7859 )
6431e409 7860 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
252b5132 7861 }
a262ae96 7862 /* Linkonce section to be combined with .debug_info at link time. */
09fd7e38 7863 else if ((do_debugging || do_debug_info)
24d127aa 7864 && startswith (name, ".gnu.linkonce.wi."))
6431e409 7865 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
18bd398b 7866 else if (do_debug_frames && streq (name, ".eh_frame"))
6431e409 7867 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
61364358
JK
7868 else if (do_gdb_index && (streq (name, ".gdb_index")
7869 || streq (name, ".debug_names")))
6431e409 7870 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6f875884
TG
7871 /* Trace sections for Itanium VMS. */
7872 else if ((do_debugging || do_trace_info || do_trace_abbrevs
7873 || do_trace_aranges)
24d127aa 7874 && startswith (name, ".trace_"))
6f875884
TG
7875 {
7876 name += sizeof (".trace_") - 1;
7877
7878 if (do_debugging
7879 || (do_trace_info && streq (name, "info"))
7880 || (do_trace_abbrevs && streq (name, "abbrev"))
7881 || (do_trace_aranges && streq (name, "aranges"))
7882 )
6431e409 7883 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6f875884 7884 }
dda8d76d 7885 else if ((do_debugging || do_debug_links)
24d127aa
ML
7886 && (startswith (name, ".gnu_debuglink")
7887 || startswith (name, ".gnu_debugaltlink")))
6431e409 7888 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
252b5132
RH
7889 }
7890
7891 if (! do_sections)
015dc7e1 7892 return true;
252b5132 7893
ca0e11aa 7894 if (filedata->is_separate && ! process_links)
015dc7e1 7895 return true;
ca0e11aa
NC
7896
7897 if (filedata->is_separate)
7898 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
7899 else if (filedata->file_header.e_shnum > 1)
3a1a2036
NC
7900 printf (_("\nSection Headers:\n"));
7901 else
7902 printf (_("\nSection Header:\n"));
76da6bbe 7903
f7a99963 7904 if (is_32bit_elf)
595cf52e 7905 {
5477e8a0 7906 if (do_section_details)
595cf52e
L
7907 {
7908 printf (_(" [Nr] Name\n"));
5477e8a0 7909 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
595cf52e
L
7910 }
7911 else
7912 printf
7913 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
7914 }
d974e256 7915 else if (do_wide)
595cf52e 7916 {
5477e8a0 7917 if (do_section_details)
595cf52e
L
7918 {
7919 printf (_(" [Nr] Name\n"));
5477e8a0 7920 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
595cf52e
L
7921 }
7922 else
7923 printf
7924 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
7925 }
f7a99963
NC
7926 else
7927 {
5477e8a0 7928 if (do_section_details)
595cf52e
L
7929 {
7930 printf (_(" [Nr] Name\n"));
5477e8a0
L
7931 printf (_(" Type Address Offset Link\n"));
7932 printf (_(" Size EntSize Info Align\n"));
595cf52e
L
7933 }
7934 else
7935 {
7936 printf (_(" [Nr] Name Type Address Offset\n"));
7937 printf (_(" Size EntSize Flags Link Info Align\n"));
7938 }
f7a99963 7939 }
252b5132 7940
5477e8a0
L
7941 if (do_section_details)
7942 printf (_(" Flags\n"));
7943
dda8d76d
NC
7944 for (i = 0, section = filedata->section_headers;
7945 i < filedata->file_header.e_shnum;
b34976b6 7946 i++, section++)
252b5132 7947 {
dd905818
NC
7948 /* Run some sanity checks on the section header. */
7949
7950 /* Check the sh_link field. */
7951 switch (section->sh_type)
7952 {
285e3f99
AM
7953 case SHT_REL:
7954 case SHT_RELA:
7955 if (section->sh_link == 0
7956 && (filedata->file_header.e_type == ET_EXEC
7957 || filedata->file_header.e_type == ET_DYN))
7958 /* A dynamic relocation section where all entries use a
7959 zero symbol index need not specify a symtab section. */
7960 break;
7961 /* Fall through. */
dd905818
NC
7962 case SHT_SYMTAB_SHNDX:
7963 case SHT_GROUP:
7964 case SHT_HASH:
7965 case SHT_GNU_HASH:
7966 case SHT_GNU_versym:
285e3f99 7967 if (section->sh_link == 0
dda8d76d
NC
7968 || section->sh_link >= filedata->file_header.e_shnum
7969 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
7970 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
dd905818
NC
7971 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
7972 i, section->sh_link);
7973 break;
7974
7975 case SHT_DYNAMIC:
7976 case SHT_SYMTAB:
7977 case SHT_DYNSYM:
7978 case SHT_GNU_verneed:
7979 case SHT_GNU_verdef:
7980 case SHT_GNU_LIBLIST:
285e3f99 7981 if (section->sh_link == 0
dda8d76d
NC
7982 || section->sh_link >= filedata->file_header.e_shnum
7983 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
dd905818
NC
7984 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
7985 i, section->sh_link);
7986 break;
7987
7988 case SHT_INIT_ARRAY:
7989 case SHT_FINI_ARRAY:
7990 case SHT_PREINIT_ARRAY:
7991 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7992 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7993 i, section->sh_link);
7994 break;
7995
7996 default:
7997 /* FIXME: Add support for target specific section types. */
7998#if 0 /* Currently we do not check other section types as there are too
7999 many special cases. Stab sections for example have a type
8000 of SHT_PROGBITS but an sh_link field that links to the .stabstr
8001 section. */
8002 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
8003 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
8004 i, section->sh_link);
8005#endif
8006 break;
8007 }
8008
8009 /* Check the sh_info field. */
8010 switch (section->sh_type)
8011 {
8012 case SHT_REL:
8013 case SHT_RELA:
285e3f99
AM
8014 if (section->sh_info == 0
8015 && (filedata->file_header.e_type == ET_EXEC
8016 || filedata->file_header.e_type == ET_DYN))
8017 /* Dynamic relocations apply to segments, so they do not
8018 need to specify the section they relocate. */
8019 break;
8020 if (section->sh_info == 0
dda8d76d
NC
8021 || section->sh_info >= filedata->file_header.e_shnum
8022 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
8023 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
8024 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
8025 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
385e5b90
L
8026 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
8027 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
dd905818 8028 /* FIXME: Are other section types valid ? */
dda8d76d 8029 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
285e3f99
AM
8030 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
8031 i, section->sh_info);
dd905818
NC
8032 break;
8033
8034 case SHT_DYNAMIC:
8035 case SHT_HASH:
8036 case SHT_SYMTAB_SHNDX:
8037 case SHT_INIT_ARRAY:
8038 case SHT_FINI_ARRAY:
8039 case SHT_PREINIT_ARRAY:
8040 if (section->sh_info != 0)
8041 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
8042 i, section->sh_info);
8043 break;
8044
8045 case SHT_GROUP:
8046 case SHT_SYMTAB:
8047 case SHT_DYNSYM:
8048 /* A symbol index - we assume that it is valid. */
8049 break;
8050
8051 default:
8052 /* FIXME: Add support for target specific section types. */
8053 if (section->sh_type == SHT_NOBITS)
8054 /* NOBITS section headers with non-zero sh_info fields can be
8055 created when a binary is stripped of everything but its debug
1a9ccd70
NC
8056 information. The stripped sections have their headers
8057 preserved but their types set to SHT_NOBITS. So do not check
8058 this type of section. */
dd905818
NC
8059 ;
8060 else if (section->sh_flags & SHF_INFO_LINK)
8061 {
dda8d76d 8062 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
dd905818
NC
8063 warn (_("[%2u]: Expected link to another section in info field"), i);
8064 }
a91e1603
L
8065 else if (section->sh_type < SHT_LOOS
8066 && (section->sh_flags & SHF_GNU_MBIND) == 0
8067 && section->sh_info != 0)
dd905818
NC
8068 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
8069 i, section->sh_info);
8070 break;
8071 }
8072
3e6b6445 8073 /* Check the sh_size field. */
dda8d76d 8074 if (section->sh_size > filedata->file_size
3e6b6445
NC
8075 && section->sh_type != SHT_NOBITS
8076 && section->sh_type != SHT_NULL
8077 && section->sh_type < SHT_LOOS)
8078 warn (_("Size of section %u is larger than the entire file!\n"), i);
8079
7bfd842d 8080 printf (" [%2u] ", i);
5477e8a0 8081 if (do_section_details)
dda8d76d 8082 printf ("%s\n ", printable_section_name (filedata, section));
595cf52e 8083 else
b6ac461a 8084 print_symbol_name (-17, printable_section_name (filedata, section));
0b4362b0 8085
ea52a088 8086 printf (do_wide ? " %-15s " : " %-15.15s ",
dda8d76d 8087 get_section_type_name (filedata, section->sh_type));
0b4362b0 8088
f7a99963
NC
8089 if (is_32bit_elf)
8090 {
cfcac11d
NC
8091 const char * link_too_big = NULL;
8092
f7a99963 8093 print_vma (section->sh_addr, LONG_HEX);
76da6bbe 8094
f7a99963
NC
8095 printf ( " %6.6lx %6.6lx %2.2lx",
8096 (unsigned long) section->sh_offset,
8097 (unsigned long) section->sh_size,
8098 (unsigned long) section->sh_entsize);
d1133906 8099
5477e8a0
L
8100 if (do_section_details)
8101 fputs (" ", stdout);
8102 else
dda8d76d 8103 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
76da6bbe 8104
dda8d76d 8105 if (section->sh_link >= filedata->file_header.e_shnum)
cfcac11d
NC
8106 {
8107 link_too_big = "";
8108 /* The sh_link value is out of range. Normally this indicates
caa83f8b 8109 an error but it can have special values in Solaris binaries. */
dda8d76d 8110 switch (filedata->file_header.e_machine)
cfcac11d 8111 {
caa83f8b 8112 case EM_386:
22abe556 8113 case EM_IAMCU:
caa83f8b 8114 case EM_X86_64:
7f502d6c 8115 case EM_L1OM:
7a9068fe 8116 case EM_K1OM:
cfcac11d
NC
8117 case EM_OLD_SPARCV9:
8118 case EM_SPARC32PLUS:
8119 case EM_SPARCV9:
8120 case EM_SPARC:
8121 if (section->sh_link == (SHN_BEFORE & 0xffff))
8122 link_too_big = "BEFORE";
8123 else if (section->sh_link == (SHN_AFTER & 0xffff))
8124 link_too_big = "AFTER";
8125 break;
8126 default:
8127 break;
8128 }
8129 }
8130
8131 if (do_section_details)
8132 {
8133 if (link_too_big != NULL && * link_too_big)
8134 printf ("<%s> ", link_too_big);
8135 else
8136 printf ("%2u ", section->sh_link);
8137 printf ("%3u %2lu\n", section->sh_info,
8138 (unsigned long) section->sh_addralign);
8139 }
8140 else
8141 printf ("%2u %3u %2lu\n",
8142 section->sh_link,
8143 section->sh_info,
8144 (unsigned long) section->sh_addralign);
8145
8146 if (link_too_big && ! * link_too_big)
8147 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
8148 i, section->sh_link);
f7a99963 8149 }
d974e256
JJ
8150 else if (do_wide)
8151 {
8152 print_vma (section->sh_addr, LONG_HEX);
8153
8154 if ((long) section->sh_offset == section->sh_offset)
8155 printf (" %6.6lx", (unsigned long) section->sh_offset);
8156 else
8157 {
8158 putchar (' ');
8159 print_vma (section->sh_offset, LONG_HEX);
8160 }
8161
8162 if ((unsigned long) section->sh_size == section->sh_size)
8163 printf (" %6.6lx", (unsigned long) section->sh_size);
8164 else
8165 {
8166 putchar (' ');
8167 print_vma (section->sh_size, LONG_HEX);
8168 }
8169
8170 if ((unsigned long) section->sh_entsize == section->sh_entsize)
8171 printf (" %2.2lx", (unsigned long) section->sh_entsize);
8172 else
8173 {
8174 putchar (' ');
8175 print_vma (section->sh_entsize, LONG_HEX);
8176 }
8177
5477e8a0
L
8178 if (do_section_details)
8179 fputs (" ", stdout);
8180 else
dda8d76d 8181 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
d974e256 8182
72de5009 8183 printf ("%2u %3u ", section->sh_link, section->sh_info);
d974e256
JJ
8184
8185 if ((unsigned long) section->sh_addralign == section->sh_addralign)
72de5009 8186 printf ("%2lu\n", (unsigned long) section->sh_addralign);
d974e256
JJ
8187 else
8188 {
8189 print_vma (section->sh_addralign, DEC);
8190 putchar ('\n');
8191 }
8192 }
5477e8a0 8193 else if (do_section_details)
595cf52e 8194 {
55cc53e9 8195 putchar (' ');
595cf52e
L
8196 print_vma (section->sh_addr, LONG_HEX);
8197 if ((long) section->sh_offset == section->sh_offset)
5477e8a0 8198 printf (" %16.16lx", (unsigned long) section->sh_offset);
595cf52e
L
8199 else
8200 {
8201 printf (" ");
8202 print_vma (section->sh_offset, LONG_HEX);
8203 }
72de5009 8204 printf (" %u\n ", section->sh_link);
595cf52e 8205 print_vma (section->sh_size, LONG_HEX);
5477e8a0 8206 putchar (' ');
595cf52e
L
8207 print_vma (section->sh_entsize, LONG_HEX);
8208
72de5009
AM
8209 printf (" %-16u %lu\n",
8210 section->sh_info,
595cf52e
L
8211 (unsigned long) section->sh_addralign);
8212 }
f7a99963
NC
8213 else
8214 {
8215 putchar (' ');
8216 print_vma (section->sh_addr, LONG_HEX);
53c7db4b
KH
8217 if ((long) section->sh_offset == section->sh_offset)
8218 printf (" %8.8lx", (unsigned long) section->sh_offset);
8219 else
8220 {
8221 printf (" ");
8222 print_vma (section->sh_offset, LONG_HEX);
8223 }
f7a99963
NC
8224 printf ("\n ");
8225 print_vma (section->sh_size, LONG_HEX);
8226 printf (" ");
8227 print_vma (section->sh_entsize, LONG_HEX);
76da6bbe 8228
dda8d76d 8229 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
76da6bbe 8230
72de5009
AM
8231 printf (" %2u %3u %lu\n",
8232 section->sh_link,
8233 section->sh_info,
f7a99963
NC
8234 (unsigned long) section->sh_addralign);
8235 }
5477e8a0
L
8236
8237 if (do_section_details)
77115a4a 8238 {
dda8d76d 8239 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
77115a4a
L
8240 if ((section->sh_flags & SHF_COMPRESSED) != 0)
8241 {
8242 /* Minimum section size is 12 bytes for 32-bit compression
8243 header + 12 bytes for compressed data header. */
8244 unsigned char buf[24];
d8024a91 8245
77115a4a 8246 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
dda8d76d 8247 if (get_data (&buf, filedata, section->sh_offset, 1,
77115a4a
L
8248 sizeof (buf), _("compression header")))
8249 {
8250 Elf_Internal_Chdr chdr;
d8024a91 8251
5844b465
NC
8252 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
8253 printf (_(" [<corrupt>]\n"));
77115a4a 8254 else
5844b465 8255 {
89dbeac7 8256 if (chdr.ch_type == ch_compress_zlib)
5844b465 8257 printf (" ZLIB, ");
89dbeac7 8258 else if (chdr.ch_type == ch_compress_zstd)
1369522f 8259 printf (" ZSTD, ");
5844b465
NC
8260 else
8261 printf (_(" [<unknown>: 0x%x], "),
8262 chdr.ch_type);
8263 print_vma (chdr.ch_size, LONG_HEX);
8264 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
8265 }
77115a4a
L
8266 }
8267 }
8268 }
252b5132
RH
8269 }
8270
5477e8a0 8271 if (!do_section_details)
3dbcc61d 8272 {
9fb71ee4
NC
8273 /* The ordering of the letters shown here matches the ordering of the
8274 corresponding SHF_xxx values, and hence the order in which these
8275 letters will be displayed to the user. */
8276 printf (_("Key to Flags:\n\
8277 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
8278 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
fd85a6a1 8279 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
5424d7ed
L
8280 switch (filedata->file_header.e_ident[EI_OSABI])
8281 {
8282 case ELFOSABI_GNU:
8283 case ELFOSABI_FREEBSD:
8284 printf (_("R (retain), "));
8285 /* Fall through */
8286 case ELFOSABI_NONE:
8287 printf (_("D (mbind), "));
8288 break;
8289 default:
8290 break;
8291 }
dda8d76d
NC
8292 if (filedata->file_header.e_machine == EM_X86_64
8293 || filedata->file_header.e_machine == EM_L1OM
8294 || filedata->file_header.e_machine == EM_K1OM)
9fb71ee4 8295 printf (_("l (large), "));
dda8d76d 8296 else if (filedata->file_header.e_machine == EM_ARM)
f0728ee3 8297 printf (_("y (purecode), "));
dda8d76d 8298 else if (filedata->file_header.e_machine == EM_PPC)
83eef883 8299 printf (_("v (VLE), "));
9fb71ee4 8300 printf ("p (processor specific)\n");
0b4362b0 8301 }
d1133906 8302
015dc7e1 8303 return true;
252b5132
RH
8304}
8305
015dc7e1 8306static bool
28d13567 8307get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
26c527e6
AM
8308 Elf_Internal_Sym **symtab, uint64_t *nsyms,
8309 char **strtab, uint64_t *strtablen)
28d13567
AM
8310{
8311 *strtab = NULL;
8312 *strtablen = 0;
4de91c10 8313 *symtab = get_elf_symbols (filedata, symsec, nsyms);
28d13567
AM
8314
8315 if (*symtab == NULL)
015dc7e1 8316 return false;
28d13567
AM
8317
8318 if (symsec->sh_link != 0)
8319 {
8320 Elf_Internal_Shdr *strsec;
8321
8322 if (symsec->sh_link >= filedata->file_header.e_shnum)
8323 {
8324 error (_("Bad sh_link in symbol table section\n"));
8325 free (*symtab);
8326 *symtab = NULL;
8327 *nsyms = 0;
015dc7e1 8328 return false;
28d13567
AM
8329 }
8330
8331 strsec = filedata->section_headers + symsec->sh_link;
8332
8333 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
8334 1, strsec->sh_size, _("string table"));
8335 if (*strtab == NULL)
8336 {
8337 free (*symtab);
8338 *symtab = NULL;
8339 *nsyms = 0;
015dc7e1 8340 return false;
28d13567
AM
8341 }
8342 *strtablen = strsec->sh_size;
8343 }
015dc7e1 8344 return true;
28d13567
AM
8345}
8346
f5842774
L
8347static const char *
8348get_group_flags (unsigned int flags)
8349{
1449284b 8350 static char buff[128];
220453ec 8351
6d913794
NC
8352 if (flags == 0)
8353 return "";
8354 else if (flags == GRP_COMDAT)
8355 return "COMDAT ";
f5842774 8356
89246a0e
AM
8357 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
8358 flags,
8359 flags & GRP_MASKOS ? _("<OS specific>") : "",
8360 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
8361 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
8362 ? _("<unknown>") : ""));
6d913794 8363
f5842774
L
8364 return buff;
8365}
8366
015dc7e1 8367static bool
dda8d76d 8368process_section_groups (Filedata * filedata)
f5842774 8369{
2cf0635d 8370 Elf_Internal_Shdr * section;
f5842774 8371 unsigned int i;
2cf0635d
NC
8372 struct group * group;
8373 Elf_Internal_Shdr * symtab_sec;
8374 Elf_Internal_Shdr * strtab_sec;
8375 Elf_Internal_Sym * symtab;
26c527e6 8376 uint64_t num_syms;
2cf0635d 8377 char * strtab;
c256ffe7 8378 size_t strtab_size;
d1f5c6e3
L
8379
8380 /* Don't process section groups unless needed. */
8381 if (!do_unwind && !do_section_groups)
015dc7e1 8382 return true;
f5842774 8383
dda8d76d 8384 if (filedata->file_header.e_shnum == 0)
f5842774
L
8385 {
8386 if (do_section_groups)
ca0e11aa
NC
8387 {
8388 if (filedata->is_separate)
8389 printf (_("\nThere are no sections group in linked file '%s'.\n"),
8390 filedata->file_name);
8391 else
8392 printf (_("\nThere are no section groups in this file.\n"));
8393 }
015dc7e1 8394 return true;
f5842774
L
8395 }
8396
dda8d76d 8397 if (filedata->section_headers == NULL)
f5842774
L
8398 {
8399 error (_("Section headers are not available!\n"));
fa1908fd 8400 /* PR 13622: This can happen with a corrupt ELF header. */
015dc7e1 8401 return false;
f5842774
L
8402 }
8403
978c4450
AM
8404 filedata->section_headers_groups
8405 = (struct group **) calloc (filedata->file_header.e_shnum,
8406 sizeof (struct group *));
e4b17d5c 8407
978c4450 8408 if (filedata->section_headers_groups == NULL)
e4b17d5c 8409 {
8b73c356 8410 error (_("Out of memory reading %u section group headers\n"),
dda8d76d 8411 filedata->file_header.e_shnum);
015dc7e1 8412 return false;
e4b17d5c
L
8413 }
8414
f5842774 8415 /* Scan the sections for the group section. */
978c4450 8416 filedata->group_count = 0;
dda8d76d
NC
8417 for (i = 0, section = filedata->section_headers;
8418 i < filedata->file_header.e_shnum;
f5842774 8419 i++, section++)
e4b17d5c 8420 if (section->sh_type == SHT_GROUP)
978c4450 8421 filedata->group_count++;
e4b17d5c 8422
978c4450 8423 if (filedata->group_count == 0)
d1f5c6e3
L
8424 {
8425 if (do_section_groups)
ca0e11aa
NC
8426 {
8427 if (filedata->is_separate)
8428 printf (_("\nThere are no section groups in linked file '%s'.\n"),
8429 filedata->file_name);
8430 else
8431 printf (_("\nThere are no section groups in this file.\n"));
8432 }
d1f5c6e3 8433
015dc7e1 8434 return true;
d1f5c6e3
L
8435 }
8436
978c4450
AM
8437 filedata->section_groups = (struct group *) calloc (filedata->group_count,
8438 sizeof (struct group));
e4b17d5c 8439
978c4450 8440 if (filedata->section_groups == NULL)
e4b17d5c 8441 {
26c527e6 8442 error (_("Out of memory reading %zu groups\n"), filedata->group_count);
015dc7e1 8443 return false;
e4b17d5c
L
8444 }
8445
d1f5c6e3
L
8446 symtab_sec = NULL;
8447 strtab_sec = NULL;
8448 symtab = NULL;
ba5cdace 8449 num_syms = 0;
d1f5c6e3 8450 strtab = NULL;
c256ffe7 8451 strtab_size = 0;
ca0e11aa
NC
8452
8453 if (filedata->is_separate)
8454 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
047c3dbf 8455
978c4450 8456 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
dda8d76d 8457 i < filedata->file_header.e_shnum;
e4b17d5c 8458 i++, section++)
f5842774
L
8459 {
8460 if (section->sh_type == SHT_GROUP)
8461 {
dda8d76d 8462 const char * name = printable_section_name (filedata, section);
74e1a04b 8463 const char * group_name;
2cf0635d
NC
8464 unsigned char * start;
8465 unsigned char * indices;
f5842774 8466 unsigned int entry, j, size;
2cf0635d
NC
8467 Elf_Internal_Shdr * sec;
8468 Elf_Internal_Sym * sym;
f5842774
L
8469
8470 /* Get the symbol table. */
dda8d76d
NC
8471 if (section->sh_link >= filedata->file_header.e_shnum
8472 || ((sec = filedata->section_headers + section->sh_link)->sh_type
c256ffe7 8473 != SHT_SYMTAB))
f5842774
L
8474 {
8475 error (_("Bad sh_link in group section `%s'\n"), name);
8476 continue;
8477 }
d1f5c6e3
L
8478
8479 if (symtab_sec != sec)
8480 {
8481 symtab_sec = sec;
9db70fc3 8482 free (symtab);
4de91c10 8483 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
d1f5c6e3 8484 }
f5842774 8485
dd24e3da
NC
8486 if (symtab == NULL)
8487 {
8488 error (_("Corrupt header in group section `%s'\n"), name);
8489 continue;
8490 }
8491
ba5cdace
NC
8492 if (section->sh_info >= num_syms)
8493 {
8494 error (_("Bad sh_info in group section `%s'\n"), name);
8495 continue;
8496 }
8497
f5842774
L
8498 sym = symtab + section->sh_info;
8499
8500 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8501 {
4fbb74a6 8502 if (sym->st_shndx == 0
dda8d76d 8503 || sym->st_shndx >= filedata->file_header.e_shnum)
f5842774
L
8504 {
8505 error (_("Bad sh_info in group section `%s'\n"), name);
8506 continue;
8507 }
ba2685cc 8508
b6ac461a
NC
8509 group_name = printable_section_name (filedata,
8510 filedata->section_headers
8511 + sym->st_shndx);
c256ffe7 8512 strtab_sec = NULL;
9db70fc3 8513 free (strtab);
f5842774 8514 strtab = NULL;
c256ffe7 8515 strtab_size = 0;
f5842774
L
8516 }
8517 else
8518 {
8519 /* Get the string table. */
dda8d76d 8520 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
c256ffe7
JJ
8521 {
8522 strtab_sec = NULL;
9db70fc3 8523 free (strtab);
c256ffe7
JJ
8524 strtab = NULL;
8525 strtab_size = 0;
8526 }
8527 else if (strtab_sec
dda8d76d 8528 != (sec = filedata->section_headers + symtab_sec->sh_link))
d1f5c6e3
L
8529 {
8530 strtab_sec = sec;
9db70fc3 8531 free (strtab);
071436c6 8532
dda8d76d 8533 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
071436c6
NC
8534 1, strtab_sec->sh_size,
8535 _("string table"));
c256ffe7 8536 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
d1f5c6e3 8537 }
c256ffe7 8538 group_name = sym->st_name < strtab_size
2b692964 8539 ? strtab + sym->st_name : _("<corrupt>");
f5842774
L
8540 }
8541
c9c1d674
EG
8542 /* PR 17531: file: loop. */
8543 if (section->sh_entsize > section->sh_size)
8544 {
26c527e6
AM
8545 error (_("Section %s has sh_entsize (%#" PRIx64 ")"
8546 " which is larger than its size (%#" PRIx64 ")\n"),
dda8d76d 8547 printable_section_name (filedata, section),
26c527e6
AM
8548 section->sh_entsize,
8549 section->sh_size);
61dd8e19 8550 continue;
c9c1d674
EG
8551 }
8552
dda8d76d 8553 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
3f5e193b
NC
8554 1, section->sh_size,
8555 _("section data"));
59245841
NC
8556 if (start == NULL)
8557 continue;
f5842774
L
8558
8559 indices = start;
8560 size = (section->sh_size / section->sh_entsize) - 1;
8561 entry = byte_get (indices, 4);
8562 indices += 4;
e4b17d5c
L
8563
8564 if (do_section_groups)
8565 {
2b692964 8566 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
391cb864 8567 get_group_flags (entry), i, name, group_name, size);
ba2685cc 8568
e4b17d5c
L
8569 printf (_(" [Index] Name\n"));
8570 }
8571
8572 group->group_index = i;
8573
f5842774
L
8574 for (j = 0; j < size; j++)
8575 {
2cf0635d 8576 struct group_list * g;
e4b17d5c 8577
f5842774
L
8578 entry = byte_get (indices, 4);
8579 indices += 4;
8580
dda8d76d 8581 if (entry >= filedata->file_header.e_shnum)
391cb864 8582 {
57028622
NC
8583 static unsigned num_group_errors = 0;
8584
8585 if (num_group_errors ++ < 10)
8586 {
8587 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
dda8d76d 8588 entry, i, filedata->file_header.e_shnum - 1);
57028622 8589 if (num_group_errors == 10)
67ce483b 8590 warn (_("Further error messages about overlarge group section indices suppressed\n"));
57028622 8591 }
391cb864
L
8592 continue;
8593 }
391cb864 8594
978c4450 8595 if (filedata->section_headers_groups [entry] != NULL)
e4b17d5c 8596 {
d1f5c6e3
L
8597 if (entry)
8598 {
57028622
NC
8599 static unsigned num_errs = 0;
8600
8601 if (num_errs ++ < 10)
8602 {
8603 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
8604 entry, i,
978c4450 8605 filedata->section_headers_groups [entry]->group_index);
57028622
NC
8606 if (num_errs == 10)
8607 warn (_("Further error messages about already contained group sections suppressed\n"));
8608 }
d1f5c6e3
L
8609 continue;
8610 }
8611 else
8612 {
8613 /* Intel C/C++ compiler may put section 0 in a
32ec8896 8614 section group. We just warn it the first time
d1f5c6e3 8615 and ignore it afterwards. */
015dc7e1 8616 static bool warned = false;
d1f5c6e3
L
8617 if (!warned)
8618 {
8619 error (_("section 0 in group section [%5u]\n"),
978c4450 8620 filedata->section_headers_groups [entry]->group_index);
015dc7e1 8621 warned = true;
d1f5c6e3
L
8622 }
8623 }
e4b17d5c
L
8624 }
8625
978c4450 8626 filedata->section_headers_groups [entry] = group;
e4b17d5c
L
8627
8628 if (do_section_groups)
8629 {
dda8d76d
NC
8630 sec = filedata->section_headers + entry;
8631 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
ba2685cc
AM
8632 }
8633
3f5e193b 8634 g = (struct group_list *) xmalloc (sizeof (struct group_list));
e4b17d5c
L
8635 g->section_index = entry;
8636 g->next = group->root;
8637 group->root = g;
f5842774
L
8638 }
8639
9db70fc3 8640 free (start);
e4b17d5c
L
8641
8642 group++;
f5842774
L
8643 }
8644 }
8645
9db70fc3
AM
8646 free (symtab);
8647 free (strtab);
015dc7e1 8648 return true;
f5842774
L
8649}
8650
28f997cf
TG
8651/* Data used to display dynamic fixups. */
8652
8653struct ia64_vms_dynfixup
8654{
625d49fc
AM
8655 uint64_t needed_ident; /* Library ident number. */
8656 uint64_t needed; /* Index in the dstrtab of the library name. */
8657 uint64_t fixup_needed; /* Index of the library. */
8658 uint64_t fixup_rela_cnt; /* Number of fixups. */
8659 uint64_t fixup_rela_off; /* Fixups offset in the dynamic segment. */
28f997cf
TG
8660};
8661
8662/* Data used to display dynamic relocations. */
8663
8664struct ia64_vms_dynimgrela
8665{
625d49fc
AM
8666 uint64_t img_rela_cnt; /* Number of relocations. */
8667 uint64_t img_rela_off; /* Reloc offset in the dynamic segment. */
28f997cf
TG
8668};
8669
8670/* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
8671 library). */
8672
015dc7e1 8673static bool
dda8d76d
NC
8674dump_ia64_vms_dynamic_fixups (Filedata * filedata,
8675 struct ia64_vms_dynfixup * fixup,
8676 const char * strtab,
8677 unsigned int strtab_sz)
28f997cf 8678{
32ec8896 8679 Elf64_External_VMS_IMAGE_FIXUP * imfs;
26c527e6 8680 size_t i;
32ec8896 8681 const char * lib_name;
28f997cf 8682
978c4450
AM
8683 imfs = get_data (NULL, filedata,
8684 filedata->dynamic_addr + fixup->fixup_rela_off,
95099889 8685 sizeof (*imfs), fixup->fixup_rela_cnt,
28f997cf
TG
8686 _("dynamic section image fixups"));
8687 if (!imfs)
015dc7e1 8688 return false;
28f997cf
TG
8689
8690 if (fixup->needed < strtab_sz)
8691 lib_name = strtab + fixup->needed;
8692 else
8693 {
26c527e6
AM
8694 warn (_("corrupt library name index of %#" PRIx64
8695 " found in dynamic entry"), fixup->needed);
28f997cf
TG
8696 lib_name = "???";
8697 }
736990c4 8698
26c527e6
AM
8699 printf (_("\nImage fixups for needed library #%" PRId64
8700 ": %s - ident: %" PRIx64 "\n"),
8701 fixup->fixup_needed, lib_name, fixup->needed_ident);
28f997cf
TG
8702 printf
8703 (_("Seg Offset Type SymVec DataType\n"));
8704
26c527e6 8705 for (i = 0; i < (size_t) fixup->fixup_rela_cnt; i++)
28f997cf
TG
8706 {
8707 unsigned int type;
8708 const char *rtype;
8709
8710 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
625d49fc 8711 printf ("%016" PRIx64 " ", BYTE_GET (imfs [i].fixup_offset));
28f997cf
TG
8712 type = BYTE_GET (imfs [i].type);
8713 rtype = elf_ia64_reloc_type (type);
8714 if (rtype == NULL)
f493c217 8715 printf ("0x%08x ", type);
28f997cf 8716 else
f493c217 8717 printf ("%-32s ", rtype);
28f997cf
TG
8718 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
8719 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
8720 }
8721
8722 free (imfs);
015dc7e1 8723 return true;
28f997cf
TG
8724}
8725
8726/* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
8727
015dc7e1 8728static bool
dda8d76d 8729dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
28f997cf
TG
8730{
8731 Elf64_External_VMS_IMAGE_RELA *imrs;
26c527e6 8732 size_t i;
28f997cf 8733
978c4450
AM
8734 imrs = get_data (NULL, filedata,
8735 filedata->dynamic_addr + imgrela->img_rela_off,
95099889 8736 sizeof (*imrs), imgrela->img_rela_cnt,
9cf03b7e 8737 _("dynamic section image relocations"));
28f997cf 8738 if (!imrs)
015dc7e1 8739 return false;
28f997cf
TG
8740
8741 printf (_("\nImage relocs\n"));
8742 printf
8743 (_("Seg Offset Type Addend Seg Sym Off\n"));
8744
26c527e6 8745 for (i = 0; i < (size_t) imgrela->img_rela_cnt; i++)
28f997cf
TG
8746 {
8747 unsigned int type;
8748 const char *rtype;
8749
8750 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
625d49fc 8751 printf ("%08" PRIx64 " ", BYTE_GET (imrs [i].rela_offset));
28f997cf
TG
8752 type = BYTE_GET (imrs [i].type);
8753 rtype = elf_ia64_reloc_type (type);
8754 if (rtype == NULL)
8755 printf ("0x%08x ", type);
8756 else
8757 printf ("%-31s ", rtype);
8758 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
8759 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
625d49fc 8760 printf ("%08" PRIx64 "\n", BYTE_GET (imrs [i].sym_offset));
28f997cf
TG
8761 }
8762
8763 free (imrs);
015dc7e1 8764 return true;
28f997cf
TG
8765}
8766
8767/* Display IA-64 OpenVMS dynamic relocations and fixups. */
8768
015dc7e1 8769static bool
dda8d76d 8770process_ia64_vms_dynamic_relocs (Filedata * filedata)
28f997cf
TG
8771{
8772 struct ia64_vms_dynfixup fixup;
8773 struct ia64_vms_dynimgrela imgrela;
8774 Elf_Internal_Dyn *entry;
625d49fc
AM
8775 uint64_t strtab_off = 0;
8776 uint64_t strtab_sz = 0;
28f997cf 8777 char *strtab = NULL;
015dc7e1 8778 bool res = true;
28f997cf
TG
8779
8780 memset (&fixup, 0, sizeof (fixup));
8781 memset (&imgrela, 0, sizeof (imgrela));
8782
8783 /* Note: the order of the entries is specified by the OpenVMS specs. */
978c4450
AM
8784 for (entry = filedata->dynamic_section;
8785 entry < filedata->dynamic_section + filedata->dynamic_nent;
28f997cf
TG
8786 entry++)
8787 {
8788 switch (entry->d_tag)
8789 {
8790 case DT_IA_64_VMS_STRTAB_OFFSET:
8791 strtab_off = entry->d_un.d_val;
8792 break;
8793 case DT_STRSZ:
8794 strtab_sz = entry->d_un.d_val;
8795 if (strtab == NULL)
978c4450
AM
8796 strtab = get_data (NULL, filedata,
8797 filedata->dynamic_addr + strtab_off,
28f997cf 8798 1, strtab_sz, _("dynamic string section"));
736990c4
NC
8799 if (strtab == NULL)
8800 strtab_sz = 0;
28f997cf
TG
8801 break;
8802
8803 case DT_IA_64_VMS_NEEDED_IDENT:
8804 fixup.needed_ident = entry->d_un.d_val;
8805 break;
8806 case DT_NEEDED:
8807 fixup.needed = entry->d_un.d_val;
8808 break;
8809 case DT_IA_64_VMS_FIXUP_NEEDED:
8810 fixup.fixup_needed = entry->d_un.d_val;
8811 break;
8812 case DT_IA_64_VMS_FIXUP_RELA_CNT:
8813 fixup.fixup_rela_cnt = entry->d_un.d_val;
8814 break;
8815 case DT_IA_64_VMS_FIXUP_RELA_OFF:
8816 fixup.fixup_rela_off = entry->d_un.d_val;
dda8d76d 8817 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
015dc7e1 8818 res = false;
28f997cf 8819 break;
28f997cf
TG
8820 case DT_IA_64_VMS_IMG_RELA_CNT:
8821 imgrela.img_rela_cnt = entry->d_un.d_val;
8822 break;
8823 case DT_IA_64_VMS_IMG_RELA_OFF:
8824 imgrela.img_rela_off = entry->d_un.d_val;
dda8d76d 8825 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
015dc7e1 8826 res = false;
28f997cf
TG
8827 break;
8828
8829 default:
8830 break;
8831 }
8832 }
8833
9db70fc3 8834 free (strtab);
28f997cf
TG
8835
8836 return res;
8837}
8838
85b1c36d 8839static struct
566b0d53 8840{
2cf0635d 8841 const char * name;
566b0d53
L
8842 int reloc;
8843 int size;
a7fd1186 8844 relocation_type rel_type;
32ec8896
NC
8845}
8846 dynamic_relocations [] =
566b0d53 8847{
a7fd1186
FS
8848 { "REL", DT_REL, DT_RELSZ, reltype_rel },
8849 { "RELA", DT_RELA, DT_RELASZ, reltype_rela },
8850 { "RELR", DT_RELR, DT_RELRSZ, reltype_relr },
8851 { "PLT", DT_JMPREL, DT_PLTRELSZ, reltype_unknown }
566b0d53
L
8852};
8853
252b5132 8854/* Process the reloc section. */
18bd398b 8855
015dc7e1 8856static bool
dda8d76d 8857process_relocs (Filedata * filedata)
252b5132 8858{
26c527e6
AM
8859 uint64_t rel_size;
8860 uint64_t rel_offset;
252b5132 8861
252b5132 8862 if (!do_reloc)
015dc7e1 8863 return true;
252b5132
RH
8864
8865 if (do_using_dynamic)
8866 {
a7fd1186 8867 relocation_type rel_type;
2cf0635d 8868 const char * name;
015dc7e1 8869 bool has_dynamic_reloc;
566b0d53 8870 unsigned int i;
0de14b54 8871
015dc7e1 8872 has_dynamic_reloc = false;
252b5132 8873
566b0d53 8874 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
252b5132 8875 {
a7fd1186 8876 rel_type = dynamic_relocations [i].rel_type;
566b0d53 8877 name = dynamic_relocations [i].name;
978c4450
AM
8878 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
8879 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
103f02d3 8880
32ec8896 8881 if (rel_size)
015dc7e1 8882 has_dynamic_reloc = true;
566b0d53 8883
a7fd1186 8884 if (rel_type == reltype_unknown)
aa903cfb 8885 {
566b0d53 8886 if (dynamic_relocations [i].reloc == DT_JMPREL)
978c4450 8887 switch (filedata->dynamic_info[DT_PLTREL])
566b0d53
L
8888 {
8889 case DT_REL:
a7fd1186 8890 rel_type = reltype_rel;
566b0d53
L
8891 break;
8892 case DT_RELA:
a7fd1186 8893 rel_type = reltype_rela;
566b0d53
L
8894 break;
8895 }
aa903cfb 8896 }
252b5132 8897
566b0d53
L
8898 if (rel_size)
8899 {
ca0e11aa
NC
8900 if (filedata->is_separate)
8901 printf
26c527e6
AM
8902 (_("\nIn linked file '%s' section '%s' at offset %#" PRIx64
8903 " contains %" PRId64 " bytes:\n"),
ca0e11aa
NC
8904 filedata->file_name, name, rel_offset, rel_size);
8905 else
8906 printf
26c527e6
AM
8907 (_("\n'%s' relocation section at offset %#" PRIx64
8908 " contains %" PRId64 " bytes:\n"),
ca0e11aa 8909 name, rel_offset, rel_size);
252b5132 8910
dda8d76d
NC
8911 dump_relocations (filedata,
8912 offset_from_vma (filedata, rel_offset, rel_size),
d93f0186 8913 rel_size,
978c4450
AM
8914 filedata->dynamic_symbols,
8915 filedata->num_dynamic_syms,
8916 filedata->dynamic_strings,
8917 filedata->dynamic_strings_length,
a7fd1186 8918 rel_type, true /* is_dynamic */);
566b0d53 8919 }
252b5132 8920 }
566b0d53 8921
dda8d76d
NC
8922 if (is_ia64_vms (filedata))
8923 if (process_ia64_vms_dynamic_relocs (filedata))
015dc7e1 8924 has_dynamic_reloc = true;
28f997cf 8925
566b0d53 8926 if (! has_dynamic_reloc)
ca0e11aa
NC
8927 {
8928 if (filedata->is_separate)
8929 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
8930 filedata->file_name);
8931 else
8932 printf (_("\nThere are no dynamic relocations in this file.\n"));
8933 }
252b5132
RH
8934 }
8935 else
8936 {
2cf0635d 8937 Elf_Internal_Shdr * section;
26c527e6 8938 size_t i;
015dc7e1 8939 bool found = false;
252b5132 8940
dda8d76d
NC
8941 for (i = 0, section = filedata->section_headers;
8942 i < filedata->file_header.e_shnum;
b34976b6 8943 i++, section++)
252b5132
RH
8944 {
8945 if ( section->sh_type != SHT_RELA
a7fd1186
FS
8946 && section->sh_type != SHT_REL
8947 && section->sh_type != SHT_RELR)
252b5132
RH
8948 continue;
8949
8950 rel_offset = section->sh_offset;
8951 rel_size = section->sh_size;
8952
8953 if (rel_size)
8954 {
a7fd1186 8955 relocation_type rel_type;
26c527e6 8956 uint64_t num_rela;
103f02d3 8957
ca0e11aa
NC
8958 if (filedata->is_separate)
8959 printf (_("\nIn linked file '%s' relocation section "),
8960 filedata->file_name);
8961 else
8962 printf (_("\nRelocation section "));
252b5132 8963
dda8d76d 8964 if (filedata->string_table == NULL)
19936277 8965 printf ("%d", section->sh_name);
252b5132 8966 else
dda8d76d 8967 printf ("'%s'", printable_section_name (filedata, section));
252b5132 8968
d3a49aa8 8969 num_rela = rel_size / section->sh_entsize;
26c527e6
AM
8970 printf (ngettext (" at offset %#" PRIx64
8971 " contains %" PRIu64 " entry:\n",
8972 " at offset %#" PRIx64
8973 " contains %" PRId64 " entries:\n",
d3a49aa8
AM
8974 num_rela),
8975 rel_offset, num_rela);
252b5132 8976
a7fd1186
FS
8977 rel_type = section->sh_type == SHT_RELA ? reltype_rela :
8978 section->sh_type == SHT_REL ? reltype_rel : reltype_relr;
d79b3d50 8979
4fbb74a6 8980 if (section->sh_link != 0
dda8d76d 8981 && section->sh_link < filedata->file_header.e_shnum)
af3fc3bc 8982 {
26c527e6
AM
8983 Elf_Internal_Shdr *symsec;
8984 Elf_Internal_Sym *symtab;
8985 uint64_t nsyms;
8986 uint64_t strtablen = 0;
8987 char *strtab = NULL;
57346661 8988
dda8d76d 8989 symsec = filedata->section_headers + section->sh_link;
08d8fa11
JJ
8990 if (symsec->sh_type != SHT_SYMTAB
8991 && symsec->sh_type != SHT_DYNSYM)
8992 continue;
8993
28d13567
AM
8994 if (!get_symtab (filedata, symsec,
8995 &symtab, &nsyms, &strtab, &strtablen))
af3fc3bc 8996 continue;
252b5132 8997
dda8d76d 8998 dump_relocations (filedata, rel_offset, rel_size,
bb4d2ac2 8999 symtab, nsyms, strtab, strtablen,
a7fd1186 9000 rel_type,
bb4d2ac2 9001 symsec->sh_type == SHT_DYNSYM);
9db70fc3 9002 free (strtab);
d79b3d50
NC
9003 free (symtab);
9004 }
9005 else
dda8d76d 9006 dump_relocations (filedata, rel_offset, rel_size,
a7fd1186 9007 NULL, 0, NULL, 0, rel_type, false /* is_dynamic */);
252b5132 9008
015dc7e1 9009 found = true;
252b5132
RH
9010 }
9011 }
9012
9013 if (! found)
45ac8f4f
NC
9014 {
9015 /* Users sometimes forget the -D option, so try to be helpful. */
9016 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
9017 {
978c4450 9018 if (filedata->dynamic_info[dynamic_relocations [i].size])
45ac8f4f 9019 {
ca0e11aa
NC
9020 if (filedata->is_separate)
9021 printf (_("\nThere are no static relocations in linked file '%s'."),
9022 filedata->file_name);
9023 else
9024 printf (_("\nThere are no static relocations in this file."));
45ac8f4f
NC
9025 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
9026
9027 break;
9028 }
9029 }
9030 if (i == ARRAY_SIZE (dynamic_relocations))
ca0e11aa
NC
9031 {
9032 if (filedata->is_separate)
9033 printf (_("\nThere are no relocations in linked file '%s'.\n"),
9034 filedata->file_name);
9035 else
9036 printf (_("\nThere are no relocations in this file.\n"));
9037 }
45ac8f4f 9038 }
252b5132
RH
9039 }
9040
015dc7e1 9041 return true;
252b5132
RH
9042}
9043
4d6ed7c8
NC
9044/* An absolute address consists of a section and an offset. If the
9045 section is NULL, the offset itself is the address, otherwise, the
9046 address equals to LOAD_ADDRESS(section) + offset. */
9047
9048struct absaddr
948f632f
DA
9049{
9050 unsigned short section;
625d49fc 9051 uint64_t offset;
948f632f 9052};
4d6ed7c8 9053
948f632f
DA
9054/* Find the nearest symbol at or below ADDR. Returns the symbol
9055 name, if found, and the offset from the symbol to ADDR. */
4d6ed7c8 9056
4d6ed7c8 9057static void
26c527e6
AM
9058find_symbol_for_address (Filedata *filedata,
9059 Elf_Internal_Sym *symtab,
9060 uint64_t nsyms,
9061 const char *strtab,
9062 uint64_t strtab_size,
9063 struct absaddr addr,
9064 const char **symname,
9065 uint64_t *offset)
4d6ed7c8 9066{
625d49fc 9067 uint64_t dist = 0x100000;
2cf0635d 9068 Elf_Internal_Sym * sym;
948f632f
DA
9069 Elf_Internal_Sym * beg;
9070 Elf_Internal_Sym * end;
2cf0635d 9071 Elf_Internal_Sym * best = NULL;
4d6ed7c8 9072
0b6ae522 9073 REMOVE_ARCH_BITS (addr.offset);
948f632f
DA
9074 beg = symtab;
9075 end = symtab + nsyms;
0b6ae522 9076
948f632f 9077 while (beg < end)
4d6ed7c8 9078 {
625d49fc 9079 uint64_t value;
948f632f
DA
9080
9081 sym = beg + (end - beg) / 2;
0b6ae522 9082
948f632f 9083 value = sym->st_value;
0b6ae522
DJ
9084 REMOVE_ARCH_BITS (value);
9085
948f632f 9086 if (sym->st_name != 0
4d6ed7c8 9087 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
0b6ae522
DJ
9088 && addr.offset >= value
9089 && addr.offset - value < dist)
4d6ed7c8
NC
9090 {
9091 best = sym;
0b6ae522 9092 dist = addr.offset - value;
4d6ed7c8
NC
9093 if (!dist)
9094 break;
9095 }
948f632f
DA
9096
9097 if (addr.offset < value)
9098 end = sym;
9099 else
9100 beg = sym + 1;
4d6ed7c8 9101 }
1b31d05e 9102
4d6ed7c8
NC
9103 if (best)
9104 {
57346661 9105 *symname = (best->st_name >= strtab_size
2b692964 9106 ? _("<corrupt>") : strtab + best->st_name);
4d6ed7c8
NC
9107 *offset = dist;
9108 return;
9109 }
1b31d05e 9110
4d6ed7c8
NC
9111 *symname = NULL;
9112 *offset = addr.offset;
9113}
9114
32ec8896 9115static /* signed */ int
948f632f
DA
9116symcmp (const void *p, const void *q)
9117{
9118 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
9119 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
9120
9121 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
9122}
9123
9124/* Process the unwind section. */
9125
9126#include "unwind-ia64.h"
9127
9128struct ia64_unw_table_entry
9129{
9130 struct absaddr start;
9131 struct absaddr end;
9132 struct absaddr info;
9133};
9134
9135struct ia64_unw_aux_info
9136{
32ec8896 9137 struct ia64_unw_table_entry * table; /* Unwind table. */
26c527e6 9138 uint64_t table_len; /* Length of unwind table. */
32ec8896 9139 unsigned char * info; /* Unwind info. */
26c527e6 9140 uint64_t info_size; /* Size of unwind info. */
625d49fc
AM
9141 uint64_t info_addr; /* Starting address of unwind info. */
9142 uint64_t seg_base; /* Starting address of segment. */
32ec8896 9143 Elf_Internal_Sym * symtab; /* The symbol table. */
26c527e6 9144 uint64_t nsyms; /* Number of symbols. */
32ec8896 9145 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
26c527e6 9146 uint64_t nfuns; /* Number of entries in funtab. */
32ec8896 9147 char * strtab; /* The string table. */
26c527e6 9148 uint64_t strtab_size; /* Size of string table. */
948f632f
DA
9149};
9150
015dc7e1 9151static bool
dda8d76d 9152dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
4d6ed7c8 9153{
2cf0635d 9154 struct ia64_unw_table_entry * tp;
26c527e6 9155 size_t j, nfuns;
4d6ed7c8 9156 int in_body;
015dc7e1 9157 bool res = true;
7036c0e1 9158
948f632f
DA
9159 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9160 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9161 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9162 aux->funtab[nfuns++] = aux->symtab[j];
9163 aux->nfuns = nfuns;
9164 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9165
4d6ed7c8
NC
9166 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
9167 {
625d49fc
AM
9168 uint64_t stamp;
9169 uint64_t offset;
2cf0635d
NC
9170 const unsigned char * dp;
9171 const unsigned char * head;
53774b7e 9172 const unsigned char * end;
2cf0635d 9173 const char * procname;
4d6ed7c8 9174
dda8d76d 9175 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
57346661 9176 aux->strtab_size, tp->start, &procname, &offset);
4d6ed7c8
NC
9177
9178 fputs ("\n<", stdout);
9179
9180 if (procname)
9181 {
9182 fputs (procname, stdout);
9183
9184 if (offset)
26c527e6 9185 printf ("+%" PRIx64, offset);
4d6ed7c8
NC
9186 }
9187
9188 fputs (">: [", stdout);
9189 print_vma (tp->start.offset, PREFIX_HEX);
9190 fputc ('-', stdout);
9191 print_vma (tp->end.offset, PREFIX_HEX);
26c527e6
AM
9192 printf ("], info at +0x%" PRIx64 "\n",
9193 tp->info.offset - aux->seg_base);
4d6ed7c8 9194
53774b7e
NC
9195 /* PR 17531: file: 86232b32. */
9196 if (aux->info == NULL)
9197 continue;
9198
97c0a079
AM
9199 offset = tp->info.offset;
9200 if (tp->info.section)
9201 {
9202 if (tp->info.section >= filedata->file_header.e_shnum)
9203 {
26c527e6
AM
9204 warn (_("Invalid section %u in table entry %td\n"),
9205 tp->info.section, tp - aux->table);
015dc7e1 9206 res = false;
97c0a079
AM
9207 continue;
9208 }
9209 offset += filedata->section_headers[tp->info.section].sh_addr;
9210 }
9211 offset -= aux->info_addr;
53774b7e 9212 /* PR 17531: file: 0997b4d1. */
90679903
AM
9213 if (offset >= aux->info_size
9214 || aux->info_size - offset < 8)
53774b7e 9215 {
26c527e6
AM
9216 warn (_("Invalid offset %" PRIx64 " in table entry %td\n"),
9217 tp->info.offset, tp - aux->table);
015dc7e1 9218 res = false;
53774b7e
NC
9219 continue;
9220 }
9221
97c0a079 9222 head = aux->info + offset;
a4a00738 9223 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
4d6ed7c8 9224
86f55779 9225 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
4d6ed7c8
NC
9226 (unsigned) UNW_VER (stamp),
9227 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
9228 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
9229 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
89fac5e3 9230 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
4d6ed7c8
NC
9231
9232 if (UNW_VER (stamp) != 1)
9233 {
2b692964 9234 printf (_("\tUnknown version.\n"));
4d6ed7c8
NC
9235 continue;
9236 }
9237
9238 in_body = 0;
53774b7e
NC
9239 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
9240 /* PR 17531: file: 16ceda89. */
9241 if (end > aux->info + aux->info_size)
9242 end = aux->info + aux->info_size;
9243 for (dp = head + 8; dp < end;)
b4477bc8 9244 dp = unw_decode (dp, in_body, & in_body, end);
4d6ed7c8 9245 }
948f632f
DA
9246
9247 free (aux->funtab);
32ec8896
NC
9248
9249 return res;
4d6ed7c8
NC
9250}
9251
015dc7e1 9252static bool
dda8d76d
NC
9253slurp_ia64_unwind_table (Filedata * filedata,
9254 struct ia64_unw_aux_info * aux,
9255 Elf_Internal_Shdr * sec)
4d6ed7c8 9256{
26c527e6 9257 uint64_t size, nrelas, i;
2cf0635d
NC
9258 Elf_Internal_Phdr * seg;
9259 struct ia64_unw_table_entry * tep;
9260 Elf_Internal_Shdr * relsec;
9261 Elf_Internal_Rela * rela;
9262 Elf_Internal_Rela * rp;
9263 unsigned char * table;
9264 unsigned char * tp;
9265 Elf_Internal_Sym * sym;
9266 const char * relname;
4d6ed7c8 9267
53774b7e
NC
9268 aux->table_len = 0;
9269
4d6ed7c8
NC
9270 /* First, find the starting address of the segment that includes
9271 this section: */
9272
dda8d76d 9273 if (filedata->file_header.e_phnum)
4d6ed7c8 9274 {
dda8d76d 9275 if (! get_program_headers (filedata))
015dc7e1 9276 return false;
4d6ed7c8 9277
dda8d76d
NC
9278 for (seg = filedata->program_headers;
9279 seg < filedata->program_headers + filedata->file_header.e_phnum;
d93f0186 9280 ++seg)
4d6ed7c8
NC
9281 {
9282 if (seg->p_type != PT_LOAD)
9283 continue;
9284
9285 if (sec->sh_addr >= seg->p_vaddr
9286 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
9287 {
9288 aux->seg_base = seg->p_vaddr;
9289 break;
9290 }
9291 }
4d6ed7c8
NC
9292 }
9293
9294 /* Second, build the unwind table from the contents of the unwind section: */
9295 size = sec->sh_size;
dda8d76d 9296 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
3f5e193b 9297 _("unwind table"));
a6e9f9df 9298 if (!table)
015dc7e1 9299 return false;
4d6ed7c8 9300
53774b7e 9301 aux->table_len = size / (3 * eh_addr_size);
3f5e193b 9302 aux->table = (struct ia64_unw_table_entry *)
53774b7e 9303 xcmalloc (aux->table_len, sizeof (aux->table[0]));
89fac5e3 9304 tep = aux->table;
53774b7e
NC
9305
9306 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
4d6ed7c8
NC
9307 {
9308 tep->start.section = SHN_UNDEF;
9309 tep->end.section = SHN_UNDEF;
9310 tep->info.section = SHN_UNDEF;
c6a0c689
AM
9311 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
9312 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
9313 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
4d6ed7c8
NC
9314 tep->start.offset += aux->seg_base;
9315 tep->end.offset += aux->seg_base;
9316 tep->info.offset += aux->seg_base;
9317 }
9318 free (table);
9319
41e92641 9320 /* Third, apply any relocations to the unwind table: */
dda8d76d
NC
9321 for (relsec = filedata->section_headers;
9322 relsec < filedata->section_headers + filedata->file_header.e_shnum;
4d6ed7c8
NC
9323 ++relsec)
9324 {
9325 if (relsec->sh_type != SHT_RELA
dda8d76d
NC
9326 || relsec->sh_info >= filedata->file_header.e_shnum
9327 || filedata->section_headers + relsec->sh_info != sec)
4d6ed7c8
NC
9328 continue;
9329
dda8d76d 9330 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
4d6ed7c8 9331 & rela, & nrelas))
53774b7e
NC
9332 {
9333 free (aux->table);
9334 aux->table = NULL;
9335 aux->table_len = 0;
015dc7e1 9336 return false;
53774b7e 9337 }
4d6ed7c8
NC
9338
9339 for (rp = rela; rp < rela + nrelas; ++rp)
9340 {
4770fb94 9341 unsigned int sym_ndx;
726bd37d
AM
9342 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
9343 relname = elf_ia64_reloc_type (r_type);
4d6ed7c8 9344
82b1b41b
NC
9345 /* PR 17531: file: 9fa67536. */
9346 if (relname == NULL)
9347 {
726bd37d 9348 warn (_("Skipping unknown relocation type: %u\n"), r_type);
82b1b41b
NC
9349 continue;
9350 }
948f632f 9351
24d127aa 9352 if (! startswith (relname, "R_IA64_SEGREL"))
4d6ed7c8 9353 {
82b1b41b 9354 warn (_("Skipping unexpected relocation type: %s\n"), relname);
4d6ed7c8
NC
9355 continue;
9356 }
9357
89fac5e3 9358 i = rp->r_offset / (3 * eh_addr_size);
4d6ed7c8 9359
53774b7e
NC
9360 /* PR 17531: file: 5bc8d9bf. */
9361 if (i >= aux->table_len)
9362 {
26c527e6
AM
9363 warn (_("Skipping reloc with overlarge offset: %#" PRIx64 "\n"),
9364 i);
53774b7e
NC
9365 continue;
9366 }
9367
4770fb94
AM
9368 sym_ndx = get_reloc_symindex (rp->r_info);
9369 if (sym_ndx >= aux->nsyms)
9370 {
9371 warn (_("Skipping reloc with invalid symbol index: %u\n"),
9372 sym_ndx);
9373 continue;
9374 }
9375 sym = aux->symtab + sym_ndx;
9376
53774b7e 9377 switch (rp->r_offset / eh_addr_size % 3)
4d6ed7c8
NC
9378 {
9379 case 0:
9380 aux->table[i].start.section = sym->st_shndx;
e466bc6e 9381 aux->table[i].start.offset = rp->r_addend + sym->st_value;
4d6ed7c8
NC
9382 break;
9383 case 1:
9384 aux->table[i].end.section = sym->st_shndx;
e466bc6e 9385 aux->table[i].end.offset = rp->r_addend + sym->st_value;
4d6ed7c8
NC
9386 break;
9387 case 2:
9388 aux->table[i].info.section = sym->st_shndx;
e466bc6e 9389 aux->table[i].info.offset = rp->r_addend + sym->st_value;
4d6ed7c8
NC
9390 break;
9391 default:
9392 break;
9393 }
9394 }
9395
9396 free (rela);
9397 }
9398
015dc7e1 9399 return true;
4d6ed7c8
NC
9400}
9401
015dc7e1 9402static bool
dda8d76d 9403ia64_process_unwind (Filedata * filedata)
4d6ed7c8 9404{
2cf0635d
NC
9405 Elf_Internal_Shdr * sec;
9406 Elf_Internal_Shdr * unwsec = NULL;
26c527e6 9407 uint64_t i, unwcount = 0, unwstart = 0;
57346661 9408 struct ia64_unw_aux_info aux;
015dc7e1 9409 bool res = true;
f1467e33 9410
4d6ed7c8
NC
9411 memset (& aux, 0, sizeof (aux));
9412
dda8d76d 9413 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
4d6ed7c8 9414 {
28d13567 9415 if (sec->sh_type == SHT_SYMTAB)
4d6ed7c8 9416 {
28d13567 9417 if (aux.symtab)
4082ef84 9418 {
28d13567
AM
9419 error (_("Multiple symbol tables encountered\n"));
9420 free (aux.symtab);
9421 aux.symtab = NULL;
4082ef84 9422 free (aux.strtab);
28d13567 9423 aux.strtab = NULL;
4082ef84 9424 }
28d13567
AM
9425 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9426 &aux.strtab, &aux.strtab_size))
015dc7e1 9427 return false;
4d6ed7c8
NC
9428 }
9429 else if (sec->sh_type == SHT_IA_64_UNWIND)
579f31ac
JJ
9430 unwcount++;
9431 }
9432
9433 if (!unwcount)
9434 printf (_("\nThere are no unwind sections in this file.\n"));
9435
9436 while (unwcount-- > 0)
9437 {
84714f86 9438 const char *suffix;
579f31ac
JJ
9439 size_t len, len2;
9440
dda8d76d
NC
9441 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
9442 i < filedata->file_header.e_shnum; ++i, ++sec)
579f31ac
JJ
9443 if (sec->sh_type == SHT_IA_64_UNWIND)
9444 {
9445 unwsec = sec;
9446 break;
9447 }
4082ef84
NC
9448 /* We have already counted the number of SHT_IA64_UNWIND
9449 sections so the loop above should never fail. */
9450 assert (unwsec != NULL);
579f31ac
JJ
9451
9452 unwstart = i + 1;
9453 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
9454
e4b17d5c
L
9455 if ((unwsec->sh_flags & SHF_GROUP) != 0)
9456 {
9457 /* We need to find which section group it is in. */
4082ef84 9458 struct group_list * g;
e4b17d5c 9459
978c4450
AM
9460 if (filedata->section_headers_groups == NULL
9461 || filedata->section_headers_groups[i] == NULL)
dda8d76d 9462 i = filedata->file_header.e_shnum;
4082ef84 9463 else
e4b17d5c 9464 {
978c4450 9465 g = filedata->section_headers_groups[i]->root;
18bd398b 9466
4082ef84
NC
9467 for (; g != NULL; g = g->next)
9468 {
dda8d76d 9469 sec = filedata->section_headers + g->section_index;
e4b17d5c 9470
84714f86
AM
9471 if (section_name_valid (filedata, sec)
9472 && streq (section_name (filedata, sec),
9473 ELF_STRING_ia64_unwind_info))
4082ef84
NC
9474 break;
9475 }
9476
9477 if (g == NULL)
dda8d76d 9478 i = filedata->file_header.e_shnum;
4082ef84 9479 }
e4b17d5c 9480 }
84714f86
AM
9481 else if (section_name_valid (filedata, unwsec)
9482 && startswith (section_name (filedata, unwsec),
e9b095a5 9483 ELF_STRING_ia64_unwind_once))
579f31ac 9484 {
18bd398b 9485 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
579f31ac 9486 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
84714f86 9487 suffix = section_name (filedata, unwsec) + len;
b9e920ec
AM
9488 for (i = 0, sec = filedata->section_headers;
9489 i < filedata->file_header.e_shnum;
579f31ac 9490 ++i, ++sec)
84714f86
AM
9491 if (section_name_valid (filedata, sec)
9492 && startswith (section_name (filedata, sec),
e9b095a5 9493 ELF_STRING_ia64_unwind_info_once)
84714f86 9494 && streq (section_name (filedata, sec) + len2, suffix))
579f31ac
JJ
9495 break;
9496 }
9497 else
9498 {
9499 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
18bd398b 9500 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
579f31ac
JJ
9501 len = sizeof (ELF_STRING_ia64_unwind) - 1;
9502 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
9503 suffix = "";
84714f86
AM
9504 if (section_name_valid (filedata, unwsec)
9505 && startswith (section_name (filedata, unwsec),
9506 ELF_STRING_ia64_unwind))
9507 suffix = section_name (filedata, unwsec) + len;
b9e920ec
AM
9508 for (i = 0, sec = filedata->section_headers;
9509 i < filedata->file_header.e_shnum;
579f31ac 9510 ++i, ++sec)
84714f86
AM
9511 if (section_name_valid (filedata, sec)
9512 && startswith (section_name (filedata, sec),
9513 ELF_STRING_ia64_unwind_info)
9514 && streq (section_name (filedata, sec) + len2, suffix))
579f31ac
JJ
9515 break;
9516 }
9517
dda8d76d 9518 if (i == filedata->file_header.e_shnum)
579f31ac
JJ
9519 {
9520 printf (_("\nCould not find unwind info section for "));
9521
dda8d76d 9522 if (filedata->string_table == NULL)
579f31ac
JJ
9523 printf ("%d", unwsec->sh_name);
9524 else
dda8d76d 9525 printf ("'%s'", printable_section_name (filedata, unwsec));
579f31ac
JJ
9526 }
9527 else
4d6ed7c8 9528 {
4d6ed7c8 9529 aux.info_addr = sec->sh_addr;
dda8d76d 9530 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
4082ef84
NC
9531 sec->sh_size,
9532 _("unwind info"));
59245841 9533 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
4d6ed7c8 9534
579f31ac 9535 printf (_("\nUnwind section "));
4d6ed7c8 9536
dda8d76d 9537 if (filedata->string_table == NULL)
579f31ac
JJ
9538 printf ("%d", unwsec->sh_name);
9539 else
dda8d76d 9540 printf ("'%s'", printable_section_name (filedata, unwsec));
4d6ed7c8 9541
26c527e6
AM
9542 printf (_(" at offset %#" PRIx64 " contains %" PRIu64 " entries:\n"),
9543 unwsec->sh_offset,
9544 unwsec->sh_size / (3 * eh_addr_size));
4d6ed7c8 9545
dda8d76d 9546 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
53774b7e 9547 && aux.table_len > 0)
dda8d76d 9548 dump_ia64_unwind (filedata, & aux);
579f31ac 9549
9db70fc3
AM
9550 free ((char *) aux.table);
9551 free ((char *) aux.info);
579f31ac
JJ
9552 aux.table = NULL;
9553 aux.info = NULL;
9554 }
4d6ed7c8 9555 }
4d6ed7c8 9556
9db70fc3
AM
9557 free (aux.symtab);
9558 free ((char *) aux.strtab);
32ec8896
NC
9559
9560 return res;
4d6ed7c8
NC
9561}
9562
3f5e193b 9563struct hppa_unw_table_entry
32ec8896
NC
9564{
9565 struct absaddr start;
9566 struct absaddr end;
9567 unsigned int Cannot_unwind:1; /* 0 */
9568 unsigned int Millicode:1; /* 1 */
9569 unsigned int Millicode_save_sr0:1; /* 2 */
9570 unsigned int Region_description:2; /* 3..4 */
9571 unsigned int reserved1:1; /* 5 */
9572 unsigned int Entry_SR:1; /* 6 */
9573 unsigned int Entry_FR:4; /* Number saved 7..10 */
9574 unsigned int Entry_GR:5; /* Number saved 11..15 */
9575 unsigned int Args_stored:1; /* 16 */
9576 unsigned int Variable_Frame:1; /* 17 */
9577 unsigned int Separate_Package_Body:1; /* 18 */
9578 unsigned int Frame_Extension_Millicode:1; /* 19 */
9579 unsigned int Stack_Overflow_Check:1; /* 20 */
9580 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
9581 unsigned int Ada_Region:1; /* 22 */
9582 unsigned int cxx_info:1; /* 23 */
9583 unsigned int cxx_try_catch:1; /* 24 */
9584 unsigned int sched_entry_seq:1; /* 25 */
9585 unsigned int reserved2:1; /* 26 */
9586 unsigned int Save_SP:1; /* 27 */
9587 unsigned int Save_RP:1; /* 28 */
9588 unsigned int Save_MRP_in_frame:1; /* 29 */
9589 unsigned int extn_ptr_defined:1; /* 30 */
9590 unsigned int Cleanup_defined:1; /* 31 */
9591
9592 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
9593 unsigned int HP_UX_interrupt_marker:1; /* 1 */
9594 unsigned int Large_frame:1; /* 2 */
9595 unsigned int Pseudo_SP_Set:1; /* 3 */
9596 unsigned int reserved4:1; /* 4 */
9597 unsigned int Total_frame_size:27; /* 5..31 */
9598};
3f5e193b 9599
57346661 9600struct hppa_unw_aux_info
948f632f 9601{
32ec8896 9602 struct hppa_unw_table_entry * table; /* Unwind table. */
26c527e6 9603 uint64_t table_len; /* Length of unwind table. */
625d49fc 9604 uint64_t seg_base; /* Starting address of segment. */
32ec8896 9605 Elf_Internal_Sym * symtab; /* The symbol table. */
26c527e6 9606 uint64_t nsyms; /* Number of symbols. */
32ec8896 9607 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
26c527e6 9608 uint64_t nfuns; /* Number of entries in funtab. */
32ec8896 9609 char * strtab; /* The string table. */
26c527e6 9610 uint64_t strtab_size; /* Size of string table. */
948f632f 9611};
57346661 9612
015dc7e1 9613static bool
dda8d76d 9614dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
57346661 9615{
2cf0635d 9616 struct hppa_unw_table_entry * tp;
26c527e6 9617 uint64_t j, nfuns;
015dc7e1 9618 bool res = true;
948f632f
DA
9619
9620 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9621 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9622 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9623 aux->funtab[nfuns++] = aux->symtab[j];
9624 aux->nfuns = nfuns;
9625 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
57346661 9626
57346661
AM
9627 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
9628 {
625d49fc 9629 uint64_t offset;
2cf0635d 9630 const char * procname;
57346661 9631
dda8d76d 9632 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
57346661
AM
9633 aux->strtab_size, tp->start, &procname,
9634 &offset);
9635
9636 fputs ("\n<", stdout);
9637
9638 if (procname)
9639 {
9640 fputs (procname, stdout);
9641
9642 if (offset)
26c527e6 9643 printf ("+%" PRIx64, offset);
57346661
AM
9644 }
9645
9646 fputs (">: [", stdout);
9647 print_vma (tp->start.offset, PREFIX_HEX);
9648 fputc ('-', stdout);
9649 print_vma (tp->end.offset, PREFIX_HEX);
9650 printf ("]\n\t");
9651
18bd398b
NC
9652#define PF(_m) if (tp->_m) printf (#_m " ");
9653#define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
57346661
AM
9654 PF(Cannot_unwind);
9655 PF(Millicode);
9656 PF(Millicode_save_sr0);
18bd398b 9657 /* PV(Region_description); */
57346661
AM
9658 PF(Entry_SR);
9659 PV(Entry_FR);
9660 PV(Entry_GR);
9661 PF(Args_stored);
9662 PF(Variable_Frame);
9663 PF(Separate_Package_Body);
9664 PF(Frame_Extension_Millicode);
9665 PF(Stack_Overflow_Check);
9666 PF(Two_Instruction_SP_Increment);
9667 PF(Ada_Region);
9668 PF(cxx_info);
9669 PF(cxx_try_catch);
9670 PF(sched_entry_seq);
9671 PF(Save_SP);
9672 PF(Save_RP);
9673 PF(Save_MRP_in_frame);
9674 PF(extn_ptr_defined);
9675 PF(Cleanup_defined);
9676 PF(MPE_XL_interrupt_marker);
9677 PF(HP_UX_interrupt_marker);
9678 PF(Large_frame);
9679 PF(Pseudo_SP_Set);
9680 PV(Total_frame_size);
9681#undef PF
9682#undef PV
9683 }
9684
18bd398b 9685 printf ("\n");
948f632f
DA
9686
9687 free (aux->funtab);
32ec8896
NC
9688
9689 return res;
57346661
AM
9690}
9691
015dc7e1 9692static bool
dda8d76d
NC
9693slurp_hppa_unwind_table (Filedata * filedata,
9694 struct hppa_unw_aux_info * aux,
9695 Elf_Internal_Shdr * sec)
57346661 9696{
26c527e6 9697 uint64_t size, unw_ent_size, nentries, nrelas, i;
2cf0635d
NC
9698 Elf_Internal_Phdr * seg;
9699 struct hppa_unw_table_entry * tep;
9700 Elf_Internal_Shdr * relsec;
9701 Elf_Internal_Rela * rela;
9702 Elf_Internal_Rela * rp;
9703 unsigned char * table;
9704 unsigned char * tp;
9705 Elf_Internal_Sym * sym;
9706 const char * relname;
57346661 9707
57346661
AM
9708 /* First, find the starting address of the segment that includes
9709 this section. */
dda8d76d 9710 if (filedata->file_header.e_phnum)
57346661 9711 {
dda8d76d 9712 if (! get_program_headers (filedata))
015dc7e1 9713 return false;
57346661 9714
dda8d76d
NC
9715 for (seg = filedata->program_headers;
9716 seg < filedata->program_headers + filedata->file_header.e_phnum;
57346661
AM
9717 ++seg)
9718 {
9719 if (seg->p_type != PT_LOAD)
9720 continue;
9721
9722 if (sec->sh_addr >= seg->p_vaddr
9723 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
9724 {
9725 aux->seg_base = seg->p_vaddr;
9726 break;
9727 }
9728 }
9729 }
9730
9731 /* Second, build the unwind table from the contents of the unwind
9732 section. */
9733 size = sec->sh_size;
dda8d76d 9734 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
3f5e193b 9735 _("unwind table"));
57346661 9736 if (!table)
015dc7e1 9737 return false;
57346661 9738
1c0751b2
DA
9739 unw_ent_size = 16;
9740 nentries = size / unw_ent_size;
9741 size = unw_ent_size * nentries;
57346661 9742
e3fdc001 9743 aux->table_len = nentries;
3f5e193b
NC
9744 tep = aux->table = (struct hppa_unw_table_entry *)
9745 xcmalloc (nentries, sizeof (aux->table[0]));
57346661 9746
1c0751b2 9747 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
57346661
AM
9748 {
9749 unsigned int tmp1, tmp2;
9750
9751 tep->start.section = SHN_UNDEF;
9752 tep->end.section = SHN_UNDEF;
9753
1c0751b2
DA
9754 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
9755 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
9756 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
9757 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
9758
9759 tep->start.offset += aux->seg_base;
9760 tep->end.offset += aux->seg_base;
57346661
AM
9761
9762 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
9763 tep->Millicode = (tmp1 >> 30) & 0x1;
9764 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
9765 tep->Region_description = (tmp1 >> 27) & 0x3;
9766 tep->reserved1 = (tmp1 >> 26) & 0x1;
9767 tep->Entry_SR = (tmp1 >> 25) & 0x1;
9768 tep->Entry_FR = (tmp1 >> 21) & 0xf;
9769 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
9770 tep->Args_stored = (tmp1 >> 15) & 0x1;
9771 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
9772 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
9773 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
9774 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
9775 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
9776 tep->Ada_Region = (tmp1 >> 9) & 0x1;
9777 tep->cxx_info = (tmp1 >> 8) & 0x1;
9778 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
9779 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
9780 tep->reserved2 = (tmp1 >> 5) & 0x1;
9781 tep->Save_SP = (tmp1 >> 4) & 0x1;
9782 tep->Save_RP = (tmp1 >> 3) & 0x1;
9783 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
9784 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
9785 tep->Cleanup_defined = tmp1 & 0x1;
9786
9787 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
9788 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
9789 tep->Large_frame = (tmp2 >> 29) & 0x1;
9790 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
9791 tep->reserved4 = (tmp2 >> 27) & 0x1;
9792 tep->Total_frame_size = tmp2 & 0x7ffffff;
57346661
AM
9793 }
9794 free (table);
9795
9796 /* Third, apply any relocations to the unwind table. */
dda8d76d
NC
9797 for (relsec = filedata->section_headers;
9798 relsec < filedata->section_headers + filedata->file_header.e_shnum;
57346661
AM
9799 ++relsec)
9800 {
9801 if (relsec->sh_type != SHT_RELA
dda8d76d
NC
9802 || relsec->sh_info >= filedata->file_header.e_shnum
9803 || filedata->section_headers + relsec->sh_info != sec)
57346661
AM
9804 continue;
9805
dda8d76d 9806 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
57346661 9807 & rela, & nrelas))
015dc7e1 9808 return false;
57346661
AM
9809
9810 for (rp = rela; rp < rela + nrelas; ++rp)
9811 {
4770fb94 9812 unsigned int sym_ndx;
726bd37d
AM
9813 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
9814 relname = elf_hppa_reloc_type (r_type);
57346661 9815
726bd37d
AM
9816 if (relname == NULL)
9817 {
9818 warn (_("Skipping unknown relocation type: %u\n"), r_type);
9819 continue;
9820 }
9821
57346661 9822 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
24d127aa 9823 if (! startswith (relname, "R_PARISC_SEGREL"))
57346661 9824 {
726bd37d 9825 warn (_("Skipping unexpected relocation type: %s\n"), relname);
57346661
AM
9826 continue;
9827 }
9828
9829 i = rp->r_offset / unw_ent_size;
726bd37d
AM
9830 if (i >= aux->table_len)
9831 {
26c527e6
AM
9832 warn (_("Skipping reloc with overlarge offset: %#" PRIx64 "\n"),
9833 i);
726bd37d
AM
9834 continue;
9835 }
57346661 9836
4770fb94
AM
9837 sym_ndx = get_reloc_symindex (rp->r_info);
9838 if (sym_ndx >= aux->nsyms)
9839 {
9840 warn (_("Skipping reloc with invalid symbol index: %u\n"),
9841 sym_ndx);
9842 continue;
9843 }
9844 sym = aux->symtab + sym_ndx;
9845
43f6cd05 9846 switch ((rp->r_offset % unw_ent_size) / 4)
57346661
AM
9847 {
9848 case 0:
9849 aux->table[i].start.section = sym->st_shndx;
1e456d54 9850 aux->table[i].start.offset = sym->st_value + rp->r_addend;
57346661
AM
9851 break;
9852 case 1:
9853 aux->table[i].end.section = sym->st_shndx;
1e456d54 9854 aux->table[i].end.offset = sym->st_value + rp->r_addend;
57346661
AM
9855 break;
9856 default:
9857 break;
9858 }
9859 }
9860
9861 free (rela);
9862 }
9863
015dc7e1 9864 return true;
57346661
AM
9865}
9866
015dc7e1 9867static bool
dda8d76d 9868hppa_process_unwind (Filedata * filedata)
57346661 9869{
57346661 9870 struct hppa_unw_aux_info aux;
2cf0635d 9871 Elf_Internal_Shdr * unwsec = NULL;
2cf0635d 9872 Elf_Internal_Shdr * sec;
26c527e6 9873 size_t i;
015dc7e1 9874 bool res = true;
57346661 9875
dda8d76d 9876 if (filedata->string_table == NULL)
015dc7e1 9877 return false;
1b31d05e
NC
9878
9879 memset (& aux, 0, sizeof (aux));
57346661 9880
dda8d76d 9881 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
57346661 9882 {
28d13567 9883 if (sec->sh_type == SHT_SYMTAB)
57346661 9884 {
28d13567 9885 if (aux.symtab)
4082ef84 9886 {
28d13567
AM
9887 error (_("Multiple symbol tables encountered\n"));
9888 free (aux.symtab);
9889 aux.symtab = NULL;
4082ef84 9890 free (aux.strtab);
28d13567 9891 aux.strtab = NULL;
4082ef84 9892 }
28d13567
AM
9893 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9894 &aux.strtab, &aux.strtab_size))
015dc7e1 9895 return false;
57346661 9896 }
84714f86
AM
9897 else if (section_name_valid (filedata, sec)
9898 && streq (section_name (filedata, sec), ".PARISC.unwind"))
57346661
AM
9899 unwsec = sec;
9900 }
9901
9902 if (!unwsec)
9903 printf (_("\nThere are no unwind sections in this file.\n"));
9904
dda8d76d 9905 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
57346661 9906 {
84714f86
AM
9907 if (section_name_valid (filedata, sec)
9908 && streq (section_name (filedata, sec), ".PARISC.unwind"))
57346661 9909 {
26c527e6 9910 uint64_t num_unwind = sec->sh_size / 16;
dda8d76d 9911
26c527e6
AM
9912 printf (ngettext ("\nUnwind section '%s' at offset %#" PRIx64 " "
9913 "contains %" PRIu64 " entry:\n",
9914 "\nUnwind section '%s' at offset %#" PRIx64 " "
9915 "contains %" PRIu64 " entries:\n",
d3a49aa8 9916 num_unwind),
dda8d76d 9917 printable_section_name (filedata, sec),
26c527e6 9918 sec->sh_offset,
d3a49aa8 9919 num_unwind);
57346661 9920
dda8d76d 9921 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
015dc7e1 9922 res = false;
66b09c7e
S
9923
9924 if (res && aux.table_len > 0)
32ec8896 9925 {
dda8d76d 9926 if (! dump_hppa_unwind (filedata, &aux))
015dc7e1 9927 res = false;
32ec8896 9928 }
57346661 9929
9db70fc3 9930 free ((char *) aux.table);
57346661
AM
9931 aux.table = NULL;
9932 }
9933 }
9934
9db70fc3
AM
9935 free (aux.symtab);
9936 free ((char *) aux.strtab);
32ec8896
NC
9937
9938 return res;
57346661
AM
9939}
9940
0b6ae522
DJ
9941struct arm_section
9942{
a734115a
NC
9943 unsigned char * data; /* The unwind data. */
9944 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
9945 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
26c527e6 9946 uint64_t nrelas; /* The number of relocations. */
a734115a
NC
9947 unsigned int rel_type; /* REL or RELA ? */
9948 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
0b6ae522
DJ
9949};
9950
9951struct arm_unw_aux_info
9952{
dda8d76d 9953 Filedata * filedata; /* The file containing the unwind sections. */
a734115a 9954 Elf_Internal_Sym * symtab; /* The file's symbol table. */
26c527e6 9955 uint64_t nsyms; /* Number of symbols. */
948f632f 9956 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
26c527e6 9957 uint64_t nfuns; /* Number of these symbols. */
a734115a 9958 char * strtab; /* The file's string table. */
26c527e6 9959 uint64_t strtab_size; /* Size of string table. */
0b6ae522
DJ
9960};
9961
9962static const char *
dda8d76d
NC
9963arm_print_vma_and_name (Filedata * filedata,
9964 struct arm_unw_aux_info * aux,
625d49fc 9965 uint64_t fn,
dda8d76d 9966 struct absaddr addr)
0b6ae522
DJ
9967{
9968 const char *procname;
625d49fc 9969 uint64_t sym_offset;
0b6ae522
DJ
9970
9971 if (addr.section == SHN_UNDEF)
9972 addr.offset = fn;
9973
dda8d76d 9974 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
0b6ae522
DJ
9975 aux->strtab_size, addr, &procname,
9976 &sym_offset);
9977
9978 print_vma (fn, PREFIX_HEX);
9979
9980 if (procname)
9981 {
9982 fputs (" <", stdout);
9983 fputs (procname, stdout);
9984
9985 if (sym_offset)
26c527e6 9986 printf ("+0x%" PRIx64, sym_offset);
0b6ae522
DJ
9987 fputc ('>', stdout);
9988 }
9989
9990 return procname;
9991}
9992
9993static void
9994arm_free_section (struct arm_section *arm_sec)
9995{
9db70fc3
AM
9996 free (arm_sec->data);
9997 free (arm_sec->rela);
0b6ae522
DJ
9998}
9999
a734115a
NC
10000/* 1) If SEC does not match the one cached in ARM_SEC, then free the current
10001 cached section and install SEC instead.
10002 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
10003 and return its valued in * WORDP, relocating if necessary.
1b31d05e 10004 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
a734115a 10005 relocation's offset in ADDR.
1b31d05e
NC
10006 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
10007 into the string table of the symbol associated with the reloc. If no
10008 reloc was applied store -1 there.
10009 5) Return TRUE upon success, FALSE otherwise. */
a734115a 10010
015dc7e1 10011static bool
dda8d76d
NC
10012get_unwind_section_word (Filedata * filedata,
10013 struct arm_unw_aux_info * aux,
1b31d05e
NC
10014 struct arm_section * arm_sec,
10015 Elf_Internal_Shdr * sec,
625d49fc 10016 uint64_t word_offset,
1b31d05e
NC
10017 unsigned int * wordp,
10018 struct absaddr * addr,
625d49fc 10019 uint64_t * sym_name)
0b6ae522
DJ
10020{
10021 Elf_Internal_Rela *rp;
10022 Elf_Internal_Sym *sym;
10023 const char * relname;
10024 unsigned int word;
015dc7e1 10025 bool wrapped;
0b6ae522 10026
e0a31db1 10027 if (sec == NULL || arm_sec == NULL)
015dc7e1 10028 return false;
e0a31db1 10029
0b6ae522
DJ
10030 addr->section = SHN_UNDEF;
10031 addr->offset = 0;
10032
1b31d05e 10033 if (sym_name != NULL)
625d49fc 10034 *sym_name = (uint64_t) -1;
1b31d05e 10035
a734115a 10036 /* If necessary, update the section cache. */
0b6ae522
DJ
10037 if (sec != arm_sec->sec)
10038 {
10039 Elf_Internal_Shdr *relsec;
10040
10041 arm_free_section (arm_sec);
10042
10043 arm_sec->sec = sec;
dda8d76d 10044 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
0b6ae522 10045 sec->sh_size, _("unwind data"));
0b6ae522
DJ
10046 arm_sec->rela = NULL;
10047 arm_sec->nrelas = 0;
10048
dda8d76d
NC
10049 for (relsec = filedata->section_headers;
10050 relsec < filedata->section_headers + filedata->file_header.e_shnum;
0b6ae522
DJ
10051 ++relsec)
10052 {
dda8d76d
NC
10053 if (relsec->sh_info >= filedata->file_header.e_shnum
10054 || filedata->section_headers + relsec->sh_info != sec
1ae40aa4
NC
10055 /* PR 15745: Check the section type as well. */
10056 || (relsec->sh_type != SHT_REL
10057 && relsec->sh_type != SHT_RELA))
0b6ae522
DJ
10058 continue;
10059
a734115a 10060 arm_sec->rel_type = relsec->sh_type;
0b6ae522
DJ
10061 if (relsec->sh_type == SHT_REL)
10062 {
dda8d76d 10063 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
0b6ae522
DJ
10064 relsec->sh_size,
10065 & arm_sec->rela, & arm_sec->nrelas))
015dc7e1 10066 return false;
0b6ae522 10067 }
1ae40aa4 10068 else /* relsec->sh_type == SHT_RELA */
0b6ae522 10069 {
dda8d76d 10070 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
0b6ae522
DJ
10071 relsec->sh_size,
10072 & arm_sec->rela, & arm_sec->nrelas))
015dc7e1 10073 return false;
0b6ae522 10074 }
1ae40aa4 10075 break;
0b6ae522
DJ
10076 }
10077
10078 arm_sec->next_rela = arm_sec->rela;
10079 }
10080
a734115a 10081 /* If there is no unwind data we can do nothing. */
0b6ae522 10082 if (arm_sec->data == NULL)
015dc7e1 10083 return false;
0b6ae522 10084
e0a31db1 10085 /* If the offset is invalid then fail. */
f32ba729
NC
10086 if (/* PR 21343 *//* PR 18879 */
10087 sec->sh_size < 4
625d49fc 10088 || word_offset > sec->sh_size - 4)
015dc7e1 10089 return false;
e0a31db1 10090
a734115a 10091 /* Get the word at the required offset. */
0b6ae522
DJ
10092 word = byte_get (arm_sec->data + word_offset, 4);
10093
0eff7165
NC
10094 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
10095 if (arm_sec->rela == NULL)
10096 {
10097 * wordp = word;
015dc7e1 10098 return true;
0eff7165
NC
10099 }
10100
a734115a 10101 /* Look through the relocs to find the one that applies to the provided offset. */
015dc7e1 10102 wrapped = false;
0b6ae522
DJ
10103 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
10104 {
625d49fc 10105 uint64_t prelval, offset;
0b6ae522
DJ
10106
10107 if (rp->r_offset > word_offset && !wrapped)
10108 {
10109 rp = arm_sec->rela;
015dc7e1 10110 wrapped = true;
0b6ae522
DJ
10111 }
10112 if (rp->r_offset > word_offset)
10113 break;
10114
10115 if (rp->r_offset & 3)
10116 {
26c527e6
AM
10117 warn (_("Skipping unexpected relocation at offset %#" PRIx64 "\n"),
10118 rp->r_offset);
0b6ae522
DJ
10119 continue;
10120 }
10121
10122 if (rp->r_offset < word_offset)
10123 continue;
10124
74e1a04b
NC
10125 /* PR 17531: file: 027-161405-0.004 */
10126 if (aux->symtab == NULL)
10127 continue;
10128
0b6ae522
DJ
10129 if (arm_sec->rel_type == SHT_REL)
10130 {
10131 offset = word & 0x7fffffff;
10132 if (offset & 0x40000000)
625d49fc 10133 offset |= ~ (uint64_t) 0x7fffffff;
0b6ae522 10134 }
a734115a 10135 else if (arm_sec->rel_type == SHT_RELA)
0b6ae522 10136 offset = rp->r_addend;
a734115a 10137 else
74e1a04b
NC
10138 {
10139 error (_("Unknown section relocation type %d encountered\n"),
10140 arm_sec->rel_type);
10141 break;
10142 }
0b6ae522 10143
071436c6
NC
10144 /* PR 17531 file: 027-1241568-0.004. */
10145 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
10146 {
26c527e6
AM
10147 error (_("Bad symbol index in unwind relocation "
10148 "(%" PRIu64 " > %" PRIu64 ")\n"),
10149 ELF32_R_SYM (rp->r_info), aux->nsyms);
071436c6
NC
10150 break;
10151 }
10152
10153 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
0b6ae522
DJ
10154 offset += sym->st_value;
10155 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
10156
a734115a 10157 /* Check that we are processing the expected reloc type. */
dda8d76d 10158 if (filedata->file_header.e_machine == EM_ARM)
a734115a
NC
10159 {
10160 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
071436c6
NC
10161 if (relname == NULL)
10162 {
10163 warn (_("Skipping unknown ARM relocation type: %d\n"),
10164 (int) ELF32_R_TYPE (rp->r_info));
10165 continue;
10166 }
a734115a
NC
10167
10168 if (streq (relname, "R_ARM_NONE"))
10169 continue;
0b4362b0 10170
a734115a
NC
10171 if (! streq (relname, "R_ARM_PREL31"))
10172 {
071436c6 10173 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
a734115a
NC
10174 continue;
10175 }
10176 }
dda8d76d 10177 else if (filedata->file_header.e_machine == EM_TI_C6000)
a734115a
NC
10178 {
10179 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
071436c6
NC
10180 if (relname == NULL)
10181 {
10182 warn (_("Skipping unknown C6000 relocation type: %d\n"),
10183 (int) ELF32_R_TYPE (rp->r_info));
10184 continue;
10185 }
0b4362b0 10186
a734115a
NC
10187 if (streq (relname, "R_C6000_NONE"))
10188 continue;
10189
10190 if (! streq (relname, "R_C6000_PREL31"))
10191 {
071436c6 10192 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
a734115a
NC
10193 continue;
10194 }
10195
10196 prelval >>= 1;
10197 }
10198 else
74e1a04b
NC
10199 {
10200 /* This function currently only supports ARM and TI unwinders. */
10201 warn (_("Only TI and ARM unwinders are currently supported\n"));
10202 break;
10203 }
fa197c1c 10204
625d49fc 10205 word = (word & ~ (uint64_t) 0x7fffffff) | (prelval & 0x7fffffff);
0b6ae522
DJ
10206 addr->section = sym->st_shndx;
10207 addr->offset = offset;
74e1a04b 10208
1b31d05e
NC
10209 if (sym_name)
10210 * sym_name = sym->st_name;
0b6ae522
DJ
10211 break;
10212 }
10213
10214 *wordp = word;
10215 arm_sec->next_rela = rp;
10216
015dc7e1 10217 return true;
0b6ae522
DJ
10218}
10219
a734115a
NC
10220static const char *tic6x_unwind_regnames[16] =
10221{
0b4362b0
RM
10222 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
10223 "A14", "A13", "A12", "A11", "A10",
a734115a
NC
10224 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
10225};
fa197c1c 10226
0b6ae522 10227static void
fa197c1c 10228decode_tic6x_unwind_regmask (unsigned int mask)
0b6ae522 10229{
fa197c1c
PB
10230 int i;
10231
10232 for (i = 12; mask; mask >>= 1, i--)
10233 {
10234 if (mask & 1)
10235 {
10236 fputs (tic6x_unwind_regnames[i], stdout);
10237 if (mask > 1)
10238 fputs (", ", stdout);
10239 }
10240 }
10241}
0b6ae522
DJ
10242
10243#define ADVANCE \
10244 if (remaining == 0 && more_words) \
10245 { \
10246 data_offset += 4; \
dda8d76d 10247 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
1b31d05e 10248 data_offset, & word, & addr, NULL)) \
015dc7e1 10249 return false; \
0b6ae522
DJ
10250 remaining = 4; \
10251 more_words--; \
10252 } \
10253
10254#define GET_OP(OP) \
10255 ADVANCE; \
10256 if (remaining) \
10257 { \
10258 remaining--; \
10259 (OP) = word >> 24; \
10260 word <<= 8; \
10261 } \
10262 else \
10263 { \
2b692964 10264 printf (_("[Truncated opcode]\n")); \
015dc7e1 10265 return false; \
0b6ae522 10266 } \
cc5914eb 10267 printf ("0x%02x ", OP)
0b6ae522 10268
015dc7e1 10269static bool
dda8d76d
NC
10270decode_arm_unwind_bytecode (Filedata * filedata,
10271 struct arm_unw_aux_info * aux,
948f632f
DA
10272 unsigned int word,
10273 unsigned int remaining,
10274 unsigned int more_words,
625d49fc 10275 uint64_t data_offset,
948f632f
DA
10276 Elf_Internal_Shdr * data_sec,
10277 struct arm_section * data_arm_sec)
fa197c1c
PB
10278{
10279 struct absaddr addr;
015dc7e1 10280 bool res = true;
0b6ae522
DJ
10281
10282 /* Decode the unwinding instructions. */
10283 while (1)
10284 {
10285 unsigned int op, op2;
10286
10287 ADVANCE;
10288 if (remaining == 0)
10289 break;
10290 remaining--;
10291 op = word >> 24;
10292 word <<= 8;
10293
cc5914eb 10294 printf (" 0x%02x ", op);
0b6ae522
DJ
10295
10296 if ((op & 0xc0) == 0x00)
10297 {
10298 int offset = ((op & 0x3f) << 2) + 4;
61865e30 10299
cc5914eb 10300 printf (" vsp = vsp + %d", offset);
0b6ae522
DJ
10301 }
10302 else if ((op & 0xc0) == 0x40)
10303 {
10304 int offset = ((op & 0x3f) << 2) + 4;
61865e30 10305
cc5914eb 10306 printf (" vsp = vsp - %d", offset);
0b6ae522
DJ
10307 }
10308 else if ((op & 0xf0) == 0x80)
10309 {
10310 GET_OP (op2);
10311 if (op == 0x80 && op2 == 0)
10312 printf (_("Refuse to unwind"));
10313 else
10314 {
10315 unsigned int mask = ((op & 0x0f) << 8) | op2;
015dc7e1 10316 bool first = true;
0b6ae522 10317 int i;
2b692964 10318
0b6ae522
DJ
10319 printf ("pop {");
10320 for (i = 0; i < 12; i++)
10321 if (mask & (1 << i))
10322 {
10323 if (first)
015dc7e1 10324 first = false;
0b6ae522
DJ
10325 else
10326 printf (", ");
10327 printf ("r%d", 4 + i);
10328 }
10329 printf ("}");
10330 }
10331 }
10332 else if ((op & 0xf0) == 0x90)
10333 {
10334 if (op == 0x9d || op == 0x9f)
10335 printf (_(" [Reserved]"));
10336 else
cc5914eb 10337 printf (" vsp = r%d", op & 0x0f);
0b6ae522
DJ
10338 }
10339 else if ((op & 0xf0) == 0xa0)
10340 {
10341 int end = 4 + (op & 0x07);
015dc7e1 10342 bool first = true;
0b6ae522 10343 int i;
61865e30 10344
0b6ae522
DJ
10345 printf (" pop {");
10346 for (i = 4; i <= end; i++)
10347 {
10348 if (first)
015dc7e1 10349 first = false;
0b6ae522
DJ
10350 else
10351 printf (", ");
10352 printf ("r%d", i);
10353 }
10354 if (op & 0x08)
10355 {
1b31d05e 10356 if (!first)
0b6ae522
DJ
10357 printf (", ");
10358 printf ("r14");
10359 }
10360 printf ("}");
10361 }
10362 else if (op == 0xb0)
10363 printf (_(" finish"));
10364 else if (op == 0xb1)
10365 {
10366 GET_OP (op2);
10367 if (op2 == 0 || (op2 & 0xf0) != 0)
10368 printf (_("[Spare]"));
10369 else
10370 {
10371 unsigned int mask = op2 & 0x0f;
015dc7e1 10372 bool first = true;
0b6ae522 10373 int i;
61865e30 10374
0b6ae522
DJ
10375 printf ("pop {");
10376 for (i = 0; i < 12; i++)
10377 if (mask & (1 << i))
10378 {
10379 if (first)
015dc7e1 10380 first = false;
0b6ae522
DJ
10381 else
10382 printf (", ");
10383 printf ("r%d", i);
10384 }
10385 printf ("}");
10386 }
10387 }
10388 else if (op == 0xb2)
10389 {
b115cf96 10390 unsigned char buf[9];
0b6ae522 10391 unsigned int i, len;
26c527e6 10392 uint64_t offset;
61865e30 10393
b115cf96 10394 for (i = 0; i < sizeof (buf); i++)
0b6ae522
DJ
10395 {
10396 GET_OP (buf[i]);
10397 if ((buf[i] & 0x80) == 0)
10398 break;
10399 }
4082ef84 10400 if (i == sizeof (buf))
32ec8896 10401 {
27a45f42 10402 error (_("corrupt change to vsp\n"));
015dc7e1 10403 res = false;
32ec8896 10404 }
4082ef84
NC
10405 else
10406 {
015dc7e1 10407 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
4082ef84
NC
10408 assert (len == i + 1);
10409 offset = offset * 4 + 0x204;
26c527e6 10410 printf ("vsp = vsp + %" PRId64, offset);
4082ef84 10411 }
0b6ae522 10412 }
61865e30 10413 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
0b6ae522 10414 {
61865e30
NC
10415 unsigned int first, last;
10416
10417 GET_OP (op2);
10418 first = op2 >> 4;
10419 last = op2 & 0x0f;
10420 if (op == 0xc8)
10421 first = first + 16;
10422 printf ("pop {D%d", first);
10423 if (last)
10424 printf ("-D%d", first + last);
10425 printf ("}");
10426 }
09854a88
TB
10427 else if (op == 0xb4)
10428 printf (_(" pop {ra_auth_code}"));
b62fb887
SP
10429 else if (op == 0xb5)
10430 printf (_(" vsp as modifier for PAC validation"));
61865e30
NC
10431 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
10432 {
10433 unsigned int count = op & 0x07;
10434
10435 printf ("pop {D8");
10436 if (count)
10437 printf ("-D%d", 8 + count);
10438 printf ("}");
10439 }
10440 else if (op >= 0xc0 && op <= 0xc5)
10441 {
10442 unsigned int count = op & 0x07;
10443
10444 printf (" pop {wR10");
10445 if (count)
10446 printf ("-wR%d", 10 + count);
10447 printf ("}");
10448 }
10449 else if (op == 0xc6)
10450 {
10451 unsigned int first, last;
10452
10453 GET_OP (op2);
10454 first = op2 >> 4;
10455 last = op2 & 0x0f;
10456 printf ("pop {wR%d", first);
10457 if (last)
10458 printf ("-wR%d", first + last);
10459 printf ("}");
10460 }
10461 else if (op == 0xc7)
10462 {
10463 GET_OP (op2);
10464 if (op2 == 0 || (op2 & 0xf0) != 0)
10465 printf (_("[Spare]"));
0b6ae522
DJ
10466 else
10467 {
61865e30 10468 unsigned int mask = op2 & 0x0f;
015dc7e1 10469 bool first = true;
61865e30
NC
10470 int i;
10471
10472 printf ("pop {");
10473 for (i = 0; i < 4; i++)
10474 if (mask & (1 << i))
10475 {
10476 if (first)
015dc7e1 10477 first = false;
61865e30
NC
10478 else
10479 printf (", ");
10480 printf ("wCGR%d", i);
10481 }
10482 printf ("}");
0b6ae522
DJ
10483 }
10484 }
61865e30 10485 else
32ec8896
NC
10486 {
10487 printf (_(" [unsupported opcode]"));
015dc7e1 10488 res = false;
32ec8896
NC
10489 }
10490
0b6ae522
DJ
10491 printf ("\n");
10492 }
32ec8896
NC
10493
10494 return res;
fa197c1c
PB
10495}
10496
015dc7e1 10497static bool
dda8d76d
NC
10498decode_tic6x_unwind_bytecode (Filedata * filedata,
10499 struct arm_unw_aux_info * aux,
948f632f
DA
10500 unsigned int word,
10501 unsigned int remaining,
10502 unsigned int more_words,
625d49fc 10503 uint64_t data_offset,
948f632f
DA
10504 Elf_Internal_Shdr * data_sec,
10505 struct arm_section * data_arm_sec)
fa197c1c
PB
10506{
10507 struct absaddr addr;
10508
10509 /* Decode the unwinding instructions. */
10510 while (1)
10511 {
10512 unsigned int op, op2;
10513
10514 ADVANCE;
10515 if (remaining == 0)
10516 break;
10517 remaining--;
10518 op = word >> 24;
10519 word <<= 8;
10520
9cf03b7e 10521 printf (" 0x%02x ", op);
fa197c1c
PB
10522
10523 if ((op & 0xc0) == 0x00)
10524 {
10525 int offset = ((op & 0x3f) << 3) + 8;
9cf03b7e 10526 printf (" sp = sp + %d", offset);
fa197c1c
PB
10527 }
10528 else if ((op & 0xc0) == 0x80)
10529 {
10530 GET_OP (op2);
10531 if (op == 0x80 && op2 == 0)
10532 printf (_("Refuse to unwind"));
10533 else
10534 {
10535 unsigned int mask = ((op & 0x1f) << 8) | op2;
10536 if (op & 0x20)
10537 printf ("pop compact {");
10538 else
10539 printf ("pop {");
10540
10541 decode_tic6x_unwind_regmask (mask);
10542 printf("}");
10543 }
10544 }
10545 else if ((op & 0xf0) == 0xc0)
10546 {
10547 unsigned int reg;
10548 unsigned int nregs;
10549 unsigned int i;
10550 const char *name;
a734115a
NC
10551 struct
10552 {
32ec8896
NC
10553 unsigned int offset;
10554 unsigned int reg;
fa197c1c
PB
10555 } regpos[16];
10556
10557 /* Scan entire instruction first so that GET_OP output is not
10558 interleaved with disassembly. */
10559 nregs = 0;
10560 for (i = 0; nregs < (op & 0xf); i++)
10561 {
10562 GET_OP (op2);
10563 reg = op2 >> 4;
10564 if (reg != 0xf)
10565 {
10566 regpos[nregs].offset = i * 2;
10567 regpos[nregs].reg = reg;
10568 nregs++;
10569 }
10570
10571 reg = op2 & 0xf;
10572 if (reg != 0xf)
10573 {
10574 regpos[nregs].offset = i * 2 + 1;
10575 regpos[nregs].reg = reg;
10576 nregs++;
10577 }
10578 }
10579
10580 printf (_("pop frame {"));
18344509 10581 if (nregs == 0)
fa197c1c 10582 {
18344509
NC
10583 printf (_("*corrupt* - no registers specified"));
10584 }
10585 else
10586 {
10587 reg = nregs - 1;
10588 for (i = i * 2; i > 0; i--)
fa197c1c 10589 {
18344509
NC
10590 if (regpos[reg].offset == i - 1)
10591 {
10592 name = tic6x_unwind_regnames[regpos[reg].reg];
10593 if (reg > 0)
10594 reg--;
10595 }
10596 else
10597 name = _("[pad]");
fa197c1c 10598
18344509
NC
10599 fputs (name, stdout);
10600 if (i > 1)
10601 printf (", ");
10602 }
fa197c1c
PB
10603 }
10604
10605 printf ("}");
10606 }
10607 else if (op == 0xd0)
10608 printf (" MOV FP, SP");
10609 else if (op == 0xd1)
10610 printf (" __c6xabi_pop_rts");
10611 else if (op == 0xd2)
10612 {
10613 unsigned char buf[9];
10614 unsigned int i, len;
26c527e6 10615 uint64_t offset;
a734115a 10616
fa197c1c
PB
10617 for (i = 0; i < sizeof (buf); i++)
10618 {
10619 GET_OP (buf[i]);
10620 if ((buf[i] & 0x80) == 0)
10621 break;
10622 }
0eff7165
NC
10623 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
10624 if (i == sizeof (buf))
10625 {
0eff7165 10626 warn (_("Corrupt stack pointer adjustment detected\n"));
015dc7e1 10627 return false;
0eff7165 10628 }
948f632f 10629
015dc7e1 10630 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
fa197c1c
PB
10631 assert (len == i + 1);
10632 offset = offset * 8 + 0x408;
26c527e6 10633 printf (_("sp = sp + %" PRId64), offset);
fa197c1c
PB
10634 }
10635 else if ((op & 0xf0) == 0xe0)
10636 {
10637 if ((op & 0x0f) == 7)
10638 printf (" RETURN");
10639 else
10640 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
10641 }
10642 else
10643 {
10644 printf (_(" [unsupported opcode]"));
10645 }
10646 putchar ('\n');
10647 }
32ec8896 10648
015dc7e1 10649 return true;
fa197c1c
PB
10650}
10651
625d49fc
AM
10652static uint64_t
10653arm_expand_prel31 (Filedata * filedata, uint64_t word, uint64_t where)
fa197c1c 10654{
625d49fc 10655 uint64_t offset;
fa197c1c
PB
10656
10657 offset = word & 0x7fffffff;
10658 if (offset & 0x40000000)
625d49fc 10659 offset |= ~ (uint64_t) 0x7fffffff;
fa197c1c 10660
dda8d76d 10661 if (filedata->file_header.e_machine == EM_TI_C6000)
fa197c1c
PB
10662 offset <<= 1;
10663
10664 return offset + where;
10665}
10666
015dc7e1 10667static bool
dda8d76d
NC
10668decode_arm_unwind (Filedata * filedata,
10669 struct arm_unw_aux_info * aux,
1b31d05e
NC
10670 unsigned int word,
10671 unsigned int remaining,
625d49fc 10672 uint64_t data_offset,
1b31d05e
NC
10673 Elf_Internal_Shdr * data_sec,
10674 struct arm_section * data_arm_sec)
fa197c1c
PB
10675{
10676 int per_index;
10677 unsigned int more_words = 0;
37e14bc3 10678 struct absaddr addr;
625d49fc 10679 uint64_t sym_name = (uint64_t) -1;
015dc7e1 10680 bool res = true;
fa197c1c
PB
10681
10682 if (remaining == 0)
10683 {
1b31d05e
NC
10684 /* Fetch the first word.
10685 Note - when decoding an object file the address extracted
10686 here will always be 0. So we also pass in the sym_name
10687 parameter so that we can find the symbol associated with
10688 the personality routine. */
dda8d76d 10689 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
1b31d05e 10690 & word, & addr, & sym_name))
015dc7e1 10691 return false;
1b31d05e 10692
fa197c1c
PB
10693 remaining = 4;
10694 }
c93dbb25
CZ
10695 else
10696 {
10697 addr.section = SHN_UNDEF;
10698 addr.offset = 0;
10699 }
fa197c1c
PB
10700
10701 if ((word & 0x80000000) == 0)
10702 {
10703 /* Expand prel31 for personality routine. */
625d49fc 10704 uint64_t fn;
fa197c1c
PB
10705 const char *procname;
10706
dda8d76d 10707 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
fa197c1c 10708 printf (_(" Personality routine: "));
1b31d05e
NC
10709 if (fn == 0
10710 && addr.section == SHN_UNDEF && addr.offset == 0
625d49fc 10711 && sym_name != (uint64_t) -1 && sym_name < aux->strtab_size)
1b31d05e
NC
10712 {
10713 procname = aux->strtab + sym_name;
10714 print_vma (fn, PREFIX_HEX);
10715 if (procname)
10716 {
10717 fputs (" <", stdout);
10718 fputs (procname, stdout);
10719 fputc ('>', stdout);
10720 }
10721 }
10722 else
dda8d76d 10723 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
fa197c1c
PB
10724 fputc ('\n', stdout);
10725
10726 /* The GCC personality routines use the standard compact
10727 encoding, starting with one byte giving the number of
10728 words. */
10729 if (procname != NULL
24d127aa
ML
10730 && (startswith (procname, "__gcc_personality_v0")
10731 || startswith (procname, "__gxx_personality_v0")
10732 || startswith (procname, "__gcj_personality_v0")
10733 || startswith (procname, "__gnu_objc_personality_v0")))
fa197c1c
PB
10734 {
10735 remaining = 0;
10736 more_words = 1;
10737 ADVANCE;
10738 if (!remaining)
10739 {
10740 printf (_(" [Truncated data]\n"));
015dc7e1 10741 return false;
fa197c1c
PB
10742 }
10743 more_words = word >> 24;
10744 word <<= 8;
10745 remaining--;
10746 per_index = -1;
10747 }
10748 else
015dc7e1 10749 return true;
fa197c1c
PB
10750 }
10751 else
10752 {
1b31d05e 10753 /* ARM EHABI Section 6.3:
0b4362b0 10754
1b31d05e 10755 An exception-handling table entry for the compact model looks like:
0b4362b0 10756
1b31d05e
NC
10757 31 30-28 27-24 23-0
10758 -- ----- ----- ----
10759 1 0 index Data for personalityRoutine[index] */
10760
dda8d76d 10761 if (filedata->file_header.e_machine == EM_ARM
1b31d05e 10762 && (word & 0x70000000))
32ec8896
NC
10763 {
10764 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
015dc7e1 10765 res = false;
32ec8896 10766 }
1b31d05e 10767
fa197c1c 10768 per_index = (word >> 24) & 0x7f;
1b31d05e 10769 printf (_(" Compact model index: %d\n"), per_index);
fa197c1c
PB
10770 if (per_index == 0)
10771 {
10772 more_words = 0;
10773 word <<= 8;
10774 remaining--;
10775 }
10776 else if (per_index < 3)
10777 {
10778 more_words = (word >> 16) & 0xff;
10779 word <<= 16;
10780 remaining -= 2;
10781 }
10782 }
10783
dda8d76d 10784 switch (filedata->file_header.e_machine)
fa197c1c
PB
10785 {
10786 case EM_ARM:
10787 if (per_index < 3)
10788 {
dda8d76d 10789 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
32ec8896 10790 data_offset, data_sec, data_arm_sec))
015dc7e1 10791 res = false;
fa197c1c
PB
10792 }
10793 else
1b31d05e
NC
10794 {
10795 warn (_("Unknown ARM compact model index encountered\n"));
10796 printf (_(" [reserved]\n"));
015dc7e1 10797 res = false;
1b31d05e 10798 }
fa197c1c
PB
10799 break;
10800
10801 case EM_TI_C6000:
10802 if (per_index < 3)
10803 {
dda8d76d 10804 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
32ec8896 10805 data_offset, data_sec, data_arm_sec))
015dc7e1 10806 res = false;
fa197c1c
PB
10807 }
10808 else if (per_index < 5)
10809 {
10810 if (((word >> 17) & 0x7f) == 0x7f)
10811 printf (_(" Restore stack from frame pointer\n"));
10812 else
10813 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
10814 printf (_(" Registers restored: "));
10815 if (per_index == 4)
10816 printf (" (compact) ");
10817 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
10818 putchar ('\n');
10819 printf (_(" Return register: %s\n"),
10820 tic6x_unwind_regnames[word & 0xf]);
10821 }
10822 else
1b31d05e 10823 printf (_(" [reserved (%d)]\n"), per_index);
fa197c1c
PB
10824 break;
10825
10826 default:
74e1a04b 10827 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
dda8d76d 10828 filedata->file_header.e_machine);
015dc7e1 10829 res = false;
fa197c1c 10830 }
0b6ae522
DJ
10831
10832 /* Decode the descriptors. Not implemented. */
32ec8896
NC
10833
10834 return res;
0b6ae522
DJ
10835}
10836
015dc7e1 10837static bool
dda8d76d
NC
10838dump_arm_unwind (Filedata * filedata,
10839 struct arm_unw_aux_info * aux,
10840 Elf_Internal_Shdr * exidx_sec)
0b6ae522
DJ
10841{
10842 struct arm_section exidx_arm_sec, extab_arm_sec;
10843 unsigned int i, exidx_len;
26c527e6 10844 uint64_t j, nfuns;
015dc7e1 10845 bool res = true;
0b6ae522
DJ
10846
10847 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
10848 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
10849 exidx_len = exidx_sec->sh_size / 8;
10850
948f632f
DA
10851 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
10852 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
10853 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
10854 aux->funtab[nfuns++] = aux->symtab[j];
10855 aux->nfuns = nfuns;
10856 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
10857
0b6ae522
DJ
10858 for (i = 0; i < exidx_len; i++)
10859 {
10860 unsigned int exidx_fn, exidx_entry;
10861 struct absaddr fn_addr, entry_addr;
625d49fc 10862 uint64_t fn;
0b6ae522
DJ
10863
10864 fputc ('\n', stdout);
10865
dda8d76d 10866 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
1b31d05e 10867 8 * i, & exidx_fn, & fn_addr, NULL)
dda8d76d 10868 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
1b31d05e 10869 8 * i + 4, & exidx_entry, & entry_addr, NULL))
0b6ae522 10870 {
948f632f 10871 free (aux->funtab);
1b31d05e
NC
10872 arm_free_section (& exidx_arm_sec);
10873 arm_free_section (& extab_arm_sec);
015dc7e1 10874 return false;
0b6ae522
DJ
10875 }
10876
83c257ca
NC
10877 /* ARM EHABI, Section 5:
10878 An index table entry consists of 2 words.
10879 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
10880 if (exidx_fn & 0x80000000)
32ec8896
NC
10881 {
10882 warn (_("corrupt index table entry: %x\n"), exidx_fn);
015dc7e1 10883 res = false;
32ec8896 10884 }
83c257ca 10885
dda8d76d 10886 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
0b6ae522 10887
dda8d76d 10888 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
0b6ae522
DJ
10889 fputs (": ", stdout);
10890
10891 if (exidx_entry == 1)
10892 {
10893 print_vma (exidx_entry, PREFIX_HEX);
10894 fputs (" [cantunwind]\n", stdout);
10895 }
10896 else if (exidx_entry & 0x80000000)
10897 {
10898 print_vma (exidx_entry, PREFIX_HEX);
10899 fputc ('\n', stdout);
dda8d76d 10900 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
0b6ae522
DJ
10901 }
10902 else
10903 {
625d49fc 10904 uint64_t table, table_offset = 0;
0b6ae522
DJ
10905 Elf_Internal_Shdr *table_sec;
10906
10907 fputs ("@", stdout);
dda8d76d 10908 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
0b6ae522
DJ
10909 print_vma (table, PREFIX_HEX);
10910 printf ("\n");
10911
10912 /* Locate the matching .ARM.extab. */
10913 if (entry_addr.section != SHN_UNDEF
dda8d76d 10914 && entry_addr.section < filedata->file_header.e_shnum)
0b6ae522 10915 {
dda8d76d 10916 table_sec = filedata->section_headers + entry_addr.section;
0b6ae522 10917 table_offset = entry_addr.offset;
1a915552 10918 /* PR 18879 */
625d49fc 10919 if (table_offset > table_sec->sh_size)
1a915552 10920 {
26c527e6
AM
10921 warn (_("Unwind entry contains corrupt offset (%#" PRIx64 ") into section %s\n"),
10922 table_offset,
dda8d76d 10923 printable_section_name (filedata, table_sec));
015dc7e1 10924 res = false;
1a915552
NC
10925 continue;
10926 }
0b6ae522
DJ
10927 }
10928 else
10929 {
dda8d76d 10930 table_sec = find_section_by_address (filedata, table);
0b6ae522
DJ
10931 if (table_sec != NULL)
10932 table_offset = table - table_sec->sh_addr;
10933 }
32ec8896 10934
0b6ae522
DJ
10935 if (table_sec == NULL)
10936 {
26c527e6
AM
10937 warn (_("Could not locate .ARM.extab section containing %#" PRIx64 ".\n"),
10938 table);
015dc7e1 10939 res = false;
0b6ae522
DJ
10940 continue;
10941 }
32ec8896 10942
dda8d76d 10943 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
32ec8896 10944 &extab_arm_sec))
015dc7e1 10945 res = false;
0b6ae522
DJ
10946 }
10947 }
10948
10949 printf ("\n");
10950
948f632f 10951 free (aux->funtab);
0b6ae522
DJ
10952 arm_free_section (&exidx_arm_sec);
10953 arm_free_section (&extab_arm_sec);
32ec8896
NC
10954
10955 return res;
0b6ae522
DJ
10956}
10957
fa197c1c 10958/* Used for both ARM and C6X unwinding tables. */
1b31d05e 10959
015dc7e1 10960static bool
dda8d76d 10961arm_process_unwind (Filedata * filedata)
0b6ae522
DJ
10962{
10963 struct arm_unw_aux_info aux;
10964 Elf_Internal_Shdr *unwsec = NULL;
0b6ae522 10965 Elf_Internal_Shdr *sec;
26c527e6 10966 size_t i;
fa197c1c 10967 unsigned int sec_type;
015dc7e1 10968 bool res = true;
0b6ae522 10969
dda8d76d 10970 switch (filedata->file_header.e_machine)
fa197c1c
PB
10971 {
10972 case EM_ARM:
10973 sec_type = SHT_ARM_EXIDX;
10974 break;
10975
10976 case EM_TI_C6000:
10977 sec_type = SHT_C6000_UNWIND;
10978 break;
10979
0b4362b0 10980 default:
74e1a04b 10981 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
dda8d76d 10982 filedata->file_header.e_machine);
015dc7e1 10983 return false;
fa197c1c
PB
10984 }
10985
dda8d76d 10986 if (filedata->string_table == NULL)
015dc7e1 10987 return false;
1b31d05e
NC
10988
10989 memset (& aux, 0, sizeof (aux));
dda8d76d 10990 aux.filedata = filedata;
0b6ae522 10991
dda8d76d 10992 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
0b6ae522 10993 {
28d13567 10994 if (sec->sh_type == SHT_SYMTAB)
0b6ae522 10995 {
28d13567 10996 if (aux.symtab)
74e1a04b 10997 {
28d13567
AM
10998 error (_("Multiple symbol tables encountered\n"));
10999 free (aux.symtab);
11000 aux.symtab = NULL;
74e1a04b 11001 free (aux.strtab);
28d13567 11002 aux.strtab = NULL;
74e1a04b 11003 }
28d13567
AM
11004 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
11005 &aux.strtab, &aux.strtab_size))
015dc7e1 11006 return false;
0b6ae522 11007 }
fa197c1c 11008 else if (sec->sh_type == sec_type)
0b6ae522
DJ
11009 unwsec = sec;
11010 }
11011
1b31d05e 11012 if (unwsec == NULL)
0b6ae522 11013 printf (_("\nThere are no unwind sections in this file.\n"));
1b31d05e 11014 else
dda8d76d 11015 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
1b31d05e
NC
11016 {
11017 if (sec->sh_type == sec_type)
11018 {
26c527e6
AM
11019 uint64_t num_unwind = sec->sh_size / (2 * eh_addr_size);
11020 printf (ngettext ("\nUnwind section '%s' at offset %#" PRIx64 " "
11021 "contains %" PRIu64 " entry:\n",
11022 "\nUnwind section '%s' at offset %#" PRIx64 " "
11023 "contains %" PRIu64 " entries:\n",
d3a49aa8 11024 num_unwind),
dda8d76d 11025 printable_section_name (filedata, sec),
26c527e6 11026 sec->sh_offset,
d3a49aa8 11027 num_unwind);
0b6ae522 11028
dda8d76d 11029 if (! dump_arm_unwind (filedata, &aux, sec))
015dc7e1 11030 res = false;
1b31d05e
NC
11031 }
11032 }
0b6ae522 11033
9db70fc3
AM
11034 free (aux.symtab);
11035 free ((char *) aux.strtab);
32ec8896
NC
11036
11037 return res;
0b6ae522
DJ
11038}
11039
3ecc00ec
NC
11040static bool
11041no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
11042{
11043 printf (_("No processor specific unwind information to decode\n"));
11044 return true;
11045}
11046
015dc7e1 11047static bool
dda8d76d 11048process_unwind (Filedata * filedata)
57346661 11049{
2cf0635d
NC
11050 struct unwind_handler
11051 {
32ec8896 11052 unsigned int machtype;
015dc7e1 11053 bool (* handler)(Filedata *);
2cf0635d
NC
11054 } handlers[] =
11055 {
0b6ae522 11056 { EM_ARM, arm_process_unwind },
57346661
AM
11057 { EM_IA_64, ia64_process_unwind },
11058 { EM_PARISC, hppa_process_unwind },
fa197c1c 11059 { EM_TI_C6000, arm_process_unwind },
3ecc00ec
NC
11060 { EM_386, no_processor_specific_unwind },
11061 { EM_X86_64, no_processor_specific_unwind },
32ec8896 11062 { 0, NULL }
57346661
AM
11063 };
11064 int i;
11065
11066 if (!do_unwind)
015dc7e1 11067 return true;
57346661
AM
11068
11069 for (i = 0; handlers[i].handler != NULL; i++)
dda8d76d
NC
11070 if (filedata->file_header.e_machine == handlers[i].machtype)
11071 return handlers[i].handler (filedata);
57346661 11072
1b31d05e 11073 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
dda8d76d 11074 get_machine_name (filedata->file_header.e_machine));
015dc7e1 11075 return true;
57346661
AM
11076}
11077
37c18eed
SD
11078static void
11079dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
11080{
11081 switch (entry->d_tag)
11082 {
11083 case DT_AARCH64_BTI_PLT:
1dbade74 11084 case DT_AARCH64_PAC_PLT:
37c18eed
SD
11085 break;
11086 default:
11087 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11088 break;
11089 }
11090 putchar ('\n');
11091}
11092
252b5132 11093static void
978c4450 11094dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
252b5132
RH
11095{
11096 switch (entry->d_tag)
11097 {
11098 case DT_MIPS_FLAGS:
11099 if (entry->d_un.d_val == 0)
4b68bca3 11100 printf (_("NONE"));
252b5132
RH
11101 else
11102 {
11103 static const char * opts[] =
11104 {
11105 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
11106 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
11107 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
11108 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
11109 "RLD_ORDER_SAFE"
11110 };
11111 unsigned int cnt;
015dc7e1 11112 bool first = true;
2b692964 11113
60bca95a 11114 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
252b5132
RH
11115 if (entry->d_un.d_val & (1 << cnt))
11116 {
11117 printf ("%s%s", first ? "" : " ", opts[cnt]);
015dc7e1 11118 first = false;
252b5132 11119 }
252b5132
RH
11120 }
11121 break;
103f02d3 11122
252b5132 11123 case DT_MIPS_IVERSION:
84714f86 11124 if (valid_dynamic_name (filedata, entry->d_un.d_val))
978c4450 11125 printf (_("Interface Version: %s"),
84714f86 11126 get_dynamic_name (filedata, entry->d_un.d_val));
252b5132 11127 else
f493c217 11128 printf (_("Interface Version: <corrupt: %" PRIx64 ">"),
625d49fc 11129 entry->d_un.d_ptr);
252b5132 11130 break;
103f02d3 11131
252b5132
RH
11132 case DT_MIPS_TIME_STAMP:
11133 {
d5b07ef4 11134 char timebuf[128];
2cf0635d 11135 struct tm * tmp;
91d6fa6a 11136 time_t atime = entry->d_un.d_val;
82b1b41b 11137
91d6fa6a 11138 tmp = gmtime (&atime);
82b1b41b
NC
11139 /* PR 17531: file: 6accc532. */
11140 if (tmp == NULL)
11141 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
11142 else
11143 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
11144 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11145 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
4b68bca3 11146 printf (_("Time Stamp: %s"), timebuf);
252b5132
RH
11147 }
11148 break;
103f02d3 11149
252b5132
RH
11150 case DT_MIPS_RLD_VERSION:
11151 case DT_MIPS_LOCAL_GOTNO:
11152 case DT_MIPS_CONFLICTNO:
11153 case DT_MIPS_LIBLISTNO:
11154 case DT_MIPS_SYMTABNO:
11155 case DT_MIPS_UNREFEXTNO:
11156 case DT_MIPS_HIPAGENO:
11157 case DT_MIPS_DELTA_CLASS_NO:
11158 case DT_MIPS_DELTA_INSTANCE_NO:
11159 case DT_MIPS_DELTA_RELOC_NO:
11160 case DT_MIPS_DELTA_SYM_NO:
11161 case DT_MIPS_DELTA_CLASSSYM_NO:
11162 case DT_MIPS_COMPACT_SIZE:
c69075ac 11163 print_vma (entry->d_un.d_val, DEC);
252b5132 11164 break;
103f02d3 11165
f16a9783 11166 case DT_MIPS_XHASH:
978c4450
AM
11167 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
11168 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
f16a9783
MS
11169 /* Falls through. */
11170
103f02d3 11171 default:
4b68bca3 11172 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
103f02d3 11173 }
4b68bca3 11174 putchar ('\n');
103f02d3
UD
11175}
11176
103f02d3 11177static void
2cf0635d 11178dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
103f02d3
UD
11179{
11180 switch (entry->d_tag)
11181 {
11182 case DT_HP_DLD_FLAGS:
11183 {
11184 static struct
11185 {
26c527e6 11186 unsigned int bit;
2cf0635d 11187 const char * str;
5e220199
NC
11188 }
11189 flags[] =
11190 {
11191 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
11192 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
11193 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
11194 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
11195 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
11196 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
11197 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
11198 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
11199 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
11200 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
eec8f817
DA
11201 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
11202 { DT_HP_GST, "HP_GST" },
11203 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
11204 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
11205 { DT_HP_NODELETE, "HP_NODELETE" },
11206 { DT_HP_GROUP, "HP_GROUP" },
11207 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
5e220199 11208 };
015dc7e1 11209 bool first = true;
5e220199 11210 size_t cnt;
625d49fc 11211 uint64_t val = entry->d_un.d_val;
103f02d3 11212
60bca95a 11213 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
103f02d3 11214 if (val & flags[cnt].bit)
30800947
NC
11215 {
11216 if (! first)
11217 putchar (' ');
11218 fputs (flags[cnt].str, stdout);
015dc7e1 11219 first = false;
30800947
NC
11220 val ^= flags[cnt].bit;
11221 }
76da6bbe 11222
103f02d3 11223 if (val != 0 || first)
f7a99963
NC
11224 {
11225 if (! first)
11226 putchar (' ');
11227 print_vma (val, HEX);
11228 }
103f02d3
UD
11229 }
11230 break;
76da6bbe 11231
252b5132 11232 default:
f7a99963
NC
11233 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11234 break;
252b5132 11235 }
35b1837e 11236 putchar ('\n');
252b5132
RH
11237}
11238
28f997cf
TG
11239/* VMS vs Unix time offset and factor. */
11240
11241#define VMS_EPOCH_OFFSET 35067168000000000LL
11242#define VMS_GRANULARITY_FACTOR 10000000
dccc31de
AM
11243#ifndef INT64_MIN
11244#define INT64_MIN (-9223372036854775807LL - 1)
11245#endif
28f997cf
TG
11246
11247/* Display a VMS time in a human readable format. */
11248
11249static void
0e3c1eeb 11250print_vms_time (int64_t vmstime)
28f997cf 11251{
dccc31de 11252 struct tm *tm = NULL;
28f997cf
TG
11253 time_t unxtime;
11254
dccc31de
AM
11255 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
11256 {
11257 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
11258 unxtime = vmstime;
11259 if (unxtime == vmstime)
11260 tm = gmtime (&unxtime);
11261 }
11262 if (tm != NULL)
11263 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
11264 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
11265 tm->tm_hour, tm->tm_min, tm->tm_sec);
28f997cf 11266}
28f997cf 11267
ecc51f48 11268static void
2cf0635d 11269dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
ecc51f48
NC
11270{
11271 switch (entry->d_tag)
11272 {
0de14b54 11273 case DT_IA_64_PLT_RESERVE:
bdf4d63a 11274 /* First 3 slots reserved. */
ecc51f48
NC
11275 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11276 printf (" -- ");
11277 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
bdf4d63a
JJ
11278 break;
11279
28f997cf 11280 case DT_IA_64_VMS_LINKTIME:
28f997cf 11281 print_vms_time (entry->d_un.d_val);
28f997cf
TG
11282 break;
11283
11284 case DT_IA_64_VMS_LNKFLAGS:
11285 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11286 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
11287 printf (" CALL_DEBUG");
11288 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
11289 printf (" NOP0BUFS");
11290 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
11291 printf (" P0IMAGE");
11292 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
11293 printf (" MKTHREADS");
11294 if (entry->d_un.d_val & VMS_LF_UPCALLS)
11295 printf (" UPCALLS");
11296 if (entry->d_un.d_val & VMS_LF_IMGSTA)
11297 printf (" IMGSTA");
11298 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
11299 printf (" INITIALIZE");
11300 if (entry->d_un.d_val & VMS_LF_MAIN)
11301 printf (" MAIN");
11302 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
11303 printf (" EXE_INIT");
11304 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
11305 printf (" TBK_IN_IMG");
11306 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
11307 printf (" DBG_IN_IMG");
11308 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
11309 printf (" TBK_IN_DSF");
11310 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
11311 printf (" DBG_IN_DSF");
11312 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
11313 printf (" SIGNATURES");
11314 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
11315 printf (" REL_SEG_OFF");
11316 break;
11317
bdf4d63a
JJ
11318 default:
11319 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11320 break;
ecc51f48 11321 }
bdf4d63a 11322 putchar ('\n');
ecc51f48
NC
11323}
11324
015dc7e1 11325static bool
dda8d76d 11326get_32bit_dynamic_section (Filedata * filedata)
252b5132 11327{
2cf0635d
NC
11328 Elf32_External_Dyn * edyn;
11329 Elf32_External_Dyn * ext;
11330 Elf_Internal_Dyn * entry;
103f02d3 11331
978c4450
AM
11332 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
11333 filedata->dynamic_addr, 1,
11334 filedata->dynamic_size,
11335 _("dynamic section"));
a6e9f9df 11336 if (!edyn)
015dc7e1 11337 return false;
103f02d3 11338
071436c6
NC
11339 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
11340 might not have the luxury of section headers. Look for the DT_NULL
11341 terminator to determine the number of entries. */
978c4450
AM
11342 for (ext = edyn, filedata->dynamic_nent = 0;
11343 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
ba2685cc
AM
11344 ext++)
11345 {
978c4450 11346 filedata->dynamic_nent++;
ba2685cc
AM
11347 if (BYTE_GET (ext->d_tag) == DT_NULL)
11348 break;
11349 }
252b5132 11350
978c4450
AM
11351 filedata->dynamic_section
11352 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
11353 if (filedata->dynamic_section == NULL)
252b5132 11354 {
26c527e6
AM
11355 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
11356 filedata->dynamic_nent);
9ea033b2 11357 free (edyn);
015dc7e1 11358 return false;
9ea033b2 11359 }
252b5132 11360
978c4450
AM
11361 for (ext = edyn, entry = filedata->dynamic_section;
11362 entry < filedata->dynamic_section + filedata->dynamic_nent;
fb514b26 11363 ext++, entry++)
9ea033b2 11364 {
fb514b26
AM
11365 entry->d_tag = BYTE_GET (ext->d_tag);
11366 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
252b5132
RH
11367 }
11368
9ea033b2
NC
11369 free (edyn);
11370
015dc7e1 11371 return true;
9ea033b2
NC
11372}
11373
015dc7e1 11374static bool
dda8d76d 11375get_64bit_dynamic_section (Filedata * filedata)
9ea033b2 11376{
2cf0635d
NC
11377 Elf64_External_Dyn * edyn;
11378 Elf64_External_Dyn * ext;
11379 Elf_Internal_Dyn * entry;
103f02d3 11380
071436c6 11381 /* Read in the data. */
978c4450
AM
11382 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
11383 filedata->dynamic_addr, 1,
11384 filedata->dynamic_size,
11385 _("dynamic section"));
a6e9f9df 11386 if (!edyn)
015dc7e1 11387 return false;
103f02d3 11388
071436c6
NC
11389 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
11390 might not have the luxury of section headers. Look for the DT_NULL
11391 terminator to determine the number of entries. */
978c4450 11392 for (ext = edyn, filedata->dynamic_nent = 0;
53c3012c 11393 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
978c4450 11394 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
ba2685cc
AM
11395 ext++)
11396 {
978c4450 11397 filedata->dynamic_nent++;
66543521 11398 if (BYTE_GET (ext->d_tag) == DT_NULL)
ba2685cc
AM
11399 break;
11400 }
252b5132 11401
978c4450
AM
11402 filedata->dynamic_section
11403 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
11404 if (filedata->dynamic_section == NULL)
252b5132 11405 {
26c527e6
AM
11406 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
11407 filedata->dynamic_nent);
252b5132 11408 free (edyn);
015dc7e1 11409 return false;
252b5132
RH
11410 }
11411
071436c6 11412 /* Convert from external to internal formats. */
978c4450
AM
11413 for (ext = edyn, entry = filedata->dynamic_section;
11414 entry < filedata->dynamic_section + filedata->dynamic_nent;
fb514b26 11415 ext++, entry++)
252b5132 11416 {
66543521
AM
11417 entry->d_tag = BYTE_GET (ext->d_tag);
11418 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
252b5132
RH
11419 }
11420
11421 free (edyn);
11422
015dc7e1 11423 return true;
9ea033b2
NC
11424}
11425
4de91c10
AM
11426static bool
11427get_dynamic_section (Filedata *filedata)
11428{
11429 if (filedata->dynamic_section)
11430 return true;
11431
11432 if (is_32bit_elf)
11433 return get_32bit_dynamic_section (filedata);
11434 else
11435 return get_64bit_dynamic_section (filedata);
11436}
11437
e9e44622 11438static void
625d49fc 11439print_dynamic_flags (uint64_t flags)
d1133906 11440{
015dc7e1 11441 bool first = true;
13ae64f3 11442
d1133906
NC
11443 while (flags)
11444 {
625d49fc 11445 uint64_t flag;
d1133906
NC
11446
11447 flag = flags & - flags;
11448 flags &= ~ flag;
11449
e9e44622 11450 if (first)
015dc7e1 11451 first = false;
e9e44622
JJ
11452 else
11453 putc (' ', stdout);
13ae64f3 11454
d1133906
NC
11455 switch (flag)
11456 {
e9e44622
JJ
11457 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
11458 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
11459 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
11460 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
11461 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
2b692964 11462 default: fputs (_("unknown"), stdout); break;
d1133906
NC
11463 }
11464 }
e9e44622 11465 puts ("");
d1133906
NC
11466}
11467
625d49fc 11468static uint64_t *
be7d229a 11469get_dynamic_data (Filedata * filedata, uint64_t number, unsigned int ent_size)
10ca4b04
L
11470{
11471 unsigned char * e_data;
625d49fc 11472 uint64_t * i_data;
10ca4b04 11473
be7d229a
AM
11474 /* If size_t is smaller than uint64_t, eg because you are building
11475 on a 32-bit host, then make sure that when number is cast to
11476 size_t no information is lost. */
11477 if ((size_t) number != number
11478 || ent_size * number / ent_size != number)
10ca4b04 11479 {
be7d229a 11480 error (_("Size overflow prevents reading %" PRIu64
b8281767 11481 " elements of size %u\n"),
be7d229a 11482 number, ent_size);
10ca4b04
L
11483 return NULL;
11484 }
11485
11486 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
11487 attempting to allocate memory when the read is bound to fail. */
11488 if (ent_size * number > filedata->file_size)
11489 {
b8281767 11490 error (_("Invalid number of dynamic entries: %" PRIu64 "\n"),
be7d229a 11491 number);
10ca4b04
L
11492 return NULL;
11493 }
11494
11495 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
11496 if (e_data == NULL)
11497 {
b8281767 11498 error (_("Out of memory reading %" PRIu64 " dynamic entries\n"),
be7d229a 11499 number);
10ca4b04
L
11500 return NULL;
11501 }
11502
11503 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
11504 {
b8281767 11505 error (_("Unable to read in %" PRIu64 " bytes of dynamic data\n"),
be7d229a 11506 number * ent_size);
10ca4b04
L
11507 free (e_data);
11508 return NULL;
11509 }
11510
625d49fc 11511 i_data = (uint64_t *) cmalloc ((size_t) number, sizeof (*i_data));
10ca4b04
L
11512 if (i_data == NULL)
11513 {
b8281767 11514 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
be7d229a 11515 number);
10ca4b04
L
11516 free (e_data);
11517 return NULL;
11518 }
11519
11520 while (number--)
11521 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
11522
11523 free (e_data);
11524
11525 return i_data;
11526}
11527
26c527e6 11528static uint64_t
10ca4b04
L
11529get_num_dynamic_syms (Filedata * filedata)
11530{
26c527e6 11531 uint64_t num_of_syms = 0;
10ca4b04
L
11532
11533 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
11534 return num_of_syms;
11535
978c4450 11536 if (filedata->dynamic_info[DT_HASH])
10ca4b04
L
11537 {
11538 unsigned char nb[8];
11539 unsigned char nc[8];
11540 unsigned int hash_ent_size = 4;
11541
11542 if ((filedata->file_header.e_machine == EM_ALPHA
11543 || filedata->file_header.e_machine == EM_S390
11544 || filedata->file_header.e_machine == EM_S390_OLD)
11545 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
11546 hash_ent_size = 8;
11547
63cf857e
AM
11548 if (fseek64 (filedata->handle,
11549 (filedata->archive_file_offset
11550 + offset_from_vma (filedata,
11551 filedata->dynamic_info[DT_HASH],
11552 sizeof nb + sizeof nc)),
11553 SEEK_SET))
10ca4b04
L
11554 {
11555 error (_("Unable to seek to start of dynamic information\n"));
11556 goto no_hash;
11557 }
11558
11559 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
11560 {
11561 error (_("Failed to read in number of buckets\n"));
11562 goto no_hash;
11563 }
11564
11565 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
11566 {
11567 error (_("Failed to read in number of chains\n"));
11568 goto no_hash;
11569 }
11570
978c4450
AM
11571 filedata->nbuckets = byte_get (nb, hash_ent_size);
11572 filedata->nchains = byte_get (nc, hash_ent_size);
10ca4b04 11573
2482f306
AM
11574 if (filedata->nbuckets != 0 && filedata->nchains != 0)
11575 {
11576 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
11577 hash_ent_size);
11578 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
11579 hash_ent_size);
001890e1 11580
2482f306
AM
11581 if (filedata->buckets != NULL && filedata->chains != NULL)
11582 num_of_syms = filedata->nchains;
11583 }
ceb9bf11 11584 no_hash:
10ca4b04
L
11585 if (num_of_syms == 0)
11586 {
9db70fc3
AM
11587 free (filedata->buckets);
11588 filedata->buckets = NULL;
11589 free (filedata->chains);
11590 filedata->chains = NULL;
978c4450 11591 filedata->nbuckets = 0;
10ca4b04
L
11592 }
11593 }
11594
978c4450 11595 if (filedata->dynamic_info_DT_GNU_HASH)
10ca4b04
L
11596 {
11597 unsigned char nb[16];
625d49fc
AM
11598 uint64_t i, maxchain = 0xffffffff, bitmaskwords;
11599 uint64_t buckets_vma;
26c527e6 11600 uint64_t hn;
10ca4b04 11601
63cf857e
AM
11602 if (fseek64 (filedata->handle,
11603 (filedata->archive_file_offset
11604 + offset_from_vma (filedata,
11605 filedata->dynamic_info_DT_GNU_HASH,
11606 sizeof nb)),
11607 SEEK_SET))
10ca4b04
L
11608 {
11609 error (_("Unable to seek to start of dynamic information\n"));
10ca4b04
L
11610 goto no_gnu_hash;
11611 }
11612
11613 if (fread (nb, 16, 1, filedata->handle) != 1)
11614 {
11615 error (_("Failed to read in number of buckets\n"));
10ca4b04
L
11616 goto no_gnu_hash;
11617 }
11618
978c4450
AM
11619 filedata->ngnubuckets = byte_get (nb, 4);
11620 filedata->gnusymidx = byte_get (nb + 4, 4);
10ca4b04 11621 bitmaskwords = byte_get (nb + 8, 4);
978c4450 11622 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
10ca4b04
L
11623 if (is_32bit_elf)
11624 buckets_vma += bitmaskwords * 4;
11625 else
11626 buckets_vma += bitmaskwords * 8;
11627
63cf857e
AM
11628 if (fseek64 (filedata->handle,
11629 (filedata->archive_file_offset
11630 + offset_from_vma (filedata, buckets_vma, 4)),
11631 SEEK_SET))
10ca4b04
L
11632 {
11633 error (_("Unable to seek to start of dynamic information\n"));
10ca4b04
L
11634 goto no_gnu_hash;
11635 }
11636
978c4450
AM
11637 filedata->gnubuckets
11638 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
10ca4b04 11639
978c4450 11640 if (filedata->gnubuckets == NULL)
90837ea7 11641 goto no_gnu_hash;
10ca4b04 11642
978c4450
AM
11643 for (i = 0; i < filedata->ngnubuckets; i++)
11644 if (filedata->gnubuckets[i] != 0)
10ca4b04 11645 {
978c4450 11646 if (filedata->gnubuckets[i] < filedata->gnusymidx)
90837ea7 11647 goto no_gnu_hash;
10ca4b04 11648
978c4450
AM
11649 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
11650 maxchain = filedata->gnubuckets[i];
10ca4b04
L
11651 }
11652
11653 if (maxchain == 0xffffffff)
90837ea7 11654 goto no_gnu_hash;
10ca4b04 11655
978c4450 11656 maxchain -= filedata->gnusymidx;
10ca4b04 11657
63cf857e
AM
11658 if (fseek64 (filedata->handle,
11659 (filedata->archive_file_offset
11660 + offset_from_vma (filedata,
11661 buckets_vma + 4 * (filedata->ngnubuckets
11662 + maxchain),
11663 4)),
11664 SEEK_SET))
10ca4b04
L
11665 {
11666 error (_("Unable to seek to start of dynamic information\n"));
10ca4b04
L
11667 goto no_gnu_hash;
11668 }
11669
11670 do
11671 {
11672 if (fread (nb, 4, 1, filedata->handle) != 1)
11673 {
11674 error (_("Failed to determine last chain length\n"));
10ca4b04
L
11675 goto no_gnu_hash;
11676 }
11677
11678 if (maxchain + 1 == 0)
90837ea7 11679 goto no_gnu_hash;
10ca4b04
L
11680
11681 ++maxchain;
11682 }
11683 while ((byte_get (nb, 4) & 1) == 0);
11684
63cf857e
AM
11685 if (fseek64 (filedata->handle,
11686 (filedata->archive_file_offset
11687 + offset_from_vma (filedata, (buckets_vma
11688 + 4 * filedata->ngnubuckets),
11689 4)),
11690 SEEK_SET))
10ca4b04
L
11691 {
11692 error (_("Unable to seek to start of dynamic information\n"));
10ca4b04
L
11693 goto no_gnu_hash;
11694 }
11695
978c4450
AM
11696 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
11697 filedata->ngnuchains = maxchain;
10ca4b04 11698
978c4450 11699 if (filedata->gnuchains == NULL)
90837ea7 11700 goto no_gnu_hash;
10ca4b04 11701
978c4450 11702 if (filedata->dynamic_info_DT_MIPS_XHASH)
10ca4b04 11703 {
63cf857e
AM
11704 if (fseek64 (filedata->handle,
11705 (filedata->archive_file_offset
11706 + offset_from_vma (filedata, (buckets_vma
11707 + 4 * (filedata->ngnubuckets
11708 + maxchain)), 4)),
11709 SEEK_SET))
10ca4b04
L
11710 {
11711 error (_("Unable to seek to start of dynamic information\n"));
10ca4b04
L
11712 goto no_gnu_hash;
11713 }
11714
978c4450 11715 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
90837ea7
AM
11716 if (filedata->mipsxlat == NULL)
11717 goto no_gnu_hash;
10ca4b04
L
11718 }
11719
978c4450
AM
11720 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
11721 if (filedata->gnubuckets[hn] != 0)
10ca4b04 11722 {
625d49fc
AM
11723 uint64_t si = filedata->gnubuckets[hn];
11724 uint64_t off = si - filedata->gnusymidx;
10ca4b04
L
11725
11726 do
11727 {
978c4450 11728 if (filedata->dynamic_info_DT_MIPS_XHASH)
10ca4b04 11729 {
c31ab5a0
AM
11730 if (off < filedata->ngnuchains
11731 && filedata->mipsxlat[off] >= num_of_syms)
978c4450 11732 num_of_syms = filedata->mipsxlat[off] + 1;
10ca4b04
L
11733 }
11734 else
11735 {
11736 if (si >= num_of_syms)
11737 num_of_syms = si + 1;
11738 }
11739 si++;
11740 }
978c4450
AM
11741 while (off < filedata->ngnuchains
11742 && (filedata->gnuchains[off++] & 1) == 0);
10ca4b04
L
11743 }
11744
90837ea7 11745 if (num_of_syms == 0)
10ca4b04 11746 {
90837ea7 11747 no_gnu_hash:
9db70fc3
AM
11748 free (filedata->mipsxlat);
11749 filedata->mipsxlat = NULL;
11750 free (filedata->gnuchains);
11751 filedata->gnuchains = NULL;
11752 free (filedata->gnubuckets);
11753 filedata->gnubuckets = NULL;
978c4450
AM
11754 filedata->ngnubuckets = 0;
11755 filedata->ngnuchains = 0;
10ca4b04
L
11756 }
11757 }
11758
11759 return num_of_syms;
11760}
11761
b2d38a17
NC
11762/* Parse and display the contents of the dynamic section. */
11763
015dc7e1 11764static bool
dda8d76d 11765process_dynamic_section (Filedata * filedata)
9ea033b2 11766{
2cf0635d 11767 Elf_Internal_Dyn * entry;
9ea033b2 11768
93df3340 11769 if (filedata->dynamic_size <= 1)
9ea033b2
NC
11770 {
11771 if (do_dynamic)
ca0e11aa
NC
11772 {
11773 if (filedata->is_separate)
11774 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
11775 filedata->file_name);
11776 else
11777 printf (_("\nThere is no dynamic section in this file.\n"));
11778 }
9ea033b2 11779
015dc7e1 11780 return true;
9ea033b2
NC
11781 }
11782
4de91c10
AM
11783 if (!get_dynamic_section (filedata))
11784 return false;
9ea033b2 11785
252b5132 11786 /* Find the appropriate symbol table. */
978c4450 11787 if (filedata->dynamic_symbols == NULL || do_histogram)
252b5132 11788 {
26c527e6 11789 uint64_t num_of_syms;
2482f306 11790
978c4450
AM
11791 for (entry = filedata->dynamic_section;
11792 entry < filedata->dynamic_section + filedata->dynamic_nent;
86dba8ee 11793 ++entry)
10ca4b04 11794 if (entry->d_tag == DT_SYMTAB)
978c4450 11795 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
10ca4b04 11796 else if (entry->d_tag == DT_SYMENT)
978c4450 11797 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
10ca4b04 11798 else if (entry->d_tag == DT_HASH)
978c4450 11799 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
10ca4b04 11800 else if (entry->d_tag == DT_GNU_HASH)
978c4450 11801 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10ca4b04
L
11802 else if ((filedata->file_header.e_machine == EM_MIPS
11803 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
11804 && entry->d_tag == DT_MIPS_XHASH)
11805 {
978c4450
AM
11806 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
11807 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10ca4b04 11808 }
252b5132 11809
2482f306
AM
11810 num_of_syms = get_num_dynamic_syms (filedata);
11811
11812 if (num_of_syms != 0
11813 && filedata->dynamic_symbols == NULL
11814 && filedata->dynamic_info[DT_SYMTAB]
978c4450 11815 && filedata->dynamic_info[DT_SYMENT])
10ca4b04
L
11816 {
11817 Elf_Internal_Phdr *seg;
625d49fc 11818 uint64_t vma = filedata->dynamic_info[DT_SYMTAB];
252b5132 11819
2482f306
AM
11820 if (! get_program_headers (filedata))
11821 {
11822 error (_("Cannot interpret virtual addresses "
11823 "without program headers.\n"));
015dc7e1 11824 return false;
2482f306 11825 }
252b5132 11826
2482f306
AM
11827 for (seg = filedata->program_headers;
11828 seg < filedata->program_headers + filedata->file_header.e_phnum;
11829 ++seg)
11830 {
11831 if (seg->p_type != PT_LOAD)
11832 continue;
252b5132 11833
2482f306
AM
11834 if (seg->p_offset + seg->p_filesz > filedata->file_size)
11835 {
11836 /* See PR 21379 for a reproducer. */
11837 error (_("Invalid PT_LOAD entry\n"));
015dc7e1 11838 return false;
2482f306 11839 }
252b5132 11840
2482f306
AM
11841 if (vma >= (seg->p_vaddr & -seg->p_align)
11842 && vma < seg->p_vaddr + seg->p_filesz)
11843 {
11844 /* Since we do not know how big the symbol table is,
11845 we default to reading in up to the end of PT_LOAD
11846 segment and processing that. This is overkill, I
11847 know, but it should work. */
11848 Elf_Internal_Shdr section;
11849 section.sh_offset = (vma - seg->p_vaddr
11850 + seg->p_offset);
11851 section.sh_size = (num_of_syms
11852 * filedata->dynamic_info[DT_SYMENT]);
11853 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
8ac10c5b
L
11854
11855 if (do_checks
11856 && filedata->dynamic_symtab_section != NULL
11857 && ((filedata->dynamic_symtab_section->sh_offset
11858 != section.sh_offset)
11859 || (filedata->dynamic_symtab_section->sh_size
11860 != section.sh_size)
11861 || (filedata->dynamic_symtab_section->sh_entsize
11862 != section.sh_entsize)))
11863 warn (_("\
11864the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
11865
2482f306
AM
11866 section.sh_name = filedata->string_table_length;
11867 filedata->dynamic_symbols
4de91c10 11868 = get_elf_symbols (filedata, &section,
2482f306
AM
11869 &filedata->num_dynamic_syms);
11870 if (filedata->dynamic_symbols == NULL
11871 || filedata->num_dynamic_syms != num_of_syms)
11872 {
11873 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
015dc7e1 11874 return false;
2482f306
AM
11875 }
11876 break;
11877 }
11878 }
11879 }
11880 }
252b5132
RH
11881
11882 /* Similarly find a string table. */
978c4450
AM
11883 if (filedata->dynamic_strings == NULL)
11884 for (entry = filedata->dynamic_section;
11885 entry < filedata->dynamic_section + filedata->dynamic_nent;
10ca4b04
L
11886 ++entry)
11887 {
11888 if (entry->d_tag == DT_STRTAB)
978c4450 11889 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
252b5132 11890
10ca4b04 11891 if (entry->d_tag == DT_STRSZ)
978c4450 11892 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
252b5132 11893
978c4450
AM
11894 if (filedata->dynamic_info[DT_STRTAB]
11895 && filedata->dynamic_info[DT_STRSZ])
10ca4b04 11896 {
26c527e6 11897 uint64_t offset;
be7d229a 11898 uint64_t str_tab_len = filedata->dynamic_info[DT_STRSZ];
10ca4b04
L
11899
11900 offset = offset_from_vma (filedata,
978c4450 11901 filedata->dynamic_info[DT_STRTAB],
10ca4b04 11902 str_tab_len);
8ac10c5b
L
11903 if (do_checks
11904 && filedata->dynamic_strtab_section
11905 && ((filedata->dynamic_strtab_section->sh_offset
11906 != (file_ptr) offset)
11907 || (filedata->dynamic_strtab_section->sh_size
11908 != str_tab_len)))
11909 warn (_("\
11910the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
11911
978c4450
AM
11912 filedata->dynamic_strings
11913 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
11914 _("dynamic string table"));
11915 if (filedata->dynamic_strings == NULL)
10ca4b04
L
11916 {
11917 error (_("Corrupt DT_STRTAB dynamic entry\n"));
11918 break;
11919 }
e3d39609 11920
978c4450 11921 filedata->dynamic_strings_length = str_tab_len;
10ca4b04
L
11922 break;
11923 }
11924 }
252b5132
RH
11925
11926 /* And find the syminfo section if available. */
978c4450 11927 if (filedata->dynamic_syminfo == NULL)
252b5132 11928 {
26c527e6 11929 uint64_t syminsz = 0;
252b5132 11930
978c4450
AM
11931 for (entry = filedata->dynamic_section;
11932 entry < filedata->dynamic_section + filedata->dynamic_nent;
86dba8ee 11933 ++entry)
252b5132
RH
11934 {
11935 if (entry->d_tag == DT_SYMINENT)
11936 {
11937 /* Note: these braces are necessary to avoid a syntax
11938 error from the SunOS4 C compiler. */
049b0c3a
NC
11939 /* PR binutils/17531: A corrupt file can trigger this test.
11940 So do not use an assert, instead generate an error message. */
11941 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
071436c6 11942 error (_("Bad value (%d) for SYMINENT entry\n"),
049b0c3a 11943 (int) entry->d_un.d_val);
252b5132
RH
11944 }
11945 else if (entry->d_tag == DT_SYMINSZ)
11946 syminsz = entry->d_un.d_val;
11947 else if (entry->d_tag == DT_SYMINFO)
978c4450
AM
11948 filedata->dynamic_syminfo_offset
11949 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
252b5132
RH
11950 }
11951
978c4450 11952 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
252b5132 11953 {
2cf0635d
NC
11954 Elf_External_Syminfo * extsyminfo;
11955 Elf_External_Syminfo * extsym;
11956 Elf_Internal_Syminfo * syminfo;
252b5132
RH
11957
11958 /* There is a syminfo section. Read the data. */
3f5e193b 11959 extsyminfo = (Elf_External_Syminfo *)
978c4450
AM
11960 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
11961 1, syminsz, _("symbol information"));
a6e9f9df 11962 if (!extsyminfo)
015dc7e1 11963 return false;
252b5132 11964
978c4450 11965 if (filedata->dynamic_syminfo != NULL)
e3d39609
NC
11966 {
11967 error (_("Multiple dynamic symbol information sections found\n"));
978c4450 11968 free (filedata->dynamic_syminfo);
e3d39609 11969 }
978c4450
AM
11970 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
11971 if (filedata->dynamic_syminfo == NULL)
252b5132 11972 {
26c527e6
AM
11973 error (_("Out of memory allocating %" PRIu64
11974 " bytes for dynamic symbol info\n"),
11975 syminsz);
015dc7e1 11976 return false;
252b5132
RH
11977 }
11978
2482f306
AM
11979 filedata->dynamic_syminfo_nent
11980 = syminsz / sizeof (Elf_External_Syminfo);
978c4450 11981 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
2482f306
AM
11982 syminfo < (filedata->dynamic_syminfo
11983 + filedata->dynamic_syminfo_nent);
86dba8ee 11984 ++syminfo, ++extsym)
252b5132 11985 {
86dba8ee
AM
11986 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
11987 syminfo->si_flags = BYTE_GET (extsym->si_flags);
252b5132
RH
11988 }
11989
11990 free (extsyminfo);
11991 }
11992 }
11993
978c4450 11994 if (do_dynamic && filedata->dynamic_addr)
ca0e11aa 11995 {
f253158f 11996 if (filedata->is_separate)
26c527e6
AM
11997 printf (ngettext ("\nIn linked file '%s' the dynamic section at offset %#" PRIx64 " contains %" PRIu64 " entry:\n",
11998 "\nIn linked file '%s' the dynamic section at offset %#" PRIx64 " contains %" PRIu64 " entries:\n",
11999 filedata->dynamic_nent),
f253158f
NC
12000 filedata->file_name,
12001 filedata->dynamic_addr,
26c527e6 12002 filedata->dynamic_nent);
84a9f195 12003 else
02da71ee 12004 printf (ngettext ("\nDynamic section at offset %#" PRIx64 " contains %" PRIu64 " entry:\n",
26c527e6
AM
12005 "\nDynamic section at offset %#" PRIx64 " contains %" PRIu64 " entries:\n",
12006 filedata->dynamic_nent),
84a9f195 12007 filedata->dynamic_addr,
26c527e6 12008 filedata->dynamic_nent);
ca0e11aa 12009 }
252b5132
RH
12010 if (do_dynamic)
12011 printf (_(" Tag Type Name/Value\n"));
12012
978c4450
AM
12013 for (entry = filedata->dynamic_section;
12014 entry < filedata->dynamic_section + filedata->dynamic_nent;
86dba8ee 12015 entry++)
252b5132
RH
12016 {
12017 if (do_dynamic)
f7a99963 12018 {
2cf0635d 12019 const char * dtype;
e699b9ff 12020
f7a99963
NC
12021 putchar (' ');
12022 print_vma (entry->d_tag, FULL_HEX);
dda8d76d 12023 dtype = get_dynamic_type (filedata, entry->d_tag);
e699b9ff 12024 printf (" (%s)%*s", dtype,
32ec8896 12025 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
f7a99963 12026 }
252b5132
RH
12027
12028 switch (entry->d_tag)
12029 {
d1133906
NC
12030 case DT_FLAGS:
12031 if (do_dynamic)
e9e44622 12032 print_dynamic_flags (entry->d_un.d_val);
d1133906 12033 break;
76da6bbe 12034
252b5132
RH
12035 case DT_AUXILIARY:
12036 case DT_FILTER:
019148e4
L
12037 case DT_CONFIG:
12038 case DT_DEPAUDIT:
12039 case DT_AUDIT:
252b5132
RH
12040 if (do_dynamic)
12041 {
019148e4 12042 switch (entry->d_tag)
b34976b6 12043 {
019148e4
L
12044 case DT_AUXILIARY:
12045 printf (_("Auxiliary library"));
12046 break;
12047
12048 case DT_FILTER:
12049 printf (_("Filter library"));
12050 break;
12051
b34976b6 12052 case DT_CONFIG:
019148e4
L
12053 printf (_("Configuration file"));
12054 break;
12055
12056 case DT_DEPAUDIT:
12057 printf (_("Dependency audit library"));
12058 break;
12059
12060 case DT_AUDIT:
12061 printf (_("Audit library"));
12062 break;
12063 }
252b5132 12064
84714f86 12065 if (valid_dynamic_name (filedata, entry->d_un.d_val))
978c4450 12066 printf (": [%s]\n",
84714f86 12067 get_dynamic_name (filedata, entry->d_un.d_val));
252b5132 12068 else
f7a99963
NC
12069 {
12070 printf (": ");
12071 print_vma (entry->d_un.d_val, PREFIX_HEX);
12072 putchar ('\n');
12073 }
252b5132
RH
12074 }
12075 break;
12076
dcefbbbd 12077 case DT_FEATURE:
252b5132
RH
12078 if (do_dynamic)
12079 {
12080 printf (_("Flags:"));
86f55779 12081
252b5132
RH
12082 if (entry->d_un.d_val == 0)
12083 printf (_(" None\n"));
12084 else
12085 {
26c527e6 12086 uint64_t val = entry->d_un.d_val;
86f55779 12087
252b5132
RH
12088 if (val & DTF_1_PARINIT)
12089 {
12090 printf (" PARINIT");
12091 val ^= DTF_1_PARINIT;
12092 }
dcefbbbd
L
12093 if (val & DTF_1_CONFEXP)
12094 {
12095 printf (" CONFEXP");
12096 val ^= DTF_1_CONFEXP;
12097 }
252b5132 12098 if (val != 0)
26c527e6 12099 printf (" %" PRIx64, val);
252b5132
RH
12100 puts ("");
12101 }
12102 }
12103 break;
12104
12105 case DT_POSFLAG_1:
12106 if (do_dynamic)
12107 {
12108 printf (_("Flags:"));
86f55779 12109
252b5132
RH
12110 if (entry->d_un.d_val == 0)
12111 printf (_(" None\n"));
12112 else
12113 {
26c527e6 12114 uint64_t val = entry->d_un.d_val;
86f55779 12115
252b5132
RH
12116 if (val & DF_P1_LAZYLOAD)
12117 {
12118 printf (" LAZYLOAD");
12119 val ^= DF_P1_LAZYLOAD;
12120 }
12121 if (val & DF_P1_GROUPPERM)
12122 {
12123 printf (" GROUPPERM");
12124 val ^= DF_P1_GROUPPERM;
12125 }
12126 if (val != 0)
26c527e6 12127 printf (" %" PRIx64, val);
252b5132
RH
12128 puts ("");
12129 }
12130 }
12131 break;
12132
12133 case DT_FLAGS_1:
12134 if (do_dynamic)
12135 {
12136 printf (_("Flags:"));
12137 if (entry->d_un.d_val == 0)
12138 printf (_(" None\n"));
12139 else
12140 {
26c527e6 12141 uint64_t val = entry->d_un.d_val;
86f55779 12142
252b5132
RH
12143 if (val & DF_1_NOW)
12144 {
12145 printf (" NOW");
12146 val ^= DF_1_NOW;
12147 }
12148 if (val & DF_1_GLOBAL)
12149 {
12150 printf (" GLOBAL");
12151 val ^= DF_1_GLOBAL;
12152 }
12153 if (val & DF_1_GROUP)
12154 {
12155 printf (" GROUP");
12156 val ^= DF_1_GROUP;
12157 }
12158 if (val & DF_1_NODELETE)
12159 {
12160 printf (" NODELETE");
12161 val ^= DF_1_NODELETE;
12162 }
12163 if (val & DF_1_LOADFLTR)
12164 {
12165 printf (" LOADFLTR");
12166 val ^= DF_1_LOADFLTR;
12167 }
12168 if (val & DF_1_INITFIRST)
12169 {
12170 printf (" INITFIRST");
12171 val ^= DF_1_INITFIRST;
12172 }
12173 if (val & DF_1_NOOPEN)
12174 {
12175 printf (" NOOPEN");
12176 val ^= DF_1_NOOPEN;
12177 }
12178 if (val & DF_1_ORIGIN)
12179 {
12180 printf (" ORIGIN");
12181 val ^= DF_1_ORIGIN;
12182 }
12183 if (val & DF_1_DIRECT)
12184 {
12185 printf (" DIRECT");
12186 val ^= DF_1_DIRECT;
12187 }
12188 if (val & DF_1_TRANS)
12189 {
12190 printf (" TRANS");
12191 val ^= DF_1_TRANS;
12192 }
12193 if (val & DF_1_INTERPOSE)
12194 {
12195 printf (" INTERPOSE");
12196 val ^= DF_1_INTERPOSE;
12197 }
f7db6139 12198 if (val & DF_1_NODEFLIB)
dcefbbbd 12199 {
f7db6139
L
12200 printf (" NODEFLIB");
12201 val ^= DF_1_NODEFLIB;
dcefbbbd
L
12202 }
12203 if (val & DF_1_NODUMP)
12204 {
12205 printf (" NODUMP");
12206 val ^= DF_1_NODUMP;
12207 }
34b60028 12208 if (val & DF_1_CONFALT)
dcefbbbd 12209 {
34b60028
L
12210 printf (" CONFALT");
12211 val ^= DF_1_CONFALT;
12212 }
12213 if (val & DF_1_ENDFILTEE)
12214 {
12215 printf (" ENDFILTEE");
12216 val ^= DF_1_ENDFILTEE;
12217 }
12218 if (val & DF_1_DISPRELDNE)
12219 {
12220 printf (" DISPRELDNE");
12221 val ^= DF_1_DISPRELDNE;
12222 }
12223 if (val & DF_1_DISPRELPND)
12224 {
12225 printf (" DISPRELPND");
12226 val ^= DF_1_DISPRELPND;
12227 }
12228 if (val & DF_1_NODIRECT)
12229 {
12230 printf (" NODIRECT");
12231 val ^= DF_1_NODIRECT;
12232 }
12233 if (val & DF_1_IGNMULDEF)
12234 {
12235 printf (" IGNMULDEF");
12236 val ^= DF_1_IGNMULDEF;
12237 }
12238 if (val & DF_1_NOKSYMS)
12239 {
12240 printf (" NOKSYMS");
12241 val ^= DF_1_NOKSYMS;
12242 }
12243 if (val & DF_1_NOHDR)
12244 {
12245 printf (" NOHDR");
12246 val ^= DF_1_NOHDR;
12247 }
12248 if (val & DF_1_EDITED)
12249 {
12250 printf (" EDITED");
12251 val ^= DF_1_EDITED;
12252 }
12253 if (val & DF_1_NORELOC)
12254 {
12255 printf (" NORELOC");
12256 val ^= DF_1_NORELOC;
12257 }
12258 if (val & DF_1_SYMINTPOSE)
12259 {
12260 printf (" SYMINTPOSE");
12261 val ^= DF_1_SYMINTPOSE;
12262 }
12263 if (val & DF_1_GLOBAUDIT)
12264 {
12265 printf (" GLOBAUDIT");
12266 val ^= DF_1_GLOBAUDIT;
12267 }
12268 if (val & DF_1_SINGLETON)
12269 {
12270 printf (" SINGLETON");
12271 val ^= DF_1_SINGLETON;
dcefbbbd 12272 }
5c383f02
RO
12273 if (val & DF_1_STUB)
12274 {
12275 printf (" STUB");
12276 val ^= DF_1_STUB;
12277 }
12278 if (val & DF_1_PIE)
12279 {
12280 printf (" PIE");
12281 val ^= DF_1_PIE;
12282 }
b1202ffa
L
12283 if (val & DF_1_KMOD)
12284 {
12285 printf (" KMOD");
12286 val ^= DF_1_KMOD;
12287 }
12288 if (val & DF_1_WEAKFILTER)
12289 {
12290 printf (" WEAKFILTER");
12291 val ^= DF_1_WEAKFILTER;
12292 }
12293 if (val & DF_1_NOCOMMON)
12294 {
12295 printf (" NOCOMMON");
12296 val ^= DF_1_NOCOMMON;
12297 }
252b5132 12298 if (val != 0)
26c527e6 12299 printf (" %" PRIx64, val);
252b5132
RH
12300 puts ("");
12301 }
12302 }
12303 break;
12304
12305 case DT_PLTREL:
978c4450 12306 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
252b5132 12307 if (do_dynamic)
dda8d76d 12308 puts (get_dynamic_type (filedata, entry->d_un.d_val));
252b5132
RH
12309 break;
12310
12311 case DT_NULL :
12312 case DT_NEEDED :
12313 case DT_PLTGOT :
12314 case DT_HASH :
12315 case DT_STRTAB :
12316 case DT_SYMTAB :
12317 case DT_RELA :
12318 case DT_INIT :
12319 case DT_FINI :
12320 case DT_SONAME :
12321 case DT_RPATH :
12322 case DT_SYMBOLIC:
12323 case DT_REL :
a7fd1186 12324 case DT_RELR :
252b5132
RH
12325 case DT_DEBUG :
12326 case DT_TEXTREL :
12327 case DT_JMPREL :
019148e4 12328 case DT_RUNPATH :
978c4450 12329 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
252b5132
RH
12330
12331 if (do_dynamic)
12332 {
84714f86 12333 const char *name;
252b5132 12334
84714f86
AM
12335 if (valid_dynamic_name (filedata, entry->d_un.d_val))
12336 name = get_dynamic_name (filedata, entry->d_un.d_val);
252b5132 12337 else
d79b3d50 12338 name = NULL;
252b5132
RH
12339
12340 if (name)
12341 {
12342 switch (entry->d_tag)
12343 {
12344 case DT_NEEDED:
12345 printf (_("Shared library: [%s]"), name);
12346
13acb58d
AM
12347 if (filedata->program_interpreter
12348 && streq (name, filedata->program_interpreter))
f7a99963 12349 printf (_(" program interpreter"));
252b5132
RH
12350 break;
12351
12352 case DT_SONAME:
f7a99963 12353 printf (_("Library soname: [%s]"), name);
252b5132
RH
12354 break;
12355
12356 case DT_RPATH:
f7a99963 12357 printf (_("Library rpath: [%s]"), name);
252b5132
RH
12358 break;
12359
019148e4
L
12360 case DT_RUNPATH:
12361 printf (_("Library runpath: [%s]"), name);
12362 break;
12363
252b5132 12364 default:
f7a99963
NC
12365 print_vma (entry->d_un.d_val, PREFIX_HEX);
12366 break;
252b5132
RH
12367 }
12368 }
12369 else
f7a99963
NC
12370 print_vma (entry->d_un.d_val, PREFIX_HEX);
12371
12372 putchar ('\n');
252b5132
RH
12373 }
12374 break;
12375
12376 case DT_PLTRELSZ:
12377 case DT_RELASZ :
12378 case DT_STRSZ :
12379 case DT_RELSZ :
12380 case DT_RELAENT :
a7fd1186
FS
12381 case DT_RELRENT :
12382 case DT_RELRSZ :
252b5132
RH
12383 case DT_SYMENT :
12384 case DT_RELENT :
978c4450 12385 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
1a0670f3 12386 /* Fall through. */
252b5132
RH
12387 case DT_PLTPADSZ:
12388 case DT_MOVEENT :
12389 case DT_MOVESZ :
04d8355a 12390 case DT_PREINIT_ARRAYSZ:
252b5132
RH
12391 case DT_INIT_ARRAYSZ:
12392 case DT_FINI_ARRAYSZ:
047b2264
JJ
12393 case DT_GNU_CONFLICTSZ:
12394 case DT_GNU_LIBLISTSZ:
252b5132 12395 if (do_dynamic)
f7a99963
NC
12396 {
12397 print_vma (entry->d_un.d_val, UNSIGNED);
2b692964 12398 printf (_(" (bytes)\n"));
f7a99963 12399 }
252b5132
RH
12400 break;
12401
12402 case DT_VERDEFNUM:
12403 case DT_VERNEEDNUM:
12404 case DT_RELACOUNT:
12405 case DT_RELCOUNT:
12406 if (do_dynamic)
f7a99963
NC
12407 {
12408 print_vma (entry->d_un.d_val, UNSIGNED);
12409 putchar ('\n');
12410 }
252b5132
RH
12411 break;
12412
12413 case DT_SYMINSZ:
12414 case DT_SYMINENT:
12415 case DT_SYMINFO:
12416 case DT_USED:
12417 case DT_INIT_ARRAY:
12418 case DT_FINI_ARRAY:
12419 if (do_dynamic)
12420 {
d79b3d50 12421 if (entry->d_tag == DT_USED
84714f86 12422 && valid_dynamic_name (filedata, entry->d_un.d_val))
252b5132 12423 {
84714f86
AM
12424 const char *name
12425 = get_dynamic_name (filedata, entry->d_un.d_val);
252b5132 12426
b34976b6 12427 if (*name)
252b5132
RH
12428 {
12429 printf (_("Not needed object: [%s]\n"), name);
12430 break;
12431 }
12432 }
103f02d3 12433
f7a99963
NC
12434 print_vma (entry->d_un.d_val, PREFIX_HEX);
12435 putchar ('\n');
252b5132
RH
12436 }
12437 break;
12438
12439 case DT_BIND_NOW:
12440 /* The value of this entry is ignored. */
35b1837e
AM
12441 if (do_dynamic)
12442 putchar ('\n');
252b5132 12443 break;
103f02d3 12444
047b2264
JJ
12445 case DT_GNU_PRELINKED:
12446 if (do_dynamic)
12447 {
2cf0635d 12448 struct tm * tmp;
91d6fa6a 12449 time_t atime = entry->d_un.d_val;
047b2264 12450
91d6fa6a 12451 tmp = gmtime (&atime);
071436c6
NC
12452 /* PR 17533 file: 041-1244816-0.004. */
12453 if (tmp == NULL)
26c527e6
AM
12454 printf (_("<corrupt time val: %" PRIx64),
12455 (uint64_t) atime);
071436c6
NC
12456 else
12457 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
12458 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
12459 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
047b2264
JJ
12460
12461 }
12462 break;
12463
fdc90cb4 12464 case DT_GNU_HASH:
978c4450 12465 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
fdc90cb4
JJ
12466 if (do_dynamic)
12467 {
12468 print_vma (entry->d_un.d_val, PREFIX_HEX);
12469 putchar ('\n');
12470 }
12471 break;
12472
a5da3dee
VDM
12473 case DT_GNU_FLAGS_1:
12474 if (do_dynamic)
12475 {
12476 printf (_("Flags:"));
12477 if (entry->d_un.d_val == 0)
12478 printf (_(" None\n"));
12479 else
12480 {
26c527e6 12481 uint64_t val = entry->d_un.d_val;
a5da3dee
VDM
12482
12483 if (val & DF_GNU_1_UNIQUE)
12484 {
12485 printf (" UNIQUE");
12486 val ^= DF_GNU_1_UNIQUE;
12487 }
12488 if (val != 0)
26c527e6 12489 printf (" %" PRIx64, val);
a5da3dee
VDM
12490 puts ("");
12491 }
12492 }
12493 break;
12494
252b5132
RH
12495 default:
12496 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
978c4450
AM
12497 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
12498 = entry->d_un.d_val;
252b5132
RH
12499
12500 if (do_dynamic)
12501 {
dda8d76d 12502 switch (filedata->file_header.e_machine)
252b5132 12503 {
37c18eed
SD
12504 case EM_AARCH64:
12505 dynamic_section_aarch64_val (entry);
12506 break;
252b5132 12507 case EM_MIPS:
4fe85591 12508 case EM_MIPS_RS3_LE:
978c4450 12509 dynamic_section_mips_val (filedata, entry);
252b5132 12510 break;
103f02d3 12511 case EM_PARISC:
b2d38a17 12512 dynamic_section_parisc_val (entry);
103f02d3 12513 break;
ecc51f48 12514 case EM_IA_64:
b2d38a17 12515 dynamic_section_ia64_val (entry);
ecc51f48 12516 break;
252b5132 12517 default:
f7a99963
NC
12518 print_vma (entry->d_un.d_val, PREFIX_HEX);
12519 putchar ('\n');
252b5132
RH
12520 }
12521 }
12522 break;
12523 }
12524 }
12525
015dc7e1 12526 return true;
252b5132
RH
12527}
12528
12529static char *
d3ba0551 12530get_ver_flags (unsigned int flags)
252b5132 12531{
6d4f21f6 12532 static char buff[128];
252b5132
RH
12533
12534 buff[0] = 0;
12535
12536 if (flags == 0)
12537 return _("none");
12538
12539 if (flags & VER_FLG_BASE)
7bb1ad17 12540 strcat (buff, "BASE");
252b5132
RH
12541
12542 if (flags & VER_FLG_WEAK)
12543 {
12544 if (flags & VER_FLG_BASE)
7bb1ad17 12545 strcat (buff, " | ");
252b5132 12546
7bb1ad17 12547 strcat (buff, "WEAK");
252b5132
RH
12548 }
12549
44ec90b9
RO
12550 if (flags & VER_FLG_INFO)
12551 {
12552 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
7bb1ad17 12553 strcat (buff, " | ");
44ec90b9 12554
7bb1ad17 12555 strcat (buff, "INFO");
44ec90b9
RO
12556 }
12557
12558 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
7bb1ad17
MR
12559 {
12560 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12561 strcat (buff, " | ");
12562
12563 strcat (buff, _("<unknown>"));
12564 }
252b5132
RH
12565
12566 return buff;
12567}
12568
12569/* Display the contents of the version sections. */
98fb390a 12570
015dc7e1 12571static bool
dda8d76d 12572process_version_sections (Filedata * filedata)
252b5132 12573{
2cf0635d 12574 Elf_Internal_Shdr * section;
b34976b6 12575 unsigned i;
015dc7e1 12576 bool found = false;
252b5132
RH
12577
12578 if (! do_version)
015dc7e1 12579 return true;
252b5132 12580
dda8d76d
NC
12581 for (i = 0, section = filedata->section_headers;
12582 i < filedata->file_header.e_shnum;
b34976b6 12583 i++, section++)
252b5132
RH
12584 {
12585 switch (section->sh_type)
12586 {
12587 case SHT_GNU_verdef:
12588 {
2cf0635d 12589 Elf_External_Verdef * edefs;
26c527e6
AM
12590 size_t idx;
12591 size_t cnt;
2cf0635d 12592 char * endbuf;
252b5132 12593
015dc7e1 12594 found = true;
252b5132 12595
ca0e11aa
NC
12596 if (filedata->is_separate)
12597 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
12598 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
12599 section->sh_info),
12600 filedata->file_name,
12601 printable_section_name (filedata, section),
12602 section->sh_info);
12603 else
12604 printf (ngettext ("\nVersion definition section '%s' "
12605 "contains %u entry:\n",
12606 "\nVersion definition section '%s' "
12607 "contains %u entries:\n",
12608 section->sh_info),
12609 printable_section_name (filedata, section),
12610 section->sh_info);
047c3dbf 12611
625d49fc 12612 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
26c527e6
AM
12613 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
12614 section->sh_offset, section->sh_link,
b6ac461a 12615 printable_section_name_from_index (filedata, section->sh_link, NULL));
252b5132 12616
3f5e193b 12617 edefs = (Elf_External_Verdef *)
dda8d76d 12618 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
3f5e193b 12619 _("version definition section"));
a6e9f9df
AM
12620 if (!edefs)
12621 break;
59245841 12622 endbuf = (char *) edefs + section->sh_size;
252b5132 12623
1445030f 12624 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
252b5132 12625 {
2cf0635d
NC
12626 char * vstart;
12627 Elf_External_Verdef * edef;
b34976b6 12628 Elf_Internal_Verdef ent;
2cf0635d 12629 Elf_External_Verdaux * eaux;
b34976b6 12630 Elf_Internal_Verdaux aux;
26c527e6 12631 size_t isum;
b34976b6 12632 int j;
103f02d3 12633
252b5132 12634 vstart = ((char *) edefs) + idx;
54806181
AM
12635 if (vstart + sizeof (*edef) > endbuf)
12636 break;
252b5132
RH
12637
12638 edef = (Elf_External_Verdef *) vstart;
12639
12640 ent.vd_version = BYTE_GET (edef->vd_version);
12641 ent.vd_flags = BYTE_GET (edef->vd_flags);
12642 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
12643 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
12644 ent.vd_hash = BYTE_GET (edef->vd_hash);
12645 ent.vd_aux = BYTE_GET (edef->vd_aux);
12646 ent.vd_next = BYTE_GET (edef->vd_next);
12647
26c527e6 12648 printf (_(" %#06zx: Rev: %d Flags: %s"),
252b5132
RH
12649 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
12650
12651 printf (_(" Index: %d Cnt: %d "),
12652 ent.vd_ndx, ent.vd_cnt);
12653
452bf675 12654 /* Check for overflow. */
1445030f 12655 if (ent.vd_aux > (size_t) (endbuf - vstart))
dd24e3da
NC
12656 break;
12657
252b5132
RH
12658 vstart += ent.vd_aux;
12659
1445030f
AM
12660 if (vstart + sizeof (*eaux) > endbuf)
12661 break;
252b5132
RH
12662 eaux = (Elf_External_Verdaux *) vstart;
12663
12664 aux.vda_name = BYTE_GET (eaux->vda_name);
12665 aux.vda_next = BYTE_GET (eaux->vda_next);
12666
84714f86 12667 if (valid_dynamic_name (filedata, aux.vda_name))
978c4450 12668 printf (_("Name: %s\n"),
84714f86 12669 get_dynamic_name (filedata, aux.vda_name));
252b5132
RH
12670 else
12671 printf (_("Name index: %ld\n"), aux.vda_name);
12672
12673 isum = idx + ent.vd_aux;
12674
b34976b6 12675 for (j = 1; j < ent.vd_cnt; j++)
252b5132 12676 {
1445030f
AM
12677 if (aux.vda_next < sizeof (*eaux)
12678 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
12679 {
12680 warn (_("Invalid vda_next field of %lx\n"),
12681 aux.vda_next);
12682 j = ent.vd_cnt;
12683 break;
12684 }
dd24e3da 12685 /* Check for overflow. */
7e26601c 12686 if (aux.vda_next > (size_t) (endbuf - vstart))
dd24e3da
NC
12687 break;
12688
252b5132
RH
12689 isum += aux.vda_next;
12690 vstart += aux.vda_next;
12691
54806181
AM
12692 if (vstart + sizeof (*eaux) > endbuf)
12693 break;
1445030f 12694 eaux = (Elf_External_Verdaux *) vstart;
252b5132
RH
12695
12696 aux.vda_name = BYTE_GET (eaux->vda_name);
12697 aux.vda_next = BYTE_GET (eaux->vda_next);
12698
84714f86 12699 if (valid_dynamic_name (filedata, aux.vda_name))
26c527e6 12700 printf (_(" %#06zx: Parent %d: %s\n"),
978c4450 12701 isum, j,
84714f86 12702 get_dynamic_name (filedata, aux.vda_name));
252b5132 12703 else
26c527e6 12704 printf (_(" %#06zx: Parent %d, name index: %ld\n"),
252b5132
RH
12705 isum, j, aux.vda_name);
12706 }
dd24e3da 12707
54806181
AM
12708 if (j < ent.vd_cnt)
12709 printf (_(" Version def aux past end of section\n"));
252b5132 12710
c9f02c3e
MR
12711 /* PR 17531:
12712 file: id:000001,src:000172+005151,op:splice,rep:2. */
1445030f
AM
12713 if (ent.vd_next < sizeof (*edef)
12714 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
12715 {
12716 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
12717 cnt = section->sh_info;
12718 break;
12719 }
452bf675 12720 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
5d921cbd
NC
12721 break;
12722
252b5132
RH
12723 idx += ent.vd_next;
12724 }
dd24e3da 12725
54806181
AM
12726 if (cnt < section->sh_info)
12727 printf (_(" Version definition past end of section\n"));
252b5132
RH
12728
12729 free (edefs);
12730 }
12731 break;
103f02d3 12732
252b5132
RH
12733 case SHT_GNU_verneed:
12734 {
2cf0635d 12735 Elf_External_Verneed * eneed;
26c527e6
AM
12736 size_t idx;
12737 size_t cnt;
2cf0635d 12738 char * endbuf;
252b5132 12739
015dc7e1 12740 found = true;
252b5132 12741
ca0e11aa
NC
12742 if (filedata->is_separate)
12743 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
12744 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
12745 section->sh_info),
12746 filedata->file_name,
12747 printable_section_name (filedata, section),
12748 section->sh_info);
12749 else
12750 printf (ngettext ("\nVersion needs section '%s' "
12751 "contains %u entry:\n",
12752 "\nVersion needs section '%s' "
12753 "contains %u entries:\n",
12754 section->sh_info),
12755 printable_section_name (filedata, section),
12756 section->sh_info);
047c3dbf 12757
625d49fc 12758 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
26c527e6
AM
12759 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
12760 section->sh_offset, section->sh_link,
b6ac461a 12761 printable_section_name_from_index (filedata, section->sh_link, NULL));
252b5132 12762
dda8d76d 12763 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
3f5e193b
NC
12764 section->sh_offset, 1,
12765 section->sh_size,
9cf03b7e 12766 _("Version Needs section"));
a6e9f9df
AM
12767 if (!eneed)
12768 break;
59245841 12769 endbuf = (char *) eneed + section->sh_size;
252b5132
RH
12770
12771 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12772 {
2cf0635d 12773 Elf_External_Verneed * entry;
b34976b6 12774 Elf_Internal_Verneed ent;
26c527e6 12775 size_t isum;
b34976b6 12776 int j;
2cf0635d 12777 char * vstart;
252b5132
RH
12778
12779 vstart = ((char *) eneed) + idx;
54806181
AM
12780 if (vstart + sizeof (*entry) > endbuf)
12781 break;
252b5132
RH
12782
12783 entry = (Elf_External_Verneed *) vstart;
12784
12785 ent.vn_version = BYTE_GET (entry->vn_version);
12786 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
12787 ent.vn_file = BYTE_GET (entry->vn_file);
12788 ent.vn_aux = BYTE_GET (entry->vn_aux);
12789 ent.vn_next = BYTE_GET (entry->vn_next);
12790
26c527e6 12791 printf (_(" %#06zx: Version: %d"), idx, ent.vn_version);
252b5132 12792
84714f86 12793 if (valid_dynamic_name (filedata, ent.vn_file))
978c4450 12794 printf (_(" File: %s"),
84714f86 12795 get_dynamic_name (filedata, ent.vn_file));
252b5132
RH
12796 else
12797 printf (_(" File: %lx"), ent.vn_file);
12798
12799 printf (_(" Cnt: %d\n"), ent.vn_cnt);
12800
dd24e3da 12801 /* Check for overflow. */
7e26601c 12802 if (ent.vn_aux > (size_t) (endbuf - vstart))
dd24e3da 12803 break;
252b5132
RH
12804 vstart += ent.vn_aux;
12805
12806 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
12807 {
2cf0635d 12808 Elf_External_Vernaux * eaux;
b34976b6 12809 Elf_Internal_Vernaux aux;
252b5132 12810
54806181
AM
12811 if (vstart + sizeof (*eaux) > endbuf)
12812 break;
252b5132
RH
12813 eaux = (Elf_External_Vernaux *) vstart;
12814
12815 aux.vna_hash = BYTE_GET (eaux->vna_hash);
12816 aux.vna_flags = BYTE_GET (eaux->vna_flags);
12817 aux.vna_other = BYTE_GET (eaux->vna_other);
12818 aux.vna_name = BYTE_GET (eaux->vna_name);
12819 aux.vna_next = BYTE_GET (eaux->vna_next);
12820
84714f86 12821 if (valid_dynamic_name (filedata, aux.vna_name))
26c527e6 12822 printf (_(" %#06zx: Name: %s"),
84714f86 12823 isum, get_dynamic_name (filedata, aux.vna_name));
252b5132 12824 else
26c527e6 12825 printf (_(" %#06zx: Name index: %lx"),
252b5132
RH
12826 isum, aux.vna_name);
12827
12828 printf (_(" Flags: %s Version: %d\n"),
12829 get_ver_flags (aux.vna_flags), aux.vna_other);
12830
1445030f
AM
12831 if (aux.vna_next < sizeof (*eaux)
12832 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
53774b7e
NC
12833 {
12834 warn (_("Invalid vna_next field of %lx\n"),
12835 aux.vna_next);
12836 j = ent.vn_cnt;
12837 break;
12838 }
1445030f
AM
12839 /* Check for overflow. */
12840 if (aux.vna_next > (size_t) (endbuf - vstart))
12841 break;
252b5132
RH
12842 isum += aux.vna_next;
12843 vstart += aux.vna_next;
12844 }
9cf03b7e 12845
54806181 12846 if (j < ent.vn_cnt)
f9a6a8f0 12847 warn (_("Missing Version Needs auxiliary information\n"));
252b5132 12848
1445030f
AM
12849 if (ent.vn_next < sizeof (*entry)
12850 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
c24cf8b6 12851 {
452bf675 12852 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
c24cf8b6
NC
12853 cnt = section->sh_info;
12854 break;
12855 }
1445030f
AM
12856 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
12857 break;
252b5132
RH
12858 idx += ent.vn_next;
12859 }
9cf03b7e 12860
54806181 12861 if (cnt < section->sh_info)
9cf03b7e 12862 warn (_("Missing Version Needs information\n"));
103f02d3 12863
252b5132
RH
12864 free (eneed);
12865 }
12866 break;
12867
12868 case SHT_GNU_versym:
12869 {
2cf0635d 12870 Elf_Internal_Shdr * link_section;
26c527e6 12871 uint64_t total;
8b73c356 12872 unsigned int cnt;
2cf0635d
NC
12873 unsigned char * edata;
12874 unsigned short * data;
12875 char * strtab;
12876 Elf_Internal_Sym * symbols;
12877 Elf_Internal_Shdr * string_sec;
26c527e6
AM
12878 uint64_t num_syms;
12879 uint64_t off;
252b5132 12880
dda8d76d 12881 if (section->sh_link >= filedata->file_header.e_shnum)
c256ffe7
JJ
12882 break;
12883
dda8d76d 12884 link_section = filedata->section_headers + section->sh_link;
08d8fa11 12885 total = section->sh_size / sizeof (Elf_External_Versym);
252b5132 12886
dda8d76d 12887 if (link_section->sh_link >= filedata->file_header.e_shnum)
c256ffe7
JJ
12888 break;
12889
015dc7e1 12890 found = true;
252b5132 12891
4de91c10 12892 symbols = get_elf_symbols (filedata, link_section, & num_syms);
dd24e3da
NC
12893 if (symbols == NULL)
12894 break;
252b5132 12895
dda8d76d 12896 string_sec = filedata->section_headers + link_section->sh_link;
252b5132 12897
dda8d76d 12898 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
3f5e193b
NC
12899 string_sec->sh_size,
12900 _("version string table"));
a6e9f9df 12901 if (!strtab)
0429c154
MS
12902 {
12903 free (symbols);
12904 break;
12905 }
252b5132 12906
ca0e11aa 12907 if (filedata->is_separate)
26c527e6
AM
12908 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %" PRIu64 " entry:\n",
12909 "\nIn linked file '%s' the version symbols section '%s' contains %" PRIu64 " entries:\n",
ca0e11aa
NC
12910 total),
12911 filedata->file_name,
12912 printable_section_name (filedata, section),
26c527e6 12913 total);
ca0e11aa
NC
12914 else
12915 printf (ngettext ("\nVersion symbols section '%s' "
26c527e6 12916 "contains %" PRIu64 " entry:\n",
ca0e11aa 12917 "\nVersion symbols section '%s' "
26c527e6 12918 "contains %" PRIu64 " entries:\n",
ca0e11aa
NC
12919 total),
12920 printable_section_name (filedata, section),
26c527e6 12921 total);
252b5132 12922
625d49fc 12923 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
26c527e6
AM
12924 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
12925 section->sh_offset, section->sh_link,
dda8d76d 12926 printable_section_name (filedata, link_section));
252b5132 12927
dda8d76d 12928 off = offset_from_vma (filedata,
978c4450 12929 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
d3ba0551 12930 total * sizeof (short));
95099889
AM
12931 edata = (unsigned char *) get_data (NULL, filedata, off,
12932 sizeof (short), total,
12933 _("version symbol data"));
a6e9f9df
AM
12934 if (!edata)
12935 {
12936 free (strtab);
0429c154 12937 free (symbols);
a6e9f9df
AM
12938 break;
12939 }
252b5132 12940
3f5e193b 12941 data = (short unsigned int *) cmalloc (total, sizeof (short));
252b5132
RH
12942
12943 for (cnt = total; cnt --;)
b34976b6
AM
12944 data[cnt] = byte_get (edata + cnt * sizeof (short),
12945 sizeof (short));
252b5132
RH
12946
12947 free (edata);
12948
12949 for (cnt = 0; cnt < total; cnt += 4)
12950 {
12951 int j, nn;
ab273396
AM
12952 char *name;
12953 char *invalid = _("*invalid*");
252b5132
RH
12954
12955 printf (" %03x:", cnt);
12956
12957 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
b34976b6 12958 switch (data[cnt + j])
252b5132
RH
12959 {
12960 case 0:
12961 fputs (_(" 0 (*local*) "), stdout);
12962 break;
12963
12964 case 1:
12965 fputs (_(" 1 (*global*) "), stdout);
12966 break;
12967
12968 default:
c244d050
NC
12969 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
12970 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
252b5132 12971
dd24e3da 12972 /* If this index value is greater than the size of the symbols
ba5cdace 12973 array, break to avoid an out-of-bounds read. */
26c527e6 12974 if (cnt + j >= num_syms)
dd24e3da
NC
12975 {
12976 warn (_("invalid index into symbol array\n"));
12977 break;
12978 }
12979
ab273396 12980 name = NULL;
978c4450 12981 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
252b5132 12982 {
b34976b6 12983 Elf_Internal_Verneed ivn;
26c527e6 12984 uint64_t offset;
252b5132 12985
d93f0186 12986 offset = offset_from_vma
978c4450
AM
12987 (filedata,
12988 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
d93f0186 12989 sizeof (Elf_External_Verneed));
252b5132 12990
b34976b6 12991 do
252b5132 12992 {
b34976b6
AM
12993 Elf_Internal_Vernaux ivna;
12994 Elf_External_Verneed evn;
12995 Elf_External_Vernaux evna;
26c527e6 12996 uint64_t a_off;
252b5132 12997
dda8d76d 12998 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
59245841
NC
12999 _("version need")) == NULL)
13000 break;
0b4362b0 13001
252b5132
RH
13002 ivn.vn_aux = BYTE_GET (evn.vn_aux);
13003 ivn.vn_next = BYTE_GET (evn.vn_next);
13004
13005 a_off = offset + ivn.vn_aux;
13006
13007 do
13008 {
dda8d76d 13009 if (get_data (&evna, filedata, a_off, sizeof (evna),
59245841
NC
13010 1, _("version need aux (2)")) == NULL)
13011 {
13012 ivna.vna_next = 0;
13013 ivna.vna_other = 0;
13014 }
13015 else
13016 {
13017 ivna.vna_next = BYTE_GET (evna.vna_next);
13018 ivna.vna_other = BYTE_GET (evna.vna_other);
13019 }
252b5132
RH
13020
13021 a_off += ivna.vna_next;
13022 }
b34976b6 13023 while (ivna.vna_other != data[cnt + j]
252b5132
RH
13024 && ivna.vna_next != 0);
13025
b34976b6 13026 if (ivna.vna_other == data[cnt + j])
252b5132
RH
13027 {
13028 ivna.vna_name = BYTE_GET (evna.vna_name);
13029
54806181 13030 if (ivna.vna_name >= string_sec->sh_size)
ab273396 13031 name = invalid;
54806181
AM
13032 else
13033 name = strtab + ivna.vna_name;
252b5132
RH
13034 break;
13035 }
13036
13037 offset += ivn.vn_next;
13038 }
13039 while (ivn.vn_next);
13040 }
00d93f34 13041
ab273396 13042 if (data[cnt + j] != 0x8001
978c4450 13043 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
252b5132 13044 {
b34976b6
AM
13045 Elf_Internal_Verdef ivd;
13046 Elf_External_Verdef evd;
26c527e6 13047 uint64_t offset;
252b5132 13048
d93f0186 13049 offset = offset_from_vma
978c4450
AM
13050 (filedata,
13051 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
d93f0186 13052 sizeof evd);
252b5132
RH
13053
13054 do
13055 {
dda8d76d 13056 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
59245841
NC
13057 _("version def")) == NULL)
13058 {
13059 ivd.vd_next = 0;
948f632f 13060 /* PR 17531: file: 046-1082287-0.004. */
3102e897
NC
13061 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
13062 break;
59245841
NC
13063 }
13064 else
13065 {
13066 ivd.vd_next = BYTE_GET (evd.vd_next);
13067 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
13068 }
252b5132
RH
13069
13070 offset += ivd.vd_next;
13071 }
c244d050 13072 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
252b5132
RH
13073 && ivd.vd_next != 0);
13074
c244d050 13075 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
252b5132 13076 {
b34976b6
AM
13077 Elf_External_Verdaux evda;
13078 Elf_Internal_Verdaux ivda;
252b5132
RH
13079
13080 ivd.vd_aux = BYTE_GET (evd.vd_aux);
13081
dda8d76d 13082 if (get_data (&evda, filedata,
59245841
NC
13083 offset - ivd.vd_next + ivd.vd_aux,
13084 sizeof (evda), 1,
13085 _("version def aux")) == NULL)
13086 break;
252b5132
RH
13087
13088 ivda.vda_name = BYTE_GET (evda.vda_name);
13089
54806181 13090 if (ivda.vda_name >= string_sec->sh_size)
ab273396
AM
13091 name = invalid;
13092 else if (name != NULL && name != invalid)
13093 name = _("*both*");
54806181
AM
13094 else
13095 name = strtab + ivda.vda_name;
252b5132
RH
13096 }
13097 }
ab273396
AM
13098 if (name != NULL)
13099 nn += printf ("(%s%-*s",
13100 name,
13101 12 - (int) strlen (name),
13102 ")");
252b5132
RH
13103
13104 if (nn < 18)
13105 printf ("%*c", 18 - nn, ' ');
13106 }
13107
13108 putchar ('\n');
13109 }
13110
13111 free (data);
13112 free (strtab);
13113 free (symbols);
13114 }
13115 break;
103f02d3 13116
252b5132
RH
13117 default:
13118 break;
13119 }
13120 }
13121
13122 if (! found)
ca0e11aa
NC
13123 {
13124 if (filedata->is_separate)
13125 printf (_("\nNo version information found in linked file '%s'.\n"),
13126 filedata->file_name);
13127 else
13128 printf (_("\nNo version information found in this file.\n"));
13129 }
252b5132 13130
015dc7e1 13131 return true;
252b5132
RH
13132}
13133
d1133906 13134static const char *
dda8d76d 13135get_symbol_binding (Filedata * filedata, unsigned int binding)
252b5132 13136{
89246a0e 13137 static char buff[64];
252b5132
RH
13138
13139 switch (binding)
13140 {
b34976b6
AM
13141 case STB_LOCAL: return "LOCAL";
13142 case STB_GLOBAL: return "GLOBAL";
13143 case STB_WEAK: return "WEAK";
252b5132
RH
13144 default:
13145 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
e9e44622
JJ
13146 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
13147 binding);
252b5132 13148 else if (binding >= STB_LOOS && binding <= STB_HIOS)
3e7a7d11
NC
13149 {
13150 if (binding == STB_GNU_UNIQUE
df3a023b 13151 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
3e7a7d11
NC
13152 return "UNIQUE";
13153 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
13154 }
252b5132 13155 else
e9e44622 13156 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
252b5132
RH
13157 return buff;
13158 }
13159}
13160
d1133906 13161static const char *
dda8d76d 13162get_symbol_type (Filedata * filedata, unsigned int type)
252b5132 13163{
89246a0e 13164 static char buff[64];
252b5132
RH
13165
13166 switch (type)
13167 {
b34976b6
AM
13168 case STT_NOTYPE: return "NOTYPE";
13169 case STT_OBJECT: return "OBJECT";
13170 case STT_FUNC: return "FUNC";
13171 case STT_SECTION: return "SECTION";
13172 case STT_FILE: return "FILE";
13173 case STT_COMMON: return "COMMON";
13174 case STT_TLS: return "TLS";
15ab5209
DB
13175 case STT_RELC: return "RELC";
13176 case STT_SRELC: return "SRELC";
252b5132
RH
13177 default:
13178 if (type >= STT_LOPROC && type <= STT_HIPROC)
df75f1af 13179 {
dda8d76d 13180 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
3510a7b8 13181 return "THUMB_FUNC";
103f02d3 13182
dda8d76d 13183 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
103f02d3
UD
13184 return "REGISTER";
13185
dda8d76d 13186 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
103f02d3
UD
13187 return "PARISC_MILLI";
13188
e9e44622 13189 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
df75f1af 13190 }
252b5132 13191 else if (type >= STT_LOOS && type <= STT_HIOS)
103f02d3 13192 {
dda8d76d 13193 if (filedata->file_header.e_machine == EM_PARISC)
103f02d3
UD
13194 {
13195 if (type == STT_HP_OPAQUE)
13196 return "HP_OPAQUE";
13197 if (type == STT_HP_STUB)
13198 return "HP_STUB";
13199 }
13200
8654c01f
ML
13201 if (type == STT_GNU_IFUNC
13202 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
13203 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
13204 return "IFUNC";
13205
e9e44622 13206 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
103f02d3 13207 }
252b5132 13208 else
e9e44622 13209 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
252b5132
RH
13210 return buff;
13211 }
13212}
13213
d1133906 13214static const char *
d3ba0551 13215get_symbol_visibility (unsigned int visibility)
d1133906
NC
13216{
13217 switch (visibility)
13218 {
b34976b6
AM
13219 case STV_DEFAULT: return "DEFAULT";
13220 case STV_INTERNAL: return "INTERNAL";
13221 case STV_HIDDEN: return "HIDDEN";
d1133906 13222 case STV_PROTECTED: return "PROTECTED";
bee0ee85 13223 default:
27a45f42 13224 error (_("Unrecognized visibility value: %u\n"), visibility);
bee0ee85 13225 return _("<unknown>");
d1133906
NC
13226 }
13227}
13228
2057d69d
CZ
13229static const char *
13230get_alpha_symbol_other (unsigned int other)
9abca702 13231{
2057d69d
CZ
13232 switch (other)
13233 {
13234 case STO_ALPHA_NOPV: return "NOPV";
13235 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
13236 default:
27a45f42 13237 error (_("Unrecognized alpha specific other value: %u\n"), other);
2057d69d 13238 return _("<unknown>");
9abca702 13239 }
2057d69d
CZ
13240}
13241
fd85a6a1
NC
13242static const char *
13243get_solaris_symbol_visibility (unsigned int visibility)
13244{
13245 switch (visibility)
13246 {
13247 case 4: return "EXPORTED";
13248 case 5: return "SINGLETON";
13249 case 6: return "ELIMINATE";
13250 default: return get_symbol_visibility (visibility);
13251 }
13252}
13253
2301ed1c
SN
13254static const char *
13255get_aarch64_symbol_other (unsigned int other)
13256{
13257 static char buf[32];
13258
13259 if (other & STO_AARCH64_VARIANT_PCS)
13260 {
13261 other &= ~STO_AARCH64_VARIANT_PCS;
13262 if (other == 0)
13263 return "VARIANT_PCS";
13264 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
13265 return buf;
13266 }
13267 return NULL;
13268}
13269
5e2b0d47
NC
13270static const char *
13271get_mips_symbol_other (unsigned int other)
13272{
13273 switch (other)
13274 {
32ec8896
NC
13275 case STO_OPTIONAL: return "OPTIONAL";
13276 case STO_MIPS_PLT: return "MIPS PLT";
13277 case STO_MIPS_PIC: return "MIPS PIC";
13278 case STO_MICROMIPS: return "MICROMIPS";
13279 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
13280 case STO_MIPS16: return "MIPS16";
13281 default: return NULL;
5e2b0d47
NC
13282 }
13283}
13284
28f997cf 13285static const char *
dda8d76d 13286get_ia64_symbol_other (Filedata * filedata, unsigned int other)
28f997cf 13287{
dda8d76d 13288 if (is_ia64_vms (filedata))
28f997cf
TG
13289 {
13290 static char res[32];
13291
13292 res[0] = 0;
13293
13294 /* Function types is for images and .STB files only. */
dda8d76d 13295 switch (filedata->file_header.e_type)
28f997cf
TG
13296 {
13297 case ET_DYN:
13298 case ET_EXEC:
13299 switch (VMS_ST_FUNC_TYPE (other))
13300 {
13301 case VMS_SFT_CODE_ADDR:
13302 strcat (res, " CA");
13303 break;
13304 case VMS_SFT_SYMV_IDX:
13305 strcat (res, " VEC");
13306 break;
13307 case VMS_SFT_FD:
13308 strcat (res, " FD");
13309 break;
13310 case VMS_SFT_RESERVE:
13311 strcat (res, " RSV");
13312 break;
13313 default:
bee0ee85
NC
13314 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
13315 VMS_ST_FUNC_TYPE (other));
13316 strcat (res, " <unknown>");
13317 break;
28f997cf
TG
13318 }
13319 break;
13320 default:
13321 break;
13322 }
13323 switch (VMS_ST_LINKAGE (other))
13324 {
13325 case VMS_STL_IGNORE:
13326 strcat (res, " IGN");
13327 break;
13328 case VMS_STL_RESERVE:
13329 strcat (res, " RSV");
13330 break;
13331 case VMS_STL_STD:
13332 strcat (res, " STD");
13333 break;
13334 case VMS_STL_LNK:
13335 strcat (res, " LNK");
13336 break;
13337 default:
bee0ee85
NC
13338 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
13339 VMS_ST_LINKAGE (other));
13340 strcat (res, " <unknown>");
13341 break;
28f997cf
TG
13342 }
13343
13344 if (res[0] != 0)
13345 return res + 1;
13346 else
13347 return res;
13348 }
13349 return NULL;
13350}
13351
6911b7dc
AM
13352static const char *
13353get_ppc64_symbol_other (unsigned int other)
13354{
14732552
AM
13355 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
13356 return NULL;
13357
13358 other >>= STO_PPC64_LOCAL_BIT;
13359 if (other <= 6)
6911b7dc 13360 {
89246a0e 13361 static char buf[64];
14732552
AM
13362 if (other >= 2)
13363 other = ppc64_decode_local_entry (other);
13364 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
6911b7dc
AM
13365 return buf;
13366 }
13367 return NULL;
13368}
13369
8155b853
NC
13370static const char *
13371get_riscv_symbol_other (unsigned int other)
13372{
13373 static char buf[32];
13374 buf[0] = 0;
13375
13376 if (other & STO_RISCV_VARIANT_CC)
13377 {
13378 strcat (buf, _(" VARIANT_CC"));
13379 other &= ~STO_RISCV_VARIANT_CC;
13380 }
13381
13382 if (other != 0)
13383 snprintf (buf, sizeof buf, " %x", other);
13384
13385
13386 if (buf[0] != 0)
13387 return buf + 1;
13388 else
13389 return buf;
13390}
13391
5e2b0d47 13392static const char *
dda8d76d 13393get_symbol_other (Filedata * filedata, unsigned int other)
5e2b0d47
NC
13394{
13395 const char * result = NULL;
89246a0e 13396 static char buff [64];
5e2b0d47
NC
13397
13398 if (other == 0)
13399 return "";
13400
dda8d76d 13401 switch (filedata->file_header.e_machine)
5e2b0d47 13402 {
2057d69d
CZ
13403 case EM_ALPHA:
13404 result = get_alpha_symbol_other (other);
13405 break;
2301ed1c
SN
13406 case EM_AARCH64:
13407 result = get_aarch64_symbol_other (other);
13408 break;
5e2b0d47
NC
13409 case EM_MIPS:
13410 result = get_mips_symbol_other (other);
28f997cf
TG
13411 break;
13412 case EM_IA_64:
dda8d76d 13413 result = get_ia64_symbol_other (filedata, other);
28f997cf 13414 break;
6911b7dc
AM
13415 case EM_PPC64:
13416 result = get_ppc64_symbol_other (other);
13417 break;
8155b853
NC
13418 case EM_RISCV:
13419 result = get_riscv_symbol_other (other);
13420 break;
5e2b0d47 13421 default:
fd85a6a1 13422 result = NULL;
5e2b0d47
NC
13423 break;
13424 }
13425
13426 if (result)
13427 return result;
13428
13429 snprintf (buff, sizeof buff, _("<other>: %x"), other);
13430 return buff;
13431}
13432
bb4d2ac2 13433static const char *
26c527e6
AM
13434get_symbol_version_string (Filedata *filedata,
13435 bool is_dynsym,
13436 const char *strtab,
13437 size_t strtab_size,
13438 unsigned int si,
13439 Elf_Internal_Sym *psym,
13440 enum versioned_symbol_info *sym_info,
13441 unsigned short *vna_other)
bb4d2ac2 13442{
ab273396
AM
13443 unsigned char data[2];
13444 unsigned short vers_data;
26c527e6 13445 uint64_t offset;
7a815dd5 13446 unsigned short max_vd_ndx;
bb4d2ac2 13447
ab273396 13448 if (!is_dynsym
978c4450 13449 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
ab273396 13450 return NULL;
bb4d2ac2 13451
978c4450
AM
13452 offset = offset_from_vma (filedata,
13453 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
ab273396 13454 sizeof data + si * sizeof (vers_data));
bb4d2ac2 13455
dda8d76d 13456 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
ab273396
AM
13457 sizeof (data), 1, _("version data")) == NULL)
13458 return NULL;
13459
13460 vers_data = byte_get (data, 2);
bb4d2ac2 13461
1f6f5dba 13462 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
ab273396 13463 return NULL;
bb4d2ac2 13464
0b8b7609 13465 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
7a815dd5
L
13466 max_vd_ndx = 0;
13467
ab273396
AM
13468 /* Usually we'd only see verdef for defined symbols, and verneed for
13469 undefined symbols. However, symbols defined by the linker in
13470 .dynbss for variables copied from a shared library in order to
13471 avoid text relocations are defined yet have verneed. We could
13472 use a heuristic to detect the special case, for example, check
13473 for verneed first on symbols defined in SHT_NOBITS sections, but
13474 it is simpler and more reliable to just look for both verdef and
13475 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
bb4d2ac2 13476
ab273396
AM
13477 if (psym->st_shndx != SHN_UNDEF
13478 && vers_data != 0x8001
978c4450 13479 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
ab273396
AM
13480 {
13481 Elf_Internal_Verdef ivd;
13482 Elf_Internal_Verdaux ivda;
13483 Elf_External_Verdaux evda;
26c527e6 13484 uint64_t off;
bb4d2ac2 13485
dda8d76d 13486 off = offset_from_vma (filedata,
978c4450 13487 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
ab273396
AM
13488 sizeof (Elf_External_Verdef));
13489
13490 do
bb4d2ac2 13491 {
ab273396
AM
13492 Elf_External_Verdef evd;
13493
dda8d76d 13494 if (get_data (&evd, filedata, off, sizeof (evd), 1,
ab273396
AM
13495 _("version def")) == NULL)
13496 {
13497 ivd.vd_ndx = 0;
13498 ivd.vd_aux = 0;
13499 ivd.vd_next = 0;
1f6f5dba 13500 ivd.vd_flags = 0;
ab273396
AM
13501 }
13502 else
bb4d2ac2 13503 {
ab273396
AM
13504 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
13505 ivd.vd_aux = BYTE_GET (evd.vd_aux);
13506 ivd.vd_next = BYTE_GET (evd.vd_next);
1f6f5dba 13507 ivd.vd_flags = BYTE_GET (evd.vd_flags);
ab273396 13508 }
bb4d2ac2 13509
7a815dd5
L
13510 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
13511 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
13512
ab273396
AM
13513 off += ivd.vd_next;
13514 }
13515 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
bb4d2ac2 13516
ab273396
AM
13517 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
13518 {
9abca702 13519 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
1f6f5dba
L
13520 return NULL;
13521
ab273396
AM
13522 off -= ivd.vd_next;
13523 off += ivd.vd_aux;
bb4d2ac2 13524
dda8d76d 13525 if (get_data (&evda, filedata, off, sizeof (evda), 1,
ab273396
AM
13526 _("version def aux")) != NULL)
13527 {
13528 ivda.vda_name = BYTE_GET (evda.vda_name);
bb4d2ac2 13529
ab273396 13530 if (psym->st_name != ivda.vda_name)
0b8b7609
AM
13531 return (ivda.vda_name < strtab_size
13532 ? strtab + ivda.vda_name : _("<corrupt>"));
ab273396
AM
13533 }
13534 }
13535 }
bb4d2ac2 13536
978c4450 13537 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
ab273396
AM
13538 {
13539 Elf_External_Verneed evn;
13540 Elf_Internal_Verneed ivn;
13541 Elf_Internal_Vernaux ivna;
bb4d2ac2 13542
dda8d76d 13543 offset = offset_from_vma (filedata,
978c4450 13544 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
ab273396
AM
13545 sizeof evn);
13546 do
13547 {
26c527e6 13548 uint64_t vna_off;
bb4d2ac2 13549
dda8d76d 13550 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
ab273396
AM
13551 _("version need")) == NULL)
13552 {
13553 ivna.vna_next = 0;
13554 ivna.vna_other = 0;
13555 ivna.vna_name = 0;
13556 break;
13557 }
bb4d2ac2 13558
ab273396
AM
13559 ivn.vn_aux = BYTE_GET (evn.vn_aux);
13560 ivn.vn_next = BYTE_GET (evn.vn_next);
bb4d2ac2 13561
ab273396 13562 vna_off = offset + ivn.vn_aux;
bb4d2ac2 13563
ab273396
AM
13564 do
13565 {
13566 Elf_External_Vernaux evna;
bb4d2ac2 13567
dda8d76d 13568 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
ab273396 13569 _("version need aux (3)")) == NULL)
bb4d2ac2 13570 {
ab273396
AM
13571 ivna.vna_next = 0;
13572 ivna.vna_other = 0;
13573 ivna.vna_name = 0;
bb4d2ac2 13574 }
bb4d2ac2 13575 else
bb4d2ac2 13576 {
ab273396
AM
13577 ivna.vna_other = BYTE_GET (evna.vna_other);
13578 ivna.vna_next = BYTE_GET (evna.vna_next);
13579 ivna.vna_name = BYTE_GET (evna.vna_name);
13580 }
bb4d2ac2 13581
ab273396
AM
13582 vna_off += ivna.vna_next;
13583 }
13584 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
bb4d2ac2 13585
ab273396
AM
13586 if (ivna.vna_other == vers_data)
13587 break;
bb4d2ac2 13588
ab273396
AM
13589 offset += ivn.vn_next;
13590 }
13591 while (ivn.vn_next != 0);
bb4d2ac2 13592
ab273396
AM
13593 if (ivna.vna_other == vers_data)
13594 {
13595 *sym_info = symbol_undefined;
13596 *vna_other = ivna.vna_other;
13597 return (ivna.vna_name < strtab_size
13598 ? strtab + ivna.vna_name : _("<corrupt>"));
bb4d2ac2 13599 }
7a815dd5
L
13600 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
13601 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
13602 return _("<corrupt>");
bb4d2ac2 13603 }
ab273396 13604 return NULL;
bb4d2ac2
L
13605}
13606
047c3dbf
NL
13607/* Display a symbol size on stdout. Format is based on --sym-base setting. */
13608
13609static unsigned int
b6ac461a 13610print_symbol_size (uint64_t vma, int base)
047c3dbf
NL
13611{
13612 switch (base)
13613 {
13614 case 8:
13615 return print_vma (vma, OCTAL_5);
13616
13617 case 10:
13618 return print_vma (vma, UNSIGNED_5);
13619
13620 case 16:
13621 return print_vma (vma, PREFIX_HEX_5);
13622
13623 case 0:
13624 default:
13625 return print_vma (vma, DEC_5);
13626 }
13627}
13628
b6ac461a
NC
13629/* Print information on a single symbol. */
13630
10ca4b04 13631static void
b6ac461a
NC
13632print_symbol (Filedata * filedata,
13633 uint64_t symbol_index,
13634 Elf_Internal_Sym * symtab,
13635 Elf_Internal_Shdr * section,
13636 char * strtab,
13637 size_t strtab_size)
252b5132 13638{
10ca4b04
L
13639 const char *version_string;
13640 enum versioned_symbol_info sym_info;
13641 unsigned short vna_other;
23356397 13642 const char * sstr;
b6ac461a 13643 Elf_Internal_Sym *psym = symtab + symbol_index;
b9e920ec 13644
b6ac461a
NC
13645 /* FIXME: We should have a table of field widths,
13646 rather than using hard coded constants. */
13647 printf ("%6" PRId64 ": ", symbol_index);
10ca4b04
L
13648 print_vma (psym->st_value, LONG_HEX);
13649 putchar (' ');
b6ac461a 13650 print_symbol_size (psym->st_size, sym_base);
10ca4b04
L
13651 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
13652 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
13653 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
13654 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
13655 else
252b5132 13656 {
10ca4b04 13657 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
252b5132 13658
10ca4b04 13659 printf (" %-7s", get_symbol_visibility (vis));
b6ac461a 13660
10ca4b04 13661 /* Check to see if any other bits in the st_other field are set.
b6ac461a
NC
13662 FIXME: Displaying this information here disrupts the layout
13663 of the table being generated. */
10ca4b04
L
13664 if (psym->st_other ^ vis)
13665 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
252b5132 13666 }
0942c7ab 13667
b6ac461a
NC
13668 bool is_special;
13669
13670 sstr = printable_section_name_from_index (filedata, psym->st_shndx, & is_special);
13671
13672 /* Print the symbol's section index. If the index is special
13673 then print the index's name rather than its number. */
13674 if (is_special)
13675 {
13676 int printed;
13677
13678 /* Special case: If there are no section headers, and the printable
13679 name is "<section 0x...." then just display the section number
13680 as a decimal. This happens when objcopy --strip -section-headers
13681 is used. */
13682 if (filedata->file_header.e_shnum == 0 && startswith (sstr, "<section"))
13683 printed = printf (" %4d ", psym->st_shndx);
13684 else
13685 printed = printf (" %4s ", sstr);
13686
13687 if (extra_sym_info && printed < 16)
13688 printf ("%*s", 16 - printed, "");
13689 }
13690 else
13691 {
13692 printf (" %4u ", psym->st_shndx);
13693
13694 if (extra_sym_info)
13695 {
13696 /* Display the section name referenced by the section index. */
13697 int printed = printf ("(%s) ", sstr);
13698 if (printed < 10)
13699 printf ("%*s", 10 - printed, "");
13700 }
13701 }
13702
13703 /* Get the symbol's name. For section symbols without a
13704 specific name use the (already computed) section name. */
23356397 13705 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
b6ac461a 13706 && section_index_real (filedata, psym->st_shndx)
23356397
NC
13707 && psym->st_name == 0)
13708 {
b6ac461a 13709 ;
23356397
NC
13710 }
13711 else
13712 {
b6ac461a
NC
13713 bool is_valid;
13714
84714f86 13715 is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
23356397
NC
13716 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
13717 }
10ca4b04
L
13718
13719 version_string
13720 = get_symbol_version_string (filedata,
13721 (section == NULL
13722 || section->sh_type == SHT_DYNSYM),
b6ac461a 13723 strtab, strtab_size, symbol_index,
10ca4b04 13724 psym, &sym_info, &vna_other);
b9e920ec 13725
0942c7ab
NC
13726 int len_avail = 21;
13727 if (! do_wide && version_string != NULL)
13728 {
ddb43bab 13729 char buffer[16];
0942c7ab 13730
ddb43bab 13731 len_avail -= 1 + strlen (version_string);
0942c7ab
NC
13732
13733 if (sym_info == symbol_undefined)
13734 len_avail -= sprintf (buffer," (%d)", vna_other);
13735 else if (sym_info != symbol_hidden)
13736 len_avail -= 1;
13737 }
13738
b6ac461a 13739 print_symbol_name (len_avail, sstr);
b9e920ec 13740
10ca4b04
L
13741 if (version_string)
13742 {
13743 if (sym_info == symbol_undefined)
13744 printf ("@%s (%d)", version_string, vna_other);
f7a99963 13745 else
10ca4b04
L
13746 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
13747 version_string);
13748 }
6bd1a22c 13749
10ca4b04 13750 putchar ('\n');
6bd1a22c 13751
10ca4b04
L
13752 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
13753 && section != NULL
b6ac461a 13754 && symbol_index >= section->sh_info
10ca4b04
L
13755 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
13756 && filedata->file_header.e_machine != EM_MIPS
13757 /* Solaris binaries have been found to violate this requirement as
13758 well. Not sure if this is a bug or an ABI requirement. */
13759 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
26c527e6 13760 warn (_("local symbol %" PRIu64 " found at index >= %s's sh_info value of %u\n"),
b6ac461a 13761 symbol_index, printable_section_name (filedata, section), section->sh_info);
10ca4b04 13762}
f16a9783 13763
0f03783c
NC
13764static const char *
13765get_lto_kind (unsigned int kind)
13766{
13767 switch (kind)
13768 {
13769 case 0: return "DEF";
13770 case 1: return "WEAKDEF";
13771 case 2: return "UNDEF";
13772 case 3: return "WEAKUNDEF";
13773 case 4: return "COMMON";
13774 default:
13775 break;
13776 }
13777
13778 static char buffer[30];
13779 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
13780 sprintf (buffer, "<unknown: %u>", kind);
13781 return buffer;
13782}
13783
13784static const char *
13785get_lto_visibility (unsigned int visibility)
13786{
13787 switch (visibility)
13788 {
13789 case 0: return "DEFAULT";
13790 case 1: return "PROTECTED";
13791 case 2: return "INTERNAL";
13792 case 3: return "HIDDEN";
13793 default:
13794 break;
13795 }
13796
13797 static char buffer[30];
13798 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
13799 sprintf (buffer, "<unknown: %u>", visibility);
13800 return buffer;
13801}
13802
13803static const char *
13804get_lto_sym_type (unsigned int sym_type)
13805{
13806 switch (sym_type)
13807 {
13808 case 0: return "UNKNOWN";
13809 case 1: return "FUNCTION";
13810 case 2: return "VARIABLE";
13811 default:
13812 break;
13813 }
13814
13815 static char buffer[30];
13816 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
13817 sprintf (buffer, "<unknown: %u>", sym_type);
13818 return buffer;
13819}
13820
13821/* Display an LTO format symbol table.
13822 FIXME: The format of LTO symbol tables is not formalized.
13823 So this code could need changing in the future. */
13824
015dc7e1 13825static bool
0f03783c
NC
13826display_lto_symtab (Filedata * filedata,
13827 Elf_Internal_Shdr * section)
13828{
13829 if (section->sh_size == 0)
13830 {
ca0e11aa
NC
13831 if (filedata->is_separate)
13832 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
13833 printable_section_name (filedata, section),
13834 filedata->file_name);
13835 else
13836 printf (_("\nLTO Symbol table '%s' is empty!\n"),
13837 printable_section_name (filedata, section));
047c3dbf 13838
015dc7e1 13839 return true;
0f03783c
NC
13840 }
13841
13842 if (section->sh_size > filedata->file_size)
13843 {
26c527e6 13844 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
0f03783c 13845 printable_section_name (filedata, section),
26c527e6 13846 section->sh_size);
015dc7e1 13847 return false;
0f03783c
NC
13848 }
13849
13850 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
13851 section->sh_size, 1, _("LTO symbols"));
13852 if (alloced_data == NULL)
015dc7e1 13853 return false;
0f03783c
NC
13854
13855 /* Look for extended data for the symbol table. */
13856 Elf_Internal_Shdr * ext;
13857 void * ext_data_orig = NULL;
13858 char * ext_data = NULL;
13859 char * ext_data_end = NULL;
13860 char * ext_name = NULL;
13861
13862 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
84714f86
AM
13863 (section_name (filedata, section)
13864 + sizeof (".gnu.lto_.symtab.") - 1)) > 0
0f03783c
NC
13865 && ext_name != NULL /* Paranoia. */
13866 && (ext = find_section (filedata, ext_name)) != NULL)
13867 {
13868 if (ext->sh_size < 3)
13869 error (_("LTO Symbol extension table '%s' is empty!\n"),
13870 printable_section_name (filedata, ext));
13871 else
13872 {
13873 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
13874 ext->sh_size, 1,
13875 _("LTO ext symbol data"));
13876 if (ext_data != NULL)
13877 {
13878 ext_data_end = ext_data + ext->sh_size;
13879 if (* ext_data++ != 1)
13880 error (_("Unexpected version number in symbol extension table\n"));
13881 }
13882 }
13883 }
b9e920ec 13884
0f03783c
NC
13885 const unsigned char * data = (const unsigned char *) alloced_data;
13886 const unsigned char * end = data + section->sh_size;
13887
ca0e11aa
NC
13888 if (filedata->is_separate)
13889 printf (_("\nIn linked file '%s': "), filedata->file_name);
13890 else
13891 printf ("\n");
13892
0f03783c
NC
13893 if (ext_data_orig != NULL)
13894 {
13895 if (do_wide)
ca0e11aa 13896 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
0f03783c
NC
13897 printable_section_name (filedata, section),
13898 printable_section_name (filedata, ext));
13899 else
13900 {
ca0e11aa 13901 printf (_("LTO Symbol table '%s'\n"),
0f03783c
NC
13902 printable_section_name (filedata, section));
13903 printf (_(" and extension table '%s' contain:\n"),
13904 printable_section_name (filedata, ext));
13905 }
13906 }
13907 else
ca0e11aa 13908 printf (_("LTO Symbol table '%s' contains:\n"),
0f03783c 13909 printable_section_name (filedata, section));
b9e920ec 13910
0f03783c 13911 /* FIXME: Add a wide version. */
b9e920ec 13912 if (ext_data_orig != NULL)
0f03783c
NC
13913 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
13914 else
13915 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
13916
13917 /* FIXME: We do not handle style prefixes. */
13918
13919 while (data < end)
13920 {
13921 const unsigned char * sym_name = data;
13922 data += strnlen ((const char *) sym_name, end - data) + 1;
13923 if (data >= end)
13924 goto fail;
13925
13926 const unsigned char * comdat_key = data;
13927 data += strnlen ((const char *) comdat_key, end - data) + 1;
13928 if (data >= end)
13929 goto fail;
13930
13931 if (data + 2 + 8 + 4 > end)
13932 goto fail;
13933
13934 unsigned int kind = *data++;
13935 unsigned int visibility = *data++;
13936
928c411d 13937 uint64_t size = byte_get (data, 8);
0f03783c
NC
13938 data += 8;
13939
928c411d 13940 uint64_t slot = byte_get (data, 4);
0f03783c
NC
13941 data += 4;
13942
13943 if (ext_data != NULL)
13944 {
13945 if (ext_data < (ext_data_end - 1))
13946 {
13947 unsigned int sym_type = * ext_data ++;
13948 unsigned int sec_kind = * ext_data ++;
13949
31e5a3a3 13950 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " %9s %08x _",
0f03783c
NC
13951 * comdat_key == 0 ? "-" : (char *) comdat_key,
13952 get_lto_kind (kind),
13953 get_lto_visibility (visibility),
31e5a3a3
AM
13954 size,
13955 slot,
0f03783c 13956 get_lto_sym_type (sym_type),
31e5a3a3 13957 sec_kind);
b6ac461a 13958 print_symbol_name (6, (const char *) sym_name);
0f03783c
NC
13959 }
13960 else
13961 {
13962 error (_("Ran out of LTO symbol extension data\n"));
13963 ext_data = NULL;
13964 /* FIXME: return FAIL result ? */
13965 }
13966 }
13967 else
13968 {
31e5a3a3 13969 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " _",
0f03783c
NC
13970 * comdat_key == 0 ? "-" : (char *) comdat_key,
13971 get_lto_kind (kind),
13972 get_lto_visibility (visibility),
31e5a3a3
AM
13973 size,
13974 slot);
b6ac461a 13975 print_symbol_name (21, (const char *) sym_name);
0f03783c
NC
13976 }
13977 putchar ('\n');
13978 }
13979
13980 if (ext_data != NULL && ext_data < ext_data_end)
13981 {
13982 error (_("Data remains in the LTO symbol extension table\n"));
13983 goto fail;
13984 }
13985
13986 free (alloced_data);
13987 free (ext_data_orig);
13988 free (ext_name);
015dc7e1 13989 return true;
b9e920ec 13990
0f03783c
NC
13991 fail:
13992 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
13993 free (alloced_data);
13994 free (ext_data_orig);
13995 free (ext_name);
015dc7e1 13996 return false;
0f03783c
NC
13997}
13998
13999/* Display LTO symbol tables. */
14000
015dc7e1 14001static bool
0f03783c
NC
14002process_lto_symbol_tables (Filedata * filedata)
14003{
14004 Elf_Internal_Shdr * section;
14005 unsigned int i;
015dc7e1 14006 bool res = true;
0f03783c
NC
14007
14008 if (!do_lto_syms)
015dc7e1 14009 return true;
0f03783c
NC
14010
14011 if (filedata->section_headers == NULL)
015dc7e1 14012 return true;
0f03783c
NC
14013
14014 for (i = 0, section = filedata->section_headers;
14015 i < filedata->file_header.e_shnum;
14016 i++, section++)
84714f86
AM
14017 if (section_name_valid (filedata, section)
14018 && startswith (section_name (filedata, section), ".gnu.lto_.symtab."))
0f03783c
NC
14019 res &= display_lto_symtab (filedata, section);
14020
b9e920ec 14021 return res;
0f03783c
NC
14022}
14023
b6ac461a
NC
14024static void
14025print_symbol_table_heading (void)
14026{
14027 /* FIXME: We should store the size of each field in the display in a table and
14028 then use the values inside print_symbol(), instead of that function using
14029 hard coded constants. */
14030 if (is_32bit_elf)
14031 {
14032 if (extra_sym_info)
14033 {
14034 printf (_(" Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]\n"));
14035 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |---8--| |----13.....| |........... */
14036 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 (.text) get_sections */
14037 }
14038 else if (do_wide)
14039 {
14040 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
14041 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |-4| |........... */
14042 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 get_sections */
14043 }
14044 else
14045 {
14046 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
14047 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |-4| |------------29-------------| */
14048 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 get_sections */
14049 }
14050 }
14051 else
14052 {
14053 if (extra_sym_info)
14054 {
14055 printf (_(" Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]\n"));
14056 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-------14---| |..... */
14057 /* eg: 2: 0000000000000000 0 FUNC LOCAL DEFAULT 1 (.text) .very_long_function_name */
14058
14059 }
14060 else if (do_wide)
14061 {
14062 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
14063 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-4| |........... */
14064 /* eg: 5: 0000000000000000 14 FUNC LOCAL DEFAULT 1 very_long_function_name */
14065 }
14066 else
14067 {
14068 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
14069 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-4| |--------21---------| */
14070 /* eg: 5: 0000000000000000 14 FUNC LOCAL DEFAULT 1 very_long_functi[...] */
14071 }
14072 }
14073}
14074
10ca4b04 14075/* Dump the symbol table. */
0f03783c 14076
015dc7e1 14077static bool
10ca4b04
L
14078process_symbol_table (Filedata * filedata)
14079{
14080 Elf_Internal_Shdr * section;
f16a9783 14081
10ca4b04 14082 if (!do_syms && !do_dyn_syms && !do_histogram)
015dc7e1 14083 return true;
6bd1a22c 14084
978c4450 14085 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
6bd1a22c
L
14086 && do_syms
14087 && do_using_dynamic
978c4450
AM
14088 && filedata->dynamic_strings != NULL
14089 && filedata->dynamic_symbols != NULL)
6bd1a22c 14090 {
26c527e6 14091 uint64_t si;
6bd1a22c 14092
ca0e11aa
NC
14093 if (filedata->is_separate)
14094 {
26c527e6
AM
14095 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table"
14096 " contains %" PRIu64 " entry:\n",
14097 "\nIn linked file '%s' the dynamic symbol table"
14098 " contains %" PRIu64 " entries:\n",
ca0e11aa
NC
14099 filedata->num_dynamic_syms),
14100 filedata->file_name,
14101 filedata->num_dynamic_syms);
14102 }
14103 else
14104 {
26c527e6
AM
14105 printf (ngettext ("\nSymbol table for image contains %" PRIu64
14106 " entry:\n",
14107 "\nSymbol table for image contains %" PRIu64
14108 " entries:\n",
ca0e11aa
NC
14109 filedata->num_dynamic_syms),
14110 filedata->num_dynamic_syms);
14111 }
b6ac461a
NC
14112
14113 print_symbol_table_heading ();
6bd1a22c 14114
978c4450 14115 for (si = 0; si < filedata->num_dynamic_syms; si++)
b6ac461a
NC
14116 print_symbol (filedata, si, filedata->dynamic_symbols, NULL,
14117 filedata->dynamic_strings,
14118 filedata->dynamic_strings_length);
252b5132 14119 }
8b73c356 14120 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
dda8d76d 14121 && filedata->section_headers != NULL)
252b5132 14122 {
b34976b6 14123 unsigned int i;
252b5132 14124
dda8d76d
NC
14125 for (i = 0, section = filedata->section_headers;
14126 i < filedata->file_header.e_shnum;
252b5132
RH
14127 i++, section++)
14128 {
2cf0635d 14129 char * strtab = NULL;
26c527e6 14130 uint64_t strtab_size = 0;
2cf0635d 14131 Elf_Internal_Sym * symtab;
26c527e6 14132 uint64_t si, num_syms;
252b5132 14133
2c610e4b
L
14134 if ((section->sh_type != SHT_SYMTAB
14135 && section->sh_type != SHT_DYNSYM)
14136 || (!do_syms
14137 && section->sh_type == SHT_SYMTAB))
252b5132
RH
14138 continue;
14139
dd24e3da
NC
14140 if (section->sh_entsize == 0)
14141 {
14142 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
dda8d76d 14143 printable_section_name (filedata, section));
dd24e3da
NC
14144 continue;
14145 }
14146
d3a49aa8 14147 num_syms = section->sh_size / section->sh_entsize;
ca0e11aa
NC
14148
14149 if (filedata->is_separate)
26c527e6
AM
14150 printf (ngettext ("\nIn linked file '%s' symbol section '%s'"
14151 " contains %" PRIu64 " entry:\n",
14152 "\nIn linked file '%s' symbol section '%s'"
14153 " contains %" PRIu64 " entries:\n",
ca0e11aa
NC
14154 num_syms),
14155 filedata->file_name,
14156 printable_section_name (filedata, section),
14157 num_syms);
14158 else
26c527e6
AM
14159 printf (ngettext ("\nSymbol table '%s' contains %" PRIu64
14160 " entry:\n",
14161 "\nSymbol table '%s' contains %" PRIu64
14162 " entries:\n",
ca0e11aa
NC
14163 num_syms),
14164 printable_section_name (filedata, section),
14165 num_syms);
dd24e3da 14166
b6ac461a 14167 print_symbol_table_heading ();
252b5132 14168
4de91c10 14169 symtab = get_elf_symbols (filedata, section, & num_syms);
252b5132
RH
14170 if (symtab == NULL)
14171 continue;
14172
dda8d76d 14173 if (section->sh_link == filedata->file_header.e_shstrndx)
c256ffe7 14174 {
dda8d76d
NC
14175 strtab = filedata->string_table;
14176 strtab_size = filedata->string_table_length;
c256ffe7 14177 }
dda8d76d 14178 else if (section->sh_link < filedata->file_header.e_shnum)
252b5132 14179 {
2cf0635d 14180 Elf_Internal_Shdr * string_sec;
252b5132 14181
dda8d76d 14182 string_sec = filedata->section_headers + section->sh_link;
252b5132 14183
dda8d76d 14184 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
3f5e193b
NC
14185 1, string_sec->sh_size,
14186 _("string table"));
c256ffe7 14187 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
252b5132
RH
14188 }
14189
10ca4b04 14190 for (si = 0; si < num_syms; si++)
b6ac461a
NC
14191 print_symbol (filedata, si, symtab, section,
14192 strtab, strtab_size);
252b5132
RH
14193
14194 free (symtab);
dda8d76d 14195 if (strtab != filedata->string_table)
252b5132
RH
14196 free (strtab);
14197 }
14198 }
14199 else if (do_syms)
14200 printf
14201 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
14202
978c4450 14203 if (do_histogram && filedata->buckets != NULL)
252b5132 14204 {
26c527e6
AM
14205 uint64_t *lengths;
14206 uint64_t *counts;
14207 uint64_t hn;
625d49fc 14208 uint64_t si;
26c527e6
AM
14209 uint64_t maxlength = 0;
14210 uint64_t nzero_counts = 0;
14211 uint64_t nsyms = 0;
6bd6a03d 14212 char *visited;
252b5132 14213
d3a49aa8 14214 printf (ngettext ("\nHistogram for bucket list length "
26c527e6 14215 "(total of %" PRIu64 " bucket):\n",
d3a49aa8 14216 "\nHistogram for bucket list length "
26c527e6
AM
14217 "(total of %" PRIu64 " buckets):\n",
14218 filedata->nbuckets),
14219 filedata->nbuckets);
252b5132 14220
26c527e6 14221 lengths = calloc (filedata->nbuckets, sizeof (*lengths));
252b5132
RH
14222 if (lengths == NULL)
14223 {
8b73c356 14224 error (_("Out of memory allocating space for histogram buckets\n"));
fd486f32 14225 goto err_out;
252b5132 14226 }
978c4450
AM
14227 visited = xcmalloc (filedata->nchains, 1);
14228 memset (visited, 0, filedata->nchains);
8b73c356
NC
14229
14230 printf (_(" Length Number %% of total Coverage\n"));
978c4450 14231 for (hn = 0; hn < filedata->nbuckets; ++hn)
252b5132 14232 {
978c4450 14233 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
252b5132 14234 {
b34976b6 14235 ++nsyms;
252b5132 14236 if (maxlength < ++lengths[hn])
b34976b6 14237 ++maxlength;
978c4450 14238 if (si >= filedata->nchains || visited[si])
6bd6a03d
AM
14239 {
14240 error (_("histogram chain is corrupt\n"));
14241 break;
14242 }
14243 visited[si] = 1;
252b5132
RH
14244 }
14245 }
6bd6a03d 14246 free (visited);
252b5132 14247
26c527e6 14248 counts = calloc (maxlength + 1, sizeof (*counts));
252b5132
RH
14249 if (counts == NULL)
14250 {
b2e951ec 14251 free (lengths);
8b73c356 14252 error (_("Out of memory allocating space for histogram counts\n"));
fd486f32 14253 goto err_out;
252b5132
RH
14254 }
14255
978c4450 14256 for (hn = 0; hn < filedata->nbuckets; ++hn)
b34976b6 14257 ++counts[lengths[hn]];
252b5132 14258
978c4450 14259 if (filedata->nbuckets > 0)
252b5132 14260 {
26c527e6
AM
14261 uint64_t i;
14262 printf (" 0 %-10" PRIu64 " (%5.1f%%)\n",
978c4450 14263 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
66543521 14264 for (i = 1; i <= maxlength; ++i)
103f02d3 14265 {
66543521 14266 nzero_counts += counts[i] * i;
26c527e6 14267 printf ("%7" PRIu64 " %-10" PRIu64 " (%5.1f%%) %5.1f%%\n",
978c4450 14268 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
103f02d3
UD
14269 (nzero_counts * 100.0) / nsyms);
14270 }
252b5132
RH
14271 }
14272
14273 free (counts);
14274 free (lengths);
14275 }
14276
978c4450
AM
14277 free (filedata->buckets);
14278 filedata->buckets = NULL;
14279 filedata->nbuckets = 0;
14280 free (filedata->chains);
14281 filedata->chains = NULL;
252b5132 14282
978c4450 14283 if (do_histogram && filedata->gnubuckets != NULL)
fdc90cb4 14284 {
26c527e6
AM
14285 uint64_t *lengths;
14286 uint64_t *counts;
14287 uint64_t hn;
14288 uint64_t maxlength = 0;
14289 uint64_t nzero_counts = 0;
14290 uint64_t nsyms = 0;
fdc90cb4 14291
f16a9783 14292 printf (ngettext ("\nHistogram for `%s' bucket list length "
26c527e6 14293 "(total of %" PRIu64 " bucket):\n",
f16a9783 14294 "\nHistogram for `%s' bucket list length "
26c527e6
AM
14295 "(total of %" PRIu64 " buckets):\n",
14296 filedata->ngnubuckets),
978c4450 14297 GNU_HASH_SECTION_NAME (filedata),
26c527e6 14298 filedata->ngnubuckets);
8b73c356 14299
26c527e6 14300 lengths = calloc (filedata->ngnubuckets, sizeof (*lengths));
fdc90cb4
JJ
14301 if (lengths == NULL)
14302 {
8b73c356 14303 error (_("Out of memory allocating space for gnu histogram buckets\n"));
fd486f32 14304 goto err_out;
fdc90cb4
JJ
14305 }
14306
fdc90cb4
JJ
14307 printf (_(" Length Number %% of total Coverage\n"));
14308
978c4450
AM
14309 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
14310 if (filedata->gnubuckets[hn] != 0)
fdc90cb4 14311 {
625d49fc 14312 uint64_t off, length = 1;
fdc90cb4 14313
978c4450 14314 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
071436c6 14315 /* PR 17531 file: 010-77222-0.004. */
978c4450
AM
14316 off < filedata->ngnuchains
14317 && (filedata->gnuchains[off] & 1) == 0;
071436c6 14318 ++off)
fdc90cb4
JJ
14319 ++length;
14320 lengths[hn] = length;
14321 if (length > maxlength)
14322 maxlength = length;
14323 nsyms += length;
14324 }
14325
26c527e6 14326 counts = calloc (maxlength + 1, sizeof (*counts));
fdc90cb4
JJ
14327 if (counts == NULL)
14328 {
b2e951ec 14329 free (lengths);
8b73c356 14330 error (_("Out of memory allocating space for gnu histogram counts\n"));
fd486f32 14331 goto err_out;
fdc90cb4
JJ
14332 }
14333
978c4450 14334 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
fdc90cb4
JJ
14335 ++counts[lengths[hn]];
14336
978c4450 14337 if (filedata->ngnubuckets > 0)
fdc90cb4 14338 {
26c527e6
AM
14339 uint64_t j;
14340 printf (" 0 %-10" PRIu64 " (%5.1f%%)\n",
978c4450 14341 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
fdc90cb4
JJ
14342 for (j = 1; j <= maxlength; ++j)
14343 {
14344 nzero_counts += counts[j] * j;
26c527e6 14345 printf ("%7" PRIu64 " %-10" PRIu64 " (%5.1f%%) %5.1f%%\n",
978c4450 14346 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
fdc90cb4
JJ
14347 (nzero_counts * 100.0) / nsyms);
14348 }
14349 }
14350
14351 free (counts);
14352 free (lengths);
fdc90cb4 14353 }
978c4450
AM
14354 free (filedata->gnubuckets);
14355 filedata->gnubuckets = NULL;
14356 filedata->ngnubuckets = 0;
14357 free (filedata->gnuchains);
14358 filedata->gnuchains = NULL;
14359 filedata->ngnuchains = 0;
14360 free (filedata->mipsxlat);
14361 filedata->mipsxlat = NULL;
015dc7e1 14362 return true;
fd486f32
AM
14363
14364 err_out:
978c4450
AM
14365 free (filedata->gnubuckets);
14366 filedata->gnubuckets = NULL;
14367 filedata->ngnubuckets = 0;
14368 free (filedata->gnuchains);
14369 filedata->gnuchains = NULL;
14370 filedata->ngnuchains = 0;
14371 free (filedata->mipsxlat);
14372 filedata->mipsxlat = NULL;
14373 free (filedata->buckets);
14374 filedata->buckets = NULL;
14375 filedata->nbuckets = 0;
14376 free (filedata->chains);
14377 filedata->chains = NULL;
015dc7e1 14378 return false;
252b5132
RH
14379}
14380
015dc7e1 14381static bool
ca0e11aa 14382process_syminfo (Filedata * filedata)
252b5132 14383{
b4c96d0d 14384 unsigned int i;
252b5132 14385
978c4450 14386 if (filedata->dynamic_syminfo == NULL
252b5132
RH
14387 || !do_dynamic)
14388 /* No syminfo, this is ok. */
015dc7e1 14389 return true;
252b5132
RH
14390
14391 /* There better should be a dynamic symbol section. */
978c4450 14392 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
015dc7e1 14393 return false;
252b5132 14394
ca0e11aa 14395 if (filedata->is_separate)
26c527e6
AM
14396 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset %#" PRIx64 " contains %d entry:\n",
14397 "\nIn linked file '%s: the dynamic info segment at offset %#" PRIx64 " contains %d entries:\n",
ca0e11aa
NC
14398 filedata->dynamic_syminfo_nent),
14399 filedata->file_name,
14400 filedata->dynamic_syminfo_offset,
14401 filedata->dynamic_syminfo_nent);
14402 else
26c527e6
AM
14403 printf (ngettext ("\nDynamic info segment at offset %#" PRIx64
14404 " contains %d entry:\n",
14405 "\nDynamic info segment at offset %#" PRIx64
14406 " contains %d entries:\n",
978c4450 14407 filedata->dynamic_syminfo_nent),
ca0e11aa
NC
14408 filedata->dynamic_syminfo_offset,
14409 filedata->dynamic_syminfo_nent);
252b5132
RH
14410
14411 printf (_(" Num: Name BoundTo Flags\n"));
978c4450 14412 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
252b5132 14413 {
978c4450 14414 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
252b5132 14415
31104126 14416 printf ("%4d: ", i);
978c4450 14417 if (i >= filedata->num_dynamic_syms)
4082ef84 14418 printf (_("<corrupt index>"));
84714f86 14419 else if (valid_dynamic_name (filedata, filedata->dynamic_symbols[i].st_name))
b6ac461a 14420 print_symbol_name (30, get_dynamic_name (filedata,
978c4450 14421 filedata->dynamic_symbols[i].st_name));
d79b3d50 14422 else
978c4450 14423 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
31104126 14424 putchar (' ');
252b5132 14425
978c4450 14426 switch (filedata->dynamic_syminfo[i].si_boundto)
252b5132
RH
14427 {
14428 case SYMINFO_BT_SELF:
14429 fputs ("SELF ", stdout);
14430 break;
14431 case SYMINFO_BT_PARENT:
14432 fputs ("PARENT ", stdout);
14433 break;
14434 default:
978c4450
AM
14435 if (filedata->dynamic_syminfo[i].si_boundto > 0
14436 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
84714f86 14437 && valid_dynamic_name (filedata,
978c4450 14438 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
31104126 14439 {
b6ac461a 14440 print_symbol_name (10, get_dynamic_name (filedata,
978c4450 14441 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
31104126
NC
14442 putchar (' ' );
14443 }
252b5132 14444 else
978c4450 14445 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
252b5132
RH
14446 break;
14447 }
14448
14449 if (flags & SYMINFO_FLG_DIRECT)
14450 printf (" DIRECT");
14451 if (flags & SYMINFO_FLG_PASSTHRU)
14452 printf (" PASSTHRU");
14453 if (flags & SYMINFO_FLG_COPY)
14454 printf (" COPY");
14455 if (flags & SYMINFO_FLG_LAZYLOAD)
14456 printf (" LAZYLOAD");
14457
14458 puts ("");
14459 }
14460
015dc7e1 14461 return true;
252b5132
RH
14462}
14463
75802ccb
CE
14464/* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
14465 is contained by the region START .. END. The types of ADDR, START
14466 and END should all be the same. Note both ADDR + NELEM and END
14467 point to just beyond the end of the regions that are being tested. */
14468#define IN_RANGE(START,END,ADDR,NELEM) \
14469 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
b32e566b 14470
cf13d699
NC
14471/* Check to see if the given reloc needs to be handled in a target specific
14472 manner. If so then process the reloc and return TRUE otherwise return
f84ce13b
NC
14473 FALSE.
14474
14475 If called with reloc == NULL, then this is a signal that reloc processing
14476 for the current section has finished, and any saved state should be
14477 discarded. */
09c11c86 14478
015dc7e1 14479static bool
26c527e6
AM
14480target_specific_reloc_handling (Filedata *filedata,
14481 Elf_Internal_Rela *reloc,
14482 unsigned char *start,
14483 unsigned char *end,
14484 Elf_Internal_Sym *symtab,
14485 uint64_t num_syms)
252b5132 14486{
f84ce13b 14487 unsigned int reloc_type = 0;
26c527e6 14488 uint64_t sym_index = 0;
f84ce13b
NC
14489
14490 if (reloc)
14491 {
dda8d76d 14492 reloc_type = get_reloc_type (filedata, reloc->r_info);
f84ce13b
NC
14493 sym_index = get_reloc_symindex (reloc->r_info);
14494 }
252b5132 14495
dda8d76d 14496 switch (filedata->file_header.e_machine)
252b5132 14497 {
76244462 14498 case EM_LOONGARCH:
14499 {
14500 switch (reloc_type)
14501 {
14502 /* For .uleb128 .LFE1-.LFB1, loongarch write 0 to object file
14503 at assembly time. */
14504 case 107: /* R_LARCH_ADD_ULEB128. */
14505 case 108: /* R_LARCH_SUB_ULEB128. */
14506 {
d3f34076 14507 uint64_t value = 0;
76244462 14508 unsigned int reloc_size = 0;
14509 int leb_ret = 0;
14510
89c70cd3
AM
14511 if (reloc->r_offset < (size_t) (end - start))
14512 value = read_leb128 (start + reloc->r_offset, end, false,
14513 &reloc_size, &leb_ret);
76244462 14514 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
14515 error (_("LoongArch ULEB128 field at 0x%lx contains invalid "
14516 "ULEB128 value\n"),
14517 (long) reloc->r_offset);
14518
74a965d8
AM
14519 else if (sym_index >= num_syms)
14520 error (_("%s reloc contains invalid symbol index "
14521 "%" PRIu64 "\n"),
14522 (reloc_type == 107
14523 ? "R_LARCH_ADD_ULEB128"
14524 : "R_LARCH_SUB_ULEB128"),
14525 sym_index);
14526 else
76244462 14527 {
74a965d8
AM
14528 if (reloc_type == 107)
14529 value += reloc->r_addend + symtab[sym_index].st_value;
14530 else
14531 value -= reloc->r_addend + symtab[sym_index].st_value;
14532
14533 /* Write uleb128 value to p. */
14534 bfd_byte *p = start + reloc->r_offset;
14535 do
14536 {
14537 bfd_byte c = value & 0x7f;
14538 value >>= 7;
14539 if (--reloc_size != 0)
14540 c |= 0x80;
14541 *p++ = c;
14542 }
14543 while (reloc_size);
76244462 14544 }
76244462 14545
14546 return true;
14547 }
14548 }
14549 break;
14550 }
14551
13761a11
NC
14552 case EM_MSP430:
14553 case EM_MSP430_OLD:
14554 {
14555 static Elf_Internal_Sym * saved_sym = NULL;
14556
f84ce13b
NC
14557 if (reloc == NULL)
14558 {
14559 saved_sym = NULL;
015dc7e1 14560 return true;
f84ce13b
NC
14561 }
14562
13761a11
NC
14563 switch (reloc_type)
14564 {
14565 case 10: /* R_MSP430_SYM_DIFF */
7d81bc93 14566 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
dda8d76d 14567 if (uses_msp430x_relocs (filedata))
13761a11 14568 break;
1a0670f3 14569 /* Fall through. */
13761a11 14570 case 21: /* R_MSP430X_SYM_DIFF */
7d81bc93 14571 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
f84ce13b
NC
14572 /* PR 21139. */
14573 if (sym_index >= num_syms)
74a965d8
AM
14574 error (_("%s reloc contains invalid symbol index "
14575 "%" PRIu64 "\n"), "MSP430 SYM_DIFF", sym_index);
f84ce13b
NC
14576 else
14577 saved_sym = symtab + sym_index;
015dc7e1 14578 return true;
13761a11
NC
14579
14580 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
14581 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
14582 goto handle_sym_diff;
0b4362b0 14583
13761a11
NC
14584 case 5: /* R_MSP430_16_BYTE */
14585 case 9: /* R_MSP430_8 */
7d81bc93 14586 case 11: /* R_MSP430_GNU_SET_ULEB128 */
dda8d76d 14587 if (uses_msp430x_relocs (filedata))
13761a11
NC
14588 break;
14589 goto handle_sym_diff;
14590
14591 case 2: /* R_MSP430_ABS16 */
14592 case 15: /* R_MSP430X_ABS16 */
7d81bc93 14593 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
dda8d76d 14594 if (! uses_msp430x_relocs (filedata))
13761a11
NC
14595 break;
14596 goto handle_sym_diff;
0b4362b0 14597
13761a11
NC
14598 handle_sym_diff:
14599 if (saved_sym != NULL)
14600 {
625d49fc 14601 uint64_t value;
5a805384 14602 unsigned int reloc_size = 0;
7d81bc93
JL
14603 int leb_ret = 0;
14604 switch (reloc_type)
14605 {
14606 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
14607 reloc_size = 4;
14608 break;
14609 case 11: /* R_MSP430_GNU_SET_ULEB128 */
14610 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
5a805384 14611 if (reloc->r_offset < (size_t) (end - start))
015dc7e1 14612 read_leb128 (start + reloc->r_offset, end, false,
5a805384 14613 &reloc_size, &leb_ret);
7d81bc93
JL
14614 break;
14615 default:
14616 reloc_size = 2;
14617 break;
14618 }
13761a11 14619
5a805384 14620 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
26c527e6
AM
14621 error (_("MSP430 ULEB128 field at %#" PRIx64
14622 " contains invalid ULEB128 value\n"),
14623 reloc->r_offset);
7d81bc93 14624 else if (sym_index >= num_syms)
74a965d8
AM
14625 error (_("%s reloc contains invalid symbol index "
14626 "%" PRIu64 "\n"), "MSP430", sym_index);
03f7786e 14627 else
f84ce13b
NC
14628 {
14629 value = reloc->r_addend + (symtab[sym_index].st_value
14630 - saved_sym->st_value);
14631
b32e566b 14632 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
f84ce13b 14633 byte_put (start + reloc->r_offset, value, reloc_size);
b32e566b
NC
14634 else
14635 /* PR 21137 */
26c527e6
AM
14636 error (_("MSP430 sym diff reloc contains invalid offset: "
14637 "%#" PRIx64 "\n"),
14638 reloc->r_offset);
f84ce13b 14639 }
13761a11
NC
14640
14641 saved_sym = NULL;
015dc7e1 14642 return true;
13761a11
NC
14643 }
14644 break;
14645
14646 default:
14647 if (saved_sym != NULL)
071436c6 14648 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
13761a11
NC
14649 break;
14650 }
14651 break;
14652 }
14653
cf13d699
NC
14654 case EM_MN10300:
14655 case EM_CYGNUS_MN10300:
14656 {
14657 static Elf_Internal_Sym * saved_sym = NULL;
252b5132 14658
f84ce13b
NC
14659 if (reloc == NULL)
14660 {
14661 saved_sym = NULL;
015dc7e1 14662 return true;
f84ce13b
NC
14663 }
14664
cf13d699
NC
14665 switch (reloc_type)
14666 {
14667 case 34: /* R_MN10300_ALIGN */
015dc7e1 14668 return true;
cf13d699 14669 case 33: /* R_MN10300_SYM_DIFF */
f84ce13b 14670 if (sym_index >= num_syms)
74a965d8
AM
14671 error (_("%s reloc contains invalid symbol index "
14672 "%" PRIu64 "\n"), "MN10300_SYM_DIFF", sym_index);
f84ce13b
NC
14673 else
14674 saved_sym = symtab + sym_index;
015dc7e1 14675 return true;
f84ce13b 14676
cf13d699
NC
14677 case 1: /* R_MN10300_32 */
14678 case 2: /* R_MN10300_16 */
14679 if (saved_sym != NULL)
14680 {
03f7786e 14681 int reloc_size = reloc_type == 1 ? 4 : 2;
625d49fc 14682 uint64_t value;
252b5132 14683
f84ce13b 14684 if (sym_index >= num_syms)
74a965d8
AM
14685 error (_("%s reloc contains invalid symbol index "
14686 "%" PRIu64 "\n"), "MN10300", sym_index);
03f7786e 14687 else
f84ce13b
NC
14688 {
14689 value = reloc->r_addend + (symtab[sym_index].st_value
14690 - saved_sym->st_value);
14691
b32e566b 14692 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
f84ce13b 14693 byte_put (start + reloc->r_offset, value, reloc_size);
b32e566b 14694 else
26c527e6
AM
14695 error (_("MN10300 sym diff reloc contains invalid offset:"
14696 " %#" PRIx64 "\n"),
14697 reloc->r_offset);
f84ce13b 14698 }
252b5132 14699
cf13d699 14700 saved_sym = NULL;
015dc7e1 14701 return true;
cf13d699
NC
14702 }
14703 break;
14704 default:
14705 if (saved_sym != NULL)
071436c6 14706 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
cf13d699
NC
14707 break;
14708 }
14709 break;
14710 }
6ff71e76
NC
14711
14712 case EM_RL78:
14713 {
625d49fc
AM
14714 static uint64_t saved_sym1 = 0;
14715 static uint64_t saved_sym2 = 0;
14716 static uint64_t value;
6ff71e76 14717
f84ce13b
NC
14718 if (reloc == NULL)
14719 {
14720 saved_sym1 = saved_sym2 = 0;
015dc7e1 14721 return true;
f84ce13b
NC
14722 }
14723
6ff71e76
NC
14724 switch (reloc_type)
14725 {
14726 case 0x80: /* R_RL78_SYM. */
14727 saved_sym1 = saved_sym2;
f84ce13b 14728 if (sym_index >= num_syms)
74a965d8
AM
14729 error (_("%s reloc contains invalid symbol index "
14730 "%" PRIu64 "\n"), "RL78_SYM", sym_index);
f84ce13b
NC
14731 else
14732 {
14733 saved_sym2 = symtab[sym_index].st_value;
14734 saved_sym2 += reloc->r_addend;
14735 }
015dc7e1 14736 return true;
6ff71e76
NC
14737
14738 case 0x83: /* R_RL78_OPsub. */
14739 value = saved_sym1 - saved_sym2;
14740 saved_sym2 = saved_sym1 = 0;
015dc7e1 14741 return true;
6ff71e76
NC
14742 break;
14743
14744 case 0x41: /* R_RL78_ABS32. */
b32e566b 14745 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
03f7786e 14746 byte_put (start + reloc->r_offset, value, 4);
b32e566b 14747 else
26c527e6
AM
14748 error (_("RL78 sym diff reloc contains invalid offset: "
14749 "%#" PRIx64 "\n"),
14750 reloc->r_offset);
6ff71e76 14751 value = 0;
015dc7e1 14752 return true;
6ff71e76
NC
14753
14754 case 0x43: /* R_RL78_ABS16. */
b32e566b 14755 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
03f7786e 14756 byte_put (start + reloc->r_offset, value, 2);
b32e566b 14757 else
26c527e6
AM
14758 error (_("RL78 sym diff reloc contains invalid offset: "
14759 "%#" PRIx64 "\n"),
14760 reloc->r_offset);
6ff71e76 14761 value = 0;
015dc7e1 14762 return true;
6ff71e76
NC
14763
14764 default:
14765 break;
14766 }
14767 break;
14768 }
252b5132
RH
14769 }
14770
015dc7e1 14771 return false;
252b5132
RH
14772}
14773
aca88567
NC
14774/* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
14775 DWARF debug sections. This is a target specific test. Note - we do not
14776 go through the whole including-target-headers-multiple-times route, (as
14777 we have already done with <elf/h8.h>) because this would become very
14778 messy and even then this function would have to contain target specific
14779 information (the names of the relocs instead of their numeric values).
14780 FIXME: This is not the correct way to solve this problem. The proper way
14781 is to have target specific reloc sizing and typing functions created by
14782 the reloc-macros.h header, in the same way that it already creates the
14783 reloc naming functions. */
14784
015dc7e1 14785static bool
dda8d76d 14786is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
aca88567 14787{
d347c9df 14788 /* Please keep this table alpha-sorted for ease of visual lookup. */
dda8d76d 14789 switch (filedata->file_header.e_machine)
aca88567 14790 {
41e92641 14791 case EM_386:
22abe556 14792 case EM_IAMCU:
41e92641 14793 return reloc_type == 1; /* R_386_32. */
aca88567
NC
14794 case EM_68K:
14795 return reloc_type == 1; /* R_68K_32. */
f954747f
AM
14796 case EM_860:
14797 return reloc_type == 1; /* R_860_32. */
14798 case EM_960:
14799 return reloc_type == 2; /* R_960_32. */
a06ea964 14800 case EM_AARCH64:
9282b95a
JW
14801 return (reloc_type == 258
14802 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
aca4efc7
JM
14803 case EM_BPF:
14804 return reloc_type == 11; /* R_BPF_DATA_32 */
d347c9df
PS
14805 case EM_ADAPTEVA_EPIPHANY:
14806 return reloc_type == 3;
aca88567 14807 case EM_ALPHA:
137b6b5f 14808 return reloc_type == 1; /* R_ALPHA_REFLONG. */
41e92641
NC
14809 case EM_ARC:
14810 return reloc_type == 1; /* R_ARC_32. */
886a2506
NC
14811 case EM_ARC_COMPACT:
14812 case EM_ARC_COMPACT2:
b5c37946
SJ
14813 case EM_ARC_COMPACT3:
14814 case EM_ARC_COMPACT3_64:
886a2506 14815 return reloc_type == 4; /* R_ARC_32. */
41e92641
NC
14816 case EM_ARM:
14817 return reloc_type == 2; /* R_ARM_ABS32 */
cb8f3167 14818 case EM_AVR_OLD:
aca88567
NC
14819 case EM_AVR:
14820 return reloc_type == 1;
14821 case EM_BLACKFIN:
14822 return reloc_type == 0x12; /* R_byte4_data. */
14823 case EM_CRIS:
14824 return reloc_type == 3; /* R_CRIS_32. */
14825 case EM_CR16:
14826 return reloc_type == 3; /* R_CR16_NUM32. */
14827 case EM_CRX:
14828 return reloc_type == 15; /* R_CRX_NUM32. */
b8891f8d
AJ
14829 case EM_CSKY:
14830 return reloc_type == 1; /* R_CKCORE_ADDR32. */
aca88567
NC
14831 case EM_CYGNUS_FRV:
14832 return reloc_type == 1;
41e92641
NC
14833 case EM_CYGNUS_D10V:
14834 case EM_D10V:
14835 return reloc_type == 6; /* R_D10V_32. */
aca88567
NC
14836 case EM_CYGNUS_D30V:
14837 case EM_D30V:
14838 return reloc_type == 12; /* R_D30V_32_NORMAL. */
41e92641
NC
14839 case EM_DLX:
14840 return reloc_type == 3; /* R_DLX_RELOC_32. */
aca88567
NC
14841 case EM_CYGNUS_FR30:
14842 case EM_FR30:
14843 return reloc_type == 3; /* R_FR30_32. */
3f8107ab
AM
14844 case EM_FT32:
14845 return reloc_type == 1; /* R_FT32_32. */
aca88567
NC
14846 case EM_H8S:
14847 case EM_H8_300:
14848 case EM_H8_300H:
14849 return reloc_type == 1; /* R_H8_DIR32. */
3730236a 14850 case EM_IA_64:
262cdac7
AM
14851 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
14852 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
14853 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
14854 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
aca88567
NC
14855 case EM_IP2K_OLD:
14856 case EM_IP2K:
14857 return reloc_type == 2; /* R_IP2K_32. */
14858 case EM_IQ2000:
14859 return reloc_type == 2; /* R_IQ2000_32. */
6e712424
PI
14860 case EM_KVX:
14861 return reloc_type == 2; /* R_KVX_32. */
84e94c90
NC
14862 case EM_LATTICEMICO32:
14863 return reloc_type == 3; /* R_LM32_32. */
e9a0721f 14864 case EM_LOONGARCH:
14865 return reloc_type == 1; /* R_LARCH_32. */
ff7eeb89 14866 case EM_M32C_OLD:
aca88567
NC
14867 case EM_M32C:
14868 return reloc_type == 3; /* R_M32C_32. */
14869 case EM_M32R:
14870 return reloc_type == 34; /* R_M32R_32_RELA. */
adec12c1
AM
14871 case EM_68HC11:
14872 case EM_68HC12:
14873 return reloc_type == 6; /* R_M68HC11_32. */
7b4ae824 14874 case EM_S12Z:
2849d19f
JD
14875 return reloc_type == 7 || /* R_S12Z_EXT32 */
14876 reloc_type == 6; /* R_S12Z_CW32. */
aca88567
NC
14877 case EM_MCORE:
14878 return reloc_type == 1; /* R_MCORE_ADDR32. */
14879 case EM_CYGNUS_MEP:
14880 return reloc_type == 4; /* R_MEP_32. */
a3c62988
NC
14881 case EM_METAG:
14882 return reloc_type == 2; /* R_METAG_ADDR32. */
137b6b5f
AM
14883 case EM_MICROBLAZE:
14884 return reloc_type == 1; /* R_MICROBLAZE_32. */
aca88567
NC
14885 case EM_MIPS:
14886 return reloc_type == 2; /* R_MIPS_32. */
14887 case EM_MMIX:
14888 return reloc_type == 4; /* R_MMIX_32. */
14889 case EM_CYGNUS_MN10200:
14890 case EM_MN10200:
14891 return reloc_type == 1; /* R_MN10200_32. */
14892 case EM_CYGNUS_MN10300:
14893 case EM_MN10300:
14894 return reloc_type == 1; /* R_MN10300_32. */
5506d11a
AM
14895 case EM_MOXIE:
14896 return reloc_type == 1; /* R_MOXIE_32. */
aca88567
NC
14897 case EM_MSP430_OLD:
14898 case EM_MSP430:
13761a11 14899 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
aca88567
NC
14900 case EM_MT:
14901 return reloc_type == 2; /* R_MT_32. */
35c08157 14902 case EM_NDS32:
81c5e376 14903 return reloc_type == 20; /* R_NDS32_32_RELA. */
3e0873ac 14904 case EM_ALTERA_NIOS2:
36591ba1 14905 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
3e0873ac
NC
14906 case EM_NIOS32:
14907 return reloc_type == 1; /* R_NIOS_32. */
73589c9d
CS
14908 case EM_OR1K:
14909 return reloc_type == 1; /* R_OR1K_32. */
aca88567 14910 case EM_PARISC:
9abca702 14911 return (reloc_type == 1 /* R_PARISC_DIR32. */
0df8ad28 14912 || reloc_type == 2 /* R_PARISC_DIR21L. */
5fda8eca 14913 || reloc_type == 41); /* R_PARISC_SECREL32. */
aca88567
NC
14914 case EM_PJ:
14915 case EM_PJ_OLD:
14916 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
14917 case EM_PPC64:
14918 return reloc_type == 1; /* R_PPC64_ADDR32. */
14919 case EM_PPC:
14920 return reloc_type == 1; /* R_PPC_ADDR32. */
2b100bb5
DD
14921 case EM_TI_PRU:
14922 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
e23eba97
NC
14923 case EM_RISCV:
14924 return reloc_type == 1; /* R_RISCV_32. */
99c513f6
DD
14925 case EM_RL78:
14926 return reloc_type == 1; /* R_RL78_DIR32. */
c7927a3c
NC
14927 case EM_RX:
14928 return reloc_type == 1; /* R_RX_DIR32. */
f954747f
AM
14929 case EM_S370:
14930 return reloc_type == 1; /* R_I370_ADDR31. */
aca88567
NC
14931 case EM_S390_OLD:
14932 case EM_S390:
14933 return reloc_type == 4; /* R_S390_32. */
41e92641
NC
14934 case EM_SCORE:
14935 return reloc_type == 8; /* R_SCORE_ABS32. */
aca88567
NC
14936 case EM_SH:
14937 return reloc_type == 1; /* R_SH_DIR32. */
14938 case EM_SPARC32PLUS:
14939 case EM_SPARCV9:
14940 case EM_SPARC:
14941 return reloc_type == 3 /* R_SPARC_32. */
14942 || reloc_type == 23; /* R_SPARC_UA32. */
a7dd7d05
AM
14943 case EM_SPU:
14944 return reloc_type == 6; /* R_SPU_ADDR32 */
40b36596
JM
14945 case EM_TI_C6000:
14946 return reloc_type == 1; /* R_C6000_ABS32. */
aa137e4d
NC
14947 case EM_TILEGX:
14948 return reloc_type == 2; /* R_TILEGX_32. */
14949 case EM_TILEPRO:
14950 return reloc_type == 1; /* R_TILEPRO_32. */
aca88567
NC
14951 case EM_CYGNUS_V850:
14952 case EM_V850:
14953 return reloc_type == 6; /* R_V850_ABS32. */
708e2187
NC
14954 case EM_V800:
14955 return reloc_type == 0x33; /* R_V810_WORD. */
aca88567
NC
14956 case EM_VAX:
14957 return reloc_type == 1; /* R_VAX_32. */
619ed720
EB
14958 case EM_VISIUM:
14959 return reloc_type == 3; /* R_VISIUM_32. */
f96bd6c2
PC
14960 case EM_WEBASSEMBLY:
14961 return reloc_type == 1; /* R_WASM32_32. */
aca88567 14962 case EM_X86_64:
8a9036a4 14963 case EM_L1OM:
7a9068fe 14964 case EM_K1OM:
aca88567 14965 return reloc_type == 10; /* R_X86_64_32. */
f6c1a2d5
NC
14966 case EM_XGATE:
14967 return reloc_type == 4; /* R_XGATE_32. */
aca88567
NC
14968 case EM_XSTORMY16:
14969 return reloc_type == 1; /* R_XSTROMY16_32. */
14970 case EM_XTENSA_OLD:
14971 case EM_XTENSA:
14972 return reloc_type == 1; /* R_XTENSA_32. */
6655dba2
SB
14973 case EM_Z80:
14974 return reloc_type == 6; /* R_Z80_32. */
aca88567 14975 default:
bee0ee85
NC
14976 {
14977 static unsigned int prev_warn = 0;
14978
14979 /* Avoid repeating the same warning multiple times. */
dda8d76d 14980 if (prev_warn != filedata->file_header.e_machine)
bee0ee85 14981 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
dda8d76d
NC
14982 filedata->file_header.e_machine);
14983 prev_warn = filedata->file_header.e_machine;
015dc7e1 14984 return false;
bee0ee85 14985 }
aca88567
NC
14986 }
14987}
14988
14989/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14990 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
14991
015dc7e1 14992static bool
dda8d76d 14993is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
aca88567 14994{
dda8d76d 14995 switch (filedata->file_header.e_machine)
d347c9df 14996 /* Please keep this table alpha-sorted for ease of visual lookup. */
aca88567 14997 {
41e92641 14998 case EM_386:
22abe556 14999 case EM_IAMCU:
3e0873ac 15000 return reloc_type == 2; /* R_386_PC32. */
aca88567 15001 case EM_68K:
3e0873ac 15002 return reloc_type == 4; /* R_68K_PC32. */
a06ea964
NC
15003 case EM_AARCH64:
15004 return reloc_type == 261; /* R_AARCH64_PREL32 */
cfb8c092
NC
15005 case EM_ADAPTEVA_EPIPHANY:
15006 return reloc_type == 6;
aca88567
NC
15007 case EM_ALPHA:
15008 return reloc_type == 10; /* R_ALPHA_SREL32. */
726c18e1
CZ
15009 case EM_ARC_COMPACT:
15010 case EM_ARC_COMPACT2:
b5c37946
SJ
15011 case EM_ARC_COMPACT3:
15012 case EM_ARC_COMPACT3_64:
726c18e1 15013 return reloc_type == 49; /* R_ARC_32_PCREL. */
41e92641 15014 case EM_ARM:
3e0873ac 15015 return reloc_type == 3; /* R_ARM_REL32 */
d347c9df
PS
15016 case EM_AVR_OLD:
15017 case EM_AVR:
15018 return reloc_type == 36; /* R_AVR_32_PCREL. */
98011207 15019 case EM_LOONGARCH:
15020 return reloc_type == 99; /* R_LARCH_32_PCREL. */
137b6b5f
AM
15021 case EM_MICROBLAZE:
15022 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
73589c9d
CS
15023 case EM_OR1K:
15024 return reloc_type == 9; /* R_OR1K_32_PCREL. */
aca88567 15025 case EM_PARISC:
85acf597 15026 return reloc_type == 9; /* R_PARISC_PCREL32. */
aca88567
NC
15027 case EM_PPC:
15028 return reloc_type == 26; /* R_PPC_REL32. */
15029 case EM_PPC64:
3e0873ac 15030 return reloc_type == 26; /* R_PPC64_REL32. */
25cbdcbb
AS
15031 case EM_RISCV:
15032 return reloc_type == 57; /* R_RISCV_32_PCREL. */
aca88567
NC
15033 case EM_S390_OLD:
15034 case EM_S390:
3e0873ac 15035 return reloc_type == 5; /* R_390_PC32. */
aca88567 15036 case EM_SH:
3e0873ac 15037 return reloc_type == 2; /* R_SH_REL32. */
aca88567
NC
15038 case EM_SPARC32PLUS:
15039 case EM_SPARCV9:
15040 case EM_SPARC:
3e0873ac 15041 return reloc_type == 6; /* R_SPARC_DISP32. */
a7dd7d05
AM
15042 case EM_SPU:
15043 return reloc_type == 13; /* R_SPU_REL32. */
aa137e4d
NC
15044 case EM_TILEGX:
15045 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
15046 case EM_TILEPRO:
15047 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
619ed720
EB
15048 case EM_VISIUM:
15049 return reloc_type == 6; /* R_VISIUM_32_PCREL */
aca88567 15050 case EM_X86_64:
8a9036a4 15051 case EM_L1OM:
7a9068fe 15052 case EM_K1OM:
3e0873ac 15053 return reloc_type == 2; /* R_X86_64_PC32. */
2057d69d
CZ
15054 case EM_VAX:
15055 return reloc_type == 4; /* R_VAX_PCREL32. */
2fcb9706
BW
15056 case EM_XTENSA_OLD:
15057 case EM_XTENSA:
15058 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
6e712424
PI
15059 case EM_KVX:
15060 return reloc_type == 7; /* R_KVX_32_PCREL */
aca88567
NC
15061 default:
15062 /* Do not abort or issue an error message here. Not all targets use
15063 pc-relative 32-bit relocs in their DWARF debug information and we
15064 have already tested for target coverage in is_32bit_abs_reloc. A
cf13d699
NC
15065 more helpful warning message will be generated by apply_relocations
15066 anyway, so just return. */
015dc7e1 15067 return false;
aca88567
NC
15068 }
15069}
15070
15071/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15072 a 64-bit absolute RELA relocation used in DWARF debug sections. */
15073
015dc7e1 15074static bool
dda8d76d 15075is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
aca88567 15076{
dda8d76d 15077 switch (filedata->file_header.e_machine)
aca88567 15078 {
a06ea964
NC
15079 case EM_AARCH64:
15080 return reloc_type == 257; /* R_AARCH64_ABS64. */
b5c37946
SJ
15081 case EM_ARC_COMPACT3_64:
15082 return reloc_type == 5; /* R_ARC_64. */
aca88567
NC
15083 case EM_ALPHA:
15084 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
3730236a 15085 case EM_IA_64:
262cdac7
AM
15086 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
15087 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
e9a0721f 15088 case EM_LOONGARCH:
15089 return reloc_type == 2; /* R_LARCH_64 */
3e0873ac
NC
15090 case EM_PARISC:
15091 return reloc_type == 80; /* R_PARISC_DIR64. */
aca88567
NC
15092 case EM_PPC64:
15093 return reloc_type == 38; /* R_PPC64_ADDR64. */
e23eba97
NC
15094 case EM_RISCV:
15095 return reloc_type == 2; /* R_RISCV_64. */
aca88567
NC
15096 case EM_SPARC32PLUS:
15097 case EM_SPARCV9:
15098 case EM_SPARC:
714da62f
NC
15099 return reloc_type == 32 /* R_SPARC_64. */
15100 || reloc_type == 54; /* R_SPARC_UA64. */
aca88567 15101 case EM_X86_64:
8a9036a4 15102 case EM_L1OM:
7a9068fe 15103 case EM_K1OM:
aca88567 15104 return reloc_type == 1; /* R_X86_64_64. */
e819ade1
AS
15105 case EM_S390_OLD:
15106 case EM_S390:
aa137e4d
NC
15107 return reloc_type == 22; /* R_S390_64. */
15108 case EM_TILEGX:
15109 return reloc_type == 1; /* R_TILEGX_64. */
85a82265 15110 case EM_MIPS:
aa137e4d 15111 return reloc_type == 18; /* R_MIPS_64. */
6e712424
PI
15112 case EM_KVX:
15113 return reloc_type == 3; /* R_KVX_64 */
aca88567 15114 default:
015dc7e1 15115 return false;
aca88567
NC
15116 }
15117}
15118
85acf597
RH
15119/* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
15120 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
15121
015dc7e1 15122static bool
dda8d76d 15123is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
85acf597 15124{
dda8d76d 15125 switch (filedata->file_header.e_machine)
85acf597 15126 {
a06ea964
NC
15127 case EM_AARCH64:
15128 return reloc_type == 260; /* R_AARCH64_PREL64. */
85acf597 15129 case EM_ALPHA:
aa137e4d 15130 return reloc_type == 11; /* R_ALPHA_SREL64. */
85acf597 15131 case EM_IA_64:
262cdac7
AM
15132 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
15133 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
85acf597 15134 case EM_PARISC:
aa137e4d 15135 return reloc_type == 72; /* R_PARISC_PCREL64. */
85acf597 15136 case EM_PPC64:
aa137e4d 15137 return reloc_type == 44; /* R_PPC64_REL64. */
85acf597
RH
15138 case EM_SPARC32PLUS:
15139 case EM_SPARCV9:
15140 case EM_SPARC:
aa137e4d 15141 return reloc_type == 46; /* R_SPARC_DISP64. */
85acf597 15142 case EM_X86_64:
8a9036a4 15143 case EM_L1OM:
7a9068fe 15144 case EM_K1OM:
aa137e4d 15145 return reloc_type == 24; /* R_X86_64_PC64. */
85acf597
RH
15146 case EM_S390_OLD:
15147 case EM_S390:
aa137e4d
NC
15148 return reloc_type == 23; /* R_S390_PC64. */
15149 case EM_TILEGX:
15150 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
85acf597 15151 default:
015dc7e1 15152 return false;
85acf597
RH
15153 }
15154}
15155
4dc3c23d
AM
15156/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15157 a 24-bit absolute RELA relocation used in DWARF debug sections. */
15158
015dc7e1 15159static bool
dda8d76d 15160is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
4dc3c23d 15161{
dda8d76d 15162 switch (filedata->file_header.e_machine)
4dc3c23d
AM
15163 {
15164 case EM_CYGNUS_MN10200:
15165 case EM_MN10200:
15166 return reloc_type == 4; /* R_MN10200_24. */
3ee6e4fb
NC
15167 case EM_FT32:
15168 return reloc_type == 5; /* R_FT32_20. */
6655dba2
SB
15169 case EM_Z80:
15170 return reloc_type == 5; /* R_Z80_24. */
4dc3c23d 15171 default:
015dc7e1 15172 return false;
4dc3c23d
AM
15173 }
15174}
15175
aca88567
NC
15176/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15177 a 16-bit absolute RELA relocation used in DWARF debug sections. */
15178
015dc7e1 15179static bool
dda8d76d 15180is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
4b78141a 15181{
d347c9df 15182 /* Please keep this table alpha-sorted for ease of visual lookup. */
dda8d76d 15183 switch (filedata->file_header.e_machine)
4b78141a 15184 {
886a2506
NC
15185 case EM_ARC:
15186 case EM_ARC_COMPACT:
15187 case EM_ARC_COMPACT2:
b5c37946
SJ
15188 case EM_ARC_COMPACT3:
15189 case EM_ARC_COMPACT3_64:
886a2506 15190 return reloc_type == 2; /* R_ARC_16. */
d347c9df
PS
15191 case EM_ADAPTEVA_EPIPHANY:
15192 return reloc_type == 5;
aca88567
NC
15193 case EM_AVR_OLD:
15194 case EM_AVR:
15195 return reloc_type == 4; /* R_AVR_16. */
41e92641
NC
15196 case EM_CYGNUS_D10V:
15197 case EM_D10V:
15198 return reloc_type == 3; /* R_D10V_16. */
81b42bca
JB
15199 case EM_FT32:
15200 return reloc_type == 2; /* R_FT32_16. */
4b78141a
NC
15201 case EM_H8S:
15202 case EM_H8_300:
15203 case EM_H8_300H:
aca88567
NC
15204 return reloc_type == R_H8_DIR16;
15205 case EM_IP2K_OLD:
15206 case EM_IP2K:
15207 return reloc_type == 1; /* R_IP2K_16. */
ff7eeb89 15208 case EM_M32C_OLD:
f4236fe4
DD
15209 case EM_M32C:
15210 return reloc_type == 1; /* R_M32C_16 */
d347c9df
PS
15211 case EM_CYGNUS_MN10200:
15212 case EM_MN10200:
15213 return reloc_type == 2; /* R_MN10200_16. */
15214 case EM_CYGNUS_MN10300:
15215 case EM_MN10300:
15216 return reloc_type == 2; /* R_MN10300_16. */
6e712424
PI
15217 case EM_KVX:
15218 return reloc_type == 1; /* R_KVX_16 */
aca88567 15219 case EM_MSP430:
dda8d76d 15220 if (uses_msp430x_relocs (filedata))
13761a11 15221 return reloc_type == 2; /* R_MSP430_ABS16. */
1a0670f3 15222 /* Fall through. */
78c8d46c 15223 case EM_MSP430_OLD:
aca88567 15224 return reloc_type == 5; /* R_MSP430_16_BYTE. */
35c08157 15225 case EM_NDS32:
81c5e376 15226 return reloc_type == 19; /* R_NDS32_16_RELA. */
3e0873ac 15227 case EM_ALTERA_NIOS2:
36591ba1 15228 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
3e0873ac
NC
15229 case EM_NIOS32:
15230 return reloc_type == 9; /* R_NIOS_16. */
73589c9d
CS
15231 case EM_OR1K:
15232 return reloc_type == 2; /* R_OR1K_16. */
39e07931
AS
15233 case EM_RISCV:
15234 return reloc_type == 55; /* R_RISCV_SET16. */
2b100bb5
DD
15235 case EM_TI_PRU:
15236 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
40b36596
JM
15237 case EM_TI_C6000:
15238 return reloc_type == 2; /* R_C6000_ABS16. */
d347c9df
PS
15239 case EM_VISIUM:
15240 return reloc_type == 2; /* R_VISIUM_16. */
f6c1a2d5
NC
15241 case EM_XGATE:
15242 return reloc_type == 3; /* R_XGATE_16. */
6655dba2
SB
15243 case EM_Z80:
15244 return reloc_type == 4; /* R_Z80_16. */
4b78141a 15245 default:
015dc7e1 15246 return false;
4b78141a
NC
15247 }
15248}
15249
39e07931
AS
15250/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15251 a 8-bit absolute RELA relocation used in DWARF debug sections. */
15252
015dc7e1 15253static bool
39e07931
AS
15254is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15255{
15256 switch (filedata->file_header.e_machine)
15257 {
15258 case EM_RISCV:
15259 return reloc_type == 54; /* R_RISCV_SET8. */
6655dba2
SB
15260 case EM_Z80:
15261 return reloc_type == 1; /* R_Z80_8. */
39e07931 15262 default:
015dc7e1 15263 return false;
39e07931
AS
15264 }
15265}
15266
15267/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15268 a 6-bit absolute RELA relocation used in DWARF debug sections. */
15269
015dc7e1 15270static bool
39e07931
AS
15271is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15272{
15273 switch (filedata->file_header.e_machine)
15274 {
15275 case EM_RISCV:
15276 return reloc_type == 53; /* R_RISCV_SET6. */
15277 default:
015dc7e1 15278 return false;
39e07931
AS
15279 }
15280}
15281
03336641
JW
15282/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15283 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
15284
015dc7e1 15285static bool
03336641
JW
15286is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15287{
15288 /* Please keep this table alpha-sorted for ease of visual lookup. */
15289 switch (filedata->file_header.e_machine)
15290 {
76244462 15291 case EM_LOONGARCH:
15292 return reloc_type == 50; /* R_LARCH_ADD32. */
03336641
JW
15293 case EM_RISCV:
15294 return reloc_type == 35; /* R_RISCV_ADD32. */
15295 default:
015dc7e1 15296 return false;
03336641
JW
15297 }
15298}
15299
15300/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15301 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
15302
015dc7e1 15303static bool
03336641
JW
15304is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15305{
15306 /* Please keep this table alpha-sorted for ease of visual lookup. */
15307 switch (filedata->file_header.e_machine)
15308 {
76244462 15309 case EM_LOONGARCH:
15310 return reloc_type == 55; /* R_LARCH_SUB32. */
03336641
JW
15311 case EM_RISCV:
15312 return reloc_type == 39; /* R_RISCV_SUB32. */
15313 default:
015dc7e1 15314 return false;
03336641
JW
15315 }
15316}
15317
15318/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15319 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
15320
015dc7e1 15321static bool
03336641
JW
15322is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15323{
15324 /* Please keep this table alpha-sorted for ease of visual lookup. */
15325 switch (filedata->file_header.e_machine)
15326 {
76244462 15327 case EM_LOONGARCH:
15328 return reloc_type == 51; /* R_LARCH_ADD64. */
03336641
JW
15329 case EM_RISCV:
15330 return reloc_type == 36; /* R_RISCV_ADD64. */
15331 default:
015dc7e1 15332 return false;
03336641
JW
15333 }
15334}
15335
15336/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15337 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
15338
015dc7e1 15339static bool
03336641
JW
15340is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15341{
15342 /* Please keep this table alpha-sorted for ease of visual lookup. */
15343 switch (filedata->file_header.e_machine)
15344 {
76244462 15345 case EM_LOONGARCH:
15346 return reloc_type == 56; /* R_LARCH_SUB64. */
03336641
JW
15347 case EM_RISCV:
15348 return reloc_type == 40; /* R_RISCV_SUB64. */
15349 default:
015dc7e1 15350 return false;
03336641
JW
15351 }
15352}
15353
15354/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15355 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
15356
015dc7e1 15357static bool
03336641
JW
15358is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15359{
15360 /* Please keep this table alpha-sorted for ease of visual lookup. */
15361 switch (filedata->file_header.e_machine)
15362 {
76244462 15363 case EM_LOONGARCH:
15364 return reloc_type == 48; /* R_LARCH_ADD16. */
03336641
JW
15365 case EM_RISCV:
15366 return reloc_type == 34; /* R_RISCV_ADD16. */
15367 default:
015dc7e1 15368 return false;
03336641
JW
15369 }
15370}
15371
15372/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15373 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
15374
015dc7e1 15375static bool
03336641
JW
15376is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15377{
15378 /* Please keep this table alpha-sorted for ease of visual lookup. */
15379 switch (filedata->file_header.e_machine)
15380 {
76244462 15381 case EM_LOONGARCH:
15382 return reloc_type == 53; /* R_LARCH_SUB16. */
03336641
JW
15383 case EM_RISCV:
15384 return reloc_type == 38; /* R_RISCV_SUB16. */
15385 default:
015dc7e1 15386 return false;
03336641
JW
15387 }
15388}
15389
15390/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15391 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
15392
015dc7e1 15393static bool
03336641
JW
15394is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15395{
15396 /* Please keep this table alpha-sorted for ease of visual lookup. */
15397 switch (filedata->file_header.e_machine)
15398 {
76244462 15399 case EM_LOONGARCH:
15400 return reloc_type == 47; /* R_LARCH_ADD8. */
03336641
JW
15401 case EM_RISCV:
15402 return reloc_type == 33; /* R_RISCV_ADD8. */
15403 default:
015dc7e1 15404 return false;
03336641
JW
15405 }
15406}
15407
15408/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15409 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
15410
015dc7e1 15411static bool
03336641
JW
15412is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15413{
15414 /* Please keep this table alpha-sorted for ease of visual lookup. */
15415 switch (filedata->file_header.e_machine)
15416 {
76244462 15417 case EM_LOONGARCH:
15418 return reloc_type == 52; /* R_LARCH_SUB8. */
03336641
JW
15419 case EM_RISCV:
15420 return reloc_type == 37; /* R_RISCV_SUB8. */
15421 default:
015dc7e1 15422 return false;
03336641
JW
15423 }
15424}
15425
76244462 15426/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15427 a 6-bit inplace add RELA relocation used in DWARF debug sections. */
15428
15429static bool
15430is_6bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15431{
15432 switch (filedata->file_header.e_machine)
15433 {
15434 case EM_LOONGARCH:
15435 return reloc_type == 105; /* R_LARCH_ADD6. */
15436 default:
15437 return false;
15438 }
15439}
15440
39e07931
AS
15441/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15442 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
15443
015dc7e1 15444static bool
39e07931
AS
15445is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15446{
15447 switch (filedata->file_header.e_machine)
15448 {
76244462 15449 case EM_LOONGARCH:
15450 return reloc_type == 106; /* R_LARCH_SUB6. */
39e07931
AS
15451 case EM_RISCV:
15452 return reloc_type == 52; /* R_RISCV_SUB6. */
15453 default:
015dc7e1 15454 return false;
39e07931
AS
15455 }
15456}
15457
2a7b2e88
JK
15458/* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
15459 relocation entries (possibly formerly used for SHT_GROUP sections). */
15460
015dc7e1 15461static bool
dda8d76d 15462is_none_reloc (Filedata * filedata, unsigned int reloc_type)
2a7b2e88 15463{
dda8d76d 15464 switch (filedata->file_header.e_machine)
2a7b2e88 15465 {
cb8f3167 15466 case EM_386: /* R_386_NONE. */
d347c9df 15467 case EM_68K: /* R_68K_NONE. */
cfb8c092 15468 case EM_ADAPTEVA_EPIPHANY:
d347c9df
PS
15469 case EM_ALPHA: /* R_ALPHA_NONE. */
15470 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
886a2506 15471 case EM_ARC: /* R_ARC_NONE. */
886a2506 15472 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
d347c9df 15473 case EM_ARC_COMPACT: /* R_ARC_NONE. */
b5c37946
SJ
15474 case EM_ARC_COMPACT3: /* R_ARC_NONE. */
15475 case EM_ARC_COMPACT3_64: /* R_ARC_NONE. */
cb8f3167 15476 case EM_ARM: /* R_ARM_NONE. */
cb8f3167 15477 case EM_CRIS: /* R_CRIS_NONE. */
d347c9df
PS
15478 case EM_FT32: /* R_FT32_NONE. */
15479 case EM_IA_64: /* R_IA64_NONE. */
7a9068fe 15480 case EM_K1OM: /* R_X86_64_NONE. */
6e712424 15481 case EM_KVX: /* R_KVX_NONE. */
d347c9df
PS
15482 case EM_L1OM: /* R_X86_64_NONE. */
15483 case EM_M32R: /* R_M32R_NONE. */
15484 case EM_MIPS: /* R_MIPS_NONE. */
cb8f3167 15485 case EM_MN10300: /* R_MN10300_NONE. */
5506d11a 15486 case EM_MOXIE: /* R_MOXIE_NONE. */
d347c9df
PS
15487 case EM_NIOS32: /* R_NIOS_NONE. */
15488 case EM_OR1K: /* R_OR1K_NONE. */
15489 case EM_PARISC: /* R_PARISC_NONE. */
15490 case EM_PPC64: /* R_PPC64_NONE. */
15491 case EM_PPC: /* R_PPC_NONE. */
e23eba97 15492 case EM_RISCV: /* R_RISCV_NONE. */
d347c9df
PS
15493 case EM_S390: /* R_390_NONE. */
15494 case EM_S390_OLD:
15495 case EM_SH: /* R_SH_NONE. */
15496 case EM_SPARC32PLUS:
15497 case EM_SPARC: /* R_SPARC_NONE. */
15498 case EM_SPARCV9:
aa137e4d
NC
15499 case EM_TILEGX: /* R_TILEGX_NONE. */
15500 case EM_TILEPRO: /* R_TILEPRO_NONE. */
d347c9df
PS
15501 case EM_TI_C6000:/* R_C6000_NONE. */
15502 case EM_X86_64: /* R_X86_64_NONE. */
6655dba2 15503 case EM_Z80: /* R_Z80_NONE. */
f96bd6c2 15504 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
cb8f3167 15505 return reloc_type == 0;
d347c9df 15506
a06ea964
NC
15507 case EM_AARCH64:
15508 return reloc_type == 0 || reloc_type == 256;
d347c9df
PS
15509 case EM_AVR_OLD:
15510 case EM_AVR:
15511 return (reloc_type == 0 /* R_AVR_NONE. */
15512 || reloc_type == 30 /* R_AVR_DIFF8. */
15513 || reloc_type == 31 /* R_AVR_DIFF16. */
15514 || reloc_type == 32 /* R_AVR_DIFF32. */);
15515 case EM_METAG:
15516 return reloc_type == 3; /* R_METAG_NONE. */
35c08157 15517 case EM_NDS32:
81c5e376
AM
15518 return (reloc_type == 0 /* R_NDS32_NONE. */
15519 || reloc_type == 205 /* R_NDS32_DIFF8. */
15520 || reloc_type == 206 /* R_NDS32_DIFF16. */
15521 || reloc_type == 207 /* R_NDS32_DIFF32. */
15522 || reloc_type == 208 /* R_NDS32_DIFF_ULEB128. */);
2b100bb5
DD
15523 case EM_TI_PRU:
15524 return (reloc_type == 0 /* R_PRU_NONE. */
15525 || reloc_type == 65 /* R_PRU_DIFF8. */
15526 || reloc_type == 66 /* R_PRU_DIFF16. */
15527 || reloc_type == 67 /* R_PRU_DIFF32. */);
58332dda
JK
15528 case EM_XTENSA_OLD:
15529 case EM_XTENSA:
4dc3c23d
AM
15530 return (reloc_type == 0 /* R_XTENSA_NONE. */
15531 || reloc_type == 17 /* R_XTENSA_DIFF8. */
15532 || reloc_type == 18 /* R_XTENSA_DIFF16. */
30ce8e47
MF
15533 || reloc_type == 19 /* R_XTENSA_DIFF32. */
15534 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
15535 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
15536 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
15537 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
15538 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
15539 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
2a7b2e88 15540 }
015dc7e1 15541 return false;
2a7b2e88
JK
15542}
15543
d1c4b12b
NC
15544/* Returns TRUE if there is a relocation against
15545 section NAME at OFFSET bytes. */
15546
015dc7e1 15547bool
31e5a3a3 15548reloc_at (struct dwarf_section * dsec, uint64_t offset)
d1c4b12b
NC
15549{
15550 Elf_Internal_Rela * relocs;
15551 Elf_Internal_Rela * rp;
15552
15553 if (dsec == NULL || dsec->reloc_info == NULL)
015dc7e1 15554 return false;
d1c4b12b
NC
15555
15556 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
15557
15558 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
15559 if (rp->r_offset == offset)
015dc7e1 15560 return true;
d1c4b12b 15561
015dc7e1 15562 return false;
d1c4b12b
NC
15563}
15564
cf13d699 15565/* Apply relocations to a section.
32ec8896
NC
15566 Returns TRUE upon success, FALSE otherwise.
15567 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
15568 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
15569 will be set to the number of relocs loaded.
15570
cf13d699 15571 Note: So far support has been added only for those relocations
32ec8896
NC
15572 which can be found in debug sections. FIXME: Add support for
15573 more relocations ? */
1b315056 15574
015dc7e1 15575static bool
be7d229a
AM
15576apply_relocations (Filedata *filedata,
15577 const Elf_Internal_Shdr *section,
15578 unsigned char *start,
15579 size_t size,
15580 void **relocs_return,
26c527e6 15581 uint64_t *num_relocs_return)
1b315056 15582{
cf13d699 15583 Elf_Internal_Shdr * relsec;
0d2a7a93 15584 unsigned char * end = start + size;
cb8f3167 15585
d1c4b12b
NC
15586 if (relocs_return != NULL)
15587 {
15588 * (Elf_Internal_Rela **) relocs_return = NULL;
15589 * num_relocs_return = 0;
15590 }
15591
dda8d76d 15592 if (filedata->file_header.e_type != ET_REL)
32ec8896 15593 /* No relocs to apply. */
015dc7e1 15594 return true;
1b315056 15595
cf13d699 15596 /* Find the reloc section associated with the section. */
dda8d76d
NC
15597 for (relsec = filedata->section_headers;
15598 relsec < filedata->section_headers + filedata->file_header.e_shnum;
5b18a4bc 15599 ++relsec)
252b5132 15600 {
015dc7e1 15601 bool is_rela;
26c527e6 15602 uint64_t num_relocs;
2cf0635d
NC
15603 Elf_Internal_Rela * relocs;
15604 Elf_Internal_Rela * rp;
15605 Elf_Internal_Shdr * symsec;
15606 Elf_Internal_Sym * symtab;
26c527e6 15607 uint64_t num_syms;
2cf0635d 15608 Elf_Internal_Sym * sym;
252b5132 15609
41e92641 15610 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
dda8d76d
NC
15611 || relsec->sh_info >= filedata->file_header.e_shnum
15612 || filedata->section_headers + relsec->sh_info != section
c256ffe7 15613 || relsec->sh_size == 0
dda8d76d 15614 || relsec->sh_link >= filedata->file_header.e_shnum)
5b18a4bc 15615 continue;
428409d5 15616
a788aedd
AM
15617 symsec = filedata->section_headers + relsec->sh_link;
15618 if (symsec->sh_type != SHT_SYMTAB
15619 && symsec->sh_type != SHT_DYNSYM)
015dc7e1 15620 return false;
a788aedd 15621
41e92641
NC
15622 is_rela = relsec->sh_type == SHT_RELA;
15623
15624 if (is_rela)
15625 {
dda8d76d 15626 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
3f5e193b 15627 relsec->sh_size, & relocs, & num_relocs))
015dc7e1 15628 return false;
41e92641
NC
15629 }
15630 else
15631 {
dda8d76d 15632 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
3f5e193b 15633 relsec->sh_size, & relocs, & num_relocs))
015dc7e1 15634 return false;
41e92641
NC
15635 }
15636
15637 /* SH uses RELA but uses in place value instead of the addend field. */
dda8d76d 15638 if (filedata->file_header.e_machine == EM_SH)
015dc7e1 15639 is_rela = false;
428409d5 15640
4de91c10 15641 symtab = get_elf_symbols (filedata, symsec, & num_syms);
103f02d3 15642
41e92641 15643 for (rp = relocs; rp < relocs + num_relocs; ++rp)
252b5132 15644 {
625d49fc 15645 uint64_t addend;
015dc7e1
AM
15646 unsigned int reloc_type;
15647 unsigned int reloc_size;
15648 bool reloc_inplace = false;
15649 bool reloc_subtract = false;
15650 unsigned char *rloc;
26c527e6 15651 uint64_t sym_index;
4b78141a 15652
dda8d76d 15653 reloc_type = get_reloc_type (filedata, rp->r_info);
41e92641 15654
dda8d76d 15655 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
2a7b2e88 15656 continue;
dda8d76d 15657 else if (is_none_reloc (filedata, reloc_type))
98fb390a 15658 continue;
dda8d76d
NC
15659 else if (is_32bit_abs_reloc (filedata, reloc_type)
15660 || is_32bit_pcrel_reloc (filedata, reloc_type))
aca88567 15661 reloc_size = 4;
dda8d76d
NC
15662 else if (is_64bit_abs_reloc (filedata, reloc_type)
15663 || is_64bit_pcrel_reloc (filedata, reloc_type))
aca88567 15664 reloc_size = 8;
dda8d76d 15665 else if (is_24bit_abs_reloc (filedata, reloc_type))
4dc3c23d 15666 reloc_size = 3;
dda8d76d 15667 else if (is_16bit_abs_reloc (filedata, reloc_type))
aca88567 15668 reloc_size = 2;
39e07931
AS
15669 else if (is_8bit_abs_reloc (filedata, reloc_type)
15670 || is_6bit_abs_reloc (filedata, reloc_type))
15671 reloc_size = 1;
03336641
JW
15672 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
15673 reloc_type))
15674 || is_32bit_inplace_add_reloc (filedata, reloc_type))
15675 {
15676 reloc_size = 4;
015dc7e1 15677 reloc_inplace = true;
03336641
JW
15678 }
15679 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
15680 reloc_type))
15681 || is_64bit_inplace_add_reloc (filedata, reloc_type))
15682 {
15683 reloc_size = 8;
015dc7e1 15684 reloc_inplace = true;
03336641
JW
15685 }
15686 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
15687 reloc_type))
15688 || is_16bit_inplace_add_reloc (filedata, reloc_type))
15689 {
15690 reloc_size = 2;
015dc7e1 15691 reloc_inplace = true;
03336641
JW
15692 }
15693 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
15694 reloc_type))
15695 || is_8bit_inplace_add_reloc (filedata, reloc_type))
15696 {
15697 reloc_size = 1;
015dc7e1 15698 reloc_inplace = true;
03336641 15699 }
39e07931 15700 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
76244462 15701 reloc_type))
15702 || is_6bit_inplace_add_reloc (filedata, reloc_type))
39e07931
AS
15703 {
15704 reloc_size = 1;
015dc7e1 15705 reloc_inplace = true;
39e07931 15706 }
aca88567 15707 else
4b78141a 15708 {
bee0ee85 15709 static unsigned int prev_reloc = 0;
dda8d76d 15710
bee0ee85
NC
15711 if (reloc_type != prev_reloc)
15712 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
dda8d76d 15713 reloc_type, printable_section_name (filedata, section));
bee0ee85 15714 prev_reloc = reloc_type;
4b78141a
NC
15715 continue;
15716 }
103f02d3 15717
91d6fa6a 15718 rloc = start + rp->r_offset;
75802ccb 15719 if (!IN_RANGE (start, end, rloc, reloc_size))
700dd8b7 15720 {
26c527e6
AM
15721 warn (_("skipping invalid relocation offset %#" PRIx64
15722 " in section %s\n"),
15723 rp->r_offset,
dda8d76d 15724 printable_section_name (filedata, section));
700dd8b7
L
15725 continue;
15726 }
103f02d3 15727
26c527e6 15728 sym_index = get_reloc_symindex (rp->r_info);
ba5cdace
NC
15729 if (sym_index >= num_syms)
15730 {
26c527e6
AM
15731 warn (_("skipping invalid relocation symbol index %#" PRIx64
15732 " in section %s\n"),
dda8d76d 15733 sym_index, printable_section_name (filedata, section));
ba5cdace
NC
15734 continue;
15735 }
15736 sym = symtab + sym_index;
41e92641
NC
15737
15738 /* If the reloc has a symbol associated with it,
55f25fc3
L
15739 make sure that it is of an appropriate type.
15740
15741 Relocations against symbols without type can happen.
15742 Gcc -feliminate-dwarf2-dups may generate symbols
15743 without type for debug info.
15744
15745 Icc generates relocations against function symbols
15746 instead of local labels.
15747
15748 Relocations against object symbols can happen, eg when
15749 referencing a global array. For an example of this see
15750 the _clz.o binary in libgcc.a. */
aca88567 15751 if (sym != symtab
b8871f35 15752 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
55f25fc3 15753 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
5b18a4bc 15754 {
26c527e6 15755 warn (_("skipping unexpected symbol type %s in section %s relocation %tu\n"),
dda8d76d
NC
15756 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
15757 printable_section_name (filedata, relsec),
26c527e6 15758 rp - relocs);
aca88567 15759 continue;
5b18a4bc 15760 }
252b5132 15761
4dc3c23d
AM
15762 addend = 0;
15763 if (is_rela)
15764 addend += rp->r_addend;
c47320c3
AM
15765 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
15766 partial_inplace. */
4dc3c23d 15767 if (!is_rela
dda8d76d 15768 || (filedata->file_header.e_machine == EM_XTENSA
4dc3c23d 15769 && reloc_type == 1)
dda8d76d
NC
15770 || ((filedata->file_header.e_machine == EM_PJ
15771 || filedata->file_header.e_machine == EM_PJ_OLD)
c47320c3 15772 && reloc_type == 1)
dda8d76d
NC
15773 || ((filedata->file_header.e_machine == EM_D30V
15774 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
03336641
JW
15775 && reloc_type == 12)
15776 || reloc_inplace)
39e07931
AS
15777 {
15778 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
15779 addend += byte_get (rloc, reloc_size) & 0x3f;
15780 else
15781 addend += byte_get (rloc, reloc_size);
15782 }
cb8f3167 15783
dda8d76d
NC
15784 if (is_32bit_pcrel_reloc (filedata, reloc_type)
15785 || is_64bit_pcrel_reloc (filedata, reloc_type))
85acf597
RH
15786 {
15787 /* On HPPA, all pc-relative relocations are biased by 8. */
dda8d76d 15788 if (filedata->file_header.e_machine == EM_PARISC)
85acf597 15789 addend -= 8;
91d6fa6a 15790 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
85acf597
RH
15791 reloc_size);
15792 }
39e07931 15793 else if (is_6bit_abs_reloc (filedata, reloc_type)
76244462 15794 || is_6bit_inplace_sub_reloc (filedata, reloc_type)
15795 || is_6bit_inplace_add_reloc (filedata, reloc_type))
39e07931
AS
15796 {
15797 if (reloc_subtract)
15798 addend -= sym->st_value;
15799 else
15800 addend += sym->st_value;
15801 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
15802 byte_put (rloc, addend, reloc_size);
15803 }
03336641
JW
15804 else if (reloc_subtract)
15805 byte_put (rloc, addend - sym->st_value, reloc_size);
41e92641 15806 else
91d6fa6a 15807 byte_put (rloc, addend + sym->st_value, reloc_size);
5b18a4bc 15808 }
252b5132 15809
5b18a4bc 15810 free (symtab);
f84ce13b
NC
15811 /* Let the target specific reloc processing code know that
15812 we have finished with these relocs. */
dda8d76d 15813 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
d1c4b12b
NC
15814
15815 if (relocs_return)
15816 {
15817 * (Elf_Internal_Rela **) relocs_return = relocs;
15818 * num_relocs_return = num_relocs;
15819 }
15820 else
15821 free (relocs);
15822
5b18a4bc
NC
15823 break;
15824 }
32ec8896 15825
015dc7e1 15826 return true;
5b18a4bc 15827}
103f02d3 15828
cf13d699 15829#ifdef SUPPORT_DISASSEMBLY
015dc7e1 15830static bool
dda8d76d 15831disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
cf13d699 15832{
dda8d76d 15833 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
cf13d699 15834
74e1a04b 15835 /* FIXME: XXX -- to be done --- XXX */
cf13d699 15836
015dc7e1 15837 return true;
cf13d699
NC
15838}
15839#endif
15840
15841/* Reads in the contents of SECTION from FILE, returning a pointer
15842 to a malloc'ed buffer or NULL if something went wrong. */
15843
15844static char *
dda8d76d 15845get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
cf13d699 15846{
be7d229a 15847 uint64_t num_bytes = section->sh_size;
cf13d699
NC
15848
15849 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
15850 {
c6b78c96 15851 printf (_("Section '%s' has no data to dump.\n"),
dda8d76d 15852 printable_section_name (filedata, section));
cf13d699
NC
15853 return NULL;
15854 }
15855
dda8d76d 15856 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
3f5e193b 15857 _("section contents"));
cf13d699
NC
15858}
15859
1f5a3546 15860/* Uncompresses a section that was compressed using zlib/zstd, in place. */
0e602686 15861
015dc7e1 15862static bool
45f5fe46
NC
15863uncompress_section_contents (bool is_zstd,
15864 unsigned char ** buffer,
15865 uint64_t uncompressed_size,
15866 uint64_t * size,
15867 uint64_t file_size)
0e602686 15868{
31e5a3a3
AM
15869 uint64_t compressed_size = *size;
15870 unsigned char *compressed_buffer = *buffer;
45f5fe46 15871 unsigned char *uncompressed_buffer = NULL;
0e602686
NC
15872 z_stream strm;
15873 int rc;
15874
45f5fe46
NC
15875 /* Similar to _bfd_section_size_insane() in the BFD library we expect an
15876 upper limit of ~10x compression. Any compression larger than that is
15877 thought to be due to fuzzing of the compression header. */
15878 if (uncompressed_size > file_size * 10)
15879 {
15880 error (_("Uncompressed section size is suspiciously large: 0x%" PRIu64 "\n"),
15881 uncompressed_size);
15882 goto fail;
15883 }
15884
15885 uncompressed_buffer = xmalloc (uncompressed_size);
15886
1f5a3546
FS
15887 if (is_zstd)
15888 {
15889#ifdef HAVE_ZSTD
15890 size_t ret = ZSTD_decompress (uncompressed_buffer, uncompressed_size,
15891 compressed_buffer, compressed_size);
15892 if (ZSTD_isError (ret))
15893 goto fail;
15894#endif
15895 }
15896 else
15897 {
15898 /* It is possible the section consists of several compressed
15899 buffers concatenated together, so we uncompress in a loop. */
15900 /* PR 18313: The state field in the z_stream structure is supposed
15901 to be invisible to the user (ie us), but some compilers will
15902 still complain about it being used without initialisation. So
15903 we first zero the entire z_stream structure and then set the fields
15904 that we need. */
15905 memset (&strm, 0, sizeof strm);
15906 strm.avail_in = compressed_size;
15907 strm.next_in = (Bytef *)compressed_buffer;
15908 strm.avail_out = uncompressed_size;
15909
15910 rc = inflateInit (&strm);
15911 while (strm.avail_in > 0)
15912 {
15913 if (rc != Z_OK)
15914 break;
15915 strm.next_out = ((Bytef *)uncompressed_buffer
15916 + (uncompressed_size - strm.avail_out));
15917 rc = inflate (&strm, Z_FINISH);
15918 if (rc != Z_STREAM_END)
15919 break;
15920 rc = inflateReset (&strm);
15921 }
15922 if (inflateEnd (&strm) != Z_OK || rc != Z_OK || strm.avail_out != 0)
15923 goto fail;
15924 }
0e602686
NC
15925
15926 *buffer = uncompressed_buffer;
15927 *size = uncompressed_size;
015dc7e1 15928 return true;
0e602686
NC
15929
15930 fail:
15931 free (uncompressed_buffer);
15932 /* Indicate decompression failure. */
15933 *buffer = NULL;
015dc7e1 15934 return false;
0e602686 15935}
dd24e3da 15936
015dc7e1 15937static bool
dda8d76d 15938dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
cf13d699 15939{
015dc7e1 15940 Elf_Internal_Shdr *relsec;
be7d229a 15941 uint64_t num_bytes;
015dc7e1
AM
15942 unsigned char *data;
15943 unsigned char *end;
15944 unsigned char *real_start;
15945 unsigned char *start;
15946 bool some_strings_shown;
cf13d699 15947
dda8d76d 15948 real_start = start = (unsigned char *) get_section_contents (section, filedata);
cf13d699 15949 if (start == NULL)
c6b78c96 15950 /* PR 21820: Do not fail if the section was empty. */
63b4cc53 15951 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
c6b78c96 15952
0e602686 15953 num_bytes = section->sh_size;
cf13d699 15954
835f2fae
NC
15955 if (filedata->is_separate)
15956 printf (_("\nString dump of section '%s' in linked file %s:\n"),
15957 printable_section_name (filedata, section),
15958 filedata->file_name);
15959 else
15960 printf (_("\nString dump of section '%s':\n"),
15961 printable_section_name (filedata, section));
cf13d699 15962
0e602686
NC
15963 if (decompress_dumps)
15964 {
31e5a3a3
AM
15965 uint64_t new_size = num_bytes;
15966 uint64_t uncompressed_size = 0;
1f5a3546 15967 bool is_zstd = false;
0e602686
NC
15968
15969 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15970 {
15971 Elf_Internal_Chdr chdr;
15972 unsigned int compression_header_size
ebdf1ebf
NC
15973 = get_compression_header (& chdr, (unsigned char *) start,
15974 num_bytes);
5844b465
NC
15975 if (compression_header_size == 0)
15976 /* An error message will have already been generated
15977 by get_compression_header. */
15978 goto error_out;
0e602686 15979
89dbeac7 15980 if (chdr.ch_type == ch_compress_zlib)
1f5a3546
FS
15981 ;
15982#ifdef HAVE_ZSTD
89dbeac7 15983 else if (chdr.ch_type == ch_compress_zstd)
1f5a3546
FS
15984 is_zstd = true;
15985#endif
15986 else
0e602686 15987 {
813dabb9 15988 warn (_("section '%s' has unsupported compress type: %d\n"),
dda8d76d 15989 printable_section_name (filedata, section), chdr.ch_type);
f761cb13 15990 goto error_out;
813dabb9 15991 }
813dabb9
L
15992 uncompressed_size = chdr.ch_size;
15993 start += compression_header_size;
15994 new_size -= compression_header_size;
0e602686
NC
15995 }
15996 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15997 {
15998 /* Read the zlib header. In this case, it should be "ZLIB"
15999 followed by the uncompressed section size, 8 bytes in
16000 big-endian order. */
16001 uncompressed_size = start[4]; uncompressed_size <<= 8;
16002 uncompressed_size += start[5]; uncompressed_size <<= 8;
16003 uncompressed_size += start[6]; uncompressed_size <<= 8;
16004 uncompressed_size += start[7]; uncompressed_size <<= 8;
16005 uncompressed_size += start[8]; uncompressed_size <<= 8;
16006 uncompressed_size += start[9]; uncompressed_size <<= 8;
16007 uncompressed_size += start[10]; uncompressed_size <<= 8;
16008 uncompressed_size += start[11];
16009 start += 12;
16010 new_size -= 12;
16011 }
16012
1835f746
NC
16013 if (uncompressed_size)
16014 {
1f5a3546 16015 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
45f5fe46 16016 &new_size, filedata->file_size))
1835f746
NC
16017 num_bytes = new_size;
16018 else
16019 {
16020 error (_("Unable to decompress section %s\n"),
dda8d76d 16021 printable_section_name (filedata, section));
f761cb13 16022 goto error_out;
1835f746
NC
16023 }
16024 }
bc303e5d
NC
16025 else
16026 start = real_start;
0e602686 16027 }
fd8008d8 16028
cf13d699
NC
16029 /* If the section being dumped has relocations against it the user might
16030 be expecting these relocations to have been applied. Check for this
16031 case and issue a warning message in order to avoid confusion.
16032 FIXME: Maybe we ought to have an option that dumps a section with
16033 relocs applied ? */
dda8d76d
NC
16034 for (relsec = filedata->section_headers;
16035 relsec < filedata->section_headers + filedata->file_header.e_shnum;
cf13d699
NC
16036 ++relsec)
16037 {
16038 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
dda8d76d
NC
16039 || relsec->sh_info >= filedata->file_header.e_shnum
16040 || filedata->section_headers + relsec->sh_info != section
cf13d699 16041 || relsec->sh_size == 0
dda8d76d 16042 || relsec->sh_link >= filedata->file_header.e_shnum)
cf13d699
NC
16043 continue;
16044
16045 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
16046 break;
16047 }
16048
cf13d699
NC
16049 data = start;
16050 end = start + num_bytes;
015dc7e1 16051 some_strings_shown = false;
cf13d699 16052
ba3265d0
NC
16053#ifdef HAVE_MBSTATE_T
16054 mbstate_t state;
16055 /* Initialise the multibyte conversion state. */
16056 memset (& state, 0, sizeof (state));
16057#endif
16058
015dc7e1 16059 bool continuing = false;
ba3265d0 16060
cf13d699
NC
16061 while (data < end)
16062 {
16063 while (!ISPRINT (* data))
16064 if (++ data >= end)
16065 break;
16066
16067 if (data < end)
16068 {
071436c6
NC
16069 size_t maxlen = end - data;
16070
ba3265d0
NC
16071 if (continuing)
16072 {
16073 printf (" ");
015dc7e1 16074 continuing = false;
ba3265d0
NC
16075 }
16076 else
16077 {
26c527e6 16078 printf (" [%6tx] ", data - start);
ba3265d0
NC
16079 }
16080
4082ef84
NC
16081 if (maxlen > 0)
16082 {
f3da8a96 16083 char c = 0;
ba3265d0
NC
16084
16085 while (maxlen)
16086 {
16087 c = *data++;
16088
16089 if (c == 0)
16090 break;
16091
16092 /* PR 25543: Treat new-lines as string-ending characters. */
16093 if (c == '\n')
16094 {
16095 printf ("\\n\n");
16096 if (*data != 0)
015dc7e1 16097 continuing = true;
ba3265d0
NC
16098 break;
16099 }
16100
16101 /* Do not print control characters directly as they can affect terminal
16102 settings. Such characters usually appear in the names generated
16103 by the assembler for local labels. */
16104 if (ISCNTRL (c))
16105 {
16106 printf ("^%c", c + 0x40);
16107 }
16108 else if (ISPRINT (c))
16109 {
16110 putchar (c);
16111 }
16112 else
16113 {
16114 size_t n;
16115#ifdef HAVE_MBSTATE_T
16116 wchar_t w;
16117#endif
16118 /* Let printf do the hard work of displaying multibyte characters. */
16119 printf ("%.1s", data - 1);
16120#ifdef HAVE_MBSTATE_T
16121 /* Try to find out how many bytes made up the character that was
16122 just printed. Advance the symbol pointer past the bytes that
16123 were displayed. */
16124 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
16125#else
16126 n = 1;
16127#endif
16128 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
16129 data += (n - 1);
16130 }
16131 }
16132
16133 if (c != '\n')
16134 putchar ('\n');
4082ef84
NC
16135 }
16136 else
16137 {
16138 printf (_("<corrupt>\n"));
16139 data = end;
16140 }
015dc7e1 16141 some_strings_shown = true;
cf13d699
NC
16142 }
16143 }
16144
16145 if (! some_strings_shown)
16146 printf (_(" No strings found in this section."));
16147
0e602686 16148 free (real_start);
cf13d699
NC
16149
16150 putchar ('\n');
015dc7e1 16151 return true;
f761cb13
AM
16152
16153error_out:
16154 free (real_start);
015dc7e1 16155 return false;
cf13d699
NC
16156}
16157
015dc7e1
AM
16158static bool
16159dump_section_as_bytes (Elf_Internal_Shdr *section,
16160 Filedata *filedata,
16161 bool relocate)
cf13d699 16162{
be7d229a
AM
16163 Elf_Internal_Shdr *relsec;
16164 size_t bytes;
16165 uint64_t section_size;
625d49fc 16166 uint64_t addr;
be7d229a
AM
16167 unsigned char *data;
16168 unsigned char *real_start;
16169 unsigned char *start;
0e602686 16170
dda8d76d 16171 real_start = start = (unsigned char *) get_section_contents (section, filedata);
cf13d699 16172 if (start == NULL)
c6b78c96 16173 /* PR 21820: Do not fail if the section was empty. */
63b4cc53 16174 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
32ec8896 16175
0e602686 16176 section_size = section->sh_size;
cf13d699 16177
835f2fae
NC
16178 if (filedata->is_separate)
16179 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
16180 printable_section_name (filedata, section),
16181 filedata->file_name);
16182 else
16183 printf (_("\nHex dump of section '%s':\n"),
16184 printable_section_name (filedata, section));
cf13d699 16185
0e602686
NC
16186 if (decompress_dumps)
16187 {
31e5a3a3
AM
16188 uint64_t new_size = section_size;
16189 uint64_t uncompressed_size = 0;
1f5a3546 16190 bool is_zstd = false;
0e602686
NC
16191
16192 if ((section->sh_flags & SHF_COMPRESSED) != 0)
16193 {
16194 Elf_Internal_Chdr chdr;
16195 unsigned int compression_header_size
ebdf1ebf 16196 = get_compression_header (& chdr, start, section_size);
0e602686 16197
5844b465
NC
16198 if (compression_header_size == 0)
16199 /* An error message will have already been generated
16200 by get_compression_header. */
16201 goto error_out;
16202
89dbeac7 16203 if (chdr.ch_type == ch_compress_zlib)
1f5a3546
FS
16204 ;
16205#ifdef HAVE_ZSTD
89dbeac7 16206 else if (chdr.ch_type == ch_compress_zstd)
1f5a3546
FS
16207 is_zstd = true;
16208#endif
16209 else
0e602686 16210 {
813dabb9 16211 warn (_("section '%s' has unsupported compress type: %d\n"),
dda8d76d 16212 printable_section_name (filedata, section), chdr.ch_type);
f761cb13 16213 goto error_out;
0e602686 16214 }
813dabb9
L
16215 uncompressed_size = chdr.ch_size;
16216 start += compression_header_size;
16217 new_size -= compression_header_size;
0e602686
NC
16218 }
16219 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
16220 {
16221 /* Read the zlib header. In this case, it should be "ZLIB"
16222 followed by the uncompressed section size, 8 bytes in
16223 big-endian order. */
16224 uncompressed_size = start[4]; uncompressed_size <<= 8;
16225 uncompressed_size += start[5]; uncompressed_size <<= 8;
16226 uncompressed_size += start[6]; uncompressed_size <<= 8;
16227 uncompressed_size += start[7]; uncompressed_size <<= 8;
16228 uncompressed_size += start[8]; uncompressed_size <<= 8;
16229 uncompressed_size += start[9]; uncompressed_size <<= 8;
16230 uncompressed_size += start[10]; uncompressed_size <<= 8;
16231 uncompressed_size += start[11];
16232 start += 12;
16233 new_size -= 12;
16234 }
16235
f055032e
NC
16236 if (uncompressed_size)
16237 {
1f5a3546 16238 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
45f5fe46 16239 &new_size, filedata->file_size))
bc303e5d
NC
16240 {
16241 section_size = new_size;
16242 }
f055032e
NC
16243 else
16244 {
16245 error (_("Unable to decompress section %s\n"),
dda8d76d 16246 printable_section_name (filedata, section));
bc303e5d 16247 /* FIXME: Print the section anyway ? */
f761cb13 16248 goto error_out;
f055032e
NC
16249 }
16250 }
bc303e5d
NC
16251 else
16252 start = real_start;
0e602686 16253 }
14ae95f2 16254
cf13d699
NC
16255 if (relocate)
16256 {
dda8d76d 16257 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
f761cb13 16258 goto error_out;
cf13d699
NC
16259 }
16260 else
16261 {
16262 /* If the section being dumped has relocations against it the user might
16263 be expecting these relocations to have been applied. Check for this
16264 case and issue a warning message in order to avoid confusion.
16265 FIXME: Maybe we ought to have an option that dumps a section with
16266 relocs applied ? */
dda8d76d
NC
16267 for (relsec = filedata->section_headers;
16268 relsec < filedata->section_headers + filedata->file_header.e_shnum;
cf13d699
NC
16269 ++relsec)
16270 {
16271 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
dda8d76d
NC
16272 || relsec->sh_info >= filedata->file_header.e_shnum
16273 || filedata->section_headers + relsec->sh_info != section
cf13d699 16274 || relsec->sh_size == 0
dda8d76d 16275 || relsec->sh_link >= filedata->file_header.e_shnum)
cf13d699
NC
16276 continue;
16277
16278 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
16279 break;
16280 }
16281 }
16282
16283 addr = section->sh_addr;
0e602686 16284 bytes = section_size;
cf13d699
NC
16285 data = start;
16286
16287 while (bytes)
16288 {
16289 int j;
16290 int k;
16291 int lbytes;
16292
16293 lbytes = (bytes > 16 ? 16 : bytes);
16294
26c527e6 16295 printf (" 0x%8.8" PRIx64 " ", addr);
cf13d699
NC
16296
16297 for (j = 0; j < 16; j++)
16298 {
16299 if (j < lbytes)
16300 printf ("%2.2x", data[j]);
16301 else
16302 printf (" ");
16303
16304 if ((j & 3) == 3)
16305 printf (" ");
16306 }
16307
16308 for (j = 0; j < lbytes; j++)
16309 {
16310 k = data[j];
16311 if (k >= ' ' && k < 0x7f)
16312 printf ("%c", k);
16313 else
16314 printf (".");
16315 }
16316
16317 putchar ('\n');
16318
16319 data += lbytes;
16320 addr += lbytes;
16321 bytes -= lbytes;
16322 }
16323
0e602686 16324 free (real_start);
cf13d699
NC
16325
16326 putchar ('\n');
015dc7e1 16327 return true;
f761cb13
AM
16328
16329 error_out:
16330 free (real_start);
015dc7e1 16331 return false;
cf13d699
NC
16332}
16333
094e34f2 16334#ifdef ENABLE_LIBCTF
7d9813f1
NA
16335static ctf_sect_t *
16336shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
16337{
b6ac461a 16338 buf->cts_name = printable_section_name (filedata, shdr);
7d9813f1
NA
16339 buf->cts_size = shdr->sh_size;
16340 buf->cts_entsize = shdr->sh_entsize;
7d9813f1
NA
16341
16342 return buf;
16343}
16344
16345/* Formatting callback function passed to ctf_dump. Returns either the pointer
16346 it is passed, or a pointer to newly-allocated storage, in which case
16347 dump_ctf() will free it when it no longer needs it. */
16348
2f6ecaed
NA
16349static char *
16350dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
16351 char *s, void *arg)
7d9813f1 16352{
3e50a591 16353 const char *blanks = arg;
7d9813f1
NA
16354 char *new_s;
16355
3e50a591 16356 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
7d9813f1
NA
16357 return s;
16358 return new_s;
16359}
16360
926c9e76
NA
16361/* Dump CTF errors/warnings. */
16362static void
139633c3 16363dump_ctf_errs (ctf_dict_t *fp)
926c9e76
NA
16364{
16365 ctf_next_t *it = NULL;
16366 char *errtext;
16367 int is_warning;
16368 int err;
16369
16370 /* Dump accumulated errors and warnings. */
16371 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
16372 {
5e9b84f7 16373 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
926c9e76
NA
16374 errtext);
16375 free (errtext);
16376 }
16377 if (err != ECTF_NEXT_END)
16378 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
16379}
16380
2f6ecaed
NA
16381/* Dump one CTF archive member. */
16382
80b56fad
NA
16383static void
16384dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
16385 size_t member)
2f6ecaed 16386{
2f6ecaed
NA
16387 const char *things[] = {"Header", "Labels", "Data objects",
16388 "Function objects", "Variables", "Types", "Strings",
16389 ""};
16390 const char **thing;
16391 size_t i;
16392
80b56fad
NA
16393 /* Don't print out the name of the default-named archive member if it appears
16394 first in the list. The name .ctf appears everywhere, even for things that
16395 aren't really archives, so printing it out is liable to be confusing; also,
16396 the common case by far is for only one archive member to exist, and hiding
16397 it in that case seems worthwhile. */
2f6ecaed 16398
80b56fad
NA
16399 if (strcmp (name, ".ctf") != 0 || member != 0)
16400 printf (_("\nCTF archive member: %s:\n"), name);
2f6ecaed 16401
80b56fad
NA
16402 if (ctf_parent_name (ctf) != NULL)
16403 ctf_import (ctf, parent);
2f6ecaed
NA
16404
16405 for (i = 0, thing = things; *thing[0]; thing++, i++)
16406 {
16407 ctf_dump_state_t *s = NULL;
16408 char *item;
16409
16410 printf ("\n %s:\n", *thing);
16411 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
16412 (void *) " ")) != NULL)
16413 {
16414 printf ("%s\n", item);
16415 free (item);
16416 }
16417
16418 if (ctf_errno (ctf))
16419 {
16420 error (_("Iteration failed: %s, %s\n"), *thing,
16421 ctf_errmsg (ctf_errno (ctf)));
80b56fad 16422 break;
2f6ecaed
NA
16423 }
16424 }
8b37e7b6 16425
926c9e76 16426 dump_ctf_errs (ctf);
2f6ecaed
NA
16427}
16428
015dc7e1 16429static bool
7d9813f1
NA
16430dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
16431{
7d9813f1
NA
16432 Elf_Internal_Shdr * symtab_sec = NULL;
16433 Elf_Internal_Shdr * strtab_sec = NULL;
d344b407
NA
16434 void * data = NULL;
16435 void * symdata = NULL;
16436 void * strdata = NULL;
80b56fad 16437 ctf_sect_t ctfsect, symsect, strsect;
d344b407
NA
16438 ctf_sect_t * symsectp = NULL;
16439 ctf_sect_t * strsectp = NULL;
2f6ecaed 16440 ctf_archive_t * ctfa = NULL;
139633c3 16441 ctf_dict_t * parent = NULL;
80b56fad 16442 ctf_dict_t * fp;
7d9813f1 16443
80b56fad
NA
16444 ctf_next_t *i = NULL;
16445 const char *name;
16446 size_t member = 0;
7d9813f1 16447 int err;
015dc7e1 16448 bool ret = false;
7d9813f1
NA
16449
16450 shdr_to_ctf_sect (&ctfsect, section, filedata);
16451 data = get_section_contents (section, filedata);
16452 ctfsect.cts_data = data;
16453
616febde 16454 if (!dump_ctf_symtab_name)
3d16b64e 16455 dump_ctf_symtab_name = strdup (".dynsym");
616febde
NA
16456
16457 if (!dump_ctf_strtab_name)
3d16b64e 16458 dump_ctf_strtab_name = strdup (".dynstr");
616febde
NA
16459
16460 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
7d9813f1
NA
16461 {
16462 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
16463 {
16464 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
16465 goto fail;
16466 }
16467 if ((symdata = (void *) get_data (NULL, filedata,
16468 symtab_sec->sh_offset, 1,
16469 symtab_sec->sh_size,
16470 _("symbols"))) == NULL)
16471 goto fail;
16472 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
16473 symsect.cts_data = symdata;
16474 }
835f2fae 16475
df16e041 16476 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
7d9813f1
NA
16477 {
16478 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
16479 {
16480 error (_("No string table section named %s\n"),
16481 dump_ctf_strtab_name);
16482 goto fail;
16483 }
16484 if ((strdata = (void *) get_data (NULL, filedata,
16485 strtab_sec->sh_offset, 1,
16486 strtab_sec->sh_size,
16487 _("strings"))) == NULL)
16488 goto fail;
16489 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
16490 strsect.cts_data = strdata;
16491 }
835f2fae 16492
2f6ecaed
NA
16493 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
16494 libctf papers over the difference, so we can pretend it is always an
80b56fad 16495 archive. */
7d9813f1 16496
2f6ecaed 16497 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
7d9813f1 16498 {
926c9e76 16499 dump_ctf_errs (NULL);
7d9813f1
NA
16500 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
16501 goto fail;
16502 }
16503
96c61be5
NA
16504 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
16505 != ELFDATA2MSB);
16506
80b56fad
NA
16507 /* Preload the parent dict, since it will need to be imported into every
16508 child in turn. */
16509 if ((parent = ctf_dict_open (ctfa, dump_ctf_parent_name, &err)) == NULL)
2f6ecaed 16510 {
926c9e76 16511 dump_ctf_errs (NULL);
2f6ecaed
NA
16512 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
16513 goto fail;
7d9813f1
NA
16514 }
16515
015dc7e1 16516 ret = true;
7d9813f1 16517
835f2fae
NC
16518 if (filedata->is_separate)
16519 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
16520 printable_section_name (filedata, section),
16521 filedata->file_name);
16522 else
16523 printf (_("\nDump of CTF section '%s':\n"),
16524 printable_section_name (filedata, section));
7d9813f1 16525
80b56fad
NA
16526 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
16527 dump_ctf_archive_member (fp, name, parent, member++);
16528 if (err != ECTF_NEXT_END)
16529 {
16530 dump_ctf_errs (NULL);
16531 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
16532 ret = false;
16533 }
7d9813f1
NA
16534
16535 fail:
139633c3 16536 ctf_dict_close (parent);
2f6ecaed 16537 ctf_close (ctfa);
7d9813f1
NA
16538 free (data);
16539 free (symdata);
16540 free (strdata);
16541 return ret;
16542}
094e34f2 16543#endif
7d9813f1 16544
42b6953b
IB
16545static bool
16546dump_section_as_sframe (Elf_Internal_Shdr * section, Filedata * filedata)
16547{
16548 void * data = NULL;
16549 sframe_decoder_ctx *sfd_ctx = NULL;
16550 const char *print_name = printable_section_name (filedata, section);
16551
16552 bool ret = true;
16553 size_t sf_size;
16554 int err = 0;
16555
16556 if (strcmp (print_name, "") == 0)
16557 {
16558 error (_("Section name must be provided \n"));
16559 ret = false;
16560 return ret;
16561 }
16562
16563 data = get_section_contents (section, filedata);
16564 sf_size = section->sh_size;
16565 /* Decode the contents of the section. */
16566 sfd_ctx = sframe_decode ((const char*)data, sf_size, &err);
16567 if (!sfd_ctx)
16568 {
16569 ret = false;
16570 error (_("SFrame decode failure: %s\n"), sframe_errmsg (err));
16571 goto fail;
16572 }
16573
16574 printf (_("Contents of the SFrame section %s:"), print_name);
16575 /* Dump the contents as text. */
16576 dump_sframe (sfd_ctx, section->sh_addr);
16577
16578 fail:
16579 free (data);
16580 return ret;
16581}
16582
015dc7e1 16583static bool
dda8d76d
NC
16584load_specific_debug_section (enum dwarf_section_display_enum debug,
16585 const Elf_Internal_Shdr * sec,
16586 void * data)
1007acb3 16587{
2cf0635d 16588 struct dwarf_section * section = &debug_displays [debug].section;
19e6b90e 16589 char buf [64];
dda8d76d 16590 Filedata * filedata = (Filedata *) data;
9abca702 16591
19e6b90e 16592 if (section->start != NULL)
dda8d76d
NC
16593 {
16594 /* If it is already loaded, do nothing. */
16595 if (streq (section->filename, filedata->file_name))
015dc7e1 16596 return true;
dda8d76d
NC
16597 free (section->start);
16598 }
1007acb3 16599
19e6b90e
L
16600 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
16601 section->address = sec->sh_addr;
dda8d76d
NC
16602 section->filename = filedata->file_name;
16603 section->start = (unsigned char *) get_data (NULL, filedata,
3f5e193b
NC
16604 sec->sh_offset, 1,
16605 sec->sh_size, buf);
59245841
NC
16606 if (section->start == NULL)
16607 section->size = 0;
16608 else
16609 {
77115a4a 16610 unsigned char *start = section->start;
31e5a3a3
AM
16611 uint64_t size = sec->sh_size;
16612 uint64_t uncompressed_size = 0;
1f5a3546 16613 bool is_zstd = false;
77115a4a
L
16614
16615 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
16616 {
16617 Elf_Internal_Chdr chdr;
d8024a91
NC
16618 unsigned int compression_header_size;
16619
f53be977
L
16620 if (size < (is_32bit_elf
16621 ? sizeof (Elf32_External_Chdr)
16622 : sizeof (Elf64_External_Chdr)))
d8024a91 16623 {
55be8fd0 16624 warn (_("compressed section %s is too small to contain a compression header\n"),
d8024a91 16625 section->name);
015dc7e1 16626 return false;
d8024a91
NC
16627 }
16628
ebdf1ebf 16629 compression_header_size = get_compression_header (&chdr, start, size);
5844b465
NC
16630 if (compression_header_size == 0)
16631 /* An error message will have already been generated
16632 by get_compression_header. */
015dc7e1 16633 return false;
d8024a91 16634
89dbeac7 16635 if (chdr.ch_type == ch_compress_zlib)
1f5a3546
FS
16636 ;
16637#ifdef HAVE_ZSTD
89dbeac7 16638 else if (chdr.ch_type == ch_compress_zstd)
1f5a3546
FS
16639 is_zstd = true;
16640#endif
16641 else
813dabb9
L
16642 {
16643 warn (_("section '%s' has unsupported compress type: %d\n"),
16644 section->name, chdr.ch_type);
015dc7e1 16645 return false;
813dabb9 16646 }
dab394de 16647 uncompressed_size = chdr.ch_size;
77115a4a
L
16648 start += compression_header_size;
16649 size -= compression_header_size;
16650 }
dab394de
L
16651 else if (size > 12 && streq ((char *) start, "ZLIB"))
16652 {
16653 /* Read the zlib header. In this case, it should be "ZLIB"
16654 followed by the uncompressed section size, 8 bytes in
16655 big-endian order. */
16656 uncompressed_size = start[4]; uncompressed_size <<= 8;
16657 uncompressed_size += start[5]; uncompressed_size <<= 8;
16658 uncompressed_size += start[6]; uncompressed_size <<= 8;
16659 uncompressed_size += start[7]; uncompressed_size <<= 8;
16660 uncompressed_size += start[8]; uncompressed_size <<= 8;
16661 uncompressed_size += start[9]; uncompressed_size <<= 8;
16662 uncompressed_size += start[10]; uncompressed_size <<= 8;
16663 uncompressed_size += start[11];
16664 start += 12;
16665 size -= 12;
16666 }
16667
1835f746 16668 if (uncompressed_size)
77115a4a 16669 {
1f5a3546 16670 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
45f5fe46 16671 &size, filedata->file_size))
1835f746
NC
16672 {
16673 /* Free the compressed buffer, update the section buffer
16674 and the section size if uncompress is successful. */
16675 free (section->start);
16676 section->start = start;
16677 }
16678 else
16679 {
16680 error (_("Unable to decompress section %s\n"),
dda8d76d 16681 printable_section_name (filedata, sec));
015dc7e1 16682 return false;
1835f746 16683 }
77115a4a 16684 }
bc303e5d 16685
77115a4a 16686 section->size = size;
59245841 16687 }
4a114e3e 16688
1b315056 16689 if (section->start == NULL)
015dc7e1 16690 return false;
1b315056 16691
19e6b90e 16692 if (debug_displays [debug].relocate)
32ec8896 16693 {
dda8d76d 16694 if (! apply_relocations (filedata, sec, section->start, section->size,
32ec8896 16695 & section->reloc_info, & section->num_relocs))
015dc7e1 16696 return false;
32ec8896 16697 }
d1c4b12b
NC
16698 else
16699 {
16700 section->reloc_info = NULL;
16701 section->num_relocs = 0;
16702 }
1007acb3 16703
015dc7e1 16704 return true;
1007acb3
L
16705}
16706
301a9420
AM
16707#if HAVE_LIBDEBUGINFOD
16708/* Return a hex string representation of the build-id. */
16709unsigned char *
16710get_build_id (void * data)
16711{
ca0e11aa 16712 Filedata * filedata = (Filedata *) data;
301a9420 16713 Elf_Internal_Shdr * shdr;
26c527e6 16714 size_t i;
301a9420 16715
55be8fd0
NC
16716 /* Iterate through notes to find note.gnu.build-id.
16717 FIXME: Only the first note in any note section is examined. */
301a9420
AM
16718 for (i = 0, shdr = filedata->section_headers;
16719 i < filedata->file_header.e_shnum && shdr != NULL;
16720 i++, shdr++)
16721 {
16722 if (shdr->sh_type != SHT_NOTE)
16723 continue;
16724
16725 char * next;
16726 char * end;
16727 size_t data_remaining;
16728 size_t min_notesz;
16729 Elf_External_Note * enote;
16730 Elf_Internal_Note inote;
16731
625d49fc
AM
16732 uint64_t offset = shdr->sh_offset;
16733 uint64_t align = shdr->sh_addralign;
16734 uint64_t length = shdr->sh_size;
301a9420
AM
16735
16736 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
16737 if (enote == NULL)
16738 continue;
16739
16740 if (align < 4)
16741 align = 4;
16742 else if (align != 4 && align != 8)
f761cb13
AM
16743 {
16744 free (enote);
16745 continue;
16746 }
301a9420
AM
16747
16748 end = (char *) enote + length;
16749 data_remaining = end - (char *) enote;
16750
16751 if (!is_ia64_vms (filedata))
16752 {
16753 min_notesz = offsetof (Elf_External_Note, name);
16754 if (data_remaining < min_notesz)
16755 {
55be8fd0
NC
16756 warn (_("\
16757malformed note encountered in section %s whilst scanning for build-id note\n"),
16758 printable_section_name (filedata, shdr));
f761cb13 16759 free (enote);
55be8fd0 16760 continue;
301a9420
AM
16761 }
16762 data_remaining -= min_notesz;
16763
16764 inote.type = BYTE_GET (enote->type);
16765 inote.namesz = BYTE_GET (enote->namesz);
16766 inote.namedata = enote->name;
16767 inote.descsz = BYTE_GET (enote->descsz);
16768 inote.descdata = ((char *) enote
16769 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
16770 inote.descpos = offset + (inote.descdata - (char *) enote);
16771 next = ((char *) enote
16772 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
16773 }
16774 else
16775 {
16776 Elf64_External_VMS_Note *vms_enote;
16777
16778 /* PR binutils/15191
16779 Make sure that there is enough data to read. */
16780 min_notesz = offsetof (Elf64_External_VMS_Note, name);
16781 if (data_remaining < min_notesz)
16782 {
55be8fd0
NC
16783 warn (_("\
16784malformed note encountered in section %s whilst scanning for build-id note\n"),
16785 printable_section_name (filedata, shdr));
f761cb13 16786 free (enote);
55be8fd0 16787 continue;
301a9420
AM
16788 }
16789 data_remaining -= min_notesz;
16790
16791 vms_enote = (Elf64_External_VMS_Note *) enote;
16792 inote.type = BYTE_GET (vms_enote->type);
16793 inote.namesz = BYTE_GET (vms_enote->namesz);
16794 inote.namedata = vms_enote->name;
16795 inote.descsz = BYTE_GET (vms_enote->descsz);
16796 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
16797 inote.descpos = offset + (inote.descdata - (char *) enote);
16798 next = inote.descdata + align_power (inote.descsz, 3);
16799 }
16800
16801 /* Skip malformed notes. */
16802 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
16803 || (size_t) (inote.descdata - inote.namedata) > data_remaining
16804 || (size_t) (next - inote.descdata) < inote.descsz
16805 || ((size_t) (next - inote.descdata)
16806 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
16807 {
55be8fd0
NC
16808 warn (_("\
16809malformed note encountered in section %s whilst scanning for build-id note\n"),
16810 printable_section_name (filedata, shdr));
f761cb13 16811 free (enote);
301a9420
AM
16812 continue;
16813 }
16814
16815 /* Check if this is the build-id note. If so then convert the build-id
16816 bytes to a hex string. */
16817 if (inote.namesz > 0
24d127aa 16818 && startswith (inote.namedata, "GNU")
301a9420
AM
16819 && inote.type == NT_GNU_BUILD_ID)
16820 {
26c527e6 16821 size_t j;
301a9420
AM
16822 char * build_id;
16823
16824 build_id = malloc (inote.descsz * 2 + 1);
16825 if (build_id == NULL)
f761cb13
AM
16826 {
16827 free (enote);
16828 return NULL;
16829 }
301a9420
AM
16830
16831 for (j = 0; j < inote.descsz; ++j)
16832 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
16833 build_id[inote.descsz * 2] = '\0';
f761cb13 16834 free (enote);
301a9420 16835
55be8fd0 16836 return (unsigned char *) build_id;
301a9420 16837 }
f761cb13 16838 free (enote);
301a9420
AM
16839 }
16840
16841 return NULL;
16842}
16843#endif /* HAVE_LIBDEBUGINFOD */
16844
657d0d47
CC
16845/* If this is not NULL, load_debug_section will only look for sections
16846 within the list of sections given here. */
32ec8896 16847static unsigned int * section_subset = NULL;
657d0d47 16848
015dc7e1 16849bool
dda8d76d 16850load_debug_section (enum dwarf_section_display_enum debug, void * data)
d966045b 16851{
2cf0635d
NC
16852 struct dwarf_section * section = &debug_displays [debug].section;
16853 Elf_Internal_Shdr * sec;
dda8d76d
NC
16854 Filedata * filedata = (Filedata *) data;
16855
e1dbfc17
L
16856 if (!dump_any_debugging)
16857 return false;
16858
f425ec66
NC
16859 /* Without section headers we cannot find any sections. */
16860 if (filedata->section_headers == NULL)
015dc7e1 16861 return false;
f425ec66 16862
9c1ce108
AM
16863 if (filedata->string_table == NULL
16864 && filedata->file_header.e_shstrndx != SHN_UNDEF
16865 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
dda8d76d
NC
16866 {
16867 Elf_Internal_Shdr * strs;
16868
16869 /* Read in the string table, so that we have section names to scan. */
16870 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
16871
4dff97b2 16872 if (strs != NULL && strs->sh_size != 0)
dda8d76d 16873 {
9c1ce108
AM
16874 filedata->string_table
16875 = (char *) get_data (NULL, filedata, strs->sh_offset,
16876 1, strs->sh_size, _("string table"));
dda8d76d 16877
9c1ce108
AM
16878 filedata->string_table_length
16879 = filedata->string_table != NULL ? strs->sh_size : 0;
dda8d76d
NC
16880 }
16881 }
d966045b
DJ
16882
16883 /* Locate the debug section. */
dda8d76d 16884 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
d966045b
DJ
16885 if (sec != NULL)
16886 section->name = section->uncompressed_name;
16887 else
16888 {
dda8d76d 16889 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
d966045b
DJ
16890 if (sec != NULL)
16891 section->name = section->compressed_name;
16892 }
16893 if (sec == NULL)
015dc7e1 16894 return false;
d966045b 16895
657d0d47
CC
16896 /* If we're loading from a subset of sections, and we've loaded
16897 a section matching this name before, it's likely that it's a
16898 different one. */
16899 if (section_subset != NULL)
16900 free_debug_section (debug);
16901
dda8d76d 16902 return load_specific_debug_section (debug, sec, data);
d966045b
DJ
16903}
16904
19e6b90e
L
16905void
16906free_debug_section (enum dwarf_section_display_enum debug)
1007acb3 16907{
2cf0635d 16908 struct dwarf_section * section = &debug_displays [debug].section;
1007acb3 16909
19e6b90e
L
16910 if (section->start == NULL)
16911 return;
1007acb3 16912
19e6b90e
L
16913 free ((char *) section->start);
16914 section->start = NULL;
16915 section->address = 0;
16916 section->size = 0;
a788aedd 16917
9db70fc3
AM
16918 free (section->reloc_info);
16919 section->reloc_info = NULL;
16920 section->num_relocs = 0;
1007acb3
L
16921}
16922
015dc7e1 16923static bool
dda8d76d 16924display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
1007acb3 16925{
84714f86
AM
16926 const char *name = (section_name_valid (filedata, section)
16927 ? section_name (filedata, section) : "");
16928 const char *print_name = printable_section_name (filedata, section);
be7d229a 16929 uint64_t length;
015dc7e1 16930 bool result = true;
3f5e193b 16931 int i;
1007acb3 16932
19e6b90e
L
16933 length = section->sh_size;
16934 if (length == 0)
1007acb3 16935 {
74e1a04b 16936 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
015dc7e1 16937 return true;
1007acb3 16938 }
5dff79d8
NC
16939 if (section->sh_type == SHT_NOBITS)
16940 {
16941 /* There is no point in dumping the contents of a debugging section
16942 which has the NOBITS type - the bits in the file will be random.
16943 This can happen when a file containing a .eh_frame section is
16944 stripped with the --only-keep-debug command line option. */
74e1a04b
NC
16945 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
16946 print_name);
015dc7e1 16947 return false;
5dff79d8 16948 }
1007acb3 16949
24d127aa 16950 if (startswith (name, ".gnu.linkonce.wi."))
19e6b90e 16951 name = ".debug_info";
1007acb3 16952
19e6b90e
L
16953 /* See if we know how to display the contents of this section. */
16954 for (i = 0; i < max; i++)
d85bf2ba
NC
16955 {
16956 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
16957 struct dwarf_section_display * display = debug_displays + i;
16958 struct dwarf_section * sec = & display->section;
d966045b 16959
d85bf2ba 16960 if (streq (sec->uncompressed_name, name)
24d127aa 16961 || (id == line && startswith (name, ".debug_line."))
d85bf2ba
NC
16962 || streq (sec->compressed_name, name))
16963 {
015dc7e1 16964 bool secondary = (section != find_section (filedata, name));
1007acb3 16965
d85bf2ba
NC
16966 if (secondary)
16967 free_debug_section (id);
dda8d76d 16968
24d127aa 16969 if (i == line && startswith (name, ".debug_line."))
d85bf2ba
NC
16970 sec->name = name;
16971 else if (streq (sec->uncompressed_name, name))
16972 sec->name = sec->uncompressed_name;
16973 else
16974 sec->name = sec->compressed_name;
657d0d47 16975
d85bf2ba
NC
16976 if (load_specific_debug_section (id, section, filedata))
16977 {
16978 /* If this debug section is part of a CU/TU set in a .dwp file,
16979 restrict load_debug_section to the sections in that set. */
16980 section_subset = find_cu_tu_set (filedata, shndx);
1007acb3 16981
d85bf2ba 16982 result &= display->display (sec, filedata);
657d0d47 16983
d85bf2ba 16984 section_subset = NULL;
1007acb3 16985
44266f36 16986 if (secondary || (id != info && id != abbrev && id != debug_addr))
d85bf2ba
NC
16987 free_debug_section (id);
16988 }
16989 break;
16990 }
16991 }
1007acb3 16992
19e6b90e 16993 if (i == max)
1007acb3 16994 {
74e1a04b 16995 printf (_("Unrecognized debug section: %s\n"), print_name);
015dc7e1 16996 result = false;
1007acb3
L
16997 }
16998
19e6b90e 16999 return result;
5b18a4bc 17000}
103f02d3 17001
aef1f6d0
DJ
17002/* Set DUMP_SECTS for all sections where dumps were requested
17003 based on section name. */
17004
17005static void
dda8d76d 17006initialise_dumps_byname (Filedata * filedata)
aef1f6d0 17007{
2cf0635d 17008 struct dump_list_entry * cur;
aef1f6d0
DJ
17009
17010 for (cur = dump_sects_byname; cur; cur = cur->next)
17011 {
17012 unsigned int i;
015dc7e1 17013 bool any = false;
aef1f6d0 17014
dda8d76d 17015 for (i = 0; i < filedata->file_header.e_shnum; i++)
84714f86
AM
17016 if (section_name_valid (filedata, filedata->section_headers + i)
17017 && streq (section_name (filedata, filedata->section_headers + i),
17018 cur->name))
aef1f6d0 17019 {
6431e409 17020 request_dump_bynumber (&filedata->dump, i, cur->type);
015dc7e1 17021 any = true;
aef1f6d0
DJ
17022 }
17023
835f2fae
NC
17024 if (!any && !filedata->is_separate)
17025 warn (_("Section '%s' was not dumped because it does not exist\n"),
17026 cur->name);
aef1f6d0
DJ
17027 }
17028}
17029
015dc7e1 17030static bool
dda8d76d 17031process_section_contents (Filedata * filedata)
5b18a4bc 17032{
2cf0635d 17033 Elf_Internal_Shdr * section;
19e6b90e 17034 unsigned int i;
015dc7e1 17035 bool res = true;
103f02d3 17036
19e6b90e 17037 if (! do_dump)
015dc7e1 17038 return true;
103f02d3 17039
dda8d76d 17040 initialise_dumps_byname (filedata);
aef1f6d0 17041
dda8d76d 17042 for (i = 0, section = filedata->section_headers;
6431e409 17043 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
19e6b90e
L
17044 i++, section++)
17045 {
6431e409 17046 dump_type dump = filedata->dump.dump_sects[i];
dda8d76d 17047
d6bfbc39
NC
17048 if (filedata->is_separate && ! process_links)
17049 dump &= DEBUG_DUMP;
047c3dbf 17050
19e6b90e 17051#ifdef SUPPORT_DISASSEMBLY
dda8d76d
NC
17052 if (dump & DISASS_DUMP)
17053 {
17054 if (! disassemble_section (section, filedata))
015dc7e1 17055 res = false;
dda8d76d 17056 }
19e6b90e 17057#endif
dda8d76d 17058 if (dump & HEX_DUMP)
32ec8896 17059 {
015dc7e1
AM
17060 if (! dump_section_as_bytes (section, filedata, false))
17061 res = false;
32ec8896 17062 }
103f02d3 17063
dda8d76d 17064 if (dump & RELOC_DUMP)
32ec8896 17065 {
015dc7e1
AM
17066 if (! dump_section_as_bytes (section, filedata, true))
17067 res = false;
32ec8896 17068 }
09c11c86 17069
dda8d76d 17070 if (dump & STRING_DUMP)
32ec8896 17071 {
dda8d76d 17072 if (! dump_section_as_strings (section, filedata))
015dc7e1 17073 res = false;
32ec8896 17074 }
cf13d699 17075
dda8d76d 17076 if (dump & DEBUG_DUMP)
32ec8896 17077 {
dda8d76d 17078 if (! display_debug_section (i, section, filedata))
015dc7e1 17079 res = false;
32ec8896 17080 }
7d9813f1 17081
094e34f2 17082#ifdef ENABLE_LIBCTF
7d9813f1
NA
17083 if (dump & CTF_DUMP)
17084 {
17085 if (! dump_section_as_ctf (section, filedata))
015dc7e1 17086 res = false;
7d9813f1 17087 }
094e34f2 17088#endif
42b6953b
IB
17089 if (dump & SFRAME_DUMP)
17090 {
17091 if (! dump_section_as_sframe (section, filedata))
17092 res = false;
17093 }
5b18a4bc 17094 }
103f02d3 17095
835f2fae 17096 if (! filedata->is_separate)
0ee3043f 17097 {
835f2fae
NC
17098 /* Check to see if the user requested a
17099 dump of a section that does not exist. */
17100 for (; i < filedata->dump.num_dump_sects; i++)
17101 if (filedata->dump.dump_sects[i])
17102 {
ca0e11aa 17103 warn (_("Section %d was not dumped because it does not exist!\n"), i);
015dc7e1 17104 res = false;
835f2fae 17105 }
0ee3043f 17106 }
32ec8896
NC
17107
17108 return res;
5b18a4bc 17109}
103f02d3 17110
5b18a4bc 17111static void
19e6b90e 17112process_mips_fpe_exception (int mask)
5b18a4bc 17113{
19e6b90e
L
17114 if (mask)
17115 {
015dc7e1 17116 bool first = true;
32ec8896 17117
19e6b90e 17118 if (mask & OEX_FPU_INEX)
015dc7e1 17119 fputs ("INEX", stdout), first = false;
19e6b90e 17120 if (mask & OEX_FPU_UFLO)
015dc7e1 17121 printf ("%sUFLO", first ? "" : "|"), first = false;
19e6b90e 17122 if (mask & OEX_FPU_OFLO)
015dc7e1 17123 printf ("%sOFLO", first ? "" : "|"), first = false;
19e6b90e 17124 if (mask & OEX_FPU_DIV0)
015dc7e1 17125 printf ("%sDIV0", first ? "" : "|"), first = false;
19e6b90e
L
17126 if (mask & OEX_FPU_INVAL)
17127 printf ("%sINVAL", first ? "" : "|");
17128 }
5b18a4bc 17129 else
19e6b90e 17130 fputs ("0", stdout);
5b18a4bc 17131}
103f02d3 17132
f6f0e17b
NC
17133/* Display's the value of TAG at location P. If TAG is
17134 greater than 0 it is assumed to be an unknown tag, and
17135 a message is printed to this effect. Otherwise it is
17136 assumed that a message has already been printed.
17137
17138 If the bottom bit of TAG is set it assumed to have a
17139 string value, otherwise it is assumed to have an integer
17140 value.
17141
17142 Returns an updated P pointing to the first unread byte
17143 beyond the end of TAG's value.
17144
17145 Reads at or beyond END will not be made. */
17146
17147static unsigned char *
60abdbed 17148display_tag_value (signed int tag,
f6f0e17b
NC
17149 unsigned char * p,
17150 const unsigned char * const end)
17151{
26c527e6 17152 uint64_t val;
f6f0e17b
NC
17153
17154 if (tag > 0)
17155 printf (" Tag_unknown_%d: ", tag);
17156
17157 if (p >= end)
17158 {
4082ef84 17159 warn (_("<corrupt tag>\n"));
f6f0e17b
NC
17160 }
17161 else if (tag & 1)
17162 {
071436c6
NC
17163 /* PR 17531 file: 027-19978-0.004. */
17164 size_t maxlen = (end - p) - 1;
17165
17166 putchar ('"');
4082ef84
NC
17167 if (maxlen > 0)
17168 {
b6ac461a 17169 print_symbol_name ((int) maxlen, (const char *) p);
4082ef84
NC
17170 p += strnlen ((char *) p, maxlen) + 1;
17171 }
17172 else
17173 {
17174 printf (_("<corrupt string tag>"));
17175 p = (unsigned char *) end;
17176 }
071436c6 17177 printf ("\"\n");
f6f0e17b
NC
17178 }
17179 else
17180 {
cd30bcef 17181 READ_ULEB (val, p, end);
26c527e6 17182 printf ("%" PRId64 " (0x%" PRIx64 ")\n", val, val);
f6f0e17b
NC
17183 }
17184
4082ef84 17185 assert (p <= end);
f6f0e17b
NC
17186 return p;
17187}
17188
53a346d8
CZ
17189/* ARC ABI attributes section. */
17190
17191static unsigned char *
17192display_arc_attribute (unsigned char * p,
17193 const unsigned char * const end)
17194{
17195 unsigned int tag;
53a346d8
CZ
17196 unsigned int val;
17197
cd30bcef 17198 READ_ULEB (tag, p, end);
53a346d8
CZ
17199
17200 switch (tag)
17201 {
17202 case Tag_ARC_PCS_config:
cd30bcef 17203 READ_ULEB (val, p, end);
53a346d8
CZ
17204 printf (" Tag_ARC_PCS_config: ");
17205 switch (val)
17206 {
17207 case 0:
17208 printf (_("Absent/Non standard\n"));
17209 break;
17210 case 1:
17211 printf (_("Bare metal/mwdt\n"));
17212 break;
17213 case 2:
17214 printf (_("Bare metal/newlib\n"));
17215 break;
17216 case 3:
17217 printf (_("Linux/uclibc\n"));
17218 break;
17219 case 4:
17220 printf (_("Linux/glibc\n"));
17221 break;
17222 default:
17223 printf (_("Unknown\n"));
17224 break;
17225 }
17226 break;
17227
17228 case Tag_ARC_CPU_base:
cd30bcef 17229 READ_ULEB (val, p, end);
53a346d8
CZ
17230 printf (" Tag_ARC_CPU_base: ");
17231 switch (val)
17232 {
17233 default:
17234 case TAG_CPU_NONE:
17235 printf (_("Absent\n"));
17236 break;
17237 case TAG_CPU_ARC6xx:
17238 printf ("ARC6xx\n");
17239 break;
17240 case TAG_CPU_ARC7xx:
17241 printf ("ARC7xx\n");
17242 break;
17243 case TAG_CPU_ARCEM:
17244 printf ("ARCEM\n");
17245 break;
17246 case TAG_CPU_ARCHS:
17247 printf ("ARCHS\n");
17248 break;
17249 }
17250 break;
17251
17252 case Tag_ARC_CPU_variation:
cd30bcef 17253 READ_ULEB (val, p, end);
53a346d8
CZ
17254 printf (" Tag_ARC_CPU_variation: ");
17255 switch (val)
17256 {
17257 default:
17258 if (val > 0 && val < 16)
53a346d8 17259 printf ("Core%d\n", val);
d8cbc93b
JL
17260 else
17261 printf ("Unknown\n");
17262 break;
17263
53a346d8
CZ
17264 case 0:
17265 printf (_("Absent\n"));
17266 break;
17267 }
17268 break;
17269
17270 case Tag_ARC_CPU_name:
17271 printf (" Tag_ARC_CPU_name: ");
17272 p = display_tag_value (-1, p, end);
17273 break;
17274
17275 case Tag_ARC_ABI_rf16:
cd30bcef 17276 READ_ULEB (val, p, end);
53a346d8
CZ
17277 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
17278 break;
17279
17280 case Tag_ARC_ABI_osver:
cd30bcef 17281 READ_ULEB (val, p, end);
53a346d8
CZ
17282 printf (" Tag_ARC_ABI_osver: v%d\n", val);
17283 break;
17284
17285 case Tag_ARC_ABI_pic:
17286 case Tag_ARC_ABI_sda:
cd30bcef 17287 READ_ULEB (val, p, end);
53a346d8
CZ
17288 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
17289 : " Tag_ARC_ABI_pic: ");
17290 switch (val)
17291 {
17292 case 0:
17293 printf (_("Absent\n"));
17294 break;
17295 case 1:
17296 printf ("MWDT\n");
17297 break;
17298 case 2:
17299 printf ("GNU\n");
17300 break;
17301 default:
17302 printf (_("Unknown\n"));
17303 break;
17304 }
17305 break;
17306
17307 case Tag_ARC_ABI_tls:
cd30bcef 17308 READ_ULEB (val, p, end);
53a346d8
CZ
17309 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
17310 break;
17311
17312 case Tag_ARC_ABI_enumsize:
cd30bcef 17313 READ_ULEB (val, p, end);
53a346d8
CZ
17314 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
17315 _("smallest"));
17316 break;
17317
17318 case Tag_ARC_ABI_exceptions:
cd30bcef 17319 READ_ULEB (val, p, end);
53a346d8
CZ
17320 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
17321 : _("default"));
17322 break;
17323
17324 case Tag_ARC_ABI_double_size:
cd30bcef 17325 READ_ULEB (val, p, end);
53a346d8
CZ
17326 printf (" Tag_ARC_ABI_double_size: %d\n", val);
17327 break;
17328
17329 case Tag_ARC_ISA_config:
17330 printf (" Tag_ARC_ISA_config: ");
17331 p = display_tag_value (-1, p, end);
17332 break;
17333
17334 case Tag_ARC_ISA_apex:
17335 printf (" Tag_ARC_ISA_apex: ");
17336 p = display_tag_value (-1, p, end);
17337 break;
17338
17339 case Tag_ARC_ISA_mpy_option:
cd30bcef 17340 READ_ULEB (val, p, end);
53a346d8
CZ
17341 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
17342 break;
17343
db1e1b45 17344 case Tag_ARC_ATR_version:
cd30bcef 17345 READ_ULEB (val, p, end);
db1e1b45 17346 printf (" Tag_ARC_ATR_version: %d\n", val);
17347 break;
17348
53a346d8
CZ
17349 default:
17350 return display_tag_value (tag & 1, p, end);
17351 }
17352
17353 return p;
17354}
17355
11c1ff18
PB
17356/* ARM EABI attributes section. */
17357typedef struct
17358{
70e99720 17359 unsigned int tag;
2cf0635d 17360 const char * name;
11c1ff18 17361 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
70e99720 17362 unsigned int type;
288f0ba2 17363 const char *const *table;
11c1ff18
PB
17364} arm_attr_public_tag;
17365
288f0ba2 17366static const char *const arm_attr_tag_CPU_arch[] =
11c1ff18 17367 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
ced40572 17368 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
3197e593
PW
17369 "v8-M.mainline", "v8.1-A", "v8.2-A", "v8.3-A",
17370 "v8.1-M.mainline", "v9"};
288f0ba2
AM
17371static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
17372static const char *const arm_attr_tag_THUMB_ISA_use[] =
4ed7ed8d 17373 {"No", "Thumb-1", "Thumb-2", "Yes"};
288f0ba2 17374static const char *const arm_attr_tag_FP_arch[] =
bca38921 17375 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
a715796b 17376 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
288f0ba2
AM
17377static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
17378static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
9411fd44
MW
17379 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
17380 "NEON for ARMv8.1"};
288f0ba2 17381static const char *const arm_attr_tag_PCS_config[] =
11c1ff18
PB
17382 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
17383 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
288f0ba2 17384static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
11c1ff18 17385 {"V6", "SB", "TLS", "Unused"};
288f0ba2 17386static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
11c1ff18 17387 {"Absolute", "PC-relative", "SB-relative", "None"};
288f0ba2 17388static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
11c1ff18 17389 {"Absolute", "PC-relative", "None"};
288f0ba2 17390static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
11c1ff18 17391 {"None", "direct", "GOT-indirect"};
288f0ba2 17392static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
11c1ff18 17393 {"None", "??? 1", "2", "??? 3", "4"};
288f0ba2
AM
17394static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
17395static const char *const arm_attr_tag_ABI_FP_denormal[] =
f5f53991 17396 {"Unused", "Needed", "Sign only"};
288f0ba2
AM
17397static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
17398static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
17399static const char *const arm_attr_tag_ABI_FP_number_model[] =
11c1ff18 17400 {"Unused", "Finite", "RTABI", "IEEE 754"};
288f0ba2 17401static const char *const arm_attr_tag_ABI_enum_size[] =
11c1ff18 17402 {"Unused", "small", "int", "forced to int"};
288f0ba2 17403static const char *const arm_attr_tag_ABI_HardFP_use[] =
99654aaf 17404 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
288f0ba2 17405static const char *const arm_attr_tag_ABI_VFP_args[] =
5c294fee 17406 {"AAPCS", "VFP registers", "custom", "compatible"};
288f0ba2 17407static const char *const arm_attr_tag_ABI_WMMX_args[] =
11c1ff18 17408 {"AAPCS", "WMMX registers", "custom"};
288f0ba2 17409static const char *const arm_attr_tag_ABI_optimization_goals[] =
11c1ff18
PB
17410 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
17411 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
288f0ba2 17412static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
11c1ff18
PB
17413 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
17414 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
288f0ba2
AM
17415static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
17416static const char *const arm_attr_tag_FP_HP_extension[] =
8e79c3df 17417 {"Not Allowed", "Allowed"};
288f0ba2 17418static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
8e79c3df 17419 {"None", "IEEE 754", "Alternative Format"};
288f0ba2 17420static const char *const arm_attr_tag_DSP_extension[] =
15afaa63 17421 {"Follow architecture", "Allowed"};
288f0ba2 17422static const char *const arm_attr_tag_MPextension_use[] =
cd21e546 17423 {"Not Allowed", "Allowed"};
288f0ba2 17424static const char *const arm_attr_tag_DIV_use[] =
dd24e3da 17425 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
cd21e546 17426 "Allowed in v7-A with integer division extension"};
288f0ba2
AM
17427static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
17428static const char *const arm_attr_tag_Virtualization_use[] =
dd24e3da 17429 {"Not Allowed", "TrustZone", "Virtualization Extensions",
cd21e546 17430 "TrustZone and Virtualization Extensions"};
288f0ba2 17431static const char *const arm_attr_tag_MPextension_use_legacy[] =
f5f53991 17432 {"Not Allowed", "Allowed"};
11c1ff18 17433
288f0ba2 17434static const char *const arm_attr_tag_MVE_arch[] =
a7ad558c
AV
17435 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
17436
99db83d0
AC
17437static const char * arm_attr_tag_PAC_extension[] =
17438 {"No PAC/AUT instructions",
17439 "PAC/AUT instructions permitted in the NOP space",
17440 "PAC/AUT instructions permitted in the NOP and in the non-NOP space"};
17441
4b535030
AC
17442static const char * arm_attr_tag_BTI_extension[] =
17443 {"BTI instructions not permitted",
17444 "BTI instructions permitted in the NOP space",
17445 "BTI instructions permitted in the NOP and in the non-NOP space"};
17446
b81ee92f
AC
17447static const char * arm_attr_tag_BTI_use[] =
17448 {"Compiled without branch target enforcement",
17449 "Compiled with branch target enforcement"};
17450
c9fed665
AC
17451static const char * arm_attr_tag_PACRET_use[] =
17452 {"Compiled without return address signing and authentication",
17453 "Compiled with return address signing and authentication"};
17454
11c1ff18
PB
17455#define LOOKUP(id, name) \
17456 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
d70c5fc7 17457static arm_attr_public_tag arm_attr_public_tags[] =
11c1ff18
PB
17458{
17459 {4, "CPU_raw_name", 1, NULL},
17460 {5, "CPU_name", 1, NULL},
17461 LOOKUP(6, CPU_arch),
17462 {7, "CPU_arch_profile", 0, NULL},
17463 LOOKUP(8, ARM_ISA_use),
17464 LOOKUP(9, THUMB_ISA_use),
75375b3e 17465 LOOKUP(10, FP_arch),
11c1ff18 17466 LOOKUP(11, WMMX_arch),
f5f53991
AS
17467 LOOKUP(12, Advanced_SIMD_arch),
17468 LOOKUP(13, PCS_config),
11c1ff18
PB
17469 LOOKUP(14, ABI_PCS_R9_use),
17470 LOOKUP(15, ABI_PCS_RW_data),
f5f53991 17471 LOOKUP(16, ABI_PCS_RO_data),
11c1ff18
PB
17472 LOOKUP(17, ABI_PCS_GOT_use),
17473 LOOKUP(18, ABI_PCS_wchar_t),
17474 LOOKUP(19, ABI_FP_rounding),
17475 LOOKUP(20, ABI_FP_denormal),
17476 LOOKUP(21, ABI_FP_exceptions),
17477 LOOKUP(22, ABI_FP_user_exceptions),
17478 LOOKUP(23, ABI_FP_number_model),
75375b3e
MGD
17479 {24, "ABI_align_needed", 0, NULL},
17480 {25, "ABI_align_preserved", 0, NULL},
11c1ff18
PB
17481 LOOKUP(26, ABI_enum_size),
17482 LOOKUP(27, ABI_HardFP_use),
17483 LOOKUP(28, ABI_VFP_args),
17484 LOOKUP(29, ABI_WMMX_args),
17485 LOOKUP(30, ABI_optimization_goals),
17486 LOOKUP(31, ABI_FP_optimization_goals),
8e79c3df 17487 {32, "compatibility", 0, NULL},
f5f53991 17488 LOOKUP(34, CPU_unaligned_access),
75375b3e 17489 LOOKUP(36, FP_HP_extension),
8e79c3df 17490 LOOKUP(38, ABI_FP_16bit_format),
cd21e546
MGD
17491 LOOKUP(42, MPextension_use),
17492 LOOKUP(44, DIV_use),
15afaa63 17493 LOOKUP(46, DSP_extension),
a7ad558c 17494 LOOKUP(48, MVE_arch),
99db83d0 17495 LOOKUP(50, PAC_extension),
4b535030 17496 LOOKUP(52, BTI_extension),
b81ee92f 17497 LOOKUP(74, BTI_use),
c9fed665 17498 LOOKUP(76, PACRET_use),
f5f53991
AS
17499 {64, "nodefaults", 0, NULL},
17500 {65, "also_compatible_with", 0, NULL},
17501 LOOKUP(66, T2EE_use),
17502 {67, "conformance", 1, NULL},
17503 LOOKUP(68, Virtualization_use),
cd21e546 17504 LOOKUP(70, MPextension_use_legacy)
11c1ff18
PB
17505};
17506#undef LOOKUP
17507
11c1ff18 17508static unsigned char *
f6f0e17b
NC
17509display_arm_attribute (unsigned char * p,
17510 const unsigned char * const end)
11c1ff18 17511{
70e99720 17512 unsigned int tag;
70e99720 17513 unsigned int val;
2cf0635d 17514 arm_attr_public_tag * attr;
11c1ff18 17515 unsigned i;
70e99720 17516 unsigned int type;
11c1ff18 17517
cd30bcef 17518 READ_ULEB (tag, p, end);
11c1ff18 17519 attr = NULL;
2cf0635d 17520 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
11c1ff18
PB
17521 {
17522 if (arm_attr_public_tags[i].tag == tag)
17523 {
17524 attr = &arm_attr_public_tags[i];
17525 break;
17526 }
17527 }
17528
17529 if (attr)
17530 {
17531 printf (" Tag_%s: ", attr->name);
17532 switch (attr->type)
17533 {
17534 case 0:
17535 switch (tag)
17536 {
17537 case 7: /* Tag_CPU_arch_profile. */
cd30bcef 17538 READ_ULEB (val, p, end);
11c1ff18
PB
17539 switch (val)
17540 {
2b692964
NC
17541 case 0: printf (_("None\n")); break;
17542 case 'A': printf (_("Application\n")); break;
17543 case 'R': printf (_("Realtime\n")); break;
17544 case 'M': printf (_("Microcontroller\n")); break;
17545 case 'S': printf (_("Application or Realtime\n")); break;
11c1ff18
PB
17546 default: printf ("??? (%d)\n", val); break;
17547 }
17548 break;
17549
75375b3e 17550 case 24: /* Tag_align_needed. */
cd30bcef 17551 READ_ULEB (val, p, end);
75375b3e
MGD
17552 switch (val)
17553 {
2b692964
NC
17554 case 0: printf (_("None\n")); break;
17555 case 1: printf (_("8-byte\n")); break;
17556 case 2: printf (_("4-byte\n")); break;
75375b3e
MGD
17557 case 3: printf ("??? 3\n"); break;
17558 default:
17559 if (val <= 12)
dd24e3da 17560 printf (_("8-byte and up to %d-byte extended\n"),
75375b3e
MGD
17561 1 << val);
17562 else
17563 printf ("??? (%d)\n", val);
17564 break;
17565 }
17566 break;
17567
17568 case 25: /* Tag_align_preserved. */
cd30bcef 17569 READ_ULEB (val, p, end);
75375b3e
MGD
17570 switch (val)
17571 {
2b692964
NC
17572 case 0: printf (_("None\n")); break;
17573 case 1: printf (_("8-byte, except leaf SP\n")); break;
17574 case 2: printf (_("8-byte\n")); break;
75375b3e
MGD
17575 case 3: printf ("??? 3\n"); break;
17576 default:
17577 if (val <= 12)
dd24e3da 17578 printf (_("8-byte and up to %d-byte extended\n"),
75375b3e
MGD
17579 1 << val);
17580 else
17581 printf ("??? (%d)\n", val);
17582 break;
17583 }
17584 break;
17585
11c1ff18 17586 case 32: /* Tag_compatibility. */
071436c6 17587 {
cd30bcef 17588 READ_ULEB (val, p, end);
071436c6 17589 printf (_("flag = %d, vendor = "), val);
4082ef84
NC
17590 if (p < end - 1)
17591 {
17592 size_t maxlen = (end - p) - 1;
17593
b6ac461a 17594 print_symbol_name ((int) maxlen, (const char *) p);
4082ef84
NC
17595 p += strnlen ((char *) p, maxlen) + 1;
17596 }
17597 else
17598 {
17599 printf (_("<corrupt>"));
17600 p = (unsigned char *) end;
17601 }
071436c6 17602 putchar ('\n');
071436c6 17603 }
11c1ff18
PB
17604 break;
17605
f5f53991 17606 case 64: /* Tag_nodefaults. */
541a3cbd
NC
17607 /* PR 17531: file: 001-505008-0.01. */
17608 if (p < end)
17609 p++;
2b692964 17610 printf (_("True\n"));
f5f53991
AS
17611 break;
17612
17613 case 65: /* Tag_also_compatible_with. */
cd30bcef 17614 READ_ULEB (val, p, end);
f5f53991
AS
17615 if (val == 6 /* Tag_CPU_arch. */)
17616 {
cd30bcef 17617 READ_ULEB (val, p, end);
071436c6 17618 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
f5f53991
AS
17619 printf ("??? (%d)\n", val);
17620 else
17621 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
17622 }
17623 else
17624 printf ("???\n");
071436c6
NC
17625 while (p < end && *(p++) != '\0' /* NUL terminator. */)
17626 ;
f5f53991
AS
17627 break;
17628
11c1ff18 17629 default:
bee0ee85
NC
17630 printf (_("<unknown: %d>\n"), tag);
17631 break;
11c1ff18
PB
17632 }
17633 return p;
17634
17635 case 1:
f6f0e17b 17636 return display_tag_value (-1, p, end);
11c1ff18 17637 case 2:
f6f0e17b 17638 return display_tag_value (0, p, end);
11c1ff18
PB
17639
17640 default:
17641 assert (attr->type & 0x80);
cd30bcef 17642 READ_ULEB (val, p, end);
11c1ff18
PB
17643 type = attr->type & 0x7f;
17644 if (val >= type)
17645 printf ("??? (%d)\n", val);
17646 else
17647 printf ("%s\n", attr->table[val]);
17648 return p;
17649 }
17650 }
11c1ff18 17651
f6f0e17b 17652 return display_tag_value (tag, p, end);
11c1ff18
PB
17653}
17654
104d59d1 17655static unsigned char *
60bca95a 17656display_gnu_attribute (unsigned char * p,
60abdbed 17657 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
f6f0e17b 17658 const unsigned char * const end)
104d59d1 17659{
cd30bcef 17660 unsigned int tag;
60abdbed 17661 unsigned int val;
104d59d1 17662
cd30bcef 17663 READ_ULEB (tag, p, end);
104d59d1
JM
17664
17665 /* Tag_compatibility is the only generic GNU attribute defined at
17666 present. */
17667 if (tag == 32)
17668 {
cd30bcef 17669 READ_ULEB (val, p, end);
071436c6
NC
17670
17671 printf (_("flag = %d, vendor = "), val);
f6f0e17b
NC
17672 if (p == end)
17673 {
071436c6 17674 printf (_("<corrupt>\n"));
f6f0e17b
NC
17675 warn (_("corrupt vendor attribute\n"));
17676 }
17677 else
17678 {
4082ef84
NC
17679 if (p < end - 1)
17680 {
17681 size_t maxlen = (end - p) - 1;
071436c6 17682
b6ac461a 17683 print_symbol_name ((int) maxlen, (const char *) p);
4082ef84
NC
17684 p += strnlen ((char *) p, maxlen) + 1;
17685 }
17686 else
17687 {
17688 printf (_("<corrupt>"));
17689 p = (unsigned char *) end;
17690 }
071436c6 17691 putchar ('\n');
f6f0e17b 17692 }
104d59d1
JM
17693 return p;
17694 }
17695
17696 if ((tag & 2) == 0 && display_proc_gnu_attribute)
f6f0e17b 17697 return display_proc_gnu_attribute (p, tag, end);
104d59d1 17698
f6f0e17b 17699 return display_tag_value (tag, p, end);
104d59d1
JM
17700}
17701
85f7484a
PB
17702static unsigned char *
17703display_m68k_gnu_attribute (unsigned char * p,
17704 unsigned int tag,
17705 const unsigned char * const end)
17706{
17707 unsigned int val;
17708
17709 if (tag == Tag_GNU_M68K_ABI_FP)
17710 {
17711 printf (" Tag_GNU_M68K_ABI_FP: ");
17712 if (p == end)
17713 {
17714 printf (_("<corrupt>\n"));
17715 return p;
17716 }
17717 READ_ULEB (val, p, end);
17718
17719 if (val > 3)
17720 printf ("(%#x), ", val);
17721
17722 switch (val & 3)
17723 {
17724 case 0:
17725 printf (_("unspecified hard/soft float\n"));
17726 break;
17727 case 1:
17728 printf (_("hard float\n"));
17729 break;
17730 case 2:
17731 printf (_("soft float\n"));
17732 break;
17733 }
17734 return p;
17735 }
17736
17737 return display_tag_value (tag & 1, p, end);
17738}
17739
34c8bcba 17740static unsigned char *
f6f0e17b 17741display_power_gnu_attribute (unsigned char * p,
60abdbed 17742 unsigned int tag,
f6f0e17b 17743 const unsigned char * const end)
34c8bcba 17744{
005d79fd 17745 unsigned int val;
34c8bcba
JM
17746
17747 if (tag == Tag_GNU_Power_ABI_FP)
17748 {
34c8bcba 17749 printf (" Tag_GNU_Power_ABI_FP: ");
cd30bcef 17750 if (p == end)
005d79fd
AM
17751 {
17752 printf (_("<corrupt>\n"));
17753 return p;
17754 }
cd30bcef 17755 READ_ULEB (val, p, end);
60bca95a 17756
005d79fd
AM
17757 if (val > 15)
17758 printf ("(%#x), ", val);
17759
17760 switch (val & 3)
34c8bcba
JM
17761 {
17762 case 0:
005d79fd 17763 printf (_("unspecified hard/soft float, "));
34c8bcba
JM
17764 break;
17765 case 1:
005d79fd 17766 printf (_("hard float, "));
34c8bcba
JM
17767 break;
17768 case 2:
005d79fd 17769 printf (_("soft float, "));
34c8bcba 17770 break;
3c7b9897 17771 case 3:
005d79fd 17772 printf (_("single-precision hard float, "));
3c7b9897 17773 break;
005d79fd
AM
17774 }
17775
17776 switch (val & 0xC)
17777 {
17778 case 0:
17779 printf (_("unspecified long double\n"));
17780 break;
17781 case 4:
17782 printf (_("128-bit IBM long double\n"));
17783 break;
17784 case 8:
17785 printf (_("64-bit long double\n"));
17786 break;
17787 case 12:
17788 printf (_("128-bit IEEE long double\n"));
34c8bcba
JM
17789 break;
17790 }
17791 return p;
005d79fd 17792 }
34c8bcba 17793
c6e65352
DJ
17794 if (tag == Tag_GNU_Power_ABI_Vector)
17795 {
c6e65352 17796 printf (" Tag_GNU_Power_ABI_Vector: ");
cd30bcef 17797 if (p == end)
005d79fd
AM
17798 {
17799 printf (_("<corrupt>\n"));
17800 return p;
17801 }
cd30bcef 17802 READ_ULEB (val, p, end);
005d79fd
AM
17803
17804 if (val > 3)
17805 printf ("(%#x), ", val);
17806
17807 switch (val & 3)
c6e65352
DJ
17808 {
17809 case 0:
005d79fd 17810 printf (_("unspecified\n"));
c6e65352
DJ
17811 break;
17812 case 1:
005d79fd 17813 printf (_("generic\n"));
c6e65352
DJ
17814 break;
17815 case 2:
17816 printf ("AltiVec\n");
17817 break;
17818 case 3:
17819 printf ("SPE\n");
17820 break;
c6e65352
DJ
17821 }
17822 return p;
005d79fd 17823 }
c6e65352 17824
f82e0623
NF
17825 if (tag == Tag_GNU_Power_ABI_Struct_Return)
17826 {
005d79fd 17827 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
cd30bcef 17828 if (p == end)
f6f0e17b 17829 {
005d79fd 17830 printf (_("<corrupt>\n"));
f6f0e17b
NC
17831 return p;
17832 }
cd30bcef 17833 READ_ULEB (val, p, end);
0b4362b0 17834
005d79fd
AM
17835 if (val > 2)
17836 printf ("(%#x), ", val);
17837
17838 switch (val & 3)
17839 {
17840 case 0:
17841 printf (_("unspecified\n"));
17842 break;
17843 case 1:
17844 printf ("r3/r4\n");
17845 break;
17846 case 2:
17847 printf (_("memory\n"));
17848 break;
17849 case 3:
17850 printf ("???\n");
17851 break;
17852 }
f82e0623
NF
17853 return p;
17854 }
17855
f6f0e17b 17856 return display_tag_value (tag & 1, p, end);
34c8bcba
JM
17857}
17858
643f7afb
AK
17859static unsigned char *
17860display_s390_gnu_attribute (unsigned char * p,
60abdbed 17861 unsigned int tag,
643f7afb
AK
17862 const unsigned char * const end)
17863{
cd30bcef 17864 unsigned int val;
643f7afb
AK
17865
17866 if (tag == Tag_GNU_S390_ABI_Vector)
17867 {
643f7afb 17868 printf (" Tag_GNU_S390_ABI_Vector: ");
cd30bcef 17869 READ_ULEB (val, p, end);
643f7afb
AK
17870
17871 switch (val)
17872 {
17873 case 0:
17874 printf (_("any\n"));
17875 break;
17876 case 1:
17877 printf (_("software\n"));
17878 break;
17879 case 2:
17880 printf (_("hardware\n"));
17881 break;
17882 default:
17883 printf ("??? (%d)\n", val);
17884 break;
17885 }
17886 return p;
17887 }
17888
17889 return display_tag_value (tag & 1, p, end);
17890}
17891
9e8c70f9 17892static void
60abdbed 17893display_sparc_hwcaps (unsigned int mask)
9e8c70f9
DM
17894{
17895 if (mask)
17896 {
015dc7e1 17897 bool first = true;
071436c6 17898
9e8c70f9 17899 if (mask & ELF_SPARC_HWCAP_MUL32)
015dc7e1 17900 fputs ("mul32", stdout), first = false;
9e8c70f9 17901 if (mask & ELF_SPARC_HWCAP_DIV32)
015dc7e1 17902 printf ("%sdiv32", first ? "" : "|"), first = false;
9e8c70f9 17903 if (mask & ELF_SPARC_HWCAP_FSMULD)
015dc7e1 17904 printf ("%sfsmuld", first ? "" : "|"), first = false;
9e8c70f9 17905 if (mask & ELF_SPARC_HWCAP_V8PLUS)
015dc7e1 17906 printf ("%sv8plus", first ? "" : "|"), first = false;
9e8c70f9 17907 if (mask & ELF_SPARC_HWCAP_POPC)
015dc7e1 17908 printf ("%spopc", first ? "" : "|"), first = false;
9e8c70f9 17909 if (mask & ELF_SPARC_HWCAP_VIS)
015dc7e1 17910 printf ("%svis", first ? "" : "|"), first = false;
9e8c70f9 17911 if (mask & ELF_SPARC_HWCAP_VIS2)
015dc7e1 17912 printf ("%svis2", first ? "" : "|"), first = false;
9e8c70f9 17913 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
015dc7e1 17914 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
9e8c70f9 17915 if (mask & ELF_SPARC_HWCAP_FMAF)
015dc7e1 17916 printf ("%sfmaf", first ? "" : "|"), first = false;
9e8c70f9 17917 if (mask & ELF_SPARC_HWCAP_VIS3)
015dc7e1 17918 printf ("%svis3", first ? "" : "|"), first = false;
9e8c70f9 17919 if (mask & ELF_SPARC_HWCAP_HPC)
015dc7e1 17920 printf ("%shpc", first ? "" : "|"), first = false;
9e8c70f9 17921 if (mask & ELF_SPARC_HWCAP_RANDOM)
015dc7e1 17922 printf ("%srandom", first ? "" : "|"), first = false;
9e8c70f9 17923 if (mask & ELF_SPARC_HWCAP_TRANS)
015dc7e1 17924 printf ("%strans", first ? "" : "|"), first = false;
9e8c70f9 17925 if (mask & ELF_SPARC_HWCAP_FJFMAU)
015dc7e1 17926 printf ("%sfjfmau", first ? "" : "|"), first = false;
9e8c70f9 17927 if (mask & ELF_SPARC_HWCAP_IMA)
015dc7e1 17928 printf ("%sima", first ? "" : "|"), first = false;
9e8c70f9 17929 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
015dc7e1 17930 printf ("%scspare", first ? "" : "|"), first = false;
9e8c70f9
DM
17931 }
17932 else
071436c6
NC
17933 fputc ('0', stdout);
17934 fputc ('\n', stdout);
9e8c70f9
DM
17935}
17936
3d68f91c 17937static void
60abdbed 17938display_sparc_hwcaps2 (unsigned int mask)
3d68f91c
JM
17939{
17940 if (mask)
17941 {
015dc7e1 17942 bool first = true;
071436c6 17943
3d68f91c 17944 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
015dc7e1 17945 fputs ("fjathplus", stdout), first = false;
3d68f91c 17946 if (mask & ELF_SPARC_HWCAP2_VIS3B)
015dc7e1 17947 printf ("%svis3b", first ? "" : "|"), first = false;
3d68f91c 17948 if (mask & ELF_SPARC_HWCAP2_ADP)
015dc7e1 17949 printf ("%sadp", first ? "" : "|"), first = false;
3d68f91c 17950 if (mask & ELF_SPARC_HWCAP2_SPARC5)
015dc7e1 17951 printf ("%ssparc5", first ? "" : "|"), first = false;
3d68f91c 17952 if (mask & ELF_SPARC_HWCAP2_MWAIT)
015dc7e1 17953 printf ("%smwait", first ? "" : "|"), first = false;
3d68f91c 17954 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
015dc7e1 17955 printf ("%sxmpmul", first ? "" : "|"), first = false;
3d68f91c 17956 if (mask & ELF_SPARC_HWCAP2_XMONT)
015dc7e1 17957 printf ("%sxmont2", first ? "" : "|"), first = false;
3d68f91c 17958 if (mask & ELF_SPARC_HWCAP2_NSEC)
015dc7e1 17959 printf ("%snsec", first ? "" : "|"), first = false;
3d68f91c 17960 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
015dc7e1 17961 printf ("%sfjathhpc", first ? "" : "|"), first = false;
3d68f91c 17962 if (mask & ELF_SPARC_HWCAP2_FJDES)
015dc7e1 17963 printf ("%sfjdes", first ? "" : "|"), first = false;
3d68f91c 17964 if (mask & ELF_SPARC_HWCAP2_FJAES)
015dc7e1 17965 printf ("%sfjaes", first ? "" : "|"), first = false;
3d68f91c
JM
17966 }
17967 else
071436c6
NC
17968 fputc ('0', stdout);
17969 fputc ('\n', stdout);
3d68f91c
JM
17970}
17971
9e8c70f9 17972static unsigned char *
f6f0e17b 17973display_sparc_gnu_attribute (unsigned char * p,
60abdbed 17974 unsigned int tag,
f6f0e17b 17975 const unsigned char * const end)
9e8c70f9 17976{
cd30bcef 17977 unsigned int val;
3d68f91c 17978
9e8c70f9
DM
17979 if (tag == Tag_GNU_Sparc_HWCAPS)
17980 {
cd30bcef 17981 READ_ULEB (val, p, end);
9e8c70f9 17982 printf (" Tag_GNU_Sparc_HWCAPS: ");
9e8c70f9
DM
17983 display_sparc_hwcaps (val);
17984 return p;
3d68f91c
JM
17985 }
17986 if (tag == Tag_GNU_Sparc_HWCAPS2)
17987 {
cd30bcef 17988 READ_ULEB (val, p, end);
3d68f91c
JM
17989 printf (" Tag_GNU_Sparc_HWCAPS2: ");
17990 display_sparc_hwcaps2 (val);
17991 return p;
17992 }
9e8c70f9 17993
f6f0e17b 17994 return display_tag_value (tag, p, end);
9e8c70f9
DM
17995}
17996
351cdf24 17997static void
32ec8896 17998print_mips_fp_abi_value (unsigned int val)
351cdf24
MF
17999{
18000 switch (val)
18001 {
18002 case Val_GNU_MIPS_ABI_FP_ANY:
18003 printf (_("Hard or soft float\n"));
18004 break;
18005 case Val_GNU_MIPS_ABI_FP_DOUBLE:
18006 printf (_("Hard float (double precision)\n"));
18007 break;
18008 case Val_GNU_MIPS_ABI_FP_SINGLE:
18009 printf (_("Hard float (single precision)\n"));
18010 break;
18011 case Val_GNU_MIPS_ABI_FP_SOFT:
18012 printf (_("Soft float\n"));
18013 break;
18014 case Val_GNU_MIPS_ABI_FP_OLD_64:
18015 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
18016 break;
18017 case Val_GNU_MIPS_ABI_FP_XX:
18018 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
18019 break;
18020 case Val_GNU_MIPS_ABI_FP_64:
18021 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
18022 break;
18023 case Val_GNU_MIPS_ABI_FP_64A:
18024 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
18025 break;
3350cc01
CM
18026 case Val_GNU_MIPS_ABI_FP_NAN2008:
18027 printf (_("NaN 2008 compatibility\n"));
18028 break;
351cdf24
MF
18029 default:
18030 printf ("??? (%d)\n", val);
18031 break;
18032 }
18033}
18034
2cf19d5c 18035static unsigned char *
f6f0e17b 18036display_mips_gnu_attribute (unsigned char * p,
60abdbed 18037 unsigned int tag,
f6f0e17b 18038 const unsigned char * const end)
2cf19d5c 18039{
2cf19d5c
JM
18040 if (tag == Tag_GNU_MIPS_ABI_FP)
18041 {
32ec8896 18042 unsigned int val;
f6f0e17b 18043
2cf19d5c 18044 printf (" Tag_GNU_MIPS_ABI_FP: ");
cd30bcef 18045 READ_ULEB (val, p, end);
351cdf24 18046 print_mips_fp_abi_value (val);
2cf19d5c
JM
18047 return p;
18048 }
18049
a9f58168
CF
18050 if (tag == Tag_GNU_MIPS_ABI_MSA)
18051 {
32ec8896 18052 unsigned int val;
a9f58168 18053
a9f58168 18054 printf (" Tag_GNU_MIPS_ABI_MSA: ");
cd30bcef 18055 READ_ULEB (val, p, end);
a9f58168
CF
18056
18057 switch (val)
18058 {
18059 case Val_GNU_MIPS_ABI_MSA_ANY:
18060 printf (_("Any MSA or not\n"));
18061 break;
18062 case Val_GNU_MIPS_ABI_MSA_128:
18063 printf (_("128-bit MSA\n"));
18064 break;
18065 default:
18066 printf ("??? (%d)\n", val);
18067 break;
18068 }
18069 return p;
18070 }
18071
f6f0e17b 18072 return display_tag_value (tag & 1, p, end);
2cf19d5c
JM
18073}
18074
59e6276b 18075static unsigned char *
f6f0e17b
NC
18076display_tic6x_attribute (unsigned char * p,
18077 const unsigned char * const end)
59e6276b 18078{
60abdbed 18079 unsigned int tag;
cd30bcef 18080 unsigned int val;
59e6276b 18081
cd30bcef 18082 READ_ULEB (tag, p, end);
59e6276b
JM
18083
18084 switch (tag)
18085 {
75fa6dc1 18086 case Tag_ISA:
75fa6dc1 18087 printf (" Tag_ISA: ");
cd30bcef 18088 READ_ULEB (val, p, end);
59e6276b
JM
18089
18090 switch (val)
18091 {
75fa6dc1 18092 case C6XABI_Tag_ISA_none:
59e6276b
JM
18093 printf (_("None\n"));
18094 break;
75fa6dc1 18095 case C6XABI_Tag_ISA_C62X:
59e6276b
JM
18096 printf ("C62x\n");
18097 break;
75fa6dc1 18098 case C6XABI_Tag_ISA_C67X:
59e6276b
JM
18099 printf ("C67x\n");
18100 break;
75fa6dc1 18101 case C6XABI_Tag_ISA_C67XP:
59e6276b
JM
18102 printf ("C67x+\n");
18103 break;
75fa6dc1 18104 case C6XABI_Tag_ISA_C64X:
59e6276b
JM
18105 printf ("C64x\n");
18106 break;
75fa6dc1 18107 case C6XABI_Tag_ISA_C64XP:
59e6276b
JM
18108 printf ("C64x+\n");
18109 break;
75fa6dc1 18110 case C6XABI_Tag_ISA_C674X:
59e6276b
JM
18111 printf ("C674x\n");
18112 break;
18113 default:
18114 printf ("??? (%d)\n", val);
18115 break;
18116 }
18117 return p;
18118
87779176 18119 case Tag_ABI_wchar_t:
87779176 18120 printf (" Tag_ABI_wchar_t: ");
cd30bcef 18121 READ_ULEB (val, p, end);
87779176
JM
18122 switch (val)
18123 {
18124 case 0:
18125 printf (_("Not used\n"));
18126 break;
18127 case 1:
18128 printf (_("2 bytes\n"));
18129 break;
18130 case 2:
18131 printf (_("4 bytes\n"));
18132 break;
18133 default:
18134 printf ("??? (%d)\n", val);
18135 break;
18136 }
18137 return p;
18138
18139 case Tag_ABI_stack_align_needed:
87779176 18140 printf (" Tag_ABI_stack_align_needed: ");
cd30bcef 18141 READ_ULEB (val, p, end);
87779176
JM
18142 switch (val)
18143 {
18144 case 0:
18145 printf (_("8-byte\n"));
18146 break;
18147 case 1:
18148 printf (_("16-byte\n"));
18149 break;
18150 default:
18151 printf ("??? (%d)\n", val);
18152 break;
18153 }
18154 return p;
18155
18156 case Tag_ABI_stack_align_preserved:
cd30bcef 18157 READ_ULEB (val, p, end);
87779176
JM
18158 printf (" Tag_ABI_stack_align_preserved: ");
18159 switch (val)
18160 {
18161 case 0:
18162 printf (_("8-byte\n"));
18163 break;
18164 case 1:
18165 printf (_("16-byte\n"));
18166 break;
18167 default:
18168 printf ("??? (%d)\n", val);
18169 break;
18170 }
18171 return p;
18172
b5593623 18173 case Tag_ABI_DSBT:
cd30bcef 18174 READ_ULEB (val, p, end);
b5593623
JM
18175 printf (" Tag_ABI_DSBT: ");
18176 switch (val)
18177 {
18178 case 0:
18179 printf (_("DSBT addressing not used\n"));
18180 break;
18181 case 1:
18182 printf (_("DSBT addressing used\n"));
18183 break;
18184 default:
18185 printf ("??? (%d)\n", val);
18186 break;
18187 }
18188 return p;
18189
87779176 18190 case Tag_ABI_PID:
cd30bcef 18191 READ_ULEB (val, p, end);
87779176
JM
18192 printf (" Tag_ABI_PID: ");
18193 switch (val)
18194 {
18195 case 0:
18196 printf (_("Data addressing position-dependent\n"));
18197 break;
18198 case 1:
18199 printf (_("Data addressing position-independent, GOT near DP\n"));
18200 break;
18201 case 2:
18202 printf (_("Data addressing position-independent, GOT far from DP\n"));
18203 break;
18204 default:
18205 printf ("??? (%d)\n", val);
18206 break;
18207 }
18208 return p;
18209
18210 case Tag_ABI_PIC:
cd30bcef 18211 READ_ULEB (val, p, end);
87779176
JM
18212 printf (" Tag_ABI_PIC: ");
18213 switch (val)
18214 {
18215 case 0:
18216 printf (_("Code addressing position-dependent\n"));
18217 break;
18218 case 1:
18219 printf (_("Code addressing position-independent\n"));
18220 break;
18221 default:
18222 printf ("??? (%d)\n", val);
18223 break;
18224 }
18225 return p;
18226
18227 case Tag_ABI_array_object_alignment:
cd30bcef 18228 READ_ULEB (val, p, end);
87779176
JM
18229 printf (" Tag_ABI_array_object_alignment: ");
18230 switch (val)
18231 {
18232 case 0:
18233 printf (_("8-byte\n"));
18234 break;
18235 case 1:
18236 printf (_("4-byte\n"));
18237 break;
18238 case 2:
18239 printf (_("16-byte\n"));
18240 break;
18241 default:
18242 printf ("??? (%d)\n", val);
18243 break;
18244 }
18245 return p;
18246
18247 case Tag_ABI_array_object_align_expected:
cd30bcef 18248 READ_ULEB (val, p, end);
87779176
JM
18249 printf (" Tag_ABI_array_object_align_expected: ");
18250 switch (val)
18251 {
18252 case 0:
18253 printf (_("8-byte\n"));
18254 break;
18255 case 1:
18256 printf (_("4-byte\n"));
18257 break;
18258 case 2:
18259 printf (_("16-byte\n"));
18260 break;
18261 default:
18262 printf ("??? (%d)\n", val);
18263 break;
18264 }
18265 return p;
18266
3cbd1c06 18267 case Tag_ABI_compatibility:
071436c6 18268 {
cd30bcef 18269 READ_ULEB (val, p, end);
071436c6 18270 printf (" Tag_ABI_compatibility: ");
071436c6 18271 printf (_("flag = %d, vendor = "), val);
4082ef84
NC
18272 if (p < end - 1)
18273 {
18274 size_t maxlen = (end - p) - 1;
18275
b6ac461a 18276 print_symbol_name ((int) maxlen, (const char *) p);
4082ef84
NC
18277 p += strnlen ((char *) p, maxlen) + 1;
18278 }
18279 else
18280 {
18281 printf (_("<corrupt>"));
18282 p = (unsigned char *) end;
18283 }
071436c6 18284 putchar ('\n');
071436c6
NC
18285 return p;
18286 }
87779176
JM
18287
18288 case Tag_ABI_conformance:
071436c6 18289 {
4082ef84
NC
18290 printf (" Tag_ABI_conformance: \"");
18291 if (p < end - 1)
18292 {
18293 size_t maxlen = (end - p) - 1;
071436c6 18294
b6ac461a 18295 print_symbol_name ((int) maxlen, (const char *) p);
4082ef84
NC
18296 p += strnlen ((char *) p, maxlen) + 1;
18297 }
18298 else
18299 {
18300 printf (_("<corrupt>"));
18301 p = (unsigned char *) end;
18302 }
071436c6 18303 printf ("\"\n");
071436c6
NC
18304 return p;
18305 }
59e6276b
JM
18306 }
18307
f6f0e17b
NC
18308 return display_tag_value (tag, p, end);
18309}
59e6276b 18310
f6f0e17b 18311static void
60abdbed 18312display_raw_attribute (unsigned char * p, unsigned char const * const end)
f6f0e17b 18313{
26c527e6 18314 uint64_t addr = 0;
f6f0e17b
NC
18315 size_t bytes = end - p;
18316
feceaa59 18317 assert (end >= p);
f6f0e17b 18318 while (bytes)
87779176 18319 {
f6f0e17b
NC
18320 int j;
18321 int k;
18322 int lbytes = (bytes > 16 ? 16 : bytes);
18323
26c527e6 18324 printf (" 0x%8.8" PRIx64 " ", addr);
f6f0e17b
NC
18325
18326 for (j = 0; j < 16; j++)
18327 {
18328 if (j < lbytes)
18329 printf ("%2.2x", p[j]);
18330 else
18331 printf (" ");
18332
18333 if ((j & 3) == 3)
18334 printf (" ");
18335 }
18336
18337 for (j = 0; j < lbytes; j++)
18338 {
18339 k = p[j];
18340 if (k >= ' ' && k < 0x7f)
18341 printf ("%c", k);
18342 else
18343 printf (".");
18344 }
18345
18346 putchar ('\n');
18347
18348 p += lbytes;
18349 bytes -= lbytes;
18350 addr += lbytes;
87779176 18351 }
59e6276b 18352
f6f0e17b 18353 putchar ('\n');
59e6276b
JM
18354}
18355
13761a11 18356static unsigned char *
b0191216 18357display_msp430_attribute (unsigned char * p,
26c527e6 18358 const unsigned char * const end)
13761a11 18359{
26c527e6
AM
18360 uint64_t val;
18361 uint64_t tag;
13761a11 18362
cd30bcef 18363 READ_ULEB (tag, p, end);
0b4362b0 18364
13761a11
NC
18365 switch (tag)
18366 {
18367 case OFBA_MSPABI_Tag_ISA:
13761a11 18368 printf (" Tag_ISA: ");
cd30bcef 18369 READ_ULEB (val, p, end);
13761a11
NC
18370 switch (val)
18371 {
18372 case 0: printf (_("None\n")); break;
18373 case 1: printf (_("MSP430\n")); break;
18374 case 2: printf (_("MSP430X\n")); break;
26c527e6 18375 default: printf ("??? (%" PRId64 ")\n", val); break;
13761a11
NC
18376 }
18377 break;
18378
18379 case OFBA_MSPABI_Tag_Code_Model:
13761a11 18380 printf (" Tag_Code_Model: ");
cd30bcef 18381 READ_ULEB (val, p, end);
13761a11
NC
18382 switch (val)
18383 {
18384 case 0: printf (_("None\n")); break;
18385 case 1: printf (_("Small\n")); break;
18386 case 2: printf (_("Large\n")); break;
26c527e6 18387 default: printf ("??? (%" PRId64 ")\n", val); break;
13761a11
NC
18388 }
18389 break;
18390
18391 case OFBA_MSPABI_Tag_Data_Model:
13761a11 18392 printf (" Tag_Data_Model: ");
cd30bcef 18393 READ_ULEB (val, p, end);
13761a11
NC
18394 switch (val)
18395 {
18396 case 0: printf (_("None\n")); break;
18397 case 1: printf (_("Small\n")); break;
18398 case 2: printf (_("Large\n")); break;
18399 case 3: printf (_("Restricted Large\n")); break;
26c527e6 18400 default: printf ("??? (%" PRId64 ")\n", val); break;
13761a11
NC
18401 }
18402 break;
18403
18404 default:
26c527e6 18405 printf (_(" <unknown tag %" PRId64 ">: "), tag);
13761a11
NC
18406
18407 if (tag & 1)
18408 {
071436c6 18409 putchar ('"');
4082ef84
NC
18410 if (p < end - 1)
18411 {
18412 size_t maxlen = (end - p) - 1;
18413
b6ac461a 18414 print_symbol_name ((int) maxlen, (const char *) p);
4082ef84
NC
18415 p += strnlen ((char *) p, maxlen) + 1;
18416 }
18417 else
18418 {
18419 printf (_("<corrupt>"));
18420 p = (unsigned char *) end;
18421 }
071436c6 18422 printf ("\"\n");
13761a11
NC
18423 }
18424 else
18425 {
cd30bcef 18426 READ_ULEB (val, p, end);
26c527e6 18427 printf ("%" PRId64 " (0x%" PRIx64 ")\n", val, val);
13761a11
NC
18428 }
18429 break;
18430 }
18431
4082ef84 18432 assert (p <= end);
13761a11
NC
18433 return p;
18434}
18435
c0ea7c52
JL
18436static unsigned char *
18437display_msp430_gnu_attribute (unsigned char * p,
18438 unsigned int tag,
18439 const unsigned char * const end)
18440{
18441 if (tag == Tag_GNU_MSP430_Data_Region)
18442 {
26c527e6 18443 uint64_t val;
c0ea7c52 18444
c0ea7c52 18445 printf (" Tag_GNU_MSP430_Data_Region: ");
cd30bcef 18446 READ_ULEB (val, p, end);
c0ea7c52
JL
18447
18448 switch (val)
18449 {
18450 case Val_GNU_MSP430_Data_Region_Any:
18451 printf (_("Any Region\n"));
18452 break;
18453 case Val_GNU_MSP430_Data_Region_Lower:
18454 printf (_("Lower Region Only\n"));
18455 break;
18456 default:
26c527e6 18457 printf ("??? (%" PRIu64 ")\n", val);
c0ea7c52
JL
18458 }
18459 return p;
18460 }
18461 return display_tag_value (tag & 1, p, end);
18462}
18463
2dc8dd17
JW
18464struct riscv_attr_tag_t {
18465 const char *name;
cd30bcef 18466 unsigned int tag;
2dc8dd17
JW
18467};
18468
18469static struct riscv_attr_tag_t riscv_attr_tag[] =
18470{
18471#define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
18472 T(arch),
18473 T(priv_spec),
18474 T(priv_spec_minor),
18475 T(priv_spec_revision),
18476 T(unaligned_access),
18477 T(stack_align),
18478#undef T
18479};
18480
18481static unsigned char *
18482display_riscv_attribute (unsigned char *p,
18483 const unsigned char * const end)
18484{
26c527e6
AM
18485 uint64_t val;
18486 uint64_t tag;
2dc8dd17
JW
18487 struct riscv_attr_tag_t *attr = NULL;
18488 unsigned i;
18489
cd30bcef 18490 READ_ULEB (tag, p, end);
2dc8dd17
JW
18491
18492 /* Find the name of attribute. */
18493 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
18494 {
18495 if (riscv_attr_tag[i].tag == tag)
18496 {
18497 attr = &riscv_attr_tag[i];
18498 break;
18499 }
18500 }
18501
18502 if (attr)
18503 printf (" %s: ", attr->name);
18504 else
18505 return display_tag_value (tag, p, end);
18506
18507 switch (tag)
18508 {
18509 case Tag_RISCV_priv_spec:
18510 case Tag_RISCV_priv_spec_minor:
18511 case Tag_RISCV_priv_spec_revision:
cd30bcef 18512 READ_ULEB (val, p, end);
26c527e6 18513 printf ("%" PRIu64 "\n", val);
2dc8dd17
JW
18514 break;
18515 case Tag_RISCV_unaligned_access:
cd30bcef 18516 READ_ULEB (val, p, end);
2dc8dd17
JW
18517 switch (val)
18518 {
18519 case 0:
18520 printf (_("No unaligned access\n"));
18521 break;
18522 case 1:
18523 printf (_("Unaligned access\n"));
18524 break;
18525 }
18526 break;
18527 case Tag_RISCV_stack_align:
cd30bcef 18528 READ_ULEB (val, p, end);
26c527e6 18529 printf (_("%" PRIu64 "-bytes\n"), val);
2dc8dd17
JW
18530 break;
18531 case Tag_RISCV_arch:
18532 p = display_tag_value (-1, p, end);
18533 break;
18534 default:
18535 return display_tag_value (tag, p, end);
18536 }
18537
18538 return p;
18539}
18540
0861f561
CQ
18541static unsigned char *
18542display_csky_attribute (unsigned char * p,
18543 const unsigned char * const end)
18544{
26c527e6
AM
18545 uint64_t tag;
18546 uint64_t val;
0861f561
CQ
18547 READ_ULEB (tag, p, end);
18548
18549 if (tag >= Tag_CSKY_MAX)
18550 {
18551 return display_tag_value (-1, p, end);
18552 }
18553
18554 switch (tag)
18555 {
18556 case Tag_CSKY_ARCH_NAME:
18557 printf (" Tag_CSKY_ARCH_NAME:\t\t");
18558 return display_tag_value (-1, p, end);
18559 case Tag_CSKY_CPU_NAME:
18560 printf (" Tag_CSKY_CPU_NAME:\t\t");
18561 return display_tag_value (-1, p, end);
18562
18563 case Tag_CSKY_ISA_FLAGS:
18564 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
18565 return display_tag_value (0, p, end);
18566 case Tag_CSKY_ISA_EXT_FLAGS:
18567 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
18568 return display_tag_value (0, p, end);
18569
18570 case Tag_CSKY_DSP_VERSION:
18571 printf (" Tag_CSKY_DSP_VERSION:\t\t");
18572 READ_ULEB (val, p, end);
18573 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
18574 printf ("DSP Extension\n");
18575 else if (val == VAL_CSKY_DSP_VERSION_2)
18576 printf ("DSP 2.0\n");
18577 break;
18578
18579 case Tag_CSKY_VDSP_VERSION:
18580 printf (" Tag_CSKY_VDSP_VERSION:\t");
18581 READ_ULEB (val, p, end);
26c527e6 18582 printf ("VDSP Version %" PRId64 "\n", val);
0861f561
CQ
18583 break;
18584
18585 case Tag_CSKY_FPU_VERSION:
18586 printf (" Tag_CSKY_FPU_VERSION:\t\t");
18587 READ_ULEB (val, p, end);
18588 if (val == VAL_CSKY_FPU_VERSION_1)
18589 printf ("ABIV1 FPU Version 1\n");
18590 else if (val == VAL_CSKY_FPU_VERSION_2)
18591 printf ("FPU Version 2\n");
18592 break;
18593
18594 case Tag_CSKY_FPU_ABI:
18595 printf (" Tag_CSKY_FPU_ABI:\t\t");
18596 READ_ULEB (val, p, end);
18597 if (val == VAL_CSKY_FPU_ABI_HARD)
18598 printf ("Hard\n");
18599 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
18600 printf ("SoftFP\n");
18601 else if (val == VAL_CSKY_FPU_ABI_SOFT)
18602 printf ("Soft\n");
18603 break;
18604 case Tag_CSKY_FPU_ROUNDING:
18605 READ_ULEB (val, p, end);
f253158f
NC
18606 if (val == 1)
18607 {
18608 printf (" Tag_CSKY_FPU_ROUNDING:\t");
18609 printf ("Needed\n");
18610 }
0861f561
CQ
18611 break;
18612 case Tag_CSKY_FPU_DENORMAL:
18613 READ_ULEB (val, p, end);
f253158f
NC
18614 if (val == 1)
18615 {
18616 printf (" Tag_CSKY_FPU_DENORMAL:\t");
18617 printf ("Needed\n");
18618 }
0861f561
CQ
18619 break;
18620 case Tag_CSKY_FPU_Exception:
18621 READ_ULEB (val, p, end);
f253158f
NC
18622 if (val == 1)
18623 {
18624 printf (" Tag_CSKY_FPU_Exception:\t");
18625 printf ("Needed\n");
18626 }
0861f561
CQ
18627 break;
18628 case Tag_CSKY_FPU_NUMBER_MODULE:
18629 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
18630 return display_tag_value (-1, p, end);
18631 case Tag_CSKY_FPU_HARDFP:
18632 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
18633 READ_ULEB (val, p, end);
18634 if (val & VAL_CSKY_FPU_HARDFP_HALF)
18635 printf (" Half");
18636 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
18637 printf (" Single");
18638 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
18639 printf (" Double");
18640 printf ("\n");
18641 break;
18642 default:
18643 return display_tag_value (tag, p, end);
18644 }
18645 return p;
18646}
18647
015dc7e1 18648static bool
dda8d76d 18649process_attributes (Filedata * filedata,
60bca95a 18650 const char * public_name,
104d59d1 18651 unsigned int proc_type,
f6f0e17b 18652 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
60abdbed 18653 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
11c1ff18 18654{
2cf0635d 18655 Elf_Internal_Shdr * sect;
11c1ff18 18656 unsigned i;
015dc7e1 18657 bool res = true;
11c1ff18
PB
18658
18659 /* Find the section header so that we get the size. */
dda8d76d
NC
18660 for (i = 0, sect = filedata->section_headers;
18661 i < filedata->file_header.e_shnum;
11c1ff18
PB
18662 i++, sect++)
18663 {
071436c6
NC
18664 unsigned char * contents;
18665 unsigned char * p;
18666
104d59d1 18667 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
11c1ff18
PB
18668 continue;
18669
dda8d76d 18670 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
3f5e193b 18671 sect->sh_size, _("attributes"));
60bca95a 18672 if (contents == NULL)
32ec8896 18673 {
015dc7e1 18674 res = false;
32ec8896
NC
18675 continue;
18676 }
60bca95a 18677
11c1ff18 18678 p = contents;
60abdbed
NC
18679 /* The first character is the version of the attributes.
18680 Currently only version 1, (aka 'A') is recognised here. */
18681 if (*p != 'A')
32ec8896
NC
18682 {
18683 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
015dc7e1 18684 res = false;
32ec8896 18685 }
60abdbed 18686 else
11c1ff18 18687 {
625d49fc 18688 uint64_t section_len;
071436c6
NC
18689
18690 section_len = sect->sh_size - 1;
11c1ff18 18691 p++;
60bca95a 18692
071436c6 18693 while (section_len > 0)
11c1ff18 18694 {
625d49fc 18695 uint64_t attr_len;
e9847026 18696 unsigned int namelen;
015dc7e1
AM
18697 bool public_section;
18698 bool gnu_section;
11c1ff18 18699
071436c6 18700 if (section_len <= 4)
e0a31db1
NC
18701 {
18702 error (_("Tag section ends prematurely\n"));
015dc7e1 18703 res = false;
e0a31db1
NC
18704 break;
18705 }
071436c6 18706 attr_len = byte_get (p, 4);
11c1ff18 18707 p += 4;
60bca95a 18708
071436c6 18709 if (attr_len > section_len)
11c1ff18 18710 {
071436c6
NC
18711 error (_("Bad attribute length (%u > %u)\n"),
18712 (unsigned) attr_len, (unsigned) section_len);
18713 attr_len = section_len;
015dc7e1 18714 res = false;
11c1ff18 18715 }
74e1a04b 18716 /* PR 17531: file: 001-101425-0.004 */
071436c6 18717 else if (attr_len < 5)
74e1a04b 18718 {
071436c6 18719 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
015dc7e1 18720 res = false;
74e1a04b
NC
18721 break;
18722 }
e9847026 18723
071436c6
NC
18724 section_len -= attr_len;
18725 attr_len -= 4;
18726
18727 namelen = strnlen ((char *) p, attr_len) + 1;
18728 if (namelen == 0 || namelen >= attr_len)
e9847026
NC
18729 {
18730 error (_("Corrupt attribute section name\n"));
015dc7e1 18731 res = false;
e9847026
NC
18732 break;
18733 }
18734
071436c6 18735 printf (_("Attribute Section: "));
b6ac461a 18736 print_symbol_name (INT_MAX, (const char *) p);
071436c6 18737 putchar ('\n');
60bca95a
NC
18738
18739 if (public_name && streq ((char *) p, public_name))
015dc7e1 18740 public_section = true;
11c1ff18 18741 else
015dc7e1 18742 public_section = false;
60bca95a
NC
18743
18744 if (streq ((char *) p, "gnu"))
015dc7e1 18745 gnu_section = true;
104d59d1 18746 else
015dc7e1 18747 gnu_section = false;
60bca95a 18748
11c1ff18 18749 p += namelen;
071436c6 18750 attr_len -= namelen;
e0a31db1 18751
071436c6 18752 while (attr_len > 0 && p < contents + sect->sh_size)
11c1ff18 18753 {
e0a31db1 18754 int tag;
cd30bcef 18755 unsigned int val;
625d49fc 18756 uint64_t size;
071436c6 18757 unsigned char * end;
60bca95a 18758
e0a31db1 18759 /* PR binutils/17531: Safe handling of corrupt files. */
071436c6 18760 if (attr_len < 6)
e0a31db1
NC
18761 {
18762 error (_("Unused bytes at end of section\n"));
015dc7e1 18763 res = false;
e0a31db1
NC
18764 section_len = 0;
18765 break;
18766 }
18767
18768 tag = *(p++);
11c1ff18 18769 size = byte_get (p, 4);
071436c6 18770 if (size > attr_len)
11c1ff18 18771 {
e9847026 18772 error (_("Bad subsection length (%u > %u)\n"),
071436c6 18773 (unsigned) size, (unsigned) attr_len);
015dc7e1 18774 res = false;
071436c6 18775 size = attr_len;
11c1ff18 18776 }
e0a31db1
NC
18777 /* PR binutils/17531: Safe handling of corrupt files. */
18778 if (size < 6)
18779 {
18780 error (_("Bad subsection length (%u < 6)\n"),
18781 (unsigned) size);
015dc7e1 18782 res = false;
e0a31db1
NC
18783 section_len = 0;
18784 break;
18785 }
60bca95a 18786
071436c6 18787 attr_len -= size;
11c1ff18 18788 end = p + size - 1;
071436c6 18789 assert (end <= contents + sect->sh_size);
11c1ff18 18790 p += 4;
60bca95a 18791
11c1ff18
PB
18792 switch (tag)
18793 {
18794 case 1:
2b692964 18795 printf (_("File Attributes\n"));
11c1ff18
PB
18796 break;
18797 case 2:
2b692964 18798 printf (_("Section Attributes:"));
11c1ff18
PB
18799 goto do_numlist;
18800 case 3:
2b692964 18801 printf (_("Symbol Attributes:"));
1a0670f3 18802 /* Fall through. */
11c1ff18
PB
18803 do_numlist:
18804 for (;;)
18805 {
cd30bcef 18806 READ_ULEB (val, p, end);
11c1ff18
PB
18807 if (val == 0)
18808 break;
18809 printf (" %d", val);
18810 }
18811 printf ("\n");
18812 break;
18813 default:
2b692964 18814 printf (_("Unknown tag: %d\n"), tag);
015dc7e1 18815 public_section = false;
11c1ff18
PB
18816 break;
18817 }
60bca95a 18818
071436c6 18819 if (public_section && display_pub_attribute != NULL)
11c1ff18
PB
18820 {
18821 while (p < end)
f6f0e17b 18822 p = display_pub_attribute (p, end);
60abdbed 18823 assert (p == end);
104d59d1 18824 }
071436c6 18825 else if (gnu_section && display_proc_gnu_attribute != NULL)
104d59d1
JM
18826 {
18827 while (p < end)
18828 p = display_gnu_attribute (p,
f6f0e17b
NC
18829 display_proc_gnu_attribute,
18830 end);
60abdbed 18831 assert (p == end);
11c1ff18 18832 }
071436c6 18833 else if (p < end)
11c1ff18 18834 {
071436c6 18835 printf (_(" Unknown attribute:\n"));
f6f0e17b 18836 display_raw_attribute (p, end);
11c1ff18
PB
18837 p = end;
18838 }
071436c6
NC
18839 else
18840 attr_len = 0;
11c1ff18
PB
18841 }
18842 }
18843 }
d70c5fc7 18844
60bca95a 18845 free (contents);
11c1ff18 18846 }
32ec8896
NC
18847
18848 return res;
11c1ff18
PB
18849}
18850
ccb4c951
RS
18851/* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
18852 Print the Address, Access and Initial fields of an entry at VMA ADDR
82b1b41b
NC
18853 and return the VMA of the next entry, or -1 if there was a problem.
18854 Does not read from DATA_END or beyond. */
ccb4c951 18855
625d49fc
AM
18856static uint64_t
18857print_mips_got_entry (unsigned char * data, uint64_t pltgot, uint64_t addr,
82b1b41b 18858 unsigned char * data_end)
ccb4c951
RS
18859{
18860 printf (" ");
18861 print_vma (addr, LONG_HEX);
18862 printf (" ");
18863 if (addr < pltgot + 0xfff0)
18864 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
18865 else
18866 printf ("%10s", "");
18867 printf (" ");
18868 if (data == NULL)
2b692964 18869 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
ccb4c951
RS
18870 else
18871 {
625d49fc 18872 uint64_t entry;
82b1b41b 18873 unsigned char * from = data + addr - pltgot;
ccb4c951 18874
82b1b41b
NC
18875 if (from + (is_32bit_elf ? 4 : 8) > data_end)
18876 {
18877 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
18878 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
625d49fc 18879 return (uint64_t) -1;
82b1b41b
NC
18880 }
18881 else
18882 {
18883 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18884 print_vma (entry, LONG_HEX);
18885 }
ccb4c951
RS
18886 }
18887 return addr + (is_32bit_elf ? 4 : 8);
18888}
18889
861fb55a
DJ
18890/* DATA points to the contents of a MIPS PLT GOT that starts at VMA
18891 PLTGOT. Print the Address and Initial fields of an entry at VMA
18892 ADDR and return the VMA of the next entry. */
18893
625d49fc
AM
18894static uint64_t
18895print_mips_pltgot_entry (unsigned char * data, uint64_t pltgot, uint64_t addr)
861fb55a
DJ
18896{
18897 printf (" ");
18898 print_vma (addr, LONG_HEX);
18899 printf (" ");
18900 if (data == NULL)
2b692964 18901 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
861fb55a
DJ
18902 else
18903 {
625d49fc 18904 uint64_t entry;
861fb55a
DJ
18905
18906 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18907 print_vma (entry, LONG_HEX);
18908 }
18909 return addr + (is_32bit_elf ? 4 : 8);
18910}
18911
351cdf24
MF
18912static void
18913print_mips_ases (unsigned int mask)
18914{
18915 if (mask & AFL_ASE_DSP)
18916 fputs ("\n\tDSP ASE", stdout);
18917 if (mask & AFL_ASE_DSPR2)
18918 fputs ("\n\tDSP R2 ASE", stdout);
8f4f9071
MF
18919 if (mask & AFL_ASE_DSPR3)
18920 fputs ("\n\tDSP R3 ASE", stdout);
351cdf24
MF
18921 if (mask & AFL_ASE_EVA)
18922 fputs ("\n\tEnhanced VA Scheme", stdout);
18923 if (mask & AFL_ASE_MCU)
18924 fputs ("\n\tMCU (MicroController) ASE", stdout);
18925 if (mask & AFL_ASE_MDMX)
18926 fputs ("\n\tMDMX ASE", stdout);
18927 if (mask & AFL_ASE_MIPS3D)
18928 fputs ("\n\tMIPS-3D ASE", stdout);
18929 if (mask & AFL_ASE_MT)
18930 fputs ("\n\tMT ASE", stdout);
18931 if (mask & AFL_ASE_SMARTMIPS)
18932 fputs ("\n\tSmartMIPS ASE", stdout);
18933 if (mask & AFL_ASE_VIRT)
18934 fputs ("\n\tVZ ASE", stdout);
18935 if (mask & AFL_ASE_MSA)
18936 fputs ("\n\tMSA ASE", stdout);
18937 if (mask & AFL_ASE_MIPS16)
18938 fputs ("\n\tMIPS16 ASE", stdout);
18939 if (mask & AFL_ASE_MICROMIPS)
18940 fputs ("\n\tMICROMIPS ASE", stdout);
18941 if (mask & AFL_ASE_XPA)
18942 fputs ("\n\tXPA ASE", stdout);
25499ac7
MR
18943 if (mask & AFL_ASE_MIPS16E2)
18944 fputs ("\n\tMIPS16e2 ASE", stdout);
730c3174
SE
18945 if (mask & AFL_ASE_CRC)
18946 fputs ("\n\tCRC ASE", stdout);
6f20c942
FS
18947 if (mask & AFL_ASE_GINV)
18948 fputs ("\n\tGINV ASE", stdout);
8095d2f7
CX
18949 if (mask & AFL_ASE_LOONGSON_MMI)
18950 fputs ("\n\tLoongson MMI ASE", stdout);
716c08de
CX
18951 if (mask & AFL_ASE_LOONGSON_CAM)
18952 fputs ("\n\tLoongson CAM ASE", stdout);
bdc6c06e
CX
18953 if (mask & AFL_ASE_LOONGSON_EXT)
18954 fputs ("\n\tLoongson EXT ASE", stdout);
a693765e
CX
18955 if (mask & AFL_ASE_LOONGSON_EXT2)
18956 fputs ("\n\tLoongson EXT2 ASE", stdout);
351cdf24
MF
18957 if (mask == 0)
18958 fprintf (stdout, "\n\t%s", _("None"));
00ac7aa0
MF
18959 else if ((mask & ~AFL_ASE_MASK) != 0)
18960 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
351cdf24
MF
18961}
18962
18963static void
18964print_mips_isa_ext (unsigned int isa_ext)
18965{
18966 switch (isa_ext)
18967 {
18968 case 0:
18969 fputs (_("None"), stdout);
18970 break;
18971 case AFL_EXT_XLR:
18972 fputs ("RMI XLR", stdout);
18973 break;
2c629856
N
18974 case AFL_EXT_OCTEON3:
18975 fputs ("Cavium Networks Octeon3", stdout);
18976 break;
351cdf24
MF
18977 case AFL_EXT_OCTEON2:
18978 fputs ("Cavium Networks Octeon2", stdout);
18979 break;
18980 case AFL_EXT_OCTEONP:
18981 fputs ("Cavium Networks OcteonP", stdout);
18982 break;
351cdf24
MF
18983 case AFL_EXT_OCTEON:
18984 fputs ("Cavium Networks Octeon", stdout);
18985 break;
18986 case AFL_EXT_5900:
18987 fputs ("Toshiba R5900", stdout);
18988 break;
18989 case AFL_EXT_4650:
18990 fputs ("MIPS R4650", stdout);
18991 break;
18992 case AFL_EXT_4010:
18993 fputs ("LSI R4010", stdout);
18994 break;
18995 case AFL_EXT_4100:
18996 fputs ("NEC VR4100", stdout);
18997 break;
18998 case AFL_EXT_3900:
18999 fputs ("Toshiba R3900", stdout);
19000 break;
19001 case AFL_EXT_10000:
19002 fputs ("MIPS R10000", stdout);
19003 break;
19004 case AFL_EXT_SB1:
19005 fputs ("Broadcom SB-1", stdout);
19006 break;
19007 case AFL_EXT_4111:
19008 fputs ("NEC VR4111/VR4181", stdout);
19009 break;
19010 case AFL_EXT_4120:
19011 fputs ("NEC VR4120", stdout);
19012 break;
19013 case AFL_EXT_5400:
19014 fputs ("NEC VR5400", stdout);
19015 break;
19016 case AFL_EXT_5500:
19017 fputs ("NEC VR5500", stdout);
19018 break;
19019 case AFL_EXT_LOONGSON_2E:
19020 fputs ("ST Microelectronics Loongson 2E", stdout);
19021 break;
19022 case AFL_EXT_LOONGSON_2F:
19023 fputs ("ST Microelectronics Loongson 2F", stdout);
19024 break;
38bf472a
MR
19025 case AFL_EXT_INTERAPTIV_MR2:
19026 fputs ("Imagination interAptiv MR2", stdout);
19027 break;
351cdf24 19028 default:
00ac7aa0 19029 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
351cdf24
MF
19030 }
19031}
19032
32ec8896 19033static signed int
351cdf24
MF
19034get_mips_reg_size (int reg_size)
19035{
19036 return (reg_size == AFL_REG_NONE) ? 0
19037 : (reg_size == AFL_REG_32) ? 32
19038 : (reg_size == AFL_REG_64) ? 64
19039 : (reg_size == AFL_REG_128) ? 128
19040 : -1;
19041}
19042
015dc7e1 19043static bool
dda8d76d 19044process_mips_specific (Filedata * filedata)
5b18a4bc 19045{
2cf0635d 19046 Elf_Internal_Dyn * entry;
351cdf24 19047 Elf_Internal_Shdr *sect = NULL;
19e6b90e
L
19048 size_t liblist_offset = 0;
19049 size_t liblistno = 0;
19050 size_t conflictsno = 0;
19051 size_t options_offset = 0;
19052 size_t conflicts_offset = 0;
861fb55a
DJ
19053 size_t pltrelsz = 0;
19054 size_t pltrel = 0;
625d49fc
AM
19055 uint64_t pltgot = 0;
19056 uint64_t mips_pltgot = 0;
19057 uint64_t jmprel = 0;
19058 uint64_t local_gotno = 0;
19059 uint64_t gotsym = 0;
19060 uint64_t symtabno = 0;
015dc7e1 19061 bool res = true;
103f02d3 19062
dda8d76d 19063 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
32ec8896 19064 display_mips_gnu_attribute))
015dc7e1 19065 res = false;
2cf19d5c 19066
dda8d76d 19067 sect = find_section (filedata, ".MIPS.abiflags");
351cdf24
MF
19068
19069 if (sect != NULL)
19070 {
19071 Elf_External_ABIFlags_v0 *abiflags_ext;
19072 Elf_Internal_ABIFlags_v0 abiflags_in;
19073
19074 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
32ec8896
NC
19075 {
19076 error (_("Corrupt MIPS ABI Flags section.\n"));
015dc7e1 19077 res = false;
32ec8896 19078 }
351cdf24
MF
19079 else
19080 {
dda8d76d 19081 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
351cdf24
MF
19082 sect->sh_size, _("MIPS ABI Flags section"));
19083 if (abiflags_ext)
19084 {
19085 abiflags_in.version = BYTE_GET (abiflags_ext->version);
19086 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
19087 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
19088 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
19089 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
19090 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
19091 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
19092 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
19093 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
19094 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
19095 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
19096
19097 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
19098 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
19099 if (abiflags_in.isa_rev > 1)
19100 printf ("r%d", abiflags_in.isa_rev);
19101 printf ("\nGPR size: %d",
19102 get_mips_reg_size (abiflags_in.gpr_size));
19103 printf ("\nCPR1 size: %d",
19104 get_mips_reg_size (abiflags_in.cpr1_size));
19105 printf ("\nCPR2 size: %d",
19106 get_mips_reg_size (abiflags_in.cpr2_size));
19107 fputs ("\nFP ABI: ", stdout);
19108 print_mips_fp_abi_value (abiflags_in.fp_abi);
19109 fputs ("ISA Extension: ", stdout);
19110 print_mips_isa_ext (abiflags_in.isa_ext);
19111 fputs ("\nASEs:", stdout);
19112 print_mips_ases (abiflags_in.ases);
19113 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
19114 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
19115 fputc ('\n', stdout);
19116 free (abiflags_ext);
19117 }
19118 }
19119 }
19120
19e6b90e 19121 /* We have a lot of special sections. Thanks SGI! */
978c4450 19122 if (filedata->dynamic_section == NULL)
bbdd9a68
MR
19123 {
19124 /* No dynamic information available. See if there is static GOT. */
dda8d76d 19125 sect = find_section (filedata, ".got");
bbdd9a68
MR
19126 if (sect != NULL)
19127 {
19128 unsigned char *data_end;
19129 unsigned char *data;
625d49fc 19130 uint64_t ent, end;
bbdd9a68
MR
19131 int addr_size;
19132
19133 pltgot = sect->sh_addr;
19134
19135 ent = pltgot;
19136 addr_size = (is_32bit_elf ? 4 : 8);
19137 end = pltgot + sect->sh_size;
19138
dda8d76d 19139 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
bbdd9a68
MR
19140 end - pltgot, 1,
19141 _("Global Offset Table data"));
19142 /* PR 12855: Null data is handled gracefully throughout. */
19143 data_end = data + (end - pltgot);
19144
19145 printf (_("\nStatic GOT:\n"));
19146 printf (_(" Canonical gp value: "));
19147 print_vma (ent + 0x7ff0, LONG_HEX);
19148 printf ("\n\n");
19149
19150 /* In a dynamic binary GOT[0] is reserved for the dynamic
19151 loader to store the lazy resolver pointer, however in
19152 a static binary it may well have been omitted and GOT
19153 reduced to a table of addresses.
19154 PR 21344: Check for the entry being fully available
19155 before fetching it. */
19156 if (data
19157 && data + ent - pltgot + addr_size <= data_end
19158 && byte_get (data + ent - pltgot, addr_size) == 0)
19159 {
19160 printf (_(" Reserved entries:\n"));
19161 printf (_(" %*s %10s %*s\n"),
19162 addr_size * 2, _("Address"), _("Access"),
19163 addr_size * 2, _("Value"));
19164 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19165 printf ("\n");
625d49fc 19166 if (ent == (uint64_t) -1)
bbdd9a68
MR
19167 goto sgot_print_fail;
19168
19169 /* Check for the MSB of GOT[1] being set, identifying a
19170 GNU object. This entry will be used by some runtime
19171 loaders, to store the module pointer. Otherwise this
19172 is an ordinary local entry.
19173 PR 21344: Check for the entry being fully available
19174 before fetching it. */
19175 if (data
19176 && data + ent - pltgot + addr_size <= data_end
19177 && (byte_get (data + ent - pltgot, addr_size)
19178 >> (addr_size * 8 - 1)) != 0)
19179 {
19180 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19181 printf ("\n");
625d49fc 19182 if (ent == (uint64_t) -1)
bbdd9a68
MR
19183 goto sgot_print_fail;
19184 }
19185 printf ("\n");
19186 }
19187
f17e9d8a 19188 if (data != NULL && ent < end)
bbdd9a68
MR
19189 {
19190 printf (_(" Local entries:\n"));
19191 printf (" %*s %10s %*s\n",
19192 addr_size * 2, _("Address"), _("Access"),
19193 addr_size * 2, _("Value"));
19194 while (ent < end)
19195 {
19196 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19197 printf ("\n");
625d49fc 19198 if (ent == (uint64_t) -1)
bbdd9a68
MR
19199 goto sgot_print_fail;
19200 }
19201 printf ("\n");
19202 }
19203
19204 sgot_print_fail:
9db70fc3 19205 free (data);
bbdd9a68
MR
19206 }
19207 return res;
19208 }
252b5132 19209
978c4450 19210 for (entry = filedata->dynamic_section;
071436c6 19211 /* PR 17531 file: 012-50589-0.004. */
978c4450
AM
19212 (entry < filedata->dynamic_section + filedata->dynamic_nent
19213 && entry->d_tag != DT_NULL);
071436c6 19214 ++entry)
252b5132
RH
19215 switch (entry->d_tag)
19216 {
19217 case DT_MIPS_LIBLIST:
d93f0186 19218 liblist_offset
dda8d76d 19219 = offset_from_vma (filedata, entry->d_un.d_val,
d93f0186 19220 liblistno * sizeof (Elf32_External_Lib));
252b5132
RH
19221 break;
19222 case DT_MIPS_LIBLISTNO:
19223 liblistno = entry->d_un.d_val;
19224 break;
19225 case DT_MIPS_OPTIONS:
dda8d76d 19226 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
252b5132
RH
19227 break;
19228 case DT_MIPS_CONFLICT:
d93f0186 19229 conflicts_offset
dda8d76d 19230 = offset_from_vma (filedata, entry->d_un.d_val,
d93f0186 19231 conflictsno * sizeof (Elf32_External_Conflict));
252b5132
RH
19232 break;
19233 case DT_MIPS_CONFLICTNO:
19234 conflictsno = entry->d_un.d_val;
19235 break;
ccb4c951 19236 case DT_PLTGOT:
861fb55a
DJ
19237 pltgot = entry->d_un.d_ptr;
19238 break;
ccb4c951
RS
19239 case DT_MIPS_LOCAL_GOTNO:
19240 local_gotno = entry->d_un.d_val;
19241 break;
19242 case DT_MIPS_GOTSYM:
19243 gotsym = entry->d_un.d_val;
19244 break;
19245 case DT_MIPS_SYMTABNO:
19246 symtabno = entry->d_un.d_val;
19247 break;
861fb55a
DJ
19248 case DT_MIPS_PLTGOT:
19249 mips_pltgot = entry->d_un.d_ptr;
19250 break;
19251 case DT_PLTREL:
19252 pltrel = entry->d_un.d_val;
19253 break;
19254 case DT_PLTRELSZ:
19255 pltrelsz = entry->d_un.d_val;
19256 break;
19257 case DT_JMPREL:
19258 jmprel = entry->d_un.d_ptr;
19259 break;
252b5132
RH
19260 default:
19261 break;
19262 }
19263
19264 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
19265 {
2cf0635d 19266 Elf32_External_Lib * elib;
252b5132
RH
19267 size_t cnt;
19268
dda8d76d 19269 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
95099889
AM
19270 sizeof (Elf32_External_Lib),
19271 liblistno,
19272 _("liblist section data"));
a6e9f9df 19273 if (elib)
252b5132 19274 {
26c527e6
AM
19275 printf (ngettext ("\nSection '.liblist' contains %zu entry:\n",
19276 "\nSection '.liblist' contains %zu entries:\n",
19277 liblistno),
19278 liblistno);
2b692964 19279 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
a6e9f9df
AM
19280 stdout);
19281
19282 for (cnt = 0; cnt < liblistno; ++cnt)
252b5132 19283 {
a6e9f9df 19284 Elf32_Lib liblist;
91d6fa6a 19285 time_t atime;
d5b07ef4 19286 char timebuf[128];
2cf0635d 19287 struct tm * tmp;
a6e9f9df
AM
19288
19289 liblist.l_name = BYTE_GET (elib[cnt].l_name);
91d6fa6a 19290 atime = BYTE_GET (elib[cnt].l_time_stamp);
a6e9f9df
AM
19291 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
19292 liblist.l_version = BYTE_GET (elib[cnt].l_version);
19293 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
19294
91d6fa6a 19295 tmp = gmtime (&atime);
e9e44622
JJ
19296 snprintf (timebuf, sizeof (timebuf),
19297 "%04u-%02u-%02uT%02u:%02u:%02u",
19298 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
19299 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
a6e9f9df 19300
26c527e6 19301 printf ("%3zu: ", cnt);
84714f86 19302 if (valid_dynamic_name (filedata, liblist.l_name))
b6ac461a 19303 print_symbol_name (20, get_dynamic_name (filedata, liblist.l_name));
d79b3d50 19304 else
2b692964 19305 printf (_("<corrupt: %9ld>"), liblist.l_name);
31104126
NC
19306 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
19307 liblist.l_version);
a6e9f9df
AM
19308
19309 if (liblist.l_flags == 0)
2b692964 19310 puts (_(" NONE"));
a6e9f9df
AM
19311 else
19312 {
19313 static const struct
252b5132 19314 {
2cf0635d 19315 const char * name;
a6e9f9df 19316 int bit;
252b5132 19317 }
a6e9f9df
AM
19318 l_flags_vals[] =
19319 {
19320 { " EXACT_MATCH", LL_EXACT_MATCH },
19321 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
19322 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
19323 { " EXPORTS", LL_EXPORTS },
19324 { " DELAY_LOAD", LL_DELAY_LOAD },
19325 { " DELTA", LL_DELTA }
19326 };
19327 int flags = liblist.l_flags;
19328 size_t fcnt;
19329
60bca95a 19330 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
a6e9f9df
AM
19331 if ((flags & l_flags_vals[fcnt].bit) != 0)
19332 {
19333 fputs (l_flags_vals[fcnt].name, stdout);
19334 flags ^= l_flags_vals[fcnt].bit;
19335 }
19336 if (flags != 0)
19337 printf (" %#x", (unsigned int) flags);
252b5132 19338
a6e9f9df
AM
19339 puts ("");
19340 }
252b5132 19341 }
252b5132 19342
a6e9f9df
AM
19343 free (elib);
19344 }
32ec8896 19345 else
015dc7e1 19346 res = false;
252b5132
RH
19347 }
19348
19349 if (options_offset != 0)
19350 {
2cf0635d 19351 Elf_External_Options * eopt;
252b5132
RH
19352 size_t offset;
19353 int cnt;
19354
19355 /* Find the section header so that we get the size. */
dda8d76d 19356 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
948f632f 19357 /* PR 17533 file: 012-277276-0.004. */
071436c6
NC
19358 if (sect == NULL)
19359 {
19360 error (_("No MIPS_OPTIONS header found\n"));
015dc7e1 19361 return false;
071436c6 19362 }
7fc0c668
NC
19363 /* PR 24243 */
19364 if (sect->sh_size < sizeof (* eopt))
19365 {
19366 error (_("The MIPS options section is too small.\n"));
015dc7e1 19367 return false;
7fc0c668 19368 }
252b5132 19369
dda8d76d 19370 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
3f5e193b 19371 sect->sh_size, _("options"));
a6e9f9df 19372 if (eopt)
252b5132 19373 {
fd17d1e6 19374 Elf_Internal_Options option;
76da6bbe 19375
a6e9f9df 19376 offset = cnt = 0;
82b1b41b 19377 while (offset <= sect->sh_size - sizeof (* eopt))
a6e9f9df 19378 {
2cf0635d 19379 Elf_External_Options * eoption;
fd17d1e6 19380 unsigned int optsize;
252b5132 19381
a6e9f9df 19382 eoption = (Elf_External_Options *) ((char *) eopt + offset);
252b5132 19383
fd17d1e6 19384 optsize = BYTE_GET (eoption->size);
76da6bbe 19385
82b1b41b 19386 /* PR 17531: file: ffa0fa3b. */
fd17d1e6
AM
19387 if (optsize < sizeof (* eopt)
19388 || optsize > sect->sh_size - offset)
82b1b41b 19389 {
645f43a8 19390 error (_("Invalid size (%u) for MIPS option\n"),
fd17d1e6 19391 optsize);
645f43a8 19392 free (eopt);
015dc7e1 19393 return false;
82b1b41b 19394 }
fd17d1e6 19395 offset += optsize;
a6e9f9df
AM
19396 ++cnt;
19397 }
252b5132 19398
d3a49aa8
AM
19399 printf (ngettext ("\nSection '%s' contains %d entry:\n",
19400 "\nSection '%s' contains %d entries:\n",
19401 cnt),
dda8d76d 19402 printable_section_name (filedata, sect), cnt);
76da6bbe 19403
82b1b41b 19404 offset = 0;
a6e9f9df 19405 while (cnt-- > 0)
252b5132 19406 {
a6e9f9df 19407 size_t len;
fd17d1e6
AM
19408 Elf_External_Options * eoption;
19409
19410 eoption = (Elf_External_Options *) ((char *) eopt + offset);
19411
19412 option.kind = BYTE_GET (eoption->kind);
19413 option.size = BYTE_GET (eoption->size);
19414 option.section = BYTE_GET (eoption->section);
19415 option.info = BYTE_GET (eoption->info);
a6e9f9df 19416
fd17d1e6 19417 switch (option.kind)
252b5132 19418 {
a6e9f9df
AM
19419 case ODK_NULL:
19420 /* This shouldn't happen. */
d0c4e780 19421 printf (" NULL %" PRId16 " %" PRIx32,
fd17d1e6 19422 option.section, option.info);
a6e9f9df 19423 break;
2e6be59c 19424
a6e9f9df
AM
19425 case ODK_REGINFO:
19426 printf (" REGINFO ");
dda8d76d 19427 if (filedata->file_header.e_machine == EM_MIPS)
a6e9f9df 19428 {
2cf0635d 19429 Elf32_External_RegInfo * ereg;
b34976b6 19430 Elf32_RegInfo reginfo;
a6e9f9df 19431
2e6be59c 19432 /* 32bit form. */
fd17d1e6
AM
19433 if (option.size < (sizeof (Elf_External_Options)
19434 + sizeof (Elf32_External_RegInfo)))
2e6be59c
NC
19435 {
19436 printf (_("<corrupt>\n"));
19437 error (_("Truncated MIPS REGINFO option\n"));
19438 cnt = 0;
19439 break;
19440 }
19441
fd17d1e6 19442 ereg = (Elf32_External_RegInfo *) (eoption + 1);
2e6be59c 19443
a6e9f9df
AM
19444 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
19445 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
19446 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
19447 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
19448 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
19449 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
19450
d0c4e780
AM
19451 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
19452 reginfo.ri_gprmask, reginfo.ri_gp_value);
19453 printf (" "
19454 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
19455 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
a6e9f9df
AM
19456 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
19457 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
19458 }
19459 else
19460 {
19461 /* 64 bit form. */
2cf0635d 19462 Elf64_External_RegInfo * ereg;
a6e9f9df
AM
19463 Elf64_Internal_RegInfo reginfo;
19464
fd17d1e6
AM
19465 if (option.size < (sizeof (Elf_External_Options)
19466 + sizeof (Elf64_External_RegInfo)))
2e6be59c
NC
19467 {
19468 printf (_("<corrupt>\n"));
19469 error (_("Truncated MIPS REGINFO option\n"));
19470 cnt = 0;
19471 break;
19472 }
19473
fd17d1e6 19474 ereg = (Elf64_External_RegInfo *) (eoption + 1);
a6e9f9df
AM
19475 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
19476 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
19477 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
19478 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
19479 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
66543521 19480 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
a6e9f9df 19481
d0c4e780
AM
19482 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
19483 reginfo.ri_gprmask, reginfo.ri_gp_value);
19484 printf (" "
19485 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
19486 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
a6e9f9df
AM
19487 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
19488 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
19489 }
fd17d1e6 19490 offset += option.size;
a6e9f9df 19491 continue;
2e6be59c 19492
a6e9f9df
AM
19493 case ODK_EXCEPTIONS:
19494 fputs (" EXCEPTIONS fpe_min(", stdout);
fd17d1e6 19495 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
a6e9f9df 19496 fputs (") fpe_max(", stdout);
fd17d1e6 19497 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
a6e9f9df
AM
19498 fputs (")", stdout);
19499
fd17d1e6 19500 if (option.info & OEX_PAGE0)
a6e9f9df 19501 fputs (" PAGE0", stdout);
fd17d1e6 19502 if (option.info & OEX_SMM)
a6e9f9df 19503 fputs (" SMM", stdout);
fd17d1e6 19504 if (option.info & OEX_FPDBUG)
a6e9f9df 19505 fputs (" FPDBUG", stdout);
fd17d1e6 19506 if (option.info & OEX_DISMISS)
a6e9f9df
AM
19507 fputs (" DISMISS", stdout);
19508 break;
2e6be59c 19509
a6e9f9df
AM
19510 case ODK_PAD:
19511 fputs (" PAD ", stdout);
fd17d1e6 19512 if (option.info & OPAD_PREFIX)
a6e9f9df 19513 fputs (" PREFIX", stdout);
fd17d1e6 19514 if (option.info & OPAD_POSTFIX)
a6e9f9df 19515 fputs (" POSTFIX", stdout);
fd17d1e6 19516 if (option.info & OPAD_SYMBOL)
a6e9f9df
AM
19517 fputs (" SYMBOL", stdout);
19518 break;
2e6be59c 19519
a6e9f9df
AM
19520 case ODK_HWPATCH:
19521 fputs (" HWPATCH ", stdout);
fd17d1e6 19522 if (option.info & OHW_R4KEOP)
a6e9f9df 19523 fputs (" R4KEOP", stdout);
fd17d1e6 19524 if (option.info & OHW_R8KPFETCH)
a6e9f9df 19525 fputs (" R8KPFETCH", stdout);
fd17d1e6 19526 if (option.info & OHW_R5KEOP)
a6e9f9df 19527 fputs (" R5KEOP", stdout);
fd17d1e6 19528 if (option.info & OHW_R5KCVTL)
a6e9f9df
AM
19529 fputs (" R5KCVTL", stdout);
19530 break;
2e6be59c 19531
a6e9f9df
AM
19532 case ODK_FILL:
19533 fputs (" FILL ", stdout);
19534 /* XXX Print content of info word? */
19535 break;
2e6be59c 19536
a6e9f9df
AM
19537 case ODK_TAGS:
19538 fputs (" TAGS ", stdout);
19539 /* XXX Print content of info word? */
19540 break;
2e6be59c 19541
a6e9f9df
AM
19542 case ODK_HWAND:
19543 fputs (" HWAND ", stdout);
fd17d1e6 19544 if (option.info & OHWA0_R4KEOP_CHECKED)
a6e9f9df 19545 fputs (" R4KEOP_CHECKED", stdout);
fd17d1e6 19546 if (option.info & OHWA0_R4KEOP_CLEAN)
a6e9f9df
AM
19547 fputs (" R4KEOP_CLEAN", stdout);
19548 break;
2e6be59c 19549
a6e9f9df
AM
19550 case ODK_HWOR:
19551 fputs (" HWOR ", stdout);
fd17d1e6 19552 if (option.info & OHWA0_R4KEOP_CHECKED)
a6e9f9df 19553 fputs (" R4KEOP_CHECKED", stdout);
fd17d1e6 19554 if (option.info & OHWA0_R4KEOP_CLEAN)
a6e9f9df
AM
19555 fputs (" R4KEOP_CLEAN", stdout);
19556 break;
2e6be59c 19557
a6e9f9df 19558 case ODK_GP_GROUP:
d0c4e780 19559 printf (" GP_GROUP %#06x self-contained %#06x",
fd17d1e6
AM
19560 option.info & OGP_GROUP,
19561 (option.info & OGP_SELF) >> 16);
a6e9f9df 19562 break;
2e6be59c 19563
a6e9f9df 19564 case ODK_IDENT:
d0c4e780 19565 printf (" IDENT %#06x self-contained %#06x",
fd17d1e6
AM
19566 option.info & OGP_GROUP,
19567 (option.info & OGP_SELF) >> 16);
a6e9f9df 19568 break;
2e6be59c 19569
a6e9f9df
AM
19570 default:
19571 /* This shouldn't happen. */
d0c4e780 19572 printf (" %3d ??? %" PRId16 " %" PRIx32,
fd17d1e6 19573 option.kind, option.section, option.info);
a6e9f9df 19574 break;
252b5132 19575 }
a6e9f9df 19576
2cf0635d 19577 len = sizeof (* eopt);
fd17d1e6 19578 while (len < option.size)
82b1b41b 19579 {
fd17d1e6 19580 unsigned char datum = *((unsigned char *) eoption + len);
a6e9f9df 19581
82b1b41b
NC
19582 if (ISPRINT (datum))
19583 printf ("%c", datum);
19584 else
19585 printf ("\\%03o", datum);
19586 len ++;
19587 }
a6e9f9df 19588 fputs ("\n", stdout);
82b1b41b 19589
fd17d1e6 19590 offset += option.size;
252b5132 19591 }
a6e9f9df 19592 free (eopt);
252b5132 19593 }
32ec8896 19594 else
015dc7e1 19595 res = false;
252b5132
RH
19596 }
19597
19598 if (conflicts_offset != 0 && conflictsno != 0)
19599 {
2cf0635d 19600 Elf32_Conflict * iconf;
252b5132
RH
19601 size_t cnt;
19602
978c4450 19603 if (filedata->dynamic_symbols == NULL)
252b5132 19604 {
591a748a 19605 error (_("conflict list found without a dynamic symbol table\n"));
015dc7e1 19606 return false;
252b5132
RH
19607 }
19608
7296a62a
NC
19609 /* PR 21345 - print a slightly more helpful error message
19610 if we are sure that the cmalloc will fail. */
645f43a8 19611 if (conflictsno > filedata->file_size / sizeof (* iconf))
7296a62a 19612 {
26c527e6
AM
19613 error (_("Overlarge number of conflicts detected: %zx\n"),
19614 conflictsno);
015dc7e1 19615 return false;
7296a62a
NC
19616 }
19617
3f5e193b 19618 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
252b5132
RH
19619 if (iconf == NULL)
19620 {
8b73c356 19621 error (_("Out of memory allocating space for dynamic conflicts\n"));
015dc7e1 19622 return false;
252b5132
RH
19623 }
19624
9ea033b2 19625 if (is_32bit_elf)
252b5132 19626 {
2cf0635d 19627 Elf32_External_Conflict * econf32;
a6e9f9df 19628
3f5e193b 19629 econf32 = (Elf32_External_Conflict *)
95099889
AM
19630 get_data (NULL, filedata, conflicts_offset,
19631 sizeof (*econf32), conflictsno, _("conflict"));
a6e9f9df 19632 if (!econf32)
5a814d6d
AM
19633 {
19634 free (iconf);
015dc7e1 19635 return false;
5a814d6d 19636 }
252b5132
RH
19637
19638 for (cnt = 0; cnt < conflictsno; ++cnt)
19639 iconf[cnt] = BYTE_GET (econf32[cnt]);
a6e9f9df
AM
19640
19641 free (econf32);
252b5132
RH
19642 }
19643 else
19644 {
2cf0635d 19645 Elf64_External_Conflict * econf64;
a6e9f9df 19646
3f5e193b 19647 econf64 = (Elf64_External_Conflict *)
95099889
AM
19648 get_data (NULL, filedata, conflicts_offset,
19649 sizeof (*econf64), conflictsno, _("conflict"));
a6e9f9df 19650 if (!econf64)
5a814d6d
AM
19651 {
19652 free (iconf);
015dc7e1 19653 return false;
5a814d6d 19654 }
252b5132
RH
19655
19656 for (cnt = 0; cnt < conflictsno; ++cnt)
19657 iconf[cnt] = BYTE_GET (econf64[cnt]);
a6e9f9df
AM
19658
19659 free (econf64);
252b5132
RH
19660 }
19661
26c527e6
AM
19662 printf (ngettext ("\nSection '.conflict' contains %zu entry:\n",
19663 "\nSection '.conflict' contains %zu entries:\n",
19664 conflictsno),
19665 conflictsno);
252b5132
RH
19666 puts (_(" Num: Index Value Name"));
19667
19668 for (cnt = 0; cnt < conflictsno; ++cnt)
19669 {
26c527e6 19670 printf ("%5zu: %8lu ", cnt, iconf[cnt]);
e0a31db1 19671
978c4450 19672 if (iconf[cnt] >= filedata->num_dynamic_syms)
e0a31db1 19673 printf (_("<corrupt symbol index>"));
d79b3d50 19674 else
e0a31db1
NC
19675 {
19676 Elf_Internal_Sym * psym;
19677
978c4450 19678 psym = & filedata->dynamic_symbols[iconf[cnt]];
e0a31db1
NC
19679 print_vma (psym->st_value, FULL_HEX);
19680 putchar (' ');
84714f86 19681 if (valid_dynamic_name (filedata, psym->st_name))
b6ac461a 19682 print_symbol_name (25, get_dynamic_name (filedata, psym->st_name));
e0a31db1
NC
19683 else
19684 printf (_("<corrupt: %14ld>"), psym->st_name);
19685 }
31104126 19686 putchar ('\n');
252b5132
RH
19687 }
19688
252b5132
RH
19689 free (iconf);
19690 }
19691
ccb4c951
RS
19692 if (pltgot != 0 && local_gotno != 0)
19693 {
625d49fc 19694 uint64_t ent, local_end, global_end;
bbeee7ea 19695 size_t i, offset;
2cf0635d 19696 unsigned char * data;
82b1b41b 19697 unsigned char * data_end;
bbeee7ea 19698 int addr_size;
ccb4c951 19699
91d6fa6a 19700 ent = pltgot;
ccb4c951
RS
19701 addr_size = (is_32bit_elf ? 4 : 8);
19702 local_end = pltgot + local_gotno * addr_size;
ccb4c951 19703
74e1a04b
NC
19704 /* PR binutils/17533 file: 012-111227-0.004 */
19705 if (symtabno < gotsym)
19706 {
26c527e6
AM
19707 error (_("The GOT symbol offset (%" PRIu64
19708 ") is greater than the symbol table size (%" PRIu64 ")\n"),
19709 gotsym, symtabno);
015dc7e1 19710 return false;
74e1a04b 19711 }
82b1b41b 19712
74e1a04b 19713 global_end = local_end + (symtabno - gotsym) * addr_size;
82b1b41b
NC
19714 /* PR 17531: file: 54c91a34. */
19715 if (global_end < local_end)
19716 {
26c527e6 19717 error (_("Too many GOT symbols: %" PRIu64 "\n"), symtabno);
015dc7e1 19718 return false;
82b1b41b 19719 }
948f632f 19720
dda8d76d
NC
19721 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
19722 data = (unsigned char *) get_data (NULL, filedata, offset,
9cf03b7e
NC
19723 global_end - pltgot, 1,
19724 _("Global Offset Table data"));
919383ac 19725 /* PR 12855: Null data is handled gracefully throughout. */
82b1b41b 19726 data_end = data + (global_end - pltgot);
59245841 19727
ccb4c951
RS
19728 printf (_("\nPrimary GOT:\n"));
19729 printf (_(" Canonical gp value: "));
19730 print_vma (pltgot + 0x7ff0, LONG_HEX);
19731 printf ("\n\n");
19732
19733 printf (_(" Reserved entries:\n"));
19734 printf (_(" %*s %10s %*s Purpose\n"),
2b692964
NC
19735 addr_size * 2, _("Address"), _("Access"),
19736 addr_size * 2, _("Initial"));
82b1b41b 19737 ent = print_mips_got_entry (data, pltgot, ent, data_end);
2b692964 19738 printf (_(" Lazy resolver\n"));
625d49fc 19739 if (ent == (uint64_t) -1)
82b1b41b 19740 goto got_print_fail;
75ec1fdb 19741
c4ab9505
MR
19742 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
19743 This entry will be used by some runtime loaders, to store the
19744 module pointer. Otherwise this is an ordinary local entry.
19745 PR 21344: Check for the entry being fully available before
19746 fetching it. */
19747 if (data
19748 && data + ent - pltgot + addr_size <= data_end
19749 && (byte_get (data + ent - pltgot, addr_size)
19750 >> (addr_size * 8 - 1)) != 0)
19751 {
19752 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19753 printf (_(" Module pointer (GNU extension)\n"));
625d49fc 19754 if (ent == (uint64_t) -1)
c4ab9505 19755 goto got_print_fail;
ccb4c951
RS
19756 }
19757 printf ("\n");
19758
f17e9d8a 19759 if (data != NULL && ent < local_end)
ccb4c951
RS
19760 {
19761 printf (_(" Local entries:\n"));
cc5914eb 19762 printf (" %*s %10s %*s\n",
2b692964
NC
19763 addr_size * 2, _("Address"), _("Access"),
19764 addr_size * 2, _("Initial"));
91d6fa6a 19765 while (ent < local_end)
ccb4c951 19766 {
82b1b41b 19767 ent = print_mips_got_entry (data, pltgot, ent, data_end);
ccb4c951 19768 printf ("\n");
625d49fc 19769 if (ent == (uint64_t) -1)
82b1b41b 19770 goto got_print_fail;
ccb4c951
RS
19771 }
19772 printf ("\n");
19773 }
19774
f17e9d8a 19775 if (data != NULL && gotsym < symtabno)
ccb4c951
RS
19776 {
19777 int sym_width;
19778
19779 printf (_(" Global entries:\n"));
cc5914eb 19780 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
9cf03b7e
NC
19781 addr_size * 2, _("Address"),
19782 _("Access"),
2b692964 19783 addr_size * 2, _("Initial"),
9cf03b7e
NC
19784 addr_size * 2, _("Sym.Val."),
19785 _("Type"),
19786 /* Note for translators: "Ndx" = abbreviated form of "Index". */
19787 _("Ndx"), _("Name"));
0b4362b0 19788
ccb4c951 19789 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
e0a31db1 19790
ccb4c951
RS
19791 for (i = gotsym; i < symtabno; i++)
19792 {
82b1b41b 19793 ent = print_mips_got_entry (data, pltgot, ent, data_end);
ccb4c951 19794 printf (" ");
e0a31db1 19795
978c4450 19796 if (filedata->dynamic_symbols == NULL)
e0a31db1 19797 printf (_("<no dynamic symbols>"));
978c4450 19798 else if (i < filedata->num_dynamic_syms)
e0a31db1 19799 {
978c4450 19800 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
e0a31db1
NC
19801
19802 print_vma (psym->st_value, LONG_HEX);
b6ac461a
NC
19803 printf (" %-7s ", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
19804
19805 bool is_special;
19806 const char * s = printable_section_name_from_index (filedata, psym->st_shndx, & is_special);
19807 if (is_special)
19808 printf ("%3s ", s);
19809 else
19810 printf ("%3u ", psym->st_shndx);
e0a31db1 19811
84714f86 19812 if (valid_dynamic_name (filedata, psym->st_name))
b6ac461a 19813 print_symbol_name (sym_width,
84714f86 19814 get_dynamic_name (filedata, psym->st_name));
e0a31db1
NC
19815 else
19816 printf (_("<corrupt: %14ld>"), psym->st_name);
19817 }
ccb4c951 19818 else
26c527e6
AM
19819 printf (_("<symbol index %zu exceeds number of dynamic symbols>"),
19820 i);
e0a31db1 19821
ccb4c951 19822 printf ("\n");
625d49fc 19823 if (ent == (uint64_t) -1)
82b1b41b 19824 break;
ccb4c951
RS
19825 }
19826 printf ("\n");
19827 }
19828
82b1b41b 19829 got_print_fail:
9db70fc3 19830 free (data);
ccb4c951
RS
19831 }
19832
861fb55a
DJ
19833 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
19834 {
625d49fc 19835 uint64_t ent, end;
26c527e6
AM
19836 uint64_t offset, rel_offset;
19837 uint64_t count, i;
2cf0635d 19838 unsigned char * data;
861fb55a 19839 int addr_size, sym_width;
2cf0635d 19840 Elf_Internal_Rela * rels;
861fb55a 19841
dda8d76d 19842 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
861fb55a
DJ
19843 if (pltrel == DT_RELA)
19844 {
dda8d76d 19845 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
015dc7e1 19846 return false;
861fb55a
DJ
19847 }
19848 else
19849 {
dda8d76d 19850 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
015dc7e1 19851 return false;
861fb55a
DJ
19852 }
19853
91d6fa6a 19854 ent = mips_pltgot;
861fb55a
DJ
19855 addr_size = (is_32bit_elf ? 4 : 8);
19856 end = mips_pltgot + (2 + count) * addr_size;
19857
dda8d76d
NC
19858 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
19859 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
9cf03b7e 19860 1, _("Procedure Linkage Table data"));
59245841 19861 if (data == NULL)
288f0ba2
AM
19862 {
19863 free (rels);
015dc7e1 19864 return false;
288f0ba2 19865 }
59245841 19866
9cf03b7e 19867 printf ("\nPLT GOT:\n\n");
861fb55a
DJ
19868 printf (_(" Reserved entries:\n"));
19869 printf (_(" %*s %*s Purpose\n"),
2b692964 19870 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
91d6fa6a 19871 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
2b692964 19872 printf (_(" PLT lazy resolver\n"));
91d6fa6a 19873 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
2b692964 19874 printf (_(" Module pointer\n"));
861fb55a
DJ
19875 printf ("\n");
19876
19877 printf (_(" Entries:\n"));
cc5914eb 19878 printf (" %*s %*s %*s %-7s %3s %s\n",
2b692964
NC
19879 addr_size * 2, _("Address"),
19880 addr_size * 2, _("Initial"),
19881 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
861fb55a
DJ
19882 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
19883 for (i = 0; i < count; i++)
19884 {
26c527e6 19885 uint64_t idx = get_reloc_symindex (rels[i].r_info);
861fb55a 19886
91d6fa6a 19887 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
861fb55a 19888 printf (" ");
e0a31db1 19889
978c4450 19890 if (idx >= filedata->num_dynamic_syms)
26c527e6 19891 printf (_("<corrupt symbol index: %" PRIu64 ">"), idx);
861fb55a 19892 else
e0a31db1 19893 {
978c4450 19894 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
e0a31db1
NC
19895
19896 print_vma (psym->st_value, LONG_HEX);
19897 printf (" %-7s %3s ",
dda8d76d 19898 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
b6ac461a 19899 printable_section_name_from_index (filedata, psym->st_shndx, NULL));
84714f86 19900 if (valid_dynamic_name (filedata, psym->st_name))
b6ac461a 19901 print_symbol_name (sym_width,
84714f86 19902 get_dynamic_name (filedata, psym->st_name));
e0a31db1
NC
19903 else
19904 printf (_("<corrupt: %14ld>"), psym->st_name);
19905 }
861fb55a
DJ
19906 printf ("\n");
19907 }
19908 printf ("\n");
19909
9db70fc3 19910 free (data);
861fb55a
DJ
19911 free (rels);
19912 }
19913
32ec8896 19914 return res;
252b5132
RH
19915}
19916
015dc7e1 19917static bool
dda8d76d 19918process_nds32_specific (Filedata * filedata)
35c08157
KLC
19919{
19920 Elf_Internal_Shdr *sect = NULL;
19921
dda8d76d 19922 sect = find_section (filedata, ".nds32_e_flags");
9c7b8e9b 19923 if (sect != NULL && sect->sh_size >= 4)
35c08157 19924 {
9c7b8e9b
AM
19925 unsigned char *buf;
19926 unsigned int flag;
35c08157
KLC
19927
19928 printf ("\nNDS32 elf flags section:\n");
9c7b8e9b
AM
19929 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
19930 _("NDS32 elf flags section"));
35c08157 19931
9c7b8e9b 19932 if (buf == NULL)
015dc7e1 19933 return false;
32ec8896 19934
9c7b8e9b
AM
19935 flag = byte_get (buf, 4);
19936 free (buf);
19937 switch (flag & 0x3)
35c08157
KLC
19938 {
19939 case 0:
19940 printf ("(VEC_SIZE):\tNo entry.\n");
19941 break;
19942 case 1:
19943 printf ("(VEC_SIZE):\t4 bytes\n");
19944 break;
19945 case 2:
19946 printf ("(VEC_SIZE):\t16 bytes\n");
19947 break;
19948 case 3:
19949 printf ("(VEC_SIZE):\treserved\n");
19950 break;
19951 }
19952 }
19953
015dc7e1 19954 return true;
35c08157
KLC
19955}
19956
015dc7e1 19957static bool
dda8d76d 19958process_gnu_liblist (Filedata * filedata)
047b2264 19959{
2cf0635d
NC
19960 Elf_Internal_Shdr * section;
19961 Elf_Internal_Shdr * string_sec;
19962 Elf32_External_Lib * elib;
19963 char * strtab;
c256ffe7 19964 size_t strtab_size;
047b2264 19965 size_t cnt;
26c527e6 19966 uint64_t num_liblist;
047b2264 19967 unsigned i;
015dc7e1 19968 bool res = true;
047b2264
JJ
19969
19970 if (! do_arch)
015dc7e1 19971 return true;
047b2264 19972
dda8d76d
NC
19973 for (i = 0, section = filedata->section_headers;
19974 i < filedata->file_header.e_shnum;
b34976b6 19975 i++, section++)
047b2264
JJ
19976 {
19977 switch (section->sh_type)
19978 {
19979 case SHT_GNU_LIBLIST:
dda8d76d 19980 if (section->sh_link >= filedata->file_header.e_shnum)
c256ffe7
JJ
19981 break;
19982
3f5e193b 19983 elib = (Elf32_External_Lib *)
dda8d76d 19984 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
9cf03b7e 19985 _("liblist section data"));
047b2264
JJ
19986
19987 if (elib == NULL)
32ec8896 19988 {
015dc7e1 19989 res = false;
32ec8896
NC
19990 break;
19991 }
047b2264 19992
dda8d76d
NC
19993 string_sec = filedata->section_headers + section->sh_link;
19994 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
3f5e193b
NC
19995 string_sec->sh_size,
19996 _("liblist string table"));
047b2264
JJ
19997 if (strtab == NULL
19998 || section->sh_entsize != sizeof (Elf32_External_Lib))
19999 {
20000 free (elib);
2842702f 20001 free (strtab);
015dc7e1 20002 res = false;
047b2264
JJ
20003 break;
20004 }
59245841 20005 strtab_size = string_sec->sh_size;
047b2264 20006
d3a49aa8 20007 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
26c527e6
AM
20008 printf (ngettext ("\nLibrary list section '%s' contains %" PRIu64
20009 " entries:\n",
20010 "\nLibrary list section '%s' contains %" PRIu64
20011 " entries:\n",
d3a49aa8 20012 num_liblist),
dda8d76d 20013 printable_section_name (filedata, section),
d3a49aa8 20014 num_liblist);
047b2264 20015
2b692964 20016 puts (_(" Library Time Stamp Checksum Version Flags"));
047b2264
JJ
20017
20018 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
20019 ++cnt)
20020 {
20021 Elf32_Lib liblist;
91d6fa6a 20022 time_t atime;
d5b07ef4 20023 char timebuf[128];
2cf0635d 20024 struct tm * tmp;
047b2264
JJ
20025
20026 liblist.l_name = BYTE_GET (elib[cnt].l_name);
91d6fa6a 20027 atime = BYTE_GET (elib[cnt].l_time_stamp);
047b2264
JJ
20028 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
20029 liblist.l_version = BYTE_GET (elib[cnt].l_version);
20030 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
20031
91d6fa6a 20032 tmp = gmtime (&atime);
e9e44622
JJ
20033 snprintf (timebuf, sizeof (timebuf),
20034 "%04u-%02u-%02uT%02u:%02u:%02u",
20035 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
20036 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
047b2264 20037
26c527e6 20038 printf ("%3zu: ", cnt);
047b2264 20039 if (do_wide)
c256ffe7 20040 printf ("%-20s", liblist.l_name < strtab_size
2b692964 20041 ? strtab + liblist.l_name : _("<corrupt>"));
047b2264 20042 else
c256ffe7 20043 printf ("%-20.20s", liblist.l_name < strtab_size
2b692964 20044 ? strtab + liblist.l_name : _("<corrupt>"));
047b2264
JJ
20045 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
20046 liblist.l_version, liblist.l_flags);
20047 }
20048
20049 free (elib);
2842702f 20050 free (strtab);
047b2264
JJ
20051 }
20052 }
20053
32ec8896 20054 return res;
047b2264
JJ
20055}
20056
9437c45b 20057static const char *
dda8d76d 20058get_note_type (Filedata * filedata, unsigned e_type)
779fe533
NC
20059{
20060 static char buff[64];
103f02d3 20061
dda8d76d 20062 if (filedata->file_header.e_type == ET_CORE)
1ec5cd37
NC
20063 switch (e_type)
20064 {
57346661 20065 case NT_AUXV:
1ec5cd37 20066 return _("NT_AUXV (auxiliary vector)");
57346661 20067 case NT_PRSTATUS:
1ec5cd37 20068 return _("NT_PRSTATUS (prstatus structure)");
57346661 20069 case NT_FPREGSET:
1ec5cd37 20070 return _("NT_FPREGSET (floating point registers)");
57346661 20071 case NT_PRPSINFO:
1ec5cd37 20072 return _("NT_PRPSINFO (prpsinfo structure)");
57346661 20073 case NT_TASKSTRUCT:
1ec5cd37 20074 return _("NT_TASKSTRUCT (task structure)");
b63a5e38
AB
20075 case NT_GDB_TDESC:
20076 return _("NT_GDB_TDESC (GDB XML target description)");
57346661 20077 case NT_PRXFPREG:
1ec5cd37 20078 return _("NT_PRXFPREG (user_xfpregs structure)");
e1e95dec
AM
20079 case NT_PPC_VMX:
20080 return _("NT_PPC_VMX (ppc Altivec registers)");
89eeb0bc
LM
20081 case NT_PPC_VSX:
20082 return _("NT_PPC_VSX (ppc VSX registers)");
66c3b5f8
GR
20083 case NT_PPC_TAR:
20084 return _("NT_PPC_TAR (ppc TAR register)");
20085 case NT_PPC_PPR:
20086 return _("NT_PPC_PPR (ppc PPR register)");
20087 case NT_PPC_DSCR:
20088 return _("NT_PPC_DSCR (ppc DSCR register)");
20089 case NT_PPC_EBB:
20090 return _("NT_PPC_EBB (ppc EBB registers)");
20091 case NT_PPC_PMU:
20092 return _("NT_PPC_PMU (ppc PMU registers)");
20093 case NT_PPC_TM_CGPR:
20094 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
20095 case NT_PPC_TM_CFPR:
20096 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
20097 case NT_PPC_TM_CVMX:
20098 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
20099 case NT_PPC_TM_CVSX:
3fd21718 20100 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
66c3b5f8
GR
20101 case NT_PPC_TM_SPR:
20102 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
20103 case NT_PPC_TM_CTAR:
20104 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
20105 case NT_PPC_TM_CPPR:
20106 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
20107 case NT_PPC_TM_CDSCR:
20108 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
ff826ef3
TT
20109 case NT_386_TLS:
20110 return _("NT_386_TLS (x86 TLS information)");
20111 case NT_386_IOPERM:
20112 return _("NT_386_IOPERM (x86 I/O permissions)");
4339cae0
L
20113 case NT_X86_XSTATE:
20114 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
8d58ed37
L
20115 case NT_X86_CET:
20116 return _("NT_X86_CET (x86 CET state)");
0675e188
UW
20117 case NT_S390_HIGH_GPRS:
20118 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
d7eeb400
MS
20119 case NT_S390_TIMER:
20120 return _("NT_S390_TIMER (s390 timer register)");
20121 case NT_S390_TODCMP:
20122 return _("NT_S390_TODCMP (s390 TOD comparator register)");
20123 case NT_S390_TODPREG:
20124 return _("NT_S390_TODPREG (s390 TOD programmable register)");
20125 case NT_S390_CTRS:
20126 return _("NT_S390_CTRS (s390 control registers)");
20127 case NT_S390_PREFIX:
20128 return _("NT_S390_PREFIX (s390 prefix register)");
a367d729
AK
20129 case NT_S390_LAST_BREAK:
20130 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
20131 case NT_S390_SYSTEM_CALL:
20132 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
abb3f6cc
NC
20133 case NT_S390_TDB:
20134 return _("NT_S390_TDB (s390 transaction diagnostic block)");
4ef9f41a
AA
20135 case NT_S390_VXRS_LOW:
20136 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
20137 case NT_S390_VXRS_HIGH:
20138 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
88ab90e8
AA
20139 case NT_S390_GS_CB:
20140 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
20141 case NT_S390_GS_BC:
20142 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
faa9a424
UW
20143 case NT_ARM_VFP:
20144 return _("NT_ARM_VFP (arm VFP registers)");
652451f8
YZ
20145 case NT_ARM_TLS:
20146 return _("NT_ARM_TLS (AArch TLS registers)");
20147 case NT_ARM_HW_BREAK:
20148 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
20149 case NT_ARM_HW_WATCH:
20150 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
eb33f697
LM
20151 case NT_ARM_SYSTEM_CALL:
20152 return _("NT_ARM_SYSTEM_CALL (AArch system call number)");
3b2bef8b
LM
20153 case NT_ARM_SVE:
20154 return _("NT_ARM_SVE (AArch SVE registers)");
20155 case NT_ARM_PAC_MASK:
20156 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
3af2785c
LM
20157 case NT_ARM_PACA_KEYS:
20158 return _("NT_ARM_PACA_KEYS (ARM pointer authentication address keys)");
20159 case NT_ARM_PACG_KEYS:
20160 return _("NT_ARM_PACG_KEYS (ARM pointer authentication generic keys)");
3b2bef8b
LM
20161 case NT_ARM_TAGGED_ADDR_CTRL:
20162 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
a8f175d9
LM
20163 case NT_ARM_SSVE:
20164 return _("NT_ARM_SSVE (AArch64 streaming SVE registers)");
20165 case NT_ARM_ZA:
20166 return _("NT_ARM_ZA (AArch64 SME ZA register)");
11e3488d
LM
20167 case NT_ARM_ZT:
20168 return _("NT_ARM_ZT (AArch64 SME2 ZT registers)");
3af2785c
LM
20169 case NT_ARM_PAC_ENABLED_KEYS:
20170 return _("NT_ARM_PAC_ENABLED_KEYS (AArch64 pointer authentication enabled keys)");
27456742
AK
20171 case NT_ARC_V2:
20172 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
db6092f3
AB
20173 case NT_RISCV_CSR:
20174 return _("NT_RISCV_CSR (RISC-V control and status registers)");
57346661 20175 case NT_PSTATUS:
1ec5cd37 20176 return _("NT_PSTATUS (pstatus structure)");
57346661 20177 case NT_FPREGS:
1ec5cd37 20178 return _("NT_FPREGS (floating point registers)");
57346661 20179 case NT_PSINFO:
1ec5cd37 20180 return _("NT_PSINFO (psinfo structure)");
57346661 20181 case NT_LWPSTATUS:
1ec5cd37 20182 return _("NT_LWPSTATUS (lwpstatus_t structure)");
57346661 20183 case NT_LWPSINFO:
1ec5cd37 20184 return _("NT_LWPSINFO (lwpsinfo_t structure)");
57346661 20185 case NT_WIN32PSTATUS:
1ec5cd37 20186 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
9ece1fa9
TT
20187 case NT_SIGINFO:
20188 return _("NT_SIGINFO (siginfo_t data)");
20189 case NT_FILE:
20190 return _("NT_FILE (mapped files)");
1ec5cd37
NC
20191 default:
20192 break;
20193 }
20194 else
20195 switch (e_type)
20196 {
20197 case NT_VERSION:
20198 return _("NT_VERSION (version)");
20199 case NT_ARCH:
20200 return _("NT_ARCH (architecture)");
9ef920e9 20201 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
6f156d7a 20202 return _("OPEN");
9ef920e9 20203 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
6f156d7a 20204 return _("func");
c8795e1f
NC
20205 case NT_GO_BUILDID:
20206 return _("GO BUILDID");
3ac925fc
LB
20207 case FDO_PACKAGING_METADATA:
20208 return _("FDO_PACKAGING_METADATA");
1ec5cd37
NC
20209 default:
20210 break;
20211 }
20212
e9e44622 20213 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
1ec5cd37 20214 return buff;
779fe533
NC
20215}
20216
015dc7e1 20217static bool
9ece1fa9
TT
20218print_core_note (Elf_Internal_Note *pnote)
20219{
20220 unsigned int addr_size = is_32bit_elf ? 4 : 8;
625d49fc 20221 uint64_t count, page_size;
9ece1fa9
TT
20222 unsigned char *descdata, *filenames, *descend;
20223
20224 if (pnote->type != NT_FILE)
04ac15ab
AS
20225 {
20226 if (do_wide)
20227 printf ("\n");
015dc7e1 20228 return true;
04ac15ab 20229 }
9ece1fa9 20230
9ece1fa9
TT
20231 if (!is_32bit_elf)
20232 {
20233 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
20234 /* Still "successful". */
015dc7e1 20235 return true;
9ece1fa9 20236 }
9ece1fa9
TT
20237
20238 if (pnote->descsz < 2 * addr_size)
20239 {
32ec8896 20240 error (_(" Malformed note - too short for header\n"));
015dc7e1 20241 return false;
9ece1fa9
TT
20242 }
20243
20244 descdata = (unsigned char *) pnote->descdata;
20245 descend = descdata + pnote->descsz;
20246
20247 if (descdata[pnote->descsz - 1] != '\0')
20248 {
32ec8896 20249 error (_(" Malformed note - does not end with \\0\n"));
015dc7e1 20250 return false;
9ece1fa9
TT
20251 }
20252
20253 count = byte_get (descdata, addr_size);
20254 descdata += addr_size;
20255
20256 page_size = byte_get (descdata, addr_size);
20257 descdata += addr_size;
20258
625d49fc 20259 if (count > ((uint64_t) -1 - 2 * addr_size) / (3 * addr_size)
5396a86e 20260 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
9ece1fa9 20261 {
32ec8896 20262 error (_(" Malformed note - too short for supplied file count\n"));
015dc7e1 20263 return false;
9ece1fa9
TT
20264 }
20265
20266 printf (_(" Page size: "));
20267 print_vma (page_size, DEC);
20268 printf ("\n");
20269
20270 printf (_(" %*s%*s%*s\n"),
20271 (int) (2 + 2 * addr_size), _("Start"),
20272 (int) (4 + 2 * addr_size), _("End"),
20273 (int) (4 + 2 * addr_size), _("Page Offset"));
20274 filenames = descdata + count * 3 * addr_size;
595712bb 20275 while (count-- > 0)
9ece1fa9 20276 {
625d49fc 20277 uint64_t start, end, file_ofs;
9ece1fa9
TT
20278
20279 if (filenames == descend)
20280 {
32ec8896 20281 error (_(" Malformed note - filenames end too early\n"));
015dc7e1 20282 return false;
9ece1fa9
TT
20283 }
20284
20285 start = byte_get (descdata, addr_size);
20286 descdata += addr_size;
20287 end = byte_get (descdata, addr_size);
20288 descdata += addr_size;
20289 file_ofs = byte_get (descdata, addr_size);
20290 descdata += addr_size;
20291
20292 printf (" ");
20293 print_vma (start, FULL_HEX);
20294 printf (" ");
20295 print_vma (end, FULL_HEX);
20296 printf (" ");
20297 print_vma (file_ofs, FULL_HEX);
20298 printf ("\n %s\n", filenames);
20299
20300 filenames += 1 + strlen ((char *) filenames);
20301 }
20302
015dc7e1 20303 return true;
9ece1fa9
TT
20304}
20305
1118d252
RM
20306static const char *
20307get_gnu_elf_note_type (unsigned e_type)
20308{
1449284b 20309 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
1118d252
RM
20310 switch (e_type)
20311 {
20312 case NT_GNU_ABI_TAG:
20313 return _("NT_GNU_ABI_TAG (ABI version tag)");
20314 case NT_GNU_HWCAP:
20315 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
20316 case NT_GNU_BUILD_ID:
20317 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
0297aed6
DM
20318 case NT_GNU_GOLD_VERSION:
20319 return _("NT_GNU_GOLD_VERSION (gold version)");
9ef920e9
NC
20320 case NT_GNU_PROPERTY_TYPE_0:
20321 return _("NT_GNU_PROPERTY_TYPE_0");
20322 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
20323 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
20324 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
20325 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
1118d252 20326 default:
1449284b
NC
20327 {
20328 static char buff[64];
1118d252 20329
1449284b
NC
20330 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20331 return buff;
20332 }
20333 }
1118d252
RM
20334}
20335
a9eafb08
L
20336static void
20337decode_x86_compat_isa (unsigned int bitmask)
20338{
20339 while (bitmask)
20340 {
20341 unsigned int bit = bitmask & (- bitmask);
20342
20343 bitmask &= ~ bit;
20344 switch (bit)
20345 {
20346 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
20347 printf ("i486");
20348 break;
20349 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
20350 printf ("586");
20351 break;
20352 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
20353 printf ("686");
20354 break;
20355 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
20356 printf ("SSE");
20357 break;
20358 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
20359 printf ("SSE2");
20360 break;
20361 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
20362 printf ("SSE3");
20363 break;
20364 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
20365 printf ("SSSE3");
20366 break;
20367 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
20368 printf ("SSE4_1");
20369 break;
20370 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
20371 printf ("SSE4_2");
20372 break;
20373 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
20374 printf ("AVX");
20375 break;
20376 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
20377 printf ("AVX2");
20378 break;
20379 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
20380 printf ("AVX512F");
20381 break;
20382 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
20383 printf ("AVX512CD");
20384 break;
20385 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
20386 printf ("AVX512ER");
20387 break;
20388 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
20389 printf ("AVX512PF");
20390 break;
20391 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
20392 printf ("AVX512VL");
20393 break;
20394 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
20395 printf ("AVX512DQ");
20396 break;
20397 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
20398 printf ("AVX512BW");
20399 break;
65b3d26e
L
20400 default:
20401 printf (_("<unknown: %x>"), bit);
20402 break;
a9eafb08
L
20403 }
20404 if (bitmask)
20405 printf (", ");
20406 }
20407}
20408
9ef920e9 20409static void
32930e4e 20410decode_x86_compat_2_isa (unsigned int bitmask)
9ef920e9 20411{
0a59decb 20412 if (!bitmask)
90c745dc
L
20413 {
20414 printf (_("<None>"));
20415 return;
20416 }
90c745dc 20417
9ef920e9
NC
20418 while (bitmask)
20419 {
1fc87489 20420 unsigned int bit = bitmask & (- bitmask);
9ef920e9
NC
20421
20422 bitmask &= ~ bit;
20423 switch (bit)
20424 {
32930e4e 20425 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
a9eafb08
L
20426 printf ("CMOV");
20427 break;
32930e4e 20428 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
a9eafb08
L
20429 printf ("SSE");
20430 break;
32930e4e 20431 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
a9eafb08
L
20432 printf ("SSE2");
20433 break;
32930e4e 20434 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
a9eafb08
L
20435 printf ("SSE3");
20436 break;
32930e4e 20437 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
a9eafb08
L
20438 printf ("SSSE3");
20439 break;
32930e4e 20440 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
a9eafb08
L
20441 printf ("SSE4_1");
20442 break;
32930e4e 20443 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
a9eafb08
L
20444 printf ("SSE4_2");
20445 break;
32930e4e 20446 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
a9eafb08
L
20447 printf ("AVX");
20448 break;
32930e4e 20449 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
a9eafb08
L
20450 printf ("AVX2");
20451 break;
32930e4e 20452 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
a9eafb08
L
20453 printf ("FMA");
20454 break;
32930e4e 20455 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
a9eafb08
L
20456 printf ("AVX512F");
20457 break;
32930e4e 20458 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
a9eafb08
L
20459 printf ("AVX512CD");
20460 break;
32930e4e 20461 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
a9eafb08
L
20462 printf ("AVX512ER");
20463 break;
32930e4e 20464 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
a9eafb08
L
20465 printf ("AVX512PF");
20466 break;
32930e4e 20467 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
a9eafb08
L
20468 printf ("AVX512VL");
20469 break;
32930e4e 20470 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
a9eafb08
L
20471 printf ("AVX512DQ");
20472 break;
32930e4e 20473 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
a9eafb08
L
20474 printf ("AVX512BW");
20475 break;
32930e4e 20476 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
a9eafb08
L
20477 printf ("AVX512_4FMAPS");
20478 break;
32930e4e 20479 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
a9eafb08
L
20480 printf ("AVX512_4VNNIW");
20481 break;
32930e4e 20482 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
a9eafb08
L
20483 printf ("AVX512_BITALG");
20484 break;
32930e4e 20485 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
a9eafb08
L
20486 printf ("AVX512_IFMA");
20487 break;
32930e4e 20488 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
a9eafb08
L
20489 printf ("AVX512_VBMI");
20490 break;
32930e4e 20491 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
a9eafb08
L
20492 printf ("AVX512_VBMI2");
20493 break;
32930e4e 20494 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
a9eafb08
L
20495 printf ("AVX512_VNNI");
20496 break;
32930e4e 20497 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
462cac58
L
20498 printf ("AVX512_BF16");
20499 break;
65b3d26e
L
20500 default:
20501 printf (_("<unknown: %x>"), bit);
20502 break;
9ef920e9
NC
20503 }
20504 if (bitmask)
20505 printf (", ");
20506 }
20507}
20508
28cdbb18
SM
20509static const char *
20510get_amdgpu_elf_note_type (unsigned int e_type)
20511{
20512 switch (e_type)
20513 {
20514 case NT_AMDGPU_METADATA:
20515 return _("NT_AMDGPU_METADATA (code object metadata)");
20516 default:
20517 {
20518 static char buf[64];
20519 snprintf (buf, sizeof (buf), _("Unknown note type: (0x%08x)"), e_type);
20520 return buf;
20521 }
20522 }
20523}
20524
32930e4e
L
20525static void
20526decode_x86_isa (unsigned int bitmask)
20527{
32930e4e
L
20528 while (bitmask)
20529 {
20530 unsigned int bit = bitmask & (- bitmask);
20531
20532 bitmask &= ~ bit;
20533 switch (bit)
20534 {
b0ab0693
L
20535 case GNU_PROPERTY_X86_ISA_1_BASELINE:
20536 printf ("x86-64-baseline");
20537 break;
32930e4e
L
20538 case GNU_PROPERTY_X86_ISA_1_V2:
20539 printf ("x86-64-v2");
20540 break;
20541 case GNU_PROPERTY_X86_ISA_1_V3:
20542 printf ("x86-64-v3");
20543 break;
20544 case GNU_PROPERTY_X86_ISA_1_V4:
20545 printf ("x86-64-v4");
20546 break;
20547 default:
20548 printf (_("<unknown: %x>"), bit);
20549 break;
20550 }
20551 if (bitmask)
20552 printf (", ");
20553 }
20554}
20555
ee2fdd6f 20556static void
a9eafb08 20557decode_x86_feature_1 (unsigned int bitmask)
ee2fdd6f 20558{
0a59decb 20559 if (!bitmask)
90c745dc
L
20560 {
20561 printf (_("<None>"));
20562 return;
20563 }
90c745dc 20564
ee2fdd6f
L
20565 while (bitmask)
20566 {
20567 unsigned int bit = bitmask & (- bitmask);
20568
20569 bitmask &= ~ bit;
20570 switch (bit)
20571 {
20572 case GNU_PROPERTY_X86_FEATURE_1_IBT:
a9eafb08 20573 printf ("IBT");
ee2fdd6f 20574 break;
48580982 20575 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
a9eafb08 20576 printf ("SHSTK");
48580982 20577 break;
279d901e
L
20578 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
20579 printf ("LAM_U48");
20580 break;
20581 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
20582 printf ("LAM_U57");
20583 break;
ee2fdd6f
L
20584 default:
20585 printf (_("<unknown: %x>"), bit);
20586 break;
20587 }
20588 if (bitmask)
20589 printf (", ");
20590 }
20591}
20592
a9eafb08
L
20593static void
20594decode_x86_feature_2 (unsigned int bitmask)
20595{
0a59decb 20596 if (!bitmask)
90c745dc
L
20597 {
20598 printf (_("<None>"));
20599 return;
20600 }
90c745dc 20601
a9eafb08
L
20602 while (bitmask)
20603 {
20604 unsigned int bit = bitmask & (- bitmask);
20605
20606 bitmask &= ~ bit;
20607 switch (bit)
20608 {
20609 case GNU_PROPERTY_X86_FEATURE_2_X86:
20610 printf ("x86");
20611 break;
20612 case GNU_PROPERTY_X86_FEATURE_2_X87:
20613 printf ("x87");
20614 break;
20615 case GNU_PROPERTY_X86_FEATURE_2_MMX:
20616 printf ("MMX");
20617 break;
20618 case GNU_PROPERTY_X86_FEATURE_2_XMM:
20619 printf ("XMM");
20620 break;
20621 case GNU_PROPERTY_X86_FEATURE_2_YMM:
20622 printf ("YMM");
20623 break;
20624 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
20625 printf ("ZMM");
20626 break;
a308b89d
L
20627 case GNU_PROPERTY_X86_FEATURE_2_TMM:
20628 printf ("TMM");
20629 break;
32930e4e
L
20630 case GNU_PROPERTY_X86_FEATURE_2_MASK:
20631 printf ("MASK");
20632 break;
a9eafb08
L
20633 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
20634 printf ("FXSR");
20635 break;
20636 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
20637 printf ("XSAVE");
20638 break;
20639 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
20640 printf ("XSAVEOPT");
20641 break;
20642 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
20643 printf ("XSAVEC");
20644 break;
65b3d26e
L
20645 default:
20646 printf (_("<unknown: %x>"), bit);
20647 break;
a9eafb08
L
20648 }
20649 if (bitmask)
20650 printf (", ");
20651 }
20652}
20653
cd702818
SD
20654static void
20655decode_aarch64_feature_1_and (unsigned int bitmask)
20656{
20657 while (bitmask)
20658 {
20659 unsigned int bit = bitmask & (- bitmask);
20660
20661 bitmask &= ~ bit;
20662 switch (bit)
20663 {
20664 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
20665 printf ("BTI");
20666 break;
20667
20668 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
20669 printf ("PAC");
20670 break;
20671
20672 default:
20673 printf (_("<unknown: %x>"), bit);
20674 break;
20675 }
20676 if (bitmask)
20677 printf (", ");
20678 }
20679}
20680
6320fd00
L
20681static void
20682decode_1_needed (unsigned int bitmask)
20683{
20684 while (bitmask)
20685 {
20686 unsigned int bit = bitmask & (- bitmask);
20687
20688 bitmask &= ~ bit;
20689 switch (bit)
20690 {
20691 case GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
20692 printf ("indirect external access");
20693 break;
20694 default:
20695 printf (_("<unknown: %x>"), bit);
20696 break;
20697 }
20698 if (bitmask)
20699 printf (", ");
20700 }
20701}
20702
9ef920e9 20703static void
dda8d76d 20704print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
9ef920e9
NC
20705{
20706 unsigned char * ptr = (unsigned char *) pnote->descdata;
20707 unsigned char * ptr_end = ptr + pnote->descsz;
20708 unsigned int size = is_32bit_elf ? 4 : 8;
20709
20710 printf (_(" Properties: "));
20711
1fc87489 20712 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
9ef920e9
NC
20713 {
20714 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
20715 return;
20716 }
20717
6ab2c4ed 20718 while (ptr < ptr_end)
9ef920e9 20719 {
1fc87489 20720 unsigned int j;
6ab2c4ed
MC
20721 unsigned int type;
20722 unsigned int datasz;
20723
20724 if ((size_t) (ptr_end - ptr) < 8)
20725 {
20726 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
20727 break;
20728 }
20729
20730 type = byte_get (ptr, 4);
20731 datasz = byte_get (ptr + 4, 4);
9ef920e9 20732
1fc87489 20733 ptr += 8;
9ef920e9 20734
6ab2c4ed 20735 if (datasz > (size_t) (ptr_end - ptr))
9ef920e9 20736 {
1fc87489
L
20737 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
20738 type, datasz);
9ef920e9 20739 break;
1fc87489 20740 }
9ef920e9 20741
1fc87489
L
20742 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
20743 {
dda8d76d
NC
20744 if (filedata->file_header.e_machine == EM_X86_64
20745 || filedata->file_header.e_machine == EM_IAMCU
20746 || filedata->file_header.e_machine == EM_386)
1fc87489 20747 {
aa7bca9b
L
20748 unsigned int bitmask;
20749
20750 if (datasz == 4)
0a59decb 20751 bitmask = byte_get (ptr, 4);
aa7bca9b
L
20752 else
20753 bitmask = 0;
20754
1fc87489
L
20755 switch (type)
20756 {
20757 case GNU_PROPERTY_X86_ISA_1_USED:
1fc87489 20758 if (datasz != 4)
aa7bca9b
L
20759 printf (_("x86 ISA used: <corrupt length: %#x> "),
20760 datasz);
1fc87489 20761 else
aa7bca9b
L
20762 {
20763 printf ("x86 ISA used: ");
20764 decode_x86_isa (bitmask);
20765 }
1fc87489 20766 goto next;
9ef920e9 20767
1fc87489 20768 case GNU_PROPERTY_X86_ISA_1_NEEDED:
1fc87489 20769 if (datasz != 4)
aa7bca9b
L
20770 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20771 datasz);
1fc87489 20772 else
aa7bca9b
L
20773 {
20774 printf ("x86 ISA needed: ");
20775 decode_x86_isa (bitmask);
20776 }
1fc87489 20777 goto next;
9ef920e9 20778
ee2fdd6f 20779 case GNU_PROPERTY_X86_FEATURE_1_AND:
ee2fdd6f 20780 if (datasz != 4)
aa7bca9b
L
20781 printf (_("x86 feature: <corrupt length: %#x> "),
20782 datasz);
ee2fdd6f 20783 else
aa7bca9b
L
20784 {
20785 printf ("x86 feature: ");
a9eafb08
L
20786 decode_x86_feature_1 (bitmask);
20787 }
20788 goto next;
20789
20790 case GNU_PROPERTY_X86_FEATURE_2_USED:
20791 if (datasz != 4)
20792 printf (_("x86 feature used: <corrupt length: %#x> "),
20793 datasz);
20794 else
20795 {
20796 printf ("x86 feature used: ");
20797 decode_x86_feature_2 (bitmask);
20798 }
20799 goto next;
20800
20801 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
20802 if (datasz != 4)
20803 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
20804 else
20805 {
20806 printf ("x86 feature needed: ");
20807 decode_x86_feature_2 (bitmask);
20808 }
20809 goto next;
20810
20811 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
20812 if (datasz != 4)
20813 printf (_("x86 ISA used: <corrupt length: %#x> "),
20814 datasz);
20815 else
20816 {
20817 printf ("x86 ISA used: ");
20818 decode_x86_compat_isa (bitmask);
20819 }
20820 goto next;
20821
20822 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
20823 if (datasz != 4)
20824 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20825 datasz);
20826 else
20827 {
20828 printf ("x86 ISA needed: ");
20829 decode_x86_compat_isa (bitmask);
aa7bca9b 20830 }
ee2fdd6f
L
20831 goto next;
20832
32930e4e
L
20833 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
20834 if (datasz != 4)
20835 printf (_("x86 ISA used: <corrupt length: %#x> "),
20836 datasz);
20837 else
20838 {
20839 printf ("x86 ISA used: ");
20840 decode_x86_compat_2_isa (bitmask);
20841 }
20842 goto next;
20843
20844 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
20845 if (datasz != 4)
20846 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20847 datasz);
20848 else
20849 {
20850 printf ("x86 ISA needed: ");
20851 decode_x86_compat_2_isa (bitmask);
20852 }
20853 goto next;
20854
1fc87489
L
20855 default:
20856 break;
20857 }
20858 }
cd702818
SD
20859 else if (filedata->file_header.e_machine == EM_AARCH64)
20860 {
20861 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
20862 {
20863 printf ("AArch64 feature: ");
20864 if (datasz != 4)
20865 printf (_("<corrupt length: %#x> "), datasz);
20866 else
20867 decode_aarch64_feature_1_and (byte_get (ptr, 4));
20868 goto next;
20869 }
20870 }
1fc87489
L
20871 }
20872 else
20873 {
20874 switch (type)
9ef920e9 20875 {
1fc87489
L
20876 case GNU_PROPERTY_STACK_SIZE:
20877 printf (_("stack size: "));
20878 if (datasz != size)
20879 printf (_("<corrupt length: %#x> "), datasz);
20880 else
26c527e6 20881 printf ("%#" PRIx64, byte_get (ptr, size));
1fc87489
L
20882 goto next;
20883
20884 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
20885 printf ("no copy on protected ");
20886 if (datasz)
20887 printf (_("<corrupt length: %#x> "), datasz);
20888 goto next;
20889
20890 default:
5a767724
L
20891 if ((type >= GNU_PROPERTY_UINT32_AND_LO
20892 && type <= GNU_PROPERTY_UINT32_AND_HI)
20893 || (type >= GNU_PROPERTY_UINT32_OR_LO
20894 && type <= GNU_PROPERTY_UINT32_OR_HI))
20895 {
6320fd00
L
20896 switch (type)
20897 {
20898 case GNU_PROPERTY_1_NEEDED:
20899 if (datasz != 4)
20900 printf (_("1_needed: <corrupt length: %#x> "),
20901 datasz);
20902 else
20903 {
20904 unsigned int bitmask = byte_get (ptr, 4);
20905 printf ("1_needed: ");
20906 decode_1_needed (bitmask);
20907 }
20908 goto next;
20909
20910 default:
20911 break;
20912 }
5a767724
L
20913 if (type <= GNU_PROPERTY_UINT32_AND_HI)
20914 printf (_("UINT32_AND (%#x): "), type);
20915 else
20916 printf (_("UINT32_OR (%#x): "), type);
20917 if (datasz != 4)
20918 printf (_("<corrupt length: %#x> "), datasz);
20919 else
20920 printf ("%#x", (unsigned int) byte_get (ptr, 4));
20921 goto next;
20922 }
9ef920e9
NC
20923 break;
20924 }
9ef920e9
NC
20925 }
20926
1fc87489
L
20927 if (type < GNU_PROPERTY_LOPROC)
20928 printf (_("<unknown type %#x data: "), type);
20929 else if (type < GNU_PROPERTY_LOUSER)
8c3853d9 20930 printf (_("<processor-specific type %#x data: "), type);
1fc87489
L
20931 else
20932 printf (_("<application-specific type %#x data: "), type);
20933 for (j = 0; j < datasz; ++j)
20934 printf ("%02x ", ptr[j] & 0xff);
20935 printf (">");
20936
dc1e8a47 20937 next:
9ef920e9 20938 ptr += ((datasz + (size - 1)) & ~ (size - 1));
1fc87489
L
20939 if (ptr == ptr_end)
20940 break;
1fc87489 20941
6ab2c4ed
MC
20942 if (do_wide)
20943 printf (", ");
20944 else
20945 printf ("\n\t");
9ef920e9
NC
20946 }
20947
20948 printf ("\n");
20949}
20950
015dc7e1 20951static bool
dda8d76d 20952print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
664f90a3 20953{
1449284b 20954 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
664f90a3
TT
20955 switch (pnote->type)
20956 {
20957 case NT_GNU_BUILD_ID:
20958 {
26c527e6 20959 size_t i;
664f90a3
TT
20960
20961 printf (_(" Build ID: "));
20962 for (i = 0; i < pnote->descsz; ++i)
20963 printf ("%02x", pnote->descdata[i] & 0xff);
9cf03b7e 20964 printf ("\n");
664f90a3
TT
20965 }
20966 break;
20967
20968 case NT_GNU_ABI_TAG:
20969 {
26c527e6 20970 unsigned int os, major, minor, subminor;
664f90a3
TT
20971 const char *osname;
20972
3102e897
NC
20973 /* PR 17531: file: 030-599401-0.004. */
20974 if (pnote->descsz < 16)
20975 {
20976 printf (_(" <corrupt GNU_ABI_TAG>\n"));
20977 break;
20978 }
20979
664f90a3
TT
20980 os = byte_get ((unsigned char *) pnote->descdata, 4);
20981 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20982 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
20983 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
20984
20985 switch (os)
20986 {
20987 case GNU_ABI_TAG_LINUX:
20988 osname = "Linux";
20989 break;
20990 case GNU_ABI_TAG_HURD:
20991 osname = "Hurd";
20992 break;
20993 case GNU_ABI_TAG_SOLARIS:
20994 osname = "Solaris";
20995 break;
20996 case GNU_ABI_TAG_FREEBSD:
20997 osname = "FreeBSD";
20998 break;
20999 case GNU_ABI_TAG_NETBSD:
21000 osname = "NetBSD";
21001 break;
14ae95f2
RM
21002 case GNU_ABI_TAG_SYLLABLE:
21003 osname = "Syllable";
21004 break;
21005 case GNU_ABI_TAG_NACL:
21006 osname = "NaCl";
21007 break;
664f90a3
TT
21008 default:
21009 osname = "Unknown";
21010 break;
21011 }
21012
26c527e6 21013 printf (_(" OS: %s, ABI: %d.%d.%d\n"), osname,
664f90a3
TT
21014 major, minor, subminor);
21015 }
21016 break;
926c5385
CC
21017
21018 case NT_GNU_GOLD_VERSION:
21019 {
26c527e6 21020 size_t i;
926c5385
CC
21021
21022 printf (_(" Version: "));
21023 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
21024 printf ("%c", pnote->descdata[i]);
21025 printf ("\n");
21026 }
21027 break;
1449284b
NC
21028
21029 case NT_GNU_HWCAP:
21030 {
26c527e6 21031 unsigned int num_entries, mask;
1449284b
NC
21032
21033 /* Hardware capabilities information. Word 0 is the number of entries.
21034 Word 1 is a bitmask of enabled entries. The rest of the descriptor
21035 is a series of entries, where each entry is a single byte followed
21036 by a nul terminated string. The byte gives the bit number to test
21037 if enabled in the bitmask. */
21038 printf (_(" Hardware Capabilities: "));
21039 if (pnote->descsz < 8)
21040 {
32ec8896 21041 error (_("<corrupt GNU_HWCAP>\n"));
015dc7e1 21042 return false;
1449284b
NC
21043 }
21044 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
21045 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
26c527e6 21046 printf (_("num entries: %d, enabled mask: %x\n"), num_entries, mask);
1449284b
NC
21047 /* FIXME: Add code to display the entries... */
21048 }
21049 break;
21050
9ef920e9 21051 case NT_GNU_PROPERTY_TYPE_0:
dda8d76d 21052 print_gnu_property_note (filedata, pnote);
9ef920e9 21053 break;
9abca702 21054
1449284b
NC
21055 default:
21056 /* Handle unrecognised types. An error message should have already been
21057 created by get_gnu_elf_note_type(), so all that we need to do is to
21058 display the data. */
21059 {
26c527e6 21060 size_t i;
1449284b
NC
21061
21062 printf (_(" Description data: "));
21063 for (i = 0; i < pnote->descsz; ++i)
21064 printf ("%02x ", pnote->descdata[i] & 0xff);
21065 printf ("\n");
21066 }
21067 break;
664f90a3
TT
21068 }
21069
015dc7e1 21070 return true;
664f90a3
TT
21071}
21072
685080f2
NC
21073static const char *
21074get_v850_elf_note_type (enum v850_notes n_type)
21075{
21076 static char buff[64];
21077
21078 switch (n_type)
21079 {
21080 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
21081 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
21082 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
21083 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
21084 case V850_NOTE_CACHE_INFO: return _("Use of cache");
21085 case V850_NOTE_MMU_INFO: return _("Use of MMU");
21086 default:
21087 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
21088 return buff;
21089 }
21090}
21091
015dc7e1 21092static bool
685080f2
NC
21093print_v850_note (Elf_Internal_Note * pnote)
21094{
21095 unsigned int val;
21096
21097 if (pnote->descsz != 4)
015dc7e1 21098 return false;
32ec8896 21099
685080f2
NC
21100 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
21101
21102 if (val == 0)
21103 {
21104 printf (_("not set\n"));
015dc7e1 21105 return true;
685080f2
NC
21106 }
21107
21108 switch (pnote->type)
21109 {
21110 case V850_NOTE_ALIGNMENT:
21111 switch (val)
21112 {
015dc7e1
AM
21113 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
21114 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
685080f2
NC
21115 }
21116 break;
14ae95f2 21117
685080f2
NC
21118 case V850_NOTE_DATA_SIZE:
21119 switch (val)
21120 {
015dc7e1
AM
21121 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
21122 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
685080f2
NC
21123 }
21124 break;
14ae95f2 21125
685080f2
NC
21126 case V850_NOTE_FPU_INFO:
21127 switch (val)
21128 {
015dc7e1
AM
21129 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
21130 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
685080f2
NC
21131 }
21132 break;
14ae95f2 21133
685080f2
NC
21134 case V850_NOTE_MMU_INFO:
21135 case V850_NOTE_CACHE_INFO:
21136 case V850_NOTE_SIMD_INFO:
21137 if (val == EF_RH850_SIMD)
21138 {
21139 printf (_("yes\n"));
015dc7e1 21140 return true;
685080f2
NC
21141 }
21142 break;
21143
21144 default:
21145 /* An 'unknown note type' message will already have been displayed. */
21146 break;
21147 }
21148
21149 printf (_("unknown value: %x\n"), val);
015dc7e1 21150 return false;
685080f2
NC
21151}
21152
015dc7e1 21153static bool
c6056a74
SF
21154process_netbsd_elf_note (Elf_Internal_Note * pnote)
21155{
21156 unsigned int version;
21157
21158 switch (pnote->type)
21159 {
21160 case NT_NETBSD_IDENT:
b966f55f
AM
21161 if (pnote->descsz < 1)
21162 break;
c6056a74
SF
21163 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
21164 if ((version / 10000) % 100)
b966f55f 21165 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
c6056a74
SF
21166 version, version / 100000000, (version / 1000000) % 100,
21167 (version / 10000) % 100 > 26 ? "Z" : "",
15f205b1 21168 'A' + (version / 10000) % 26);
c6056a74
SF
21169 else
21170 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
b966f55f 21171 version, version / 100000000, (version / 1000000) % 100,
15f205b1 21172 (version / 100) % 100);
015dc7e1 21173 return true;
c6056a74
SF
21174
21175 case NT_NETBSD_MARCH:
9abca702 21176 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
c6056a74 21177 pnote->descdata);
015dc7e1 21178 return true;
c6056a74 21179
9abca702 21180 case NT_NETBSD_PAX:
b966f55f
AM
21181 if (pnote->descsz < 1)
21182 break;
9abca702
CZ
21183 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
21184 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
21185 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
21186 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
21187 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
21188 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
21189 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
21190 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
015dc7e1 21191 return true;
c6056a74 21192 }
b966f55f
AM
21193
21194 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
21195 pnote->descsz, pnote->type);
015dc7e1 21196 return false;
c6056a74
SF
21197}
21198
f4ddf30f 21199static const char *
dda8d76d 21200get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
f4ddf30f 21201{
f4ddf30f
JB
21202 switch (e_type)
21203 {
21204 case NT_FREEBSD_THRMISC:
21205 return _("NT_THRMISC (thrmisc structure)");
21206 case NT_FREEBSD_PROCSTAT_PROC:
21207 return _("NT_PROCSTAT_PROC (proc data)");
21208 case NT_FREEBSD_PROCSTAT_FILES:
21209 return _("NT_PROCSTAT_FILES (files data)");
21210 case NT_FREEBSD_PROCSTAT_VMMAP:
21211 return _("NT_PROCSTAT_VMMAP (vmmap data)");
21212 case NT_FREEBSD_PROCSTAT_GROUPS:
21213 return _("NT_PROCSTAT_GROUPS (groups data)");
21214 case NT_FREEBSD_PROCSTAT_UMASK:
21215 return _("NT_PROCSTAT_UMASK (umask data)");
21216 case NT_FREEBSD_PROCSTAT_RLIMIT:
21217 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
21218 case NT_FREEBSD_PROCSTAT_OSREL:
21219 return _("NT_PROCSTAT_OSREL (osreldate data)");
21220 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
21221 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
21222 case NT_FREEBSD_PROCSTAT_AUXV:
21223 return _("NT_PROCSTAT_AUXV (auxv data)");
0b9305ed
JB
21224 case NT_FREEBSD_PTLWPINFO:
21225 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
a171378a
JB
21226 case NT_FREEBSD_X86_SEGBASES:
21227 return _("NT_X86_SEGBASES (x86 segment base registers)");
f4ddf30f 21228 }
dda8d76d 21229 return get_note_type (filedata, e_type);
f4ddf30f
JB
21230}
21231
9437c45b 21232static const char *
dda8d76d 21233get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
9437c45b
JT
21234{
21235 static char buff[64];
21236
540e6170
CZ
21237 switch (e_type)
21238 {
21239 case NT_NETBSDCORE_PROCINFO:
21240 /* NetBSD core "procinfo" structure. */
21241 return _("NetBSD procinfo structure");
9437c45b 21242
540e6170
CZ
21243 case NT_NETBSDCORE_AUXV:
21244 return _("NetBSD ELF auxiliary vector data");
9437c45b 21245
06d949ec
KR
21246 case NT_NETBSDCORE_LWPSTATUS:
21247 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
06d949ec 21248
540e6170 21249 default:
06d949ec 21250 /* As of Jan 2020 there are no other machine-independent notes
540e6170
CZ
21251 defined for NetBSD core files. If the note type is less
21252 than the start of the machine-dependent note types, we don't
21253 understand it. */
21254
21255 if (e_type < NT_NETBSDCORE_FIRSTMACH)
21256 {
21257 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
21258 return buff;
21259 }
21260 break;
9437c45b
JT
21261 }
21262
dda8d76d 21263 switch (filedata->file_header.e_machine)
9437c45b
JT
21264 {
21265 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
21266 and PT_GETFPREGS == mach+2. */
21267
21268 case EM_OLD_ALPHA:
21269 case EM_ALPHA:
21270 case EM_SPARC:
21271 case EM_SPARC32PLUS:
21272 case EM_SPARCV9:
21273 switch (e_type)
21274 {
2b692964 21275 case NT_NETBSDCORE_FIRSTMACH + 0:
b4db1224 21276 return _("PT_GETREGS (reg structure)");
2b692964 21277 case NT_NETBSDCORE_FIRSTMACH + 2:
b4db1224 21278 return _("PT_GETFPREGS (fpreg structure)");
9437c45b
JT
21279 default:
21280 break;
21281 }
21282 break;
21283
c0d38b0e
CZ
21284 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
21285 There's also old PT___GETREGS40 == mach + 1 for old reg
21286 structure which lacks GBR. */
21287 case EM_SH:
21288 switch (e_type)
21289 {
21290 case NT_NETBSDCORE_FIRSTMACH + 1:
21291 return _("PT___GETREGS40 (old reg structure)");
21292 case NT_NETBSDCORE_FIRSTMACH + 3:
21293 return _("PT_GETREGS (reg structure)");
21294 case NT_NETBSDCORE_FIRSTMACH + 5:
21295 return _("PT_GETFPREGS (fpreg structure)");
21296 default:
21297 break;
21298 }
21299 break;
21300
9437c45b
JT
21301 /* On all other arch's, PT_GETREGS == mach+1 and
21302 PT_GETFPREGS == mach+3. */
21303 default:
21304 switch (e_type)
21305 {
2b692964 21306 case NT_NETBSDCORE_FIRSTMACH + 1:
b4db1224 21307 return _("PT_GETREGS (reg structure)");
2b692964 21308 case NT_NETBSDCORE_FIRSTMACH + 3:
b4db1224 21309 return _("PT_GETFPREGS (fpreg structure)");
9437c45b
JT
21310 default:
21311 break;
21312 }
21313 }
21314
9cf03b7e 21315 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
e9e44622 21316 e_type - NT_NETBSDCORE_FIRSTMACH);
9437c45b
JT
21317 return buff;
21318}
21319
98ca73af
FC
21320static const char *
21321get_openbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
21322{
21323 switch (e_type)
21324 {
21325 case NT_OPENBSD_PROCINFO:
21326 return _("OpenBSD procinfo structure");
21327 case NT_OPENBSD_AUXV:
21328 return _("OpenBSD ELF auxiliary vector data");
21329 case NT_OPENBSD_REGS:
21330 return _("OpenBSD regular registers");
21331 case NT_OPENBSD_FPREGS:
21332 return _("OpenBSD floating point registers");
21333 case NT_OPENBSD_WCOOKIE:
21334 return _("OpenBSD window cookie");
21335 }
21336
21337 return get_note_type (filedata, e_type);
21338}
21339
e263a66b
CC
21340static const char *
21341get_qnx_elfcore_note_type (Filedata * filedata, unsigned e_type)
21342{
21343 switch (e_type)
21344 {
21345 case QNT_DEBUG_FULLPATH:
21346 return _("QNX debug fullpath");
21347 case QNT_DEBUG_RELOC:
21348 return _("QNX debug relocation");
21349 case QNT_STACK:
21350 return _("QNX stack");
21351 case QNT_GENERATOR:
21352 return _("QNX generator");
21353 case QNT_DEFAULT_LIB:
21354 return _("QNX default library");
21355 case QNT_CORE_SYSINFO:
21356 return _("QNX core sysinfo");
21357 case QNT_CORE_INFO:
21358 return _("QNX core info");
21359 case QNT_CORE_STATUS:
21360 return _("QNX core status");
21361 case QNT_CORE_GREG:
21362 return _("QNX general registers");
21363 case QNT_CORE_FPREG:
21364 return _("QNX floating point registers");
21365 case QNT_LINK_MAP:
21366 return _("QNX link map");
21367 }
21368
21369 return get_note_type (filedata, e_type);
21370}
21371
70616151
TT
21372static const char *
21373get_stapsdt_note_type (unsigned e_type)
21374{
21375 static char buff[64];
21376
21377 switch (e_type)
21378 {
21379 case NT_STAPSDT:
21380 return _("NT_STAPSDT (SystemTap probe descriptors)");
21381
21382 default:
21383 break;
21384 }
21385
21386 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
21387 return buff;
21388}
21389
015dc7e1 21390static bool
c6a9fc58
TT
21391print_stapsdt_note (Elf_Internal_Note *pnote)
21392{
3ca60c57 21393 size_t len, maxlen;
26c527e6 21394 size_t addr_size = is_32bit_elf ? 4 : 8;
c6a9fc58
TT
21395 char *data = pnote->descdata;
21396 char *data_end = pnote->descdata + pnote->descsz;
625d49fc 21397 uint64_t pc, base_addr, semaphore;
c6a9fc58
TT
21398 char *provider, *probe, *arg_fmt;
21399
3ca60c57
NC
21400 if (pnote->descsz < (addr_size * 3))
21401 goto stapdt_note_too_small;
21402
c6a9fc58
TT
21403 pc = byte_get ((unsigned char *) data, addr_size);
21404 data += addr_size;
3ca60c57 21405
c6a9fc58
TT
21406 base_addr = byte_get ((unsigned char *) data, addr_size);
21407 data += addr_size;
3ca60c57 21408
c6a9fc58
TT
21409 semaphore = byte_get ((unsigned char *) data, addr_size);
21410 data += addr_size;
21411
3ca60c57
NC
21412 if (data >= data_end)
21413 goto stapdt_note_too_small;
21414 maxlen = data_end - data;
21415 len = strnlen (data, maxlen);
21416 if (len < maxlen)
21417 {
21418 provider = data;
21419 data += len + 1;
21420 }
21421 else
21422 goto stapdt_note_too_small;
21423
21424 if (data >= data_end)
21425 goto stapdt_note_too_small;
21426 maxlen = data_end - data;
21427 len = strnlen (data, maxlen);
21428 if (len < maxlen)
21429 {
21430 probe = data;
21431 data += len + 1;
21432 }
21433 else
21434 goto stapdt_note_too_small;
9abca702 21435
3ca60c57
NC
21436 if (data >= data_end)
21437 goto stapdt_note_too_small;
21438 maxlen = data_end - data;
21439 len = strnlen (data, maxlen);
21440 if (len < maxlen)
21441 {
21442 arg_fmt = data;
21443 data += len + 1;
21444 }
21445 else
21446 goto stapdt_note_too_small;
c6a9fc58
TT
21447
21448 printf (_(" Provider: %s\n"), provider);
21449 printf (_(" Name: %s\n"), probe);
21450 printf (_(" Location: "));
21451 print_vma (pc, FULL_HEX);
21452 printf (_(", Base: "));
21453 print_vma (base_addr, FULL_HEX);
21454 printf (_(", Semaphore: "));
21455 print_vma (semaphore, FULL_HEX);
9cf03b7e 21456 printf ("\n");
c6a9fc58
TT
21457 printf (_(" Arguments: %s\n"), arg_fmt);
21458
21459 return data == data_end;
3ca60c57
NC
21460
21461 stapdt_note_too_small:
21462 printf (_(" <corrupt - note is too small>\n"));
21463 error (_("corrupt stapdt note - the data size is too small\n"));
015dc7e1 21464 return false;
c6a9fc58
TT
21465}
21466
e5382207
LB
21467static bool
21468print_fdo_note (Elf_Internal_Note * pnote)
21469{
21470 if (pnote->descsz > 0 && pnote->type == FDO_PACKAGING_METADATA)
21471 {
21472 printf (_(" Packaging Metadata: %.*s\n"), (int) pnote->descsz, pnote->descdata);
21473 return true;
21474 }
21475 return false;
21476}
21477
00e98fc7
TG
21478static const char *
21479get_ia64_vms_note_type (unsigned e_type)
21480{
21481 static char buff[64];
21482
21483 switch (e_type)
21484 {
21485 case NT_VMS_MHD:
21486 return _("NT_VMS_MHD (module header)");
21487 case NT_VMS_LNM:
21488 return _("NT_VMS_LNM (language name)");
21489 case NT_VMS_SRC:
21490 return _("NT_VMS_SRC (source files)");
21491 case NT_VMS_TITLE:
9cf03b7e 21492 return "NT_VMS_TITLE";
00e98fc7
TG
21493 case NT_VMS_EIDC:
21494 return _("NT_VMS_EIDC (consistency check)");
21495 case NT_VMS_FPMODE:
21496 return _("NT_VMS_FPMODE (FP mode)");
21497 case NT_VMS_LINKTIME:
9cf03b7e 21498 return "NT_VMS_LINKTIME";
00e98fc7
TG
21499 case NT_VMS_IMGNAM:
21500 return _("NT_VMS_IMGNAM (image name)");
21501 case NT_VMS_IMGID:
21502 return _("NT_VMS_IMGID (image id)");
21503 case NT_VMS_LINKID:
21504 return _("NT_VMS_LINKID (link id)");
21505 case NT_VMS_IMGBID:
21506 return _("NT_VMS_IMGBID (build id)");
21507 case NT_VMS_GSTNAM:
21508 return _("NT_VMS_GSTNAM (sym table name)");
21509 case NT_VMS_ORIG_DYN:
9cf03b7e 21510 return "NT_VMS_ORIG_DYN";
00e98fc7 21511 case NT_VMS_PATCHTIME:
9cf03b7e 21512 return "NT_VMS_PATCHTIME";
00e98fc7
TG
21513 default:
21514 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
21515 return buff;
21516 }
21517}
21518
015dc7e1 21519static bool
00e98fc7
TG
21520print_ia64_vms_note (Elf_Internal_Note * pnote)
21521{
26c527e6 21522 unsigned int maxlen = pnote->descsz;
8d18bf79 21523
26c527e6 21524 if (maxlen < 2 || maxlen != pnote->descsz)
8d18bf79
NC
21525 goto desc_size_fail;
21526
00e98fc7
TG
21527 switch (pnote->type)
21528 {
21529 case NT_VMS_MHD:
8d18bf79
NC
21530 if (maxlen <= 36)
21531 goto desc_size_fail;
21532
26c527e6 21533 size_t l = strnlen (pnote->descdata + 34, maxlen - 34);
8d18bf79
NC
21534
21535 printf (_(" Creation date : %.17s\n"), pnote->descdata);
21536 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
21537 if (l + 34 < maxlen)
21538 {
21539 printf (_(" Module name : %s\n"), pnote->descdata + 34);
21540 if (l + 35 < maxlen)
21541 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
21542 else
21543 printf (_(" Module version : <missing>\n"));
21544 }
00e98fc7 21545 else
8d18bf79
NC
21546 {
21547 printf (_(" Module name : <missing>\n"));
21548 printf (_(" Module version : <missing>\n"));
21549 }
00e98fc7 21550 break;
8d18bf79 21551
00e98fc7 21552 case NT_VMS_LNM:
8d18bf79 21553 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
00e98fc7 21554 break;
8d18bf79 21555
00e98fc7 21556 case NT_VMS_FPMODE:
9cf03b7e 21557 printf (_(" Floating Point mode: "));
8d18bf79
NC
21558 if (maxlen < 8)
21559 goto desc_size_fail;
21560 /* FIXME: Generate an error if descsz > 8 ? */
21561
b8281767 21562 printf ("0x%016" PRIx64 "\n",
625d49fc 21563 byte_get ((unsigned char *) pnote->descdata, 8));
00e98fc7 21564 break;
8d18bf79 21565
00e98fc7
TG
21566 case NT_VMS_LINKTIME:
21567 printf (_(" Link time: "));
8d18bf79
NC
21568 if (maxlen < 8)
21569 goto desc_size_fail;
21570 /* FIXME: Generate an error if descsz > 8 ? */
21571
0e3c1eeb 21572 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
00e98fc7
TG
21573 printf ("\n");
21574 break;
8d18bf79 21575
00e98fc7
TG
21576 case NT_VMS_PATCHTIME:
21577 printf (_(" Patch time: "));
8d18bf79
NC
21578 if (maxlen < 8)
21579 goto desc_size_fail;
21580 /* FIXME: Generate an error if descsz > 8 ? */
21581
0e3c1eeb 21582 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
00e98fc7
TG
21583 printf ("\n");
21584 break;
8d18bf79 21585
00e98fc7 21586 case NT_VMS_ORIG_DYN:
8d18bf79
NC
21587 if (maxlen < 34)
21588 goto desc_size_fail;
21589
00e98fc7 21590 printf (_(" Major id: %u, minor id: %u\n"),
0e3c1eeb
AM
21591 (unsigned) byte_get ((unsigned char *) pnote->descdata, 4),
21592 (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
9cf03b7e 21593 printf (_(" Last modified : "));
0e3c1eeb 21594 print_vms_time (byte_get ((unsigned char *) pnote->descdata + 8, 8));
9cf03b7e 21595 printf (_("\n Link flags : "));
b8281767 21596 printf ("0x%016" PRIx64 "\n",
625d49fc 21597 byte_get ((unsigned char *) pnote->descdata + 16, 8));
00e98fc7 21598 printf (_(" Header flags: 0x%08x\n"),
0e3c1eeb 21599 (unsigned) byte_get ((unsigned char *) pnote->descdata + 24, 4));
8d18bf79 21600 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
00e98fc7 21601 break;
8d18bf79 21602
00e98fc7 21603 case NT_VMS_IMGNAM:
8d18bf79 21604 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
00e98fc7 21605 break;
8d18bf79 21606
00e98fc7 21607 case NT_VMS_GSTNAM:
8d18bf79 21608 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
00e98fc7 21609 break;
8d18bf79 21610
00e98fc7 21611 case NT_VMS_IMGID:
8d18bf79 21612 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
00e98fc7 21613 break;
8d18bf79 21614
00e98fc7 21615 case NT_VMS_LINKID:
8d18bf79 21616 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
00e98fc7 21617 break;
8d18bf79 21618
00e98fc7 21619 default:
015dc7e1 21620 return false;
00e98fc7 21621 }
8d18bf79 21622
015dc7e1 21623 return true;
8d18bf79
NC
21624
21625 desc_size_fail:
21626 printf (_(" <corrupt - data size is too small>\n"));
21627 error (_("corrupt IA64 note: data size is too small\n"));
015dc7e1 21628 return false;
00e98fc7
TG
21629}
21630
fd486f32
AM
21631struct build_attr_cache {
21632 Filedata *filedata;
21633 char *strtab;
26c527e6 21634 uint64_t strtablen;
fd486f32 21635 Elf_Internal_Sym *symtab;
26c527e6 21636 uint64_t nsyms;
fd486f32
AM
21637} ba_cache;
21638
6f156d7a
NC
21639/* Find the symbol associated with a build attribute that is attached
21640 to address OFFSET. If PNAME is non-NULL then store the name of
21641 the symbol (if found) in the provided pointer, Returns NULL if a
21642 symbol could not be found. */
c799a79d 21643
6f156d7a 21644static Elf_Internal_Sym *
015dc7e1 21645get_symbol_for_build_attribute (Filedata *filedata,
26c527e6 21646 uint64_t offset,
015dc7e1
AM
21647 bool is_open_attr,
21648 const char **pname)
9ef920e9 21649{
fd486f32
AM
21650 Elf_Internal_Sym *saved_sym = NULL;
21651 Elf_Internal_Sym *sym;
9ef920e9 21652
dda8d76d 21653 if (filedata->section_headers != NULL
fd486f32 21654 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
9ef920e9 21655 {
c799a79d 21656 Elf_Internal_Shdr * symsec;
9ef920e9 21657
fd486f32
AM
21658 free (ba_cache.strtab);
21659 ba_cache.strtab = NULL;
21660 free (ba_cache.symtab);
21661 ba_cache.symtab = NULL;
21662
c799a79d 21663 /* Load the symbol and string sections. */
dda8d76d
NC
21664 for (symsec = filedata->section_headers;
21665 symsec < filedata->section_headers + filedata->file_header.e_shnum;
c799a79d 21666 symsec ++)
9ef920e9 21667 {
28d13567
AM
21668 if (symsec->sh_type == SHT_SYMTAB
21669 && get_symtab (filedata, symsec,
21670 &ba_cache.symtab, &ba_cache.nsyms,
21671 &ba_cache.strtab, &ba_cache.strtablen))
21672 break;
9ef920e9 21673 }
fd486f32 21674 ba_cache.filedata = filedata;
9ef920e9
NC
21675 }
21676
fd486f32 21677 if (ba_cache.symtab == NULL)
6f156d7a 21678 return NULL;
9ef920e9 21679
c799a79d 21680 /* Find a symbol whose value matches offset. */
fd486f32 21681 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
c799a79d
NC
21682 if (sym->st_value == offset)
21683 {
fd486f32 21684 if (sym->st_name >= ba_cache.strtablen)
c799a79d
NC
21685 /* Huh ? This should not happen. */
21686 continue;
9ef920e9 21687
fd486f32 21688 if (ba_cache.strtab[sym->st_name] == 0)
c799a79d 21689 continue;
9ef920e9 21690
9b9b1092 21691 /* The AArch64, ARM and RISC-V architectures define mapping symbols
8fd75781 21692 (eg $d, $x, $t) which we want to ignore. */
fd486f32
AM
21693 if (ba_cache.strtab[sym->st_name] == '$'
21694 && ba_cache.strtab[sym->st_name + 1] != 0
21695 && ba_cache.strtab[sym->st_name + 2] == 0)
8fd75781
NC
21696 continue;
21697
c799a79d
NC
21698 if (is_open_attr)
21699 {
21700 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
21701 and FILE or OBJECT symbols over NOTYPE symbols. We skip
21702 FUNC symbols entirely. */
21703 switch (ELF_ST_TYPE (sym->st_info))
21704 {
c799a79d 21705 case STT_OBJECT:
6f156d7a 21706 case STT_FILE:
c799a79d 21707 saved_sym = sym;
6f156d7a
NC
21708 if (sym->st_size)
21709 {
21710 /* If the symbol has a size associated
21711 with it then we can stop searching. */
fd486f32 21712 sym = ba_cache.symtab + ba_cache.nsyms;
6f156d7a 21713 }
c799a79d 21714 continue;
9ef920e9 21715
c799a79d
NC
21716 case STT_FUNC:
21717 /* Ignore function symbols. */
21718 continue;
21719
21720 default:
21721 break;
21722 }
21723
21724 switch (ELF_ST_BIND (sym->st_info))
9ef920e9 21725 {
c799a79d
NC
21726 case STB_GLOBAL:
21727 if (saved_sym == NULL
21728 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
21729 saved_sym = sym;
21730 break;
c871dade 21731
c799a79d
NC
21732 case STB_LOCAL:
21733 if (saved_sym == NULL)
21734 saved_sym = sym;
21735 break;
21736
21737 default:
9ef920e9
NC
21738 break;
21739 }
21740 }
c799a79d
NC
21741 else
21742 {
21743 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
21744 continue;
21745
21746 saved_sym = sym;
21747 break;
21748 }
21749 }
21750
6f156d7a 21751 if (saved_sym && pname)
fd486f32 21752 * pname = ba_cache.strtab + saved_sym->st_name;
6f156d7a
NC
21753
21754 return saved_sym;
c799a79d
NC
21755}
21756
d20e98ab
NC
21757/* Returns true iff addr1 and addr2 are in the same section. */
21758
015dc7e1 21759static bool
26c527e6 21760same_section (Filedata * filedata, uint64_t addr1, uint64_t addr2)
d20e98ab
NC
21761{
21762 Elf_Internal_Shdr * a1;
21763 Elf_Internal_Shdr * a2;
21764
21765 a1 = find_section_by_address (filedata, addr1);
21766 a2 = find_section_by_address (filedata, addr2);
9abca702 21767
d20e98ab
NC
21768 return a1 == a2 && a1 != NULL;
21769}
21770
015dc7e1 21771static bool
dda8d76d
NC
21772print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
21773 Filedata * filedata)
c799a79d 21774{
26c527e6
AM
21775 static uint64_t global_offset = 0;
21776 static uint64_t global_end = 0;
21777 static uint64_t func_offset = 0;
21778 static uint64_t func_end = 0;
c871dade 21779
015dc7e1
AM
21780 Elf_Internal_Sym *sym;
21781 const char *name;
26c527e6
AM
21782 uint64_t start;
21783 uint64_t end;
015dc7e1 21784 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
6f156d7a
NC
21785
21786 switch (pnote->descsz)
c799a79d 21787 {
6f156d7a
NC
21788 case 0:
21789 /* A zero-length description means that the range of
21790 the previous note of the same type should be used. */
c799a79d 21791 if (is_open_attr)
c871dade 21792 {
6f156d7a 21793 if (global_end > global_offset)
26c527e6
AM
21794 printf (_(" Applies to region from %#" PRIx64
21795 " to %#" PRIx64 "\n"), global_offset, global_end);
6f156d7a 21796 else
26c527e6
AM
21797 printf (_(" Applies to region from %#" PRIx64
21798 "\n"), global_offset);
c799a79d
NC
21799 }
21800 else
21801 {
6f156d7a 21802 if (func_end > func_offset)
26c527e6
AM
21803 printf (_(" Applies to region from %#" PRIx64
21804 " to %#" PRIx64 "\n"), func_offset, func_end);
6f156d7a 21805 else
26c527e6
AM
21806 printf (_(" Applies to region from %#" PRIx64
21807 "\n"), func_offset);
c871dade 21808 }
015dc7e1 21809 return true;
9ef920e9 21810
6f156d7a
NC
21811 case 4:
21812 start = byte_get ((unsigned char *) pnote->descdata, 4);
21813 end = 0;
21814 break;
21815
21816 case 8:
c74147bb
NC
21817 start = byte_get ((unsigned char *) pnote->descdata, 4);
21818 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
6f156d7a
NC
21819 break;
21820
21821 case 16:
21822 start = byte_get ((unsigned char *) pnote->descdata, 8);
21823 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
21824 break;
9abca702 21825
6f156d7a 21826 default:
c799a79d
NC
21827 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
21828 printf (_(" <invalid descsz>"));
015dc7e1 21829 return false;
c799a79d
NC
21830 }
21831
6f156d7a
NC
21832 name = NULL;
21833 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
8fd75781
NC
21834 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
21835 in order to avoid them being confused with the start address of the
21836 first function in the file... */
21837 if (sym == NULL && is_open_attr)
21838 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
21839 & name);
6f156d7a
NC
21840
21841 if (end == 0 && sym != NULL && sym->st_size > 0)
21842 end = start + sym->st_size;
c799a79d
NC
21843
21844 if (is_open_attr)
21845 {
d20e98ab
NC
21846 /* FIXME: Need to properly allow for section alignment.
21847 16 is just the alignment used on x86_64. */
21848 if (global_end > 0
21849 && start > BFD_ALIGN (global_end, 16)
21850 /* Build notes are not guaranteed to be organised in order of
21851 increasing address, but we should find the all of the notes
21852 for one section in the same place. */
21853 && same_section (filedata, start, global_end))
26c527e6
AM
21854 warn (_("Gap in build notes detected from %#" PRIx64
21855 " to %#" PRIx64 "\n"),
6f156d7a
NC
21856 global_end + 1, start - 1);
21857
26c527e6 21858 printf (_(" Applies to region from %#" PRIx64), start);
6f156d7a
NC
21859 global_offset = start;
21860
21861 if (end)
21862 {
26c527e6 21863 printf (_(" to %#" PRIx64), end);
6f156d7a
NC
21864 global_end = end;
21865 }
c799a79d
NC
21866 }
21867 else
21868 {
26c527e6 21869 printf (_(" Applies to region from %#" PRIx64), start);
6f156d7a
NC
21870 func_offset = start;
21871
21872 if (end)
21873 {
26c527e6 21874 printf (_(" to %#" PRIx64), end);
6f156d7a
NC
21875 func_end = end;
21876 }
c799a79d
NC
21877 }
21878
6f156d7a
NC
21879 if (sym && name)
21880 printf (_(" (%s)"), name);
21881
21882 printf ("\n");
015dc7e1 21883 return true;
9ef920e9
NC
21884}
21885
015dc7e1 21886static bool
9ef920e9
NC
21887print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
21888{
1d15e434
NC
21889 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
21890 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
21891 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
9ef920e9
NC
21892 char name_type;
21893 char name_attribute;
1d15e434 21894 const char * expected_types;
9ef920e9
NC
21895 const char * name = pnote->namedata;
21896 const char * text;
88305e1b 21897 signed int left;
9ef920e9
NC
21898
21899 if (name == NULL || pnote->namesz < 2)
21900 {
21901 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
b6ac461a 21902 print_symbol_name (-20, _(" <corrupt name>"));
015dc7e1 21903 return false;
9ef920e9
NC
21904 }
21905
6f156d7a
NC
21906 if (do_wide)
21907 left = 28;
21908 else
21909 left = 20;
88305e1b
NC
21910
21911 /* Version 2 of the spec adds a "GA" prefix to the name field. */
21912 if (name[0] == 'G' && name[1] == 'A')
21913 {
6f156d7a
NC
21914 if (pnote->namesz < 4)
21915 {
21916 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
b6ac461a 21917 print_symbol_name (-20, _(" <corrupt name>"));
015dc7e1 21918 return false;
6f156d7a
NC
21919 }
21920
88305e1b
NC
21921 printf ("GA");
21922 name += 2;
21923 left -= 2;
21924 }
21925
9ef920e9
NC
21926 switch ((name_type = * name))
21927 {
21928 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21929 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21930 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21931 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21932 printf ("%c", * name);
88305e1b 21933 left --;
9ef920e9
NC
21934 break;
21935 default:
21936 error (_("unrecognised attribute type in name field: %d\n"), name_type);
b6ac461a 21937 print_symbol_name (-20, _("<unknown name type>"));
015dc7e1 21938 return false;
9ef920e9
NC
21939 }
21940
9ef920e9
NC
21941 ++ name;
21942 text = NULL;
21943
21944 switch ((name_attribute = * name))
21945 {
21946 case GNU_BUILD_ATTRIBUTE_VERSION:
21947 text = _("<version>");
1d15e434 21948 expected_types = string_expected;
9ef920e9
NC
21949 ++ name;
21950 break;
21951 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21952 text = _("<stack prot>");
75d7d298 21953 expected_types = "!+*";
9ef920e9
NC
21954 ++ name;
21955 break;
21956 case GNU_BUILD_ATTRIBUTE_RELRO:
21957 text = _("<relro>");
1d15e434 21958 expected_types = bool_expected;
9ef920e9
NC
21959 ++ name;
21960 break;
21961 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
21962 text = _("<stack size>");
1d15e434 21963 expected_types = number_expected;
9ef920e9
NC
21964 ++ name;
21965 break;
21966 case GNU_BUILD_ATTRIBUTE_TOOL:
21967 text = _("<tool>");
1d15e434 21968 expected_types = string_expected;
9ef920e9
NC
21969 ++ name;
21970 break;
21971 case GNU_BUILD_ATTRIBUTE_ABI:
21972 text = _("<ABI>");
21973 expected_types = "$*";
21974 ++ name;
21975 break;
21976 case GNU_BUILD_ATTRIBUTE_PIC:
21977 text = _("<PIC>");
1d15e434 21978 expected_types = number_expected;
9ef920e9
NC
21979 ++ name;
21980 break;
a8be5506
NC
21981 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
21982 text = _("<short enum>");
1d15e434 21983 expected_types = bool_expected;
a8be5506
NC
21984 ++ name;
21985 break;
9ef920e9
NC
21986 default:
21987 if (ISPRINT (* name))
21988 {
21989 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
21990
21991 if (len > left && ! do_wide)
21992 len = left;
75d7d298 21993 printf ("%.*s:", len, name);
9ef920e9 21994 left -= len;
0dd6ae21 21995 name += len;
9ef920e9
NC
21996 }
21997 else
21998 {
3e6b6445 21999 static char tmpbuf [128];
88305e1b 22000
3e6b6445
NC
22001 error (_("unrecognised byte in name field: %d\n"), * name);
22002 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
22003 text = tmpbuf;
22004 name ++;
9ef920e9
NC
22005 }
22006 expected_types = "*$!+";
22007 break;
22008 }
22009
22010 if (text)
88305e1b 22011 left -= printf ("%s", text);
9ef920e9
NC
22012
22013 if (strchr (expected_types, name_type) == NULL)
75d7d298 22014 warn (_("attribute does not have an expected type (%c)\n"), name_type);
9ef920e9 22015
26c527e6 22016 if ((size_t) (name - pnote->namedata) > pnote->namesz)
9ef920e9 22017 {
26c527e6
AM
22018 error (_("corrupt name field: namesz: %lu but parsing gets to %td\n"),
22019 pnote->namesz,
22020 name - pnote->namedata);
015dc7e1 22021 return false;
9ef920e9
NC
22022 }
22023
22024 if (left < 1 && ! do_wide)
015dc7e1 22025 return true;
9ef920e9
NC
22026
22027 switch (name_type)
22028 {
22029 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
22030 {
26c527e6
AM
22031 unsigned int bytes;
22032 uint64_t val = 0;
22033 unsigned int shift = 0;
22034 char *decoded = NULL;
ddef72cd 22035
b06b2c92
NC
22036 bytes = pnote->namesz - (name - pnote->namedata);
22037 if (bytes > 0)
22038 /* The -1 is because the name field is always 0 terminated, and we
22039 want to be able to ensure that the shift in the while loop below
22040 will not overflow. */
22041 -- bytes;
22042
ddef72cd
NC
22043 if (bytes > sizeof (val))
22044 {
3e6b6445
NC
22045 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
22046 bytes);
22047 bytes = sizeof (val);
ddef72cd 22048 }
3e6b6445
NC
22049 /* We do not bother to warn if bytes == 0 as this can
22050 happen with some early versions of the gcc plugin. */
9ef920e9
NC
22051
22052 while (bytes --)
22053 {
26c527e6 22054 uint64_t byte = *name++ & 0xff;
79a964dc
NC
22055
22056 val |= byte << shift;
9ef920e9
NC
22057 shift += 8;
22058 }
22059
75d7d298 22060 switch (name_attribute)
9ef920e9 22061 {
75d7d298 22062 case GNU_BUILD_ATTRIBUTE_PIC:
9ef920e9
NC
22063 switch (val)
22064 {
75d7d298
NC
22065 case 0: decoded = "static"; break;
22066 case 1: decoded = "pic"; break;
22067 case 2: decoded = "PIC"; break;
22068 case 3: decoded = "pie"; break;
22069 case 4: decoded = "PIE"; break;
22070 default: break;
9ef920e9 22071 }
75d7d298
NC
22072 break;
22073 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
22074 switch (val)
9ef920e9 22075 {
75d7d298
NC
22076 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
22077 case 0: decoded = "off"; break;
22078 case 1: decoded = "on"; break;
22079 case 2: decoded = "all"; break;
22080 case 3: decoded = "strong"; break;
22081 case 4: decoded = "explicit"; break;
22082 default: break;
9ef920e9 22083 }
75d7d298
NC
22084 break;
22085 default:
22086 break;
9ef920e9
NC
22087 }
22088
75d7d298 22089 if (decoded != NULL)
3e6b6445 22090 {
b6ac461a 22091 print_symbol_name (-left, decoded);
3e6b6445
NC
22092 left = 0;
22093 }
22094 else if (val == 0)
22095 {
22096 printf ("0x0");
22097 left -= 3;
22098 }
9ef920e9 22099 else
75d7d298
NC
22100 {
22101 if (do_wide)
26c527e6 22102 left -= printf ("0x%" PRIx64, val);
75d7d298 22103 else
26c527e6 22104 left -= printf ("0x%-.*" PRIx64, left, val);
75d7d298 22105 }
9ef920e9
NC
22106 }
22107 break;
22108 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
b6ac461a 22109 left -= print_symbol_name (- left, name);
9ef920e9
NC
22110 break;
22111 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
b6ac461a 22112 left -= print_symbol_name (- left, "true");
9ef920e9
NC
22113 break;
22114 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
b6ac461a 22115 left -= print_symbol_name (- left, "false");
9ef920e9
NC
22116 break;
22117 }
22118
22119 if (do_wide && left > 0)
22120 printf ("%-*s", left, " ");
9abca702 22121
015dc7e1 22122 return true;
9ef920e9
NC
22123}
22124
2952f10c
SM
22125/* Print the contents of PNOTE as hex. */
22126
22127static void
22128print_note_contents_hex (Elf_Internal_Note *pnote)
22129{
22130 if (pnote->descsz)
22131 {
26c527e6 22132 size_t i;
2952f10c
SM
22133
22134 printf (_(" description data: "));
22135 for (i = 0; i < pnote->descsz; i++)
22136 printf ("%02x ", pnote->descdata[i] & 0xff);
22137 if (!do_wide)
22138 printf ("\n");
22139 }
22140
22141 if (do_wide)
22142 printf ("\n");
22143}
22144
22145#if defined HAVE_MSGPACK
22146
22147static void
22148print_indents (int n)
22149{
22150 printf (" ");
22151
22152 for (int i = 0; i < n; i++)
22153 printf (" ");
22154}
22155
22156/* Print OBJ in human-readable form. */
22157
22158static void
22159dump_msgpack_obj (const msgpack_object *obj, int indent)
22160{
22161 switch (obj->type)
22162 {
22163 case MSGPACK_OBJECT_NIL:
22164 printf ("(nil)");
22165 break;
22166
22167 case MSGPACK_OBJECT_BOOLEAN:
22168 printf ("%s", obj->via.boolean ? "true" : "false");
22169 break;
22170
22171 case MSGPACK_OBJECT_POSITIVE_INTEGER:
22172 printf ("%" PRIu64, obj->via.u64);
22173 break;
22174
22175 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
22176 printf ("%" PRIi64, obj->via.i64);
22177 break;
22178
22179 case MSGPACK_OBJECT_FLOAT32:
22180 case MSGPACK_OBJECT_FLOAT64:
22181 printf ("%f", obj->via.f64);
22182 break;
22183
22184 case MSGPACK_OBJECT_STR:
22185 printf ("\"%.*s\"", obj->via.str.size, obj->via.str.ptr);
22186 break;
22187
22188 case MSGPACK_OBJECT_ARRAY:
22189 {
22190 const msgpack_object_array *array = &obj->via.array;
22191
22192 printf ("[\n");
22193 ++indent;
22194
22195 for (uint32_t i = 0; i < array->size; ++i)
22196 {
22197 const msgpack_object *item = &array->ptr[i];
22198
22199 print_indents (indent);
22200 dump_msgpack_obj (item, indent);
22201 printf (",\n");
22202 }
22203
22204 --indent;
22205 print_indents (indent);
22206 printf ("]");
22207 break;
22208 }
22209 break;
22210
22211 case MSGPACK_OBJECT_MAP:
22212 {
22213 const msgpack_object_map *map = &obj->via.map;
22214
22215 printf ("{\n");
22216 ++indent;
22217
22218 for (uint32_t i = 0; i < map->size; ++i)
22219 {
22220 const msgpack_object_kv *kv = &map->ptr[i];
22221 const msgpack_object *key = &kv->key;
22222 const msgpack_object *val = &kv->val;
22223
22224 print_indents (indent);
22225 dump_msgpack_obj (key, indent);
22226 printf (": ");
22227 dump_msgpack_obj (val, indent);
22228
22229 printf (",\n");
22230 }
22231
22232 --indent;
22233 print_indents (indent);
22234 printf ("}");
22235
22236 break;
22237 }
22238
22239 case MSGPACK_OBJECT_BIN:
22240 printf ("(bin)");
22241 break;
22242
22243 case MSGPACK_OBJECT_EXT:
22244 printf ("(ext)");
22245 break;
22246 }
22247}
22248
22249static void
22250dump_msgpack (const msgpack_unpacked *msg)
22251{
22252 print_indents (0);
22253 dump_msgpack_obj (&msg->data, 0);
22254 printf ("\n");
22255}
22256
22257#endif /* defined HAVE_MSGPACK */
22258
22259static bool
22260print_amdgpu_note (Elf_Internal_Note *pnote)
22261{
22262#if defined HAVE_MSGPACK
22263 /* If msgpack is available, decode and dump the note's content. */
22264 bool ret;
22265 msgpack_unpacked msg;
22266 msgpack_unpack_return msgpack_ret;
22267
22268 assert (pnote->type == NT_AMDGPU_METADATA);
22269
22270 msgpack_unpacked_init (&msg);
22271 msgpack_ret = msgpack_unpack_next (&msg, pnote->descdata, pnote->descsz,
22272 NULL);
22273
22274 switch (msgpack_ret)
22275 {
22276 case MSGPACK_UNPACK_SUCCESS:
22277 dump_msgpack (&msg);
22278 ret = true;
22279 break;
22280
22281 default:
22282 error (_("failed to unpack msgpack contents in NT_AMDGPU_METADATA note"));
22283 ret = false;
22284 break;
22285 }
22286
22287 msgpack_unpacked_destroy (&msg);
22288 return ret;
22289#else
22290 /* msgpack is not available, dump contents as hex. */
22291 print_note_contents_hex (pnote);
22292 return true;
22293#endif
22294}
22295
e263a66b
CC
22296static bool
22297print_qnx_note (Elf_Internal_Note *pnote)
22298{
22299 switch (pnote->type)
22300 {
22301 case QNT_STACK:
22302 if (pnote->descsz != 12)
22303 goto desc_size_fail;
22304
22305 printf (_(" Stack Size: 0x%" PRIx32 "\n"),
22306 (unsigned) byte_get ((unsigned char *) pnote->descdata, 4));
22307 printf (_(" Stack allocated: %" PRIx32 "\n"),
22308 (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
22309 printf (_(" Executable: %s\n"),
22310 ((unsigned) byte_get ((unsigned char *) pnote->descdata + 8, 1)) ? "no": "yes");
22311 break;
22312
22313 default:
22314 print_note_contents_hex(pnote);
22315 }
22316 return true;
22317
22318desc_size_fail:
22319 printf (_(" <corrupt - data size is too small>\n"));
22320 error (_("corrupt QNX note: data size is too small\n"));
22321 return false;
22322}
22323
22324
6d118b09
NC
22325/* Note that by the ELF standard, the name field is already null byte
22326 terminated, and namesz includes the terminating null byte.
22327 I.E. the value of namesz for the name "FSF" is 4.
22328
e3c8793a 22329 If the value of namesz is zero, there is no name present. */
9ef920e9 22330
015dc7e1 22331static bool
9ef920e9 22332process_note (Elf_Internal_Note * pnote,
dda8d76d 22333 Filedata * filedata)
779fe533 22334{
2cf0635d
NC
22335 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
22336 const char * nt;
9437c45b
JT
22337
22338 if (pnote->namesz == 0)
1ec5cd37
NC
22339 /* If there is no note name, then use the default set of
22340 note type strings. */
dda8d76d 22341 nt = get_note_type (filedata, pnote->type);
1ec5cd37 22342
24d127aa 22343 else if (startswith (pnote->namedata, "GNU"))
1118d252
RM
22344 /* GNU-specific object file notes. */
22345 nt = get_gnu_elf_note_type (pnote->type);
f4ddf30f 22346
28cdbb18
SM
22347 else if (startswith (pnote->namedata, "AMDGPU"))
22348 /* AMDGPU-specific object file notes. */
22349 nt = get_amdgpu_elf_note_type (pnote->type);
22350
24d127aa 22351 else if (startswith (pnote->namedata, "FreeBSD"))
f4ddf30f 22352 /* FreeBSD-specific core file notes. */
dda8d76d 22353 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
1118d252 22354
24d127aa 22355 else if (startswith (pnote->namedata, "NetBSD-CORE"))
1ec5cd37 22356 /* NetBSD-specific core file notes. */
dda8d76d 22357 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
1ec5cd37 22358
24d127aa 22359 else if (startswith (pnote->namedata, "NetBSD"))
c6056a74
SF
22360 /* NetBSD-specific core file notes. */
22361 return process_netbsd_elf_note (pnote);
22362
24d127aa 22363 else if (startswith (pnote->namedata, "PaX"))
9abca702
CZ
22364 /* NetBSD-specific core file notes. */
22365 return process_netbsd_elf_note (pnote);
22366
98ca73af
FC
22367 else if (startswith (pnote->namedata, "OpenBSD"))
22368 /* OpenBSD-specific core file notes. */
22369 nt = get_openbsd_elfcore_note_type (filedata, pnote->type);
22370
e263a66b
CC
22371 else if (startswith (pnote->namedata, "QNX"))
22372 /* QNX-specific core file notes. */
22373 nt = get_qnx_elfcore_note_type (filedata, pnote->type);
22374
e9b095a5 22375 else if (startswith (pnote->namedata, "SPU/"))
b15fa79e
AM
22376 {
22377 /* SPU-specific core file notes. */
22378 nt = pnote->namedata + 4;
22379 name = "SPU";
22380 }
22381
24d127aa 22382 else if (startswith (pnote->namedata, "IPF/VMS"))
00e98fc7
TG
22383 /* VMS/ia64-specific file notes. */
22384 nt = get_ia64_vms_note_type (pnote->type);
22385
24d127aa 22386 else if (startswith (pnote->namedata, "stapsdt"))
70616151
TT
22387 nt = get_stapsdt_note_type (pnote->type);
22388
9437c45b 22389 else
1ec5cd37
NC
22390 /* Don't recognize this note name; just use the default set of
22391 note type strings. */
dda8d76d 22392 nt = get_note_type (filedata, pnote->type);
9437c45b 22393
1449284b 22394 printf (" ");
9ef920e9 22395
24d127aa 22396 if (((startswith (pnote->namedata, "GA")
483767a3
AM
22397 && strchr ("*$!+", pnote->namedata[2]) != NULL)
22398 || strchr ("*$!+", pnote->namedata[0]) != NULL)
22399 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
22400 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
9ef920e9
NC
22401 print_gnu_build_attribute_name (pnote);
22402 else
b6ac461a 22403 print_symbol_name (-20, name);
9ef920e9
NC
22404
22405 if (do_wide)
22406 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
22407 else
22408 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
00e98fc7 22409
24d127aa 22410 if (startswith (pnote->namedata, "IPF/VMS"))
00e98fc7 22411 return print_ia64_vms_note (pnote);
24d127aa 22412 else if (startswith (pnote->namedata, "GNU"))
dda8d76d 22413 return print_gnu_note (filedata, pnote);
24d127aa 22414 else if (startswith (pnote->namedata, "stapsdt"))
c6a9fc58 22415 return print_stapsdt_note (pnote);
24d127aa 22416 else if (startswith (pnote->namedata, "CORE"))
9ece1fa9 22417 return print_core_note (pnote);
e5382207
LB
22418 else if (startswith (pnote->namedata, "FDO"))
22419 return print_fdo_note (pnote);
24d127aa 22420 else if (((startswith (pnote->namedata, "GA")
483767a3
AM
22421 && strchr ("*$!+", pnote->namedata[2]) != NULL)
22422 || strchr ("*$!+", pnote->namedata[0]) != NULL)
22423 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
22424 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
dda8d76d 22425 return print_gnu_build_attribute_description (pnote, filedata);
2952f10c
SM
22426 else if (startswith (pnote->namedata, "AMDGPU")
22427 && pnote->type == NT_AMDGPU_METADATA)
22428 return print_amdgpu_note (pnote);
e263a66b
CC
22429 else if (startswith (pnote->namedata, "QNX"))
22430 return print_qnx_note (pnote);
779fe533 22431
2952f10c 22432 print_note_contents_hex (pnote);
015dc7e1 22433 return true;
1449284b 22434}
6d118b09 22435
015dc7e1 22436static bool
dda8d76d
NC
22437process_notes_at (Filedata * filedata,
22438 Elf_Internal_Shdr * section,
625d49fc
AM
22439 uint64_t offset,
22440 uint64_t length,
22441 uint64_t align)
779fe533 22442{
015dc7e1
AM
22443 Elf_External_Note *pnotes;
22444 Elf_External_Note *external;
22445 char *end;
22446 bool res = true;
103f02d3 22447
779fe533 22448 if (length <= 0)
015dc7e1 22449 return false;
103f02d3 22450
1449284b
NC
22451 if (section)
22452 {
dda8d76d 22453 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
1449284b 22454 if (pnotes)
32ec8896 22455 {
dda8d76d 22456 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
f761cb13
AM
22457 {
22458 free (pnotes);
015dc7e1 22459 return false;
f761cb13 22460 }
32ec8896 22461 }
1449284b
NC
22462 }
22463 else
82ed9683 22464 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
1449284b 22465 _("notes"));
4dff97b2 22466
dd24e3da 22467 if (pnotes == NULL)
015dc7e1 22468 return false;
779fe533 22469
103f02d3 22470 external = pnotes;
103f02d3 22471
ca0e11aa
NC
22472 if (filedata->is_separate)
22473 printf (_("In linked file '%s': "), filedata->file_name);
22474 else
22475 printf ("\n");
1449284b 22476 if (section)
ca0e11aa 22477 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
1449284b 22478 else
26c527e6
AM
22479 printf (_("Displaying notes found at file offset 0x%08" PRIx64
22480 " with length 0x%08" PRIx64 ":\n"),
22481 offset, length);
1449284b 22482
82ed9683
L
22483 /* NB: Some note sections may have alignment value of 0 or 1. gABI
22484 specifies that notes should be aligned to 4 bytes in 32-bit
22485 objects and to 8 bytes in 64-bit objects. As a Linux extension,
22486 we also support 4 byte alignment in 64-bit objects. If section
22487 alignment is less than 4, we treate alignment as 4 bytes. */
22488 if (align < 4)
22489 align = 4;
22490 else if (align != 4 && align != 8)
22491 {
26c527e6
AM
22492 warn (_("Corrupt note: alignment %" PRId64 ", expecting 4 or 8\n"),
22493 align);
a788aedd 22494 free (pnotes);
015dc7e1 22495 return false;
82ed9683
L
22496 }
22497
dbe15e4e 22498 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
103f02d3 22499
c8071705
NC
22500 end = (char *) pnotes + length;
22501 while ((char *) external < end)
779fe533 22502 {
b34976b6 22503 Elf_Internal_Note inote;
15b42fb0 22504 size_t min_notesz;
4dff97b2 22505 char * next;
2cf0635d 22506 char * temp = NULL;
c8071705 22507 size_t data_remaining = end - (char *) external;
6d118b09 22508
dda8d76d 22509 if (!is_ia64_vms (filedata))
15b42fb0 22510 {
9dd3a467
NC
22511 /* PR binutils/15191
22512 Make sure that there is enough data to read. */
15b42fb0
AM
22513 min_notesz = offsetof (Elf_External_Note, name);
22514 if (data_remaining < min_notesz)
9dd3a467 22515 {
26c527e6 22516 warn (ngettext ("Corrupt note: only %zd byte remains, "
d3a49aa8 22517 "not enough for a full note\n",
26c527e6 22518 "Corrupt note: only %zd bytes remain, "
d3a49aa8
AM
22519 "not enough for a full note\n",
22520 data_remaining),
26c527e6 22521 data_remaining);
9dd3a467
NC
22522 break;
22523 }
5396a86e
AM
22524 data_remaining -= min_notesz;
22525
15b42fb0
AM
22526 inote.type = BYTE_GET (external->type);
22527 inote.namesz = BYTE_GET (external->namesz);
22528 inote.namedata = external->name;
22529 inote.descsz = BYTE_GET (external->descsz);
276da9b3 22530 inote.descdata = ((char *) external
4dff97b2 22531 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
15b42fb0 22532 inote.descpos = offset + (inote.descdata - (char *) pnotes);
276da9b3 22533 next = ((char *) external
4dff97b2 22534 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
15b42fb0 22535 }
00e98fc7 22536 else
15b42fb0
AM
22537 {
22538 Elf64_External_VMS_Note *vms_external;
00e98fc7 22539
9dd3a467
NC
22540 /* PR binutils/15191
22541 Make sure that there is enough data to read. */
15b42fb0
AM
22542 min_notesz = offsetof (Elf64_External_VMS_Note, name);
22543 if (data_remaining < min_notesz)
9dd3a467 22544 {
26c527e6 22545 warn (ngettext ("Corrupt note: only %zd byte remains, "
d3a49aa8 22546 "not enough for a full note\n",
26c527e6 22547 "Corrupt note: only %zd bytes remain, "
d3a49aa8
AM
22548 "not enough for a full note\n",
22549 data_remaining),
26c527e6 22550 data_remaining);
9dd3a467
NC
22551 break;
22552 }
5396a86e 22553 data_remaining -= min_notesz;
3e55a963 22554
15b42fb0
AM
22555 vms_external = (Elf64_External_VMS_Note *) external;
22556 inote.type = BYTE_GET (vms_external->type);
22557 inote.namesz = BYTE_GET (vms_external->namesz);
22558 inote.namedata = vms_external->name;
22559 inote.descsz = BYTE_GET (vms_external->descsz);
22560 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
22561 inote.descpos = offset + (inote.descdata - (char *) pnotes);
22562 next = inote.descdata + align_power (inote.descsz, 3);
22563 }
22564
5396a86e
AM
22565 /* PR 17531: file: 3443835e. */
22566 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
22567 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
22568 || (size_t) (inote.descdata - inote.namedata) > data_remaining
22569 || (size_t) (next - inote.descdata) < inote.descsz
22570 || ((size_t) (next - inote.descdata)
22571 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
3e55a963 22572 {
26c527e6
AM
22573 warn (_("note with invalid namesz and/or descsz found at offset %#tx\n"),
22574 (char *) external - (char *) pnotes);
22575 warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx, alignment: %u\n"),
4dff97b2 22576 inote.type, inote.namesz, inote.descsz, (int) align);
3e55a963
NC
22577 break;
22578 }
22579
15b42fb0 22580 external = (Elf_External_Note *) next;
dd24e3da 22581
6d118b09
NC
22582 /* Verify that name is null terminated. It appears that at least
22583 one version of Linux (RedHat 6.0) generates corefiles that don't
22584 comply with the ELF spec by failing to include the null byte in
22585 namesz. */
18344509 22586 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
6d118b09 22587 {
5396a86e 22588 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
6d118b09 22589 {
5396a86e
AM
22590 temp = (char *) malloc (inote.namesz + 1);
22591 if (temp == NULL)
22592 {
22593 error (_("Out of memory allocating space for inote name\n"));
015dc7e1 22594 res = false;
5396a86e
AM
22595 break;
22596 }
76da6bbe 22597
5396a86e
AM
22598 memcpy (temp, inote.namedata, inote.namesz);
22599 inote.namedata = temp;
22600 }
22601 inote.namedata[inote.namesz] = 0;
6d118b09
NC
22602 }
22603
dda8d76d 22604 if (! process_note (& inote, filedata))
015dc7e1 22605 res = false;
103f02d3 22606
9db70fc3
AM
22607 free (temp);
22608 temp = NULL;
779fe533
NC
22609 }
22610
22611 free (pnotes);
103f02d3 22612
779fe533
NC
22613 return res;
22614}
22615
015dc7e1 22616static bool
dda8d76d 22617process_corefile_note_segments (Filedata * filedata)
779fe533 22618{
015dc7e1 22619 Elf_Internal_Phdr *segment;
b34976b6 22620 unsigned int i;
015dc7e1 22621 bool res = true;
103f02d3 22622
dda8d76d 22623 if (! get_program_headers (filedata))
015dc7e1 22624 return true;
103f02d3 22625
dda8d76d
NC
22626 for (i = 0, segment = filedata->program_headers;
22627 i < filedata->file_header.e_phnum;
b34976b6 22628 i++, segment++)
779fe533
NC
22629 {
22630 if (segment->p_type == PT_NOTE)
625d49fc
AM
22631 if (! process_notes_at (filedata, NULL, segment->p_offset,
22632 segment->p_filesz, segment->p_align))
015dc7e1 22633 res = false;
779fe533 22634 }
103f02d3 22635
779fe533
NC
22636 return res;
22637}
22638
015dc7e1 22639static bool
625d49fc 22640process_v850_notes (Filedata * filedata, uint64_t offset, uint64_t length)
685080f2
NC
22641{
22642 Elf_External_Note * pnotes;
22643 Elf_External_Note * external;
c8071705 22644 char * end;
015dc7e1 22645 bool res = true;
685080f2
NC
22646
22647 if (length <= 0)
015dc7e1 22648 return false;
685080f2 22649
dda8d76d 22650 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
685080f2
NC
22651 _("v850 notes"));
22652 if (pnotes == NULL)
015dc7e1 22653 return false;
685080f2
NC
22654
22655 external = pnotes;
c8071705 22656 end = (char*) pnotes + length;
685080f2 22657
26c527e6
AM
22658 printf (_("\nDisplaying contents of Renesas V850 notes section at offset"
22659 " %#" PRIx64 " with length %#" PRIx64 ":\n"),
22660 offset, length);
685080f2 22661
c8071705 22662 while ((char *) external + sizeof (Elf_External_Note) < end)
685080f2
NC
22663 {
22664 Elf_External_Note * next;
22665 Elf_Internal_Note inote;
22666
22667 inote.type = BYTE_GET (external->type);
22668 inote.namesz = BYTE_GET (external->namesz);
22669 inote.namedata = external->name;
22670 inote.descsz = BYTE_GET (external->descsz);
22671 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
22672 inote.descpos = offset + (inote.descdata - (char *) pnotes);
22673
c8071705
NC
22674 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
22675 {
22676 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
22677 inote.descdata = inote.namedata;
22678 inote.namesz = 0;
22679 }
22680
685080f2
NC
22681 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
22682
c8071705 22683 if ( ((char *) next > end)
685080f2
NC
22684 || ((char *) next < (char *) pnotes))
22685 {
26c527e6
AM
22686 warn (_("corrupt descsz found in note at offset %#tx\n"),
22687 (char *) external - (char *) pnotes);
22688 warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx\n"),
685080f2
NC
22689 inote.type, inote.namesz, inote.descsz);
22690 break;
22691 }
22692
22693 external = next;
22694
22695 /* Prevent out-of-bounds indexing. */
c8071705 22696 if ( inote.namedata + inote.namesz > end
685080f2
NC
22697 || inote.namedata + inote.namesz < inote.namedata)
22698 {
26c527e6
AM
22699 warn (_("corrupt namesz found in note at offset %#zx\n"),
22700 (char *) external - (char *) pnotes);
22701 warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx\n"),
685080f2
NC
22702 inote.type, inote.namesz, inote.descsz);
22703 break;
22704 }
22705
22706 printf (" %s: ", get_v850_elf_note_type (inote.type));
22707
22708 if (! print_v850_note (& inote))
22709 {
015dc7e1 22710 res = false;
26c527e6 22711 printf ("<corrupt sizes: namesz: %#lx, descsz: %#lx>\n",
685080f2
NC
22712 inote.namesz, inote.descsz);
22713 }
22714 }
22715
22716 free (pnotes);
22717
22718 return res;
22719}
22720
015dc7e1 22721static bool
dda8d76d 22722process_note_sections (Filedata * filedata)
1ec5cd37 22723{
015dc7e1 22724 Elf_Internal_Shdr *section;
26c527e6 22725 size_t i;
32ec8896 22726 unsigned int n = 0;
015dc7e1 22727 bool res = true;
1ec5cd37 22728
dda8d76d
NC
22729 for (i = 0, section = filedata->section_headers;
22730 i < filedata->file_header.e_shnum && section != NULL;
1ec5cd37 22731 i++, section++)
685080f2
NC
22732 {
22733 if (section->sh_type == SHT_NOTE)
22734 {
625d49fc
AM
22735 if (! process_notes_at (filedata, section, section->sh_offset,
22736 section->sh_size, section->sh_addralign))
015dc7e1 22737 res = false;
685080f2
NC
22738 n++;
22739 }
22740
dda8d76d
NC
22741 if (( filedata->file_header.e_machine == EM_V800
22742 || filedata->file_header.e_machine == EM_V850
22743 || filedata->file_header.e_machine == EM_CYGNUS_V850)
685080f2
NC
22744 && section->sh_type == SHT_RENESAS_INFO)
22745 {
625d49fc
AM
22746 if (! process_v850_notes (filedata, section->sh_offset,
22747 section->sh_size))
015dc7e1 22748 res = false;
685080f2
NC
22749 n++;
22750 }
22751 }
df565f32
NC
22752
22753 if (n == 0)
22754 /* Try processing NOTE segments instead. */
dda8d76d 22755 return process_corefile_note_segments (filedata);
1ec5cd37
NC
22756
22757 return res;
22758}
22759
015dc7e1 22760static bool
dda8d76d 22761process_notes (Filedata * filedata)
779fe533
NC
22762{
22763 /* If we have not been asked to display the notes then do nothing. */
22764 if (! do_notes)
015dc7e1 22765 return true;
103f02d3 22766
dda8d76d
NC
22767 if (filedata->file_header.e_type != ET_CORE)
22768 return process_note_sections (filedata);
103f02d3 22769
779fe533 22770 /* No program headers means no NOTE segment. */
dda8d76d
NC
22771 if (filedata->file_header.e_phnum > 0)
22772 return process_corefile_note_segments (filedata);
779fe533 22773
ca0e11aa
NC
22774 if (filedata->is_separate)
22775 printf (_("No notes found in linked file '%s'.\n"),
22776 filedata->file_name);
22777 else
22778 printf (_("No notes found file.\n"));
22779
015dc7e1 22780 return true;
779fe533
NC
22781}
22782
60abdbed
NC
22783static unsigned char *
22784display_public_gnu_attributes (unsigned char * start,
22785 const unsigned char * const end)
22786{
22787 printf (_(" Unknown GNU attribute: %s\n"), start);
22788
22789 start += strnlen ((char *) start, end - start);
22790 display_raw_attribute (start, end);
22791
22792 return (unsigned char *) end;
22793}
22794
22795static unsigned char *
22796display_generic_attribute (unsigned char * start,
22797 unsigned int tag,
22798 const unsigned char * const end)
22799{
22800 if (tag == 0)
22801 return (unsigned char *) end;
22802
22803 return display_tag_value (tag, start, end);
22804}
22805
015dc7e1 22806static bool
dda8d76d 22807process_arch_specific (Filedata * filedata)
252b5132 22808{
a952a375 22809 if (! do_arch)
015dc7e1 22810 return true;
a952a375 22811
dda8d76d 22812 switch (filedata->file_header.e_machine)
252b5132 22813 {
53a346d8
CZ
22814 case EM_ARC:
22815 case EM_ARC_COMPACT:
22816 case EM_ARC_COMPACT2:
b5c37946
SJ
22817 case EM_ARC_COMPACT3:
22818 case EM_ARC_COMPACT3_64:
dda8d76d 22819 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
53a346d8
CZ
22820 display_arc_attribute,
22821 display_generic_attribute);
11c1ff18 22822 case EM_ARM:
dda8d76d 22823 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
60abdbed
NC
22824 display_arm_attribute,
22825 display_generic_attribute);
22826
252b5132 22827 case EM_MIPS:
4fe85591 22828 case EM_MIPS_RS3_LE:
dda8d76d 22829 return process_mips_specific (filedata);
60abdbed
NC
22830
22831 case EM_MSP430:
dda8d76d 22832 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
b0191216 22833 display_msp430_attribute,
c0ea7c52 22834 display_msp430_gnu_attribute);
60abdbed 22835
2dc8dd17
JW
22836 case EM_RISCV:
22837 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
22838 display_riscv_attribute,
22839 display_generic_attribute);
22840
35c08157 22841 case EM_NDS32:
dda8d76d 22842 return process_nds32_specific (filedata);
60abdbed 22843
85f7484a
PB
22844 case EM_68K:
22845 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22846 display_m68k_gnu_attribute);
22847
34c8bcba 22848 case EM_PPC:
b82317dd 22849 case EM_PPC64:
dda8d76d 22850 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
60abdbed
NC
22851 display_power_gnu_attribute);
22852
643f7afb
AK
22853 case EM_S390:
22854 case EM_S390_OLD:
dda8d76d 22855 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
60abdbed
NC
22856 display_s390_gnu_attribute);
22857
9e8c70f9
DM
22858 case EM_SPARC:
22859 case EM_SPARC32PLUS:
22860 case EM_SPARCV9:
dda8d76d 22861 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
60abdbed
NC
22862 display_sparc_gnu_attribute);
22863
59e6276b 22864 case EM_TI_C6000:
dda8d76d 22865 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
60abdbed
NC
22866 display_tic6x_attribute,
22867 display_generic_attribute);
22868
0861f561
CQ
22869 case EM_CSKY:
22870 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
22871 display_csky_attribute, NULL);
22872
252b5132 22873 default:
dda8d76d 22874 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
60abdbed
NC
22875 display_public_gnu_attributes,
22876 display_generic_attribute);
252b5132 22877 }
252b5132
RH
22878}
22879
015dc7e1 22880static bool
dda8d76d 22881get_file_header (Filedata * filedata)
252b5132 22882{
9ea033b2 22883 /* Read in the identity array. */
dda8d76d 22884 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
015dc7e1 22885 return false;
252b5132 22886
9ea033b2 22887 /* Determine how to read the rest of the header. */
dda8d76d 22888 switch (filedata->file_header.e_ident[EI_DATA])
9ea033b2 22889 {
1a0670f3
AM
22890 default:
22891 case ELFDATANONE:
adab8cdc
AO
22892 case ELFDATA2LSB:
22893 byte_get = byte_get_little_endian;
22894 byte_put = byte_put_little_endian;
22895 break;
22896 case ELFDATA2MSB:
22897 byte_get = byte_get_big_endian;
22898 byte_put = byte_put_big_endian;
22899 break;
9ea033b2
NC
22900 }
22901
22902 /* For now we only support 32 bit and 64 bit ELF files. */
dda8d76d 22903 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
9ea033b2
NC
22904
22905 /* Read in the rest of the header. */
22906 if (is_32bit_elf)
22907 {
22908 Elf32_External_Ehdr ehdr32;
252b5132 22909
dda8d76d 22910 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
015dc7e1 22911 return false;
103f02d3 22912
dda8d76d
NC
22913 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
22914 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
22915 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
22916 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
22917 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
22918 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
22919 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
22920 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
22921 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
22922 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
22923 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
22924 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
22925 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
9ea033b2 22926 }
252b5132 22927 else
9ea033b2
NC
22928 {
22929 Elf64_External_Ehdr ehdr64;
a952a375 22930
dda8d76d 22931 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
015dc7e1 22932 return false;
103f02d3 22933
dda8d76d
NC
22934 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
22935 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
22936 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
22937 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
22938 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
22939 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
22940 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
22941 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
22942 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
22943 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
22944 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
22945 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
22946 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
9ea033b2 22947 }
252b5132 22948
015dc7e1 22949 return true;
252b5132
RH
22950}
22951
13acb58d
AM
22952static void
22953free_filedata (Filedata *filedata)
22954{
22955 free (filedata->program_interpreter);
13acb58d 22956 free (filedata->program_headers);
13acb58d 22957 free (filedata->section_headers);
13acb58d 22958 free (filedata->string_table);
13acb58d 22959 free (filedata->dump.dump_sects);
13acb58d 22960 free (filedata->dynamic_strings);
13acb58d 22961 free (filedata->dynamic_symbols);
13acb58d 22962 free (filedata->dynamic_syminfo);
13acb58d 22963 free (filedata->dynamic_section);
13acb58d
AM
22964
22965 while (filedata->symtab_shndx_list != NULL)
22966 {
22967 elf_section_list *next = filedata->symtab_shndx_list->next;
22968 free (filedata->symtab_shndx_list);
22969 filedata->symtab_shndx_list = next;
22970 }
22971
22972 free (filedata->section_headers_groups);
13acb58d
AM
22973
22974 if (filedata->section_groups)
22975 {
22976 size_t i;
22977 struct group_list * g;
22978 struct group_list * next;
22979
22980 for (i = 0; i < filedata->group_count; i++)
22981 {
22982 for (g = filedata->section_groups [i].root; g != NULL; g = next)
22983 {
22984 next = g->next;
22985 free (g);
22986 }
22987 }
22988
22989 free (filedata->section_groups);
13acb58d 22990 }
066f8fbe
AM
22991 memset (&filedata->section_headers, 0,
22992 sizeof (Filedata) - offsetof (Filedata, section_headers));
13acb58d
AM
22993}
22994
dda8d76d
NC
22995static void
22996close_file (Filedata * filedata)
22997{
22998 if (filedata)
22999 {
23000 if (filedata->handle)
23001 fclose (filedata->handle);
23002 free (filedata);
23003 }
23004}
23005
23006void
23007close_debug_file (void * data)
23008{
13acb58d 23009 free_filedata ((Filedata *) data);
dda8d76d
NC
23010 close_file ((Filedata *) data);
23011}
23012
23013static Filedata *
015dc7e1 23014open_file (const char * pathname, bool is_separate)
dda8d76d
NC
23015{
23016 struct stat statbuf;
23017 Filedata * filedata = NULL;
23018
23019 if (stat (pathname, & statbuf) < 0
23020 || ! S_ISREG (statbuf.st_mode))
23021 goto fail;
23022
23023 filedata = calloc (1, sizeof * filedata);
23024 if (filedata == NULL)
23025 goto fail;
23026
23027 filedata->handle = fopen (pathname, "rb");
23028 if (filedata->handle == NULL)
23029 goto fail;
23030
be7d229a 23031 filedata->file_size = statbuf.st_size;
dda8d76d 23032 filedata->file_name = pathname;
ca0e11aa 23033 filedata->is_separate = is_separate;
dda8d76d
NC
23034
23035 if (! get_file_header (filedata))
23036 goto fail;
23037
4de91c10
AM
23038 if (!get_section_headers (filedata, false))
23039 goto fail;
dda8d76d
NC
23040
23041 return filedata;
23042
23043 fail:
23044 if (filedata)
23045 {
23046 if (filedata->handle)
23047 fclose (filedata->handle);
23048 free (filedata);
23049 }
23050 return NULL;
23051}
23052
23053void *
23054open_debug_file (const char * pathname)
23055{
015dc7e1 23056 return open_file (pathname, true);
dda8d76d
NC
23057}
23058
835f2fae
NC
23059static void
23060initialise_dump_sects (Filedata * filedata)
23061{
23062 /* Initialise the dump_sects array from the cmdline_dump_sects array.
23063 Note we do this even if cmdline_dump_sects is empty because we
23064 must make sure that the dump_sets array is zeroed out before each
23065 object file is processed. */
23066 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
23067 memset (filedata->dump.dump_sects, 0,
23068 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
23069
23070 if (cmdline.num_dump_sects > 0)
23071 {
23072 if (filedata->dump.num_dump_sects == 0)
23073 /* A sneaky way of allocating the dump_sects array. */
23074 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
23075
23076 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
23077 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
23078 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
23079 }
23080}
23081
94585d6d
NC
23082static bool
23083might_need_separate_debug_info (Filedata * filedata)
23084{
23085 /* Debuginfo files do not need further separate file loading. */
23086 if (filedata->file_header.e_shstrndx == SHN_UNDEF)
23087 return false;
23088
23089 /* Since do_follow_links might be enabled by default, only treat it as an
23090 indication that separate files should be loaded if setting it was a
23091 deliberate user action. */
23092 if (DEFAULT_FOR_FOLLOW_LINKS == 0 && do_follow_links)
23093 return true;
23094
23095 if (process_links || do_syms || do_unwind
23096 || dump_any_debugging || do_dump || do_debugging)
23097 return true;
23098
23099 return false;
23100}
23101
fb52b2f4
NC
23102/* Process one ELF object file according to the command line options.
23103 This file may actually be stored in an archive. The file is
32ec8896
NC
23104 positioned at the start of the ELF object. Returns TRUE if no
23105 problems were encountered, FALSE otherwise. */
fb52b2f4 23106
015dc7e1 23107static bool
dda8d76d 23108process_object (Filedata * filedata)
252b5132 23109{
015dc7e1 23110 bool have_separate_files;
252b5132 23111 unsigned int i;
015dc7e1 23112 bool res;
252b5132 23113
dda8d76d 23114 if (! get_file_header (filedata))
252b5132 23115 {
dda8d76d 23116 error (_("%s: Failed to read file header\n"), filedata->file_name);
015dc7e1 23117 return false;
252b5132
RH
23118 }
23119
23120 /* Initialise per file variables. */
978c4450
AM
23121 for (i = ARRAY_SIZE (filedata->version_info); i--;)
23122 filedata->version_info[i] = 0;
252b5132 23123
978c4450
AM
23124 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
23125 filedata->dynamic_info[i] = 0;
23126 filedata->dynamic_info_DT_GNU_HASH = 0;
23127 filedata->dynamic_info_DT_MIPS_XHASH = 0;
252b5132
RH
23128
23129 /* Process the file. */
23130 if (show_name)
dda8d76d 23131 printf (_("\nFile: %s\n"), filedata->file_name);
252b5132 23132
835f2fae 23133 initialise_dump_sects (filedata);
d70c5fc7 23134
4de91c10
AM
23135 /* There may be some extensions in the first section header. Don't
23136 bomb if we can't read it. */
23137 get_section_headers (filedata, true);
23138
dda8d76d 23139 if (! process_file_header (filedata))
4de91c10
AM
23140 {
23141 res = false;
23142 goto out;
23143 }
252b5132 23144
e331b18d
AM
23145 /* Throw away the single section header read above, so that we
23146 re-read the entire set. */
23147 free (filedata->section_headers);
23148 filedata->section_headers = NULL;
23149
dda8d76d 23150 if (! process_section_headers (filedata))
2f62977e 23151 {
32ec8896 23152 /* Without loaded section headers we cannot process lots of things. */
015dc7e1 23153 do_unwind = do_version = do_dump = do_arch = false;
252b5132 23154
2f62977e 23155 if (! do_using_dynamic)
015dc7e1 23156 do_syms = do_dyn_syms = do_reloc = false;
2f62977e 23157 }
252b5132 23158
dda8d76d 23159 if (! process_section_groups (filedata))
32ec8896 23160 /* Without loaded section groups we cannot process unwind. */
015dc7e1 23161 do_unwind = false;
d1f5c6e3 23162
93df3340
AM
23163 process_program_headers (filedata);
23164
23165 res = process_dynamic_section (filedata);
252b5132 23166
dda8d76d 23167 if (! process_relocs (filedata))
015dc7e1 23168 res = false;
252b5132 23169
dda8d76d 23170 if (! process_unwind (filedata))
015dc7e1 23171 res = false;
4d6ed7c8 23172
dda8d76d 23173 if (! process_symbol_table (filedata))
015dc7e1 23174 res = false;
252b5132 23175
0f03783c 23176 if (! process_lto_symbol_tables (filedata))
015dc7e1 23177 res = false;
b9e920ec 23178
dda8d76d 23179 if (! process_syminfo (filedata))
015dc7e1 23180 res = false;
252b5132 23181
dda8d76d 23182 if (! process_version_sections (filedata))
015dc7e1 23183 res = false;
252b5132 23184
94585d6d 23185 if (might_need_separate_debug_info (filedata))
24841daa 23186 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
82ed9683 23187 else
015dc7e1 23188 have_separate_files = false;
dda8d76d
NC
23189
23190 if (! process_section_contents (filedata))
015dc7e1 23191 res = false;
f5842774 23192
24841daa 23193 if (have_separate_files)
dda8d76d 23194 {
24841daa
NC
23195 separate_info * d;
23196
23197 for (d = first_separate_info; d != NULL; d = d->next)
23198 {
835f2fae
NC
23199 initialise_dump_sects (d->handle);
23200
ca0e11aa 23201 if (process_links && ! process_file_header (d->handle))
015dc7e1 23202 res = false;
ca0e11aa 23203 else if (! process_section_headers (d->handle))
015dc7e1 23204 res = false;
d6bfbc39 23205 else if (! process_section_contents (d->handle))
015dc7e1 23206 res = false;
ca0e11aa
NC
23207 else if (process_links)
23208 {
ca0e11aa 23209 if (! process_section_groups (d->handle))
015dc7e1 23210 res = false;
93df3340 23211 process_program_headers (d->handle);
ca0e11aa 23212 if (! process_dynamic_section (d->handle))
015dc7e1 23213 res = false;
ca0e11aa 23214 if (! process_relocs (d->handle))
015dc7e1 23215 res = false;
ca0e11aa 23216 if (! process_unwind (d->handle))
015dc7e1 23217 res = false;
ca0e11aa 23218 if (! process_symbol_table (d->handle))
015dc7e1 23219 res = false;
ca0e11aa 23220 if (! process_lto_symbol_tables (d->handle))
015dc7e1 23221 res = false;
ca0e11aa 23222 if (! process_syminfo (d->handle))
015dc7e1 23223 res = false;
ca0e11aa 23224 if (! process_version_sections (d->handle))
015dc7e1 23225 res = false;
ca0e11aa 23226 if (! process_notes (d->handle))
015dc7e1 23227 res = false;
ca0e11aa 23228 }
24841daa
NC
23229 }
23230
23231 /* The file handles are closed by the call to free_debug_memory() below. */
dda8d76d
NC
23232 }
23233
23234 if (! process_notes (filedata))
015dc7e1 23235 res = false;
103f02d3 23236
dda8d76d 23237 if (! process_gnu_liblist (filedata))
015dc7e1 23238 res = false;
047b2264 23239
dda8d76d 23240 if (! process_arch_specific (filedata))
015dc7e1 23241 res = false;
252b5132 23242
4de91c10 23243 out:
13acb58d 23244 free_filedata (filedata);
e4b17d5c 23245
19e6b90e 23246 free_debug_memory ();
18bd398b 23247
32ec8896 23248 return res;
252b5132
RH
23249}
23250
2cf0635d 23251/* Process an ELF archive.
32ec8896
NC
23252 On entry the file is positioned just after the ARMAG string.
23253 Returns TRUE upon success, FALSE otherwise. */
2cf0635d 23254
015dc7e1
AM
23255static bool
23256process_archive (Filedata * filedata, bool is_thin_archive)
2cf0635d
NC
23257{
23258 struct archive_info arch;
23259 struct archive_info nested_arch;
23260 size_t got;
015dc7e1 23261 bool ret = true;
2cf0635d 23262
015dc7e1 23263 show_name = true;
2cf0635d
NC
23264
23265 /* The ARCH structure is used to hold information about this archive. */
23266 arch.file_name = NULL;
23267 arch.file = NULL;
23268 arch.index_array = NULL;
23269 arch.sym_table = NULL;
23270 arch.longnames = NULL;
23271
23272 /* The NESTED_ARCH structure is used as a single-item cache of information
23273 about a nested archive (when members of a thin archive reside within
23274 another regular archive file). */
23275 nested_arch.file_name = NULL;
23276 nested_arch.file = NULL;
23277 nested_arch.index_array = NULL;
23278 nested_arch.sym_table = NULL;
23279 nested_arch.longnames = NULL;
23280
dda8d76d 23281 if (setup_archive (&arch, filedata->file_name, filedata->handle,
780f96ae
AM
23282 filedata->file_size, is_thin_archive,
23283 do_archive_index) != 0)
2cf0635d 23284 {
015dc7e1 23285 ret = false;
2cf0635d 23286 goto out;
4145f1d5 23287 }
fb52b2f4 23288
4145f1d5
NC
23289 if (do_archive_index)
23290 {
2cf0635d 23291 if (arch.sym_table == NULL)
1cb7d8b1
AM
23292 error (_("%s: unable to dump the index as none was found\n"),
23293 filedata->file_name);
4145f1d5
NC
23294 else
23295 {
26c527e6
AM
23296 uint64_t i, l;
23297 uint64_t current_pos;
4145f1d5 23298
26c527e6
AM
23299 printf (_("Index of archive %s: (%" PRIu64 " entries,"
23300 " %#" PRIx64 " bytes in the symbol table)\n"),
23301 filedata->file_name, arch.index_num,
1cb7d8b1 23302 arch.sym_size);
dda8d76d
NC
23303
23304 current_pos = ftell (filedata->handle);
4145f1d5 23305
2cf0635d 23306 for (i = l = 0; i < arch.index_num; i++)
4145f1d5 23307 {
1cb7d8b1
AM
23308 if (i == 0
23309 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
23310 {
23311 char * member_name
23312 = get_archive_member_name_at (&arch, arch.index_array[i],
23313 &nested_arch);
2cf0635d 23314
1cb7d8b1
AM
23315 if (member_name != NULL)
23316 {
23317 char * qualified_name
23318 = make_qualified_name (&arch, &nested_arch,
23319 member_name);
2cf0635d 23320
1cb7d8b1
AM
23321 if (qualified_name != NULL)
23322 {
23323 printf (_("Contents of binary %s at offset "),
23324 qualified_name);
c2a7d3f5
NC
23325 (void) print_vma (arch.index_array[i], PREFIX_HEX);
23326 putchar ('\n');
1cb7d8b1
AM
23327 free (qualified_name);
23328 }
fd486f32 23329 free (member_name);
4145f1d5
NC
23330 }
23331 }
2cf0635d
NC
23332
23333 if (l >= arch.sym_size)
4145f1d5 23334 {
1cb7d8b1
AM
23335 error (_("%s: end of the symbol table reached "
23336 "before the end of the index\n"),
dda8d76d 23337 filedata->file_name);
015dc7e1 23338 ret = false;
cb8f3167 23339 break;
4145f1d5 23340 }
591f7597 23341 /* PR 17531: file: 0b6630b2. */
1cb7d8b1
AM
23342 printf ("\t%.*s\n",
23343 (int) (arch.sym_size - l), arch.sym_table + l);
591f7597 23344 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
4145f1d5
NC
23345 }
23346
67ce483b 23347 if (arch.uses_64bit_indices)
c2a7d3f5
NC
23348 l = (l + 7) & ~ 7;
23349 else
23350 l += l & 1;
23351
2cf0635d 23352 if (l < arch.sym_size)
32ec8896 23353 {
26c527e6 23354 error (ngettext ("%s: %" PRId64 " byte remains in the symbol table, "
d3a49aa8
AM
23355 "but without corresponding entries in "
23356 "the index table\n",
26c527e6 23357 "%s: %" PRId64 " bytes remain in the symbol table, "
d3a49aa8
AM
23358 "but without corresponding entries in "
23359 "the index table\n",
23360 arch.sym_size - l),
dda8d76d 23361 filedata->file_name, arch.sym_size - l);
015dc7e1 23362 ret = false;
32ec8896 23363 }
4145f1d5 23364
63cf857e 23365 if (fseek64 (filedata->handle, current_pos, SEEK_SET) != 0)
4145f1d5 23366 {
1cb7d8b1
AM
23367 error (_("%s: failed to seek back to start of object files "
23368 "in the archive\n"),
dda8d76d 23369 filedata->file_name);
015dc7e1 23370 ret = false;
2cf0635d 23371 goto out;
4145f1d5 23372 }
fb52b2f4 23373 }
4145f1d5
NC
23374
23375 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
23376 && !do_segments && !do_header && !do_dump && !do_version
23377 && !do_histogram && !do_debugging && !do_arch && !do_notes
2c610e4b 23378 && !do_section_groups && !do_dyn_syms)
2cf0635d 23379 {
015dc7e1 23380 ret = true; /* Archive index only. */
2cf0635d
NC
23381 goto out;
23382 }
fb52b2f4
NC
23383 }
23384
fb52b2f4
NC
23385 while (1)
23386 {
2cf0635d
NC
23387 char * name;
23388 size_t namelen;
23389 char * qualified_name;
23390
23391 /* Read the next archive header. */
63cf857e 23392 if (fseek64 (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
1cb7d8b1
AM
23393 {
23394 error (_("%s: failed to seek to next archive header\n"),
23395 arch.file_name);
015dc7e1 23396 ret = false;
1cb7d8b1
AM
23397 break;
23398 }
dda8d76d 23399 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
2cf0635d 23400 if (got != sizeof arch.arhdr)
1cb7d8b1
AM
23401 {
23402 if (got == 0)
2cf0635d 23403 break;
28e817cc
NC
23404 /* PR 24049 - we cannot use filedata->file_name as this will
23405 have already been freed. */
23406 error (_("%s: failed to read archive header\n"), arch.file_name);
9abca702 23407
015dc7e1 23408 ret = false;
1cb7d8b1
AM
23409 break;
23410 }
2cf0635d 23411 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
1cb7d8b1
AM
23412 {
23413 error (_("%s: did not find a valid archive header\n"),
23414 arch.file_name);
015dc7e1 23415 ret = false;
1cb7d8b1
AM
23416 break;
23417 }
2cf0635d
NC
23418
23419 arch.next_arhdr_offset += sizeof arch.arhdr;
23420
978c4450 23421 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
2cf0635d
NC
23422
23423 name = get_archive_member_name (&arch, &nested_arch);
23424 if (name == NULL)
fb52b2f4 23425 {
28e817cc 23426 error (_("%s: bad archive file name\n"), arch.file_name);
015dc7e1 23427 ret = false;
d989285c 23428 break;
fb52b2f4 23429 }
2cf0635d 23430 namelen = strlen (name);
fb52b2f4 23431
2cf0635d
NC
23432 qualified_name = make_qualified_name (&arch, &nested_arch, name);
23433 if (qualified_name == NULL)
fb52b2f4 23434 {
28e817cc 23435 error (_("%s: bad archive file name\n"), arch.file_name);
fd486f32 23436 free (name);
015dc7e1 23437 ret = false;
d989285c 23438 break;
fb52b2f4
NC
23439 }
23440
2cf0635d 23441 if (is_thin_archive && arch.nested_member_origin == 0)
1cb7d8b1
AM
23442 {
23443 /* This is a proxy for an external member of a thin archive. */
23444 Filedata * member_filedata;
23445 char * member_file_name = adjust_relative_path
dda8d76d 23446 (filedata->file_name, name, namelen);
32ec8896 23447
fd486f32 23448 free (name);
1cb7d8b1
AM
23449 if (member_file_name == NULL)
23450 {
fd486f32 23451 free (qualified_name);
015dc7e1 23452 ret = false;
1cb7d8b1
AM
23453 break;
23454 }
2cf0635d 23455
015dc7e1 23456 member_filedata = open_file (member_file_name, false);
1cb7d8b1
AM
23457 if (member_filedata == NULL)
23458 {
23459 error (_("Input file '%s' is not readable.\n"), member_file_name);
23460 free (member_file_name);
fd486f32 23461 free (qualified_name);
015dc7e1 23462 ret = false;
1cb7d8b1
AM
23463 break;
23464 }
2cf0635d 23465
978c4450 23466 filedata->archive_file_offset = arch.nested_member_origin;
dda8d76d 23467 member_filedata->file_name = qualified_name;
2cf0635d 23468
75a2da57
AH
23469 /* The call to process_object() expects the file to be at the beginning. */
23470 rewind (member_filedata->handle);
23471
1cb7d8b1 23472 if (! process_object (member_filedata))
015dc7e1 23473 ret = false;
2cf0635d 23474
1cb7d8b1
AM
23475 close_file (member_filedata);
23476 free (member_file_name);
1cb7d8b1 23477 }
2cf0635d 23478 else if (is_thin_archive)
1cb7d8b1
AM
23479 {
23480 Filedata thin_filedata;
eb02c04d 23481
1cb7d8b1 23482 memset (&thin_filedata, 0, sizeof (thin_filedata));
dda8d76d 23483
a043396b
NC
23484 /* PR 15140: Allow for corrupt thin archives. */
23485 if (nested_arch.file == NULL)
23486 {
23487 error (_("%s: contains corrupt thin archive: %s\n"),
28e817cc 23488 qualified_name, name);
fd486f32
AM
23489 free (qualified_name);
23490 free (name);
015dc7e1 23491 ret = false;
a043396b
NC
23492 break;
23493 }
fd486f32 23494 free (name);
a043396b 23495
1cb7d8b1 23496 /* This is a proxy for a member of a nested archive. */
978c4450
AM
23497 filedata->archive_file_offset
23498 = arch.nested_member_origin + sizeof arch.arhdr;
2cf0635d 23499
1cb7d8b1
AM
23500 /* The nested archive file will have been opened and setup by
23501 get_archive_member_name. */
63cf857e
AM
23502 if (fseek64 (nested_arch.file, filedata->archive_file_offset,
23503 SEEK_SET) != 0)
1cb7d8b1
AM
23504 {
23505 error (_("%s: failed to seek to archive member.\n"),
23506 nested_arch.file_name);
fd486f32 23507 free (qualified_name);
015dc7e1 23508 ret = false;
1cb7d8b1
AM
23509 break;
23510 }
2cf0635d 23511
dda8d76d
NC
23512 thin_filedata.handle = nested_arch.file;
23513 thin_filedata.file_name = qualified_name;
9abca702 23514
1cb7d8b1 23515 if (! process_object (& thin_filedata))
015dc7e1 23516 ret = false;
1cb7d8b1 23517 }
2cf0635d 23518 else
1cb7d8b1 23519 {
fd486f32 23520 free (name);
978c4450 23521 filedata->archive_file_offset = arch.next_arhdr_offset;
6a6196fc 23522 filedata->file_name = qualified_name;
1cb7d8b1 23523 if (! process_object (filedata))
015dc7e1 23524 ret = false;
237877b8 23525 arch.next_arhdr_offset += (filedata->archive_file_size + 1) & -2;
4c836627 23526 /* Stop looping with "negative" archive_file_size. */
978c4450 23527 if (arch.next_arhdr_offset < filedata->archive_file_size)
80e2a3b6 23528 arch.next_arhdr_offset = -1ul;
1cb7d8b1 23529 }
fb52b2f4 23530
2cf0635d 23531 free (qualified_name);
fb52b2f4
NC
23532 }
23533
4145f1d5 23534 out:
2cf0635d
NC
23535 if (nested_arch.file != NULL)
23536 fclose (nested_arch.file);
23537 release_archive (&nested_arch);
23538 release_archive (&arch);
fb52b2f4 23539
d989285c 23540 return ret;
fb52b2f4
NC
23541}
23542
015dc7e1 23543static bool
2cf0635d 23544process_file (char * file_name)
fb52b2f4 23545{
dda8d76d 23546 Filedata * filedata = NULL;
fb52b2f4
NC
23547 struct stat statbuf;
23548 char armag[SARMAG];
015dc7e1 23549 bool ret = true;
fb52b2f4
NC
23550
23551 if (stat (file_name, &statbuf) < 0)
23552 {
f24ddbdd
NC
23553 if (errno == ENOENT)
23554 error (_("'%s': No such file\n"), file_name);
23555 else
23556 error (_("Could not locate '%s'. System error message: %s\n"),
23557 file_name, strerror (errno));
015dc7e1 23558 return false;
f24ddbdd
NC
23559 }
23560
23561 if (! S_ISREG (statbuf.st_mode))
23562 {
23563 error (_("'%s' is not an ordinary file\n"), file_name);
015dc7e1 23564 return false;
fb52b2f4
NC
23565 }
23566
dda8d76d
NC
23567 filedata = calloc (1, sizeof * filedata);
23568 if (filedata == NULL)
23569 {
23570 error (_("Out of memory allocating file data structure\n"));
015dc7e1 23571 return false;
dda8d76d
NC
23572 }
23573
23574 filedata->file_name = file_name;
23575 filedata->handle = fopen (file_name, "rb");
23576 if (filedata->handle == NULL)
fb52b2f4 23577 {
f24ddbdd 23578 error (_("Input file '%s' is not readable.\n"), file_name);
dda8d76d 23579 free (filedata);
015dc7e1 23580 return false;
fb52b2f4
NC
23581 }
23582
dda8d76d 23583 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
fb52b2f4 23584 {
4145f1d5 23585 error (_("%s: Failed to read file's magic number\n"), file_name);
dda8d76d
NC
23586 fclose (filedata->handle);
23587 free (filedata);
015dc7e1 23588 return false;
fb52b2f4
NC
23589 }
23590
be7d229a 23591 filedata->file_size = statbuf.st_size;
015dc7e1 23592 filedata->is_separate = false;
f54498b4 23593
fb52b2f4 23594 if (memcmp (armag, ARMAG, SARMAG) == 0)
32ec8896 23595 {
015dc7e1
AM
23596 if (! process_archive (filedata, false))
23597 ret = false;
32ec8896 23598 }
2cf0635d 23599 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
32ec8896 23600 {
015dc7e1
AM
23601 if ( ! process_archive (filedata, true))
23602 ret = false;
32ec8896 23603 }
fb52b2f4
NC
23604 else
23605 {
1b513401 23606 if (do_archive_index && !check_all)
4145f1d5
NC
23607 error (_("File %s is not an archive so its index cannot be displayed.\n"),
23608 file_name);
23609
dda8d76d 23610 rewind (filedata->handle);
978c4450 23611 filedata->archive_file_size = filedata->archive_file_offset = 0;
32ec8896 23612
dda8d76d 23613 if (! process_object (filedata))
015dc7e1 23614 ret = false;
fb52b2f4
NC
23615 }
23616
dda8d76d 23617 fclose (filedata->handle);
8fb879cd
AM
23618 free (filedata->section_headers);
23619 free (filedata->program_headers);
23620 free (filedata->string_table);
6431e409 23621 free (filedata->dump.dump_sects);
dda8d76d 23622 free (filedata);
32ec8896 23623
fd486f32 23624 free (ba_cache.strtab);
1bd6175a 23625 ba_cache.strtab = NULL;
fd486f32 23626 free (ba_cache.symtab);
1bd6175a 23627 ba_cache.symtab = NULL;
fd486f32
AM
23628 ba_cache.filedata = NULL;
23629
fb52b2f4
NC
23630 return ret;
23631}
23632
252b5132
RH
23633#ifdef SUPPORT_DISASSEMBLY
23634/* Needed by the i386 disassembler. For extra credit, someone could
9ea033b2 23635 fix this so that we insert symbolic addresses here, esp for GOT/PLT
e3c8793a 23636 symbols. */
252b5132
RH
23637
23638void
2cf0635d 23639print_address (unsigned int addr, FILE * outfile)
252b5132
RH
23640{
23641 fprintf (outfile,"0x%8.8x", addr);
23642}
23643
e3c8793a 23644/* Needed by the i386 disassembler. */
dda8d76d 23645
252b5132
RH
23646void
23647db_task_printsym (unsigned int addr)
23648{
23649 print_address (addr, stderr);
23650}
23651#endif
23652
23653int
2cf0635d 23654main (int argc, char ** argv)
252b5132 23655{
ff78d6d6
L
23656 int err;
23657
87b9f255 23658#ifdef HAVE_LC_MESSAGES
252b5132 23659 setlocale (LC_MESSAGES, "");
3882b010 23660#endif
3882b010 23661 setlocale (LC_CTYPE, "");
252b5132
RH
23662 bindtextdomain (PACKAGE, LOCALEDIR);
23663 textdomain (PACKAGE);
23664
869b9d07
MM
23665 expandargv (&argc, &argv);
23666
dda8d76d 23667 parse_args (& cmdline, argc, argv);
59f14fc0 23668
18bd398b 23669 if (optind < (argc - 1))
1b513401
NC
23670 /* When displaying information for more than one file,
23671 prefix the information with the file name. */
015dc7e1 23672 show_name = true;
5656ba2c
L
23673 else if (optind >= argc)
23674 {
1b513401 23675 /* Ensure that the warning is always displayed. */
015dc7e1 23676 do_checks = true;
1b513401 23677
5656ba2c
L
23678 warn (_("Nothing to do.\n"));
23679 usage (stderr);
23680 }
18bd398b 23681
015dc7e1 23682 err = false;
252b5132 23683 while (optind < argc)
32ec8896 23684 if (! process_file (argv[optind++]))
015dc7e1 23685 err = true;
252b5132 23686
9db70fc3 23687 free (cmdline.dump_sects);
252b5132 23688
7d9813f1
NA
23689 free (dump_ctf_symtab_name);
23690 free (dump_ctf_strtab_name);
23691 free (dump_ctf_parent_name);
23692
32ec8896 23693 return err ? EXIT_FAILURE : EXIT_SUCCESS;
252b5132 23694}