]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame_incremental - binutils/readelf.c
Default dwarf_synchronous to true
[thirdparty/binutils-gdb.git] / binutils / readelf.c
... / ...
CommitLineData
1/* readelf.c -- display contents of an ELF format file
2 Copyright (C) 1998-2024 Free Software Foundation, Inc.
3
4 Originally developed by Eric Youngdale <eric@andante.jic.com>
5 Modifications by Nick Clifton <nickc@redhat.com>
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
11 the Free Software Foundation; either version 3 of the License, or
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
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
22 02110-1301, USA. */
23\f
24/* The difference between readelf and objdump:
25
26 Both programs are capable of displaying the contents of ELF format files,
27 so why does the binutils project have two file dumpers ?
28
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
43#include "sysdep.h"
44#include <assert.h>
45#include <time.h>
46#include <zlib.h>
47#ifdef HAVE_ZSTD
48#include <zstd.h>
49#endif
50#include <wchar.h>
51
52#if defined HAVE_MSGPACK
53#include <msgpack.h>
54#endif
55
56/* Define BFD64 here, even if our default architecture is 32 bit ELF
57 as this will allow us to read in and parse 64bit and 32bit ELF files. */
58#define BFD64
59
60#include "bfd.h"
61#include "bucomm.h"
62#include "elfcomm.h"
63#include "demanguse.h"
64#include "dwarf.h"
65#include "ctf-api.h"
66#include "sframe-api.h"
67#include "demangle.h"
68
69#include "elf/common.h"
70#include "elf/external.h"
71#include "elf/internal.h"
72
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
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
97#include "elf/aarch64.h"
98#include "elf/alpha.h"
99#include "elf/amdgpu.h"
100#include "elf/arc.h"
101#include "elf/arm.h"
102#include "elf/avr.h"
103#include "elf/bfin.h"
104#include "elf/cr16.h"
105#include "elf/cris.h"
106#include "elf/crx.h"
107#include "elf/csky.h"
108#include "elf/d10v.h"
109#include "elf/d30v.h"
110#include "elf/dlx.h"
111#include "elf/bpf.h"
112#include "elf/epiphany.h"
113#include "elf/fr30.h"
114#include "elf/frv.h"
115#include "elf/ft32.h"
116#include "elf/h8.h"
117#include "elf/hppa.h"
118#include "elf/i386.h"
119#include "elf/i370.h"
120#include "elf/i860.h"
121#include "elf/i960.h"
122#include "elf/ia64.h"
123#include "elf/ip2k.h"
124#include "elf/kvx.h"
125#include "elf/lm32.h"
126#include "elf/iq2000.h"
127#include "elf/m32c.h"
128#include "elf/m32r.h"
129#include "elf/m68k.h"
130#include "elf/m68hc11.h"
131#include "elf/s12z.h"
132#include "elf/mcore.h"
133#include "elf/mep.h"
134#include "elf/metag.h"
135#include "elf/microblaze.h"
136#include "elf/mips.h"
137#include "elf/mmix.h"
138#include "elf/mn10200.h"
139#include "elf/mn10300.h"
140#include "elf/moxie.h"
141#include "elf/mt.h"
142#include "elf/msp430.h"
143#include "elf/nds32.h"
144#include "elf/nfp.h"
145#include "elf/nios2.h"
146#include "elf/or1k.h"
147#include "elf/pj.h"
148#include "elf/ppc.h"
149#include "elf/ppc64.h"
150#include "elf/pru.h"
151#include "elf/riscv.h"
152#include "elf/rl78.h"
153#include "elf/rx.h"
154#include "elf/s390.h"
155#include "elf/score.h"
156#include "elf/sh.h"
157#include "elf/sparc.h"
158#include "elf/spu.h"
159#include "elf/tic6x.h"
160#include "elf/tilegx.h"
161#include "elf/tilepro.h"
162#include "elf/v850.h"
163#include "elf/vax.h"
164#include "elf/visium.h"
165#include "elf/wasm32.h"
166#include "elf/x86-64.h"
167#include "elf/xgate.h"
168#include "elf/xstormy16.h"
169#include "elf/xtensa.h"
170#include "elf/z80.h"
171#include "elf/loongarch.h"
172#include "elf/bpf.h"
173
174#include "getopt.h"
175#include "libiberty.h"
176#include "safe-ctype.h"
177#include "filenames.h"
178
179#ifndef offsetof
180#define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
181#endif
182
183typedef struct elf_section_list
184{
185 Elf_Internal_Shdr * hdr;
186 struct elf_section_list * next;
187} elf_section_list;
188
189/* Flag bits indicating particular types of dump. */
190#define HEX_DUMP (1 << 0) /* The -x command line switch. */
191#ifdef SUPPORT_DISASSEMBLY
192#define DISASS_DUMP (1 << 1) /* The -i command line switch. */
193#endif
194#define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
195#define STRING_DUMP (1 << 3) /* The -p command line switch. */
196#define RELOC_DUMP (1 << 4) /* The -R command line switch. */
197#define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
198#define SFRAME_DUMP (1 << 6) /* The --sframe command line switch. */
199#define AUTO_DUMP (1 << 7) /* The -j command line switch. */
200
201typedef unsigned char dump_type;
202
203/* A linked list of the section names for which dumps were requested. */
204struct dump_list_entry
205{
206 char * name;
207 dump_type type;
208 struct dump_list_entry * next;
209};
210
211/* A dynamic array of flags indicating for which sections a dump
212 has been requested via command line switches. */
213struct dump_data
214{
215 dump_type * dump_sects;
216 unsigned int num_dump_sects;
217};
218
219static struct dump_data cmdline;
220
221static struct dump_list_entry * dump_sects_byname;
222
223char * program_name = "readelf";
224
225static bool show_name = false;
226static bool do_dynamic = false;
227static bool do_syms = false;
228static bool do_dyn_syms = false;
229static bool do_lto_syms = false;
230static bool do_reloc = false;
231static bool do_sections = false;
232static bool do_section_groups = false;
233static bool do_section_details = false;
234static bool do_segments = false;
235static bool do_unwind = false;
236static bool do_using_dynamic = false;
237static bool do_header = false;
238static bool do_dump = false;
239static bool do_version = false;
240static bool do_histogram = false;
241static bool do_debugging = false;
242static bool do_ctf = false;
243static bool do_sframe = false;
244static bool do_arch = false;
245static bool do_notes = false;
246static bool do_archive_index = false;
247static bool check_all = false;
248static bool is_32bit_elf = false;
249static bool decompress_dumps = false;
250static bool do_not_show_symbol_truncation = false;
251static bool do_demangle = false; /* Pretty print C++ symbol names. */
252static bool process_links = false;
253static bool dump_any_debugging = false;
254static bool extra_sym_info = false;
255static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
256static int sym_base = 0;
257
258static char *dump_ctf_parent_name;
259static char *dump_ctf_symtab_name;
260static char *dump_ctf_strtab_name;
261
262struct group_list
263{
264 struct group_list * next;
265 unsigned int section_index;
266};
267
268struct group
269{
270 struct group_list * root;
271 unsigned int group_index;
272};
273
274typedef struct filedata
275{
276 const char * file_name;
277 bool is_separate;
278 FILE * handle;
279 uint64_t file_size;
280 Elf_Internal_Ehdr file_header;
281 uint64_t archive_file_offset;
282 uint64_t archive_file_size;
283 /* Everything below this point is cleared out by free_filedata. */
284 Elf_Internal_Shdr * section_headers;
285 Elf_Internal_Phdr * program_headers;
286 char * string_table;
287 uint64_t string_table_length;
288 uint64_t dynamic_addr;
289 uint64_t dynamic_size;
290 uint64_t dynamic_nent;
291 Elf_Internal_Dyn * dynamic_section;
292 Elf_Internal_Shdr * dynamic_strtab_section;
293 char * dynamic_strings;
294 uint64_t dynamic_strings_length;
295 Elf_Internal_Shdr * dynamic_symtab_section;
296 uint64_t num_dynamic_syms;
297 Elf_Internal_Sym * dynamic_symbols;
298 uint64_t version_info[16];
299 unsigned int dynamic_syminfo_nent;
300 Elf_Internal_Syminfo * dynamic_syminfo;
301 uint64_t dynamic_syminfo_offset;
302 uint64_t nbuckets;
303 uint64_t nchains;
304 uint64_t * buckets;
305 uint64_t * chains;
306 uint64_t ngnubuckets;
307 uint64_t ngnuchains;
308 uint64_t * gnubuckets;
309 uint64_t * gnuchains;
310 uint64_t * mipsxlat;
311 uint64_t gnusymidx;
312 char * program_interpreter;
313 uint64_t dynamic_info[DT_RELRENT + 1];
314 uint64_t dynamic_info_DT_GNU_HASH;
315 uint64_t dynamic_info_DT_MIPS_XHASH;
316 elf_section_list * symtab_shndx_list;
317 size_t group_count;
318 struct group * section_groups;
319 struct group ** section_headers_groups;
320 /* A dynamic array of flags indicating for which sections a dump of
321 some kind has been requested. It is reset on a per-object file
322 basis and then initialised from the cmdline_dump_sects array,
323 the results of interpreting the -w switch, and the
324 dump_sects_byname list. */
325 struct dump_data dump;
326} Filedata;
327
328/* How to print a vma value. */
329typedef enum print_mode
330{
331 HEX,
332 HEX_5,
333 DEC,
334 DEC_5,
335 UNSIGNED,
336 UNSIGNED_5,
337 PREFIX_HEX,
338 PREFIX_HEX_5,
339 FULL_HEX,
340 LONG_HEX,
341 ZERO_HEX,
342 OCTAL,
343 OCTAL_5
344}
345print_mode;
346
347typedef enum unicode_display_type
348{
349 unicode_default = 0,
350 unicode_locale,
351 unicode_escape,
352 unicode_hex,
353 unicode_highlight,
354 unicode_invalid
355} unicode_display_type;
356
357static unicode_display_type unicode_display = unicode_default;
358
359typedef enum
360{
361 reltype_unknown,
362 reltype_rel,
363 reltype_rela,
364 reltype_relr
365} relocation_type;
366
367/* Versioned symbol info. */
368enum versioned_symbol_info
369{
370 symbol_undefined,
371 symbol_hidden,
372 symbol_public
373};
374
375static int
376fseek64 (FILE *stream, int64_t offset, int whence)
377{
378#if defined (HAVE_FSEEKO64)
379 off64_t o = offset;
380 if (o != offset)
381 {
382 errno = EINVAL;
383 return -1;
384 }
385 return fseeko64 (stream, o, whence);
386#elif defined (HAVE_FSEEKO)
387 off_t o = offset;
388 if (o != offset)
389 {
390 errno = EINVAL;
391 return -1;
392 }
393 return fseeko (stream, o, whence);
394#else
395 long o = offset;
396 if (o != offset)
397 {
398 errno = EINVAL;
399 return -1;
400 }
401 return fseek (stream, o, whence);
402#endif
403}
404
405static const char * get_symbol_version_string
406 (Filedata *, bool, const char *, size_t, unsigned,
407 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
408
409static bool process_notes_at
410 (Filedata *, Elf_Internal_Shdr *, uint64_t, uint64_t, uint64_t);
411
412#define UNKNOWN -1
413
414static inline const char *
415section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
416{
417 return filedata->string_table + hdr->sh_name;
418}
419
420static inline bool
421section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
422{
423 return (filedata != NULL
424 && hdr != NULL
425 && filedata->string_table != NULL
426 && hdr->sh_name < filedata->string_table_length);
427}
428
429/* Returns true if the given index is real/valid. Note: "real" here
430 means "references a real section in the section header" and not
431 "is a valid section index as per the ELF standard". */
432
433static inline bool
434section_index_real (const Filedata *filedata, unsigned int ndx)
435{
436 return (filedata != NULL
437 && filedata->section_headers != NULL
438 && ndx < filedata->file_header.e_shnum
439 && ndx > 0);
440}
441
442#define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
443
444static inline bool
445valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
446{
447 return strtab != NULL && offset < strtab_size;
448}
449
450static inline bool
451valid_dynamic_name (const Filedata *filedata, uint64_t offset)
452{
453 return valid_symbol_name (filedata->dynamic_strings,
454 filedata->dynamic_strings_length, offset);
455}
456
457/* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
458 already been called and verified that the string exists. */
459static inline const char *
460get_dynamic_name (const Filedata *filedata, size_t offset)
461{
462 return filedata->dynamic_strings + offset;
463}
464
465#define REMOVE_ARCH_BITS(ADDR) \
466 do \
467 { \
468 if (filedata->file_header.e_machine == EM_ARM) \
469 (ADDR) &= ~1; \
470 } \
471 while (0)
472
473/* Get the correct GNU hash section name. */
474#define GNU_HASH_SECTION_NAME(filedata) \
475 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
476\f
477/* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
478 OFFSET + the offset of the current archive member, if we are examining an
479 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
480 allocate a buffer using malloc and fill that. In either case return the
481 pointer to the start of the retrieved data or NULL if something went wrong.
482 If something does go wrong and REASON is not NULL then emit an error
483 message using REASON as part of the context. */
484
485static void *
486get_data (void *var,
487 Filedata *filedata,
488 uint64_t offset,
489 uint64_t size,
490 uint64_t nmemb,
491 const char *reason)
492{
493 void * mvar;
494 uint64_t amt = size * nmemb;
495
496 if (size == 0 || nmemb == 0)
497 return NULL;
498
499 /* If size_t is smaller than uint64_t, eg because you are building
500 on a 32-bit host, then make sure that when the sizes are cast to
501 size_t no information is lost. */
502 if ((size_t) size != size
503 || (size_t) nmemb != nmemb
504 || (size_t) amt != amt
505 || amt / size != nmemb
506 || (size_t) amt + 1 == 0)
507 {
508 if (reason)
509 error (_("Size overflow prevents reading %" PRIu64
510 " elements of size %" PRIu64 " for %s\n"),
511 nmemb, size, reason);
512 return NULL;
513 }
514
515 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
516 attempting to allocate memory when the read is bound to fail. */
517 if (filedata->archive_file_offset > filedata->file_size
518 || offset > filedata->file_size - filedata->archive_file_offset
519 || amt > filedata->file_size - filedata->archive_file_offset - offset)
520 {
521 if (reason)
522 error (_("Reading %" PRIu64 " bytes extends past end of file for %s\n"),
523 amt, reason);
524 return NULL;
525 }
526
527 if (fseek64 (filedata->handle, filedata->archive_file_offset + offset,
528 SEEK_SET))
529 {
530 if (reason)
531 error (_("Unable to seek to %#" PRIx64 " for %s\n"),
532 filedata->archive_file_offset + offset, reason);
533 return NULL;
534 }
535
536 mvar = var;
537 if (mvar == NULL)
538 {
539 /* + 1 so that we can '\0' terminate invalid string table sections. */
540 mvar = malloc ((size_t) amt + 1);
541
542 if (mvar == NULL)
543 {
544 if (reason)
545 error (_("Out of memory allocating %" PRIu64 " bytes for %s\n"),
546 amt, reason);
547 return NULL;
548 }
549
550 ((char *) mvar)[amt] = '\0';
551 }
552
553 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
554 {
555 if (reason)
556 error (_("Unable to read in %" PRIu64 " bytes of %s\n"),
557 amt, reason);
558 if (mvar != var)
559 free (mvar);
560 return NULL;
561 }
562
563 return mvar;
564}
565
566/* Print a VMA value in the MODE specified.
567 Returns the number of characters displayed. */
568
569static unsigned int
570print_vma (uint64_t vma, print_mode mode)
571{
572 unsigned int nc = 0;
573
574 switch (mode)
575 {
576 case FULL_HEX:
577 nc = printf ("0x");
578 /* Fall through. */
579 case LONG_HEX:
580 if (!is_32bit_elf)
581 return nc + printf ("%16.16" PRIx64, vma);
582 return nc + printf ("%8.8" PRIx64, vma);
583
584 case ZERO_HEX:
585 if (is_32bit_elf)
586 return printf ("%08" PRIx64, vma);
587 return printf ("%016" PRIx64, vma);
588
589 case DEC_5:
590 if (vma <= 99999)
591 return printf ("%5" PRId64, vma);
592 /* Fall through. */
593 case PREFIX_HEX:
594 nc = printf ("0x");
595 /* Fall through. */
596 case HEX:
597 return nc + printf ("%" PRIx64, vma);
598
599 case PREFIX_HEX_5:
600 nc = printf ("0x");
601 /* Fall through. */
602 case HEX_5:
603 return nc + printf ("%05" PRIx64, vma);
604
605 case DEC:
606 return printf ("%" PRId64, vma);
607
608 case UNSIGNED:
609 return printf ("%" PRIu64, vma);
610
611 case UNSIGNED_5:
612 return printf ("%5" PRIu64, vma);
613
614 case OCTAL:
615 return printf ("%" PRIo64, vma);
616
617 case OCTAL_5:
618 return printf ("%5" PRIo64, vma);
619
620 default:
621 /* FIXME: Report unrecognised mode ? */
622 return 0;
623 }
624}
625
626
627/* Display a symbol on stdout. Handles the display of control characters and
628 multibye characters (assuming the host environment supports them).
629
630 Display at most abs(WIDTH) characters, truncating as necessary,
631 unless do_wide or extra_sym_info is true.
632
633 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
634 abs(WIDTH) - 5 characters followed by "[...]".
635
636 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
637 padding as necessary.
638
639 Returns the number of emitted characters. */
640
641static unsigned int
642print_symbol_name (signed int width, const char * symbol)
643{
644 bool extra_padding = false;
645 bool do_dots = false;
646 signed int num_printed = 0;
647#ifdef HAVE_MBSTATE_T
648 mbstate_t state;
649#endif
650 unsigned int width_remaining;
651 const void * alloced_symbol = NULL;
652
653 if (width < 0)
654 {
655 /* Keep the width positive. This helps the code below. */
656 width = - width;
657 extra_padding = true;
658 }
659 else if (width == 0)
660 return 0;
661
662 if (do_wide || extra_sym_info)
663 /* Set the remaining width to a very large value.
664 This simplifies the code below. */
665 width_remaining = INT_MAX;
666 else
667 {
668 width_remaining = width;
669
670 if (! do_not_show_symbol_truncation
671 && (int) strlen (symbol) > width)
672 {
673 width_remaining -= 5;
674 if ((int) width_remaining < 0)
675 width_remaining = 0;
676 do_dots = true;
677 }
678 }
679
680#ifdef HAVE_MBSTATE_T
681 /* Initialise the multibyte conversion state. */
682 memset (& state, 0, sizeof (state));
683#endif
684
685 if (do_demangle && *symbol)
686 {
687 const char * res = cplus_demangle (symbol, demangle_flags);
688
689 if (res != NULL)
690 alloced_symbol = symbol = res;
691 }
692
693 while (width_remaining)
694 {
695 size_t n;
696 const char c = *symbol++;
697
698 if (c == 0)
699 break;
700
701 if (ISPRINT (c))
702 {
703 putchar (c);
704 width_remaining --;
705 num_printed ++;
706 }
707 else if (ISCNTRL (c))
708 {
709 /* Do not print control characters directly as they can affect terminal
710 settings. Such characters usually appear in the names generated
711 by the assembler for local labels. */
712
713 if (width_remaining < 2)
714 break;
715
716 printf ("^%c", c + 0x40);
717 width_remaining -= 2;
718 num_printed += 2;
719 }
720 else if (c == 0x7f)
721 {
722 if (width_remaining < 5)
723 break;
724 printf ("<DEL>");
725 width_remaining -= 5;
726 num_printed += 5;
727 }
728 else if (unicode_display != unicode_locale
729 && unicode_display != unicode_default)
730 {
731 /* Display unicode characters as something else. */
732 unsigned char bytes[4];
733 bool is_utf8;
734 unsigned int nbytes;
735
736 bytes[0] = c;
737
738 if (bytes[0] < 0xc0)
739 {
740 nbytes = 1;
741 is_utf8 = false;
742 }
743 else
744 {
745 bytes[1] = *symbol++;
746
747 if ((bytes[1] & 0xc0) != 0x80)
748 {
749 is_utf8 = false;
750 /* Do not consume this character. It may only
751 be the first byte in the sequence that was
752 corrupt. */
753 --symbol;
754 nbytes = 1;
755 }
756 else if ((bytes[0] & 0x20) == 0)
757 {
758 is_utf8 = true;
759 nbytes = 2;
760 }
761 else
762 {
763 bytes[2] = *symbol++;
764
765 if ((bytes[2] & 0xc0) != 0x80)
766 {
767 is_utf8 = false;
768 symbol -= 2;
769 nbytes = 1;
770 }
771 else if ((bytes[0] & 0x10) == 0)
772 {
773 is_utf8 = true;
774 nbytes = 3;
775 }
776 else
777 {
778 bytes[3] = *symbol++;
779
780 nbytes = 4;
781
782 if ((bytes[3] & 0xc0) != 0x80)
783 {
784 is_utf8 = false;
785 symbol -= 3;
786 nbytes = 1;
787 }
788 else
789 is_utf8 = true;
790 }
791 }
792 }
793
794 if (unicode_display == unicode_invalid)
795 is_utf8 = false;
796
797 if (unicode_display == unicode_hex || ! is_utf8)
798 {
799 unsigned int i;
800
801 if (width_remaining < (nbytes * 2) + 2)
802 break;
803
804 putchar (is_utf8 ? '<' : '{');
805 printf ("0x");
806 for (i = 0; i < nbytes; i++)
807 printf ("%02x", bytes[i]);
808 putchar (is_utf8 ? '>' : '}');
809 }
810 else
811 {
812 if (unicode_display == unicode_highlight && isatty (1))
813 printf ("\x1B[31;47m"); /* Red. */
814
815 switch (nbytes)
816 {
817 case 2:
818 if (width_remaining < 6)
819 break;
820 printf ("\\u%02x%02x",
821 (bytes[0] & 0x1c) >> 2,
822 ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
823 break;
824 case 3:
825 if (width_remaining < 6)
826 break;
827 printf ("\\u%02x%02x",
828 ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
829 ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
830 break;
831 case 4:
832 if (width_remaining < 8)
833 break;
834 printf ("\\u%02x%02x%02x",
835 ((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
836 ((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
837 ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
838
839 break;
840 default:
841 /* URG. */
842 break;
843 }
844
845 if (unicode_display == unicode_highlight && isatty (1))
846 printf ("\033[0m"); /* Default colour. */
847 }
848
849 if (bytes[nbytes - 1] == 0)
850 break;
851 }
852 else
853 {
854#ifdef HAVE_MBSTATE_T
855 wchar_t w;
856#endif
857 /* Let printf do the hard work of displaying multibyte characters. */
858 printf ("%.1s", symbol - 1);
859 width_remaining --;
860 num_printed ++;
861
862#ifdef HAVE_MBSTATE_T
863 /* Try to find out how many bytes made up the character that was
864 just printed. Advance the symbol pointer past the bytes that
865 were displayed. */
866 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
867#else
868 n = 1;
869#endif
870 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
871 symbol += (n - 1);
872 }
873 }
874
875 if (do_dots)
876 num_printed += printf ("[...]");
877
878 if (extra_padding && num_printed < width)
879 {
880 /* Fill in the remaining spaces. */
881 printf ("%-*s", width - num_printed, " ");
882 num_printed = width;
883 }
884
885 free ((void *) alloced_symbol);
886 return num_printed;
887}
888
889/* Returns a pointer to a static buffer containing a printable version of
890 the given section's name. Like print_symbol, except that it does not try
891 to print multibyte characters, it just interprets them as hex values. */
892
893static const char *
894printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
895{
896#define NUM_SEC_NAME_BUFS 5
897#define MAX_PRINT_SEC_NAME_LEN 256
898
899 static int sec_name_buf_index = 0;
900 /* We use a rotating array of static buffers, so that multiple successive calls
901 to printable_section_name() will still work. eg when used in a printf. */
902 static char sec_name_buf [NUM_SEC_NAME_BUFS][MAX_PRINT_SEC_NAME_LEN + 1];
903
904 const char * name;
905 char * buf;
906 char * buf_start;
907 char c;
908 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
909
910 /* Validate the input parameters. */
911 if (filedata == NULL)
912 return _("<internal error>");
913 if (sec == NULL)
914 return _("<none>");
915 if (filedata->string_table == NULL)
916 return _("<no-strings>");
917 if (sec->sh_name >= filedata->string_table_length)
918 return _("<corrupt>");
919
920 /* Select a buffer to use. */
921 buf_start = buf = sec_name_buf[sec_name_buf_index];
922 if (++sec_name_buf_index >= NUM_SEC_NAME_BUFS)
923 sec_name_buf_index = 0;
924
925 name = section_name (filedata, sec);
926
927 while ((c = * name ++) != 0)
928 {
929 if (ISCNTRL (c))
930 {
931 if (remaining < 2)
932 break;
933
934 * buf ++ = '^';
935 * buf ++ = c + 0x40;
936 remaining -= 2;
937 }
938 else if (ISPRINT (c))
939 {
940 * buf ++ = c;
941 remaining -= 1;
942 }
943 else
944 {
945 static char hex[17] = "0123456789ABCDEF";
946
947 if (remaining < 4)
948 break;
949 * buf ++ = '<';
950 * buf ++ = hex[(c & 0xf0) >> 4];
951 * buf ++ = hex[c & 0x0f];
952 * buf ++ = '>';
953 remaining -= 4;
954 }
955
956 if (remaining == 0)
957 break;
958 }
959
960 * buf = 0;
961 return buf_start;
962}
963
964/* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
965 This OS has so many departures from the ELF standard that we test it at
966 many places. */
967
968static inline bool
969is_ia64_vms (Filedata * filedata)
970{
971 return filedata->file_header.e_machine == EM_IA_64
972 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
973}
974
975static const char *
976printable_section_name_from_index (Filedata * filedata,
977 size_t ndx,
978 bool * is_special)
979{
980 if (is_special != NULL)
981 * is_special = true;
982
983 switch (ndx)
984 {
985 case SHN_UNDEF: return "UND";
986 case SHN_ABS: return "ABS";
987 case SHN_COMMON: return "COM";
988 break;
989 }
990
991 if (filedata != NULL)
992 {
993 switch (filedata->file_header.e_machine)
994 {
995 case EM_MIPS:
996 if (ndx == SHN_MIPS_SCOMMON)
997 return "SCOMMON";
998 if (ndx == SHN_MIPS_SUNDEFINED)
999 return "SUNDEF";
1000 break;
1001
1002 case EM_TI_C6000:
1003 if (ndx == SHN_TIC6X_SCOMMON)
1004 return "SCOM";
1005 break;
1006
1007 case EM_X86_64:
1008 case EM_L1OM:
1009 case EM_K1OM:
1010 if (ndx == SHN_X86_64_LCOMMON)
1011 return "LARGE_COM";
1012 break;
1013
1014 case EM_IA_64:
1015 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1016 && ndx == SHN_IA_64_ANSI_COMMON)
1017 return "ANSI_COM";
1018
1019 if (is_ia64_vms (filedata) && ndx == SHN_IA_64_VMS_SYMVEC)
1020 return "VMS_SYMVEC";
1021 break;
1022
1023 default:
1024 break;
1025 }
1026
1027 if (filedata->section_headers != NULL
1028 && ndx < filedata->file_header.e_shnum)
1029 {
1030 const char * res;
1031
1032 res = printable_section_name (filedata, filedata->section_headers + ndx);
1033 if (is_special != NULL)
1034 * is_special = (res[0] == '<');
1035
1036 return res;
1037 }
1038 }
1039
1040 static char name_buf[40];
1041 unsigned int short_ndx = (unsigned int) (ndx & 0xffff);
1042
1043 if (ndx >= SHN_LOPROC && ndx <= SHN_HIPROC)
1044 sprintf (name_buf, "PRC[0x%04x]", short_ndx);
1045 else if (ndx >= SHN_LOOS && ndx <= SHN_HIOS)
1046 sprintf (name_buf, "OS [0x%04x]", short_ndx);
1047 else if (ndx >= SHN_LORESERVE)
1048 sprintf (name_buf, "RSV[0x%04x]", short_ndx);
1049 else if (filedata->file_header.e_shnum != 0
1050 && ndx >= filedata->file_header.e_shnum)
1051 sprintf (name_buf, _("BAD[0x%lx]"), (long) ndx);
1052 else
1053 sprintf (name_buf, "<section 0x%lx>", (long) ndx);
1054
1055 return name_buf;
1056}
1057
1058/* Return a pointer to section NAME, or NULL if no such section exists. */
1059
1060static Elf_Internal_Shdr *
1061find_section (Filedata * filedata, const char * name)
1062{
1063 unsigned int i;
1064
1065 if (filedata->section_headers == NULL)
1066 return NULL;
1067
1068 for (i = 0; i < filedata->file_header.e_shnum; i++)
1069 if (section_name_valid (filedata, filedata->section_headers + i)
1070 && streq (section_name (filedata, filedata->section_headers + i),
1071 name))
1072 return filedata->section_headers + i;
1073
1074 return NULL;
1075}
1076
1077/* Return a pointer to a section containing ADDR, or NULL if no such
1078 section exists. */
1079
1080static Elf_Internal_Shdr *
1081find_section_by_address (Filedata * filedata, uint64_t addr)
1082{
1083 unsigned int i;
1084
1085 if (filedata->section_headers == NULL)
1086 return NULL;
1087
1088 for (i = 0; i < filedata->file_header.e_shnum; i++)
1089 {
1090 Elf_Internal_Shdr *sec = filedata->section_headers + i;
1091
1092 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
1093 return sec;
1094 }
1095
1096 return NULL;
1097}
1098
1099static Elf_Internal_Shdr *
1100find_section_by_type (Filedata * filedata, unsigned int type)
1101{
1102 unsigned int i;
1103
1104 if (filedata->section_headers == NULL)
1105 return NULL;
1106
1107 for (i = 0; i < filedata->file_header.e_shnum; i++)
1108 {
1109 Elf_Internal_Shdr *sec = filedata->section_headers + i;
1110
1111 if (sec->sh_type == type)
1112 return sec;
1113 }
1114
1115 return NULL;
1116}
1117
1118static Elf_Internal_Shdr *
1119find_section_by_name (Filedata * filedata, const char * name)
1120{
1121 unsigned int i;
1122
1123 if (filedata->section_headers == NULL || filedata->string_table_length == 0)
1124 return NULL;
1125
1126 for (i = 0; i < filedata->file_header.e_shnum; i++)
1127 {
1128 Elf_Internal_Shdr *sec = filedata->section_headers + i;
1129
1130 if (sec->sh_name < filedata->string_table_length
1131 && streq (name, filedata->string_table + sec->sh_name))
1132 return sec;
1133 }
1134
1135 return NULL;
1136}
1137
1138/* Return a pointer to section NAME, or NULL if no such section exists,
1139 restricted to the list of sections given in SET. */
1140
1141static Elf_Internal_Shdr *
1142find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
1143{
1144 unsigned int i;
1145
1146 if (filedata->section_headers == NULL)
1147 return NULL;
1148
1149 if (set != NULL)
1150 {
1151 while ((i = *set++) > 0)
1152 {
1153 /* See PR 21156 for a reproducer. */
1154 if (i >= filedata->file_header.e_shnum)
1155 continue; /* FIXME: Should we issue an error message ? */
1156
1157 if (section_name_valid (filedata, filedata->section_headers + i)
1158 && streq (section_name (filedata, filedata->section_headers + i),
1159 name))
1160 return filedata->section_headers + i;
1161 }
1162 }
1163
1164 return find_section (filedata, name);
1165}
1166
1167/* Guess the relocation size commonly used by the specific machines. */
1168
1169static bool
1170guess_is_rela (unsigned int e_machine)
1171{
1172 switch (e_machine)
1173 {
1174 /* Targets that use REL relocations. */
1175 case EM_386:
1176 case EM_IAMCU:
1177 case EM_960:
1178 case EM_ARM:
1179 case EM_D10V:
1180 case EM_CYGNUS_D10V:
1181 case EM_DLX:
1182 case EM_MIPS:
1183 case EM_MIPS_RS3_LE:
1184 case EM_CYGNUS_M32R:
1185 case EM_SCORE:
1186 case EM_XGATE:
1187 case EM_NFP:
1188 case EM_BPF:
1189 return false;
1190
1191 /* Targets that use RELA relocations. */
1192 case EM_68K:
1193 case EM_860:
1194 case EM_AARCH64:
1195 case EM_ADAPTEVA_EPIPHANY:
1196 case EM_ALPHA:
1197 case EM_ALTERA_NIOS2:
1198 case EM_ARC:
1199 case EM_ARC_COMPACT:
1200 case EM_ARC_COMPACT2:
1201 case EM_ARC_COMPACT3:
1202 case EM_ARC_COMPACT3_64:
1203 case EM_AVR:
1204 case EM_AVR_OLD:
1205 case EM_BLACKFIN:
1206 case EM_CR16:
1207 case EM_CRIS:
1208 case EM_CRX:
1209 case EM_CSKY:
1210 case EM_D30V:
1211 case EM_CYGNUS_D30V:
1212 case EM_FR30:
1213 case EM_FT32:
1214 case EM_CYGNUS_FR30:
1215 case EM_CYGNUS_FRV:
1216 case EM_H8S:
1217 case EM_H8_300:
1218 case EM_H8_300H:
1219 case EM_IA_64:
1220 case EM_IP2K:
1221 case EM_IP2K_OLD:
1222 case EM_IQ2000:
1223 case EM_KVX:
1224 case EM_LATTICEMICO32:
1225 case EM_M32C_OLD:
1226 case EM_M32C:
1227 case EM_M32R:
1228 case EM_MCORE:
1229 case EM_CYGNUS_MEP:
1230 case EM_METAG:
1231 case EM_MMIX:
1232 case EM_MN10200:
1233 case EM_CYGNUS_MN10200:
1234 case EM_MN10300:
1235 case EM_CYGNUS_MN10300:
1236 case EM_MOXIE:
1237 case EM_MSP430:
1238 case EM_MSP430_OLD:
1239 case EM_MT:
1240 case EM_NDS32:
1241 case EM_NIOS32:
1242 case EM_OR1K:
1243 case EM_PPC64:
1244 case EM_PPC:
1245 case EM_TI_PRU:
1246 case EM_RISCV:
1247 case EM_RL78:
1248 case EM_RX:
1249 case EM_S390:
1250 case EM_S390_OLD:
1251 case EM_SH:
1252 case EM_SPARC:
1253 case EM_SPARC32PLUS:
1254 case EM_SPARCV9:
1255 case EM_SPU:
1256 case EM_TI_C6000:
1257 case EM_TILEGX:
1258 case EM_TILEPRO:
1259 case EM_V800:
1260 case EM_V850:
1261 case EM_CYGNUS_V850:
1262 case EM_VAX:
1263 case EM_VISIUM:
1264 case EM_X86_64:
1265 case EM_L1OM:
1266 case EM_K1OM:
1267 case EM_XSTORMY16:
1268 case EM_XTENSA:
1269 case EM_XTENSA_OLD:
1270 case EM_MICROBLAZE:
1271 case EM_MICROBLAZE_OLD:
1272 case EM_WEBASSEMBLY:
1273 return true;
1274
1275 case EM_68HC05:
1276 case EM_68HC08:
1277 case EM_68HC11:
1278 case EM_68HC16:
1279 case EM_FX66:
1280 case EM_ME16:
1281 case EM_MMA:
1282 case EM_NCPU:
1283 case EM_NDR1:
1284 case EM_PCP:
1285 case EM_ST100:
1286 case EM_ST19:
1287 case EM_ST7:
1288 case EM_ST9PLUS:
1289 case EM_STARCORE:
1290 case EM_SVX:
1291 case EM_TINYJ:
1292 default:
1293 warn (_("Don't know about relocations on this machine architecture\n"));
1294 return false;
1295 }
1296}
1297
1298/* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1299 Returns TRUE upon success, FALSE otherwise. If successful then a
1300 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
1301 and the number of relocs loaded is placed in *NRELASP. It is the caller's
1302 responsibility to free the allocated buffer. */
1303
1304static bool
1305slurp_rela_relocs (Filedata *filedata,
1306 uint64_t rel_offset,
1307 uint64_t rel_size,
1308 Elf_Internal_Rela **relasp,
1309 uint64_t *nrelasp)
1310{
1311 Elf_Internal_Rela * relas;
1312 uint64_t nrelas;
1313 unsigned int i;
1314
1315 if (is_32bit_elf)
1316 {
1317 Elf32_External_Rela * erelas;
1318
1319 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1320 rel_size, _("32-bit relocation data"));
1321 if (!erelas)
1322 return false;
1323
1324 nrelas = rel_size / sizeof (Elf32_External_Rela);
1325
1326 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1327 sizeof (Elf_Internal_Rela));
1328
1329 if (relas == NULL)
1330 {
1331 free (erelas);
1332 error (_("out of memory parsing relocs\n"));
1333 return false;
1334 }
1335
1336 for (i = 0; i < nrelas; i++)
1337 {
1338 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1339 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1340 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1341 }
1342
1343 free (erelas);
1344 }
1345 else
1346 {
1347 Elf64_External_Rela * erelas;
1348
1349 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1350 rel_size, _("64-bit relocation data"));
1351 if (!erelas)
1352 return false;
1353
1354 nrelas = rel_size / sizeof (Elf64_External_Rela);
1355
1356 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1357 sizeof (Elf_Internal_Rela));
1358
1359 if (relas == NULL)
1360 {
1361 free (erelas);
1362 error (_("out of memory parsing relocs\n"));
1363 return false;
1364 }
1365
1366 for (i = 0; i < nrelas; i++)
1367 {
1368 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1369 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1370 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1371
1372 if (filedata->file_header.e_machine == EM_MIPS
1373 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1374 {
1375 /* In little-endian objects, r_info isn't really a
1376 64-bit little-endian value: it has a 32-bit
1377 little-endian symbol index followed by four
1378 individual byte fields. Reorder INFO
1379 accordingly. */
1380 uint64_t inf = relas[i].r_info;
1381 inf = (((inf & 0xffffffff) << 32)
1382 | ((inf >> 56) & 0xff)
1383 | ((inf >> 40) & 0xff00)
1384 | ((inf >> 24) & 0xff0000)
1385 | ((inf >> 8) & 0xff000000));
1386 relas[i].r_info = inf;
1387 }
1388 }
1389
1390 free (erelas);
1391 }
1392
1393 *relasp = relas;
1394 *nrelasp = nrelas;
1395 return true;
1396}
1397
1398/* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1399 Returns TRUE upon success, FALSE otherwise. If successful then a
1400 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1401 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1402 responsibility to free the allocated buffer. */
1403
1404static bool
1405slurp_rel_relocs (Filedata *filedata,
1406 uint64_t rel_offset,
1407 uint64_t rel_size,
1408 Elf_Internal_Rela **relsp,
1409 uint64_t *nrelsp)
1410{
1411 Elf_Internal_Rela * rels;
1412 uint64_t nrels;
1413 unsigned int i;
1414
1415 if (is_32bit_elf)
1416 {
1417 Elf32_External_Rel * erels;
1418
1419 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1420 rel_size, _("32-bit relocation data"));
1421 if (!erels)
1422 return false;
1423
1424 nrels = rel_size / sizeof (Elf32_External_Rel);
1425
1426 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1427
1428 if (rels == NULL)
1429 {
1430 free (erels);
1431 error (_("out of memory parsing relocs\n"));
1432 return false;
1433 }
1434
1435 for (i = 0; i < nrels; i++)
1436 {
1437 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1438 rels[i].r_info = BYTE_GET (erels[i].r_info);
1439 rels[i].r_addend = 0;
1440 }
1441
1442 free (erels);
1443 }
1444 else
1445 {
1446 Elf64_External_Rel * erels;
1447
1448 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1449 rel_size, _("64-bit relocation data"));
1450 if (!erels)
1451 return false;
1452
1453 nrels = rel_size / sizeof (Elf64_External_Rel);
1454
1455 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1456
1457 if (rels == NULL)
1458 {
1459 free (erels);
1460 error (_("out of memory parsing relocs\n"));
1461 return false;
1462 }
1463
1464 for (i = 0; i < nrels; i++)
1465 {
1466 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1467 rels[i].r_info = BYTE_GET (erels[i].r_info);
1468 rels[i].r_addend = 0;
1469
1470 if (filedata->file_header.e_machine == EM_MIPS
1471 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1472 {
1473 /* In little-endian objects, r_info isn't really a
1474 64-bit little-endian value: it has a 32-bit
1475 little-endian symbol index followed by four
1476 individual byte fields. Reorder INFO
1477 accordingly. */
1478 uint64_t inf = rels[i].r_info;
1479 inf = (((inf & 0xffffffff) << 32)
1480 | ((inf >> 56) & 0xff)
1481 | ((inf >> 40) & 0xff00)
1482 | ((inf >> 24) & 0xff0000)
1483 | ((inf >> 8) & 0xff000000));
1484 rels[i].r_info = inf;
1485 }
1486 }
1487
1488 free (erels);
1489 }
1490
1491 *relsp = rels;
1492 *nrelsp = nrels;
1493 return true;
1494}
1495
1496/* Returns the reloc type extracted from the reloc info field. */
1497
1498static unsigned int
1499get_reloc_type (Filedata * filedata, uint64_t reloc_info)
1500{
1501 if (is_32bit_elf)
1502 return ELF32_R_TYPE (reloc_info);
1503
1504 switch (filedata->file_header.e_machine)
1505 {
1506 case EM_MIPS:
1507 /* Note: We assume that reloc_info has already been adjusted for us. */
1508 return ELF64_MIPS_R_TYPE (reloc_info);
1509
1510 case EM_SPARCV9:
1511 return ELF64_R_TYPE_ID (reloc_info);
1512
1513 default:
1514 return ELF64_R_TYPE (reloc_info);
1515 }
1516}
1517
1518/* Return the symbol index extracted from the reloc info field. */
1519
1520static uint64_t
1521get_reloc_symindex (uint64_t reloc_info)
1522{
1523 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1524}
1525
1526static inline bool
1527uses_msp430x_relocs (Filedata * filedata)
1528{
1529 return
1530 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
1531 /* GCC uses osabi == ELFOSBI_STANDALONE. */
1532 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1533 /* TI compiler uses ELFOSABI_NONE. */
1534 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1535}
1536
1537
1538static const char *
1539get_symbol_at (Elf_Internal_Sym * symtab,
1540 uint64_t nsyms,
1541 char * strtab,
1542 uint64_t strtablen,
1543 uint64_t where,
1544 uint64_t * offset_return)
1545{
1546 Elf_Internal_Sym * beg = symtab;
1547 Elf_Internal_Sym * end = symtab + nsyms;
1548 Elf_Internal_Sym * best = NULL;
1549 uint64_t dist = 0x100000;
1550
1551 /* Paranoia. */
1552 if (symtab == NULL || nsyms == 0 || strtab == NULL || strtablen == 0)
1553 return NULL;
1554
1555 /* FIXME: Since this function is likely to be called repeatedly with
1556 slightly increasing addresses each time, we could speed things up by
1557 caching the last returned value and starting our search from there. */
1558 while (beg < end)
1559 {
1560 Elf_Internal_Sym * sym;
1561 uint64_t value;
1562
1563 sym = beg + (end - beg) / 2;
1564
1565 value = sym->st_value;
1566
1567 if (sym->st_name != 0
1568 && where >= value
1569 && where - value < dist)
1570 {
1571 best = sym;
1572 dist = where - value;
1573 if (dist == 0)
1574 break;
1575 }
1576
1577 if (where < value)
1578 end = sym;
1579 else
1580 beg = sym + 1;
1581 }
1582
1583 if (best == NULL)
1584 return NULL;
1585
1586 if (best->st_name >= strtablen)
1587 return NULL;
1588
1589 if (offset_return != NULL)
1590 * offset_return = dist;
1591
1592 return strtab + best->st_name;
1593}
1594
1595static void
1596print_relr_addr_and_sym (Elf_Internal_Sym * symtab,
1597 uint64_t nsyms,
1598 char * strtab,
1599 uint64_t strtablen,
1600 uint64_t where)
1601{
1602 const char * symname = NULL;
1603 uint64_t offset = 0;
1604
1605 print_vma (where, ZERO_HEX);
1606 printf (" ");
1607
1608 symname = get_symbol_at (symtab, nsyms, strtab, strtablen, where, & offset);
1609
1610 if (symname == NULL)
1611 printf ("<no sym>");
1612 else if (offset == 0)
1613 print_symbol_name (38, symname);
1614 else
1615 {
1616 print_symbol_name (28, symname);
1617 printf (" + ");
1618 print_vma (offset, PREFIX_HEX);
1619 }
1620}
1621
1622static /* signed */ int
1623symcmp (const void *p, const void *q)
1624{
1625 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
1626 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
1627
1628 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
1629}
1630
1631static uint64_t
1632count_relr_relocations (Filedata * filedata,
1633 Elf_Internal_Shdr * section)
1634{
1635 uint64_t * relrs;
1636 uint64_t nentries;
1637 uint64_t i;
1638 uint64_t count;
1639 int entsize;
1640
1641 if (section == NULL
1642 || section->sh_type != SHT_RELR
1643 || section->sh_size == 0)
1644 return 0;
1645
1646 entsize = section->sh_entsize;
1647 if (entsize == 0)
1648 entsize = is_32bit_elf
1649 ? sizeof (Elf32_External_Relr) : sizeof (Elf64_External_Relr);
1650 else if (entsize != sizeof (Elf32_External_Relr)
1651 && entsize != sizeof (Elf64_External_Relr))
1652 return 0;
1653
1654 nentries = section->sh_size / entsize;
1655 if (nentries == 0)
1656 return 0;
1657
1658 /* FIXME: This call to get_data duplicates one that follows in
1659 dump_relr_relocations(). They could be combined into just
1660 one call. */
1661 relrs = get_data (NULL, filedata, section->sh_offset, 1,
1662 section->sh_size, _("RELR relocation data"));
1663 if (relrs == NULL)
1664 return 0;
1665
1666 for (count = i = 0; i < nentries; i++)
1667 {
1668 uint64_t entry;
1669
1670 if (entsize == sizeof (Elf32_External_Relr))
1671 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1672 else
1673 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1674
1675 if ((entry & 1) == 0)
1676 {
1677 ++ count;
1678 }
1679 else
1680 {
1681 if (entry == 1)
1682 continue;
1683
1684 for (; entry >>= 1;)
1685 if ((entry & 1) == 1)
1686 ++ count;
1687 }
1688 }
1689
1690 free (relrs);
1691 return count;
1692}
1693
1694static bool
1695dump_relr_relocations (Filedata * filedata,
1696 Elf_Internal_Shdr * section,
1697 Elf_Internal_Sym * symtab,
1698 uint64_t nsyms,
1699 char * strtab,
1700 uint64_t strtablen)
1701{
1702 uint64_t * relrs;
1703 uint64_t nentries, i;
1704 uint64_t relr_size = section->sh_size;
1705 int relr_entsize = section->sh_entsize;
1706 uint64_t relr_offset = section->sh_offset;
1707 uint64_t where = 0;
1708 int num_bits_in_entry;
1709
1710 if (relr_entsize == 0)
1711 relr_entsize = is_32bit_elf
1712 ? sizeof (Elf32_External_Relr) : sizeof (Elf64_External_Relr);
1713
1714 nentries = relr_size / relr_entsize;
1715
1716 if (nentries == 0)
1717 return true;
1718
1719 if (relr_entsize == sizeof (Elf32_External_Relr))
1720 num_bits_in_entry = 31;
1721 else if (relr_entsize == sizeof (Elf64_External_Relr))
1722 num_bits_in_entry = 63;
1723 else
1724 {
1725 warn (_("Unexpected entsize for RELR section\n"));
1726 return false;
1727 }
1728
1729 relrs = get_data (NULL, filedata, relr_offset, 1, relr_size, _("RELR relocation data"));
1730 if (relrs == NULL)
1731 return false;
1732
1733 if (symtab != NULL)
1734 {
1735 /* Symbol tables are not sorted on address, but we want a quick lookup
1736 for the symbol associated with each address computed below, so sort
1737 the table now. FIXME: This assumes that the symbol table will not
1738 be used later on for some other purpose. */
1739 qsort (symtab, nsyms, sizeof (Elf_Internal_Sym), symcmp);
1740 }
1741
1742 if (relr_entsize == sizeof (Elf32_External_Relr))
1743 printf (_ ("Index: Entry Address Symbolic Address\n"));
1744 else
1745 printf (_ ("Index: Entry Address Symbolic Address\n"));
1746
1747 for (i = 0; i < nentries; i++)
1748 {
1749 uint64_t entry;
1750
1751 if (relr_entsize == sizeof (Elf32_External_Relr))
1752 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1753 else
1754 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1755
1756 /* We assume that there will never be more than 9999 entries. */
1757 printf (_("%04u: "), (unsigned int) i);
1758 print_vma (entry, ZERO_HEX);
1759 printf (" ");
1760
1761 if ((entry & 1) == 0)
1762 {
1763 where = entry;
1764 print_relr_addr_and_sym (symtab, nsyms, strtab, strtablen, where);
1765 printf ("\n");
1766 where += relr_entsize;
1767 }
1768 else
1769 {
1770 bool first = true;
1771 int j;
1772
1773 /* The least significant bit is ignored. */
1774 if (entry == 1)
1775 /* This can actually happen when the linker is allowed to shrink
1776 RELR sections. For more details see: https://reviews.llvm.org/D67164. */
1777 continue;
1778 else if (i == 0)
1779 warn (_("Unusual RELR bitmap - no previous entry to set the base address\n"));
1780
1781 for (j = 0; entry >>= 1; j++)
1782 if ((entry & 1) == 1)
1783 {
1784 uint64_t addr = where + (j * relr_entsize);
1785
1786 if (first)
1787 {
1788 print_relr_addr_and_sym (symtab, nsyms, strtab, strtablen, addr);
1789 first = false;
1790 }
1791 else
1792 {
1793 printf (_("\n%*s "), relr_entsize == 4 ? 15 : 23, " ");
1794 print_relr_addr_and_sym (symtab, nsyms, strtab, strtablen, addr);
1795 }
1796 }
1797
1798 printf ("\n");
1799 where += num_bits_in_entry * relr_entsize;
1800 }
1801 }
1802
1803 free (relrs);
1804 return true;
1805}
1806
1807/* Display the contents of the relocation data found at the specified
1808 offset. */
1809
1810static bool
1811dump_relocations (Filedata * filedata,
1812 uint64_t rel_offset,
1813 uint64_t rel_size,
1814 Elf_Internal_Sym * symtab,
1815 uint64_t nsyms,
1816 char * strtab,
1817 uint64_t strtablen,
1818 relocation_type rel_type,
1819 bool is_dynsym)
1820{
1821 size_t i;
1822 Elf_Internal_Rela * rels;
1823 bool res = true;
1824
1825 if (rel_type == reltype_unknown)
1826 rel_type = guess_is_rela (filedata->file_header.e_machine) ? reltype_rela : reltype_rel;
1827
1828 if (rel_type == reltype_rela)
1829 {
1830 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1831 return false;
1832 }
1833 else if (rel_type == reltype_rel)
1834 {
1835 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1836 return false;
1837 }
1838 else if (rel_type == reltype_relr)
1839 {
1840 /* This should have been handled by display_relocations(). */
1841 return false;
1842 }
1843
1844 if (is_32bit_elf)
1845 {
1846 if (rel_type == reltype_rela)
1847 {
1848 if (do_wide)
1849 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1850 else
1851 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1852 }
1853 else
1854 {
1855 if (do_wide)
1856 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1857 else
1858 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1859 }
1860 }
1861 else
1862 {
1863 if (rel_type == reltype_rela)
1864 {
1865 if (do_wide)
1866 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
1867 else
1868 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1869 }
1870 else
1871 {
1872 if (do_wide)
1873 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
1874 else
1875 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1876 }
1877 }
1878
1879 for (i = 0; i < rel_size; i++)
1880 {
1881 const char * rtype;
1882 uint64_t offset;
1883 uint64_t inf;
1884 uint64_t symtab_index;
1885 uint64_t type;
1886
1887 offset = rels[i].r_offset;
1888 inf = rels[i].r_info;
1889
1890 type = get_reloc_type (filedata, inf);
1891 symtab_index = get_reloc_symindex (inf);
1892
1893 if (is_32bit_elf)
1894 {
1895 printf ("%8.8lx %8.8lx ",
1896 (unsigned long) offset & 0xffffffff,
1897 (unsigned long) inf & 0xffffffff);
1898 }
1899 else
1900 {
1901 printf (do_wide
1902 ? "%16.16" PRIx64 " %16.16" PRIx64 " "
1903 : "%12.12" PRIx64 " %12.12" PRIx64 " ",
1904 offset, inf);
1905 }
1906
1907 switch (filedata->file_header.e_machine)
1908 {
1909 default:
1910 rtype = NULL;
1911 break;
1912
1913 case EM_AARCH64:
1914 rtype = elf_aarch64_reloc_type (type);
1915 break;
1916
1917 case EM_M32R:
1918 case EM_CYGNUS_M32R:
1919 rtype = elf_m32r_reloc_type (type);
1920 break;
1921
1922 case EM_386:
1923 case EM_IAMCU:
1924 rtype = elf_i386_reloc_type (type);
1925 break;
1926
1927 case EM_68HC11:
1928 case EM_68HC12:
1929 rtype = elf_m68hc11_reloc_type (type);
1930 break;
1931
1932 case EM_S12Z:
1933 rtype = elf_s12z_reloc_type (type);
1934 break;
1935
1936 case EM_68K:
1937 rtype = elf_m68k_reloc_type (type);
1938 break;
1939
1940 case EM_960:
1941 rtype = elf_i960_reloc_type (type);
1942 break;
1943
1944 case EM_AVR:
1945 case EM_AVR_OLD:
1946 rtype = elf_avr_reloc_type (type);
1947 break;
1948
1949 case EM_OLD_SPARCV9:
1950 case EM_SPARC32PLUS:
1951 case EM_SPARCV9:
1952 case EM_SPARC:
1953 rtype = elf_sparc_reloc_type (type);
1954 break;
1955
1956 case EM_SPU:
1957 rtype = elf_spu_reloc_type (type);
1958 break;
1959
1960 case EM_V800:
1961 rtype = v800_reloc_type (type);
1962 break;
1963 case EM_V850:
1964 case EM_CYGNUS_V850:
1965 rtype = v850_reloc_type (type);
1966 break;
1967
1968 case EM_D10V:
1969 case EM_CYGNUS_D10V:
1970 rtype = elf_d10v_reloc_type (type);
1971 break;
1972
1973 case EM_D30V:
1974 case EM_CYGNUS_D30V:
1975 rtype = elf_d30v_reloc_type (type);
1976 break;
1977
1978 case EM_DLX:
1979 rtype = elf_dlx_reloc_type (type);
1980 break;
1981
1982 case EM_SH:
1983 rtype = elf_sh_reloc_type (type);
1984 break;
1985
1986 case EM_MN10300:
1987 case EM_CYGNUS_MN10300:
1988 rtype = elf_mn10300_reloc_type (type);
1989 break;
1990
1991 case EM_MN10200:
1992 case EM_CYGNUS_MN10200:
1993 rtype = elf_mn10200_reloc_type (type);
1994 break;
1995
1996 case EM_FR30:
1997 case EM_CYGNUS_FR30:
1998 rtype = elf_fr30_reloc_type (type);
1999 break;
2000
2001 case EM_CYGNUS_FRV:
2002 rtype = elf_frv_reloc_type (type);
2003 break;
2004
2005 case EM_CSKY:
2006 rtype = elf_csky_reloc_type (type);
2007 break;
2008
2009 case EM_FT32:
2010 rtype = elf_ft32_reloc_type (type);
2011 break;
2012
2013 case EM_MCORE:
2014 rtype = elf_mcore_reloc_type (type);
2015 break;
2016
2017 case EM_MMIX:
2018 rtype = elf_mmix_reloc_type (type);
2019 break;
2020
2021 case EM_MOXIE:
2022 rtype = elf_moxie_reloc_type (type);
2023 break;
2024
2025 case EM_MSP430:
2026 if (uses_msp430x_relocs (filedata))
2027 {
2028 rtype = elf_msp430x_reloc_type (type);
2029 break;
2030 }
2031 /* Fall through. */
2032 case EM_MSP430_OLD:
2033 rtype = elf_msp430_reloc_type (type);
2034 break;
2035
2036 case EM_NDS32:
2037 rtype = elf_nds32_reloc_type (type);
2038 break;
2039
2040 case EM_PPC:
2041 rtype = elf_ppc_reloc_type (type);
2042 break;
2043
2044 case EM_PPC64:
2045 rtype = elf_ppc64_reloc_type (type);
2046 break;
2047
2048 case EM_MIPS:
2049 case EM_MIPS_RS3_LE:
2050 rtype = elf_mips_reloc_type (type);
2051 break;
2052
2053 case EM_RISCV:
2054 rtype = elf_riscv_reloc_type (type);
2055 break;
2056
2057 case EM_ALPHA:
2058 rtype = elf_alpha_reloc_type (type);
2059 break;
2060
2061 case EM_ARM:
2062 rtype = elf_arm_reloc_type (type);
2063 break;
2064
2065 case EM_ARC:
2066 case EM_ARC_COMPACT:
2067 case EM_ARC_COMPACT2:
2068 case EM_ARC_COMPACT3:
2069 case EM_ARC_COMPACT3_64:
2070 rtype = elf_arc_reloc_type (type);
2071 break;
2072
2073 case EM_PARISC:
2074 rtype = elf_hppa_reloc_type (type);
2075 break;
2076
2077 case EM_H8_300:
2078 case EM_H8_300H:
2079 case EM_H8S:
2080 rtype = elf_h8_reloc_type (type);
2081 break;
2082
2083 case EM_OR1K:
2084 rtype = elf_or1k_reloc_type (type);
2085 break;
2086
2087 case EM_PJ:
2088 case EM_PJ_OLD:
2089 rtype = elf_pj_reloc_type (type);
2090 break;
2091 case EM_IA_64:
2092 rtype = elf_ia64_reloc_type (type);
2093 break;
2094
2095 case EM_KVX:
2096 rtype = elf_kvx_reloc_type (type);
2097 break;
2098
2099 case EM_CRIS:
2100 rtype = elf_cris_reloc_type (type);
2101 break;
2102
2103 case EM_860:
2104 rtype = elf_i860_reloc_type (type);
2105 break;
2106
2107 case EM_X86_64:
2108 case EM_L1OM:
2109 case EM_K1OM:
2110 rtype = elf_x86_64_reloc_type (type);
2111 break;
2112
2113 case EM_S370:
2114 rtype = i370_reloc_type (type);
2115 break;
2116
2117 case EM_S390_OLD:
2118 case EM_S390:
2119 rtype = elf_s390_reloc_type (type);
2120 break;
2121
2122 case EM_SCORE:
2123 rtype = elf_score_reloc_type (type);
2124 break;
2125
2126 case EM_XSTORMY16:
2127 rtype = elf_xstormy16_reloc_type (type);
2128 break;
2129
2130 case EM_CRX:
2131 rtype = elf_crx_reloc_type (type);
2132 break;
2133
2134 case EM_VAX:
2135 rtype = elf_vax_reloc_type (type);
2136 break;
2137
2138 case EM_VISIUM:
2139 rtype = elf_visium_reloc_type (type);
2140 break;
2141
2142 case EM_BPF:
2143 rtype = elf_bpf_reloc_type (type);
2144 break;
2145
2146 case EM_ADAPTEVA_EPIPHANY:
2147 rtype = elf_epiphany_reloc_type (type);
2148 break;
2149
2150 case EM_IP2K:
2151 case EM_IP2K_OLD:
2152 rtype = elf_ip2k_reloc_type (type);
2153 break;
2154
2155 case EM_IQ2000:
2156 rtype = elf_iq2000_reloc_type (type);
2157 break;
2158
2159 case EM_XTENSA_OLD:
2160 case EM_XTENSA:
2161 rtype = elf_xtensa_reloc_type (type);
2162 break;
2163
2164 case EM_LATTICEMICO32:
2165 rtype = elf_lm32_reloc_type (type);
2166 break;
2167
2168 case EM_M32C_OLD:
2169 case EM_M32C:
2170 rtype = elf_m32c_reloc_type (type);
2171 break;
2172
2173 case EM_MT:
2174 rtype = elf_mt_reloc_type (type);
2175 break;
2176
2177 case EM_BLACKFIN:
2178 rtype = elf_bfin_reloc_type (type);
2179 break;
2180
2181 case EM_CYGNUS_MEP:
2182 rtype = elf_mep_reloc_type (type);
2183 break;
2184
2185 case EM_CR16:
2186 rtype = elf_cr16_reloc_type (type);
2187 break;
2188
2189 case EM_MICROBLAZE:
2190 case EM_MICROBLAZE_OLD:
2191 rtype = elf_microblaze_reloc_type (type);
2192 break;
2193
2194 case EM_RL78:
2195 rtype = elf_rl78_reloc_type (type);
2196 break;
2197
2198 case EM_RX:
2199 rtype = elf_rx_reloc_type (type);
2200 break;
2201
2202 case EM_METAG:
2203 rtype = elf_metag_reloc_type (type);
2204 break;
2205
2206 case EM_TI_C6000:
2207 rtype = elf_tic6x_reloc_type (type);
2208 break;
2209
2210 case EM_TILEGX:
2211 rtype = elf_tilegx_reloc_type (type);
2212 break;
2213
2214 case EM_TILEPRO:
2215 rtype = elf_tilepro_reloc_type (type);
2216 break;
2217
2218 case EM_WEBASSEMBLY:
2219 rtype = elf_wasm32_reloc_type (type);
2220 break;
2221
2222 case EM_XGATE:
2223 rtype = elf_xgate_reloc_type (type);
2224 break;
2225
2226 case EM_ALTERA_NIOS2:
2227 rtype = elf_nios2_reloc_type (type);
2228 break;
2229
2230 case EM_TI_PRU:
2231 rtype = elf_pru_reloc_type (type);
2232 break;
2233
2234 case EM_NFP:
2235 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
2236 rtype = elf_nfp3200_reloc_type (type);
2237 else
2238 rtype = elf_nfp_reloc_type (type);
2239 break;
2240
2241 case EM_Z80:
2242 rtype = elf_z80_reloc_type (type);
2243 break;
2244
2245 case EM_LOONGARCH:
2246 rtype = elf_loongarch_reloc_type (type);
2247 break;
2248
2249 case EM_AMDGPU:
2250 rtype = elf_amdgpu_reloc_type (type);
2251 break;
2252 }
2253
2254 if (rtype == NULL)
2255 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
2256 else
2257 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
2258
2259 if (filedata->file_header.e_machine == EM_ALPHA
2260 && rtype != NULL
2261 && streq (rtype, "R_ALPHA_LITUSE")
2262 && rel_type == reltype_rela)
2263 {
2264 switch (rels[i].r_addend)
2265 {
2266 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
2267 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
2268 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
2269 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
2270 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
2271 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
2272 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
2273 default: rtype = NULL;
2274 }
2275
2276 if (rtype)
2277 printf (" (%s)", rtype);
2278 else
2279 {
2280 putchar (' ');
2281 printf (_("<unknown addend: %" PRIx64 ">"),
2282 rels[i].r_addend);
2283 res = false;
2284 }
2285 }
2286 else if (symtab_index)
2287 {
2288 if (symtab == NULL || symtab_index >= nsyms)
2289 {
2290 error (_(" bad symbol index: %08lx in reloc\n"),
2291 (unsigned long) symtab_index);
2292 res = false;
2293 }
2294 else
2295 {
2296 Elf_Internal_Sym * psym;
2297 const char * version_string;
2298 enum versioned_symbol_info sym_info;
2299 unsigned short vna_other;
2300
2301 psym = symtab + symtab_index;
2302
2303 version_string
2304 = get_symbol_version_string (filedata, is_dynsym,
2305 strtab, strtablen,
2306 symtab_index,
2307 psym,
2308 &sym_info,
2309 &vna_other);
2310
2311 printf (" ");
2312
2313 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
2314 {
2315 const char * name;
2316 unsigned int len;
2317 unsigned int width = is_32bit_elf ? 8 : 14;
2318
2319 /* Relocations against GNU_IFUNC symbols do not use the value
2320 of the symbol as the address to relocate against. Instead
2321 they invoke the function named by the symbol and use its
2322 result as the address for relocation.
2323
2324 To indicate this to the user, do not display the value of
2325 the symbol in the "Symbols's Value" field. Instead show
2326 its name followed by () as a hint that the symbol is
2327 invoked. */
2328
2329 if (strtab == NULL
2330 || psym->st_name == 0
2331 || psym->st_name >= strtablen)
2332 name = "??";
2333 else
2334 name = strtab + psym->st_name;
2335
2336 len = print_symbol_name (width, name);
2337 if (version_string)
2338 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2339 version_string);
2340 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
2341 }
2342 else
2343 {
2344 print_vma (psym->st_value, LONG_HEX);
2345
2346 printf (is_32bit_elf ? " " : " ");
2347 }
2348
2349 if (psym->st_name == 0)
2350 {
2351 const char * sec_name = "<null>";
2352
2353 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
2354 sec_name = printable_section_name_from_index
2355 (filedata, psym->st_shndx, NULL);
2356
2357 print_symbol_name (22, sec_name);
2358 }
2359 else if (strtab == NULL)
2360 printf (_("<string table index: %3ld>"), psym->st_name);
2361 else if (psym->st_name >= strtablen)
2362 {
2363 error (_("<corrupt string table index: %3ld>\n"),
2364 psym->st_name);
2365 res = false;
2366 }
2367 else
2368 {
2369 print_symbol_name (22, strtab + psym->st_name);
2370 if (version_string)
2371 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2372 version_string);
2373 }
2374
2375 if (rel_type == reltype_rela)
2376 {
2377 uint64_t off = rels[i].r_addend;
2378
2379 if ((int64_t) off < 0)
2380 printf (" - %" PRIx64, -off);
2381 else
2382 printf (" + %" PRIx64, off);
2383 }
2384 }
2385 }
2386 else if (rel_type == reltype_rela)
2387 {
2388 uint64_t off = rels[i].r_addend;
2389
2390 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
2391 if ((int64_t) off < 0)
2392 printf ("-%" PRIx64, -off);
2393 else
2394 printf ("%" PRIx64, off);
2395 }
2396
2397 if (filedata->file_header.e_machine == EM_SPARCV9
2398 && rtype != NULL
2399 && streq (rtype, "R_SPARC_OLO10"))
2400 printf (" + %" PRIx64, ELF64_R_TYPE_DATA (inf));
2401
2402 putchar ('\n');
2403
2404 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
2405 {
2406 uint64_t type2 = ELF64_MIPS_R_TYPE2 (inf);
2407 uint64_t type3 = ELF64_MIPS_R_TYPE3 (inf);
2408 const char * rtype2 = elf_mips_reloc_type (type2);
2409 const char * rtype3 = elf_mips_reloc_type (type3);
2410
2411 printf (" Type2: ");
2412
2413 if (rtype2 == NULL)
2414 printf (_("unrecognized: %-7lx"),
2415 (unsigned long) type2 & 0xffffffff);
2416 else
2417 printf ("%-17.17s", rtype2);
2418
2419 printf ("\n Type3: ");
2420
2421 if (rtype3 == NULL)
2422 printf (_("unrecognized: %-7lx"),
2423 (unsigned long) type3 & 0xffffffff);
2424 else
2425 printf ("%-17.17s", rtype3);
2426
2427 putchar ('\n');
2428 }
2429 }
2430
2431 free (rels);
2432
2433 return res;
2434}
2435
2436static const char *
2437get_aarch64_dynamic_type (unsigned long type)
2438{
2439 switch (type)
2440 {
2441 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
2442 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
2443 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
2444 default:
2445 return NULL;
2446 }
2447}
2448
2449static const char *
2450get_mips_dynamic_type (unsigned long type)
2451{
2452 switch (type)
2453 {
2454 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
2455 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
2456 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
2457 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
2458 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
2459 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
2460 case DT_MIPS_MSYM: return "MIPS_MSYM";
2461 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2462 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2463 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
2464 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
2465 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
2466 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
2467 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
2468 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
2469 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
2470 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
2471 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
2472 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
2473 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
2474 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
2475 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
2476 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
2477 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
2478 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
2479 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
2480 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
2481 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
2482 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
2483 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
2484 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2485 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
2486 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
2487 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
2488 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
2489 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2490 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
2491 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
2492 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
2493 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
2494 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
2495 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
2496 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
2497 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
2498 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
2499 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
2500 case DT_MIPS_XHASH: return "MIPS_XHASH";
2501 default:
2502 return NULL;
2503 }
2504}
2505
2506static const char *
2507get_sparc64_dynamic_type (unsigned long type)
2508{
2509 switch (type)
2510 {
2511 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
2512 default:
2513 return NULL;
2514 }
2515}
2516
2517static const char *
2518get_ppc_dynamic_type (unsigned long type)
2519{
2520 switch (type)
2521 {
2522 case DT_PPC_GOT: return "PPC_GOT";
2523 case DT_PPC_OPT: return "PPC_OPT";
2524 default:
2525 return NULL;
2526 }
2527}
2528
2529static const char *
2530get_ppc64_dynamic_type (unsigned long type)
2531{
2532 switch (type)
2533 {
2534 case DT_PPC64_GLINK: return "PPC64_GLINK";
2535 case DT_PPC64_OPD: return "PPC64_OPD";
2536 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
2537 case DT_PPC64_OPT: return "PPC64_OPT";
2538 default:
2539 return NULL;
2540 }
2541}
2542
2543static const char *
2544get_parisc_dynamic_type (unsigned long type)
2545{
2546 switch (type)
2547 {
2548 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
2549 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
2550 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
2551 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
2552 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
2553 case DT_HP_PREINIT: return "HP_PREINIT";
2554 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
2555 case DT_HP_NEEDED: return "HP_NEEDED";
2556 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
2557 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
2558 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
2559 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
2560 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
2561 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
2562 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
2563 case DT_HP_FILTERED: return "HP_FILTERED";
2564 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
2565 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
2566 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
2567 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
2568 case DT_PLT: return "PLT";
2569 case DT_PLT_SIZE: return "PLT_SIZE";
2570 case DT_DLT: return "DLT";
2571 case DT_DLT_SIZE: return "DLT_SIZE";
2572 default:
2573 return NULL;
2574 }
2575}
2576
2577static const char *
2578get_ia64_dynamic_type (unsigned long type)
2579{
2580 switch (type)
2581 {
2582 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2583 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2584 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2585 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2586 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2587 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2588 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2589 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2590 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2591 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2592 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2593 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2594 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2595 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2596 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2597 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2598 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2599 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2600 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2601 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2602 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2603 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2604 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2605 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2606 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2607 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2608 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2609 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2610 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2611 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2612 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
2613 default:
2614 return NULL;
2615 }
2616}
2617
2618static const char *
2619get_solaris_section_type (unsigned long type)
2620{
2621 switch (type)
2622 {
2623 case 0x6fffffee: return "SUNW_ancillary";
2624 case 0x6fffffef: return "SUNW_capchain";
2625 case 0x6ffffff0: return "SUNW_capinfo";
2626 case 0x6ffffff1: return "SUNW_symsort";
2627 case 0x6ffffff2: return "SUNW_tlssort";
2628 case 0x6ffffff3: return "SUNW_LDYNSYM";
2629 case 0x6ffffff4: return "SUNW_dof";
2630 case 0x6ffffff5: return "SUNW_cap";
2631 case 0x6ffffff6: return "SUNW_SIGNATURE";
2632 case 0x6ffffff7: return "SUNW_ANNOTATE";
2633 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2634 case 0x6ffffff9: return "SUNW_DEBUG";
2635 case 0x6ffffffa: return "SUNW_move";
2636 case 0x6ffffffb: return "SUNW_COMDAT";
2637 case 0x6ffffffc: return "SUNW_syminfo";
2638 case 0x6ffffffd: return "SUNW_verdef";
2639 case 0x6ffffffe: return "SUNW_verneed";
2640 case 0x6fffffff: return "SUNW_versym";
2641 case 0x70000000: return "SPARC_GOTDATA";
2642 default: return NULL;
2643 }
2644}
2645
2646static const char *
2647get_alpha_dynamic_type (unsigned long type)
2648{
2649 switch (type)
2650 {
2651 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2652 default: return NULL;
2653 }
2654}
2655
2656static const char *
2657get_score_dynamic_type (unsigned long type)
2658{
2659 switch (type)
2660 {
2661 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2662 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2663 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2664 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2665 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2666 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
2667 default: return NULL;
2668 }
2669}
2670
2671static const char *
2672get_tic6x_dynamic_type (unsigned long type)
2673{
2674 switch (type)
2675 {
2676 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2677 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2678 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2679 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2680 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2681 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
2682 default: return NULL;
2683 }
2684}
2685
2686static const char *
2687get_nios2_dynamic_type (unsigned long type)
2688{
2689 switch (type)
2690 {
2691 case DT_NIOS2_GP: return "NIOS2_GP";
2692 default: return NULL;
2693 }
2694}
2695
2696static const char *
2697get_solaris_dynamic_type (unsigned long type)
2698{
2699 switch (type)
2700 {
2701 case 0x6000000d: return "SUNW_AUXILIARY";
2702 case 0x6000000e: return "SUNW_RTLDINF";
2703 case 0x6000000f: return "SUNW_FILTER";
2704 case 0x60000010: return "SUNW_CAP";
2705 case 0x60000011: return "SUNW_SYMTAB";
2706 case 0x60000012: return "SUNW_SYMSZ";
2707 case 0x60000013: return "SUNW_SORTENT";
2708 case 0x60000014: return "SUNW_SYMSORT";
2709 case 0x60000015: return "SUNW_SYMSORTSZ";
2710 case 0x60000016: return "SUNW_TLSSORT";
2711 case 0x60000017: return "SUNW_TLSSORTSZ";
2712 case 0x60000018: return "SUNW_CAPINFO";
2713 case 0x60000019: return "SUNW_STRPAD";
2714 case 0x6000001a: return "SUNW_CAPCHAIN";
2715 case 0x6000001b: return "SUNW_LDMACH";
2716 case 0x6000001d: return "SUNW_CAPCHAINENT";
2717 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2718 case 0x60000021: return "SUNW_PARENT";
2719 case 0x60000023: return "SUNW_ASLR";
2720 case 0x60000025: return "SUNW_RELAX";
2721 case 0x60000029: return "SUNW_NXHEAP";
2722 case 0x6000002b: return "SUNW_NXSTACK";
2723
2724 case 0x70000001: return "SPARC_REGISTER";
2725 case 0x7ffffffd: return "AUXILIARY";
2726 case 0x7ffffffe: return "USED";
2727 case 0x7fffffff: return "FILTER";
2728
2729 default: return NULL;
2730 }
2731}
2732
2733static const char *
2734get_riscv_dynamic_type (unsigned long type)
2735{
2736 switch (type)
2737 {
2738 case DT_RISCV_VARIANT_CC: return "RISCV_VARIANT_CC";
2739 default:
2740 return NULL;
2741 }
2742}
2743
2744static const char *
2745get_x86_64_dynamic_type (unsigned long type)
2746{
2747 switch (type)
2748 {
2749 case DT_X86_64_PLT:
2750 return "DT_X86_64_PLT";
2751 case DT_X86_64_PLTSZ:
2752 return "DT_X86_64_PLTSZ";
2753 case DT_X86_64_PLTENT:
2754 return "DT_X86_64_PLTENT";
2755 default:
2756 return NULL;
2757 }
2758}
2759
2760static const char *
2761get_dynamic_type (Filedata * filedata, unsigned long type)
2762{
2763 static char buff[64];
2764
2765 switch (type)
2766 {
2767 case DT_NULL: return "NULL";
2768 case DT_NEEDED: return "NEEDED";
2769 case DT_PLTRELSZ: return "PLTRELSZ";
2770 case DT_PLTGOT: return "PLTGOT";
2771 case DT_HASH: return "HASH";
2772 case DT_STRTAB: return "STRTAB";
2773 case DT_SYMTAB: return "SYMTAB";
2774 case DT_RELA: return "RELA";
2775 case DT_RELASZ: return "RELASZ";
2776 case DT_RELAENT: return "RELAENT";
2777 case DT_STRSZ: return "STRSZ";
2778 case DT_SYMENT: return "SYMENT";
2779 case DT_INIT: return "INIT";
2780 case DT_FINI: return "FINI";
2781 case DT_SONAME: return "SONAME";
2782 case DT_RPATH: return "RPATH";
2783 case DT_SYMBOLIC: return "SYMBOLIC";
2784 case DT_REL: return "REL";
2785 case DT_RELSZ: return "RELSZ";
2786 case DT_RELENT: return "RELENT";
2787 case DT_RELR: return "RELR";
2788 case DT_RELRSZ: return "RELRSZ";
2789 case DT_RELRENT: return "RELRENT";
2790 case DT_PLTREL: return "PLTREL";
2791 case DT_DEBUG: return "DEBUG";
2792 case DT_TEXTREL: return "TEXTREL";
2793 case DT_JMPREL: return "JMPREL";
2794 case DT_BIND_NOW: return "BIND_NOW";
2795 case DT_INIT_ARRAY: return "INIT_ARRAY";
2796 case DT_FINI_ARRAY: return "FINI_ARRAY";
2797 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2798 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2799 case DT_RUNPATH: return "RUNPATH";
2800 case DT_FLAGS: return "FLAGS";
2801
2802 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2803 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2804 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2805
2806 case DT_CHECKSUM: return "CHECKSUM";
2807 case DT_PLTPADSZ: return "PLTPADSZ";
2808 case DT_MOVEENT: return "MOVEENT";
2809 case DT_MOVESZ: return "MOVESZ";
2810 case DT_FEATURE: return "FEATURE";
2811 case DT_POSFLAG_1: return "POSFLAG_1";
2812 case DT_SYMINSZ: return "SYMINSZ";
2813 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
2814
2815 case DT_ADDRRNGLO: return "ADDRRNGLO";
2816 case DT_CONFIG: return "CONFIG";
2817 case DT_DEPAUDIT: return "DEPAUDIT";
2818 case DT_AUDIT: return "AUDIT";
2819 case DT_PLTPAD: return "PLTPAD";
2820 case DT_MOVETAB: return "MOVETAB";
2821 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
2822
2823 case DT_VERSYM: return "VERSYM";
2824
2825 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2826 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2827 case DT_RELACOUNT: return "RELACOUNT";
2828 case DT_RELCOUNT: return "RELCOUNT";
2829 case DT_FLAGS_1: return "FLAGS_1";
2830 case DT_VERDEF: return "VERDEF";
2831 case DT_VERDEFNUM: return "VERDEFNUM";
2832 case DT_VERNEED: return "VERNEED";
2833 case DT_VERNEEDNUM: return "VERNEEDNUM";
2834
2835 case DT_AUXILIARY: return "AUXILIARY";
2836 case DT_USED: return "USED";
2837 case DT_FILTER: return "FILTER";
2838
2839 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2840 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2841 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2842 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2843 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2844 case DT_GNU_HASH: return "GNU_HASH";
2845 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
2846
2847 default:
2848 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2849 {
2850 const char * result;
2851
2852 switch (filedata->file_header.e_machine)
2853 {
2854 case EM_AARCH64:
2855 result = get_aarch64_dynamic_type (type);
2856 break;
2857 case EM_MIPS:
2858 case EM_MIPS_RS3_LE:
2859 result = get_mips_dynamic_type (type);
2860 break;
2861 case EM_SPARCV9:
2862 result = get_sparc64_dynamic_type (type);
2863 break;
2864 case EM_PPC:
2865 result = get_ppc_dynamic_type (type);
2866 break;
2867 case EM_PPC64:
2868 result = get_ppc64_dynamic_type (type);
2869 break;
2870 case EM_IA_64:
2871 result = get_ia64_dynamic_type (type);
2872 break;
2873 case EM_ALPHA:
2874 result = get_alpha_dynamic_type (type);
2875 break;
2876 case EM_SCORE:
2877 result = get_score_dynamic_type (type);
2878 break;
2879 case EM_TI_C6000:
2880 result = get_tic6x_dynamic_type (type);
2881 break;
2882 case EM_ALTERA_NIOS2:
2883 result = get_nios2_dynamic_type (type);
2884 break;
2885 case EM_RISCV:
2886 result = get_riscv_dynamic_type (type);
2887 break;
2888 case EM_X86_64:
2889 result = get_x86_64_dynamic_type (type);
2890 break;
2891 default:
2892 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2893 result = get_solaris_dynamic_type (type);
2894 else
2895 result = NULL;
2896 break;
2897 }
2898
2899 if (result != NULL)
2900 return result;
2901
2902 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2903 }
2904 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2905 || (filedata->file_header.e_machine == EM_PARISC
2906 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2907 {
2908 const char * result;
2909
2910 switch (filedata->file_header.e_machine)
2911 {
2912 case EM_PARISC:
2913 result = get_parisc_dynamic_type (type);
2914 break;
2915 case EM_IA_64:
2916 result = get_ia64_dynamic_type (type);
2917 break;
2918 default:
2919 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2920 result = get_solaris_dynamic_type (type);
2921 else
2922 result = NULL;
2923 break;
2924 }
2925
2926 if (result != NULL)
2927 return result;
2928
2929 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2930 type);
2931 }
2932 else
2933 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2934
2935 return buff;
2936 }
2937}
2938
2939static bool get_program_headers (Filedata *);
2940static bool get_dynamic_section (Filedata *);
2941
2942static void
2943locate_dynamic_section (Filedata *filedata)
2944{
2945 uint64_t dynamic_addr = 0;
2946 uint64_t dynamic_size = 0;
2947
2948 if (filedata->file_header.e_phnum != 0
2949 && get_program_headers (filedata))
2950 {
2951 Elf_Internal_Phdr *segment;
2952 unsigned int i;
2953
2954 for (i = 0, segment = filedata->program_headers;
2955 i < filedata->file_header.e_phnum;
2956 i++, segment++)
2957 {
2958 if (segment->p_type == PT_DYNAMIC)
2959 {
2960 dynamic_addr = segment->p_offset;
2961 dynamic_size = segment->p_filesz;
2962
2963 if (filedata->section_headers != NULL)
2964 {
2965 Elf_Internal_Shdr *sec;
2966
2967 sec = find_section (filedata, ".dynamic");
2968 if (sec != NULL)
2969 {
2970 if (sec->sh_size == 0
2971 || sec->sh_type == SHT_NOBITS)
2972 {
2973 dynamic_addr = 0;
2974 dynamic_size = 0;
2975 }
2976 else
2977 {
2978 dynamic_addr = sec->sh_offset;
2979 dynamic_size = sec->sh_size;
2980 }
2981 }
2982 }
2983
2984 if (dynamic_addr > filedata->file_size
2985 || (dynamic_size > filedata->file_size - dynamic_addr))
2986 {
2987 dynamic_addr = 0;
2988 dynamic_size = 0;
2989 }
2990 break;
2991 }
2992 }
2993 }
2994 filedata->dynamic_addr = dynamic_addr;
2995 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
2996}
2997
2998static bool
2999is_pie (Filedata *filedata)
3000{
3001 Elf_Internal_Dyn *entry;
3002
3003 if (filedata->dynamic_size == 0)
3004 locate_dynamic_section (filedata);
3005 if (filedata->dynamic_size <= 1)
3006 return false;
3007
3008 if (!get_dynamic_section (filedata))
3009 return false;
3010
3011 for (entry = filedata->dynamic_section;
3012 entry < filedata->dynamic_section + filedata->dynamic_nent;
3013 entry++)
3014 {
3015 if (entry->d_tag == DT_FLAGS_1)
3016 {
3017 if ((entry->d_un.d_val & DF_1_PIE) != 0)
3018 return true;
3019 break;
3020 }
3021 }
3022 return false;
3023}
3024
3025static char *
3026get_file_type (Filedata *filedata)
3027{
3028 unsigned e_type = filedata->file_header.e_type;
3029 static char buff[64];
3030
3031 switch (e_type)
3032 {
3033 case ET_NONE: return _("NONE (None)");
3034 case ET_REL: return _("REL (Relocatable file)");
3035 case ET_EXEC: return _("EXEC (Executable file)");
3036 case ET_DYN:
3037 if (is_pie (filedata))
3038 return _("DYN (Position-Independent Executable file)");
3039 else
3040 return _("DYN (Shared object file)");
3041 case ET_CORE: return _("CORE (Core file)");
3042
3043 default:
3044 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
3045 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
3046 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
3047 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
3048 else
3049 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
3050 return buff;
3051 }
3052}
3053
3054static char *
3055get_machine_name (unsigned e_machine)
3056{
3057 static char buff[64]; /* XXX */
3058
3059 switch (e_machine)
3060 {
3061 /* Please keep this switch table sorted by increasing EM_ value. */
3062 /* 0 */
3063 case EM_NONE: return _("None");
3064 case EM_M32: return "WE32100";
3065 case EM_SPARC: return "Sparc";
3066 case EM_386: return "Intel 80386";
3067 case EM_68K: return "MC68000";
3068 case EM_88K: return "MC88000";
3069 case EM_IAMCU: return "Intel MCU";
3070 case EM_860: return "Intel 80860";
3071 case EM_MIPS: return "MIPS R3000";
3072 case EM_S370: return "IBM System/370";
3073 /* 10 */
3074 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
3075 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
3076 case EM_PARISC: return "HPPA";
3077 case EM_VPP550: return "Fujitsu VPP500";
3078 case EM_SPARC32PLUS: return "Sparc v8+" ;
3079 case EM_960: return "Intel 80960";
3080 case EM_PPC: return "PowerPC";
3081 /* 20 */
3082 case EM_PPC64: return "PowerPC64";
3083 case EM_S390_OLD:
3084 case EM_S390: return "IBM S/390";
3085 case EM_SPU: return "SPU";
3086 /* 30 */
3087 case EM_V800: return "Renesas V850 (using RH850 ABI)";
3088 case EM_FR20: return "Fujitsu FR20";
3089 case EM_RH32: return "TRW RH32";
3090 case EM_MCORE: return "MCORE";
3091 /* 40 */
3092 case EM_ARM: return "ARM";
3093 case EM_OLD_ALPHA: return "Digital Alpha (old)";
3094 case EM_SH: return "Renesas / SuperH SH";
3095 case EM_SPARCV9: return "Sparc v9";
3096 case EM_TRICORE: return "Siemens Tricore";
3097 case EM_ARC: return "ARC";
3098 case EM_H8_300: return "Renesas H8/300";
3099 case EM_H8_300H: return "Renesas H8/300H";
3100 case EM_H8S: return "Renesas H8S";
3101 case EM_H8_500: return "Renesas H8/500";
3102 /* 50 */
3103 case EM_IA_64: return "Intel IA-64";
3104 case EM_MIPS_X: return "Stanford MIPS-X";
3105 case EM_COLDFIRE: return "Motorola Coldfire";
3106 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
3107 case EM_MMA: return "Fujitsu Multimedia Accelerator";
3108 case EM_PCP: return "Siemens PCP";
3109 case EM_NCPU: return "Sony nCPU embedded RISC processor";
3110 case EM_NDR1: return "Denso NDR1 microprocessor";
3111 case EM_STARCORE: return "Motorola Star*Core processor";
3112 case EM_ME16: return "Toyota ME16 processor";
3113 /* 60 */
3114 case EM_ST100: return "STMicroelectronics ST100 processor";
3115 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
3116 case EM_X86_64: return "Advanced Micro Devices X86-64";
3117 case EM_PDSP: return "Sony DSP processor";
3118 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
3119 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
3120 case EM_FX66: return "Siemens FX66 microcontroller";
3121 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
3122 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
3123 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
3124 /* 70 */
3125 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
3126 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
3127 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
3128 case EM_SVX: return "Silicon Graphics SVx";
3129 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
3130 case EM_VAX: return "Digital VAX";
3131 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
3132 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
3133 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
3134 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
3135 /* 80 */
3136 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
3137 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
3138 case EM_PRISM: return "Vitesse Prism";
3139 case EM_AVR_OLD:
3140 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
3141 case EM_CYGNUS_FR30:
3142 case EM_FR30: return "Fujitsu FR30";
3143 case EM_CYGNUS_D10V:
3144 case EM_D10V: return "d10v";
3145 case EM_CYGNUS_D30V:
3146 case EM_D30V: return "d30v";
3147 case EM_CYGNUS_V850:
3148 case EM_V850: return "Renesas V850";
3149 case EM_CYGNUS_M32R:
3150 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
3151 case EM_CYGNUS_MN10300:
3152 case EM_MN10300: return "mn10300";
3153 /* 90 */
3154 case EM_CYGNUS_MN10200:
3155 case EM_MN10200: return "mn10200";
3156 case EM_PJ: return "picoJava";
3157 case EM_OR1K: return "OpenRISC 1000";
3158 case EM_ARC_COMPACT: return "ARCompact";
3159 case EM_XTENSA_OLD:
3160 case EM_XTENSA: return "Tensilica Xtensa Processor";
3161 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
3162 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
3163 case EM_NS32K: return "National Semiconductor 32000 series";
3164 case EM_TPC: return "Tenor Network TPC processor";
3165 case EM_SNP1K: return "Trebia SNP 1000 processor";
3166 /* 100 */
3167 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
3168 case EM_IP2K_OLD:
3169 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
3170 case EM_MAX: return "MAX Processor";
3171 case EM_CR: return "National Semiconductor CompactRISC";
3172 case EM_F2MC16: return "Fujitsu F2MC16";
3173 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
3174 case EM_BLACKFIN: return "Analog Devices Blackfin";
3175 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
3176 case EM_SEP: return "Sharp embedded microprocessor";
3177 case EM_ARCA: return "Arca RISC microprocessor";
3178 /* 110 */
3179 case EM_UNICORE: return "Unicore";
3180 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
3181 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
3182 case EM_ALTERA_NIOS2: return "Altera Nios II";
3183 case EM_CRX: return "National Semiconductor CRX microprocessor";
3184 case EM_XGATE: return "Motorola XGATE embedded processor";
3185 case EM_C166:
3186 case EM_XC16X: return "Infineon Technologies xc16x";
3187 case EM_M16C: return "Renesas M16C series microprocessors";
3188 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
3189 case EM_CE: return "Freescale Communication Engine RISC core";
3190 /* 120 */
3191 case EM_M32C: return "Renesas M32c";
3192 /* 130 */
3193 case EM_TSK3000: return "Altium TSK3000 core";
3194 case EM_RS08: return "Freescale RS08 embedded processor";
3195 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
3196 case EM_SCORE: return "SUNPLUS S+Core";
3197 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
3198 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
3199 case EM_LATTICEMICO32: return "Lattice Mico32";
3200 case EM_SE_C17: return "Seiko Epson C17 family";
3201 /* 140 */
3202 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
3203 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
3204 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
3205 case EM_TI_PRU: return "TI PRU I/O processor";
3206 /* 160 */
3207 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
3208 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
3209 case EM_R32C: return "Renesas R32C series microprocessors";
3210 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
3211 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
3212 case EM_8051: return "Intel 8051 and variants";
3213 case EM_STXP7X: return "STMicroelectronics STxP7x family";
3214 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
3215 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
3216 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
3217 /* 170 */
3218 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
3219 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
3220 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
3221 case EM_RX: return "Renesas RX";
3222 case EM_METAG: return "Imagination Technologies Meta processor architecture";
3223 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
3224 case EM_ECOG16: return "Cyan Technology eCOG16 family";
3225 case EM_CR16:
3226 case EM_MICROBLAZE:
3227 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
3228 case EM_ETPU: return "Freescale Extended Time Processing Unit";
3229 case EM_SLE9X: return "Infineon Technologies SLE9X core";
3230 /* 180 */
3231 case EM_L1OM: return "Intel L1OM";
3232 case EM_K1OM: return "Intel K1OM";
3233 case EM_INTEL182: return "Intel (reserved)";
3234 case EM_AARCH64: return "AArch64";
3235 case EM_ARM184: return "ARM (reserved)";
3236 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
3237 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
3238 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
3239 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
3240 /* 190 */
3241 case EM_CUDA: return "NVIDIA CUDA architecture";
3242 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
3243 case EM_CLOUDSHIELD: return "CloudShield architecture family";
3244 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
3245 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
3246 case EM_ARC_COMPACT2: return "ARCv2";
3247 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
3248 case EM_RL78: return "Renesas RL78";
3249 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
3250 case EM_78K0R: return "Renesas 78K0R";
3251 /* 200 */
3252 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
3253 case EM_BA1: return "Beyond BA1 CPU architecture";
3254 case EM_BA2: return "Beyond BA2 CPU architecture";
3255 case EM_XCORE: return "XMOS xCORE processor family";
3256 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
3257 case EM_INTELGT: return "Intel Graphics Technology";
3258 /* 210 */
3259 case EM_KM32: return "KM211 KM32 32-bit processor";
3260 case EM_KMX32: return "KM211 KMX32 32-bit processor";
3261 case EM_KMX16: return "KM211 KMX16 16-bit processor";
3262 case EM_KMX8: return "KM211 KMX8 8-bit processor";
3263 case EM_KVARC: return "KM211 KVARC processor";
3264 case EM_CDP: return "Paneve CDP architecture family";
3265 case EM_COGE: return "Cognitive Smart Memory Processor";
3266 case EM_COOL: return "Bluechip Systems CoolEngine";
3267 case EM_NORC: return "Nanoradio Optimized RISC";
3268 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
3269 /* 220 */
3270 case EM_Z80: return "Zilog Z80";
3271 case EM_VISIUM: return "CDS VISIUMcore processor";
3272 case EM_FT32: return "FTDI Chip FT32";
3273 case EM_MOXIE: return "Moxie";
3274 case EM_AMDGPU: return "AMD GPU";
3275 /* 230 (all reserved) */
3276 /* 240 */
3277 case EM_RISCV: return "RISC-V";
3278 case EM_LANAI: return "Lanai 32-bit processor";
3279 case EM_CEVA: return "CEVA Processor Architecture Family";
3280 case EM_CEVA_X2: return "CEVA X2 Processor Family";
3281 case EM_BPF: return "Linux BPF";
3282 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
3283 case EM_IMG1: return "Imagination Technologies";
3284 /* 250 */
3285 case EM_NFP: return "Netronome Flow Processor";
3286 case EM_VE: return "NEC Vector Engine";
3287 case EM_CSKY: return "C-SKY";
3288 case EM_ARC_COMPACT3_64: return "Synopsys ARCv3 64-bit processor";
3289 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
3290 case EM_ARC_COMPACT3: return "Synopsys ARCv3 32-bit processor";
3291 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
3292 case EM_65816: return "WDC 65816/65C816";
3293 case EM_LOONGARCH: return "LoongArch";
3294 case EM_KF32: return "ChipON KungFu32";
3295
3296 /* Large numbers... */
3297 case EM_MT: return "Morpho Techologies MT processor";
3298 case EM_ALPHA: return "Alpha";
3299 case EM_WEBASSEMBLY: return "Web Assembly";
3300 case EM_DLX: return "OpenDLX";
3301 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
3302 case EM_IQ2000: return "Vitesse IQ2000";
3303 case EM_M32C_OLD:
3304 case EM_NIOS32: return "Altera Nios";
3305 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
3306 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
3307 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
3308 case EM_S12Z: return "Freescale S12Z";
3309
3310 default:
3311 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
3312 return buff;
3313 }
3314}
3315
3316static char *
3317decode_ARC_machine_flags (char *out, unsigned e_flags, unsigned e_machine)
3318{
3319 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
3320 other compilers don't specify an architecture type in the e_flags, and
3321 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
3322 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
3323 architectures.
3324
3325 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
3326 but also sets a specific architecture type in the e_flags field.
3327
3328 However, when decoding the flags we don't worry if we see an
3329 unexpected pairing, for example EM_ARC_COMPACT machine type, with
3330 ARCEM architecture type. */
3331
3332 switch (e_flags & EF_ARC_MACH_MSK)
3333 {
3334 /* We only expect these to occur for EM_ARC_COMPACT2. */
3335 case EF_ARC_CPU_ARCV2EM:
3336 out = stpcpy (out, ", ARC EM");
3337 break;
3338 case EF_ARC_CPU_ARCV2HS:
3339 out = stpcpy (out, ", ARC HS");
3340 break;
3341
3342 /* We only expect these to occur for EM_ARC_COMPACT. */
3343 case E_ARC_MACH_ARC600:
3344 out = stpcpy (out, ", ARC600");
3345 break;
3346 case E_ARC_MACH_ARC601:
3347 out = stpcpy (out, ", ARC601");
3348 break;
3349 case E_ARC_MACH_ARC700:
3350 out = stpcpy (out, ", ARC700");
3351 break;
3352
3353 /* The only times we should end up here are (a) A corrupt ELF, (b) A
3354 new ELF with new architecture being read by an old version of
3355 readelf, or (c) An ELF built with non-GNU compiler that does not
3356 set the architecture in the e_flags. */
3357 default:
3358 if (e_machine == EM_ARC_COMPACT)
3359 out = stpcpy (out, ", Unknown ARCompact");
3360 else
3361 out = stpcpy (out, ", Unknown ARC");
3362 break;
3363 }
3364
3365 switch (e_flags & EF_ARC_OSABI_MSK)
3366 {
3367 case E_ARC_OSABI_ORIG:
3368 out = stpcpy (out, ", (ABI:legacy)");
3369 break;
3370 case E_ARC_OSABI_V2:
3371 out = stpcpy (out, ", (ABI:v2)");
3372 break;
3373 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
3374 case E_ARC_OSABI_V3:
3375 out = stpcpy (out, ", v3 no-legacy-syscalls ABI");
3376 break;
3377 case E_ARC_OSABI_V4:
3378 out = stpcpy (out, ", v4 ABI");
3379 break;
3380 default:
3381 out = stpcpy (out, ", unrecognised ARC OSABI flag");
3382 break;
3383 }
3384 return out;
3385}
3386
3387static char *
3388decode_ARM_machine_flags (char *out, unsigned e_flags)
3389{
3390 unsigned eabi;
3391 bool unknown = false;
3392
3393 eabi = EF_ARM_EABI_VERSION (e_flags);
3394 e_flags &= ~ EF_ARM_EABIMASK;
3395
3396 /* Handle "generic" ARM flags. */
3397 if (e_flags & EF_ARM_RELEXEC)
3398 {
3399 out = stpcpy (out, ", relocatable executable");
3400 e_flags &= ~ EF_ARM_RELEXEC;
3401 }
3402
3403 if (e_flags & EF_ARM_PIC)
3404 {
3405 out = stpcpy (out, ", position independent");
3406 e_flags &= ~ EF_ARM_PIC;
3407 }
3408
3409 /* Now handle EABI specific flags. */
3410 switch (eabi)
3411 {
3412 default:
3413 out = stpcpy (out, ", <unrecognized EABI>");
3414 if (e_flags)
3415 unknown = true;
3416 break;
3417
3418 case EF_ARM_EABI_VER1:
3419 out = stpcpy (out, ", Version1 EABI");
3420 while (e_flags)
3421 {
3422 unsigned flag;
3423
3424 /* Process flags one bit at a time. */
3425 flag = e_flags & - e_flags;
3426 e_flags &= ~ flag;
3427
3428 switch (flag)
3429 {
3430 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3431 out = stpcpy (out, ", sorted symbol tables");
3432 break;
3433
3434 default:
3435 unknown = true;
3436 break;
3437 }
3438 }
3439 break;
3440
3441 case EF_ARM_EABI_VER2:
3442 out = stpcpy (out, ", Version2 EABI");
3443 while (e_flags)
3444 {
3445 unsigned flag;
3446
3447 /* Process flags one bit at a time. */
3448 flag = e_flags & - e_flags;
3449 e_flags &= ~ flag;
3450
3451 switch (flag)
3452 {
3453 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3454 out = stpcpy (out, ", sorted symbol tables");
3455 break;
3456
3457 case EF_ARM_DYNSYMSUSESEGIDX:
3458 out = stpcpy (out, ", dynamic symbols use segment index");
3459 break;
3460
3461 case EF_ARM_MAPSYMSFIRST:
3462 out = stpcpy (out, ", mapping symbols precede others");
3463 break;
3464
3465 default:
3466 unknown = true;
3467 break;
3468 }
3469 }
3470 break;
3471
3472 case EF_ARM_EABI_VER3:
3473 out = stpcpy (out, ", Version3 EABI");
3474 break;
3475
3476 case EF_ARM_EABI_VER4:
3477 out = stpcpy (out, ", Version4 EABI");
3478 while (e_flags)
3479 {
3480 unsigned flag;
3481
3482 /* Process flags one bit at a time. */
3483 flag = e_flags & - e_flags;
3484 e_flags &= ~ flag;
3485
3486 switch (flag)
3487 {
3488 case EF_ARM_BE8:
3489 out = stpcpy (out, ", BE8");
3490 break;
3491
3492 case EF_ARM_LE8:
3493 out = stpcpy (out, ", LE8");
3494 break;
3495
3496 default:
3497 unknown = true;
3498 break;
3499 }
3500 }
3501 break;
3502
3503 case EF_ARM_EABI_VER5:
3504 out = stpcpy (out, ", Version5 EABI");
3505 while (e_flags)
3506 {
3507 unsigned flag;
3508
3509 /* Process flags one bit at a time. */
3510 flag = e_flags & - e_flags;
3511 e_flags &= ~ flag;
3512
3513 switch (flag)
3514 {
3515 case EF_ARM_BE8:
3516 out = stpcpy (out, ", BE8");
3517 break;
3518
3519 case EF_ARM_LE8:
3520 out = stpcpy (out, ", LE8");
3521 break;
3522
3523 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
3524 out = stpcpy (out, ", soft-float ABI");
3525 break;
3526
3527 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
3528 out = stpcpy (out, ", hard-float ABI");
3529 break;
3530
3531 default:
3532 unknown = true;
3533 break;
3534 }
3535 }
3536 break;
3537
3538 case EF_ARM_EABI_UNKNOWN:
3539 out = stpcpy (out, ", GNU EABI");
3540 while (e_flags)
3541 {
3542 unsigned flag;
3543
3544 /* Process flags one bit at a time. */
3545 flag = e_flags & - e_flags;
3546 e_flags &= ~ flag;
3547
3548 switch (flag)
3549 {
3550 case EF_ARM_INTERWORK:
3551 out = stpcpy (out, ", interworking enabled");
3552 break;
3553
3554 case EF_ARM_APCS_26:
3555 out = stpcpy (out, ", uses APCS/26");
3556 break;
3557
3558 case EF_ARM_APCS_FLOAT:
3559 out = stpcpy (out, ", uses APCS/float");
3560 break;
3561
3562 case EF_ARM_PIC:
3563 out = stpcpy (out, ", position independent");
3564 break;
3565
3566 case EF_ARM_ALIGN8:
3567 out = stpcpy (out, ", 8 bit structure alignment");
3568 break;
3569
3570 case EF_ARM_NEW_ABI:
3571 out = stpcpy (out, ", uses new ABI");
3572 break;
3573
3574 case EF_ARM_OLD_ABI:
3575 out = stpcpy (out, ", uses old ABI");
3576 break;
3577
3578 case EF_ARM_SOFT_FLOAT:
3579 out = stpcpy (out, ", software FP");
3580 break;
3581
3582 case EF_ARM_VFP_FLOAT:
3583 out = stpcpy (out, ", VFP");
3584 break;
3585
3586 default:
3587 unknown = true;
3588 break;
3589 }
3590 }
3591 }
3592
3593 if (unknown)
3594 out = stpcpy (out,_(", <unknown>"));
3595 return out;
3596}
3597
3598static char *
3599decode_AVR_machine_flags (char *out, unsigned e_flags)
3600{
3601 switch (e_flags & EF_AVR_MACH)
3602 {
3603 case E_AVR_MACH_AVR1:
3604 out = stpcpy (out, ", avr:1");
3605 break;
3606 case E_AVR_MACH_AVR2:
3607 out = stpcpy (out, ", avr:2");
3608 break;
3609 case E_AVR_MACH_AVR25:
3610 out = stpcpy (out, ", avr:25");
3611 break;
3612 case E_AVR_MACH_AVR3:
3613 out = stpcpy (out, ", avr:3");
3614 break;
3615 case E_AVR_MACH_AVR31:
3616 out = stpcpy (out, ", avr:31");
3617 break;
3618 case E_AVR_MACH_AVR35:
3619 out = stpcpy (out, ", avr:35");
3620 break;
3621 case E_AVR_MACH_AVR4:
3622 out = stpcpy (out, ", avr:4");
3623 break;
3624 case E_AVR_MACH_AVR5:
3625 out = stpcpy (out, ", avr:5");
3626 break;
3627 case E_AVR_MACH_AVR51:
3628 out = stpcpy (out, ", avr:51");
3629 break;
3630 case E_AVR_MACH_AVR6:
3631 out = stpcpy (out, ", avr:6");
3632 break;
3633 case E_AVR_MACH_AVRTINY:
3634 out = stpcpy (out, ", avr:100");
3635 break;
3636 case E_AVR_MACH_XMEGA1:
3637 out = stpcpy (out, ", avr:101");
3638 break;
3639 case E_AVR_MACH_XMEGA2:
3640 out = stpcpy (out, ", avr:102");
3641 break;
3642 case E_AVR_MACH_XMEGA3:
3643 out = stpcpy (out, ", avr:103");
3644 break;
3645 case E_AVR_MACH_XMEGA4:
3646 out = stpcpy (out, ", avr:104");
3647 break;
3648 case E_AVR_MACH_XMEGA5:
3649 out = stpcpy (out, ", avr:105");
3650 break;
3651 case E_AVR_MACH_XMEGA6:
3652 out = stpcpy (out, ", avr:106");
3653 break;
3654 case E_AVR_MACH_XMEGA7:
3655 out = stpcpy (out, ", avr:107");
3656 break;
3657 default:
3658 out = stpcpy (out, ", avr:<unknown>");
3659 break;
3660 }
3661
3662 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
3663 out = stpcpy (out, ", link-relax");
3664 return out;
3665}
3666
3667static char *
3668decode_BLACKFIN_machine_flags (char *out, unsigned e_flags)
3669{
3670 if (e_flags & EF_BFIN_PIC)
3671 out = stpcpy (out, ", PIC");
3672
3673 if (e_flags & EF_BFIN_FDPIC)
3674 out = stpcpy (out, ", FDPIC");
3675
3676 if (e_flags & EF_BFIN_CODE_IN_L1)
3677 out = stpcpy (out, ", code in L1");
3678
3679 if (e_flags & EF_BFIN_DATA_IN_L1)
3680 out = stpcpy (out, ", data in L1");
3681 return out;
3682}
3683
3684static char *
3685decode_FRV_machine_flags (char *out, unsigned e_flags)
3686{
3687 switch (e_flags & EF_FRV_CPU_MASK)
3688 {
3689 case EF_FRV_CPU_GENERIC:
3690 break;
3691
3692 default:
3693 out = stpcpy (out, ", fr???");
3694 break;
3695
3696 case EF_FRV_CPU_FR300:
3697 out = stpcpy (out, ", fr300");
3698 break;
3699
3700 case EF_FRV_CPU_FR400:
3701 out = stpcpy (out, ", fr400");
3702 break;
3703 case EF_FRV_CPU_FR405:
3704 out = stpcpy (out, ", fr405");
3705 break;
3706
3707 case EF_FRV_CPU_FR450:
3708 out = stpcpy (out, ", fr450");
3709 break;
3710
3711 case EF_FRV_CPU_FR500:
3712 out = stpcpy (out, ", fr500");
3713 break;
3714 case EF_FRV_CPU_FR550:
3715 out = stpcpy (out, ", fr550");
3716 break;
3717
3718 case EF_FRV_CPU_SIMPLE:
3719 out = stpcpy (out, ", simple");
3720 break;
3721 case EF_FRV_CPU_TOMCAT:
3722 out = stpcpy (out, ", tomcat");
3723 break;
3724 }
3725 return out;
3726}
3727
3728static char *
3729decode_IA64_machine_flags (char *out, unsigned e_flags, Filedata *filedata)
3730{
3731 if ((e_flags & EF_IA_64_ABI64))
3732 out = stpcpy (out, ", 64-bit");
3733 else
3734 out = stpcpy (out, ", 32-bit");
3735 if ((e_flags & EF_IA_64_REDUCEDFP))
3736 out = stpcpy (out, ", reduced fp model");
3737 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3738 out = stpcpy (out, ", no function descriptors, constant gp");
3739 else if ((e_flags & EF_IA_64_CONS_GP))
3740 out = stpcpy (out, ", constant gp");
3741 if ((e_flags & EF_IA_64_ABSOLUTE))
3742 out = stpcpy (out, ", absolute");
3743 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3744 {
3745 if ((e_flags & EF_IA_64_VMS_LINKAGES))
3746 out = stpcpy (out, ", vms_linkages");
3747 switch ((e_flags & EF_IA_64_VMS_COMCOD))
3748 {
3749 case EF_IA_64_VMS_COMCOD_SUCCESS:
3750 break;
3751 case EF_IA_64_VMS_COMCOD_WARNING:
3752 out = stpcpy (out, ", warning");
3753 break;
3754 case EF_IA_64_VMS_COMCOD_ERROR:
3755 out = stpcpy (out, ", error");
3756 break;
3757 case EF_IA_64_VMS_COMCOD_ABORT:
3758 out = stpcpy (out, ", abort");
3759 break;
3760 default:
3761 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
3762 e_flags & EF_IA_64_VMS_COMCOD);
3763 out = stpcpy (out, ", <unknown>");
3764 }
3765 }
3766 return out;
3767}
3768
3769static char *
3770decode_LOONGARCH_machine_flags (char *out, unsigned int e_flags)
3771{
3772 if (EF_LOONGARCH_IS_SOFT_FLOAT (e_flags))
3773 out = stpcpy (out, ", SOFT-FLOAT");
3774 else if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
3775 out = stpcpy (out, ", SINGLE-FLOAT");
3776 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
3777 out = stpcpy (out, ", DOUBLE-FLOAT");
3778
3779 if (EF_LOONGARCH_IS_OBJ_V0 (e_flags))
3780 out = stpcpy (out, ", OBJ-v0");
3781 else if (EF_LOONGARCH_IS_OBJ_V1 (e_flags))
3782 out = stpcpy (out, ", OBJ-v1");
3783 return out;
3784}
3785
3786static char *
3787decode_M68K_machine_flags (char *out, unsigned int e_flags)
3788{
3789 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3790 out = stpcpy (out, ", m68000");
3791 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3792 out = stpcpy (out, ", cpu32");
3793 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3794 out = stpcpy (out, ", fido_a");
3795 else
3796 {
3797 char const *isa = _("unknown");
3798 char const *mac = _("unknown mac");
3799 char const *additional = NULL;
3800
3801 switch (e_flags & EF_M68K_CF_ISA_MASK)
3802 {
3803 case EF_M68K_CF_ISA_A_NODIV:
3804 isa = "A";
3805 additional = ", nodiv";
3806 break;
3807 case EF_M68K_CF_ISA_A:
3808 isa = "A";
3809 break;
3810 case EF_M68K_CF_ISA_A_PLUS:
3811 isa = "A+";
3812 break;
3813 case EF_M68K_CF_ISA_B_NOUSP:
3814 isa = "B";
3815 additional = ", nousp";
3816 break;
3817 case EF_M68K_CF_ISA_B:
3818 isa = "B";
3819 break;
3820 case EF_M68K_CF_ISA_C:
3821 isa = "C";
3822 break;
3823 case EF_M68K_CF_ISA_C_NODIV:
3824 isa = "C";
3825 additional = ", nodiv";
3826 break;
3827 }
3828 out = stpcpy (out, ", cf, isa ");
3829 out = stpcpy (out, isa);
3830 if (additional)
3831 out = stpcpy (out, additional);
3832 if (e_flags & EF_M68K_CF_FLOAT)
3833 out = stpcpy (out, ", float");
3834 switch (e_flags & EF_M68K_CF_MAC_MASK)
3835 {
3836 case 0:
3837 mac = NULL;
3838 break;
3839 case EF_M68K_CF_MAC:
3840 mac = "mac";
3841 break;
3842 case EF_M68K_CF_EMAC:
3843 mac = "emac";
3844 break;
3845 case EF_M68K_CF_EMAC_B:
3846 mac = "emac_b";
3847 break;
3848 }
3849 if (mac)
3850 {
3851 out = stpcpy (out, ", ");
3852 out = stpcpy (out, mac);
3853 }
3854 }
3855 return out;
3856}
3857
3858static char *
3859decode_MeP_machine_flags (char *out, unsigned int e_flags)
3860{
3861 switch (e_flags & EF_MEP_CPU_MASK)
3862 {
3863 case EF_MEP_CPU_MEP:
3864 out = stpcpy (out, ", generic MeP");
3865 break;
3866 case EF_MEP_CPU_C2:
3867 out = stpcpy (out, ", MeP C2");
3868 break;
3869 case EF_MEP_CPU_C3:
3870 out = stpcpy (out, ", MeP C3");
3871 break;
3872 case EF_MEP_CPU_C4:
3873 out = stpcpy (out, ", MeP C4");
3874 break;
3875 case EF_MEP_CPU_C5:
3876 out = stpcpy (out, ", MeP C5");
3877 break;
3878 case EF_MEP_CPU_H1:
3879 out = stpcpy (out, ", MeP H1");
3880 break;
3881 default:
3882 out = stpcpy (out, _(", <unknown MeP cpu type>"));
3883 break;
3884 }
3885
3886 switch (e_flags & EF_MEP_COP_MASK)
3887 {
3888 case EF_MEP_COP_NONE:
3889 break;
3890 case EF_MEP_COP_AVC:
3891 out = stpcpy (out, ", AVC coprocessor");
3892 break;
3893 case EF_MEP_COP_AVC2:
3894 out = stpcpy (out, ", AVC2 coprocessor");
3895 break;
3896 case EF_MEP_COP_FMAX:
3897 out = stpcpy (out, ", FMAX coprocessor");
3898 break;
3899 case EF_MEP_COP_IVC2:
3900 out = stpcpy (out, ", IVC2 coprocessor");
3901 break;
3902 default:
3903 out = stpcpy (out, _("<unknown MeP copro type>"));
3904 break;
3905 }
3906
3907 if (e_flags & EF_MEP_LIBRARY)
3908 out = stpcpy (out, ", Built for Library");
3909
3910 if (e_flags & EF_MEP_INDEX_MASK)
3911 out += sprintf (out, ", Configuration Index: %#x",
3912 e_flags & EF_MEP_INDEX_MASK);
3913
3914 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3915 out += sprintf (out, _(", unknown flags bits: %#x"),
3916 e_flags & ~ EF_MEP_ALL_FLAGS);
3917 return out;
3918}
3919
3920static char *
3921decode_MIPS_machine_flags (char *out, unsigned int e_flags)
3922{
3923 if (e_flags & EF_MIPS_NOREORDER)
3924 out = stpcpy (out, ", noreorder");
3925
3926 if (e_flags & EF_MIPS_PIC)
3927 out = stpcpy (out, ", pic");
3928
3929 if (e_flags & EF_MIPS_CPIC)
3930 out = stpcpy (out, ", cpic");
3931
3932 if (e_flags & EF_MIPS_UCODE)
3933 out = stpcpy (out, ", ugen_reserved");
3934
3935 if (e_flags & EF_MIPS_ABI2)
3936 out = stpcpy (out, ", abi2");
3937
3938 if (e_flags & EF_MIPS_OPTIONS_FIRST)
3939 out = stpcpy (out, ", odk first");
3940
3941 if (e_flags & EF_MIPS_32BITMODE)
3942 out = stpcpy (out, ", 32bitmode");
3943
3944 if (e_flags & EF_MIPS_NAN2008)
3945 out = stpcpy (out, ", nan2008");
3946
3947 if (e_flags & EF_MIPS_FP64)
3948 out = stpcpy (out, ", fp64");
3949
3950 switch ((e_flags & EF_MIPS_MACH))
3951 {
3952 case EF_MIPS_MACH_3900:
3953 out = stpcpy (out, ", 3900");
3954 break;
3955 case EF_MIPS_MACH_4010:
3956 out = stpcpy (out, ", 4010");
3957 break;
3958 case EF_MIPS_MACH_4100:
3959 out = stpcpy (out, ", 4100");
3960 break;
3961 case EF_MIPS_MACH_4111:
3962 out = stpcpy (out, ", 4111");
3963 break;
3964 case EF_MIPS_MACH_4120:
3965 out = stpcpy (out, ", 4120");
3966 break;
3967 case EF_MIPS_MACH_4650:
3968 out = stpcpy (out, ", 4650");
3969 break;
3970 case EF_MIPS_MACH_5400:
3971 out = stpcpy (out, ", 5400");
3972 break;
3973 case EF_MIPS_MACH_5500:
3974 out = stpcpy (out, ", 5500");
3975 break;
3976 case EF_MIPS_MACH_5900:
3977 out = stpcpy (out, ", 5900");
3978 break;
3979 case EF_MIPS_MACH_SB1:
3980 out = stpcpy (out, ", sb1");
3981 break;
3982 case EF_MIPS_MACH_9000:
3983 out = stpcpy (out, ", 9000");
3984 break;
3985 case EF_MIPS_MACH_LS2E:
3986 out = stpcpy (out, ", loongson-2e");
3987 break;
3988 case EF_MIPS_MACH_LS2F:
3989 out = stpcpy (out, ", loongson-2f");
3990 break;
3991 case EF_MIPS_MACH_GS464:
3992 out = stpcpy (out, ", gs464");
3993 break;
3994 case EF_MIPS_MACH_GS464E:
3995 out = stpcpy (out, ", gs464e");
3996 break;
3997 case EF_MIPS_MACH_GS264E:
3998 out = stpcpy (out, ", gs264e");
3999 break;
4000 case EF_MIPS_MACH_OCTEON:
4001 out = stpcpy (out, ", octeon");
4002 break;
4003 case EF_MIPS_MACH_OCTEON2:
4004 out = stpcpy (out, ", octeon2");
4005 break;
4006 case EF_MIPS_MACH_OCTEON3:
4007 out = stpcpy (out, ", octeon3");
4008 break;
4009 case EF_MIPS_MACH_XLR:
4010 out = stpcpy (out, ", xlr");
4011 break;
4012 case EF_MIPS_MACH_IAMR2:
4013 out = stpcpy (out, ", interaptiv-mr2");
4014 break;
4015 case EF_MIPS_MACH_ALLEGREX:
4016 out = stpcpy (out, ", allegrex");
4017 break;
4018 case 0:
4019 /* We simply ignore the field in this case to avoid confusion:
4020 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
4021 extension. */
4022 break;
4023 default:
4024 out = stpcpy (out, _(", unknown CPU"));
4025 break;
4026 }
4027
4028 switch ((e_flags & EF_MIPS_ABI))
4029 {
4030 case EF_MIPS_ABI_O32:
4031 out = stpcpy (out, ", o32");
4032 break;
4033 case EF_MIPS_ABI_O64:
4034 out = stpcpy (out, ", o64");
4035 break;
4036 case EF_MIPS_ABI_EABI32:
4037 out = stpcpy (out, ", eabi32");
4038 break;
4039 case EF_MIPS_ABI_EABI64:
4040 out = stpcpy (out, ", eabi64");
4041 break;
4042 case 0:
4043 /* We simply ignore the field in this case to avoid confusion:
4044 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
4045 This means it is likely to be an o32 file, but not for
4046 sure. */
4047 break;
4048 default:
4049 out = stpcpy (out, _(", unknown ABI"));
4050 break;
4051 }
4052
4053 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
4054 out = stpcpy (out, ", mdmx");
4055
4056 if (e_flags & EF_MIPS_ARCH_ASE_M16)
4057 out = stpcpy (out, ", mips16");
4058
4059 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
4060 out = stpcpy (out, ", micromips");
4061
4062 switch ((e_flags & EF_MIPS_ARCH))
4063 {
4064 case EF_MIPS_ARCH_1:
4065 out = stpcpy (out, ", mips1");
4066 break;
4067 case EF_MIPS_ARCH_2:
4068 out = stpcpy (out, ", mips2");
4069 break;
4070 case EF_MIPS_ARCH_3:
4071 out = stpcpy (out, ", mips3");
4072 break;
4073 case EF_MIPS_ARCH_4:
4074 out = stpcpy (out, ", mips4");
4075 break;
4076 case EF_MIPS_ARCH_5:
4077 out = stpcpy (out, ", mips5");
4078 break;
4079 case EF_MIPS_ARCH_32:
4080 out = stpcpy (out, ", mips32");
4081 break;
4082 case EF_MIPS_ARCH_32R2:
4083 out = stpcpy (out, ", mips32r2");
4084 break;
4085 case EF_MIPS_ARCH_32R6:
4086 out = stpcpy (out, ", mips32r6");
4087 break;
4088 case EF_MIPS_ARCH_64:
4089 out = stpcpy (out, ", mips64");
4090 break;
4091 case EF_MIPS_ARCH_64R2:
4092 out = stpcpy (out, ", mips64r2");
4093 break;
4094 case EF_MIPS_ARCH_64R6:
4095 out = stpcpy (out, ", mips64r6");
4096 break;
4097 default:
4098 out = stpcpy (out, _(", unknown ISA"));
4099 break;
4100 }
4101 return out;
4102}
4103
4104static char *
4105decode_MSP430_machine_flags (char *out, unsigned e_flags)
4106{
4107 out = stpcpy (out, _(": architecture variant: "));
4108 switch (e_flags & EF_MSP430_MACH)
4109 {
4110 case E_MSP430_MACH_MSP430x11:
4111 out = stpcpy (out, "MSP430x11");
4112 break;
4113 case E_MSP430_MACH_MSP430x11x1:
4114 out = stpcpy (out, "MSP430x11x1 ");
4115 break;
4116 case E_MSP430_MACH_MSP430x12:
4117 out = stpcpy (out, "MSP430x12");
4118 break;
4119 case E_MSP430_MACH_MSP430x13:
4120 out = stpcpy (out, "MSP430x13");
4121 break;
4122 case E_MSP430_MACH_MSP430x14:
4123 out = stpcpy (out, "MSP430x14");
4124 break;
4125 case E_MSP430_MACH_MSP430x15:
4126 out = stpcpy (out, "MSP430x15");
4127 break;
4128 case E_MSP430_MACH_MSP430x16:
4129 out = stpcpy (out, "MSP430x16");
4130 break;
4131 case E_MSP430_MACH_MSP430x31:
4132 out = stpcpy (out, "MSP430x31");
4133 break;
4134 case E_MSP430_MACH_MSP430x32:
4135 out = stpcpy (out, "MSP430x32");
4136 break;
4137 case E_MSP430_MACH_MSP430x33:
4138 out = stpcpy (out, "MSP430x33");
4139 break;
4140 case E_MSP430_MACH_MSP430x41:
4141 out = stpcpy (out, "MSP430x41");
4142 break;
4143 case E_MSP430_MACH_MSP430x42:
4144 out = stpcpy (out, "MSP430x42");
4145 break;
4146 case E_MSP430_MACH_MSP430x43:
4147 out = stpcpy (out, "MSP430x43");
4148 break;
4149 case E_MSP430_MACH_MSP430x44:
4150 out = stpcpy (out, "MSP430x44");
4151 break;
4152 case E_MSP430_MACH_MSP430X :
4153 out = stpcpy (out, "MSP430X");
4154 break;
4155 default:
4156 out = stpcpy (out, _(": unknown"));
4157 break;
4158 }
4159
4160 if (e_flags & ~ EF_MSP430_MACH)
4161 out = stpcpy (out, _(": unknown extra flag bits also present"));
4162 return out;
4163}
4164
4165static char *
4166decode_NDS32_machine_flags (char *out, unsigned e_flags)
4167{
4168 unsigned abi;
4169 unsigned arch;
4170 unsigned config;
4171 unsigned version;
4172 bool has_fpu = false;
4173
4174 static const char *ABI_STRINGS[] =
4175 {
4176 "ABI v0", /* use r5 as return register; only used in N1213HC */
4177 "ABI v1", /* use r0 as return register */
4178 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
4179 "ABI v2fp", /* for FPU */
4180 "AABI",
4181 "ABI2 FP+"
4182 };
4183 static const char *VER_STRINGS[] =
4184 {
4185 "Andes ELF V1.3 or older",
4186 "Andes ELF V1.3.1",
4187 "Andes ELF V1.4"
4188 };
4189 static const char *ARCH_STRINGS[] =
4190 {
4191 "",
4192 "Andes Star v1.0",
4193 "Andes Star v2.0",
4194 "Andes Star v3.0",
4195 "Andes Star v3.0m"
4196 };
4197
4198 abi = EF_NDS_ABI & e_flags;
4199 arch = EF_NDS_ARCH & e_flags;
4200 config = EF_NDS_INST & e_flags;
4201 version = EF_NDS32_ELF_VERSION & e_flags;
4202
4203 switch (abi)
4204 {
4205 case E_NDS_ABI_V0:
4206 case E_NDS_ABI_V1:
4207 case E_NDS_ABI_V2:
4208 case E_NDS_ABI_V2FP:
4209 case E_NDS_ABI_AABI:
4210 case E_NDS_ABI_V2FP_PLUS:
4211 /* In case there are holes in the array. */
4212 out += sprintf (out, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
4213 break;
4214
4215 default:
4216 out = stpcpy (out, ", <unrecognized ABI>");
4217 break;
4218 }
4219
4220 switch (version)
4221 {
4222 case E_NDS32_ELF_VER_1_2:
4223 case E_NDS32_ELF_VER_1_3:
4224 case E_NDS32_ELF_VER_1_4:
4225 out += sprintf (out, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
4226 break;
4227
4228 default:
4229 out = stpcpy (out, ", <unrecognized ELF version number>");
4230 break;
4231 }
4232
4233 if (E_NDS_ABI_V0 == abi)
4234 {
4235 /* OLD ABI; only used in N1213HC, has performance extension 1. */
4236 out = stpcpy (out, ", Andes Star v1.0, N1213HC, MAC, PERF1");
4237 if (arch == E_NDS_ARCH_STAR_V1_0)
4238 out = stpcpy (out, ", 16b"); /* has 16-bit instructions */
4239 return out;
4240 }
4241
4242 switch (arch)
4243 {
4244 case E_NDS_ARCH_STAR_V1_0:
4245 case E_NDS_ARCH_STAR_V2_0:
4246 case E_NDS_ARCH_STAR_V3_0:
4247 case E_NDS_ARCH_STAR_V3_M:
4248 out += sprintf (out, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
4249 break;
4250
4251 default:
4252 out = stpcpy (out, ", <unrecognized architecture>");
4253 /* ARCH version determines how the e_flags are interpreted.
4254 If it is unknown, we cannot proceed. */
4255 return out;
4256 }
4257
4258 /* Newer ABI; Now handle architecture specific flags. */
4259 if (arch == E_NDS_ARCH_STAR_V1_0)
4260 {
4261 if (config & E_NDS32_HAS_MFUSR_PC_INST)
4262 out = stpcpy (out, ", MFUSR_PC");
4263
4264 if (!(config & E_NDS32_HAS_NO_MAC_INST))
4265 out = stpcpy (out, ", MAC");
4266
4267 if (config & E_NDS32_HAS_DIV_INST)
4268 out = stpcpy (out, ", DIV");
4269
4270 if (config & E_NDS32_HAS_16BIT_INST)
4271 out = stpcpy (out, ", 16b");
4272 }
4273 else
4274 {
4275 if (config & E_NDS32_HAS_MFUSR_PC_INST)
4276 {
4277 if (version <= E_NDS32_ELF_VER_1_3)
4278 out = stpcpy (out, ", [B8]");
4279 else
4280 out = stpcpy (out, ", EX9");
4281 }
4282
4283 if (config & E_NDS32_HAS_MAC_DX_INST)
4284 out = stpcpy (out, ", MAC_DX");
4285
4286 if (config & E_NDS32_HAS_DIV_DX_INST)
4287 out = stpcpy (out, ", DIV_DX");
4288
4289 if (config & E_NDS32_HAS_16BIT_INST)
4290 {
4291 if (version <= E_NDS32_ELF_VER_1_3)
4292 out = stpcpy (out, ", 16b");
4293 else
4294 out = stpcpy (out, ", IFC");
4295 }
4296 }
4297
4298 if (config & E_NDS32_HAS_EXT_INST)
4299 out = stpcpy (out, ", PERF1");
4300
4301 if (config & E_NDS32_HAS_EXT2_INST)
4302 out = stpcpy (out, ", PERF2");
4303
4304 if (config & E_NDS32_HAS_FPU_INST)
4305 {
4306 has_fpu = true;
4307 out = stpcpy (out, ", FPU_SP");
4308 }
4309
4310 if (config & E_NDS32_HAS_FPU_DP_INST)
4311 {
4312 has_fpu = true;
4313 out = stpcpy (out, ", FPU_DP");
4314 }
4315
4316 if (config & E_NDS32_HAS_FPU_MAC_INST)
4317 {
4318 has_fpu = true;
4319 out = stpcpy (out, ", FPU_MAC");
4320 }
4321
4322 if (has_fpu)
4323 {
4324 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
4325 {
4326 case E_NDS32_FPU_REG_8SP_4DP:
4327 out = stpcpy (out, ", FPU_REG:8/4");
4328 break;
4329 case E_NDS32_FPU_REG_16SP_8DP:
4330 out = stpcpy (out, ", FPU_REG:16/8");
4331 break;
4332 case E_NDS32_FPU_REG_32SP_16DP:
4333 out = stpcpy (out, ", FPU_REG:32/16");
4334 break;
4335 case E_NDS32_FPU_REG_32SP_32DP:
4336 out = stpcpy (out, ", FPU_REG:32/32");
4337 break;
4338 }
4339 }
4340
4341 if (config & E_NDS32_HAS_AUDIO_INST)
4342 out = stpcpy (out, ", AUDIO");
4343
4344 if (config & E_NDS32_HAS_STRING_INST)
4345 out = stpcpy (out, ", STR");
4346
4347 if (config & E_NDS32_HAS_REDUCED_REGS)
4348 out = stpcpy (out, ", 16REG");
4349
4350 if (config & E_NDS32_HAS_VIDEO_INST)
4351 {
4352 if (version <= E_NDS32_ELF_VER_1_3)
4353 out = stpcpy (out, ", VIDEO");
4354 else
4355 out = stpcpy (out, ", SATURATION");
4356 }
4357
4358 if (config & E_NDS32_HAS_ENCRIPT_INST)
4359 out = stpcpy (out, ", ENCRP");
4360
4361 if (config & E_NDS32_HAS_L2C_INST)
4362 out = stpcpy (out, ", L2C");
4363
4364 return out;
4365}
4366
4367static char *
4368decode_PARISC_machine_flags (char *out, unsigned e_flags)
4369{
4370 switch (e_flags & EF_PARISC_ARCH)
4371 {
4372 case EFA_PARISC_1_0:
4373 out = stpcpy (out, ", PA-RISC 1.0");
4374 break;
4375 case EFA_PARISC_1_1:
4376 out = stpcpy (out, ", PA-RISC 1.1");
4377 break;
4378 case EFA_PARISC_2_0:
4379 out = stpcpy (out, ", PA-RISC 2.0");
4380 break;
4381 default:
4382 break;
4383 }
4384 if (e_flags & EF_PARISC_TRAPNIL)
4385 out = stpcpy (out, ", trapnil");
4386 if (e_flags & EF_PARISC_EXT)
4387 out = stpcpy (out, ", ext");
4388 if (e_flags & EF_PARISC_LSB)
4389 out = stpcpy (out, ", lsb");
4390 if (e_flags & EF_PARISC_WIDE)
4391 out = stpcpy (out, ", wide");
4392 if (e_flags & EF_PARISC_NO_KABP)
4393 out = stpcpy (out, ", no kabp");
4394 if (e_flags & EF_PARISC_LAZYSWAP)
4395 out = stpcpy (out, ", lazyswap");
4396 return out;
4397}
4398
4399static char *
4400decode_RISCV_machine_flags (char *out, unsigned e_flags)
4401{
4402 if (e_flags & EF_RISCV_RVC)
4403 out = stpcpy (out, ", RVC");
4404
4405 if (e_flags & EF_RISCV_RVE)
4406 out = stpcpy (out, ", RVE");
4407
4408 if (e_flags & EF_RISCV_TSO)
4409 out = stpcpy (out, ", TSO");
4410
4411 switch (e_flags & EF_RISCV_FLOAT_ABI)
4412 {
4413 case EF_RISCV_FLOAT_ABI_SOFT:
4414 out = stpcpy (out, ", soft-float ABI");
4415 break;
4416
4417 case EF_RISCV_FLOAT_ABI_SINGLE:
4418 out = stpcpy (out, ", single-float ABI");
4419 break;
4420
4421 case EF_RISCV_FLOAT_ABI_DOUBLE:
4422 out = stpcpy (out, ", double-float ABI");
4423 break;
4424
4425 case EF_RISCV_FLOAT_ABI_QUAD:
4426 out = stpcpy (out, ", quad-float ABI");
4427 break;
4428 }
4429 return out;
4430}
4431
4432static char *
4433decode_RL78_machine_flags (char *out, unsigned e_flags)
4434{
4435 switch (e_flags & E_FLAG_RL78_CPU_MASK)
4436 {
4437 case E_FLAG_RL78_ANY_CPU:
4438 break;
4439 case E_FLAG_RL78_G10:
4440 out = stpcpy (out, ", G10");
4441 break;
4442 case E_FLAG_RL78_G13:
4443 out = stpcpy (out, ", G13");
4444 break;
4445 case E_FLAG_RL78_G14:
4446 out = stpcpy (out, ", G14");
4447 break;
4448 }
4449 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
4450 out = stpcpy (out, ", 64-bit doubles");
4451 return out;
4452}
4453
4454static char *
4455decode_RX_machine_flags (char *out, unsigned e_flags)
4456{
4457 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
4458 out = stpcpy (out, ", 64-bit doubles");
4459 if (e_flags & E_FLAG_RX_DSP)
4460 out = stpcpy (out, ", dsp");
4461 if (e_flags & E_FLAG_RX_PID)
4462 out = stpcpy (out, ", pid");
4463 if (e_flags & E_FLAG_RX_ABI)
4464 out = stpcpy (out, ", RX ABI");
4465 if (e_flags & E_FLAG_RX_SINSNS_SET)
4466 out = stpcpy (out, (e_flags & E_FLAG_RX_SINSNS_YES
4467 ? ", uses String instructions"
4468 : ", bans String instructions"));
4469 if (e_flags & E_FLAG_RX_V2)
4470 out = stpcpy (out, ", V2");
4471 if (e_flags & E_FLAG_RX_V3)
4472 out = stpcpy (out, ", V3");
4473 return out;
4474}
4475
4476static char *
4477decode_SH_machine_flags (char *out, unsigned e_flags)
4478{
4479 switch ((e_flags & EF_SH_MACH_MASK))
4480 {
4481 case EF_SH1:
4482 out = stpcpy (out, ", sh1");
4483 break;
4484 case EF_SH2:
4485 out = stpcpy (out, ", sh2");
4486 break;
4487 case EF_SH3:
4488 out = stpcpy (out, ", sh3");
4489 break;
4490 case EF_SH_DSP:
4491 out = stpcpy (out, ", sh-dsp");
4492 break;
4493 case EF_SH3_DSP:
4494 out = stpcpy (out, ", sh3-dsp");
4495 break;
4496 case EF_SH4AL_DSP:
4497 out = stpcpy (out, ", sh4al-dsp");
4498 break;
4499 case EF_SH3E:
4500 out = stpcpy (out, ", sh3e");
4501 break;
4502 case EF_SH4:
4503 out = stpcpy (out, ", sh4");
4504 break;
4505 case EF_SH5:
4506 out = stpcpy (out, ", sh5");
4507 break;
4508 case EF_SH2E:
4509 out = stpcpy (out, ", sh2e");
4510 break;
4511 case EF_SH4A:
4512 out = stpcpy (out, ", sh4a");
4513 break;
4514 case EF_SH2A:
4515 out = stpcpy (out, ", sh2a");
4516 break;
4517 case EF_SH4_NOFPU:
4518 out = stpcpy (out, ", sh4-nofpu");
4519 break;
4520 case EF_SH4A_NOFPU:
4521 out = stpcpy (out, ", sh4a-nofpu");
4522 break;
4523 case EF_SH2A_NOFPU:
4524 out = stpcpy (out, ", sh2a-nofpu");
4525 break;
4526 case EF_SH3_NOMMU:
4527 out = stpcpy (out, ", sh3-nommu");
4528 break;
4529 case EF_SH4_NOMMU_NOFPU:
4530 out = stpcpy (out, ", sh4-nommu-nofpu");
4531 break;
4532 case EF_SH2A_SH4_NOFPU:
4533 out = stpcpy (out, ", sh2a-nofpu-or-sh4-nommu-nofpu");
4534 break;
4535 case EF_SH2A_SH3_NOFPU:
4536 out = stpcpy (out, ", sh2a-nofpu-or-sh3-nommu");
4537 break;
4538 case EF_SH2A_SH4:
4539 out = stpcpy (out, ", sh2a-or-sh4");
4540 break;
4541 case EF_SH2A_SH3E:
4542 out = stpcpy (out, ", sh2a-or-sh3e");
4543 break;
4544 default:
4545 out = stpcpy (out, _(", unknown ISA"));
4546 break;
4547 }
4548
4549 if (e_flags & EF_SH_PIC)
4550 out = stpcpy (out, ", pic");
4551
4552 if (e_flags & EF_SH_FDPIC)
4553 out = stpcpy (out, ", fdpic");
4554 return out;
4555}
4556
4557static char *
4558decode_SPARC_machine_flags (char *out, unsigned e_flags)
4559{
4560 if (e_flags & EF_SPARC_32PLUS)
4561 out = stpcpy (out, ", v8+");
4562
4563 if (e_flags & EF_SPARC_SUN_US1)
4564 out = stpcpy (out, ", ultrasparcI");
4565
4566 if (e_flags & EF_SPARC_SUN_US3)
4567 out = stpcpy (out, ", ultrasparcIII");
4568
4569 if (e_flags & EF_SPARC_HAL_R1)
4570 out = stpcpy (out, ", halr1");
4571
4572 if (e_flags & EF_SPARC_LEDATA)
4573 out = stpcpy (out, ", ledata");
4574
4575 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
4576 out = stpcpy (out, ", tso");
4577
4578 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
4579 out = stpcpy (out, ", pso");
4580
4581 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
4582 out = stpcpy (out, ", rmo");
4583 return out;
4584}
4585
4586static char *
4587decode_V800_machine_flags (char *out, unsigned int e_flags)
4588{
4589 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
4590 out = stpcpy (out, ", RH850 ABI");
4591
4592 if (e_flags & EF_V800_850E3)
4593 out = stpcpy (out, ", V3 architecture");
4594
4595 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
4596 out = stpcpy (out, ", FPU not used");
4597
4598 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
4599 out = stpcpy (out, ", regmode: COMMON");
4600
4601 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
4602 out = stpcpy (out, ", r4 not used");
4603
4604 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
4605 out = stpcpy (out, ", r30 not used");
4606
4607 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
4608 out = stpcpy (out, ", r5 not used");
4609
4610 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
4611 out = stpcpy (out, ", r2 not used");
4612
4613 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
4614 {
4615 switch (e_flags & - e_flags)
4616 {
4617 case EF_RH850_FPU_DOUBLE:
4618 out = stpcpy (out, ", double precision FPU");
4619 break;
4620 case EF_RH850_FPU_SINGLE:
4621 out = stpcpy (out, ", single precision FPU");
4622 break;
4623 case EF_RH850_REGMODE22:
4624 out = stpcpy (out, ", regmode:22");
4625 break;
4626 case EF_RH850_REGMODE32:
4627 out = stpcpy (out, ", regmode:23");
4628 break;
4629 case EF_RH850_GP_FIX:
4630 out = stpcpy (out, ", r4 fixed");
4631 break;
4632 case EF_RH850_GP_NOFIX:
4633 out = stpcpy (out, ", r4 free");
4634 break;
4635 case EF_RH850_EP_FIX:
4636 out = stpcpy (out, ", r30 fixed");
4637 break;
4638 case EF_RH850_EP_NOFIX:
4639 out = stpcpy (out, ", r30 free");
4640 break;
4641 case EF_RH850_TP_FIX:
4642 out = stpcpy (out, ", r5 fixed");
4643 break;
4644 case EF_RH850_TP_NOFIX:
4645 out = stpcpy (out, ", r5 free");
4646 break;
4647 case EF_RH850_REG2_RESERVE:
4648 out = stpcpy (out, ", r2 fixed");
4649 break;
4650 case EF_RH850_REG2_NORESERVE:
4651 out = stpcpy (out, ", r2 free");
4652 break;
4653 default:
4654 break;
4655 }
4656 }
4657 return out;
4658}
4659
4660static char *
4661decode_V850_machine_flags (char *out, unsigned int e_flags)
4662{
4663 switch (e_flags & EF_V850_ARCH)
4664 {
4665 case E_V850E3V5_ARCH:
4666 out = stpcpy (out, ", v850e3v5");
4667 break;
4668 case E_V850E2V3_ARCH:
4669 out = stpcpy (out, ", v850e2v3");
4670 break;
4671 case E_V850E2_ARCH:
4672 out = stpcpy (out, ", v850e2");
4673 break;
4674 case E_V850E1_ARCH:
4675 out = stpcpy (out, ", v850e1");
4676 break;
4677 case E_V850E_ARCH:
4678 out = stpcpy (out, ", v850e");
4679 break;
4680 case E_V850_ARCH:
4681 out = stpcpy (out, ", v850");
4682 break;
4683 default:
4684 out = stpcpy (out, _(", unknown v850 architecture variant"));
4685 break;
4686 }
4687 return out;
4688}
4689
4690static char *
4691decode_Z80_machine_flags (char *out, unsigned int e_flags)
4692{
4693 switch (e_flags & EF_Z80_MACH_MSK)
4694 {
4695 case EF_Z80_MACH_Z80:
4696 out = stpcpy (out, ", Z80");
4697 break;
4698 case EF_Z80_MACH_Z180:
4699 out = stpcpy (out, ", Z180");
4700 break;
4701 case EF_Z80_MACH_R800:
4702 out = stpcpy (out, ", R800");
4703 break;
4704 case EF_Z80_MACH_EZ80_Z80:
4705 out = stpcpy (out, ", EZ80");
4706 break;
4707 case EF_Z80_MACH_EZ80_ADL:
4708 out = stpcpy (out, ", EZ80, ADL");
4709 break;
4710 case EF_Z80_MACH_GBZ80:
4711 out = stpcpy (out, ", GBZ80");
4712 break;
4713 case EF_Z80_MACH_Z80N:
4714 out = stpcpy (out, ", Z80N");
4715 break;
4716 default:
4717 out = stpcpy (out, _(", unknown"));
4718 break;
4719 }
4720 return out;
4721}
4722
4723static char *
4724decode_AMDGPU_machine_flags (char *out, unsigned int e_flags, Filedata *filedata)
4725{
4726 unsigned char *e_ident = filedata->file_header.e_ident;
4727 unsigned char osabi = e_ident[EI_OSABI];
4728 unsigned char abiversion = e_ident[EI_ABIVERSION];
4729 unsigned int mach;
4730
4731 /* HSA OS ABI v2 used a different encoding, but we don't need to support it,
4732 it has been deprecated for a while.
4733
4734 The PAL, MESA3D and NONE OS ABIs are not properly versioned, at the time
4735 of writing, they use the same flags as HSA v3, so the code below uses that
4736 assumption. */
4737 if (osabi == ELFOSABI_AMDGPU_HSA && abiversion < ELFABIVERSION_AMDGPU_HSA_V3)
4738 return out;
4739
4740 mach = e_flags & EF_AMDGPU_MACH;
4741 switch (mach)
4742 {
4743#define AMDGPU_CASE(code, string) \
4744 case code: out = stpcpy (out, ", " string); break;
4745 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX600, "gfx600")
4746 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX601, "gfx601")
4747 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX700, "gfx700")
4748 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX701, "gfx701")
4749 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX702, "gfx702")
4750 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX703, "gfx703")
4751 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX704, "gfx704")
4752 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX801, "gfx801")
4753 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX802, "gfx802")
4754 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX803, "gfx803")
4755 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX810, "gfx810")
4756 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX900, "gfx900")
4757 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX902, "gfx902")
4758 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX904, "gfx904")
4759 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX906, "gfx906")
4760 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX908, "gfx908")
4761 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX909, "gfx909")
4762 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90C, "gfx90c")
4763 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1010, "gfx1010")
4764 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1011, "gfx1011")
4765 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1012, "gfx1012")
4766 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1030, "gfx1030")
4767 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1031, "gfx1031")
4768 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1032, "gfx1032")
4769 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1033, "gfx1033")
4770 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1100, "gfx1100")
4771 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1101, "gfx1101")
4772 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1102, "gfx1102")
4773 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX602, "gfx602")
4774 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX705, "gfx705")
4775 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX805, "gfx805")
4776 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1035, "gfx1035")
4777 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1034, "gfx1034")
4778 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90A, "gfx90a")
4779 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX940, "gfx940")
4780 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1013, "gfx1013")
4781 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1036, "gfx1036")
4782 default:
4783 out += sprintf (out, _(", <unknown AMDGPU GPU type: %#x>"), mach);
4784 break;
4785#undef AMDGPU_CASE
4786 }
4787
4788 e_flags &= ~EF_AMDGPU_MACH;
4789
4790 if ((osabi == ELFOSABI_AMDGPU_HSA
4791 && abiversion == ELFABIVERSION_AMDGPU_HSA_V3)
4792 || osabi != ELFOSABI_AMDGPU_HSA)
4793 {
4794 /* For HSA v3 and other OS ABIs. */
4795 if (e_flags & EF_AMDGPU_FEATURE_XNACK_V3)
4796 {
4797 out = stpcpy (out, ", xnack on");
4798 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V3;
4799 }
4800
4801 if (e_flags & EF_AMDGPU_FEATURE_SRAMECC_V3)
4802 {
4803 out = stpcpy (out, ", sramecc on");
4804 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V3;
4805 }
4806 }
4807 else
4808 {
4809 /* For HSA v4+. */
4810 int xnack, sramecc;
4811
4812 xnack = e_flags & EF_AMDGPU_FEATURE_XNACK_V4;
4813 switch (xnack)
4814 {
4815 case EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4:
4816 break;
4817
4818 case EF_AMDGPU_FEATURE_XNACK_ANY_V4:
4819 out = stpcpy (out, ", xnack any");
4820 break;
4821
4822 case EF_AMDGPU_FEATURE_XNACK_OFF_V4:
4823 out = stpcpy (out, ", xnack off");
4824 break;
4825
4826 case EF_AMDGPU_FEATURE_XNACK_ON_V4:
4827 out = stpcpy (out, ", xnack on");
4828 break;
4829
4830 default:
4831 out += sprintf (out, _(", <unknown xnack value: %#x>"), xnack);
4832 break;
4833 }
4834
4835 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V4;
4836
4837 sramecc = e_flags & EF_AMDGPU_FEATURE_SRAMECC_V4;
4838 switch (sramecc)
4839 {
4840 case EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4:
4841 break;
4842
4843 case EF_AMDGPU_FEATURE_SRAMECC_ANY_V4:
4844 out = stpcpy (out, ", sramecc any");
4845 break;
4846
4847 case EF_AMDGPU_FEATURE_SRAMECC_OFF_V4:
4848 out = stpcpy (out, ", sramecc off");
4849 break;
4850
4851 case EF_AMDGPU_FEATURE_SRAMECC_ON_V4:
4852 out = stpcpy (out, ", sramecc on");
4853 break;
4854
4855 default:
4856 out += sprintf (out, _(", <unknown sramecc value: %#x>"), sramecc);
4857 break;
4858 }
4859
4860 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V4;
4861 }
4862
4863 if (e_flags != 0)
4864 out += sprintf (out, _(", unknown flags bits: %#x"), e_flags);
4865 return out;
4866}
4867
4868static char *
4869get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
4870{
4871 static char buf[1024];
4872 char *out = buf;
4873
4874 buf[0] = '\0';
4875
4876 if (e_flags)
4877 {
4878 switch (e_machine)
4879 {
4880 default:
4881 break;
4882
4883 case EM_ARC_COMPACT3:
4884 out = stpcpy (out, ", HS5x");
4885 break;
4886
4887 case EM_ARC_COMPACT3_64:
4888 out = stpcpy (out, ", HS6x");
4889 break;
4890
4891 case EM_ARC_COMPACT2:
4892 case EM_ARC_COMPACT:
4893 out = decode_ARC_machine_flags (out, e_flags, e_machine);
4894 break;
4895
4896 case EM_ARM:
4897 out = decode_ARM_machine_flags (out, e_flags);
4898 break;
4899
4900 case EM_AVR:
4901 out = decode_AVR_machine_flags (out, e_flags);
4902 break;
4903
4904 case EM_BLACKFIN:
4905 out = decode_BLACKFIN_machine_flags (out, e_flags);
4906 break;
4907
4908 case EM_CYGNUS_FRV:
4909 out = decode_FRV_machine_flags (out, e_flags);
4910 break;
4911
4912 case EM_68K:
4913 out = decode_M68K_machine_flags (out, e_flags);
4914 break;
4915
4916 case EM_AMDGPU:
4917 out = decode_AMDGPU_machine_flags (out, e_flags, filedata);
4918 break;
4919
4920 case EM_CYGNUS_MEP:
4921 out = decode_MeP_machine_flags (out, e_flags);
4922 break;
4923
4924 case EM_PPC:
4925 if (e_flags & EF_PPC_EMB)
4926 out = stpcpy (out, ", emb");
4927
4928 if (e_flags & EF_PPC_RELOCATABLE)
4929 out = stpcpy (out, _(", relocatable"));
4930
4931 if (e_flags & EF_PPC_RELOCATABLE_LIB)
4932 out = stpcpy (out, _(", relocatable-lib"));
4933 break;
4934
4935 case EM_PPC64:
4936 if (e_flags & EF_PPC64_ABI)
4937 out += sprintf (out, ", abiv%d", e_flags & EF_PPC64_ABI);
4938 break;
4939
4940 case EM_V800:
4941 out = decode_V800_machine_flags (out, e_flags);
4942 break;
4943
4944 case EM_V850:
4945 case EM_CYGNUS_V850:
4946 out = decode_V850_machine_flags (out, e_flags);
4947 break;
4948
4949 case EM_M32R:
4950 case EM_CYGNUS_M32R:
4951 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
4952 out = stpcpy (out, ", m32r");
4953 break;
4954
4955 case EM_MIPS:
4956 case EM_MIPS_RS3_LE:
4957 out = decode_MIPS_machine_flags (out, e_flags);
4958 break;
4959
4960 case EM_NDS32:
4961 out = decode_NDS32_machine_flags (out, e_flags);
4962 break;
4963
4964 case EM_NFP:
4965 switch (EF_NFP_MACH (e_flags))
4966 {
4967 case E_NFP_MACH_3200:
4968 out = stpcpy (out, ", NFP-32xx");
4969 break;
4970 case E_NFP_MACH_6000:
4971 out = stpcpy (out, ", NFP-6xxx");
4972 break;
4973 }
4974 break;
4975
4976 case EM_RISCV:
4977 out = decode_RISCV_machine_flags (out, e_flags);
4978 break;
4979
4980 case EM_SH:
4981 out = decode_SH_machine_flags (out, e_flags);
4982 break;
4983
4984 case EM_OR1K:
4985 if (e_flags & EF_OR1K_NODELAY)
4986 out = stpcpy (out, ", no delay");
4987 break;
4988
4989 case EM_BPF:
4990 out += sprintf (out, ", CPU Version: %u", e_flags & EF_BPF_CPUVER);
4991 break;
4992
4993 case EM_SPARCV9:
4994 out = decode_SPARC_machine_flags (out, e_flags);
4995 break;
4996
4997 case EM_PARISC:
4998 out = decode_PARISC_machine_flags (out, e_flags);
4999 break;
5000
5001 case EM_PJ:
5002 case EM_PJ_OLD:
5003 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
5004 out = stpcpy (out, ", new calling convention");
5005
5006 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
5007 out = stpcpy (out, ", gnu calling convention");
5008 break;
5009
5010 case EM_IA_64:
5011 out = decode_IA64_machine_flags (out, e_flags, filedata);
5012 break;
5013
5014 case EM_VAX:
5015 if ((e_flags & EF_VAX_NONPIC))
5016 out = stpcpy (out, ", non-PIC");
5017 if ((e_flags & EF_VAX_DFLOAT))
5018 out = stpcpy (out, ", D-Float");
5019 if ((e_flags & EF_VAX_GFLOAT))
5020 out = stpcpy (out, ", G-Float");
5021 break;
5022
5023 case EM_VISIUM:
5024 if (e_flags & EF_VISIUM_ARCH_MCM)
5025 out = stpcpy (out, ", mcm");
5026 else if (e_flags & EF_VISIUM_ARCH_MCM24)
5027 out = stpcpy (out, ", mcm24");
5028 if (e_flags & EF_VISIUM_ARCH_GR6)
5029 out = stpcpy (out, ", gr6");
5030 break;
5031
5032 case EM_RL78:
5033 out = decode_RL78_machine_flags (out, e_flags);
5034 break;
5035
5036 case EM_RX:
5037 out = decode_RX_machine_flags (out, e_flags);
5038 break;
5039
5040 case EM_S390:
5041 if (e_flags & EF_S390_HIGH_GPRS)
5042 out = stpcpy (out, ", highgprs");
5043 break;
5044
5045 case EM_TI_C6000:
5046 if ((e_flags & EF_C6000_REL))
5047 out = stpcpy (out, ", relocatable module");
5048 break;
5049
5050 case EM_KVX:
5051 if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV3_1)
5052 strcat (buf, ", Kalray VLIW kv3-1");
5053 else if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV3_2)
5054 strcat (buf, ", Kalray VLIW kv3-2");
5055 else if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV4_1)
5056 strcat (buf, ", Kalray VLIW kv4-1");
5057 else
5058 strcat (buf, ", unknown KVX MPPA");
5059 break;
5060
5061 case EM_MSP430:
5062 out = decode_MSP430_machine_flags (out, e_flags);
5063 break;
5064
5065 case EM_Z80:
5066 out = decode_Z80_machine_flags (out, e_flags);
5067 break;
5068
5069 case EM_LOONGARCH:
5070 out = decode_LOONGARCH_machine_flags (out, e_flags);
5071 break;
5072 }
5073 }
5074
5075 return buf;
5076}
5077
5078static const char *
5079get_osabi_name (Filedata * filedata, unsigned int osabi)
5080{
5081 static char buff[32];
5082
5083 switch (osabi)
5084 {
5085 case ELFOSABI_NONE: return "UNIX - System V";
5086 case ELFOSABI_HPUX: return "UNIX - HP-UX";
5087 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
5088 case ELFOSABI_GNU: return "UNIX - GNU";
5089 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
5090 case ELFOSABI_AIX: return "UNIX - AIX";
5091 case ELFOSABI_IRIX: return "UNIX - IRIX";
5092 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
5093 case ELFOSABI_TRU64: return "UNIX - TRU64";
5094 case ELFOSABI_MODESTO: return "Novell - Modesto";
5095 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
5096 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
5097 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
5098 case ELFOSABI_AROS: return "AROS";
5099 case ELFOSABI_FENIXOS: return "FenixOS";
5100 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
5101 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
5102 default:
5103 if (osabi >= 64)
5104 switch (filedata->file_header.e_machine)
5105 {
5106 case EM_AMDGPU:
5107 switch (osabi)
5108 {
5109 case ELFOSABI_AMDGPU_HSA: return "AMD HSA";
5110 case ELFOSABI_AMDGPU_PAL: return "AMD PAL";
5111 case ELFOSABI_AMDGPU_MESA3D: return "AMD Mesa3D";
5112 default:
5113 break;
5114 }
5115 break;
5116
5117 case EM_ARM:
5118 switch (osabi)
5119 {
5120 case ELFOSABI_ARM: return "ARM";
5121 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
5122 default:
5123 break;
5124 }
5125 break;
5126
5127 case EM_MSP430:
5128 case EM_MSP430_OLD:
5129 case EM_VISIUM:
5130 switch (osabi)
5131 {
5132 case ELFOSABI_STANDALONE: return _("Standalone App");
5133 default:
5134 break;
5135 }
5136 break;
5137
5138 case EM_TI_C6000:
5139 switch (osabi)
5140 {
5141 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
5142 case ELFOSABI_C6000_LINUX: return "Linux C6000";
5143 default:
5144 break;
5145 }
5146 break;
5147
5148 default:
5149 break;
5150 }
5151 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
5152 return buff;
5153 }
5154}
5155
5156static const char *
5157get_aarch64_segment_type (unsigned long type)
5158{
5159 switch (type)
5160 {
5161 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
5162 case PT_AARCH64_MEMTAG_MTE: return "AARCH64_MEMTAG_MTE";
5163 default: return NULL;
5164 }
5165}
5166
5167static const char *
5168get_arm_segment_type (unsigned long type)
5169{
5170 switch (type)
5171 {
5172 case PT_ARM_EXIDX: return "EXIDX";
5173 default: return NULL;
5174 }
5175}
5176
5177static const char *
5178get_s390_segment_type (unsigned long type)
5179{
5180 switch (type)
5181 {
5182 case PT_S390_PGSTE: return "S390_PGSTE";
5183 default: return NULL;
5184 }
5185}
5186
5187static const char *
5188get_mips_segment_type (unsigned long type)
5189{
5190 switch (type)
5191 {
5192 case PT_MIPS_REGINFO: return "REGINFO";
5193 case PT_MIPS_RTPROC: return "RTPROC";
5194 case PT_MIPS_OPTIONS: return "OPTIONS";
5195 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
5196 default: return NULL;
5197 }
5198}
5199
5200static const char *
5201get_parisc_segment_type (unsigned long type)
5202{
5203 switch (type)
5204 {
5205 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
5206 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
5207 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
5208 default: return NULL;
5209 }
5210}
5211
5212static const char *
5213get_ia64_segment_type (unsigned long type)
5214{
5215 switch (type)
5216 {
5217 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
5218 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
5219 default: return NULL;
5220 }
5221}
5222
5223static const char *
5224get_tic6x_segment_type (unsigned long type)
5225{
5226 switch (type)
5227 {
5228 case PT_C6000_PHATTR: return "C6000_PHATTR";
5229 default: return NULL;
5230 }
5231}
5232
5233static const char *
5234get_riscv_segment_type (unsigned long type)
5235{
5236 switch (type)
5237 {
5238 case PT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
5239 default: return NULL;
5240 }
5241}
5242
5243static const char *
5244get_hpux_segment_type (unsigned long type, unsigned e_machine)
5245{
5246 if (e_machine == EM_PARISC)
5247 switch (type)
5248 {
5249 case PT_HP_TLS: return "HP_TLS";
5250 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
5251 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
5252 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
5253 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
5254 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
5255 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
5256 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
5257 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
5258 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
5259 case PT_HP_PARALLEL: return "HP_PARALLEL";
5260 case PT_HP_FASTBIND: return "HP_FASTBIND";
5261 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
5262 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
5263 case PT_HP_STACK: return "HP_STACK";
5264 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
5265 default: return NULL;
5266 }
5267
5268 if (e_machine == EM_IA_64)
5269 switch (type)
5270 {
5271 case PT_HP_TLS: return "HP_TLS";
5272 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
5273 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
5274 case PT_IA_64_HP_STACK: return "HP_STACK";
5275 default: return NULL;
5276 }
5277
5278 return NULL;
5279}
5280
5281static const char *
5282get_solaris_segment_type (unsigned long type)
5283{
5284 switch (type)
5285 {
5286 case 0x6464e550: return "PT_SUNW_UNWIND";
5287 case 0x6474e550: return "PT_SUNW_EH_FRAME";
5288 case 0x6ffffff7: return "PT_LOSUNW";
5289 case 0x6ffffffa: return "PT_SUNWBSS";
5290 case 0x6ffffffb: return "PT_SUNWSTACK";
5291 case 0x6ffffffc: return "PT_SUNWDTRACE";
5292 case 0x6ffffffd: return "PT_SUNWCAP";
5293 case 0x6fffffff: return "PT_HISUNW";
5294 default: return NULL;
5295 }
5296}
5297
5298static const char *
5299get_segment_type (Filedata * filedata, unsigned long p_type)
5300{
5301 static char buff[32];
5302
5303 switch (p_type)
5304 {
5305 case PT_NULL: return "NULL";
5306 case PT_LOAD: return "LOAD";
5307 case PT_DYNAMIC: return "DYNAMIC";
5308 case PT_INTERP: return "INTERP";
5309 case PT_NOTE: return "NOTE";
5310 case PT_SHLIB: return "SHLIB";
5311 case PT_PHDR: return "PHDR";
5312 case PT_TLS: return "TLS";
5313 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
5314 case PT_GNU_STACK: return "GNU_STACK";
5315 case PT_GNU_RELRO: return "GNU_RELRO";
5316 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
5317 case PT_GNU_SFRAME: return "GNU_SFRAME";
5318
5319 case PT_OPENBSD_MUTABLE: return "OPENBSD_MUTABLE";
5320 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
5321 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
5322 case PT_OPENBSD_NOBTCFI: return "OPENBSD_NOBTCFI";
5323 case PT_OPENBSD_SYSCALLS: return "OPENBSD_SYSCALLS";
5324 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
5325
5326 default:
5327 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
5328 {
5329 const char * result;
5330
5331 switch (filedata->file_header.e_machine)
5332 {
5333 case EM_AARCH64:
5334 result = get_aarch64_segment_type (p_type);
5335 break;
5336 case EM_ARM:
5337 result = get_arm_segment_type (p_type);
5338 break;
5339 case EM_MIPS:
5340 case EM_MIPS_RS3_LE:
5341 result = get_mips_segment_type (p_type);
5342 break;
5343 case EM_PARISC:
5344 result = get_parisc_segment_type (p_type);
5345 break;
5346 case EM_IA_64:
5347 result = get_ia64_segment_type (p_type);
5348 break;
5349 case EM_TI_C6000:
5350 result = get_tic6x_segment_type (p_type);
5351 break;
5352 case EM_S390:
5353 case EM_S390_OLD:
5354 result = get_s390_segment_type (p_type);
5355 break;
5356 case EM_RISCV:
5357 result = get_riscv_segment_type (p_type);
5358 break;
5359 default:
5360 result = NULL;
5361 break;
5362 }
5363
5364 if (result != NULL)
5365 return result;
5366
5367 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
5368 }
5369 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
5370 {
5371 const char * result = NULL;
5372
5373 switch (filedata->file_header.e_ident[EI_OSABI])
5374 {
5375 case ELFOSABI_GNU:
5376 case ELFOSABI_FREEBSD:
5377 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
5378 {
5379 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
5380 result = buff;
5381 }
5382 break;
5383 case ELFOSABI_HPUX:
5384 result = get_hpux_segment_type (p_type,
5385 filedata->file_header.e_machine);
5386 break;
5387 case ELFOSABI_SOLARIS:
5388 result = get_solaris_segment_type (p_type);
5389 break;
5390 default:
5391 break;
5392 }
5393 if (result != NULL)
5394 return result;
5395
5396 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
5397 }
5398 else
5399 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
5400
5401 return buff;
5402 }
5403}
5404
5405static const char *
5406get_arc_section_type_name (unsigned int sh_type)
5407{
5408 switch (sh_type)
5409 {
5410 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
5411 default:
5412 break;
5413 }
5414 return NULL;
5415}
5416
5417static const char *
5418get_mips_section_type_name (unsigned int sh_type)
5419{
5420 switch (sh_type)
5421 {
5422 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
5423 case SHT_MIPS_MSYM: return "MIPS_MSYM";
5424 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
5425 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
5426 case SHT_MIPS_UCODE: return "MIPS_UCODE";
5427 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
5428 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
5429 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
5430 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
5431 case SHT_MIPS_RELD: return "MIPS_RELD";
5432 case SHT_MIPS_IFACE: return "MIPS_IFACE";
5433 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
5434 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
5435 case SHT_MIPS_SHDR: return "MIPS_SHDR";
5436 case SHT_MIPS_FDESC: return "MIPS_FDESC";
5437 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
5438 case SHT_MIPS_DENSE: return "MIPS_DENSE";
5439 case SHT_MIPS_PDESC: return "MIPS_PDESC";
5440 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
5441 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
5442 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
5443 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
5444 case SHT_MIPS_LINE: return "MIPS_LINE";
5445 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
5446 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
5447 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
5448 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
5449 case SHT_MIPS_DWARF: return "MIPS_DWARF";
5450 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
5451 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
5452 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
5453 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
5454 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
5455 case SHT_MIPS_XLATE: return "MIPS_XLATE";
5456 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
5457 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
5458 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
5459 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
5460 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
5461 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
5462 case SHT_MIPS_XHASH: return "MIPS_XHASH";
5463 default:
5464 break;
5465 }
5466 return NULL;
5467}
5468
5469static const char *
5470get_parisc_section_type_name (unsigned int sh_type)
5471{
5472 switch (sh_type)
5473 {
5474 case SHT_PARISC_EXT: return "PARISC_EXT";
5475 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
5476 case SHT_PARISC_DOC: return "PARISC_DOC";
5477 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
5478 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
5479 case SHT_PARISC_STUBS: return "PARISC_STUBS";
5480 case SHT_PARISC_DLKM: return "PARISC_DLKM";
5481 default: return NULL;
5482 }
5483}
5484
5485static const char *
5486get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
5487{
5488 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
5489 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
5490 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
5491
5492 switch (sh_type)
5493 {
5494 case SHT_IA_64_EXT: return "IA_64_EXT";
5495 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
5496 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
5497 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
5498 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
5499 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
5500 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
5501 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
5502 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
5503 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
5504 default:
5505 break;
5506 }
5507 return NULL;
5508}
5509
5510static const char *
5511get_x86_64_section_type_name (unsigned int sh_type)
5512{
5513 switch (sh_type)
5514 {
5515 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
5516 default: return NULL;
5517 }
5518}
5519
5520static const char *
5521get_aarch64_section_type_name (unsigned int sh_type)
5522{
5523 switch (sh_type)
5524 {
5525 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
5526 default: return NULL;
5527 }
5528}
5529
5530static const char *
5531get_arm_section_type_name (unsigned int sh_type)
5532{
5533 switch (sh_type)
5534 {
5535 case SHT_ARM_EXIDX: return "ARM_EXIDX";
5536 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
5537 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
5538 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
5539 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
5540 default: return NULL;
5541 }
5542}
5543
5544static const char *
5545get_tic6x_section_type_name (unsigned int sh_type)
5546{
5547 switch (sh_type)
5548 {
5549 case SHT_C6000_UNWIND: return "C6000_UNWIND";
5550 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
5551 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
5552 case SHT_TI_ICODE: return "TI_ICODE";
5553 case SHT_TI_XREF: return "TI_XREF";
5554 case SHT_TI_HANDLER: return "TI_HANDLER";
5555 case SHT_TI_INITINFO: return "TI_INITINFO";
5556 case SHT_TI_PHATTRS: return "TI_PHATTRS";
5557 default: return NULL;
5558 }
5559}
5560
5561static const char *
5562get_msp430_section_type_name (unsigned int sh_type)
5563{
5564 switch (sh_type)
5565 {
5566 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
5567 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
5568 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
5569 default: return NULL;
5570 }
5571}
5572
5573static const char *
5574get_nfp_section_type_name (unsigned int sh_type)
5575{
5576 switch (sh_type)
5577 {
5578 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
5579 case SHT_NFP_INITREG: return "NFP_INITREG";
5580 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
5581 default: return NULL;
5582 }
5583}
5584
5585static const char *
5586get_v850_section_type_name (unsigned int sh_type)
5587{
5588 switch (sh_type)
5589 {
5590 case SHT_V850_SCOMMON: return "V850 Small Common";
5591 case SHT_V850_TCOMMON: return "V850 Tiny Common";
5592 case SHT_V850_ZCOMMON: return "V850 Zero Common";
5593 case SHT_RENESAS_IOP: return "RENESAS IOP";
5594 case SHT_RENESAS_INFO: return "RENESAS INFO";
5595 default: return NULL;
5596 }
5597}
5598
5599static const char *
5600get_riscv_section_type_name (unsigned int sh_type)
5601{
5602 switch (sh_type)
5603 {
5604 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
5605 default: return NULL;
5606 }
5607}
5608
5609static const char *
5610get_csky_section_type_name (unsigned int sh_type)
5611{
5612 switch (sh_type)
5613 {
5614 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
5615 default: return NULL;
5616 }
5617}
5618
5619static const char *
5620get_section_type_name (Filedata * filedata, unsigned int sh_type)
5621{
5622 static char buff[32];
5623 const char * result;
5624
5625 switch (sh_type)
5626 {
5627 case SHT_NULL: return "NULL";
5628 case SHT_PROGBITS: return "PROGBITS";
5629 case SHT_SYMTAB: return "SYMTAB";
5630 case SHT_STRTAB: return "STRTAB";
5631 case SHT_RELA: return "RELA";
5632 case SHT_HASH: return "HASH";
5633 case SHT_DYNAMIC: return "DYNAMIC";
5634 case SHT_NOTE: return "NOTE";
5635 case SHT_NOBITS: return "NOBITS";
5636 case SHT_REL: return "REL";
5637 case SHT_SHLIB: return "SHLIB";
5638 case SHT_DYNSYM: return "DYNSYM";
5639 /* 12 and 13 are not defined. */
5640 case SHT_INIT_ARRAY: return "INIT_ARRAY";
5641 case SHT_FINI_ARRAY: return "FINI_ARRAY";
5642 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
5643 case SHT_GROUP: return "GROUP";
5644 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
5645 case SHT_RELR: return "RELR";
5646 /* End of generic section types. */
5647
5648 /* OS specific section types: */
5649 case SHT_GNU_verdef: return "VERDEF";
5650 case SHT_GNU_verneed: return "VERNEED";
5651 case SHT_GNU_versym: return "VERSYM";
5652 case SHT_GNU_INCREMENTAL_INPUTS: return "GNU_INCREMENTAL_INPUTS";
5653 case 0x6ffffff0: return "VERSYM";
5654 case SHT_GNU_ATTRIBUTES: return "GNU_ATTRIBUTES";
5655 case SHT_GNU_HASH: return "GNU_HASH";
5656 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
5657 case 0x6ffffffc: return "VERDEF";
5658 case 0x7ffffffd: return "AUXILIARY";
5659 case 0x7fffffff: return "FILTER";
5660
5661 default:
5662 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
5663 {
5664 switch (filedata->file_header.e_machine)
5665 {
5666 case EM_ARC:
5667 case EM_ARC_COMPACT:
5668 case EM_ARC_COMPACT2:
5669 case EM_ARC_COMPACT3:
5670 case EM_ARC_COMPACT3_64:
5671 result = get_arc_section_type_name (sh_type);
5672 break;
5673 case EM_MIPS:
5674 case EM_MIPS_RS3_LE:
5675 result = get_mips_section_type_name (sh_type);
5676 break;
5677 case EM_PARISC:
5678 result = get_parisc_section_type_name (sh_type);
5679 break;
5680 case EM_IA_64:
5681 result = get_ia64_section_type_name (filedata, sh_type);
5682 break;
5683 case EM_X86_64:
5684 case EM_L1OM:
5685 case EM_K1OM:
5686 result = get_x86_64_section_type_name (sh_type);
5687 break;
5688 case EM_AARCH64:
5689 result = get_aarch64_section_type_name (sh_type);
5690 break;
5691 case EM_ARM:
5692 result = get_arm_section_type_name (sh_type);
5693 break;
5694 case EM_TI_C6000:
5695 result = get_tic6x_section_type_name (sh_type);
5696 break;
5697 case EM_MSP430:
5698 result = get_msp430_section_type_name (sh_type);
5699 break;
5700 case EM_NFP:
5701 result = get_nfp_section_type_name (sh_type);
5702 break;
5703 case EM_V800:
5704 case EM_V850:
5705 case EM_CYGNUS_V850:
5706 result = get_v850_section_type_name (sh_type);
5707 break;
5708 case EM_RISCV:
5709 result = get_riscv_section_type_name (sh_type);
5710 break;
5711 case EM_CSKY:
5712 result = get_csky_section_type_name (sh_type);
5713 break;
5714 default:
5715 result = NULL;
5716 break;
5717 }
5718
5719 if (result != NULL)
5720 return result;
5721
5722 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
5723 }
5724 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
5725 {
5726 switch (filedata->file_header.e_machine)
5727 {
5728 case EM_IA_64:
5729 result = get_ia64_section_type_name (filedata, sh_type);
5730 break;
5731 default:
5732 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
5733 result = get_solaris_section_type (sh_type);
5734 else
5735 {
5736 switch (sh_type)
5737 {
5738 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
5739 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
5740 case SHT_GNU_HASH: result = "GNU_HASH"; break;
5741 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
5742 default:
5743 result = NULL;
5744 break;
5745 }
5746 }
5747 break;
5748 }
5749
5750 if (result != NULL)
5751 return result;
5752
5753 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
5754 }
5755 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
5756 {
5757 switch (filedata->file_header.e_machine)
5758 {
5759 case EM_V800:
5760 case EM_V850:
5761 case EM_CYGNUS_V850:
5762 result = get_v850_section_type_name (sh_type);
5763 break;
5764 default:
5765 result = NULL;
5766 break;
5767 }
5768
5769 if (result != NULL)
5770 return result;
5771
5772 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
5773 }
5774 else
5775 /* This message is probably going to be displayed in a 15
5776 character wide field, so put the hex value first. */
5777 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
5778
5779 return buff;
5780 }
5781}
5782
5783enum long_option_values
5784{
5785 OPTION_DEBUG_DUMP = 512,
5786 OPTION_DYN_SYMS,
5787 OPTION_LTO_SYMS,
5788 OPTION_DWARF_DEPTH,
5789 OPTION_DWARF_START,
5790 OPTION_DWARF_CHECK,
5791 OPTION_CTF_DUMP,
5792 OPTION_CTF_PARENT,
5793 OPTION_CTF_SYMBOLS,
5794 OPTION_CTF_STRINGS,
5795 OPTION_SFRAME_DUMP,
5796 OPTION_WITH_SYMBOL_VERSIONS,
5797 OPTION_RECURSE_LIMIT,
5798 OPTION_NO_RECURSE_LIMIT,
5799 OPTION_NO_DEMANGLING,
5800 OPTION_NO_EXTRA_SYM_INFO,
5801 OPTION_SYM_BASE
5802};
5803
5804static struct option options[] =
5805{
5806 /* Note - This table is alpha-sorted on the 'val'
5807 field in order to make adding new options easier. */
5808 {"arch-specific", no_argument, 0, 'A'},
5809 {"all", no_argument, 0, 'a'},
5810 {"demangle", optional_argument, 0, 'C'},
5811 {"archive-index", no_argument, 0, 'c'},
5812 {"use-dynamic", no_argument, 0, 'D'},
5813 {"dynamic", no_argument, 0, 'd'},
5814 {"headers", no_argument, 0, 'e'},
5815 {"section-groups", no_argument, 0, 'g'},
5816 {"help", no_argument, 0, 'H'},
5817 {"file-header", no_argument, 0, 'h'},
5818 {"histogram", no_argument, 0, 'I'},
5819 {"display-section", required_argument, 0, 'j'},
5820 {"lint", no_argument, 0, 'L'},
5821 {"enable-checks", no_argument, 0, 'L'},
5822 {"program-headers", no_argument, 0, 'l'},
5823 {"segments", no_argument, 0, 'l'},
5824 {"full-section-name",no_argument, 0, 'N'},
5825 {"notes", no_argument, 0, 'n'},
5826 {"process-links", no_argument, 0, 'P'},
5827 {"string-dump", required_argument, 0, 'p'},
5828 {"relocated-dump", required_argument, 0, 'R'},
5829 {"relocs", no_argument, 0, 'r'},
5830 {"section-headers", no_argument, 0, 'S'},
5831 {"sections", no_argument, 0, 'S'},
5832 {"symbols", no_argument, 0, 's'},
5833 {"syms", no_argument, 0, 's'},
5834 {"silent-truncation",no_argument, 0, 'T'},
5835 {"section-details", no_argument, 0, 't'},
5836 {"unicode", required_argument, NULL, 'U'},
5837 {"unwind", no_argument, 0, 'u'},
5838 {"version-info", no_argument, 0, 'V'},
5839 {"version", no_argument, 0, 'v'},
5840 {"wide", no_argument, 0, 'W'},
5841 {"extra-sym-info", no_argument, 0, 'X'},
5842 {"hex-dump", required_argument, 0, 'x'},
5843 {"decompress", no_argument, 0, 'z'},
5844
5845 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
5846 {"no-extra-sym-info",no_argument, 0, OPTION_NO_EXTRA_SYM_INFO},
5847 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
5848 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5849 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5850 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
5851 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
5852 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
5853 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
5854 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
5855 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
5856#ifdef ENABLE_LIBCTF
5857 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
5858 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
5859 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
5860 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
5861#endif
5862 {"sframe", optional_argument, 0, OPTION_SFRAME_DUMP},
5863 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
5864
5865 {0, no_argument, 0, 0}
5866};
5867
5868static void
5869usage (FILE * stream)
5870{
5871 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
5872 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
5873 fprintf (stream, _(" Options are:\n"));
5874 fprintf (stream, _("\
5875 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
5876 fprintf (stream, _("\
5877 -h --file-header Display the ELF file header\n"));
5878 fprintf (stream, _("\
5879 -l --program-headers Display the program headers\n"));
5880 fprintf (stream, _("\
5881 --segments An alias for --program-headers\n"));
5882 fprintf (stream, _("\
5883 -S --section-headers Display the sections' header\n"));
5884 fprintf (stream, _("\
5885 --sections An alias for --section-headers\n"));
5886 fprintf (stream, _("\
5887 -g --section-groups Display the section groups\n"));
5888 fprintf (stream, _("\
5889 -t --section-details Display the section details\n"));
5890 fprintf (stream, _("\
5891 -e --headers Equivalent to: -h -l -S\n"));
5892 fprintf (stream, _("\
5893 -s --syms Display the symbol table\n"));
5894 fprintf (stream, _("\
5895 --symbols An alias for --syms\n"));
5896 fprintf (stream, _("\
5897 --dyn-syms Display the dynamic symbol table\n"));
5898 fprintf (stream, _("\
5899 --lto-syms Display LTO symbol tables\n"));
5900 fprintf (stream, _("\
5901 --sym-base=[0|8|10|16] \n\
5902 Force base for symbol sizes. The options are \n\
5903 mixed (the default), octal, decimal, hexadecimal.\n"));
5904 fprintf (stream, _("\
5905 -C --demangle[=STYLE] Decode mangled/processed symbol names\n"));
5906 display_demangler_styles (stream, _("\
5907 STYLE can be "));
5908 fprintf (stream, _("\
5909 --no-demangle Do not demangle low-level symbol names. (default)\n"));
5910 fprintf (stream, _("\
5911 --recurse-limit Enable a demangling recursion limit. (default)\n"));
5912 fprintf (stream, _("\
5913 --no-recurse-limit Disable a demangling recursion limit\n"));
5914 fprintf (stream, _("\
5915 -U[dlexhi] --unicode=[default|locale|escape|hex|highlight|invalid]\n\
5916 Display unicode characters as determined by the current locale\n\
5917 (default), escape sequences, \"<hex sequences>\", highlighted\n\
5918 escape sequences, or treat them as invalid and display as\n\
5919 \"{hex sequences}\"\n"));
5920 fprintf (stream, _("\
5921 -X --extra-sym-info Display extra information when showing symbols\n"));
5922 fprintf (stream, _("\
5923 --no-extra-sym-info Do not display extra information when showing symbols (default)\n"));
5924 fprintf (stream, _("\
5925 -n --notes Display the contents of note sections (if present)\n"));
5926 fprintf (stream, _("\
5927 -r --relocs Display the relocations (if present)\n"));
5928 fprintf (stream, _("\
5929 -u --unwind Display the unwind info (if present)\n"));
5930 fprintf (stream, _("\
5931 -d --dynamic Display the dynamic section (if present)\n"));
5932 fprintf (stream, _("\
5933 -V --version-info Display the version sections (if present)\n"));
5934 fprintf (stream, _("\
5935 -A --arch-specific Display architecture specific information (if any)\n"));
5936 fprintf (stream, _("\
5937 -c --archive-index Display the symbol/file index in an archive\n"));
5938 fprintf (stream, _("\
5939 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
5940 fprintf (stream, _("\
5941 -L --lint|--enable-checks\n\
5942 Display warning messages for possible problems\n"));
5943 fprintf (stream, _("\
5944 -x --hex-dump=<number|name>\n\
5945 Dump the contents of section <number|name> as bytes\n"));
5946 fprintf (stream, _("\
5947 -p --string-dump=<number|name>\n\
5948 Dump the contents of section <number|name> as strings\n"));
5949 fprintf (stream, _("\
5950 -R --relocated-dump=<number|name>\n\
5951 Dump the relocated contents of section <number|name>\n"));
5952 fprintf (stream, _("\
5953 -z --decompress Decompress section before dumping it\n"));
5954 fprintf (stream, _("\n\
5955 -j --display-section=<name|number>\n\
5956 Display the contents of the indicated section. Can be repeated\n"));
5957 fprintf (stream, _("\
5958 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
5959 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
5960 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
5961 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
5962 U/=trace_info]\n\
5963 Display the contents of DWARF debug sections\n"));
5964 fprintf (stream, _("\
5965 -wk --debug-dump=links Display the contents of sections that link to separate\n\
5966 debuginfo files\n"));
5967 fprintf (stream, _("\
5968 -P --process-links Display the contents of non-debug sections in separate\n\
5969 debuginfo files. (Implies -wK)\n"));
5970#if DEFAULT_FOR_FOLLOW_LINKS
5971 fprintf (stream, _("\
5972 -wK --debug-dump=follow-links\n\
5973 Follow links to separate debug info files (default)\n"));
5974 fprintf (stream, _("\
5975 -wN --debug-dump=no-follow-links\n\
5976 Do not follow links to separate debug info files\n"));
5977#else
5978 fprintf (stream, _("\
5979 -wK --debug-dump=follow-links\n\
5980 Follow links to separate debug info files\n"));
5981 fprintf (stream, _("\
5982 -wN --debug-dump=no-follow-links\n\
5983 Do not follow links to separate debug info files\n\
5984 (default)\n"));
5985#endif
5986#if HAVE_LIBDEBUGINFOD
5987 fprintf (stream, _("\
5988 -wD --debug-dump=use-debuginfod\n\
5989 When following links, also query debuginfod servers (default)\n"));
5990 fprintf (stream, _("\
5991 -wE --debug-dump=do-not-use-debuginfod\n\
5992 When following links, do not query debuginfod servers\n"));
5993#endif
5994 fprintf (stream, _("\
5995 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
5996 fprintf (stream, _("\
5997 --dwarf-start=N Display DIEs starting at offset N\n"));
5998#ifdef ENABLE_LIBCTF
5999 fprintf (stream, _("\
6000 --ctf=<number|name> Display CTF info from section <number|name>\n"));
6001 fprintf (stream, _("\
6002 --ctf-parent=<name> Use CTF archive member <name> as the CTF parent\n"));
6003 fprintf (stream, _("\
6004 --ctf-symbols=<number|name>\n\
6005 Use section <number|name> as the CTF external symtab\n"));
6006 fprintf (stream, _("\
6007 --ctf-strings=<number|name>\n\
6008 Use section <number|name> as the CTF external strtab\n"));
6009#endif
6010 fprintf (stream, _("\
6011 --sframe[=NAME] Display SFrame info from section NAME, (default '.sframe')\n"));
6012
6013#ifdef SUPPORT_DISASSEMBLY
6014 fprintf (stream, _("\
6015 -i --instruction-dump=<number|name>\n\
6016 Disassemble the contents of section <number|name>\n"));
6017#endif
6018 fprintf (stream, _("\
6019 -I --histogram Display histogram of bucket list lengths\n"));
6020 fprintf (stream, _("\
6021 -W --wide Allow output width to exceed 80 characters\n"));
6022 fprintf (stream, _("\
6023 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
6024 fprintf (stream, _("\
6025 @<file> Read options from <file>\n"));
6026 fprintf (stream, _("\
6027 -H --help Display this information\n"));
6028 fprintf (stream, _("\
6029 -v --version Display the version number of readelf\n"));
6030
6031 if (REPORT_BUGS_TO[0] && stream == stdout)
6032 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
6033
6034 exit (stream == stdout ? 0 : 1);
6035}
6036
6037/* Record the fact that the user wants the contents of section number
6038 SECTION to be displayed using the method(s) encoded as flags bits
6039 in TYPE. Note, TYPE can be zero if we are creating the array for
6040 the first time. */
6041
6042static void
6043request_dump_bynumber (struct dump_data *dumpdata,
6044 unsigned int section, dump_type type)
6045{
6046 if (section >= dumpdata->num_dump_sects)
6047 {
6048 dump_type * new_dump_sects;
6049
6050 new_dump_sects = (dump_type *) calloc (section + 1,
6051 sizeof (* new_dump_sects));
6052
6053 if (new_dump_sects == NULL)
6054 error (_("Out of memory allocating dump request table.\n"));
6055 else
6056 {
6057 if (dumpdata->dump_sects)
6058 {
6059 /* Copy current flag settings. */
6060 memcpy (new_dump_sects, dumpdata->dump_sects,
6061 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
6062
6063 free (dumpdata->dump_sects);
6064 }
6065
6066 dumpdata->dump_sects = new_dump_sects;
6067 dumpdata->num_dump_sects = section + 1;
6068 }
6069 }
6070
6071 if (dumpdata->dump_sects)
6072 dumpdata->dump_sects[section] |= type;
6073}
6074
6075/* Request a dump by section name. */
6076
6077static void
6078request_dump_byname (const char * section, dump_type type)
6079{
6080 struct dump_list_entry * new_request;
6081
6082 new_request = (struct dump_list_entry *)
6083 malloc (sizeof (struct dump_list_entry));
6084 if (!new_request)
6085 error (_("Out of memory allocating dump request table.\n"));
6086
6087 new_request->name = strdup (section);
6088 if (!new_request->name)
6089 error (_("Out of memory allocating dump request table.\n"));
6090
6091 new_request->type = type;
6092
6093 new_request->next = dump_sects_byname;
6094 dump_sects_byname = new_request;
6095}
6096
6097static inline void
6098request_dump (struct dump_data *dumpdata, dump_type type)
6099{
6100 int section;
6101 char * cp;
6102
6103 do_dump = true;
6104 section = strtoul (optarg, & cp, 0);
6105
6106 if (! *cp && section >= 0)
6107 request_dump_bynumber (dumpdata, section, type);
6108 else
6109 request_dump_byname (optarg, type);
6110}
6111
6112static void
6113parse_args (struct dump_data *dumpdata, int argc, char ** argv)
6114{
6115 int c;
6116
6117 if (argc < 2)
6118 usage (stderr);
6119
6120 while ((c = getopt_long
6121 (argc, argv, "ACDHILNPR:STU:VWXacdeghi:j:lnp:rstuvw::x:z", options, NULL)) != EOF)
6122 {
6123 switch (c)
6124 {
6125 case 0:
6126 /* Long options. */
6127 break;
6128 case 'H':
6129 usage (stdout);
6130 break;
6131
6132 case 'a':
6133 do_syms = true;
6134 do_reloc = true;
6135 do_unwind = true;
6136 do_dynamic = true;
6137 do_header = true;
6138 do_sections = true;
6139 do_section_groups = true;
6140 do_segments = true;
6141 do_version = true;
6142 do_histogram = true;
6143 do_arch = true;
6144 do_notes = true;
6145 break;
6146
6147 case 'g':
6148 do_section_groups = true;
6149 break;
6150 case 't':
6151 case 'N':
6152 do_sections = true;
6153 do_section_details = true;
6154 break;
6155 case 'e':
6156 do_header = true;
6157 do_sections = true;
6158 do_segments = true;
6159 break;
6160 case 'A':
6161 do_arch = true;
6162 break;
6163 case 'D':
6164 do_using_dynamic = true;
6165 break;
6166 case 'r':
6167 do_reloc = true;
6168 break;
6169 case 'u':
6170 do_unwind = true;
6171 break;
6172 case 'h':
6173 do_header = true;
6174 break;
6175 case 'l':
6176 do_segments = true;
6177 break;
6178 case 's':
6179 do_syms = true;
6180 break;
6181 case 'S':
6182 do_sections = true;
6183 break;
6184 case 'd':
6185 do_dynamic = true;
6186 break;
6187 case 'I':
6188 do_histogram = true;
6189 break;
6190 case 'n':
6191 do_notes = true;
6192 break;
6193 case 'c':
6194 do_archive_index = true;
6195 break;
6196 case 'L':
6197 do_checks = true;
6198 break;
6199 case 'P':
6200 process_links = true;
6201 do_follow_links = true;
6202 dump_any_debugging = true;
6203 break;
6204 case 'j':
6205 request_dump (dumpdata, AUTO_DUMP);
6206 break;
6207 case 'x':
6208 request_dump (dumpdata, HEX_DUMP);
6209 break;
6210 case 'p':
6211 request_dump (dumpdata, STRING_DUMP);
6212 break;
6213 case 'R':
6214 request_dump (dumpdata, RELOC_DUMP);
6215 break;
6216 case 'z':
6217 decompress_dumps = true;
6218 break;
6219 case 'w':
6220 if (optarg == NULL)
6221 {
6222 do_debugging = true;
6223 do_dump = true;
6224 dump_any_debugging = true;
6225 dwarf_select_sections_all ();
6226 }
6227 else
6228 {
6229 do_debugging = false;
6230 if (dwarf_select_sections_by_letters (optarg))
6231 {
6232 do_dump = true;
6233 dump_any_debugging = true;
6234 }
6235 }
6236 break;
6237 case OPTION_DEBUG_DUMP:
6238 if (optarg == NULL)
6239 {
6240 do_dump = true;
6241 do_debugging = true;
6242 dump_any_debugging = true;
6243 dwarf_select_sections_all ();
6244 }
6245 else
6246 {
6247 do_debugging = false;
6248 if (dwarf_select_sections_by_names (optarg))
6249 {
6250 do_dump = true;
6251 dump_any_debugging = true;
6252 }
6253 }
6254 break;
6255 case OPTION_DWARF_DEPTH:
6256 {
6257 char *cp;
6258
6259 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
6260 }
6261 break;
6262 case OPTION_DWARF_START:
6263 {
6264 char *cp;
6265
6266 dwarf_start_die = strtoul (optarg, & cp, 0);
6267 }
6268 break;
6269 case OPTION_DWARF_CHECK:
6270 dwarf_check = true;
6271 break;
6272 case OPTION_CTF_DUMP:
6273 do_ctf = true;
6274 request_dump (dumpdata, CTF_DUMP);
6275 break;
6276 case OPTION_CTF_SYMBOLS:
6277 free (dump_ctf_symtab_name);
6278 dump_ctf_symtab_name = strdup (optarg);
6279 break;
6280 case OPTION_CTF_STRINGS:
6281 free (dump_ctf_strtab_name);
6282 dump_ctf_strtab_name = strdup (optarg);
6283 break;
6284 case OPTION_CTF_PARENT:
6285 free (dump_ctf_parent_name);
6286 dump_ctf_parent_name = strdup (optarg);
6287 break;
6288 case OPTION_SFRAME_DUMP:
6289 do_sframe = true;
6290 /* Providing section name is optional. request_dump (), however,
6291 thrives on non NULL optarg. Handle it explicitly here. */
6292 if (optarg != NULL)
6293 request_dump (dumpdata, SFRAME_DUMP);
6294 else
6295 {
6296 do_dump = true;
6297 const char *sframe_sec_name = strdup (".sframe");
6298 request_dump_byname (sframe_sec_name, SFRAME_DUMP);
6299 }
6300 break;
6301 case OPTION_DYN_SYMS:
6302 do_dyn_syms = true;
6303 break;
6304 case OPTION_LTO_SYMS:
6305 do_lto_syms = true;
6306 break;
6307 case 'X':
6308 extra_sym_info = true;
6309 break;
6310 case OPTION_NO_EXTRA_SYM_INFO:
6311 extra_sym_info = false;
6312 break;
6313
6314#ifdef SUPPORT_DISASSEMBLY
6315 case 'i':
6316 request_dump (dumpdata, DISASS_DUMP);
6317 break;
6318#endif
6319 case 'v':
6320 print_version (program_name);
6321 break;
6322 case 'V':
6323 do_version = true;
6324 break;
6325 case 'W':
6326 do_wide = true;
6327 break;
6328 case 'T':
6329 do_not_show_symbol_truncation = true;
6330 break;
6331 case 'C':
6332 do_demangle = true;
6333 if (optarg != NULL)
6334 {
6335 enum demangling_styles style;
6336
6337 style = cplus_demangle_name_to_style (optarg);
6338 if (style == unknown_demangling)
6339 error (_("unknown demangling style `%s'"), optarg);
6340
6341 cplus_demangle_set_style (style);
6342 }
6343 break;
6344 case OPTION_NO_DEMANGLING:
6345 do_demangle = false;
6346 break;
6347 case OPTION_RECURSE_LIMIT:
6348 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
6349 break;
6350 case OPTION_NO_RECURSE_LIMIT:
6351 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
6352 break;
6353 case OPTION_WITH_SYMBOL_VERSIONS:
6354 /* Ignored for backward compatibility. */
6355 break;
6356
6357 case 'U':
6358 if (optarg == NULL)
6359 error (_("Missing arg to -U/--unicode")); /* Can this happen ? */
6360 else if (streq (optarg, "default") || streq (optarg, "d"))
6361 unicode_display = unicode_default;
6362 else if (streq (optarg, "locale") || streq (optarg, "l"))
6363 unicode_display = unicode_locale;
6364 else if (streq (optarg, "escape") || streq (optarg, "e"))
6365 unicode_display = unicode_escape;
6366 else if (streq (optarg, "invalid") || streq (optarg, "i"))
6367 unicode_display = unicode_invalid;
6368 else if (streq (optarg, "hex") || streq (optarg, "x"))
6369 unicode_display = unicode_hex;
6370 else if (streq (optarg, "highlight") || streq (optarg, "h"))
6371 unicode_display = unicode_highlight;
6372 else
6373 error (_("invalid argument to -U/--unicode: %s"), optarg);
6374 break;
6375
6376 case OPTION_SYM_BASE:
6377 sym_base = 0;
6378 if (optarg != NULL)
6379 {
6380 sym_base = strtoul (optarg, NULL, 0);
6381 switch (sym_base)
6382 {
6383 case 0:
6384 case 8:
6385 case 10:
6386 case 16:
6387 break;
6388
6389 default:
6390 sym_base = 0;
6391 break;
6392 }
6393 }
6394 break;
6395
6396 default:
6397 /* xgettext:c-format */
6398 error (_("Invalid option '-%c'\n"), c);
6399 /* Fall through. */
6400 case '?':
6401 usage (stderr);
6402 }
6403 }
6404
6405 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
6406 && !do_segments && !do_header && !do_dump && !do_version
6407 && !do_histogram && !do_debugging && !do_arch && !do_notes
6408 && !do_section_groups && !do_archive_index
6409 && !do_dyn_syms && !do_lto_syms)
6410 {
6411 if (do_checks)
6412 {
6413 check_all = true;
6414 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
6415 do_segments = do_header = do_dump = do_version = true;
6416 do_histogram = do_debugging = do_arch = do_notes = true;
6417 do_section_groups = do_archive_index = do_dyn_syms = true;
6418 do_lto_syms = true;
6419 }
6420 else
6421 usage (stderr);
6422 }
6423}
6424
6425static const char *
6426get_elf_class (unsigned int elf_class)
6427{
6428 static char buff[32];
6429
6430 switch (elf_class)
6431 {
6432 case ELFCLASSNONE: return _("none");
6433 case ELFCLASS32: return "ELF32";
6434 case ELFCLASS64: return "ELF64";
6435 default:
6436 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
6437 return buff;
6438 }
6439}
6440
6441static const char *
6442get_data_encoding (unsigned int encoding)
6443{
6444 static char buff[32];
6445
6446 switch (encoding)
6447 {
6448 case ELFDATANONE: return _("none");
6449 case ELFDATA2LSB: return _("2's complement, little endian");
6450 case ELFDATA2MSB: return _("2's complement, big endian");
6451 default:
6452 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
6453 return buff;
6454 }
6455}
6456
6457static bool
6458check_magic_number (Filedata * filedata, Elf_Internal_Ehdr * header)
6459{
6460 if (header->e_ident[EI_MAG0] == ELFMAG0
6461 && header->e_ident[EI_MAG1] == ELFMAG1
6462 && header->e_ident[EI_MAG2] == ELFMAG2
6463 && header->e_ident[EI_MAG3] == ELFMAG3)
6464 return true;
6465
6466 /* Some compilers produce object files that are not in the ELF file format.
6467 As an aid to users of readelf, try to identify these cases and suggest
6468 alternative tools.
6469
6470 FIXME: It is not clear if all four bytes are used as constant magic
6471 valus by all compilers. It may be necessary to recode this function if
6472 different tools use different length sequences. */
6473
6474 static struct
6475 {
6476 unsigned char magic[4];
6477 const char * obj_message;
6478 const char * ar_message;
6479 }
6480 known_magic[] =
6481 {
6482 { { 'B', 'C', 0xc0, 0xde },
6483 N_("This is a LLVM bitcode file - try using llvm-bcanalyzer\n"),
6484 N_("This is a LLVM bitcode file - try extracting and then using llvm-bcanalyzer\n")
6485 },
6486 { { 'g', 'o', ' ', 'o' },
6487 N_("This is a GO binary file - try using 'go tool objdump' or 'go tool nm'\n"),
6488 NULL
6489 }
6490 };
6491 int i;
6492
6493 for (i = ARRAY_SIZE (known_magic); i--;)
6494 {
6495 if (header->e_ident[EI_MAG0] == known_magic[i].magic[0]
6496 && header->e_ident[EI_MAG1] == known_magic[i].magic[1]
6497 && header->e_ident[EI_MAG2] == known_magic[i].magic[2]
6498 && header->e_ident[EI_MAG3] == known_magic[i].magic[3])
6499 {
6500 /* Some compiler's analyzer tools do not handle archives,
6501 so we provide two different kinds of error message. */
6502 if (filedata->archive_file_size > 0
6503 && known_magic[i].ar_message != NULL)
6504 error ("%s", known_magic[i].ar_message);
6505 else
6506 error ("%s", known_magic[i].obj_message);
6507 return false;
6508 }
6509 }
6510
6511 error (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
6512 return false;
6513}
6514
6515/* Decode the data held in 'filedata->file_header'. */
6516
6517static bool
6518process_file_header (Filedata * filedata)
6519{
6520 Elf_Internal_Ehdr * header = & filedata->file_header;
6521
6522 if (! check_magic_number (filedata, header))
6523 return false;
6524
6525 if (! filedata->is_separate)
6526 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
6527
6528 if (do_header)
6529 {
6530 unsigned i;
6531
6532 if (filedata->is_separate)
6533 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
6534 else
6535 printf (_("ELF Header:\n"));
6536 printf (_(" Magic: "));
6537 for (i = 0; i < EI_NIDENT; i++)
6538 printf ("%2.2x ", header->e_ident[i]);
6539 printf ("\n");
6540 printf (_(" Class: %s\n"),
6541 get_elf_class (header->e_ident[EI_CLASS]));
6542 printf (_(" Data: %s\n"),
6543 get_data_encoding (header->e_ident[EI_DATA]));
6544 printf (_(" Version: %d%s\n"),
6545 header->e_ident[EI_VERSION],
6546 (header->e_ident[EI_VERSION] == EV_CURRENT
6547 ? _(" (current)")
6548 : (header->e_ident[EI_VERSION] != EV_NONE
6549 ? _(" <unknown>")
6550 : "")));
6551 printf (_(" OS/ABI: %s\n"),
6552 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
6553 printf (_(" ABI Version: %d\n"),
6554 header->e_ident[EI_ABIVERSION]);
6555 printf (_(" Type: %s\n"),
6556 get_file_type (filedata));
6557 printf (_(" Machine: %s\n"),
6558 get_machine_name (header->e_machine));
6559 printf (_(" Version: 0x%lx\n"),
6560 header->e_version);
6561
6562 printf (_(" Entry point address: "));
6563 print_vma (header->e_entry, PREFIX_HEX);
6564 printf (_("\n Start of program headers: "));
6565 print_vma (header->e_phoff, DEC);
6566 printf (_(" (bytes into file)\n Start of section headers: "));
6567 print_vma (header->e_shoff, DEC);
6568 printf (_(" (bytes into file)\n"));
6569
6570 printf (_(" Flags: 0x%lx%s\n"),
6571 header->e_flags,
6572 get_machine_flags (filedata, header->e_flags, header->e_machine));
6573 printf (_(" Size of this header: %u (bytes)\n"),
6574 header->e_ehsize);
6575 printf (_(" Size of program headers: %u (bytes)\n"),
6576 header->e_phentsize);
6577 printf (_(" Number of program headers: %u"),
6578 header->e_phnum);
6579 if (filedata->section_headers != NULL
6580 && header->e_phnum == PN_XNUM
6581 && filedata->section_headers[0].sh_info != 0)
6582 printf (" (%u)", filedata->section_headers[0].sh_info);
6583 putc ('\n', stdout);
6584 printf (_(" Size of section headers: %u (bytes)\n"),
6585 header->e_shentsize);
6586 printf (_(" Number of section headers: %u"),
6587 header->e_shnum);
6588 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
6589 {
6590 header->e_shnum = filedata->section_headers[0].sh_size;
6591 printf (" (%u)", header->e_shnum);
6592 }
6593 putc ('\n', stdout);
6594 printf (_(" Section header string table index: %u"),
6595 header->e_shstrndx);
6596 if (filedata->section_headers != NULL
6597 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
6598 {
6599 header->e_shstrndx = filedata->section_headers[0].sh_link;
6600 printf (" (%u)", header->e_shstrndx);
6601 }
6602 if (header->e_shstrndx != SHN_UNDEF
6603 && header->e_shstrndx >= header->e_shnum)
6604 {
6605 header->e_shstrndx = SHN_UNDEF;
6606 printf (_(" <corrupt: out of range>"));
6607 }
6608 putc ('\n', stdout);
6609 }
6610
6611 if (filedata->section_headers != NULL)
6612 {
6613 if (header->e_phnum == PN_XNUM
6614 && filedata->section_headers[0].sh_info != 0)
6615 {
6616 /* Throw away any cached read of PN_XNUM headers. */
6617 free (filedata->program_headers);
6618 filedata->program_headers = NULL;
6619 header->e_phnum = filedata->section_headers[0].sh_info;
6620 }
6621 if (header->e_shnum == SHN_UNDEF)
6622 header->e_shnum = filedata->section_headers[0].sh_size;
6623 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
6624 header->e_shstrndx = filedata->section_headers[0].sh_link;
6625 if (header->e_shstrndx >= header->e_shnum)
6626 header->e_shstrndx = SHN_UNDEF;
6627 }
6628
6629 return true;
6630}
6631
6632/* Read in the program headers from FILEDATA and store them in PHEADERS.
6633 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
6634
6635static bool
6636get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
6637{
6638 Elf32_External_Phdr * phdrs;
6639 Elf32_External_Phdr * external;
6640 Elf_Internal_Phdr * internal;
6641 unsigned int i;
6642 unsigned int size = filedata->file_header.e_phentsize;
6643 unsigned int num = filedata->file_header.e_phnum;
6644
6645 /* PR binutils/17531: Cope with unexpected section header sizes. */
6646 if (size == 0 || num == 0)
6647 return false;
6648 if (size < sizeof * phdrs)
6649 {
6650 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
6651 return false;
6652 }
6653 if (size > sizeof * phdrs)
6654 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
6655
6656 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
6657 size, num, _("program headers"));
6658 if (phdrs == NULL)
6659 return false;
6660
6661 for (i = 0, internal = pheaders, external = phdrs;
6662 i < filedata->file_header.e_phnum;
6663 i++, internal++, external++)
6664 {
6665 internal->p_type = BYTE_GET (external->p_type);
6666 internal->p_offset = BYTE_GET (external->p_offset);
6667 internal->p_vaddr = BYTE_GET (external->p_vaddr);
6668 internal->p_paddr = BYTE_GET (external->p_paddr);
6669 internal->p_filesz = BYTE_GET (external->p_filesz);
6670 internal->p_memsz = BYTE_GET (external->p_memsz);
6671 internal->p_flags = BYTE_GET (external->p_flags);
6672 internal->p_align = BYTE_GET (external->p_align);
6673 }
6674
6675 free (phdrs);
6676 return true;
6677}
6678
6679/* Read in the program headers from FILEDATA and store them in PHEADERS.
6680 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
6681
6682static bool
6683get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
6684{
6685 Elf64_External_Phdr * phdrs;
6686 Elf64_External_Phdr * external;
6687 Elf_Internal_Phdr * internal;
6688 unsigned int i;
6689 unsigned int size = filedata->file_header.e_phentsize;
6690 unsigned int num = filedata->file_header.e_phnum;
6691
6692 /* PR binutils/17531: Cope with unexpected section header sizes. */
6693 if (size == 0 || num == 0)
6694 return false;
6695 if (size < sizeof * phdrs)
6696 {
6697 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
6698 return false;
6699 }
6700 if (size > sizeof * phdrs)
6701 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
6702
6703 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
6704 size, num, _("program headers"));
6705 if (!phdrs)
6706 return false;
6707
6708 for (i = 0, internal = pheaders, external = phdrs;
6709 i < filedata->file_header.e_phnum;
6710 i++, internal++, external++)
6711 {
6712 internal->p_type = BYTE_GET (external->p_type);
6713 internal->p_flags = BYTE_GET (external->p_flags);
6714 internal->p_offset = BYTE_GET (external->p_offset);
6715 internal->p_vaddr = BYTE_GET (external->p_vaddr);
6716 internal->p_paddr = BYTE_GET (external->p_paddr);
6717 internal->p_filesz = BYTE_GET (external->p_filesz);
6718 internal->p_memsz = BYTE_GET (external->p_memsz);
6719 internal->p_align = BYTE_GET (external->p_align);
6720 }
6721
6722 free (phdrs);
6723 return true;
6724}
6725
6726/* Returns TRUE if the program headers were read into `program_headers'. */
6727
6728static bool
6729get_program_headers (Filedata * filedata)
6730{
6731 Elf_Internal_Phdr * phdrs;
6732
6733 /* Check cache of prior read. */
6734 if (filedata->program_headers != NULL)
6735 return true;
6736
6737 /* Be kind to memory checkers by looking for
6738 e_phnum values which we know must be invalid. */
6739 if (filedata->file_header.e_phnum
6740 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
6741 >= filedata->file_size)
6742 {
6743 error (_("Too many program headers - %#x - the file is not that big\n"),
6744 filedata->file_header.e_phnum);
6745 return false;
6746 }
6747
6748 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
6749 sizeof (Elf_Internal_Phdr));
6750 if (phdrs == NULL)
6751 {
6752 error (_("Out of memory reading %u program headers\n"),
6753 filedata->file_header.e_phnum);
6754 return false;
6755 }
6756
6757 if (is_32bit_elf
6758 ? get_32bit_program_headers (filedata, phdrs)
6759 : get_64bit_program_headers (filedata, phdrs))
6760 {
6761 filedata->program_headers = phdrs;
6762 return true;
6763 }
6764
6765 free (phdrs);
6766 return false;
6767}
6768
6769/* Print program header info and locate dynamic section. */
6770
6771static void
6772process_program_headers (Filedata * filedata)
6773{
6774 Elf_Internal_Phdr * segment;
6775 unsigned int i;
6776 Elf_Internal_Phdr * previous_load = NULL;
6777
6778 if (filedata->file_header.e_phnum == 0)
6779 {
6780 /* PR binutils/12467. */
6781 if (filedata->file_header.e_phoff != 0)
6782 warn (_("possibly corrupt ELF header - it has a non-zero program"
6783 " header offset, but no program headers\n"));
6784 else if (do_segments)
6785 {
6786 if (filedata->is_separate)
6787 printf (_("\nThere are no program headers in linked file '%s'.\n"),
6788 filedata->file_name);
6789 else
6790 printf (_("\nThere are no program headers in this file.\n"));
6791 }
6792 goto no_headers;
6793 }
6794
6795 if (do_segments && !do_header)
6796 {
6797 if (filedata->is_separate)
6798 printf ("\nIn linked file '%s' the ELF file type is %s\n",
6799 filedata->file_name, get_file_type (filedata));
6800 else
6801 printf (_("\nElf file type is %s\n"), get_file_type (filedata));
6802 printf (_("Entry point 0x%" PRIx64 "\n"),
6803 filedata->file_header.e_entry);
6804 printf (ngettext ("There is %d program header,"
6805 " starting at offset %" PRIu64 "\n",
6806 "There are %d program headers,"
6807 " starting at offset %" PRIu64 "\n",
6808 filedata->file_header.e_phnum),
6809 filedata->file_header.e_phnum,
6810 filedata->file_header.e_phoff);
6811 }
6812
6813 if (! get_program_headers (filedata))
6814 goto no_headers;
6815
6816 if (do_segments)
6817 {
6818 if (filedata->file_header.e_phnum > 1)
6819 printf (_("\nProgram Headers:\n"));
6820 else
6821 printf (_("\nProgram Headers:\n"));
6822
6823 if (is_32bit_elf)
6824 printf
6825 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6826 else if (do_wide)
6827 printf
6828 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6829 else
6830 {
6831 printf
6832 (_(" Type Offset VirtAddr PhysAddr\n"));
6833 printf
6834 (_(" FileSiz MemSiz Flags Align\n"));
6835 }
6836 }
6837
6838 uint64_t dynamic_addr = 0;
6839 uint64_t dynamic_size = 0;
6840 for (i = 0, segment = filedata->program_headers;
6841 i < filedata->file_header.e_phnum;
6842 i++, segment++)
6843 {
6844 if (do_segments)
6845 {
6846 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
6847
6848 if (is_32bit_elf)
6849 {
6850 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6851 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
6852 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
6853 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
6854 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
6855 printf ("%c%c%c ",
6856 (segment->p_flags & PF_R ? 'R' : ' '),
6857 (segment->p_flags & PF_W ? 'W' : ' '),
6858 (segment->p_flags & PF_X ? 'E' : ' '));
6859 printf ("%#lx", (unsigned long) segment->p_align);
6860 }
6861 else if (do_wide)
6862 {
6863 if ((unsigned long) segment->p_offset == segment->p_offset)
6864 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6865 else
6866 {
6867 print_vma (segment->p_offset, FULL_HEX);
6868 putchar (' ');
6869 }
6870
6871 print_vma (segment->p_vaddr, FULL_HEX);
6872 putchar (' ');
6873 print_vma (segment->p_paddr, FULL_HEX);
6874 putchar (' ');
6875
6876 if ((unsigned long) segment->p_filesz == segment->p_filesz)
6877 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
6878 else
6879 {
6880 print_vma (segment->p_filesz, FULL_HEX);
6881 putchar (' ');
6882 }
6883
6884 if ((unsigned long) segment->p_memsz == segment->p_memsz)
6885 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
6886 else
6887 {
6888 print_vma (segment->p_memsz, FULL_HEX);
6889 }
6890
6891 printf (" %c%c%c ",
6892 (segment->p_flags & PF_R ? 'R' : ' '),
6893 (segment->p_flags & PF_W ? 'W' : ' '),
6894 (segment->p_flags & PF_X ? 'E' : ' '));
6895
6896 if ((unsigned long) segment->p_align == segment->p_align)
6897 printf ("%#lx", (unsigned long) segment->p_align);
6898 else
6899 {
6900 print_vma (segment->p_align, PREFIX_HEX);
6901 }
6902 }
6903 else
6904 {
6905 print_vma (segment->p_offset, FULL_HEX);
6906 putchar (' ');
6907 print_vma (segment->p_vaddr, FULL_HEX);
6908 putchar (' ');
6909 print_vma (segment->p_paddr, FULL_HEX);
6910 printf ("\n ");
6911 print_vma (segment->p_filesz, FULL_HEX);
6912 putchar (' ');
6913 print_vma (segment->p_memsz, FULL_HEX);
6914 printf (" %c%c%c ",
6915 (segment->p_flags & PF_R ? 'R' : ' '),
6916 (segment->p_flags & PF_W ? 'W' : ' '),
6917 (segment->p_flags & PF_X ? 'E' : ' '));
6918 print_vma (segment->p_align, PREFIX_HEX);
6919 }
6920
6921 putc ('\n', stdout);
6922 }
6923
6924 switch (segment->p_type)
6925 {
6926 case PT_LOAD:
6927#if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
6928 required by the ELF standard, several programs, including the Linux
6929 kernel, make use of non-ordered segments. */
6930 if (previous_load
6931 && previous_load->p_vaddr > segment->p_vaddr)
6932 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
6933#endif
6934 if (segment->p_memsz < segment->p_filesz)
6935 error (_("the segment's file size is larger than its memory size\n"));
6936 previous_load = segment;
6937 break;
6938
6939 case PT_PHDR:
6940 /* PR 20815 - Verify that the program header is loaded into memory. */
6941 if (i > 0 && previous_load != NULL)
6942 error (_("the PHDR segment must occur before any LOAD segment\n"));
6943 if (filedata->file_header.e_machine != EM_PARISC)
6944 {
6945 unsigned int j;
6946
6947 for (j = 1; j < filedata->file_header.e_phnum; j++)
6948 {
6949 Elf_Internal_Phdr *load = filedata->program_headers + j;
6950 if (load->p_type == PT_LOAD
6951 && load->p_offset <= segment->p_offset
6952 && (load->p_offset + load->p_filesz
6953 >= segment->p_offset + segment->p_filesz)
6954 && load->p_vaddr <= segment->p_vaddr
6955 && (load->p_vaddr + load->p_filesz
6956 >= segment->p_vaddr + segment->p_filesz))
6957 break;
6958 }
6959 if (j == filedata->file_header.e_phnum)
6960 error (_("the PHDR segment is not covered by a LOAD segment\n"));
6961 }
6962 break;
6963
6964 case PT_DYNAMIC:
6965 if (dynamic_addr)
6966 error (_("more than one dynamic segment\n"));
6967
6968 /* By default, assume that the .dynamic section is the first
6969 section in the DYNAMIC segment. */
6970 dynamic_addr = segment->p_offset;
6971 dynamic_size = segment->p_filesz;
6972
6973 /* Try to locate the .dynamic section. If there is
6974 a section header table, we can easily locate it. */
6975 if (filedata->section_headers != NULL)
6976 {
6977 Elf_Internal_Shdr * sec;
6978
6979 sec = find_section (filedata, ".dynamic");
6980 if (sec == NULL || sec->sh_size == 0)
6981 {
6982 /* A corresponding .dynamic section is expected, but on
6983 IA-64/OpenVMS it is OK for it to be missing. */
6984 if (!is_ia64_vms (filedata))
6985 error (_("no .dynamic section in the dynamic segment\n"));
6986 break;
6987 }
6988
6989 if (sec->sh_type == SHT_NOBITS)
6990 {
6991 dynamic_addr = 0;
6992 dynamic_size = 0;
6993 break;
6994 }
6995
6996 dynamic_addr = sec->sh_offset;
6997 dynamic_size = sec->sh_size;
6998
6999 /* The PT_DYNAMIC segment, which is used by the run-time
7000 loader, should exactly match the .dynamic section. */
7001 if (do_checks
7002 && (dynamic_addr != segment->p_offset
7003 || dynamic_size != segment->p_filesz))
7004 warn (_("\
7005the .dynamic section is not the same as the dynamic segment\n"));
7006 }
7007
7008 /* PR binutils/17512: Avoid corrupt dynamic section info in the
7009 segment. Check this after matching against the section headers
7010 so we don't warn on debuginfo file (which have NOBITS .dynamic
7011 sections). */
7012 if (dynamic_addr > filedata->file_size
7013 || (dynamic_size > filedata->file_size - dynamic_addr))
7014 {
7015 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
7016 dynamic_addr = 0;
7017 dynamic_size = 0;
7018 }
7019 break;
7020
7021 case PT_INTERP:
7022 if (segment->p_offset >= filedata->file_size
7023 || segment->p_filesz > filedata->file_size - segment->p_offset
7024 || segment->p_filesz - 1 >= (size_t) -2
7025 || fseek64 (filedata->handle,
7026 filedata->archive_file_offset + segment->p_offset,
7027 SEEK_SET))
7028 error (_("Unable to find program interpreter name\n"));
7029 else
7030 {
7031 size_t len = segment->p_filesz;
7032 free (filedata->program_interpreter);
7033 filedata->program_interpreter = xmalloc (len + 1);
7034 len = fread (filedata->program_interpreter, 1, len,
7035 filedata->handle);
7036 filedata->program_interpreter[len] = 0;
7037
7038 if (do_segments)
7039 printf (_(" [Requesting program interpreter: %s]\n"),
7040 filedata->program_interpreter);
7041 }
7042 break;
7043 }
7044 }
7045
7046 if (do_segments
7047 && filedata->section_headers != NULL
7048 && filedata->string_table != NULL)
7049 {
7050 printf (_("\n Section to Segment mapping:\n"));
7051 printf (_(" Segment Sections...\n"));
7052
7053 for (i = 0; i < filedata->file_header.e_phnum; i++)
7054 {
7055 unsigned int j;
7056 Elf_Internal_Shdr * section;
7057
7058 segment = filedata->program_headers + i;
7059 section = filedata->section_headers + 1;
7060
7061 printf (" %2.2d ", i);
7062
7063 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
7064 {
7065 if (!ELF_TBSS_SPECIAL (section, segment)
7066 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
7067 printf ("%s ", printable_section_name (filedata, section));
7068 }
7069
7070 putc ('\n',stdout);
7071 }
7072 }
7073
7074 filedata->dynamic_addr = dynamic_addr;
7075 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
7076 return;
7077
7078 no_headers:
7079 filedata->dynamic_addr = 0;
7080 filedata->dynamic_size = 1;
7081}
7082
7083
7084/* Find the file offset corresponding to VMA by using the program headers. */
7085
7086static int64_t
7087offset_from_vma (Filedata * filedata, uint64_t vma, uint64_t size)
7088{
7089 Elf_Internal_Phdr * seg;
7090
7091 if (! get_program_headers (filedata))
7092 {
7093 warn (_("Cannot interpret virtual addresses without program headers.\n"));
7094 return (long) vma;
7095 }
7096
7097 for (seg = filedata->program_headers;
7098 seg < filedata->program_headers + filedata->file_header.e_phnum;
7099 ++seg)
7100 {
7101 if (seg->p_type != PT_LOAD)
7102 continue;
7103
7104 if (vma >= (seg->p_vaddr & -seg->p_align)
7105 && vma + size <= seg->p_vaddr + seg->p_filesz)
7106 return vma - seg->p_vaddr + seg->p_offset;
7107 }
7108
7109 warn (_("Virtual address %#" PRIx64
7110 " not located in any PT_LOAD segment.\n"), vma);
7111 return vma;
7112}
7113
7114
7115/* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
7116 If PROBE is true, this is just a probe and we do not generate any error
7117 messages if the load fails. */
7118
7119static bool
7120get_32bit_section_headers (Filedata * filedata, bool probe)
7121{
7122 Elf32_External_Shdr * shdrs;
7123 Elf_Internal_Shdr * internal;
7124 unsigned int i;
7125 unsigned int size = filedata->file_header.e_shentsize;
7126 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
7127
7128 /* PR binutils/17531: Cope with unexpected section header sizes. */
7129 if (size == 0 || num == 0)
7130 return false;
7131
7132 /* The section header cannot be at the start of the file - that is
7133 where the ELF file header is located. A file with absolutely no
7134 sections in it will use a shoff of 0. */
7135 if (filedata->file_header.e_shoff == 0)
7136 return false;
7137
7138 if (size < sizeof * shdrs)
7139 {
7140 if (! probe)
7141 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
7142 return false;
7143 }
7144 if (!probe && size > sizeof * shdrs)
7145 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
7146
7147 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
7148 size, num,
7149 probe ? NULL : _("section headers"));
7150 if (shdrs == NULL)
7151 return false;
7152
7153 filedata->section_headers = (Elf_Internal_Shdr *)
7154 cmalloc (num, sizeof (Elf_Internal_Shdr));
7155 if (filedata->section_headers == NULL)
7156 {
7157 if (!probe)
7158 error (_("Out of memory reading %u section headers\n"), num);
7159 free (shdrs);
7160 return false;
7161 }
7162
7163 for (i = 0, internal = filedata->section_headers;
7164 i < num;
7165 i++, internal++)
7166 {
7167 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
7168 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
7169 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
7170 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
7171 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
7172 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
7173 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
7174 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
7175 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
7176 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
7177 if (!probe && internal->sh_link > num)
7178 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
7179 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
7180 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
7181 }
7182
7183 free (shdrs);
7184 return true;
7185}
7186
7187/* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
7188
7189static bool
7190get_64bit_section_headers (Filedata * filedata, bool probe)
7191{
7192 Elf64_External_Shdr * shdrs;
7193 Elf_Internal_Shdr * internal;
7194 unsigned int i;
7195 unsigned int size = filedata->file_header.e_shentsize;
7196 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
7197
7198 /* PR binutils/17531: Cope with unexpected section header sizes. */
7199 if (size == 0 || num == 0)
7200 return false;
7201
7202 /* The section header cannot be at the start of the file - that is
7203 where the ELF file header is located. A file with absolutely no
7204 sections in it will use a shoff of 0. */
7205 if (filedata->file_header.e_shoff == 0)
7206 return false;
7207
7208 if (size < sizeof * shdrs)
7209 {
7210 if (! probe)
7211 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
7212 return false;
7213 }
7214
7215 if (! probe && size > sizeof * shdrs)
7216 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
7217
7218 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
7219 filedata->file_header.e_shoff,
7220 size, num,
7221 probe ? NULL : _("section headers"));
7222 if (shdrs == NULL)
7223 return false;
7224
7225 filedata->section_headers = (Elf_Internal_Shdr *)
7226 cmalloc (num, sizeof (Elf_Internal_Shdr));
7227 if (filedata->section_headers == NULL)
7228 {
7229 if (! probe)
7230 error (_("Out of memory reading %u section headers\n"), num);
7231 free (shdrs);
7232 return false;
7233 }
7234
7235 for (i = 0, internal = filedata->section_headers;
7236 i < num;
7237 i++, internal++)
7238 {
7239 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
7240 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
7241 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
7242 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
7243 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
7244 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
7245 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
7246 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
7247 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
7248 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
7249 if (!probe && internal->sh_link > num)
7250 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
7251 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
7252 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
7253 }
7254
7255 free (shdrs);
7256 return true;
7257}
7258
7259static bool
7260get_section_headers (Filedata *filedata, bool probe)
7261{
7262 if (filedata->section_headers != NULL)
7263 return true;
7264
7265 if (is_32bit_elf)
7266 return get_32bit_section_headers (filedata, probe);
7267 else
7268 return get_64bit_section_headers (filedata, probe);
7269}
7270
7271static Elf_Internal_Sym *
7272get_32bit_elf_symbols (Filedata *filedata,
7273 Elf_Internal_Shdr *section,
7274 uint64_t *num_syms_return)
7275{
7276 uint64_t number = 0;
7277 Elf32_External_Sym * esyms = NULL;
7278 Elf_External_Sym_Shndx * shndx = NULL;
7279 Elf_Internal_Sym * isyms = NULL;
7280 Elf_Internal_Sym * psym;
7281 unsigned int j;
7282 elf_section_list * entry;
7283
7284 if (section->sh_size == 0)
7285 {
7286 if (num_syms_return != NULL)
7287 * num_syms_return = 0;
7288 return NULL;
7289 }
7290
7291 /* Run some sanity checks first. */
7292 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
7293 {
7294 error (_("Section %s has an invalid sh_entsize of %#" PRIx64 "\n"),
7295 printable_section_name (filedata, section),
7296 section->sh_entsize);
7297 goto exit_point;
7298 }
7299
7300 if (section->sh_size > filedata->file_size)
7301 {
7302 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
7303 printable_section_name (filedata, section),
7304 section->sh_size);
7305 goto exit_point;
7306 }
7307
7308 number = section->sh_size / section->sh_entsize;
7309
7310 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
7311 {
7312 error (_("Size (%#" PRIx64 ") of section %s "
7313 "is not a multiple of its sh_entsize (%#" PRIx64 ")\n"),
7314 section->sh_size,
7315 printable_section_name (filedata, section),
7316 section->sh_entsize);
7317 goto exit_point;
7318 }
7319
7320 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
7321 section->sh_size, _("symbols"));
7322 if (esyms == NULL)
7323 goto exit_point;
7324
7325 shndx = NULL;
7326 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
7327 {
7328 if (entry->hdr->sh_link != (size_t) (section - filedata->section_headers))
7329 continue;
7330
7331 if (shndx != NULL)
7332 {
7333 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
7334 free (shndx);
7335 }
7336
7337 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
7338 entry->hdr->sh_offset,
7339 1, entry->hdr->sh_size,
7340 _("symbol table section indices"));
7341 if (shndx == NULL)
7342 goto exit_point;
7343
7344 /* PR17531: file: heap-buffer-overflow */
7345 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
7346 {
7347 error (_("Index section %s has an sh_size of %#" PRIx64 " - expected %#" PRIx64 "\n"),
7348 printable_section_name (filedata, entry->hdr),
7349 entry->hdr->sh_size,
7350 section->sh_size);
7351 goto exit_point;
7352 }
7353 }
7354
7355 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
7356
7357 if (isyms == NULL)
7358 {
7359 error (_("Out of memory reading %" PRIu64 " symbols\n"), number);
7360 goto exit_point;
7361 }
7362
7363 for (j = 0, psym = isyms; j < number; j++, psym++)
7364 {
7365 psym->st_name = BYTE_GET (esyms[j].st_name);
7366 psym->st_value = BYTE_GET (esyms[j].st_value);
7367 psym->st_size = BYTE_GET (esyms[j].st_size);
7368 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
7369 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
7370 psym->st_shndx
7371 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
7372 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
7373 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
7374 psym->st_info = BYTE_GET (esyms[j].st_info);
7375 psym->st_other = BYTE_GET (esyms[j].st_other);
7376 }
7377
7378 exit_point:
7379 free (shndx);
7380 free (esyms);
7381
7382 if (num_syms_return != NULL)
7383 * num_syms_return = isyms == NULL ? 0 : number;
7384
7385 return isyms;
7386}
7387
7388static Elf_Internal_Sym *
7389get_64bit_elf_symbols (Filedata *filedata,
7390 Elf_Internal_Shdr *section,
7391 uint64_t *num_syms_return)
7392{
7393 uint64_t number = 0;
7394 Elf64_External_Sym * esyms = NULL;
7395 Elf_External_Sym_Shndx * shndx = NULL;
7396 Elf_Internal_Sym * isyms = NULL;
7397 Elf_Internal_Sym * psym;
7398 unsigned int j;
7399 elf_section_list * entry;
7400
7401 if (section->sh_size == 0)
7402 {
7403 if (num_syms_return != NULL)
7404 * num_syms_return = 0;
7405 return NULL;
7406 }
7407
7408 /* Run some sanity checks first. */
7409 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
7410 {
7411 error (_("Section %s has an invalid sh_entsize of %#" PRIx64 "\n"),
7412 printable_section_name (filedata, section),
7413 section->sh_entsize);
7414 goto exit_point;
7415 }
7416
7417 if (section->sh_size > filedata->file_size)
7418 {
7419 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
7420 printable_section_name (filedata, section),
7421 section->sh_size);
7422 goto exit_point;
7423 }
7424
7425 number = section->sh_size / section->sh_entsize;
7426
7427 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
7428 {
7429 error (_("Size (%#" PRIx64 ") of section %s "
7430 "is not a multiple of its sh_entsize (%#" PRIx64 ")\n"),
7431 section->sh_size,
7432 printable_section_name (filedata, section),
7433 section->sh_entsize);
7434 goto exit_point;
7435 }
7436
7437 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
7438 section->sh_size, _("symbols"));
7439 if (!esyms)
7440 goto exit_point;
7441
7442 shndx = NULL;
7443 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
7444 {
7445 if (entry->hdr->sh_link != (size_t) (section - filedata->section_headers))
7446 continue;
7447
7448 if (shndx != NULL)
7449 {
7450 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
7451 free (shndx);
7452 }
7453
7454 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
7455 entry->hdr->sh_offset,
7456 1, entry->hdr->sh_size,
7457 _("symbol table section indices"));
7458 if (shndx == NULL)
7459 goto exit_point;
7460
7461 /* PR17531: file: heap-buffer-overflow */
7462 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
7463 {
7464 error (_("Index section %s has an sh_size of %#" PRIx64 " - expected %#" PRIx64 "\n"),
7465 printable_section_name (filedata, entry->hdr),
7466 entry->hdr->sh_size,
7467 section->sh_size);
7468 goto exit_point;
7469 }
7470 }
7471
7472 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
7473
7474 if (isyms == NULL)
7475 {
7476 error (_("Out of memory reading %" PRIu64 " symbols\n"), number);
7477 goto exit_point;
7478 }
7479
7480 for (j = 0, psym = isyms; j < number; j++, psym++)
7481 {
7482 psym->st_name = BYTE_GET (esyms[j].st_name);
7483 psym->st_info = BYTE_GET (esyms[j].st_info);
7484 psym->st_other = BYTE_GET (esyms[j].st_other);
7485 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
7486
7487 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
7488 psym->st_shndx
7489 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
7490 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
7491 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
7492
7493 psym->st_value = BYTE_GET (esyms[j].st_value);
7494 psym->st_size = BYTE_GET (esyms[j].st_size);
7495 }
7496
7497 exit_point:
7498 free (shndx);
7499 free (esyms);
7500
7501 if (num_syms_return != NULL)
7502 * num_syms_return = isyms == NULL ? 0 : number;
7503
7504 return isyms;
7505}
7506
7507static Elf_Internal_Sym *
7508get_elf_symbols (Filedata *filedata,
7509 Elf_Internal_Shdr *section,
7510 uint64_t *num_syms_return)
7511{
7512 if (is_32bit_elf)
7513 return get_32bit_elf_symbols (filedata, section, num_syms_return);
7514 else
7515 return get_64bit_elf_symbols (filedata, section, num_syms_return);
7516}
7517
7518static const char *
7519get_elf_section_flags (Filedata * filedata, uint64_t sh_flags)
7520{
7521 static char buff[1024];
7522 char * p = buff;
7523 unsigned int field_size = is_32bit_elf ? 8 : 16;
7524 signed int sindex;
7525 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
7526 uint64_t os_flags = 0;
7527 uint64_t proc_flags = 0;
7528 uint64_t unknown_flags = 0;
7529 static const struct
7530 {
7531 const char * str;
7532 unsigned int len;
7533 }
7534 flags [] =
7535 {
7536 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
7537 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
7538 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
7539 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
7540 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
7541 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
7542 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
7543 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
7544 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
7545 /* 9 */ { STRING_COMMA_LEN ("TLS") },
7546 /* IA-64 specific. */
7547 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
7548 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
7549 /* IA-64 OpenVMS specific. */
7550 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
7551 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
7552 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
7553 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
7554 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
7555 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
7556 /* Generic. */
7557 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
7558 /* SPARC specific. */
7559 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
7560 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
7561 /* ARM specific. */
7562 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
7563 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
7564 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
7565 /* GNU specific. */
7566 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
7567 /* VLE specific. */
7568 /* 25 */ { STRING_COMMA_LEN ("VLE") },
7569 /* GNU specific. */
7570 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
7571 };
7572
7573 if (do_section_details)
7574 p += sprintf (p, "[%*.*lx]: ",
7575 field_size, field_size, (unsigned long) sh_flags);
7576
7577 while (sh_flags)
7578 {
7579 uint64_t flag;
7580
7581 flag = sh_flags & - sh_flags;
7582 sh_flags &= ~ flag;
7583
7584 if (do_section_details)
7585 {
7586 switch (flag)
7587 {
7588 case SHF_WRITE: sindex = 0; break;
7589 case SHF_ALLOC: sindex = 1; break;
7590 case SHF_EXECINSTR: sindex = 2; break;
7591 case SHF_MERGE: sindex = 3; break;
7592 case SHF_STRINGS: sindex = 4; break;
7593 case SHF_INFO_LINK: sindex = 5; break;
7594 case SHF_LINK_ORDER: sindex = 6; break;
7595 case SHF_OS_NONCONFORMING: sindex = 7; break;
7596 case SHF_GROUP: sindex = 8; break;
7597 case SHF_TLS: sindex = 9; break;
7598 case SHF_EXCLUDE: sindex = 18; break;
7599 case SHF_COMPRESSED: sindex = 20; break;
7600
7601 default:
7602 sindex = -1;
7603 switch (filedata->file_header.e_machine)
7604 {
7605 case EM_IA_64:
7606 if (flag == SHF_IA_64_SHORT)
7607 sindex = 10;
7608 else if (flag == SHF_IA_64_NORECOV)
7609 sindex = 11;
7610 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
7611 switch (flag)
7612 {
7613 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
7614 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
7615 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
7616 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
7617 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
7618 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
7619 default: break;
7620 }
7621 break;
7622
7623 case EM_386:
7624 case EM_IAMCU:
7625 case EM_X86_64:
7626 case EM_L1OM:
7627 case EM_K1OM:
7628 case EM_OLD_SPARCV9:
7629 case EM_SPARC32PLUS:
7630 case EM_SPARCV9:
7631 case EM_SPARC:
7632 if (flag == SHF_ORDERED)
7633 sindex = 19;
7634 break;
7635
7636 case EM_ARM:
7637 switch (flag)
7638 {
7639 case SHF_ENTRYSECT: sindex = 21; break;
7640 case SHF_ARM_PURECODE: sindex = 22; break;
7641 case SHF_COMDEF: sindex = 23; break;
7642 default: break;
7643 }
7644 break;
7645 case EM_PPC:
7646 if (flag == SHF_PPC_VLE)
7647 sindex = 25;
7648 break;
7649 default:
7650 break;
7651 }
7652
7653 switch (filedata->file_header.e_ident[EI_OSABI])
7654 {
7655 case ELFOSABI_GNU:
7656 case ELFOSABI_FREEBSD:
7657 if (flag == SHF_GNU_RETAIN)
7658 sindex = 26;
7659 /* Fall through */
7660 case ELFOSABI_NONE:
7661 if (flag == SHF_GNU_MBIND)
7662 /* We should not recognize SHF_GNU_MBIND for
7663 ELFOSABI_NONE, but binutils as of 2019-07-23 did
7664 not set the EI_OSABI header byte. */
7665 sindex = 24;
7666 break;
7667 default:
7668 break;
7669 }
7670 break;
7671 }
7672
7673 if (sindex != -1)
7674 {
7675 if (p != buff + field_size + 4)
7676 {
7677 if (size < (10 + 2))
7678 {
7679 warn (_("Internal error: not enough buffer room for section flag info"));
7680 return _("<unknown>");
7681 }
7682 size -= 2;
7683 *p++ = ',';
7684 *p++ = ' ';
7685 }
7686
7687 size -= flags [sindex].len;
7688 p = stpcpy (p, flags [sindex].str);
7689 }
7690 else if (flag & SHF_MASKOS)
7691 os_flags |= flag;
7692 else if (flag & SHF_MASKPROC)
7693 proc_flags |= flag;
7694 else
7695 unknown_flags |= flag;
7696 }
7697 else
7698 {
7699 switch (flag)
7700 {
7701 case SHF_WRITE: *p = 'W'; break;
7702 case SHF_ALLOC: *p = 'A'; break;
7703 case SHF_EXECINSTR: *p = 'X'; break;
7704 case SHF_MERGE: *p = 'M'; break;
7705 case SHF_STRINGS: *p = 'S'; break;
7706 case SHF_INFO_LINK: *p = 'I'; break;
7707 case SHF_LINK_ORDER: *p = 'L'; break;
7708 case SHF_OS_NONCONFORMING: *p = 'O'; break;
7709 case SHF_GROUP: *p = 'G'; break;
7710 case SHF_TLS: *p = 'T'; break;
7711 case SHF_EXCLUDE: *p = 'E'; break;
7712 case SHF_COMPRESSED: *p = 'C'; break;
7713
7714 default:
7715 if ((filedata->file_header.e_machine == EM_X86_64
7716 || filedata->file_header.e_machine == EM_L1OM
7717 || filedata->file_header.e_machine == EM_K1OM)
7718 && flag == SHF_X86_64_LARGE)
7719 *p = 'l';
7720 else if (filedata->file_header.e_machine == EM_ARM
7721 && flag == SHF_ARM_PURECODE)
7722 *p = 'y';
7723 else if (filedata->file_header.e_machine == EM_PPC
7724 && flag == SHF_PPC_VLE)
7725 *p = 'v';
7726 else if (flag & SHF_MASKOS)
7727 {
7728 switch (filedata->file_header.e_ident[EI_OSABI])
7729 {
7730 case ELFOSABI_GNU:
7731 case ELFOSABI_FREEBSD:
7732 if (flag == SHF_GNU_RETAIN)
7733 {
7734 *p = 'R';
7735 break;
7736 }
7737 /* Fall through */
7738 case ELFOSABI_NONE:
7739 if (flag == SHF_GNU_MBIND)
7740 {
7741 /* We should not recognize SHF_GNU_MBIND for
7742 ELFOSABI_NONE, but binutils as of 2019-07-23 did
7743 not set the EI_OSABI header byte. */
7744 *p = 'D';
7745 break;
7746 }
7747 /* Fall through */
7748 default:
7749 *p = 'o';
7750 sh_flags &= ~SHF_MASKOS;
7751 break;
7752 }
7753 }
7754 else if (flag & SHF_MASKPROC)
7755 {
7756 *p = 'p';
7757 sh_flags &= ~ SHF_MASKPROC;
7758 }
7759 else
7760 *p = 'x';
7761 break;
7762 }
7763 p++;
7764 }
7765 }
7766
7767 if (do_section_details)
7768 {
7769 if (os_flags)
7770 {
7771 if (p != buff + field_size + 4)
7772 {
7773 if (size < 2 + 5 + field_size + 1)
7774 {
7775 warn (_("Internal error: not enough buffer room for section flag info"));
7776 return _("<unknown>");
7777 }
7778 size -= 2;
7779 *p++ = ',';
7780 *p++ = ' ';
7781 }
7782 size -= 5 + field_size;
7783 p += sprintf (p, "OS (%*.*lx)", field_size, field_size,
7784 (unsigned long) os_flags);
7785 }
7786 if (proc_flags)
7787 {
7788 if (p != buff + field_size + 4)
7789 {
7790 if (size < 2 + 7 + field_size + 1)
7791 {
7792 warn (_("Internal error: not enough buffer room for section flag info"));
7793 return _("<unknown>");
7794 }
7795 size -= 2;
7796 *p++ = ',';
7797 *p++ = ' ';
7798 }
7799 size -= 7 + field_size;
7800 p += sprintf (p, "PROC (%*.*lx)", field_size, field_size,
7801 (unsigned long) proc_flags);
7802 }
7803 if (unknown_flags)
7804 {
7805 if (p != buff + field_size + 4)
7806 {
7807 if (size < 2 + 10 + field_size + 1)
7808 {
7809 warn (_("Internal error: not enough buffer room for section flag info"));
7810 return _("<unknown>");
7811 }
7812 size -= 2;
7813 *p++ = ',';
7814 *p++ = ' ';
7815 }
7816 size -= 10 + field_size;
7817 p += sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
7818 (unsigned long) unknown_flags);
7819 }
7820 }
7821
7822 *p = '\0';
7823 return buff;
7824}
7825
7826static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
7827get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf,
7828 uint64_t size)
7829{
7830 if (is_32bit_elf)
7831 {
7832 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
7833
7834 if (size < sizeof (* echdr))
7835 {
7836 error (_("Compressed section is too small even for a compression header\n"));
7837 return 0;
7838 }
7839
7840 chdr->ch_type = BYTE_GET (echdr->ch_type);
7841 chdr->ch_size = BYTE_GET (echdr->ch_size);
7842 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7843 return sizeof (*echdr);
7844 }
7845 else
7846 {
7847 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
7848
7849 if (size < sizeof (* echdr))
7850 {
7851 error (_("Compressed section is too small even for a compression header\n"));
7852 return 0;
7853 }
7854
7855 chdr->ch_type = BYTE_GET (echdr->ch_type);
7856 chdr->ch_size = BYTE_GET (echdr->ch_size);
7857 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7858 return sizeof (*echdr);
7859 }
7860}
7861
7862static bool
7863process_section_headers (Filedata * filedata)
7864{
7865 Elf_Internal_Shdr * section;
7866 unsigned int i;
7867
7868 if (filedata->file_header.e_shnum == 0)
7869 {
7870 /* PR binutils/12467. */
7871 if (filedata->file_header.e_shoff != 0)
7872 {
7873 warn (_("possibly corrupt ELF file header - it has a non-zero"
7874 " section header offset, but no section headers\n"));
7875 return false;
7876 }
7877 else if (do_sections)
7878 printf (_("\nThere are no sections in this file.\n"));
7879
7880 return true;
7881 }
7882
7883 if (do_sections && !do_header)
7884 {
7885 if (filedata->is_separate && process_links)
7886 printf (_("In linked file '%s': "), filedata->file_name);
7887 if (! filedata->is_separate || process_links)
7888 printf (ngettext ("There is %d section header, "
7889 "starting at offset %#" PRIx64 ":\n",
7890 "There are %d section headers, "
7891 "starting at offset %#" PRIx64 ":\n",
7892 filedata->file_header.e_shnum),
7893 filedata->file_header.e_shnum,
7894 filedata->file_header.e_shoff);
7895 }
7896
7897 if (!get_section_headers (filedata, false))
7898 return false;
7899
7900 /* Read in the string table, so that we have names to display. */
7901 if (filedata->file_header.e_shstrndx != SHN_UNDEF
7902 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
7903 {
7904 section = filedata->section_headers + filedata->file_header.e_shstrndx;
7905
7906 if (section->sh_size != 0)
7907 {
7908 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
7909 1, section->sh_size,
7910 _("string table"));
7911
7912 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
7913 }
7914 }
7915
7916 /* Scan the sections for the dynamic symbol table
7917 and dynamic string table and debug sections. */
7918 eh_addr_size = is_32bit_elf ? 4 : 8;
7919 switch (filedata->file_header.e_machine)
7920 {
7921 case EM_MIPS:
7922 case EM_MIPS_RS3_LE:
7923 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
7924 FDE addresses. However, the ABI also has a semi-official ILP32
7925 variant for which the normal FDE address size rules apply.
7926
7927 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
7928 section, where XX is the size of longs in bits. Unfortunately,
7929 earlier compilers provided no way of distinguishing ILP32 objects
7930 from LP64 objects, so if there's any doubt, we should assume that
7931 the official LP64 form is being used. */
7932 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64
7933 && find_section (filedata, ".gcc_compiled_long32") == NULL)
7934 eh_addr_size = 8;
7935 break;
7936
7937 case EM_H8_300:
7938 case EM_H8_300H:
7939 switch (filedata->file_header.e_flags & EF_H8_MACH)
7940 {
7941 case E_H8_MACH_H8300:
7942 case E_H8_MACH_H8300HN:
7943 case E_H8_MACH_H8300SN:
7944 case E_H8_MACH_H8300SXN:
7945 eh_addr_size = 2;
7946 break;
7947 case E_H8_MACH_H8300H:
7948 case E_H8_MACH_H8300S:
7949 case E_H8_MACH_H8300SX:
7950 eh_addr_size = 4;
7951 break;
7952 }
7953 break;
7954
7955 case EM_M32C_OLD:
7956 case EM_M32C:
7957 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
7958 {
7959 case EF_M32C_CPU_M16C:
7960 eh_addr_size = 2;
7961 break;
7962 }
7963 break;
7964 }
7965
7966#define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
7967 do \
7968 { \
7969 uint64_t expected_entsize = is_32bit_elf ? size32 : size64; \
7970 if (section->sh_entsize != expected_entsize) \
7971 { \
7972 error (_("Section %d has invalid sh_entsize of %" PRIx64 "\n"), \
7973 i, section->sh_entsize); \
7974 error (_("(Using the expected size of %" PRIx64 " for the rest of this dump)\n"), \
7975 expected_entsize); \
7976 section->sh_entsize = expected_entsize; \
7977 } \
7978 } \
7979 while (0)
7980
7981#define CHECK_ENTSIZE(section, i, type) \
7982 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
7983 sizeof (Elf64_External_##type))
7984
7985 for (i = 0, section = filedata->section_headers;
7986 i < filedata->file_header.e_shnum;
7987 i++, section++)
7988 {
7989 const char *name = printable_section_name (filedata, section);
7990
7991 /* Run some sanity checks on the headers and
7992 possibly fill in some file data as well. */
7993 switch (section->sh_type)
7994 {
7995 case SHT_DYNSYM:
7996 if (filedata->dynamic_symbols != NULL)
7997 {
7998 error (_("File contains multiple dynamic symbol tables\n"));
7999 continue;
8000 }
8001
8002 CHECK_ENTSIZE (section, i, Sym);
8003 filedata->dynamic_symbols
8004 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
8005 filedata->dynamic_symtab_section = section;
8006 break;
8007
8008 case SHT_STRTAB:
8009 if (streq (name, ".dynstr"))
8010 {
8011 if (filedata->dynamic_strings != NULL)
8012 {
8013 error (_("File contains multiple dynamic string tables\n"));
8014 continue;
8015 }
8016
8017 filedata->dynamic_strings
8018 = (char *) get_data (NULL, filedata, section->sh_offset,
8019 1, section->sh_size, _("dynamic strings"));
8020 filedata->dynamic_strings_length
8021 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
8022 filedata->dynamic_strtab_section = section;
8023 }
8024 break;
8025
8026 case SHT_SYMTAB_SHNDX:
8027 {
8028 elf_section_list * entry = xmalloc (sizeof * entry);
8029
8030 entry->hdr = section;
8031 entry->next = filedata->symtab_shndx_list;
8032 filedata->symtab_shndx_list = entry;
8033 }
8034 break;
8035
8036 case SHT_SYMTAB:
8037 CHECK_ENTSIZE (section, i, Sym);
8038 break;
8039
8040 case SHT_GROUP:
8041 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
8042 break;
8043
8044 case SHT_REL:
8045 CHECK_ENTSIZE (section, i, Rel);
8046 if (do_checks && section->sh_size == 0)
8047 warn (_("Section '%s': zero-sized relocation section\n"), name);
8048 break;
8049
8050 case SHT_RELA:
8051 CHECK_ENTSIZE (section, i, Rela);
8052 if (do_checks && section->sh_size == 0)
8053 warn (_("Section '%s': zero-sized relocation section\n"), name);
8054 break;
8055
8056 case SHT_RELR:
8057 CHECK_ENTSIZE (section, i, Relr);
8058 break;
8059
8060 case SHT_NOTE:
8061 case SHT_PROGBITS:
8062 /* Having a zero sized section is not illegal according to the
8063 ELF standard, but it might be an indication that something
8064 is wrong. So issue a warning if we are running in lint mode. */
8065 if (do_checks && section->sh_size == 0)
8066 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
8067 break;
8068
8069 default:
8070 break;
8071 }
8072
8073 if ((do_debugging || do_debug_info || do_debug_abbrevs
8074 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
8075 || do_debug_aranges || do_debug_frames || do_debug_macinfo
8076 || do_debug_str || do_debug_str_offsets || do_debug_loc
8077 || do_debug_ranges
8078 || do_debug_addr || do_debug_cu_index || do_debug_links)
8079 && (startswith (name, ".debug_")
8080 || startswith (name, ".zdebug_")))
8081 {
8082 if (name[1] == 'z')
8083 name += sizeof (".zdebug_") - 1;
8084 else
8085 name += sizeof (".debug_") - 1;
8086
8087 if (do_debugging
8088 || (do_debug_info && startswith (name, "info"))
8089 || (do_debug_info && startswith (name, "types"))
8090 || (do_debug_abbrevs && startswith (name, "abbrev"))
8091 || (do_debug_lines && strcmp (name, "line") == 0)
8092 || (do_debug_lines && startswith (name, "line."))
8093 || (do_debug_pubnames && startswith (name, "pubnames"))
8094 || (do_debug_pubtypes && startswith (name, "pubtypes"))
8095 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
8096 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
8097 || (do_debug_aranges && startswith (name, "aranges"))
8098 || (do_debug_ranges && startswith (name, "ranges"))
8099 || (do_debug_ranges && startswith (name, "rnglists"))
8100 || (do_debug_frames && startswith (name, "frame"))
8101 || (do_debug_macinfo && startswith (name, "macinfo"))
8102 || (do_debug_macinfo && startswith (name, "macro"))
8103 || (do_debug_str && startswith (name, "str"))
8104 || (do_debug_links && startswith (name, "sup"))
8105 || (do_debug_str_offsets && startswith (name, "str_offsets"))
8106 || (do_debug_loc && startswith (name, "loc"))
8107 || (do_debug_loc && startswith (name, "loclists"))
8108 || (do_debug_addr && startswith (name, "addr"))
8109 || (do_debug_cu_index && startswith (name, "cu_index"))
8110 || (do_debug_cu_index && startswith (name, "tu_index"))
8111 )
8112 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8113 }
8114 /* Linkonce section to be combined with .debug_info at link time. */
8115 else if ((do_debugging || do_debug_info)
8116 && startswith (name, ".gnu.linkonce.wi."))
8117 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8118 else if (do_debug_frames && streq (name, ".eh_frame"))
8119 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8120 else if (do_debug_frames && streq (name, ".eh_frame_hdr"))
8121 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8122 else if (do_gdb_index && (streq (name, ".gdb_index")
8123 || streq (name, ".debug_names")))
8124 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8125 /* Trace sections for Itanium VMS. */
8126 else if ((do_debugging || do_trace_info || do_trace_abbrevs
8127 || do_trace_aranges)
8128 && startswith (name, ".trace_"))
8129 {
8130 name += sizeof (".trace_") - 1;
8131
8132 if (do_debugging
8133 || (do_trace_info && streq (name, "info"))
8134 || (do_trace_abbrevs && streq (name, "abbrev"))
8135 || (do_trace_aranges && streq (name, "aranges"))
8136 )
8137 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8138 }
8139 else if ((do_debugging || do_debug_links)
8140 && (startswith (name, ".gnu_debuglink")
8141 || startswith (name, ".gnu_debugaltlink")))
8142 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8143 }
8144
8145 if (! do_sections)
8146 return true;
8147
8148 if (filedata->is_separate && ! process_links)
8149 return true;
8150
8151 if (filedata->is_separate)
8152 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
8153 else if (filedata->file_header.e_shnum > 1)
8154 printf (_("\nSection Headers:\n"));
8155 else
8156 printf (_("\nSection Header:\n"));
8157
8158 if (is_32bit_elf)
8159 {
8160 if (do_section_details)
8161 {
8162 printf (_(" [Nr] Name\n"));
8163 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
8164 }
8165 else
8166 printf
8167 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
8168 }
8169 else if (do_wide)
8170 {
8171 if (do_section_details)
8172 {
8173 printf (_(" [Nr] Name\n"));
8174 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
8175 }
8176 else
8177 printf
8178 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
8179 }
8180 else
8181 {
8182 if (do_section_details)
8183 {
8184 printf (_(" [Nr] Name\n"));
8185 printf (_(" Type Address Offset Link\n"));
8186 printf (_(" Size EntSize Info Align\n"));
8187 }
8188 else
8189 {
8190 printf (_(" [Nr] Name Type Address Offset\n"));
8191 printf (_(" Size EntSize Flags Link Info Align\n"));
8192 }
8193 }
8194
8195 if (do_section_details)
8196 printf (_(" Flags\n"));
8197
8198 for (i = 0, section = filedata->section_headers;
8199 i < filedata->file_header.e_shnum;
8200 i++, section++)
8201 {
8202 /* Run some sanity checks on the section header. */
8203
8204 /* Check the sh_link field. */
8205 switch (section->sh_type)
8206 {
8207 case SHT_REL:
8208 case SHT_RELR:
8209 case SHT_RELA:
8210 if (section->sh_link == 0
8211 && (filedata->file_header.e_type == ET_EXEC
8212 || filedata->file_header.e_type == ET_DYN))
8213 /* A dynamic relocation section where all entries use a
8214 zero symbol index need not specify a symtab section. */
8215 break;
8216 /* Fall through. */
8217 case SHT_SYMTAB_SHNDX:
8218 case SHT_GROUP:
8219 case SHT_HASH:
8220 case SHT_GNU_HASH:
8221 case SHT_GNU_versym:
8222 if (section->sh_link == 0
8223 || section->sh_link >= filedata->file_header.e_shnum
8224 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
8225 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
8226 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
8227 i, section->sh_link);
8228 break;
8229
8230 case SHT_DYNAMIC:
8231 case SHT_SYMTAB:
8232 case SHT_DYNSYM:
8233 case SHT_GNU_verneed:
8234 case SHT_GNU_verdef:
8235 case SHT_GNU_LIBLIST:
8236 if (section->sh_link == 0
8237 || section->sh_link >= filedata->file_header.e_shnum
8238 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
8239 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
8240 i, section->sh_link);
8241 break;
8242
8243 case SHT_INIT_ARRAY:
8244 case SHT_FINI_ARRAY:
8245 case SHT_PREINIT_ARRAY:
8246 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
8247 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
8248 i, section->sh_link);
8249 break;
8250
8251 default:
8252 /* FIXME: Add support for target specific section types. */
8253#if 0 /* Currently we do not check other section types as there are too
8254 many special cases. Stab sections for example have a type
8255 of SHT_PROGBITS but an sh_link field that links to the .stabstr
8256 section. */
8257 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
8258 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
8259 i, section->sh_link);
8260#endif
8261 break;
8262 }
8263
8264 /* Check the sh_info field. */
8265 switch (section->sh_type)
8266 {
8267 case SHT_REL:
8268 case SHT_RELA:
8269 if (section->sh_info == 0
8270 && (filedata->file_header.e_type == ET_EXEC
8271 || filedata->file_header.e_type == ET_DYN))
8272 /* Dynamic relocations apply to segments, so they do not
8273 need to specify the section they relocate. */
8274 break;
8275 if (section->sh_info == 0
8276 || section->sh_info >= filedata->file_header.e_shnum
8277 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
8278 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
8279 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
8280 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
8281 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
8282 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
8283 /* FIXME: Are other section types valid ? */
8284 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
8285 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
8286 i, section->sh_info);
8287 break;
8288
8289 case SHT_DYNAMIC:
8290 case SHT_HASH:
8291 case SHT_SYMTAB_SHNDX:
8292 case SHT_INIT_ARRAY:
8293 case SHT_FINI_ARRAY:
8294 case SHT_PREINIT_ARRAY:
8295 if (section->sh_info != 0)
8296 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
8297 i, section->sh_info);
8298 break;
8299
8300 case SHT_GROUP:
8301 case SHT_SYMTAB:
8302 case SHT_DYNSYM:
8303 /* A symbol index - we assume that it is valid. */
8304 break;
8305
8306 default:
8307 /* FIXME: Add support for target specific section types. */
8308 if (section->sh_type == SHT_NOBITS)
8309 /* NOBITS section headers with non-zero sh_info fields can be
8310 created when a binary is stripped of everything but its debug
8311 information. The stripped sections have their headers
8312 preserved but their types set to SHT_NOBITS. So do not check
8313 this type of section. */
8314 ;
8315 else if (section->sh_flags & SHF_INFO_LINK)
8316 {
8317 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
8318 warn (_("[%2u]: Expected link to another section in info field"), i);
8319 }
8320 else if (section->sh_type < SHT_LOOS
8321 && (section->sh_flags & SHF_GNU_MBIND) == 0
8322 && section->sh_info != 0)
8323 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
8324 i, section->sh_info);
8325 break;
8326 }
8327
8328 /* Check the sh_size field. */
8329 if (section->sh_size > filedata->file_size
8330 && section->sh_type != SHT_NOBITS
8331 && section->sh_type != SHT_NULL
8332 && section->sh_type < SHT_LOOS)
8333 warn (_("Size of section %u is larger than the entire file!\n"), i);
8334
8335 printf (" [%2u] ", i);
8336 if (do_section_details)
8337 printf ("%s\n ", printable_section_name (filedata, section));
8338 else
8339 print_symbol_name (-17, printable_section_name (filedata, section));
8340
8341 printf (do_wide ? " %-15s " : " %-15.15s ",
8342 get_section_type_name (filedata, section->sh_type));
8343
8344 if (is_32bit_elf)
8345 {
8346 const char * link_too_big = NULL;
8347
8348 print_vma (section->sh_addr, LONG_HEX);
8349
8350 printf ( " %6.6lx %6.6lx %2.2lx",
8351 (unsigned long) section->sh_offset,
8352 (unsigned long) section->sh_size,
8353 (unsigned long) section->sh_entsize);
8354
8355 if (do_section_details)
8356 fputs (" ", stdout);
8357 else
8358 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
8359
8360 if (section->sh_link >= filedata->file_header.e_shnum)
8361 {
8362 link_too_big = "";
8363 /* The sh_link value is out of range. Normally this indicates
8364 an error but it can have special values in Solaris binaries. */
8365 switch (filedata->file_header.e_machine)
8366 {
8367 case EM_386:
8368 case EM_IAMCU:
8369 case EM_X86_64:
8370 case EM_L1OM:
8371 case EM_K1OM:
8372 case EM_OLD_SPARCV9:
8373 case EM_SPARC32PLUS:
8374 case EM_SPARCV9:
8375 case EM_SPARC:
8376 if (section->sh_link == (SHN_BEFORE & 0xffff))
8377 link_too_big = "BEFORE";
8378 else if (section->sh_link == (SHN_AFTER & 0xffff))
8379 link_too_big = "AFTER";
8380 break;
8381 default:
8382 break;
8383 }
8384 }
8385
8386 if (do_section_details)
8387 {
8388 if (link_too_big != NULL && * link_too_big)
8389 printf ("<%s> ", link_too_big);
8390 else
8391 printf ("%2u ", section->sh_link);
8392 printf ("%3u %2lu\n", section->sh_info,
8393 (unsigned long) section->sh_addralign);
8394 }
8395 else
8396 printf ("%2u %3u %2lu\n",
8397 section->sh_link,
8398 section->sh_info,
8399 (unsigned long) section->sh_addralign);
8400
8401 if (link_too_big && ! * link_too_big)
8402 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
8403 i, section->sh_link);
8404 }
8405 else if (do_wide)
8406 {
8407 print_vma (section->sh_addr, LONG_HEX);
8408
8409 if ((long) section->sh_offset == section->sh_offset)
8410 printf (" %6.6lx", (unsigned long) section->sh_offset);
8411 else
8412 {
8413 putchar (' ');
8414 print_vma (section->sh_offset, LONG_HEX);
8415 }
8416
8417 if ((unsigned long) section->sh_size == section->sh_size)
8418 printf (" %6.6lx", (unsigned long) section->sh_size);
8419 else
8420 {
8421 putchar (' ');
8422 print_vma (section->sh_size, LONG_HEX);
8423 }
8424
8425 if ((unsigned long) section->sh_entsize == section->sh_entsize)
8426 printf (" %2.2lx", (unsigned long) section->sh_entsize);
8427 else
8428 {
8429 putchar (' ');
8430 print_vma (section->sh_entsize, LONG_HEX);
8431 }
8432
8433 if (do_section_details)
8434 fputs (" ", stdout);
8435 else
8436 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
8437
8438 printf ("%2u %3u ", section->sh_link, section->sh_info);
8439
8440 if ((unsigned long) section->sh_addralign == section->sh_addralign)
8441 printf ("%2lu\n", (unsigned long) section->sh_addralign);
8442 else
8443 {
8444 print_vma (section->sh_addralign, DEC);
8445 putchar ('\n');
8446 }
8447 }
8448 else if (do_section_details)
8449 {
8450 putchar (' ');
8451 print_vma (section->sh_addr, LONG_HEX);
8452 if ((long) section->sh_offset == section->sh_offset)
8453 printf (" %16.16lx", (unsigned long) section->sh_offset);
8454 else
8455 {
8456 printf (" ");
8457 print_vma (section->sh_offset, LONG_HEX);
8458 }
8459 printf (" %u\n ", section->sh_link);
8460 print_vma (section->sh_size, LONG_HEX);
8461 putchar (' ');
8462 print_vma (section->sh_entsize, LONG_HEX);
8463
8464 printf (" %-16u %lu\n",
8465 section->sh_info,
8466 (unsigned long) section->sh_addralign);
8467 }
8468 else
8469 {
8470 putchar (' ');
8471 print_vma (section->sh_addr, LONG_HEX);
8472 if ((long) section->sh_offset == section->sh_offset)
8473 printf (" %8.8lx", (unsigned long) section->sh_offset);
8474 else
8475 {
8476 printf (" ");
8477 print_vma (section->sh_offset, LONG_HEX);
8478 }
8479 printf ("\n ");
8480 print_vma (section->sh_size, LONG_HEX);
8481 printf (" ");
8482 print_vma (section->sh_entsize, LONG_HEX);
8483
8484 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
8485
8486 printf (" %2u %3u %lu\n",
8487 section->sh_link,
8488 section->sh_info,
8489 (unsigned long) section->sh_addralign);
8490 }
8491
8492 if (do_section_details)
8493 {
8494 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
8495 if ((section->sh_flags & SHF_COMPRESSED) != 0)
8496 {
8497 /* Minimum section size is 12 bytes for 32-bit compression
8498 header + 12 bytes for compressed data header. */
8499 unsigned char buf[24];
8500
8501 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
8502 if (get_data (&buf, filedata, section->sh_offset, 1,
8503 sizeof (buf), _("compression header")))
8504 {
8505 Elf_Internal_Chdr chdr;
8506
8507 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
8508 printf (_(" [<corrupt>]\n"));
8509 else
8510 {
8511 if (chdr.ch_type == ch_compress_zlib)
8512 printf (" ZLIB, ");
8513 else if (chdr.ch_type == ch_compress_zstd)
8514 printf (" ZSTD, ");
8515 else
8516 printf (_(" [<unknown>: 0x%x], "),
8517 chdr.ch_type);
8518 print_vma (chdr.ch_size, LONG_HEX);
8519 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
8520 }
8521 }
8522 }
8523 }
8524 }
8525
8526 if (!do_section_details)
8527 {
8528 /* The ordering of the letters shown here matches the ordering of the
8529 corresponding SHF_xxx values, and hence the order in which these
8530 letters will be displayed to the user. */
8531 printf (_("Key to Flags:\n\
8532 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
8533 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
8534 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
8535 switch (filedata->file_header.e_ident[EI_OSABI])
8536 {
8537 case ELFOSABI_GNU:
8538 case ELFOSABI_FREEBSD:
8539 printf (_("R (retain), "));
8540 /* Fall through */
8541 case ELFOSABI_NONE:
8542 printf (_("D (mbind), "));
8543 break;
8544 default:
8545 break;
8546 }
8547 if (filedata->file_header.e_machine == EM_X86_64
8548 || filedata->file_header.e_machine == EM_L1OM
8549 || filedata->file_header.e_machine == EM_K1OM)
8550 printf (_("l (large), "));
8551 else if (filedata->file_header.e_machine == EM_ARM)
8552 printf (_("y (purecode), "));
8553 else if (filedata->file_header.e_machine == EM_PPC)
8554 printf (_("v (VLE), "));
8555 printf ("p (processor specific)\n");
8556 }
8557
8558 return true;
8559}
8560
8561static bool
8562get_symtab (Filedata * filedata,
8563 Elf_Internal_Shdr * symsec,
8564 Elf_Internal_Sym ** symtab,
8565 uint64_t * nsyms,
8566 char ** strtab,
8567 uint64_t * strtablen)
8568{
8569 *strtab = NULL;
8570 *strtablen = 0;
8571 *symtab = get_elf_symbols (filedata, symsec, nsyms);
8572
8573 if (*symtab == NULL)
8574 return false;
8575
8576 if (symsec->sh_link != 0)
8577 {
8578 Elf_Internal_Shdr *strsec;
8579
8580 if (symsec->sh_link >= filedata->file_header.e_shnum)
8581 {
8582 error (_("Bad sh_link in symbol table section\n"));
8583 free (*symtab);
8584 *symtab = NULL;
8585 *nsyms = 0;
8586 return false;
8587 }
8588
8589 strsec = filedata->section_headers + symsec->sh_link;
8590
8591 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
8592 1, strsec->sh_size, _("string table"));
8593 if (*strtab == NULL)
8594 {
8595 free (*symtab);
8596 *symtab = NULL;
8597 *nsyms = 0;
8598 return false;
8599 }
8600 *strtablen = strsec->sh_size;
8601 }
8602 return true;
8603}
8604
8605static const char *
8606get_group_flags (unsigned int flags)
8607{
8608 static char buff[128];
8609
8610 if (flags == 0)
8611 return "";
8612 else if (flags == GRP_COMDAT)
8613 return "COMDAT ";
8614
8615 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
8616 flags,
8617 flags & GRP_MASKOS ? _("<OS specific>") : "",
8618 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
8619 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
8620 ? _("<unknown>") : ""));
8621
8622 return buff;
8623}
8624
8625static bool
8626process_section_groups (Filedata * filedata)
8627{
8628 Elf_Internal_Shdr * section;
8629 unsigned int i;
8630 struct group * group;
8631 Elf_Internal_Shdr * symtab_sec;
8632 Elf_Internal_Shdr * strtab_sec;
8633 Elf_Internal_Sym * symtab;
8634 uint64_t num_syms;
8635 char * strtab;
8636 size_t strtab_size;
8637
8638 /* Don't process section groups unless needed. */
8639 if (!do_unwind && !do_section_groups)
8640 return true;
8641
8642 if (filedata->file_header.e_shnum == 0)
8643 {
8644 if (do_section_groups)
8645 {
8646 if (filedata->is_separate)
8647 printf (_("\nThere are no sections group in linked file '%s'.\n"),
8648 filedata->file_name);
8649 else
8650 printf (_("\nThere are no section groups in this file.\n"));
8651 }
8652 return true;
8653 }
8654
8655 if (filedata->section_headers == NULL)
8656 {
8657 error (_("Section headers are not available!\n"));
8658 /* PR 13622: This can happen with a corrupt ELF header. */
8659 return false;
8660 }
8661
8662 filedata->section_headers_groups
8663 = (struct group **) calloc (filedata->file_header.e_shnum,
8664 sizeof (struct group *));
8665
8666 if (filedata->section_headers_groups == NULL)
8667 {
8668 error (_("Out of memory reading %u section group headers\n"),
8669 filedata->file_header.e_shnum);
8670 return false;
8671 }
8672
8673 /* Scan the sections for the group section. */
8674 filedata->group_count = 0;
8675 for (i = 0, section = filedata->section_headers;
8676 i < filedata->file_header.e_shnum;
8677 i++, section++)
8678 if (section->sh_type == SHT_GROUP)
8679 filedata->group_count++;
8680
8681 if (filedata->group_count == 0)
8682 {
8683 if (do_section_groups)
8684 {
8685 if (filedata->is_separate)
8686 printf (_("\nThere are no section groups in linked file '%s'.\n"),
8687 filedata->file_name);
8688 else
8689 printf (_("\nThere are no section groups in this file.\n"));
8690 }
8691
8692 return true;
8693 }
8694
8695 filedata->section_groups = (struct group *) calloc (filedata->group_count,
8696 sizeof (struct group));
8697
8698 if (filedata->section_groups == NULL)
8699 {
8700 error (_("Out of memory reading %zu groups\n"), filedata->group_count);
8701 return false;
8702 }
8703
8704 symtab_sec = NULL;
8705 strtab_sec = NULL;
8706 symtab = NULL;
8707 num_syms = 0;
8708 strtab = NULL;
8709 strtab_size = 0;
8710
8711 if (filedata->is_separate)
8712 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
8713
8714 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
8715 i < filedata->file_header.e_shnum;
8716 i++, section++)
8717 {
8718 if (section->sh_type == SHT_GROUP)
8719 {
8720 const char * name = printable_section_name (filedata, section);
8721 const char * group_name;
8722 unsigned char * start;
8723 unsigned char * indices;
8724 unsigned int entry, j, size;
8725 Elf_Internal_Shdr * sec;
8726 Elf_Internal_Sym * sym;
8727
8728 /* Get the symbol table. */
8729 if (section->sh_link >= filedata->file_header.e_shnum
8730 || ((sec = filedata->section_headers + section->sh_link)->sh_type
8731 != SHT_SYMTAB))
8732 {
8733 error (_("Bad sh_link in group section `%s'\n"), name);
8734 continue;
8735 }
8736
8737 if (symtab_sec != sec)
8738 {
8739 symtab_sec = sec;
8740 free (symtab);
8741 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
8742 }
8743
8744 if (symtab == NULL)
8745 {
8746 error (_("Corrupt header in group section `%s'\n"), name);
8747 continue;
8748 }
8749
8750 if (section->sh_info >= num_syms)
8751 {
8752 error (_("Bad sh_info in group section `%s'\n"), name);
8753 continue;
8754 }
8755
8756 sym = symtab + section->sh_info;
8757
8758 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8759 {
8760 if (sym->st_shndx == 0
8761 || sym->st_shndx >= filedata->file_header.e_shnum)
8762 {
8763 error (_("Bad sh_info in group section `%s'\n"), name);
8764 continue;
8765 }
8766
8767 group_name = printable_section_name (filedata,
8768 filedata->section_headers
8769 + sym->st_shndx);
8770 strtab_sec = NULL;
8771 free (strtab);
8772 strtab = NULL;
8773 strtab_size = 0;
8774 }
8775 else
8776 {
8777 /* Get the string table. */
8778 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
8779 {
8780 strtab_sec = NULL;
8781 free (strtab);
8782 strtab = NULL;
8783 strtab_size = 0;
8784 }
8785 else if (strtab_sec
8786 != (sec = filedata->section_headers + symtab_sec->sh_link))
8787 {
8788 strtab_sec = sec;
8789 free (strtab);
8790
8791 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
8792 1, strtab_sec->sh_size,
8793 _("string table"));
8794 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
8795 }
8796 group_name = sym->st_name < strtab_size
8797 ? strtab + sym->st_name : _("<corrupt>");
8798 }
8799
8800 /* PR 17531: file: loop. */
8801 if (section->sh_entsize > section->sh_size)
8802 {
8803 error (_("Section %s has sh_entsize (%#" PRIx64 ")"
8804 " which is larger than its size (%#" PRIx64 ")\n"),
8805 printable_section_name (filedata, section),
8806 section->sh_entsize,
8807 section->sh_size);
8808 continue;
8809 }
8810
8811 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
8812 1, section->sh_size,
8813 _("section data"));
8814 if (start == NULL)
8815 continue;
8816
8817 indices = start;
8818 size = (section->sh_size / section->sh_entsize) - 1;
8819 entry = byte_get (indices, 4);
8820 indices += 4;
8821
8822 if (do_section_groups)
8823 {
8824 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
8825 get_group_flags (entry), i, name, group_name, size);
8826
8827 printf (_(" [Index] Name\n"));
8828 }
8829
8830 group->group_index = i;
8831
8832 for (j = 0; j < size; j++)
8833 {
8834 struct group_list * g;
8835
8836 entry = byte_get (indices, 4);
8837 indices += 4;
8838
8839 if (entry >= filedata->file_header.e_shnum)
8840 {
8841 static unsigned num_group_errors = 0;
8842
8843 if (num_group_errors ++ < 10)
8844 {
8845 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
8846 entry, i, filedata->file_header.e_shnum - 1);
8847 if (num_group_errors == 10)
8848 warn (_("Further error messages about overlarge group section indices suppressed\n"));
8849 }
8850 continue;
8851 }
8852
8853 if (filedata->section_headers_groups [entry] != NULL)
8854 {
8855 if (entry)
8856 {
8857 static unsigned num_errs = 0;
8858
8859 if (num_errs ++ < 10)
8860 {
8861 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
8862 entry, i,
8863 filedata->section_headers_groups [entry]->group_index);
8864 if (num_errs == 10)
8865 warn (_("Further error messages about already contained group sections suppressed\n"));
8866 }
8867 continue;
8868 }
8869 else
8870 {
8871 /* Intel C/C++ compiler may put section 0 in a
8872 section group. We just warn it the first time
8873 and ignore it afterwards. */
8874 static bool warned = false;
8875 if (!warned)
8876 {
8877 error (_("section 0 in group section [%5u]\n"),
8878 filedata->section_headers_groups [entry]->group_index);
8879 warned = true;
8880 }
8881 }
8882 }
8883
8884 filedata->section_headers_groups [entry] = group;
8885
8886 if (do_section_groups)
8887 {
8888 sec = filedata->section_headers + entry;
8889 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
8890 }
8891
8892 g = (struct group_list *) xmalloc (sizeof (struct group_list));
8893 g->section_index = entry;
8894 g->next = group->root;
8895 group->root = g;
8896 }
8897
8898 free (start);
8899
8900 group++;
8901 }
8902 }
8903
8904 free (symtab);
8905 free (strtab);
8906 return true;
8907}
8908
8909/* Data used to display dynamic fixups. */
8910
8911struct ia64_vms_dynfixup
8912{
8913 uint64_t needed_ident; /* Library ident number. */
8914 uint64_t needed; /* Index in the dstrtab of the library name. */
8915 uint64_t fixup_needed; /* Index of the library. */
8916 uint64_t fixup_rela_cnt; /* Number of fixups. */
8917 uint64_t fixup_rela_off; /* Fixups offset in the dynamic segment. */
8918};
8919
8920/* Data used to display dynamic relocations. */
8921
8922struct ia64_vms_dynimgrela
8923{
8924 uint64_t img_rela_cnt; /* Number of relocations. */
8925 uint64_t img_rela_off; /* Reloc offset in the dynamic segment. */
8926};
8927
8928/* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
8929 library). */
8930
8931static bool
8932dump_ia64_vms_dynamic_fixups (Filedata * filedata,
8933 struct ia64_vms_dynfixup * fixup,
8934 const char * strtab,
8935 unsigned int strtab_sz)
8936{
8937 Elf64_External_VMS_IMAGE_FIXUP * imfs;
8938 size_t i;
8939 const char * lib_name;
8940
8941 imfs = get_data (NULL, filedata,
8942 filedata->dynamic_addr + fixup->fixup_rela_off,
8943 sizeof (*imfs), fixup->fixup_rela_cnt,
8944 _("dynamic section image fixups"));
8945 if (!imfs)
8946 return false;
8947
8948 if (fixup->needed < strtab_sz)
8949 lib_name = strtab + fixup->needed;
8950 else
8951 {
8952 warn (_("corrupt library name index of %#" PRIx64
8953 " found in dynamic entry"), fixup->needed);
8954 lib_name = "???";
8955 }
8956
8957 printf (_("\nImage fixups for needed library #%" PRId64
8958 ": %s - ident: %" PRIx64 "\n"),
8959 fixup->fixup_needed, lib_name, fixup->needed_ident);
8960 printf
8961 (_("Seg Offset Type SymVec DataType\n"));
8962
8963 for (i = 0; i < (size_t) fixup->fixup_rela_cnt; i++)
8964 {
8965 unsigned int type;
8966 const char *rtype;
8967
8968 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
8969 printf ("%016" PRIx64 " ", BYTE_GET (imfs [i].fixup_offset));
8970 type = BYTE_GET (imfs [i].type);
8971 rtype = elf_ia64_reloc_type (type);
8972 if (rtype == NULL)
8973 printf ("0x%08x ", type);
8974 else
8975 printf ("%-32s ", rtype);
8976 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
8977 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
8978 }
8979
8980 free (imfs);
8981 return true;
8982}
8983
8984/* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
8985
8986static bool
8987dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
8988{
8989 Elf64_External_VMS_IMAGE_RELA *imrs;
8990 size_t i;
8991
8992 imrs = get_data (NULL, filedata,
8993 filedata->dynamic_addr + imgrela->img_rela_off,
8994 sizeof (*imrs), imgrela->img_rela_cnt,
8995 _("dynamic section image relocations"));
8996 if (!imrs)
8997 return false;
8998
8999 printf (_("\nImage relocs\n"));
9000 printf
9001 (_("Seg Offset Type Addend Seg Sym Off\n"));
9002
9003 for (i = 0; i < (size_t) imgrela->img_rela_cnt; i++)
9004 {
9005 unsigned int type;
9006 const char *rtype;
9007
9008 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
9009 printf ("%08" PRIx64 " ", BYTE_GET (imrs [i].rela_offset));
9010 type = BYTE_GET (imrs [i].type);
9011 rtype = elf_ia64_reloc_type (type);
9012 if (rtype == NULL)
9013 printf ("0x%08x ", type);
9014 else
9015 printf ("%-31s ", rtype);
9016 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
9017 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
9018 printf ("%08" PRIx64 "\n", BYTE_GET (imrs [i].sym_offset));
9019 }
9020
9021 free (imrs);
9022 return true;
9023}
9024
9025/* Display IA-64 OpenVMS dynamic relocations and fixups. */
9026
9027static bool
9028process_ia64_vms_dynamic_relocs (Filedata * filedata)
9029{
9030 struct ia64_vms_dynfixup fixup;
9031 struct ia64_vms_dynimgrela imgrela;
9032 Elf_Internal_Dyn *entry;
9033 uint64_t strtab_off = 0;
9034 uint64_t strtab_sz = 0;
9035 char *strtab = NULL;
9036 bool res = true;
9037
9038 memset (&fixup, 0, sizeof (fixup));
9039 memset (&imgrela, 0, sizeof (imgrela));
9040
9041 /* Note: the order of the entries is specified by the OpenVMS specs. */
9042 for (entry = filedata->dynamic_section;
9043 entry < filedata->dynamic_section + filedata->dynamic_nent;
9044 entry++)
9045 {
9046 switch (entry->d_tag)
9047 {
9048 case DT_IA_64_VMS_STRTAB_OFFSET:
9049 strtab_off = entry->d_un.d_val;
9050 break;
9051 case DT_STRSZ:
9052 strtab_sz = entry->d_un.d_val;
9053 if (strtab == NULL)
9054 strtab = get_data (NULL, filedata,
9055 filedata->dynamic_addr + strtab_off,
9056 1, strtab_sz, _("dynamic string section"));
9057 if (strtab == NULL)
9058 strtab_sz = 0;
9059 break;
9060
9061 case DT_IA_64_VMS_NEEDED_IDENT:
9062 fixup.needed_ident = entry->d_un.d_val;
9063 break;
9064 case DT_NEEDED:
9065 fixup.needed = entry->d_un.d_val;
9066 break;
9067 case DT_IA_64_VMS_FIXUP_NEEDED:
9068 fixup.fixup_needed = entry->d_un.d_val;
9069 break;
9070 case DT_IA_64_VMS_FIXUP_RELA_CNT:
9071 fixup.fixup_rela_cnt = entry->d_un.d_val;
9072 break;
9073 case DT_IA_64_VMS_FIXUP_RELA_OFF:
9074 fixup.fixup_rela_off = entry->d_un.d_val;
9075 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
9076 res = false;
9077 break;
9078 case DT_IA_64_VMS_IMG_RELA_CNT:
9079 imgrela.img_rela_cnt = entry->d_un.d_val;
9080 break;
9081 case DT_IA_64_VMS_IMG_RELA_OFF:
9082 imgrela.img_rela_off = entry->d_un.d_val;
9083 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
9084 res = false;
9085 break;
9086
9087 default:
9088 break;
9089 }
9090 }
9091
9092 free (strtab);
9093
9094 return res;
9095}
9096
9097static struct
9098{
9099 const char * name;
9100 int reloc;
9101 int size;
9102 relocation_type rel_type;
9103}
9104 dynamic_relocations [] =
9105{
9106 { "REL", DT_REL, DT_RELSZ, reltype_rel },
9107 { "RELA", DT_RELA, DT_RELASZ, reltype_rela },
9108 { "RELR", DT_RELR, DT_RELRSZ, reltype_relr },
9109 { "PLT", DT_JMPREL, DT_PLTRELSZ, reltype_unknown }
9110};
9111
9112static relocation_type
9113rel_type_from_sh_type (unsigned int sh_type)
9114{
9115 switch (sh_type)
9116 {
9117 case SHT_RELA: return reltype_rela;
9118 case SHT_REL: return reltype_rel;
9119 case SHT_RELR: return reltype_relr;
9120 default: return reltype_unknown;
9121 }
9122}
9123
9124static bool
9125display_relocations (Elf_Internal_Shdr * section,
9126 Filedata * filedata)
9127{
9128 relocation_type rel_type = rel_type_from_sh_type (section->sh_type);
9129
9130 if (rel_type == reltype_unknown)
9131 return false;
9132
9133 uint64_t rel_size = section->sh_size;
9134
9135 if (rel_size == 0)
9136 return false;
9137
9138 if (filedata->is_separate)
9139 printf (_("\nIn linked file '%s' relocation section "),
9140 filedata->file_name);
9141 else
9142 printf (_("\nRelocation section "));
9143
9144 if (filedata->string_table == NULL)
9145 printf ("%d", section->sh_name);
9146 else
9147 printf ("'%s'", printable_section_name (filedata, section));
9148
9149 uint64_t num_rela = rel_size / section->sh_entsize;
9150 uint64_t rel_offset = section->sh_offset;
9151
9152 if (rel_type == reltype_relr)
9153 {
9154 /* Just stating the 'number of entries' in a RELR section can be
9155 misleading, since this is not the number of locations relocated, but
9156 the number of words in the compressed RELR format. So also provide
9157 the number of locations affected. */
9158 if (num_rela == 1)
9159 /* This is unlikely, but possible. */
9160 printf (_(" at offset %#" PRIx64
9161 " contains 1 entry which relocates 1 location:\n"),
9162 rel_offset);
9163 else
9164 printf (_(" at offset %#" PRIx64 " contains %" PRIu64
9165 " entries which relocate %" PRIu64 " locations:\n"),
9166 rel_offset, num_rela, count_relr_relocations (filedata, section));
9167 }
9168 else
9169 {
9170 printf (ngettext (" at offset %#" PRIx64
9171 " contains %" PRIu64 " entry:\n",
9172 " at offset %#" PRIx64
9173 " contains %" PRIu64 " entries:\n",
9174 num_rela),
9175 rel_offset, num_rela);
9176 }
9177
9178 Elf_Internal_Shdr * symsec;
9179 Elf_Internal_Sym * symtab = NULL;
9180 uint64_t nsyms = 0;
9181 uint64_t strtablen = 0;
9182 char * strtab = NULL;
9183
9184 if (section->sh_link == 0
9185 || section->sh_link >= filedata->file_header.e_shnum)
9186 {
9187 /* Symbol data not available.
9188 This can happen, especially with RELR relocs.
9189 See if there is a .symtab section present.
9190 If so then use it. */
9191 symsec = find_section_by_name (filedata, ".symtab");
9192 }
9193 else
9194 {
9195 symsec = filedata->section_headers + section->sh_link;
9196
9197 if (symsec->sh_type != SHT_SYMTAB
9198 && symsec->sh_type != SHT_DYNSYM)
9199 return false;
9200 }
9201
9202 if (symsec != NULL
9203 && !get_symtab (filedata, symsec, &symtab, &nsyms, &strtab, &strtablen))
9204 return false;
9205
9206 bool res;
9207
9208 if (rel_type == reltype_relr)
9209 res = dump_relr_relocations (filedata, section, symtab, nsyms, strtab, strtablen);
9210 else
9211 res = dump_relocations (filedata, rel_offset, rel_size,
9212 symtab, nsyms, strtab, strtablen,
9213 rel_type,
9214 symsec == NULL ? false : symsec->sh_type == SHT_DYNSYM);
9215 free (strtab);
9216 free (symtab);
9217
9218 return res;
9219}
9220
9221/* Process the reloc section. */
9222
9223static bool
9224process_relocs (Filedata * filedata)
9225{
9226 uint64_t rel_size;
9227 uint64_t rel_offset;
9228
9229 if (!do_reloc)
9230 return true;
9231
9232 if (do_using_dynamic)
9233 {
9234 relocation_type rel_type;
9235 const char * name;
9236 bool has_dynamic_reloc;
9237 unsigned int i;
9238
9239 has_dynamic_reloc = false;
9240
9241 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
9242 {
9243 rel_type = dynamic_relocations [i].rel_type;
9244 name = dynamic_relocations [i].name;
9245 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
9246 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
9247
9248 if (rel_size)
9249 has_dynamic_reloc = true;
9250
9251 if (rel_type == reltype_unknown)
9252 {
9253 if (dynamic_relocations [i].reloc == DT_JMPREL)
9254 switch (filedata->dynamic_info[DT_PLTREL])
9255 {
9256 case DT_REL:
9257 rel_type = reltype_rel;
9258 break;
9259 case DT_RELA:
9260 rel_type = reltype_rela;
9261 break;
9262 }
9263 }
9264
9265 if (rel_size)
9266 {
9267 if (filedata->is_separate)
9268 printf
9269 (_("\nIn linked file '%s' section '%s' at offset %#" PRIx64
9270 " contains %" PRId64 " bytes:\n"),
9271 filedata->file_name, name, rel_offset, rel_size);
9272 else
9273 printf
9274 (_("\n'%s' relocation section at offset %#" PRIx64
9275 " contains %" PRId64 " bytes:\n"),
9276 name, rel_offset, rel_size);
9277
9278 dump_relocations (filedata,
9279 offset_from_vma (filedata, rel_offset, rel_size),
9280 rel_size,
9281 filedata->dynamic_symbols,
9282 filedata->num_dynamic_syms,
9283 filedata->dynamic_strings,
9284 filedata->dynamic_strings_length,
9285 rel_type, true /* is_dynamic */);
9286 }
9287 }
9288
9289 if (is_ia64_vms (filedata))
9290 if (process_ia64_vms_dynamic_relocs (filedata))
9291 has_dynamic_reloc = true;
9292
9293 if (! has_dynamic_reloc)
9294 {
9295 if (filedata->is_separate)
9296 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
9297 filedata->file_name);
9298 else
9299 printf (_("\nThere are no dynamic relocations in this file.\n"));
9300 }
9301 }
9302 else
9303 {
9304 Elf_Internal_Shdr * section;
9305 size_t i;
9306 bool found = false;
9307
9308 for (i = 0, section = filedata->section_headers;
9309 i < filedata->file_header.e_shnum;
9310 i++, section++)
9311 {
9312 if (display_relocations (section, filedata))
9313 found = true;
9314 }
9315
9316 if (! found)
9317 {
9318 /* Users sometimes forget the -D option, so try to be helpful. */
9319 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
9320 {
9321 if (filedata->dynamic_info[dynamic_relocations [i].size])
9322 {
9323 if (filedata->is_separate)
9324 printf (_("\nThere are no static relocations in linked file '%s'."),
9325 filedata->file_name);
9326 else
9327 printf (_("\nThere are no static relocations in this file."));
9328 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
9329
9330 break;
9331 }
9332 }
9333 if (i == ARRAY_SIZE (dynamic_relocations))
9334 {
9335 if (filedata->is_separate)
9336 printf (_("\nThere are no relocations in linked file '%s'.\n"),
9337 filedata->file_name);
9338 else
9339 printf (_("\nThere are no relocations in this file.\n"));
9340 }
9341 }
9342 }
9343
9344 return true;
9345}
9346
9347/* An absolute address consists of a section and an offset. If the
9348 section is NULL, the offset itself is the address, otherwise, the
9349 address equals to LOAD_ADDRESS(section) + offset. */
9350
9351struct absaddr
9352{
9353 unsigned short section;
9354 uint64_t offset;
9355};
9356
9357/* Find the nearest symbol at or below ADDR. Returns the symbol
9358 name, if found, and the offset from the symbol to ADDR. */
9359
9360static void
9361find_symbol_for_address (Filedata *filedata,
9362 Elf_Internal_Sym *symtab,
9363 uint64_t nsyms,
9364 const char *strtab,
9365 uint64_t strtab_size,
9366 struct absaddr addr,
9367 const char **symname,
9368 uint64_t *offset)
9369{
9370 uint64_t dist = 0x100000;
9371 Elf_Internal_Sym * sym;
9372 Elf_Internal_Sym * beg;
9373 Elf_Internal_Sym * end;
9374 Elf_Internal_Sym * best = NULL;
9375
9376 REMOVE_ARCH_BITS (addr.offset);
9377 beg = symtab;
9378 end = symtab + nsyms;
9379
9380 while (beg < end)
9381 {
9382 uint64_t value;
9383
9384 sym = beg + (end - beg) / 2;
9385
9386 value = sym->st_value;
9387 REMOVE_ARCH_BITS (value);
9388
9389 if (sym->st_name != 0
9390 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
9391 && addr.offset >= value
9392 && addr.offset - value < dist)
9393 {
9394 best = sym;
9395 dist = addr.offset - value;
9396 if (!dist)
9397 break;
9398 }
9399
9400 if (addr.offset < value)
9401 end = sym;
9402 else
9403 beg = sym + 1;
9404 }
9405
9406 if (best)
9407 {
9408 *symname = (best->st_name >= strtab_size
9409 ? _("<corrupt>") : strtab + best->st_name);
9410 *offset = dist;
9411 return;
9412 }
9413
9414 *symname = NULL;
9415 *offset = addr.offset;
9416}
9417
9418/* Process the unwind section. */
9419
9420#include "unwind-ia64.h"
9421
9422struct ia64_unw_table_entry
9423{
9424 struct absaddr start;
9425 struct absaddr end;
9426 struct absaddr info;
9427};
9428
9429struct ia64_unw_aux_info
9430{
9431 struct ia64_unw_table_entry * table; /* Unwind table. */
9432 uint64_t table_len; /* Length of unwind table. */
9433 unsigned char * info; /* Unwind info. */
9434 uint64_t info_size; /* Size of unwind info. */
9435 uint64_t info_addr; /* Starting address of unwind info. */
9436 uint64_t seg_base; /* Starting address of segment. */
9437 Elf_Internal_Sym * symtab; /* The symbol table. */
9438 uint64_t nsyms; /* Number of symbols. */
9439 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9440 uint64_t nfuns; /* Number of entries in funtab. */
9441 char * strtab; /* The string table. */
9442 uint64_t strtab_size; /* Size of string table. */
9443};
9444
9445static bool
9446dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
9447{
9448 struct ia64_unw_table_entry * tp;
9449 size_t j, nfuns;
9450 int in_body;
9451 bool res = true;
9452
9453 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9454 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9455 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9456 aux->funtab[nfuns++] = aux->symtab[j];
9457 aux->nfuns = nfuns;
9458 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9459
9460 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
9461 {
9462 uint64_t stamp;
9463 uint64_t offset;
9464 const unsigned char * dp;
9465 const unsigned char * head;
9466 const unsigned char * end;
9467 const char * procname;
9468
9469 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9470 aux->strtab_size, tp->start, &procname, &offset);
9471
9472 fputs ("\n<", stdout);
9473
9474 if (procname)
9475 {
9476 fputs (procname, stdout);
9477
9478 if (offset)
9479 printf ("+%" PRIx64, offset);
9480 }
9481
9482 fputs (">: [", stdout);
9483 print_vma (tp->start.offset, PREFIX_HEX);
9484 fputc ('-', stdout);
9485 print_vma (tp->end.offset, PREFIX_HEX);
9486 printf ("], info at +0x%" PRIx64 "\n",
9487 tp->info.offset - aux->seg_base);
9488
9489 /* PR 17531: file: 86232b32. */
9490 if (aux->info == NULL)
9491 continue;
9492
9493 offset = tp->info.offset;
9494 if (tp->info.section)
9495 {
9496 if (tp->info.section >= filedata->file_header.e_shnum)
9497 {
9498 warn (_("Invalid section %u in table entry %td\n"),
9499 tp->info.section, tp - aux->table);
9500 res = false;
9501 continue;
9502 }
9503 offset += filedata->section_headers[tp->info.section].sh_addr;
9504 }
9505 offset -= aux->info_addr;
9506 /* PR 17531: file: 0997b4d1. */
9507 if (offset >= aux->info_size
9508 || aux->info_size - offset < 8)
9509 {
9510 warn (_("Invalid offset %" PRIx64 " in table entry %td\n"),
9511 tp->info.offset, tp - aux->table);
9512 res = false;
9513 continue;
9514 }
9515
9516 head = aux->info + offset;
9517 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
9518
9519 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
9520 (unsigned) UNW_VER (stamp),
9521 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
9522 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
9523 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
9524 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
9525
9526 if (UNW_VER (stamp) != 1)
9527 {
9528 printf (_("\tUnknown version.\n"));
9529 continue;
9530 }
9531
9532 in_body = 0;
9533 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
9534 /* PR 17531: file: 16ceda89. */
9535 if (end > aux->info + aux->info_size)
9536 end = aux->info + aux->info_size;
9537 for (dp = head + 8; dp < end;)
9538 dp = unw_decode (dp, in_body, & in_body, end);
9539 }
9540
9541 free (aux->funtab);
9542
9543 return res;
9544}
9545
9546static bool
9547slurp_ia64_unwind_table (Filedata * filedata,
9548 struct ia64_unw_aux_info * aux,
9549 Elf_Internal_Shdr * sec)
9550{
9551 uint64_t size, nrelas, i;
9552 Elf_Internal_Phdr * seg;
9553 struct ia64_unw_table_entry * tep;
9554 Elf_Internal_Shdr * relsec;
9555 Elf_Internal_Rela * rela;
9556 Elf_Internal_Rela * rp;
9557 unsigned char * table;
9558 unsigned char * tp;
9559 Elf_Internal_Sym * sym;
9560 const char * relname;
9561
9562 aux->table_len = 0;
9563
9564 /* First, find the starting address of the segment that includes
9565 this section: */
9566
9567 if (filedata->file_header.e_phnum)
9568 {
9569 if (! get_program_headers (filedata))
9570 return false;
9571
9572 for (seg = filedata->program_headers;
9573 seg < filedata->program_headers + filedata->file_header.e_phnum;
9574 ++seg)
9575 {
9576 if (seg->p_type != PT_LOAD)
9577 continue;
9578
9579 if (sec->sh_addr >= seg->p_vaddr
9580 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
9581 {
9582 aux->seg_base = seg->p_vaddr;
9583 break;
9584 }
9585 }
9586 }
9587
9588 /* Second, build the unwind table from the contents of the unwind section: */
9589 size = sec->sh_size;
9590 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
9591 _("unwind table"));
9592 if (!table)
9593 return false;
9594
9595 aux->table_len = size / (3 * eh_addr_size);
9596 aux->table = (struct ia64_unw_table_entry *)
9597 xcmalloc (aux->table_len, sizeof (aux->table[0]));
9598 tep = aux->table;
9599
9600 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
9601 {
9602 tep->start.section = SHN_UNDEF;
9603 tep->end.section = SHN_UNDEF;
9604 tep->info.section = SHN_UNDEF;
9605 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
9606 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
9607 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
9608 tep->start.offset += aux->seg_base;
9609 tep->end.offset += aux->seg_base;
9610 tep->info.offset += aux->seg_base;
9611 }
9612 free (table);
9613
9614 /* Third, apply any relocations to the unwind table: */
9615 for (relsec = filedata->section_headers;
9616 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9617 ++relsec)
9618 {
9619 if (relsec->sh_type != SHT_RELA
9620 || relsec->sh_info >= filedata->file_header.e_shnum
9621 || filedata->section_headers + relsec->sh_info != sec)
9622 continue;
9623
9624 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
9625 & rela, & nrelas))
9626 {
9627 free (aux->table);
9628 aux->table = NULL;
9629 aux->table_len = 0;
9630 return false;
9631 }
9632
9633 for (rp = rela; rp < rela + nrelas; ++rp)
9634 {
9635 unsigned int sym_ndx;
9636 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
9637 relname = elf_ia64_reloc_type (r_type);
9638
9639 /* PR 17531: file: 9fa67536. */
9640 if (relname == NULL)
9641 {
9642 warn (_("Skipping unknown relocation type: %u\n"), r_type);
9643 continue;
9644 }
9645
9646 if (! startswith (relname, "R_IA64_SEGREL"))
9647 {
9648 warn (_("Skipping unexpected relocation type: %s\n"), relname);
9649 continue;
9650 }
9651
9652 i = rp->r_offset / (3 * eh_addr_size);
9653
9654 /* PR 17531: file: 5bc8d9bf. */
9655 if (i >= aux->table_len)
9656 {
9657 warn (_("Skipping reloc with overlarge offset: %#" PRIx64 "\n"),
9658 i);
9659 continue;
9660 }
9661
9662 sym_ndx = get_reloc_symindex (rp->r_info);
9663 if (sym_ndx >= aux->nsyms)
9664 {
9665 warn (_("Skipping reloc with invalid symbol index: %u\n"),
9666 sym_ndx);
9667 continue;
9668 }
9669 sym = aux->symtab + sym_ndx;
9670
9671 switch (rp->r_offset / eh_addr_size % 3)
9672 {
9673 case 0:
9674 aux->table[i].start.section = sym->st_shndx;
9675 aux->table[i].start.offset = rp->r_addend + sym->st_value;
9676 break;
9677 case 1:
9678 aux->table[i].end.section = sym->st_shndx;
9679 aux->table[i].end.offset = rp->r_addend + sym->st_value;
9680 break;
9681 case 2:
9682 aux->table[i].info.section = sym->st_shndx;
9683 aux->table[i].info.offset = rp->r_addend + sym->st_value;
9684 break;
9685 default:
9686 break;
9687 }
9688 }
9689
9690 free (rela);
9691 }
9692
9693 return true;
9694}
9695
9696static bool
9697ia64_process_unwind (Filedata * filedata)
9698{
9699 Elf_Internal_Shdr * sec;
9700 Elf_Internal_Shdr * unwsec = NULL;
9701 uint64_t i, unwcount = 0, unwstart = 0;
9702 struct ia64_unw_aux_info aux;
9703 bool res = true;
9704
9705 memset (& aux, 0, sizeof (aux));
9706
9707 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9708 {
9709 if (sec->sh_type == SHT_SYMTAB)
9710 {
9711 if (aux.symtab)
9712 {
9713 error (_("Multiple symbol tables encountered\n"));
9714 free (aux.symtab);
9715 aux.symtab = NULL;
9716 free (aux.strtab);
9717 aux.strtab = NULL;
9718 }
9719 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9720 &aux.strtab, &aux.strtab_size))
9721 return false;
9722 }
9723 else if (sec->sh_type == SHT_IA_64_UNWIND)
9724 unwcount++;
9725 }
9726
9727 if (!unwcount)
9728 printf (_("\nThere are no unwind sections in this file.\n"));
9729
9730 while (unwcount-- > 0)
9731 {
9732 const char *suffix;
9733 size_t len, len2;
9734
9735 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
9736 i < filedata->file_header.e_shnum; ++i, ++sec)
9737 if (sec->sh_type == SHT_IA_64_UNWIND)
9738 {
9739 unwsec = sec;
9740 break;
9741 }
9742 /* We have already counted the number of SHT_IA64_UNWIND
9743 sections so the loop above should never fail. */
9744 assert (unwsec != NULL);
9745
9746 unwstart = i + 1;
9747 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
9748
9749 if ((unwsec->sh_flags & SHF_GROUP) != 0)
9750 {
9751 /* We need to find which section group it is in. */
9752 struct group_list * g;
9753
9754 if (filedata->section_headers_groups == NULL
9755 || filedata->section_headers_groups[i] == NULL)
9756 i = filedata->file_header.e_shnum;
9757 else
9758 {
9759 g = filedata->section_headers_groups[i]->root;
9760
9761 for (; g != NULL; g = g->next)
9762 {
9763 sec = filedata->section_headers + g->section_index;
9764
9765 if (section_name_valid (filedata, sec)
9766 && streq (section_name (filedata, sec),
9767 ELF_STRING_ia64_unwind_info))
9768 break;
9769 }
9770
9771 if (g == NULL)
9772 i = filedata->file_header.e_shnum;
9773 }
9774 }
9775 else if (section_name_valid (filedata, unwsec)
9776 && startswith (section_name (filedata, unwsec),
9777 ELF_STRING_ia64_unwind_once))
9778 {
9779 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
9780 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
9781 suffix = section_name (filedata, unwsec) + len;
9782 for (i = 0, sec = filedata->section_headers;
9783 i < filedata->file_header.e_shnum;
9784 ++i, ++sec)
9785 if (section_name_valid (filedata, sec)
9786 && startswith (section_name (filedata, sec),
9787 ELF_STRING_ia64_unwind_info_once)
9788 && streq (section_name (filedata, sec) + len2, suffix))
9789 break;
9790 }
9791 else
9792 {
9793 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
9794 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
9795 len = sizeof (ELF_STRING_ia64_unwind) - 1;
9796 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
9797 suffix = "";
9798 if (section_name_valid (filedata, unwsec)
9799 && startswith (section_name (filedata, unwsec),
9800 ELF_STRING_ia64_unwind))
9801 suffix = section_name (filedata, unwsec) + len;
9802 for (i = 0, sec = filedata->section_headers;
9803 i < filedata->file_header.e_shnum;
9804 ++i, ++sec)
9805 if (section_name_valid (filedata, sec)
9806 && startswith (section_name (filedata, sec),
9807 ELF_STRING_ia64_unwind_info)
9808 && streq (section_name (filedata, sec) + len2, suffix))
9809 break;
9810 }
9811
9812 if (i == filedata->file_header.e_shnum)
9813 {
9814 printf (_("\nCould not find unwind info section for "));
9815
9816 if (filedata->string_table == NULL)
9817 printf ("%d", unwsec->sh_name);
9818 else
9819 printf ("'%s'", printable_section_name (filedata, unwsec));
9820 }
9821 else
9822 {
9823 aux.info_addr = sec->sh_addr;
9824 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
9825 sec->sh_size,
9826 _("unwind info"));
9827 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
9828
9829 printf (_("\nUnwind section "));
9830
9831 if (filedata->string_table == NULL)
9832 printf ("%d", unwsec->sh_name);
9833 else
9834 printf ("'%s'", printable_section_name (filedata, unwsec));
9835
9836 printf (_(" at offset %#" PRIx64 " contains %" PRIu64 " entries:\n"),
9837 unwsec->sh_offset,
9838 unwsec->sh_size / (3 * eh_addr_size));
9839
9840 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
9841 && aux.table_len > 0)
9842 dump_ia64_unwind (filedata, & aux);
9843
9844 free ((char *) aux.table);
9845 free ((char *) aux.info);
9846 aux.table = NULL;
9847 aux.info = NULL;
9848 }
9849 }
9850
9851 free (aux.symtab);
9852 free ((char *) aux.strtab);
9853
9854 return res;
9855}
9856
9857struct hppa_unw_table_entry
9858{
9859 struct absaddr start;
9860 struct absaddr end;
9861 unsigned int Cannot_unwind:1; /* 0 */
9862 unsigned int Millicode:1; /* 1 */
9863 unsigned int Millicode_save_sr0:1; /* 2 */
9864 unsigned int Region_description:2; /* 3..4 */
9865 unsigned int reserved1:1; /* 5 */
9866 unsigned int Entry_SR:1; /* 6 */
9867 unsigned int Entry_FR:4; /* Number saved 7..10 */
9868 unsigned int Entry_GR:5; /* Number saved 11..15 */
9869 unsigned int Args_stored:1; /* 16 */
9870 unsigned int Variable_Frame:1; /* 17 */
9871 unsigned int Separate_Package_Body:1; /* 18 */
9872 unsigned int Frame_Extension_Millicode:1; /* 19 */
9873 unsigned int Stack_Overflow_Check:1; /* 20 */
9874 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
9875 unsigned int Ada_Region:1; /* 22 */
9876 unsigned int cxx_info:1; /* 23 */
9877 unsigned int cxx_try_catch:1; /* 24 */
9878 unsigned int sched_entry_seq:1; /* 25 */
9879 unsigned int reserved2:1; /* 26 */
9880 unsigned int Save_SP:1; /* 27 */
9881 unsigned int Save_RP:1; /* 28 */
9882 unsigned int Save_MRP_in_frame:1; /* 29 */
9883 unsigned int extn_ptr_defined:1; /* 30 */
9884 unsigned int Cleanup_defined:1; /* 31 */
9885
9886 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
9887 unsigned int HP_UX_interrupt_marker:1; /* 1 */
9888 unsigned int Large_frame:1; /* 2 */
9889 unsigned int Pseudo_SP_Set:1; /* 3 */
9890 unsigned int reserved4:1; /* 4 */
9891 unsigned int Total_frame_size:27; /* 5..31 */
9892};
9893
9894struct hppa_unw_aux_info
9895{
9896 struct hppa_unw_table_entry * table; /* Unwind table. */
9897 uint64_t table_len; /* Length of unwind table. */
9898 uint64_t seg_base; /* Starting address of segment. */
9899 Elf_Internal_Sym * symtab; /* The symbol table. */
9900 uint64_t nsyms; /* Number of symbols. */
9901 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9902 uint64_t nfuns; /* Number of entries in funtab. */
9903 char * strtab; /* The string table. */
9904 uint64_t strtab_size; /* Size of string table. */
9905};
9906
9907static bool
9908dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
9909{
9910 struct hppa_unw_table_entry * tp;
9911 uint64_t j, nfuns;
9912 bool res = true;
9913
9914 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9915 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9916 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9917 aux->funtab[nfuns++] = aux->symtab[j];
9918 aux->nfuns = nfuns;
9919 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9920
9921 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
9922 {
9923 uint64_t offset;
9924 const char * procname;
9925
9926 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9927 aux->strtab_size, tp->start, &procname,
9928 &offset);
9929
9930 fputs ("\n<", stdout);
9931
9932 if (procname)
9933 {
9934 fputs (procname, stdout);
9935
9936 if (offset)
9937 printf ("+%" PRIx64, offset);
9938 }
9939
9940 fputs (">: [", stdout);
9941 print_vma (tp->start.offset, PREFIX_HEX);
9942 fputc ('-', stdout);
9943 print_vma (tp->end.offset, PREFIX_HEX);
9944 printf ("]\n\t");
9945
9946#define PF(_m) if (tp->_m) printf (#_m " ");
9947#define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
9948 PF(Cannot_unwind);
9949 PF(Millicode);
9950 PF(Millicode_save_sr0);
9951 /* PV(Region_description); */
9952 PF(Entry_SR);
9953 PV(Entry_FR);
9954 PV(Entry_GR);
9955 PF(Args_stored);
9956 PF(Variable_Frame);
9957 PF(Separate_Package_Body);
9958 PF(Frame_Extension_Millicode);
9959 PF(Stack_Overflow_Check);
9960 PF(Two_Instruction_SP_Increment);
9961 PF(Ada_Region);
9962 PF(cxx_info);
9963 PF(cxx_try_catch);
9964 PF(sched_entry_seq);
9965 PF(Save_SP);
9966 PF(Save_RP);
9967 PF(Save_MRP_in_frame);
9968 PF(extn_ptr_defined);
9969 PF(Cleanup_defined);
9970 PF(MPE_XL_interrupt_marker);
9971 PF(HP_UX_interrupt_marker);
9972 PF(Large_frame);
9973 PF(Pseudo_SP_Set);
9974 PV(Total_frame_size);
9975#undef PF
9976#undef PV
9977 }
9978
9979 printf ("\n");
9980
9981 free (aux->funtab);
9982
9983 return res;
9984}
9985
9986static bool
9987slurp_hppa_unwind_table (Filedata * filedata,
9988 struct hppa_unw_aux_info * aux,
9989 Elf_Internal_Shdr * sec)
9990{
9991 uint64_t size, unw_ent_size, nentries, nrelas, i;
9992 Elf_Internal_Phdr * seg;
9993 struct hppa_unw_table_entry * tep;
9994 Elf_Internal_Shdr * relsec;
9995 Elf_Internal_Rela * rela;
9996 Elf_Internal_Rela * rp;
9997 unsigned char * table;
9998 unsigned char * tp;
9999 Elf_Internal_Sym * sym;
10000 const char * relname;
10001
10002 /* First, find the starting address of the segment that includes
10003 this section. */
10004 if (filedata->file_header.e_phnum)
10005 {
10006 if (! get_program_headers (filedata))
10007 return false;
10008
10009 for (seg = filedata->program_headers;
10010 seg < filedata->program_headers + filedata->file_header.e_phnum;
10011 ++seg)
10012 {
10013 if (seg->p_type != PT_LOAD)
10014 continue;
10015
10016 if (sec->sh_addr >= seg->p_vaddr
10017 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
10018 {
10019 aux->seg_base = seg->p_vaddr;
10020 break;
10021 }
10022 }
10023 }
10024
10025 /* Second, build the unwind table from the contents of the unwind
10026 section. */
10027 size = sec->sh_size;
10028 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
10029 _("unwind table"));
10030 if (!table)
10031 return false;
10032
10033 unw_ent_size = 16;
10034 nentries = size / unw_ent_size;
10035 size = unw_ent_size * nentries;
10036
10037 aux->table_len = nentries;
10038 tep = aux->table = (struct hppa_unw_table_entry *)
10039 xcmalloc (nentries, sizeof (aux->table[0]));
10040
10041 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
10042 {
10043 unsigned int tmp1, tmp2;
10044
10045 tep->start.section = SHN_UNDEF;
10046 tep->end.section = SHN_UNDEF;
10047
10048 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
10049 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
10050 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
10051 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
10052
10053 tep->start.offset += aux->seg_base;
10054 tep->end.offset += aux->seg_base;
10055
10056 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
10057 tep->Millicode = (tmp1 >> 30) & 0x1;
10058 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
10059 tep->Region_description = (tmp1 >> 27) & 0x3;
10060 tep->reserved1 = (tmp1 >> 26) & 0x1;
10061 tep->Entry_SR = (tmp1 >> 25) & 0x1;
10062 tep->Entry_FR = (tmp1 >> 21) & 0xf;
10063 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
10064 tep->Args_stored = (tmp1 >> 15) & 0x1;
10065 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
10066 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
10067 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
10068 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
10069 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
10070 tep->Ada_Region = (tmp1 >> 9) & 0x1;
10071 tep->cxx_info = (tmp1 >> 8) & 0x1;
10072 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
10073 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
10074 tep->reserved2 = (tmp1 >> 5) & 0x1;
10075 tep->Save_SP = (tmp1 >> 4) & 0x1;
10076 tep->Save_RP = (tmp1 >> 3) & 0x1;
10077 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
10078 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
10079 tep->Cleanup_defined = tmp1 & 0x1;
10080
10081 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
10082 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
10083 tep->Large_frame = (tmp2 >> 29) & 0x1;
10084 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
10085 tep->reserved4 = (tmp2 >> 27) & 0x1;
10086 tep->Total_frame_size = tmp2 & 0x7ffffff;
10087 }
10088 free (table);
10089
10090 /* Third, apply any relocations to the unwind table. */
10091 for (relsec = filedata->section_headers;
10092 relsec < filedata->section_headers + filedata->file_header.e_shnum;
10093 ++relsec)
10094 {
10095 if (relsec->sh_type != SHT_RELA
10096 || relsec->sh_info >= filedata->file_header.e_shnum
10097 || filedata->section_headers + relsec->sh_info != sec)
10098 continue;
10099
10100 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
10101 & rela, & nrelas))
10102 return false;
10103
10104 for (rp = rela; rp < rela + nrelas; ++rp)
10105 {
10106 unsigned int sym_ndx;
10107 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
10108 relname = elf_hppa_reloc_type (r_type);
10109
10110 if (relname == NULL)
10111 {
10112 warn (_("Skipping unknown relocation type: %u\n"), r_type);
10113 continue;
10114 }
10115
10116 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
10117 if (! startswith (relname, "R_PARISC_SEGREL"))
10118 {
10119 warn (_("Skipping unexpected relocation type: %s\n"), relname);
10120 continue;
10121 }
10122
10123 i = rp->r_offset / unw_ent_size;
10124 if (i >= aux->table_len)
10125 {
10126 warn (_("Skipping reloc with overlarge offset: %#" PRIx64 "\n"),
10127 i);
10128 continue;
10129 }
10130
10131 sym_ndx = get_reloc_symindex (rp->r_info);
10132 if (sym_ndx >= aux->nsyms)
10133 {
10134 warn (_("Skipping reloc with invalid symbol index: %u\n"),
10135 sym_ndx);
10136 continue;
10137 }
10138 sym = aux->symtab + sym_ndx;
10139
10140 switch ((rp->r_offset % unw_ent_size) / 4)
10141 {
10142 case 0:
10143 aux->table[i].start.section = sym->st_shndx;
10144 aux->table[i].start.offset = sym->st_value + rp->r_addend;
10145 break;
10146 case 1:
10147 aux->table[i].end.section = sym->st_shndx;
10148 aux->table[i].end.offset = sym->st_value + rp->r_addend;
10149 break;
10150 default:
10151 break;
10152 }
10153 }
10154
10155 free (rela);
10156 }
10157
10158 return true;
10159}
10160
10161static bool
10162hppa_process_unwind (Filedata * filedata)
10163{
10164 struct hppa_unw_aux_info aux;
10165 Elf_Internal_Shdr * unwsec = NULL;
10166 Elf_Internal_Shdr * sec;
10167 size_t i;
10168 bool res = true;
10169
10170 if (filedata->string_table == NULL)
10171 return false;
10172
10173 memset (& aux, 0, sizeof (aux));
10174
10175 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10176 {
10177 if (sec->sh_type == SHT_SYMTAB)
10178 {
10179 if (aux.symtab)
10180 {
10181 error (_("Multiple symbol tables encountered\n"));
10182 free (aux.symtab);
10183 aux.symtab = NULL;
10184 free (aux.strtab);
10185 aux.strtab = NULL;
10186 }
10187 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
10188 &aux.strtab, &aux.strtab_size))
10189 return false;
10190 }
10191 else if (section_name_valid (filedata, sec)
10192 && streq (section_name (filedata, sec), ".PARISC.unwind"))
10193 unwsec = sec;
10194 }
10195
10196 if (!unwsec)
10197 printf (_("\nThere are no unwind sections in this file.\n"));
10198
10199 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10200 {
10201 if (section_name_valid (filedata, sec)
10202 && streq (section_name (filedata, sec), ".PARISC.unwind"))
10203 {
10204 uint64_t num_unwind = sec->sh_size / 16;
10205
10206 printf (ngettext ("\nUnwind section '%s' at offset %#" PRIx64 " "
10207 "contains %" PRIu64 " entry:\n",
10208 "\nUnwind section '%s' at offset %#" PRIx64 " "
10209 "contains %" PRIu64 " entries:\n",
10210 num_unwind),
10211 printable_section_name (filedata, sec),
10212 sec->sh_offset,
10213 num_unwind);
10214
10215 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
10216 res = false;
10217
10218 if (res && aux.table_len > 0)
10219 {
10220 if (! dump_hppa_unwind (filedata, &aux))
10221 res = false;
10222 }
10223
10224 free ((char *) aux.table);
10225 aux.table = NULL;
10226 }
10227 }
10228
10229 free (aux.symtab);
10230 free ((char *) aux.strtab);
10231
10232 return res;
10233}
10234
10235struct arm_section
10236{
10237 unsigned char * data; /* The unwind data. */
10238 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
10239 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
10240 uint64_t nrelas; /* The number of relocations. */
10241 unsigned int rel_type; /* REL or RELA ? */
10242 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
10243};
10244
10245struct arm_unw_aux_info
10246{
10247 Filedata * filedata; /* The file containing the unwind sections. */
10248 Elf_Internal_Sym * symtab; /* The file's symbol table. */
10249 uint64_t nsyms; /* Number of symbols. */
10250 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
10251 uint64_t nfuns; /* Number of these symbols. */
10252 char * strtab; /* The file's string table. */
10253 uint64_t strtab_size; /* Size of string table. */
10254};
10255
10256static const char *
10257arm_print_vma_and_name (Filedata * filedata,
10258 struct arm_unw_aux_info * aux,
10259 uint64_t fn,
10260 struct absaddr addr)
10261{
10262 const char *procname;
10263 uint64_t sym_offset;
10264
10265 if (addr.section == SHN_UNDEF)
10266 addr.offset = fn;
10267
10268 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
10269 aux->strtab_size, addr, &procname,
10270 &sym_offset);
10271
10272 print_vma (fn, PREFIX_HEX);
10273
10274 if (procname)
10275 {
10276 fputs (" <", stdout);
10277 fputs (procname, stdout);
10278
10279 if (sym_offset)
10280 printf ("+0x%" PRIx64, sym_offset);
10281 fputc ('>', stdout);
10282 }
10283
10284 return procname;
10285}
10286
10287static void
10288arm_free_section (struct arm_section *arm_sec)
10289{
10290 free (arm_sec->data);
10291 free (arm_sec->rela);
10292}
10293
10294/* 1) If SEC does not match the one cached in ARM_SEC, then free the current
10295 cached section and install SEC instead.
10296 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
10297 and return its valued in * WORDP, relocating if necessary.
10298 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
10299 relocation's offset in ADDR.
10300 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
10301 into the string table of the symbol associated with the reloc. If no
10302 reloc was applied store -1 there.
10303 5) Return TRUE upon success, FALSE otherwise. */
10304
10305static bool
10306get_unwind_section_word (Filedata * filedata,
10307 struct arm_unw_aux_info * aux,
10308 struct arm_section * arm_sec,
10309 Elf_Internal_Shdr * sec,
10310 uint64_t word_offset,
10311 unsigned int * wordp,
10312 struct absaddr * addr,
10313 uint64_t * sym_name)
10314{
10315 Elf_Internal_Rela *rp;
10316 Elf_Internal_Sym *sym;
10317 const char * relname;
10318 unsigned int word;
10319 bool wrapped;
10320
10321 if (sec == NULL || arm_sec == NULL)
10322 return false;
10323
10324 addr->section = SHN_UNDEF;
10325 addr->offset = 0;
10326
10327 if (sym_name != NULL)
10328 *sym_name = (uint64_t) -1;
10329
10330 /* If necessary, update the section cache. */
10331 if (sec != arm_sec->sec)
10332 {
10333 Elf_Internal_Shdr *relsec;
10334
10335 arm_free_section (arm_sec);
10336
10337 arm_sec->sec = sec;
10338 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
10339 sec->sh_size, _("unwind data"));
10340 arm_sec->rela = NULL;
10341 arm_sec->nrelas = 0;
10342
10343 for (relsec = filedata->section_headers;
10344 relsec < filedata->section_headers + filedata->file_header.e_shnum;
10345 ++relsec)
10346 {
10347 if (relsec->sh_info >= filedata->file_header.e_shnum
10348 || filedata->section_headers + relsec->sh_info != sec
10349 /* PR 15745: Check the section type as well. */
10350 || (relsec->sh_type != SHT_REL
10351 && relsec->sh_type != SHT_RELA))
10352 continue;
10353
10354 arm_sec->rel_type = relsec->sh_type;
10355 if (relsec->sh_type == SHT_REL)
10356 {
10357 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
10358 relsec->sh_size,
10359 & arm_sec->rela, & arm_sec->nrelas))
10360 return false;
10361 }
10362 else /* relsec->sh_type == SHT_RELA */
10363 {
10364 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
10365 relsec->sh_size,
10366 & arm_sec->rela, & arm_sec->nrelas))
10367 return false;
10368 }
10369 break;
10370 }
10371
10372 arm_sec->next_rela = arm_sec->rela;
10373 }
10374
10375 /* If there is no unwind data we can do nothing. */
10376 if (arm_sec->data == NULL)
10377 return false;
10378
10379 /* If the offset is invalid then fail. */
10380 if (/* PR 21343 *//* PR 18879 */
10381 sec->sh_size < 4
10382 || word_offset > sec->sh_size - 4)
10383 return false;
10384
10385 /* Get the word at the required offset. */
10386 word = byte_get (arm_sec->data + word_offset, 4);
10387
10388 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
10389 if (arm_sec->rela == NULL)
10390 {
10391 * wordp = word;
10392 return true;
10393 }
10394
10395 /* Look through the relocs to find the one that applies to the provided offset. */
10396 wrapped = false;
10397 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
10398 {
10399 uint64_t prelval, offset;
10400
10401 if (rp->r_offset > word_offset && !wrapped)
10402 {
10403 rp = arm_sec->rela;
10404 wrapped = true;
10405 }
10406 if (rp->r_offset > word_offset)
10407 break;
10408
10409 if (rp->r_offset & 3)
10410 {
10411 warn (_("Skipping unexpected relocation at offset %#" PRIx64 "\n"),
10412 rp->r_offset);
10413 continue;
10414 }
10415
10416 if (rp->r_offset < word_offset)
10417 continue;
10418
10419 /* PR 17531: file: 027-161405-0.004 */
10420 if (aux->symtab == NULL)
10421 continue;
10422
10423 if (arm_sec->rel_type == SHT_REL)
10424 {
10425 offset = word & 0x7fffffff;
10426 if (offset & 0x40000000)
10427 offset |= ~ (uint64_t) 0x7fffffff;
10428 }
10429 else if (arm_sec->rel_type == SHT_RELA)
10430 offset = rp->r_addend;
10431 else
10432 {
10433 error (_("Unknown section relocation type %d encountered\n"),
10434 arm_sec->rel_type);
10435 break;
10436 }
10437
10438 /* PR 17531 file: 027-1241568-0.004. */
10439 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
10440 {
10441 error (_("Bad symbol index in unwind relocation "
10442 "(%" PRIu64 " > %" PRIu64 ")\n"),
10443 ELF32_R_SYM (rp->r_info), aux->nsyms);
10444 break;
10445 }
10446
10447 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
10448 offset += sym->st_value;
10449 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
10450
10451 /* Check that we are processing the expected reloc type. */
10452 if (filedata->file_header.e_machine == EM_ARM)
10453 {
10454 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
10455 if (relname == NULL)
10456 {
10457 warn (_("Skipping unknown ARM relocation type: %d\n"),
10458 (int) ELF32_R_TYPE (rp->r_info));
10459 continue;
10460 }
10461
10462 if (streq (relname, "R_ARM_NONE"))
10463 continue;
10464
10465 if (! streq (relname, "R_ARM_PREL31"))
10466 {
10467 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
10468 continue;
10469 }
10470 }
10471 else if (filedata->file_header.e_machine == EM_TI_C6000)
10472 {
10473 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
10474 if (relname == NULL)
10475 {
10476 warn (_("Skipping unknown C6000 relocation type: %d\n"),
10477 (int) ELF32_R_TYPE (rp->r_info));
10478 continue;
10479 }
10480
10481 if (streq (relname, "R_C6000_NONE"))
10482 continue;
10483
10484 if (! streq (relname, "R_C6000_PREL31"))
10485 {
10486 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
10487 continue;
10488 }
10489
10490 prelval >>= 1;
10491 }
10492 else
10493 {
10494 /* This function currently only supports ARM and TI unwinders. */
10495 warn (_("Only TI and ARM unwinders are currently supported\n"));
10496 break;
10497 }
10498
10499 word = (word & ~ (uint64_t) 0x7fffffff) | (prelval & 0x7fffffff);
10500 addr->section = sym->st_shndx;
10501 addr->offset = offset;
10502
10503 if (sym_name)
10504 * sym_name = sym->st_name;
10505 break;
10506 }
10507
10508 *wordp = word;
10509 arm_sec->next_rela = rp;
10510
10511 return true;
10512}
10513
10514static const char *tic6x_unwind_regnames[16] =
10515{
10516 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
10517 "A14", "A13", "A12", "A11", "A10",
10518 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
10519};
10520
10521static void
10522decode_tic6x_unwind_regmask (unsigned int mask)
10523{
10524 int i;
10525
10526 for (i = 12; mask; mask >>= 1, i--)
10527 {
10528 if (mask & 1)
10529 {
10530 fputs (tic6x_unwind_regnames[i], stdout);
10531 if (mask > 1)
10532 fputs (", ", stdout);
10533 }
10534 }
10535}
10536
10537#define ADVANCE \
10538 if (remaining == 0 && more_words) \
10539 { \
10540 data_offset += 4; \
10541 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
10542 data_offset, & word, & addr, NULL)) \
10543 return false; \
10544 remaining = 4; \
10545 more_words--; \
10546 } \
10547
10548#define GET_OP(OP) \
10549 ADVANCE; \
10550 if (remaining) \
10551 { \
10552 remaining--; \
10553 (OP) = word >> 24; \
10554 word <<= 8; \
10555 } \
10556 else \
10557 { \
10558 printf (_("[Truncated opcode]\n")); \
10559 return false; \
10560 } \
10561 printf ("0x%02x ", OP)
10562
10563static bool
10564decode_arm_unwind_bytecode (Filedata * filedata,
10565 struct arm_unw_aux_info * aux,
10566 unsigned int word,
10567 unsigned int remaining,
10568 unsigned int more_words,
10569 uint64_t data_offset,
10570 Elf_Internal_Shdr * data_sec,
10571 struct arm_section * data_arm_sec)
10572{
10573 struct absaddr addr;
10574 bool res = true;
10575
10576 /* Decode the unwinding instructions. */
10577 while (1)
10578 {
10579 unsigned int op, op2;
10580
10581 ADVANCE;
10582 if (remaining == 0)
10583 break;
10584 remaining--;
10585 op = word >> 24;
10586 word <<= 8;
10587
10588 printf (" 0x%02x ", op);
10589
10590 if ((op & 0xc0) == 0x00)
10591 {
10592 int offset = ((op & 0x3f) << 2) + 4;
10593
10594 printf (" vsp = vsp + %d", offset);
10595 }
10596 else if ((op & 0xc0) == 0x40)
10597 {
10598 int offset = ((op & 0x3f) << 2) + 4;
10599
10600 printf (" vsp = vsp - %d", offset);
10601 }
10602 else if ((op & 0xf0) == 0x80)
10603 {
10604 GET_OP (op2);
10605 if (op == 0x80 && op2 == 0)
10606 printf (_("Refuse to unwind"));
10607 else
10608 {
10609 unsigned int mask = ((op & 0x0f) << 8) | op2;
10610 bool first = true;
10611 int i;
10612
10613 printf ("pop {");
10614 for (i = 0; i < 12; i++)
10615 if (mask & (1 << i))
10616 {
10617 if (first)
10618 first = false;
10619 else
10620 printf (", ");
10621 printf ("r%d", 4 + i);
10622 }
10623 printf ("}");
10624 }
10625 }
10626 else if ((op & 0xf0) == 0x90)
10627 {
10628 if (op == 0x9d || op == 0x9f)
10629 printf (_(" [Reserved]"));
10630 else
10631 printf (" vsp = r%d", op & 0x0f);
10632 }
10633 else if ((op & 0xf0) == 0xa0)
10634 {
10635 int end = 4 + (op & 0x07);
10636 bool first = true;
10637 int i;
10638
10639 printf (" pop {");
10640 for (i = 4; i <= end; i++)
10641 {
10642 if (first)
10643 first = false;
10644 else
10645 printf (", ");
10646 printf ("r%d", i);
10647 }
10648 if (op & 0x08)
10649 {
10650 if (!first)
10651 printf (", ");
10652 printf ("r14");
10653 }
10654 printf ("}");
10655 }
10656 else if (op == 0xb0)
10657 printf (_(" finish"));
10658 else if (op == 0xb1)
10659 {
10660 GET_OP (op2);
10661 if (op2 == 0 || (op2 & 0xf0) != 0)
10662 printf (_("[Spare]"));
10663 else
10664 {
10665 unsigned int mask = op2 & 0x0f;
10666 bool first = true;
10667 int i;
10668
10669 printf ("pop {");
10670 for (i = 0; i < 12; i++)
10671 if (mask & (1 << i))
10672 {
10673 if (first)
10674 first = false;
10675 else
10676 printf (", ");
10677 printf ("r%d", i);
10678 }
10679 printf ("}");
10680 }
10681 }
10682 else if (op == 0xb2)
10683 {
10684 unsigned char buf[9];
10685 unsigned int i, len;
10686 uint64_t offset;
10687
10688 for (i = 0; i < sizeof (buf); i++)
10689 {
10690 GET_OP (buf[i]);
10691 if ((buf[i] & 0x80) == 0)
10692 break;
10693 }
10694 if (i == sizeof (buf))
10695 {
10696 error (_("corrupt change to vsp\n"));
10697 res = false;
10698 }
10699 else
10700 {
10701 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
10702 assert (len == i + 1);
10703 offset = offset * 4 + 0x204;
10704 printf ("vsp = vsp + %" PRId64, offset);
10705 }
10706 }
10707 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
10708 {
10709 unsigned int first, last;
10710
10711 GET_OP (op2);
10712 first = op2 >> 4;
10713 last = op2 & 0x0f;
10714 if (op == 0xc8)
10715 first = first + 16;
10716 printf ("pop {D%d", first);
10717 if (last)
10718 printf ("-D%d", first + last);
10719 printf ("}");
10720 }
10721 else if (op == 0xb4)
10722 printf (_(" pop {ra_auth_code}"));
10723 else if (op == 0xb5)
10724 printf (_(" vsp as modifier for PAC validation"));
10725 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
10726 {
10727 unsigned int count = op & 0x07;
10728
10729 printf ("pop {D8");
10730 if (count)
10731 printf ("-D%d", 8 + count);
10732 printf ("}");
10733 }
10734 else if (op >= 0xc0 && op <= 0xc5)
10735 {
10736 unsigned int count = op & 0x07;
10737
10738 printf (" pop {wR10");
10739 if (count)
10740 printf ("-wR%d", 10 + count);
10741 printf ("}");
10742 }
10743 else if (op == 0xc6)
10744 {
10745 unsigned int first, last;
10746
10747 GET_OP (op2);
10748 first = op2 >> 4;
10749 last = op2 & 0x0f;
10750 printf ("pop {wR%d", first);
10751 if (last)
10752 printf ("-wR%d", first + last);
10753 printf ("}");
10754 }
10755 else if (op == 0xc7)
10756 {
10757 GET_OP (op2);
10758 if (op2 == 0 || (op2 & 0xf0) != 0)
10759 printf (_("[Spare]"));
10760 else
10761 {
10762 unsigned int mask = op2 & 0x0f;
10763 bool first = true;
10764 int i;
10765
10766 printf ("pop {");
10767 for (i = 0; i < 4; i++)
10768 if (mask & (1 << i))
10769 {
10770 if (first)
10771 first = false;
10772 else
10773 printf (", ");
10774 printf ("wCGR%d", i);
10775 }
10776 printf ("}");
10777 }
10778 }
10779 else
10780 {
10781 printf (_(" [unsupported opcode]"));
10782 res = false;
10783 }
10784
10785 printf ("\n");
10786 }
10787
10788 return res;
10789}
10790
10791static bool
10792decode_tic6x_unwind_bytecode (Filedata * filedata,
10793 struct arm_unw_aux_info * aux,
10794 unsigned int word,
10795 unsigned int remaining,
10796 unsigned int more_words,
10797 uint64_t data_offset,
10798 Elf_Internal_Shdr * data_sec,
10799 struct arm_section * data_arm_sec)
10800{
10801 struct absaddr addr;
10802
10803 /* Decode the unwinding instructions. */
10804 while (1)
10805 {
10806 unsigned int op, op2;
10807
10808 ADVANCE;
10809 if (remaining == 0)
10810 break;
10811 remaining--;
10812 op = word >> 24;
10813 word <<= 8;
10814
10815 printf (" 0x%02x ", op);
10816
10817 if ((op & 0xc0) == 0x00)
10818 {
10819 int offset = ((op & 0x3f) << 3) + 8;
10820 printf (" sp = sp + %d", offset);
10821 }
10822 else if ((op & 0xc0) == 0x80)
10823 {
10824 GET_OP (op2);
10825 if (op == 0x80 && op2 == 0)
10826 printf (_("Refuse to unwind"));
10827 else
10828 {
10829 unsigned int mask = ((op & 0x1f) << 8) | op2;
10830 if (op & 0x20)
10831 printf ("pop compact {");
10832 else
10833 printf ("pop {");
10834
10835 decode_tic6x_unwind_regmask (mask);
10836 printf("}");
10837 }
10838 }
10839 else if ((op & 0xf0) == 0xc0)
10840 {
10841 unsigned int reg;
10842 unsigned int nregs;
10843 unsigned int i;
10844 const char *name;
10845 struct
10846 {
10847 unsigned int offset;
10848 unsigned int reg;
10849 } regpos[16];
10850
10851 /* Scan entire instruction first so that GET_OP output is not
10852 interleaved with disassembly. */
10853 nregs = 0;
10854 for (i = 0; nregs < (op & 0xf); i++)
10855 {
10856 GET_OP (op2);
10857 reg = op2 >> 4;
10858 if (reg != 0xf)
10859 {
10860 regpos[nregs].offset = i * 2;
10861 regpos[nregs].reg = reg;
10862 nregs++;
10863 }
10864
10865 reg = op2 & 0xf;
10866 if (reg != 0xf)
10867 {
10868 regpos[nregs].offset = i * 2 + 1;
10869 regpos[nregs].reg = reg;
10870 nregs++;
10871 }
10872 }
10873
10874 printf (_("pop frame {"));
10875 if (nregs == 0)
10876 {
10877 printf (_("*corrupt* - no registers specified"));
10878 }
10879 else
10880 {
10881 reg = nregs - 1;
10882 for (i = i * 2; i > 0; i--)
10883 {
10884 if (regpos[reg].offset == i - 1)
10885 {
10886 name = tic6x_unwind_regnames[regpos[reg].reg];
10887 if (reg > 0)
10888 reg--;
10889 }
10890 else
10891 name = _("[pad]");
10892
10893 fputs (name, stdout);
10894 if (i > 1)
10895 printf (", ");
10896 }
10897 }
10898
10899 printf ("}");
10900 }
10901 else if (op == 0xd0)
10902 printf (" MOV FP, SP");
10903 else if (op == 0xd1)
10904 printf (" __c6xabi_pop_rts");
10905 else if (op == 0xd2)
10906 {
10907 unsigned char buf[9];
10908 unsigned int i, len;
10909 uint64_t offset;
10910
10911 for (i = 0; i < sizeof (buf); i++)
10912 {
10913 GET_OP (buf[i]);
10914 if ((buf[i] & 0x80) == 0)
10915 break;
10916 }
10917 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
10918 if (i == sizeof (buf))
10919 {
10920 warn (_("Corrupt stack pointer adjustment detected\n"));
10921 return false;
10922 }
10923
10924 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
10925 assert (len == i + 1);
10926 offset = offset * 8 + 0x408;
10927 printf (_("sp = sp + %" PRId64), offset);
10928 }
10929 else if ((op & 0xf0) == 0xe0)
10930 {
10931 if ((op & 0x0f) == 7)
10932 printf (" RETURN");
10933 else
10934 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
10935 }
10936 else
10937 {
10938 printf (_(" [unsupported opcode]"));
10939 }
10940 putchar ('\n');
10941 }
10942
10943 return true;
10944}
10945
10946static uint64_t
10947arm_expand_prel31 (Filedata * filedata, uint64_t word, uint64_t where)
10948{
10949 uint64_t offset;
10950
10951 offset = word & 0x7fffffff;
10952 if (offset & 0x40000000)
10953 offset |= ~ (uint64_t) 0x7fffffff;
10954
10955 if (filedata->file_header.e_machine == EM_TI_C6000)
10956 offset <<= 1;
10957
10958 return offset + where;
10959}
10960
10961static bool
10962decode_arm_unwind (Filedata * filedata,
10963 struct arm_unw_aux_info * aux,
10964 unsigned int word,
10965 unsigned int remaining,
10966 uint64_t data_offset,
10967 Elf_Internal_Shdr * data_sec,
10968 struct arm_section * data_arm_sec)
10969{
10970 int per_index;
10971 unsigned int more_words = 0;
10972 struct absaddr addr;
10973 uint64_t sym_name = (uint64_t) -1;
10974 bool res = true;
10975
10976 if (remaining == 0)
10977 {
10978 /* Fetch the first word.
10979 Note - when decoding an object file the address extracted
10980 here will always be 0. So we also pass in the sym_name
10981 parameter so that we can find the symbol associated with
10982 the personality routine. */
10983 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
10984 & word, & addr, & sym_name))
10985 return false;
10986
10987 remaining = 4;
10988 }
10989 else
10990 {
10991 addr.section = SHN_UNDEF;
10992 addr.offset = 0;
10993 }
10994
10995 if ((word & 0x80000000) == 0)
10996 {
10997 /* Expand prel31 for personality routine. */
10998 uint64_t fn;
10999 const char *procname;
11000
11001 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
11002 printf (_(" Personality routine: "));
11003 if (fn == 0
11004 && addr.section == SHN_UNDEF && addr.offset == 0
11005 && sym_name != (uint64_t) -1 && sym_name < aux->strtab_size)
11006 {
11007 procname = aux->strtab + sym_name;
11008 print_vma (fn, PREFIX_HEX);
11009 if (procname)
11010 {
11011 fputs (" <", stdout);
11012 fputs (procname, stdout);
11013 fputc ('>', stdout);
11014 }
11015 }
11016 else
11017 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
11018 fputc ('\n', stdout);
11019
11020 /* The GCC personality routines use the standard compact
11021 encoding, starting with one byte giving the number of
11022 words. */
11023 if (procname != NULL
11024 && (startswith (procname, "__gcc_personality_v0")
11025 || startswith (procname, "__gxx_personality_v0")
11026 || startswith (procname, "__gcj_personality_v0")
11027 || startswith (procname, "__gnu_objc_personality_v0")))
11028 {
11029 remaining = 0;
11030 more_words = 1;
11031 ADVANCE;
11032 if (!remaining)
11033 {
11034 printf (_(" [Truncated data]\n"));
11035 return false;
11036 }
11037 more_words = word >> 24;
11038 word <<= 8;
11039 remaining--;
11040 per_index = -1;
11041 }
11042 else
11043 return true;
11044 }
11045 else
11046 {
11047 /* ARM EHABI Section 6.3:
11048
11049 An exception-handling table entry for the compact model looks like:
11050
11051 31 30-28 27-24 23-0
11052 -- ----- ----- ----
11053 1 0 index Data for personalityRoutine[index] */
11054
11055 if (filedata->file_header.e_machine == EM_ARM
11056 && (word & 0x70000000))
11057 {
11058 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
11059 res = false;
11060 }
11061
11062 per_index = (word >> 24) & 0x7f;
11063 printf (_(" Compact model index: %d\n"), per_index);
11064 if (per_index == 0)
11065 {
11066 more_words = 0;
11067 word <<= 8;
11068 remaining--;
11069 }
11070 else if (per_index < 3)
11071 {
11072 more_words = (word >> 16) & 0xff;
11073 word <<= 16;
11074 remaining -= 2;
11075 }
11076 }
11077
11078 switch (filedata->file_header.e_machine)
11079 {
11080 case EM_ARM:
11081 if (per_index < 3)
11082 {
11083 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
11084 data_offset, data_sec, data_arm_sec))
11085 res = false;
11086 }
11087 else
11088 {
11089 warn (_("Unknown ARM compact model index encountered\n"));
11090 printf (_(" [reserved]\n"));
11091 res = false;
11092 }
11093 break;
11094
11095 case EM_TI_C6000:
11096 if (per_index < 3)
11097 {
11098 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
11099 data_offset, data_sec, data_arm_sec))
11100 res = false;
11101 }
11102 else if (per_index < 5)
11103 {
11104 if (((word >> 17) & 0x7f) == 0x7f)
11105 printf (_(" Restore stack from frame pointer\n"));
11106 else
11107 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
11108 printf (_(" Registers restored: "));
11109 if (per_index == 4)
11110 printf (" (compact) ");
11111 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
11112 putchar ('\n');
11113 printf (_(" Return register: %s\n"),
11114 tic6x_unwind_regnames[word & 0xf]);
11115 }
11116 else
11117 printf (_(" [reserved (%d)]\n"), per_index);
11118 break;
11119
11120 default:
11121 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
11122 filedata->file_header.e_machine);
11123 res = false;
11124 }
11125
11126 /* Decode the descriptors. Not implemented. */
11127
11128 return res;
11129}
11130
11131static bool
11132dump_arm_unwind (Filedata * filedata,
11133 struct arm_unw_aux_info * aux,
11134 Elf_Internal_Shdr * exidx_sec)
11135{
11136 struct arm_section exidx_arm_sec, extab_arm_sec;
11137 unsigned int i, exidx_len;
11138 uint64_t j, nfuns;
11139 bool res = true;
11140
11141 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
11142 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
11143 exidx_len = exidx_sec->sh_size / 8;
11144
11145 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
11146 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
11147 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
11148 aux->funtab[nfuns++] = aux->symtab[j];
11149 aux->nfuns = nfuns;
11150 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
11151
11152 for (i = 0; i < exidx_len; i++)
11153 {
11154 unsigned int exidx_fn, exidx_entry;
11155 struct absaddr fn_addr, entry_addr;
11156 uint64_t fn;
11157
11158 fputc ('\n', stdout);
11159
11160 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
11161 8 * i, & exidx_fn, & fn_addr, NULL)
11162 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
11163 8 * i + 4, & exidx_entry, & entry_addr, NULL))
11164 {
11165 free (aux->funtab);
11166 arm_free_section (& exidx_arm_sec);
11167 arm_free_section (& extab_arm_sec);
11168 return false;
11169 }
11170
11171 /* ARM EHABI, Section 5:
11172 An index table entry consists of 2 words.
11173 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
11174 if (exidx_fn & 0x80000000)
11175 {
11176 warn (_("corrupt index table entry: %x\n"), exidx_fn);
11177 res = false;
11178 }
11179
11180 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
11181
11182 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
11183 fputs (": ", stdout);
11184
11185 if (exidx_entry == 1)
11186 {
11187 print_vma (exidx_entry, PREFIX_HEX);
11188 fputs (" [cantunwind]\n", stdout);
11189 }
11190 else if (exidx_entry & 0x80000000)
11191 {
11192 print_vma (exidx_entry, PREFIX_HEX);
11193 fputc ('\n', stdout);
11194 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
11195 }
11196 else
11197 {
11198 uint64_t table, table_offset = 0;
11199 Elf_Internal_Shdr *table_sec;
11200
11201 fputs ("@", stdout);
11202 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
11203 print_vma (table, PREFIX_HEX);
11204 printf ("\n");
11205
11206 /* Locate the matching .ARM.extab. */
11207 if (entry_addr.section != SHN_UNDEF
11208 && entry_addr.section < filedata->file_header.e_shnum)
11209 {
11210 table_sec = filedata->section_headers + entry_addr.section;
11211 table_offset = entry_addr.offset;
11212 /* PR 18879 */
11213 if (table_offset > table_sec->sh_size)
11214 {
11215 warn (_("Unwind entry contains corrupt offset (%#" PRIx64 ") into section %s\n"),
11216 table_offset,
11217 printable_section_name (filedata, table_sec));
11218 res = false;
11219 continue;
11220 }
11221 }
11222 else
11223 {
11224 table_sec = find_section_by_address (filedata, table);
11225 if (table_sec != NULL)
11226 table_offset = table - table_sec->sh_addr;
11227 }
11228
11229 if (table_sec == NULL)
11230 {
11231 warn (_("Could not locate .ARM.extab section containing %#" PRIx64 ".\n"),
11232 table);
11233 res = false;
11234 continue;
11235 }
11236
11237 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
11238 &extab_arm_sec))
11239 res = false;
11240 }
11241 }
11242
11243 printf ("\n");
11244
11245 free (aux->funtab);
11246 arm_free_section (&exidx_arm_sec);
11247 arm_free_section (&extab_arm_sec);
11248
11249 return res;
11250}
11251
11252/* Used for both ARM and C6X unwinding tables. */
11253
11254static bool
11255arm_process_unwind (Filedata * filedata)
11256{
11257 struct arm_unw_aux_info aux;
11258 Elf_Internal_Shdr *unwsec = NULL;
11259 Elf_Internal_Shdr *sec;
11260 size_t i;
11261 unsigned int sec_type;
11262 bool res = true;
11263
11264 switch (filedata->file_header.e_machine)
11265 {
11266 case EM_ARM:
11267 sec_type = SHT_ARM_EXIDX;
11268 break;
11269
11270 case EM_TI_C6000:
11271 sec_type = SHT_C6000_UNWIND;
11272 break;
11273
11274 default:
11275 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
11276 filedata->file_header.e_machine);
11277 return false;
11278 }
11279
11280 if (filedata->string_table == NULL)
11281 return false;
11282
11283 memset (& aux, 0, sizeof (aux));
11284 aux.filedata = filedata;
11285
11286 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
11287 {
11288 if (sec->sh_type == SHT_SYMTAB)
11289 {
11290 if (aux.symtab)
11291 {
11292 error (_("Multiple symbol tables encountered\n"));
11293 free (aux.symtab);
11294 aux.symtab = NULL;
11295 free (aux.strtab);
11296 aux.strtab = NULL;
11297 }
11298 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
11299 &aux.strtab, &aux.strtab_size))
11300 return false;
11301 }
11302 else if (sec->sh_type == sec_type)
11303 unwsec = sec;
11304 }
11305
11306 if (unwsec == NULL)
11307 printf (_("\nThere are no unwind sections in this file.\n"));
11308 else
11309 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
11310 {
11311 if (sec->sh_type == sec_type)
11312 {
11313 uint64_t num_unwind = sec->sh_size / (2 * eh_addr_size);
11314 printf (ngettext ("\nUnwind section '%s' at offset %#" PRIx64 " "
11315 "contains %" PRIu64 " entry:\n",
11316 "\nUnwind section '%s' at offset %#" PRIx64 " "
11317 "contains %" PRIu64 " entries:\n",
11318 num_unwind),
11319 printable_section_name (filedata, sec),
11320 sec->sh_offset,
11321 num_unwind);
11322
11323 if (! dump_arm_unwind (filedata, &aux, sec))
11324 res = false;
11325 }
11326 }
11327
11328 free (aux.symtab);
11329 free ((char *) aux.strtab);
11330
11331 return res;
11332}
11333
11334static bool
11335no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
11336{
11337 printf (_("No processor specific unwind information to decode\n"));
11338 return true;
11339}
11340
11341static bool
11342process_unwind (Filedata * filedata)
11343{
11344 struct unwind_handler
11345 {
11346 unsigned int machtype;
11347 bool (* handler)(Filedata *);
11348 } handlers[] =
11349 {
11350 { EM_ARM, arm_process_unwind },
11351 { EM_IA_64, ia64_process_unwind },
11352 { EM_PARISC, hppa_process_unwind },
11353 { EM_TI_C6000, arm_process_unwind },
11354 { EM_386, no_processor_specific_unwind },
11355 { EM_X86_64, no_processor_specific_unwind },
11356 { 0, NULL }
11357 };
11358 int i;
11359
11360 if (!do_unwind)
11361 return true;
11362
11363 for (i = 0; handlers[i].handler != NULL; i++)
11364 if (filedata->file_header.e_machine == handlers[i].machtype)
11365 return handlers[i].handler (filedata);
11366
11367 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
11368 get_machine_name (filedata->file_header.e_machine));
11369 return true;
11370}
11371
11372static void
11373dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
11374{
11375 switch (entry->d_tag)
11376 {
11377 case DT_AARCH64_BTI_PLT:
11378 case DT_AARCH64_PAC_PLT:
11379 break;
11380 default:
11381 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11382 break;
11383 }
11384 putchar ('\n');
11385}
11386
11387static void
11388dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
11389{
11390 switch (entry->d_tag)
11391 {
11392 case DT_MIPS_FLAGS:
11393 if (entry->d_un.d_val == 0)
11394 printf (_("NONE"));
11395 else
11396 {
11397 static const char * opts[] =
11398 {
11399 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
11400 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
11401 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
11402 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
11403 "RLD_ORDER_SAFE"
11404 };
11405 unsigned int cnt;
11406 bool first = true;
11407
11408 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
11409 if (entry->d_un.d_val & (1 << cnt))
11410 {
11411 printf ("%s%s", first ? "" : " ", opts[cnt]);
11412 first = false;
11413 }
11414 }
11415 break;
11416
11417 case DT_MIPS_IVERSION:
11418 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11419 printf (_("Interface Version: %s"),
11420 get_dynamic_name (filedata, entry->d_un.d_val));
11421 else
11422 printf (_("Interface Version: <corrupt: %" PRIx64 ">"),
11423 entry->d_un.d_ptr);
11424 break;
11425
11426 case DT_MIPS_TIME_STAMP:
11427 {
11428 char timebuf[128];
11429 struct tm * tmp;
11430 time_t atime = entry->d_un.d_val;
11431
11432 tmp = gmtime (&atime);
11433 /* PR 17531: file: 6accc532. */
11434 if (tmp == NULL)
11435 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
11436 else
11437 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
11438 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11439 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
11440 printf (_("Time Stamp: %s"), timebuf);
11441 }
11442 break;
11443
11444 case DT_MIPS_RLD_VERSION:
11445 case DT_MIPS_LOCAL_GOTNO:
11446 case DT_MIPS_CONFLICTNO:
11447 case DT_MIPS_LIBLISTNO:
11448 case DT_MIPS_SYMTABNO:
11449 case DT_MIPS_UNREFEXTNO:
11450 case DT_MIPS_HIPAGENO:
11451 case DT_MIPS_DELTA_CLASS_NO:
11452 case DT_MIPS_DELTA_INSTANCE_NO:
11453 case DT_MIPS_DELTA_RELOC_NO:
11454 case DT_MIPS_DELTA_SYM_NO:
11455 case DT_MIPS_DELTA_CLASSSYM_NO:
11456 case DT_MIPS_COMPACT_SIZE:
11457 print_vma (entry->d_un.d_val, DEC);
11458 break;
11459
11460 case DT_MIPS_XHASH:
11461 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
11462 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11463 /* Falls through. */
11464
11465 default:
11466 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11467 }
11468 putchar ('\n');
11469}
11470
11471static void
11472dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
11473{
11474 switch (entry->d_tag)
11475 {
11476 case DT_HP_DLD_FLAGS:
11477 {
11478 static struct
11479 {
11480 unsigned int bit;
11481 const char * str;
11482 }
11483 flags[] =
11484 {
11485 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
11486 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
11487 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
11488 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
11489 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
11490 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
11491 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
11492 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
11493 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
11494 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
11495 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
11496 { DT_HP_GST, "HP_GST" },
11497 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
11498 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
11499 { DT_HP_NODELETE, "HP_NODELETE" },
11500 { DT_HP_GROUP, "HP_GROUP" },
11501 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
11502 };
11503 bool first = true;
11504 size_t cnt;
11505 uint64_t val = entry->d_un.d_val;
11506
11507 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
11508 if (val & flags[cnt].bit)
11509 {
11510 if (! first)
11511 putchar (' ');
11512 fputs (flags[cnt].str, stdout);
11513 first = false;
11514 val ^= flags[cnt].bit;
11515 }
11516
11517 if (val != 0 || first)
11518 {
11519 if (! first)
11520 putchar (' ');
11521 print_vma (val, HEX);
11522 }
11523 }
11524 break;
11525
11526 default:
11527 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11528 break;
11529 }
11530 putchar ('\n');
11531}
11532
11533/* VMS vs Unix time offset and factor. */
11534
11535#define VMS_EPOCH_OFFSET 35067168000000000LL
11536#define VMS_GRANULARITY_FACTOR 10000000
11537#ifndef INT64_MIN
11538#define INT64_MIN (-9223372036854775807LL - 1)
11539#endif
11540
11541/* Display a VMS time in a human readable format. */
11542
11543static void
11544print_vms_time (int64_t vmstime)
11545{
11546 struct tm *tm = NULL;
11547 time_t unxtime;
11548
11549 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
11550 {
11551 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
11552 unxtime = vmstime;
11553 if (unxtime == vmstime)
11554 tm = gmtime (&unxtime);
11555 }
11556 if (tm != NULL)
11557 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
11558 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
11559 tm->tm_hour, tm->tm_min, tm->tm_sec);
11560}
11561
11562static void
11563dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
11564{
11565 switch (entry->d_tag)
11566 {
11567 case DT_IA_64_PLT_RESERVE:
11568 /* First 3 slots reserved. */
11569 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11570 printf (" -- ");
11571 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
11572 break;
11573
11574 case DT_IA_64_VMS_LINKTIME:
11575 print_vms_time (entry->d_un.d_val);
11576 break;
11577
11578 case DT_IA_64_VMS_LNKFLAGS:
11579 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11580 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
11581 printf (" CALL_DEBUG");
11582 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
11583 printf (" NOP0BUFS");
11584 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
11585 printf (" P0IMAGE");
11586 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
11587 printf (" MKTHREADS");
11588 if (entry->d_un.d_val & VMS_LF_UPCALLS)
11589 printf (" UPCALLS");
11590 if (entry->d_un.d_val & VMS_LF_IMGSTA)
11591 printf (" IMGSTA");
11592 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
11593 printf (" INITIALIZE");
11594 if (entry->d_un.d_val & VMS_LF_MAIN)
11595 printf (" MAIN");
11596 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
11597 printf (" EXE_INIT");
11598 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
11599 printf (" TBK_IN_IMG");
11600 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
11601 printf (" DBG_IN_IMG");
11602 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
11603 printf (" TBK_IN_DSF");
11604 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
11605 printf (" DBG_IN_DSF");
11606 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
11607 printf (" SIGNATURES");
11608 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
11609 printf (" REL_SEG_OFF");
11610 break;
11611
11612 default:
11613 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11614 break;
11615 }
11616 putchar ('\n');
11617}
11618
11619static bool
11620get_32bit_dynamic_section (Filedata * filedata)
11621{
11622 Elf32_External_Dyn * edyn;
11623 Elf32_External_Dyn * ext;
11624 Elf_Internal_Dyn * entry;
11625
11626 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
11627 filedata->dynamic_addr, 1,
11628 filedata->dynamic_size,
11629 _("dynamic section"));
11630 if (!edyn)
11631 return false;
11632
11633 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
11634 might not have the luxury of section headers. Look for the DT_NULL
11635 terminator to determine the number of entries. */
11636 for (ext = edyn, filedata->dynamic_nent = 0;
11637 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
11638 ext++)
11639 {
11640 filedata->dynamic_nent++;
11641 if (BYTE_GET (ext->d_tag) == DT_NULL)
11642 break;
11643 }
11644
11645 filedata->dynamic_section
11646 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
11647 if (filedata->dynamic_section == NULL)
11648 {
11649 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
11650 filedata->dynamic_nent);
11651 free (edyn);
11652 return false;
11653 }
11654
11655 for (ext = edyn, entry = filedata->dynamic_section;
11656 entry < filedata->dynamic_section + filedata->dynamic_nent;
11657 ext++, entry++)
11658 {
11659 entry->d_tag = BYTE_GET (ext->d_tag);
11660 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
11661 }
11662
11663 free (edyn);
11664
11665 return true;
11666}
11667
11668static bool
11669get_64bit_dynamic_section (Filedata * filedata)
11670{
11671 Elf64_External_Dyn * edyn;
11672 Elf64_External_Dyn * ext;
11673 Elf_Internal_Dyn * entry;
11674
11675 /* Read in the data. */
11676 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
11677 filedata->dynamic_addr, 1,
11678 filedata->dynamic_size,
11679 _("dynamic section"));
11680 if (!edyn)
11681 return false;
11682
11683 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
11684 might not have the luxury of section headers. Look for the DT_NULL
11685 terminator to determine the number of entries. */
11686 for (ext = edyn, filedata->dynamic_nent = 0;
11687 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
11688 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
11689 ext++)
11690 {
11691 filedata->dynamic_nent++;
11692 if (BYTE_GET (ext->d_tag) == DT_NULL)
11693 break;
11694 }
11695
11696 filedata->dynamic_section
11697 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
11698 if (filedata->dynamic_section == NULL)
11699 {
11700 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
11701 filedata->dynamic_nent);
11702 free (edyn);
11703 return false;
11704 }
11705
11706 /* Convert from external to internal formats. */
11707 for (ext = edyn, entry = filedata->dynamic_section;
11708 entry < filedata->dynamic_section + filedata->dynamic_nent;
11709 ext++, entry++)
11710 {
11711 entry->d_tag = BYTE_GET (ext->d_tag);
11712 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
11713 }
11714
11715 free (edyn);
11716
11717 return true;
11718}
11719
11720static bool
11721get_dynamic_section (Filedata *filedata)
11722{
11723 if (filedata->dynamic_section)
11724 return true;
11725
11726 if (is_32bit_elf)
11727 return get_32bit_dynamic_section (filedata);
11728 else
11729 return get_64bit_dynamic_section (filedata);
11730}
11731
11732static void
11733print_dynamic_flags (uint64_t flags)
11734{
11735 bool first = true;
11736
11737 while (flags)
11738 {
11739 uint64_t flag;
11740
11741 flag = flags & - flags;
11742 flags &= ~ flag;
11743
11744 if (first)
11745 first = false;
11746 else
11747 putc (' ', stdout);
11748
11749 switch (flag)
11750 {
11751 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
11752 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
11753 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
11754 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
11755 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
11756 default: fputs (_("unknown"), stdout); break;
11757 }
11758 }
11759 puts ("");
11760}
11761
11762static uint64_t *
11763get_dynamic_data (Filedata * filedata, uint64_t number, unsigned int ent_size)
11764{
11765 unsigned char * e_data;
11766 uint64_t * i_data;
11767
11768 /* If size_t is smaller than uint64_t, eg because you are building
11769 on a 32-bit host, then make sure that when number is cast to
11770 size_t no information is lost. */
11771 if ((size_t) number != number
11772 || ent_size * number / ent_size != number)
11773 {
11774 error (_("Size overflow prevents reading %" PRIu64
11775 " elements of size %u\n"),
11776 number, ent_size);
11777 return NULL;
11778 }
11779
11780 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
11781 attempting to allocate memory when the read is bound to fail. */
11782 if (ent_size * number > filedata->file_size)
11783 {
11784 error (_("Invalid number of dynamic entries: %" PRIu64 "\n"),
11785 number);
11786 return NULL;
11787 }
11788
11789 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
11790 if (e_data == NULL)
11791 {
11792 error (_("Out of memory reading %" PRIu64 " dynamic entries\n"),
11793 number);
11794 return NULL;
11795 }
11796
11797 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
11798 {
11799 error (_("Unable to read in %" PRIu64 " bytes of dynamic data\n"),
11800 number * ent_size);
11801 free (e_data);
11802 return NULL;
11803 }
11804
11805 i_data = (uint64_t *) cmalloc ((size_t) number, sizeof (*i_data));
11806 if (i_data == NULL)
11807 {
11808 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
11809 number);
11810 free (e_data);
11811 return NULL;
11812 }
11813
11814 while (number--)
11815 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
11816
11817 free (e_data);
11818
11819 return i_data;
11820}
11821
11822static uint64_t
11823get_num_dynamic_syms (Filedata * filedata)
11824{
11825 uint64_t num_of_syms = 0;
11826
11827 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
11828 return num_of_syms;
11829
11830 if (filedata->dynamic_info[DT_HASH])
11831 {
11832 unsigned char nb[8];
11833 unsigned char nc[8];
11834 unsigned int hash_ent_size = 4;
11835
11836 if ((filedata->file_header.e_machine == EM_ALPHA
11837 || filedata->file_header.e_machine == EM_S390
11838 || filedata->file_header.e_machine == EM_S390_OLD)
11839 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
11840 hash_ent_size = 8;
11841
11842 if (fseek64 (filedata->handle,
11843 (filedata->archive_file_offset
11844 + offset_from_vma (filedata,
11845 filedata->dynamic_info[DT_HASH],
11846 sizeof nb + sizeof nc)),
11847 SEEK_SET))
11848 {
11849 error (_("Unable to seek to start of dynamic information\n"));
11850 goto no_hash;
11851 }
11852
11853 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
11854 {
11855 error (_("Failed to read in number of buckets\n"));
11856 goto no_hash;
11857 }
11858
11859 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
11860 {
11861 error (_("Failed to read in number of chains\n"));
11862 goto no_hash;
11863 }
11864
11865 filedata->nbuckets = byte_get (nb, hash_ent_size);
11866 filedata->nchains = byte_get (nc, hash_ent_size);
11867
11868 if (filedata->nbuckets != 0 && filedata->nchains != 0)
11869 {
11870 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
11871 hash_ent_size);
11872 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
11873 hash_ent_size);
11874
11875 if (filedata->buckets != NULL && filedata->chains != NULL)
11876 num_of_syms = filedata->nchains;
11877 }
11878 no_hash:
11879 if (num_of_syms == 0)
11880 {
11881 free (filedata->buckets);
11882 filedata->buckets = NULL;
11883 free (filedata->chains);
11884 filedata->chains = NULL;
11885 filedata->nbuckets = 0;
11886 }
11887 }
11888
11889 if (filedata->dynamic_info_DT_GNU_HASH)
11890 {
11891 unsigned char nb[16];
11892 uint64_t i, maxchain = 0xffffffff, bitmaskwords;
11893 uint64_t buckets_vma;
11894 uint64_t hn;
11895
11896 if (fseek64 (filedata->handle,
11897 (filedata->archive_file_offset
11898 + offset_from_vma (filedata,
11899 filedata->dynamic_info_DT_GNU_HASH,
11900 sizeof nb)),
11901 SEEK_SET))
11902 {
11903 error (_("Unable to seek to start of dynamic information\n"));
11904 goto no_gnu_hash;
11905 }
11906
11907 if (fread (nb, 16, 1, filedata->handle) != 1)
11908 {
11909 error (_("Failed to read in number of buckets\n"));
11910 goto no_gnu_hash;
11911 }
11912
11913 filedata->ngnubuckets = byte_get (nb, 4);
11914 filedata->gnusymidx = byte_get (nb + 4, 4);
11915 bitmaskwords = byte_get (nb + 8, 4);
11916 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
11917 if (is_32bit_elf)
11918 buckets_vma += bitmaskwords * 4;
11919 else
11920 buckets_vma += bitmaskwords * 8;
11921
11922 if (fseek64 (filedata->handle,
11923 (filedata->archive_file_offset
11924 + offset_from_vma (filedata, buckets_vma, 4)),
11925 SEEK_SET))
11926 {
11927 error (_("Unable to seek to start of dynamic information\n"));
11928 goto no_gnu_hash;
11929 }
11930
11931 filedata->gnubuckets
11932 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
11933
11934 if (filedata->gnubuckets == NULL)
11935 goto no_gnu_hash;
11936
11937 for (i = 0; i < filedata->ngnubuckets; i++)
11938 if (filedata->gnubuckets[i] != 0)
11939 {
11940 if (filedata->gnubuckets[i] < filedata->gnusymidx)
11941 goto no_gnu_hash;
11942
11943 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
11944 maxchain = filedata->gnubuckets[i];
11945 }
11946
11947 if (maxchain == 0xffffffff)
11948 goto no_gnu_hash;
11949
11950 maxchain -= filedata->gnusymidx;
11951
11952 if (fseek64 (filedata->handle,
11953 (filedata->archive_file_offset
11954 + offset_from_vma (filedata,
11955 buckets_vma + 4 * (filedata->ngnubuckets
11956 + maxchain),
11957 4)),
11958 SEEK_SET))
11959 {
11960 error (_("Unable to seek to start of dynamic information\n"));
11961 goto no_gnu_hash;
11962 }
11963
11964 do
11965 {
11966 if (fread (nb, 4, 1, filedata->handle) != 1)
11967 {
11968 error (_("Failed to determine last chain length\n"));
11969 goto no_gnu_hash;
11970 }
11971
11972 if (maxchain + 1 == 0)
11973 goto no_gnu_hash;
11974
11975 ++maxchain;
11976 }
11977 while ((byte_get (nb, 4) & 1) == 0);
11978
11979 if (fseek64 (filedata->handle,
11980 (filedata->archive_file_offset
11981 + offset_from_vma (filedata, (buckets_vma
11982 + 4 * filedata->ngnubuckets),
11983 4)),
11984 SEEK_SET))
11985 {
11986 error (_("Unable to seek to start of dynamic information\n"));
11987 goto no_gnu_hash;
11988 }
11989
11990 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
11991 filedata->ngnuchains = maxchain;
11992
11993 if (filedata->gnuchains == NULL)
11994 goto no_gnu_hash;
11995
11996 if (filedata->dynamic_info_DT_MIPS_XHASH)
11997 {
11998 if (fseek64 (filedata->handle,
11999 (filedata->archive_file_offset
12000 + offset_from_vma (filedata, (buckets_vma
12001 + 4 * (filedata->ngnubuckets
12002 + maxchain)), 4)),
12003 SEEK_SET))
12004 {
12005 error (_("Unable to seek to start of dynamic information\n"));
12006 goto no_gnu_hash;
12007 }
12008
12009 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
12010 if (filedata->mipsxlat == NULL)
12011 goto no_gnu_hash;
12012 }
12013
12014 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
12015 if (filedata->gnubuckets[hn] != 0)
12016 {
12017 uint64_t si = filedata->gnubuckets[hn];
12018 uint64_t off = si - filedata->gnusymidx;
12019
12020 do
12021 {
12022 if (filedata->dynamic_info_DT_MIPS_XHASH)
12023 {
12024 if (off < filedata->ngnuchains
12025 && filedata->mipsxlat[off] >= num_of_syms)
12026 num_of_syms = filedata->mipsxlat[off] + 1;
12027 }
12028 else
12029 {
12030 if (si >= num_of_syms)
12031 num_of_syms = si + 1;
12032 }
12033 si++;
12034 }
12035 while (off < filedata->ngnuchains
12036 && (filedata->gnuchains[off++] & 1) == 0);
12037 }
12038
12039 if (num_of_syms == 0)
12040 {
12041 no_gnu_hash:
12042 free (filedata->mipsxlat);
12043 filedata->mipsxlat = NULL;
12044 free (filedata->gnuchains);
12045 filedata->gnuchains = NULL;
12046 free (filedata->gnubuckets);
12047 filedata->gnubuckets = NULL;
12048 filedata->ngnubuckets = 0;
12049 filedata->ngnuchains = 0;
12050 }
12051 }
12052
12053 return num_of_syms;
12054}
12055
12056/* Parse and display the contents of the dynamic section. */
12057
12058static bool
12059process_dynamic_section (Filedata * filedata)
12060{
12061 Elf_Internal_Dyn * entry;
12062
12063 if (filedata->dynamic_size <= 1)
12064 {
12065 if (do_dynamic)
12066 {
12067 if (filedata->is_separate)
12068 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
12069 filedata->file_name);
12070 else
12071 printf (_("\nThere is no dynamic section in this file.\n"));
12072 }
12073
12074 return true;
12075 }
12076
12077 if (!get_dynamic_section (filedata))
12078 return false;
12079
12080 /* Find the appropriate symbol table. */
12081 if (filedata->dynamic_symbols == NULL || do_histogram)
12082 {
12083 uint64_t num_of_syms;
12084
12085 for (entry = filedata->dynamic_section;
12086 entry < filedata->dynamic_section + filedata->dynamic_nent;
12087 ++entry)
12088 if (entry->d_tag == DT_SYMTAB)
12089 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
12090 else if (entry->d_tag == DT_SYMENT)
12091 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
12092 else if (entry->d_tag == DT_HASH)
12093 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
12094 else if (entry->d_tag == DT_GNU_HASH)
12095 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
12096 else if ((filedata->file_header.e_machine == EM_MIPS
12097 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
12098 && entry->d_tag == DT_MIPS_XHASH)
12099 {
12100 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
12101 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
12102 }
12103
12104 num_of_syms = get_num_dynamic_syms (filedata);
12105
12106 if (num_of_syms != 0
12107 && filedata->dynamic_symbols == NULL
12108 && filedata->dynamic_info[DT_SYMTAB]
12109 && filedata->dynamic_info[DT_SYMENT])
12110 {
12111 Elf_Internal_Phdr *seg;
12112 uint64_t vma = filedata->dynamic_info[DT_SYMTAB];
12113
12114 if (! get_program_headers (filedata))
12115 {
12116 error (_("Cannot interpret virtual addresses "
12117 "without program headers.\n"));
12118 return false;
12119 }
12120
12121 for (seg = filedata->program_headers;
12122 seg < filedata->program_headers + filedata->file_header.e_phnum;
12123 ++seg)
12124 {
12125 if (seg->p_type != PT_LOAD)
12126 continue;
12127
12128 if (seg->p_offset + seg->p_filesz > filedata->file_size)
12129 {
12130 /* See PR 21379 for a reproducer. */
12131 error (_("Invalid PT_LOAD entry\n"));
12132 return false;
12133 }
12134
12135 if (vma >= (seg->p_vaddr & -seg->p_align)
12136 && vma < seg->p_vaddr + seg->p_filesz)
12137 {
12138 /* Since we do not know how big the symbol table is,
12139 we default to reading in up to the end of PT_LOAD
12140 segment and processing that. This is overkill, I
12141 know, but it should work. */
12142 Elf_Internal_Shdr section;
12143 section.sh_offset = (vma - seg->p_vaddr
12144 + seg->p_offset);
12145 section.sh_size = (num_of_syms
12146 * filedata->dynamic_info[DT_SYMENT]);
12147 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
12148
12149 if (do_checks
12150 && filedata->dynamic_symtab_section != NULL
12151 && ((filedata->dynamic_symtab_section->sh_offset
12152 != section.sh_offset)
12153 || (filedata->dynamic_symtab_section->sh_size
12154 != section.sh_size)
12155 || (filedata->dynamic_symtab_section->sh_entsize
12156 != section.sh_entsize)))
12157 warn (_("\
12158the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
12159
12160 section.sh_name = filedata->string_table_length;
12161 filedata->dynamic_symbols
12162 = get_elf_symbols (filedata, &section,
12163 &filedata->num_dynamic_syms);
12164 if (filedata->dynamic_symbols == NULL
12165 || filedata->num_dynamic_syms != num_of_syms)
12166 {
12167 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
12168 return false;
12169 }
12170 break;
12171 }
12172 }
12173 }
12174 }
12175
12176 /* Similarly find a string table. */
12177 if (filedata->dynamic_strings == NULL)
12178 for (entry = filedata->dynamic_section;
12179 entry < filedata->dynamic_section + filedata->dynamic_nent;
12180 ++entry)
12181 {
12182 if (entry->d_tag == DT_STRTAB)
12183 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
12184
12185 if (entry->d_tag == DT_STRSZ)
12186 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
12187
12188 if (filedata->dynamic_info[DT_STRTAB]
12189 && filedata->dynamic_info[DT_STRSZ])
12190 {
12191 uint64_t offset;
12192 uint64_t str_tab_len = filedata->dynamic_info[DT_STRSZ];
12193
12194 offset = offset_from_vma (filedata,
12195 filedata->dynamic_info[DT_STRTAB],
12196 str_tab_len);
12197 if (do_checks
12198 && filedata->dynamic_strtab_section
12199 && ((filedata->dynamic_strtab_section->sh_offset
12200 != (file_ptr) offset)
12201 || (filedata->dynamic_strtab_section->sh_size
12202 != str_tab_len)))
12203 warn (_("\
12204the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
12205
12206 filedata->dynamic_strings
12207 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
12208 _("dynamic string table"));
12209 if (filedata->dynamic_strings == NULL)
12210 {
12211 error (_("Corrupt DT_STRTAB dynamic entry\n"));
12212 break;
12213 }
12214
12215 filedata->dynamic_strings_length = str_tab_len;
12216 break;
12217 }
12218 }
12219
12220 /* And find the syminfo section if available. */
12221 if (filedata->dynamic_syminfo == NULL)
12222 {
12223 uint64_t syminsz = 0;
12224
12225 for (entry = filedata->dynamic_section;
12226 entry < filedata->dynamic_section + filedata->dynamic_nent;
12227 ++entry)
12228 {
12229 if (entry->d_tag == DT_SYMINENT)
12230 {
12231 /* Note: these braces are necessary to avoid a syntax
12232 error from the SunOS4 C compiler. */
12233 /* PR binutils/17531: A corrupt file can trigger this test.
12234 So do not use an assert, instead generate an error message. */
12235 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
12236 error (_("Bad value (%d) for SYMINENT entry\n"),
12237 (int) entry->d_un.d_val);
12238 }
12239 else if (entry->d_tag == DT_SYMINSZ)
12240 syminsz = entry->d_un.d_val;
12241 else if (entry->d_tag == DT_SYMINFO)
12242 filedata->dynamic_syminfo_offset
12243 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
12244 }
12245
12246 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
12247 {
12248 Elf_External_Syminfo * extsyminfo;
12249 Elf_External_Syminfo * extsym;
12250 Elf_Internal_Syminfo * syminfo;
12251
12252 /* There is a syminfo section. Read the data. */
12253 extsyminfo = (Elf_External_Syminfo *)
12254 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
12255 1, syminsz, _("symbol information"));
12256 if (!extsyminfo)
12257 return false;
12258
12259 if (filedata->dynamic_syminfo != NULL)
12260 {
12261 error (_("Multiple dynamic symbol information sections found\n"));
12262 free (filedata->dynamic_syminfo);
12263 }
12264 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
12265 if (filedata->dynamic_syminfo == NULL)
12266 {
12267 error (_("Out of memory allocating %" PRIu64
12268 " bytes for dynamic symbol info\n"),
12269 syminsz);
12270 return false;
12271 }
12272
12273 filedata->dynamic_syminfo_nent
12274 = syminsz / sizeof (Elf_External_Syminfo);
12275 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
12276 syminfo < (filedata->dynamic_syminfo
12277 + filedata->dynamic_syminfo_nent);
12278 ++syminfo, ++extsym)
12279 {
12280 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
12281 syminfo->si_flags = BYTE_GET (extsym->si_flags);
12282 }
12283
12284 free (extsyminfo);
12285 }
12286 }
12287
12288 if (do_dynamic && filedata->dynamic_addr)
12289 {
12290 if (filedata->is_separate)
12291 printf (ngettext ("\nIn linked file '%s' the dynamic section at offset %#" PRIx64 " contains %" PRIu64 " entry:\n",
12292 "\nIn linked file '%s' the dynamic section at offset %#" PRIx64 " contains %" PRIu64 " entries:\n",
12293 filedata->dynamic_nent),
12294 filedata->file_name,
12295 filedata->dynamic_addr,
12296 filedata->dynamic_nent);
12297 else
12298 printf (ngettext ("\nDynamic section at offset %#" PRIx64 " contains %" PRIu64 " entry:\n",
12299 "\nDynamic section at offset %#" PRIx64 " contains %" PRIu64 " entries:\n",
12300 filedata->dynamic_nent),
12301 filedata->dynamic_addr,
12302 filedata->dynamic_nent);
12303 }
12304 if (do_dynamic)
12305 printf (_(" Tag Type Name/Value\n"));
12306
12307 for (entry = filedata->dynamic_section;
12308 entry < filedata->dynamic_section + filedata->dynamic_nent;
12309 entry++)
12310 {
12311 if (do_dynamic)
12312 {
12313 const char * dtype;
12314
12315 putchar (' ');
12316 print_vma (entry->d_tag, FULL_HEX);
12317 dtype = get_dynamic_type (filedata, entry->d_tag);
12318 printf (" (%s)%*s", dtype,
12319 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
12320 }
12321
12322 switch (entry->d_tag)
12323 {
12324 case DT_FLAGS:
12325 if (do_dynamic)
12326 print_dynamic_flags (entry->d_un.d_val);
12327 break;
12328
12329 case DT_AUXILIARY:
12330 case DT_FILTER:
12331 case DT_CONFIG:
12332 case DT_DEPAUDIT:
12333 case DT_AUDIT:
12334 if (do_dynamic)
12335 {
12336 switch (entry->d_tag)
12337 {
12338 case DT_AUXILIARY:
12339 printf (_("Auxiliary library"));
12340 break;
12341
12342 case DT_FILTER:
12343 printf (_("Filter library"));
12344 break;
12345
12346 case DT_CONFIG:
12347 printf (_("Configuration file"));
12348 break;
12349
12350 case DT_DEPAUDIT:
12351 printf (_("Dependency audit library"));
12352 break;
12353
12354 case DT_AUDIT:
12355 printf (_("Audit library"));
12356 break;
12357 }
12358
12359 if (valid_dynamic_name (filedata, entry->d_un.d_val))
12360 printf (": [%s]\n",
12361 get_dynamic_name (filedata, entry->d_un.d_val));
12362 else
12363 {
12364 printf (": ");
12365 print_vma (entry->d_un.d_val, PREFIX_HEX);
12366 putchar ('\n');
12367 }
12368 }
12369 break;
12370
12371 case DT_FEATURE:
12372 if (do_dynamic)
12373 {
12374 printf (_("Flags:"));
12375
12376 if (entry->d_un.d_val == 0)
12377 printf (_(" None\n"));
12378 else
12379 {
12380 uint64_t val = entry->d_un.d_val;
12381
12382 if (val & DTF_1_PARINIT)
12383 {
12384 printf (" PARINIT");
12385 val ^= DTF_1_PARINIT;
12386 }
12387 if (val & DTF_1_CONFEXP)
12388 {
12389 printf (" CONFEXP");
12390 val ^= DTF_1_CONFEXP;
12391 }
12392 if (val != 0)
12393 printf (" %" PRIx64, val);
12394 puts ("");
12395 }
12396 }
12397 break;
12398
12399 case DT_POSFLAG_1:
12400 if (do_dynamic)
12401 {
12402 printf (_("Flags:"));
12403
12404 if (entry->d_un.d_val == 0)
12405 printf (_(" None\n"));
12406 else
12407 {
12408 uint64_t val = entry->d_un.d_val;
12409
12410 if (val & DF_P1_LAZYLOAD)
12411 {
12412 printf (" LAZYLOAD");
12413 val ^= DF_P1_LAZYLOAD;
12414 }
12415 if (val & DF_P1_GROUPPERM)
12416 {
12417 printf (" GROUPPERM");
12418 val ^= DF_P1_GROUPPERM;
12419 }
12420 if (val != 0)
12421 printf (" %" PRIx64, val);
12422 puts ("");
12423 }
12424 }
12425 break;
12426
12427 case DT_FLAGS_1:
12428 if (do_dynamic)
12429 {
12430 printf (_("Flags:"));
12431 if (entry->d_un.d_val == 0)
12432 printf (_(" None\n"));
12433 else
12434 {
12435 uint64_t val = entry->d_un.d_val;
12436
12437 if (val & DF_1_NOW)
12438 {
12439 printf (" NOW");
12440 val ^= DF_1_NOW;
12441 }
12442 if (val & DF_1_GLOBAL)
12443 {
12444 printf (" GLOBAL");
12445 val ^= DF_1_GLOBAL;
12446 }
12447 if (val & DF_1_GROUP)
12448 {
12449 printf (" GROUP");
12450 val ^= DF_1_GROUP;
12451 }
12452 if (val & DF_1_NODELETE)
12453 {
12454 printf (" NODELETE");
12455 val ^= DF_1_NODELETE;
12456 }
12457 if (val & DF_1_LOADFLTR)
12458 {
12459 printf (" LOADFLTR");
12460 val ^= DF_1_LOADFLTR;
12461 }
12462 if (val & DF_1_INITFIRST)
12463 {
12464 printf (" INITFIRST");
12465 val ^= DF_1_INITFIRST;
12466 }
12467 if (val & DF_1_NOOPEN)
12468 {
12469 printf (" NOOPEN");
12470 val ^= DF_1_NOOPEN;
12471 }
12472 if (val & DF_1_ORIGIN)
12473 {
12474 printf (" ORIGIN");
12475 val ^= DF_1_ORIGIN;
12476 }
12477 if (val & DF_1_DIRECT)
12478 {
12479 printf (" DIRECT");
12480 val ^= DF_1_DIRECT;
12481 }
12482 if (val & DF_1_TRANS)
12483 {
12484 printf (" TRANS");
12485 val ^= DF_1_TRANS;
12486 }
12487 if (val & DF_1_INTERPOSE)
12488 {
12489 printf (" INTERPOSE");
12490 val ^= DF_1_INTERPOSE;
12491 }
12492 if (val & DF_1_NODEFLIB)
12493 {
12494 printf (" NODEFLIB");
12495 val ^= DF_1_NODEFLIB;
12496 }
12497 if (val & DF_1_NODUMP)
12498 {
12499 printf (" NODUMP");
12500 val ^= DF_1_NODUMP;
12501 }
12502 if (val & DF_1_CONFALT)
12503 {
12504 printf (" CONFALT");
12505 val ^= DF_1_CONFALT;
12506 }
12507 if (val & DF_1_ENDFILTEE)
12508 {
12509 printf (" ENDFILTEE");
12510 val ^= DF_1_ENDFILTEE;
12511 }
12512 if (val & DF_1_DISPRELDNE)
12513 {
12514 printf (" DISPRELDNE");
12515 val ^= DF_1_DISPRELDNE;
12516 }
12517 if (val & DF_1_DISPRELPND)
12518 {
12519 printf (" DISPRELPND");
12520 val ^= DF_1_DISPRELPND;
12521 }
12522 if (val & DF_1_NODIRECT)
12523 {
12524 printf (" NODIRECT");
12525 val ^= DF_1_NODIRECT;
12526 }
12527 if (val & DF_1_IGNMULDEF)
12528 {
12529 printf (" IGNMULDEF");
12530 val ^= DF_1_IGNMULDEF;
12531 }
12532 if (val & DF_1_NOKSYMS)
12533 {
12534 printf (" NOKSYMS");
12535 val ^= DF_1_NOKSYMS;
12536 }
12537 if (val & DF_1_NOHDR)
12538 {
12539 printf (" NOHDR");
12540 val ^= DF_1_NOHDR;
12541 }
12542 if (val & DF_1_EDITED)
12543 {
12544 printf (" EDITED");
12545 val ^= DF_1_EDITED;
12546 }
12547 if (val & DF_1_NORELOC)
12548 {
12549 printf (" NORELOC");
12550 val ^= DF_1_NORELOC;
12551 }
12552 if (val & DF_1_SYMINTPOSE)
12553 {
12554 printf (" SYMINTPOSE");
12555 val ^= DF_1_SYMINTPOSE;
12556 }
12557 if (val & DF_1_GLOBAUDIT)
12558 {
12559 printf (" GLOBAUDIT");
12560 val ^= DF_1_GLOBAUDIT;
12561 }
12562 if (val & DF_1_SINGLETON)
12563 {
12564 printf (" SINGLETON");
12565 val ^= DF_1_SINGLETON;
12566 }
12567 if (val & DF_1_STUB)
12568 {
12569 printf (" STUB");
12570 val ^= DF_1_STUB;
12571 }
12572 if (val & DF_1_PIE)
12573 {
12574 printf (" PIE");
12575 val ^= DF_1_PIE;
12576 }
12577 if (val & DF_1_KMOD)
12578 {
12579 printf (" KMOD");
12580 val ^= DF_1_KMOD;
12581 }
12582 if (val & DF_1_WEAKFILTER)
12583 {
12584 printf (" WEAKFILTER");
12585 val ^= DF_1_WEAKFILTER;
12586 }
12587 if (val & DF_1_NOCOMMON)
12588 {
12589 printf (" NOCOMMON");
12590 val ^= DF_1_NOCOMMON;
12591 }
12592 if (val != 0)
12593 printf (" %" PRIx64, val);
12594 puts ("");
12595 }
12596 }
12597 break;
12598
12599 case DT_PLTREL:
12600 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
12601 if (do_dynamic)
12602 puts (get_dynamic_type (filedata, entry->d_un.d_val));
12603 break;
12604
12605 case DT_NULL :
12606 case DT_NEEDED :
12607 case DT_PLTGOT :
12608 case DT_HASH :
12609 case DT_STRTAB :
12610 case DT_SYMTAB :
12611 case DT_RELA :
12612 case DT_INIT :
12613 case DT_FINI :
12614 case DT_SONAME :
12615 case DT_RPATH :
12616 case DT_SYMBOLIC:
12617 case DT_REL :
12618 case DT_RELR :
12619 case DT_DEBUG :
12620 case DT_TEXTREL :
12621 case DT_JMPREL :
12622 case DT_RUNPATH :
12623 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
12624
12625 if (do_dynamic)
12626 {
12627 const char *name;
12628
12629 if (valid_dynamic_name (filedata, entry->d_un.d_val))
12630 name = get_dynamic_name (filedata, entry->d_un.d_val);
12631 else
12632 name = NULL;
12633
12634 if (name)
12635 {
12636 switch (entry->d_tag)
12637 {
12638 case DT_NEEDED:
12639 printf (_("Shared library: [%s]"), name);
12640
12641 if (filedata->program_interpreter
12642 && streq (name, filedata->program_interpreter))
12643 printf (_(" program interpreter"));
12644 break;
12645
12646 case DT_SONAME:
12647 printf (_("Library soname: [%s]"), name);
12648 break;
12649
12650 case DT_RPATH:
12651 printf (_("Library rpath: [%s]"), name);
12652 break;
12653
12654 case DT_RUNPATH:
12655 printf (_("Library runpath: [%s]"), name);
12656 break;
12657
12658 default:
12659 print_vma (entry->d_un.d_val, PREFIX_HEX);
12660 break;
12661 }
12662 }
12663 else
12664 print_vma (entry->d_un.d_val, PREFIX_HEX);
12665
12666 putchar ('\n');
12667 }
12668 break;
12669
12670 case DT_PLTRELSZ:
12671 case DT_RELASZ :
12672 case DT_STRSZ :
12673 case DT_RELSZ :
12674 case DT_RELAENT :
12675 case DT_RELRENT :
12676 case DT_RELRSZ :
12677 case DT_SYMENT :
12678 case DT_RELENT :
12679 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
12680 /* Fall through. */
12681 case DT_PLTPADSZ:
12682 case DT_MOVEENT :
12683 case DT_MOVESZ :
12684 case DT_PREINIT_ARRAYSZ:
12685 case DT_INIT_ARRAYSZ:
12686 case DT_FINI_ARRAYSZ:
12687 case DT_GNU_CONFLICTSZ:
12688 case DT_GNU_LIBLISTSZ:
12689 if (do_dynamic)
12690 {
12691 print_vma (entry->d_un.d_val, UNSIGNED);
12692 printf (_(" (bytes)\n"));
12693 }
12694 break;
12695
12696 case DT_VERDEFNUM:
12697 case DT_VERNEEDNUM:
12698 case DT_RELACOUNT:
12699 case DT_RELCOUNT:
12700 if (do_dynamic)
12701 {
12702 print_vma (entry->d_un.d_val, UNSIGNED);
12703 putchar ('\n');
12704 }
12705 break;
12706
12707 case DT_SYMINSZ:
12708 case DT_SYMINENT:
12709 case DT_SYMINFO:
12710 case DT_USED:
12711 case DT_INIT_ARRAY:
12712 case DT_FINI_ARRAY:
12713 if (do_dynamic)
12714 {
12715 if (entry->d_tag == DT_USED
12716 && valid_dynamic_name (filedata, entry->d_un.d_val))
12717 {
12718 const char *name
12719 = get_dynamic_name (filedata, entry->d_un.d_val);
12720
12721 if (*name)
12722 {
12723 printf (_("Not needed object: [%s]\n"), name);
12724 break;
12725 }
12726 }
12727
12728 print_vma (entry->d_un.d_val, PREFIX_HEX);
12729 putchar ('\n');
12730 }
12731 break;
12732
12733 case DT_BIND_NOW:
12734 /* The value of this entry is ignored. */
12735 if (do_dynamic)
12736 putchar ('\n');
12737 break;
12738
12739 case DT_GNU_PRELINKED:
12740 if (do_dynamic)
12741 {
12742 struct tm * tmp;
12743 time_t atime = entry->d_un.d_val;
12744
12745 tmp = gmtime (&atime);
12746 /* PR 17533 file: 041-1244816-0.004. */
12747 if (tmp == NULL)
12748 printf (_("<corrupt time val: %" PRIx64),
12749 (uint64_t) atime);
12750 else
12751 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
12752 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
12753 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
12754
12755 }
12756 break;
12757
12758 case DT_GNU_HASH:
12759 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
12760 if (do_dynamic)
12761 {
12762 print_vma (entry->d_un.d_val, PREFIX_HEX);
12763 putchar ('\n');
12764 }
12765 break;
12766
12767 case DT_GNU_FLAGS_1:
12768 if (do_dynamic)
12769 {
12770 printf (_("Flags:"));
12771 if (entry->d_un.d_val == 0)
12772 printf (_(" None\n"));
12773 else
12774 {
12775 uint64_t val = entry->d_un.d_val;
12776
12777 if (val & DF_GNU_1_UNIQUE)
12778 {
12779 printf (" UNIQUE");
12780 val ^= DF_GNU_1_UNIQUE;
12781 }
12782 if (val != 0)
12783 printf (" %" PRIx64, val);
12784 puts ("");
12785 }
12786 }
12787 break;
12788
12789 default:
12790 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
12791 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
12792 = entry->d_un.d_val;
12793
12794 if (do_dynamic)
12795 {
12796 switch (filedata->file_header.e_machine)
12797 {
12798 case EM_AARCH64:
12799 dynamic_section_aarch64_val (entry);
12800 break;
12801 case EM_MIPS:
12802 case EM_MIPS_RS3_LE:
12803 dynamic_section_mips_val (filedata, entry);
12804 break;
12805 case EM_PARISC:
12806 dynamic_section_parisc_val (entry);
12807 break;
12808 case EM_IA_64:
12809 dynamic_section_ia64_val (entry);
12810 break;
12811 default:
12812 print_vma (entry->d_un.d_val, PREFIX_HEX);
12813 putchar ('\n');
12814 }
12815 }
12816 break;
12817 }
12818 }
12819
12820 return true;
12821}
12822
12823static char *
12824get_ver_flags (unsigned int flags)
12825{
12826 static char buff[128];
12827
12828 buff[0] = 0;
12829
12830 if (flags == 0)
12831 return _("none");
12832
12833 if (flags & VER_FLG_BASE)
12834 strcat (buff, "BASE");
12835
12836 if (flags & VER_FLG_WEAK)
12837 {
12838 if (flags & VER_FLG_BASE)
12839 strcat (buff, " | ");
12840
12841 strcat (buff, "WEAK");
12842 }
12843
12844 if (flags & VER_FLG_INFO)
12845 {
12846 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
12847 strcat (buff, " | ");
12848
12849 strcat (buff, "INFO");
12850 }
12851
12852 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12853 {
12854 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12855 strcat (buff, " | ");
12856
12857 strcat (buff, _("<unknown>"));
12858 }
12859
12860 return buff;
12861}
12862
12863/* Display the contents of the version sections. */
12864
12865static bool
12866process_version_sections (Filedata * filedata)
12867{
12868 Elf_Internal_Shdr * section;
12869 unsigned i;
12870 bool found = false;
12871
12872 if (! do_version)
12873 return true;
12874
12875 for (i = 0, section = filedata->section_headers;
12876 i < filedata->file_header.e_shnum;
12877 i++, section++)
12878 {
12879 switch (section->sh_type)
12880 {
12881 case SHT_GNU_verdef:
12882 {
12883 Elf_External_Verdef * edefs;
12884 size_t idx;
12885 size_t cnt;
12886 char * endbuf;
12887
12888 found = true;
12889
12890 if (filedata->is_separate)
12891 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
12892 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
12893 section->sh_info),
12894 filedata->file_name,
12895 printable_section_name (filedata, section),
12896 section->sh_info);
12897 else
12898 printf (ngettext ("\nVersion definition section '%s' "
12899 "contains %u entry:\n",
12900 "\nVersion definition section '%s' "
12901 "contains %u entries:\n",
12902 section->sh_info),
12903 printable_section_name (filedata, section),
12904 section->sh_info);
12905
12906 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12907 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
12908 section->sh_offset, section->sh_link,
12909 printable_section_name_from_index (filedata, section->sh_link, NULL));
12910
12911 edefs = (Elf_External_Verdef *)
12912 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
12913 _("version definition section"));
12914 if (!edefs)
12915 break;
12916 endbuf = (char *) edefs + section->sh_size;
12917
12918 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12919 {
12920 char * vstart;
12921 Elf_External_Verdef * edef;
12922 Elf_Internal_Verdef ent;
12923 Elf_External_Verdaux * eaux;
12924 Elf_Internal_Verdaux aux;
12925 size_t isum;
12926 int j;
12927
12928 vstart = ((char *) edefs) + idx;
12929 if (vstart + sizeof (*edef) > endbuf)
12930 break;
12931
12932 edef = (Elf_External_Verdef *) vstart;
12933
12934 ent.vd_version = BYTE_GET (edef->vd_version);
12935 ent.vd_flags = BYTE_GET (edef->vd_flags);
12936 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
12937 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
12938 ent.vd_hash = BYTE_GET (edef->vd_hash);
12939 ent.vd_aux = BYTE_GET (edef->vd_aux);
12940 ent.vd_next = BYTE_GET (edef->vd_next);
12941
12942 printf (_(" %#06zx: Rev: %d Flags: %s"),
12943 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
12944
12945 printf (_(" Index: %d Cnt: %d "),
12946 ent.vd_ndx, ent.vd_cnt);
12947
12948 /* Check for overflow. */
12949 if (ent.vd_aux > (size_t) (endbuf - vstart))
12950 break;
12951
12952 vstart += ent.vd_aux;
12953
12954 if (vstart + sizeof (*eaux) > endbuf)
12955 break;
12956 eaux = (Elf_External_Verdaux *) vstart;
12957
12958 aux.vda_name = BYTE_GET (eaux->vda_name);
12959 aux.vda_next = BYTE_GET (eaux->vda_next);
12960
12961 if (valid_dynamic_name (filedata, aux.vda_name))
12962 printf (_("Name: %s\n"),
12963 get_dynamic_name (filedata, aux.vda_name));
12964 else
12965 printf (_("Name index: %ld\n"), aux.vda_name);
12966
12967 isum = idx + ent.vd_aux;
12968
12969 for (j = 1; j < ent.vd_cnt; j++)
12970 {
12971 if (aux.vda_next < sizeof (*eaux)
12972 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
12973 {
12974 warn (_("Invalid vda_next field of %lx\n"),
12975 aux.vda_next);
12976 j = ent.vd_cnt;
12977 break;
12978 }
12979 /* Check for overflow. */
12980 if (aux.vda_next > (size_t) (endbuf - vstart))
12981 break;
12982
12983 isum += aux.vda_next;
12984 vstart += aux.vda_next;
12985
12986 if (vstart + sizeof (*eaux) > endbuf)
12987 break;
12988 eaux = (Elf_External_Verdaux *) vstart;
12989
12990 aux.vda_name = BYTE_GET (eaux->vda_name);
12991 aux.vda_next = BYTE_GET (eaux->vda_next);
12992
12993 if (valid_dynamic_name (filedata, aux.vda_name))
12994 printf (_(" %#06zx: Parent %d: %s\n"),
12995 isum, j,
12996 get_dynamic_name (filedata, aux.vda_name));
12997 else
12998 printf (_(" %#06zx: Parent %d, name index: %ld\n"),
12999 isum, j, aux.vda_name);
13000 }
13001
13002 if (j < ent.vd_cnt)
13003 printf (_(" Version def aux past end of section\n"));
13004
13005 /* PR 17531:
13006 file: id:000001,src:000172+005151,op:splice,rep:2. */
13007 if (ent.vd_next < sizeof (*edef)
13008 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
13009 {
13010 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
13011 cnt = section->sh_info;
13012 break;
13013 }
13014 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
13015 break;
13016
13017 idx += ent.vd_next;
13018 }
13019
13020 if (cnt < section->sh_info)
13021 printf (_(" Version definition past end of section\n"));
13022
13023 free (edefs);
13024 }
13025 break;
13026
13027 case SHT_GNU_verneed:
13028 {
13029 Elf_External_Verneed * eneed;
13030 size_t idx;
13031 size_t cnt;
13032 char * endbuf;
13033
13034 found = true;
13035
13036 if (filedata->is_separate)
13037 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
13038 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
13039 section->sh_info),
13040 filedata->file_name,
13041 printable_section_name (filedata, section),
13042 section->sh_info);
13043 else
13044 printf (ngettext ("\nVersion needs section '%s' "
13045 "contains %u entry:\n",
13046 "\nVersion needs section '%s' "
13047 "contains %u entries:\n",
13048 section->sh_info),
13049 printable_section_name (filedata, section),
13050 section->sh_info);
13051
13052 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
13053 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
13054 section->sh_offset, section->sh_link,
13055 printable_section_name_from_index (filedata, section->sh_link, NULL));
13056
13057 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
13058 section->sh_offset, 1,
13059 section->sh_size,
13060 _("Version Needs section"));
13061 if (!eneed)
13062 break;
13063 endbuf = (char *) eneed + section->sh_size;
13064
13065 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
13066 {
13067 Elf_External_Verneed * entry;
13068 Elf_Internal_Verneed ent;
13069 size_t isum;
13070 int j;
13071 char * vstart;
13072
13073 vstart = ((char *) eneed) + idx;
13074 if (vstart + sizeof (*entry) > endbuf)
13075 break;
13076
13077 entry = (Elf_External_Verneed *) vstart;
13078
13079 ent.vn_version = BYTE_GET (entry->vn_version);
13080 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
13081 ent.vn_file = BYTE_GET (entry->vn_file);
13082 ent.vn_aux = BYTE_GET (entry->vn_aux);
13083 ent.vn_next = BYTE_GET (entry->vn_next);
13084
13085 printf (_(" %#06zx: Version: %d"), idx, ent.vn_version);
13086
13087 if (valid_dynamic_name (filedata, ent.vn_file))
13088 printf (_(" File: %s"),
13089 get_dynamic_name (filedata, ent.vn_file));
13090 else
13091 printf (_(" File: %lx"), ent.vn_file);
13092
13093 printf (_(" Cnt: %d\n"), ent.vn_cnt);
13094
13095 /* Check for overflow. */
13096 if (ent.vn_aux > (size_t) (endbuf - vstart))
13097 break;
13098 vstart += ent.vn_aux;
13099
13100 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
13101 {
13102 Elf_External_Vernaux * eaux;
13103 Elf_Internal_Vernaux aux;
13104
13105 if (vstart + sizeof (*eaux) > endbuf)
13106 break;
13107 eaux = (Elf_External_Vernaux *) vstart;
13108
13109 aux.vna_hash = BYTE_GET (eaux->vna_hash);
13110 aux.vna_flags = BYTE_GET (eaux->vna_flags);
13111 aux.vna_other = BYTE_GET (eaux->vna_other);
13112 aux.vna_name = BYTE_GET (eaux->vna_name);
13113 aux.vna_next = BYTE_GET (eaux->vna_next);
13114
13115 if (valid_dynamic_name (filedata, aux.vna_name))
13116 printf (_(" %#06zx: Name: %s"),
13117 isum, get_dynamic_name (filedata, aux.vna_name));
13118 else
13119 printf (_(" %#06zx: Name index: %lx"),
13120 isum, aux.vna_name);
13121
13122 printf (_(" Flags: %s Version: %d\n"),
13123 get_ver_flags (aux.vna_flags), aux.vna_other);
13124
13125 if (aux.vna_next < sizeof (*eaux)
13126 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
13127 {
13128 warn (_("Invalid vna_next field of %lx\n"),
13129 aux.vna_next);
13130 j = ent.vn_cnt;
13131 break;
13132 }
13133 /* Check for overflow. */
13134 if (aux.vna_next > (size_t) (endbuf - vstart))
13135 break;
13136 isum += aux.vna_next;
13137 vstart += aux.vna_next;
13138 }
13139
13140 if (j < ent.vn_cnt)
13141 warn (_("Missing Version Needs auxiliary information\n"));
13142
13143 if (ent.vn_next < sizeof (*entry)
13144 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
13145 {
13146 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
13147 cnt = section->sh_info;
13148 break;
13149 }
13150 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
13151 break;
13152 idx += ent.vn_next;
13153 }
13154
13155 if (cnt < section->sh_info)
13156 warn (_("Missing Version Needs information\n"));
13157
13158 free (eneed);
13159 }
13160 break;
13161
13162 case SHT_GNU_versym:
13163 {
13164 Elf_Internal_Shdr * link_section;
13165 uint64_t total;
13166 unsigned int cnt;
13167 unsigned char * edata;
13168 unsigned short * data;
13169 char * strtab;
13170 Elf_Internal_Sym * symbols;
13171 Elf_Internal_Shdr * string_sec;
13172 uint64_t num_syms;
13173 uint64_t off;
13174
13175 if (section->sh_link >= filedata->file_header.e_shnum)
13176 break;
13177
13178 link_section = filedata->section_headers + section->sh_link;
13179 total = section->sh_size / sizeof (Elf_External_Versym);
13180
13181 if (link_section->sh_link >= filedata->file_header.e_shnum)
13182 break;
13183
13184 found = true;
13185
13186 symbols = get_elf_symbols (filedata, link_section, & num_syms);
13187 if (symbols == NULL)
13188 break;
13189
13190 string_sec = filedata->section_headers + link_section->sh_link;
13191
13192 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
13193 string_sec->sh_size,
13194 _("version string table"));
13195 if (!strtab)
13196 {
13197 free (symbols);
13198 break;
13199 }
13200
13201 if (filedata->is_separate)
13202 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %" PRIu64 " entry:\n",
13203 "\nIn linked file '%s' the version symbols section '%s' contains %" PRIu64 " entries:\n",
13204 total),
13205 filedata->file_name,
13206 printable_section_name (filedata, section),
13207 total);
13208 else
13209 printf (ngettext ("\nVersion symbols section '%s' "
13210 "contains %" PRIu64 " entry:\n",
13211 "\nVersion symbols section '%s' "
13212 "contains %" PRIu64 " entries:\n",
13213 total),
13214 printable_section_name (filedata, section),
13215 total);
13216
13217 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
13218 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
13219 section->sh_offset, section->sh_link,
13220 printable_section_name (filedata, link_section));
13221
13222 off = offset_from_vma (filedata,
13223 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
13224 total * sizeof (short));
13225 edata = (unsigned char *) get_data (NULL, filedata, off,
13226 sizeof (short), total,
13227 _("version symbol data"));
13228 if (!edata)
13229 {
13230 free (strtab);
13231 free (symbols);
13232 break;
13233 }
13234
13235 data = (short unsigned int *) cmalloc (total, sizeof (short));
13236
13237 for (cnt = total; cnt --;)
13238 data[cnt] = byte_get (edata + cnt * sizeof (short),
13239 sizeof (short));
13240
13241 free (edata);
13242
13243 for (cnt = 0; cnt < total; cnt += 4)
13244 {
13245 int j, nn;
13246 char *name;
13247 char *invalid = _("*invalid*");
13248
13249 printf (" %03x:", cnt);
13250
13251 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
13252 switch (data[cnt + j])
13253 {
13254 case 0:
13255 fputs (_(" 0 (*local*) "), stdout);
13256 break;
13257
13258 case 1:
13259 fputs (_(" 1 (*global*) "), stdout);
13260 break;
13261
13262 default:
13263 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
13264 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
13265
13266 /* If this index value is greater than the size of the symbols
13267 array, break to avoid an out-of-bounds read. */
13268 if (cnt + j >= num_syms)
13269 {
13270 warn (_("invalid index into symbol array\n"));
13271 break;
13272 }
13273
13274 name = NULL;
13275 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
13276 {
13277 Elf_Internal_Verneed ivn;
13278 uint64_t offset;
13279
13280 offset = offset_from_vma
13281 (filedata,
13282 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
13283 sizeof (Elf_External_Verneed));
13284
13285 do
13286 {
13287 Elf_Internal_Vernaux ivna;
13288 Elf_External_Verneed evn;
13289 Elf_External_Vernaux evna;
13290 uint64_t a_off;
13291
13292 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
13293 _("version need")) == NULL)
13294 break;
13295
13296 ivn.vn_aux = BYTE_GET (evn.vn_aux);
13297 ivn.vn_next = BYTE_GET (evn.vn_next);
13298
13299 a_off = offset + ivn.vn_aux;
13300
13301 do
13302 {
13303 if (get_data (&evna, filedata, a_off, sizeof (evna),
13304 1, _("version need aux (2)")) == NULL)
13305 {
13306 ivna.vna_next = 0;
13307 ivna.vna_other = 0;
13308 }
13309 else
13310 {
13311 ivna.vna_next = BYTE_GET (evna.vna_next);
13312 ivna.vna_other = BYTE_GET (evna.vna_other);
13313 }
13314
13315 a_off += ivna.vna_next;
13316 }
13317 while (ivna.vna_other != data[cnt + j]
13318 && ivna.vna_next != 0);
13319
13320 if (ivna.vna_other == data[cnt + j])
13321 {
13322 ivna.vna_name = BYTE_GET (evna.vna_name);
13323
13324 if (ivna.vna_name >= string_sec->sh_size)
13325 name = invalid;
13326 else
13327 name = strtab + ivna.vna_name;
13328 break;
13329 }
13330
13331 offset += ivn.vn_next;
13332 }
13333 while (ivn.vn_next);
13334 }
13335
13336 if (data[cnt + j] != 0x8001
13337 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
13338 {
13339 Elf_Internal_Verdef ivd;
13340 Elf_External_Verdef evd;
13341 uint64_t offset;
13342
13343 offset = offset_from_vma
13344 (filedata,
13345 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
13346 sizeof evd);
13347
13348 do
13349 {
13350 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
13351 _("version def")) == NULL)
13352 {
13353 ivd.vd_next = 0;
13354 /* PR 17531: file: 046-1082287-0.004. */
13355 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
13356 break;
13357 }
13358 else
13359 {
13360 ivd.vd_next = BYTE_GET (evd.vd_next);
13361 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
13362 }
13363
13364 offset += ivd.vd_next;
13365 }
13366 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
13367 && ivd.vd_next != 0);
13368
13369 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
13370 {
13371 Elf_External_Verdaux evda;
13372 Elf_Internal_Verdaux ivda;
13373
13374 ivd.vd_aux = BYTE_GET (evd.vd_aux);
13375
13376 if (get_data (&evda, filedata,
13377 offset - ivd.vd_next + ivd.vd_aux,
13378 sizeof (evda), 1,
13379 _("version def aux")) == NULL)
13380 break;
13381
13382 ivda.vda_name = BYTE_GET (evda.vda_name);
13383
13384 if (ivda.vda_name >= string_sec->sh_size)
13385 name = invalid;
13386 else if (name != NULL && name != invalid)
13387 name = _("*both*");
13388 else
13389 name = strtab + ivda.vda_name;
13390 }
13391 }
13392 if (name != NULL)
13393 nn += printf ("(%s%-*s",
13394 name,
13395 12 - (int) strlen (name),
13396 ")");
13397
13398 if (nn < 18)
13399 printf ("%*c", 18 - nn, ' ');
13400 }
13401
13402 putchar ('\n');
13403 }
13404
13405 free (data);
13406 free (strtab);
13407 free (symbols);
13408 }
13409 break;
13410
13411 default:
13412 break;
13413 }
13414 }
13415
13416 if (! found)
13417 {
13418 if (filedata->is_separate)
13419 printf (_("\nNo version information found in linked file '%s'.\n"),
13420 filedata->file_name);
13421 else
13422 printf (_("\nNo version information found in this file.\n"));
13423 }
13424
13425 return true;
13426}
13427
13428static const char *
13429get_symbol_binding (Filedata * filedata, unsigned int binding)
13430{
13431 static char buff[64];
13432
13433 switch (binding)
13434 {
13435 case STB_LOCAL: return "LOCAL";
13436 case STB_GLOBAL: return "GLOBAL";
13437 case STB_WEAK: return "WEAK";
13438 default:
13439 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
13440 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
13441 binding);
13442 else if (binding >= STB_LOOS && binding <= STB_HIOS)
13443 {
13444 if (binding == STB_GNU_UNIQUE
13445 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
13446 return "UNIQUE";
13447 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
13448 }
13449 else
13450 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
13451 return buff;
13452 }
13453}
13454
13455static const char *
13456get_symbol_type (Filedata * filedata, unsigned int type)
13457{
13458 static char buff[64];
13459
13460 switch (type)
13461 {
13462 case STT_NOTYPE: return "NOTYPE";
13463 case STT_OBJECT: return "OBJECT";
13464 case STT_FUNC: return "FUNC";
13465 case STT_SECTION: return "SECTION";
13466 case STT_FILE: return "FILE";
13467 case STT_COMMON: return "COMMON";
13468 case STT_TLS: return "TLS";
13469 case STT_RELC: return "RELC";
13470 case STT_SRELC: return "SRELC";
13471 default:
13472 if (type >= STT_LOPROC && type <= STT_HIPROC)
13473 {
13474 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
13475 return "THUMB_FUNC";
13476
13477 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
13478 return "REGISTER";
13479
13480 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
13481 return "PARISC_MILLI";
13482
13483 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
13484 }
13485 else if (type >= STT_LOOS && type <= STT_HIOS)
13486 {
13487 if (filedata->file_header.e_machine == EM_PARISC)
13488 {
13489 if (type == STT_HP_OPAQUE)
13490 return "HP_OPAQUE";
13491 if (type == STT_HP_STUB)
13492 return "HP_STUB";
13493 }
13494
13495 if (type == STT_GNU_IFUNC
13496 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
13497 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
13498 return "IFUNC";
13499
13500 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
13501 }
13502 else
13503 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
13504 return buff;
13505 }
13506}
13507
13508static const char *
13509get_symbol_visibility (unsigned int visibility)
13510{
13511 switch (visibility)
13512 {
13513 case STV_DEFAULT: return "DEFAULT";
13514 case STV_INTERNAL: return "INTERNAL";
13515 case STV_HIDDEN: return "HIDDEN";
13516 case STV_PROTECTED: return "PROTECTED";
13517 default:
13518 error (_("Unrecognized visibility value: %u\n"), visibility);
13519 return _("<unknown>");
13520 }
13521}
13522
13523static const char *
13524get_alpha_symbol_other (unsigned int other)
13525{
13526 switch (other)
13527 {
13528 case STO_ALPHA_NOPV: return "NOPV";
13529 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
13530 default:
13531 error (_("Unrecognized alpha specific other value: %u\n"), other);
13532 return _("<unknown>");
13533 }
13534}
13535
13536static const char *
13537get_solaris_symbol_visibility (unsigned int visibility)
13538{
13539 switch (visibility)
13540 {
13541 case 4: return "EXPORTED";
13542 case 5: return "SINGLETON";
13543 case 6: return "ELIMINATE";
13544 default: return get_symbol_visibility (visibility);
13545 }
13546}
13547
13548static const char *
13549get_aarch64_symbol_other (unsigned int other)
13550{
13551 static char buf[32];
13552
13553 if (other & STO_AARCH64_VARIANT_PCS)
13554 {
13555 other &= ~STO_AARCH64_VARIANT_PCS;
13556 if (other == 0)
13557 return "VARIANT_PCS";
13558 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
13559 return buf;
13560 }
13561 return NULL;
13562}
13563
13564static const char *
13565get_mips_symbol_other (unsigned int other)
13566{
13567 switch (other)
13568 {
13569 case STO_OPTIONAL: return "OPTIONAL";
13570 case STO_MIPS_PLT: return "MIPS PLT";
13571 case STO_MIPS_PIC: return "MIPS PIC";
13572 case STO_MICROMIPS: return "MICROMIPS";
13573 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
13574 case STO_MIPS16: return "MIPS16";
13575 default: return NULL;
13576 }
13577}
13578
13579static const char *
13580get_ia64_symbol_other (Filedata * filedata, unsigned int other)
13581{
13582 if (is_ia64_vms (filedata))
13583 {
13584 static char res[32];
13585
13586 res[0] = 0;
13587
13588 /* Function types is for images and .STB files only. */
13589 switch (filedata->file_header.e_type)
13590 {
13591 case ET_DYN:
13592 case ET_EXEC:
13593 switch (VMS_ST_FUNC_TYPE (other))
13594 {
13595 case VMS_SFT_CODE_ADDR:
13596 strcat (res, " CA");
13597 break;
13598 case VMS_SFT_SYMV_IDX:
13599 strcat (res, " VEC");
13600 break;
13601 case VMS_SFT_FD:
13602 strcat (res, " FD");
13603 break;
13604 case VMS_SFT_RESERVE:
13605 strcat (res, " RSV");
13606 break;
13607 default:
13608 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
13609 VMS_ST_FUNC_TYPE (other));
13610 strcat (res, " <unknown>");
13611 break;
13612 }
13613 break;
13614 default:
13615 break;
13616 }
13617 switch (VMS_ST_LINKAGE (other))
13618 {
13619 case VMS_STL_IGNORE:
13620 strcat (res, " IGN");
13621 break;
13622 case VMS_STL_RESERVE:
13623 strcat (res, " RSV");
13624 break;
13625 case VMS_STL_STD:
13626 strcat (res, " STD");
13627 break;
13628 case VMS_STL_LNK:
13629 strcat (res, " LNK");
13630 break;
13631 default:
13632 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
13633 VMS_ST_LINKAGE (other));
13634 strcat (res, " <unknown>");
13635 break;
13636 }
13637
13638 if (res[0] != 0)
13639 return res + 1;
13640 else
13641 return res;
13642 }
13643 return NULL;
13644}
13645
13646static const char *
13647get_ppc64_symbol_other (unsigned int other)
13648{
13649 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
13650 return NULL;
13651
13652 other >>= STO_PPC64_LOCAL_BIT;
13653 if (other <= 6)
13654 {
13655 static char buf[64];
13656 if (other >= 2)
13657 other = ppc64_decode_local_entry (other);
13658 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
13659 return buf;
13660 }
13661 return NULL;
13662}
13663
13664static const char *
13665get_riscv_symbol_other (unsigned int other)
13666{
13667 static char buf[32];
13668 buf[0] = 0;
13669
13670 if (other & STO_RISCV_VARIANT_CC)
13671 {
13672 strcat (buf, _(" VARIANT_CC"));
13673 other &= ~STO_RISCV_VARIANT_CC;
13674 }
13675
13676 if (other != 0)
13677 snprintf (buf, sizeof buf, " %x", other);
13678
13679
13680 if (buf[0] != 0)
13681 return buf + 1;
13682 else
13683 return buf;
13684}
13685
13686static const char *
13687get_symbol_other (Filedata * filedata, unsigned int other)
13688{
13689 const char * result = NULL;
13690 static char buff [64];
13691
13692 if (other == 0)
13693 return "";
13694
13695 switch (filedata->file_header.e_machine)
13696 {
13697 case EM_ALPHA:
13698 result = get_alpha_symbol_other (other);
13699 break;
13700 case EM_AARCH64:
13701 result = get_aarch64_symbol_other (other);
13702 break;
13703 case EM_MIPS:
13704 result = get_mips_symbol_other (other);
13705 break;
13706 case EM_IA_64:
13707 result = get_ia64_symbol_other (filedata, other);
13708 break;
13709 case EM_PPC64:
13710 result = get_ppc64_symbol_other (other);
13711 break;
13712 case EM_RISCV:
13713 result = get_riscv_symbol_other (other);
13714 break;
13715 default:
13716 result = NULL;
13717 break;
13718 }
13719
13720 if (result)
13721 return result;
13722
13723 snprintf (buff, sizeof buff, _("<other>: %x"), other);
13724 return buff;
13725}
13726
13727static const char *
13728get_symbol_version_string (Filedata *filedata,
13729 bool is_dynsym,
13730 const char *strtab,
13731 size_t strtab_size,
13732 unsigned int si,
13733 Elf_Internal_Sym *psym,
13734 enum versioned_symbol_info *sym_info,
13735 unsigned short *vna_other)
13736{
13737 unsigned char data[2];
13738 unsigned short vers_data;
13739 uint64_t offset;
13740 unsigned short max_vd_ndx;
13741
13742 if (!is_dynsym
13743 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
13744 return NULL;
13745
13746 offset = offset_from_vma (filedata,
13747 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
13748 sizeof data + si * sizeof (vers_data));
13749
13750 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
13751 sizeof (data), 1, _("version data")) == NULL)
13752 return NULL;
13753
13754 vers_data = byte_get (data, 2);
13755
13756 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
13757 return NULL;
13758
13759 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
13760 max_vd_ndx = 0;
13761
13762 /* Usually we'd only see verdef for defined symbols, and verneed for
13763 undefined symbols. However, symbols defined by the linker in
13764 .dynbss for variables copied from a shared library in order to
13765 avoid text relocations are defined yet have verneed. We could
13766 use a heuristic to detect the special case, for example, check
13767 for verneed first on symbols defined in SHT_NOBITS sections, but
13768 it is simpler and more reliable to just look for both verdef and
13769 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
13770
13771 if (psym->st_shndx != SHN_UNDEF
13772 && vers_data != 0x8001
13773 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
13774 {
13775 Elf_Internal_Verdef ivd;
13776 Elf_Internal_Verdaux ivda;
13777 Elf_External_Verdaux evda;
13778 uint64_t off;
13779
13780 off = offset_from_vma (filedata,
13781 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
13782 sizeof (Elf_External_Verdef));
13783
13784 do
13785 {
13786 Elf_External_Verdef evd;
13787
13788 if (get_data (&evd, filedata, off, sizeof (evd), 1,
13789 _("version def")) == NULL)
13790 {
13791 ivd.vd_ndx = 0;
13792 ivd.vd_aux = 0;
13793 ivd.vd_next = 0;
13794 ivd.vd_flags = 0;
13795 }
13796 else
13797 {
13798 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
13799 ivd.vd_aux = BYTE_GET (evd.vd_aux);
13800 ivd.vd_next = BYTE_GET (evd.vd_next);
13801 ivd.vd_flags = BYTE_GET (evd.vd_flags);
13802 }
13803
13804 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
13805 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
13806
13807 off += ivd.vd_next;
13808 }
13809 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
13810
13811 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
13812 {
13813 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
13814 return NULL;
13815
13816 off -= ivd.vd_next;
13817 off += ivd.vd_aux;
13818
13819 if (get_data (&evda, filedata, off, sizeof (evda), 1,
13820 _("version def aux")) != NULL)
13821 {
13822 ivda.vda_name = BYTE_GET (evda.vda_name);
13823
13824 if (psym->st_name != ivda.vda_name)
13825 return (ivda.vda_name < strtab_size
13826 ? strtab + ivda.vda_name : _("<corrupt>"));
13827 }
13828 }
13829 }
13830
13831 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
13832 {
13833 Elf_External_Verneed evn;
13834 Elf_Internal_Verneed ivn;
13835 Elf_Internal_Vernaux ivna;
13836
13837 offset = offset_from_vma (filedata,
13838 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
13839 sizeof evn);
13840 do
13841 {
13842 uint64_t vna_off;
13843
13844 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
13845 _("version need")) == NULL)
13846 {
13847 ivna.vna_next = 0;
13848 ivna.vna_other = 0;
13849 ivna.vna_name = 0;
13850 break;
13851 }
13852
13853 ivn.vn_aux = BYTE_GET (evn.vn_aux);
13854 ivn.vn_next = BYTE_GET (evn.vn_next);
13855
13856 vna_off = offset + ivn.vn_aux;
13857
13858 do
13859 {
13860 Elf_External_Vernaux evna;
13861
13862 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
13863 _("version need aux (3)")) == NULL)
13864 {
13865 ivna.vna_next = 0;
13866 ivna.vna_other = 0;
13867 ivna.vna_name = 0;
13868 }
13869 else
13870 {
13871 ivna.vna_other = BYTE_GET (evna.vna_other);
13872 ivna.vna_next = BYTE_GET (evna.vna_next);
13873 ivna.vna_name = BYTE_GET (evna.vna_name);
13874 }
13875
13876 vna_off += ivna.vna_next;
13877 }
13878 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
13879
13880 if (ivna.vna_other == vers_data)
13881 break;
13882
13883 offset += ivn.vn_next;
13884 }
13885 while (ivn.vn_next != 0);
13886
13887 if (ivna.vna_other == vers_data)
13888 {
13889 *sym_info = symbol_undefined;
13890 *vna_other = ivna.vna_other;
13891 return (ivna.vna_name < strtab_size
13892 ? strtab + ivna.vna_name : _("<corrupt>"));
13893 }
13894 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
13895 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
13896 return _("<corrupt>");
13897 }
13898 return NULL;
13899}
13900
13901/* Display a symbol size on stdout. Format is based on --sym-base setting. */
13902
13903static unsigned int
13904print_symbol_size (uint64_t vma, int base)
13905{
13906 switch (base)
13907 {
13908 case 8:
13909 return print_vma (vma, OCTAL_5);
13910
13911 case 10:
13912 return print_vma (vma, UNSIGNED_5);
13913
13914 case 16:
13915 return print_vma (vma, PREFIX_HEX_5);
13916
13917 case 0:
13918 default:
13919 return print_vma (vma, DEC_5);
13920 }
13921}
13922
13923/* Print information on a single symbol. */
13924
13925static void
13926print_symbol (Filedata * filedata,
13927 uint64_t symbol_index,
13928 Elf_Internal_Sym * symtab,
13929 Elf_Internal_Shdr * section,
13930 char * strtab,
13931 size_t strtab_size)
13932{
13933 const char *version_string;
13934 enum versioned_symbol_info sym_info;
13935 unsigned short vna_other;
13936 const char * sstr;
13937 Elf_Internal_Sym *psym = symtab + symbol_index;
13938
13939 /* FIXME: We should have a table of field widths,
13940 rather than using hard coded constants. */
13941 printf ("%6" PRId64 ": ", symbol_index);
13942 print_vma (psym->st_value, LONG_HEX);
13943 putchar (' ');
13944 print_symbol_size (psym->st_size, sym_base);
13945 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
13946 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
13947 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
13948 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
13949 else
13950 {
13951 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
13952
13953 printf (" %-7s", get_symbol_visibility (vis));
13954
13955 /* Check to see if any other bits in the st_other field are set.
13956 FIXME: Displaying this information here disrupts the layout
13957 of the table being generated. */
13958 if (psym->st_other ^ vis)
13959 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
13960 }
13961
13962 bool is_special;
13963
13964 sstr = printable_section_name_from_index (filedata, psym->st_shndx, & is_special);
13965
13966 /* Print the symbol's section index. If the index is special
13967 then print the index's name rather than its number. */
13968 if (is_special)
13969 {
13970 int printed;
13971
13972 /* Special case: If there are no section headers, and the printable
13973 name is "<section 0x...." then just display the section number
13974 as a decimal. This happens when objcopy --strip -section-headers
13975 is used. */
13976 if (filedata->file_header.e_shnum == 0 && startswith (sstr, "<section"))
13977 printed = printf (" %4d ", psym->st_shndx);
13978 else
13979 printed = printf (" %4s ", sstr);
13980
13981 if (extra_sym_info && printed < 16)
13982 printf ("%*s", 16 - printed, "");
13983 }
13984 else
13985 {
13986 printf (" %4u ", psym->st_shndx);
13987
13988 if (extra_sym_info)
13989 {
13990 /* Display the section name referenced by the section index. */
13991 int printed = printf ("(%s) ", sstr);
13992 if (printed < 10)
13993 printf ("%*s", 10 - printed, "");
13994 }
13995 }
13996
13997 /* Get the symbol's name. For section symbols without a
13998 specific name use the (already computed) section name. */
13999 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
14000 && section_index_real (filedata, psym->st_shndx)
14001 && psym->st_name == 0)
14002 {
14003 ;
14004 }
14005 else
14006 {
14007 bool is_valid;
14008
14009 is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
14010 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
14011 }
14012
14013 version_string
14014 = get_symbol_version_string (filedata,
14015 (section == NULL
14016 || section->sh_type == SHT_DYNSYM),
14017 strtab, strtab_size, symbol_index,
14018 psym, &sym_info, &vna_other);
14019
14020 int len_avail = 21;
14021 if (! do_wide && version_string != NULL)
14022 {
14023 char buffer[16];
14024
14025 len_avail -= 1 + strlen (version_string);
14026
14027 if (sym_info == symbol_undefined)
14028 len_avail -= sprintf (buffer," (%d)", vna_other);
14029 else if (sym_info != symbol_hidden)
14030 len_avail -= 1;
14031 }
14032
14033 print_symbol_name (len_avail, sstr);
14034
14035 if (version_string)
14036 {
14037 if (sym_info == symbol_undefined)
14038 printf ("@%s (%d)", version_string, vna_other);
14039 else
14040 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
14041 version_string);
14042 }
14043
14044 putchar ('\n');
14045
14046 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
14047 && section != NULL
14048 && symbol_index >= section->sh_info
14049 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
14050 && filedata->file_header.e_machine != EM_MIPS
14051 /* Solaris binaries have been found to violate this requirement as
14052 well. Not sure if this is a bug or an ABI requirement. */
14053 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
14054 warn (_("local symbol %" PRIu64 " found at index >= %s's sh_info value of %u\n"),
14055 symbol_index, printable_section_name (filedata, section), section->sh_info);
14056}
14057
14058static const char *
14059get_lto_kind (unsigned int kind)
14060{
14061 switch (kind)
14062 {
14063 case 0: return "DEF";
14064 case 1: return "WEAKDEF";
14065 case 2: return "UNDEF";
14066 case 3: return "WEAKUNDEF";
14067 case 4: return "COMMON";
14068 default:
14069 break;
14070 }
14071
14072 static char buffer[30];
14073 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
14074 sprintf (buffer, "<unknown: %u>", kind);
14075 return buffer;
14076}
14077
14078static const char *
14079get_lto_visibility (unsigned int visibility)
14080{
14081 switch (visibility)
14082 {
14083 case 0: return "DEFAULT";
14084 case 1: return "PROTECTED";
14085 case 2: return "INTERNAL";
14086 case 3: return "HIDDEN";
14087 default:
14088 break;
14089 }
14090
14091 static char buffer[30];
14092 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
14093 sprintf (buffer, "<unknown: %u>", visibility);
14094 return buffer;
14095}
14096
14097static const char *
14098get_lto_sym_type (unsigned int sym_type)
14099{
14100 switch (sym_type)
14101 {
14102 case 0: return "UNKNOWN";
14103 case 1: return "FUNCTION";
14104 case 2: return "VARIABLE";
14105 default:
14106 break;
14107 }
14108
14109 static char buffer[30];
14110 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
14111 sprintf (buffer, "<unknown: %u>", sym_type);
14112 return buffer;
14113}
14114
14115/* Display an LTO format symbol table.
14116 FIXME: The format of LTO symbol tables is not formalized.
14117 So this code could need changing in the future. */
14118
14119static bool
14120display_lto_symtab (Filedata * filedata,
14121 Elf_Internal_Shdr * section)
14122{
14123 if (section->sh_size == 0)
14124 {
14125 if (filedata->is_separate)
14126 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
14127 printable_section_name (filedata, section),
14128 filedata->file_name);
14129 else
14130 printf (_("\nLTO Symbol table '%s' is empty!\n"),
14131 printable_section_name (filedata, section));
14132
14133 return true;
14134 }
14135
14136 if (section->sh_size > filedata->file_size)
14137 {
14138 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
14139 printable_section_name (filedata, section),
14140 section->sh_size);
14141 return false;
14142 }
14143
14144 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
14145 section->sh_size, 1, _("LTO symbols"));
14146 if (alloced_data == NULL)
14147 return false;
14148
14149 /* Look for extended data for the symbol table. */
14150 Elf_Internal_Shdr * ext = NULL;
14151 void * ext_data_orig = NULL;
14152 char * ext_data = NULL;
14153 char * ext_data_end = NULL;
14154 char * ext_name = NULL;
14155
14156 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
14157 (section_name (filedata, section)
14158 + sizeof (".gnu.lto_.symtab.") - 1)) > 0
14159 && ext_name != NULL /* Paranoia. */
14160 && (ext = find_section (filedata, ext_name)) != NULL)
14161 {
14162 if (ext->sh_size < 3)
14163 error (_("LTO Symbol extension table '%s' is empty!\n"),
14164 printable_section_name (filedata, ext));
14165 else
14166 {
14167 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
14168 ext->sh_size, 1,
14169 _("LTO ext symbol data"));
14170 if (ext_data != NULL)
14171 {
14172 ext_data_end = ext_data + ext->sh_size;
14173 if (* ext_data++ != 1)
14174 error (_("Unexpected version number in symbol extension table\n"));
14175 }
14176 }
14177 }
14178
14179 const unsigned char * data = (const unsigned char *) alloced_data;
14180 const unsigned char * end = data + section->sh_size;
14181
14182 if (filedata->is_separate)
14183 printf (_("\nIn linked file '%s': "), filedata->file_name);
14184 else
14185 printf ("\n");
14186
14187 if (ext_data_orig != NULL)
14188 {
14189 if (do_wide)
14190 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
14191 printable_section_name (filedata, section),
14192 printable_section_name (filedata, ext));
14193 else
14194 {
14195 printf (_("LTO Symbol table '%s'\n"),
14196 printable_section_name (filedata, section));
14197 printf (_(" and extension table '%s' contain:\n"),
14198 printable_section_name (filedata, ext));
14199 }
14200 }
14201 else
14202 printf (_("LTO Symbol table '%s' contains:\n"),
14203 printable_section_name (filedata, section));
14204
14205 /* FIXME: Add a wide version. */
14206 if (ext_data_orig != NULL)
14207 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
14208 else
14209 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
14210
14211 /* FIXME: We do not handle style prefixes. */
14212
14213 while (data < end)
14214 {
14215 const unsigned char * sym_name = data;
14216 data += strnlen ((const char *) sym_name, end - data) + 1;
14217 if (data >= end)
14218 goto fail;
14219
14220 const unsigned char * comdat_key = data;
14221 data += strnlen ((const char *) comdat_key, end - data) + 1;
14222 if (data >= end)
14223 goto fail;
14224
14225 if (data + 2 + 8 + 4 > end)
14226 goto fail;
14227
14228 unsigned int kind = *data++;
14229 unsigned int visibility = *data++;
14230
14231 uint64_t size = byte_get (data, 8);
14232 data += 8;
14233
14234 uint64_t slot = byte_get (data, 4);
14235 data += 4;
14236
14237 if (ext_data != NULL)
14238 {
14239 if (ext_data < (ext_data_end - 1))
14240 {
14241 unsigned int sym_type = * ext_data ++;
14242 unsigned int sec_kind = * ext_data ++;
14243
14244 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " %9s %08x _",
14245 * comdat_key == 0 ? "-" : (char *) comdat_key,
14246 get_lto_kind (kind),
14247 get_lto_visibility (visibility),
14248 size,
14249 slot,
14250 get_lto_sym_type (sym_type),
14251 sec_kind);
14252 print_symbol_name (6, (const char *) sym_name);
14253 }
14254 else
14255 {
14256 error (_("Ran out of LTO symbol extension data\n"));
14257 ext_data = NULL;
14258 /* FIXME: return FAIL result ? */
14259 }
14260 }
14261 else
14262 {
14263 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " _",
14264 * comdat_key == 0 ? "-" : (char *) comdat_key,
14265 get_lto_kind (kind),
14266 get_lto_visibility (visibility),
14267 size,
14268 slot);
14269 print_symbol_name (21, (const char *) sym_name);
14270 }
14271 putchar ('\n');
14272 }
14273
14274 if (ext_data != NULL && ext_data < ext_data_end)
14275 {
14276 error (_("Data remains in the LTO symbol extension table\n"));
14277 goto fail;
14278 }
14279
14280 free (alloced_data);
14281 free (ext_data_orig);
14282 free (ext_name);
14283 return true;
14284
14285 fail:
14286 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
14287 free (alloced_data);
14288 free (ext_data_orig);
14289 free (ext_name);
14290 return false;
14291}
14292
14293/* Display LTO symbol tables. */
14294
14295static bool
14296process_lto_symbol_tables (Filedata * filedata)
14297{
14298 Elf_Internal_Shdr * section;
14299 unsigned int i;
14300 bool res = true;
14301
14302 if (!do_lto_syms)
14303 return true;
14304
14305 if (filedata->section_headers == NULL)
14306 return true;
14307
14308 for (i = 0, section = filedata->section_headers;
14309 i < filedata->file_header.e_shnum;
14310 i++, section++)
14311 if (section_name_valid (filedata, section)
14312 && startswith (section_name (filedata, section), ".gnu.lto_.symtab."))
14313 res &= display_lto_symtab (filedata, section);
14314
14315 return res;
14316}
14317
14318static void
14319print_symbol_table_heading (void)
14320{
14321 /* FIXME: We should store the size of each field in the display in a table and
14322 then use the values inside print_symbol(), instead of that function using
14323 hard coded constants. */
14324 if (is_32bit_elf)
14325 {
14326 if (extra_sym_info)
14327 {
14328 printf (_(" Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]\n"));
14329 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |---8--| |----13.....| |........... */
14330 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 (.text) get_sections */
14331 }
14332 else if (do_wide)
14333 {
14334 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
14335 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |-4| |........... */
14336 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 get_sections */
14337 }
14338 else
14339 {
14340 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
14341 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |-4| |------------29-------------| */
14342 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 get_sections */
14343 }
14344 }
14345 else
14346 {
14347 if (extra_sym_info)
14348 {
14349 printf (_(" Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]\n"));
14350 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-------14---| |..... */
14351 /* eg: 2: 0000000000000000 0 FUNC LOCAL DEFAULT 1 (.text) .very_long_function_name */
14352
14353 }
14354 else if (do_wide)
14355 {
14356 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
14357 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-4| |........... */
14358 /* eg: 5: 0000000000000000 14 FUNC LOCAL DEFAULT 1 very_long_function_name */
14359 }
14360 else
14361 {
14362 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
14363 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-4| |--------21---------| */
14364 /* eg: 5: 0000000000000000 14 FUNC LOCAL DEFAULT 1 very_long_functi[...] */
14365 }
14366 }
14367}
14368
14369static bool
14370dump_symbol_section (Elf_Internal_Shdr * section,
14371 Filedata * filedata)
14372{
14373 if (section->sh_entsize == 0)
14374 {
14375 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
14376 printable_section_name (filedata, section));
14377 return false;
14378 }
14379
14380 uint64_t num_syms = section->sh_size / section->sh_entsize;
14381
14382 if (filedata->is_separate)
14383 printf (ngettext ("\nIn linked file '%s' symbol section '%s'"
14384 " contains %" PRIu64 " entry:\n",
14385 "\nIn linked file '%s' symbol section '%s'"
14386 " contains %" PRIu64 " entries:\n",
14387 num_syms),
14388 filedata->file_name,
14389 printable_section_name (filedata, section),
14390 num_syms);
14391 else
14392 printf (ngettext ("\nSymbol table '%s' contains %" PRIu64
14393 " entry:\n",
14394 "\nSymbol table '%s' contains %" PRIu64
14395 " entries:\n",
14396 num_syms),
14397 printable_section_name (filedata, section),
14398 num_syms);
14399
14400 print_symbol_table_heading ();
14401
14402 Elf_Internal_Sym * symtab = get_elf_symbols (filedata, section, & num_syms);
14403 if (symtab == NULL)
14404 /* An error message will have already been displayed. */
14405 return false;
14406
14407 char * strtab = NULL;
14408 uint64_t strtab_size = 0;
14409
14410 if (section->sh_link == filedata->file_header.e_shstrndx)
14411 {
14412 strtab = filedata->string_table;
14413 strtab_size = filedata->string_table_length;
14414 }
14415 else if (section->sh_link < filedata->file_header.e_shnum)
14416 {
14417 Elf_Internal_Shdr * string_sec;
14418
14419 string_sec = filedata->section_headers + section->sh_link;
14420
14421 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
14422 1, string_sec->sh_size,
14423 _("string table"));
14424 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
14425 }
14426
14427 uint64_t si;
14428
14429 for (si = 0; si < num_syms; si++)
14430 print_symbol (filedata, si, symtab, section, strtab, strtab_size);
14431
14432 free (symtab);
14433
14434 if (strtab != filedata->string_table)
14435 free (strtab);
14436
14437 return true;
14438}
14439
14440/* Dump the symbol table. */
14441
14442static bool
14443process_symbol_table (Filedata * filedata)
14444{
14445 Elf_Internal_Shdr * section;
14446
14447 if (!do_syms && !do_dyn_syms && !do_histogram)
14448 return true;
14449
14450 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
14451 && do_syms
14452 && do_using_dynamic
14453 && filedata->dynamic_strings != NULL
14454 && filedata->dynamic_symbols != NULL)
14455 {
14456 uint64_t si;
14457
14458 if (filedata->is_separate)
14459 {
14460 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table"
14461 " contains %" PRIu64 " entry:\n",
14462 "\nIn linked file '%s' the dynamic symbol table"
14463 " contains %" PRIu64 " entries:\n",
14464 filedata->num_dynamic_syms),
14465 filedata->file_name,
14466 filedata->num_dynamic_syms);
14467 }
14468 else
14469 {
14470 printf (ngettext ("\nSymbol table for image contains %" PRIu64
14471 " entry:\n",
14472 "\nSymbol table for image contains %" PRIu64
14473 " entries:\n",
14474 filedata->num_dynamic_syms),
14475 filedata->num_dynamic_syms);
14476 }
14477
14478 print_symbol_table_heading ();
14479
14480 for (si = 0; si < filedata->num_dynamic_syms; si++)
14481 print_symbol (filedata, si, filedata->dynamic_symbols, NULL,
14482 filedata->dynamic_strings,
14483 filedata->dynamic_strings_length);
14484 }
14485 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
14486 && filedata->section_headers != NULL)
14487 {
14488 unsigned int i;
14489
14490 for (i = 0, section = filedata->section_headers;
14491 i < filedata->file_header.e_shnum;
14492 i++, section++)
14493 {
14494 if ((section->sh_type != SHT_SYMTAB
14495 && section->sh_type != SHT_DYNSYM)
14496 || (!do_syms
14497 && section->sh_type == SHT_SYMTAB))
14498 continue;
14499
14500 dump_symbol_section (section, filedata);
14501 }
14502 }
14503 else if (do_syms)
14504 printf
14505 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
14506
14507 if (do_histogram && filedata->buckets != NULL)
14508 {
14509 uint64_t *lengths;
14510 uint64_t *counts;
14511 uint64_t hn;
14512 uint64_t si;
14513 uint64_t maxlength = 0;
14514 uint64_t nzero_counts = 0;
14515 uint64_t nsyms = 0;
14516 char *visited;
14517
14518 printf (ngettext ("\nHistogram for bucket list length "
14519 "(total of %" PRIu64 " bucket):\n",
14520 "\nHistogram for bucket list length "
14521 "(total of %" PRIu64 " buckets):\n",
14522 filedata->nbuckets),
14523 filedata->nbuckets);
14524
14525 lengths = calloc (filedata->nbuckets, sizeof (*lengths));
14526 if (lengths == NULL)
14527 {
14528 error (_("Out of memory allocating space for histogram buckets\n"));
14529 goto err_out;
14530 }
14531 visited = xcmalloc (filedata->nchains, 1);
14532 memset (visited, 0, filedata->nchains);
14533
14534 printf (_(" Length Number %% of total Coverage\n"));
14535 for (hn = 0; hn < filedata->nbuckets; ++hn)
14536 {
14537 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
14538 {
14539 ++nsyms;
14540 if (maxlength < ++lengths[hn])
14541 ++maxlength;
14542 if (si >= filedata->nchains || visited[si])
14543 {
14544 error (_("histogram chain is corrupt\n"));
14545 break;
14546 }
14547 visited[si] = 1;
14548 }
14549 }
14550 free (visited);
14551
14552 counts = calloc (maxlength + 1, sizeof (*counts));
14553 if (counts == NULL)
14554 {
14555 free (lengths);
14556 error (_("Out of memory allocating space for histogram counts\n"));
14557 goto err_out;
14558 }
14559
14560 for (hn = 0; hn < filedata->nbuckets; ++hn)
14561 ++counts[lengths[hn]];
14562
14563 if (filedata->nbuckets > 0)
14564 {
14565 uint64_t i;
14566 printf (" 0 %-10" PRIu64 " (%5.1f%%)\n",
14567 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
14568 for (i = 1; i <= maxlength; ++i)
14569 {
14570 nzero_counts += counts[i] * i;
14571 printf ("%7" PRIu64 " %-10" PRIu64 " (%5.1f%%) %5.1f%%\n",
14572 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
14573 (nzero_counts * 100.0) / nsyms);
14574 }
14575 }
14576
14577 free (counts);
14578 free (lengths);
14579 }
14580
14581 free (filedata->buckets);
14582 filedata->buckets = NULL;
14583 filedata->nbuckets = 0;
14584 free (filedata->chains);
14585 filedata->chains = NULL;
14586
14587 if (do_histogram && filedata->gnubuckets != NULL)
14588 {
14589 uint64_t *lengths;
14590 uint64_t *counts;
14591 uint64_t hn;
14592 uint64_t maxlength = 0;
14593 uint64_t nzero_counts = 0;
14594 uint64_t nsyms = 0;
14595
14596 printf (ngettext ("\nHistogram for `%s' bucket list length "
14597 "(total of %" PRIu64 " bucket):\n",
14598 "\nHistogram for `%s' bucket list length "
14599 "(total of %" PRIu64 " buckets):\n",
14600 filedata->ngnubuckets),
14601 GNU_HASH_SECTION_NAME (filedata),
14602 filedata->ngnubuckets);
14603
14604 lengths = calloc (filedata->ngnubuckets, sizeof (*lengths));
14605 if (lengths == NULL)
14606 {
14607 error (_("Out of memory allocating space for gnu histogram buckets\n"));
14608 goto err_out;
14609 }
14610
14611 printf (_(" Length Number %% of total Coverage\n"));
14612
14613 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
14614 if (filedata->gnubuckets[hn] != 0)
14615 {
14616 uint64_t off, length = 1;
14617
14618 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
14619 /* PR 17531 file: 010-77222-0.004. */
14620 off < filedata->ngnuchains
14621 && (filedata->gnuchains[off] & 1) == 0;
14622 ++off)
14623 ++length;
14624 lengths[hn] = length;
14625 if (length > maxlength)
14626 maxlength = length;
14627 nsyms += length;
14628 }
14629
14630 counts = calloc (maxlength + 1, sizeof (*counts));
14631 if (counts == NULL)
14632 {
14633 free (lengths);
14634 error (_("Out of memory allocating space for gnu histogram counts\n"));
14635 goto err_out;
14636 }
14637
14638 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
14639 ++counts[lengths[hn]];
14640
14641 if (filedata->ngnubuckets > 0)
14642 {
14643 uint64_t j;
14644 printf (" 0 %-10" PRIu64 " (%5.1f%%)\n",
14645 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
14646 for (j = 1; j <= maxlength; ++j)
14647 {
14648 nzero_counts += counts[j] * j;
14649 printf ("%7" PRIu64 " %-10" PRIu64 " (%5.1f%%) %5.1f%%\n",
14650 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
14651 (nzero_counts * 100.0) / nsyms);
14652 }
14653 }
14654
14655 free (counts);
14656 free (lengths);
14657 }
14658 free (filedata->gnubuckets);
14659 filedata->gnubuckets = NULL;
14660 filedata->ngnubuckets = 0;
14661 free (filedata->gnuchains);
14662 filedata->gnuchains = NULL;
14663 filedata->ngnuchains = 0;
14664 free (filedata->mipsxlat);
14665 filedata->mipsxlat = NULL;
14666 return true;
14667
14668 err_out:
14669 free (filedata->gnubuckets);
14670 filedata->gnubuckets = NULL;
14671 filedata->ngnubuckets = 0;
14672 free (filedata->gnuchains);
14673 filedata->gnuchains = NULL;
14674 filedata->ngnuchains = 0;
14675 free (filedata->mipsxlat);
14676 filedata->mipsxlat = NULL;
14677 free (filedata->buckets);
14678 filedata->buckets = NULL;
14679 filedata->nbuckets = 0;
14680 free (filedata->chains);
14681 filedata->chains = NULL;
14682 return false;
14683}
14684
14685static bool
14686process_syminfo (Filedata * filedata)
14687{
14688 unsigned int i;
14689
14690 if (filedata->dynamic_syminfo == NULL
14691 || !do_dynamic)
14692 /* No syminfo, this is ok. */
14693 return true;
14694
14695 /* There better should be a dynamic symbol section. */
14696 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
14697 return false;
14698
14699 if (filedata->is_separate)
14700 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset %#" PRIx64 " contains %d entry:\n",
14701 "\nIn linked file '%s: the dynamic info segment at offset %#" PRIx64 " contains %d entries:\n",
14702 filedata->dynamic_syminfo_nent),
14703 filedata->file_name,
14704 filedata->dynamic_syminfo_offset,
14705 filedata->dynamic_syminfo_nent);
14706 else
14707 printf (ngettext ("\nDynamic info segment at offset %#" PRIx64
14708 " contains %d entry:\n",
14709 "\nDynamic info segment at offset %#" PRIx64
14710 " contains %d entries:\n",
14711 filedata->dynamic_syminfo_nent),
14712 filedata->dynamic_syminfo_offset,
14713 filedata->dynamic_syminfo_nent);
14714
14715 printf (_(" Num: Name BoundTo Flags\n"));
14716 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
14717 {
14718 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
14719
14720 printf ("%4d: ", i);
14721 if (i >= filedata->num_dynamic_syms)
14722 printf (_("<corrupt index>"));
14723 else if (valid_dynamic_name (filedata, filedata->dynamic_symbols[i].st_name))
14724 print_symbol_name (30, get_dynamic_name (filedata,
14725 filedata->dynamic_symbols[i].st_name));
14726 else
14727 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
14728 putchar (' ');
14729
14730 switch (filedata->dynamic_syminfo[i].si_boundto)
14731 {
14732 case SYMINFO_BT_SELF:
14733 fputs ("SELF ", stdout);
14734 break;
14735 case SYMINFO_BT_PARENT:
14736 fputs ("PARENT ", stdout);
14737 break;
14738 default:
14739 if (filedata->dynamic_syminfo[i].si_boundto > 0
14740 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
14741 && valid_dynamic_name (filedata,
14742 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
14743 {
14744 print_symbol_name (10, get_dynamic_name (filedata,
14745 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
14746 putchar (' ' );
14747 }
14748 else
14749 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
14750 break;
14751 }
14752
14753 if (flags & SYMINFO_FLG_DIRECT)
14754 printf (" DIRECT");
14755 if (flags & SYMINFO_FLG_PASSTHRU)
14756 printf (" PASSTHRU");
14757 if (flags & SYMINFO_FLG_COPY)
14758 printf (" COPY");
14759 if (flags & SYMINFO_FLG_LAZYLOAD)
14760 printf (" LAZYLOAD");
14761
14762 puts ("");
14763 }
14764
14765 return true;
14766}
14767
14768/* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
14769 is contained by the region START .. END. The types of ADDR, START
14770 and END should all be the same. Note both ADDR + NELEM and END
14771 point to just beyond the end of the regions that are being tested. */
14772#define IN_RANGE(START,END,ADDR,NELEM) \
14773 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
14774
14775/* Check to see if the given reloc needs to be handled in a target specific
14776 manner. If so then process the reloc and return TRUE otherwise return
14777 FALSE.
14778
14779 If called with reloc == NULL, then this is a signal that reloc processing
14780 for the current section has finished, and any saved state should be
14781 discarded. */
14782
14783static bool
14784target_specific_reloc_handling (Filedata *filedata,
14785 Elf_Internal_Rela *reloc,
14786 unsigned char *start,
14787 unsigned char *end,
14788 Elf_Internal_Sym *symtab,
14789 uint64_t num_syms)
14790{
14791 unsigned int reloc_type = 0;
14792 uint64_t sym_index = 0;
14793
14794 if (reloc)
14795 {
14796 reloc_type = get_reloc_type (filedata, reloc->r_info);
14797 sym_index = get_reloc_symindex (reloc->r_info);
14798 }
14799
14800 switch (filedata->file_header.e_machine)
14801 {
14802 case EM_LOONGARCH:
14803 {
14804 switch (reloc_type)
14805 {
14806 /* For .uleb128 .LFE1-.LFB1, loongarch write 0 to object file
14807 at assembly time. */
14808 case 107: /* R_LARCH_ADD_ULEB128. */
14809 case 108: /* R_LARCH_SUB_ULEB128. */
14810 {
14811 uint64_t value = 0;
14812 unsigned int reloc_size = 0;
14813 int leb_ret = 0;
14814
14815 if (reloc->r_offset < (size_t) (end - start))
14816 value = read_leb128 (start + reloc->r_offset, end, false,
14817 &reloc_size, &leb_ret);
14818 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
14819 error (_("LoongArch ULEB128 field at 0x%lx contains invalid "
14820 "ULEB128 value\n"),
14821 (long) reloc->r_offset);
14822
14823 else if (sym_index >= num_syms)
14824 error (_("%s reloc contains invalid symbol index "
14825 "%" PRIu64 "\n"),
14826 (reloc_type == 107
14827 ? "R_LARCH_ADD_ULEB128"
14828 : "R_LARCH_SUB_ULEB128"),
14829 sym_index);
14830 else
14831 {
14832 if (reloc_type == 107)
14833 value += reloc->r_addend + symtab[sym_index].st_value;
14834 else
14835 value -= reloc->r_addend + symtab[sym_index].st_value;
14836
14837 /* Write uleb128 value to p. */
14838 bfd_byte *p = start + reloc->r_offset;
14839 do
14840 {
14841 bfd_byte c = value & 0x7f;
14842 value >>= 7;
14843 if (--reloc_size != 0)
14844 c |= 0x80;
14845 *p++ = c;
14846 }
14847 while (reloc_size);
14848 }
14849
14850 return true;
14851 }
14852 }
14853 break;
14854 }
14855
14856 case EM_MSP430:
14857 case EM_MSP430_OLD:
14858 {
14859 static Elf_Internal_Sym * saved_sym = NULL;
14860
14861 if (reloc == NULL)
14862 {
14863 saved_sym = NULL;
14864 return true;
14865 }
14866
14867 switch (reloc_type)
14868 {
14869 case 10: /* R_MSP430_SYM_DIFF */
14870 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
14871 if (uses_msp430x_relocs (filedata))
14872 break;
14873 /* Fall through. */
14874 case 21: /* R_MSP430X_SYM_DIFF */
14875 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
14876 /* PR 21139. */
14877 if (sym_index >= num_syms)
14878 error (_("%s reloc contains invalid symbol index "
14879 "%" PRIu64 "\n"), "MSP430 SYM_DIFF", sym_index);
14880 else
14881 saved_sym = symtab + sym_index;
14882 return true;
14883
14884 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
14885 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
14886 goto handle_sym_diff;
14887
14888 case 5: /* R_MSP430_16_BYTE */
14889 case 9: /* R_MSP430_8 */
14890 case 11: /* R_MSP430_GNU_SET_ULEB128 */
14891 if (uses_msp430x_relocs (filedata))
14892 break;
14893 goto handle_sym_diff;
14894
14895 case 2: /* R_MSP430_ABS16 */
14896 case 15: /* R_MSP430X_ABS16 */
14897 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
14898 if (! uses_msp430x_relocs (filedata))
14899 break;
14900 goto handle_sym_diff;
14901
14902 handle_sym_diff:
14903 if (saved_sym != NULL)
14904 {
14905 uint64_t value;
14906 unsigned int reloc_size = 0;
14907 int leb_ret = 0;
14908 switch (reloc_type)
14909 {
14910 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
14911 reloc_size = 4;
14912 break;
14913 case 11: /* R_MSP430_GNU_SET_ULEB128 */
14914 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
14915 if (reloc->r_offset < (size_t) (end - start))
14916 read_leb128 (start + reloc->r_offset, end, false,
14917 &reloc_size, &leb_ret);
14918 break;
14919 default:
14920 reloc_size = 2;
14921 break;
14922 }
14923
14924 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
14925 error (_("MSP430 ULEB128 field at %#" PRIx64
14926 " contains invalid ULEB128 value\n"),
14927 reloc->r_offset);
14928 else if (sym_index >= num_syms)
14929 error (_("%s reloc contains invalid symbol index "
14930 "%" PRIu64 "\n"), "MSP430", sym_index);
14931 else
14932 {
14933 value = reloc->r_addend + (symtab[sym_index].st_value
14934 - saved_sym->st_value);
14935
14936 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14937 byte_put (start + reloc->r_offset, value, reloc_size);
14938 else
14939 /* PR 21137 */
14940 error (_("MSP430 sym diff reloc contains invalid offset: "
14941 "%#" PRIx64 "\n"),
14942 reloc->r_offset);
14943 }
14944
14945 saved_sym = NULL;
14946 return true;
14947 }
14948 break;
14949
14950 default:
14951 if (saved_sym != NULL)
14952 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
14953 break;
14954 }
14955 break;
14956 }
14957
14958 case EM_MN10300:
14959 case EM_CYGNUS_MN10300:
14960 {
14961 static Elf_Internal_Sym * saved_sym = NULL;
14962
14963 if (reloc == NULL)
14964 {
14965 saved_sym = NULL;
14966 return true;
14967 }
14968
14969 switch (reloc_type)
14970 {
14971 case 34: /* R_MN10300_ALIGN */
14972 return true;
14973 case 33: /* R_MN10300_SYM_DIFF */
14974 if (sym_index >= num_syms)
14975 error (_("%s reloc contains invalid symbol index "
14976 "%" PRIu64 "\n"), "MN10300_SYM_DIFF", sym_index);
14977 else
14978 saved_sym = symtab + sym_index;
14979 return true;
14980
14981 case 1: /* R_MN10300_32 */
14982 case 2: /* R_MN10300_16 */
14983 if (saved_sym != NULL)
14984 {
14985 int reloc_size = reloc_type == 1 ? 4 : 2;
14986 uint64_t value;
14987
14988 if (sym_index >= num_syms)
14989 error (_("%s reloc contains invalid symbol index "
14990 "%" PRIu64 "\n"), "MN10300", sym_index);
14991 else
14992 {
14993 value = reloc->r_addend + (symtab[sym_index].st_value
14994 - saved_sym->st_value);
14995
14996 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14997 byte_put (start + reloc->r_offset, value, reloc_size);
14998 else
14999 error (_("MN10300 sym diff reloc contains invalid offset:"
15000 " %#" PRIx64 "\n"),
15001 reloc->r_offset);
15002 }
15003
15004 saved_sym = NULL;
15005 return true;
15006 }
15007 break;
15008 default:
15009 if (saved_sym != NULL)
15010 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
15011 break;
15012 }
15013 break;
15014 }
15015
15016 case EM_RL78:
15017 {
15018 static uint64_t saved_sym1 = 0;
15019 static uint64_t saved_sym2 = 0;
15020 static uint64_t value;
15021
15022 if (reloc == NULL)
15023 {
15024 saved_sym1 = saved_sym2 = 0;
15025 return true;
15026 }
15027
15028 switch (reloc_type)
15029 {
15030 case 0x80: /* R_RL78_SYM. */
15031 saved_sym1 = saved_sym2;
15032 if (sym_index >= num_syms)
15033 error (_("%s reloc contains invalid symbol index "
15034 "%" PRIu64 "\n"), "RL78_SYM", sym_index);
15035 else
15036 {
15037 saved_sym2 = symtab[sym_index].st_value;
15038 saved_sym2 += reloc->r_addend;
15039 }
15040 return true;
15041
15042 case 0x83: /* R_RL78_OPsub. */
15043 value = saved_sym1 - saved_sym2;
15044 saved_sym2 = saved_sym1 = 0;
15045 return true;
15046 break;
15047
15048 case 0x41: /* R_RL78_ABS32. */
15049 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
15050 byte_put (start + reloc->r_offset, value, 4);
15051 else
15052 error (_("RL78 sym diff reloc contains invalid offset: "
15053 "%#" PRIx64 "\n"),
15054 reloc->r_offset);
15055 value = 0;
15056 return true;
15057
15058 case 0x43: /* R_RL78_ABS16. */
15059 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
15060 byte_put (start + reloc->r_offset, value, 2);
15061 else
15062 error (_("RL78 sym diff reloc contains invalid offset: "
15063 "%#" PRIx64 "\n"),
15064 reloc->r_offset);
15065 value = 0;
15066 return true;
15067
15068 default:
15069 break;
15070 }
15071 break;
15072 }
15073 }
15074
15075 return false;
15076}
15077
15078/* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
15079 DWARF debug sections. This is a target specific test. Note - we do not
15080 go through the whole including-target-headers-multiple-times route, (as
15081 we have already done with <elf/h8.h>) because this would become very
15082 messy and even then this function would have to contain target specific
15083 information (the names of the relocs instead of their numeric values).
15084 FIXME: This is not the correct way to solve this problem. The proper way
15085 is to have target specific reloc sizing and typing functions created by
15086 the reloc-macros.h header, in the same way that it already creates the
15087 reloc naming functions. */
15088
15089static bool
15090is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15091{
15092 /* Please keep this table alpha-sorted for ease of visual lookup. */
15093 switch (filedata->file_header.e_machine)
15094 {
15095 case EM_386:
15096 case EM_IAMCU:
15097 return reloc_type == 1; /* R_386_32. */
15098 case EM_68K:
15099 return reloc_type == 1; /* R_68K_32. */
15100 case EM_860:
15101 return reloc_type == 1; /* R_860_32. */
15102 case EM_960:
15103 return reloc_type == 2; /* R_960_32. */
15104 case EM_AARCH64:
15105 return (reloc_type == 258
15106 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
15107 case EM_BPF:
15108 return reloc_type == 11; /* R_BPF_DATA_32 */
15109 case EM_ADAPTEVA_EPIPHANY:
15110 return reloc_type == 3;
15111 case EM_ALPHA:
15112 return reloc_type == 1; /* R_ALPHA_REFLONG. */
15113 case EM_ARC:
15114 return reloc_type == 1; /* R_ARC_32. */
15115 case EM_ARC_COMPACT:
15116 case EM_ARC_COMPACT2:
15117 case EM_ARC_COMPACT3:
15118 case EM_ARC_COMPACT3_64:
15119 return reloc_type == 4; /* R_ARC_32. */
15120 case EM_ARM:
15121 return reloc_type == 2; /* R_ARM_ABS32 */
15122 case EM_AVR_OLD:
15123 case EM_AVR:
15124 return reloc_type == 1;
15125 case EM_BLACKFIN:
15126 return reloc_type == 0x12; /* R_byte4_data. */
15127 case EM_CRIS:
15128 return reloc_type == 3; /* R_CRIS_32. */
15129 case EM_CR16:
15130 return reloc_type == 3; /* R_CR16_NUM32. */
15131 case EM_CRX:
15132 return reloc_type == 15; /* R_CRX_NUM32. */
15133 case EM_CSKY:
15134 return reloc_type == 1; /* R_CKCORE_ADDR32. */
15135 case EM_CYGNUS_FRV:
15136 return reloc_type == 1;
15137 case EM_CYGNUS_D10V:
15138 case EM_D10V:
15139 return reloc_type == 6; /* R_D10V_32. */
15140 case EM_CYGNUS_D30V:
15141 case EM_D30V:
15142 return reloc_type == 12; /* R_D30V_32_NORMAL. */
15143 case EM_DLX:
15144 return reloc_type == 3; /* R_DLX_RELOC_32. */
15145 case EM_CYGNUS_FR30:
15146 case EM_FR30:
15147 return reloc_type == 3; /* R_FR30_32. */
15148 case EM_FT32:
15149 return reloc_type == 1; /* R_FT32_32. */
15150 case EM_H8S:
15151 case EM_H8_300:
15152 case EM_H8_300H:
15153 return reloc_type == 1; /* R_H8_DIR32. */
15154 case EM_IA_64:
15155 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
15156 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
15157 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
15158 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
15159 case EM_IP2K_OLD:
15160 case EM_IP2K:
15161 return reloc_type == 2; /* R_IP2K_32. */
15162 case EM_IQ2000:
15163 return reloc_type == 2; /* R_IQ2000_32. */
15164 case EM_KVX:
15165 return reloc_type == 2; /* R_KVX_32. */
15166 case EM_LATTICEMICO32:
15167 return reloc_type == 3; /* R_LM32_32. */
15168 case EM_LOONGARCH:
15169 return reloc_type == 1; /* R_LARCH_32. */
15170 case EM_M32C_OLD:
15171 case EM_M32C:
15172 return reloc_type == 3; /* R_M32C_32. */
15173 case EM_M32R:
15174 return reloc_type == 34; /* R_M32R_32_RELA. */
15175 case EM_68HC11:
15176 case EM_68HC12:
15177 return reloc_type == 6; /* R_M68HC11_32. */
15178 case EM_S12Z:
15179 return reloc_type == 7 || /* R_S12Z_EXT32 */
15180 reloc_type == 6; /* R_S12Z_CW32. */
15181 case EM_MCORE:
15182 return reloc_type == 1; /* R_MCORE_ADDR32. */
15183 case EM_CYGNUS_MEP:
15184 return reloc_type == 4; /* R_MEP_32. */
15185 case EM_METAG:
15186 return reloc_type == 2; /* R_METAG_ADDR32. */
15187 case EM_MICROBLAZE:
15188 return reloc_type == 1; /* R_MICROBLAZE_32. */
15189 case EM_MIPS:
15190 return reloc_type == 2; /* R_MIPS_32. */
15191 case EM_MMIX:
15192 return reloc_type == 4; /* R_MMIX_32. */
15193 case EM_CYGNUS_MN10200:
15194 case EM_MN10200:
15195 return reloc_type == 1; /* R_MN10200_32. */
15196 case EM_CYGNUS_MN10300:
15197 case EM_MN10300:
15198 return reloc_type == 1; /* R_MN10300_32. */
15199 case EM_MOXIE:
15200 return reloc_type == 1; /* R_MOXIE_32. */
15201 case EM_MSP430_OLD:
15202 case EM_MSP430:
15203 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
15204 case EM_MT:
15205 return reloc_type == 2; /* R_MT_32. */
15206 case EM_NDS32:
15207 return reloc_type == 20; /* R_NDS32_32_RELA. */
15208 case EM_ALTERA_NIOS2:
15209 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
15210 case EM_NIOS32:
15211 return reloc_type == 1; /* R_NIOS_32. */
15212 case EM_OR1K:
15213 return reloc_type == 1; /* R_OR1K_32. */
15214 case EM_PARISC:
15215 return (reloc_type == 1 /* R_PARISC_DIR32. */
15216 || reloc_type == 2 /* R_PARISC_DIR21L. */
15217 || reloc_type == 41); /* R_PARISC_SECREL32. */
15218 case EM_PJ:
15219 case EM_PJ_OLD:
15220 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
15221 case EM_PPC64:
15222 return reloc_type == 1; /* R_PPC64_ADDR32. */
15223 case EM_PPC:
15224 return reloc_type == 1; /* R_PPC_ADDR32. */
15225 case EM_TI_PRU:
15226 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
15227 case EM_RISCV:
15228 return reloc_type == 1; /* R_RISCV_32. */
15229 case EM_RL78:
15230 return reloc_type == 1; /* R_RL78_DIR32. */
15231 case EM_RX:
15232 return reloc_type == 1; /* R_RX_DIR32. */
15233 case EM_S370:
15234 return reloc_type == 1; /* R_I370_ADDR31. */
15235 case EM_S390_OLD:
15236 case EM_S390:
15237 return reloc_type == 4; /* R_S390_32. */
15238 case EM_SCORE:
15239 return reloc_type == 8; /* R_SCORE_ABS32. */
15240 case EM_SH:
15241 return reloc_type == 1; /* R_SH_DIR32. */
15242 case EM_SPARC32PLUS:
15243 case EM_SPARCV9:
15244 case EM_SPARC:
15245 return reloc_type == 3 /* R_SPARC_32. */
15246 || reloc_type == 23; /* R_SPARC_UA32. */
15247 case EM_SPU:
15248 return reloc_type == 6; /* R_SPU_ADDR32 */
15249 case EM_TI_C6000:
15250 return reloc_type == 1; /* R_C6000_ABS32. */
15251 case EM_TILEGX:
15252 return reloc_type == 2; /* R_TILEGX_32. */
15253 case EM_TILEPRO:
15254 return reloc_type == 1; /* R_TILEPRO_32. */
15255 case EM_CYGNUS_V850:
15256 case EM_V850:
15257 return reloc_type == 6; /* R_V850_ABS32. */
15258 case EM_V800:
15259 return reloc_type == 0x33; /* R_V810_WORD. */
15260 case EM_VAX:
15261 return reloc_type == 1; /* R_VAX_32. */
15262 case EM_VISIUM:
15263 return reloc_type == 3; /* R_VISIUM_32. */
15264 case EM_WEBASSEMBLY:
15265 return reloc_type == 1; /* R_WASM32_32. */
15266 case EM_X86_64:
15267 case EM_L1OM:
15268 case EM_K1OM:
15269 return reloc_type == 10; /* R_X86_64_32. */
15270 case EM_XGATE:
15271 return reloc_type == 4; /* R_XGATE_32. */
15272 case EM_XSTORMY16:
15273 return reloc_type == 1; /* R_XSTROMY16_32. */
15274 case EM_XTENSA_OLD:
15275 case EM_XTENSA:
15276 return reloc_type == 1; /* R_XTENSA_32. */
15277 case EM_Z80:
15278 return reloc_type == 6; /* R_Z80_32. */
15279 default:
15280 {
15281 static unsigned int prev_warn = 0;
15282
15283 /* Avoid repeating the same warning multiple times. */
15284 if (prev_warn != filedata->file_header.e_machine)
15285 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
15286 filedata->file_header.e_machine);
15287 prev_warn = filedata->file_header.e_machine;
15288 return false;
15289 }
15290 }
15291}
15292
15293/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15294 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
15295
15296static bool
15297is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
15298{
15299 switch (filedata->file_header.e_machine)
15300 /* Please keep this table alpha-sorted for ease of visual lookup. */
15301 {
15302 case EM_386:
15303 case EM_IAMCU:
15304 return reloc_type == 2; /* R_386_PC32. */
15305 case EM_68K:
15306 return reloc_type == 4; /* R_68K_PC32. */
15307 case EM_AARCH64:
15308 return reloc_type == 261; /* R_AARCH64_PREL32 */
15309 case EM_ADAPTEVA_EPIPHANY:
15310 return reloc_type == 6;
15311 case EM_ALPHA:
15312 return reloc_type == 10; /* R_ALPHA_SREL32. */
15313 case EM_ARC_COMPACT:
15314 case EM_ARC_COMPACT2:
15315 case EM_ARC_COMPACT3:
15316 case EM_ARC_COMPACT3_64:
15317 return reloc_type == 49; /* R_ARC_32_PCREL. */
15318 case EM_ARM:
15319 return reloc_type == 3; /* R_ARM_REL32 */
15320 case EM_AVR_OLD:
15321 case EM_AVR:
15322 return reloc_type == 36; /* R_AVR_32_PCREL. */
15323 case EM_LOONGARCH:
15324 return reloc_type == 99; /* R_LARCH_32_PCREL. */
15325 case EM_MICROBLAZE:
15326 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
15327 case EM_OR1K:
15328 return reloc_type == 9; /* R_OR1K_32_PCREL. */
15329 case EM_PARISC:
15330 return reloc_type == 9; /* R_PARISC_PCREL32. */
15331 case EM_PPC:
15332 return reloc_type == 26; /* R_PPC_REL32. */
15333 case EM_PPC64:
15334 return reloc_type == 26; /* R_PPC64_REL32. */
15335 case EM_RISCV:
15336 return reloc_type == 57; /* R_RISCV_32_PCREL. */
15337 case EM_S390_OLD:
15338 case EM_S390:
15339 return reloc_type == 5; /* R_390_PC32. */
15340 case EM_SH:
15341 return reloc_type == 2; /* R_SH_REL32. */
15342 case EM_SPARC32PLUS:
15343 case EM_SPARCV9:
15344 case EM_SPARC:
15345 return reloc_type == 6; /* R_SPARC_DISP32. */
15346 case EM_SPU:
15347 return reloc_type == 13; /* R_SPU_REL32. */
15348 case EM_TILEGX:
15349 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
15350 case EM_TILEPRO:
15351 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
15352 case EM_VISIUM:
15353 return reloc_type == 6; /* R_VISIUM_32_PCREL */
15354 case EM_X86_64:
15355 case EM_L1OM:
15356 case EM_K1OM:
15357 return reloc_type == 2; /* R_X86_64_PC32. */
15358 case EM_VAX:
15359 return reloc_type == 4; /* R_VAX_PCREL32. */
15360 case EM_XTENSA_OLD:
15361 case EM_XTENSA:
15362 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
15363 case EM_KVX:
15364 return reloc_type == 7; /* R_KVX_32_PCREL */
15365 default:
15366 /* Do not abort or issue an error message here. Not all targets use
15367 pc-relative 32-bit relocs in their DWARF debug information and we
15368 have already tested for target coverage in is_32bit_abs_reloc. A
15369 more helpful warning message will be generated by apply_relocations
15370 anyway, so just return. */
15371 return false;
15372 }
15373}
15374
15375/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15376 a 64-bit absolute RELA relocation used in DWARF debug sections. */
15377
15378static bool
15379is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15380{
15381 switch (filedata->file_header.e_machine)
15382 {
15383 case EM_AARCH64:
15384 return reloc_type == 257; /* R_AARCH64_ABS64. */
15385 case EM_ARC_COMPACT3_64:
15386 return reloc_type == 5; /* R_ARC_64. */
15387 case EM_ALPHA:
15388 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
15389 case EM_IA_64:
15390 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
15391 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
15392 case EM_LOONGARCH:
15393 return reloc_type == 2; /* R_LARCH_64 */
15394 case EM_PARISC:
15395 return reloc_type == 80; /* R_PARISC_DIR64. */
15396 case EM_PPC64:
15397 return reloc_type == 38; /* R_PPC64_ADDR64. */
15398 case EM_RISCV:
15399 return reloc_type == 2; /* R_RISCV_64. */
15400 case EM_SPARC32PLUS:
15401 case EM_SPARCV9:
15402 case EM_SPARC:
15403 return reloc_type == 32 /* R_SPARC_64. */
15404 || reloc_type == 54; /* R_SPARC_UA64. */
15405 case EM_X86_64:
15406 case EM_L1OM:
15407 case EM_K1OM:
15408 return reloc_type == 1; /* R_X86_64_64. */
15409 case EM_S390_OLD:
15410 case EM_S390:
15411 return reloc_type == 22; /* R_S390_64. */
15412 case EM_TILEGX:
15413 return reloc_type == 1; /* R_TILEGX_64. */
15414 case EM_MIPS:
15415 return reloc_type == 18; /* R_MIPS_64. */
15416 case EM_KVX:
15417 return reloc_type == 3; /* R_KVX_64 */
15418 default:
15419 return false;
15420 }
15421}
15422
15423/* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
15424 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
15425
15426static bool
15427is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
15428{
15429 switch (filedata->file_header.e_machine)
15430 {
15431 case EM_AARCH64:
15432 return reloc_type == 260; /* R_AARCH64_PREL64. */
15433 case EM_ALPHA:
15434 return reloc_type == 11; /* R_ALPHA_SREL64. */
15435 case EM_IA_64:
15436 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
15437 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
15438 case EM_PARISC:
15439 return reloc_type == 72; /* R_PARISC_PCREL64. */
15440 case EM_PPC64:
15441 return reloc_type == 44; /* R_PPC64_REL64. */
15442 case EM_SPARC32PLUS:
15443 case EM_SPARCV9:
15444 case EM_SPARC:
15445 return reloc_type == 46; /* R_SPARC_DISP64. */
15446 case EM_X86_64:
15447 case EM_L1OM:
15448 case EM_K1OM:
15449 return reloc_type == 24; /* R_X86_64_PC64. */
15450 case EM_S390_OLD:
15451 case EM_S390:
15452 return reloc_type == 23; /* R_S390_PC64. */
15453 case EM_TILEGX:
15454 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
15455 default:
15456 return false;
15457 }
15458}
15459
15460/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15461 a 24-bit absolute RELA relocation used in DWARF debug sections. */
15462
15463static bool
15464is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15465{
15466 switch (filedata->file_header.e_machine)
15467 {
15468 case EM_CYGNUS_MN10200:
15469 case EM_MN10200:
15470 return reloc_type == 4; /* R_MN10200_24. */
15471 case EM_FT32:
15472 return reloc_type == 5; /* R_FT32_20. */
15473 case EM_Z80:
15474 return reloc_type == 5; /* R_Z80_24. */
15475 default:
15476 return false;
15477 }
15478}
15479
15480/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15481 a 16-bit absolute RELA relocation used in DWARF debug sections. */
15482
15483static bool
15484is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15485{
15486 /* Please keep this table alpha-sorted for ease of visual lookup. */
15487 switch (filedata->file_header.e_machine)
15488 {
15489 case EM_ARC:
15490 case EM_ARC_COMPACT:
15491 case EM_ARC_COMPACT2:
15492 case EM_ARC_COMPACT3:
15493 case EM_ARC_COMPACT3_64:
15494 return reloc_type == 2; /* R_ARC_16. */
15495 case EM_ADAPTEVA_EPIPHANY:
15496 return reloc_type == 5;
15497 case EM_AVR_OLD:
15498 case EM_AVR:
15499 return reloc_type == 4; /* R_AVR_16. */
15500 case EM_CYGNUS_D10V:
15501 case EM_D10V:
15502 return reloc_type == 3; /* R_D10V_16. */
15503 case EM_FT32:
15504 return reloc_type == 2; /* R_FT32_16. */
15505 case EM_H8S:
15506 case EM_H8_300:
15507 case EM_H8_300H:
15508 return reloc_type == R_H8_DIR16;
15509 case EM_IP2K_OLD:
15510 case EM_IP2K:
15511 return reloc_type == 1; /* R_IP2K_16. */
15512 case EM_M32C_OLD:
15513 case EM_M32C:
15514 return reloc_type == 1; /* R_M32C_16 */
15515 case EM_CYGNUS_MN10200:
15516 case EM_MN10200:
15517 return reloc_type == 2; /* R_MN10200_16. */
15518 case EM_CYGNUS_MN10300:
15519 case EM_MN10300:
15520 return reloc_type == 2; /* R_MN10300_16. */
15521 case EM_KVX:
15522 return reloc_type == 1; /* R_KVX_16 */
15523 case EM_MSP430:
15524 if (uses_msp430x_relocs (filedata))
15525 return reloc_type == 2; /* R_MSP430_ABS16. */
15526 /* Fall through. */
15527 case EM_MSP430_OLD:
15528 return reloc_type == 5; /* R_MSP430_16_BYTE. */
15529 case EM_NDS32:
15530 return reloc_type == 19; /* R_NDS32_16_RELA. */
15531 case EM_ALTERA_NIOS2:
15532 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
15533 case EM_NIOS32:
15534 return reloc_type == 9; /* R_NIOS_16. */
15535 case EM_OR1K:
15536 return reloc_type == 2; /* R_OR1K_16. */
15537 case EM_RISCV:
15538 return reloc_type == 55; /* R_RISCV_SET16. */
15539 case EM_TI_PRU:
15540 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
15541 case EM_TI_C6000:
15542 return reloc_type == 2; /* R_C6000_ABS16. */
15543 case EM_VISIUM:
15544 return reloc_type == 2; /* R_VISIUM_16. */
15545 case EM_XGATE:
15546 return reloc_type == 3; /* R_XGATE_16. */
15547 case EM_Z80:
15548 return reloc_type == 4; /* R_Z80_16. */
15549 default:
15550 return false;
15551 }
15552}
15553
15554/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15555 a 8-bit absolute RELA relocation used in DWARF debug sections. */
15556
15557static bool
15558is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15559{
15560 switch (filedata->file_header.e_machine)
15561 {
15562 case EM_RISCV:
15563 return reloc_type == 54; /* R_RISCV_SET8. */
15564 case EM_Z80:
15565 return reloc_type == 1; /* R_Z80_8. */
15566 case EM_MICROBLAZE:
15567 return (reloc_type == 33 /* R_MICROBLAZE_32_NONE. */
15568 || reloc_type == 0 /* R_MICROBLAZE_NONE. */
15569 || reloc_type == 9 /* R_MICROBLAZE_64_NONE. */);
15570 default:
15571 return false;
15572 }
15573}
15574
15575/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15576 a 6-bit absolute RELA relocation used in DWARF debug sections. */
15577
15578static bool
15579is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15580{
15581 switch (filedata->file_header.e_machine)
15582 {
15583 case EM_RISCV:
15584 return reloc_type == 53; /* R_RISCV_SET6. */
15585 default:
15586 return false;
15587 }
15588}
15589
15590/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15591 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
15592
15593static bool
15594is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15595{
15596 /* Please keep this table alpha-sorted for ease of visual lookup. */
15597 switch (filedata->file_header.e_machine)
15598 {
15599 case EM_LOONGARCH:
15600 return reloc_type == 50; /* R_LARCH_ADD32. */
15601 case EM_RISCV:
15602 return reloc_type == 35; /* R_RISCV_ADD32. */
15603 default:
15604 return false;
15605 }
15606}
15607
15608/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15609 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
15610
15611static bool
15612is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15613{
15614 /* Please keep this table alpha-sorted for ease of visual lookup. */
15615 switch (filedata->file_header.e_machine)
15616 {
15617 case EM_LOONGARCH:
15618 return reloc_type == 55; /* R_LARCH_SUB32. */
15619 case EM_RISCV:
15620 return reloc_type == 39; /* R_RISCV_SUB32. */
15621 default:
15622 return false;
15623 }
15624}
15625
15626/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15627 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
15628
15629static bool
15630is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15631{
15632 /* Please keep this table alpha-sorted for ease of visual lookup. */
15633 switch (filedata->file_header.e_machine)
15634 {
15635 case EM_LOONGARCH:
15636 return reloc_type == 51; /* R_LARCH_ADD64. */
15637 case EM_RISCV:
15638 return reloc_type == 36; /* R_RISCV_ADD64. */
15639 default:
15640 return false;
15641 }
15642}
15643
15644/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15645 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
15646
15647static bool
15648is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15649{
15650 /* Please keep this table alpha-sorted for ease of visual lookup. */
15651 switch (filedata->file_header.e_machine)
15652 {
15653 case EM_LOONGARCH:
15654 return reloc_type == 56; /* R_LARCH_SUB64. */
15655 case EM_RISCV:
15656 return reloc_type == 40; /* R_RISCV_SUB64. */
15657 default:
15658 return false;
15659 }
15660}
15661
15662/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15663 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
15664
15665static bool
15666is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15667{
15668 /* Please keep this table alpha-sorted for ease of visual lookup. */
15669 switch (filedata->file_header.e_machine)
15670 {
15671 case EM_LOONGARCH:
15672 return reloc_type == 48; /* R_LARCH_ADD16. */
15673 case EM_RISCV:
15674 return reloc_type == 34; /* R_RISCV_ADD16. */
15675 default:
15676 return false;
15677 }
15678}
15679
15680/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15681 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
15682
15683static bool
15684is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15685{
15686 /* Please keep this table alpha-sorted for ease of visual lookup. */
15687 switch (filedata->file_header.e_machine)
15688 {
15689 case EM_LOONGARCH:
15690 return reloc_type == 53; /* R_LARCH_SUB16. */
15691 case EM_RISCV:
15692 return reloc_type == 38; /* R_RISCV_SUB16. */
15693 default:
15694 return false;
15695 }
15696}
15697
15698/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15699 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
15700
15701static bool
15702is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15703{
15704 /* Please keep this table alpha-sorted for ease of visual lookup. */
15705 switch (filedata->file_header.e_machine)
15706 {
15707 case EM_LOONGARCH:
15708 return reloc_type == 47; /* R_LARCH_ADD8. */
15709 case EM_RISCV:
15710 return reloc_type == 33; /* R_RISCV_ADD8. */
15711 default:
15712 return false;
15713 }
15714}
15715
15716/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15717 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
15718
15719static bool
15720is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15721{
15722 /* Please keep this table alpha-sorted for ease of visual lookup. */
15723 switch (filedata->file_header.e_machine)
15724 {
15725 case EM_LOONGARCH:
15726 return reloc_type == 52; /* R_LARCH_SUB8. */
15727 case EM_RISCV:
15728 return reloc_type == 37; /* R_RISCV_SUB8. */
15729 default:
15730 return false;
15731 }
15732}
15733
15734/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15735 a 6-bit inplace add RELA relocation used in DWARF debug sections. */
15736
15737static bool
15738is_6bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15739{
15740 switch (filedata->file_header.e_machine)
15741 {
15742 case EM_LOONGARCH:
15743 return reloc_type == 105; /* R_LARCH_ADD6. */
15744 default:
15745 return false;
15746 }
15747}
15748
15749/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15750 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
15751
15752static bool
15753is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15754{
15755 switch (filedata->file_header.e_machine)
15756 {
15757 case EM_LOONGARCH:
15758 return reloc_type == 106; /* R_LARCH_SUB6. */
15759 case EM_RISCV:
15760 return reloc_type == 52; /* R_RISCV_SUB6. */
15761 default:
15762 return false;
15763 }
15764}
15765
15766/* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
15767 relocation entries (possibly formerly used for SHT_GROUP sections). */
15768
15769static bool
15770is_none_reloc (Filedata * filedata, unsigned int reloc_type)
15771{
15772 switch (filedata->file_header.e_machine)
15773 {
15774 case EM_386: /* R_386_NONE. */
15775 case EM_68K: /* R_68K_NONE. */
15776 case EM_ADAPTEVA_EPIPHANY:
15777 case EM_ALPHA: /* R_ALPHA_NONE. */
15778 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
15779 case EM_ARC: /* R_ARC_NONE. */
15780 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
15781 case EM_ARC_COMPACT: /* R_ARC_NONE. */
15782 case EM_ARC_COMPACT3: /* R_ARC_NONE. */
15783 case EM_ARC_COMPACT3_64: /* R_ARC_NONE. */
15784 case EM_ARM: /* R_ARM_NONE. */
15785 case EM_CRIS: /* R_CRIS_NONE. */
15786 case EM_FT32: /* R_FT32_NONE. */
15787 case EM_IA_64: /* R_IA64_NONE. */
15788 case EM_K1OM: /* R_X86_64_NONE. */
15789 case EM_KVX: /* R_KVX_NONE. */
15790 case EM_L1OM: /* R_X86_64_NONE. */
15791 case EM_M32R: /* R_M32R_NONE. */
15792 case EM_MIPS: /* R_MIPS_NONE. */
15793 case EM_MN10300: /* R_MN10300_NONE. */
15794 case EM_MOXIE: /* R_MOXIE_NONE. */
15795 case EM_NIOS32: /* R_NIOS_NONE. */
15796 case EM_OR1K: /* R_OR1K_NONE. */
15797 case EM_PARISC: /* R_PARISC_NONE. */
15798 case EM_PPC64: /* R_PPC64_NONE. */
15799 case EM_PPC: /* R_PPC_NONE. */
15800 case EM_RISCV: /* R_RISCV_NONE. */
15801 case EM_S390: /* R_390_NONE. */
15802 case EM_S390_OLD:
15803 case EM_SH: /* R_SH_NONE. */
15804 case EM_SPARC32PLUS:
15805 case EM_SPARC: /* R_SPARC_NONE. */
15806 case EM_SPARCV9:
15807 case EM_TILEGX: /* R_TILEGX_NONE. */
15808 case EM_TILEPRO: /* R_TILEPRO_NONE. */
15809 case EM_TI_C6000:/* R_C6000_NONE. */
15810 case EM_X86_64: /* R_X86_64_NONE. */
15811 case EM_Z80: /* R_Z80_NONE. */
15812 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
15813 return reloc_type == 0;
15814
15815 case EM_AARCH64:
15816 return reloc_type == 0 || reloc_type == 256;
15817 case EM_AVR_OLD:
15818 case EM_AVR:
15819 return (reloc_type == 0 /* R_AVR_NONE. */
15820 || reloc_type == 30 /* R_AVR_DIFF8. */
15821 || reloc_type == 31 /* R_AVR_DIFF16. */
15822 || reloc_type == 32 /* R_AVR_DIFF32. */);
15823 case EM_METAG:
15824 return reloc_type == 3; /* R_METAG_NONE. */
15825 case EM_NDS32:
15826 return (reloc_type == 0 /* R_NDS32_NONE. */
15827 || reloc_type == 205 /* R_NDS32_DIFF8. */
15828 || reloc_type == 206 /* R_NDS32_DIFF16. */
15829 || reloc_type == 207 /* R_NDS32_DIFF32. */
15830 || reloc_type == 208 /* R_NDS32_DIFF_ULEB128. */);
15831 case EM_TI_PRU:
15832 return (reloc_type == 0 /* R_PRU_NONE. */
15833 || reloc_type == 65 /* R_PRU_DIFF8. */
15834 || reloc_type == 66 /* R_PRU_DIFF16. */
15835 || reloc_type == 67 /* R_PRU_DIFF32. */);
15836 case EM_XTENSA_OLD:
15837 case EM_XTENSA:
15838 return (reloc_type == 0 /* R_XTENSA_NONE. */
15839 || reloc_type == 17 /* R_XTENSA_DIFF8. */
15840 || reloc_type == 18 /* R_XTENSA_DIFF16. */
15841 || reloc_type == 19 /* R_XTENSA_DIFF32. */
15842 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
15843 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
15844 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
15845 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
15846 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
15847 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
15848 }
15849 return false;
15850}
15851
15852/* Returns TRUE if there is a relocation against
15853 section NAME at OFFSET bytes. */
15854
15855bool
15856reloc_at (struct dwarf_section * dsec, uint64_t offset)
15857{
15858 Elf_Internal_Rela * relocs;
15859 Elf_Internal_Rela * rp;
15860
15861 if (dsec == NULL || dsec->reloc_info == NULL)
15862 return false;
15863
15864 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
15865
15866 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
15867 if (rp->r_offset == offset)
15868 return true;
15869
15870 return false;
15871}
15872
15873/* Apply relocations to a section.
15874 Returns TRUE upon success, FALSE otherwise.
15875 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
15876 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
15877 will be set to the number of relocs loaded.
15878
15879 Note: So far support has been added only for those relocations
15880 which can be found in debug sections. FIXME: Add support for
15881 more relocations ? */
15882
15883static bool
15884apply_relocations (Filedata *filedata,
15885 const Elf_Internal_Shdr *section,
15886 unsigned char *start,
15887 size_t size,
15888 void **relocs_return,
15889 uint64_t *num_relocs_return)
15890{
15891 Elf_Internal_Shdr * relsec;
15892 unsigned char * end = start + size;
15893
15894 if (relocs_return != NULL)
15895 {
15896 * (Elf_Internal_Rela **) relocs_return = NULL;
15897 * num_relocs_return = 0;
15898 }
15899
15900 if (filedata->file_header.e_type != ET_REL)
15901 /* No relocs to apply. */
15902 return true;
15903
15904 /* Find the reloc section associated with the section. */
15905 for (relsec = filedata->section_headers;
15906 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15907 ++relsec)
15908 {
15909 bool is_rela;
15910 uint64_t num_relocs;
15911 Elf_Internal_Rela * relocs;
15912 Elf_Internal_Rela * rp;
15913 Elf_Internal_Shdr * symsec;
15914 Elf_Internal_Sym * symtab;
15915 uint64_t num_syms;
15916 Elf_Internal_Sym * sym;
15917
15918 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15919 || relsec->sh_info >= filedata->file_header.e_shnum
15920 || filedata->section_headers + relsec->sh_info != section
15921 || relsec->sh_size == 0
15922 || relsec->sh_link >= filedata->file_header.e_shnum)
15923 continue;
15924
15925 symsec = filedata->section_headers + relsec->sh_link;
15926 if (symsec->sh_type != SHT_SYMTAB
15927 && symsec->sh_type != SHT_DYNSYM)
15928 return false;
15929
15930 is_rela = relsec->sh_type == SHT_RELA;
15931
15932 if (is_rela)
15933 {
15934 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
15935 relsec->sh_size, & relocs, & num_relocs))
15936 return false;
15937 }
15938 else
15939 {
15940 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
15941 relsec->sh_size, & relocs, & num_relocs))
15942 return false;
15943 }
15944
15945 /* SH uses RELA but uses in place value instead of the addend field. */
15946 if (filedata->file_header.e_machine == EM_SH)
15947 is_rela = false;
15948
15949 symtab = get_elf_symbols (filedata, symsec, & num_syms);
15950
15951 for (rp = relocs; rp < relocs + num_relocs; ++rp)
15952 {
15953 uint64_t addend;
15954 unsigned int reloc_type;
15955 unsigned int reloc_size;
15956 bool reloc_inplace = false;
15957 bool reloc_subtract = false;
15958 unsigned char *rloc;
15959 uint64_t sym_index;
15960
15961 reloc_type = get_reloc_type (filedata, rp->r_info);
15962
15963 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
15964 continue;
15965 else if (is_none_reloc (filedata, reloc_type))
15966 continue;
15967 else if (is_32bit_abs_reloc (filedata, reloc_type)
15968 || is_32bit_pcrel_reloc (filedata, reloc_type))
15969 reloc_size = 4;
15970 else if (is_64bit_abs_reloc (filedata, reloc_type)
15971 || is_64bit_pcrel_reloc (filedata, reloc_type))
15972 reloc_size = 8;
15973 else if (is_24bit_abs_reloc (filedata, reloc_type))
15974 reloc_size = 3;
15975 else if (is_16bit_abs_reloc (filedata, reloc_type))
15976 reloc_size = 2;
15977 else if (is_8bit_abs_reloc (filedata, reloc_type)
15978 || is_6bit_abs_reloc (filedata, reloc_type))
15979 reloc_size = 1;
15980 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
15981 reloc_type))
15982 || is_32bit_inplace_add_reloc (filedata, reloc_type))
15983 {
15984 reloc_size = 4;
15985 reloc_inplace = true;
15986 }
15987 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
15988 reloc_type))
15989 || is_64bit_inplace_add_reloc (filedata, reloc_type))
15990 {
15991 reloc_size = 8;
15992 reloc_inplace = true;
15993 }
15994 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
15995 reloc_type))
15996 || is_16bit_inplace_add_reloc (filedata, reloc_type))
15997 {
15998 reloc_size = 2;
15999 reloc_inplace = true;
16000 }
16001 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
16002 reloc_type))
16003 || is_8bit_inplace_add_reloc (filedata, reloc_type))
16004 {
16005 reloc_size = 1;
16006 reloc_inplace = true;
16007 }
16008 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
16009 reloc_type))
16010 || is_6bit_inplace_add_reloc (filedata, reloc_type))
16011 {
16012 reloc_size = 1;
16013 reloc_inplace = true;
16014 }
16015 else
16016 {
16017 static unsigned int prev_reloc = 0;
16018
16019 if (reloc_type != prev_reloc)
16020 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
16021 reloc_type, printable_section_name (filedata, section));
16022 prev_reloc = reloc_type;
16023 continue;
16024 }
16025
16026 rloc = start + rp->r_offset;
16027 if (!IN_RANGE (start, end, rloc, reloc_size))
16028 {
16029 warn (_("skipping invalid relocation offset %#" PRIx64
16030 " in section %s\n"),
16031 rp->r_offset,
16032 printable_section_name (filedata, section));
16033 continue;
16034 }
16035
16036 sym_index = get_reloc_symindex (rp->r_info);
16037 if (sym_index >= num_syms)
16038 {
16039 warn (_("skipping invalid relocation symbol index %#" PRIx64
16040 " in section %s\n"),
16041 sym_index, printable_section_name (filedata, section));
16042 continue;
16043 }
16044 sym = symtab + sym_index;
16045
16046 /* If the reloc has a symbol associated with it,
16047 make sure that it is of an appropriate type.
16048
16049 Relocations against symbols without type can happen.
16050 Gcc -feliminate-dwarf2-dups may generate symbols
16051 without type for debug info.
16052
16053 Icc generates relocations against function symbols
16054 instead of local labels.
16055
16056 Relocations against object symbols can happen, eg when
16057 referencing a global array. For an example of this see
16058 the _clz.o binary in libgcc.a. */
16059 if (sym != symtab
16060 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
16061 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
16062 {
16063 warn (_("skipping unexpected symbol type %s in section %s relocation %tu\n"),
16064 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
16065 printable_section_name (filedata, relsec),
16066 rp - relocs);
16067 continue;
16068 }
16069
16070 addend = 0;
16071 if (is_rela)
16072 addend += rp->r_addend;
16073 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
16074 partial_inplace. */
16075 if (!is_rela
16076 || (filedata->file_header.e_machine == EM_XTENSA
16077 && reloc_type == 1)
16078 || ((filedata->file_header.e_machine == EM_PJ
16079 || filedata->file_header.e_machine == EM_PJ_OLD)
16080 && reloc_type == 1)
16081 || ((filedata->file_header.e_machine == EM_D30V
16082 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
16083 && reloc_type == 12)
16084 || reloc_inplace)
16085 {
16086 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
16087 addend += byte_get (rloc, reloc_size) & 0x3f;
16088 else
16089 addend += byte_get (rloc, reloc_size);
16090 }
16091
16092 if (is_32bit_pcrel_reloc (filedata, reloc_type)
16093 || is_64bit_pcrel_reloc (filedata, reloc_type))
16094 {
16095 /* On HPPA, all pc-relative relocations are biased by 8. */
16096 if (filedata->file_header.e_machine == EM_PARISC)
16097 addend -= 8;
16098 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
16099 reloc_size);
16100 }
16101 else if (is_6bit_abs_reloc (filedata, reloc_type)
16102 || is_6bit_inplace_sub_reloc (filedata, reloc_type)
16103 || is_6bit_inplace_add_reloc (filedata, reloc_type))
16104 {
16105 if (reloc_subtract)
16106 addend -= sym->st_value;
16107 else
16108 addend += sym->st_value;
16109 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
16110 byte_put (rloc, addend, reloc_size);
16111 }
16112 else if (reloc_subtract)
16113 byte_put (rloc, addend - sym->st_value, reloc_size);
16114 else
16115 byte_put (rloc, addend + sym->st_value, reloc_size);
16116 }
16117
16118 free (symtab);
16119 /* Let the target specific reloc processing code know that
16120 we have finished with these relocs. */
16121 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
16122
16123 if (relocs_return)
16124 {
16125 * (Elf_Internal_Rela **) relocs_return = relocs;
16126 * num_relocs_return = num_relocs;
16127 }
16128 else
16129 free (relocs);
16130
16131 break;
16132 }
16133
16134 return true;
16135}
16136
16137#ifdef SUPPORT_DISASSEMBLY
16138static bool
16139disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
16140{
16141 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
16142
16143 /* FIXME: XXX -- to be done --- XXX */
16144
16145 return true;
16146}
16147#endif
16148
16149/* Reads in the contents of SECTION from FILE, returning a pointer
16150 to a malloc'ed buffer or NULL if something went wrong. */
16151
16152static char *
16153get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
16154{
16155 uint64_t num_bytes = section->sh_size;
16156
16157 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
16158 {
16159 printf (_("Section '%s' has no data to dump.\n"),
16160 printable_section_name (filedata, section));
16161 return NULL;
16162 }
16163
16164 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
16165 _("section contents"));
16166}
16167
16168/* Uncompresses a section that was compressed using zlib/zstd, in place. */
16169
16170static bool
16171uncompress_section_contents (bool is_zstd,
16172 unsigned char ** buffer,
16173 uint64_t uncompressed_size,
16174 uint64_t * size,
16175 uint64_t file_size)
16176{
16177 uint64_t compressed_size = *size;
16178 unsigned char *compressed_buffer = *buffer;
16179 unsigned char *uncompressed_buffer = NULL;
16180 z_stream strm;
16181 int rc;
16182
16183 /* Similar to bfd_section_size_insane() in the BFD library we expect an
16184 upper limit of ~10x compression. Any compression larger than that is
16185 thought to be due to fuzzing of the compression header. */
16186 if (uncompressed_size > file_size * 10)
16187 {
16188 error (_("Uncompressed section size is suspiciously large: 0x%" PRIu64 "\n"),
16189 uncompressed_size);
16190 goto fail;
16191 }
16192
16193 uncompressed_buffer = xmalloc (uncompressed_size);
16194
16195 if (is_zstd)
16196 {
16197#ifdef HAVE_ZSTD
16198 size_t ret = ZSTD_decompress (uncompressed_buffer, uncompressed_size,
16199 compressed_buffer, compressed_size);
16200 if (ZSTD_isError (ret))
16201 goto fail;
16202#endif
16203 }
16204 else
16205 {
16206 /* It is possible the section consists of several compressed
16207 buffers concatenated together, so we uncompress in a loop. */
16208 /* PR 18313: The state field in the z_stream structure is supposed
16209 to be invisible to the user (ie us), but some compilers will
16210 still complain about it being used without initialisation. So
16211 we first zero the entire z_stream structure and then set the fields
16212 that we need. */
16213 memset (&strm, 0, sizeof strm);
16214 strm.avail_in = compressed_size;
16215 strm.next_in = (Bytef *)compressed_buffer;
16216 strm.avail_out = uncompressed_size;
16217
16218 rc = inflateInit (&strm);
16219 while (strm.avail_in > 0)
16220 {
16221 if (rc != Z_OK)
16222 break;
16223 strm.next_out = ((Bytef *)uncompressed_buffer
16224 + (uncompressed_size - strm.avail_out));
16225 rc = inflate (&strm, Z_FINISH);
16226 if (rc != Z_STREAM_END)
16227 break;
16228 rc = inflateReset (&strm);
16229 }
16230 if (inflateEnd (&strm) != Z_OK || rc != Z_OK || strm.avail_out != 0)
16231 goto fail;
16232 }
16233
16234 *buffer = uncompressed_buffer;
16235 *size = uncompressed_size;
16236 return true;
16237
16238 fail:
16239 free (uncompressed_buffer);
16240 /* Indicate decompression failure. */
16241 *buffer = NULL;
16242 return false;
16243}
16244
16245static uint64_t
16246maybe_expand_or_relocate_section (Elf_Internal_Shdr * section,
16247 Filedata * filedata,
16248 unsigned char ** start_ptr,
16249 bool relocate)
16250{
16251 uint64_t section_size = section->sh_size;
16252 unsigned char * start = * start_ptr;
16253
16254 if (decompress_dumps)
16255 {
16256 uint64_t new_size = section_size;
16257 uint64_t uncompressed_size = 0;
16258 bool is_zstd = false;
16259
16260 if ((section->sh_flags & SHF_COMPRESSED) != 0)
16261 {
16262 Elf_Internal_Chdr chdr;
16263 unsigned int compression_header_size
16264 = get_compression_header (& chdr, start, section_size);
16265
16266 if (compression_header_size == 0)
16267 /* An error message will have already been generated
16268 by get_compression_header. */
16269 return (uint64_t) -1;
16270
16271 if (chdr.ch_type == ch_compress_zlib)
16272 ;
16273#ifdef HAVE_ZSTD
16274 else if (chdr.ch_type == ch_compress_zstd)
16275 is_zstd = true;
16276#endif
16277 else
16278 {
16279 warn (_("section '%s' has unsupported compress type: %d\n"),
16280 printable_section_name (filedata, section), chdr.ch_type);
16281 return (uint64_t) -1;
16282 }
16283
16284 uncompressed_size = chdr.ch_size;
16285 start += compression_header_size;
16286 new_size -= compression_header_size;
16287 }
16288 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
16289 {
16290 /* Read the zlib header. In this case, it should be "ZLIB"
16291 followed by the uncompressed section size, 8 bytes in
16292 big-endian order. */
16293 uncompressed_size = start[4]; uncompressed_size <<= 8;
16294 uncompressed_size += start[5]; uncompressed_size <<= 8;
16295 uncompressed_size += start[6]; uncompressed_size <<= 8;
16296 uncompressed_size += start[7]; uncompressed_size <<= 8;
16297 uncompressed_size += start[8]; uncompressed_size <<= 8;
16298 uncompressed_size += start[9]; uncompressed_size <<= 8;
16299 uncompressed_size += start[10]; uncompressed_size <<= 8;
16300 uncompressed_size += start[11];
16301 start += 12;
16302 new_size -= 12;
16303 }
16304
16305 if (uncompressed_size)
16306 {
16307 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
16308 &new_size, filedata->file_size))
16309 section_size = new_size;
16310 else
16311 {
16312 error (_("Unable to decompress section %s\n"),
16313 printable_section_name (filedata, section));
16314 return (uint64_t) -1;
16315 }
16316 }
16317 else
16318 start = * start_ptr;
16319 }
16320 else if (((section->sh_flags & SHF_COMPRESSED) != 0)
16321 || (section_size > 12 && streq ((char *) start, "ZLIB")))
16322 {
16323 printf (_(" NOTE: This section is compressed, but its contents have NOT been expanded for this dump.\n"));
16324 }
16325
16326 if (relocate)
16327 {
16328 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
16329 return (uint64_t) -1;
16330 }
16331 else
16332 {
16333 Elf_Internal_Shdr *relsec;
16334
16335 /* If the section being dumped has relocations against it the user might
16336 be expecting these relocations to have been applied. Check for this
16337 case and issue a warning message in order to avoid confusion.
16338 FIXME: Maybe we ought to have an option that dumps a section with
16339 relocs applied ? */
16340 for (relsec = filedata->section_headers;
16341 relsec < filedata->section_headers + filedata->file_header.e_shnum;
16342 ++relsec)
16343 {
16344 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
16345 || relsec->sh_info >= filedata->file_header.e_shnum
16346 || filedata->section_headers + relsec->sh_info != section
16347 || relsec->sh_size == 0
16348 || relsec->sh_link >= filedata->file_header.e_shnum)
16349 continue;
16350
16351 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
16352 break;
16353 }
16354 }
16355
16356 * start_ptr = start;
16357 return section_size;
16358}
16359
16360static bool
16361dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
16362{
16363 uint64_t num_bytes;
16364 unsigned char *data;
16365 unsigned char *end;
16366 unsigned char *real_start;
16367 unsigned char *start;
16368 bool some_strings_shown;
16369
16370 real_start = start = (unsigned char *) get_section_contents (section, filedata);
16371 if (start == NULL)
16372 /* PR 21820: Do not fail if the section was empty. */
16373 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
16374
16375 num_bytes = section->sh_size;
16376
16377 if (filedata->is_separate)
16378 printf (_("\nString dump of section '%s' in linked file %s:\n"),
16379 printable_section_name (filedata, section),
16380 filedata->file_name);
16381 else
16382 printf (_("\nString dump of section '%s':\n"),
16383 printable_section_name (filedata, section));
16384
16385 num_bytes = maybe_expand_or_relocate_section (section, filedata, & start, false);
16386 if (num_bytes == (uint64_t) -1)
16387 goto error_out;
16388
16389 data = start;
16390 end = start + num_bytes;
16391 some_strings_shown = false;
16392
16393#ifdef HAVE_MBSTATE_T
16394 mbstate_t state;
16395 /* Initialise the multibyte conversion state. */
16396 memset (& state, 0, sizeof (state));
16397#endif
16398
16399 bool continuing = false;
16400
16401 while (data < end)
16402 {
16403 while (!ISPRINT (* data))
16404 if (++ data >= end)
16405 break;
16406
16407 if (data < end)
16408 {
16409 size_t maxlen = end - data;
16410
16411 if (continuing)
16412 {
16413 printf (" ");
16414 continuing = false;
16415 }
16416 else
16417 {
16418 printf (" [%6tx] ", data - start);
16419 }
16420
16421 if (maxlen > 0)
16422 {
16423 char c = 0;
16424
16425 while (maxlen)
16426 {
16427 c = *data++;
16428
16429 if (c == 0)
16430 break;
16431
16432 /* PR 25543: Treat new-lines as string-ending characters. */
16433 if (c == '\n')
16434 {
16435 printf ("\\n\n");
16436 if (*data != 0)
16437 continuing = true;
16438 break;
16439 }
16440
16441 /* Do not print control characters directly as they can affect terminal
16442 settings. Such characters usually appear in the names generated
16443 by the assembler for local labels. */
16444 if (ISCNTRL (c))
16445 {
16446 printf ("^%c", c + 0x40);
16447 }
16448 else if (ISPRINT (c))
16449 {
16450 putchar (c);
16451 }
16452 else
16453 {
16454 size_t n;
16455#ifdef HAVE_MBSTATE_T
16456 wchar_t w;
16457#endif
16458 /* Let printf do the hard work of displaying multibyte characters. */
16459 printf ("%.1s", data - 1);
16460#ifdef HAVE_MBSTATE_T
16461 /* Try to find out how many bytes made up the character that was
16462 just printed. Advance the symbol pointer past the bytes that
16463 were displayed. */
16464 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
16465#else
16466 n = 1;
16467#endif
16468 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
16469 data += (n - 1);
16470 }
16471 }
16472
16473 if (c != '\n')
16474 putchar ('\n');
16475 }
16476 else
16477 {
16478 printf (_("<corrupt>\n"));
16479 data = end;
16480 }
16481 some_strings_shown = true;
16482 }
16483 }
16484
16485 if (! some_strings_shown)
16486 printf (_(" No strings found in this section."));
16487
16488 free (real_start);
16489
16490 putchar ('\n');
16491 return true;
16492
16493error_out:
16494 free (real_start);
16495 return false;
16496}
16497
16498static bool
16499dump_section_as_bytes (Elf_Internal_Shdr *section,
16500 Filedata *filedata,
16501 bool relocate)
16502{
16503 size_t bytes;
16504 uint64_t section_size;
16505 uint64_t addr;
16506 unsigned char *data;
16507 unsigned char *real_start;
16508 unsigned char *start;
16509
16510 real_start = start = (unsigned char *) get_section_contents (section, filedata);
16511 if (start == NULL)
16512 /* PR 21820: Do not fail if the section was empty. */
16513 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
16514
16515 section_size = section->sh_size;
16516
16517 if (filedata->is_separate)
16518 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
16519 printable_section_name (filedata, section),
16520 filedata->file_name);
16521 else
16522 printf (_("\nHex dump of section '%s':\n"),
16523 printable_section_name (filedata, section));
16524
16525 section_size = maybe_expand_or_relocate_section (section, filedata, & start, relocate);
16526 if (section_size == (uint64_t) -1)
16527 goto error_out;
16528
16529 addr = section->sh_addr;
16530 bytes = section_size;
16531 data = start;
16532
16533 while (bytes)
16534 {
16535 int j;
16536 int k;
16537 int lbytes;
16538
16539 lbytes = (bytes > 16 ? 16 : bytes);
16540
16541 printf (" 0x%8.8" PRIx64 " ", addr);
16542
16543 for (j = 0; j < 16; j++)
16544 {
16545 if (j < lbytes)
16546 printf ("%2.2x", data[j]);
16547 else
16548 printf (" ");
16549
16550 if ((j & 3) == 3)
16551 printf (" ");
16552 }
16553
16554 for (j = 0; j < lbytes; j++)
16555 {
16556 k = data[j];
16557 if (k >= ' ' && k < 0x7f)
16558 printf ("%c", k);
16559 else
16560 printf (".");
16561 }
16562
16563 putchar ('\n');
16564
16565 data += lbytes;
16566 addr += lbytes;
16567 bytes -= lbytes;
16568 }
16569
16570 free (real_start);
16571
16572 putchar ('\n');
16573 return true;
16574
16575 error_out:
16576 free (real_start);
16577 return false;
16578}
16579
16580#ifdef ENABLE_LIBCTF
16581static ctf_sect_t *
16582shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
16583{
16584 buf->cts_name = printable_section_name (filedata, shdr);
16585 buf->cts_size = shdr->sh_size;
16586 buf->cts_entsize = shdr->sh_entsize;
16587
16588 return buf;
16589}
16590
16591/* Formatting callback function passed to ctf_dump. Returns either the pointer
16592 it is passed, or a pointer to newly-allocated storage, in which case
16593 dump_ctf() will free it when it no longer needs it. */
16594
16595static char *
16596dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
16597 char *s, void *arg)
16598{
16599 const char *blanks = arg;
16600 char *new_s;
16601
16602 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
16603 return s;
16604 return new_s;
16605}
16606
16607/* Dump CTF errors/warnings. */
16608static void
16609dump_ctf_errs (ctf_dict_t *fp)
16610{
16611 ctf_next_t *it = NULL;
16612 char *errtext;
16613 int is_warning;
16614 int err;
16615
16616 /* Dump accumulated errors and warnings. */
16617 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
16618 {
16619 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
16620 errtext);
16621 free (errtext);
16622 }
16623 if (err != ECTF_NEXT_END)
16624 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
16625}
16626
16627/* Dump one CTF archive member. */
16628
16629static void
16630dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
16631 size_t member)
16632{
16633 const char *things[] = {"Header", "Labels", "Data objects",
16634 "Function objects", "Variables", "Types", "Strings",
16635 ""};
16636 const char **thing;
16637 size_t i;
16638
16639 /* Don't print out the name of the default-named archive member if it appears
16640 first in the list. The name .ctf appears everywhere, even for things that
16641 aren't really archives, so printing it out is liable to be confusing; also,
16642 the common case by far is for only one archive member to exist, and hiding
16643 it in that case seems worthwhile. */
16644
16645 if (strcmp (name, ".ctf") != 0 || member != 0)
16646 printf (_("\nCTF archive member: %s:\n"), name);
16647
16648 if (ctf_parent_name (ctf) != NULL)
16649 ctf_import (ctf, parent);
16650
16651 for (i = 0, thing = things; *thing[0]; thing++, i++)
16652 {
16653 ctf_dump_state_t *s = NULL;
16654 char *item;
16655
16656 printf ("\n %s:\n", *thing);
16657 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
16658 (void *) " ")) != NULL)
16659 {
16660 printf ("%s\n", item);
16661 free (item);
16662 }
16663
16664 if (ctf_errno (ctf))
16665 {
16666 error (_("Iteration failed: %s, %s\n"), *thing,
16667 ctf_errmsg (ctf_errno (ctf)));
16668 break;
16669 }
16670 }
16671
16672 dump_ctf_errs (ctf);
16673}
16674
16675static bool
16676dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
16677{
16678 Elf_Internal_Shdr * symtab_sec = NULL;
16679 Elf_Internal_Shdr * strtab_sec = NULL;
16680 void * data = NULL;
16681 void * symdata = NULL;
16682 void * strdata = NULL;
16683 ctf_sect_t ctfsect, symsect, strsect;
16684 ctf_sect_t * symsectp = NULL;
16685 ctf_sect_t * strsectp = NULL;
16686 ctf_archive_t * ctfa = NULL;
16687 ctf_dict_t * parent = NULL;
16688 ctf_dict_t * fp;
16689
16690 ctf_next_t *i = NULL;
16691 const char *name;
16692 size_t member = 0;
16693 int err;
16694 bool ret = false;
16695
16696 shdr_to_ctf_sect (&ctfsect, section, filedata);
16697 data = get_section_contents (section, filedata);
16698 ctfsect.cts_data = data;
16699
16700 if (!dump_ctf_symtab_name)
16701 dump_ctf_symtab_name = strdup (".dynsym");
16702
16703 if (!dump_ctf_strtab_name)
16704 dump_ctf_strtab_name = strdup (".dynstr");
16705
16706 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
16707 {
16708 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
16709 {
16710 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
16711 goto fail;
16712 }
16713 if ((symdata = (void *) get_data (NULL, filedata,
16714 symtab_sec->sh_offset, 1,
16715 symtab_sec->sh_size,
16716 _("symbols"))) == NULL)
16717 goto fail;
16718 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
16719 symsect.cts_data = symdata;
16720 }
16721
16722 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
16723 {
16724 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
16725 {
16726 error (_("No string table section named %s\n"),
16727 dump_ctf_strtab_name);
16728 goto fail;
16729 }
16730 if ((strdata = (void *) get_data (NULL, filedata,
16731 strtab_sec->sh_offset, 1,
16732 strtab_sec->sh_size,
16733 _("strings"))) == NULL)
16734 goto fail;
16735 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
16736 strsect.cts_data = strdata;
16737 }
16738
16739 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
16740 libctf papers over the difference, so we can pretend it is always an
16741 archive. */
16742
16743 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
16744 {
16745 dump_ctf_errs (NULL);
16746 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
16747 goto fail;
16748 }
16749
16750 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
16751 != ELFDATA2MSB);
16752
16753 /* Preload the parent dict, since it will need to be imported into every
16754 child in turn. */
16755 if ((parent = ctf_dict_open (ctfa, dump_ctf_parent_name, &err)) == NULL)
16756 {
16757 dump_ctf_errs (NULL);
16758 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
16759 goto fail;
16760 }
16761
16762 ret = true;
16763
16764 if (filedata->is_separate)
16765 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
16766 printable_section_name (filedata, section),
16767 filedata->file_name);
16768 else
16769 printf (_("\nDump of CTF section '%s':\n"),
16770 printable_section_name (filedata, section));
16771
16772 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
16773 dump_ctf_archive_member (fp, name, parent, member++);
16774 if (err != ECTF_NEXT_END)
16775 {
16776 dump_ctf_errs (NULL);
16777 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
16778 ret = false;
16779 }
16780
16781 fail:
16782 ctf_dict_close (parent);
16783 ctf_close (ctfa);
16784 free (data);
16785 free (symdata);
16786 free (strdata);
16787 return ret;
16788}
16789#endif
16790
16791static bool
16792dump_section_as_sframe (Elf_Internal_Shdr * section, Filedata * filedata)
16793{
16794 void * data = NULL;
16795 sframe_decoder_ctx *sfd_ctx = NULL;
16796 const char *print_name = printable_section_name (filedata, section);
16797
16798 bool ret = true;
16799 size_t sf_size;
16800 int err = 0;
16801
16802 if (strcmp (print_name, "") == 0)
16803 {
16804 error (_("Section name must be provided \n"));
16805 ret = false;
16806 return ret;
16807 }
16808
16809 data = get_section_contents (section, filedata);
16810 sf_size = section->sh_size;
16811 /* Decode the contents of the section. */
16812 sfd_ctx = sframe_decode ((const char*)data, sf_size, &err);
16813 if (!sfd_ctx)
16814 {
16815 ret = false;
16816 error (_("SFrame decode failure: %s\n"), sframe_errmsg (err));
16817 goto fail;
16818 }
16819
16820 printf (_("Contents of the SFrame section %s:"), print_name);
16821 /* Dump the contents as text. */
16822 dump_sframe (sfd_ctx, section->sh_addr);
16823
16824 fail:
16825 free (data);
16826 return ret;
16827}
16828
16829static bool
16830load_specific_debug_section (enum dwarf_section_display_enum debug,
16831 const Elf_Internal_Shdr * sec,
16832 void * data)
16833{
16834 struct dwarf_section * section = &debug_displays [debug].section;
16835 char buf [64];
16836 Filedata * filedata = (Filedata *) data;
16837
16838 if (section->start != NULL)
16839 {
16840 /* If it is already loaded, do nothing. */
16841 if (streq (section->filename, filedata->file_name))
16842 return true;
16843 free (section->start);
16844 }
16845
16846 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
16847 section->address = sec->sh_addr;
16848 section->filename = filedata->file_name;
16849 section->start = (unsigned char *) get_data (NULL, filedata,
16850 sec->sh_offset, 1,
16851 sec->sh_size, buf);
16852 if (section->start == NULL)
16853 section->size = 0;
16854 else
16855 {
16856 unsigned char *start = section->start;
16857 uint64_t size = sec->sh_size;
16858 uint64_t uncompressed_size = 0;
16859 bool is_zstd = false;
16860
16861 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
16862 {
16863 Elf_Internal_Chdr chdr;
16864 unsigned int compression_header_size;
16865
16866 if (size < (is_32bit_elf
16867 ? sizeof (Elf32_External_Chdr)
16868 : sizeof (Elf64_External_Chdr)))
16869 {
16870 warn (_("compressed section %s is too small to contain a compression header\n"),
16871 section->name);
16872 return false;
16873 }
16874
16875 compression_header_size = get_compression_header (&chdr, start, size);
16876 if (compression_header_size == 0)
16877 /* An error message will have already been generated
16878 by get_compression_header. */
16879 return false;
16880
16881 if (chdr.ch_type == ch_compress_zlib)
16882 ;
16883#ifdef HAVE_ZSTD
16884 else if (chdr.ch_type == ch_compress_zstd)
16885 is_zstd = true;
16886#endif
16887 else
16888 {
16889 warn (_("section '%s' has unsupported compress type: %d\n"),
16890 section->name, chdr.ch_type);
16891 return false;
16892 }
16893 uncompressed_size = chdr.ch_size;
16894 start += compression_header_size;
16895 size -= compression_header_size;
16896 }
16897 else if (size > 12 && streq ((char *) start, "ZLIB"))
16898 {
16899 /* Read the zlib header. In this case, it should be "ZLIB"
16900 followed by the uncompressed section size, 8 bytes in
16901 big-endian order. */
16902 uncompressed_size = start[4]; uncompressed_size <<= 8;
16903 uncompressed_size += start[5]; uncompressed_size <<= 8;
16904 uncompressed_size += start[6]; uncompressed_size <<= 8;
16905 uncompressed_size += start[7]; uncompressed_size <<= 8;
16906 uncompressed_size += start[8]; uncompressed_size <<= 8;
16907 uncompressed_size += start[9]; uncompressed_size <<= 8;
16908 uncompressed_size += start[10]; uncompressed_size <<= 8;
16909 uncompressed_size += start[11];
16910 start += 12;
16911 size -= 12;
16912 }
16913
16914 if (uncompressed_size)
16915 {
16916 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
16917 &size, filedata->file_size))
16918 {
16919 /* Free the compressed buffer, update the section buffer
16920 and the section size if uncompress is successful. */
16921 free (section->start);
16922 section->start = start;
16923 }
16924 else
16925 {
16926 error (_("Unable to decompress section %s\n"),
16927 printable_section_name (filedata, sec));
16928 return false;
16929 }
16930 }
16931
16932 section->size = size;
16933 }
16934
16935 if (section->start == NULL)
16936 return false;
16937
16938 if (debug_displays [debug].relocate)
16939 {
16940 if (! apply_relocations (filedata, sec, section->start, section->size,
16941 & section->reloc_info, & section->num_relocs))
16942 return false;
16943 }
16944 else
16945 {
16946 section->reloc_info = NULL;
16947 section->num_relocs = 0;
16948 }
16949
16950 return true;
16951}
16952
16953#if HAVE_LIBDEBUGINFOD
16954/* Return a hex string representation of the build-id. */
16955unsigned char *
16956get_build_id (void * data)
16957{
16958 Filedata * filedata = (Filedata *) data;
16959 Elf_Internal_Shdr * shdr;
16960 size_t i;
16961
16962 /* Iterate through notes to find note.gnu.build-id.
16963 FIXME: Only the first note in any note section is examined. */
16964 for (i = 0, shdr = filedata->section_headers;
16965 i < filedata->file_header.e_shnum && shdr != NULL;
16966 i++, shdr++)
16967 {
16968 if (shdr->sh_type != SHT_NOTE)
16969 continue;
16970
16971 char * next;
16972 char * end;
16973 size_t data_remaining;
16974 size_t min_notesz;
16975 Elf_External_Note * enote;
16976 Elf_Internal_Note inote;
16977
16978 uint64_t offset = shdr->sh_offset;
16979 uint64_t align = shdr->sh_addralign;
16980 uint64_t length = shdr->sh_size;
16981
16982 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
16983 if (enote == NULL)
16984 continue;
16985
16986 if (align < 4)
16987 align = 4;
16988 else if (align != 4 && align != 8)
16989 {
16990 free (enote);
16991 continue;
16992 }
16993
16994 end = (char *) enote + length;
16995 data_remaining = end - (char *) enote;
16996
16997 if (!is_ia64_vms (filedata))
16998 {
16999 min_notesz = offsetof (Elf_External_Note, name);
17000 if (data_remaining < min_notesz)
17001 {
17002 warn (_("\
17003malformed note encountered in section %s whilst scanning for build-id note\n"),
17004 printable_section_name (filedata, shdr));
17005 free (enote);
17006 continue;
17007 }
17008 data_remaining -= min_notesz;
17009
17010 inote.type = BYTE_GET (enote->type);
17011 inote.namesz = BYTE_GET (enote->namesz);
17012 inote.namedata = enote->name;
17013 inote.descsz = BYTE_GET (enote->descsz);
17014 inote.descdata = ((char *) enote
17015 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
17016 inote.descpos = offset + (inote.descdata - (char *) enote);
17017 next = ((char *) enote
17018 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
17019 }
17020 else
17021 {
17022 Elf64_External_VMS_Note *vms_enote;
17023
17024 /* PR binutils/15191
17025 Make sure that there is enough data to read. */
17026 min_notesz = offsetof (Elf64_External_VMS_Note, name);
17027 if (data_remaining < min_notesz)
17028 {
17029 warn (_("\
17030malformed note encountered in section %s whilst scanning for build-id note\n"),
17031 printable_section_name (filedata, shdr));
17032 free (enote);
17033 continue;
17034 }
17035 data_remaining -= min_notesz;
17036
17037 vms_enote = (Elf64_External_VMS_Note *) enote;
17038 inote.type = BYTE_GET (vms_enote->type);
17039 inote.namesz = BYTE_GET (vms_enote->namesz);
17040 inote.namedata = vms_enote->name;
17041 inote.descsz = BYTE_GET (vms_enote->descsz);
17042 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
17043 inote.descpos = offset + (inote.descdata - (char *) enote);
17044 next = inote.descdata + align_power (inote.descsz, 3);
17045 }
17046
17047 /* Skip malformed notes. */
17048 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
17049 || (size_t) (inote.descdata - inote.namedata) > data_remaining
17050 || (size_t) (next - inote.descdata) < inote.descsz
17051 || ((size_t) (next - inote.descdata)
17052 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
17053 {
17054 warn (_("\
17055malformed note encountered in section %s whilst scanning for build-id note\n"),
17056 printable_section_name (filedata, shdr));
17057 free (enote);
17058 continue;
17059 }
17060
17061 /* Check if this is the build-id note. If so then convert the build-id
17062 bytes to a hex string. */
17063 if (inote.namesz > 0
17064 && startswith (inote.namedata, "GNU")
17065 && inote.type == NT_GNU_BUILD_ID)
17066 {
17067 size_t j;
17068 char * build_id;
17069
17070 build_id = malloc (inote.descsz * 2 + 1);
17071 if (build_id == NULL)
17072 {
17073 free (enote);
17074 return NULL;
17075 }
17076
17077 for (j = 0; j < inote.descsz; ++j)
17078 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
17079 build_id[inote.descsz * 2] = '\0';
17080 free (enote);
17081
17082 return (unsigned char *) build_id;
17083 }
17084 free (enote);
17085 }
17086
17087 return NULL;
17088}
17089#endif /* HAVE_LIBDEBUGINFOD */
17090
17091/* If this is not NULL, load_debug_section will only look for sections
17092 within the list of sections given here. */
17093static unsigned int * section_subset = NULL;
17094
17095bool
17096load_debug_section (enum dwarf_section_display_enum debug, void * data)
17097{
17098 struct dwarf_section * section = &debug_displays [debug].section;
17099 Elf_Internal_Shdr * sec;
17100 Filedata * filedata = (Filedata *) data;
17101
17102 if (!dump_any_debugging)
17103 return false;
17104
17105 /* Without section headers we cannot find any sections. */
17106 if (filedata->section_headers == NULL)
17107 return false;
17108
17109 if (filedata->string_table == NULL
17110 && filedata->file_header.e_shstrndx != SHN_UNDEF
17111 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
17112 {
17113 Elf_Internal_Shdr * strs;
17114
17115 /* Read in the string table, so that we have section names to scan. */
17116 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
17117
17118 if (strs != NULL && strs->sh_size != 0)
17119 {
17120 filedata->string_table
17121 = (char *) get_data (NULL, filedata, strs->sh_offset,
17122 1, strs->sh_size, _("string table"));
17123
17124 filedata->string_table_length
17125 = filedata->string_table != NULL ? strs->sh_size : 0;
17126 }
17127 }
17128
17129 /* Locate the debug section. */
17130 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
17131 if (sec != NULL)
17132 section->name = section->uncompressed_name;
17133 else
17134 {
17135 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
17136 if (sec != NULL)
17137 section->name = section->compressed_name;
17138 }
17139 if (sec == NULL)
17140 return false;
17141
17142 /* If we're loading from a subset of sections, and we've loaded
17143 a section matching this name before, it's likely that it's a
17144 different one. */
17145 if (section_subset != NULL)
17146 free_debug_section (debug);
17147
17148 return load_specific_debug_section (debug, sec, data);
17149}
17150
17151void
17152free_debug_section (enum dwarf_section_display_enum debug)
17153{
17154 struct dwarf_section * section = &debug_displays [debug].section;
17155
17156 if (section->start == NULL)
17157 return;
17158
17159 free ((char *) section->start);
17160 section->start = NULL;
17161 section->address = 0;
17162 section->size = 0;
17163
17164 free (section->reloc_info);
17165 section->reloc_info = NULL;
17166 section->num_relocs = 0;
17167}
17168
17169static bool
17170display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
17171{
17172 const char *name = (section_name_valid (filedata, section)
17173 ? section_name (filedata, section) : "");
17174 const char *print_name = printable_section_name (filedata, section);
17175 uint64_t length;
17176 bool result = true;
17177 int i;
17178
17179 length = section->sh_size;
17180 if (length == 0)
17181 {
17182 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
17183 return true;
17184 }
17185 if (section->sh_type == SHT_NOBITS)
17186 {
17187 /* There is no point in dumping the contents of a debugging section
17188 which has the NOBITS type - the bits in the file will be random.
17189 This can happen when a file containing a .eh_frame section is
17190 stripped with the --only-keep-debug command line option. */
17191 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
17192 print_name);
17193 return false;
17194 }
17195
17196 if (startswith (name, ".gnu.linkonce.wi."))
17197 name = ".debug_info";
17198
17199 /* See if we know how to display the contents of this section. */
17200 for (i = 0; i < max; i++)
17201 {
17202 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
17203 struct dwarf_section_display * display = debug_displays + i;
17204 struct dwarf_section * sec = & display->section;
17205
17206 if (streq (sec->uncompressed_name, name)
17207 || (id == line && startswith (name, ".debug_line."))
17208 || streq (sec->compressed_name, name))
17209 {
17210 bool secondary = (section != find_section (filedata, name));
17211
17212 if (secondary)
17213 free_debug_section (id);
17214
17215 if (i == line && startswith (name, ".debug_line."))
17216 sec->name = name;
17217 else if (streq (sec->uncompressed_name, name))
17218 sec->name = sec->uncompressed_name;
17219 else
17220 sec->name = sec->compressed_name;
17221
17222 if (load_specific_debug_section (id, section, filedata))
17223 {
17224 /* If this debug section is part of a CU/TU set in a .dwp file,
17225 restrict load_debug_section to the sections in that set. */
17226 section_subset = find_cu_tu_set (filedata, shndx);
17227
17228 result &= display->display (sec, filedata);
17229
17230 section_subset = NULL;
17231
17232 if (secondary || (id != info && id != abbrev && id != debug_addr))
17233 free_debug_section (id);
17234 }
17235 break;
17236 }
17237 }
17238
17239 if (i == max)
17240 {
17241 printf (_("Unrecognized debug section: %s\n"), print_name);
17242 result = false;
17243 }
17244
17245 return result;
17246}
17247
17248/* Set DUMP_SECTS for all sections where dumps were requested
17249 based on section name. */
17250
17251static void
17252initialise_dumps_byname (Filedata * filedata)
17253{
17254 struct dump_list_entry * cur;
17255
17256 for (cur = dump_sects_byname; cur; cur = cur->next)
17257 {
17258 unsigned int i;
17259 bool any = false;
17260
17261 for (i = 0; i < filedata->file_header.e_shnum; i++)
17262 if (section_name_valid (filedata, filedata->section_headers + i)
17263 && streq (section_name (filedata, filedata->section_headers + i),
17264 cur->name))
17265 {
17266 request_dump_bynumber (&filedata->dump, i, cur->type);
17267 any = true;
17268 }
17269
17270 if (!any && !filedata->is_separate)
17271 warn (_("Section '%s' was not dumped because it does not exist\n"),
17272 cur->name);
17273 }
17274}
17275
17276static bool
17277process_section_contents (Filedata * filedata)
17278{
17279 Elf_Internal_Shdr * section;
17280 unsigned int i;
17281 bool res = true;
17282
17283 if (! do_dump)
17284 return true;
17285
17286 initialise_dumps_byname (filedata);
17287
17288 for (i = 0, section = filedata->section_headers;
17289 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
17290 i++, section++)
17291 {
17292 dump_type dump = filedata->dump.dump_sects[i];
17293
17294 if (filedata->is_separate && ! process_links)
17295 dump &= DEBUG_DUMP;
17296
17297 if (dump & AUTO_DUMP)
17298 {
17299 switch (section->sh_type)
17300 {
17301 case SHT_PROGBITS:
17302 /* FIXME: There are lots of different type of section that have
17303 SHT_PROGBITS set in their header - code, debug info, etc. So
17304 we should check the section's name and interpret its contents
17305 that way, rather than just defaulting to a byte dump. */
17306#ifdef SUPPORT_DISASSEMBLY
17307 res &= disassemble_section (section, filedata);
17308#else
17309 res &= dump_section_as_bytes (section, filedata, false);
17310#endif
17311 break;
17312
17313 case SHT_DYNSYM:
17314 case SHT_SYMTAB:
17315 res &= dump_symbol_section (section, filedata);
17316 break;
17317
17318 case SHT_STRTAB:
17319 res &= dump_section_as_strings (section, filedata);
17320 break;
17321
17322 case SHT_RELA:
17323 case SHT_REL:
17324 case SHT_RELR:
17325 res &= display_relocations (section, filedata);
17326 break;
17327
17328 case SHT_NOTE:
17329 res &= process_notes_at (filedata, section, section->sh_offset,
17330 section->sh_size, section->sh_addralign);
17331 break;
17332
17333 case SHT_NULL:
17334 inform (_("Unable to display section %d - it has a NULL type\n"), i);
17335 break;
17336
17337 case SHT_NOBITS:
17338 inform (_("Unable to display section %d - it has no contents\n"), i);
17339 break;
17340
17341 case SHT_HASH:
17342 case SHT_DYNAMIC:
17343 case SHT_GROUP:
17344 case SHT_GNU_ATTRIBUTES:
17345 /* FIXME: Implement these. */
17346 /* Fall through. */
17347 default:
17348 /* FIXME: Add Proc and OS specific section types ? */
17349 warn (_("Unable to determine how to dump section %d (type %#x)\n"),
17350 i, section->sh_type);
17351 res = false;
17352 break;
17353 }
17354 }
17355
17356#ifdef SUPPORT_DISASSEMBLY
17357 if (dump & DISASS_DUMP)
17358 {
17359 if (! disassemble_section (section, filedata))
17360 res = false;
17361 }
17362#endif
17363 if (dump & HEX_DUMP)
17364 {
17365 if (! dump_section_as_bytes (section, filedata, false))
17366 res = false;
17367 }
17368
17369 if (dump & RELOC_DUMP)
17370 {
17371 if (! dump_section_as_bytes (section, filedata, true))
17372 res = false;
17373 }
17374
17375 if (dump & STRING_DUMP)
17376 {
17377 if (! dump_section_as_strings (section, filedata))
17378 res = false;
17379 }
17380
17381 if (dump & DEBUG_DUMP)
17382 {
17383 if (! display_debug_section (i, section, filedata))
17384 res = false;
17385 }
17386
17387#ifdef ENABLE_LIBCTF
17388 if (dump & CTF_DUMP)
17389 {
17390 if (! dump_section_as_ctf (section, filedata))
17391 res = false;
17392 }
17393#endif
17394 if (dump & SFRAME_DUMP)
17395 {
17396 if (! dump_section_as_sframe (section, filedata))
17397 res = false;
17398 }
17399 }
17400
17401 if (! filedata->is_separate)
17402 {
17403 /* Check to see if the user requested a
17404 dump of a section that does not exist. */
17405 for (; i < filedata->dump.num_dump_sects; i++)
17406 if (filedata->dump.dump_sects[i])
17407 {
17408 warn (_("Section %d was not dumped because it does not exist!\n"), i);
17409 res = false;
17410 }
17411 }
17412
17413 return res;
17414}
17415
17416static void
17417process_mips_fpe_exception (int mask)
17418{
17419 if (mask)
17420 {
17421 bool first = true;
17422
17423 if (mask & OEX_FPU_INEX)
17424 fputs ("INEX", stdout), first = false;
17425 if (mask & OEX_FPU_UFLO)
17426 printf ("%sUFLO", first ? "" : "|"), first = false;
17427 if (mask & OEX_FPU_OFLO)
17428 printf ("%sOFLO", first ? "" : "|"), first = false;
17429 if (mask & OEX_FPU_DIV0)
17430 printf ("%sDIV0", first ? "" : "|"), first = false;
17431 if (mask & OEX_FPU_INVAL)
17432 printf ("%sINVAL", first ? "" : "|");
17433 }
17434 else
17435 fputs ("0", stdout);
17436}
17437
17438/* Display's the value of TAG at location P. If TAG is
17439 greater than 0 it is assumed to be an unknown tag, and
17440 a message is printed to this effect. Otherwise it is
17441 assumed that a message has already been printed.
17442
17443 If the bottom bit of TAG is set it assumed to have a
17444 string value, otherwise it is assumed to have an integer
17445 value.
17446
17447 Returns an updated P pointing to the first unread byte
17448 beyond the end of TAG's value.
17449
17450 Reads at or beyond END will not be made. */
17451
17452static unsigned char *
17453display_tag_value (signed int tag,
17454 unsigned char * p,
17455 const unsigned char * const end)
17456{
17457 uint64_t val;
17458
17459 if (tag > 0)
17460 printf (" Tag_unknown_%d: ", tag);
17461
17462 if (p >= end)
17463 {
17464 warn (_("<corrupt tag>\n"));
17465 }
17466 else if (tag & 1)
17467 {
17468 /* PR 17531 file: 027-19978-0.004. */
17469 size_t maxlen = (end - p) - 1;
17470
17471 putchar ('"');
17472 if (maxlen > 0)
17473 {
17474 print_symbol_name ((int) maxlen, (const char *) p);
17475 p += strnlen ((char *) p, maxlen) + 1;
17476 }
17477 else
17478 {
17479 printf (_("<corrupt string tag>"));
17480 p = (unsigned char *) end;
17481 }
17482 printf ("\"\n");
17483 }
17484 else
17485 {
17486 READ_ULEB (val, p, end);
17487 printf ("%" PRId64 " (0x%" PRIx64 ")\n", val, val);
17488 }
17489
17490 assert (p <= end);
17491 return p;
17492}
17493
17494/* ARC ABI attributes section. */
17495
17496static unsigned char *
17497display_arc_attribute (unsigned char * p,
17498 const unsigned char * const end)
17499{
17500 unsigned int tag;
17501 unsigned int val;
17502
17503 READ_ULEB (tag, p, end);
17504
17505 switch (tag)
17506 {
17507 case Tag_ARC_PCS_config:
17508 READ_ULEB (val, p, end);
17509 printf (" Tag_ARC_PCS_config: ");
17510 switch (val)
17511 {
17512 case 0:
17513 printf (_("Absent/Non standard\n"));
17514 break;
17515 case 1:
17516 printf (_("Bare metal/mwdt\n"));
17517 break;
17518 case 2:
17519 printf (_("Bare metal/newlib\n"));
17520 break;
17521 case 3:
17522 printf (_("Linux/uclibc\n"));
17523 break;
17524 case 4:
17525 printf (_("Linux/glibc\n"));
17526 break;
17527 default:
17528 printf (_("Unknown\n"));
17529 break;
17530 }
17531 break;
17532
17533 case Tag_ARC_CPU_base:
17534 READ_ULEB (val, p, end);
17535 printf (" Tag_ARC_CPU_base: ");
17536 switch (val)
17537 {
17538 default:
17539 case TAG_CPU_NONE:
17540 printf (_("Absent\n"));
17541 break;
17542 case TAG_CPU_ARC6xx:
17543 printf ("ARC6xx\n");
17544 break;
17545 case TAG_CPU_ARC7xx:
17546 printf ("ARC7xx\n");
17547 break;
17548 case TAG_CPU_ARCEM:
17549 printf ("ARCEM\n");
17550 break;
17551 case TAG_CPU_ARCHS:
17552 printf ("ARCHS\n");
17553 break;
17554 }
17555 break;
17556
17557 case Tag_ARC_CPU_variation:
17558 READ_ULEB (val, p, end);
17559 printf (" Tag_ARC_CPU_variation: ");
17560 switch (val)
17561 {
17562 default:
17563 if (val > 0 && val < 16)
17564 printf ("Core%d\n", val);
17565 else
17566 printf ("Unknown\n");
17567 break;
17568
17569 case 0:
17570 printf (_("Absent\n"));
17571 break;
17572 }
17573 break;
17574
17575 case Tag_ARC_CPU_name:
17576 printf (" Tag_ARC_CPU_name: ");
17577 p = display_tag_value (-1, p, end);
17578 break;
17579
17580 case Tag_ARC_ABI_rf16:
17581 READ_ULEB (val, p, end);
17582 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
17583 break;
17584
17585 case Tag_ARC_ABI_osver:
17586 READ_ULEB (val, p, end);
17587 printf (" Tag_ARC_ABI_osver: v%d\n", val);
17588 break;
17589
17590 case Tag_ARC_ABI_pic:
17591 case Tag_ARC_ABI_sda:
17592 READ_ULEB (val, p, end);
17593 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
17594 : " Tag_ARC_ABI_pic: ");
17595 switch (val)
17596 {
17597 case 0:
17598 printf (_("Absent\n"));
17599 break;
17600 case 1:
17601 printf ("MWDT\n");
17602 break;
17603 case 2:
17604 printf ("GNU\n");
17605 break;
17606 default:
17607 printf (_("Unknown\n"));
17608 break;
17609 }
17610 break;
17611
17612 case Tag_ARC_ABI_tls:
17613 READ_ULEB (val, p, end);
17614 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
17615 break;
17616
17617 case Tag_ARC_ABI_enumsize:
17618 READ_ULEB (val, p, end);
17619 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
17620 _("smallest"));
17621 break;
17622
17623 case Tag_ARC_ABI_exceptions:
17624 READ_ULEB (val, p, end);
17625 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
17626 : _("default"));
17627 break;
17628
17629 case Tag_ARC_ABI_double_size:
17630 READ_ULEB (val, p, end);
17631 printf (" Tag_ARC_ABI_double_size: %d\n", val);
17632 break;
17633
17634 case Tag_ARC_ISA_config:
17635 printf (" Tag_ARC_ISA_config: ");
17636 p = display_tag_value (-1, p, end);
17637 break;
17638
17639 case Tag_ARC_ISA_apex:
17640 printf (" Tag_ARC_ISA_apex: ");
17641 p = display_tag_value (-1, p, end);
17642 break;
17643
17644 case Tag_ARC_ISA_mpy_option:
17645 READ_ULEB (val, p, end);
17646 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
17647 break;
17648
17649 case Tag_ARC_ATR_version:
17650 READ_ULEB (val, p, end);
17651 printf (" Tag_ARC_ATR_version: %d\n", val);
17652 break;
17653
17654 default:
17655 return display_tag_value (tag & 1, p, end);
17656 }
17657
17658 return p;
17659}
17660
17661/* ARM EABI attributes section. */
17662typedef struct
17663{
17664 unsigned int tag;
17665 const char * name;
17666 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
17667 unsigned int type;
17668 const char *const *table;
17669} arm_attr_public_tag;
17670
17671static const char *const arm_attr_tag_CPU_arch[] =
17672 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
17673 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
17674 "v8-M.mainline", "v8.1-A", "v8.2-A", "v8.3-A",
17675 "v8.1-M.mainline", "v9"};
17676static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
17677static const char *const arm_attr_tag_THUMB_ISA_use[] =
17678 {"No", "Thumb-1", "Thumb-2", "Yes"};
17679static const char *const arm_attr_tag_FP_arch[] =
17680 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
17681 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
17682static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
17683static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
17684 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
17685 "NEON for ARMv8.1"};
17686static const char *const arm_attr_tag_PCS_config[] =
17687 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
17688 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
17689static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
17690 {"V6", "SB", "TLS", "Unused"};
17691static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
17692 {"Absolute", "PC-relative", "SB-relative", "None"};
17693static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
17694 {"Absolute", "PC-relative", "None"};
17695static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
17696 {"None", "direct", "GOT-indirect"};
17697static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
17698 {"None", "??? 1", "2", "??? 3", "4"};
17699static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
17700static const char *const arm_attr_tag_ABI_FP_denormal[] =
17701 {"Unused", "Needed", "Sign only"};
17702static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
17703static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
17704static const char *const arm_attr_tag_ABI_FP_number_model[] =
17705 {"Unused", "Finite", "RTABI", "IEEE 754"};
17706static const char *const arm_attr_tag_ABI_enum_size[] =
17707 {"Unused", "small", "int", "forced to int"};
17708static const char *const arm_attr_tag_ABI_HardFP_use[] =
17709 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
17710static const char *const arm_attr_tag_ABI_VFP_args[] =
17711 {"AAPCS", "VFP registers", "custom", "compatible"};
17712static const char *const arm_attr_tag_ABI_WMMX_args[] =
17713 {"AAPCS", "WMMX registers", "custom"};
17714static const char *const arm_attr_tag_ABI_optimization_goals[] =
17715 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
17716 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
17717static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
17718 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
17719 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
17720static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
17721static const char *const arm_attr_tag_FP_HP_extension[] =
17722 {"Not Allowed", "Allowed"};
17723static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
17724 {"None", "IEEE 754", "Alternative Format"};
17725static const char *const arm_attr_tag_DSP_extension[] =
17726 {"Follow architecture", "Allowed"};
17727static const char *const arm_attr_tag_MPextension_use[] =
17728 {"Not Allowed", "Allowed"};
17729static const char *const arm_attr_tag_DIV_use[] =
17730 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
17731 "Allowed in v7-A with integer division extension"};
17732static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
17733static const char *const arm_attr_tag_Virtualization_use[] =
17734 {"Not Allowed", "TrustZone", "Virtualization Extensions",
17735 "TrustZone and Virtualization Extensions"};
17736static const char *const arm_attr_tag_MPextension_use_legacy[] =
17737 {"Not Allowed", "Allowed"};
17738
17739static const char *const arm_attr_tag_MVE_arch[] =
17740 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
17741
17742static const char * arm_attr_tag_PAC_extension[] =
17743 {"No PAC/AUT instructions",
17744 "PAC/AUT instructions permitted in the NOP space",
17745 "PAC/AUT instructions permitted in the NOP and in the non-NOP space"};
17746
17747static const char * arm_attr_tag_BTI_extension[] =
17748 {"BTI instructions not permitted",
17749 "BTI instructions permitted in the NOP space",
17750 "BTI instructions permitted in the NOP and in the non-NOP space"};
17751
17752static const char * arm_attr_tag_BTI_use[] =
17753 {"Compiled without branch target enforcement",
17754 "Compiled with branch target enforcement"};
17755
17756static const char * arm_attr_tag_PACRET_use[] =
17757 {"Compiled without return address signing and authentication",
17758 "Compiled with return address signing and authentication"};
17759
17760#define LOOKUP(id, name) \
17761 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
17762static arm_attr_public_tag arm_attr_public_tags[] =
17763{
17764 {4, "CPU_raw_name", 1, NULL},
17765 {5, "CPU_name", 1, NULL},
17766 LOOKUP(6, CPU_arch),
17767 {7, "CPU_arch_profile", 0, NULL},
17768 LOOKUP(8, ARM_ISA_use),
17769 LOOKUP(9, THUMB_ISA_use),
17770 LOOKUP(10, FP_arch),
17771 LOOKUP(11, WMMX_arch),
17772 LOOKUP(12, Advanced_SIMD_arch),
17773 LOOKUP(13, PCS_config),
17774 LOOKUP(14, ABI_PCS_R9_use),
17775 LOOKUP(15, ABI_PCS_RW_data),
17776 LOOKUP(16, ABI_PCS_RO_data),
17777 LOOKUP(17, ABI_PCS_GOT_use),
17778 LOOKUP(18, ABI_PCS_wchar_t),
17779 LOOKUP(19, ABI_FP_rounding),
17780 LOOKUP(20, ABI_FP_denormal),
17781 LOOKUP(21, ABI_FP_exceptions),
17782 LOOKUP(22, ABI_FP_user_exceptions),
17783 LOOKUP(23, ABI_FP_number_model),
17784 {24, "ABI_align_needed", 0, NULL},
17785 {25, "ABI_align_preserved", 0, NULL},
17786 LOOKUP(26, ABI_enum_size),
17787 LOOKUP(27, ABI_HardFP_use),
17788 LOOKUP(28, ABI_VFP_args),
17789 LOOKUP(29, ABI_WMMX_args),
17790 LOOKUP(30, ABI_optimization_goals),
17791 LOOKUP(31, ABI_FP_optimization_goals),
17792 {32, "compatibility", 0, NULL},
17793 LOOKUP(34, CPU_unaligned_access),
17794 LOOKUP(36, FP_HP_extension),
17795 LOOKUP(38, ABI_FP_16bit_format),
17796 LOOKUP(42, MPextension_use),
17797 LOOKUP(44, DIV_use),
17798 LOOKUP(46, DSP_extension),
17799 LOOKUP(48, MVE_arch),
17800 LOOKUP(50, PAC_extension),
17801 LOOKUP(52, BTI_extension),
17802 LOOKUP(74, BTI_use),
17803 LOOKUP(76, PACRET_use),
17804 {64, "nodefaults", 0, NULL},
17805 {65, "also_compatible_with", 0, NULL},
17806 LOOKUP(66, T2EE_use),
17807 {67, "conformance", 1, NULL},
17808 LOOKUP(68, Virtualization_use),
17809 LOOKUP(70, MPextension_use_legacy)
17810};
17811#undef LOOKUP
17812
17813static unsigned char *
17814display_arm_attribute (unsigned char * p,
17815 const unsigned char * const end)
17816{
17817 unsigned int tag;
17818 unsigned int val;
17819 arm_attr_public_tag * attr;
17820 unsigned i;
17821 unsigned int type;
17822
17823 READ_ULEB (tag, p, end);
17824 attr = NULL;
17825 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
17826 {
17827 if (arm_attr_public_tags[i].tag == tag)
17828 {
17829 attr = &arm_attr_public_tags[i];
17830 break;
17831 }
17832 }
17833
17834 if (attr)
17835 {
17836 printf (" Tag_%s: ", attr->name);
17837 switch (attr->type)
17838 {
17839 case 0:
17840 switch (tag)
17841 {
17842 case 7: /* Tag_CPU_arch_profile. */
17843 READ_ULEB (val, p, end);
17844 switch (val)
17845 {
17846 case 0: printf (_("None\n")); break;
17847 case 'A': printf (_("Application\n")); break;
17848 case 'R': printf (_("Realtime\n")); break;
17849 case 'M': printf (_("Microcontroller\n")); break;
17850 case 'S': printf (_("Application or Realtime\n")); break;
17851 default: printf ("??? (%d)\n", val); break;
17852 }
17853 break;
17854
17855 case 24: /* Tag_align_needed. */
17856 READ_ULEB (val, p, end);
17857 switch (val)
17858 {
17859 case 0: printf (_("None\n")); break;
17860 case 1: printf (_("8-byte\n")); break;
17861 case 2: printf (_("4-byte\n")); break;
17862 case 3: printf ("??? 3\n"); break;
17863 default:
17864 if (val <= 12)
17865 printf (_("8-byte and up to %d-byte extended\n"),
17866 1 << val);
17867 else
17868 printf ("??? (%d)\n", val);
17869 break;
17870 }
17871 break;
17872
17873 case 25: /* Tag_align_preserved. */
17874 READ_ULEB (val, p, end);
17875 switch (val)
17876 {
17877 case 0: printf (_("None\n")); break;
17878 case 1: printf (_("8-byte, except leaf SP\n")); break;
17879 case 2: printf (_("8-byte\n")); break;
17880 case 3: printf ("??? 3\n"); break;
17881 default:
17882 if (val <= 12)
17883 printf (_("8-byte and up to %d-byte extended\n"),
17884 1 << val);
17885 else
17886 printf ("??? (%d)\n", val);
17887 break;
17888 }
17889 break;
17890
17891 case 32: /* Tag_compatibility. */
17892 {
17893 READ_ULEB (val, p, end);
17894 printf (_("flag = %d, vendor = "), val);
17895 if (p < end - 1)
17896 {
17897 size_t maxlen = (end - p) - 1;
17898
17899 print_symbol_name ((int) maxlen, (const char *) p);
17900 p += strnlen ((char *) p, maxlen) + 1;
17901 }
17902 else
17903 {
17904 printf (_("<corrupt>"));
17905 p = (unsigned char *) end;
17906 }
17907 putchar ('\n');
17908 }
17909 break;
17910
17911 case 64: /* Tag_nodefaults. */
17912 /* PR 17531: file: 001-505008-0.01. */
17913 if (p < end)
17914 p++;
17915 printf (_("True\n"));
17916 break;
17917
17918 case 65: /* Tag_also_compatible_with. */
17919 READ_ULEB (val, p, end);
17920 if (val == 6 /* Tag_CPU_arch. */)
17921 {
17922 READ_ULEB (val, p, end);
17923 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
17924 printf ("??? (%d)\n", val);
17925 else
17926 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
17927 }
17928 else
17929 printf ("???\n");
17930 while (p < end && *(p++) != '\0' /* NUL terminator. */)
17931 ;
17932 break;
17933
17934 default:
17935 printf (_("<unknown: %d>\n"), tag);
17936 break;
17937 }
17938 return p;
17939
17940 case 1:
17941 return display_tag_value (-1, p, end);
17942 case 2:
17943 return display_tag_value (0, p, end);
17944
17945 default:
17946 assert (attr->type & 0x80);
17947 READ_ULEB (val, p, end);
17948 type = attr->type & 0x7f;
17949 if (val >= type)
17950 printf ("??? (%d)\n", val);
17951 else
17952 printf ("%s\n", attr->table[val]);
17953 return p;
17954 }
17955 }
17956
17957 return display_tag_value (tag, p, end);
17958}
17959
17960static unsigned char *
17961display_gnu_attribute (unsigned char * p,
17962 unsigned char * (* display_proc_gnu_attribute)
17963 (unsigned char *, unsigned int, const unsigned char * const),
17964 const unsigned char * const end)
17965{
17966 unsigned int tag;
17967 unsigned int val;
17968
17969 READ_ULEB (tag, p, end);
17970
17971 /* Tag_compatibility is the only generic GNU attribute defined at
17972 present. */
17973 if (tag == 32)
17974 {
17975 READ_ULEB (val, p, end);
17976
17977 printf (_("flag = %d, vendor = "), val);
17978 if (p == end)
17979 {
17980 printf (_("<corrupt>\n"));
17981 warn (_("corrupt vendor attribute\n"));
17982 }
17983 else
17984 {
17985 if (p < end - 1)
17986 {
17987 size_t maxlen = (end - p) - 1;
17988
17989 print_symbol_name ((int) maxlen, (const char *) p);
17990 p += strnlen ((char *) p, maxlen) + 1;
17991 }
17992 else
17993 {
17994 printf (_("<corrupt>"));
17995 p = (unsigned char *) end;
17996 }
17997 putchar ('\n');
17998 }
17999 return p;
18000 }
18001
18002 if ((tag & 2) == 0 && display_proc_gnu_attribute)
18003 return display_proc_gnu_attribute (p, tag, end);
18004
18005 return display_tag_value (tag, p, end);
18006}
18007
18008static unsigned char *
18009display_m68k_gnu_attribute (unsigned char * p,
18010 unsigned int tag,
18011 const unsigned char * const end)
18012{
18013 unsigned int val;
18014
18015 if (tag == Tag_GNU_M68K_ABI_FP)
18016 {
18017 printf (" Tag_GNU_M68K_ABI_FP: ");
18018 if (p == end)
18019 {
18020 printf (_("<corrupt>\n"));
18021 return p;
18022 }
18023 READ_ULEB (val, p, end);
18024
18025 if (val > 3)
18026 printf ("(%#x), ", val);
18027
18028 switch (val & 3)
18029 {
18030 case 0:
18031 printf (_("unspecified hard/soft float\n"));
18032 break;
18033 case 1:
18034 printf (_("hard float\n"));
18035 break;
18036 case 2:
18037 printf (_("soft float\n"));
18038 break;
18039 }
18040 return p;
18041 }
18042
18043 return display_tag_value (tag & 1, p, end);
18044}
18045
18046static unsigned char *
18047display_power_gnu_attribute (unsigned char * p,
18048 unsigned int tag,
18049 const unsigned char * const end)
18050{
18051 unsigned int val;
18052
18053 if (tag == Tag_GNU_Power_ABI_FP)
18054 {
18055 printf (" Tag_GNU_Power_ABI_FP: ");
18056 if (p == end)
18057 {
18058 printf (_("<corrupt>\n"));
18059 return p;
18060 }
18061 READ_ULEB (val, p, end);
18062
18063 if (val > 15)
18064 printf ("(%#x), ", val);
18065
18066 switch (val & 3)
18067 {
18068 case 0:
18069 printf (_("unspecified hard/soft float, "));
18070 break;
18071 case 1:
18072 printf (_("hard float, "));
18073 break;
18074 case 2:
18075 printf (_("soft float, "));
18076 break;
18077 case 3:
18078 printf (_("single-precision hard float, "));
18079 break;
18080 }
18081
18082 switch (val & 0xC)
18083 {
18084 case 0:
18085 printf (_("unspecified long double\n"));
18086 break;
18087 case 4:
18088 printf (_("128-bit IBM long double\n"));
18089 break;
18090 case 8:
18091 printf (_("64-bit long double\n"));
18092 break;
18093 case 12:
18094 printf (_("128-bit IEEE long double\n"));
18095 break;
18096 }
18097 return p;
18098 }
18099
18100 if (tag == Tag_GNU_Power_ABI_Vector)
18101 {
18102 printf (" Tag_GNU_Power_ABI_Vector: ");
18103 if (p == end)
18104 {
18105 printf (_("<corrupt>\n"));
18106 return p;
18107 }
18108 READ_ULEB (val, p, end);
18109
18110 if (val > 3)
18111 printf ("(%#x), ", val);
18112
18113 switch (val & 3)
18114 {
18115 case 0:
18116 printf (_("unspecified\n"));
18117 break;
18118 case 1:
18119 printf (_("generic\n"));
18120 break;
18121 case 2:
18122 printf ("AltiVec\n");
18123 break;
18124 case 3:
18125 printf ("SPE\n");
18126 break;
18127 }
18128 return p;
18129 }
18130
18131 if (tag == Tag_GNU_Power_ABI_Struct_Return)
18132 {
18133 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
18134 if (p == end)
18135 {
18136 printf (_("<corrupt>\n"));
18137 return p;
18138 }
18139 READ_ULEB (val, p, end);
18140
18141 if (val > 2)
18142 printf ("(%#x), ", val);
18143
18144 switch (val & 3)
18145 {
18146 case 0:
18147 printf (_("unspecified\n"));
18148 break;
18149 case 1:
18150 printf ("r3/r4\n");
18151 break;
18152 case 2:
18153 printf (_("memory\n"));
18154 break;
18155 case 3:
18156 printf ("???\n");
18157 break;
18158 }
18159 return p;
18160 }
18161
18162 return display_tag_value (tag & 1, p, end);
18163}
18164
18165static unsigned char *
18166display_s390_gnu_attribute (unsigned char * p,
18167 unsigned int tag,
18168 const unsigned char * const end)
18169{
18170 unsigned int val;
18171
18172 if (tag == Tag_GNU_S390_ABI_Vector)
18173 {
18174 printf (" Tag_GNU_S390_ABI_Vector: ");
18175 READ_ULEB (val, p, end);
18176
18177 switch (val)
18178 {
18179 case 0:
18180 printf (_("any\n"));
18181 break;
18182 case 1:
18183 printf (_("software\n"));
18184 break;
18185 case 2:
18186 printf (_("hardware\n"));
18187 break;
18188 default:
18189 printf ("??? (%d)\n", val);
18190 break;
18191 }
18192 return p;
18193 }
18194
18195 return display_tag_value (tag & 1, p, end);
18196}
18197
18198static void
18199display_sparc_hwcaps (unsigned int mask)
18200{
18201 if (mask)
18202 {
18203 bool first = true;
18204
18205 if (mask & ELF_SPARC_HWCAP_MUL32)
18206 fputs ("mul32", stdout), first = false;
18207 if (mask & ELF_SPARC_HWCAP_DIV32)
18208 printf ("%sdiv32", first ? "" : "|"), first = false;
18209 if (mask & ELF_SPARC_HWCAP_FSMULD)
18210 printf ("%sfsmuld", first ? "" : "|"), first = false;
18211 if (mask & ELF_SPARC_HWCAP_V8PLUS)
18212 printf ("%sv8plus", first ? "" : "|"), first = false;
18213 if (mask & ELF_SPARC_HWCAP_POPC)
18214 printf ("%spopc", first ? "" : "|"), first = false;
18215 if (mask & ELF_SPARC_HWCAP_VIS)
18216 printf ("%svis", first ? "" : "|"), first = false;
18217 if (mask & ELF_SPARC_HWCAP_VIS2)
18218 printf ("%svis2", first ? "" : "|"), first = false;
18219 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
18220 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
18221 if (mask & ELF_SPARC_HWCAP_FMAF)
18222 printf ("%sfmaf", first ? "" : "|"), first = false;
18223 if (mask & ELF_SPARC_HWCAP_VIS3)
18224 printf ("%svis3", first ? "" : "|"), first = false;
18225 if (mask & ELF_SPARC_HWCAP_HPC)
18226 printf ("%shpc", first ? "" : "|"), first = false;
18227 if (mask & ELF_SPARC_HWCAP_RANDOM)
18228 printf ("%srandom", first ? "" : "|"), first = false;
18229 if (mask & ELF_SPARC_HWCAP_TRANS)
18230 printf ("%strans", first ? "" : "|"), first = false;
18231 if (mask & ELF_SPARC_HWCAP_FJFMAU)
18232 printf ("%sfjfmau", first ? "" : "|"), first = false;
18233 if (mask & ELF_SPARC_HWCAP_IMA)
18234 printf ("%sima", first ? "" : "|"), first = false;
18235 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
18236 printf ("%scspare", first ? "" : "|"), first = false;
18237 }
18238 else
18239 fputc ('0', stdout);
18240 fputc ('\n', stdout);
18241}
18242
18243static void
18244display_sparc_hwcaps2 (unsigned int mask)
18245{
18246 if (mask)
18247 {
18248 bool first = true;
18249
18250 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
18251 fputs ("fjathplus", stdout), first = false;
18252 if (mask & ELF_SPARC_HWCAP2_VIS3B)
18253 printf ("%svis3b", first ? "" : "|"), first = false;
18254 if (mask & ELF_SPARC_HWCAP2_ADP)
18255 printf ("%sadp", first ? "" : "|"), first = false;
18256 if (mask & ELF_SPARC_HWCAP2_SPARC5)
18257 printf ("%ssparc5", first ? "" : "|"), first = false;
18258 if (mask & ELF_SPARC_HWCAP2_MWAIT)
18259 printf ("%smwait", first ? "" : "|"), first = false;
18260 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
18261 printf ("%sxmpmul", first ? "" : "|"), first = false;
18262 if (mask & ELF_SPARC_HWCAP2_XMONT)
18263 printf ("%sxmont2", first ? "" : "|"), first = false;
18264 if (mask & ELF_SPARC_HWCAP2_NSEC)
18265 printf ("%snsec", first ? "" : "|"), first = false;
18266 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
18267 printf ("%sfjathhpc", first ? "" : "|"), first = false;
18268 if (mask & ELF_SPARC_HWCAP2_FJDES)
18269 printf ("%sfjdes", first ? "" : "|"), first = false;
18270 if (mask & ELF_SPARC_HWCAP2_FJAES)
18271 printf ("%sfjaes", first ? "" : "|"), first = false;
18272 }
18273 else
18274 fputc ('0', stdout);
18275 fputc ('\n', stdout);
18276}
18277
18278static unsigned char *
18279display_sparc_gnu_attribute (unsigned char * p,
18280 unsigned int tag,
18281 const unsigned char * const end)
18282{
18283 unsigned int val;
18284
18285 if (tag == Tag_GNU_Sparc_HWCAPS)
18286 {
18287 READ_ULEB (val, p, end);
18288 printf (" Tag_GNU_Sparc_HWCAPS: ");
18289 display_sparc_hwcaps (val);
18290 return p;
18291 }
18292 if (tag == Tag_GNU_Sparc_HWCAPS2)
18293 {
18294 READ_ULEB (val, p, end);
18295 printf (" Tag_GNU_Sparc_HWCAPS2: ");
18296 display_sparc_hwcaps2 (val);
18297 return p;
18298 }
18299
18300 return display_tag_value (tag, p, end);
18301}
18302
18303static void
18304print_mips_fp_abi_value (unsigned int val)
18305{
18306 switch (val)
18307 {
18308 case Val_GNU_MIPS_ABI_FP_ANY:
18309 printf (_("Hard or soft float\n"));
18310 break;
18311 case Val_GNU_MIPS_ABI_FP_DOUBLE:
18312 printf (_("Hard float (double precision)\n"));
18313 break;
18314 case Val_GNU_MIPS_ABI_FP_SINGLE:
18315 printf (_("Hard float (single precision)\n"));
18316 break;
18317 case Val_GNU_MIPS_ABI_FP_SOFT:
18318 printf (_("Soft float\n"));
18319 break;
18320 case Val_GNU_MIPS_ABI_FP_OLD_64:
18321 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
18322 break;
18323 case Val_GNU_MIPS_ABI_FP_XX:
18324 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
18325 break;
18326 case Val_GNU_MIPS_ABI_FP_64:
18327 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
18328 break;
18329 case Val_GNU_MIPS_ABI_FP_64A:
18330 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
18331 break;
18332 case Val_GNU_MIPS_ABI_FP_NAN2008:
18333 printf (_("NaN 2008 compatibility\n"));
18334 break;
18335 default:
18336 printf ("??? (%d)\n", val);
18337 break;
18338 }
18339}
18340
18341static unsigned char *
18342display_mips_gnu_attribute (unsigned char * p,
18343 unsigned int tag,
18344 const unsigned char * const end)
18345{
18346 if (tag == Tag_GNU_MIPS_ABI_FP)
18347 {
18348 unsigned int val;
18349
18350 printf (" Tag_GNU_MIPS_ABI_FP: ");
18351 READ_ULEB (val, p, end);
18352 print_mips_fp_abi_value (val);
18353 return p;
18354 }
18355
18356 if (tag == Tag_GNU_MIPS_ABI_MSA)
18357 {
18358 unsigned int val;
18359
18360 printf (" Tag_GNU_MIPS_ABI_MSA: ");
18361 READ_ULEB (val, p, end);
18362
18363 switch (val)
18364 {
18365 case Val_GNU_MIPS_ABI_MSA_ANY:
18366 printf (_("Any MSA or not\n"));
18367 break;
18368 case Val_GNU_MIPS_ABI_MSA_128:
18369 printf (_("128-bit MSA\n"));
18370 break;
18371 default:
18372 printf ("??? (%d)\n", val);
18373 break;
18374 }
18375 return p;
18376 }
18377
18378 return display_tag_value (tag & 1, p, end);
18379}
18380
18381static unsigned char *
18382display_tic6x_attribute (unsigned char * p,
18383 const unsigned char * const end)
18384{
18385 unsigned int tag;
18386 unsigned int val;
18387
18388 READ_ULEB (tag, p, end);
18389
18390 switch (tag)
18391 {
18392 case Tag_ISA:
18393 printf (" Tag_ISA: ");
18394 READ_ULEB (val, p, end);
18395
18396 switch (val)
18397 {
18398 case C6XABI_Tag_ISA_none:
18399 printf (_("None\n"));
18400 break;
18401 case C6XABI_Tag_ISA_C62X:
18402 printf ("C62x\n");
18403 break;
18404 case C6XABI_Tag_ISA_C67X:
18405 printf ("C67x\n");
18406 break;
18407 case C6XABI_Tag_ISA_C67XP:
18408 printf ("C67x+\n");
18409 break;
18410 case C6XABI_Tag_ISA_C64X:
18411 printf ("C64x\n");
18412 break;
18413 case C6XABI_Tag_ISA_C64XP:
18414 printf ("C64x+\n");
18415 break;
18416 case C6XABI_Tag_ISA_C674X:
18417 printf ("C674x\n");
18418 break;
18419 default:
18420 printf ("??? (%d)\n", val);
18421 break;
18422 }
18423 return p;
18424
18425 case Tag_ABI_wchar_t:
18426 printf (" Tag_ABI_wchar_t: ");
18427 READ_ULEB (val, p, end);
18428 switch (val)
18429 {
18430 case 0:
18431 printf (_("Not used\n"));
18432 break;
18433 case 1:
18434 printf (_("2 bytes\n"));
18435 break;
18436 case 2:
18437 printf (_("4 bytes\n"));
18438 break;
18439 default:
18440 printf ("??? (%d)\n", val);
18441 break;
18442 }
18443 return p;
18444
18445 case Tag_ABI_stack_align_needed:
18446 printf (" Tag_ABI_stack_align_needed: ");
18447 READ_ULEB (val, p, end);
18448 switch (val)
18449 {
18450 case 0:
18451 printf (_("8-byte\n"));
18452 break;
18453 case 1:
18454 printf (_("16-byte\n"));
18455 break;
18456 default:
18457 printf ("??? (%d)\n", val);
18458 break;
18459 }
18460 return p;
18461
18462 case Tag_ABI_stack_align_preserved:
18463 READ_ULEB (val, p, end);
18464 printf (" Tag_ABI_stack_align_preserved: ");
18465 switch (val)
18466 {
18467 case 0:
18468 printf (_("8-byte\n"));
18469 break;
18470 case 1:
18471 printf (_("16-byte\n"));
18472 break;
18473 default:
18474 printf ("??? (%d)\n", val);
18475 break;
18476 }
18477 return p;
18478
18479 case Tag_ABI_DSBT:
18480 READ_ULEB (val, p, end);
18481 printf (" Tag_ABI_DSBT: ");
18482 switch (val)
18483 {
18484 case 0:
18485 printf (_("DSBT addressing not used\n"));
18486 break;
18487 case 1:
18488 printf (_("DSBT addressing used\n"));
18489 break;
18490 default:
18491 printf ("??? (%d)\n", val);
18492 break;
18493 }
18494 return p;
18495
18496 case Tag_ABI_PID:
18497 READ_ULEB (val, p, end);
18498 printf (" Tag_ABI_PID: ");
18499 switch (val)
18500 {
18501 case 0:
18502 printf (_("Data addressing position-dependent\n"));
18503 break;
18504 case 1:
18505 printf (_("Data addressing position-independent, GOT near DP\n"));
18506 break;
18507 case 2:
18508 printf (_("Data addressing position-independent, GOT far from DP\n"));
18509 break;
18510 default:
18511 printf ("??? (%d)\n", val);
18512 break;
18513 }
18514 return p;
18515
18516 case Tag_ABI_PIC:
18517 READ_ULEB (val, p, end);
18518 printf (" Tag_ABI_PIC: ");
18519 switch (val)
18520 {
18521 case 0:
18522 printf (_("Code addressing position-dependent\n"));
18523 break;
18524 case 1:
18525 printf (_("Code addressing position-independent\n"));
18526 break;
18527 default:
18528 printf ("??? (%d)\n", val);
18529 break;
18530 }
18531 return p;
18532
18533 case Tag_ABI_array_object_alignment:
18534 READ_ULEB (val, p, end);
18535 printf (" Tag_ABI_array_object_alignment: ");
18536 switch (val)
18537 {
18538 case 0:
18539 printf (_("8-byte\n"));
18540 break;
18541 case 1:
18542 printf (_("4-byte\n"));
18543 break;
18544 case 2:
18545 printf (_("16-byte\n"));
18546 break;
18547 default:
18548 printf ("??? (%d)\n", val);
18549 break;
18550 }
18551 return p;
18552
18553 case Tag_ABI_array_object_align_expected:
18554 READ_ULEB (val, p, end);
18555 printf (" Tag_ABI_array_object_align_expected: ");
18556 switch (val)
18557 {
18558 case 0:
18559 printf (_("8-byte\n"));
18560 break;
18561 case 1:
18562 printf (_("4-byte\n"));
18563 break;
18564 case 2:
18565 printf (_("16-byte\n"));
18566 break;
18567 default:
18568 printf ("??? (%d)\n", val);
18569 break;
18570 }
18571 return p;
18572
18573 case Tag_ABI_compatibility:
18574 {
18575 READ_ULEB (val, p, end);
18576 printf (" Tag_ABI_compatibility: ");
18577 printf (_("flag = %d, vendor = "), val);
18578 if (p < end - 1)
18579 {
18580 size_t maxlen = (end - p) - 1;
18581
18582 print_symbol_name ((int) maxlen, (const char *) p);
18583 p += strnlen ((char *) p, maxlen) + 1;
18584 }
18585 else
18586 {
18587 printf (_("<corrupt>"));
18588 p = (unsigned char *) end;
18589 }
18590 putchar ('\n');
18591 return p;
18592 }
18593
18594 case Tag_ABI_conformance:
18595 {
18596 printf (" Tag_ABI_conformance: \"");
18597 if (p < end - 1)
18598 {
18599 size_t maxlen = (end - p) - 1;
18600
18601 print_symbol_name ((int) maxlen, (const char *) p);
18602 p += strnlen ((char *) p, maxlen) + 1;
18603 }
18604 else
18605 {
18606 printf (_("<corrupt>"));
18607 p = (unsigned char *) end;
18608 }
18609 printf ("\"\n");
18610 return p;
18611 }
18612 }
18613
18614 return display_tag_value (tag, p, end);
18615}
18616
18617static void
18618display_raw_attribute (unsigned char * p, unsigned char const * const end)
18619{
18620 uint64_t addr = 0;
18621 size_t bytes = end - p;
18622
18623 assert (end >= p);
18624 while (bytes)
18625 {
18626 int j;
18627 int k;
18628 int lbytes = (bytes > 16 ? 16 : bytes);
18629
18630 printf (" 0x%8.8" PRIx64 " ", addr);
18631
18632 for (j = 0; j < 16; j++)
18633 {
18634 if (j < lbytes)
18635 printf ("%2.2x", p[j]);
18636 else
18637 printf (" ");
18638
18639 if ((j & 3) == 3)
18640 printf (" ");
18641 }
18642
18643 for (j = 0; j < lbytes; j++)
18644 {
18645 k = p[j];
18646 if (k >= ' ' && k < 0x7f)
18647 printf ("%c", k);
18648 else
18649 printf (".");
18650 }
18651
18652 putchar ('\n');
18653
18654 p += lbytes;
18655 bytes -= lbytes;
18656 addr += lbytes;
18657 }
18658
18659 putchar ('\n');
18660}
18661
18662static unsigned char *
18663display_msp430_attribute (unsigned char * p,
18664 const unsigned char * const end)
18665{
18666 uint64_t val;
18667 uint64_t tag;
18668
18669 READ_ULEB (tag, p, end);
18670
18671 switch (tag)
18672 {
18673 case OFBA_MSPABI_Tag_ISA:
18674 printf (" Tag_ISA: ");
18675 READ_ULEB (val, p, end);
18676 switch (val)
18677 {
18678 case 0: printf (_("None\n")); break;
18679 case 1: printf (_("MSP430\n")); break;
18680 case 2: printf (_("MSP430X\n")); break;
18681 default: printf ("??? (%" PRId64 ")\n", val); break;
18682 }
18683 break;
18684
18685 case OFBA_MSPABI_Tag_Code_Model:
18686 printf (" Tag_Code_Model: ");
18687 READ_ULEB (val, p, end);
18688 switch (val)
18689 {
18690 case 0: printf (_("None\n")); break;
18691 case 1: printf (_("Small\n")); break;
18692 case 2: printf (_("Large\n")); break;
18693 default: printf ("??? (%" PRId64 ")\n", val); break;
18694 }
18695 break;
18696
18697 case OFBA_MSPABI_Tag_Data_Model:
18698 printf (" Tag_Data_Model: ");
18699 READ_ULEB (val, p, end);
18700 switch (val)
18701 {
18702 case 0: printf (_("None\n")); break;
18703 case 1: printf (_("Small\n")); break;
18704 case 2: printf (_("Large\n")); break;
18705 case 3: printf (_("Restricted Large\n")); break;
18706 default: printf ("??? (%" PRId64 ")\n", val); break;
18707 }
18708 break;
18709
18710 default:
18711 printf (_(" <unknown tag %" PRId64 ">: "), tag);
18712
18713 if (tag & 1)
18714 {
18715 putchar ('"');
18716 if (p < end - 1)
18717 {
18718 size_t maxlen = (end - p) - 1;
18719
18720 print_symbol_name ((int) maxlen, (const char *) p);
18721 p += strnlen ((char *) p, maxlen) + 1;
18722 }
18723 else
18724 {
18725 printf (_("<corrupt>"));
18726 p = (unsigned char *) end;
18727 }
18728 printf ("\"\n");
18729 }
18730 else
18731 {
18732 READ_ULEB (val, p, end);
18733 printf ("%" PRId64 " (0x%" PRIx64 ")\n", val, val);
18734 }
18735 break;
18736 }
18737
18738 assert (p <= end);
18739 return p;
18740}
18741
18742static unsigned char *
18743display_msp430_gnu_attribute (unsigned char * p,
18744 unsigned int tag,
18745 const unsigned char * const end)
18746{
18747 if (tag == Tag_GNU_MSP430_Data_Region)
18748 {
18749 uint64_t val;
18750
18751 printf (" Tag_GNU_MSP430_Data_Region: ");
18752 READ_ULEB (val, p, end);
18753
18754 switch (val)
18755 {
18756 case Val_GNU_MSP430_Data_Region_Any:
18757 printf (_("Any Region\n"));
18758 break;
18759 case Val_GNU_MSP430_Data_Region_Lower:
18760 printf (_("Lower Region Only\n"));
18761 break;
18762 default:
18763 printf ("??? (%" PRIu64 ")\n", val);
18764 }
18765 return p;
18766 }
18767 return display_tag_value (tag & 1, p, end);
18768}
18769
18770struct riscv_attr_tag_t {
18771 const char *name;
18772 unsigned int tag;
18773};
18774
18775static struct riscv_attr_tag_t riscv_attr_tag[] =
18776{
18777#define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
18778 T(arch),
18779 T(priv_spec),
18780 T(priv_spec_minor),
18781 T(priv_spec_revision),
18782 T(unaligned_access),
18783 T(stack_align),
18784#undef T
18785};
18786
18787static unsigned char *
18788display_riscv_attribute (unsigned char *p,
18789 const unsigned char * const end)
18790{
18791 uint64_t val;
18792 uint64_t tag;
18793 struct riscv_attr_tag_t *attr = NULL;
18794 unsigned i;
18795
18796 READ_ULEB (tag, p, end);
18797
18798 /* Find the name of attribute. */
18799 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
18800 {
18801 if (riscv_attr_tag[i].tag == tag)
18802 {
18803 attr = &riscv_attr_tag[i];
18804 break;
18805 }
18806 }
18807
18808 if (attr)
18809 printf (" %s: ", attr->name);
18810 else
18811 return display_tag_value (tag, p, end);
18812
18813 switch (tag)
18814 {
18815 case Tag_RISCV_priv_spec:
18816 case Tag_RISCV_priv_spec_minor:
18817 case Tag_RISCV_priv_spec_revision:
18818 READ_ULEB (val, p, end);
18819 printf ("%" PRIu64 "\n", val);
18820 break;
18821 case Tag_RISCV_unaligned_access:
18822 READ_ULEB (val, p, end);
18823 switch (val)
18824 {
18825 case 0:
18826 printf (_("No unaligned access\n"));
18827 break;
18828 case 1:
18829 printf (_("Unaligned access\n"));
18830 break;
18831 }
18832 break;
18833 case Tag_RISCV_stack_align:
18834 READ_ULEB (val, p, end);
18835 printf (_("%" PRIu64 "-bytes\n"), val);
18836 break;
18837 case Tag_RISCV_arch:
18838 p = display_tag_value (-1, p, end);
18839 break;
18840 default:
18841 return display_tag_value (tag, p, end);
18842 }
18843
18844 return p;
18845}
18846
18847static unsigned char *
18848display_csky_attribute (unsigned char * p,
18849 const unsigned char * const end)
18850{
18851 uint64_t tag;
18852 uint64_t val;
18853 READ_ULEB (tag, p, end);
18854
18855 if (tag >= Tag_CSKY_MAX)
18856 {
18857 return display_tag_value (-1, p, end);
18858 }
18859
18860 switch (tag)
18861 {
18862 case Tag_CSKY_ARCH_NAME:
18863 printf (" Tag_CSKY_ARCH_NAME:\t\t");
18864 return display_tag_value (-1, p, end);
18865 case Tag_CSKY_CPU_NAME:
18866 printf (" Tag_CSKY_CPU_NAME:\t\t");
18867 return display_tag_value (-1, p, end);
18868
18869 case Tag_CSKY_ISA_FLAGS:
18870 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
18871 return display_tag_value (0, p, end);
18872 case Tag_CSKY_ISA_EXT_FLAGS:
18873 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
18874 return display_tag_value (0, p, end);
18875
18876 case Tag_CSKY_DSP_VERSION:
18877 printf (" Tag_CSKY_DSP_VERSION:\t\t");
18878 READ_ULEB (val, p, end);
18879 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
18880 printf ("DSP Extension\n");
18881 else if (val == VAL_CSKY_DSP_VERSION_2)
18882 printf ("DSP 2.0\n");
18883 break;
18884
18885 case Tag_CSKY_VDSP_VERSION:
18886 printf (" Tag_CSKY_VDSP_VERSION:\t");
18887 READ_ULEB (val, p, end);
18888 printf ("VDSP Version %" PRId64 "\n", val);
18889 break;
18890
18891 case Tag_CSKY_FPU_VERSION:
18892 printf (" Tag_CSKY_FPU_VERSION:\t\t");
18893 READ_ULEB (val, p, end);
18894 if (val == VAL_CSKY_FPU_VERSION_1)
18895 printf ("ABIV1 FPU Version 1\n");
18896 else if (val == VAL_CSKY_FPU_VERSION_2)
18897 printf ("FPU Version 2\n");
18898 break;
18899
18900 case Tag_CSKY_FPU_ABI:
18901 printf (" Tag_CSKY_FPU_ABI:\t\t");
18902 READ_ULEB (val, p, end);
18903 if (val == VAL_CSKY_FPU_ABI_HARD)
18904 printf ("Hard\n");
18905 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
18906 printf ("SoftFP\n");
18907 else if (val == VAL_CSKY_FPU_ABI_SOFT)
18908 printf ("Soft\n");
18909 break;
18910 case Tag_CSKY_FPU_ROUNDING:
18911 READ_ULEB (val, p, end);
18912 if (val == 1)
18913 {
18914 printf (" Tag_CSKY_FPU_ROUNDING:\t");
18915 printf ("Needed\n");
18916 }
18917 break;
18918 case Tag_CSKY_FPU_DENORMAL:
18919 READ_ULEB (val, p, end);
18920 if (val == 1)
18921 {
18922 printf (" Tag_CSKY_FPU_DENORMAL:\t");
18923 printf ("Needed\n");
18924 }
18925 break;
18926 case Tag_CSKY_FPU_Exception:
18927 READ_ULEB (val, p, end);
18928 if (val == 1)
18929 {
18930 printf (" Tag_CSKY_FPU_Exception:\t");
18931 printf ("Needed\n");
18932 }
18933 break;
18934 case Tag_CSKY_FPU_NUMBER_MODULE:
18935 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
18936 return display_tag_value (-1, p, end);
18937 case Tag_CSKY_FPU_HARDFP:
18938 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
18939 READ_ULEB (val, p, end);
18940 if (val & VAL_CSKY_FPU_HARDFP_HALF)
18941 printf (" Half");
18942 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
18943 printf (" Single");
18944 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
18945 printf (" Double");
18946 printf ("\n");
18947 break;
18948 default:
18949 return display_tag_value (tag, p, end);
18950 }
18951 return p;
18952}
18953
18954static bool
18955process_attributes (Filedata * filedata,
18956 const char * public_name,
18957 unsigned int proc_type,
18958 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
18959 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
18960{
18961 Elf_Internal_Shdr * sect;
18962 unsigned i;
18963 bool res = true;
18964
18965 /* Find the section header so that we get the size. */
18966 for (i = 0, sect = filedata->section_headers;
18967 i < filedata->file_header.e_shnum;
18968 i++, sect++)
18969 {
18970 unsigned char * contents;
18971 unsigned char * p;
18972
18973 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
18974 continue;
18975
18976 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
18977 sect->sh_size, _("attributes"));
18978 if (contents == NULL)
18979 {
18980 res = false;
18981 continue;
18982 }
18983
18984 p = contents;
18985 /* The first character is the version of the attributes.
18986 Currently only version 1, (aka 'A') is recognised here. */
18987 if (*p != 'A')
18988 {
18989 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
18990 res = false;
18991 }
18992 else
18993 {
18994 uint64_t section_len;
18995
18996 section_len = sect->sh_size - 1;
18997 p++;
18998
18999 while (section_len > 0)
19000 {
19001 uint64_t attr_len;
19002 unsigned int namelen;
19003 bool public_section;
19004 bool gnu_section;
19005
19006 if (section_len <= 4)
19007 {
19008 error (_("Tag section ends prematurely\n"));
19009 res = false;
19010 break;
19011 }
19012 attr_len = byte_get (p, 4);
19013 p += 4;
19014
19015 if (attr_len > section_len)
19016 {
19017 error (_("Bad attribute length (%u > %u)\n"),
19018 (unsigned) attr_len, (unsigned) section_len);
19019 attr_len = section_len;
19020 res = false;
19021 }
19022 /* PR 17531: file: 001-101425-0.004 */
19023 else if (attr_len < 5)
19024 {
19025 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
19026 res = false;
19027 break;
19028 }
19029
19030 section_len -= attr_len;
19031 attr_len -= 4;
19032
19033 namelen = strnlen ((char *) p, attr_len) + 1;
19034 if (namelen == 0 || namelen >= attr_len)
19035 {
19036 error (_("Corrupt attribute section name\n"));
19037 res = false;
19038 break;
19039 }
19040
19041 printf (_("Attribute Section: "));
19042 print_symbol_name (INT_MAX, (const char *) p);
19043 putchar ('\n');
19044
19045 if (public_name && streq ((char *) p, public_name))
19046 public_section = true;
19047 else
19048 public_section = false;
19049
19050 if (streq ((char *) p, "gnu"))
19051 gnu_section = true;
19052 else
19053 gnu_section = false;
19054
19055 p += namelen;
19056 attr_len -= namelen;
19057
19058 while (attr_len > 0 && p < contents + sect->sh_size)
19059 {
19060 int tag;
19061 unsigned int val;
19062 uint64_t size;
19063 unsigned char * end;
19064
19065 /* PR binutils/17531: Safe handling of corrupt files. */
19066 if (attr_len < 6)
19067 {
19068 error (_("Unused bytes at end of section\n"));
19069 res = false;
19070 section_len = 0;
19071 break;
19072 }
19073
19074 tag = *(p++);
19075 size = byte_get (p, 4);
19076 if (size > attr_len)
19077 {
19078 error (_("Bad subsection length (%u > %u)\n"),
19079 (unsigned) size, (unsigned) attr_len);
19080 res = false;
19081 size = attr_len;
19082 }
19083 /* PR binutils/17531: Safe handling of corrupt files. */
19084 if (size < 6)
19085 {
19086 error (_("Bad subsection length (%u < 6)\n"),
19087 (unsigned) size);
19088 res = false;
19089 section_len = 0;
19090 break;
19091 }
19092
19093 attr_len -= size;
19094 end = p + size - 1;
19095 assert (end <= contents + sect->sh_size);
19096 p += 4;
19097
19098 switch (tag)
19099 {
19100 case 1:
19101 printf (_("File Attributes\n"));
19102 break;
19103 case 2:
19104 printf (_("Section Attributes:"));
19105 goto do_numlist;
19106 case 3:
19107 printf (_("Symbol Attributes:"));
19108 /* Fall through. */
19109 do_numlist:
19110 for (;;)
19111 {
19112 READ_ULEB (val, p, end);
19113 if (val == 0)
19114 break;
19115 printf (" %d", val);
19116 }
19117 printf ("\n");
19118 break;
19119 default:
19120 printf (_("Unknown tag: %d\n"), tag);
19121 public_section = false;
19122 break;
19123 }
19124
19125 if (public_section && display_pub_attribute != NULL)
19126 {
19127 while (p < end)
19128 p = display_pub_attribute (p, end);
19129 assert (p == end);
19130 }
19131 else if (gnu_section && display_proc_gnu_attribute != NULL)
19132 {
19133 while (p < end)
19134 p = display_gnu_attribute (p,
19135 display_proc_gnu_attribute,
19136 end);
19137 assert (p == end);
19138 }
19139 else if (p < end)
19140 {
19141 printf (_(" Unknown attribute:\n"));
19142 display_raw_attribute (p, end);
19143 p = end;
19144 }
19145 else
19146 attr_len = 0;
19147 }
19148 }
19149 }
19150
19151 free (contents);
19152 }
19153
19154 return res;
19155}
19156
19157/* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
19158 Print the Address, Access and Initial fields of an entry at VMA ADDR
19159 and return the VMA of the next entry, or -1 if there was a problem.
19160 Does not read from DATA_END or beyond. */
19161
19162static uint64_t
19163print_mips_got_entry (unsigned char * data, uint64_t pltgot, uint64_t addr,
19164 unsigned char * data_end)
19165{
19166 printf (" ");
19167 print_vma (addr, LONG_HEX);
19168 printf (" ");
19169 if (addr < pltgot + 0xfff0)
19170 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
19171 else
19172 printf ("%10s", "");
19173 printf (" ");
19174 if (data == NULL)
19175 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
19176 else
19177 {
19178 uint64_t entry;
19179 unsigned char * from = data + addr - pltgot;
19180
19181 if (from + (is_32bit_elf ? 4 : 8) > data_end)
19182 {
19183 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
19184 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
19185 return (uint64_t) -1;
19186 }
19187 else
19188 {
19189 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
19190 print_vma (entry, LONG_HEX);
19191 }
19192 }
19193 return addr + (is_32bit_elf ? 4 : 8);
19194}
19195
19196/* DATA points to the contents of a MIPS PLT GOT that starts at VMA
19197 PLTGOT. Print the Address and Initial fields of an entry at VMA
19198 ADDR and return the VMA of the next entry. */
19199
19200static uint64_t
19201print_mips_pltgot_entry (unsigned char * data, uint64_t pltgot, uint64_t addr)
19202{
19203 printf (" ");
19204 print_vma (addr, LONG_HEX);
19205 printf (" ");
19206 if (data == NULL)
19207 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
19208 else
19209 {
19210 uint64_t entry;
19211
19212 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
19213 print_vma (entry, LONG_HEX);
19214 }
19215 return addr + (is_32bit_elf ? 4 : 8);
19216}
19217
19218static void
19219print_mips_ases (unsigned int mask)
19220{
19221 if (mask & AFL_ASE_DSP)
19222 fputs ("\n\tDSP ASE", stdout);
19223 if (mask & AFL_ASE_DSPR2)
19224 fputs ("\n\tDSP R2 ASE", stdout);
19225 if (mask & AFL_ASE_DSPR3)
19226 fputs ("\n\tDSP R3 ASE", stdout);
19227 if (mask & AFL_ASE_EVA)
19228 fputs ("\n\tEnhanced VA Scheme", stdout);
19229 if (mask & AFL_ASE_MCU)
19230 fputs ("\n\tMCU (MicroController) ASE", stdout);
19231 if (mask & AFL_ASE_MDMX)
19232 fputs ("\n\tMDMX ASE", stdout);
19233 if (mask & AFL_ASE_MIPS3D)
19234 fputs ("\n\tMIPS-3D ASE", stdout);
19235 if (mask & AFL_ASE_MT)
19236 fputs ("\n\tMT ASE", stdout);
19237 if (mask & AFL_ASE_SMARTMIPS)
19238 fputs ("\n\tSmartMIPS ASE", stdout);
19239 if (mask & AFL_ASE_VIRT)
19240 fputs ("\n\tVZ ASE", stdout);
19241 if (mask & AFL_ASE_MSA)
19242 fputs ("\n\tMSA ASE", stdout);
19243 if (mask & AFL_ASE_MIPS16)
19244 fputs ("\n\tMIPS16 ASE", stdout);
19245 if (mask & AFL_ASE_MICROMIPS)
19246 fputs ("\n\tMICROMIPS ASE", stdout);
19247 if (mask & AFL_ASE_XPA)
19248 fputs ("\n\tXPA ASE", stdout);
19249 if (mask & AFL_ASE_MIPS16E2)
19250 fputs ("\n\tMIPS16e2 ASE", stdout);
19251 if (mask & AFL_ASE_CRC)
19252 fputs ("\n\tCRC ASE", stdout);
19253 if (mask & AFL_ASE_GINV)
19254 fputs ("\n\tGINV ASE", stdout);
19255 if (mask & AFL_ASE_LOONGSON_MMI)
19256 fputs ("\n\tLoongson MMI ASE", stdout);
19257 if (mask & AFL_ASE_LOONGSON_CAM)
19258 fputs ("\n\tLoongson CAM ASE", stdout);
19259 if (mask & AFL_ASE_LOONGSON_EXT)
19260 fputs ("\n\tLoongson EXT ASE", stdout);
19261 if (mask & AFL_ASE_LOONGSON_EXT2)
19262 fputs ("\n\tLoongson EXT2 ASE", stdout);
19263 if (mask == 0)
19264 fprintf (stdout, "\n\t%s", _("None"));
19265 else if ((mask & ~AFL_ASE_MASK) != 0)
19266 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
19267}
19268
19269static void
19270print_mips_isa_ext (unsigned int isa_ext)
19271{
19272 switch (isa_ext)
19273 {
19274 case 0:
19275 fputs (_("None"), stdout);
19276 break;
19277 case AFL_EXT_XLR:
19278 fputs ("RMI XLR", stdout);
19279 break;
19280 case AFL_EXT_OCTEON3:
19281 fputs ("Cavium Networks Octeon3", stdout);
19282 break;
19283 case AFL_EXT_OCTEON2:
19284 fputs ("Cavium Networks Octeon2", stdout);
19285 break;
19286 case AFL_EXT_OCTEONP:
19287 fputs ("Cavium Networks OcteonP", stdout);
19288 break;
19289 case AFL_EXT_OCTEON:
19290 fputs ("Cavium Networks Octeon", stdout);
19291 break;
19292 case AFL_EXT_5900:
19293 fputs ("Toshiba R5900", stdout);
19294 break;
19295 case AFL_EXT_4650:
19296 fputs ("MIPS R4650", stdout);
19297 break;
19298 case AFL_EXT_4010:
19299 fputs ("LSI R4010", stdout);
19300 break;
19301 case AFL_EXT_4100:
19302 fputs ("NEC VR4100", stdout);
19303 break;
19304 case AFL_EXT_3900:
19305 fputs ("Toshiba R3900", stdout);
19306 break;
19307 case AFL_EXT_10000:
19308 fputs ("MIPS R10000", stdout);
19309 break;
19310 case AFL_EXT_SB1:
19311 fputs ("Broadcom SB-1", stdout);
19312 break;
19313 case AFL_EXT_4111:
19314 fputs ("NEC VR4111/VR4181", stdout);
19315 break;
19316 case AFL_EXT_4120:
19317 fputs ("NEC VR4120", stdout);
19318 break;
19319 case AFL_EXT_5400:
19320 fputs ("NEC VR5400", stdout);
19321 break;
19322 case AFL_EXT_5500:
19323 fputs ("NEC VR5500", stdout);
19324 break;
19325 case AFL_EXT_LOONGSON_2E:
19326 fputs ("ST Microelectronics Loongson 2E", stdout);
19327 break;
19328 case AFL_EXT_LOONGSON_2F:
19329 fputs ("ST Microelectronics Loongson 2F", stdout);
19330 break;
19331 case AFL_EXT_INTERAPTIV_MR2:
19332 fputs ("Imagination interAptiv MR2", stdout);
19333 break;
19334 default:
19335 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
19336 }
19337}
19338
19339static signed int
19340get_mips_reg_size (int reg_size)
19341{
19342 return (reg_size == AFL_REG_NONE) ? 0
19343 : (reg_size == AFL_REG_32) ? 32
19344 : (reg_size == AFL_REG_64) ? 64
19345 : (reg_size == AFL_REG_128) ? 128
19346 : -1;
19347}
19348
19349static bool
19350process_mips_specific (Filedata * filedata)
19351{
19352 Elf_Internal_Dyn * entry;
19353 Elf_Internal_Shdr *sect = NULL;
19354 size_t liblist_offset = 0;
19355 size_t liblistno = 0;
19356 size_t conflictsno = 0;
19357 size_t options_offset = 0;
19358 size_t conflicts_offset = 0;
19359 size_t pltrelsz = 0;
19360 size_t pltrel = 0;
19361 uint64_t pltgot = 0;
19362 uint64_t mips_pltgot = 0;
19363 uint64_t jmprel = 0;
19364 uint64_t local_gotno = 0;
19365 uint64_t gotsym = 0;
19366 uint64_t symtabno = 0;
19367 bool res = true;
19368
19369 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
19370 display_mips_gnu_attribute))
19371 res = false;
19372
19373 sect = find_section (filedata, ".MIPS.abiflags");
19374
19375 if (sect != NULL)
19376 {
19377 Elf_External_ABIFlags_v0 *abiflags_ext;
19378 Elf_Internal_ABIFlags_v0 abiflags_in;
19379
19380 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
19381 {
19382 error (_("Corrupt MIPS ABI Flags section.\n"));
19383 res = false;
19384 }
19385 else
19386 {
19387 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
19388 sect->sh_size, _("MIPS ABI Flags section"));
19389 if (abiflags_ext)
19390 {
19391 abiflags_in.version = BYTE_GET (abiflags_ext->version);
19392 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
19393 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
19394 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
19395 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
19396 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
19397 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
19398 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
19399 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
19400 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
19401 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
19402
19403 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
19404 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
19405 if (abiflags_in.isa_rev > 1)
19406 printf ("r%d", abiflags_in.isa_rev);
19407 printf ("\nGPR size: %d",
19408 get_mips_reg_size (abiflags_in.gpr_size));
19409 printf ("\nCPR1 size: %d",
19410 get_mips_reg_size (abiflags_in.cpr1_size));
19411 printf ("\nCPR2 size: %d",
19412 get_mips_reg_size (abiflags_in.cpr2_size));
19413 fputs ("\nFP ABI: ", stdout);
19414 print_mips_fp_abi_value (abiflags_in.fp_abi);
19415 fputs ("ISA Extension: ", stdout);
19416 print_mips_isa_ext (abiflags_in.isa_ext);
19417 fputs ("\nASEs:", stdout);
19418 print_mips_ases (abiflags_in.ases);
19419 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
19420 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
19421 fputc ('\n', stdout);
19422 free (abiflags_ext);
19423 }
19424 }
19425 }
19426
19427 /* We have a lot of special sections. Thanks SGI! */
19428 if (filedata->dynamic_section == NULL)
19429 {
19430 /* No dynamic information available. See if there is static GOT. */
19431 sect = find_section (filedata, ".got");
19432 if (sect != NULL)
19433 {
19434 unsigned char *data_end;
19435 unsigned char *data;
19436 uint64_t ent, end;
19437 int addr_size;
19438
19439 pltgot = sect->sh_addr;
19440
19441 ent = pltgot;
19442 addr_size = (is_32bit_elf ? 4 : 8);
19443 end = pltgot + sect->sh_size;
19444
19445 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
19446 end - pltgot, 1,
19447 _("Global Offset Table data"));
19448 /* PR 12855: Null data is handled gracefully throughout. */
19449 data_end = data + (end - pltgot);
19450
19451 printf (_("\nStatic GOT:\n"));
19452 printf (_(" Canonical gp value: "));
19453 print_vma (ent + 0x7ff0, LONG_HEX);
19454 printf ("\n\n");
19455
19456 /* In a dynamic binary GOT[0] is reserved for the dynamic
19457 loader to store the lazy resolver pointer, however in
19458 a static binary it may well have been omitted and GOT
19459 reduced to a table of addresses.
19460 PR 21344: Check for the entry being fully available
19461 before fetching it. */
19462 if (data
19463 && data + ent - pltgot + addr_size <= data_end
19464 && byte_get (data + ent - pltgot, addr_size) == 0)
19465 {
19466 printf (_(" Reserved entries:\n"));
19467 printf (_(" %*s %10s %*s\n"),
19468 addr_size * 2, _("Address"), _("Access"),
19469 addr_size * 2, _("Value"));
19470 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19471 printf ("\n");
19472 if (ent == (uint64_t) -1)
19473 goto sgot_print_fail;
19474
19475 /* Check for the MSB of GOT[1] being set, identifying a
19476 GNU object. This entry will be used by some runtime
19477 loaders, to store the module pointer. Otherwise this
19478 is an ordinary local entry.
19479 PR 21344: Check for the entry being fully available
19480 before fetching it. */
19481 if (data
19482 && data + ent - pltgot + addr_size <= data_end
19483 && (byte_get (data + ent - pltgot, addr_size)
19484 >> (addr_size * 8 - 1)) != 0)
19485 {
19486 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19487 printf ("\n");
19488 if (ent == (uint64_t) -1)
19489 goto sgot_print_fail;
19490 }
19491 printf ("\n");
19492 }
19493
19494 if (data != NULL && ent < end)
19495 {
19496 printf (_(" Local entries:\n"));
19497 printf (" %*s %10s %*s\n",
19498 addr_size * 2, _("Address"), _("Access"),
19499 addr_size * 2, _("Value"));
19500 while (ent < end)
19501 {
19502 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19503 printf ("\n");
19504 if (ent == (uint64_t) -1)
19505 goto sgot_print_fail;
19506 }
19507 printf ("\n");
19508 }
19509
19510 sgot_print_fail:
19511 free (data);
19512 }
19513 return res;
19514 }
19515
19516 for (entry = filedata->dynamic_section;
19517 /* PR 17531 file: 012-50589-0.004. */
19518 (entry < filedata->dynamic_section + filedata->dynamic_nent
19519 && entry->d_tag != DT_NULL);
19520 ++entry)
19521 switch (entry->d_tag)
19522 {
19523 case DT_MIPS_LIBLIST:
19524 liblist_offset
19525 = offset_from_vma (filedata, entry->d_un.d_val,
19526 liblistno * sizeof (Elf32_External_Lib));
19527 break;
19528 case DT_MIPS_LIBLISTNO:
19529 liblistno = entry->d_un.d_val;
19530 break;
19531 case DT_MIPS_OPTIONS:
19532 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
19533 break;
19534 case DT_MIPS_CONFLICT:
19535 conflicts_offset
19536 = offset_from_vma (filedata, entry->d_un.d_val,
19537 conflictsno * sizeof (Elf32_External_Conflict));
19538 break;
19539 case DT_MIPS_CONFLICTNO:
19540 conflictsno = entry->d_un.d_val;
19541 break;
19542 case DT_PLTGOT:
19543 pltgot = entry->d_un.d_ptr;
19544 break;
19545 case DT_MIPS_LOCAL_GOTNO:
19546 local_gotno = entry->d_un.d_val;
19547 break;
19548 case DT_MIPS_GOTSYM:
19549 gotsym = entry->d_un.d_val;
19550 break;
19551 case DT_MIPS_SYMTABNO:
19552 symtabno = entry->d_un.d_val;
19553 break;
19554 case DT_MIPS_PLTGOT:
19555 mips_pltgot = entry->d_un.d_ptr;
19556 break;
19557 case DT_PLTREL:
19558 pltrel = entry->d_un.d_val;
19559 break;
19560 case DT_PLTRELSZ:
19561 pltrelsz = entry->d_un.d_val;
19562 break;
19563 case DT_JMPREL:
19564 jmprel = entry->d_un.d_ptr;
19565 break;
19566 default:
19567 break;
19568 }
19569
19570 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
19571 {
19572 Elf32_External_Lib * elib;
19573 size_t cnt;
19574
19575 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
19576 sizeof (Elf32_External_Lib),
19577 liblistno,
19578 _("liblist section data"));
19579 if (elib)
19580 {
19581 printf (ngettext ("\nSection '.liblist' contains %zu entry:\n",
19582 "\nSection '.liblist' contains %zu entries:\n",
19583 liblistno),
19584 liblistno);
19585 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
19586 stdout);
19587
19588 for (cnt = 0; cnt < liblistno; ++cnt)
19589 {
19590 Elf32_Lib liblist;
19591 time_t atime;
19592 char timebuf[128];
19593 struct tm * tmp;
19594
19595 liblist.l_name = BYTE_GET (elib[cnt].l_name);
19596 atime = BYTE_GET (elib[cnt].l_time_stamp);
19597 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
19598 liblist.l_version = BYTE_GET (elib[cnt].l_version);
19599 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
19600
19601 tmp = gmtime (&atime);
19602 snprintf (timebuf, sizeof (timebuf),
19603 "%04u-%02u-%02uT%02u:%02u:%02u",
19604 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
19605 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
19606
19607 printf ("%3zu: ", cnt);
19608 if (valid_dynamic_name (filedata, liblist.l_name))
19609 print_symbol_name (20, get_dynamic_name (filedata, liblist.l_name));
19610 else
19611 printf (_("<corrupt: %9ld>"), liblist.l_name);
19612 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
19613 liblist.l_version);
19614
19615 if (liblist.l_flags == 0)
19616 puts (_(" NONE"));
19617 else
19618 {
19619 static const struct
19620 {
19621 const char * name;
19622 int bit;
19623 }
19624 l_flags_vals[] =
19625 {
19626 { " EXACT_MATCH", LL_EXACT_MATCH },
19627 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
19628 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
19629 { " EXPORTS", LL_EXPORTS },
19630 { " DELAY_LOAD", LL_DELAY_LOAD },
19631 { " DELTA", LL_DELTA }
19632 };
19633 int flags = liblist.l_flags;
19634 size_t fcnt;
19635
19636 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
19637 if ((flags & l_flags_vals[fcnt].bit) != 0)
19638 {
19639 fputs (l_flags_vals[fcnt].name, stdout);
19640 flags ^= l_flags_vals[fcnt].bit;
19641 }
19642 if (flags != 0)
19643 printf (" %#x", (unsigned int) flags);
19644
19645 puts ("");
19646 }
19647 }
19648
19649 free (elib);
19650 }
19651 else
19652 res = false;
19653 }
19654
19655 if (options_offset != 0)
19656 {
19657 Elf_External_Options * eopt;
19658 size_t offset;
19659 int cnt;
19660
19661 /* Find the section header so that we get the size. */
19662 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
19663 /* PR 17533 file: 012-277276-0.004. */
19664 if (sect == NULL)
19665 {
19666 error (_("No MIPS_OPTIONS header found\n"));
19667 return false;
19668 }
19669 /* PR 24243 */
19670 if (sect->sh_size < sizeof (* eopt))
19671 {
19672 error (_("The MIPS options section is too small.\n"));
19673 return false;
19674 }
19675
19676 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
19677 sect->sh_size, _("options"));
19678 if (eopt)
19679 {
19680 Elf_Internal_Options option;
19681
19682 offset = cnt = 0;
19683 while (offset <= sect->sh_size - sizeof (* eopt))
19684 {
19685 Elf_External_Options * eoption;
19686 unsigned int optsize;
19687
19688 eoption = (Elf_External_Options *) ((char *) eopt + offset);
19689
19690 optsize = BYTE_GET (eoption->size);
19691
19692 /* PR 17531: file: ffa0fa3b. */
19693 if (optsize < sizeof (* eopt)
19694 || optsize > sect->sh_size - offset)
19695 {
19696 error (_("Invalid size (%u) for MIPS option\n"),
19697 optsize);
19698 free (eopt);
19699 return false;
19700 }
19701 offset += optsize;
19702 ++cnt;
19703 }
19704
19705 printf (ngettext ("\nSection '%s' contains %d entry:\n",
19706 "\nSection '%s' contains %d entries:\n",
19707 cnt),
19708 printable_section_name (filedata, sect), cnt);
19709
19710 offset = 0;
19711 while (cnt-- > 0)
19712 {
19713 size_t len;
19714 Elf_External_Options * eoption;
19715
19716 eoption = (Elf_External_Options *) ((char *) eopt + offset);
19717
19718 option.kind = BYTE_GET (eoption->kind);
19719 option.size = BYTE_GET (eoption->size);
19720 option.section = BYTE_GET (eoption->section);
19721 option.info = BYTE_GET (eoption->info);
19722
19723 switch (option.kind)
19724 {
19725 case ODK_NULL:
19726 /* This shouldn't happen. */
19727 printf (" NULL %" PRId16 " %" PRIx32,
19728 option.section, option.info);
19729 break;
19730
19731 case ODK_REGINFO:
19732 printf (" REGINFO ");
19733 if (filedata->file_header.e_machine == EM_MIPS)
19734 {
19735 Elf32_External_RegInfo * ereg;
19736 Elf32_RegInfo reginfo;
19737
19738 /* 32bit form. */
19739 if (option.size < (sizeof (Elf_External_Options)
19740 + sizeof (Elf32_External_RegInfo)))
19741 {
19742 printf (_("<corrupt>\n"));
19743 error (_("Truncated MIPS REGINFO option\n"));
19744 cnt = 0;
19745 break;
19746 }
19747
19748 ereg = (Elf32_External_RegInfo *) (eoption + 1);
19749
19750 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
19751 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
19752 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
19753 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
19754 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
19755 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
19756
19757 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
19758 reginfo.ri_gprmask, reginfo.ri_gp_value);
19759 printf (" "
19760 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
19761 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
19762 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
19763 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
19764 }
19765 else
19766 {
19767 /* 64 bit form. */
19768 Elf64_External_RegInfo * ereg;
19769 Elf64_Internal_RegInfo reginfo;
19770
19771 if (option.size < (sizeof (Elf_External_Options)
19772 + sizeof (Elf64_External_RegInfo)))
19773 {
19774 printf (_("<corrupt>\n"));
19775 error (_("Truncated MIPS REGINFO option\n"));
19776 cnt = 0;
19777 break;
19778 }
19779
19780 ereg = (Elf64_External_RegInfo *) (eoption + 1);
19781 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
19782 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
19783 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
19784 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
19785 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
19786 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
19787
19788 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
19789 reginfo.ri_gprmask, reginfo.ri_gp_value);
19790 printf (" "
19791 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
19792 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
19793 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
19794 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
19795 }
19796 offset += option.size;
19797 continue;
19798
19799 case ODK_EXCEPTIONS:
19800 fputs (" EXCEPTIONS fpe_min(", stdout);
19801 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
19802 fputs (") fpe_max(", stdout);
19803 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
19804 fputs (")", stdout);
19805
19806 if (option.info & OEX_PAGE0)
19807 fputs (" PAGE0", stdout);
19808 if (option.info & OEX_SMM)
19809 fputs (" SMM", stdout);
19810 if (option.info & OEX_FPDBUG)
19811 fputs (" FPDBUG", stdout);
19812 if (option.info & OEX_DISMISS)
19813 fputs (" DISMISS", stdout);
19814 break;
19815
19816 case ODK_PAD:
19817 fputs (" PAD ", stdout);
19818 if (option.info & OPAD_PREFIX)
19819 fputs (" PREFIX", stdout);
19820 if (option.info & OPAD_POSTFIX)
19821 fputs (" POSTFIX", stdout);
19822 if (option.info & OPAD_SYMBOL)
19823 fputs (" SYMBOL", stdout);
19824 break;
19825
19826 case ODK_HWPATCH:
19827 fputs (" HWPATCH ", stdout);
19828 if (option.info & OHW_R4KEOP)
19829 fputs (" R4KEOP", stdout);
19830 if (option.info & OHW_R8KPFETCH)
19831 fputs (" R8KPFETCH", stdout);
19832 if (option.info & OHW_R5KEOP)
19833 fputs (" R5KEOP", stdout);
19834 if (option.info & OHW_R5KCVTL)
19835 fputs (" R5KCVTL", stdout);
19836 break;
19837
19838 case ODK_FILL:
19839 fputs (" FILL ", stdout);
19840 /* XXX Print content of info word? */
19841 break;
19842
19843 case ODK_TAGS:
19844 fputs (" TAGS ", stdout);
19845 /* XXX Print content of info word? */
19846 break;
19847
19848 case ODK_HWAND:
19849 fputs (" HWAND ", stdout);
19850 if (option.info & OHWA0_R4KEOP_CHECKED)
19851 fputs (" R4KEOP_CHECKED", stdout);
19852 if (option.info & OHWA0_R4KEOP_CLEAN)
19853 fputs (" R4KEOP_CLEAN", stdout);
19854 break;
19855
19856 case ODK_HWOR:
19857 fputs (" HWOR ", stdout);
19858 if (option.info & OHWA0_R4KEOP_CHECKED)
19859 fputs (" R4KEOP_CHECKED", stdout);
19860 if (option.info & OHWA0_R4KEOP_CLEAN)
19861 fputs (" R4KEOP_CLEAN", stdout);
19862 break;
19863
19864 case ODK_GP_GROUP:
19865 printf (" GP_GROUP %#06x self-contained %#06x",
19866 option.info & OGP_GROUP,
19867 (option.info & OGP_SELF) >> 16);
19868 break;
19869
19870 case ODK_IDENT:
19871 printf (" IDENT %#06x self-contained %#06x",
19872 option.info & OGP_GROUP,
19873 (option.info & OGP_SELF) >> 16);
19874 break;
19875
19876 default:
19877 /* This shouldn't happen. */
19878 printf (" %3d ??? %" PRId16 " %" PRIx32,
19879 option.kind, option.section, option.info);
19880 break;
19881 }
19882
19883 len = sizeof (* eopt);
19884 while (len < option.size)
19885 {
19886 unsigned char datum = *((unsigned char *) eoption + len);
19887
19888 if (ISPRINT (datum))
19889 printf ("%c", datum);
19890 else
19891 printf ("\\%03o", datum);
19892 len ++;
19893 }
19894 fputs ("\n", stdout);
19895
19896 offset += option.size;
19897 }
19898 free (eopt);
19899 }
19900 else
19901 res = false;
19902 }
19903
19904 if (conflicts_offset != 0 && conflictsno != 0)
19905 {
19906 Elf32_Conflict * iconf;
19907 size_t cnt;
19908
19909 if (filedata->dynamic_symbols == NULL)
19910 {
19911 error (_("conflict list found without a dynamic symbol table\n"));
19912 return false;
19913 }
19914
19915 /* PR 21345 - print a slightly more helpful error message
19916 if we are sure that the cmalloc will fail. */
19917 if (conflictsno > filedata->file_size / sizeof (* iconf))
19918 {
19919 error (_("Overlarge number of conflicts detected: %zx\n"),
19920 conflictsno);
19921 return false;
19922 }
19923
19924 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
19925 if (iconf == NULL)
19926 {
19927 error (_("Out of memory allocating space for dynamic conflicts\n"));
19928 return false;
19929 }
19930
19931 if (is_32bit_elf)
19932 {
19933 Elf32_External_Conflict * econf32;
19934
19935 econf32 = (Elf32_External_Conflict *)
19936 get_data (NULL, filedata, conflicts_offset,
19937 sizeof (*econf32), conflictsno, _("conflict"));
19938 if (!econf32)
19939 {
19940 free (iconf);
19941 return false;
19942 }
19943
19944 for (cnt = 0; cnt < conflictsno; ++cnt)
19945 iconf[cnt] = BYTE_GET (econf32[cnt]);
19946
19947 free (econf32);
19948 }
19949 else
19950 {
19951 Elf64_External_Conflict * econf64;
19952
19953 econf64 = (Elf64_External_Conflict *)
19954 get_data (NULL, filedata, conflicts_offset,
19955 sizeof (*econf64), conflictsno, _("conflict"));
19956 if (!econf64)
19957 {
19958 free (iconf);
19959 return false;
19960 }
19961
19962 for (cnt = 0; cnt < conflictsno; ++cnt)
19963 iconf[cnt] = BYTE_GET (econf64[cnt]);
19964
19965 free (econf64);
19966 }
19967
19968 printf (ngettext ("\nSection '.conflict' contains %zu entry:\n",
19969 "\nSection '.conflict' contains %zu entries:\n",
19970 conflictsno),
19971 conflictsno);
19972 puts (_(" Num: Index Value Name"));
19973
19974 for (cnt = 0; cnt < conflictsno; ++cnt)
19975 {
19976 printf ("%5zu: %8lu ", cnt, iconf[cnt]);
19977
19978 if (iconf[cnt] >= filedata->num_dynamic_syms)
19979 printf (_("<corrupt symbol index>"));
19980 else
19981 {
19982 Elf_Internal_Sym * psym;
19983
19984 psym = & filedata->dynamic_symbols[iconf[cnt]];
19985 print_vma (psym->st_value, FULL_HEX);
19986 putchar (' ');
19987 if (valid_dynamic_name (filedata, psym->st_name))
19988 print_symbol_name (25, get_dynamic_name (filedata, psym->st_name));
19989 else
19990 printf (_("<corrupt: %14ld>"), psym->st_name);
19991 }
19992 putchar ('\n');
19993 }
19994
19995 free (iconf);
19996 }
19997
19998 if (pltgot != 0 && local_gotno != 0)
19999 {
20000 uint64_t ent, local_end, global_end;
20001 size_t i, offset;
20002 unsigned char * data;
20003 unsigned char * data_end;
20004 int addr_size;
20005
20006 ent = pltgot;
20007 addr_size = (is_32bit_elf ? 4 : 8);
20008 local_end = pltgot + local_gotno * addr_size;
20009
20010 /* PR binutils/17533 file: 012-111227-0.004 */
20011 if (symtabno < gotsym)
20012 {
20013 error (_("The GOT symbol offset (%" PRIu64
20014 ") is greater than the symbol table size (%" PRIu64 ")\n"),
20015 gotsym, symtabno);
20016 return false;
20017 }
20018
20019 global_end = local_end + (symtabno - gotsym) * addr_size;
20020 /* PR 17531: file: 54c91a34. */
20021 if (global_end < local_end)
20022 {
20023 error (_("Too many GOT symbols: %" PRIu64 "\n"), symtabno);
20024 return false;
20025 }
20026
20027 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
20028 data = (unsigned char *) get_data (NULL, filedata, offset,
20029 global_end - pltgot, 1,
20030 _("Global Offset Table data"));
20031 /* PR 12855: Null data is handled gracefully throughout. */
20032 data_end = data + (global_end - pltgot);
20033
20034 printf (_("\nPrimary GOT:\n"));
20035 printf (_(" Canonical gp value: "));
20036 print_vma (pltgot + 0x7ff0, LONG_HEX);
20037 printf ("\n\n");
20038
20039 printf (_(" Reserved entries:\n"));
20040 printf (_(" %*s %10s %*s Purpose\n"),
20041 addr_size * 2, _("Address"), _("Access"),
20042 addr_size * 2, _("Initial"));
20043 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20044 printf (_(" Lazy resolver\n"));
20045 if (ent == (uint64_t) -1)
20046 goto got_print_fail;
20047
20048 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
20049 This entry will be used by some runtime loaders, to store the
20050 module pointer. Otherwise this is an ordinary local entry.
20051 PR 21344: Check for the entry being fully available before
20052 fetching it. */
20053 if (data
20054 && data + ent - pltgot + addr_size <= data_end
20055 && (byte_get (data + ent - pltgot, addr_size)
20056 >> (addr_size * 8 - 1)) != 0)
20057 {
20058 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20059 printf (_(" Module pointer (GNU extension)\n"));
20060 if (ent == (uint64_t) -1)
20061 goto got_print_fail;
20062 }
20063 printf ("\n");
20064
20065 if (data != NULL && ent < local_end)
20066 {
20067 printf (_(" Local entries:\n"));
20068 printf (" %*s %10s %*s\n",
20069 addr_size * 2, _("Address"), _("Access"),
20070 addr_size * 2, _("Initial"));
20071 while (ent < local_end)
20072 {
20073 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20074 printf ("\n");
20075 if (ent == (uint64_t) -1)
20076 goto got_print_fail;
20077 }
20078 printf ("\n");
20079 }
20080
20081 if (data != NULL && gotsym < symtabno)
20082 {
20083 int sym_width;
20084
20085 printf (_(" Global entries:\n"));
20086 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
20087 addr_size * 2, _("Address"),
20088 _("Access"),
20089 addr_size * 2, _("Initial"),
20090 addr_size * 2, _("Sym.Val."),
20091 _("Type"),
20092 /* Note for translators: "Ndx" = abbreviated form of "Index". */
20093 _("Ndx"), _("Name"));
20094
20095 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
20096
20097 for (i = gotsym; i < symtabno; i++)
20098 {
20099 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20100 printf (" ");
20101
20102 if (filedata->dynamic_symbols == NULL)
20103 printf (_("<no dynamic symbols>"));
20104 else if (i < filedata->num_dynamic_syms)
20105 {
20106 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
20107
20108 print_vma (psym->st_value, LONG_HEX);
20109 printf (" %-7s ", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
20110
20111 bool is_special;
20112 const char * s = printable_section_name_from_index (filedata, psym->st_shndx, & is_special);
20113 if (is_special)
20114 printf ("%3s ", s);
20115 else
20116 printf ("%3u ", psym->st_shndx);
20117
20118 if (valid_dynamic_name (filedata, psym->st_name))
20119 print_symbol_name (sym_width,
20120 get_dynamic_name (filedata, psym->st_name));
20121 else
20122 printf (_("<corrupt: %14ld>"), psym->st_name);
20123 }
20124 else
20125 printf (_("<symbol index %zu exceeds number of dynamic symbols>"),
20126 i);
20127
20128 printf ("\n");
20129 if (ent == (uint64_t) -1)
20130 break;
20131 }
20132 printf ("\n");
20133 }
20134
20135 got_print_fail:
20136 free (data);
20137 }
20138
20139 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
20140 {
20141 uint64_t ent, end;
20142 uint64_t offset, rel_offset;
20143 uint64_t count, i;
20144 unsigned char * data;
20145 int addr_size, sym_width;
20146 Elf_Internal_Rela * rels;
20147
20148 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
20149 if (pltrel == DT_RELA)
20150 {
20151 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
20152 return false;
20153 }
20154 else
20155 {
20156 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
20157 return false;
20158 }
20159
20160 ent = mips_pltgot;
20161 addr_size = (is_32bit_elf ? 4 : 8);
20162 end = mips_pltgot + (2 + count) * addr_size;
20163
20164 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
20165 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
20166 1, _("Procedure Linkage Table data"));
20167 if (data == NULL)
20168 {
20169 free (rels);
20170 return false;
20171 }
20172
20173 printf ("\nPLT GOT:\n\n");
20174 printf (_(" Reserved entries:\n"));
20175 printf (_(" %*s %*s Purpose\n"),
20176 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
20177 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
20178 printf (_(" PLT lazy resolver\n"));
20179 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
20180 printf (_(" Module pointer\n"));
20181 printf ("\n");
20182
20183 printf (_(" Entries:\n"));
20184 printf (" %*s %*s %*s %-7s %3s %s\n",
20185 addr_size * 2, _("Address"),
20186 addr_size * 2, _("Initial"),
20187 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
20188 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
20189 for (i = 0; i < count; i++)
20190 {
20191 uint64_t idx = get_reloc_symindex (rels[i].r_info);
20192
20193 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
20194 printf (" ");
20195
20196 if (idx >= filedata->num_dynamic_syms)
20197 printf (_("<corrupt symbol index: %" PRIu64 ">"), idx);
20198 else
20199 {
20200 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
20201
20202 print_vma (psym->st_value, LONG_HEX);
20203 printf (" %-7s %3s ",
20204 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
20205 printable_section_name_from_index (filedata, psym->st_shndx, NULL));
20206 if (valid_dynamic_name (filedata, psym->st_name))
20207 print_symbol_name (sym_width,
20208 get_dynamic_name (filedata, psym->st_name));
20209 else
20210 printf (_("<corrupt: %14ld>"), psym->st_name);
20211 }
20212 printf ("\n");
20213 }
20214 printf ("\n");
20215
20216 free (data);
20217 free (rels);
20218 }
20219
20220 return res;
20221}
20222
20223static bool
20224process_nds32_specific (Filedata * filedata)
20225{
20226 Elf_Internal_Shdr *sect = NULL;
20227
20228 sect = find_section (filedata, ".nds32_e_flags");
20229 if (sect != NULL && sect->sh_size >= 4)
20230 {
20231 unsigned char *buf;
20232 unsigned int flag;
20233
20234 printf ("\nNDS32 elf flags section:\n");
20235 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
20236 _("NDS32 elf flags section"));
20237
20238 if (buf == NULL)
20239 return false;
20240
20241 flag = byte_get (buf, 4);
20242 free (buf);
20243 switch (flag & 0x3)
20244 {
20245 case 0:
20246 printf ("(VEC_SIZE):\tNo entry.\n");
20247 break;
20248 case 1:
20249 printf ("(VEC_SIZE):\t4 bytes\n");
20250 break;
20251 case 2:
20252 printf ("(VEC_SIZE):\t16 bytes\n");
20253 break;
20254 case 3:
20255 printf ("(VEC_SIZE):\treserved\n");
20256 break;
20257 }
20258 }
20259
20260 return true;
20261}
20262
20263static bool
20264process_gnu_liblist (Filedata * filedata)
20265{
20266 Elf_Internal_Shdr * section;
20267 Elf_Internal_Shdr * string_sec;
20268 Elf32_External_Lib * elib;
20269 char * strtab;
20270 size_t strtab_size;
20271 size_t cnt;
20272 uint64_t num_liblist;
20273 unsigned i;
20274 bool res = true;
20275
20276 if (! do_arch)
20277 return true;
20278
20279 for (i = 0, section = filedata->section_headers;
20280 i < filedata->file_header.e_shnum;
20281 i++, section++)
20282 {
20283 switch (section->sh_type)
20284 {
20285 case SHT_GNU_LIBLIST:
20286 if (section->sh_link >= filedata->file_header.e_shnum)
20287 break;
20288
20289 elib = (Elf32_External_Lib *)
20290 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
20291 _("liblist section data"));
20292
20293 if (elib == NULL)
20294 {
20295 res = false;
20296 break;
20297 }
20298
20299 string_sec = filedata->section_headers + section->sh_link;
20300 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
20301 string_sec->sh_size,
20302 _("liblist string table"));
20303 if (strtab == NULL
20304 || section->sh_entsize != sizeof (Elf32_External_Lib))
20305 {
20306 free (elib);
20307 free (strtab);
20308 res = false;
20309 break;
20310 }
20311 strtab_size = string_sec->sh_size;
20312
20313 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
20314 printf (ngettext ("\nLibrary list section '%s' contains %" PRIu64
20315 " entries:\n",
20316 "\nLibrary list section '%s' contains %" PRIu64
20317 " entries:\n",
20318 num_liblist),
20319 printable_section_name (filedata, section),
20320 num_liblist);
20321
20322 puts (_(" Library Time Stamp Checksum Version Flags"));
20323
20324 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
20325 ++cnt)
20326 {
20327 Elf32_Lib liblist;
20328 time_t atime;
20329 char timebuf[128];
20330 struct tm * tmp;
20331
20332 liblist.l_name = BYTE_GET (elib[cnt].l_name);
20333 atime = BYTE_GET (elib[cnt].l_time_stamp);
20334 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
20335 liblist.l_version = BYTE_GET (elib[cnt].l_version);
20336 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
20337
20338 tmp = gmtime (&atime);
20339 snprintf (timebuf, sizeof (timebuf),
20340 "%04u-%02u-%02uT%02u:%02u:%02u",
20341 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
20342 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
20343
20344 printf ("%3zu: ", cnt);
20345 if (do_wide)
20346 printf ("%-20s", liblist.l_name < strtab_size
20347 ? strtab + liblist.l_name : _("<corrupt>"));
20348 else
20349 printf ("%-20.20s", liblist.l_name < strtab_size
20350 ? strtab + liblist.l_name : _("<corrupt>"));
20351 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
20352 liblist.l_version, liblist.l_flags);
20353 }
20354
20355 free (elib);
20356 free (strtab);
20357 }
20358 }
20359
20360 return res;
20361}
20362
20363static const char *
20364get_note_type (Filedata * filedata, unsigned e_type)
20365{
20366 static char buff[64];
20367
20368 if (filedata->file_header.e_type == ET_CORE)
20369 switch (e_type)
20370 {
20371 case NT_AUXV:
20372 return _("NT_AUXV (auxiliary vector)");
20373 case NT_PRSTATUS:
20374 return _("NT_PRSTATUS (prstatus structure)");
20375 case NT_FPREGSET:
20376 return _("NT_FPREGSET (floating point registers)");
20377 case NT_PRPSINFO:
20378 return _("NT_PRPSINFO (prpsinfo structure)");
20379 case NT_TASKSTRUCT:
20380 return _("NT_TASKSTRUCT (task structure)");
20381 case NT_GDB_TDESC:
20382 return _("NT_GDB_TDESC (GDB XML target description)");
20383 case NT_PRXFPREG:
20384 return _("NT_PRXFPREG (user_xfpregs structure)");
20385 case NT_PPC_VMX:
20386 return _("NT_PPC_VMX (ppc Altivec registers)");
20387 case NT_PPC_VSX:
20388 return _("NT_PPC_VSX (ppc VSX registers)");
20389 case NT_PPC_TAR:
20390 return _("NT_PPC_TAR (ppc TAR register)");
20391 case NT_PPC_PPR:
20392 return _("NT_PPC_PPR (ppc PPR register)");
20393 case NT_PPC_DSCR:
20394 return _("NT_PPC_DSCR (ppc DSCR register)");
20395 case NT_PPC_EBB:
20396 return _("NT_PPC_EBB (ppc EBB registers)");
20397 case NT_PPC_PMU:
20398 return _("NT_PPC_PMU (ppc PMU registers)");
20399 case NT_PPC_TM_CGPR:
20400 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
20401 case NT_PPC_TM_CFPR:
20402 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
20403 case NT_PPC_TM_CVMX:
20404 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
20405 case NT_PPC_TM_CVSX:
20406 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
20407 case NT_PPC_TM_SPR:
20408 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
20409 case NT_PPC_TM_CTAR:
20410 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
20411 case NT_PPC_TM_CPPR:
20412 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
20413 case NT_PPC_TM_CDSCR:
20414 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
20415 case NT_386_TLS:
20416 return _("NT_386_TLS (x86 TLS information)");
20417 case NT_386_IOPERM:
20418 return _("NT_386_IOPERM (x86 I/O permissions)");
20419 case NT_X86_XSTATE:
20420 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
20421 case NT_X86_CET:
20422 return _("NT_X86_CET (x86 CET state)");
20423 case NT_X86_SHSTK:
20424 return _("NT_X86_SHSTK (x86 SHSTK state)");
20425 case NT_S390_HIGH_GPRS:
20426 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
20427 case NT_S390_TIMER:
20428 return _("NT_S390_TIMER (s390 timer register)");
20429 case NT_S390_TODCMP:
20430 return _("NT_S390_TODCMP (s390 TOD comparator register)");
20431 case NT_S390_TODPREG:
20432 return _("NT_S390_TODPREG (s390 TOD programmable register)");
20433 case NT_S390_CTRS:
20434 return _("NT_S390_CTRS (s390 control registers)");
20435 case NT_S390_PREFIX:
20436 return _("NT_S390_PREFIX (s390 prefix register)");
20437 case NT_S390_LAST_BREAK:
20438 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
20439 case NT_S390_SYSTEM_CALL:
20440 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
20441 case NT_S390_TDB:
20442 return _("NT_S390_TDB (s390 transaction diagnostic block)");
20443 case NT_S390_VXRS_LOW:
20444 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
20445 case NT_S390_VXRS_HIGH:
20446 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
20447 case NT_S390_GS_CB:
20448 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
20449 case NT_S390_GS_BC:
20450 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
20451 case NT_ARM_VFP:
20452 return _("NT_ARM_VFP (arm VFP registers)");
20453 case NT_ARM_TLS:
20454 return _("NT_ARM_TLS (AArch TLS registers)");
20455 case NT_ARM_HW_BREAK:
20456 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
20457 case NT_ARM_HW_WATCH:
20458 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
20459 case NT_ARM_SYSTEM_CALL:
20460 return _("NT_ARM_SYSTEM_CALL (AArch system call number)");
20461 case NT_ARM_SVE:
20462 return _("NT_ARM_SVE (AArch SVE registers)");
20463 case NT_ARM_PAC_MASK:
20464 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
20465 case NT_ARM_PACA_KEYS:
20466 return _("NT_ARM_PACA_KEYS (ARM pointer authentication address keys)");
20467 case NT_ARM_PACG_KEYS:
20468 return _("NT_ARM_PACG_KEYS (ARM pointer authentication generic keys)");
20469 case NT_ARM_TAGGED_ADDR_CTRL:
20470 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
20471 case NT_ARM_SSVE:
20472 return _("NT_ARM_SSVE (AArch64 streaming SVE registers)");
20473 case NT_ARM_ZA:
20474 return _("NT_ARM_ZA (AArch64 SME ZA register)");
20475 case NT_ARM_ZT:
20476 return _("NT_ARM_ZT (AArch64 SME2 ZT registers)");
20477 case NT_ARM_PAC_ENABLED_KEYS:
20478 return _("NT_ARM_PAC_ENABLED_KEYS (AArch64 pointer authentication enabled keys)");
20479 case NT_ARC_V2:
20480 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
20481 case NT_RISCV_CSR:
20482 return _("NT_RISCV_CSR (RISC-V control and status registers)");
20483 case NT_PSTATUS:
20484 return _("NT_PSTATUS (pstatus structure)");
20485 case NT_FPREGS:
20486 return _("NT_FPREGS (floating point registers)");
20487 case NT_PSINFO:
20488 return _("NT_PSINFO (psinfo structure)");
20489 case NT_LWPSTATUS:
20490 return _("NT_LWPSTATUS (lwpstatus_t structure)");
20491 case NT_LWPSINFO:
20492 return _("NT_LWPSINFO (lwpsinfo_t structure)");
20493 case NT_WIN32PSTATUS:
20494 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
20495 case NT_SIGINFO:
20496 return _("NT_SIGINFO (siginfo_t data)");
20497 case NT_FILE:
20498 return _("NT_FILE (mapped files)");
20499 default:
20500 break;
20501 }
20502 else
20503 switch (e_type)
20504 {
20505 case NT_VERSION:
20506 return _("NT_VERSION (version)");
20507 case NT_ARCH:
20508 return _("NT_ARCH (architecture)");
20509 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
20510 return _("OPEN");
20511 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
20512 return _("func");
20513 case NT_GO_BUILDID:
20514 return _("GO BUILDID");
20515 case FDO_PACKAGING_METADATA:
20516 return _("FDO_PACKAGING_METADATA");
20517 case FDO_DLOPEN_METADATA:
20518 return _("FDO_DLOPEN_METADATA");
20519 default:
20520 break;
20521 }
20522
20523 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20524 return buff;
20525}
20526
20527static bool
20528print_core_note (Elf_Internal_Note *pnote)
20529{
20530 unsigned int addr_size = is_32bit_elf ? 4 : 8;
20531 uint64_t count, page_size;
20532 unsigned char *descdata, *filenames, *descend;
20533
20534 if (pnote->type != NT_FILE)
20535 {
20536 if (do_wide)
20537 printf ("\n");
20538 return true;
20539 }
20540
20541 if (pnote->descsz < 2 * addr_size)
20542 {
20543 error (_(" Malformed note - too short for header\n"));
20544 return false;
20545 }
20546
20547 descdata = (unsigned char *) pnote->descdata;
20548 descend = descdata + pnote->descsz;
20549
20550 if (descdata[pnote->descsz - 1] != '\0')
20551 {
20552 error (_(" Malformed note - does not end with \\0\n"));
20553 return false;
20554 }
20555
20556 count = byte_get (descdata, addr_size);
20557 descdata += addr_size;
20558
20559 page_size = byte_get (descdata, addr_size);
20560 descdata += addr_size;
20561
20562 if (count > ((uint64_t) -1 - 2 * addr_size) / (3 * addr_size)
20563 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
20564 {
20565 error (_(" Malformed note - too short for supplied file count\n"));
20566 return false;
20567 }
20568
20569 printf (_(" Page size: "));
20570 print_vma (page_size, DEC);
20571 printf ("\n");
20572
20573 printf (_(" %*s%*s%*s\n"),
20574 (int) (2 + 2 * addr_size), _("Start"),
20575 (int) (4 + 2 * addr_size), _("End"),
20576 (int) (4 + 2 * addr_size), _("Page Offset"));
20577 filenames = descdata + count * 3 * addr_size;
20578 while (count-- > 0)
20579 {
20580 uint64_t start, end, file_ofs;
20581
20582 if (filenames == descend)
20583 {
20584 error (_(" Malformed note - filenames end too early\n"));
20585 return false;
20586 }
20587
20588 start = byte_get (descdata, addr_size);
20589 descdata += addr_size;
20590 end = byte_get (descdata, addr_size);
20591 descdata += addr_size;
20592 file_ofs = byte_get (descdata, addr_size);
20593 descdata += addr_size;
20594
20595 printf (" ");
20596 print_vma (start, FULL_HEX);
20597 printf (" ");
20598 print_vma (end, FULL_HEX);
20599 printf (" ");
20600 print_vma (file_ofs, FULL_HEX);
20601 printf ("\n %s\n", filenames);
20602
20603 filenames += 1 + strlen ((char *) filenames);
20604 }
20605
20606 return true;
20607}
20608
20609static const char *
20610get_gnu_elf_note_type (unsigned e_type)
20611{
20612 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
20613 switch (e_type)
20614 {
20615 case NT_GNU_ABI_TAG:
20616 return _("NT_GNU_ABI_TAG (ABI version tag)");
20617 case NT_GNU_HWCAP:
20618 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
20619 case NT_GNU_BUILD_ID:
20620 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
20621 case NT_GNU_GOLD_VERSION:
20622 return _("NT_GNU_GOLD_VERSION (gold version)");
20623 case NT_GNU_PROPERTY_TYPE_0:
20624 return _("NT_GNU_PROPERTY_TYPE_0");
20625 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
20626 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
20627 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
20628 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
20629 default:
20630 {
20631 static char buff[64];
20632
20633 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20634 return buff;
20635 }
20636 }
20637}
20638
20639static void
20640decode_x86_compat_isa (unsigned int bitmask)
20641{
20642 while (bitmask)
20643 {
20644 unsigned int bit = bitmask & (- bitmask);
20645
20646 bitmask &= ~ bit;
20647 switch (bit)
20648 {
20649 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
20650 printf ("i486");
20651 break;
20652 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
20653 printf ("586");
20654 break;
20655 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
20656 printf ("686");
20657 break;
20658 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
20659 printf ("SSE");
20660 break;
20661 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
20662 printf ("SSE2");
20663 break;
20664 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
20665 printf ("SSE3");
20666 break;
20667 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
20668 printf ("SSSE3");
20669 break;
20670 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
20671 printf ("SSE4_1");
20672 break;
20673 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
20674 printf ("SSE4_2");
20675 break;
20676 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
20677 printf ("AVX");
20678 break;
20679 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
20680 printf ("AVX2");
20681 break;
20682 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
20683 printf ("AVX512F");
20684 break;
20685 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
20686 printf ("AVX512CD");
20687 break;
20688 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
20689 printf ("AVX512ER");
20690 break;
20691 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
20692 printf ("AVX512PF");
20693 break;
20694 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
20695 printf ("AVX512VL");
20696 break;
20697 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
20698 printf ("AVX512DQ");
20699 break;
20700 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
20701 printf ("AVX512BW");
20702 break;
20703 default:
20704 printf (_("<unknown: %x>"), bit);
20705 break;
20706 }
20707 if (bitmask)
20708 printf (", ");
20709 }
20710}
20711
20712static void
20713decode_x86_compat_2_isa (unsigned int bitmask)
20714{
20715 if (!bitmask)
20716 {
20717 printf (_("<None>"));
20718 return;
20719 }
20720
20721 while (bitmask)
20722 {
20723 unsigned int bit = bitmask & (- bitmask);
20724
20725 bitmask &= ~ bit;
20726 switch (bit)
20727 {
20728 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
20729 printf ("CMOV");
20730 break;
20731 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
20732 printf ("SSE");
20733 break;
20734 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
20735 printf ("SSE2");
20736 break;
20737 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
20738 printf ("SSE3");
20739 break;
20740 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
20741 printf ("SSSE3");
20742 break;
20743 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
20744 printf ("SSE4_1");
20745 break;
20746 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
20747 printf ("SSE4_2");
20748 break;
20749 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
20750 printf ("AVX");
20751 break;
20752 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
20753 printf ("AVX2");
20754 break;
20755 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
20756 printf ("FMA");
20757 break;
20758 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
20759 printf ("AVX512F");
20760 break;
20761 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
20762 printf ("AVX512CD");
20763 break;
20764 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
20765 printf ("AVX512ER");
20766 break;
20767 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
20768 printf ("AVX512PF");
20769 break;
20770 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
20771 printf ("AVX512VL");
20772 break;
20773 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
20774 printf ("AVX512DQ");
20775 break;
20776 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
20777 printf ("AVX512BW");
20778 break;
20779 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
20780 printf ("AVX512_4FMAPS");
20781 break;
20782 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
20783 printf ("AVX512_4VNNIW");
20784 break;
20785 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
20786 printf ("AVX512_BITALG");
20787 break;
20788 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
20789 printf ("AVX512_IFMA");
20790 break;
20791 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
20792 printf ("AVX512_VBMI");
20793 break;
20794 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
20795 printf ("AVX512_VBMI2");
20796 break;
20797 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
20798 printf ("AVX512_VNNI");
20799 break;
20800 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
20801 printf ("AVX512_BF16");
20802 break;
20803 default:
20804 printf (_("<unknown: %x>"), bit);
20805 break;
20806 }
20807 if (bitmask)
20808 printf (", ");
20809 }
20810}
20811
20812static const char *
20813get_amdgpu_elf_note_type (unsigned int e_type)
20814{
20815 switch (e_type)
20816 {
20817 case NT_AMDGPU_METADATA:
20818 return _("NT_AMDGPU_METADATA (code object metadata)");
20819 default:
20820 {
20821 static char buf[64];
20822 snprintf (buf, sizeof (buf), _("Unknown note type: (0x%08x)"), e_type);
20823 return buf;
20824 }
20825 }
20826}
20827
20828static void
20829decode_x86_isa (unsigned int bitmask)
20830{
20831 while (bitmask)
20832 {
20833 unsigned int bit = bitmask & (- bitmask);
20834
20835 bitmask &= ~ bit;
20836 switch (bit)
20837 {
20838 case GNU_PROPERTY_X86_ISA_1_BASELINE:
20839 printf ("x86-64-baseline");
20840 break;
20841 case GNU_PROPERTY_X86_ISA_1_V2:
20842 printf ("x86-64-v2");
20843 break;
20844 case GNU_PROPERTY_X86_ISA_1_V3:
20845 printf ("x86-64-v3");
20846 break;
20847 case GNU_PROPERTY_X86_ISA_1_V4:
20848 printf ("x86-64-v4");
20849 break;
20850 default:
20851 printf (_("<unknown: %x>"), bit);
20852 break;
20853 }
20854 if (bitmask)
20855 printf (", ");
20856 }
20857}
20858
20859static void
20860decode_x86_feature_1 (unsigned int bitmask)
20861{
20862 if (!bitmask)
20863 {
20864 printf (_("<None>"));
20865 return;
20866 }
20867
20868 while (bitmask)
20869 {
20870 unsigned int bit = bitmask & (- bitmask);
20871
20872 bitmask &= ~ bit;
20873 switch (bit)
20874 {
20875 case GNU_PROPERTY_X86_FEATURE_1_IBT:
20876 printf ("IBT");
20877 break;
20878 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
20879 printf ("SHSTK");
20880 break;
20881 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
20882 printf ("LAM_U48");
20883 break;
20884 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
20885 printf ("LAM_U57");
20886 break;
20887 default:
20888 printf (_("<unknown: %x>"), bit);
20889 break;
20890 }
20891 if (bitmask)
20892 printf (", ");
20893 }
20894}
20895
20896static void
20897decode_x86_feature_2 (unsigned int bitmask)
20898{
20899 if (!bitmask)
20900 {
20901 printf (_("<None>"));
20902 return;
20903 }
20904
20905 while (bitmask)
20906 {
20907 unsigned int bit = bitmask & (- bitmask);
20908
20909 bitmask &= ~ bit;
20910 switch (bit)
20911 {
20912 case GNU_PROPERTY_X86_FEATURE_2_X86:
20913 printf ("x86");
20914 break;
20915 case GNU_PROPERTY_X86_FEATURE_2_X87:
20916 printf ("x87");
20917 break;
20918 case GNU_PROPERTY_X86_FEATURE_2_MMX:
20919 printf ("MMX");
20920 break;
20921 case GNU_PROPERTY_X86_FEATURE_2_XMM:
20922 printf ("XMM");
20923 break;
20924 case GNU_PROPERTY_X86_FEATURE_2_YMM:
20925 printf ("YMM");
20926 break;
20927 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
20928 printf ("ZMM");
20929 break;
20930 case GNU_PROPERTY_X86_FEATURE_2_TMM:
20931 printf ("TMM");
20932 break;
20933 case GNU_PROPERTY_X86_FEATURE_2_MASK:
20934 printf ("MASK");
20935 break;
20936 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
20937 printf ("FXSR");
20938 break;
20939 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
20940 printf ("XSAVE");
20941 break;
20942 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
20943 printf ("XSAVEOPT");
20944 break;
20945 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
20946 printf ("XSAVEC");
20947 break;
20948 default:
20949 printf (_("<unknown: %x>"), bit);
20950 break;
20951 }
20952 if (bitmask)
20953 printf (", ");
20954 }
20955}
20956
20957static void
20958decode_aarch64_feature_1_and (unsigned int bitmask)
20959{
20960 while (bitmask)
20961 {
20962 unsigned int bit = bitmask & (- bitmask);
20963
20964 bitmask &= ~ bit;
20965 switch (bit)
20966 {
20967 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
20968 printf ("BTI");
20969 break;
20970
20971 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
20972 printf ("PAC");
20973 break;
20974
20975 default:
20976 printf (_("<unknown: %x>"), bit);
20977 break;
20978 }
20979 if (bitmask)
20980 printf (", ");
20981 }
20982}
20983
20984static void
20985decode_1_needed (unsigned int bitmask)
20986{
20987 while (bitmask)
20988 {
20989 unsigned int bit = bitmask & (- bitmask);
20990
20991 bitmask &= ~ bit;
20992 switch (bit)
20993 {
20994 case GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
20995 printf ("indirect external access");
20996 break;
20997 default:
20998 printf (_("<unknown: %x>"), bit);
20999 break;
21000 }
21001 if (bitmask)
21002 printf (", ");
21003 }
21004}
21005
21006static void
21007print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
21008{
21009 unsigned char * ptr = (unsigned char *) pnote->descdata;
21010 unsigned char * ptr_end = ptr + pnote->descsz;
21011 unsigned int size = is_32bit_elf ? 4 : 8;
21012
21013 printf (_(" Properties: "));
21014
21015 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
21016 {
21017 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
21018 return;
21019 }
21020
21021 while (ptr < ptr_end)
21022 {
21023 unsigned int j;
21024 unsigned int type;
21025 unsigned int datasz;
21026
21027 if ((size_t) (ptr_end - ptr) < 8)
21028 {
21029 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
21030 break;
21031 }
21032
21033 type = byte_get (ptr, 4);
21034 datasz = byte_get (ptr + 4, 4);
21035
21036 ptr += 8;
21037
21038 if (datasz > (size_t) (ptr_end - ptr))
21039 {
21040 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
21041 type, datasz);
21042 break;
21043 }
21044
21045 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
21046 {
21047 if (filedata->file_header.e_machine == EM_X86_64
21048 || filedata->file_header.e_machine == EM_IAMCU
21049 || filedata->file_header.e_machine == EM_386)
21050 {
21051 unsigned int bitmask;
21052
21053 if (datasz == 4)
21054 bitmask = byte_get (ptr, 4);
21055 else
21056 bitmask = 0;
21057
21058 switch (type)
21059 {
21060 case GNU_PROPERTY_X86_ISA_1_USED:
21061 if (datasz != 4)
21062 printf (_("x86 ISA used: <corrupt length: %#x> "),
21063 datasz);
21064 else
21065 {
21066 printf ("x86 ISA used: ");
21067 decode_x86_isa (bitmask);
21068 }
21069 goto next;
21070
21071 case GNU_PROPERTY_X86_ISA_1_NEEDED:
21072 if (datasz != 4)
21073 printf (_("x86 ISA needed: <corrupt length: %#x> "),
21074 datasz);
21075 else
21076 {
21077 printf ("x86 ISA needed: ");
21078 decode_x86_isa (bitmask);
21079 }
21080 goto next;
21081
21082 case GNU_PROPERTY_X86_FEATURE_1_AND:
21083 if (datasz != 4)
21084 printf (_("x86 feature: <corrupt length: %#x> "),
21085 datasz);
21086 else
21087 {
21088 printf ("x86 feature: ");
21089 decode_x86_feature_1 (bitmask);
21090 }
21091 goto next;
21092
21093 case GNU_PROPERTY_X86_FEATURE_2_USED:
21094 if (datasz != 4)
21095 printf (_("x86 feature used: <corrupt length: %#x> "),
21096 datasz);
21097 else
21098 {
21099 printf ("x86 feature used: ");
21100 decode_x86_feature_2 (bitmask);
21101 }
21102 goto next;
21103
21104 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
21105 if (datasz != 4)
21106 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
21107 else
21108 {
21109 printf ("x86 feature needed: ");
21110 decode_x86_feature_2 (bitmask);
21111 }
21112 goto next;
21113
21114 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
21115 if (datasz != 4)
21116 printf (_("x86 ISA used: <corrupt length: %#x> "),
21117 datasz);
21118 else
21119 {
21120 printf ("x86 ISA used: ");
21121 decode_x86_compat_isa (bitmask);
21122 }
21123 goto next;
21124
21125 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
21126 if (datasz != 4)
21127 printf (_("x86 ISA needed: <corrupt length: %#x> "),
21128 datasz);
21129 else
21130 {
21131 printf ("x86 ISA needed: ");
21132 decode_x86_compat_isa (bitmask);
21133 }
21134 goto next;
21135
21136 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
21137 if (datasz != 4)
21138 printf (_("x86 ISA used: <corrupt length: %#x> "),
21139 datasz);
21140 else
21141 {
21142 printf ("x86 ISA used: ");
21143 decode_x86_compat_2_isa (bitmask);
21144 }
21145 goto next;
21146
21147 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
21148 if (datasz != 4)
21149 printf (_("x86 ISA needed: <corrupt length: %#x> "),
21150 datasz);
21151 else
21152 {
21153 printf ("x86 ISA needed: ");
21154 decode_x86_compat_2_isa (bitmask);
21155 }
21156 goto next;
21157
21158 default:
21159 break;
21160 }
21161 }
21162 else if (filedata->file_header.e_machine == EM_AARCH64)
21163 {
21164 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
21165 {
21166 printf ("AArch64 feature: ");
21167 if (datasz != 4)
21168 printf (_("<corrupt length: %#x> "), datasz);
21169 else
21170 decode_aarch64_feature_1_and (byte_get (ptr, 4));
21171 goto next;
21172 }
21173 }
21174 }
21175 else
21176 {
21177 switch (type)
21178 {
21179 case GNU_PROPERTY_STACK_SIZE:
21180 printf (_("stack size: "));
21181 if (datasz != size)
21182 printf (_("<corrupt length: %#x> "), datasz);
21183 else
21184 printf ("%#" PRIx64, byte_get (ptr, size));
21185 goto next;
21186
21187 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
21188 printf ("no copy on protected ");
21189 if (datasz)
21190 printf (_("<corrupt length: %#x> "), datasz);
21191 goto next;
21192
21193 default:
21194 if ((type >= GNU_PROPERTY_UINT32_AND_LO
21195 && type <= GNU_PROPERTY_UINT32_AND_HI)
21196 || (type >= GNU_PROPERTY_UINT32_OR_LO
21197 && type <= GNU_PROPERTY_UINT32_OR_HI))
21198 {
21199 switch (type)
21200 {
21201 case GNU_PROPERTY_1_NEEDED:
21202 if (datasz != 4)
21203 printf (_("1_needed: <corrupt length: %#x> "),
21204 datasz);
21205 else
21206 {
21207 unsigned int bitmask = byte_get (ptr, 4);
21208 printf ("1_needed: ");
21209 decode_1_needed (bitmask);
21210 }
21211 goto next;
21212
21213 default:
21214 break;
21215 }
21216 if (type <= GNU_PROPERTY_UINT32_AND_HI)
21217 printf (_("UINT32_AND (%#x): "), type);
21218 else
21219 printf (_("UINT32_OR (%#x): "), type);
21220 if (datasz != 4)
21221 printf (_("<corrupt length: %#x> "), datasz);
21222 else
21223 printf ("%#x", (unsigned int) byte_get (ptr, 4));
21224 goto next;
21225 }
21226 break;
21227 }
21228 }
21229
21230 if (type < GNU_PROPERTY_LOPROC)
21231 printf (_("<unknown type %#x data: "), type);
21232 else if (type < GNU_PROPERTY_LOUSER)
21233 printf (_("<processor-specific type %#x data: "), type);
21234 else
21235 printf (_("<application-specific type %#x data: "), type);
21236 for (j = 0; j < datasz; ++j)
21237 printf ("%02x ", ptr[j] & 0xff);
21238 printf (">");
21239
21240 next:
21241 ptr += ((datasz + (size - 1)) & ~ (size - 1));
21242 if (ptr == ptr_end)
21243 break;
21244
21245 if (do_wide)
21246 printf (", ");
21247 else
21248 printf ("\n\t");
21249 }
21250
21251 printf ("\n");
21252}
21253
21254static bool
21255print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
21256{
21257 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
21258 switch (pnote->type)
21259 {
21260 case NT_GNU_BUILD_ID:
21261 {
21262 size_t i;
21263
21264 printf (_(" Build ID: "));
21265 for (i = 0; i < pnote->descsz; ++i)
21266 printf ("%02x", pnote->descdata[i] & 0xff);
21267 printf ("\n");
21268 }
21269 break;
21270
21271 case NT_GNU_ABI_TAG:
21272 {
21273 unsigned int os, major, minor, subminor;
21274 const char *osname;
21275
21276 /* PR 17531: file: 030-599401-0.004. */
21277 if (pnote->descsz < 16)
21278 {
21279 printf (_(" <corrupt GNU_ABI_TAG>\n"));
21280 break;
21281 }
21282
21283 os = byte_get ((unsigned char *) pnote->descdata, 4);
21284 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
21285 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
21286 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
21287
21288 switch (os)
21289 {
21290 case GNU_ABI_TAG_LINUX:
21291 osname = "Linux";
21292 break;
21293 case GNU_ABI_TAG_HURD:
21294 osname = "Hurd";
21295 break;
21296 case GNU_ABI_TAG_SOLARIS:
21297 osname = "Solaris";
21298 break;
21299 case GNU_ABI_TAG_FREEBSD:
21300 osname = "FreeBSD";
21301 break;
21302 case GNU_ABI_TAG_NETBSD:
21303 osname = "NetBSD";
21304 break;
21305 case GNU_ABI_TAG_SYLLABLE:
21306 osname = "Syllable";
21307 break;
21308 case GNU_ABI_TAG_NACL:
21309 osname = "NaCl";
21310 break;
21311 default:
21312 osname = "Unknown";
21313 break;
21314 }
21315
21316 printf (_(" OS: %s, ABI: %d.%d.%d\n"), osname,
21317 major, minor, subminor);
21318 }
21319 break;
21320
21321 case NT_GNU_GOLD_VERSION:
21322 {
21323 size_t i;
21324
21325 printf (_(" Version: "));
21326 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
21327 printf ("%c", pnote->descdata[i]);
21328 printf ("\n");
21329 }
21330 break;
21331
21332 case NT_GNU_HWCAP:
21333 {
21334 unsigned int num_entries, mask;
21335
21336 /* Hardware capabilities information. Word 0 is the number of entries.
21337 Word 1 is a bitmask of enabled entries. The rest of the descriptor
21338 is a series of entries, where each entry is a single byte followed
21339 by a nul terminated string. The byte gives the bit number to test
21340 if enabled in the bitmask. */
21341 printf (_(" Hardware Capabilities: "));
21342 if (pnote->descsz < 8)
21343 {
21344 error (_("<corrupt GNU_HWCAP>\n"));
21345 return false;
21346 }
21347 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
21348 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
21349 printf (_("num entries: %d, enabled mask: %x\n"), num_entries, mask);
21350 /* FIXME: Add code to display the entries... */
21351 }
21352 break;
21353
21354 case NT_GNU_PROPERTY_TYPE_0:
21355 print_gnu_property_note (filedata, pnote);
21356 break;
21357
21358 default:
21359 /* Handle unrecognised types. An error message should have already been
21360 created by get_gnu_elf_note_type(), so all that we need to do is to
21361 display the data. */
21362 {
21363 size_t i;
21364
21365 printf (_(" Description data: "));
21366 for (i = 0; i < pnote->descsz; ++i)
21367 printf ("%02x ", pnote->descdata[i] & 0xff);
21368 printf ("\n");
21369 }
21370 break;
21371 }
21372
21373 return true;
21374}
21375
21376static const char *
21377get_v850_elf_note_type (enum v850_notes n_type)
21378{
21379 static char buff[64];
21380
21381 switch (n_type)
21382 {
21383 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
21384 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
21385 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
21386 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
21387 case V850_NOTE_CACHE_INFO: return _("Use of cache");
21388 case V850_NOTE_MMU_INFO: return _("Use of MMU");
21389 default:
21390 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
21391 return buff;
21392 }
21393}
21394
21395static bool
21396print_v850_note (Elf_Internal_Note * pnote)
21397{
21398 unsigned int val;
21399
21400 if (pnote->descsz != 4)
21401 return false;
21402
21403 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
21404
21405 if (val == 0)
21406 {
21407 printf (_("not set\n"));
21408 return true;
21409 }
21410
21411 switch (pnote->type)
21412 {
21413 case V850_NOTE_ALIGNMENT:
21414 switch (val)
21415 {
21416 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
21417 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
21418 }
21419 break;
21420
21421 case V850_NOTE_DATA_SIZE:
21422 switch (val)
21423 {
21424 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
21425 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
21426 }
21427 break;
21428
21429 case V850_NOTE_FPU_INFO:
21430 switch (val)
21431 {
21432 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
21433 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
21434 }
21435 break;
21436
21437 case V850_NOTE_MMU_INFO:
21438 case V850_NOTE_CACHE_INFO:
21439 case V850_NOTE_SIMD_INFO:
21440 if (val == EF_RH850_SIMD)
21441 {
21442 printf (_("yes\n"));
21443 return true;
21444 }
21445 break;
21446
21447 default:
21448 /* An 'unknown note type' message will already have been displayed. */
21449 break;
21450 }
21451
21452 printf (_("unknown value: %x\n"), val);
21453 return false;
21454}
21455
21456static bool
21457process_netbsd_elf_note (Elf_Internal_Note * pnote)
21458{
21459 unsigned int version;
21460
21461 switch (pnote->type)
21462 {
21463 case NT_NETBSD_IDENT:
21464 if (pnote->descsz < 1)
21465 break;
21466 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
21467 if ((version / 10000) % 100)
21468 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
21469 version, version / 100000000, (version / 1000000) % 100,
21470 (version / 10000) % 100 > 26 ? "Z" : "",
21471 'A' + (version / 10000) % 26);
21472 else
21473 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
21474 version, version / 100000000, (version / 1000000) % 100,
21475 (version / 100) % 100);
21476 return true;
21477
21478 case NT_NETBSD_MARCH:
21479 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
21480 pnote->descdata);
21481 return true;
21482
21483 case NT_NETBSD_PAX:
21484 if (pnote->descsz < 1)
21485 break;
21486 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
21487 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
21488 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
21489 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
21490 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
21491 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
21492 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
21493 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
21494 return true;
21495 }
21496
21497 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
21498 pnote->descsz, pnote->type);
21499 return false;
21500}
21501
21502static const char *
21503get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
21504{
21505 switch (e_type)
21506 {
21507 case NT_FREEBSD_THRMISC:
21508 return _("NT_THRMISC (thrmisc structure)");
21509 case NT_FREEBSD_PROCSTAT_PROC:
21510 return _("NT_PROCSTAT_PROC (proc data)");
21511 case NT_FREEBSD_PROCSTAT_FILES:
21512 return _("NT_PROCSTAT_FILES (files data)");
21513 case NT_FREEBSD_PROCSTAT_VMMAP:
21514 return _("NT_PROCSTAT_VMMAP (vmmap data)");
21515 case NT_FREEBSD_PROCSTAT_GROUPS:
21516 return _("NT_PROCSTAT_GROUPS (groups data)");
21517 case NT_FREEBSD_PROCSTAT_UMASK:
21518 return _("NT_PROCSTAT_UMASK (umask data)");
21519 case NT_FREEBSD_PROCSTAT_RLIMIT:
21520 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
21521 case NT_FREEBSD_PROCSTAT_OSREL:
21522 return _("NT_PROCSTAT_OSREL (osreldate data)");
21523 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
21524 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
21525 case NT_FREEBSD_PROCSTAT_AUXV:
21526 return _("NT_PROCSTAT_AUXV (auxv data)");
21527 case NT_FREEBSD_PTLWPINFO:
21528 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
21529 case NT_FREEBSD_X86_SEGBASES:
21530 return _("NT_X86_SEGBASES (x86 segment base registers)");
21531 }
21532 return get_note_type (filedata, e_type);
21533}
21534
21535static const char *
21536get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
21537{
21538 static char buff[64];
21539
21540 switch (e_type)
21541 {
21542 case NT_NETBSDCORE_PROCINFO:
21543 /* NetBSD core "procinfo" structure. */
21544 return _("NetBSD procinfo structure");
21545
21546 case NT_NETBSDCORE_AUXV:
21547 return _("NetBSD ELF auxiliary vector data");
21548
21549 case NT_NETBSDCORE_LWPSTATUS:
21550 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
21551
21552 default:
21553 /* As of Jan 2020 there are no other machine-independent notes
21554 defined for NetBSD core files. If the note type is less
21555 than the start of the machine-dependent note types, we don't
21556 understand it. */
21557
21558 if (e_type < NT_NETBSDCORE_FIRSTMACH)
21559 {
21560 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
21561 return buff;
21562 }
21563 break;
21564 }
21565
21566 switch (filedata->file_header.e_machine)
21567 {
21568 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
21569 and PT_GETFPREGS == mach+2. */
21570
21571 case EM_OLD_ALPHA:
21572 case EM_ALPHA:
21573 case EM_SPARC:
21574 case EM_SPARC32PLUS:
21575 case EM_SPARCV9:
21576 switch (e_type)
21577 {
21578 case NT_NETBSDCORE_FIRSTMACH + 0:
21579 return _("PT_GETREGS (reg structure)");
21580 case NT_NETBSDCORE_FIRSTMACH + 2:
21581 return _("PT_GETFPREGS (fpreg structure)");
21582 default:
21583 break;
21584 }
21585 break;
21586
21587 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
21588 There's also old PT___GETREGS40 == mach + 1 for old reg
21589 structure which lacks GBR. */
21590 case EM_SH:
21591 switch (e_type)
21592 {
21593 case NT_NETBSDCORE_FIRSTMACH + 1:
21594 return _("PT___GETREGS40 (old reg structure)");
21595 case NT_NETBSDCORE_FIRSTMACH + 3:
21596 return _("PT_GETREGS (reg structure)");
21597 case NT_NETBSDCORE_FIRSTMACH + 5:
21598 return _("PT_GETFPREGS (fpreg structure)");
21599 default:
21600 break;
21601 }
21602 break;
21603
21604 /* On all other arch's, PT_GETREGS == mach+1 and
21605 PT_GETFPREGS == mach+3. */
21606 default:
21607 switch (e_type)
21608 {
21609 case NT_NETBSDCORE_FIRSTMACH + 1:
21610 return _("PT_GETREGS (reg structure)");
21611 case NT_NETBSDCORE_FIRSTMACH + 3:
21612 return _("PT_GETFPREGS (fpreg structure)");
21613 default:
21614 break;
21615 }
21616 }
21617
21618 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
21619 e_type - NT_NETBSDCORE_FIRSTMACH);
21620 return buff;
21621}
21622
21623static const char *
21624get_openbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
21625{
21626 switch (e_type)
21627 {
21628 case NT_OPENBSD_PROCINFO:
21629 return _("OpenBSD procinfo structure");
21630 case NT_OPENBSD_AUXV:
21631 return _("OpenBSD ELF auxiliary vector data");
21632 case NT_OPENBSD_REGS:
21633 return _("OpenBSD regular registers");
21634 case NT_OPENBSD_FPREGS:
21635 return _("OpenBSD floating point registers");
21636 case NT_OPENBSD_WCOOKIE:
21637 return _("OpenBSD window cookie");
21638 }
21639
21640 return get_note_type (filedata, e_type);
21641}
21642
21643static const char *
21644get_qnx_elfcore_note_type (Filedata * filedata, unsigned e_type)
21645{
21646 switch (e_type)
21647 {
21648 case QNT_DEBUG_FULLPATH:
21649 return _("QNX debug fullpath");
21650 case QNT_DEBUG_RELOC:
21651 return _("QNX debug relocation");
21652 case QNT_STACK:
21653 return _("QNX stack");
21654 case QNT_GENERATOR:
21655 return _("QNX generator");
21656 case QNT_DEFAULT_LIB:
21657 return _("QNX default library");
21658 case QNT_CORE_SYSINFO:
21659 return _("QNX core sysinfo");
21660 case QNT_CORE_INFO:
21661 return _("QNX core info");
21662 case QNT_CORE_STATUS:
21663 return _("QNX core status");
21664 case QNT_CORE_GREG:
21665 return _("QNX general registers");
21666 case QNT_CORE_FPREG:
21667 return _("QNX floating point registers");
21668 case QNT_LINK_MAP:
21669 return _("QNX link map");
21670 }
21671
21672 return get_note_type (filedata, e_type);
21673}
21674
21675static const char *
21676get_stapsdt_note_type (unsigned e_type)
21677{
21678 static char buff[64];
21679
21680 switch (e_type)
21681 {
21682 case NT_STAPSDT:
21683 return _("NT_STAPSDT (SystemTap probe descriptors)");
21684
21685 default:
21686 break;
21687 }
21688
21689 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
21690 return buff;
21691}
21692
21693static bool
21694print_stapsdt_note (Elf_Internal_Note *pnote)
21695{
21696 size_t len, maxlen;
21697 size_t addr_size = is_32bit_elf ? 4 : 8;
21698 char *data = pnote->descdata;
21699 char *data_end = pnote->descdata + pnote->descsz;
21700 uint64_t pc, base_addr, semaphore;
21701 char *provider, *probe, *arg_fmt;
21702
21703 if (pnote->descsz < (addr_size * 3))
21704 goto stapdt_note_too_small;
21705
21706 pc = byte_get ((unsigned char *) data, addr_size);
21707 data += addr_size;
21708
21709 base_addr = byte_get ((unsigned char *) data, addr_size);
21710 data += addr_size;
21711
21712 semaphore = byte_get ((unsigned char *) data, addr_size);
21713 data += addr_size;
21714
21715 if (data >= data_end)
21716 goto stapdt_note_too_small;
21717 maxlen = data_end - data;
21718 len = strnlen (data, maxlen);
21719 if (len < maxlen)
21720 {
21721 provider = data;
21722 data += len + 1;
21723 }
21724 else
21725 goto stapdt_note_too_small;
21726
21727 if (data >= data_end)
21728 goto stapdt_note_too_small;
21729 maxlen = data_end - data;
21730 len = strnlen (data, maxlen);
21731 if (len < maxlen)
21732 {
21733 probe = data;
21734 data += len + 1;
21735 }
21736 else
21737 goto stapdt_note_too_small;
21738
21739 if (data >= data_end)
21740 goto stapdt_note_too_small;
21741 maxlen = data_end - data;
21742 len = strnlen (data, maxlen);
21743 if (len < maxlen)
21744 {
21745 arg_fmt = data;
21746 data += len + 1;
21747 }
21748 else
21749 goto stapdt_note_too_small;
21750
21751 printf (_(" Provider: %s\n"), provider);
21752 printf (_(" Name: %s\n"), probe);
21753 printf (_(" Location: "));
21754 print_vma (pc, FULL_HEX);
21755 printf (_(", Base: "));
21756 print_vma (base_addr, FULL_HEX);
21757 printf (_(", Semaphore: "));
21758 print_vma (semaphore, FULL_HEX);
21759 printf ("\n");
21760 printf (_(" Arguments: %s\n"), arg_fmt);
21761
21762 return data == data_end;
21763
21764 stapdt_note_too_small:
21765 printf (_(" <corrupt - note is too small>\n"));
21766 error (_("corrupt stapdt note - the data size is too small\n"));
21767 return false;
21768}
21769
21770static bool
21771print_fdo_note (Elf_Internal_Note * pnote)
21772{
21773 if (pnote->descsz > 0 && pnote->type == FDO_PACKAGING_METADATA)
21774 {
21775 printf (_(" Packaging Metadata: %.*s\n"), (int) pnote->descsz, pnote->descdata);
21776 return true;
21777 }
21778 if (pnote->descsz > 0 && pnote->type == FDO_DLOPEN_METADATA)
21779 {
21780 printf (_(" Dlopen Metadata: %.*s\n"), (int) pnote->descsz, pnote->descdata);
21781 return true;
21782 }
21783 return false;
21784}
21785
21786static const char *
21787get_ia64_vms_note_type (unsigned e_type)
21788{
21789 static char buff[64];
21790
21791 switch (e_type)
21792 {
21793 case NT_VMS_MHD:
21794 return _("NT_VMS_MHD (module header)");
21795 case NT_VMS_LNM:
21796 return _("NT_VMS_LNM (language name)");
21797 case NT_VMS_SRC:
21798 return _("NT_VMS_SRC (source files)");
21799 case NT_VMS_TITLE:
21800 return "NT_VMS_TITLE";
21801 case NT_VMS_EIDC:
21802 return _("NT_VMS_EIDC (consistency check)");
21803 case NT_VMS_FPMODE:
21804 return _("NT_VMS_FPMODE (FP mode)");
21805 case NT_VMS_LINKTIME:
21806 return "NT_VMS_LINKTIME";
21807 case NT_VMS_IMGNAM:
21808 return _("NT_VMS_IMGNAM (image name)");
21809 case NT_VMS_IMGID:
21810 return _("NT_VMS_IMGID (image id)");
21811 case NT_VMS_LINKID:
21812 return _("NT_VMS_LINKID (link id)");
21813 case NT_VMS_IMGBID:
21814 return _("NT_VMS_IMGBID (build id)");
21815 case NT_VMS_GSTNAM:
21816 return _("NT_VMS_GSTNAM (sym table name)");
21817 case NT_VMS_ORIG_DYN:
21818 return "NT_VMS_ORIG_DYN";
21819 case NT_VMS_PATCHTIME:
21820 return "NT_VMS_PATCHTIME";
21821 default:
21822 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
21823 return buff;
21824 }
21825}
21826
21827static bool
21828print_ia64_vms_note (Elf_Internal_Note * pnote)
21829{
21830 unsigned int maxlen = pnote->descsz;
21831
21832 if (maxlen < 2 || maxlen != pnote->descsz)
21833 goto desc_size_fail;
21834
21835 switch (pnote->type)
21836 {
21837 case NT_VMS_MHD:
21838 if (maxlen <= 36)
21839 goto desc_size_fail;
21840
21841 size_t l = strnlen (pnote->descdata + 34, maxlen - 34);
21842
21843 printf (_(" Creation date : %.17s\n"), pnote->descdata);
21844 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
21845 if (l + 34 < maxlen)
21846 {
21847 printf (_(" Module name : %s\n"), pnote->descdata + 34);
21848 if (l + 35 < maxlen)
21849 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
21850 else
21851 printf (_(" Module version : <missing>\n"));
21852 }
21853 else
21854 {
21855 printf (_(" Module name : <missing>\n"));
21856 printf (_(" Module version : <missing>\n"));
21857 }
21858 break;
21859
21860 case NT_VMS_LNM:
21861 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
21862 break;
21863
21864 case NT_VMS_FPMODE:
21865 printf (_(" Floating Point mode: "));
21866 if (maxlen < 8)
21867 goto desc_size_fail;
21868 /* FIXME: Generate an error if descsz > 8 ? */
21869
21870 printf ("0x%016" PRIx64 "\n",
21871 byte_get ((unsigned char *) pnote->descdata, 8));
21872 break;
21873
21874 case NT_VMS_LINKTIME:
21875 printf (_(" Link time: "));
21876 if (maxlen < 8)
21877 goto desc_size_fail;
21878 /* FIXME: Generate an error if descsz > 8 ? */
21879
21880 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
21881 printf ("\n");
21882 break;
21883
21884 case NT_VMS_PATCHTIME:
21885 printf (_(" Patch time: "));
21886 if (maxlen < 8)
21887 goto desc_size_fail;
21888 /* FIXME: Generate an error if descsz > 8 ? */
21889
21890 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
21891 printf ("\n");
21892 break;
21893
21894 case NT_VMS_ORIG_DYN:
21895 if (maxlen < 34)
21896 goto desc_size_fail;
21897
21898 printf (_(" Major id: %u, minor id: %u\n"),
21899 (unsigned) byte_get ((unsigned char *) pnote->descdata, 4),
21900 (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
21901 printf (_(" Last modified : "));
21902 print_vms_time (byte_get ((unsigned char *) pnote->descdata + 8, 8));
21903 printf (_("\n Link flags : "));
21904 printf ("0x%016" PRIx64 "\n",
21905 byte_get ((unsigned char *) pnote->descdata + 16, 8));
21906 printf (_(" Header flags: 0x%08x\n"),
21907 (unsigned) byte_get ((unsigned char *) pnote->descdata + 24, 4));
21908 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
21909 break;
21910
21911 case NT_VMS_IMGNAM:
21912 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
21913 break;
21914
21915 case NT_VMS_GSTNAM:
21916 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
21917 break;
21918
21919 case NT_VMS_IMGID:
21920 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
21921 break;
21922
21923 case NT_VMS_LINKID:
21924 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
21925 break;
21926
21927 default:
21928 return false;
21929 }
21930
21931 return true;
21932
21933 desc_size_fail:
21934 printf (_(" <corrupt - data size is too small>\n"));
21935 error (_("corrupt IA64 note: data size is too small\n"));
21936 return false;
21937}
21938
21939struct build_attr_cache {
21940 Filedata *filedata;
21941 char *strtab;
21942 uint64_t strtablen;
21943 Elf_Internal_Sym *symtab;
21944 uint64_t nsyms;
21945} ba_cache;
21946
21947/* Find the symbol associated with a build attribute that is attached
21948 to address OFFSET. If PNAME is non-NULL then store the name of
21949 the symbol (if found) in the provided pointer, Returns NULL if a
21950 symbol could not be found. */
21951
21952static Elf_Internal_Sym *
21953get_symbol_for_build_attribute (Filedata *filedata,
21954 uint64_t offset,
21955 bool is_open_attr,
21956 const char **pname)
21957{
21958 Elf_Internal_Sym *saved_sym = NULL;
21959 Elf_Internal_Sym *sym;
21960
21961 if (filedata->section_headers != NULL
21962 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
21963 {
21964 Elf_Internal_Shdr * symsec;
21965
21966 free (ba_cache.strtab);
21967 ba_cache.strtab = NULL;
21968 free (ba_cache.symtab);
21969 ba_cache.symtab = NULL;
21970
21971 /* Load the symbol and string sections. */
21972 for (symsec = filedata->section_headers;
21973 symsec < filedata->section_headers + filedata->file_header.e_shnum;
21974 symsec ++)
21975 {
21976 if (symsec->sh_type == SHT_SYMTAB
21977 && get_symtab (filedata, symsec,
21978 &ba_cache.symtab, &ba_cache.nsyms,
21979 &ba_cache.strtab, &ba_cache.strtablen))
21980 break;
21981 }
21982 ba_cache.filedata = filedata;
21983 }
21984
21985 if (ba_cache.symtab == NULL)
21986 return NULL;
21987
21988 /* Find a symbol whose value matches offset. */
21989 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
21990 if (sym->st_value == offset)
21991 {
21992 if (sym->st_name >= ba_cache.strtablen)
21993 /* Huh ? This should not happen. */
21994 continue;
21995
21996 if (ba_cache.strtab[sym->st_name] == 0)
21997 continue;
21998
21999 /* The AArch64, ARM and RISC-V architectures define mapping symbols
22000 (eg $d, $x, $t) which we want to ignore. */
22001 if (ba_cache.strtab[sym->st_name] == '$'
22002 && ba_cache.strtab[sym->st_name + 1] != 0
22003 && ba_cache.strtab[sym->st_name + 2] == 0)
22004 continue;
22005
22006 if (is_open_attr)
22007 {
22008 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
22009 and FILE or OBJECT symbols over NOTYPE symbols. We skip
22010 FUNC symbols entirely. */
22011 switch (ELF_ST_TYPE (sym->st_info))
22012 {
22013 case STT_OBJECT:
22014 case STT_FILE:
22015 saved_sym = sym;
22016 if (sym->st_size)
22017 {
22018 /* If the symbol has a size associated
22019 with it then we can stop searching. */
22020 sym = ba_cache.symtab + ba_cache.nsyms;
22021 }
22022 continue;
22023
22024 case STT_FUNC:
22025 /* Ignore function symbols. */
22026 continue;
22027
22028 default:
22029 break;
22030 }
22031
22032 switch (ELF_ST_BIND (sym->st_info))
22033 {
22034 case STB_GLOBAL:
22035 if (saved_sym == NULL
22036 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
22037 saved_sym = sym;
22038 break;
22039
22040 case STB_LOCAL:
22041 if (saved_sym == NULL)
22042 saved_sym = sym;
22043 break;
22044
22045 default:
22046 break;
22047 }
22048 }
22049 else
22050 {
22051 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
22052 continue;
22053
22054 saved_sym = sym;
22055 break;
22056 }
22057 }
22058
22059 if (saved_sym && pname)
22060 * pname = ba_cache.strtab + saved_sym->st_name;
22061
22062 return saved_sym;
22063}
22064
22065/* Returns true iff addr1 and addr2 are in the same section. */
22066
22067static bool
22068same_section (Filedata * filedata, uint64_t addr1, uint64_t addr2)
22069{
22070 Elf_Internal_Shdr * a1;
22071 Elf_Internal_Shdr * a2;
22072
22073 a1 = find_section_by_address (filedata, addr1);
22074 a2 = find_section_by_address (filedata, addr2);
22075
22076 return a1 == a2 && a1 != NULL;
22077}
22078
22079static bool
22080print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
22081 Filedata * filedata)
22082{
22083 static uint64_t global_offset = 0;
22084 static uint64_t global_end = 0;
22085 static uint64_t func_offset = 0;
22086 static uint64_t func_end = 0;
22087
22088 Elf_Internal_Sym *sym;
22089 const char *name;
22090 uint64_t start;
22091 uint64_t end;
22092 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
22093
22094 switch (pnote->descsz)
22095 {
22096 case 0:
22097 /* A zero-length description means that the range of
22098 the previous note of the same type should be used. */
22099 if (is_open_attr)
22100 {
22101 if (global_end > global_offset)
22102 printf (_(" Applies to region from %#" PRIx64
22103 " to %#" PRIx64 "\n"), global_offset, global_end);
22104 else
22105 printf (_(" Applies to region from %#" PRIx64
22106 "\n"), global_offset);
22107 }
22108 else
22109 {
22110 if (func_end > func_offset)
22111 printf (_(" Applies to region from %#" PRIx64
22112 " to %#" PRIx64 "\n"), func_offset, func_end);
22113 else
22114 printf (_(" Applies to region from %#" PRIx64
22115 "\n"), func_offset);
22116 }
22117 return true;
22118
22119 case 4:
22120 start = byte_get ((unsigned char *) pnote->descdata, 4);
22121 end = 0;
22122 break;
22123
22124 case 8:
22125 start = byte_get ((unsigned char *) pnote->descdata, 4);
22126 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
22127 break;
22128
22129 case 16:
22130 start = byte_get ((unsigned char *) pnote->descdata, 8);
22131 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
22132 break;
22133
22134 default:
22135 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
22136 printf (_(" <invalid descsz>"));
22137 return false;
22138 }
22139
22140 name = NULL;
22141 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
22142 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
22143 in order to avoid them being confused with the start address of the
22144 first function in the file... */
22145 if (sym == NULL && is_open_attr)
22146 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
22147 & name);
22148
22149 if (end == 0 && sym != NULL && sym->st_size > 0)
22150 end = start + sym->st_size;
22151
22152 if (is_open_attr)
22153 {
22154 /* FIXME: Need to properly allow for section alignment.
22155 16 is just the alignment used on x86_64. */
22156 if (global_end > 0
22157 && start > BFD_ALIGN (global_end, 16)
22158 /* Build notes are not guaranteed to be organised in order of
22159 increasing address, but we should find the all of the notes
22160 for one section in the same place. */
22161 && same_section (filedata, start, global_end))
22162 warn (_("Gap in build notes detected from %#" PRIx64
22163 " to %#" PRIx64 "\n"),
22164 global_end + 1, start - 1);
22165
22166 printf (_(" Applies to region from %#" PRIx64), start);
22167 global_offset = start;
22168
22169 if (end)
22170 {
22171 printf (_(" to %#" PRIx64), end);
22172 global_end = end;
22173 }
22174 }
22175 else
22176 {
22177 printf (_(" Applies to region from %#" PRIx64), start);
22178 func_offset = start;
22179
22180 if (end)
22181 {
22182 printf (_(" to %#" PRIx64), end);
22183 func_end = end;
22184 }
22185 }
22186
22187 if (sym && name)
22188 printf (_(" (%s)"), name);
22189
22190 printf ("\n");
22191 return true;
22192}
22193
22194static bool
22195print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
22196{
22197 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
22198 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
22199 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
22200 char name_type;
22201 char name_attribute;
22202 const char * expected_types;
22203 const char * name = pnote->namedata;
22204 const char * text;
22205 signed int left;
22206
22207 if (name == NULL || pnote->namesz < 2)
22208 {
22209 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
22210 print_symbol_name (-20, _(" <corrupt name>"));
22211 return false;
22212 }
22213
22214 if (do_wide)
22215 left = 28;
22216 else
22217 left = 20;
22218
22219 /* Version 2 of the spec adds a "GA" prefix to the name field. */
22220 if (name[0] == 'G' && name[1] == 'A')
22221 {
22222 if (pnote->namesz < 4)
22223 {
22224 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
22225 print_symbol_name (-20, _(" <corrupt name>"));
22226 return false;
22227 }
22228
22229 printf ("GA");
22230 name += 2;
22231 left -= 2;
22232 }
22233
22234 switch ((name_type = * name))
22235 {
22236 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
22237 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
22238 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
22239 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
22240 printf ("%c", * name);
22241 left --;
22242 break;
22243 default:
22244 error (_("unrecognised attribute type in name field: %d\n"), name_type);
22245 print_symbol_name (-20, _("<unknown name type>"));
22246 return false;
22247 }
22248
22249 ++ name;
22250 text = NULL;
22251
22252 switch ((name_attribute = * name))
22253 {
22254 case GNU_BUILD_ATTRIBUTE_VERSION:
22255 text = _("<version>");
22256 expected_types = string_expected;
22257 ++ name;
22258 break;
22259 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
22260 text = _("<stack prot>");
22261 expected_types = "!+*";
22262 ++ name;
22263 break;
22264 case GNU_BUILD_ATTRIBUTE_RELRO:
22265 text = _("<relro>");
22266 expected_types = bool_expected;
22267 ++ name;
22268 break;
22269 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
22270 text = _("<stack size>");
22271 expected_types = number_expected;
22272 ++ name;
22273 break;
22274 case GNU_BUILD_ATTRIBUTE_TOOL:
22275 text = _("<tool>");
22276 expected_types = string_expected;
22277 ++ name;
22278 break;
22279 case GNU_BUILD_ATTRIBUTE_ABI:
22280 text = _("<ABI>");
22281 expected_types = "$*";
22282 ++ name;
22283 break;
22284 case GNU_BUILD_ATTRIBUTE_PIC:
22285 text = _("<PIC>");
22286 expected_types = number_expected;
22287 ++ name;
22288 break;
22289 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
22290 text = _("<short enum>");
22291 expected_types = bool_expected;
22292 ++ name;
22293 break;
22294 default:
22295 if (ISPRINT (* name))
22296 {
22297 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
22298
22299 if (len > left && ! do_wide)
22300 len = left;
22301 printf ("%.*s:", len, name);
22302 left -= len;
22303 name += len;
22304 }
22305 else
22306 {
22307 static char tmpbuf [128];
22308
22309 error (_("unrecognised byte in name field: %d\n"), * name);
22310 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
22311 text = tmpbuf;
22312 name ++;
22313 }
22314 expected_types = "*$!+";
22315 break;
22316 }
22317
22318 if (text)
22319 left -= printf ("%s", text);
22320
22321 if (strchr (expected_types, name_type) == NULL)
22322 warn (_("attribute does not have an expected type (%c)\n"), name_type);
22323
22324 if ((size_t) (name - pnote->namedata) > pnote->namesz)
22325 {
22326 error (_("corrupt name field: namesz: %lu but parsing gets to %td\n"),
22327 pnote->namesz,
22328 name - pnote->namedata);
22329 return false;
22330 }
22331
22332 if (left < 1 && ! do_wide)
22333 return true;
22334
22335 switch (name_type)
22336 {
22337 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
22338 {
22339 unsigned int bytes;
22340 uint64_t val = 0;
22341 unsigned int shift = 0;
22342 char *decoded = NULL;
22343
22344 bytes = pnote->namesz - (name - pnote->namedata);
22345 if (bytes > 0)
22346 /* The -1 is because the name field is always 0 terminated, and we
22347 want to be able to ensure that the shift in the while loop below
22348 will not overflow. */
22349 -- bytes;
22350
22351 if (bytes > sizeof (val))
22352 {
22353 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
22354 bytes);
22355 bytes = sizeof (val);
22356 }
22357 /* We do not bother to warn if bytes == 0 as this can
22358 happen with some early versions of the gcc plugin. */
22359
22360 while (bytes --)
22361 {
22362 uint64_t byte = *name++ & 0xff;
22363
22364 val |= byte << shift;
22365 shift += 8;
22366 }
22367
22368 switch (name_attribute)
22369 {
22370 case GNU_BUILD_ATTRIBUTE_PIC:
22371 switch (val)
22372 {
22373 case 0: decoded = "static"; break;
22374 case 1: decoded = "pic"; break;
22375 case 2: decoded = "PIC"; break;
22376 case 3: decoded = "pie"; break;
22377 case 4: decoded = "PIE"; break;
22378 default: break;
22379 }
22380 break;
22381 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
22382 switch (val)
22383 {
22384 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
22385 case 0: decoded = "off"; break;
22386 case 1: decoded = "on"; break;
22387 case 2: decoded = "all"; break;
22388 case 3: decoded = "strong"; break;
22389 case 4: decoded = "explicit"; break;
22390 default: break;
22391 }
22392 break;
22393 default:
22394 break;
22395 }
22396
22397 if (decoded != NULL)
22398 {
22399 print_symbol_name (-left, decoded);
22400 left = 0;
22401 }
22402 else if (val == 0)
22403 {
22404 printf ("0x0");
22405 left -= 3;
22406 }
22407 else
22408 {
22409 if (do_wide)
22410 left -= printf ("0x%" PRIx64, val);
22411 else
22412 left -= printf ("0x%-.*" PRIx64, left, val);
22413 }
22414 }
22415 break;
22416 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
22417 left -= print_symbol_name (- left, name);
22418 break;
22419 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
22420 left -= print_symbol_name (- left, "true");
22421 break;
22422 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
22423 left -= print_symbol_name (- left, "false");
22424 break;
22425 }
22426
22427 if (do_wide && left > 0)
22428 printf ("%-*s", left, " ");
22429
22430 return true;
22431}
22432
22433/* Print the contents of PNOTE as hex. */
22434
22435static void
22436print_note_contents_hex (Elf_Internal_Note *pnote)
22437{
22438 if (pnote->descsz)
22439 {
22440 size_t i;
22441
22442 printf (_(" description data: "));
22443 for (i = 0; i < pnote->descsz; i++)
22444 printf ("%02x ", pnote->descdata[i] & 0xff);
22445 if (!do_wide)
22446 printf ("\n");
22447 }
22448
22449 if (do_wide)
22450 printf ("\n");
22451}
22452
22453#if defined HAVE_MSGPACK
22454
22455static void
22456print_indents (int n)
22457{
22458 printf (" ");
22459
22460 for (int i = 0; i < n; i++)
22461 printf (" ");
22462}
22463
22464/* Print OBJ in human-readable form. */
22465
22466static void
22467dump_msgpack_obj (const msgpack_object *obj, int indent)
22468{
22469 switch (obj->type)
22470 {
22471 case MSGPACK_OBJECT_NIL:
22472 printf ("(nil)");
22473 break;
22474
22475 case MSGPACK_OBJECT_BOOLEAN:
22476 printf ("%s", obj->via.boolean ? "true" : "false");
22477 break;
22478
22479 case MSGPACK_OBJECT_POSITIVE_INTEGER:
22480 printf ("%" PRIu64, obj->via.u64);
22481 break;
22482
22483 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
22484 printf ("%" PRIi64, obj->via.i64);
22485 break;
22486
22487 case MSGPACK_OBJECT_FLOAT32:
22488 case MSGPACK_OBJECT_FLOAT64:
22489 printf ("%f", obj->via.f64);
22490 break;
22491
22492 case MSGPACK_OBJECT_STR:
22493 printf ("\"%.*s\"", obj->via.str.size, obj->via.str.ptr);
22494 break;
22495
22496 case MSGPACK_OBJECT_ARRAY:
22497 {
22498 const msgpack_object_array *array = &obj->via.array;
22499
22500 printf ("[\n");
22501 ++indent;
22502
22503 for (uint32_t i = 0; i < array->size; ++i)
22504 {
22505 const msgpack_object *item = &array->ptr[i];
22506
22507 print_indents (indent);
22508 dump_msgpack_obj (item, indent);
22509 printf (",\n");
22510 }
22511
22512 --indent;
22513 print_indents (indent);
22514 printf ("]");
22515 break;
22516 }
22517 break;
22518
22519 case MSGPACK_OBJECT_MAP:
22520 {
22521 const msgpack_object_map *map = &obj->via.map;
22522
22523 printf ("{\n");
22524 ++indent;
22525
22526 for (uint32_t i = 0; i < map->size; ++i)
22527 {
22528 const msgpack_object_kv *kv = &map->ptr[i];
22529 const msgpack_object *key = &kv->key;
22530 const msgpack_object *val = &kv->val;
22531
22532 print_indents (indent);
22533 dump_msgpack_obj (key, indent);
22534 printf (": ");
22535 dump_msgpack_obj (val, indent);
22536
22537 printf (",\n");
22538 }
22539
22540 --indent;
22541 print_indents (indent);
22542 printf ("}");
22543
22544 break;
22545 }
22546
22547 case MSGPACK_OBJECT_BIN:
22548 printf ("(bin)");
22549 break;
22550
22551 case MSGPACK_OBJECT_EXT:
22552 printf ("(ext)");
22553 break;
22554 }
22555}
22556
22557static void
22558dump_msgpack (const msgpack_unpacked *msg)
22559{
22560 print_indents (0);
22561 dump_msgpack_obj (&msg->data, 0);
22562 printf ("\n");
22563}
22564
22565#endif /* defined HAVE_MSGPACK */
22566
22567static bool
22568print_amdgpu_note (Elf_Internal_Note *pnote)
22569{
22570#if defined HAVE_MSGPACK
22571 /* If msgpack is available, decode and dump the note's content. */
22572 bool ret;
22573 msgpack_unpacked msg;
22574 msgpack_unpack_return msgpack_ret;
22575
22576 assert (pnote->type == NT_AMDGPU_METADATA);
22577
22578 msgpack_unpacked_init (&msg);
22579 msgpack_ret = msgpack_unpack_next (&msg, pnote->descdata, pnote->descsz,
22580 NULL);
22581
22582 switch (msgpack_ret)
22583 {
22584 case MSGPACK_UNPACK_SUCCESS:
22585 dump_msgpack (&msg);
22586 ret = true;
22587 break;
22588
22589 default:
22590 error (_("failed to unpack msgpack contents in NT_AMDGPU_METADATA note"));
22591 ret = false;
22592 break;
22593 }
22594
22595 msgpack_unpacked_destroy (&msg);
22596 return ret;
22597#else
22598 /* msgpack is not available, dump contents as hex. */
22599 print_note_contents_hex (pnote);
22600 return true;
22601#endif
22602}
22603
22604static bool
22605print_qnx_note (Elf_Internal_Note *pnote)
22606{
22607 switch (pnote->type)
22608 {
22609 case QNT_STACK:
22610 if (pnote->descsz != 12)
22611 goto desc_size_fail;
22612
22613 printf (_(" Stack Size: 0x%" PRIx32 "\n"),
22614 (unsigned) byte_get ((unsigned char *) pnote->descdata, 4));
22615 printf (_(" Stack allocated: %" PRIx32 "\n"),
22616 (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
22617 printf (_(" Executable: %s\n"),
22618 ((unsigned) byte_get ((unsigned char *) pnote->descdata + 8, 1)) ? "no": "yes");
22619 break;
22620
22621 default:
22622 print_note_contents_hex(pnote);
22623 }
22624 return true;
22625
22626desc_size_fail:
22627 printf (_(" <corrupt - data size is too small>\n"));
22628 error (_("corrupt QNX note: data size is too small\n"));
22629 return false;
22630}
22631
22632
22633/* Note that by the ELF standard, the name field is already null byte
22634 terminated, and namesz includes the terminating null byte.
22635 I.E. the value of namesz for the name "FSF" is 4.
22636
22637 If the value of namesz is zero, there is no name present. */
22638
22639static bool
22640process_note (Elf_Internal_Note * pnote,
22641 Filedata * filedata)
22642{
22643 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
22644 const char * nt;
22645
22646 if (pnote->namesz == 0)
22647 /* If there is no note name, then use the default set of
22648 note type strings. */
22649 nt = get_note_type (filedata, pnote->type);
22650
22651 else if (startswith (pnote->namedata, "GNU"))
22652 /* GNU-specific object file notes. */
22653 nt = get_gnu_elf_note_type (pnote->type);
22654
22655 else if (startswith (pnote->namedata, "AMDGPU"))
22656 /* AMDGPU-specific object file notes. */
22657 nt = get_amdgpu_elf_note_type (pnote->type);
22658
22659 else if (startswith (pnote->namedata, "FreeBSD"))
22660 /* FreeBSD-specific core file notes. */
22661 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
22662
22663 else if (startswith (pnote->namedata, "NetBSD-CORE"))
22664 /* NetBSD-specific core file notes. */
22665 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
22666
22667 else if (startswith (pnote->namedata, "NetBSD"))
22668 /* NetBSD-specific core file notes. */
22669 return process_netbsd_elf_note (pnote);
22670
22671 else if (startswith (pnote->namedata, "PaX"))
22672 /* NetBSD-specific core file notes. */
22673 return process_netbsd_elf_note (pnote);
22674
22675 else if (startswith (pnote->namedata, "OpenBSD"))
22676 /* OpenBSD-specific core file notes. */
22677 nt = get_openbsd_elfcore_note_type (filedata, pnote->type);
22678
22679 else if (startswith (pnote->namedata, "QNX"))
22680 /* QNX-specific core file notes. */
22681 nt = get_qnx_elfcore_note_type (filedata, pnote->type);
22682
22683 else if (startswith (pnote->namedata, "SPU/"))
22684 {
22685 /* SPU-specific core file notes. */
22686 nt = pnote->namedata + 4;
22687 name = "SPU";
22688 }
22689
22690 else if (startswith (pnote->namedata, "IPF/VMS"))
22691 /* VMS/ia64-specific file notes. */
22692 nt = get_ia64_vms_note_type (pnote->type);
22693
22694 else if (startswith (pnote->namedata, "stapsdt"))
22695 nt = get_stapsdt_note_type (pnote->type);
22696
22697 else
22698 /* Don't recognize this note name; just use the default set of
22699 note type strings. */
22700 nt = get_note_type (filedata, pnote->type);
22701
22702 printf (" ");
22703
22704 if (((startswith (pnote->namedata, "GA")
22705 && strchr ("*$!+", pnote->namedata[2]) != NULL)
22706 || strchr ("*$!+", pnote->namedata[0]) != NULL)
22707 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
22708 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
22709 print_gnu_build_attribute_name (pnote);
22710 else
22711 print_symbol_name (-20, name);
22712
22713 if (do_wide)
22714 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
22715 else
22716 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
22717
22718 if (startswith (pnote->namedata, "IPF/VMS"))
22719 return print_ia64_vms_note (pnote);
22720 else if (startswith (pnote->namedata, "GNU"))
22721 return print_gnu_note (filedata, pnote);
22722 else if (startswith (pnote->namedata, "stapsdt"))
22723 return print_stapsdt_note (pnote);
22724 else if (startswith (pnote->namedata, "CORE"))
22725 return print_core_note (pnote);
22726 else if (startswith (pnote->namedata, "FDO"))
22727 return print_fdo_note (pnote);
22728 else if (((startswith (pnote->namedata, "GA")
22729 && strchr ("*$!+", pnote->namedata[2]) != NULL)
22730 || strchr ("*$!+", pnote->namedata[0]) != NULL)
22731 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
22732 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
22733 return print_gnu_build_attribute_description (pnote, filedata);
22734 else if (startswith (pnote->namedata, "AMDGPU")
22735 && pnote->type == NT_AMDGPU_METADATA)
22736 return print_amdgpu_note (pnote);
22737 else if (startswith (pnote->namedata, "QNX"))
22738 return print_qnx_note (pnote);
22739
22740 print_note_contents_hex (pnote);
22741 return true;
22742}
22743
22744static bool
22745process_notes_at (Filedata * filedata,
22746 Elf_Internal_Shdr * section,
22747 uint64_t offset,
22748 uint64_t length,
22749 uint64_t align)
22750{
22751 Elf_External_Note *pnotes;
22752 Elf_External_Note *external;
22753 char *end;
22754 bool res = true;
22755
22756 if (length <= 0)
22757 return false;
22758
22759 if (section)
22760 {
22761 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
22762 if (pnotes)
22763 {
22764 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
22765 {
22766 free (pnotes);
22767 return false;
22768 }
22769 }
22770 }
22771 else
22772 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
22773 _("notes"));
22774
22775 if (pnotes == NULL)
22776 return false;
22777
22778 external = pnotes;
22779
22780 if (filedata->is_separate)
22781 printf (_("In linked file '%s': "), filedata->file_name);
22782 else
22783 printf ("\n");
22784 if (section)
22785 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
22786 else
22787 printf (_("Displaying notes found at file offset 0x%08" PRIx64
22788 " with length 0x%08" PRIx64 ":\n"),
22789 offset, length);
22790
22791 /* NB: Some note sections may have alignment value of 0 or 1. gABI
22792 specifies that notes should be aligned to 4 bytes in 32-bit
22793 objects and to 8 bytes in 64-bit objects. As a Linux extension,
22794 we also support 4 byte alignment in 64-bit objects. If section
22795 alignment is less than 4, we treate alignment as 4 bytes. */
22796 if (align < 4)
22797 align = 4;
22798 else if (align != 4 && align != 8)
22799 {
22800 warn (_("Corrupt note: alignment %" PRId64 ", expecting 4 or 8\n"),
22801 align);
22802 free (pnotes);
22803 return false;
22804 }
22805
22806 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
22807
22808 end = (char *) pnotes + length;
22809 while ((char *) external < end)
22810 {
22811 Elf_Internal_Note inote;
22812 size_t min_notesz;
22813 char * next;
22814 char * temp = NULL;
22815 size_t data_remaining = end - (char *) external;
22816
22817 if (!is_ia64_vms (filedata))
22818 {
22819 /* PR binutils/15191
22820 Make sure that there is enough data to read. */
22821 min_notesz = offsetof (Elf_External_Note, name);
22822 if (data_remaining < min_notesz)
22823 {
22824 warn (ngettext ("Corrupt note: only %zd byte remains, "
22825 "not enough for a full note\n",
22826 "Corrupt note: only %zd bytes remain, "
22827 "not enough for a full note\n",
22828 data_remaining),
22829 data_remaining);
22830 break;
22831 }
22832 data_remaining -= min_notesz;
22833
22834 inote.type = BYTE_GET (external->type);
22835 inote.namesz = BYTE_GET (external->namesz);
22836 inote.namedata = external->name;
22837 inote.descsz = BYTE_GET (external->descsz);
22838 inote.descdata = ((char *) external
22839 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
22840 inote.descpos = offset + (inote.descdata - (char *) pnotes);
22841 next = ((char *) external
22842 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
22843 }
22844 else
22845 {
22846 Elf64_External_VMS_Note *vms_external;
22847
22848 /* PR binutils/15191
22849 Make sure that there is enough data to read. */
22850 min_notesz = offsetof (Elf64_External_VMS_Note, name);
22851 if (data_remaining < min_notesz)
22852 {
22853 warn (ngettext ("Corrupt note: only %zd byte remains, "
22854 "not enough for a full note\n",
22855 "Corrupt note: only %zd bytes remain, "
22856 "not enough for a full note\n",
22857 data_remaining),
22858 data_remaining);
22859 break;
22860 }
22861 data_remaining -= min_notesz;
22862
22863 vms_external = (Elf64_External_VMS_Note *) external;
22864 inote.type = BYTE_GET (vms_external->type);
22865 inote.namesz = BYTE_GET (vms_external->namesz);
22866 inote.namedata = vms_external->name;
22867 inote.descsz = BYTE_GET (vms_external->descsz);
22868 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
22869 inote.descpos = offset + (inote.descdata - (char *) pnotes);
22870 next = inote.descdata + align_power (inote.descsz, 3);
22871 }
22872
22873 /* PR 17531: file: 3443835e. */
22874 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
22875 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
22876 || (size_t) (inote.descdata - inote.namedata) > data_remaining
22877 || (size_t) (next - inote.descdata) < inote.descsz
22878 || ((size_t) (next - inote.descdata)
22879 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
22880 {
22881 warn (_("note with invalid namesz and/or descsz found at offset %#tx\n"),
22882 (char *) external - (char *) pnotes);
22883 warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx, alignment: %u\n"),
22884 inote.type, inote.namesz, inote.descsz, (int) align);
22885 break;
22886 }
22887
22888 external = (Elf_External_Note *) next;
22889
22890 /* Verify that name is null terminated. It appears that at least
22891 one version of Linux (RedHat 6.0) generates corefiles that don't
22892 comply with the ELF spec by failing to include the null byte in
22893 namesz. */
22894 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
22895 {
22896 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
22897 {
22898 temp = (char *) malloc (inote.namesz + 1);
22899 if (temp == NULL)
22900 {
22901 error (_("Out of memory allocating space for inote name\n"));
22902 res = false;
22903 break;
22904 }
22905
22906 memcpy (temp, inote.namedata, inote.namesz);
22907 inote.namedata = temp;
22908 }
22909 inote.namedata[inote.namesz] = 0;
22910 }
22911
22912 if (! process_note (& inote, filedata))
22913 res = false;
22914
22915 free (temp);
22916 temp = NULL;
22917 }
22918
22919 free (pnotes);
22920
22921 return res;
22922}
22923
22924static bool
22925process_corefile_note_segments (Filedata * filedata)
22926{
22927 Elf_Internal_Phdr *segment;
22928 unsigned int i;
22929 bool res = true;
22930
22931 if (! get_program_headers (filedata))
22932 return true;
22933
22934 for (i = 0, segment = filedata->program_headers;
22935 i < filedata->file_header.e_phnum;
22936 i++, segment++)
22937 {
22938 if (segment->p_type == PT_NOTE)
22939 if (! process_notes_at (filedata, NULL, segment->p_offset,
22940 segment->p_filesz, segment->p_align))
22941 res = false;
22942 }
22943
22944 return res;
22945}
22946
22947static bool
22948process_v850_notes (Filedata * filedata, uint64_t offset, uint64_t length)
22949{
22950 Elf_External_Note * pnotes;
22951 Elf_External_Note * external;
22952 char * end;
22953 bool res = true;
22954
22955 if (length <= 0)
22956 return false;
22957
22958 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
22959 _("v850 notes"));
22960 if (pnotes == NULL)
22961 return false;
22962
22963 external = pnotes;
22964 end = (char*) pnotes + length;
22965
22966 printf (_("\nDisplaying contents of Renesas V850 notes section at offset"
22967 " %#" PRIx64 " with length %#" PRIx64 ":\n"),
22968 offset, length);
22969
22970 while ((char *) external + sizeof (Elf_External_Note) < end)
22971 {
22972 Elf_External_Note * next;
22973 Elf_Internal_Note inote;
22974
22975 inote.type = BYTE_GET (external->type);
22976 inote.namesz = BYTE_GET (external->namesz);
22977 inote.namedata = external->name;
22978 inote.descsz = BYTE_GET (external->descsz);
22979 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
22980 inote.descpos = offset + (inote.descdata - (char *) pnotes);
22981
22982 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
22983 {
22984 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
22985 inote.descdata = inote.namedata;
22986 inote.namesz = 0;
22987 }
22988
22989 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
22990
22991 if ( ((char *) next > end)
22992 || ((char *) next < (char *) pnotes))
22993 {
22994 warn (_("corrupt descsz found in note at offset %#tx\n"),
22995 (char *) external - (char *) pnotes);
22996 warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx\n"),
22997 inote.type, inote.namesz, inote.descsz);
22998 break;
22999 }
23000
23001 external = next;
23002
23003 /* Prevent out-of-bounds indexing. */
23004 if ( inote.namedata + inote.namesz > end
23005 || inote.namedata + inote.namesz < inote.namedata)
23006 {
23007 warn (_("corrupt namesz found in note at offset %#zx\n"),
23008 (char *) external - (char *) pnotes);
23009 warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx\n"),
23010 inote.type, inote.namesz, inote.descsz);
23011 break;
23012 }
23013
23014 printf (" %s: ", get_v850_elf_note_type (inote.type));
23015
23016 if (! print_v850_note (& inote))
23017 {
23018 res = false;
23019 printf ("<corrupt sizes: namesz: %#lx, descsz: %#lx>\n",
23020 inote.namesz, inote.descsz);
23021 }
23022 }
23023
23024 free (pnotes);
23025
23026 return res;
23027}
23028
23029static bool
23030process_note_sections (Filedata * filedata)
23031{
23032 Elf_Internal_Shdr *section;
23033 size_t i;
23034 unsigned int n = 0;
23035 bool res = true;
23036
23037 for (i = 0, section = filedata->section_headers;
23038 i < filedata->file_header.e_shnum && section != NULL;
23039 i++, section++)
23040 {
23041 if (section->sh_type == SHT_NOTE)
23042 {
23043 if (! process_notes_at (filedata, section, section->sh_offset,
23044 section->sh_size, section->sh_addralign))
23045 res = false;
23046 n++;
23047 }
23048
23049 if (( filedata->file_header.e_machine == EM_V800
23050 || filedata->file_header.e_machine == EM_V850
23051 || filedata->file_header.e_machine == EM_CYGNUS_V850)
23052 && section->sh_type == SHT_RENESAS_INFO)
23053 {
23054 if (! process_v850_notes (filedata, section->sh_offset,
23055 section->sh_size))
23056 res = false;
23057 n++;
23058 }
23059 }
23060
23061 if (n == 0)
23062 /* Try processing NOTE segments instead. */
23063 return process_corefile_note_segments (filedata);
23064
23065 return res;
23066}
23067
23068static bool
23069process_notes (Filedata * filedata)
23070{
23071 /* If we have not been asked to display the notes then do nothing. */
23072 if (! do_notes)
23073 return true;
23074
23075 if (filedata->file_header.e_type != ET_CORE)
23076 return process_note_sections (filedata);
23077
23078 /* No program headers means no NOTE segment. */
23079 if (filedata->file_header.e_phnum > 0)
23080 return process_corefile_note_segments (filedata);
23081
23082 if (filedata->is_separate)
23083 printf (_("No notes found in linked file '%s'.\n"),
23084 filedata->file_name);
23085 else
23086 printf (_("No notes found file.\n"));
23087
23088 return true;
23089}
23090
23091static unsigned char *
23092display_public_gnu_attributes (unsigned char * start,
23093 const unsigned char * const end)
23094{
23095 printf (_(" Unknown GNU attribute: %s\n"), start);
23096
23097 start += strnlen ((char *) start, end - start);
23098 display_raw_attribute (start, end);
23099
23100 return (unsigned char *) end;
23101}
23102
23103static unsigned char *
23104display_generic_attribute (unsigned char * start,
23105 unsigned int tag,
23106 const unsigned char * const end)
23107{
23108 if (tag == 0)
23109 return (unsigned char *) end;
23110
23111 return display_tag_value (tag, start, end);
23112}
23113
23114static bool
23115process_arch_specific (Filedata * filedata)
23116{
23117 if (! do_arch)
23118 return true;
23119
23120 switch (filedata->file_header.e_machine)
23121 {
23122 case EM_ARC:
23123 case EM_ARC_COMPACT:
23124 case EM_ARC_COMPACT2:
23125 case EM_ARC_COMPACT3:
23126 case EM_ARC_COMPACT3_64:
23127 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
23128 display_arc_attribute,
23129 display_generic_attribute);
23130 case EM_ARM:
23131 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
23132 display_arm_attribute,
23133 display_generic_attribute);
23134
23135 case EM_MIPS:
23136 case EM_MIPS_RS3_LE:
23137 return process_mips_specific (filedata);
23138
23139 case EM_MSP430:
23140 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
23141 display_msp430_attribute,
23142 display_msp430_gnu_attribute);
23143
23144 case EM_RISCV:
23145 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
23146 display_riscv_attribute,
23147 display_generic_attribute);
23148
23149 case EM_NDS32:
23150 return process_nds32_specific (filedata);
23151
23152 case EM_68K:
23153 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
23154 display_m68k_gnu_attribute);
23155
23156 case EM_PPC:
23157 case EM_PPC64:
23158 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
23159 display_power_gnu_attribute);
23160
23161 case EM_S390:
23162 case EM_S390_OLD:
23163 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
23164 display_s390_gnu_attribute);
23165
23166 case EM_SPARC:
23167 case EM_SPARC32PLUS:
23168 case EM_SPARCV9:
23169 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
23170 display_sparc_gnu_attribute);
23171
23172 case EM_TI_C6000:
23173 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
23174 display_tic6x_attribute,
23175 display_generic_attribute);
23176
23177 case EM_CSKY:
23178 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
23179 display_csky_attribute, NULL);
23180
23181 default:
23182 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
23183 display_public_gnu_attributes,
23184 display_generic_attribute);
23185 }
23186}
23187
23188static bool
23189get_file_header (Filedata * filedata)
23190{
23191 /* Read in the identity array. */
23192 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
23193 return false;
23194
23195 /* Determine how to read the rest of the header. */
23196 switch (filedata->file_header.e_ident[EI_DATA])
23197 {
23198 default:
23199 case ELFDATANONE:
23200 case ELFDATA2LSB:
23201 byte_get = byte_get_little_endian;
23202 byte_put = byte_put_little_endian;
23203 break;
23204 case ELFDATA2MSB:
23205 byte_get = byte_get_big_endian;
23206 byte_put = byte_put_big_endian;
23207 break;
23208 }
23209
23210 /* For now we only support 32 bit and 64 bit ELF files. */
23211 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
23212
23213 /* Read in the rest of the header. */
23214 if (is_32bit_elf)
23215 {
23216 Elf32_External_Ehdr ehdr32;
23217
23218 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
23219 return false;
23220
23221 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
23222 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
23223 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
23224 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
23225 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
23226 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
23227 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
23228 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
23229 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
23230 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
23231 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
23232 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
23233 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
23234 }
23235 else
23236 {
23237 Elf64_External_Ehdr ehdr64;
23238
23239 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
23240 return false;
23241
23242 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
23243 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
23244 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
23245 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
23246 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
23247 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
23248 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
23249 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
23250 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
23251 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
23252 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
23253 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
23254 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
23255 }
23256
23257 return true;
23258}
23259
23260static void
23261free_filedata (Filedata *filedata)
23262{
23263 free (filedata->program_interpreter);
23264 free (filedata->program_headers);
23265 free (filedata->section_headers);
23266 free (filedata->string_table);
23267 free (filedata->dump.dump_sects);
23268 free (filedata->dynamic_strings);
23269 free (filedata->dynamic_symbols);
23270 free (filedata->dynamic_syminfo);
23271 free (filedata->dynamic_section);
23272
23273 while (filedata->symtab_shndx_list != NULL)
23274 {
23275 elf_section_list *next = filedata->symtab_shndx_list->next;
23276 free (filedata->symtab_shndx_list);
23277 filedata->symtab_shndx_list = next;
23278 }
23279
23280 free (filedata->section_headers_groups);
23281
23282 if (filedata->section_groups)
23283 {
23284 size_t i;
23285 struct group_list * g;
23286 struct group_list * next;
23287
23288 for (i = 0; i < filedata->group_count; i++)
23289 {
23290 for (g = filedata->section_groups [i].root; g != NULL; g = next)
23291 {
23292 next = g->next;
23293 free (g);
23294 }
23295 }
23296
23297 free (filedata->section_groups);
23298 }
23299 memset (&filedata->section_headers, 0,
23300 sizeof (Filedata) - offsetof (Filedata, section_headers));
23301}
23302
23303static void
23304close_file (Filedata * filedata)
23305{
23306 if (filedata)
23307 {
23308 if (filedata->handle)
23309 fclose (filedata->handle);
23310 free (filedata);
23311 }
23312}
23313
23314void
23315close_debug_file (void * data)
23316{
23317 free_filedata ((Filedata *) data);
23318 close_file ((Filedata *) data);
23319}
23320
23321static Filedata *
23322open_file (const char * pathname, bool is_separate)
23323{
23324 struct stat statbuf;
23325 Filedata * filedata = NULL;
23326
23327 if (stat (pathname, & statbuf) < 0
23328 || ! S_ISREG (statbuf.st_mode))
23329 goto fail;
23330
23331 filedata = calloc (1, sizeof * filedata);
23332 if (filedata == NULL)
23333 goto fail;
23334
23335 filedata->handle = fopen (pathname, "rb");
23336 if (filedata->handle == NULL)
23337 goto fail;
23338
23339 filedata->file_size = statbuf.st_size;
23340 filedata->file_name = pathname;
23341 filedata->is_separate = is_separate;
23342
23343 if (! get_file_header (filedata))
23344 goto fail;
23345
23346 if (!get_section_headers (filedata, false))
23347 goto fail;
23348
23349 return filedata;
23350
23351 fail:
23352 if (filedata)
23353 {
23354 if (filedata->handle)
23355 fclose (filedata->handle);
23356 free (filedata);
23357 }
23358 return NULL;
23359}
23360
23361void *
23362open_debug_file (const char * pathname)
23363{
23364 return open_file (pathname, true);
23365}
23366
23367static void
23368initialise_dump_sects (Filedata * filedata)
23369{
23370 /* Initialise the dump_sects array from the cmdline_dump_sects array.
23371 Note we do this even if cmdline_dump_sects is empty because we
23372 must make sure that the dump_sets array is zeroed out before each
23373 object file is processed. */
23374 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
23375 memset (filedata->dump.dump_sects, 0,
23376 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
23377
23378 if (cmdline.num_dump_sects > 0)
23379 {
23380 if (filedata->dump.num_dump_sects == 0)
23381 /* A sneaky way of allocating the dump_sects array. */
23382 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
23383
23384 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
23385 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
23386 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
23387 }
23388}
23389
23390static bool
23391might_need_separate_debug_info (Filedata * filedata)
23392{
23393 /* Debuginfo files do not need further separate file loading. */
23394 if (filedata->file_header.e_shstrndx == SHN_UNDEF)
23395 return false;
23396
23397 /* Since do_follow_links might be enabled by default, only treat it as an
23398 indication that separate files should be loaded if setting it was a
23399 deliberate user action. */
23400 if (DEFAULT_FOR_FOLLOW_LINKS == 0 && do_follow_links)
23401 return true;
23402
23403 if (process_links || do_syms || do_unwind
23404 || dump_any_debugging || do_dump || do_debugging)
23405 return true;
23406
23407 return false;
23408}
23409
23410/* Process one ELF object file according to the command line options.
23411 This file may actually be stored in an archive. The file is
23412 positioned at the start of the ELF object. Returns TRUE if no
23413 problems were encountered, FALSE otherwise. */
23414
23415static bool
23416process_object (Filedata * filedata)
23417{
23418 bool have_separate_files;
23419 unsigned int i;
23420 bool res;
23421
23422 if (! get_file_header (filedata))
23423 {
23424 error (_("%s: Failed to read file header\n"), filedata->file_name);
23425 return false;
23426 }
23427
23428 /* Initialise per file variables. */
23429 for (i = ARRAY_SIZE (filedata->version_info); i--;)
23430 filedata->version_info[i] = 0;
23431
23432 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
23433 filedata->dynamic_info[i] = 0;
23434 filedata->dynamic_info_DT_GNU_HASH = 0;
23435 filedata->dynamic_info_DT_MIPS_XHASH = 0;
23436
23437 /* Process the file. */
23438 if (show_name)
23439 printf (_("\nFile: %s\n"), filedata->file_name);
23440
23441 initialise_dump_sects (filedata);
23442
23443 /* There may be some extensions in the first section header. Don't
23444 bomb if we can't read it. */
23445 get_section_headers (filedata, true);
23446
23447 if (! process_file_header (filedata))
23448 {
23449 res = false;
23450 goto out;
23451 }
23452
23453 /* Throw away the single section header read above, so that we
23454 re-read the entire set. */
23455 free (filedata->section_headers);
23456 filedata->section_headers = NULL;
23457
23458 if (! process_section_headers (filedata))
23459 {
23460 /* Without loaded section headers we cannot process lots of things. */
23461 do_unwind = do_version = do_dump = do_arch = false;
23462
23463 if (! do_using_dynamic)
23464 do_syms = do_dyn_syms = do_reloc = false;
23465 }
23466
23467 if (! process_section_groups (filedata))
23468 /* Without loaded section groups we cannot process unwind. */
23469 do_unwind = false;
23470
23471 process_program_headers (filedata);
23472
23473 res = process_dynamic_section (filedata);
23474
23475 if (! process_relocs (filedata))
23476 res = false;
23477
23478 if (! process_unwind (filedata))
23479 res = false;
23480
23481 if (! process_symbol_table (filedata))
23482 res = false;
23483
23484 if (! process_lto_symbol_tables (filedata))
23485 res = false;
23486
23487 if (! process_syminfo (filedata))
23488 res = false;
23489
23490 if (! process_version_sections (filedata))
23491 res = false;
23492
23493 if (might_need_separate_debug_info (filedata))
23494 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
23495 else
23496 have_separate_files = false;
23497
23498 if (! process_section_contents (filedata))
23499 res = false;
23500
23501 if (have_separate_files)
23502 {
23503 separate_info * d;
23504
23505 for (d = first_separate_info; d != NULL; d = d->next)
23506 {
23507 initialise_dump_sects (d->handle);
23508
23509 if (process_links && ! process_file_header (d->handle))
23510 res = false;
23511 else if (! process_section_headers (d->handle))
23512 res = false;
23513 else if (! process_section_contents (d->handle))
23514 res = false;
23515 else if (process_links)
23516 {
23517 if (! process_section_groups (d->handle))
23518 res = false;
23519 process_program_headers (d->handle);
23520 if (! process_dynamic_section (d->handle))
23521 res = false;
23522 if (! process_relocs (d->handle))
23523 res = false;
23524 if (! process_unwind (d->handle))
23525 res = false;
23526 if (! process_symbol_table (d->handle))
23527 res = false;
23528 if (! process_lto_symbol_tables (d->handle))
23529 res = false;
23530 if (! process_syminfo (d->handle))
23531 res = false;
23532 if (! process_version_sections (d->handle))
23533 res = false;
23534 if (! process_notes (d->handle))
23535 res = false;
23536 }
23537 }
23538
23539 /* The file handles are closed by the call to free_debug_memory() below. */
23540 }
23541
23542 if (! process_notes (filedata))
23543 res = false;
23544
23545 if (! process_gnu_liblist (filedata))
23546 res = false;
23547
23548 if (! process_arch_specific (filedata))
23549 res = false;
23550
23551 out:
23552 free_filedata (filedata);
23553
23554 free_debug_memory ();
23555
23556 return res;
23557}
23558
23559/* Process an ELF archive.
23560 On entry the file is positioned just after the ARMAG string.
23561 Returns TRUE upon success, FALSE otherwise. */
23562
23563static bool
23564process_archive (Filedata * filedata, bool is_thin_archive)
23565{
23566 struct archive_info arch;
23567 struct archive_info nested_arch;
23568 size_t got;
23569 bool ret = true;
23570
23571 show_name = true;
23572
23573 /* The ARCH structure is used to hold information about this archive. */
23574 arch.file_name = NULL;
23575 arch.file = NULL;
23576 arch.index_array = NULL;
23577 arch.sym_table = NULL;
23578 arch.longnames = NULL;
23579
23580 /* The NESTED_ARCH structure is used as a single-item cache of information
23581 about a nested archive (when members of a thin archive reside within
23582 another regular archive file). */
23583 nested_arch.file_name = NULL;
23584 nested_arch.file = NULL;
23585 nested_arch.index_array = NULL;
23586 nested_arch.sym_table = NULL;
23587 nested_arch.longnames = NULL;
23588
23589 if (setup_archive (&arch, filedata->file_name, filedata->handle,
23590 filedata->file_size, is_thin_archive,
23591 do_archive_index) != 0)
23592 {
23593 ret = false;
23594 goto out;
23595 }
23596
23597 if (do_archive_index)
23598 {
23599 if (arch.sym_table == NULL)
23600 error (_("%s: unable to dump the index as none was found\n"),
23601 filedata->file_name);
23602 else
23603 {
23604 uint64_t i, l;
23605 uint64_t current_pos;
23606
23607 printf (_("Index of archive %s: (%" PRIu64 " entries,"
23608 " %#" PRIx64 " bytes in the symbol table)\n"),
23609 filedata->file_name, arch.index_num,
23610 arch.sym_size);
23611
23612 current_pos = ftell (filedata->handle);
23613
23614 for (i = l = 0; i < arch.index_num; i++)
23615 {
23616 if (i == 0
23617 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
23618 {
23619 char * member_name
23620 = get_archive_member_name_at (&arch, arch.index_array[i],
23621 &nested_arch);
23622
23623 if (member_name != NULL)
23624 {
23625 char * qualified_name
23626 = make_qualified_name (&arch, &nested_arch,
23627 member_name);
23628
23629 if (qualified_name != NULL)
23630 {
23631 printf (_("Contents of binary %s at offset "),
23632 qualified_name);
23633 (void) print_vma (arch.index_array[i], PREFIX_HEX);
23634 putchar ('\n');
23635 free (qualified_name);
23636 }
23637 free (member_name);
23638 }
23639 }
23640
23641 if (l >= arch.sym_size)
23642 {
23643 error (_("%s: end of the symbol table reached "
23644 "before the end of the index\n"),
23645 filedata->file_name);
23646 ret = false;
23647 break;
23648 }
23649 /* PR 17531: file: 0b6630b2. */
23650 printf ("\t%.*s\n",
23651 (int) (arch.sym_size - l), arch.sym_table + l);
23652 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
23653 }
23654
23655 if (arch.uses_64bit_indices)
23656 l = (l + 7) & ~ 7;
23657 else
23658 l += l & 1;
23659
23660 if (l < arch.sym_size)
23661 {
23662 error (ngettext ("%s: %" PRId64 " byte remains in the symbol table, "
23663 "but without corresponding entries in "
23664 "the index table\n",
23665 "%s: %" PRId64 " bytes remain in the symbol table, "
23666 "but without corresponding entries in "
23667 "the index table\n",
23668 arch.sym_size - l),
23669 filedata->file_name, arch.sym_size - l);
23670 ret = false;
23671 }
23672
23673 if (fseek64 (filedata->handle, current_pos, SEEK_SET) != 0)
23674 {
23675 error (_("%s: failed to seek back to start of object files "
23676 "in the archive\n"),
23677 filedata->file_name);
23678 ret = false;
23679 goto out;
23680 }
23681 }
23682
23683 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
23684 && !do_segments && !do_header && !do_dump && !do_version
23685 && !do_histogram && !do_debugging && !do_arch && !do_notes
23686 && !do_section_groups && !do_dyn_syms)
23687 {
23688 ret = true; /* Archive index only. */
23689 goto out;
23690 }
23691 }
23692
23693 while (1)
23694 {
23695 char * name;
23696 size_t namelen;
23697 char * qualified_name;
23698
23699 /* Read the next archive header. */
23700 if (fseek64 (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
23701 {
23702 error (_("%s: failed to seek to next archive header\n"),
23703 arch.file_name);
23704 ret = false;
23705 break;
23706 }
23707 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
23708 if (got != sizeof arch.arhdr)
23709 {
23710 if (got == 0)
23711 break;
23712 /* PR 24049 - we cannot use filedata->file_name as this will
23713 have already been freed. */
23714 error (_("%s: failed to read archive header\n"), arch.file_name);
23715
23716 ret = false;
23717 break;
23718 }
23719 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
23720 {
23721 error (_("%s: did not find a valid archive header\n"),
23722 arch.file_name);
23723 ret = false;
23724 break;
23725 }
23726
23727 arch.next_arhdr_offset += sizeof arch.arhdr;
23728
23729 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
23730
23731 name = get_archive_member_name (&arch, &nested_arch);
23732 if (name == NULL)
23733 {
23734 error (_("%s: bad archive file name\n"), arch.file_name);
23735 ret = false;
23736 break;
23737 }
23738 namelen = strlen (name);
23739
23740 qualified_name = make_qualified_name (&arch, &nested_arch, name);
23741 if (qualified_name == NULL)
23742 {
23743 error (_("%s: bad archive file name\n"), arch.file_name);
23744 free (name);
23745 ret = false;
23746 break;
23747 }
23748
23749 if (is_thin_archive && arch.nested_member_origin == 0)
23750 {
23751 /* This is a proxy for an external member of a thin archive. */
23752 Filedata * member_filedata;
23753 char * member_file_name = adjust_relative_path
23754 (filedata->file_name, name, namelen);
23755
23756 free (name);
23757 if (member_file_name == NULL)
23758 {
23759 free (qualified_name);
23760 ret = false;
23761 break;
23762 }
23763
23764 member_filedata = open_file (member_file_name, false);
23765 if (member_filedata == NULL)
23766 {
23767 error (_("Input file '%s' is not readable.\n"), member_file_name);
23768 free (member_file_name);
23769 free (qualified_name);
23770 ret = false;
23771 break;
23772 }
23773
23774 filedata->archive_file_offset = arch.nested_member_origin;
23775 member_filedata->file_name = qualified_name;
23776
23777 /* The call to process_object() expects the file to be at the beginning. */
23778 rewind (member_filedata->handle);
23779
23780 if (! process_object (member_filedata))
23781 ret = false;
23782
23783 close_file (member_filedata);
23784 free (member_file_name);
23785 }
23786 else if (is_thin_archive)
23787 {
23788 Filedata thin_filedata;
23789
23790 memset (&thin_filedata, 0, sizeof (thin_filedata));
23791
23792 /* PR 15140: Allow for corrupt thin archives. */
23793 if (nested_arch.file == NULL)
23794 {
23795 error (_("%s: contains corrupt thin archive: %s\n"),
23796 qualified_name, name);
23797 free (qualified_name);
23798 free (name);
23799 ret = false;
23800 break;
23801 }
23802 free (name);
23803
23804 /* This is a proxy for a member of a nested archive. */
23805 filedata->archive_file_offset
23806 = arch.nested_member_origin + sizeof arch.arhdr;
23807
23808 /* The nested archive file will have been opened and setup by
23809 get_archive_member_name. */
23810 if (fseek64 (nested_arch.file, filedata->archive_file_offset,
23811 SEEK_SET) != 0)
23812 {
23813 error (_("%s: failed to seek to archive member.\n"),
23814 nested_arch.file_name);
23815 free (qualified_name);
23816 ret = false;
23817 break;
23818 }
23819
23820 thin_filedata.handle = nested_arch.file;
23821 thin_filedata.file_name = qualified_name;
23822
23823 if (! process_object (& thin_filedata))
23824 ret = false;
23825 }
23826 else
23827 {
23828 free (name);
23829 filedata->archive_file_offset = arch.next_arhdr_offset;
23830 filedata->file_name = qualified_name;
23831 if (! process_object (filedata))
23832 ret = false;
23833 arch.next_arhdr_offset += (filedata->archive_file_size + 1) & -2;
23834 /* Stop looping with "negative" archive_file_size. */
23835 if (arch.next_arhdr_offset < filedata->archive_file_size)
23836 arch.next_arhdr_offset = -1ul;
23837 }
23838
23839 free (qualified_name);
23840 }
23841
23842 out:
23843 if (nested_arch.file != NULL)
23844 fclose (nested_arch.file);
23845 release_archive (&nested_arch);
23846 release_archive (&arch);
23847
23848 return ret;
23849}
23850
23851static bool
23852process_file (char * file_name)
23853{
23854 Filedata * filedata = NULL;
23855 struct stat statbuf;
23856 char armag[SARMAG];
23857 bool ret = true;
23858
23859 if (stat (file_name, &statbuf) < 0)
23860 {
23861 if (errno == ENOENT)
23862 error (_("'%s': No such file\n"), file_name);
23863 else
23864 error (_("Could not locate '%s'. System error message: %s\n"),
23865 file_name, strerror (errno));
23866 return false;
23867 }
23868
23869 if (! S_ISREG (statbuf.st_mode))
23870 {
23871 error (_("'%s' is not an ordinary file\n"), file_name);
23872 return false;
23873 }
23874
23875 filedata = calloc (1, sizeof * filedata);
23876 if (filedata == NULL)
23877 {
23878 error (_("Out of memory allocating file data structure\n"));
23879 return false;
23880 }
23881
23882 filedata->file_name = file_name;
23883 filedata->handle = fopen (file_name, "rb");
23884 if (filedata->handle == NULL)
23885 {
23886 error (_("Input file '%s' is not readable.\n"), file_name);
23887 free (filedata);
23888 return false;
23889 }
23890
23891 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
23892 {
23893 error (_("%s: Failed to read file's magic number\n"), file_name);
23894 fclose (filedata->handle);
23895 free (filedata);
23896 return false;
23897 }
23898
23899 filedata->file_size = statbuf.st_size;
23900 filedata->is_separate = false;
23901
23902 if (memcmp (armag, ARMAG, SARMAG) == 0)
23903 {
23904 if (! process_archive (filedata, false))
23905 ret = false;
23906 }
23907 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
23908 {
23909 if ( ! process_archive (filedata, true))
23910 ret = false;
23911 }
23912 else
23913 {
23914 if (do_archive_index && !check_all)
23915 error (_("File %s is not an archive so its index cannot be displayed.\n"),
23916 file_name);
23917
23918 rewind (filedata->handle);
23919 filedata->archive_file_size = filedata->archive_file_offset = 0;
23920
23921 if (! process_object (filedata))
23922 ret = false;
23923 }
23924
23925 fclose (filedata->handle);
23926 free (filedata->section_headers);
23927 free (filedata->program_headers);
23928 free (filedata->string_table);
23929 free (filedata->dump.dump_sects);
23930 free (filedata);
23931
23932 free (ba_cache.strtab);
23933 ba_cache.strtab = NULL;
23934 free (ba_cache.symtab);
23935 ba_cache.symtab = NULL;
23936 ba_cache.filedata = NULL;
23937
23938 return ret;
23939}
23940
23941#ifdef SUPPORT_DISASSEMBLY
23942/* Needed by the i386 disassembler. For extra credit, someone could
23943 fix this so that we insert symbolic addresses here, esp for GOT/PLT
23944 symbols. */
23945
23946void
23947print_address (unsigned int addr, FILE * outfile)
23948{
23949 fprintf (outfile,"0x%8.8x", addr);
23950}
23951
23952/* Needed by the i386 disassembler. */
23953
23954void
23955db_task_printsym (unsigned int addr)
23956{
23957 print_address (addr, stderr);
23958}
23959#endif
23960
23961int
23962main (int argc, char ** argv)
23963{
23964 int err;
23965
23966#ifdef HAVE_LC_MESSAGES
23967 setlocale (LC_MESSAGES, "");
23968#endif
23969 setlocale (LC_CTYPE, "");
23970 bindtextdomain (PACKAGE, LOCALEDIR);
23971 textdomain (PACKAGE);
23972
23973 expandargv (&argc, &argv);
23974
23975 parse_args (& cmdline, argc, argv);
23976
23977 if (optind < (argc - 1))
23978 /* When displaying information for more than one file,
23979 prefix the information with the file name. */
23980 show_name = true;
23981 else if (optind >= argc)
23982 {
23983 /* Ensure that the warning is always displayed. */
23984 do_checks = true;
23985
23986 warn (_("Nothing to do.\n"));
23987 usage (stderr);
23988 }
23989
23990 err = false;
23991 while (optind < argc)
23992 if (! process_file (argv[optind++]))
23993 err = true;
23994
23995 free (cmdline.dump_sects);
23996
23997 free (dump_ctf_symtab_name);
23998 free (dump_ctf_strtab_name);
23999 free (dump_ctf_parent_name);
24000
24001 return err ? EXIT_FAILURE : EXIT_SUCCESS;
24002}