]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame_incremental - binutils/readelf.c
Unconditionally pass is_a_field_of_this in c-exp.y
[thirdparty/binutils-gdb.git] / binutils / readelf.c
... / ...
CommitLineData
1/* readelf.c -- display contents of an ELF format file
2 Copyright (C) 1998-2025 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_got_section_contents = false;
233static bool do_section_groups = false;
234static bool do_section_details = false;
235static bool do_segments = false;
236static bool do_unwind = false;
237static bool do_using_dynamic = false;
238static bool do_header = false;
239static bool do_dump = false;
240static bool do_version = false;
241static bool do_histogram = false;
242static bool do_debugging = false;
243static bool do_ctf = false;
244static bool do_sframe = false;
245static bool do_arch = false;
246static bool do_notes = false;
247static bool do_archive_index = false;
248static bool check_all = false;
249static bool is_32bit_elf = false;
250static bool decompress_dumps = false;
251static bool do_not_show_symbol_truncation = false;
252static bool do_demangle = false; /* Pretty print C++ symbol names. */
253static bool process_links = false;
254static bool dump_any_debugging = false;
255static bool extra_sym_info = false;
256static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
257static int sym_base = 0;
258
259static char *dump_ctf_parent_name;
260static char *dump_ctf_symtab_name;
261static char *dump_ctf_strtab_name;
262
263struct group_list
264{
265 struct group_list * next;
266 unsigned int section_index;
267};
268
269struct group
270{
271 struct group_list * root;
272 unsigned int group_index;
273};
274
275typedef struct filedata
276{
277 const char * file_name;
278 bool is_separate;
279 FILE * handle;
280 uint64_t file_size;
281 Elf_Internal_Ehdr file_header;
282 uint64_t archive_file_offset;
283 uint64_t archive_file_size;
284 /* Everything below this point is cleared out by free_filedata. */
285 Elf_Internal_Shdr * section_headers;
286 Elf_Internal_Phdr * program_headers;
287 char * string_table;
288 uint64_t string_table_length;
289 uint64_t dynamic_addr;
290 uint64_t dynamic_size;
291 uint64_t dynamic_nent;
292 Elf_Internal_Dyn * dynamic_section;
293 Elf_Internal_Shdr * dynamic_strtab_section;
294 char * dynamic_strings;
295 uint64_t dynamic_strings_length;
296 Elf_Internal_Shdr * dynamic_symtab_section;
297 uint64_t num_dynamic_syms;
298 Elf_Internal_Sym * dynamic_symbols;
299 uint64_t version_info[16];
300 unsigned int dynamic_syminfo_nent;
301 Elf_Internal_Syminfo * dynamic_syminfo;
302 uint64_t dynamic_syminfo_offset;
303 uint64_t nbuckets;
304 uint64_t nchains;
305 uint64_t * buckets;
306 uint64_t * chains;
307 uint64_t ngnubuckets;
308 uint64_t ngnuchains;
309 uint64_t * gnubuckets;
310 uint64_t * gnuchains;
311 uint64_t * mipsxlat;
312 uint64_t gnusymidx;
313 char * program_interpreter;
314 uint64_t dynamic_info[DT_RELRENT + 1];
315 uint64_t dynamic_info_DT_GNU_HASH;
316 uint64_t dynamic_info_DT_MIPS_XHASH;
317 elf_section_list * symtab_shndx_list;
318 size_t group_count;
319 struct group * section_groups;
320 struct group ** section_headers_groups;
321 /* A dynamic array of flags indicating for which sections a dump of
322 some kind has been requested. It is reset on a per-object file
323 basis and then initialised from the cmdline_dump_sects array,
324 the results of interpreting the -w switch, and the
325 dump_sects_byname list. */
326 struct dump_data dump;
327} Filedata;
328
329/* How to print a vma value. */
330typedef enum print_mode
331{
332 HEX,
333 HEX_5,
334 DEC,
335 DEC_5,
336 UNSIGNED,
337 UNSIGNED_5,
338 PREFIX_HEX,
339 PREFIX_HEX_5,
340 FULL_HEX,
341 LONG_HEX,
342 ZERO_HEX,
343 OCTAL,
344 OCTAL_5
345}
346print_mode;
347
348typedef enum unicode_display_type
349{
350 unicode_default = 0,
351 unicode_locale,
352 unicode_escape,
353 unicode_hex,
354 unicode_highlight,
355 unicode_invalid
356} unicode_display_type;
357
358static unicode_display_type unicode_display = unicode_default;
359
360typedef enum
361{
362 reltype_unknown,
363 reltype_rel,
364 reltype_rela,
365 reltype_relr
366} relocation_type;
367
368/* Versioned symbol info. */
369enum versioned_symbol_info
370{
371 symbol_undefined,
372 symbol_hidden,
373 symbol_public
374};
375
376/* Relocation entries */
377
378typedef struct elf_relocation
379{
380 bfd_vma r_offset; /* Location at which to apply the action */
381 bfd_vma r_addend; /* Constant addend used to compute value */
382 const char *r_name; /* Relocation name. */
383 char *r_symbol; /* Relocation symbol. */
384 relocation_type r_type; /* Relocation type. */
385} elf_relocation;
386
387static elf_relocation *all_relocations_root;
388static elf_relocation *all_relocations;
389static size_t all_relocations_count;
390
391static int
392fseek64 (FILE *stream, int64_t offset, int whence)
393{
394#if defined (HAVE_FSEEKO64)
395 off64_t o = offset;
396 if (o != offset)
397 {
398 errno = EINVAL;
399 return -1;
400 }
401 return fseeko64 (stream, o, whence);
402#elif defined (HAVE_FSEEKO)
403 off_t o = offset;
404 if (o != offset)
405 {
406 errno = EINVAL;
407 return -1;
408 }
409 return fseeko (stream, o, whence);
410#else
411 long o = offset;
412 if (o != offset)
413 {
414 errno = EINVAL;
415 return -1;
416 }
417 return fseek (stream, o, whence);
418#endif
419}
420
421static const char * get_symbol_version_string
422 (Filedata *, bool, const char *, size_t, unsigned,
423 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
424
425static bool process_notes_at
426 (Filedata *, Elf_Internal_Shdr *, uint64_t, uint64_t, uint64_t);
427
428#define UNKNOWN -1
429
430static inline const char *
431section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
432{
433 return filedata->string_table + hdr->sh_name;
434}
435
436static inline bool
437section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
438{
439 return (filedata != NULL
440 && hdr != NULL
441 && filedata->string_table != NULL
442 && hdr->sh_name < filedata->string_table_length);
443}
444
445/* Returns true if the given index is real/valid. Note: "real" here
446 means "references a real section in the section header" and not
447 "is a valid section index as per the ELF standard". */
448
449static inline bool
450section_index_real (const Filedata *filedata, unsigned int ndx)
451{
452 return (filedata != NULL
453 && filedata->section_headers != NULL
454 && ndx < filedata->file_header.e_shnum
455 && ndx > 0);
456}
457
458#define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
459
460static inline bool
461valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
462{
463 return strtab != NULL && offset < strtab_size;
464}
465
466static inline bool
467valid_dynamic_name (const Filedata *filedata, uint64_t offset)
468{
469 return valid_symbol_name (filedata->dynamic_strings,
470 filedata->dynamic_strings_length, offset);
471}
472
473/* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
474 already been called and verified that the string exists. */
475static inline const char *
476get_dynamic_name (const Filedata *filedata, size_t offset)
477{
478 return filedata->dynamic_strings + offset;
479}
480
481#define REMOVE_ARCH_BITS(ADDR) \
482 do \
483 { \
484 if (filedata->file_header.e_machine == EM_ARM) \
485 (ADDR) &= ~1; \
486 } \
487 while (0)
488
489/* Get the correct GNU hash section name. */
490#define GNU_HASH_SECTION_NAME(filedata) \
491 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
492\f
493/* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
494 OFFSET + the offset of the current archive member, if we are examining an
495 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
496 allocate a buffer using malloc and fill that. In either case return the
497 pointer to the start of the retrieved data or NULL if something went wrong.
498 If something does go wrong and REASON is not NULL then emit an error
499 message using REASON as part of the context. */
500
501static void *
502get_data (void *var,
503 Filedata *filedata,
504 uint64_t offset,
505 uint64_t size,
506 uint64_t nmemb,
507 const char *reason)
508{
509 void * mvar;
510 uint64_t amt = size * nmemb;
511
512 if (size == 0 || nmemb == 0)
513 return NULL;
514
515 /* If size_t is smaller than uint64_t, eg because you are building
516 on a 32-bit host, then make sure that when the sizes are cast to
517 size_t no information is lost. */
518 if ((size_t) size != size
519 || (size_t) nmemb != nmemb
520 || (size_t) amt != amt
521 || amt / size != nmemb
522 || (size_t) amt + 1 == 0)
523 {
524 if (reason)
525 error (_("Size overflow prevents reading %" PRIu64
526 " elements of size %" PRIu64 " for %s\n"),
527 nmemb, size, reason);
528 return NULL;
529 }
530
531 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
532 attempting to allocate memory when the read is bound to fail. */
533 if (filedata->archive_file_offset > filedata->file_size
534 || offset > filedata->file_size - filedata->archive_file_offset
535 || amt > filedata->file_size - filedata->archive_file_offset - offset)
536 {
537 if (reason)
538 error (_("Reading %" PRIu64 " bytes extends past end of file for %s\n"),
539 amt, reason);
540 return NULL;
541 }
542
543 if (fseek64 (filedata->handle, filedata->archive_file_offset + offset,
544 SEEK_SET))
545 {
546 if (reason)
547 error (_("Unable to seek to %#" PRIx64 " for %s\n"),
548 filedata->archive_file_offset + offset, reason);
549 return NULL;
550 }
551
552 mvar = var;
553 if (mvar == NULL)
554 {
555 /* + 1 so that we can '\0' terminate invalid string table sections. */
556 mvar = malloc ((size_t) amt + 1);
557
558 if (mvar == NULL)
559 {
560 if (reason)
561 error (_("Out of memory allocating %" PRIu64 " bytes for %s\n"),
562 amt, reason);
563 return NULL;
564 }
565
566 ((char *) mvar)[amt] = '\0';
567 }
568
569 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
570 {
571 if (reason)
572 error (_("Unable to read in %" PRIu64 " bytes of %s\n"),
573 amt, reason);
574 if (mvar != var)
575 free (mvar);
576 return NULL;
577 }
578
579 return mvar;
580}
581
582/* Print a VMA value in the MODE specified.
583 Returns the number of characters displayed. */
584
585static unsigned int
586print_vma (uint64_t vma, print_mode mode)
587{
588 unsigned int nc = 0;
589
590 switch (mode)
591 {
592 case FULL_HEX:
593 nc = printf ("0x");
594 /* Fall through. */
595 case LONG_HEX:
596 if (!is_32bit_elf)
597 return nc + printf ("%16.16" PRIx64, vma);
598 return nc + printf ("%8.8" PRIx64, vma);
599
600 case ZERO_HEX:
601 if (is_32bit_elf)
602 return printf ("%08" PRIx64, vma);
603 return printf ("%016" PRIx64, vma);
604
605 case DEC_5:
606 if (vma <= 99999)
607 return printf ("%5" PRId64, vma);
608 /* Fall through. */
609 case PREFIX_HEX:
610 nc = printf ("0x");
611 /* Fall through. */
612 case HEX:
613 return nc + printf ("%" PRIx64, vma);
614
615 case PREFIX_HEX_5:
616 nc = printf ("0x");
617 /* Fall through. */
618 case HEX_5:
619 return nc + printf ("%05" PRIx64, vma);
620
621 case DEC:
622 return printf ("%" PRId64, vma);
623
624 case UNSIGNED:
625 return printf ("%" PRIu64, vma);
626
627 case UNSIGNED_5:
628 return printf ("%5" PRIu64, vma);
629
630 case OCTAL:
631 return printf ("%" PRIo64, vma);
632
633 case OCTAL_5:
634 return printf ("%5" PRIo64, vma);
635
636 default:
637 /* FIXME: Report unrecognised mode ? */
638 return 0;
639 }
640}
641
642
643/* Display a symbol on stdout. Handles the display of control characters and
644 multibye characters (assuming the host environment supports them).
645
646 Display at most abs(WIDTH) characters, truncating as necessary,
647 unless do_wide or extra_sym_info is true.
648
649 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
650 abs(WIDTH) - 5 characters followed by "[...]".
651
652 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
653 padding as necessary.
654
655 Returns the number of emitted characters. */
656
657static unsigned int
658print_symbol_name (signed int width, const char * symbol)
659{
660 bool extra_padding = false;
661 bool do_dots = false;
662 signed int num_printed = 0;
663#ifdef HAVE_MBSTATE_T
664 mbstate_t state;
665#endif
666 unsigned int width_remaining;
667 const void * alloced_symbol = NULL;
668
669 if (width < 0)
670 {
671 /* Keep the width positive. This helps the code below. */
672 width = - width;
673 extra_padding = true;
674 }
675 else if (width == 0)
676 return 0;
677
678 if (do_wide || extra_sym_info)
679 /* Set the remaining width to a very large value.
680 This simplifies the code below. */
681 width_remaining = INT_MAX;
682 else
683 {
684 width_remaining = width;
685
686 if (! do_not_show_symbol_truncation
687 && (int) strlen (symbol) > width)
688 {
689 width_remaining -= 5;
690 if ((int) width_remaining < 0)
691 width_remaining = 0;
692 do_dots = true;
693 }
694 }
695
696#ifdef HAVE_MBSTATE_T
697 /* Initialise the multibyte conversion state. */
698 memset (& state, 0, sizeof (state));
699#endif
700
701 if (do_demangle && *symbol)
702 {
703 const char * res = cplus_demangle (symbol, demangle_flags);
704
705 if (res != NULL)
706 alloced_symbol = symbol = res;
707 }
708
709 while (width_remaining)
710 {
711 size_t n;
712 const char c = *symbol++;
713
714 if (c == 0)
715 break;
716
717 if (ISPRINT (c))
718 {
719 putchar (c);
720 width_remaining --;
721 num_printed ++;
722 }
723 else if (ISCNTRL (c))
724 {
725 /* Do not print control characters directly as they can affect terminal
726 settings. Such characters usually appear in the names generated
727 by the assembler for local labels. */
728
729 if (width_remaining < 2)
730 break;
731
732 printf ("^%c", c + 0x40);
733 width_remaining -= 2;
734 num_printed += 2;
735 }
736 else if (c == 0x7f)
737 {
738 if (width_remaining < 5)
739 break;
740 printf ("<DEL>");
741 width_remaining -= 5;
742 num_printed += 5;
743 }
744 else if (unicode_display != unicode_locale
745 && unicode_display != unicode_default)
746 {
747 /* Display unicode characters as something else. */
748 unsigned char bytes[4];
749 bool is_utf8;
750 unsigned int nbytes;
751
752 bytes[0] = c;
753
754 if (bytes[0] < 0xc0)
755 {
756 nbytes = 1;
757 is_utf8 = false;
758 }
759 else
760 {
761 bytes[1] = *symbol++;
762
763 if ((bytes[1] & 0xc0) != 0x80)
764 {
765 is_utf8 = false;
766 /* Do not consume this character. It may only
767 be the first byte in the sequence that was
768 corrupt. */
769 --symbol;
770 nbytes = 1;
771 }
772 else if ((bytes[0] & 0x20) == 0)
773 {
774 is_utf8 = true;
775 nbytes = 2;
776 }
777 else
778 {
779 bytes[2] = *symbol++;
780
781 if ((bytes[2] & 0xc0) != 0x80)
782 {
783 is_utf8 = false;
784 symbol -= 2;
785 nbytes = 1;
786 }
787 else if ((bytes[0] & 0x10) == 0)
788 {
789 is_utf8 = true;
790 nbytes = 3;
791 }
792 else
793 {
794 bytes[3] = *symbol++;
795
796 nbytes = 4;
797
798 if ((bytes[3] & 0xc0) != 0x80)
799 {
800 is_utf8 = false;
801 symbol -= 3;
802 nbytes = 1;
803 }
804 else
805 is_utf8 = true;
806 }
807 }
808 }
809
810 if (unicode_display == unicode_invalid)
811 is_utf8 = false;
812
813 if (unicode_display == unicode_hex || ! is_utf8)
814 {
815 unsigned int i;
816
817 if (width_remaining < (nbytes * 2) + 2)
818 break;
819
820 putchar (is_utf8 ? '<' : '{');
821 printf ("0x");
822 for (i = 0; i < nbytes; i++)
823 printf ("%02x", bytes[i]);
824 putchar (is_utf8 ? '>' : '}');
825 }
826 else
827 {
828 if (unicode_display == unicode_highlight && isatty (1))
829 printf ("\x1B[31;47m"); /* Red. */
830
831 switch (nbytes)
832 {
833 case 2:
834 if (width_remaining < 6)
835 break;
836 printf ("\\u%02x%02x",
837 (bytes[0] & 0x1c) >> 2,
838 ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
839 break;
840 case 3:
841 if (width_remaining < 6)
842 break;
843 printf ("\\u%02x%02x",
844 ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
845 ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
846 break;
847 case 4:
848 if (width_remaining < 8)
849 break;
850 printf ("\\u%02x%02x%02x",
851 ((bytes[0] & 0x07) << 2) | ((bytes[1] & 0x30) >> 4),
852 ((bytes[1] & 0x0f) << 4) | ((bytes[2] & 0x3c) >> 2),
853 ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
854
855 break;
856 default:
857 /* URG. */
858 break;
859 }
860
861 if (unicode_display == unicode_highlight && isatty (1))
862 printf ("\033[0m"); /* Default colour. */
863 }
864
865 if (bytes[nbytes - 1] == 0)
866 break;
867 }
868 else
869 {
870#ifdef HAVE_MBSTATE_T
871 wchar_t w;
872#endif
873 /* Let printf do the hard work of displaying multibyte characters. */
874 printf ("%.1s", symbol - 1);
875 width_remaining --;
876 num_printed ++;
877
878#ifdef HAVE_MBSTATE_T
879 /* Try to find out how many bytes made up the character that was
880 just printed. Advance the symbol pointer past the bytes that
881 were displayed. */
882 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
883#else
884 n = 1;
885#endif
886 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
887 symbol += (n - 1);
888 }
889 }
890
891 if (do_dots)
892 num_printed += printf ("[...]");
893
894 if (extra_padding && num_printed < width)
895 {
896 /* Fill in the remaining spaces. */
897 printf ("%-*s", width - num_printed, " ");
898 num_printed = width;
899 }
900
901 free ((void *) alloced_symbol);
902 return num_printed;
903}
904
905/* Returns a pointer to a static buffer containing a printable version of
906 STRING. Uses a rotating array of static buffers, so that multiple
907 successive calls will still work. eg when used in a call to printf().
908
909 If supplied MAX_LEN is the maximum number of characters to be read
910 starting from STRING.
911
912 This function is similar to print_symbol_name(), except that it does
913 not try to print multibyte characters, it just shows them as hex values.
914
915 If the string is too long for the static buffer or if it is not
916 terminated then a truncated version of the string will be returned. */
917
918static const char *
919printable_string (const char * string, unsigned int max_len)
920{
921#define NUM_STRING_BUFS 5
922#define MAX_STRING_LEN 256
923
924 static int string_buf_index = 0;
925 static char string_buf [NUM_STRING_BUFS][MAX_STRING_LEN + 1];
926
927 char * buf;
928 char * buf_start;
929
930 /* Select a buffer to use. */
931 buf_start = buf = string_buf[string_buf_index];
932 if (++ string_buf_index >= NUM_STRING_BUFS)
933 string_buf_index = 0;
934
935 char c;
936 unsigned int remaining = MAX_STRING_LEN;
937
938 while ((c = * string ++) != 0)
939 {
940 if (ISCNTRL (c))
941 {
942 if (remaining < 2)
943 break;
944
945 * buf ++ = '^';
946 * buf ++ = c + 0x40;
947 remaining -= 2;
948 }
949 else if (ISPRINT (c))
950 {
951 * buf ++ = c;
952 remaining -= 1;
953 }
954 else
955 {
956 static char hex[17] = "0123456789ABCDEF";
957
958 if (remaining < 4)
959 break;
960 * buf ++ = '<';
961 * buf ++ = hex[(c & 0xf0) >> 4];
962 * buf ++ = hex[c & 0x0f];
963 * buf ++ = '>';
964 remaining -= 4;
965 }
966
967 if (remaining == 0)
968 break;
969
970 if (max_len > 0)
971 {
972 max_len -= 1;
973 if (max_len == 0)
974 break;
975 }
976 }
977
978 * buf = 0;
979 return buf_start;
980}
981
982/* Returns a pointer to a static buffer containing a
983 printable version of the given section's name. */
984
985static const char *
986printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
987{
988 /* Validate the input parameters. */
989 if (filedata == NULL)
990 return _("<internal error>");
991 if (sec == NULL)
992 return _("<none>");
993 if (filedata->string_table == NULL)
994 return _("<no-strings>");
995 if (sec->sh_name >= filedata->string_table_length)
996 return _("<corrupt>");
997
998 return printable_string (section_name (filedata, sec),
999 filedata->string_table_length - sec->sh_name);
1000}
1001
1002/* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
1003 This OS has so many departures from the ELF standard that we test it at
1004 many places. */
1005
1006static inline bool
1007is_ia64_vms (Filedata * filedata)
1008{
1009 return filedata->file_header.e_machine == EM_IA_64
1010 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
1011}
1012
1013static const char *
1014printable_section_name_from_index (Filedata * filedata,
1015 size_t ndx,
1016 bool * is_special)
1017{
1018 if (is_special != NULL)
1019 * is_special = true;
1020
1021 switch (ndx)
1022 {
1023 case SHN_UNDEF: return "UND";
1024 case SHN_ABS: return "ABS";
1025 case SHN_COMMON: return "COM";
1026 break;
1027 }
1028
1029 if (filedata != NULL)
1030 {
1031 switch (filedata->file_header.e_machine)
1032 {
1033 case EM_MIPS:
1034 if (ndx == SHN_MIPS_SCOMMON)
1035 return "SCOMMON";
1036 if (ndx == SHN_MIPS_SUNDEFINED)
1037 return "SUNDEF";
1038 break;
1039
1040 case EM_TI_C6000:
1041 if (ndx == SHN_TIC6X_SCOMMON)
1042 return "SCOM";
1043 break;
1044
1045 case EM_X86_64:
1046 case EM_L1OM:
1047 case EM_K1OM:
1048 if (ndx == SHN_X86_64_LCOMMON)
1049 return "LARGE_COM";
1050 break;
1051
1052 case EM_IA_64:
1053 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1054 && ndx == SHN_IA_64_ANSI_COMMON)
1055 return "ANSI_COM";
1056
1057 if (is_ia64_vms (filedata) && ndx == SHN_IA_64_VMS_SYMVEC)
1058 return "VMS_SYMVEC";
1059 break;
1060
1061 default:
1062 break;
1063 }
1064
1065 if (filedata->section_headers != NULL
1066 && ndx < filedata->file_header.e_shnum)
1067 {
1068 const char * res;
1069
1070 res = printable_section_name (filedata, filedata->section_headers + ndx);
1071 if (is_special != NULL)
1072 * is_special = (res[0] == '<');
1073
1074 return res;
1075 }
1076 }
1077
1078 static char name_buf[40];
1079 unsigned int short_ndx = (unsigned int) (ndx & 0xffff);
1080
1081 if (ndx >= SHN_LOPROC && ndx <= SHN_HIPROC)
1082 sprintf (name_buf, "PRC[0x%04x]", short_ndx);
1083 else if (ndx >= SHN_LOOS && ndx <= SHN_HIOS)
1084 sprintf (name_buf, "OS [0x%04x]", short_ndx);
1085 else if (ndx >= SHN_LORESERVE)
1086 sprintf (name_buf, "RSV[0x%04x]", short_ndx);
1087 else if (filedata->file_header.e_shnum != 0
1088 && ndx >= filedata->file_header.e_shnum)
1089 sprintf (name_buf, _("BAD[0x%lx]"), (long) ndx);
1090 else
1091 sprintf (name_buf, "<section 0x%lx>", (long) ndx);
1092
1093 return name_buf;
1094}
1095
1096/* Return a pointer to section NAME, or NULL if no such section exists. */
1097
1098static Elf_Internal_Shdr *
1099find_section (Filedata * filedata, const char * name)
1100{
1101 unsigned int i;
1102
1103 if (filedata->section_headers == NULL)
1104 return NULL;
1105
1106 for (i = 0; i < filedata->file_header.e_shnum; i++)
1107 if (section_name_valid (filedata, filedata->section_headers + i)
1108 && streq (section_name (filedata, filedata->section_headers + i),
1109 name))
1110 return filedata->section_headers + i;
1111
1112 return NULL;
1113}
1114
1115/* Return a pointer to a section containing ADDR, or NULL if no such
1116 section exists. */
1117
1118static Elf_Internal_Shdr *
1119find_section_by_address (Filedata * filedata, uint64_t addr)
1120{
1121 unsigned int i;
1122
1123 if (filedata->section_headers == NULL)
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 (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
1131 return sec;
1132 }
1133
1134 return NULL;
1135}
1136
1137static Elf_Internal_Shdr *
1138find_section_by_type (Filedata * filedata, unsigned int type)
1139{
1140 unsigned int i;
1141
1142 if (filedata->section_headers == NULL)
1143 return NULL;
1144
1145 for (i = 0; i < filedata->file_header.e_shnum; i++)
1146 {
1147 Elf_Internal_Shdr *sec = filedata->section_headers + i;
1148
1149 if (sec->sh_type == type)
1150 return sec;
1151 }
1152
1153 return NULL;
1154}
1155
1156static Elf_Internal_Shdr *
1157find_section_by_name (Filedata * filedata, const char * name)
1158{
1159 unsigned int i;
1160
1161 if (filedata->section_headers == NULL || filedata->string_table_length == 0)
1162 return NULL;
1163
1164 for (i = 0; i < filedata->file_header.e_shnum; i++)
1165 {
1166 Elf_Internal_Shdr *sec = filedata->section_headers + i;
1167
1168 if (sec->sh_name < filedata->string_table_length
1169 && streq (name, filedata->string_table + sec->sh_name))
1170 return sec;
1171 }
1172
1173 return NULL;
1174}
1175
1176/* Return a pointer to section NAME, or NULL if no such section exists,
1177 restricted to the list of sections given in SET. */
1178
1179static Elf_Internal_Shdr *
1180find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
1181{
1182 unsigned int i;
1183
1184 if (filedata->section_headers == NULL)
1185 return NULL;
1186
1187 if (set != NULL)
1188 {
1189 while ((i = *set++) > 0)
1190 {
1191 /* See PR 21156 for a reproducer. */
1192 if (i >= filedata->file_header.e_shnum)
1193 continue; /* FIXME: Should we issue an error message ? */
1194
1195 if (section_name_valid (filedata, filedata->section_headers + i)
1196 && streq (section_name (filedata, filedata->section_headers + i),
1197 name))
1198 return filedata->section_headers + i;
1199 }
1200 }
1201
1202 return find_section (filedata, name);
1203}
1204
1205/* Guess the relocation size commonly used by the specific machines. */
1206
1207static bool
1208guess_is_rela (unsigned int e_machine)
1209{
1210 switch (e_machine)
1211 {
1212 /* Targets that use REL relocations. */
1213 case EM_386:
1214 case EM_IAMCU:
1215 case EM_960:
1216 case EM_ARM:
1217 case EM_D10V:
1218 case EM_CYGNUS_D10V:
1219 case EM_DLX:
1220 case EM_MIPS:
1221 case EM_MIPS_RS3_LE:
1222 case EM_CYGNUS_M32R:
1223 case EM_SCORE:
1224 case EM_XGATE:
1225 case EM_NFP:
1226 case EM_BPF:
1227 return false;
1228
1229 /* Targets that use RELA relocations. */
1230 case EM_68K:
1231 case EM_860:
1232 case EM_AARCH64:
1233 case EM_ADAPTEVA_EPIPHANY:
1234 case EM_ALPHA:
1235 case EM_ALTERA_NIOS2:
1236 case EM_ARC:
1237 case EM_ARC_COMPACT:
1238 case EM_ARC_COMPACT2:
1239 case EM_ARC_COMPACT3:
1240 case EM_ARC_COMPACT3_64:
1241 case EM_AVR:
1242 case EM_AVR_OLD:
1243 case EM_BLACKFIN:
1244 case EM_CR16:
1245 case EM_CRIS:
1246 case EM_CRX:
1247 case EM_CSKY:
1248 case EM_D30V:
1249 case EM_CYGNUS_D30V:
1250 case EM_FR30:
1251 case EM_FT32:
1252 case EM_CYGNUS_FR30:
1253 case EM_CYGNUS_FRV:
1254 case EM_H8S:
1255 case EM_H8_300:
1256 case EM_H8_300H:
1257 case EM_IA_64:
1258 case EM_IP2K:
1259 case EM_IP2K_OLD:
1260 case EM_IQ2000:
1261 case EM_KVX:
1262 case EM_LATTICEMICO32:
1263 case EM_M32C_OLD:
1264 case EM_M32C:
1265 case EM_M32R:
1266 case EM_MCORE:
1267 case EM_CYGNUS_MEP:
1268 case EM_METAG:
1269 case EM_MMIX:
1270 case EM_MN10200:
1271 case EM_CYGNUS_MN10200:
1272 case EM_MN10300:
1273 case EM_CYGNUS_MN10300:
1274 case EM_MOXIE:
1275 case EM_MSP430:
1276 case EM_MSP430_OLD:
1277 case EM_MT:
1278 case EM_NDS32:
1279 case EM_NIOS32:
1280 case EM_OR1K:
1281 case EM_PPC64:
1282 case EM_PPC:
1283 case EM_TI_PRU:
1284 case EM_RISCV:
1285 case EM_RL78:
1286 case EM_RX:
1287 case EM_S390:
1288 case EM_S390_OLD:
1289 case EM_SH:
1290 case EM_SPARC:
1291 case EM_SPARC32PLUS:
1292 case EM_SPARCV9:
1293 case EM_SPU:
1294 case EM_TI_C6000:
1295 case EM_TILEGX:
1296 case EM_TILEPRO:
1297 case EM_V800:
1298 case EM_V850:
1299 case EM_CYGNUS_V850:
1300 case EM_VAX:
1301 case EM_VISIUM:
1302 case EM_X86_64:
1303 case EM_L1OM:
1304 case EM_K1OM:
1305 case EM_XSTORMY16:
1306 case EM_XTENSA:
1307 case EM_XTENSA_OLD:
1308 case EM_MICROBLAZE:
1309 case EM_MICROBLAZE_OLD:
1310 case EM_WEBASSEMBLY:
1311 return true;
1312
1313 case EM_68HC05:
1314 case EM_68HC08:
1315 case EM_68HC11:
1316 case EM_68HC16:
1317 case EM_FX66:
1318 case EM_ME16:
1319 case EM_MMA:
1320 case EM_NCPU:
1321 case EM_NDR1:
1322 case EM_PCP:
1323 case EM_ST100:
1324 case EM_ST19:
1325 case EM_ST7:
1326 case EM_ST9PLUS:
1327 case EM_STARCORE:
1328 case EM_SVX:
1329 case EM_TINYJ:
1330 default:
1331 warn (_("Don't know about relocations on this machine architecture\n"));
1332 return false;
1333 }
1334}
1335
1336/* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1337 Returns TRUE upon success, FALSE otherwise. If successful then a
1338 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
1339 and the number of relocs loaded is placed in *NRELASP. It is the caller's
1340 responsibility to free the allocated buffer. */
1341
1342static bool
1343slurp_rela_relocs (Filedata *filedata,
1344 uint64_t rel_offset,
1345 uint64_t rel_size,
1346 Elf_Internal_Rela **relasp,
1347 uint64_t *nrelasp)
1348{
1349 Elf_Internal_Rela * relas;
1350 uint64_t nrelas;
1351 unsigned int i;
1352
1353 if (is_32bit_elf)
1354 {
1355 Elf32_External_Rela * erelas;
1356
1357 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1358 rel_size, _("32-bit relocation data"));
1359 if (!erelas)
1360 return false;
1361
1362 nrelas = rel_size / sizeof (Elf32_External_Rela);
1363
1364 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1365 sizeof (Elf_Internal_Rela));
1366
1367 if (relas == NULL)
1368 {
1369 free (erelas);
1370 error (_("out of memory parsing relocs\n"));
1371 return false;
1372 }
1373
1374 for (i = 0; i < nrelas; i++)
1375 {
1376 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1377 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1378 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1379 }
1380
1381 free (erelas);
1382 }
1383 else
1384 {
1385 Elf64_External_Rela * erelas;
1386
1387 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1388 rel_size, _("64-bit relocation data"));
1389 if (!erelas)
1390 return false;
1391
1392 nrelas = rel_size / sizeof (Elf64_External_Rela);
1393
1394 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1395 sizeof (Elf_Internal_Rela));
1396
1397 if (relas == NULL)
1398 {
1399 free (erelas);
1400 error (_("out of memory parsing relocs\n"));
1401 return false;
1402 }
1403
1404 for (i = 0; i < nrelas; i++)
1405 {
1406 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1407 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1408 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1409
1410 if (filedata->file_header.e_machine == EM_MIPS
1411 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1412 {
1413 /* In little-endian objects, r_info isn't really a
1414 64-bit little-endian value: it has a 32-bit
1415 little-endian symbol index followed by four
1416 individual byte fields. Reorder INFO
1417 accordingly. */
1418 uint64_t inf = relas[i].r_info;
1419 inf = (((inf & 0xffffffff) << 32)
1420 | ((inf >> 56) & 0xff)
1421 | ((inf >> 40) & 0xff00)
1422 | ((inf >> 24) & 0xff0000)
1423 | ((inf >> 8) & 0xff000000));
1424 relas[i].r_info = inf;
1425 }
1426 }
1427
1428 free (erelas);
1429 }
1430
1431 *relasp = relas;
1432 *nrelasp = nrelas;
1433 return true;
1434}
1435
1436/* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1437 Returns TRUE upon success, FALSE otherwise. If successful then a
1438 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1439 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1440 responsibility to free the allocated buffer. */
1441
1442static bool
1443slurp_rel_relocs (Filedata *filedata,
1444 uint64_t rel_offset,
1445 uint64_t rel_size,
1446 Elf_Internal_Rela **relsp,
1447 uint64_t *nrelsp)
1448{
1449 Elf_Internal_Rela * rels;
1450 uint64_t nrels;
1451 unsigned int i;
1452
1453 if (is_32bit_elf)
1454 {
1455 Elf32_External_Rel * erels;
1456
1457 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1458 rel_size, _("32-bit relocation data"));
1459 if (!erels)
1460 return false;
1461
1462 nrels = rel_size / sizeof (Elf32_External_Rel);
1463
1464 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1465
1466 if (rels == NULL)
1467 {
1468 free (erels);
1469 error (_("out of memory parsing relocs\n"));
1470 return false;
1471 }
1472
1473 for (i = 0; i < nrels; i++)
1474 {
1475 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1476 rels[i].r_info = BYTE_GET (erels[i].r_info);
1477 rels[i].r_addend = 0;
1478 }
1479
1480 free (erels);
1481 }
1482 else
1483 {
1484 Elf64_External_Rel * erels;
1485
1486 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1487 rel_size, _("64-bit relocation data"));
1488 if (!erels)
1489 return false;
1490
1491 nrels = rel_size / sizeof (Elf64_External_Rel);
1492
1493 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1494
1495 if (rels == NULL)
1496 {
1497 free (erels);
1498 error (_("out of memory parsing relocs\n"));
1499 return false;
1500 }
1501
1502 for (i = 0; i < nrels; i++)
1503 {
1504 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1505 rels[i].r_info = BYTE_GET (erels[i].r_info);
1506 rels[i].r_addend = 0;
1507
1508 if (filedata->file_header.e_machine == EM_MIPS
1509 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1510 {
1511 /* In little-endian objects, r_info isn't really a
1512 64-bit little-endian value: it has a 32-bit
1513 little-endian symbol index followed by four
1514 individual byte fields. Reorder INFO
1515 accordingly. */
1516 uint64_t inf = rels[i].r_info;
1517 inf = (((inf & 0xffffffff) << 32)
1518 | ((inf >> 56) & 0xff)
1519 | ((inf >> 40) & 0xff00)
1520 | ((inf >> 24) & 0xff0000)
1521 | ((inf >> 8) & 0xff000000));
1522 rels[i].r_info = inf;
1523 }
1524 }
1525
1526 free (erels);
1527 }
1528
1529 *relsp = rels;
1530 *nrelsp = nrels;
1531 return true;
1532}
1533
1534/* Returns the reloc type extracted from the reloc info field. */
1535
1536static unsigned int
1537get_reloc_type (Filedata * filedata, uint64_t reloc_info)
1538{
1539 if (is_32bit_elf)
1540 return ELF32_R_TYPE (reloc_info);
1541
1542 switch (filedata->file_header.e_machine)
1543 {
1544 case EM_MIPS:
1545 /* Note: We assume that reloc_info has already been adjusted for us. */
1546 return ELF64_MIPS_R_TYPE (reloc_info);
1547
1548 case EM_SPARCV9:
1549 return ELF64_R_TYPE_ID (reloc_info);
1550
1551 default:
1552 return ELF64_R_TYPE (reloc_info);
1553 }
1554}
1555
1556/* Return the symbol index extracted from the reloc info field. */
1557
1558static uint64_t
1559get_reloc_symindex (uint64_t reloc_info)
1560{
1561 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1562}
1563
1564static inline bool
1565uses_msp430x_relocs (Filedata * filedata)
1566{
1567 return
1568 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
1569 /* GCC uses osabi == ELFOSBI_STANDALONE. */
1570 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1571 /* TI compiler uses ELFOSABI_NONE. */
1572 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1573}
1574
1575
1576static const char *
1577get_symbol_at (Filedata * filedata,
1578 Elf_Internal_Sym * symtab,
1579 uint64_t nsyms,
1580 char * strtab,
1581 uint64_t where,
1582 uint64_t * offset_return)
1583{
1584 Elf_Internal_Sym * beg = symtab;
1585 Elf_Internal_Sym * end = symtab + nsyms;
1586 Elf_Internal_Sym * best = NULL;
1587 uint64_t dist = 0x100000;
1588
1589 /* FIXME: Since this function is likely to be called repeatedly with
1590 slightly increasing addresses each time, we could speed things up by
1591 caching the last returned value and starting our search from there. */
1592 while (beg < end)
1593 {
1594 Elf_Internal_Sym * sym;
1595 uint64_t value;
1596
1597 sym = beg + (end - beg) / 2;
1598
1599 value = sym->st_value;
1600
1601 if (where >= value
1602 && where - value < dist)
1603 {
1604 best = sym;
1605 dist = where - value;
1606 if (dist == 0)
1607 break;
1608 }
1609
1610 if (where < value)
1611 end = sym;
1612 else
1613 beg = sym + 1;
1614 }
1615
1616 const char *name;
1617
1618 /* If there is a section start closer than the found symbol then
1619 use that for symbolizing the address. */
1620 Elf_Internal_Shdr *sec = find_section_by_address (filedata, where);
1621 if (sec != NULL
1622 && where - sec->sh_addr < dist
1623 && section_name_valid (filedata, sec))
1624 {
1625 name = section_name (filedata, sec);
1626 dist = where - sec->sh_addr;
1627 }
1628 else if (best != NULL)
1629 name = strtab + best->st_name;
1630 else
1631 return NULL;
1632
1633 if (offset_return != NULL)
1634 * offset_return = dist;
1635
1636 return name;
1637}
1638
1639static void
1640print_relr_addr_and_sym (Filedata * filedata,
1641 Elf_Internal_Sym * symtab,
1642 uint64_t nsyms,
1643 char * strtab,
1644 uint64_t where)
1645{
1646 const char * symname = NULL;
1647 uint64_t offset = 0;
1648
1649 print_vma (where, ZERO_HEX);
1650 printf (" ");
1651
1652 symname = get_symbol_at (filedata, symtab, nsyms, strtab, where, & offset);
1653
1654 if (symname == NULL)
1655 printf ("<no sym>");
1656 else if (offset == 0)
1657 print_symbol_name (38, symname);
1658 else
1659 {
1660 print_symbol_name (28, symname);
1661 printf (" + ");
1662 print_vma (offset, PREFIX_HEX);
1663 }
1664}
1665
1666/* See bfd_is_aarch64_special_symbol_name. */
1667
1668static bool
1669is_aarch64_special_symbol_name (const char *name)
1670{
1671 if (!name || name[0] != '$')
1672 return false;
1673 if (name[1] == 'x' || name[1] == 'd')
1674 /* Map. */;
1675 else if (name[1] == 'm' || name[1] == 'f' || name[1] == 'p')
1676 /* Tag. */;
1677 else
1678 return false;
1679 return name[2] == 0 || name[2] == '.';
1680}
1681
1682static bool
1683is_special_symbol_name (Filedata * filedata, const char * s)
1684{
1685 switch (filedata->file_header.e_machine)
1686 {
1687 case EM_AARCH64:
1688 return is_aarch64_special_symbol_name (s);
1689
1690 default:
1691 return false;
1692 }
1693}
1694
1695/* Allows selecting the best symbol from a set for displaying addresses.
1696 BEST is the current best or NULL if there are no good symbols yet.
1697 SYM is the next symbol to consider, if it is better than BEST then
1698 return SYM else return BEST. */
1699
1700static Elf_Internal_Sym *
1701select_display_sym (Filedata * filedata,
1702 char * strtab,
1703 uint64_t strtablen,
1704 Elf_Internal_Sym * best,
1705 Elf_Internal_Sym * sym)
1706{
1707 /* Ignore empty or invalid syms. */
1708 if (sym->st_name == 0)
1709 return best;
1710 if (sym->st_name >= strtablen)
1711 return best;
1712 /* Ignore undefined or TLS syms. */
1713 if (sym->st_shndx == SHN_UNDEF)
1714 return best;
1715 if (ELF_ST_TYPE (sym->st_info) == STT_TLS)
1716 return best;
1717
1718 char *s = strtab + sym->st_name;
1719
1720 /* Don't display special symbols. */
1721 if (is_special_symbol_name (filedata, s))
1722 return best;
1723
1724 /* Here SYM is good for display. */
1725
1726 if (best == NULL)
1727 return sym;
1728
1729 char *sbest = strtab + best->st_name;
1730
1731 /* Prefer non-local symbols. */
1732 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL
1733 && ELF_ST_BIND (best->st_info) != STB_LOCAL)
1734 return best;
1735 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL
1736 && ELF_ST_BIND (best->st_info) == STB_LOCAL)
1737 return sym;
1738
1739 /* Select based on lexicographic order. */
1740 return strcmp (s, sbest) < 0 ? sym : best;
1741}
1742
1743/* Filter the sorted SYMTAB symbol array in-place to select at most one
1744 symbol for an address and drop symbols that are not good to display.
1745 Returns the new array length. */
1746
1747static uint64_t
1748filter_display_syms (Filedata * filedata,
1749 Elf_Internal_Sym * symtab,
1750 uint64_t nsyms,
1751 char * strtab,
1752 uint64_t strtablen)
1753{
1754 Elf_Internal_Sym *r = symtab;
1755 Elf_Internal_Sym *w = symtab;
1756 Elf_Internal_Sym *best = NULL;
1757 Elf_Internal_Sym *end = symtab + nsyms;
1758 while (r < end)
1759 {
1760 /* Select the best symbol for an address. */
1761 while (r < end
1762 && (best == NULL || best->st_value == r->st_value))
1763 {
1764 best = select_display_sym (filedata, strtab, strtablen, best, r);
1765 r++;
1766 }
1767 if (best != NULL)
1768 {
1769 *w = *best;
1770 w++;
1771 best = NULL;
1772 }
1773 }
1774 return w - symtab;
1775}
1776
1777static /* signed */ int
1778symcmp (const void *p, const void *q)
1779{
1780 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
1781 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
1782
1783 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
1784}
1785
1786static void
1787update_all_relocations (size_t nentries)
1788{
1789 size_t sz;
1790
1791 if (!do_got_section_contents)
1792 return;
1793
1794 if (!all_relocations_root)
1795 {
1796 sz = nentries * sizeof (elf_relocation);
1797 all_relocations_root = (elf_relocation *) xmalloc (sz);
1798 all_relocations = all_relocations_root;
1799 all_relocations_count = nentries;
1800 }
1801 else
1802 {
1803 size_t orig_count = all_relocations_count;
1804 sz = (orig_count + nentries) * sizeof (elf_relocation);
1805 all_relocations_root = (elf_relocation *)
1806 xrealloc (all_relocations_root, sz);
1807 all_relocations = all_relocations_root + orig_count;
1808 all_relocations_count += nentries;
1809 }
1810}
1811
1812static uint64_t
1813count_relr_relocations (Filedata * filedata,
1814 Elf_Internal_Shdr * section,
1815 uint64_t ** relrs_p)
1816{
1817 uint64_t * relrs;
1818 uint64_t nentries;
1819 uint64_t i;
1820 uint64_t count;
1821 int entsize;
1822
1823 if (section == NULL
1824 || section->sh_type != SHT_RELR
1825 || section->sh_size == 0)
1826 return 0;
1827
1828 entsize = section->sh_entsize;
1829 if (entsize == 0)
1830 entsize = is_32bit_elf
1831 ? sizeof (Elf32_External_Relr) : sizeof (Elf64_External_Relr);
1832 else if (entsize != sizeof (Elf32_External_Relr)
1833 && entsize != sizeof (Elf64_External_Relr))
1834 return 0;
1835
1836 nentries = section->sh_size / entsize;
1837 if (nentries == 0)
1838 return 0;
1839
1840 relrs = get_data (NULL, filedata, section->sh_offset, 1,
1841 section->sh_size, _("RELR relocation data"));
1842 if (relrs == NULL)
1843 return 0;
1844
1845 for (count = i = 0; i < nentries; i++)
1846 {
1847 uint64_t entry;
1848
1849 if (entsize == sizeof (Elf32_External_Relr))
1850 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1851 else
1852 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1853
1854 if ((entry & 1) == 0)
1855 {
1856 ++ count;
1857 }
1858 else
1859 {
1860 if (entry == 1)
1861 continue;
1862
1863 for (; entry >>= 1;)
1864 if ((entry & 1) == 1)
1865 ++ count;
1866 }
1867 }
1868
1869 *relrs_p = relrs;
1870
1871 return count;
1872}
1873
1874/* If DUMP_RELOC is false, don't display RELR relocations, just collect
1875 RELR relocations for displaying GOT section contents later. */
1876
1877static bool
1878dump_relr_relocations (Filedata * filedata,
1879 uint64_t relr_size,
1880 int relr_entsize,
1881 uint64_t relr_offset,
1882 uint64_t * relrs,
1883 const Elf_Internal_Sym * symtab_p,
1884 uint64_t nsyms,
1885 char * strtab,
1886 uint64_t strtablen,
1887 bool dump_reloc)
1888{
1889 uint64_t nentries, i;
1890 uint64_t where = 0;
1891 int num_bits_in_entry;
1892
1893 if (relr_entsize == 0)
1894 relr_entsize = is_32bit_elf
1895 ? sizeof (Elf32_External_Relr) : sizeof (Elf64_External_Relr);
1896
1897 nentries = relr_size / relr_entsize;
1898
1899 if (nentries == 0)
1900 return true;
1901
1902 if (relr_entsize == sizeof (Elf32_External_Relr))
1903 num_bits_in_entry = 31;
1904 else if (relr_entsize == sizeof (Elf64_External_Relr))
1905 num_bits_in_entry = 63;
1906 else
1907 {
1908 if (dump_reloc)
1909 warn (_("Unexpected entsize for RELR section\n"));
1910 return false;
1911 }
1912
1913 uint64_t *alloc_relrs = NULL;
1914 if (relrs == NULL)
1915 {
1916 relrs = get_data (NULL, filedata, relr_offset, 1, relr_size,
1917 _("RELR relocation data"));
1918 if (relrs == NULL)
1919 return false;
1920 alloc_relrs = relrs;
1921 }
1922
1923 /* Paranoia. */
1924 if (strtab == NULL)
1925 strtablen = 0;
1926 if (symtab_p == NULL)
1927 nsyms = 0;
1928
1929 const char *rtype = NULL;
1930 if (do_got_section_contents)
1931 switch (filedata->file_header.e_machine)
1932 {
1933 default:
1934 abort ();
1935
1936 case EM_386:
1937 case EM_IAMCU:
1938 rtype = "R_386_RELATIVE";
1939 break;
1940
1941 case EM_68K:
1942 rtype = "R_68K_RELATIVE";
1943 break;
1944
1945 case EM_860:
1946 rtype = "R_860_RELATIVE";
1947 break;
1948
1949 case EM_AARCH64:
1950 rtype = "R_AARCH64_RELATIVE";
1951 break;
1952
1953 case EM_AMDGPU:
1954 rtype = "R_AMDGPU_RELATIVE64";
1955 break;
1956
1957 case EM_ALPHA:
1958 rtype = "R_ALPHA_RELATIVE";
1959 break;
1960
1961 case EM_ALTERA_NIOS2:
1962 rtype = "R_NIOS2_RELATIVE";
1963 break;
1964
1965 case EM_ARM:
1966 rtype = "R_ARM_RELATIVE";
1967 break;
1968
1969 case EM_ARC:
1970 case EM_ARC_COMPACT:
1971 case EM_ARC_COMPACT2:
1972 case EM_ARC_COMPACT3:
1973 case EM_ARC_COMPACT3_64:
1974 rtype = "R_ARC_RELATIVE";
1975 break;
1976
1977 case EM_CRIS:
1978 rtype = "R_CRIS_RELATIVE";
1979 break;
1980
1981 case EM_CSKY:
1982 rtype = "R_CKCORE_RELATIVE";
1983 break;
1984
1985 case EM_KVX:
1986 rtype = "R_KVX_RELATIVE";
1987 break;
1988
1989 case EM_LATTICEMICO32:
1990 rtype = "R_LM32_RELATIVE";
1991 break;
1992
1993 case EM_LOONGARCH:
1994 rtype = "R_LARCH_RELATIVE";
1995 break;
1996
1997 case EM_M32R:
1998 case EM_CYGNUS_M32R:
1999 rtype = "R_M32R_RELATIVE";
2000 break;
2001
2002 case EM_MCORE:
2003 rtype = "R_MCORE_RELATIVE";
2004 break;
2005
2006 case EM_METAG:
2007 rtype = "R_METAG_RELATIVE";
2008 break;
2009
2010 case EM_MN10300:
2011 case EM_CYGNUS_MN10300:
2012 rtype = "R_MN10300_RELATIVE";
2013 break;
2014
2015 case EM_NDS32:
2016 rtype = "R_NDS32_RELATIVE";
2017 break;
2018
2019 case EM_OR1K:
2020 rtype = "R_OR1K_RELATIVE";
2021 break;
2022
2023 case EM_PPC:
2024 rtype = "R_PPC_RELATIVE";
2025 break;
2026
2027 case EM_PPC64:
2028 rtype = "R_PPC64_RELATIVE";
2029 break;
2030
2031 case EM_RISCV:
2032 rtype = "R_RISCV_RELATIVE";
2033 break;
2034
2035 case EM_S370:
2036 rtype = "R_I370_RELATIVE";
2037 break;
2038
2039 case EM_S390_OLD:
2040 case EM_S390:
2041 rtype = "R_390_RELATIVE";
2042 break;
2043
2044 case EM_SH:
2045 rtype = "R_SH_RELATIVE";
2046 break;
2047
2048 case EM_OLD_SPARCV9:
2049 case EM_SPARC32PLUS:
2050 case EM_SPARCV9:
2051 case EM_SPARC:
2052 rtype = "R_SPARC_RELATIVE";
2053 break;
2054
2055 case EM_TILEGX:
2056 rtype = "R_TILEGX_RELATIVE";
2057 break;
2058
2059 case EM_TILEPRO:
2060 rtype = "R_TILEPRO_RELATIVE";
2061 break;
2062
2063 case EM_V850:
2064 case EM_CYGNUS_V850:
2065 rtype = "R_V850_RELATIVE";
2066 break;
2067
2068 case EM_VAX:
2069 rtype = "R_VAX_RELATIVE";
2070 break;
2071
2072 case EM_X86_64:
2073 case EM_L1OM:
2074 case EM_K1OM:
2075 rtype = "R_X86_64_RELATIVE";
2076 break;
2077
2078 case EM_XTENSA_OLD:
2079 case EM_XTENSA:
2080 rtype = "R_XTENSA_RELATIVE";
2081 break;
2082 }
2083
2084 Elf_Internal_Sym *symtab = NULL;
2085 if (symtab_p != NULL)
2086 {
2087 /* Symbol tables are not sorted on address, but we want a quick lookup
2088 for the symbol associated with each address computed below, so sort
2089 the table then filter out unwanted entries. */
2090 size_t sz = nsyms * sizeof (*symtab);
2091 symtab = xmemdup (symtab_p, sz, sz);
2092 qsort (symtab, nsyms, sizeof (Elf_Internal_Sym), symcmp);
2093 nsyms = filter_display_syms (filedata, symtab, nsyms, strtab, strtablen);
2094 }
2095
2096 if (dump_reloc)
2097 {
2098 if (relr_entsize == sizeof (Elf32_External_Relr))
2099 printf (_ ("Index: Entry Address Symbolic Address\n"));
2100 else
2101 printf (_ ("Index: Entry Address Symbolic Address\n"));
2102 }
2103
2104 uint64_t r = 0;
2105 for (i = 0; i < nentries; i++)
2106 {
2107 uint64_t entry;
2108
2109 if (relr_entsize == sizeof (Elf32_External_Relr))
2110 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
2111 else
2112 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
2113
2114 if (dump_reloc)
2115 {
2116 /* We assume that there will never be more than 9999
2117 entries. */
2118 printf (_("%04u: "), (unsigned int) i);
2119 print_vma (entry, ZERO_HEX);
2120 printf (" ");
2121 }
2122
2123 if ((entry & 1) == 0)
2124 {
2125 where = entry;
2126 if (dump_reloc)
2127 {
2128 print_relr_addr_and_sym (filedata, symtab, nsyms, strtab,
2129 where);
2130 printf ("\n");
2131 }
2132
2133 if (do_got_section_contents)
2134 {
2135 all_relocations[r].r_offset = where;
2136 all_relocations[r].r_name = rtype;
2137 all_relocations[r].r_symbol = NULL;
2138 all_relocations[r].r_type = reltype_relr;
2139 r++;
2140 }
2141
2142 where += relr_entsize;
2143 }
2144 else
2145 {
2146 bool first = true;
2147 int j;
2148
2149 /* The least significant bit is ignored. */
2150 if (entry == 1)
2151 /* This can actually happen when the linker is allowed to shrink
2152 RELR sections. For more details see: https://reviews.llvm.org/D67164. */
2153 continue;
2154 else if (dump_reloc && i == 0)
2155 warn (_("Unusual RELR bitmap - no previous entry to set the base address\n"));
2156
2157 for (j = 0; entry >>= 1; j++)
2158 if ((entry & 1) == 1)
2159 {
2160 uint64_t addr = where + (j * relr_entsize);
2161
2162 if (dump_reloc)
2163 {
2164 if (first)
2165 {
2166 print_relr_addr_and_sym (filedata, symtab, nsyms,
2167 strtab, addr);
2168 first = false;
2169 }
2170 else
2171 {
2172 printf (_("\n%*s "),
2173 relr_entsize == 4 ? 15 : 23, " ");
2174 print_relr_addr_and_sym (filedata, symtab, nsyms,
2175 strtab, addr);
2176 }
2177 }
2178
2179 if (do_got_section_contents)
2180 {
2181 all_relocations[r].r_offset = addr;
2182 all_relocations[r].r_name = rtype;
2183 all_relocations[r].r_symbol = NULL;
2184 all_relocations[r].r_type = reltype_relr;
2185 r++;
2186 }
2187 }
2188
2189 if (dump_reloc)
2190 printf ("\n");
2191 where += num_bits_in_entry * relr_entsize;
2192 }
2193 }
2194
2195 free (symtab);
2196 free (alloc_relrs);
2197 return true;
2198}
2199
2200/* Display the contents of the relocation data found at the specified
2201 offset. If DUMP_RELOC is false, don't display relocations, just
2202 collect relocations for displaying GOT section contents later. */
2203
2204static bool
2205dump_relocations (Filedata * filedata,
2206 uint64_t rel_offset,
2207 uint64_t rel_size,
2208 Elf_Internal_Sym * symtab,
2209 uint64_t nsyms,
2210 char * strtab,
2211 uint64_t strtablen,
2212 relocation_type rel_type,
2213 bool is_dynsym,
2214 bool dump_reloc)
2215{
2216 size_t i;
2217 Elf_Internal_Rela * rels;
2218 bool res = true;
2219
2220 if (rel_type == reltype_unknown)
2221 rel_type = guess_is_rela (filedata->file_header.e_machine) ? reltype_rela : reltype_rel;
2222
2223 if (rel_type == reltype_rela)
2224 {
2225 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
2226 return false;
2227 }
2228 else if (rel_type == reltype_rel)
2229 {
2230 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
2231 return false;
2232 }
2233 else if (rel_type == reltype_relr)
2234 {
2235 /* This should have been handled by display_relocations(). */
2236 return false;
2237 }
2238
2239 if (dump_reloc)
2240 {
2241 if (is_32bit_elf)
2242 {
2243 if (rel_type == reltype_rela)
2244 {
2245 if (do_wide)
2246 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
2247 else
2248 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
2249 }
2250 else
2251 {
2252 if (do_wide)
2253 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
2254 else
2255 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
2256 }
2257 }
2258 else if (rel_type == reltype_rela)
2259 {
2260 if (do_wide)
2261 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
2262 else
2263 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
2264 }
2265 else
2266 {
2267 if (do_wide)
2268 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
2269 else
2270 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
2271 }
2272 }
2273
2274 for (i = 0; i < rel_size; i++)
2275 {
2276 const char * rtype;
2277 uint64_t offset;
2278 uint64_t inf;
2279 uint64_t symtab_index;
2280 uint64_t type;
2281
2282 offset = rels[i].r_offset;
2283 inf = rels[i].r_info;
2284
2285 type = get_reloc_type (filedata, inf);
2286 symtab_index = get_reloc_symindex (inf);
2287
2288 if (dump_reloc)
2289 {
2290 if (is_32bit_elf)
2291 printf ("%8.8" PRIx32 " %8.8" PRIx32 " ",
2292 (uint32_t) offset, (uint32_t) inf);
2293 else
2294 printf (do_wide
2295 ? "%16.16" PRIx64 " %16.16" PRIx64 " "
2296 : "%12.12" PRIx64 " %12.12" PRIx64 " ",
2297 offset, inf);
2298 }
2299
2300 switch (filedata->file_header.e_machine)
2301 {
2302 default:
2303 rtype = NULL;
2304 break;
2305
2306 case EM_AARCH64:
2307 rtype = elf_aarch64_reloc_type (type);
2308 break;
2309
2310 case EM_M32R:
2311 case EM_CYGNUS_M32R:
2312 rtype = elf_m32r_reloc_type (type);
2313 break;
2314
2315 case EM_386:
2316 case EM_IAMCU:
2317 rtype = elf_i386_reloc_type (type);
2318 break;
2319
2320 case EM_68HC11:
2321 case EM_68HC12:
2322 rtype = elf_m68hc11_reloc_type (type);
2323 break;
2324
2325 case EM_S12Z:
2326 rtype = elf_s12z_reloc_type (type);
2327 break;
2328
2329 case EM_68K:
2330 rtype = elf_m68k_reloc_type (type);
2331 break;
2332
2333 case EM_960:
2334 rtype = elf_i960_reloc_type (type);
2335 break;
2336
2337 case EM_AVR:
2338 case EM_AVR_OLD:
2339 rtype = elf_avr_reloc_type (type);
2340 break;
2341
2342 case EM_OLD_SPARCV9:
2343 case EM_SPARC32PLUS:
2344 case EM_SPARCV9:
2345 case EM_SPARC:
2346 rtype = elf_sparc_reloc_type (type);
2347 break;
2348
2349 case EM_SPU:
2350 rtype = elf_spu_reloc_type (type);
2351 break;
2352
2353 case EM_V800:
2354 rtype = v800_reloc_type (type);
2355 break;
2356 case EM_V850:
2357 case EM_CYGNUS_V850:
2358 rtype = v850_reloc_type (type);
2359 break;
2360
2361 case EM_D10V:
2362 case EM_CYGNUS_D10V:
2363 rtype = elf_d10v_reloc_type (type);
2364 break;
2365
2366 case EM_D30V:
2367 case EM_CYGNUS_D30V:
2368 rtype = elf_d30v_reloc_type (type);
2369 break;
2370
2371 case EM_DLX:
2372 rtype = elf_dlx_reloc_type (type);
2373 break;
2374
2375 case EM_SH:
2376 rtype = elf_sh_reloc_type (type);
2377 break;
2378
2379 case EM_MN10300:
2380 case EM_CYGNUS_MN10300:
2381 rtype = elf_mn10300_reloc_type (type);
2382 break;
2383
2384 case EM_MN10200:
2385 case EM_CYGNUS_MN10200:
2386 rtype = elf_mn10200_reloc_type (type);
2387 break;
2388
2389 case EM_FR30:
2390 case EM_CYGNUS_FR30:
2391 rtype = elf_fr30_reloc_type (type);
2392 break;
2393
2394 case EM_CYGNUS_FRV:
2395 rtype = elf_frv_reloc_type (type);
2396 break;
2397
2398 case EM_CSKY:
2399 rtype = elf_csky_reloc_type (type);
2400 break;
2401
2402 case EM_FT32:
2403 rtype = elf_ft32_reloc_type (type);
2404 break;
2405
2406 case EM_MCORE:
2407 rtype = elf_mcore_reloc_type (type);
2408 break;
2409
2410 case EM_MMIX:
2411 rtype = elf_mmix_reloc_type (type);
2412 break;
2413
2414 case EM_MOXIE:
2415 rtype = elf_moxie_reloc_type (type);
2416 break;
2417
2418 case EM_MSP430:
2419 if (uses_msp430x_relocs (filedata))
2420 {
2421 rtype = elf_msp430x_reloc_type (type);
2422 break;
2423 }
2424 /* Fall through. */
2425 case EM_MSP430_OLD:
2426 rtype = elf_msp430_reloc_type (type);
2427 break;
2428
2429 case EM_NDS32:
2430 rtype = elf_nds32_reloc_type (type);
2431 break;
2432
2433 case EM_PPC:
2434 rtype = elf_ppc_reloc_type (type);
2435 break;
2436
2437 case EM_PPC64:
2438 rtype = elf_ppc64_reloc_type (type);
2439 break;
2440
2441 case EM_MIPS:
2442 case EM_MIPS_RS3_LE:
2443 rtype = elf_mips_reloc_type (type);
2444 break;
2445
2446 case EM_RISCV:
2447 rtype = elf_riscv_reloc_type (type);
2448 break;
2449
2450 case EM_ALPHA:
2451 rtype = elf_alpha_reloc_type (type);
2452 break;
2453
2454 case EM_ARM:
2455 rtype = elf_arm_reloc_type (type);
2456 break;
2457
2458 case EM_ARC:
2459 case EM_ARC_COMPACT:
2460 case EM_ARC_COMPACT2:
2461 case EM_ARC_COMPACT3:
2462 case EM_ARC_COMPACT3_64:
2463 rtype = elf_arc_reloc_type (type);
2464 break;
2465
2466 case EM_PARISC:
2467 rtype = elf_hppa_reloc_type (type);
2468 break;
2469
2470 case EM_H8_300:
2471 case EM_H8_300H:
2472 case EM_H8S:
2473 rtype = elf_h8_reloc_type (type);
2474 break;
2475
2476 case EM_OR1K:
2477 rtype = elf_or1k_reloc_type (type);
2478 break;
2479
2480 case EM_PJ:
2481 case EM_PJ_OLD:
2482 rtype = elf_pj_reloc_type (type);
2483 break;
2484 case EM_IA_64:
2485 rtype = elf_ia64_reloc_type (type);
2486 break;
2487
2488 case EM_KVX:
2489 rtype = elf_kvx_reloc_type (type);
2490 break;
2491
2492 case EM_CRIS:
2493 rtype = elf_cris_reloc_type (type);
2494 break;
2495
2496 case EM_860:
2497 rtype = elf_i860_reloc_type (type);
2498 break;
2499
2500 case EM_X86_64:
2501 case EM_L1OM:
2502 case EM_K1OM:
2503 rtype = elf_x86_64_reloc_type (type);
2504 break;
2505
2506 case EM_S370:
2507 rtype = i370_reloc_type (type);
2508 break;
2509
2510 case EM_S390_OLD:
2511 case EM_S390:
2512 rtype = elf_s390_reloc_type (type);
2513 break;
2514
2515 case EM_SCORE:
2516 rtype = elf_score_reloc_type (type);
2517 break;
2518
2519 case EM_XSTORMY16:
2520 rtype = elf_xstormy16_reloc_type (type);
2521 break;
2522
2523 case EM_CRX:
2524 rtype = elf_crx_reloc_type (type);
2525 break;
2526
2527 case EM_VAX:
2528 rtype = elf_vax_reloc_type (type);
2529 break;
2530
2531 case EM_VISIUM:
2532 rtype = elf_visium_reloc_type (type);
2533 break;
2534
2535 case EM_BPF:
2536 rtype = elf_bpf_reloc_type (type);
2537 break;
2538
2539 case EM_ADAPTEVA_EPIPHANY:
2540 rtype = elf_epiphany_reloc_type (type);
2541 break;
2542
2543 case EM_IP2K:
2544 case EM_IP2K_OLD:
2545 rtype = elf_ip2k_reloc_type (type);
2546 break;
2547
2548 case EM_IQ2000:
2549 rtype = elf_iq2000_reloc_type (type);
2550 break;
2551
2552 case EM_XTENSA_OLD:
2553 case EM_XTENSA:
2554 rtype = elf_xtensa_reloc_type (type);
2555 break;
2556
2557 case EM_LATTICEMICO32:
2558 rtype = elf_lm32_reloc_type (type);
2559 break;
2560
2561 case EM_M32C_OLD:
2562 case EM_M32C:
2563 rtype = elf_m32c_reloc_type (type);
2564 break;
2565
2566 case EM_MT:
2567 rtype = elf_mt_reloc_type (type);
2568 break;
2569
2570 case EM_BLACKFIN:
2571 rtype = elf_bfin_reloc_type (type);
2572 break;
2573
2574 case EM_CYGNUS_MEP:
2575 rtype = elf_mep_reloc_type (type);
2576 break;
2577
2578 case EM_CR16:
2579 rtype = elf_cr16_reloc_type (type);
2580 break;
2581
2582 case EM_MICROBLAZE:
2583 case EM_MICROBLAZE_OLD:
2584 rtype = elf_microblaze_reloc_type (type);
2585 break;
2586
2587 case EM_RL78:
2588 rtype = elf_rl78_reloc_type (type);
2589 break;
2590
2591 case EM_RX:
2592 rtype = elf_rx_reloc_type (type);
2593 break;
2594
2595 case EM_METAG:
2596 rtype = elf_metag_reloc_type (type);
2597 break;
2598
2599 case EM_TI_C6000:
2600 rtype = elf_tic6x_reloc_type (type);
2601 break;
2602
2603 case EM_TILEGX:
2604 rtype = elf_tilegx_reloc_type (type);
2605 break;
2606
2607 case EM_TILEPRO:
2608 rtype = elf_tilepro_reloc_type (type);
2609 break;
2610
2611 case EM_WEBASSEMBLY:
2612 rtype = elf_wasm32_reloc_type (type);
2613 break;
2614
2615 case EM_XGATE:
2616 rtype = elf_xgate_reloc_type (type);
2617 break;
2618
2619 case EM_ALTERA_NIOS2:
2620 rtype = elf_nios2_reloc_type (type);
2621 break;
2622
2623 case EM_TI_PRU:
2624 rtype = elf_pru_reloc_type (type);
2625 break;
2626
2627 case EM_NFP:
2628 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
2629 rtype = elf_nfp3200_reloc_type (type);
2630 else
2631 rtype = elf_nfp_reloc_type (type);
2632 break;
2633
2634 case EM_Z80:
2635 rtype = elf_z80_reloc_type (type);
2636 break;
2637
2638 case EM_LOONGARCH:
2639 rtype = elf_loongarch_reloc_type (type);
2640 break;
2641
2642 case EM_AMDGPU:
2643 rtype = elf_amdgpu_reloc_type (type);
2644 break;
2645 }
2646
2647 char *symbol_name = NULL;
2648 if (dump_reloc)
2649 {
2650 if (rtype == NULL)
2651 printf (_("unrecognized: %-7lx"),
2652 (unsigned long) type & 0xffffffff);
2653 else
2654 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
2655 }
2656
2657 if (filedata->file_header.e_machine == EM_ALPHA
2658 && rtype != NULL
2659 && streq (rtype, "R_ALPHA_LITUSE")
2660 && rel_type == reltype_rela)
2661 {
2662 switch (rels[i].r_addend)
2663 {
2664 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
2665 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
2666 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
2667 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
2668 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
2669 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
2670 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
2671 default: rtype = NULL;
2672 }
2673
2674 if (dump_reloc)
2675 {
2676 if (rtype)
2677 printf (" (%s)", rtype);
2678 else
2679 {
2680 putchar (' ');
2681 printf (_("<unknown addend: %" PRIx64 ">"),
2682 rels[i].r_addend);
2683 res = false;
2684 }
2685 }
2686 }
2687 else if (symtab_index)
2688 {
2689 if (symtab == NULL || symtab_index >= nsyms)
2690 {
2691 if (dump_reloc)
2692 {
2693 error (_(" bad symbol index: %08lx in reloc\n"),
2694 (unsigned long) symtab_index);
2695 res = false;
2696 }
2697 }
2698 else
2699 {
2700 Elf_Internal_Sym * psym;
2701 const char * version_string;
2702 enum versioned_symbol_info sym_info;
2703 unsigned short vna_other;
2704
2705 psym = symtab + symtab_index;
2706
2707 version_string
2708 = get_symbol_version_string (filedata, is_dynsym,
2709 strtab, strtablen,
2710 symtab_index,
2711 psym,
2712 &sym_info,
2713 &vna_other);
2714
2715 if (dump_reloc)
2716 printf (" ");
2717
2718 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
2719 {
2720 const char * name;
2721 unsigned int len;
2722 unsigned int width = is_32bit_elf ? 8 : 14;
2723
2724 /* Relocations against GNU_IFUNC symbols do not use the value
2725 of the symbol as the address to relocate against. Instead
2726 they invoke the function named by the symbol and use its
2727 result as the address for relocation.
2728
2729 To indicate this to the user, do not display the value of
2730 the symbol in the "Symbols's Value" field. Instead show
2731 its name followed by () as a hint that the symbol is
2732 invoked. */
2733
2734 if (strtab == NULL
2735 || psym->st_name == 0
2736 || psym->st_name >= strtablen)
2737 name = "??";
2738 else
2739 name = strtab + psym->st_name;
2740
2741 if (do_got_section_contents)
2742 {
2743 if (version_string)
2744 symbol_name = concat (name,
2745 sym_info == symbol_public
2746 ? "@@" : "@",
2747 version_string, NULL);
2748 else
2749 symbol_name = xstrdup (name);
2750 }
2751
2752 if (dump_reloc)
2753 {
2754 len = print_symbol_name (width, name);
2755 if (version_string)
2756 printf (sym_info == symbol_public
2757 ? "@@%s" : "@%s",
2758 version_string);
2759 printf ("()%-*s",
2760 len <= width ? (width + 1) - len : 1,
2761 " ");
2762 }
2763 }
2764 else if (dump_reloc)
2765 {
2766 print_vma (psym->st_value, LONG_HEX);
2767
2768 printf (is_32bit_elf ? " " : " ");
2769 }
2770
2771 if (psym->st_name == 0)
2772 {
2773 const char * sec_name = "<null>";
2774
2775 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
2776 sec_name = printable_section_name_from_index
2777 (filedata, psym->st_shndx, NULL);
2778
2779 if (do_got_section_contents)
2780 symbol_name = xstrdup (sec_name);
2781 if (dump_reloc)
2782 print_symbol_name (22, sec_name);
2783 }
2784 else if (strtab == NULL)
2785 {
2786 if (dump_reloc)
2787 printf (_("<string table index: %3ld>"),
2788 psym->st_name);
2789 }
2790 else if (psym->st_name >= strtablen)
2791 {
2792 if (dump_reloc)
2793 {
2794 error (_("<corrupt string table index: %3ld>\n"),
2795 psym->st_name);
2796 res = false;
2797 }
2798 }
2799 else
2800 {
2801 if (dump_reloc)
2802 {
2803 print_symbol_name (22, strtab + psym->st_name);
2804 if (version_string)
2805 printf (sym_info == symbol_public
2806 ? "@@%s" : "@%s",
2807 version_string);
2808 }
2809 if (do_got_section_contents)
2810 {
2811 if (version_string)
2812 symbol_name = concat (strtab + psym->st_name,
2813 sym_info == symbol_public
2814 ? "@@" : "@",
2815 version_string, NULL);
2816 else
2817 symbol_name = xstrdup (strtab + psym->st_name);
2818 }
2819 }
2820
2821 if (dump_reloc && rel_type == reltype_rela)
2822 {
2823 uint64_t off = rels[i].r_addend;
2824
2825 if ((int64_t) off < 0)
2826 printf (" - %" PRIx64, -off);
2827 else
2828 printf (" + %" PRIx64, off);
2829 }
2830 }
2831 }
2832 else if (dump_reloc && rel_type == reltype_rela)
2833 {
2834 uint64_t off = rels[i].r_addend;
2835
2836 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
2837 if ((int64_t) off < 0)
2838 printf ("-%" PRIx64, -off);
2839 else
2840 printf ("%" PRIx64, off);
2841 }
2842
2843 if (do_got_section_contents)
2844 {
2845 all_relocations[i].r_offset = offset;
2846 all_relocations[i].r_name = rtype;
2847 all_relocations[i].r_symbol = symbol_name;
2848 all_relocations[i].r_addend = rels[i].r_addend;
2849 all_relocations[i].r_type = rel_type;
2850 }
2851
2852 if (dump_reloc)
2853 {
2854 if (filedata->file_header.e_machine == EM_SPARCV9
2855 && rtype != NULL
2856 && streq (rtype, "R_SPARC_OLO10"))
2857 printf (" + %" PRIx64, ELF64_R_TYPE_DATA (inf));
2858
2859 putchar ('\n');
2860
2861 if (! is_32bit_elf
2862 && filedata->file_header.e_machine == EM_MIPS)
2863 {
2864 uint64_t type2 = ELF64_MIPS_R_TYPE2 (inf);
2865 uint64_t type3 = ELF64_MIPS_R_TYPE3 (inf);
2866 const char * rtype2 = elf_mips_reloc_type (type2);
2867 const char * rtype3 = elf_mips_reloc_type (type3);
2868
2869 printf (" Type2: ");
2870
2871 if (rtype2 == NULL)
2872 printf (_("unrecognized: %-7lx"),
2873 (unsigned long) type2 & 0xffffffff);
2874 else
2875 printf ("%-17.17s", rtype2);
2876
2877 printf ("\n Type3: ");
2878
2879 if (rtype3 == NULL)
2880 printf (_("unrecognized: %-7lx"),
2881 (unsigned long) type3 & 0xffffffff);
2882 else
2883 printf ("%-17.17s", rtype3);
2884
2885 putchar ('\n');
2886 }
2887 }
2888 }
2889
2890 free (rels);
2891
2892 return res;
2893}
2894
2895static const char *
2896get_aarch64_dynamic_type (unsigned long type)
2897{
2898 switch (type)
2899 {
2900 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
2901 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
2902 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
2903 case DT_AARCH64_MEMTAG_MODE: return "AARCH64_MEMTAG_MODE";
2904 case DT_AARCH64_MEMTAG_STACK: return "AARCH64_MEMTAG_STACK";
2905
2906 default:
2907 return NULL;
2908 }
2909}
2910
2911static const char *
2912get_mips_dynamic_type (unsigned long type)
2913{
2914 switch (type)
2915 {
2916 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
2917 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
2918 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
2919 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
2920 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
2921 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
2922 case DT_MIPS_MSYM: return "MIPS_MSYM";
2923 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2924 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2925 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
2926 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
2927 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
2928 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
2929 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
2930 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
2931 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
2932 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
2933 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
2934 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
2935 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
2936 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
2937 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
2938 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
2939 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
2940 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
2941 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
2942 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
2943 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
2944 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
2945 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
2946 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2947 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
2948 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
2949 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
2950 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
2951 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2952 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
2953 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
2954 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
2955 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
2956 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
2957 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
2958 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
2959 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
2960 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
2961 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
2962 case DT_MIPS_XHASH: return "MIPS_XHASH";
2963 default:
2964 return NULL;
2965 }
2966}
2967
2968static const char *
2969get_sparc64_dynamic_type (unsigned long type)
2970{
2971 switch (type)
2972 {
2973 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
2974 default:
2975 return NULL;
2976 }
2977}
2978
2979static const char *
2980get_ppc_dynamic_type (unsigned long type)
2981{
2982 switch (type)
2983 {
2984 case DT_PPC_GOT: return "PPC_GOT";
2985 case DT_PPC_OPT: return "PPC_OPT";
2986 default:
2987 return NULL;
2988 }
2989}
2990
2991static const char *
2992get_ppc64_dynamic_type (unsigned long type)
2993{
2994 switch (type)
2995 {
2996 case DT_PPC64_GLINK: return "PPC64_GLINK";
2997 case DT_PPC64_OPD: return "PPC64_OPD";
2998 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
2999 case DT_PPC64_OPT: return "PPC64_OPT";
3000 default:
3001 return NULL;
3002 }
3003}
3004
3005static const char *
3006get_parisc_dynamic_type (unsigned long type)
3007{
3008 switch (type)
3009 {
3010 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
3011 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
3012 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
3013 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
3014 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
3015 case DT_HP_PREINIT: return "HP_PREINIT";
3016 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
3017 case DT_HP_NEEDED: return "HP_NEEDED";
3018 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
3019 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
3020 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
3021 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
3022 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
3023 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
3024 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
3025 case DT_HP_FILTERED: return "HP_FILTERED";
3026 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
3027 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
3028 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
3029 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
3030 case DT_PLT: return "PLT";
3031 case DT_PLT_SIZE: return "PLT_SIZE";
3032 case DT_DLT: return "DLT";
3033 case DT_DLT_SIZE: return "DLT_SIZE";
3034 default:
3035 return NULL;
3036 }
3037}
3038
3039static const char *
3040get_ia64_dynamic_type (unsigned long type)
3041{
3042 switch (type)
3043 {
3044 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
3045 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
3046 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
3047 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
3048 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
3049 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
3050 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
3051 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
3052 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
3053 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
3054 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
3055 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
3056 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
3057 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
3058 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
3059 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
3060 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
3061 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
3062 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
3063 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
3064 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
3065 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
3066 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
3067 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
3068 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
3069 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
3070 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
3071 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
3072 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
3073 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
3074 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
3075 default:
3076 return NULL;
3077 }
3078}
3079
3080static const char *
3081get_solaris_section_type (unsigned long type)
3082{
3083 switch (type)
3084 {
3085 case SHT_SUNW_symtabnsort:
3086 return "SUNW_symtabnsort";
3087 case SHT_SUNW_ancillary:
3088 return "SUNW_ancillary";
3089 case SHT_SUNW_phname:
3090 return "SUNW_phname";
3091 case SHT_SUNW_capchain:
3092 return "SUNW_capchain";
3093 case SHT_SUNW_capinfo:
3094 return "SUNW_capinfo";
3095 case SHT_SUNW_symsort:
3096 return "SUNW_symsort";
3097 case SHT_SUNW_tlssort:
3098 return "SUNW_tlssort";
3099 case SHT_SUNW_LDYNSYM:
3100 return "SUNW_LDYNSYM";
3101 case SHT_SUNW_dof:
3102 return "SUNW_dof";
3103 case SHT_SUNW_cap:
3104 return "SUNW_cap";
3105 case SHT_SUNW_SIGNATURE:
3106 return "SUNW_SIGNATURE";
3107 case SHT_SUNW_ANNOTATE:
3108 return "SUNW_ANNOTATE";
3109 case SHT_SUNW_DEBUGSTR:
3110 return "SUNW_DEBUGSTR";
3111 case SHT_SUNW_DEBUG:
3112 return "SUNW_DEBUG";
3113 case SHT_SUNW_move:
3114 return "SUNW_move";
3115 case SHT_SUNW_COMDAT:
3116 return "SUNW_COMDAT";
3117 case SHT_SUNW_syminfo:
3118 return "SUNW_syminfo";
3119 case SHT_SUNW_verdef:
3120 return "SUNW_verdef";
3121 case SHT_SUNW_verneed:
3122 return "SUNW_verneed";
3123 case SHT_SUNW_versym:
3124 return "SUNW_versym";
3125 case 0x70000000:
3126 return "SPARC_GOTDATA";
3127 default:
3128 return NULL;
3129 }
3130}
3131
3132static const char *
3133get_alpha_dynamic_type (unsigned long type)
3134{
3135 switch (type)
3136 {
3137 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
3138 default: return NULL;
3139 }
3140}
3141
3142static const char *
3143get_score_dynamic_type (unsigned long type)
3144{
3145 switch (type)
3146 {
3147 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
3148 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
3149 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
3150 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
3151 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
3152 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
3153 default: return NULL;
3154 }
3155}
3156
3157static const char *
3158get_tic6x_dynamic_type (unsigned long type)
3159{
3160 switch (type)
3161 {
3162 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
3163 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
3164 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
3165 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
3166 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
3167 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
3168 default: return NULL;
3169 }
3170}
3171
3172static const char *
3173get_nios2_dynamic_type (unsigned long type)
3174{
3175 switch (type)
3176 {
3177 case DT_NIOS2_GP: return "NIOS2_GP";
3178 default: return NULL;
3179 }
3180}
3181
3182static const char *
3183get_solaris_dynamic_type (unsigned long type)
3184{
3185 switch (type)
3186 {
3187 case 0x6000000d: return "SUNW_AUXILIARY";
3188 case 0x6000000e: return "SUNW_RTLDINF";
3189 case 0x6000000f: return "SUNW_FILTER";
3190 case 0x60000010: return "SUNW_CAP";
3191 case 0x60000011: return "SUNW_SYMTAB";
3192 case 0x60000012: return "SUNW_SYMSZ";
3193 case 0x60000013: return "SUNW_SORTENT";
3194 case 0x60000014: return "SUNW_SYMSORT";
3195 case 0x60000015: return "SUNW_SYMSORTSZ";
3196 case 0x60000016: return "SUNW_TLSSORT";
3197 case 0x60000017: return "SUNW_TLSSORTSZ";
3198 case 0x60000018: return "SUNW_CAPINFO";
3199 case 0x60000019: return "SUNW_STRPAD";
3200 case 0x6000001a: return "SUNW_CAPCHAIN";
3201 case 0x6000001b: return "SUNW_LDMACH";
3202 case 0x6000001d: return "SUNW_CAPCHAINENT";
3203 case 0x6000001f: return "SUNW_CAPCHAINSZ";
3204 case 0x60000021: return "SUNW_PARENT";
3205 case 0x60000023: return "SUNW_ASLR";
3206 case 0x60000025: return "SUNW_RELAX";
3207 case 0x60000029: return "SUNW_NXHEAP";
3208 case 0x6000002b: return "SUNW_NXSTACK";
3209
3210 case 0x70000001: return "SPARC_REGISTER";
3211 case 0x7ffffffd: return "AUXILIARY";
3212 case 0x7ffffffe: return "USED";
3213 case 0x7fffffff: return "FILTER";
3214
3215 default: return NULL;
3216 }
3217}
3218
3219static const char *
3220get_riscv_dynamic_type (unsigned long type)
3221{
3222 switch (type)
3223 {
3224 case DT_RISCV_VARIANT_CC: return "RISCV_VARIANT_CC";
3225 default:
3226 return NULL;
3227 }
3228}
3229
3230static const char *
3231get_x86_64_dynamic_type (unsigned long type)
3232{
3233 switch (type)
3234 {
3235 case DT_X86_64_PLT:
3236 return "DT_X86_64_PLT";
3237 case DT_X86_64_PLTSZ:
3238 return "DT_X86_64_PLTSZ";
3239 case DT_X86_64_PLTENT:
3240 return "DT_X86_64_PLTENT";
3241 default:
3242 return NULL;
3243 }
3244}
3245
3246static const char *
3247get_dynamic_type (Filedata * filedata, unsigned long type)
3248{
3249 static char buff[64];
3250
3251 switch (type)
3252 {
3253 case DT_NULL: return "NULL";
3254 case DT_NEEDED: return "NEEDED";
3255 case DT_PLTRELSZ: return "PLTRELSZ";
3256 case DT_PLTGOT: return "PLTGOT";
3257 case DT_HASH: return "HASH";
3258 case DT_STRTAB: return "STRTAB";
3259 case DT_SYMTAB: return "SYMTAB";
3260 case DT_RELA: return "RELA";
3261 case DT_RELASZ: return "RELASZ";
3262 case DT_RELAENT: return "RELAENT";
3263 case DT_STRSZ: return "STRSZ";
3264 case DT_SYMENT: return "SYMENT";
3265 case DT_INIT: return "INIT";
3266 case DT_FINI: return "FINI";
3267 case DT_SONAME: return "SONAME";
3268 case DT_RPATH: return "RPATH";
3269 case DT_SYMBOLIC: return "SYMBOLIC";
3270 case DT_REL: return "REL";
3271 case DT_RELSZ: return "RELSZ";
3272 case DT_RELENT: return "RELENT";
3273 case DT_RELR: return "RELR";
3274 case DT_RELRSZ: return "RELRSZ";
3275 case DT_RELRENT: return "RELRENT";
3276 case DT_PLTREL: return "PLTREL";
3277 case DT_DEBUG: return "DEBUG";
3278 case DT_TEXTREL: return "TEXTREL";
3279 case DT_JMPREL: return "JMPREL";
3280 case DT_BIND_NOW: return "BIND_NOW";
3281 case DT_INIT_ARRAY: return "INIT_ARRAY";
3282 case DT_FINI_ARRAY: return "FINI_ARRAY";
3283 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
3284 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
3285 case DT_RUNPATH: return "RUNPATH";
3286 case DT_FLAGS: return "FLAGS";
3287
3288 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
3289 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
3290 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
3291
3292 case DT_CHECKSUM: return "CHECKSUM";
3293 case DT_PLTPADSZ: return "PLTPADSZ";
3294 case DT_MOVEENT: return "MOVEENT";
3295 case DT_MOVESZ: return "MOVESZ";
3296 case DT_FEATURE: return "FEATURE";
3297 case DT_POSFLAG_1: return "POSFLAG_1";
3298 case DT_SYMINSZ: return "SYMINSZ";
3299 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
3300
3301 case DT_ADDRRNGLO: return "ADDRRNGLO";
3302 case DT_CONFIG: return "CONFIG";
3303 case DT_DEPAUDIT: return "DEPAUDIT";
3304 case DT_AUDIT: return "AUDIT";
3305 case DT_PLTPAD: return "PLTPAD";
3306 case DT_MOVETAB: return "MOVETAB";
3307 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
3308
3309 case DT_VERSYM: return "VERSYM";
3310
3311 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
3312 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
3313 case DT_RELACOUNT: return "RELACOUNT";
3314 case DT_RELCOUNT: return "RELCOUNT";
3315 case DT_FLAGS_1: return "FLAGS_1";
3316 case DT_VERDEF: return "VERDEF";
3317 case DT_VERDEFNUM: return "VERDEFNUM";
3318 case DT_VERNEED: return "VERNEED";
3319 case DT_VERNEEDNUM: return "VERNEEDNUM";
3320
3321 case DT_AUXILIARY: return "AUXILIARY";
3322 case DT_USED: return "USED";
3323 case DT_FILTER: return "FILTER";
3324
3325 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
3326 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
3327 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
3328 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
3329 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
3330 case DT_GNU_HASH: return "GNU_HASH";
3331 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
3332
3333 default:
3334 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
3335 {
3336 const char * result;
3337
3338 switch (filedata->file_header.e_machine)
3339 {
3340 case EM_AARCH64:
3341 result = get_aarch64_dynamic_type (type);
3342 break;
3343 case EM_MIPS:
3344 case EM_MIPS_RS3_LE:
3345 result = get_mips_dynamic_type (type);
3346 break;
3347 case EM_SPARCV9:
3348 result = get_sparc64_dynamic_type (type);
3349 break;
3350 case EM_PPC:
3351 result = get_ppc_dynamic_type (type);
3352 break;
3353 case EM_PPC64:
3354 result = get_ppc64_dynamic_type (type);
3355 break;
3356 case EM_IA_64:
3357 result = get_ia64_dynamic_type (type);
3358 break;
3359 case EM_ALPHA:
3360 result = get_alpha_dynamic_type (type);
3361 break;
3362 case EM_SCORE:
3363 result = get_score_dynamic_type (type);
3364 break;
3365 case EM_TI_C6000:
3366 result = get_tic6x_dynamic_type (type);
3367 break;
3368 case EM_ALTERA_NIOS2:
3369 result = get_nios2_dynamic_type (type);
3370 break;
3371 case EM_RISCV:
3372 result = get_riscv_dynamic_type (type);
3373 break;
3374 case EM_X86_64:
3375 result = get_x86_64_dynamic_type (type);
3376 break;
3377 default:
3378 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
3379 result = get_solaris_dynamic_type (type);
3380 else
3381 result = NULL;
3382 break;
3383 }
3384
3385 if (result != NULL)
3386 return result;
3387
3388 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
3389 }
3390 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
3391 || (filedata->file_header.e_machine == EM_PARISC
3392 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
3393 {
3394 const char * result;
3395
3396 switch (filedata->file_header.e_machine)
3397 {
3398 case EM_PARISC:
3399 result = get_parisc_dynamic_type (type);
3400 break;
3401 case EM_IA_64:
3402 result = get_ia64_dynamic_type (type);
3403 break;
3404 default:
3405 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
3406 result = get_solaris_dynamic_type (type);
3407 else
3408 result = NULL;
3409 break;
3410 }
3411
3412 if (result != NULL)
3413 return result;
3414
3415 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
3416 type);
3417 }
3418 else
3419 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
3420
3421 return buff;
3422 }
3423}
3424
3425static bool get_program_headers (Filedata *);
3426static bool get_dynamic_section (Filedata *);
3427
3428static void
3429locate_dynamic_section (Filedata *filedata)
3430{
3431 uint64_t dynamic_addr = 0;
3432 uint64_t dynamic_size = 0;
3433
3434 if (filedata->file_header.e_phnum != 0
3435 && get_program_headers (filedata))
3436 {
3437 Elf_Internal_Phdr *segment;
3438 unsigned int i;
3439
3440 for (i = 0, segment = filedata->program_headers;
3441 i < filedata->file_header.e_phnum;
3442 i++, segment++)
3443 {
3444 if (segment->p_type == PT_DYNAMIC)
3445 {
3446 dynamic_addr = segment->p_offset;
3447 dynamic_size = segment->p_filesz;
3448
3449 if (filedata->section_headers != NULL)
3450 {
3451 Elf_Internal_Shdr *sec;
3452
3453 sec = find_section (filedata, ".dynamic");
3454 if (sec != NULL)
3455 {
3456 if (sec->sh_size == 0
3457 || sec->sh_type == SHT_NOBITS)
3458 {
3459 dynamic_addr = 0;
3460 dynamic_size = 0;
3461 }
3462 else
3463 {
3464 dynamic_addr = sec->sh_offset;
3465 dynamic_size = sec->sh_size;
3466 }
3467 }
3468 }
3469
3470 if (dynamic_addr > filedata->file_size
3471 || (dynamic_size > filedata->file_size - dynamic_addr))
3472 {
3473 dynamic_addr = 0;
3474 dynamic_size = 0;
3475 }
3476 break;
3477 }
3478 }
3479 }
3480 filedata->dynamic_addr = dynamic_addr;
3481 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
3482}
3483
3484static bool
3485is_pie (Filedata *filedata)
3486{
3487 Elf_Internal_Dyn *entry;
3488
3489 if (filedata->dynamic_size == 0)
3490 locate_dynamic_section (filedata);
3491 if (filedata->dynamic_size <= 1)
3492 return false;
3493
3494 if (!get_dynamic_section (filedata))
3495 return false;
3496
3497 for (entry = filedata->dynamic_section;
3498 entry < filedata->dynamic_section + filedata->dynamic_nent;
3499 entry++)
3500 {
3501 if (entry->d_tag == DT_FLAGS_1)
3502 {
3503 if ((entry->d_un.d_val & DF_1_PIE) != 0)
3504 return true;
3505 break;
3506 }
3507 }
3508 return false;
3509}
3510
3511static char *
3512get_file_type (Filedata *filedata)
3513{
3514 unsigned e_type = filedata->file_header.e_type;
3515 static char buff[64];
3516
3517 switch (e_type)
3518 {
3519 case ET_NONE: return _("NONE (None)");
3520 case ET_REL: return _("REL (Relocatable file)");
3521 case ET_EXEC: return _("EXEC (Executable file)");
3522 case ET_DYN:
3523 if (is_pie (filedata))
3524 return _("DYN (Position-Independent Executable file)");
3525 else
3526 return _("DYN (Shared object file)");
3527 case ET_CORE: return _("CORE (Core file)");
3528
3529 default:
3530 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
3531 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
3532 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
3533 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
3534 else
3535 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
3536 return buff;
3537 }
3538}
3539
3540static char *
3541get_machine_name (unsigned e_machine)
3542{
3543 static char buff[64]; /* XXX */
3544
3545 switch (e_machine)
3546 {
3547 /* Please keep this switch table sorted by increasing EM_ value. */
3548 /* 0 */
3549 case EM_NONE: return _("None");
3550 case EM_M32: return "WE32100";
3551 case EM_SPARC: return "Sparc";
3552 case EM_386: return "Intel 80386";
3553 case EM_68K: return "MC68000";
3554 case EM_88K: return "MC88000";
3555 case EM_IAMCU: return "Intel MCU";
3556 case EM_860: return "Intel 80860";
3557 case EM_MIPS: return "MIPS R3000";
3558 case EM_S370: return "IBM System/370";
3559 /* 10 */
3560 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
3561 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
3562 case EM_PARISC: return "HPPA";
3563 case EM_VPP550: return "Fujitsu VPP500";
3564 case EM_SPARC32PLUS: return "Sparc v8+" ;
3565 case EM_960: return "Intel 80960";
3566 case EM_PPC: return "PowerPC";
3567 /* 20 */
3568 case EM_PPC64: return "PowerPC64";
3569 case EM_S390_OLD:
3570 case EM_S390: return "IBM S/390";
3571 case EM_SPU: return "SPU";
3572 /* 30 */
3573 case EM_V800: return "Renesas V850 (using RH850 ABI)";
3574 case EM_FR20: return "Fujitsu FR20";
3575 case EM_RH32: return "TRW RH32";
3576 case EM_MCORE: return "MCORE";
3577 /* 40 */
3578 case EM_ARM: return "ARM";
3579 case EM_OLD_ALPHA: return "Digital Alpha (old)";
3580 case EM_SH: return "Renesas / SuperH SH";
3581 case EM_SPARCV9: return "Sparc v9";
3582 case EM_TRICORE: return "Siemens Tricore";
3583 case EM_ARC: return "ARC";
3584 case EM_H8_300: return "Renesas H8/300";
3585 case EM_H8_300H: return "Renesas H8/300H";
3586 case EM_H8S: return "Renesas H8S";
3587 case EM_H8_500: return "Renesas H8/500";
3588 /* 50 */
3589 case EM_IA_64: return "Intel IA-64";
3590 case EM_MIPS_X: return "Stanford MIPS-X";
3591 case EM_COLDFIRE: return "Motorola Coldfire";
3592 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
3593 case EM_MMA: return "Fujitsu Multimedia Accelerator";
3594 case EM_PCP: return "Siemens PCP";
3595 case EM_NCPU: return "Sony nCPU embedded RISC processor";
3596 case EM_NDR1: return "Denso NDR1 microprocessor";
3597 case EM_STARCORE: return "Motorola Star*Core processor";
3598 case EM_ME16: return "Toyota ME16 processor";
3599 /* 60 */
3600 case EM_ST100: return "STMicroelectronics ST100 processor";
3601 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
3602 case EM_X86_64: return "Advanced Micro Devices X86-64";
3603 case EM_PDSP: return "Sony DSP processor";
3604 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
3605 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
3606 case EM_FX66: return "Siemens FX66 microcontroller";
3607 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
3608 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
3609 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
3610 /* 70 */
3611 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
3612 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
3613 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
3614 case EM_SVX: return "Silicon Graphics SVx";
3615 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
3616 case EM_VAX: return "Digital VAX";
3617 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
3618 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
3619 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
3620 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
3621 /* 80 */
3622 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
3623 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
3624 case EM_PRISM: return "Vitesse Prism";
3625 case EM_AVR_OLD:
3626 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
3627 case EM_CYGNUS_FR30:
3628 case EM_FR30: return "Fujitsu FR30";
3629 case EM_CYGNUS_D10V:
3630 case EM_D10V: return "d10v";
3631 case EM_CYGNUS_D30V:
3632 case EM_D30V: return "d30v";
3633 case EM_CYGNUS_V850:
3634 case EM_V850: return "Renesas V850";
3635 case EM_CYGNUS_M32R:
3636 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
3637 case EM_CYGNUS_MN10300:
3638 case EM_MN10300: return "mn10300";
3639 /* 90 */
3640 case EM_CYGNUS_MN10200:
3641 case EM_MN10200: return "mn10200";
3642 case EM_PJ: return "picoJava";
3643 case EM_OR1K: return "OpenRISC 1000";
3644 case EM_ARC_COMPACT: return "ARCompact";
3645 case EM_XTENSA_OLD:
3646 case EM_XTENSA: return "Tensilica Xtensa Processor";
3647 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
3648 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
3649 case EM_NS32K: return "National Semiconductor 32000 series";
3650 case EM_TPC: return "Tenor Network TPC processor";
3651 case EM_SNP1K: return "Trebia SNP 1000 processor";
3652 /* 100 */
3653 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
3654 case EM_IP2K_OLD:
3655 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
3656 case EM_MAX: return "MAX Processor";
3657 case EM_CR: return "National Semiconductor CompactRISC";
3658 case EM_F2MC16: return "Fujitsu F2MC16";
3659 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
3660 case EM_BLACKFIN: return "Analog Devices Blackfin";
3661 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
3662 case EM_SEP: return "Sharp embedded microprocessor";
3663 case EM_ARCA: return "Arca RISC microprocessor";
3664 /* 110 */
3665 case EM_UNICORE: return "Unicore";
3666 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
3667 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
3668 case EM_ALTERA_NIOS2: return "Altera Nios II";
3669 case EM_CRX: return "National Semiconductor CRX microprocessor";
3670 case EM_XGATE: return "Motorola XGATE embedded processor";
3671 case EM_C166:
3672 case EM_XC16X: return "Infineon Technologies xc16x";
3673 case EM_M16C: return "Renesas M16C series microprocessors";
3674 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
3675 case EM_CE: return "Freescale Communication Engine RISC core";
3676 /* 120 */
3677 case EM_M32C: return "Renesas M32c";
3678 /* 130 */
3679 case EM_TSK3000: return "Altium TSK3000 core";
3680 case EM_RS08: return "Freescale RS08 embedded processor";
3681 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
3682 case EM_SCORE: return "SUNPLUS S+Core";
3683 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
3684 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
3685 case EM_LATTICEMICO32: return "Lattice Mico32";
3686 case EM_SE_C17: return "Seiko Epson C17 family";
3687 /* 140 */
3688 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
3689 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
3690 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
3691 case EM_TI_PRU: return "TI PRU I/O processor";
3692 /* 160 */
3693 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
3694 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
3695 case EM_R32C: return "Renesas R32C series microprocessors";
3696 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
3697 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
3698 case EM_8051: return "Intel 8051 and variants";
3699 case EM_STXP7X: return "STMicroelectronics STxP7x family";
3700 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
3701 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
3702 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
3703 /* 170 */
3704 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
3705 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
3706 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
3707 case EM_RX: return "Renesas RX";
3708 case EM_METAG: return "Imagination Technologies Meta processor architecture";
3709 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
3710 case EM_ECOG16: return "Cyan Technology eCOG16 family";
3711 case EM_CR16:
3712 case EM_MICROBLAZE:
3713 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
3714 case EM_ETPU: return "Freescale Extended Time Processing Unit";
3715 case EM_SLE9X: return "Infineon Technologies SLE9X core";
3716 /* 180 */
3717 case EM_L1OM: return "Intel L1OM";
3718 case EM_K1OM: return "Intel K1OM";
3719 case EM_INTEL182: return "Intel (reserved)";
3720 case EM_AARCH64: return "AArch64";
3721 case EM_ARM184: return "ARM (reserved)";
3722 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
3723 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
3724 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
3725 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
3726 /* 190 */
3727 case EM_CUDA: return "NVIDIA CUDA architecture";
3728 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
3729 case EM_CLOUDSHIELD: return "CloudShield architecture family";
3730 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
3731 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
3732 case EM_ARC_COMPACT2: return "ARCv2";
3733 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
3734 case EM_RL78: return "Renesas RL78";
3735 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
3736 case EM_78K0R: return "Renesas 78K0R";
3737 /* 200 */
3738 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
3739 case EM_BA1: return "Beyond BA1 CPU architecture";
3740 case EM_BA2: return "Beyond BA2 CPU architecture";
3741 case EM_XCORE: return "XMOS xCORE processor family";
3742 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
3743 case EM_INTELGT: return "Intel Graphics Technology";
3744 /* 210 */
3745 case EM_KM32: return "KM211 KM32 32-bit processor";
3746 case EM_KMX32: return "KM211 KMX32 32-bit processor";
3747 case EM_KMX16: return "KM211 KMX16 16-bit processor";
3748 case EM_KMX8: return "KM211 KMX8 8-bit processor";
3749 case EM_KVARC: return "KM211 KVARC processor";
3750 case EM_CDP: return "Paneve CDP architecture family";
3751 case EM_COGE: return "Cognitive Smart Memory Processor";
3752 case EM_COOL: return "Bluechip Systems CoolEngine";
3753 case EM_NORC: return "Nanoradio Optimized RISC";
3754 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
3755 /* 220 */
3756 case EM_Z80: return "Zilog Z80";
3757 case EM_VISIUM: return "CDS VISIUMcore processor";
3758 case EM_FT32: return "FTDI Chip FT32";
3759 case EM_MOXIE: return "Moxie";
3760 case EM_AMDGPU: return "AMD GPU";
3761 /* 230 (all reserved) */
3762 /* 240 */
3763 case EM_RISCV: return "RISC-V";
3764 case EM_LANAI: return "Lanai 32-bit processor";
3765 case EM_CEVA: return "CEVA Processor Architecture Family";
3766 case EM_CEVA_X2: return "CEVA X2 Processor Family";
3767 case EM_BPF: return "Linux BPF";
3768 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
3769 case EM_IMG1: return "Imagination Technologies";
3770 /* 250 */
3771 case EM_NFP: return "Netronome Flow Processor";
3772 case EM_VE: return "NEC Vector Engine";
3773 case EM_CSKY: return "C-SKY";
3774 case EM_ARC_COMPACT3_64: return "Synopsys ARCv3 64-bit processor";
3775 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
3776 case EM_ARC_COMPACT3: return "Synopsys ARCv3 32-bit processor";
3777 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
3778 case EM_65816: return "WDC 65816/65C816";
3779 case EM_LOONGARCH: return "LoongArch";
3780 case EM_KF32: return "ChipON KungFu32";
3781
3782 /* Large numbers... */
3783 case EM_MT: return "Morpho Techologies MT processor";
3784 case EM_ALPHA: return "Alpha";
3785 case EM_WEBASSEMBLY: return "Web Assembly";
3786 case EM_DLX: return "OpenDLX";
3787 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
3788 case EM_IQ2000: return "Vitesse IQ2000";
3789 case EM_M32C_OLD:
3790 case EM_NIOS32: return "Altera Nios";
3791 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
3792 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
3793 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
3794 case EM_S12Z: return "Freescale S12Z";
3795
3796 default:
3797 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
3798 return buff;
3799 }
3800}
3801
3802static char *
3803decode_ARC_machine_flags (char *out, unsigned e_flags, unsigned e_machine)
3804{
3805 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
3806 other compilers don't specify an architecture type in the e_flags, and
3807 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
3808 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
3809 architectures.
3810
3811 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
3812 but also sets a specific architecture type in the e_flags field.
3813
3814 However, when decoding the flags we don't worry if we see an
3815 unexpected pairing, for example EM_ARC_COMPACT machine type, with
3816 ARCEM architecture type. */
3817
3818 switch (e_flags & EF_ARC_MACH_MSK)
3819 {
3820 /* We only expect these to occur for EM_ARC_COMPACT2. */
3821 case EF_ARC_CPU_ARCV2EM:
3822 out = stpcpy (out, ", ARC EM");
3823 break;
3824 case EF_ARC_CPU_ARCV2HS:
3825 out = stpcpy (out, ", ARC HS");
3826 break;
3827
3828 /* We only expect these to occur for EM_ARC_COMPACT. */
3829 case E_ARC_MACH_ARC600:
3830 out = stpcpy (out, ", ARC600");
3831 break;
3832 case E_ARC_MACH_ARC601:
3833 out = stpcpy (out, ", ARC601");
3834 break;
3835 case E_ARC_MACH_ARC700:
3836 out = stpcpy (out, ", ARC700");
3837 break;
3838
3839 /* The only times we should end up here are (a) A corrupt ELF, (b) A
3840 new ELF with new architecture being read by an old version of
3841 readelf, or (c) An ELF built with non-GNU compiler that does not
3842 set the architecture in the e_flags. */
3843 default:
3844 if (e_machine == EM_ARC_COMPACT)
3845 out = stpcpy (out, ", Unknown ARCompact");
3846 else
3847 out = stpcpy (out, ", Unknown ARC");
3848 break;
3849 }
3850
3851 switch (e_flags & EF_ARC_OSABI_MSK)
3852 {
3853 case E_ARC_OSABI_ORIG:
3854 out = stpcpy (out, ", (ABI:legacy)");
3855 break;
3856 case E_ARC_OSABI_V2:
3857 out = stpcpy (out, ", (ABI:v2)");
3858 break;
3859 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
3860 case E_ARC_OSABI_V3:
3861 out = stpcpy (out, ", v3 no-legacy-syscalls ABI");
3862 break;
3863 case E_ARC_OSABI_V4:
3864 out = stpcpy (out, ", v4 ABI");
3865 break;
3866 default:
3867 out = stpcpy (out, ", unrecognised ARC OSABI flag");
3868 break;
3869 }
3870 return out;
3871}
3872
3873static char *
3874decode_ARM_machine_flags (char *out, unsigned e_flags)
3875{
3876 unsigned eabi;
3877 bool unknown_abi = false;
3878
3879 eabi = EF_ARM_EABI_VERSION (e_flags);
3880 e_flags &= ~ EF_ARM_EABIMASK;
3881
3882 /* Handle "generic" ARM flags. */
3883 if (e_flags & EF_ARM_RELEXEC)
3884 {
3885 out = stpcpy (out, ", relocatable executable");
3886 e_flags &= ~ EF_ARM_RELEXEC;
3887 }
3888
3889 if (e_flags & EF_ARM_PIC)
3890 {
3891 out = stpcpy (out, ", position independent");
3892 e_flags &= ~ EF_ARM_PIC;
3893 }
3894
3895 /* Now handle EABI specific flags. */
3896 switch (eabi)
3897 {
3898 default:
3899 out = stpcpy (out, ", <unrecognized EABI>");
3900 if (e_flags)
3901 unknown_abi = true;
3902 break;
3903
3904 case EF_ARM_EABI_VER1:
3905 out = stpcpy (out, ", Version1 EABI");
3906 while (e_flags)
3907 {
3908 unsigned flag;
3909
3910 /* Process flags one bit at a time. */
3911 flag = e_flags & - e_flags;
3912 e_flags &= ~ flag;
3913
3914 switch (flag)
3915 {
3916 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3917 out = stpcpy (out, ", sorted symbol tables");
3918 break;
3919
3920 default:
3921 unknown_abi = true;
3922 break;
3923 }
3924 }
3925 break;
3926
3927 case EF_ARM_EABI_VER2:
3928 out = stpcpy (out, ", Version2 EABI");
3929 while (e_flags)
3930 {
3931 unsigned flag;
3932
3933 /* Process flags one bit at a time. */
3934 flag = e_flags & - e_flags;
3935 e_flags &= ~ flag;
3936
3937 switch (flag)
3938 {
3939 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3940 out = stpcpy (out, ", sorted symbol tables");
3941 break;
3942
3943 case EF_ARM_DYNSYMSUSESEGIDX:
3944 out = stpcpy (out, ", dynamic symbols use segment index");
3945 break;
3946
3947 case EF_ARM_MAPSYMSFIRST:
3948 out = stpcpy (out, ", mapping symbols precede others");
3949 break;
3950
3951 default:
3952 unknown_abi = true;
3953 break;
3954 }
3955 }
3956 break;
3957
3958 case EF_ARM_EABI_VER3:
3959 out = stpcpy (out, ", Version3 EABI");
3960 break;
3961
3962 case EF_ARM_EABI_VER4:
3963 out = stpcpy (out, ", Version4 EABI");
3964 while (e_flags)
3965 {
3966 unsigned flag;
3967
3968 /* Process flags one bit at a time. */
3969 flag = e_flags & - e_flags;
3970 e_flags &= ~ flag;
3971
3972 switch (flag)
3973 {
3974 case EF_ARM_BE8:
3975 out = stpcpy (out, ", BE8");
3976 break;
3977
3978 case EF_ARM_LE8:
3979 out = stpcpy (out, ", LE8");
3980 break;
3981
3982 default:
3983 unknown_abi = true;
3984 break;
3985 }
3986 }
3987 break;
3988
3989 case EF_ARM_EABI_VER5:
3990 out = stpcpy (out, ", Version5 EABI");
3991 while (e_flags)
3992 {
3993 unsigned flag;
3994
3995 /* Process flags one bit at a time. */
3996 flag = e_flags & - e_flags;
3997 e_flags &= ~ flag;
3998
3999 switch (flag)
4000 {
4001 case EF_ARM_BE8:
4002 out = stpcpy (out, ", BE8");
4003 break;
4004
4005 case EF_ARM_LE8:
4006 out = stpcpy (out, ", LE8");
4007 break;
4008
4009 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
4010 out = stpcpy (out, ", soft-float ABI");
4011 break;
4012
4013 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
4014 out = stpcpy (out, ", hard-float ABI");
4015 break;
4016
4017 default:
4018 unknown_abi = true;
4019 break;
4020 }
4021 }
4022 break;
4023
4024 case EF_ARM_EABI_UNKNOWN:
4025 out = stpcpy (out, ", GNU EABI");
4026 while (e_flags)
4027 {
4028 unsigned flag;
4029
4030 /* Process flags one bit at a time. */
4031 flag = e_flags & - e_flags;
4032 e_flags &= ~ flag;
4033
4034 switch (flag)
4035 {
4036 case EF_ARM_INTERWORK:
4037 out = stpcpy (out, ", interworking enabled");
4038 break;
4039
4040 case EF_ARM_APCS_26:
4041 out = stpcpy (out, ", uses APCS/26");
4042 break;
4043
4044 case EF_ARM_APCS_FLOAT:
4045 out = stpcpy (out, ", uses APCS/float");
4046 break;
4047
4048 case EF_ARM_PIC:
4049 out = stpcpy (out, ", position independent");
4050 break;
4051
4052 case EF_ARM_ALIGN8:
4053 out = stpcpy (out, ", 8 bit structure alignment");
4054 break;
4055
4056 case EF_ARM_NEW_ABI:
4057 out = stpcpy (out, ", uses new ABI");
4058 break;
4059
4060 case EF_ARM_OLD_ABI:
4061 out = stpcpy (out, ", uses old ABI");
4062 break;
4063
4064 case EF_ARM_SOFT_FLOAT:
4065 out = stpcpy (out, ", software FP");
4066 break;
4067
4068 case EF_ARM_VFP_FLOAT:
4069 out = stpcpy (out, ", VFP");
4070 break;
4071
4072 default:
4073 unknown_abi = true;
4074 break;
4075 }
4076 }
4077 }
4078
4079 if (unknown_abi)
4080 out = stpcpy (out,_(", <unknown>"));
4081 return out;
4082}
4083
4084static char *
4085decode_AVR_machine_flags (char *out, unsigned e_flags)
4086{
4087 switch (e_flags & EF_AVR_MACH)
4088 {
4089 case E_AVR_MACH_AVR1:
4090 out = stpcpy (out, ", avr:1");
4091 break;
4092 case E_AVR_MACH_AVR2:
4093 out = stpcpy (out, ", avr:2");
4094 break;
4095 case E_AVR_MACH_AVR25:
4096 out = stpcpy (out, ", avr:25");
4097 break;
4098 case E_AVR_MACH_AVR3:
4099 out = stpcpy (out, ", avr:3");
4100 break;
4101 case E_AVR_MACH_AVR31:
4102 out = stpcpy (out, ", avr:31");
4103 break;
4104 case E_AVR_MACH_AVR35:
4105 out = stpcpy (out, ", avr:35");
4106 break;
4107 case E_AVR_MACH_AVR4:
4108 out = stpcpy (out, ", avr:4");
4109 break;
4110 case E_AVR_MACH_AVR5:
4111 out = stpcpy (out, ", avr:5");
4112 break;
4113 case E_AVR_MACH_AVR51:
4114 out = stpcpy (out, ", avr:51");
4115 break;
4116 case E_AVR_MACH_AVR6:
4117 out = stpcpy (out, ", avr:6");
4118 break;
4119 case E_AVR_MACH_AVRTINY:
4120 out = stpcpy (out, ", avr:100");
4121 break;
4122 case E_AVR_MACH_XMEGA1:
4123 out = stpcpy (out, ", avr:101");
4124 break;
4125 case E_AVR_MACH_XMEGA2:
4126 out = stpcpy (out, ", avr:102");
4127 break;
4128 case E_AVR_MACH_XMEGA3:
4129 out = stpcpy (out, ", avr:103");
4130 break;
4131 case E_AVR_MACH_XMEGA4:
4132 out = stpcpy (out, ", avr:104");
4133 break;
4134 case E_AVR_MACH_XMEGA5:
4135 out = stpcpy (out, ", avr:105");
4136 break;
4137 case E_AVR_MACH_XMEGA6:
4138 out = stpcpy (out, ", avr:106");
4139 break;
4140 case E_AVR_MACH_XMEGA7:
4141 out = stpcpy (out, ", avr:107");
4142 break;
4143 default:
4144 out = stpcpy (out, ", avr:<unknown>");
4145 break;
4146 }
4147
4148 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
4149 out = stpcpy (out, ", link-relax");
4150 return out;
4151}
4152
4153static char *
4154decode_BLACKFIN_machine_flags (char *out, unsigned e_flags)
4155{
4156 if (e_flags & EF_BFIN_PIC)
4157 out = stpcpy (out, ", PIC");
4158
4159 if (e_flags & EF_BFIN_FDPIC)
4160 out = stpcpy (out, ", FDPIC");
4161
4162 if (e_flags & EF_BFIN_CODE_IN_L1)
4163 out = stpcpy (out, ", code in L1");
4164
4165 if (e_flags & EF_BFIN_DATA_IN_L1)
4166 out = stpcpy (out, ", data in L1");
4167 return out;
4168}
4169
4170static char *
4171decode_FRV_machine_flags (char *out, unsigned e_flags)
4172{
4173 switch (e_flags & EF_FRV_CPU_MASK)
4174 {
4175 case EF_FRV_CPU_GENERIC:
4176 break;
4177
4178 default:
4179 out = stpcpy (out, ", fr???");
4180 break;
4181
4182 case EF_FRV_CPU_FR300:
4183 out = stpcpy (out, ", fr300");
4184 break;
4185
4186 case EF_FRV_CPU_FR400:
4187 out = stpcpy (out, ", fr400");
4188 break;
4189 case EF_FRV_CPU_FR405:
4190 out = stpcpy (out, ", fr405");
4191 break;
4192
4193 case EF_FRV_CPU_FR450:
4194 out = stpcpy (out, ", fr450");
4195 break;
4196
4197 case EF_FRV_CPU_FR500:
4198 out = stpcpy (out, ", fr500");
4199 break;
4200 case EF_FRV_CPU_FR550:
4201 out = stpcpy (out, ", fr550");
4202 break;
4203
4204 case EF_FRV_CPU_SIMPLE:
4205 out = stpcpy (out, ", simple");
4206 break;
4207 case EF_FRV_CPU_TOMCAT:
4208 out = stpcpy (out, ", tomcat");
4209 break;
4210 }
4211 return out;
4212}
4213
4214static char *
4215decode_IA64_machine_flags (char *out, unsigned e_flags, Filedata *filedata)
4216{
4217 if ((e_flags & EF_IA_64_ABI64))
4218 out = stpcpy (out, ", 64-bit");
4219 else
4220 out = stpcpy (out, ", 32-bit");
4221 if ((e_flags & EF_IA_64_REDUCEDFP))
4222 out = stpcpy (out, ", reduced fp model");
4223 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
4224 out = stpcpy (out, ", no function descriptors, constant gp");
4225 else if ((e_flags & EF_IA_64_CONS_GP))
4226 out = stpcpy (out, ", constant gp");
4227 if ((e_flags & EF_IA_64_ABSOLUTE))
4228 out = stpcpy (out, ", absolute");
4229 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
4230 {
4231 if ((e_flags & EF_IA_64_VMS_LINKAGES))
4232 out = stpcpy (out, ", vms_linkages");
4233 switch ((e_flags & EF_IA_64_VMS_COMCOD))
4234 {
4235 case EF_IA_64_VMS_COMCOD_SUCCESS:
4236 break;
4237 case EF_IA_64_VMS_COMCOD_WARNING:
4238 out = stpcpy (out, ", warning");
4239 break;
4240 case EF_IA_64_VMS_COMCOD_ERROR:
4241 out = stpcpy (out, ", error");
4242 break;
4243 case EF_IA_64_VMS_COMCOD_ABORT:
4244 out = stpcpy (out, ", abort");
4245 break;
4246 default:
4247 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
4248 e_flags & EF_IA_64_VMS_COMCOD);
4249 out = stpcpy (out, ", <unknown>");
4250 }
4251 }
4252 return out;
4253}
4254
4255static char *
4256decode_LOONGARCH_machine_flags (char *out, unsigned int e_flags)
4257{
4258 if (EF_LOONGARCH_IS_SOFT_FLOAT (e_flags))
4259 out = stpcpy (out, ", SOFT-FLOAT");
4260 else if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
4261 out = stpcpy (out, ", SINGLE-FLOAT");
4262 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
4263 out = stpcpy (out, ", DOUBLE-FLOAT");
4264
4265 if (EF_LOONGARCH_IS_OBJ_V0 (e_flags))
4266 out = stpcpy (out, ", OBJ-v0");
4267 else if (EF_LOONGARCH_IS_OBJ_V1 (e_flags))
4268 out = stpcpy (out, ", OBJ-v1");
4269 return out;
4270}
4271
4272static char *
4273decode_M68K_machine_flags (char *out, unsigned int e_flags)
4274{
4275 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
4276 out = stpcpy (out, ", m68000");
4277 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
4278 out = stpcpy (out, ", cpu32");
4279 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
4280 out = stpcpy (out, ", fido_a");
4281 else
4282 {
4283 char const *isa = _("unknown");
4284 char const *mac = _("unknown mac");
4285 char const *additional = NULL;
4286
4287 switch (e_flags & EF_M68K_CF_ISA_MASK)
4288 {
4289 case EF_M68K_CF_ISA_A_NODIV:
4290 isa = "A";
4291 additional = ", nodiv";
4292 break;
4293 case EF_M68K_CF_ISA_A:
4294 isa = "A";
4295 break;
4296 case EF_M68K_CF_ISA_A_PLUS:
4297 isa = "A+";
4298 break;
4299 case EF_M68K_CF_ISA_B_NOUSP:
4300 isa = "B";
4301 additional = ", nousp";
4302 break;
4303 case EF_M68K_CF_ISA_B:
4304 isa = "B";
4305 break;
4306 case EF_M68K_CF_ISA_C:
4307 isa = "C";
4308 break;
4309 case EF_M68K_CF_ISA_C_NODIV:
4310 isa = "C";
4311 additional = ", nodiv";
4312 break;
4313 }
4314 out = stpcpy (out, ", cf, isa ");
4315 out = stpcpy (out, isa);
4316 if (additional)
4317 out = stpcpy (out, additional);
4318 if (e_flags & EF_M68K_CF_FLOAT)
4319 out = stpcpy (out, ", float");
4320 switch (e_flags & EF_M68K_CF_MAC_MASK)
4321 {
4322 case 0:
4323 mac = NULL;
4324 break;
4325 case EF_M68K_CF_MAC:
4326 mac = "mac";
4327 break;
4328 case EF_M68K_CF_EMAC:
4329 mac = "emac";
4330 break;
4331 case EF_M68K_CF_EMAC_B:
4332 mac = "emac_b";
4333 break;
4334 }
4335 if (mac)
4336 {
4337 out = stpcpy (out, ", ");
4338 out = stpcpy (out, mac);
4339 }
4340 }
4341 return out;
4342}
4343
4344static char *
4345decode_MeP_machine_flags (char *out, unsigned int e_flags)
4346{
4347 switch (e_flags & EF_MEP_CPU_MASK)
4348 {
4349 case EF_MEP_CPU_MEP:
4350 out = stpcpy (out, ", generic MeP");
4351 break;
4352 case EF_MEP_CPU_C2:
4353 out = stpcpy (out, ", MeP C2");
4354 break;
4355 case EF_MEP_CPU_C3:
4356 out = stpcpy (out, ", MeP C3");
4357 break;
4358 case EF_MEP_CPU_C4:
4359 out = stpcpy (out, ", MeP C4");
4360 break;
4361 case EF_MEP_CPU_C5:
4362 out = stpcpy (out, ", MeP C5");
4363 break;
4364 case EF_MEP_CPU_H1:
4365 out = stpcpy (out, ", MeP H1");
4366 break;
4367 default:
4368 out = stpcpy (out, _(", <unknown MeP cpu type>"));
4369 break;
4370 }
4371
4372 switch (e_flags & EF_MEP_COP_MASK)
4373 {
4374 case EF_MEP_COP_NONE:
4375 break;
4376 case EF_MEP_COP_AVC:
4377 out = stpcpy (out, ", AVC coprocessor");
4378 break;
4379 case EF_MEP_COP_AVC2:
4380 out = stpcpy (out, ", AVC2 coprocessor");
4381 break;
4382 case EF_MEP_COP_FMAX:
4383 out = stpcpy (out, ", FMAX coprocessor");
4384 break;
4385 case EF_MEP_COP_IVC2:
4386 out = stpcpy (out, ", IVC2 coprocessor");
4387 break;
4388 default:
4389 out = stpcpy (out, _("<unknown MeP copro type>"));
4390 break;
4391 }
4392
4393 if (e_flags & EF_MEP_LIBRARY)
4394 out = stpcpy (out, ", Built for Library");
4395
4396 if (e_flags & EF_MEP_INDEX_MASK)
4397 out += sprintf (out, ", Configuration Index: %#x",
4398 e_flags & EF_MEP_INDEX_MASK);
4399
4400 if (e_flags & ~ EF_MEP_ALL_FLAGS)
4401 out += sprintf (out, _(", unknown flags bits: %#x"),
4402 e_flags & ~ EF_MEP_ALL_FLAGS);
4403 return out;
4404}
4405
4406static char *
4407decode_MIPS_machine_flags (char *out, unsigned int e_flags)
4408{
4409 if (e_flags & EF_MIPS_NOREORDER)
4410 out = stpcpy (out, ", noreorder");
4411
4412 if (e_flags & EF_MIPS_PIC)
4413 out = stpcpy (out, ", pic");
4414
4415 if (e_flags & EF_MIPS_CPIC)
4416 out = stpcpy (out, ", cpic");
4417
4418 if (e_flags & EF_MIPS_UCODE)
4419 out = stpcpy (out, ", ugen_reserved");
4420
4421 if (e_flags & EF_MIPS_ABI2)
4422 out = stpcpy (out, ", abi2");
4423
4424 if (e_flags & EF_MIPS_OPTIONS_FIRST)
4425 out = stpcpy (out, ", odk first");
4426
4427 if (e_flags & EF_MIPS_32BITMODE)
4428 out = stpcpy (out, ", 32bitmode");
4429
4430 if (e_flags & EF_MIPS_NAN2008)
4431 out = stpcpy (out, ", nan2008");
4432
4433 if (e_flags & EF_MIPS_FP64)
4434 out = stpcpy (out, ", fp64");
4435
4436 switch ((e_flags & EF_MIPS_MACH))
4437 {
4438 case EF_MIPS_MACH_3900:
4439 out = stpcpy (out, ", 3900");
4440 break;
4441 case EF_MIPS_MACH_4010:
4442 out = stpcpy (out, ", 4010");
4443 break;
4444 case EF_MIPS_MACH_4100:
4445 out = stpcpy (out, ", 4100");
4446 break;
4447 case EF_MIPS_MACH_4111:
4448 out = stpcpy (out, ", 4111");
4449 break;
4450 case EF_MIPS_MACH_4120:
4451 out = stpcpy (out, ", 4120");
4452 break;
4453 case EF_MIPS_MACH_4650:
4454 out = stpcpy (out, ", 4650");
4455 break;
4456 case EF_MIPS_MACH_5400:
4457 out = stpcpy (out, ", 5400");
4458 break;
4459 case EF_MIPS_MACH_5500:
4460 out = stpcpy (out, ", 5500");
4461 break;
4462 case EF_MIPS_MACH_5900:
4463 out = stpcpy (out, ", 5900");
4464 break;
4465 case EF_MIPS_MACH_SB1:
4466 out = stpcpy (out, ", sb1");
4467 break;
4468 case EF_MIPS_MACH_9000:
4469 out = stpcpy (out, ", 9000");
4470 break;
4471 case EF_MIPS_MACH_LS2E:
4472 out = stpcpy (out, ", loongson-2e");
4473 break;
4474 case EF_MIPS_MACH_LS2F:
4475 out = stpcpy (out, ", loongson-2f");
4476 break;
4477 case EF_MIPS_MACH_GS464:
4478 out = stpcpy (out, ", gs464");
4479 break;
4480 case EF_MIPS_MACH_GS464E:
4481 out = stpcpy (out, ", gs464e");
4482 break;
4483 case EF_MIPS_MACH_GS264E:
4484 out = stpcpy (out, ", gs264e");
4485 break;
4486 case EF_MIPS_MACH_OCTEON:
4487 out = stpcpy (out, ", octeon");
4488 break;
4489 case EF_MIPS_MACH_OCTEON2:
4490 out = stpcpy (out, ", octeon2");
4491 break;
4492 case EF_MIPS_MACH_OCTEON3:
4493 out = stpcpy (out, ", octeon3");
4494 break;
4495 case EF_MIPS_MACH_XLR:
4496 out = stpcpy (out, ", xlr");
4497 break;
4498 case EF_MIPS_MACH_IAMR2:
4499 out = stpcpy (out, ", interaptiv-mr2");
4500 break;
4501 case EF_MIPS_MACH_ALLEGREX:
4502 out = stpcpy (out, ", allegrex");
4503 break;
4504 case 0:
4505 /* We simply ignore the field in this case to avoid confusion:
4506 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
4507 extension. */
4508 break;
4509 default:
4510 out = stpcpy (out, _(", unknown CPU"));
4511 break;
4512 }
4513
4514 switch ((e_flags & EF_MIPS_ABI))
4515 {
4516 case EF_MIPS_ABI_O32:
4517 out = stpcpy (out, ", o32");
4518 break;
4519 case EF_MIPS_ABI_O64:
4520 out = stpcpy (out, ", o64");
4521 break;
4522 case EF_MIPS_ABI_EABI32:
4523 out = stpcpy (out, ", eabi32");
4524 break;
4525 case EF_MIPS_ABI_EABI64:
4526 out = stpcpy (out, ", eabi64");
4527 break;
4528 case 0:
4529 /* We simply ignore the field in this case to avoid confusion:
4530 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
4531 This means it is likely to be an o32 file, but not for
4532 sure. */
4533 break;
4534 default:
4535 out = stpcpy (out, _(", unknown ABI"));
4536 break;
4537 }
4538
4539 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
4540 out = stpcpy (out, ", mdmx");
4541
4542 if (e_flags & EF_MIPS_ARCH_ASE_M16)
4543 out = stpcpy (out, ", mips16");
4544
4545 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
4546 out = stpcpy (out, ", micromips");
4547
4548 switch ((e_flags & EF_MIPS_ARCH))
4549 {
4550 case EF_MIPS_ARCH_1:
4551 out = stpcpy (out, ", mips1");
4552 break;
4553 case EF_MIPS_ARCH_2:
4554 out = stpcpy (out, ", mips2");
4555 break;
4556 case EF_MIPS_ARCH_3:
4557 out = stpcpy (out, ", mips3");
4558 break;
4559 case EF_MIPS_ARCH_4:
4560 out = stpcpy (out, ", mips4");
4561 break;
4562 case EF_MIPS_ARCH_5:
4563 out = stpcpy (out, ", mips5");
4564 break;
4565 case EF_MIPS_ARCH_32:
4566 out = stpcpy (out, ", mips32");
4567 break;
4568 case EF_MIPS_ARCH_32R2:
4569 out = stpcpy (out, ", mips32r2");
4570 break;
4571 case EF_MIPS_ARCH_32R6:
4572 out = stpcpy (out, ", mips32r6");
4573 break;
4574 case EF_MIPS_ARCH_64:
4575 out = stpcpy (out, ", mips64");
4576 break;
4577 case EF_MIPS_ARCH_64R2:
4578 out = stpcpy (out, ", mips64r2");
4579 break;
4580 case EF_MIPS_ARCH_64R6:
4581 out = stpcpy (out, ", mips64r6");
4582 break;
4583 default:
4584 out = stpcpy (out, _(", unknown ISA"));
4585 break;
4586 }
4587 return out;
4588}
4589
4590static char *
4591decode_MSP430_machine_flags (char *out, unsigned e_flags)
4592{
4593 out = stpcpy (out, _(": architecture variant: "));
4594 switch (e_flags & EF_MSP430_MACH)
4595 {
4596 case E_MSP430_MACH_MSP430x11:
4597 out = stpcpy (out, "MSP430x11");
4598 break;
4599 case E_MSP430_MACH_MSP430x11x1:
4600 out = stpcpy (out, "MSP430x11x1 ");
4601 break;
4602 case E_MSP430_MACH_MSP430x12:
4603 out = stpcpy (out, "MSP430x12");
4604 break;
4605 case E_MSP430_MACH_MSP430x13:
4606 out = stpcpy (out, "MSP430x13");
4607 break;
4608 case E_MSP430_MACH_MSP430x14:
4609 out = stpcpy (out, "MSP430x14");
4610 break;
4611 case E_MSP430_MACH_MSP430x15:
4612 out = stpcpy (out, "MSP430x15");
4613 break;
4614 case E_MSP430_MACH_MSP430x16:
4615 out = stpcpy (out, "MSP430x16");
4616 break;
4617 case E_MSP430_MACH_MSP430x31:
4618 out = stpcpy (out, "MSP430x31");
4619 break;
4620 case E_MSP430_MACH_MSP430x32:
4621 out = stpcpy (out, "MSP430x32");
4622 break;
4623 case E_MSP430_MACH_MSP430x33:
4624 out = stpcpy (out, "MSP430x33");
4625 break;
4626 case E_MSP430_MACH_MSP430x41:
4627 out = stpcpy (out, "MSP430x41");
4628 break;
4629 case E_MSP430_MACH_MSP430x42:
4630 out = stpcpy (out, "MSP430x42");
4631 break;
4632 case E_MSP430_MACH_MSP430x43:
4633 out = stpcpy (out, "MSP430x43");
4634 break;
4635 case E_MSP430_MACH_MSP430x44:
4636 out = stpcpy (out, "MSP430x44");
4637 break;
4638 case E_MSP430_MACH_MSP430X :
4639 out = stpcpy (out, "MSP430X");
4640 break;
4641 default:
4642 out = stpcpy (out, _(": unknown"));
4643 break;
4644 }
4645
4646 if (e_flags & ~ EF_MSP430_MACH)
4647 out = stpcpy (out, _(": unknown extra flag bits also present"));
4648 return out;
4649}
4650
4651static char *
4652decode_NDS32_machine_flags (char *out, unsigned e_flags)
4653{
4654 unsigned abi;
4655 unsigned arch;
4656 unsigned config;
4657 unsigned version;
4658 bool has_fpu = false;
4659
4660 static const char *ABI_STRINGS[] =
4661 {
4662 "ABI v0", /* use r5 as return register; only used in N1213HC */
4663 "ABI v1", /* use r0 as return register */
4664 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
4665 "ABI v2fp", /* for FPU */
4666 "AABI",
4667 "ABI2 FP+"
4668 };
4669 static const char *VER_STRINGS[] =
4670 {
4671 "Andes ELF V1.3 or older",
4672 "Andes ELF V1.3.1",
4673 "Andes ELF V1.4"
4674 };
4675 static const char *ARCH_STRINGS[] =
4676 {
4677 "",
4678 "Andes Star v1.0",
4679 "Andes Star v2.0",
4680 "Andes Star v3.0",
4681 "Andes Star v3.0m"
4682 };
4683
4684 abi = EF_NDS_ABI & e_flags;
4685 arch = EF_NDS_ARCH & e_flags;
4686 config = EF_NDS_INST & e_flags;
4687 version = EF_NDS32_ELF_VERSION & e_flags;
4688
4689 switch (abi)
4690 {
4691 case E_NDS_ABI_V0:
4692 case E_NDS_ABI_V1:
4693 case E_NDS_ABI_V2:
4694 case E_NDS_ABI_V2FP:
4695 case E_NDS_ABI_AABI:
4696 case E_NDS_ABI_V2FP_PLUS:
4697 /* In case there are holes in the array. */
4698 out += sprintf (out, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
4699 break;
4700
4701 default:
4702 out = stpcpy (out, ", <unrecognized ABI>");
4703 break;
4704 }
4705
4706 switch (version)
4707 {
4708 case E_NDS32_ELF_VER_1_2:
4709 case E_NDS32_ELF_VER_1_3:
4710 case E_NDS32_ELF_VER_1_4:
4711 out += sprintf (out, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
4712 break;
4713
4714 default:
4715 out = stpcpy (out, ", <unrecognized ELF version number>");
4716 break;
4717 }
4718
4719 if (E_NDS_ABI_V0 == abi)
4720 {
4721 /* OLD ABI; only used in N1213HC, has performance extension 1. */
4722 out = stpcpy (out, ", Andes Star v1.0, N1213HC, MAC, PERF1");
4723 if (arch == E_NDS_ARCH_STAR_V1_0)
4724 out = stpcpy (out, ", 16b"); /* has 16-bit instructions */
4725 return out;
4726 }
4727
4728 switch (arch)
4729 {
4730 case E_NDS_ARCH_STAR_V1_0:
4731 case E_NDS_ARCH_STAR_V2_0:
4732 case E_NDS_ARCH_STAR_V3_0:
4733 case E_NDS_ARCH_STAR_V3_M:
4734 out += sprintf (out, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
4735 break;
4736
4737 default:
4738 out = stpcpy (out, ", <unrecognized architecture>");
4739 /* ARCH version determines how the e_flags are interpreted.
4740 If it is unknown, we cannot proceed. */
4741 return out;
4742 }
4743
4744 /* Newer ABI; Now handle architecture specific flags. */
4745 if (arch == E_NDS_ARCH_STAR_V1_0)
4746 {
4747 if (config & E_NDS32_HAS_MFUSR_PC_INST)
4748 out = stpcpy (out, ", MFUSR_PC");
4749
4750 if (!(config & E_NDS32_HAS_NO_MAC_INST))
4751 out = stpcpy (out, ", MAC");
4752
4753 if (config & E_NDS32_HAS_DIV_INST)
4754 out = stpcpy (out, ", DIV");
4755
4756 if (config & E_NDS32_HAS_16BIT_INST)
4757 out = stpcpy (out, ", 16b");
4758 }
4759 else
4760 {
4761 if (config & E_NDS32_HAS_MFUSR_PC_INST)
4762 {
4763 if (version <= E_NDS32_ELF_VER_1_3)
4764 out = stpcpy (out, ", [B8]");
4765 else
4766 out = stpcpy (out, ", EX9");
4767 }
4768
4769 if (config & E_NDS32_HAS_MAC_DX_INST)
4770 out = stpcpy (out, ", MAC_DX");
4771
4772 if (config & E_NDS32_HAS_DIV_DX_INST)
4773 out = stpcpy (out, ", DIV_DX");
4774
4775 if (config & E_NDS32_HAS_16BIT_INST)
4776 {
4777 if (version <= E_NDS32_ELF_VER_1_3)
4778 out = stpcpy (out, ", 16b");
4779 else
4780 out = stpcpy (out, ", IFC");
4781 }
4782 }
4783
4784 if (config & E_NDS32_HAS_EXT_INST)
4785 out = stpcpy (out, ", PERF1");
4786
4787 if (config & E_NDS32_HAS_EXT2_INST)
4788 out = stpcpy (out, ", PERF2");
4789
4790 if (config & E_NDS32_HAS_FPU_INST)
4791 {
4792 has_fpu = true;
4793 out = stpcpy (out, ", FPU_SP");
4794 }
4795
4796 if (config & E_NDS32_HAS_FPU_DP_INST)
4797 {
4798 has_fpu = true;
4799 out = stpcpy (out, ", FPU_DP");
4800 }
4801
4802 if (config & E_NDS32_HAS_FPU_MAC_INST)
4803 {
4804 has_fpu = true;
4805 out = stpcpy (out, ", FPU_MAC");
4806 }
4807
4808 if (has_fpu)
4809 {
4810 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
4811 {
4812 case E_NDS32_FPU_REG_8SP_4DP:
4813 out = stpcpy (out, ", FPU_REG:8/4");
4814 break;
4815 case E_NDS32_FPU_REG_16SP_8DP:
4816 out = stpcpy (out, ", FPU_REG:16/8");
4817 break;
4818 case E_NDS32_FPU_REG_32SP_16DP:
4819 out = stpcpy (out, ", FPU_REG:32/16");
4820 break;
4821 case E_NDS32_FPU_REG_32SP_32DP:
4822 out = stpcpy (out, ", FPU_REG:32/32");
4823 break;
4824 }
4825 }
4826
4827 if (config & E_NDS32_HAS_AUDIO_INST)
4828 out = stpcpy (out, ", AUDIO");
4829
4830 if (config & E_NDS32_HAS_STRING_INST)
4831 out = stpcpy (out, ", STR");
4832
4833 if (config & E_NDS32_HAS_REDUCED_REGS)
4834 out = stpcpy (out, ", 16REG");
4835
4836 if (config & E_NDS32_HAS_VIDEO_INST)
4837 {
4838 if (version <= E_NDS32_ELF_VER_1_3)
4839 out = stpcpy (out, ", VIDEO");
4840 else
4841 out = stpcpy (out, ", SATURATION");
4842 }
4843
4844 if (config & E_NDS32_HAS_ENCRIPT_INST)
4845 out = stpcpy (out, ", ENCRP");
4846
4847 if (config & E_NDS32_HAS_L2C_INST)
4848 out = stpcpy (out, ", L2C");
4849
4850 return out;
4851}
4852
4853static char *
4854decode_PARISC_machine_flags (char *out, unsigned e_flags)
4855{
4856 switch (e_flags & EF_PARISC_ARCH)
4857 {
4858 case EFA_PARISC_1_0:
4859 out = stpcpy (out, ", PA-RISC 1.0");
4860 break;
4861 case EFA_PARISC_1_1:
4862 out = stpcpy (out, ", PA-RISC 1.1");
4863 break;
4864 case EFA_PARISC_2_0:
4865 out = stpcpy (out, ", PA-RISC 2.0");
4866 break;
4867 default:
4868 break;
4869 }
4870 if (e_flags & EF_PARISC_TRAPNIL)
4871 out = stpcpy (out, ", trapnil");
4872 if (e_flags & EF_PARISC_EXT)
4873 out = stpcpy (out, ", ext");
4874 if (e_flags & EF_PARISC_LSB)
4875 out = stpcpy (out, ", lsb");
4876 if (e_flags & EF_PARISC_WIDE)
4877 out = stpcpy (out, ", wide");
4878 if (e_flags & EF_PARISC_NO_KABP)
4879 out = stpcpy (out, ", no kabp");
4880 if (e_flags & EF_PARISC_LAZYSWAP)
4881 out = stpcpy (out, ", lazyswap");
4882 return out;
4883}
4884
4885static char *
4886decode_RISCV_machine_flags (char *out, unsigned e_flags)
4887{
4888 if (e_flags & EF_RISCV_RVC)
4889 out = stpcpy (out, ", RVC");
4890
4891 if (e_flags & EF_RISCV_RVE)
4892 out = stpcpy (out, ", RVE");
4893
4894 if (e_flags & EF_RISCV_TSO)
4895 out = stpcpy (out, ", TSO");
4896
4897 switch (e_flags & EF_RISCV_FLOAT_ABI)
4898 {
4899 case EF_RISCV_FLOAT_ABI_SOFT:
4900 out = stpcpy (out, ", soft-float ABI");
4901 break;
4902
4903 case EF_RISCV_FLOAT_ABI_SINGLE:
4904 out = stpcpy (out, ", single-float ABI");
4905 break;
4906
4907 case EF_RISCV_FLOAT_ABI_DOUBLE:
4908 out = stpcpy (out, ", double-float ABI");
4909 break;
4910
4911 case EF_RISCV_FLOAT_ABI_QUAD:
4912 out = stpcpy (out, ", quad-float ABI");
4913 break;
4914 }
4915 return out;
4916}
4917
4918static char *
4919decode_RL78_machine_flags (char *out, unsigned e_flags)
4920{
4921 switch (e_flags & E_FLAG_RL78_CPU_MASK)
4922 {
4923 case E_FLAG_RL78_ANY_CPU:
4924 break;
4925 case E_FLAG_RL78_G10:
4926 out = stpcpy (out, ", G10");
4927 break;
4928 case E_FLAG_RL78_G13:
4929 out = stpcpy (out, ", G13");
4930 break;
4931 case E_FLAG_RL78_G14:
4932 out = stpcpy (out, ", G14");
4933 break;
4934 }
4935 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
4936 out = stpcpy (out, ", 64-bit doubles");
4937 return out;
4938}
4939
4940static char *
4941decode_RX_machine_flags (char *out, unsigned e_flags)
4942{
4943 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
4944 out = stpcpy (out, ", 64-bit doubles");
4945 if (e_flags & E_FLAG_RX_DSP)
4946 out = stpcpy (out, ", dsp");
4947 if (e_flags & E_FLAG_RX_PID)
4948 out = stpcpy (out, ", pid");
4949 if (e_flags & E_FLAG_RX_ABI)
4950 out = stpcpy (out, ", RX ABI");
4951 if (e_flags & E_FLAG_RX_SINSNS_SET)
4952 out = stpcpy (out, (e_flags & E_FLAG_RX_SINSNS_YES
4953 ? ", uses String instructions"
4954 : ", bans String instructions"));
4955 if (e_flags & E_FLAG_RX_V2)
4956 out = stpcpy (out, ", V2");
4957 if (e_flags & E_FLAG_RX_V3)
4958 out = stpcpy (out, ", V3");
4959 return out;
4960}
4961
4962static char *
4963decode_SH_machine_flags (char *out, unsigned e_flags)
4964{
4965 switch ((e_flags & EF_SH_MACH_MASK))
4966 {
4967 case EF_SH1:
4968 out = stpcpy (out, ", sh1");
4969 break;
4970 case EF_SH2:
4971 out = stpcpy (out, ", sh2");
4972 break;
4973 case EF_SH3:
4974 out = stpcpy (out, ", sh3");
4975 break;
4976 case EF_SH_DSP:
4977 out = stpcpy (out, ", sh-dsp");
4978 break;
4979 case EF_SH3_DSP:
4980 out = stpcpy (out, ", sh3-dsp");
4981 break;
4982 case EF_SH4AL_DSP:
4983 out = stpcpy (out, ", sh4al-dsp");
4984 break;
4985 case EF_SH3E:
4986 out = stpcpy (out, ", sh3e");
4987 break;
4988 case EF_SH4:
4989 out = stpcpy (out, ", sh4");
4990 break;
4991 case EF_SH5:
4992 out = stpcpy (out, ", sh5");
4993 break;
4994 case EF_SH2E:
4995 out = stpcpy (out, ", sh2e");
4996 break;
4997 case EF_SH4A:
4998 out = stpcpy (out, ", sh4a");
4999 break;
5000 case EF_SH2A:
5001 out = stpcpy (out, ", sh2a");
5002 break;
5003 case EF_SH4_NOFPU:
5004 out = stpcpy (out, ", sh4-nofpu");
5005 break;
5006 case EF_SH4A_NOFPU:
5007 out = stpcpy (out, ", sh4a-nofpu");
5008 break;
5009 case EF_SH2A_NOFPU:
5010 out = stpcpy (out, ", sh2a-nofpu");
5011 break;
5012 case EF_SH3_NOMMU:
5013 out = stpcpy (out, ", sh3-nommu");
5014 break;
5015 case EF_SH4_NOMMU_NOFPU:
5016 out = stpcpy (out, ", sh4-nommu-nofpu");
5017 break;
5018 case EF_SH2A_SH4_NOFPU:
5019 out = stpcpy (out, ", sh2a-nofpu-or-sh4-nommu-nofpu");
5020 break;
5021 case EF_SH2A_SH3_NOFPU:
5022 out = stpcpy (out, ", sh2a-nofpu-or-sh3-nommu");
5023 break;
5024 case EF_SH2A_SH4:
5025 out = stpcpy (out, ", sh2a-or-sh4");
5026 break;
5027 case EF_SH2A_SH3E:
5028 out = stpcpy (out, ", sh2a-or-sh3e");
5029 break;
5030 default:
5031 out = stpcpy (out, _(", unknown ISA"));
5032 break;
5033 }
5034
5035 if (e_flags & EF_SH_PIC)
5036 out = stpcpy (out, ", pic");
5037
5038 if (e_flags & EF_SH_FDPIC)
5039 out = stpcpy (out, ", fdpic");
5040 return out;
5041}
5042
5043static char *
5044decode_SPARC_machine_flags (char *out, unsigned e_flags)
5045{
5046 if (e_flags & EF_SPARC_32PLUS)
5047 out = stpcpy (out, ", v8+");
5048
5049 if (e_flags & EF_SPARC_SUN_US1)
5050 out = stpcpy (out, ", ultrasparcI");
5051
5052 if (e_flags & EF_SPARC_SUN_US3)
5053 out = stpcpy (out, ", ultrasparcIII");
5054
5055 if (e_flags & EF_SPARC_HAL_R1)
5056 out = stpcpy (out, ", halr1");
5057
5058 if (e_flags & EF_SPARC_LEDATA)
5059 out = stpcpy (out, ", ledata");
5060
5061 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
5062 out = stpcpy (out, ", tso");
5063
5064 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
5065 out = stpcpy (out, ", pso");
5066
5067 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
5068 out = stpcpy (out, ", rmo");
5069 return out;
5070}
5071
5072static char *
5073decode_V800_machine_flags (char *out, unsigned int e_flags)
5074{
5075 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
5076 out = stpcpy (out, ", RH850 ABI");
5077
5078 if (e_flags & EF_V800_850E3)
5079 out = stpcpy (out, ", V3 architecture");
5080
5081 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
5082 out = stpcpy (out, ", FPU not used");
5083
5084 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
5085 out = stpcpy (out, ", regmode: COMMON");
5086
5087 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
5088 out = stpcpy (out, ", r4 not used");
5089
5090 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
5091 out = stpcpy (out, ", r30 not used");
5092
5093 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
5094 out = stpcpy (out, ", r5 not used");
5095
5096 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
5097 out = stpcpy (out, ", r2 not used");
5098
5099 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
5100 {
5101 switch (e_flags & - e_flags)
5102 {
5103 case EF_RH850_FPU_DOUBLE:
5104 out = stpcpy (out, ", double precision FPU");
5105 break;
5106 case EF_RH850_FPU_SINGLE:
5107 out = stpcpy (out, ", single precision FPU");
5108 break;
5109 case EF_RH850_REGMODE22:
5110 out = stpcpy (out, ", regmode:22");
5111 break;
5112 case EF_RH850_REGMODE32:
5113 out = stpcpy (out, ", regmode:23");
5114 break;
5115 case EF_RH850_GP_FIX:
5116 out = stpcpy (out, ", r4 fixed");
5117 break;
5118 case EF_RH850_GP_NOFIX:
5119 out = stpcpy (out, ", r4 free");
5120 break;
5121 case EF_RH850_EP_FIX:
5122 out = stpcpy (out, ", r30 fixed");
5123 break;
5124 case EF_RH850_EP_NOFIX:
5125 out = stpcpy (out, ", r30 free");
5126 break;
5127 case EF_RH850_TP_FIX:
5128 out = stpcpy (out, ", r5 fixed");
5129 break;
5130 case EF_RH850_TP_NOFIX:
5131 out = stpcpy (out, ", r5 free");
5132 break;
5133 case EF_RH850_REG2_RESERVE:
5134 out = stpcpy (out, ", r2 fixed");
5135 break;
5136 case EF_RH850_REG2_NORESERVE:
5137 out = stpcpy (out, ", r2 free");
5138 break;
5139 default:
5140 break;
5141 }
5142 }
5143 return out;
5144}
5145
5146static char *
5147decode_V850_machine_flags (char *out, unsigned int e_flags)
5148{
5149 switch (e_flags & EF_V850_ARCH)
5150 {
5151 case E_V850E3V5_ARCH:
5152 out = stpcpy (out, ", v850e3v5");
5153 break;
5154 case E_V850E2V3_ARCH:
5155 out = stpcpy (out, ", v850e2v3");
5156 break;
5157 case E_V850E2_ARCH:
5158 out = stpcpy (out, ", v850e2");
5159 break;
5160 case E_V850E1_ARCH:
5161 out = stpcpy (out, ", v850e1");
5162 break;
5163 case E_V850E_ARCH:
5164 out = stpcpy (out, ", v850e");
5165 break;
5166 case E_V850_ARCH:
5167 out = stpcpy (out, ", v850");
5168 break;
5169 default:
5170 out = stpcpy (out, _(", unknown v850 architecture variant"));
5171 break;
5172 }
5173 return out;
5174}
5175
5176static char *
5177decode_Z80_machine_flags (char *out, unsigned int e_flags)
5178{
5179 switch (e_flags & EF_Z80_MACH_MSK)
5180 {
5181 case EF_Z80_MACH_Z80:
5182 out = stpcpy (out, ", Z80");
5183 break;
5184 case EF_Z80_MACH_Z180:
5185 out = stpcpy (out, ", Z180");
5186 break;
5187 case EF_Z80_MACH_R800:
5188 out = stpcpy (out, ", R800");
5189 break;
5190 case EF_Z80_MACH_EZ80_Z80:
5191 out = stpcpy (out, ", EZ80");
5192 break;
5193 case EF_Z80_MACH_EZ80_ADL:
5194 out = stpcpy (out, ", EZ80, ADL");
5195 break;
5196 case EF_Z80_MACH_GBZ80:
5197 out = stpcpy (out, ", GBZ80");
5198 break;
5199 case EF_Z80_MACH_Z80N:
5200 out = stpcpy (out, ", Z80N");
5201 break;
5202 default:
5203 out = stpcpy (out, _(", unknown"));
5204 break;
5205 }
5206 return out;
5207}
5208
5209static char *
5210decode_AMDGPU_machine_flags (char *out, unsigned int e_flags, Filedata *filedata)
5211{
5212 unsigned char *e_ident = filedata->file_header.e_ident;
5213 unsigned char osabi = e_ident[EI_OSABI];
5214 unsigned char abiversion = e_ident[EI_ABIVERSION];
5215 unsigned int mach;
5216
5217 /* HSA OS ABI v2 used a different encoding, but we don't need to support it,
5218 it has been deprecated for a while.
5219
5220 The PAL, MESA3D and NONE OS ABIs are not properly versioned, at the time
5221 of writing, they use the same flags as HSA v3, so the code below uses that
5222 assumption. */
5223 if (osabi == ELFOSABI_AMDGPU_HSA && abiversion < ELFABIVERSION_AMDGPU_HSA_V3)
5224 return out;
5225
5226 mach = e_flags & EF_AMDGPU_MACH;
5227 switch (mach)
5228 {
5229#define AMDGPU_CASE(code, string) \
5230 case code: out = stpcpy (out, ", " string); break;
5231 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX600, "gfx600")
5232 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX601, "gfx601")
5233 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX700, "gfx700")
5234 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX701, "gfx701")
5235 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX702, "gfx702")
5236 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX703, "gfx703")
5237 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX704, "gfx704")
5238 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX801, "gfx801")
5239 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX802, "gfx802")
5240 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX803, "gfx803")
5241 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX810, "gfx810")
5242 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX900, "gfx900")
5243 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX902, "gfx902")
5244 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX904, "gfx904")
5245 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX906, "gfx906")
5246 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX908, "gfx908")
5247 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX909, "gfx909")
5248 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90C, "gfx90c")
5249 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1010, "gfx1010")
5250 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1011, "gfx1011")
5251 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1012, "gfx1012")
5252 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1030, "gfx1030")
5253 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1031, "gfx1031")
5254 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1032, "gfx1032")
5255 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1033, "gfx1033")
5256 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1100, "gfx1100")
5257 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1101, "gfx1101")
5258 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1102, "gfx1102")
5259 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX602, "gfx602")
5260 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX705, "gfx705")
5261 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX805, "gfx805")
5262 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1035, "gfx1035")
5263 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1034, "gfx1034")
5264 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90A, "gfx90a")
5265 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX940, "gfx940")
5266 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1013, "gfx1013")
5267 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1036, "gfx1036")
5268 default:
5269 out += sprintf (out, _(", <unknown AMDGPU GPU type: %#x>"), mach);
5270 break;
5271#undef AMDGPU_CASE
5272 }
5273
5274 e_flags &= ~EF_AMDGPU_MACH;
5275
5276 if ((osabi == ELFOSABI_AMDGPU_HSA
5277 && abiversion == ELFABIVERSION_AMDGPU_HSA_V3)
5278 || osabi != ELFOSABI_AMDGPU_HSA)
5279 {
5280 /* For HSA v3 and other OS ABIs. */
5281 if (e_flags & EF_AMDGPU_FEATURE_XNACK_V3)
5282 {
5283 out = stpcpy (out, ", xnack on");
5284 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V3;
5285 }
5286
5287 if (e_flags & EF_AMDGPU_FEATURE_SRAMECC_V3)
5288 {
5289 out = stpcpy (out, ", sramecc on");
5290 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V3;
5291 }
5292 }
5293 else
5294 {
5295 /* For HSA v4+. */
5296 int xnack, sramecc;
5297
5298 xnack = e_flags & EF_AMDGPU_FEATURE_XNACK_V4;
5299 switch (xnack)
5300 {
5301 case EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4:
5302 break;
5303
5304 case EF_AMDGPU_FEATURE_XNACK_ANY_V4:
5305 out = stpcpy (out, ", xnack any");
5306 break;
5307
5308 case EF_AMDGPU_FEATURE_XNACK_OFF_V4:
5309 out = stpcpy (out, ", xnack off");
5310 break;
5311
5312 case EF_AMDGPU_FEATURE_XNACK_ON_V4:
5313 out = stpcpy (out, ", xnack on");
5314 break;
5315
5316 default:
5317 out += sprintf (out, _(", <unknown xnack value: %#x>"), xnack);
5318 break;
5319 }
5320
5321 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V4;
5322
5323 sramecc = e_flags & EF_AMDGPU_FEATURE_SRAMECC_V4;
5324 switch (sramecc)
5325 {
5326 case EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4:
5327 break;
5328
5329 case EF_AMDGPU_FEATURE_SRAMECC_ANY_V4:
5330 out = stpcpy (out, ", sramecc any");
5331 break;
5332
5333 case EF_AMDGPU_FEATURE_SRAMECC_OFF_V4:
5334 out = stpcpy (out, ", sramecc off");
5335 break;
5336
5337 case EF_AMDGPU_FEATURE_SRAMECC_ON_V4:
5338 out = stpcpy (out, ", sramecc on");
5339 break;
5340
5341 default:
5342 out += sprintf (out, _(", <unknown sramecc value: %#x>"), sramecc);
5343 break;
5344 }
5345
5346 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V4;
5347 }
5348
5349 if (e_flags != 0)
5350 out += sprintf (out, _(", unknown flags bits: %#x"), e_flags);
5351 return out;
5352}
5353
5354static char *
5355get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
5356{
5357 static char buf[1024];
5358 char *out = buf;
5359
5360 buf[0] = '\0';
5361
5362 if (e_flags)
5363 {
5364 switch (e_machine)
5365 {
5366 default:
5367 break;
5368
5369 case EM_ARC_COMPACT3:
5370 out = stpcpy (out, ", HS5x");
5371 break;
5372
5373 case EM_ARC_COMPACT3_64:
5374 out = stpcpy (out, ", HS6x");
5375 break;
5376
5377 case EM_ARC_COMPACT2:
5378 case EM_ARC_COMPACT:
5379 out = decode_ARC_machine_flags (out, e_flags, e_machine);
5380 break;
5381
5382 case EM_ARM:
5383 out = decode_ARM_machine_flags (out, e_flags);
5384 break;
5385
5386 case EM_AVR:
5387 out = decode_AVR_machine_flags (out, e_flags);
5388 break;
5389
5390 case EM_BLACKFIN:
5391 out = decode_BLACKFIN_machine_flags (out, e_flags);
5392 break;
5393
5394 case EM_CYGNUS_FRV:
5395 out = decode_FRV_machine_flags (out, e_flags);
5396 break;
5397
5398 case EM_68K:
5399 out = decode_M68K_machine_flags (out, e_flags);
5400 break;
5401
5402 case EM_AMDGPU:
5403 out = decode_AMDGPU_machine_flags (out, e_flags, filedata);
5404 break;
5405
5406 case EM_CYGNUS_MEP:
5407 out = decode_MeP_machine_flags (out, e_flags);
5408 break;
5409
5410 case EM_PPC:
5411 if (e_flags & EF_PPC_EMB)
5412 out = stpcpy (out, ", emb");
5413
5414 if (e_flags & EF_PPC_RELOCATABLE)
5415 out = stpcpy (out, _(", relocatable"));
5416
5417 if (e_flags & EF_PPC_RELOCATABLE_LIB)
5418 out = stpcpy (out, _(", relocatable-lib"));
5419 break;
5420
5421 case EM_PPC64:
5422 if (e_flags & EF_PPC64_ABI)
5423 out += sprintf (out, ", abiv%d", e_flags & EF_PPC64_ABI);
5424 break;
5425
5426 case EM_V800:
5427 out = decode_V800_machine_flags (out, e_flags);
5428 break;
5429
5430 case EM_V850:
5431 case EM_CYGNUS_V850:
5432 out = decode_V850_machine_flags (out, e_flags);
5433 break;
5434
5435 case EM_M32R:
5436 case EM_CYGNUS_M32R:
5437 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
5438 out = stpcpy (out, ", m32r");
5439 break;
5440
5441 case EM_MIPS:
5442 case EM_MIPS_RS3_LE:
5443 out = decode_MIPS_machine_flags (out, e_flags);
5444 break;
5445
5446 case EM_NDS32:
5447 out = decode_NDS32_machine_flags (out, e_flags);
5448 break;
5449
5450 case EM_NFP:
5451 switch (EF_NFP_MACH (e_flags))
5452 {
5453 case E_NFP_MACH_3200:
5454 out = stpcpy (out, ", NFP-32xx");
5455 break;
5456 case E_NFP_MACH_6000:
5457 out = stpcpy (out, ", NFP-6xxx");
5458 break;
5459 }
5460 break;
5461
5462 case EM_RISCV:
5463 out = decode_RISCV_machine_flags (out, e_flags);
5464 break;
5465
5466 case EM_SH:
5467 out = decode_SH_machine_flags (out, e_flags);
5468 break;
5469
5470 case EM_OR1K:
5471 if (e_flags & EF_OR1K_NODELAY)
5472 out = stpcpy (out, ", no delay");
5473 break;
5474
5475 case EM_BPF:
5476 out += sprintf (out, ", CPU Version: %u", e_flags & EF_BPF_CPUVER);
5477 break;
5478
5479 case EM_SPARCV9:
5480 out = decode_SPARC_machine_flags (out, e_flags);
5481 break;
5482
5483 case EM_PARISC:
5484 out = decode_PARISC_machine_flags (out, e_flags);
5485 break;
5486
5487 case EM_PJ:
5488 case EM_PJ_OLD:
5489 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
5490 out = stpcpy (out, ", new calling convention");
5491
5492 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
5493 out = stpcpy (out, ", gnu calling convention");
5494 break;
5495
5496 case EM_IA_64:
5497 out = decode_IA64_machine_flags (out, e_flags, filedata);
5498 break;
5499
5500 case EM_VAX:
5501 if ((e_flags & EF_VAX_NONPIC))
5502 out = stpcpy (out, ", non-PIC");
5503 if ((e_flags & EF_VAX_DFLOAT))
5504 out = stpcpy (out, ", D-Float");
5505 if ((e_flags & EF_VAX_GFLOAT))
5506 out = stpcpy (out, ", G-Float");
5507 break;
5508
5509 case EM_VISIUM:
5510 if (e_flags & EF_VISIUM_ARCH_MCM)
5511 out = stpcpy (out, ", mcm");
5512 else if (e_flags & EF_VISIUM_ARCH_MCM24)
5513 out = stpcpy (out, ", mcm24");
5514 if (e_flags & EF_VISIUM_ARCH_GR6)
5515 out = stpcpy (out, ", gr6");
5516 break;
5517
5518 case EM_RL78:
5519 out = decode_RL78_machine_flags (out, e_flags);
5520 break;
5521
5522 case EM_RX:
5523 out = decode_RX_machine_flags (out, e_flags);
5524 break;
5525
5526 case EM_S390:
5527 if (e_flags & EF_S390_HIGH_GPRS)
5528 out = stpcpy (out, ", highgprs");
5529 break;
5530
5531 case EM_TI_C6000:
5532 if ((e_flags & EF_C6000_REL))
5533 out = stpcpy (out, ", relocatable module");
5534 break;
5535
5536 case EM_KVX:
5537 if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV3_1)
5538 strcat (buf, ", Kalray VLIW kv3-1");
5539 else if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV3_2)
5540 strcat (buf, ", Kalray VLIW kv3-2");
5541 else if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV4_1)
5542 strcat (buf, ", Kalray VLIW kv4-1");
5543 else
5544 strcat (buf, ", unknown KVX MPPA");
5545 break;
5546
5547 case EM_MSP430:
5548 out = decode_MSP430_machine_flags (out, e_flags);
5549 break;
5550
5551 case EM_Z80:
5552 out = decode_Z80_machine_flags (out, e_flags);
5553 break;
5554
5555 case EM_LOONGARCH:
5556 out = decode_LOONGARCH_machine_flags (out, e_flags);
5557 break;
5558 }
5559 }
5560
5561 return buf;
5562}
5563
5564static const char *
5565get_osabi_name (Filedata * filedata, unsigned int osabi)
5566{
5567 static char buff[32];
5568
5569 switch (osabi)
5570 {
5571 case ELFOSABI_NONE: return "UNIX - System V";
5572 case ELFOSABI_HPUX: return "UNIX - HP-UX";
5573 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
5574 case ELFOSABI_GNU: return "UNIX - GNU";
5575 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
5576 case ELFOSABI_AIX: return "UNIX - AIX";
5577 case ELFOSABI_IRIX: return "UNIX - IRIX";
5578 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
5579 case ELFOSABI_TRU64: return "UNIX - TRU64";
5580 case ELFOSABI_MODESTO: return "Novell - Modesto";
5581 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
5582 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
5583 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
5584 case ELFOSABI_AROS: return "AROS";
5585 case ELFOSABI_FENIXOS: return "FenixOS";
5586 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
5587 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
5588 case ELFOSABI_CUDA: return "CUDA";
5589 default:
5590 if (osabi >= 64)
5591 switch (filedata->file_header.e_machine)
5592 {
5593 case EM_AMDGPU:
5594 switch (osabi)
5595 {
5596 case ELFOSABI_AMDGPU_HSA: return "AMD HSA";
5597 case ELFOSABI_AMDGPU_PAL: return "AMD PAL";
5598 case ELFOSABI_AMDGPU_MESA3D: return "AMD Mesa3D";
5599 default:
5600 break;
5601 }
5602 break;
5603
5604 case EM_ARM:
5605 switch (osabi)
5606 {
5607 case ELFOSABI_ARM: return "ARM";
5608 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
5609 default:
5610 break;
5611 }
5612 break;
5613
5614 case EM_MSP430:
5615 case EM_MSP430_OLD:
5616 case EM_VISIUM:
5617 switch (osabi)
5618 {
5619 case ELFOSABI_STANDALONE: return _("Standalone App");
5620 default:
5621 break;
5622 }
5623 break;
5624
5625 case EM_TI_C6000:
5626 switch (osabi)
5627 {
5628 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
5629 case ELFOSABI_C6000_LINUX: return "Linux C6000";
5630 default:
5631 break;
5632 }
5633 break;
5634
5635 default:
5636 break;
5637 }
5638 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
5639 return buff;
5640 }
5641}
5642
5643static const char *
5644get_aarch64_segment_type (unsigned long type)
5645{
5646 switch (type)
5647 {
5648 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
5649 case PT_AARCH64_MEMTAG_MTE: return "AARCH64_MEMTAG_MTE";
5650 default: return NULL;
5651 }
5652}
5653
5654static const char *
5655get_arm_segment_type (unsigned long type)
5656{
5657 switch (type)
5658 {
5659 case PT_ARM_ARCHEXT: return "ARM_ARCHEXT";
5660 case PT_ARM_EXIDX: return "ARM_EXIDX";
5661 default: return NULL;
5662 }
5663}
5664
5665static const char *
5666get_s390_segment_type (unsigned long type)
5667{
5668 switch (type)
5669 {
5670 case PT_S390_PGSTE: return "S390_PGSTE";
5671 default: return NULL;
5672 }
5673}
5674
5675static const char *
5676get_mips_segment_type (unsigned long type)
5677{
5678 switch (type)
5679 {
5680 case PT_MIPS_REGINFO: return "REGINFO";
5681 case PT_MIPS_RTPROC: return "RTPROC";
5682 case PT_MIPS_OPTIONS: return "OPTIONS";
5683 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
5684 default: return NULL;
5685 }
5686}
5687
5688static const char *
5689get_parisc_segment_type (unsigned long type)
5690{
5691 switch (type)
5692 {
5693 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
5694 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
5695 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
5696 default: return NULL;
5697 }
5698}
5699
5700static const char *
5701get_ia64_segment_type (unsigned long type)
5702{
5703 switch (type)
5704 {
5705 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
5706 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
5707 default: return NULL;
5708 }
5709}
5710
5711static const char *
5712get_tic6x_segment_type (unsigned long type)
5713{
5714 switch (type)
5715 {
5716 case PT_C6000_PHATTR: return "C6000_PHATTR";
5717 default: return NULL;
5718 }
5719}
5720
5721static const char *
5722get_riscv_segment_type (unsigned long type)
5723{
5724 switch (type)
5725 {
5726 case PT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
5727 default: return NULL;
5728 }
5729}
5730
5731static const char *
5732get_hpux_segment_type (unsigned long type, unsigned e_machine)
5733{
5734 if (e_machine == EM_PARISC)
5735 switch (type)
5736 {
5737 case PT_HP_TLS: return "HP_TLS";
5738 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
5739 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
5740 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
5741 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
5742 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
5743 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
5744 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
5745 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
5746 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
5747 case PT_HP_PARALLEL: return "HP_PARALLEL";
5748 case PT_HP_FASTBIND: return "HP_FASTBIND";
5749 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
5750 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
5751 case PT_HP_STACK: return "HP_STACK";
5752 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
5753 default:
5754 break;
5755 }
5756
5757 if (e_machine == EM_IA_64)
5758 switch (type)
5759 {
5760 case PT_HP_TLS: return "HP_TLS";
5761 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
5762 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
5763 case PT_IA_64_HP_STACK: return "HP_STACK";
5764 default:
5765 break;
5766 }
5767
5768 return NULL;
5769}
5770
5771static const char *
5772get_solaris_segment_type (unsigned long type)
5773{
5774 switch (type)
5775 {
5776 case PT_SUNW_UNWIND: return "SUNW_UNWIND";
5777 case PT_SUNW_EH_FRAME: return "SUNW_EH_FRAME";
5778 case PT_SUNWBSS: return "SUNW_BSS";
5779 case PT_SUNWSTACK: return "SUNW_STACK";
5780 case PT_SUNWDTRACE: return "SUNW_DTRACE";
5781 case PT_SUNWCAP: return "SUNW_CAP";
5782 default: return NULL;
5783 }
5784}
5785
5786static const char *
5787get_os_specific_segment_type (Filedata * filedata, unsigned long p_type)
5788{
5789 static char buff[32];
5790 const char * result = NULL;
5791
5792 switch (filedata->file_header.e_ident[EI_OSABI])
5793 {
5794 case ELFOSABI_GNU:
5795 case ELFOSABI_FREEBSD:
5796 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
5797 {
5798 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
5799 result = buff;
5800 }
5801 break;
5802
5803 case ELFOSABI_HPUX:
5804 result = get_hpux_segment_type (p_type,
5805 filedata->file_header.e_machine);
5806 break;
5807
5808 case ELFOSABI_SOLARIS:
5809 result = get_solaris_segment_type (p_type);
5810 break;
5811
5812 default:
5813 break;
5814 }
5815
5816 if (result != NULL)
5817 return result;
5818
5819 switch (p_type)
5820 {
5821 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
5822 case PT_GNU_STACK: return "GNU_STACK";
5823 case PT_GNU_RELRO: return "GNU_RELRO";
5824 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
5825 case PT_GNU_SFRAME: return "GNU_SFRAME";
5826
5827 case PT_OPENBSD_MUTABLE: return "OPENBSD_MUTABLE";
5828 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
5829 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
5830 case PT_OPENBSD_NOBTCFI: return "OPENBSD_NOBTCFI";
5831 case PT_OPENBSD_SYSCALLS: return "OPENBSD_SYSCALLS";
5832 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
5833
5834 default:
5835 break;
5836 }
5837
5838 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
5839 return buff;
5840}
5841
5842static const char *
5843get_processor_specific_segment_type (Filedata * filedata, unsigned long p_type)
5844{
5845 static char buff[32];
5846 const char * result = NULL;
5847
5848 switch (filedata->file_header.e_machine)
5849 {
5850 case EM_AARCH64:
5851 result = get_aarch64_segment_type (p_type);
5852 break;
5853
5854 case EM_ARM:
5855 result = get_arm_segment_type (p_type);
5856 break;
5857
5858 case EM_MIPS:
5859 case EM_MIPS_RS3_LE:
5860 result = get_mips_segment_type (p_type);
5861 break;
5862
5863 case EM_PARISC:
5864 result = get_parisc_segment_type (p_type);
5865 break;
5866
5867 case EM_IA_64:
5868 result = get_ia64_segment_type (p_type);
5869 break;
5870
5871 case EM_TI_C6000:
5872 result = get_tic6x_segment_type (p_type);
5873 break;
5874
5875 case EM_S390:
5876 case EM_S390_OLD:
5877 result = get_s390_segment_type (p_type);
5878 break;
5879
5880 case EM_RISCV:
5881 result = get_riscv_segment_type (p_type);
5882 break;
5883
5884 default:
5885 result = NULL;
5886 break;
5887 }
5888
5889 if (result != NULL)
5890 return result;
5891
5892 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
5893 return buff;
5894}
5895
5896static const char *
5897get_segment_type (Filedata * filedata, unsigned long p_type)
5898{
5899 static char buff[32];
5900
5901 switch (p_type)
5902 {
5903 case PT_NULL: return "NULL";
5904 case PT_LOAD: return "LOAD";
5905 case PT_DYNAMIC: return "DYNAMIC";
5906 case PT_INTERP: return "INTERP";
5907 case PT_NOTE: return "NOTE";
5908 case PT_SHLIB: return "SHLIB";
5909 case PT_PHDR: return "PHDR";
5910 case PT_TLS: return "TLS";
5911 case PT_NUM: return "NUM";
5912 }
5913
5914 if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
5915 return get_os_specific_segment_type (filedata, p_type);
5916
5917 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
5918 return get_processor_specific_segment_type (filedata, p_type);
5919
5920 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
5921 return buff;
5922}
5923
5924static const char *
5925get_arc_section_type_name (unsigned int sh_type)
5926{
5927 switch (sh_type)
5928 {
5929 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
5930 default:
5931 break;
5932 }
5933 return NULL;
5934}
5935
5936static const char *
5937get_mips_section_type_name (unsigned int sh_type)
5938{
5939 switch (sh_type)
5940 {
5941 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
5942 case SHT_MIPS_MSYM: return "MIPS_MSYM";
5943 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
5944 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
5945 case SHT_MIPS_UCODE: return "MIPS_UCODE";
5946 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
5947 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
5948 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
5949 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
5950 case SHT_MIPS_RELD: return "MIPS_RELD";
5951 case SHT_MIPS_IFACE: return "MIPS_IFACE";
5952 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
5953 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
5954 case SHT_MIPS_SHDR: return "MIPS_SHDR";
5955 case SHT_MIPS_FDESC: return "MIPS_FDESC";
5956 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
5957 case SHT_MIPS_DENSE: return "MIPS_DENSE";
5958 case SHT_MIPS_PDESC: return "MIPS_PDESC";
5959 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
5960 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
5961 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
5962 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
5963 case SHT_MIPS_LINE: return "MIPS_LINE";
5964 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
5965 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
5966 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
5967 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
5968 case SHT_MIPS_DWARF: return "MIPS_DWARF";
5969 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
5970 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
5971 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
5972 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
5973 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
5974 case SHT_MIPS_XLATE: return "MIPS_XLATE";
5975 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
5976 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
5977 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
5978 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
5979 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
5980 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
5981 case SHT_MIPS_XHASH: return "MIPS_XHASH";
5982 default:
5983 break;
5984 }
5985 return NULL;
5986}
5987
5988static const char *
5989get_parisc_section_type_name (unsigned int sh_type)
5990{
5991 switch (sh_type)
5992 {
5993 case SHT_PARISC_EXT: return "PARISC_EXT";
5994 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
5995 case SHT_PARISC_DOC: return "PARISC_DOC";
5996 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
5997 case SHT_PARISC_DLKM: return "PARISC_DLKM";
5998 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
5999 case SHT_PARISC_STUBS: return "PARISC_STUBS";
6000 default: return NULL;
6001 }
6002}
6003
6004static const char *
6005get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
6006{
6007 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
6008 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
6009 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
6010
6011 switch (sh_type)
6012 {
6013 case SHT_IA_64_EXT: return "IA_64_EXT";
6014 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
6015 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
6016 default:
6017 break;
6018 }
6019 return NULL;
6020}
6021
6022static const char *
6023get_vms_section_type_name (unsigned int sh_type)
6024{
6025 switch (sh_type)
6026 {
6027 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
6028 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
6029 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
6030 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
6031 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
6032 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
6033 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
6034 default:
6035 break;
6036 }
6037 return NULL;
6038}
6039
6040static const char *
6041get_x86_64_section_type_name (unsigned int sh_type)
6042{
6043 switch (sh_type)
6044 {
6045 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
6046 default: return NULL;
6047 }
6048}
6049
6050static const char *
6051get_aarch64_section_type_name (unsigned int sh_type)
6052{
6053 switch (sh_type)
6054 {
6055 case SHT_AARCH64_ATTRIBUTES:
6056 return "AARCH64_ATTRIBUTES";
6057 case SHT_AARCH64_AUTH_RELR:
6058 return "AARCH64_AUTH_RELR";
6059 case SHT_AARCH64_MEMTAG_GLOBALS_STATIC:
6060 return "AARCH64_MEMTAG_GLOBALS_STATIC";
6061 case SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC:
6062 return "AARCH64_MEMTAG_GLOBALS_DYNAMIC";
6063 default:
6064 return NULL;
6065 }
6066}
6067
6068static const char *
6069get_arm_section_type_name (unsigned int sh_type)
6070{
6071 switch (sh_type)
6072 {
6073 case SHT_ARM_EXIDX: return "ARM_EXIDX";
6074 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
6075 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
6076 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
6077 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
6078 default: return NULL;
6079 }
6080}
6081
6082static const char *
6083get_tic6x_section_type_name (unsigned int sh_type)
6084{
6085 switch (sh_type)
6086 {
6087 case SHT_C6000_UNWIND: return "C6000_UNWIND";
6088 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
6089 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
6090 case SHT_TI_ICODE: return "TI_ICODE";
6091 case SHT_TI_XREF: return "TI_XREF";
6092 case SHT_TI_HANDLER: return "TI_HANDLER";
6093 case SHT_TI_INITINFO: return "TI_INITINFO";
6094 case SHT_TI_PHATTRS: return "TI_PHATTRS";
6095 default: return NULL;
6096 }
6097}
6098
6099static const char *
6100get_msp430_section_type_name (unsigned int sh_type)
6101{
6102 switch (sh_type)
6103 {
6104 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
6105 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
6106 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
6107 default: return NULL;
6108 }
6109}
6110
6111static const char *
6112get_nfp_section_type_name (unsigned int sh_type)
6113{
6114 switch (sh_type)
6115 {
6116 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
6117 case SHT_NFP_INITREG: return "NFP_INITREG";
6118 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
6119 default: return NULL;
6120 }
6121}
6122
6123static const char *
6124get_v850_section_type_name (unsigned int sh_type)
6125{
6126 switch (sh_type)
6127 {
6128 case SHT_V850_SCOMMON: return "V850 Small Common";
6129 case SHT_V850_TCOMMON: return "V850 Tiny Common";
6130 case SHT_V850_ZCOMMON: return "V850 Zero Common";
6131 case SHT_RENESAS_IOP: return "RENESAS IOP";
6132 case SHT_RENESAS_INFO: return "RENESAS INFO";
6133 default: return NULL;
6134 }
6135}
6136
6137static const char *
6138get_riscv_section_type_name (unsigned int sh_type)
6139{
6140 switch (sh_type)
6141 {
6142 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
6143 default: return NULL;
6144 }
6145}
6146
6147static const char *
6148get_csky_section_type_name (unsigned int sh_type)
6149{
6150 switch (sh_type)
6151 {
6152 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
6153 default: return NULL;
6154 }
6155}
6156
6157static const char *
6158get_powerpc_section_type_name (unsigned int sh_type)
6159{
6160 switch (sh_type)
6161 {
6162 case SHT_ORDERED: return "ORDERED";
6163 default: return NULL;
6164 }
6165}
6166
6167static const char *
6168get_alpha_section_type_name (unsigned int sh_type)
6169{
6170 switch (sh_type)
6171 {
6172 case SHT_ALPHA_DEBUG: return "DEBUG";
6173 case SHT_ALPHA_REGINFO: return "REGINFO";
6174 default: return NULL;
6175 }
6176}
6177
6178static const char *
6179get_processor_specific_section_type_name (Filedata * filedata, unsigned int sh_type)
6180{
6181 static char buff[32];
6182 const char * result = NULL;
6183
6184 switch (filedata->file_header.e_machine)
6185 {
6186 case EM_AARCH64:
6187 result = get_aarch64_section_type_name (sh_type);
6188 break;
6189
6190 case EM_ALPHA:
6191 result = get_alpha_section_type_name (sh_type);
6192 break;
6193
6194 case EM_ARC:
6195 case EM_ARC_COMPACT:
6196 case EM_ARC_COMPACT2:
6197 case EM_ARC_COMPACT3:
6198 case EM_ARC_COMPACT3_64:
6199 result = get_arc_section_type_name (sh_type);
6200 break;
6201
6202 case EM_ARM:
6203 result = get_arm_section_type_name (sh_type);
6204 break;
6205
6206 case EM_CSKY:
6207 result = get_csky_section_type_name (sh_type);
6208 break;
6209
6210 case EM_IA_64:
6211 result = get_ia64_section_type_name (filedata, sh_type);
6212 break;
6213
6214 case EM_MIPS:
6215 case EM_MIPS_RS3_LE:
6216 result = get_mips_section_type_name (sh_type);
6217 break;
6218
6219 case EM_MSP430:
6220 result = get_msp430_section_type_name (sh_type);
6221 break;
6222
6223 case EM_NFP:
6224 result = get_nfp_section_type_name (sh_type);
6225 break;
6226
6227 case EM_PARISC:
6228 result = get_parisc_section_type_name (sh_type);
6229 break;
6230
6231 case EM_PPC64:
6232 case EM_PPC:
6233 return get_powerpc_section_type_name (sh_type);
6234 break;
6235
6236 case EM_RISCV:
6237 result = get_riscv_section_type_name (sh_type);
6238 break;
6239
6240 case EM_TI_C6000:
6241 result = get_tic6x_section_type_name (sh_type);
6242 break;
6243
6244 case EM_V800:
6245 case EM_V850:
6246 case EM_CYGNUS_V850:
6247 result = get_v850_section_type_name (sh_type);
6248 break;
6249
6250 case EM_X86_64:
6251 case EM_L1OM:
6252 case EM_K1OM:
6253 result = get_x86_64_section_type_name (sh_type);
6254 break;
6255
6256 default:
6257 break;
6258 }
6259
6260 if (result != NULL)
6261 return result;
6262
6263 switch (sh_type)
6264 {
6265 /* FIXME: Are these correct ? If so, why do they not have #define's ? */
6266 case 0x7ffffffd: return "AUXILIARY";
6267 case 0x7fffffff: return "FILTER";
6268 default:
6269 break;
6270 }
6271
6272 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
6273 return buff;
6274}
6275
6276static const char *
6277get_os_specific_section_type_name (Filedata * filedata, unsigned int sh_type)
6278{
6279 static char buff[32];
6280 const char * result = NULL;
6281
6282 switch (filedata->file_header.e_machine)
6283 {
6284 case EM_IA_64:
6285 result = get_vms_section_type_name (sh_type);
6286 break;
6287 default:
6288 break;
6289 }
6290
6291 if (result != NULL)
6292 return result;
6293
6294 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
6295 result = get_solaris_section_type (sh_type);
6296
6297 if (result != NULL)
6298 return result;
6299
6300 switch (sh_type)
6301 {
6302 case SHT_GNU_INCREMENTAL_INPUTS: return "GNU_INCREMENTAL_INPUTS";
6303 case SHT_GNU_ATTRIBUTES: return "GNU_ATTRIBUTES";
6304 case SHT_GNU_HASH: return "GNU_HASH";
6305 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
6306 case SHT_GNU_OBJECT_ONLY: return "GNU_OBJECT_ONLY";
6307 case SHT_GNU_SFRAME: return "GNU_SFRAME";
6308
6309 case SHT_SUNW_move: return "SUNW_MOVE";
6310 case SHT_SUNW_COMDAT: return "SUNW_COMDAT";
6311 case SHT_SUNW_syminfo: return "SUNW_SYMINFO";
6312 case SHT_GNU_verdef: return "VERDEF";
6313 case SHT_GNU_verneed: return "VERNEED";
6314 case SHT_GNU_versym: return "VERSYM";
6315
6316 case SHT_LLVM_ODRTAB: return "LLVM_ODRTAB";
6317 case SHT_LLVM_LINKER_OPTIONS: return "LLVM_LINKER_OPTIONS";
6318 case SHT_LLVM_ADDRSIG: return "LLVM_ADDRSIG";
6319 case SHT_LLVM_DEPENDENT_LIBRARIES: return "LLVM_DEPENDENT_LIBRARIES";
6320 case SHT_LLVM_SYMPART: return "LLVM_SYMPART";
6321 case SHT_LLVM_PART_EHDR: return "LLVM_PART_EHDR";
6322 case SHT_LLVM_PART_PHDR: return "LLVM_PART_PHDR";
6323 case SHT_LLVM_BB_ADDR_MAP_V0: return "LLVM_BB_ADDR_MAP_V0";
6324 case SHT_LLVM_CALL_GRAPH_PROFILE: return "LLVM_CALL_GRAPH_PROFILE";
6325 case SHT_LLVM_BB_ADDR_MAP: return "LLVM_BB_ADDR_MAP";
6326 case SHT_LLVM_OFFLOADING: return "LLVM_OFFLOADING";
6327 case SHT_LLVM_LTO: return "LLVM_LTO";
6328
6329 case SHT_ANDROID_REL: return "ANDROID_REL";
6330 case SHT_ANDROID_RELA: return "ANDROID_RELA";
6331 case SHT_ANDROID_RELR: return "ANDROID_RELR";
6332
6333 case SHT_CHECKSUM: return "CHECKSUM";
6334
6335 /* FIXME: Are these correct ? If so, why do they not have #define's ? */
6336 case 0x6ffffff0: return "VERSYM";
6337
6338 default:
6339 break;
6340 }
6341
6342 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
6343 return buff;
6344}
6345
6346static const char *
6347get_user_specific_section_type_name (Filedata * filedata, unsigned int sh_type)
6348{
6349 static char buff[32];
6350 const char * result;
6351
6352 switch (filedata->file_header.e_machine)
6353 {
6354 case EM_V800:
6355 case EM_V850:
6356 case EM_CYGNUS_V850:
6357 result = get_v850_section_type_name (sh_type);
6358 break;
6359
6360 default:
6361 result = NULL;
6362 break;
6363 }
6364
6365 if (result != NULL)
6366 return result;
6367
6368 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
6369 return buff;
6370}
6371
6372static const char *
6373get_section_type_name (Filedata * filedata,
6374 unsigned int sh_type)
6375{
6376 switch (sh_type)
6377 {
6378 case SHT_NULL: return "NULL";
6379 case SHT_PROGBITS: return "PROGBITS";
6380 case SHT_SYMTAB: return "SYMTAB";
6381 case SHT_STRTAB: return "STRTAB";
6382 case SHT_RELA: return "RELA";
6383 case SHT_HASH: return "HASH";
6384 case SHT_DYNAMIC: return "DYNAMIC";
6385 case SHT_NOTE: return "NOTE";
6386 case SHT_NOBITS: return "NOBITS";
6387 case SHT_REL: return "REL";
6388 case SHT_SHLIB: return "SHLIB";
6389 case SHT_DYNSYM: return "DYNSYM";
6390 /* 12 and 13 are not defined. */
6391 case SHT_INIT_ARRAY: return "INIT_ARRAY";
6392 case SHT_FINI_ARRAY: return "FINI_ARRAY";
6393 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
6394 case SHT_GROUP: return "GROUP";
6395 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
6396 case SHT_RELR: return "RELR";
6397 /* End of generic section types. */
6398
6399 default:
6400 break;
6401 }
6402
6403 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
6404 return get_processor_specific_section_type_name (filedata, sh_type);
6405
6406 if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
6407 return get_os_specific_section_type_name (filedata, sh_type);
6408
6409 if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
6410 return get_user_specific_section_type_name (filedata, sh_type);
6411
6412 static char buff[32];
6413
6414 /* This message is probably going to be displayed in a 15
6415 character wide field, so put the hex value first. */
6416 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
6417 return buff;
6418}
6419
6420enum long_option_values
6421{
6422 OPTION_DEBUG_DUMP = 512,
6423 OPTION_DYN_SYMS,
6424 OPTION_LTO_SYMS,
6425 OPTION_DWARF_DEPTH,
6426 OPTION_DWARF_START,
6427 OPTION_DWARF_CHECK,
6428 OPTION_CTF_DUMP,
6429 OPTION_CTF_PARENT,
6430 OPTION_CTF_SYMBOLS,
6431 OPTION_CTF_STRINGS,
6432 OPTION_SFRAME_DUMP,
6433 OPTION_WITH_SYMBOL_VERSIONS,
6434 OPTION_RECURSE_LIMIT,
6435 OPTION_NO_RECURSE_LIMIT,
6436 OPTION_NO_DEMANGLING,
6437 OPTION_NO_EXTRA_SYM_INFO,
6438 OPTION_SYM_BASE,
6439 OPTION_GOT_CONTENTS
6440};
6441
6442static struct option options[] =
6443{
6444 /* Note - This table is alpha-sorted on the 'val'
6445 field in order to make adding new options easier. */
6446 {"arch-specific", no_argument, 0, 'A'},
6447 {"all", no_argument, 0, 'a'},
6448 {"demangle", optional_argument, 0, 'C'},
6449 {"archive-index", no_argument, 0, 'c'},
6450 {"use-dynamic", no_argument, 0, 'D'},
6451 {"dynamic", no_argument, 0, 'd'},
6452 {"headers", no_argument, 0, 'e'},
6453 {"section-groups", no_argument, 0, 'g'},
6454 {"help", no_argument, 0, 'H'},
6455 {"file-header", no_argument, 0, 'h'},
6456 {"histogram", no_argument, 0, 'I'},
6457 {"display-section", required_argument, 0, 'j'},
6458 {"lint", no_argument, 0, 'L'},
6459 {"enable-checks", no_argument, 0, 'L'},
6460 {"program-headers", no_argument, 0, 'l'},
6461 {"segments", no_argument, 0, 'l'},
6462 {"full-section-name",no_argument, 0, 'N'},
6463 {"notes", no_argument, 0, 'n'},
6464 {"process-links", no_argument, 0, 'P'},
6465 {"string-dump", required_argument, 0, 'p'},
6466 {"relocated-dump", required_argument, 0, 'R'},
6467 {"relocs", no_argument, 0, 'r'},
6468 {"section-headers", no_argument, 0, 'S'},
6469 {"sections", no_argument, 0, 'S'},
6470 {"symbols", no_argument, 0, 's'},
6471 {"syms", no_argument, 0, 's'},
6472 {"silent-truncation",no_argument, 0, 'T'},
6473 {"section-details", no_argument, 0, 't'},
6474 {"unicode", required_argument, NULL, 'U'},
6475 {"unwind", no_argument, 0, 'u'},
6476 {"version-info", no_argument, 0, 'V'},
6477 {"version", no_argument, 0, 'v'},
6478 {"wide", no_argument, 0, 'W'},
6479 {"extra-sym-info", no_argument, 0, 'X'},
6480 {"hex-dump", required_argument, 0, 'x'},
6481 {"decompress", no_argument, 0, 'z'},
6482
6483 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
6484 {"no-extra-sym-info",no_argument, 0, OPTION_NO_EXTRA_SYM_INFO},
6485 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
6486 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
6487 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
6488 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
6489 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
6490 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
6491 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
6492 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
6493 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
6494#ifdef ENABLE_LIBCTF
6495 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
6496 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
6497 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
6498 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
6499#endif
6500 {"sframe", optional_argument, 0, OPTION_SFRAME_DUMP},
6501 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
6502 {"got-contents", no_argument, 0, OPTION_GOT_CONTENTS},
6503
6504 {0, no_argument, 0, 0}
6505};
6506
6507static void
6508usage (FILE * stream)
6509{
6510 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
6511 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
6512 fprintf (stream, _(" Options are:\n"));
6513 fprintf (stream, _("\
6514 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I --got-contents\n"));
6515 fprintf (stream, _("\
6516 -h --file-header Display the ELF file header\n"));
6517 fprintf (stream, _("\
6518 -l --program-headers Display the program headers\n"));
6519 fprintf (stream, _("\
6520 --segments An alias for --program-headers\n"));
6521 fprintf (stream, _("\
6522 -S --section-headers Display the sections' header\n"));
6523 fprintf (stream, _("\
6524 --sections An alias for --section-headers\n"));
6525 fprintf (stream, _("\
6526 -g --section-groups Display the section groups\n"));
6527 fprintf (stream, _("\
6528 -t --section-details Display the section details\n"));
6529 fprintf (stream, _("\
6530 -e --headers Equivalent to: -h -l -S\n"));
6531 fprintf (stream, _("\
6532 -s --syms Display the symbol table\n"));
6533 fprintf (stream, _("\
6534 --symbols An alias for --syms\n"));
6535 fprintf (stream, _("\
6536 --dyn-syms Display the dynamic symbol table\n"));
6537 fprintf (stream, _("\
6538 --lto-syms Display LTO symbol tables\n"));
6539 fprintf (stream, _("\
6540 --sym-base=[0|8|10|16] \n\
6541 Force base for symbol sizes. The options are \n\
6542 mixed (the default), octal, decimal, hexadecimal.\n"));
6543 fprintf (stream, _("\
6544 -C --demangle[=STYLE] Decode mangled/processed symbol names\n"));
6545 display_demangler_styles (stream, _("\
6546 STYLE can be "));
6547 fprintf (stream, _("\
6548 --no-demangle Do not demangle low-level symbol names. (default)\n"));
6549 fprintf (stream, _("\
6550 --recurse-limit Enable a demangling recursion limit. (default)\n"));
6551 fprintf (stream, _("\
6552 --no-recurse-limit Disable a demangling recursion limit\n"));
6553 fprintf (stream, _("\
6554 -U[dlexhi] --unicode=[default|locale|escape|hex|highlight|invalid]\n\
6555 Display unicode characters as determined by the current locale\n\
6556 (default), escape sequences, \"<hex sequences>\", highlighted\n\
6557 escape sequences, or treat them as invalid and display as\n\
6558 \"{hex sequences}\"\n"));
6559 fprintf (stream, _("\
6560 -X --extra-sym-info Display extra information when showing symbols\n"));
6561 fprintf (stream, _("\
6562 --no-extra-sym-info Do not display extra information when showing symbols (default)\n"));
6563 fprintf (stream, _("\
6564 -n --notes Display the contents of note sections (if present)\n"));
6565 fprintf (stream, _("\
6566 -r --relocs Display the relocations (if present)\n"));
6567 fprintf (stream, _("\
6568 -u --unwind Display the unwind info (if present)\n"));
6569 fprintf (stream, _("\
6570 -d --dynamic Display the dynamic section (if present)\n"));
6571 fprintf (stream, _("\
6572 -V --version-info Display the version sections (if present)\n"));
6573 fprintf (stream, _("\
6574 -A --arch-specific Display architecture specific information (if any)\n"));
6575 fprintf (stream, _("\
6576 -c --archive-index Display the symbol/file index in an archive\n"));
6577 fprintf (stream, _("\
6578 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
6579 fprintf (stream, _("\
6580 -L --lint|--enable-checks\n\
6581 Display warning messages for possible problems\n"));
6582 fprintf (stream, _("\
6583 -x --hex-dump=<number|name>\n\
6584 Dump the contents of section <number|name> as bytes\n"));
6585 fprintf (stream, _("\
6586 -p --string-dump=<number|name>\n\
6587 Dump the contents of section <number|name> as strings\n"));
6588 fprintf (stream, _("\
6589 -R --relocated-dump=<number|name>\n\
6590 Dump the relocated contents of section <number|name>\n"));
6591 fprintf (stream, _("\
6592 -z --decompress Decompress section before dumping it\n"));
6593 fprintf (stream, _("\n\
6594 -j --display-section=<name|number>\n\
6595 Display the contents of the indicated section. Can be repeated\n"));
6596 fprintf (stream, _("\
6597 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
6598 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
6599 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
6600 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
6601 U/=trace_info]\n\
6602 Display the contents of DWARF debug sections\n"));
6603 fprintf (stream, _("\
6604 -wk --debug-dump=links Display the contents of sections that link to separate\n\
6605 debuginfo files\n"));
6606 fprintf (stream, _("\
6607 -P --process-links Display the contents of non-debug sections in separate\n\
6608 debuginfo files. (Implies -wK)\n"));
6609#if DEFAULT_FOR_FOLLOW_LINKS
6610 fprintf (stream, _("\
6611 -wK --debug-dump=follow-links\n\
6612 Follow links to separate debug info files (default)\n"));
6613 fprintf (stream, _("\
6614 -wN --debug-dump=no-follow-links\n\
6615 Do not follow links to separate debug info files\n"));
6616#else
6617 fprintf (stream, _("\
6618 -wK --debug-dump=follow-links\n\
6619 Follow links to separate debug info files\n"));
6620 fprintf (stream, _("\
6621 -wN --debug-dump=no-follow-links\n\
6622 Do not follow links to separate debug info files\n\
6623 (default)\n"));
6624#endif
6625#if HAVE_LIBDEBUGINFOD
6626 fprintf (stream, _("\
6627 -wD --debug-dump=use-debuginfod\n\
6628 When following links, also query debuginfod servers (default)\n"));
6629 fprintf (stream, _("\
6630 -wE --debug-dump=do-not-use-debuginfod\n\
6631 When following links, do not query debuginfod servers\n"));
6632#endif
6633 fprintf (stream, _("\
6634 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
6635 fprintf (stream, _("\
6636 --dwarf-start=N Display DIEs starting at offset N\n"));
6637#ifdef ENABLE_LIBCTF
6638 fprintf (stream, _("\
6639 --ctf=<number|name> Display CTF info from section <number|name>\n"));
6640 fprintf (stream, _("\
6641 --ctf-parent=<name> Use CTF archive member <name> as the CTF parent\n"));
6642 fprintf (stream, _("\
6643 --ctf-symbols=<number|name>\n\
6644 Use section <number|name> as the CTF external symtab\n"));
6645 fprintf (stream, _("\
6646 --ctf-strings=<number|name>\n\
6647 Use section <number|name> as the CTF external strtab\n"));
6648#endif
6649 fprintf (stream, _("\
6650 --sframe[=NAME] Display SFrame info from section NAME, (default '.sframe')\n"));
6651
6652#ifdef SUPPORT_DISASSEMBLY
6653 fprintf (stream, _("\
6654 -i --instruction-dump=<number|name>\n\
6655 Disassemble the contents of section <number|name>\n"));
6656#endif
6657 fprintf (stream, _("\
6658 -I --histogram Display histogram of bucket list lengths\n"));
6659 fprintf (stream, _("\
6660 --got-contents Display GOT section contents\n"));
6661 fprintf (stream, _("\
6662 -W --wide Allow output width to exceed 80 characters\n"));
6663 fprintf (stream, _("\
6664 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
6665 fprintf (stream, _("\
6666 @<file> Read options from <file>\n"));
6667 fprintf (stream, _("\
6668 -H --help Display this information\n"));
6669 fprintf (stream, _("\
6670 -v --version Display the version number of readelf\n"));
6671
6672 if (REPORT_BUGS_TO[0] && stream == stdout)
6673 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
6674
6675 exit (stream == stdout ? 0 : 1);
6676}
6677
6678/* Record the fact that the user wants the contents of section number
6679 SECTION to be displayed using the method(s) encoded as flags bits
6680 in TYPE. Note, TYPE can be zero if we are creating the array for
6681 the first time. */
6682
6683static void
6684request_dump_bynumber (struct dump_data *dumpdata,
6685 unsigned int section, dump_type type)
6686{
6687 if (section >= dumpdata->num_dump_sects)
6688 {
6689 dump_type * new_dump_sects;
6690
6691 new_dump_sects = (dump_type *) calloc (section + 1,
6692 sizeof (* new_dump_sects));
6693
6694 if (new_dump_sects == NULL)
6695 error (_("Out of memory allocating dump request table.\n"));
6696 else
6697 {
6698 if (dumpdata->dump_sects)
6699 {
6700 /* Copy current flag settings. */
6701 memcpy (new_dump_sects, dumpdata->dump_sects,
6702 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
6703
6704 free (dumpdata->dump_sects);
6705 }
6706
6707 dumpdata->dump_sects = new_dump_sects;
6708 dumpdata->num_dump_sects = section + 1;
6709 }
6710 }
6711
6712 if (dumpdata->dump_sects)
6713 dumpdata->dump_sects[section] |= type;
6714}
6715
6716/* Request a dump by section name. */
6717
6718static void
6719request_dump_byname (const char * section, dump_type type)
6720{
6721 struct dump_list_entry * new_request;
6722
6723 new_request = (struct dump_list_entry *)
6724 malloc (sizeof (struct dump_list_entry));
6725 if (!new_request)
6726 error (_("Out of memory allocating dump request table.\n"));
6727
6728 new_request->name = strdup (section);
6729 if (!new_request->name)
6730 error (_("Out of memory allocating dump request table.\n"));
6731
6732 new_request->type = type;
6733
6734 new_request->next = dump_sects_byname;
6735 dump_sects_byname = new_request;
6736}
6737
6738static inline void
6739request_dump (struct dump_data *dumpdata, dump_type type)
6740{
6741 int section;
6742 char * cp;
6743
6744 do_dump = true;
6745 section = strtoul (optarg, & cp, 0);
6746
6747 if (! *cp && section >= 0)
6748 request_dump_bynumber (dumpdata, section, type);
6749 else
6750 request_dump_byname (optarg, type);
6751}
6752
6753static void
6754parse_args (struct dump_data *dumpdata, int argc, char ** argv)
6755{
6756 int c;
6757
6758 if (argc < 2)
6759 usage (stderr);
6760
6761 while ((c = getopt_long
6762 (argc, argv, "ACDHILNPR:STU:VWXacdeghi:j:lnp:rstuvw::x:z", options, NULL)) != EOF)
6763 {
6764 switch (c)
6765 {
6766 case 0:
6767 /* Long options. */
6768 break;
6769 case 'H':
6770 usage (stdout);
6771 break;
6772
6773 case 'a':
6774 do_syms = true;
6775 do_reloc = true;
6776 do_unwind = true;
6777 do_dynamic = true;
6778 do_header = true;
6779 do_sections = true;
6780 do_section_groups = true;
6781 do_segments = true;
6782 do_version = true;
6783 do_histogram = true;
6784 do_arch = true;
6785 do_notes = true;
6786 do_got_section_contents = true;
6787 break;
6788
6789 case 'g':
6790 do_section_groups = true;
6791 break;
6792 case 't':
6793 case 'N':
6794 do_sections = true;
6795 do_section_details = true;
6796 break;
6797 case 'e':
6798 do_header = true;
6799 do_sections = true;
6800 do_segments = true;
6801 break;
6802 case 'A':
6803 do_arch = true;
6804 break;
6805 case 'D':
6806 do_using_dynamic = true;
6807 break;
6808 case 'r':
6809 do_reloc = true;
6810 break;
6811 case 'u':
6812 do_unwind = true;
6813 break;
6814 case 'h':
6815 do_header = true;
6816 break;
6817 case 'l':
6818 do_segments = true;
6819 break;
6820 case 's':
6821 do_syms = true;
6822 break;
6823 case 'S':
6824 do_sections = true;
6825 break;
6826 case 'd':
6827 do_dynamic = true;
6828 break;
6829 case 'I':
6830 do_histogram = true;
6831 break;
6832 case 'n':
6833 do_notes = true;
6834 break;
6835 case 'c':
6836 do_archive_index = true;
6837 break;
6838 case 'L':
6839 do_checks = true;
6840 break;
6841 case 'P':
6842 process_links = true;
6843 do_follow_links = true;
6844 dump_any_debugging = true;
6845 break;
6846 case 'j':
6847 request_dump (dumpdata, AUTO_DUMP);
6848 break;
6849 case 'x':
6850 request_dump (dumpdata, HEX_DUMP);
6851 break;
6852 case 'p':
6853 request_dump (dumpdata, STRING_DUMP);
6854 break;
6855 case 'R':
6856 request_dump (dumpdata, RELOC_DUMP);
6857 break;
6858 case 'z':
6859 decompress_dumps = true;
6860 break;
6861 case 'w':
6862 if (optarg == NULL)
6863 {
6864 do_debugging = true;
6865 do_dump = true;
6866 dump_any_debugging = true;
6867 dwarf_select_sections_all ();
6868 }
6869 else
6870 {
6871 do_debugging = false;
6872 if (dwarf_select_sections_by_letters (optarg))
6873 {
6874 do_dump = true;
6875 dump_any_debugging = true;
6876 }
6877 }
6878 break;
6879 case OPTION_DEBUG_DUMP:
6880 if (optarg == NULL)
6881 {
6882 do_dump = true;
6883 do_debugging = true;
6884 dump_any_debugging = true;
6885 dwarf_select_sections_all ();
6886 }
6887 else if (strcmp (optarg, "sframe-internal-only") == 0)
6888 warn (_("Unrecognized debug option 'sframe-internal-only'\n"));
6889 else
6890 {
6891 do_debugging = false;
6892 if (dwarf_select_sections_by_names (optarg))
6893 {
6894 do_dump = true;
6895 dump_any_debugging = true;
6896 }
6897 }
6898 break;
6899 case OPTION_DWARF_DEPTH:
6900 {
6901 char *cp;
6902
6903 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
6904 }
6905 break;
6906 case OPTION_DWARF_START:
6907 {
6908 char *cp;
6909
6910 dwarf_start_die = strtoul (optarg, & cp, 0);
6911 }
6912 break;
6913 case OPTION_DWARF_CHECK:
6914 dwarf_check = true;
6915 break;
6916 case OPTION_CTF_DUMP:
6917 do_ctf = true;
6918 request_dump (dumpdata, CTF_DUMP);
6919 break;
6920 case OPTION_CTF_SYMBOLS:
6921 free (dump_ctf_symtab_name);
6922 dump_ctf_symtab_name = strdup (optarg);
6923 break;
6924 case OPTION_CTF_STRINGS:
6925 free (dump_ctf_strtab_name);
6926 dump_ctf_strtab_name = strdup (optarg);
6927 break;
6928 case OPTION_CTF_PARENT:
6929 free (dump_ctf_parent_name);
6930 dump_ctf_parent_name = strdup (optarg);
6931 break;
6932 case OPTION_SFRAME_DUMP:
6933 do_sframe = true;
6934 /* Fix PR/32589 but keep the error messaging same ? */
6935 if (optarg != NULL && strcmp (optarg, "") == 0)
6936 {
6937 do_dump = true;
6938 error (_("Section name must be provided\n"));
6939 }
6940 /* Providing section name is optional. request_dump (), however,
6941 thrives on non NULL optarg. Handle it explicitly here. */
6942 else if (optarg != NULL)
6943 request_dump (dumpdata, SFRAME_DUMP);
6944 else
6945 {
6946 do_dump = true;
6947 const char *sframe_sec_name = strdup (".sframe");
6948 request_dump_byname (sframe_sec_name, SFRAME_DUMP);
6949 }
6950 break;
6951 case OPTION_DYN_SYMS:
6952 do_dyn_syms = true;
6953 break;
6954 case OPTION_LTO_SYMS:
6955 do_lto_syms = true;
6956 break;
6957 case 'X':
6958 extra_sym_info = true;
6959 break;
6960 case OPTION_NO_EXTRA_SYM_INFO:
6961 extra_sym_info = false;
6962 break;
6963
6964#ifdef SUPPORT_DISASSEMBLY
6965 case 'i':
6966 request_dump (dumpdata, DISASS_DUMP);
6967 break;
6968#endif
6969 case 'v':
6970 print_version (program_name);
6971 break;
6972 case 'V':
6973 do_version = true;
6974 break;
6975 case 'W':
6976 do_wide = true;
6977 break;
6978 case 'T':
6979 do_not_show_symbol_truncation = true;
6980 break;
6981 case 'C':
6982 do_demangle = true;
6983 if (optarg != NULL)
6984 {
6985 enum demangling_styles style;
6986
6987 style = cplus_demangle_name_to_style (optarg);
6988 if (style == unknown_demangling)
6989 error (_("unknown demangling style `%s'"), optarg);
6990
6991 cplus_demangle_set_style (style);
6992 }
6993 break;
6994 case OPTION_NO_DEMANGLING:
6995 do_demangle = false;
6996 break;
6997 case OPTION_RECURSE_LIMIT:
6998 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
6999 break;
7000 case OPTION_NO_RECURSE_LIMIT:
7001 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
7002 break;
7003 case OPTION_WITH_SYMBOL_VERSIONS:
7004 /* Ignored for backward compatibility. */
7005 break;
7006
7007 case 'U':
7008 if (optarg == NULL)
7009 error (_("Missing arg to -U/--unicode")); /* Can this happen ? */
7010 else if (streq (optarg, "default") || streq (optarg, "d"))
7011 unicode_display = unicode_default;
7012 else if (streq (optarg, "locale") || streq (optarg, "l"))
7013 unicode_display = unicode_locale;
7014 else if (streq (optarg, "escape") || streq (optarg, "e"))
7015 unicode_display = unicode_escape;
7016 else if (streq (optarg, "invalid") || streq (optarg, "i"))
7017 unicode_display = unicode_invalid;
7018 else if (streq (optarg, "hex") || streq (optarg, "x"))
7019 unicode_display = unicode_hex;
7020 else if (streq (optarg, "highlight") || streq (optarg, "h"))
7021 unicode_display = unicode_highlight;
7022 else
7023 error (_("invalid argument to -U/--unicode: %s"), optarg);
7024 break;
7025
7026 case OPTION_SYM_BASE:
7027 sym_base = 0;
7028 if (optarg != NULL)
7029 {
7030 sym_base = strtoul (optarg, NULL, 0);
7031 switch (sym_base)
7032 {
7033 case 0:
7034 case 8:
7035 case 10:
7036 case 16:
7037 break;
7038
7039 default:
7040 sym_base = 0;
7041 break;
7042 }
7043 }
7044 break;
7045
7046 case OPTION_GOT_CONTENTS:
7047 do_got_section_contents = true;
7048 do_dump = true;
7049 break;
7050
7051 default:
7052 /* xgettext:c-format */
7053 error (_("Invalid option '-%c'\n"), c);
7054 /* Fall through. */
7055 case '?':
7056 usage (stderr);
7057 }
7058 }
7059
7060 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
7061 && !do_segments && !do_header && !do_dump && !do_version
7062 && !do_histogram && !do_debugging && !do_arch && !do_notes
7063 && !do_section_groups && !do_archive_index
7064 && !do_dyn_syms && !do_lto_syms)
7065 {
7066 if (do_checks)
7067 {
7068 check_all = true;
7069 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
7070 do_segments = do_header = do_dump = do_version = true;
7071 do_histogram = do_debugging = do_arch = do_notes = true;
7072 do_section_groups = do_archive_index = do_dyn_syms = true;
7073 do_lto_syms = true;
7074 }
7075 else
7076 usage (stderr);
7077 }
7078}
7079
7080static const char *
7081get_elf_class (unsigned int elf_class)
7082{
7083 static char buff[32];
7084
7085 switch (elf_class)
7086 {
7087 case ELFCLASSNONE: return _("none");
7088 case ELFCLASS32: return "ELF32";
7089 case ELFCLASS64: return "ELF64";
7090 default:
7091 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
7092 return buff;
7093 }
7094}
7095
7096static const char *
7097get_data_encoding (unsigned int encoding)
7098{
7099 static char buff[32];
7100
7101 switch (encoding)
7102 {
7103 case ELFDATANONE: return _("none");
7104 case ELFDATA2LSB: return _("2's complement, little endian");
7105 case ELFDATA2MSB: return _("2's complement, big endian");
7106 default:
7107 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
7108 return buff;
7109 }
7110}
7111
7112static bool
7113check_magic_number (Filedata * filedata, Elf_Internal_Ehdr * header)
7114{
7115 if (header->e_ident[EI_MAG0] == ELFMAG0
7116 && header->e_ident[EI_MAG1] == ELFMAG1
7117 && header->e_ident[EI_MAG2] == ELFMAG2
7118 && header->e_ident[EI_MAG3] == ELFMAG3)
7119 return true;
7120
7121 /* Some compilers produce object files that are not in the ELF file format.
7122 As an aid to users of readelf, try to identify these cases and suggest
7123 alternative tools.
7124
7125 FIXME: It is not clear if all four bytes are used as constant magic
7126 valus by all compilers. It may be necessary to recode this function if
7127 different tools use different length sequences. */
7128
7129 static struct
7130 {
7131 unsigned char magic[4];
7132 const char * obj_message;
7133 const char * ar_message;
7134 }
7135 known_magic[] =
7136 {
7137 { { 'B', 'C', 0xc0, 0xde },
7138 N_("This is a LLVM bitcode file - try using llvm-bcanalyzer\n"),
7139 N_("This is a LLVM bitcode file - try extracting and then using llvm-bcanalyzer\n")
7140 },
7141 { { 'g', 'o', ' ', 'o' },
7142 N_("This is a GO binary file - try using 'go tool objdump' or 'go tool nm'\n"),
7143 NULL
7144 }
7145 };
7146 int i;
7147
7148 for (i = ARRAY_SIZE (known_magic); i--;)
7149 {
7150 if (header->e_ident[EI_MAG0] == known_magic[i].magic[0]
7151 && header->e_ident[EI_MAG1] == known_magic[i].magic[1]
7152 && header->e_ident[EI_MAG2] == known_magic[i].magic[2]
7153 && header->e_ident[EI_MAG3] == known_magic[i].magic[3])
7154 {
7155 /* Some compiler's analyzer tools do not handle archives,
7156 so we provide two different kinds of error message. */
7157 if (filedata->archive_file_size > 0
7158 && known_magic[i].ar_message != NULL)
7159 error ("%s", known_magic[i].ar_message);
7160 else
7161 error ("%s", known_magic[i].obj_message);
7162 return false;
7163 }
7164 }
7165
7166 error (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
7167 return false;
7168}
7169
7170/* Decode the data held in 'filedata->file_header'. */
7171
7172static bool
7173process_file_header (Filedata * filedata)
7174{
7175 Elf_Internal_Ehdr * header = & filedata->file_header;
7176
7177 if (! check_magic_number (filedata, header))
7178 return false;
7179
7180 if (! filedata->is_separate)
7181 init_dwarf_by_elf_machine_code (header->e_machine);
7182
7183 if (do_header)
7184 {
7185 unsigned i;
7186
7187 if (filedata->is_separate)
7188 printf (_("ELF Header in linked file '%s':\n"),
7189 printable_string (filedata->file_name, 0));
7190 else
7191 printf (_("ELF Header:\n"));
7192 printf (_(" Magic: "));
7193 for (i = 0; i < EI_NIDENT; i++)
7194 printf ("%2.2x ", header->e_ident[i]);
7195 printf ("\n");
7196 printf (_(" Class: %s\n"),
7197 get_elf_class (header->e_ident[EI_CLASS]));
7198 printf (_(" Data: %s\n"),
7199 get_data_encoding (header->e_ident[EI_DATA]));
7200 printf (_(" Version: %d%s\n"),
7201 header->e_ident[EI_VERSION],
7202 (header->e_ident[EI_VERSION] == EV_CURRENT
7203 ? _(" (current)")
7204 : (header->e_ident[EI_VERSION] != EV_NONE
7205 ? _(" <unknown>")
7206 : "")));
7207 printf (_(" OS/ABI: %s\n"),
7208 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
7209 printf (_(" ABI Version: %d\n"),
7210 header->e_ident[EI_ABIVERSION]);
7211 printf (_(" Type: %s\n"),
7212 get_file_type (filedata));
7213 printf (_(" Machine: %s\n"),
7214 get_machine_name (header->e_machine));
7215 printf (_(" Version: 0x%lx\n"),
7216 header->e_version);
7217
7218 printf (_(" Entry point address: "));
7219 print_vma (header->e_entry, PREFIX_HEX);
7220 printf (_("\n Start of program headers: "));
7221 print_vma (header->e_phoff, DEC);
7222 printf (_(" (bytes into file)\n Start of section headers: "));
7223 print_vma (header->e_shoff, DEC);
7224 printf (_(" (bytes into file)\n"));
7225
7226 printf (_(" Flags: 0x%lx%s\n"),
7227 header->e_flags,
7228 get_machine_flags (filedata, header->e_flags, header->e_machine));
7229 printf (_(" Size of this header: %u (bytes)\n"),
7230 header->e_ehsize);
7231 printf (_(" Size of program headers: %u (bytes)\n"),
7232 header->e_phentsize);
7233 printf (_(" Number of program headers: %u"),
7234 header->e_phnum);
7235 if (filedata->section_headers != NULL
7236 && header->e_phnum == PN_XNUM
7237 && filedata->section_headers[0].sh_info != 0)
7238 printf (" (%u)", filedata->section_headers[0].sh_info);
7239 putc ('\n', stdout);
7240 printf (_(" Size of section headers: %u (bytes)\n"),
7241 header->e_shentsize);
7242 printf (_(" Number of section headers: %u"),
7243 header->e_shnum);
7244 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
7245 {
7246 header->e_shnum = filedata->section_headers[0].sh_size;
7247 printf (" (%u)", header->e_shnum);
7248 }
7249 putc ('\n', stdout);
7250 printf (_(" Section header string table index: %u"),
7251 header->e_shstrndx);
7252 if (filedata->section_headers != NULL
7253 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
7254 {
7255 header->e_shstrndx = filedata->section_headers[0].sh_link;
7256 printf (" (%u)", header->e_shstrndx);
7257 }
7258 if (header->e_shstrndx != SHN_UNDEF
7259 && header->e_shstrndx >= header->e_shnum)
7260 {
7261 header->e_shstrndx = SHN_UNDEF;
7262 printf (_(" <corrupt: out of range>"));
7263 }
7264 putc ('\n', stdout);
7265 }
7266
7267 if (filedata->section_headers != NULL)
7268 {
7269 if (header->e_phnum == PN_XNUM
7270 && filedata->section_headers[0].sh_info != 0)
7271 {
7272 /* Throw away any cached read of PN_XNUM headers. */
7273 free (filedata->program_headers);
7274 filedata->program_headers = NULL;
7275 header->e_phnum = filedata->section_headers[0].sh_info;
7276 }
7277 if (header->e_shnum == SHN_UNDEF)
7278 header->e_shnum = filedata->section_headers[0].sh_size;
7279 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
7280 header->e_shstrndx = filedata->section_headers[0].sh_link;
7281 if (header->e_shstrndx >= header->e_shnum)
7282 header->e_shstrndx = SHN_UNDEF;
7283 }
7284
7285 return true;
7286}
7287
7288/* Read in the program headers from FILEDATA and store them in PHEADERS.
7289 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
7290
7291static bool
7292get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
7293{
7294 Elf32_External_Phdr * phdrs;
7295 Elf32_External_Phdr * external;
7296 Elf_Internal_Phdr * internal;
7297 unsigned int i;
7298 unsigned int size = filedata->file_header.e_phentsize;
7299 unsigned int num = filedata->file_header.e_phnum;
7300
7301 /* PR binutils/17531: Cope with unexpected section header sizes. */
7302 if (size == 0 || num == 0)
7303 return false;
7304 if (size < sizeof * phdrs)
7305 {
7306 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
7307 return false;
7308 }
7309 if (size > sizeof * phdrs)
7310 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
7311
7312 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
7313 size, num, _("program headers"));
7314 if (phdrs == NULL)
7315 return false;
7316
7317 for (i = 0, internal = pheaders, external = phdrs;
7318 i < filedata->file_header.e_phnum;
7319 i++, internal++, external++)
7320 {
7321 internal->p_type = BYTE_GET (external->p_type);
7322 internal->p_offset = BYTE_GET (external->p_offset);
7323 internal->p_vaddr = BYTE_GET (external->p_vaddr);
7324 internal->p_paddr = BYTE_GET (external->p_paddr);
7325 internal->p_filesz = BYTE_GET (external->p_filesz);
7326 internal->p_memsz = BYTE_GET (external->p_memsz);
7327 internal->p_flags = BYTE_GET (external->p_flags);
7328 internal->p_align = BYTE_GET (external->p_align);
7329 }
7330
7331 free (phdrs);
7332 return true;
7333}
7334
7335/* Read in the program headers from FILEDATA and store them in PHEADERS.
7336 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
7337
7338static bool
7339get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
7340{
7341 Elf64_External_Phdr * phdrs;
7342 Elf64_External_Phdr * external;
7343 Elf_Internal_Phdr * internal;
7344 unsigned int i;
7345 unsigned int size = filedata->file_header.e_phentsize;
7346 unsigned int num = filedata->file_header.e_phnum;
7347
7348 /* PR binutils/17531: Cope with unexpected section header sizes. */
7349 if (size == 0 || num == 0)
7350 return false;
7351 if (size < sizeof * phdrs)
7352 {
7353 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
7354 return false;
7355 }
7356 if (size > sizeof * phdrs)
7357 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
7358
7359 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
7360 size, num, _("program headers"));
7361 if (!phdrs)
7362 return false;
7363
7364 for (i = 0, internal = pheaders, external = phdrs;
7365 i < filedata->file_header.e_phnum;
7366 i++, internal++, external++)
7367 {
7368 internal->p_type = BYTE_GET (external->p_type);
7369 internal->p_flags = BYTE_GET (external->p_flags);
7370 internal->p_offset = BYTE_GET (external->p_offset);
7371 internal->p_vaddr = BYTE_GET (external->p_vaddr);
7372 internal->p_paddr = BYTE_GET (external->p_paddr);
7373 internal->p_filesz = BYTE_GET (external->p_filesz);
7374 internal->p_memsz = BYTE_GET (external->p_memsz);
7375 internal->p_align = BYTE_GET (external->p_align);
7376 }
7377
7378 free (phdrs);
7379 return true;
7380}
7381
7382/* Returns TRUE if the program headers were read into `program_headers'. */
7383
7384static bool
7385get_program_headers (Filedata * filedata)
7386{
7387 Elf_Internal_Phdr * phdrs;
7388
7389 /* Check cache of prior read. */
7390 if (filedata->program_headers != NULL)
7391 return true;
7392
7393 /* Be kind to memory checkers by looking for
7394 e_phnum values which we know must be invalid. */
7395 if (filedata->file_header.e_phnum
7396 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
7397 >= filedata->file_size)
7398 {
7399 error (_("Too many program headers - %#x - the file is not that big\n"),
7400 filedata->file_header.e_phnum);
7401 return false;
7402 }
7403
7404 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
7405 sizeof (Elf_Internal_Phdr));
7406 if (phdrs == NULL)
7407 {
7408 error (_("Out of memory reading %u program headers\n"),
7409 filedata->file_header.e_phnum);
7410 return false;
7411 }
7412
7413 if (is_32bit_elf
7414 ? get_32bit_program_headers (filedata, phdrs)
7415 : get_64bit_program_headers (filedata, phdrs))
7416 {
7417 filedata->program_headers = phdrs;
7418 return true;
7419 }
7420
7421 free (phdrs);
7422 return false;
7423}
7424
7425/* Print program header info and locate dynamic section. */
7426
7427static void
7428process_program_headers (Filedata * filedata)
7429{
7430 Elf_Internal_Phdr * segment;
7431 unsigned int i;
7432 Elf_Internal_Phdr * previous_load = NULL;
7433
7434 if (filedata->file_header.e_phnum == 0)
7435 {
7436 /* PR binutils/12467. */
7437 if (filedata->file_header.e_phoff != 0)
7438 warn (_("possibly corrupt ELF header - it has a non-zero program"
7439 " header offset, but no program headers\n"));
7440 else if (do_segments)
7441 {
7442 if (filedata->is_separate)
7443 printf (_("\nThere are no program headers in linked file '%s'.\n"),
7444 printable_string (filedata->file_name, 0));
7445 else
7446 printf (_("\nThere are no program headers in this file.\n"));
7447 }
7448 goto no_headers;
7449 }
7450
7451 if (do_segments && !do_header)
7452 {
7453 if (filedata->is_separate)
7454 printf ("\nIn linked file '%s' the ELF file type is %s\n",
7455 printable_string (filedata->file_name, 0),
7456 get_file_type (filedata));
7457 else
7458 printf (_("\nElf file type is %s\n"), get_file_type (filedata));
7459 printf (_("Entry point 0x%" PRIx64 "\n"),
7460 filedata->file_header.e_entry);
7461 printf (ngettext ("There is %d program header,"
7462 " starting at offset %" PRIu64 "\n",
7463 "There are %d program headers,"
7464 " starting at offset %" PRIu64 "\n",
7465 filedata->file_header.e_phnum),
7466 filedata->file_header.e_phnum,
7467 filedata->file_header.e_phoff);
7468 }
7469
7470 if (! get_program_headers (filedata))
7471 goto no_headers;
7472
7473 if (do_segments)
7474 {
7475 if (filedata->file_header.e_phnum > 1)
7476 printf (_("\nProgram Headers:\n"));
7477 else
7478 printf (_("\nProgram Headers:\n"));
7479
7480 if (is_32bit_elf)
7481 printf
7482 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
7483 else if (do_wide)
7484 printf
7485 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
7486 else
7487 {
7488 printf
7489 (_(" Type Offset VirtAddr PhysAddr\n"));
7490 printf
7491 (_(" FileSiz MemSiz Flags Align\n"));
7492 }
7493 }
7494
7495 uint64_t dynamic_addr = 0;
7496 uint64_t dynamic_size = 0;
7497 for (i = 0, segment = filedata->program_headers;
7498 i < filedata->file_header.e_phnum;
7499 i++, segment++)
7500 {
7501 if (do_segments)
7502 {
7503 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
7504
7505 if (is_32bit_elf)
7506 {
7507 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
7508 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
7509 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
7510 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
7511 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
7512 printf ("%c%c%c ",
7513 (segment->p_flags & PF_R ? 'R' : ' '),
7514 (segment->p_flags & PF_W ? 'W' : ' '),
7515 (segment->p_flags & PF_X ? 'E' : ' '));
7516 printf ("%#lx", (unsigned long) segment->p_align);
7517 }
7518 else if (do_wide)
7519 {
7520 if ((unsigned long) segment->p_offset == segment->p_offset)
7521 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
7522 else
7523 {
7524 print_vma (segment->p_offset, FULL_HEX);
7525 putchar (' ');
7526 }
7527
7528 print_vma (segment->p_vaddr, FULL_HEX);
7529 putchar (' ');
7530 print_vma (segment->p_paddr, FULL_HEX);
7531 putchar (' ');
7532
7533 if ((unsigned long) segment->p_filesz == segment->p_filesz)
7534 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
7535 else
7536 {
7537 print_vma (segment->p_filesz, FULL_HEX);
7538 putchar (' ');
7539 }
7540
7541 if ((unsigned long) segment->p_memsz == segment->p_memsz)
7542 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
7543 else
7544 {
7545 print_vma (segment->p_memsz, FULL_HEX);
7546 }
7547
7548 printf (" %c%c%c ",
7549 (segment->p_flags & PF_R ? 'R' : ' '),
7550 (segment->p_flags & PF_W ? 'W' : ' '),
7551 (segment->p_flags & PF_X ? 'E' : ' '));
7552
7553 if ((unsigned long) segment->p_align == segment->p_align)
7554 printf ("%#lx", (unsigned long) segment->p_align);
7555 else
7556 {
7557 print_vma (segment->p_align, PREFIX_HEX);
7558 }
7559 }
7560 else
7561 {
7562 print_vma (segment->p_offset, FULL_HEX);
7563 putchar (' ');
7564 print_vma (segment->p_vaddr, FULL_HEX);
7565 putchar (' ');
7566 print_vma (segment->p_paddr, FULL_HEX);
7567 printf ("\n ");
7568 print_vma (segment->p_filesz, FULL_HEX);
7569 putchar (' ');
7570 print_vma (segment->p_memsz, FULL_HEX);
7571 printf (" %c%c%c ",
7572 (segment->p_flags & PF_R ? 'R' : ' '),
7573 (segment->p_flags & PF_W ? 'W' : ' '),
7574 (segment->p_flags & PF_X ? 'E' : ' '));
7575 print_vma (segment->p_align, PREFIX_HEX);
7576 }
7577
7578 putc ('\n', stdout);
7579 }
7580
7581 switch (segment->p_type)
7582 {
7583 case PT_LOAD:
7584#if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
7585 required by the ELF standard, several programs, including the Linux
7586 kernel, make use of non-ordered segments. */
7587 if (previous_load
7588 && previous_load->p_vaddr > segment->p_vaddr)
7589 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
7590#endif
7591 if (segment->p_memsz < segment->p_filesz)
7592 error (_("the segment's file size is larger than its memory size\n"));
7593 previous_load = segment;
7594 break;
7595
7596 case PT_PHDR:
7597 /* PR 20815 - Verify that the program header is loaded into memory. */
7598 if (i > 0 && previous_load != NULL)
7599 error (_("the PHDR segment must occur before any LOAD segment\n"));
7600 if (filedata->file_header.e_machine != EM_PARISC)
7601 {
7602 unsigned int j;
7603
7604 for (j = 1; j < filedata->file_header.e_phnum; j++)
7605 {
7606 Elf_Internal_Phdr *load = filedata->program_headers + j;
7607 if (load->p_type == PT_LOAD
7608 && load->p_offset <= segment->p_offset
7609 && (load->p_offset + load->p_filesz
7610 >= segment->p_offset + segment->p_filesz)
7611 && load->p_vaddr <= segment->p_vaddr
7612 && (load->p_vaddr + load->p_filesz
7613 >= segment->p_vaddr + segment->p_filesz))
7614 break;
7615 }
7616 if (j == filedata->file_header.e_phnum)
7617 error (_("the PHDR segment is not covered by a LOAD segment\n"));
7618 }
7619 break;
7620
7621 case PT_DYNAMIC:
7622 if (dynamic_addr)
7623 error (_("more than one dynamic segment\n"));
7624
7625 /* By default, assume that the .dynamic section is the first
7626 section in the DYNAMIC segment. */
7627 dynamic_addr = segment->p_offset;
7628 dynamic_size = segment->p_filesz;
7629
7630 /* Try to locate the .dynamic section. If there is
7631 a section header table, we can easily locate it. */
7632 if (filedata->section_headers != NULL)
7633 {
7634 Elf_Internal_Shdr * sec;
7635
7636 sec = find_section (filedata, ".dynamic");
7637 if (sec == NULL || sec->sh_size == 0)
7638 {
7639 /* A corresponding .dynamic section is expected, but on
7640 IA-64/OpenVMS it is OK for it to be missing. */
7641 if (!is_ia64_vms (filedata))
7642 error (_("no .dynamic section in the dynamic segment\n"));
7643 break;
7644 }
7645
7646 if (sec->sh_type == SHT_NOBITS)
7647 {
7648 dynamic_addr = 0;
7649 dynamic_size = 0;
7650 break;
7651 }
7652
7653 dynamic_addr = sec->sh_offset;
7654 dynamic_size = sec->sh_size;
7655
7656 /* The PT_DYNAMIC segment, which is used by the run-time
7657 loader, should exactly match the .dynamic section. */
7658 if (do_checks
7659 && (dynamic_addr != segment->p_offset
7660 || dynamic_size != segment->p_filesz))
7661 warn (_("\
7662the .dynamic section is not the same as the dynamic segment\n"));
7663 }
7664
7665 /* PR binutils/17512: Avoid corrupt dynamic section info in the
7666 segment. Check this after matching against the section headers
7667 so we don't warn on debuginfo file (which have NOBITS .dynamic
7668 sections). */
7669 if (dynamic_addr > filedata->file_size
7670 || (dynamic_size > filedata->file_size - dynamic_addr))
7671 {
7672 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
7673 dynamic_addr = 0;
7674 dynamic_size = 0;
7675 }
7676 break;
7677
7678 case PT_INTERP:
7679 if (segment->p_offset >= filedata->file_size
7680 || segment->p_filesz > filedata->file_size - segment->p_offset
7681 || segment->p_filesz - 1 >= (size_t) -2
7682 || fseek64 (filedata->handle,
7683 filedata->archive_file_offset + segment->p_offset,
7684 SEEK_SET))
7685 error (_("Unable to find program interpreter name\n"));
7686 else
7687 {
7688 size_t len = segment->p_filesz;
7689 free (filedata->program_interpreter);
7690 filedata->program_interpreter = xmalloc (len + 1);
7691 len = fread (filedata->program_interpreter, 1, len,
7692 filedata->handle);
7693 filedata->program_interpreter[len] = 0;
7694
7695 if (do_segments)
7696 printf (_(" [Requesting program interpreter: %s]\n"),
7697 printable_string (filedata->program_interpreter, 0));
7698 }
7699 break;
7700 }
7701 }
7702
7703 if (do_segments
7704 && filedata->section_headers != NULL
7705 && filedata->string_table != NULL)
7706 {
7707 printf (_("\n Section to Segment mapping:\n"));
7708 printf (_(" Segment Sections...\n"));
7709
7710 for (i = 0; i < filedata->file_header.e_phnum; i++)
7711 {
7712 unsigned int j;
7713 Elf_Internal_Shdr * section;
7714
7715 segment = filedata->program_headers + i;
7716 section = filedata->section_headers + 1;
7717
7718 printf (" %2.2d ", i);
7719
7720 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
7721 {
7722 if (!ELF_TBSS_SPECIAL (section, segment)
7723 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
7724 printf ("%s ", printable_section_name (filedata, section));
7725 }
7726
7727 putc ('\n',stdout);
7728 }
7729 }
7730
7731 filedata->dynamic_addr = dynamic_addr;
7732 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
7733 return;
7734
7735 no_headers:
7736 filedata->dynamic_addr = 0;
7737 filedata->dynamic_size = 1;
7738}
7739
7740
7741/* Find the file offset corresponding to VMA by using the program headers. */
7742
7743static int64_t
7744offset_from_vma (Filedata * filedata, uint64_t vma, uint64_t size)
7745{
7746 Elf_Internal_Phdr * seg;
7747
7748 if (! get_program_headers (filedata))
7749 {
7750 warn (_("Cannot interpret virtual addresses without program headers.\n"));
7751 return (long) vma;
7752 }
7753
7754 for (seg = filedata->program_headers;
7755 seg < filedata->program_headers + filedata->file_header.e_phnum;
7756 ++seg)
7757 {
7758 if (seg->p_type != PT_LOAD)
7759 continue;
7760
7761 if (vma >= (seg->p_vaddr & -seg->p_align)
7762 && vma + size <= seg->p_vaddr + seg->p_filesz)
7763 return vma - seg->p_vaddr + seg->p_offset;
7764 }
7765
7766 warn (_("Virtual address %#" PRIx64
7767 " not located in any PT_LOAD segment.\n"), vma);
7768 return vma;
7769}
7770
7771
7772/* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
7773 If PROBE is true, this is just a probe and we do not generate any error
7774 messages if the load fails. */
7775
7776static bool
7777get_32bit_section_headers (Filedata * filedata, bool probe)
7778{
7779 Elf32_External_Shdr * shdrs;
7780 Elf_Internal_Shdr * internal;
7781 unsigned int i;
7782 unsigned int size = filedata->file_header.e_shentsize;
7783 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
7784
7785 /* PR binutils/17531: Cope with unexpected section header sizes. */
7786 if (size == 0 || num == 0)
7787 return false;
7788
7789 /* The section header cannot be at the start of the file - that is
7790 where the ELF file header is located. A file with absolutely no
7791 sections in it will use a shoff of 0. */
7792 if (filedata->file_header.e_shoff == 0)
7793 return false;
7794
7795 if (size < sizeof * shdrs)
7796 {
7797 if (! probe)
7798 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
7799 return false;
7800 }
7801 if (!probe && size > sizeof * shdrs)
7802 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
7803
7804 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
7805 size, num,
7806 probe ? NULL : _("section headers"));
7807 if (shdrs == NULL)
7808 return false;
7809
7810 filedata->section_headers = (Elf_Internal_Shdr *)
7811 cmalloc (num, sizeof (Elf_Internal_Shdr));
7812 if (filedata->section_headers == NULL)
7813 {
7814 if (!probe)
7815 error (_("Out of memory reading %u section headers\n"), num);
7816 free (shdrs);
7817 return false;
7818 }
7819
7820 for (i = 0, internal = filedata->section_headers;
7821 i < num;
7822 i++, internal++)
7823 {
7824 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
7825 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
7826 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
7827 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
7828 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
7829 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
7830 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
7831 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
7832 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
7833 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
7834 if (!probe && internal->sh_link > num)
7835 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
7836 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
7837 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
7838 }
7839
7840 free (shdrs);
7841 return true;
7842}
7843
7844/* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
7845
7846static bool
7847get_64bit_section_headers (Filedata * filedata, bool probe)
7848{
7849 Elf64_External_Shdr * shdrs;
7850 Elf_Internal_Shdr * internal;
7851 unsigned int i;
7852 unsigned int size = filedata->file_header.e_shentsize;
7853 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
7854
7855 /* PR binutils/17531: Cope with unexpected section header sizes. */
7856 if (size == 0 || num == 0)
7857 return false;
7858
7859 /* The section header cannot be at the start of the file - that is
7860 where the ELF file header is located. A file with absolutely no
7861 sections in it will use a shoff of 0. */
7862 if (filedata->file_header.e_shoff == 0)
7863 return false;
7864
7865 if (size < sizeof * shdrs)
7866 {
7867 if (! probe)
7868 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
7869 return false;
7870 }
7871
7872 if (! probe && size > sizeof * shdrs)
7873 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
7874
7875 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
7876 filedata->file_header.e_shoff,
7877 size, num,
7878 probe ? NULL : _("section headers"));
7879 if (shdrs == NULL)
7880 return false;
7881
7882 filedata->section_headers = (Elf_Internal_Shdr *)
7883 cmalloc (num, sizeof (Elf_Internal_Shdr));
7884 if (filedata->section_headers == NULL)
7885 {
7886 if (! probe)
7887 error (_("Out of memory reading %u section headers\n"), num);
7888 free (shdrs);
7889 return false;
7890 }
7891
7892 for (i = 0, internal = filedata->section_headers;
7893 i < num;
7894 i++, internal++)
7895 {
7896 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
7897 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
7898 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
7899 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
7900 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
7901 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
7902 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
7903 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
7904 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
7905 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
7906 if (!probe && internal->sh_link > num)
7907 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
7908 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
7909 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
7910 }
7911
7912 free (shdrs);
7913 return true;
7914}
7915
7916static bool
7917get_section_headers (Filedata *filedata, bool probe)
7918{
7919 if (filedata->section_headers != NULL)
7920 return true;
7921
7922 if (is_32bit_elf)
7923 return get_32bit_section_headers (filedata, probe);
7924 else
7925 return get_64bit_section_headers (filedata, probe);
7926}
7927
7928static Elf_Internal_Sym *
7929get_32bit_elf_symbols (Filedata *filedata,
7930 Elf_Internal_Shdr *section,
7931 uint64_t *num_syms_return)
7932{
7933 uint64_t number = 0;
7934 Elf32_External_Sym * esyms = NULL;
7935 Elf_External_Sym_Shndx * shndx = NULL;
7936 Elf_Internal_Sym * isyms = NULL;
7937 Elf_Internal_Sym * psym;
7938 unsigned int j;
7939 elf_section_list * entry;
7940
7941 if (section->sh_size == 0)
7942 {
7943 if (num_syms_return != NULL)
7944 * num_syms_return = 0;
7945 return NULL;
7946 }
7947
7948 /* Run some sanity checks first. */
7949 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
7950 {
7951 error (_("Section %s has an invalid sh_entsize of %#" PRIx64 "\n"),
7952 printable_section_name (filedata, section),
7953 section->sh_entsize);
7954 goto exit_point;
7955 }
7956
7957 if (section->sh_size > filedata->file_size)
7958 {
7959 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
7960 printable_section_name (filedata, section),
7961 section->sh_size);
7962 goto exit_point;
7963 }
7964
7965 number = section->sh_size / section->sh_entsize;
7966
7967 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
7968 {
7969 error (_("Size (%#" PRIx64 ") of section %s "
7970 "is not a multiple of its sh_entsize (%#" PRIx64 ")\n"),
7971 section->sh_size,
7972 printable_section_name (filedata, section),
7973 section->sh_entsize);
7974 goto exit_point;
7975 }
7976
7977 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
7978 section->sh_size, _("symbols"));
7979 if (esyms == NULL)
7980 goto exit_point;
7981
7982 shndx = NULL;
7983 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
7984 {
7985 if (entry->hdr->sh_link != (size_t) (section - filedata->section_headers))
7986 continue;
7987
7988 if (shndx != NULL)
7989 {
7990 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
7991 free (shndx);
7992 }
7993
7994 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
7995 entry->hdr->sh_offset,
7996 1, entry->hdr->sh_size,
7997 _("symbol table section indices"));
7998 if (shndx == NULL)
7999 goto exit_point;
8000
8001 /* PR17531: file: heap-buffer-overflow */
8002 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
8003 {
8004 error (_("Index section %s has an sh_size of %#" PRIx64 " - expected %#" PRIx64 "\n"),
8005 printable_section_name (filedata, entry->hdr),
8006 entry->hdr->sh_size,
8007 section->sh_size);
8008 goto exit_point;
8009 }
8010 }
8011
8012 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
8013
8014 if (isyms == NULL)
8015 {
8016 error (_("Out of memory reading %" PRIu64 " symbols\n"), number);
8017 goto exit_point;
8018 }
8019
8020 for (j = 0, psym = isyms; j < number; j++, psym++)
8021 {
8022 psym->st_name = BYTE_GET (esyms[j].st_name);
8023 psym->st_value = BYTE_GET (esyms[j].st_value);
8024 psym->st_size = BYTE_GET (esyms[j].st_size);
8025 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
8026 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
8027 psym->st_shndx
8028 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
8029 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
8030 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
8031 psym->st_info = BYTE_GET (esyms[j].st_info);
8032 psym->st_other = BYTE_GET (esyms[j].st_other);
8033 }
8034
8035 exit_point:
8036 free (shndx);
8037 free (esyms);
8038
8039 if (num_syms_return != NULL)
8040 * num_syms_return = isyms == NULL ? 0 : number;
8041
8042 return isyms;
8043}
8044
8045static Elf_Internal_Sym *
8046get_64bit_elf_symbols (Filedata *filedata,
8047 Elf_Internal_Shdr *section,
8048 uint64_t *num_syms_return)
8049{
8050 uint64_t number = 0;
8051 Elf64_External_Sym * esyms = NULL;
8052 Elf_External_Sym_Shndx * shndx = NULL;
8053 Elf_Internal_Sym * isyms = NULL;
8054 Elf_Internal_Sym * psym;
8055 unsigned int j;
8056 elf_section_list * entry;
8057
8058 if (section->sh_size == 0)
8059 {
8060 if (num_syms_return != NULL)
8061 * num_syms_return = 0;
8062 return NULL;
8063 }
8064
8065 /* Run some sanity checks first. */
8066 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
8067 {
8068 error (_("Section %s has an invalid sh_entsize of %#" PRIx64 "\n"),
8069 printable_section_name (filedata, section),
8070 section->sh_entsize);
8071 goto exit_point;
8072 }
8073
8074 if (section->sh_size > filedata->file_size)
8075 {
8076 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
8077 printable_section_name (filedata, section),
8078 section->sh_size);
8079 goto exit_point;
8080 }
8081
8082 number = section->sh_size / section->sh_entsize;
8083
8084 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
8085 {
8086 error (_("Size (%#" PRIx64 ") of section %s "
8087 "is not a multiple of its sh_entsize (%#" PRIx64 ")\n"),
8088 section->sh_size,
8089 printable_section_name (filedata, section),
8090 section->sh_entsize);
8091 goto exit_point;
8092 }
8093
8094 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
8095 section->sh_size, _("symbols"));
8096 if (!esyms)
8097 goto exit_point;
8098
8099 shndx = NULL;
8100 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
8101 {
8102 if (entry->hdr->sh_link != (size_t) (section - filedata->section_headers))
8103 continue;
8104
8105 if (shndx != NULL)
8106 {
8107 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
8108 free (shndx);
8109 }
8110
8111 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
8112 entry->hdr->sh_offset,
8113 1, entry->hdr->sh_size,
8114 _("symbol table section indices"));
8115 if (shndx == NULL)
8116 goto exit_point;
8117
8118 /* PR17531: file: heap-buffer-overflow */
8119 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
8120 {
8121 error (_("Index section %s has an sh_size of %#" PRIx64 " - expected %#" PRIx64 "\n"),
8122 printable_section_name (filedata, entry->hdr),
8123 entry->hdr->sh_size,
8124 section->sh_size);
8125 goto exit_point;
8126 }
8127 }
8128
8129 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
8130
8131 if (isyms == NULL)
8132 {
8133 error (_("Out of memory reading %" PRIu64 " symbols\n"), number);
8134 goto exit_point;
8135 }
8136
8137 for (j = 0, psym = isyms; j < number; j++, psym++)
8138 {
8139 psym->st_name = BYTE_GET (esyms[j].st_name);
8140 psym->st_info = BYTE_GET (esyms[j].st_info);
8141 psym->st_other = BYTE_GET (esyms[j].st_other);
8142 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
8143
8144 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
8145 psym->st_shndx
8146 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
8147 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
8148 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
8149
8150 psym->st_value = BYTE_GET (esyms[j].st_value);
8151 psym->st_size = BYTE_GET (esyms[j].st_size);
8152 }
8153
8154 exit_point:
8155 free (shndx);
8156 free (esyms);
8157
8158 if (num_syms_return != NULL)
8159 * num_syms_return = isyms == NULL ? 0 : number;
8160
8161 return isyms;
8162}
8163
8164static Elf_Internal_Sym *
8165get_elf_symbols (Filedata *filedata,
8166 Elf_Internal_Shdr *section,
8167 uint64_t *num_syms_return)
8168{
8169 if (is_32bit_elf)
8170 return get_32bit_elf_symbols (filedata, section, num_syms_return);
8171 else
8172 return get_64bit_elf_symbols (filedata, section, num_syms_return);
8173}
8174
8175static const char *
8176get_elf_section_flags (Filedata * filedata, uint64_t sh_flags)
8177{
8178 static char buff[1024];
8179 char * p = buff;
8180 unsigned int field_size = is_32bit_elf ? 8 : 16;
8181 signed int sindex;
8182 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
8183 uint64_t os_flags = 0;
8184 uint64_t proc_flags = 0;
8185 uint64_t unknown_flags = 0;
8186 static const struct
8187 {
8188 const char * str;
8189 unsigned int len;
8190 }
8191 flags [] =
8192 {
8193 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
8194 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
8195 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
8196 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
8197 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
8198 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
8199 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
8200 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
8201 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
8202 /* 9 */ { STRING_COMMA_LEN ("TLS") },
8203 /* IA-64 specific. */
8204 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
8205 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
8206 /* IA-64 OpenVMS specific. */
8207 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
8208 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
8209 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
8210 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
8211 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
8212 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
8213 /* Generic. */
8214 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
8215 /* SPARC specific. */
8216 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
8217 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
8218 /* ARM specific. */
8219 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
8220 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
8221 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
8222 /* GNU specific. */
8223 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
8224 /* VLE specific. */
8225 /* 25 */ { STRING_COMMA_LEN ("VLE") },
8226 /* GNU specific. */
8227 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
8228 };
8229
8230 if (do_section_details)
8231 p += sprintf (p, "[%*.*lx]: ",
8232 field_size, field_size, (unsigned long) sh_flags);
8233
8234 while (sh_flags)
8235 {
8236 uint64_t flag;
8237
8238 flag = sh_flags & - sh_flags;
8239 sh_flags &= ~ flag;
8240
8241 if (do_section_details)
8242 {
8243 switch (flag)
8244 {
8245 case SHF_WRITE: sindex = 0; break;
8246 case SHF_ALLOC: sindex = 1; break;
8247 case SHF_EXECINSTR: sindex = 2; break;
8248 case SHF_MERGE: sindex = 3; break;
8249 case SHF_STRINGS: sindex = 4; break;
8250 case SHF_INFO_LINK: sindex = 5; break;
8251 case SHF_LINK_ORDER: sindex = 6; break;
8252 case SHF_OS_NONCONFORMING: sindex = 7; break;
8253 case SHF_GROUP: sindex = 8; break;
8254 case SHF_TLS: sindex = 9; break;
8255 case SHF_EXCLUDE: sindex = 18; break;
8256 case SHF_COMPRESSED: sindex = 20; break;
8257
8258 default:
8259 sindex = -1;
8260 switch (filedata->file_header.e_machine)
8261 {
8262 case EM_IA_64:
8263 if (flag == SHF_IA_64_SHORT)
8264 sindex = 10;
8265 else if (flag == SHF_IA_64_NORECOV)
8266 sindex = 11;
8267 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
8268 switch (flag)
8269 {
8270 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
8271 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
8272 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
8273 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
8274 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
8275 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
8276 default: break;
8277 }
8278 break;
8279
8280 case EM_386:
8281 case EM_IAMCU:
8282 case EM_X86_64:
8283 case EM_L1OM:
8284 case EM_K1OM:
8285 case EM_OLD_SPARCV9:
8286 case EM_SPARC32PLUS:
8287 case EM_SPARCV9:
8288 case EM_SPARC:
8289 if (flag == SHF_ORDERED)
8290 sindex = 19;
8291 break;
8292
8293 case EM_ARM:
8294 switch (flag)
8295 {
8296 case SHF_ENTRYSECT: sindex = 21; break;
8297 case SHF_ARM_PURECODE: sindex = 22; break;
8298 case SHF_COMDEF: sindex = 23; break;
8299 default: break;
8300 }
8301 break;
8302 case EM_PPC:
8303 if (flag == SHF_PPC_VLE)
8304 sindex = 25;
8305 break;
8306 default:
8307 break;
8308 }
8309
8310 switch (filedata->file_header.e_ident[EI_OSABI])
8311 {
8312 case ELFOSABI_GNU:
8313 case ELFOSABI_FREEBSD:
8314 if (flag == SHF_GNU_RETAIN)
8315 sindex = 26;
8316 /* Fall through */
8317 case ELFOSABI_NONE:
8318 if (flag == SHF_GNU_MBIND)
8319 /* We should not recognize SHF_GNU_MBIND for
8320 ELFOSABI_NONE, but binutils as of 2019-07-23 did
8321 not set the EI_OSABI header byte. */
8322 sindex = 24;
8323 break;
8324 default:
8325 break;
8326 }
8327 break;
8328 }
8329
8330 if (sindex != -1)
8331 {
8332 if (p != buff + field_size + 4)
8333 {
8334 if (size < (10 + 2))
8335 {
8336 warn (_("Internal error: not enough buffer room for section flag info"));
8337 return _("<unknown>");
8338 }
8339 size -= 2;
8340 *p++ = ',';
8341 *p++ = ' ';
8342 }
8343
8344 size -= flags [sindex].len;
8345 p = stpcpy (p, flags [sindex].str);
8346 }
8347 else if (flag & SHF_MASKOS)
8348 os_flags |= flag;
8349 else if (flag & SHF_MASKPROC)
8350 proc_flags |= flag;
8351 else
8352 unknown_flags |= flag;
8353 }
8354 else
8355 {
8356 switch (flag)
8357 {
8358 case SHF_WRITE: *p = 'W'; break;
8359 case SHF_ALLOC: *p = 'A'; break;
8360 case SHF_EXECINSTR: *p = 'X'; break;
8361 case SHF_MERGE: *p = 'M'; break;
8362 case SHF_STRINGS: *p = 'S'; break;
8363 case SHF_INFO_LINK: *p = 'I'; break;
8364 case SHF_LINK_ORDER: *p = 'L'; break;
8365 case SHF_OS_NONCONFORMING: *p = 'O'; break;
8366 case SHF_GROUP: *p = 'G'; break;
8367 case SHF_TLS: *p = 'T'; break;
8368 case SHF_EXCLUDE: *p = 'E'; break;
8369 case SHF_COMPRESSED: *p = 'C'; break;
8370
8371 default:
8372 if ((filedata->file_header.e_machine == EM_X86_64
8373 || filedata->file_header.e_machine == EM_L1OM
8374 || filedata->file_header.e_machine == EM_K1OM)
8375 && flag == SHF_X86_64_LARGE)
8376 *p = 'l';
8377 else if (filedata->file_header.e_machine == EM_ARM
8378 && flag == SHF_ARM_PURECODE)
8379 *p = 'y';
8380 else if (filedata->file_header.e_machine == EM_PPC
8381 && flag == SHF_PPC_VLE)
8382 *p = 'v';
8383 else if (flag & SHF_MASKOS)
8384 {
8385 switch (filedata->file_header.e_ident[EI_OSABI])
8386 {
8387 case ELFOSABI_GNU:
8388 case ELFOSABI_FREEBSD:
8389 if (flag == SHF_GNU_RETAIN)
8390 {
8391 *p = 'R';
8392 break;
8393 }
8394 /* Fall through */
8395 case ELFOSABI_NONE:
8396 if (flag == SHF_GNU_MBIND)
8397 {
8398 /* We should not recognize SHF_GNU_MBIND for
8399 ELFOSABI_NONE, but binutils as of 2019-07-23 did
8400 not set the EI_OSABI header byte. */
8401 *p = 'D';
8402 break;
8403 }
8404 /* Fall through */
8405 default:
8406 *p = 'o';
8407 sh_flags &= ~SHF_MASKOS;
8408 break;
8409 }
8410 }
8411 else if (flag & SHF_MASKPROC)
8412 {
8413 *p = 'p';
8414 sh_flags &= ~ SHF_MASKPROC;
8415 }
8416 else
8417 *p = 'x';
8418 break;
8419 }
8420 p++;
8421 }
8422 }
8423
8424 if (do_section_details)
8425 {
8426 if (os_flags)
8427 {
8428 if (p != buff + field_size + 4)
8429 {
8430 if (size < 2 + 5 + field_size + 1)
8431 {
8432 warn (_("Internal error: not enough buffer room for section flag info"));
8433 return _("<unknown>");
8434 }
8435 size -= 2;
8436 *p++ = ',';
8437 *p++ = ' ';
8438 }
8439 size -= 5 + field_size;
8440 p += sprintf (p, "OS (%*.*lx)", field_size, field_size,
8441 (unsigned long) os_flags);
8442 }
8443 if (proc_flags)
8444 {
8445 if (p != buff + field_size + 4)
8446 {
8447 if (size < 2 + 7 + field_size + 1)
8448 {
8449 warn (_("Internal error: not enough buffer room for section flag info"));
8450 return _("<unknown>");
8451 }
8452 size -= 2;
8453 *p++ = ',';
8454 *p++ = ' ';
8455 }
8456 size -= 7 + field_size;
8457 p += sprintf (p, "PROC (%*.*lx)", field_size, field_size,
8458 (unsigned long) proc_flags);
8459 }
8460 if (unknown_flags)
8461 {
8462 if (p != buff + field_size + 4)
8463 {
8464 if (size < 2 + 10 + field_size + 1)
8465 {
8466 warn (_("Internal error: not enough buffer room for section flag info"));
8467 return _("<unknown>");
8468 }
8469 size -= 2;
8470 *p++ = ',';
8471 *p++ = ' ';
8472 }
8473 size -= 10 + field_size;
8474 p += sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
8475 (unsigned long) unknown_flags);
8476 }
8477 }
8478
8479 *p = '\0';
8480 return buff;
8481}
8482
8483static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
8484get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf,
8485 uint64_t size)
8486{
8487 if (is_32bit_elf)
8488 {
8489 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
8490
8491 if (size < sizeof (* echdr))
8492 {
8493 error (_("Compressed section is too small even for a compression header\n"));
8494 return 0;
8495 }
8496
8497 chdr->ch_type = BYTE_GET (echdr->ch_type);
8498 chdr->ch_size = BYTE_GET (echdr->ch_size);
8499 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
8500 return sizeof (*echdr);
8501 }
8502 else
8503 {
8504 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
8505
8506 if (size < sizeof (* echdr))
8507 {
8508 error (_("Compressed section is too small even for a compression header\n"));
8509 return 0;
8510 }
8511
8512 chdr->ch_type = BYTE_GET (echdr->ch_type);
8513 chdr->ch_size = BYTE_GET (echdr->ch_size);
8514 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
8515 return sizeof (*echdr);
8516 }
8517}
8518
8519static bool
8520process_section_headers (Filedata * filedata)
8521{
8522 Elf_Internal_Shdr * section;
8523 unsigned int i;
8524
8525 if (filedata->file_header.e_shnum == 0)
8526 {
8527 /* PR binutils/12467. */
8528 if (filedata->file_header.e_shoff != 0)
8529 {
8530 warn (_("possibly corrupt ELF file header - it has a non-zero"
8531 " section header offset, but no section headers\n"));
8532 return false;
8533 }
8534 else if (do_sections)
8535 printf (_("\nThere are no sections in this file.\n"));
8536
8537 return true;
8538 }
8539
8540 if (do_sections && !do_header)
8541 {
8542 if (filedata->is_separate && process_links)
8543 printf (_("In linked file '%s': "),
8544 printable_string (filedata->file_name, 0));
8545 if (! filedata->is_separate || process_links)
8546 printf (ngettext ("There is %d section header, "
8547 "starting at offset %#" PRIx64 ":\n",
8548 "There are %d section headers, "
8549 "starting at offset %#" PRIx64 ":\n",
8550 filedata->file_header.e_shnum),
8551 filedata->file_header.e_shnum,
8552 filedata->file_header.e_shoff);
8553 }
8554
8555 if (!get_section_headers (filedata, false))
8556 return false;
8557
8558 /* Read in the string table, so that we have names to display. */
8559 if (filedata->string_table == NULL
8560 && filedata->file_header.e_shstrndx != SHN_UNDEF
8561 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
8562 {
8563 section = filedata->section_headers + filedata->file_header.e_shstrndx;
8564
8565 if (section->sh_size != 0)
8566 {
8567 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
8568 1, section->sh_size,
8569 _("string table"));
8570
8571 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
8572 }
8573 }
8574
8575 /* Scan the sections for the dynamic symbol table
8576 and dynamic string table and debug sections. */
8577 eh_addr_size = is_32bit_elf ? 4 : 8;
8578 switch (filedata->file_header.e_machine)
8579 {
8580 case EM_MIPS:
8581 case EM_MIPS_RS3_LE:
8582 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
8583 FDE addresses. However, the ABI also has a semi-official ILP32
8584 variant for which the normal FDE address size rules apply.
8585
8586 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
8587 section, where XX is the size of longs in bits. Unfortunately,
8588 earlier compilers provided no way of distinguishing ILP32 objects
8589 from LP64 objects, so if there's any doubt, we should assume that
8590 the official LP64 form is being used. */
8591 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == EF_MIPS_ABI_EABI64
8592 && find_section (filedata, ".gcc_compiled_long32") == NULL)
8593 eh_addr_size = 8;
8594 break;
8595
8596 case EM_H8_300:
8597 case EM_H8_300H:
8598 switch (filedata->file_header.e_flags & EF_H8_MACH)
8599 {
8600 case E_H8_MACH_H8300:
8601 case E_H8_MACH_H8300HN:
8602 case E_H8_MACH_H8300SN:
8603 case E_H8_MACH_H8300SXN:
8604 eh_addr_size = 2;
8605 break;
8606 case E_H8_MACH_H8300H:
8607 case E_H8_MACH_H8300S:
8608 case E_H8_MACH_H8300SX:
8609 eh_addr_size = 4;
8610 break;
8611 }
8612 break;
8613
8614 case EM_M32C_OLD:
8615 case EM_M32C:
8616 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
8617 {
8618 case EF_M32C_CPU_M16C:
8619 eh_addr_size = 2;
8620 break;
8621 }
8622 break;
8623 }
8624
8625#define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
8626 do \
8627 { \
8628 uint64_t expected_entsize = is_32bit_elf ? size32 : size64; \
8629 if (section->sh_entsize != expected_entsize) \
8630 { \
8631 error (_("Section %d has invalid sh_entsize of %" PRIx64 "\n"), \
8632 i, section->sh_entsize); \
8633 error (_("(Using the expected size of %" PRIx64 " for the rest of this dump)\n"), \
8634 expected_entsize); \
8635 section->sh_entsize = expected_entsize; \
8636 } \
8637 } \
8638 while (0)
8639
8640#define CHECK_ENTSIZE(section, i, type) \
8641 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
8642 sizeof (Elf64_External_##type))
8643
8644 for (i = 0, section = filedata->section_headers;
8645 i < filedata->file_header.e_shnum;
8646 i++, section++)
8647 {
8648 const char *name = printable_section_name (filedata, section);
8649
8650 /* Run some sanity checks on the headers and
8651 possibly fill in some file data as well. */
8652 switch (section->sh_type)
8653 {
8654 case SHT_DYNSYM:
8655 if (filedata->dynamic_symbols != NULL)
8656 {
8657 error (_("File contains multiple dynamic symbol tables\n"));
8658 continue;
8659 }
8660
8661 CHECK_ENTSIZE (section, i, Sym);
8662 filedata->dynamic_symbols
8663 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
8664 filedata->dynamic_symtab_section = section;
8665 break;
8666
8667 case SHT_STRTAB:
8668 if (streq (name, ".dynstr"))
8669 {
8670 if (filedata->dynamic_strings != NULL)
8671 {
8672 error (_("File contains multiple dynamic string tables\n"));
8673 continue;
8674 }
8675
8676 filedata->dynamic_strings
8677 = (char *) get_data (NULL, filedata, section->sh_offset,
8678 1, section->sh_size, _("dynamic strings"));
8679 filedata->dynamic_strings_length
8680 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
8681 filedata->dynamic_strtab_section = section;
8682 }
8683 break;
8684
8685 case SHT_SYMTAB_SHNDX:
8686 {
8687 elf_section_list * entry = xmalloc (sizeof * entry);
8688
8689 entry->hdr = section;
8690 entry->next = filedata->symtab_shndx_list;
8691 filedata->symtab_shndx_list = entry;
8692 }
8693 break;
8694
8695 case SHT_SYMTAB:
8696 CHECK_ENTSIZE (section, i, Sym);
8697 break;
8698
8699 case SHT_GROUP:
8700 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
8701 break;
8702
8703 case SHT_REL:
8704 CHECK_ENTSIZE (section, i, Rel);
8705 if (do_checks && section->sh_size == 0)
8706 warn (_("Section '%s': zero-sized relocation section\n"), name);
8707 break;
8708
8709 case SHT_RELA:
8710 CHECK_ENTSIZE (section, i, Rela);
8711 if (do_checks && section->sh_size == 0)
8712 warn (_("Section '%s': zero-sized relocation section\n"), name);
8713 break;
8714
8715 case SHT_RELR:
8716 CHECK_ENTSIZE (section, i, Relr);
8717 break;
8718
8719 case SHT_NOTE:
8720 case SHT_PROGBITS:
8721 case SHT_GNU_SFRAME:
8722 /* Having a zero sized section is not illegal according to the
8723 ELF standard, but it might be an indication that something
8724 is wrong. So issue a warning if we are running in lint mode. */
8725 if (do_checks && section->sh_size == 0)
8726 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
8727 break;
8728
8729 default:
8730 break;
8731 }
8732
8733 if ((do_debugging || do_debug_info || do_debug_abbrevs
8734 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
8735 || do_debug_aranges || do_debug_frames || do_debug_macinfo
8736 || do_debug_str || do_debug_str_offsets || do_debug_loc
8737 || do_debug_ranges
8738 || do_debug_addr || do_debug_cu_index || do_debug_links)
8739 && (startswith (name, ".debug_")
8740 || startswith (name, ".zdebug_")))
8741 {
8742 if (name[1] == 'z')
8743 name += sizeof (".zdebug_") - 1;
8744 else
8745 name += sizeof (".debug_") - 1;
8746
8747 if (do_debugging
8748 || (do_debug_info && startswith (name, "info"))
8749 || (do_debug_info && startswith (name, "types"))
8750 || (do_debug_abbrevs && startswith (name, "abbrev"))
8751 || (do_debug_lines && strcmp (name, "line") == 0)
8752 || (do_debug_lines && startswith (name, "line."))
8753 || (do_debug_pubnames && startswith (name, "pubnames"))
8754 || (do_debug_pubtypes && startswith (name, "pubtypes"))
8755 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
8756 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
8757 || (do_debug_aranges && startswith (name, "aranges"))
8758 || (do_debug_ranges && startswith (name, "ranges"))
8759 || (do_debug_ranges && startswith (name, "rnglists"))
8760 || (do_debug_frames && startswith (name, "frame"))
8761 || (do_debug_macinfo && startswith (name, "macinfo"))
8762 || (do_debug_macinfo && startswith (name, "macro"))
8763 || (do_debug_str && startswith (name, "str"))
8764 || (do_debug_links && startswith (name, "sup"))
8765 || (do_debug_str_offsets && startswith (name, "str_offsets"))
8766 || (do_debug_loc && startswith (name, "loc"))
8767 || (do_debug_loc && startswith (name, "loclists"))
8768 || (do_debug_addr && startswith (name, "addr"))
8769 || (do_debug_cu_index && startswith (name, "cu_index"))
8770 || (do_debug_cu_index && startswith (name, "tu_index"))
8771 )
8772 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8773 }
8774 /* Linkonce section to be combined with .debug_info at link time. */
8775 else if ((do_debugging || do_debug_info)
8776 && startswith (name, ".gnu.linkonce.wi."))
8777 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8778 else if (do_debug_frames && streq (name, ".eh_frame"))
8779 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8780 else if (do_debug_frames && streq (name, ".eh_frame_hdr"))
8781 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8782 else if (do_gdb_index && (streq (name, ".gdb_index")
8783 || streq (name, ".debug_names")))
8784 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8785 /* Trace sections for Itanium VMS. */
8786 else if ((do_debugging || do_trace_info || do_trace_abbrevs
8787 || do_trace_aranges)
8788 && startswith (name, ".trace_"))
8789 {
8790 name += sizeof (".trace_") - 1;
8791
8792 if (do_debugging
8793 || (do_trace_info && streq (name, "info"))
8794 || (do_trace_abbrevs && streq (name, "abbrev"))
8795 || (do_trace_aranges && streq (name, "aranges"))
8796 )
8797 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8798 }
8799 else if ((do_debugging || do_debug_links)
8800 && (startswith (name, ".gnu_debuglink")
8801 || startswith (name, ".gnu_debugaltlink")))
8802 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
8803 }
8804
8805 if (! do_sections)
8806 return true;
8807
8808 if (filedata->is_separate && ! process_links)
8809 return true;
8810
8811 if (filedata->is_separate)
8812 printf (_("\nSection Headers in linked file '%s':\n"),
8813 printable_string (filedata->file_name, 0));
8814 else if (filedata->file_header.e_shnum > 1)
8815 printf (_("\nSection Headers:\n"));
8816 else
8817 printf (_("\nSection Header:\n"));
8818
8819 if (is_32bit_elf)
8820 {
8821 if (do_section_details)
8822 {
8823 printf (_(" [Nr] Name\n"));
8824 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
8825 }
8826 else
8827 printf
8828 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
8829 }
8830 else if (do_wide)
8831 {
8832 if (do_section_details)
8833 {
8834 printf (_(" [Nr] Name\n"));
8835 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
8836 }
8837 else
8838 printf
8839 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
8840 }
8841 else
8842 {
8843 if (do_section_details)
8844 {
8845 printf (_(" [Nr] Name\n"));
8846 printf (_(" Type Address Offset Link\n"));
8847 printf (_(" Size EntSize Info Align\n"));
8848 }
8849 else
8850 {
8851 printf (_(" [Nr] Name Type Address Offset\n"));
8852 printf (_(" Size EntSize Flags Link Info Align\n"));
8853 }
8854 }
8855
8856 if (do_section_details)
8857 printf (_(" Flags\n"));
8858
8859 for (i = 0, section = filedata->section_headers;
8860 i < filedata->file_header.e_shnum;
8861 i++, section++)
8862 {
8863 /* Run some sanity checks on the section header. */
8864
8865 /* Check the sh_link field. */
8866 switch (section->sh_type)
8867 {
8868 case SHT_REL:
8869 case SHT_RELR:
8870 case SHT_RELA:
8871 if (section->sh_link == 0
8872 && (filedata->file_header.e_type == ET_EXEC
8873 || filedata->file_header.e_type == ET_DYN))
8874 /* A dynamic relocation section where all entries use a
8875 zero symbol index need not specify a symtab section. */
8876 break;
8877 /* Fall through. */
8878 case SHT_SYMTAB_SHNDX:
8879 case SHT_GROUP:
8880 case SHT_HASH:
8881 case SHT_GNU_HASH:
8882 case SHT_GNU_versym:
8883 if (section->sh_link == 0
8884 || section->sh_link >= filedata->file_header.e_shnum
8885 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
8886 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
8887 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
8888 i, section->sh_link);
8889 break;
8890
8891 case SHT_DYNAMIC:
8892 case SHT_SYMTAB:
8893 case SHT_DYNSYM:
8894 case SHT_GNU_verneed:
8895 case SHT_GNU_verdef:
8896 case SHT_GNU_LIBLIST:
8897 if (section->sh_link == 0
8898 || section->sh_link >= filedata->file_header.e_shnum
8899 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
8900 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
8901 i, section->sh_link);
8902 break;
8903
8904 case SHT_INIT_ARRAY:
8905 case SHT_FINI_ARRAY:
8906 case SHT_PREINIT_ARRAY:
8907 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
8908 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
8909 i, section->sh_link);
8910 break;
8911
8912 default:
8913 /* FIXME: Add support for target specific section types. */
8914#if 0 /* Currently we do not check other section types as there are too
8915 many special cases. Stab sections for example have a type
8916 of SHT_PROGBITS but an sh_link field that links to the .stabstr
8917 section. */
8918 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
8919 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
8920 i, section->sh_link);
8921#endif
8922 break;
8923 }
8924
8925 /* Check the sh_info field. */
8926 switch (section->sh_type)
8927 {
8928 case SHT_REL:
8929 case SHT_RELA:
8930 if (section->sh_info == 0
8931 && (filedata->file_header.e_type == ET_EXEC
8932 || filedata->file_header.e_type == ET_DYN))
8933 /* Dynamic relocations apply to segments, so they do not
8934 need to specify the section they relocate. */
8935 break;
8936 if (section->sh_info == 0
8937 || section->sh_info >= filedata->file_header.e_shnum
8938 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
8939 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
8940 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
8941 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
8942 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
8943 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
8944 /* FIXME: Are other section types valid ? */
8945 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
8946 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
8947 i, section->sh_info);
8948 break;
8949
8950 case SHT_DYNAMIC:
8951 case SHT_HASH:
8952 case SHT_SYMTAB_SHNDX:
8953 case SHT_INIT_ARRAY:
8954 case SHT_FINI_ARRAY:
8955 case SHT_PREINIT_ARRAY:
8956 if (section->sh_info != 0)
8957 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
8958 i, section->sh_info);
8959 break;
8960
8961 case SHT_GROUP:
8962 case SHT_SYMTAB:
8963 case SHT_DYNSYM:
8964 /* A symbol index - we assume that it is valid. */
8965 break;
8966
8967 default:
8968 /* FIXME: Add support for target specific section types. */
8969 if (section->sh_type == SHT_NOBITS)
8970 /* NOBITS section headers with non-zero sh_info fields can be
8971 created when a binary is stripped of everything but its debug
8972 information. The stripped sections have their headers
8973 preserved but their types set to SHT_NOBITS. So do not check
8974 this type of section. */
8975 ;
8976 else if (section->sh_flags & SHF_INFO_LINK)
8977 {
8978 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
8979 warn (_("[%2u]: Expected link to another section in info field"), i);
8980 }
8981 else if (section->sh_type < SHT_LOOS
8982 && (section->sh_flags & SHF_GNU_MBIND) == 0
8983 && section->sh_info != 0)
8984 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
8985 i, section->sh_info);
8986 break;
8987 }
8988
8989 /* Check the sh_size field. */
8990 if (section->sh_size > filedata->file_size
8991 && section->sh_type != SHT_NOBITS
8992 && section->sh_type != SHT_NULL
8993 && section->sh_type < SHT_LOOS)
8994 warn (_("Size of section %u is larger than the entire file!\n"), i);
8995
8996 printf (" [%2u] ", i);
8997 if (do_section_details)
8998 printf ("%s\n ", printable_section_name (filedata, section));
8999 else
9000 print_symbol_name (-17, printable_section_name (filedata, section));
9001
9002 printf (do_wide ? " %-15s " : " %-15.15s ",
9003 get_section_type_name (filedata, section->sh_type));
9004
9005 if (is_32bit_elf)
9006 {
9007 const char * link_too_big = NULL;
9008
9009 print_vma (section->sh_addr, LONG_HEX);
9010
9011 printf ( " %6.6lx %6.6lx %2.2lx",
9012 (unsigned long) section->sh_offset,
9013 (unsigned long) section->sh_size,
9014 (unsigned long) section->sh_entsize);
9015
9016 if (do_section_details)
9017 fputs (" ", stdout);
9018 else
9019 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
9020
9021 if (section->sh_link >= filedata->file_header.e_shnum)
9022 {
9023 link_too_big = "";
9024 /* The sh_link value is out of range. Normally this indicates
9025 an error but it can have special values in Solaris binaries. */
9026 switch (filedata->file_header.e_machine)
9027 {
9028 case EM_386:
9029 case EM_IAMCU:
9030 case EM_X86_64:
9031 case EM_L1OM:
9032 case EM_K1OM:
9033 case EM_OLD_SPARCV9:
9034 case EM_SPARC32PLUS:
9035 case EM_SPARCV9:
9036 case EM_SPARC:
9037 if (section->sh_link == (SHN_BEFORE & 0xffff))
9038 link_too_big = "BEFORE";
9039 else if (section->sh_link == (SHN_AFTER & 0xffff))
9040 link_too_big = "AFTER";
9041 break;
9042 default:
9043 break;
9044 }
9045 }
9046
9047 if (do_section_details)
9048 {
9049 if (link_too_big != NULL && * link_too_big)
9050 printf ("<%s> ", link_too_big);
9051 else
9052 printf ("%2u ", section->sh_link);
9053 printf ("%3u %2lu\n", section->sh_info,
9054 (unsigned long) section->sh_addralign);
9055 }
9056 else
9057 printf ("%2u %3u %2lu\n",
9058 section->sh_link,
9059 section->sh_info,
9060 (unsigned long) section->sh_addralign);
9061
9062 if (link_too_big && ! * link_too_big)
9063 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
9064 i, section->sh_link);
9065 }
9066 else if (do_wide)
9067 {
9068 print_vma (section->sh_addr, LONG_HEX);
9069
9070 if ((long) section->sh_offset == section->sh_offset)
9071 printf (" %6.6lx", (unsigned long) section->sh_offset);
9072 else
9073 {
9074 putchar (' ');
9075 print_vma (section->sh_offset, LONG_HEX);
9076 }
9077
9078 if ((unsigned long) section->sh_size == section->sh_size)
9079 printf (" %6.6lx", (unsigned long) section->sh_size);
9080 else
9081 {
9082 putchar (' ');
9083 print_vma (section->sh_size, LONG_HEX);
9084 }
9085
9086 if ((unsigned long) section->sh_entsize == section->sh_entsize)
9087 printf (" %2.2lx", (unsigned long) section->sh_entsize);
9088 else
9089 {
9090 putchar (' ');
9091 print_vma (section->sh_entsize, LONG_HEX);
9092 }
9093
9094 if (do_section_details)
9095 fputs (" ", stdout);
9096 else
9097 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
9098
9099 printf ("%2u %3u ", section->sh_link, section->sh_info);
9100
9101 if ((unsigned long) section->sh_addralign == section->sh_addralign)
9102 printf ("%2lu\n", (unsigned long) section->sh_addralign);
9103 else
9104 {
9105 print_vma (section->sh_addralign, DEC);
9106 putchar ('\n');
9107 }
9108 }
9109 else if (do_section_details)
9110 {
9111 putchar (' ');
9112 print_vma (section->sh_addr, LONG_HEX);
9113 if ((long) section->sh_offset == section->sh_offset)
9114 printf (" %16.16lx", (unsigned long) section->sh_offset);
9115 else
9116 {
9117 printf (" ");
9118 print_vma (section->sh_offset, LONG_HEX);
9119 }
9120 printf (" %u\n ", section->sh_link);
9121 print_vma (section->sh_size, LONG_HEX);
9122 putchar (' ');
9123 print_vma (section->sh_entsize, LONG_HEX);
9124
9125 printf (" %-16u %lu\n",
9126 section->sh_info,
9127 (unsigned long) section->sh_addralign);
9128 }
9129 else
9130 {
9131 putchar (' ');
9132 print_vma (section->sh_addr, LONG_HEX);
9133 if ((long) section->sh_offset == section->sh_offset)
9134 printf (" %8.8lx", (unsigned long) section->sh_offset);
9135 else
9136 {
9137 printf (" ");
9138 print_vma (section->sh_offset, LONG_HEX);
9139 }
9140 printf ("\n ");
9141 print_vma (section->sh_size, LONG_HEX);
9142 printf (" ");
9143 print_vma (section->sh_entsize, LONG_HEX);
9144
9145 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
9146
9147 printf (" %2u %3u %lu\n",
9148 section->sh_link,
9149 section->sh_info,
9150 (unsigned long) section->sh_addralign);
9151 }
9152
9153 if (do_section_details)
9154 {
9155 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
9156 if ((section->sh_flags & SHF_COMPRESSED) != 0)
9157 {
9158 /* Minimum section size is 12 bytes for 32-bit compression
9159 header + 12 bytes for compressed data header. */
9160 unsigned char buf[24];
9161
9162 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
9163 if (get_data (&buf, filedata, section->sh_offset, 1,
9164 sizeof (buf), _("compression header")))
9165 {
9166 Elf_Internal_Chdr chdr;
9167
9168 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
9169 printf (_(" [<corrupt>]\n"));
9170 else
9171 {
9172 if (chdr.ch_type == ch_compress_zlib)
9173 printf (" ZLIB, ");
9174 else if (chdr.ch_type == ch_compress_zstd)
9175 printf (" ZSTD, ");
9176 else
9177 printf (_(" [<unknown>: 0x%x], "),
9178 chdr.ch_type);
9179 print_vma (chdr.ch_size, LONG_HEX);
9180 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
9181 }
9182 }
9183 }
9184 }
9185 }
9186
9187 if (!do_section_details)
9188 {
9189 /* The ordering of the letters shown here matches the ordering of the
9190 corresponding SHF_xxx values, and hence the order in which these
9191 letters will be displayed to the user. */
9192 printf (_("Key to Flags:\n\
9193 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
9194 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
9195 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
9196 switch (filedata->file_header.e_ident[EI_OSABI])
9197 {
9198 case ELFOSABI_GNU:
9199 case ELFOSABI_FREEBSD:
9200 printf (_("R (retain), "));
9201 /* Fall through */
9202 case ELFOSABI_NONE:
9203 printf (_("D (mbind), "));
9204 break;
9205 default:
9206 break;
9207 }
9208 if (filedata->file_header.e_machine == EM_X86_64
9209 || filedata->file_header.e_machine == EM_L1OM
9210 || filedata->file_header.e_machine == EM_K1OM)
9211 printf (_("l (large), "));
9212 else if (filedata->file_header.e_machine == EM_ARM)
9213 printf (_("y (purecode), "));
9214 else if (filedata->file_header.e_machine == EM_PPC)
9215 printf (_("v (VLE), "));
9216 printf ("p (processor specific)\n");
9217 }
9218
9219 return true;
9220}
9221
9222static bool
9223get_symtab (Filedata * filedata,
9224 Elf_Internal_Shdr * symsec,
9225 Elf_Internal_Sym ** symtab,
9226 uint64_t * nsyms,
9227 char ** strtab,
9228 uint64_t * strtablen)
9229{
9230 *strtab = NULL;
9231 *strtablen = 0;
9232 *symtab = get_elf_symbols (filedata, symsec, nsyms);
9233
9234 if (*symtab == NULL)
9235 return false;
9236
9237 if (symsec->sh_link != 0)
9238 {
9239 Elf_Internal_Shdr *strsec;
9240
9241 if (symsec->sh_link >= filedata->file_header.e_shnum)
9242 {
9243 error (_("Bad sh_link in symbol table section\n"));
9244 free (*symtab);
9245 *symtab = NULL;
9246 *nsyms = 0;
9247 return false;
9248 }
9249
9250 strsec = filedata->section_headers + symsec->sh_link;
9251
9252 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
9253 1, strsec->sh_size, _("string table"));
9254 if (*strtab == NULL)
9255 {
9256 free (*symtab);
9257 *symtab = NULL;
9258 *nsyms = 0;
9259 return false;
9260 }
9261 *strtablen = strsec->sh_size;
9262 }
9263 return true;
9264}
9265
9266static const char *
9267get_group_flags (unsigned int flags)
9268{
9269 static char buff[128];
9270
9271 if (flags == 0)
9272 return "";
9273 else if (flags == GRP_COMDAT)
9274 return "COMDAT ";
9275
9276 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
9277 flags,
9278 flags & GRP_MASKOS ? _("<OS specific>") : "",
9279 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
9280 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
9281 ? _("<unknown>") : ""));
9282
9283 return buff;
9284}
9285
9286static bool
9287process_section_groups (Filedata * filedata)
9288{
9289 Elf_Internal_Shdr * section;
9290 unsigned int i;
9291 struct group * group;
9292 Elf_Internal_Shdr * symtab_sec;
9293 Elf_Internal_Shdr * strtab_sec;
9294 Elf_Internal_Sym * symtab;
9295 uint64_t num_syms;
9296 char * strtab;
9297 size_t strtab_size;
9298
9299 /* Don't process section groups unless needed. */
9300 if (!do_unwind && !do_section_groups)
9301 return true;
9302
9303 if (filedata->file_header.e_shnum == 0)
9304 {
9305 if (do_section_groups)
9306 {
9307 if (filedata->is_separate)
9308 printf (_("\nThere are no sections group in linked file '%s'.\n"),
9309 printable_string (filedata->file_name, 0));
9310 else
9311 printf (_("\nThere are no section groups in this file.\n"));
9312 }
9313 return true;
9314 }
9315
9316 if (filedata->section_headers == NULL)
9317 {
9318 error (_("Section headers are not available!\n"));
9319 /* PR 13622: This can happen with a corrupt ELF header. */
9320 return false;
9321 }
9322
9323 filedata->section_headers_groups
9324 = (struct group **) calloc (filedata->file_header.e_shnum,
9325 sizeof (struct group *));
9326
9327 if (filedata->section_headers_groups == NULL)
9328 {
9329 error (_("Out of memory reading %u section group headers\n"),
9330 filedata->file_header.e_shnum);
9331 return false;
9332 }
9333
9334 /* Scan the sections for the group section. */
9335 filedata->group_count = 0;
9336 for (i = 0, section = filedata->section_headers;
9337 i < filedata->file_header.e_shnum;
9338 i++, section++)
9339 if (section->sh_type == SHT_GROUP)
9340 filedata->group_count++;
9341
9342 if (filedata->group_count == 0)
9343 {
9344 if (do_section_groups)
9345 {
9346 if (filedata->is_separate)
9347 printf (_("\nThere are no section groups in linked file '%s'.\n"),
9348 printable_string (filedata->file_name, 0));
9349 else
9350 printf (_("\nThere are no section groups in this file.\n"));
9351 }
9352
9353 return true;
9354 }
9355
9356 filedata->section_groups = (struct group *) calloc (filedata->group_count,
9357 sizeof (struct group));
9358
9359 if (filedata->section_groups == NULL)
9360 {
9361 error (_("Out of memory reading %zu groups\n"), filedata->group_count);
9362 return false;
9363 }
9364
9365 symtab_sec = NULL;
9366 strtab_sec = NULL;
9367 symtab = NULL;
9368 num_syms = 0;
9369 strtab = NULL;
9370 strtab_size = 0;
9371
9372 if (filedata->is_separate)
9373 printf (_("Section groups in linked file '%s'\n"),
9374 printable_string (filedata->file_name, 0));
9375
9376 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
9377 i < filedata->file_header.e_shnum;
9378 i++, section++)
9379 {
9380 if (section->sh_type == SHT_GROUP)
9381 {
9382 const char * name = printable_section_name (filedata, section);
9383 const char * group_name;
9384 unsigned char * start;
9385 unsigned char * indices;
9386 unsigned int entry, j, size;
9387 Elf_Internal_Shdr * sec;
9388 Elf_Internal_Sym * sym;
9389
9390 /* Get the symbol table. */
9391 if (section->sh_link >= filedata->file_header.e_shnum
9392 || ((sec = filedata->section_headers + section->sh_link)->sh_type
9393 != SHT_SYMTAB))
9394 {
9395 error (_("Bad sh_link in group section `%s'\n"), name);
9396 continue;
9397 }
9398
9399 if (symtab_sec != sec)
9400 {
9401 symtab_sec = sec;
9402 free (symtab);
9403 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
9404 }
9405
9406 if (symtab == NULL)
9407 {
9408 error (_("Corrupt header in group section `%s'\n"), name);
9409 continue;
9410 }
9411
9412 if (section->sh_info >= num_syms)
9413 {
9414 error (_("Bad sh_info in group section `%s'\n"), name);
9415 continue;
9416 }
9417
9418 sym = symtab + section->sh_info;
9419
9420 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9421 {
9422 if (sym->st_shndx == 0
9423 || sym->st_shndx >= filedata->file_header.e_shnum)
9424 {
9425 error (_("Bad sh_info in group section `%s'\n"), name);
9426 continue;
9427 }
9428
9429 group_name = printable_section_name (filedata,
9430 filedata->section_headers
9431 + sym->st_shndx);
9432 strtab_sec = NULL;
9433 free (strtab);
9434 strtab = NULL;
9435 strtab_size = 0;
9436 }
9437 else
9438 {
9439 /* Get the string table. */
9440 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
9441 {
9442 strtab_sec = NULL;
9443 free (strtab);
9444 strtab = NULL;
9445 strtab_size = 0;
9446 }
9447 else if (strtab_sec
9448 != (sec = filedata->section_headers + symtab_sec->sh_link))
9449 {
9450 strtab_sec = sec;
9451 free (strtab);
9452
9453 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
9454 1, strtab_sec->sh_size,
9455 _("string table"));
9456 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
9457 }
9458 group_name = sym->st_name < strtab_size
9459 ? strtab + sym->st_name : _("<corrupt>");
9460 }
9461
9462 /* PR 17531: file: loop. */
9463 if (section->sh_entsize > section->sh_size)
9464 {
9465 error (_("Section %s has sh_entsize (%#" PRIx64 ")"
9466 " which is larger than its size (%#" PRIx64 ")\n"),
9467 printable_section_name (filedata, section),
9468 section->sh_entsize,
9469 section->sh_size);
9470 continue;
9471 }
9472
9473 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
9474 1, section->sh_size,
9475 _("section data"));
9476 if (start == NULL)
9477 continue;
9478
9479 indices = start;
9480 size = (section->sh_size / section->sh_entsize) - 1;
9481 entry = byte_get (indices, 4);
9482 indices += 4;
9483
9484 if (do_section_groups)
9485 {
9486 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
9487 get_group_flags (entry), i, name, group_name, size);
9488
9489 printf (_(" [Index] Name\n"));
9490 }
9491
9492 group->group_index = i;
9493
9494 for (j = 0; j < size; j++)
9495 {
9496 struct group_list * g;
9497
9498 entry = byte_get (indices, 4);
9499 indices += 4;
9500
9501 if (entry >= filedata->file_header.e_shnum)
9502 {
9503 static unsigned num_group_errors = 0;
9504
9505 if (num_group_errors ++ < 10)
9506 {
9507 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
9508 entry, i, filedata->file_header.e_shnum - 1);
9509 if (num_group_errors == 10)
9510 warn (_("Further error messages about overlarge group section indices suppressed\n"));
9511 }
9512 continue;
9513 }
9514
9515 if (filedata->section_headers_groups [entry] != NULL)
9516 {
9517 if (entry)
9518 {
9519 static unsigned num_errs = 0;
9520
9521 if (num_errs ++ < 10)
9522 {
9523 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
9524 entry, i,
9525 filedata->section_headers_groups [entry]->group_index);
9526 if (num_errs == 10)
9527 warn (_("Further error messages about already contained group sections suppressed\n"));
9528 }
9529 continue;
9530 }
9531 else
9532 {
9533 /* Intel C/C++ compiler may put section 0 in a
9534 section group. We just warn it the first time
9535 and ignore it afterwards. */
9536 static bool warned = false;
9537 if (!warned)
9538 {
9539 error (_("section 0 in group section [%5u]\n"),
9540 filedata->section_headers_groups [entry]->group_index);
9541 warned = true;
9542 }
9543 }
9544 }
9545
9546 filedata->section_headers_groups [entry] = group;
9547
9548 if (do_section_groups)
9549 {
9550 sec = filedata->section_headers + entry;
9551 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
9552 }
9553
9554 g = (struct group_list *) xmalloc (sizeof (struct group_list));
9555 g->section_index = entry;
9556 g->next = group->root;
9557 group->root = g;
9558 }
9559
9560 free (start);
9561
9562 group++;
9563 }
9564 }
9565
9566 free (symtab);
9567 free (strtab);
9568 return true;
9569}
9570
9571/* Data used to display dynamic fixups. */
9572
9573struct ia64_vms_dynfixup
9574{
9575 uint64_t needed_ident; /* Library ident number. */
9576 uint64_t needed; /* Index in the dstrtab of the library name. */
9577 uint64_t fixup_needed; /* Index of the library. */
9578 uint64_t fixup_rela_cnt; /* Number of fixups. */
9579 uint64_t fixup_rela_off; /* Fixups offset in the dynamic segment. */
9580};
9581
9582/* Data used to display dynamic relocations. */
9583
9584struct ia64_vms_dynimgrela
9585{
9586 uint64_t img_rela_cnt; /* Number of relocations. */
9587 uint64_t img_rela_off; /* Reloc offset in the dynamic segment. */
9588};
9589
9590/* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
9591 library). */
9592
9593static bool
9594dump_ia64_vms_dynamic_fixups (Filedata * filedata,
9595 struct ia64_vms_dynfixup * fixup,
9596 const char * strtab,
9597 unsigned int strtab_sz)
9598{
9599 Elf64_External_VMS_IMAGE_FIXUP * imfs;
9600 size_t i;
9601 const char * lib_name;
9602
9603 imfs = get_data (NULL, filedata,
9604 filedata->dynamic_addr + fixup->fixup_rela_off,
9605 sizeof (*imfs), fixup->fixup_rela_cnt,
9606 _("dynamic section image fixups"));
9607 if (!imfs)
9608 return false;
9609
9610 if (fixup->needed < strtab_sz)
9611 lib_name = strtab + fixup->needed;
9612 else
9613 {
9614 warn (_("corrupt library name index of %#" PRIx64
9615 " found in dynamic entry"), fixup->needed);
9616 lib_name = "???";
9617 }
9618
9619 printf (_("\nImage fixups for needed library #%" PRId64
9620 ": %s - ident: %" PRIx64 "\n"),
9621 fixup->fixup_needed, lib_name, fixup->needed_ident);
9622 printf
9623 (_("Seg Offset Type SymVec DataType\n"));
9624
9625 for (i = 0; i < (size_t) fixup->fixup_rela_cnt; i++)
9626 {
9627 unsigned int type;
9628 const char *rtype;
9629
9630 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
9631 printf ("%016" PRIx64 " ", BYTE_GET (imfs [i].fixup_offset));
9632 type = BYTE_GET (imfs [i].type);
9633 rtype = elf_ia64_reloc_type (type);
9634 if (rtype == NULL)
9635 printf ("0x%08x ", type);
9636 else
9637 printf ("%-32s ", rtype);
9638 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
9639 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
9640 }
9641
9642 free (imfs);
9643 return true;
9644}
9645
9646/* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
9647
9648static bool
9649dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
9650{
9651 Elf64_External_VMS_IMAGE_RELA *imrs;
9652 size_t i;
9653
9654 imrs = get_data (NULL, filedata,
9655 filedata->dynamic_addr + imgrela->img_rela_off,
9656 sizeof (*imrs), imgrela->img_rela_cnt,
9657 _("dynamic section image relocations"));
9658 if (!imrs)
9659 return false;
9660
9661 printf (_("\nImage relocs\n"));
9662 printf
9663 (_("Seg Offset Type Addend Seg Sym Off\n"));
9664
9665 for (i = 0; i < (size_t) imgrela->img_rela_cnt; i++)
9666 {
9667 unsigned int type;
9668 const char *rtype;
9669
9670 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
9671 printf ("%08" PRIx64 " ", BYTE_GET (imrs [i].rela_offset));
9672 type = BYTE_GET (imrs [i].type);
9673 rtype = elf_ia64_reloc_type (type);
9674 if (rtype == NULL)
9675 printf ("0x%08x ", type);
9676 else
9677 printf ("%-31s ", rtype);
9678 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
9679 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
9680 printf ("%08" PRIx64 "\n", BYTE_GET (imrs [i].sym_offset));
9681 }
9682
9683 free (imrs);
9684 return true;
9685}
9686
9687/* Display IA-64 OpenVMS dynamic relocations and fixups. */
9688
9689static bool
9690process_ia64_vms_dynamic_relocs (Filedata * filedata)
9691{
9692 struct ia64_vms_dynfixup fixup;
9693 struct ia64_vms_dynimgrela imgrela;
9694 Elf_Internal_Dyn *entry;
9695 uint64_t strtab_off = 0;
9696 uint64_t strtab_sz = 0;
9697 char *strtab = NULL;
9698 bool res = true;
9699
9700 memset (&fixup, 0, sizeof (fixup));
9701 memset (&imgrela, 0, sizeof (imgrela));
9702
9703 /* Note: the order of the entries is specified by the OpenVMS specs. */
9704 for (entry = filedata->dynamic_section;
9705 entry < filedata->dynamic_section + filedata->dynamic_nent;
9706 entry++)
9707 {
9708 switch (entry->d_tag)
9709 {
9710 case DT_IA_64_VMS_STRTAB_OFFSET:
9711 strtab_off = entry->d_un.d_val;
9712 break;
9713 case DT_STRSZ:
9714 strtab_sz = entry->d_un.d_val;
9715 if (strtab == NULL)
9716 strtab = get_data (NULL, filedata,
9717 filedata->dynamic_addr + strtab_off,
9718 1, strtab_sz, _("dynamic string section"));
9719 if (strtab == NULL)
9720 strtab_sz = 0;
9721 break;
9722
9723 case DT_IA_64_VMS_NEEDED_IDENT:
9724 fixup.needed_ident = entry->d_un.d_val;
9725 break;
9726 case DT_NEEDED:
9727 fixup.needed = entry->d_un.d_val;
9728 break;
9729 case DT_IA_64_VMS_FIXUP_NEEDED:
9730 fixup.fixup_needed = entry->d_un.d_val;
9731 break;
9732 case DT_IA_64_VMS_FIXUP_RELA_CNT:
9733 fixup.fixup_rela_cnt = entry->d_un.d_val;
9734 break;
9735 case DT_IA_64_VMS_FIXUP_RELA_OFF:
9736 fixup.fixup_rela_off = entry->d_un.d_val;
9737 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
9738 res = false;
9739 break;
9740 case DT_IA_64_VMS_IMG_RELA_CNT:
9741 imgrela.img_rela_cnt = entry->d_un.d_val;
9742 break;
9743 case DT_IA_64_VMS_IMG_RELA_OFF:
9744 imgrela.img_rela_off = entry->d_un.d_val;
9745 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
9746 res = false;
9747 break;
9748
9749 default:
9750 break;
9751 }
9752 }
9753
9754 free (strtab);
9755
9756 return res;
9757}
9758
9759static struct
9760{
9761 const char * name;
9762 int reloc;
9763 int size;
9764 relocation_type rel_type;
9765}
9766 dynamic_relocations [] =
9767{
9768 { "REL", DT_REL, DT_RELSZ, reltype_rel },
9769 { "RELA", DT_RELA, DT_RELASZ, reltype_rela },
9770 { "RELR", DT_RELR, DT_RELRSZ, reltype_relr },
9771 { "PLT", DT_JMPREL, DT_PLTRELSZ, reltype_unknown }
9772};
9773
9774static relocation_type
9775rel_type_from_sh_type (unsigned int sh_type)
9776{
9777 switch (sh_type)
9778 {
9779 case SHT_RELA: return reltype_rela;
9780 case SHT_REL: return reltype_rel;
9781 case SHT_RELR: return reltype_relr;
9782 default: return reltype_unknown;
9783 }
9784}
9785
9786static bool
9787display_relocations (Elf_Internal_Shdr * section,
9788 Filedata * filedata,
9789 bool dump_reloc)
9790{
9791 relocation_type rel_type = rel_type_from_sh_type (section->sh_type);
9792
9793 if (rel_type == reltype_unknown)
9794 return false;
9795
9796 uint64_t rel_size = section->sh_size;
9797
9798 if (rel_size == 0)
9799 return false;
9800
9801 if (dump_reloc)
9802 {
9803 if (filedata->is_separate)
9804 printf (_("\nIn linked file '%s' relocation section "),
9805 printable_string (filedata->file_name, 0));
9806 else
9807 printf (_("\nRelocation section "));
9808
9809 if (filedata->string_table == NULL)
9810 printf ("%d", section->sh_name);
9811 else
9812 printf ("'%s'", printable_section_name (filedata, section));
9813 }
9814
9815 uint64_t rel_offset = section->sh_offset;
9816 uint64_t num_rela = rel_size / section->sh_entsize;
9817 uint64_t num_reloc;
9818 uint64_t *relrs = NULL;
9819
9820 if (rel_type == reltype_relr)
9821 {
9822 /* Just stating the 'number of entries' in a RELR section can be
9823 misleading, since this is not the number of locations relocated, but
9824 the number of words in the compressed RELR format. So also provide
9825 the number of locations affected. */
9826
9827 num_reloc = count_relr_relocations (filedata, section, &relrs);
9828
9829 if (dump_reloc)
9830 {
9831 printf (_(" at offset %#" PRIx64), rel_offset);
9832 printf (ngettext (" contains %" PRIu64 " entry which relocates",
9833 " contains %" PRIu64 " entries which relocate",
9834 num_rela), num_rela);
9835 printf (ngettext (" %" PRIu64 " location:\n",
9836 " %" PRIu64 " locations:\n",
9837 num_reloc), num_reloc);
9838 }
9839 }
9840 else
9841 {
9842 num_reloc = num_rela;
9843 if (dump_reloc)
9844 printf (ngettext (" at offset %#" PRIx64
9845 " contains %" PRIu64 " entry:\n",
9846 " at offset %#" PRIx64
9847 " contains %" PRIu64 " entries:\n",
9848 num_rela),
9849 rel_offset, num_rela);
9850 }
9851
9852 update_all_relocations (num_reloc);
9853
9854 Elf_Internal_Shdr * symsec;
9855 Elf_Internal_Sym * symtab = NULL;
9856 uint64_t nsyms = 0;
9857 uint64_t strtablen = 0;
9858 char * strtab = NULL;
9859
9860 if (section->sh_link == 0
9861 || section->sh_link >= filedata->file_header.e_shnum)
9862 {
9863 /* Symbol data not available.
9864 This can happen, especially with RELR relocs.
9865 See if there is a .symtab section present.
9866 If so then use it. */
9867 symsec = find_section_by_name (filedata, ".symtab");
9868 }
9869 else
9870 {
9871 symsec = filedata->section_headers + section->sh_link;
9872
9873 if (symsec->sh_type != SHT_SYMTAB
9874 && symsec->sh_type != SHT_DYNSYM)
9875 {
9876 free (relrs);
9877 return false;
9878 }
9879 }
9880
9881 if (symsec != NULL
9882 && !get_symtab (filedata, symsec, &symtab, &nsyms, &strtab, &strtablen))
9883 {
9884 free (relrs);
9885 return false;
9886 }
9887
9888 bool res;
9889
9890 if (rel_type == reltype_relr)
9891 {
9892 res = dump_relr_relocations (filedata, section->sh_size,
9893 section->sh_entsize,
9894 section->sh_offset,
9895 relrs,
9896 symtab, nsyms, strtab, strtablen,
9897 dump_reloc);
9898 free (relrs);
9899 }
9900 else
9901 res = dump_relocations (filedata, rel_offset, rel_size,
9902 symtab, nsyms, strtab, strtablen,
9903 rel_type,
9904 symsec == NULL
9905 ? false : symsec->sh_type == SHT_DYNSYM,
9906 dump_reloc);
9907 free (strtab);
9908 free (symtab);
9909
9910 return res;
9911}
9912
9913/* Process the reloc section. */
9914
9915static bool
9916process_relocs (Filedata * filedata)
9917{
9918 uint64_t rel_size;
9919 uint64_t rel_offset;
9920 unsigned int rel_entsz;
9921
9922 if (!do_reloc && !do_got_section_contents)
9923 return true;
9924
9925 if (do_using_dynamic)
9926 {
9927 relocation_type rel_type;
9928 const char * name;
9929 const char * entsz_name;
9930 bool has_dynamic_reloc;
9931 unsigned int i;
9932
9933 has_dynamic_reloc = false;
9934
9935 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
9936 {
9937 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
9938
9939 if (!rel_size)
9940 continue;
9941
9942 has_dynamic_reloc = true;
9943
9944 rel_type = dynamic_relocations [i].rel_type;
9945 name = dynamic_relocations [i].name;
9946 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
9947
9948 if (rel_type == reltype_unknown)
9949 {
9950 if (dynamic_relocations [i].reloc != DT_JMPREL)
9951 abort ();
9952 switch (filedata->dynamic_info[DT_PLTREL])
9953 {
9954 case DT_REL:
9955 rel_type = reltype_rel;
9956 break;
9957 case DT_RELA:
9958 rel_type = reltype_rela;
9959 break;
9960 }
9961 }
9962
9963 switch (rel_type)
9964 {
9965 default:
9966 abort ();
9967 case reltype_rel:
9968 rel_entsz = filedata->dynamic_info[DT_RELENT];
9969 entsz_name = "DT_RELENT";
9970 break;
9971 case reltype_rela:
9972 rel_entsz = filedata->dynamic_info[DT_RELAENT];
9973 entsz_name = "DT_RELAENT";
9974 break;
9975 case reltype_relr:
9976 rel_entsz = filedata->dynamic_info[DT_RELRENT];
9977 entsz_name = "DT_RELRENT";
9978 break;
9979 }
9980
9981 if (do_reloc)
9982 {
9983 if (filedata->is_separate)
9984 printf
9985 (_("\nIn linked file '%s' section '%s' at offset"
9986 "%#" PRIx64 " contains %" PRId64 " bytes:\n"),
9987 filedata->file_name, name, rel_offset, rel_size);
9988 else
9989 printf
9990 (_("\n'%s' relocation section at offset %#" PRIx64
9991 " contains %" PRId64 " bytes:\n"),
9992 name, rel_offset, rel_size);
9993 }
9994
9995 if (rel_type == reltype_relr)
9996 dump_relr_relocations (filedata,
9997 filedata->dynamic_info[DT_RELRSZ],
9998 filedata->dynamic_info[DT_RELRENT],
9999 filedata->dynamic_info[DT_RELR],
10000 NULL,
10001 filedata->dynamic_symbols,
10002 filedata->num_dynamic_syms,
10003 filedata->dynamic_strings,
10004 filedata->dynamic_strings_length,
10005 do_reloc);
10006 else
10007 {
10008 if (rel_entsz == 0)
10009 {
10010 printf (_("<missing or corrupt dynamic tag: %s>\n"),
10011 entsz_name);
10012 continue;
10013 }
10014
10015 update_all_relocations (rel_size / rel_entsz);
10016
10017 dump_relocations (filedata,
10018 offset_from_vma (filedata, rel_offset,
10019 rel_size),
10020 rel_size,
10021 filedata->dynamic_symbols,
10022 filedata->num_dynamic_syms,
10023 filedata->dynamic_strings,
10024 filedata->dynamic_strings_length,
10025 rel_type, true /* is_dynamic */,
10026 do_reloc);
10027 }
10028 }
10029
10030 if (is_ia64_vms (filedata)
10031 && process_ia64_vms_dynamic_relocs (filedata))
10032 has_dynamic_reloc = true;
10033
10034 if (do_reloc && ! has_dynamic_reloc)
10035 {
10036 if (filedata->is_separate)
10037 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
10038 filedata->file_name);
10039 else
10040 printf (_("\nThere are no dynamic relocations in this file.\n"));
10041 }
10042 }
10043 else
10044 {
10045 Elf_Internal_Shdr * section;
10046 size_t i;
10047 bool found = false;
10048
10049 for (i = 0, section = filedata->section_headers;
10050 i < filedata->file_header.e_shnum;
10051 i++, section++)
10052 {
10053 if (display_relocations (section, filedata, do_reloc))
10054 found = true;
10055 }
10056
10057 if (do_reloc && ! found)
10058 {
10059 /* Users sometimes forget the -D option, so try to be helpful. */
10060 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
10061 {
10062 if (filedata->dynamic_info[dynamic_relocations [i].size])
10063 {
10064 if (filedata->is_separate)
10065 printf (_("\nThere are no static relocations in linked file '%s'."),
10066 filedata->file_name);
10067 else
10068 printf (_("\nThere are no static relocations in this file."));
10069 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
10070
10071 break;
10072 }
10073 }
10074 if (i == ARRAY_SIZE (dynamic_relocations))
10075 {
10076 if (filedata->is_separate)
10077 printf (_("\nThere are no relocations in linked file '%s'.\n"),
10078 filedata->file_name);
10079 else
10080 printf (_("\nThere are no relocations in this file.\n"));
10081 }
10082 }
10083 }
10084
10085 return true;
10086}
10087
10088/* An absolute address consists of a section and an offset. If the
10089 section is NULL, the offset itself is the address, otherwise, the
10090 address equals to LOAD_ADDRESS(section) + offset. */
10091
10092struct absaddr
10093{
10094 unsigned short section;
10095 uint64_t offset;
10096};
10097
10098/* Find the nearest symbol at or below ADDR. Returns the symbol
10099 name, if found, and the offset from the symbol to ADDR. */
10100
10101static void
10102find_symbol_for_address (Filedata *filedata,
10103 Elf_Internal_Sym *symtab,
10104 uint64_t nsyms,
10105 const char *strtab,
10106 uint64_t strtab_size,
10107 struct absaddr addr,
10108 const char **symname,
10109 uint64_t *offset)
10110{
10111 uint64_t dist = 0x100000;
10112 Elf_Internal_Sym * sym;
10113 Elf_Internal_Sym * beg;
10114 Elf_Internal_Sym * end;
10115 Elf_Internal_Sym * best = NULL;
10116
10117 REMOVE_ARCH_BITS (addr.offset);
10118 beg = symtab;
10119 end = symtab + nsyms;
10120
10121 while (beg < end)
10122 {
10123 uint64_t value;
10124
10125 sym = beg + (end - beg) / 2;
10126
10127 value = sym->st_value;
10128 REMOVE_ARCH_BITS (value);
10129
10130 if (sym->st_name != 0
10131 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
10132 && addr.offset >= value
10133 && addr.offset - value < dist)
10134 {
10135 best = sym;
10136 dist = addr.offset - value;
10137 if (!dist)
10138 break;
10139 }
10140
10141 if (addr.offset < value)
10142 end = sym;
10143 else
10144 beg = sym + 1;
10145 }
10146
10147 if (best)
10148 {
10149 *symname = (best->st_name >= strtab_size
10150 ? _("<corrupt>") : strtab + best->st_name);
10151 *offset = dist;
10152 return;
10153 }
10154
10155 *symname = NULL;
10156 *offset = addr.offset;
10157}
10158
10159/* Process the unwind section. */
10160
10161#include "unwind-ia64.h"
10162
10163struct ia64_unw_table_entry
10164{
10165 struct absaddr start;
10166 struct absaddr end;
10167 struct absaddr info;
10168};
10169
10170struct ia64_unw_aux_info
10171{
10172 struct ia64_unw_table_entry * table; /* Unwind table. */
10173 uint64_t table_len; /* Length of unwind table. */
10174 unsigned char * info; /* Unwind info. */
10175 uint64_t info_size; /* Size of unwind info. */
10176 uint64_t info_addr; /* Starting address of unwind info. */
10177 uint64_t seg_base; /* Starting address of segment. */
10178 Elf_Internal_Sym * symtab; /* The symbol table. */
10179 uint64_t nsyms; /* Number of symbols. */
10180 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
10181 uint64_t nfuns; /* Number of entries in funtab. */
10182 char * strtab; /* The string table. */
10183 uint64_t strtab_size; /* Size of string table. */
10184};
10185
10186static bool
10187dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
10188{
10189 struct ia64_unw_table_entry * tp;
10190 size_t j, nfuns;
10191 int in_body;
10192 bool res = true;
10193
10194 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
10195 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
10196 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
10197 aux->funtab[nfuns++] = aux->symtab[j];
10198 aux->nfuns = nfuns;
10199 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
10200
10201 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
10202 {
10203 uint64_t stamp;
10204 uint64_t offset;
10205 const unsigned char * dp;
10206 const unsigned char * head;
10207 const unsigned char * end;
10208 const char * procname;
10209
10210 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
10211 aux->strtab_size, tp->start, &procname, &offset);
10212
10213 fputs ("\n<", stdout);
10214
10215 if (procname)
10216 {
10217 fputs (procname, stdout);
10218
10219 if (offset)
10220 printf ("+%" PRIx64, offset);
10221 }
10222
10223 fputs (">: [", stdout);
10224 print_vma (tp->start.offset, PREFIX_HEX);
10225 fputc ('-', stdout);
10226 print_vma (tp->end.offset, PREFIX_HEX);
10227 printf ("], info at +0x%" PRIx64 "\n",
10228 tp->info.offset - aux->seg_base);
10229
10230 /* PR 17531: file: 86232b32. */
10231 if (aux->info == NULL)
10232 continue;
10233
10234 offset = tp->info.offset;
10235 if (tp->info.section)
10236 {
10237 if (tp->info.section >= filedata->file_header.e_shnum)
10238 {
10239 warn (_("Invalid section %u in table entry %td\n"),
10240 tp->info.section, tp - aux->table);
10241 res = false;
10242 continue;
10243 }
10244 offset += filedata->section_headers[tp->info.section].sh_addr;
10245 }
10246 offset -= aux->info_addr;
10247 /* PR 17531: file: 0997b4d1. */
10248 if (offset >= aux->info_size
10249 || aux->info_size - offset < 8)
10250 {
10251 warn (_("Invalid offset %" PRIx64 " in table entry %td\n"),
10252 tp->info.offset, tp - aux->table);
10253 res = false;
10254 continue;
10255 }
10256
10257 head = aux->info + offset;
10258 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
10259
10260 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
10261 (unsigned) UNW_VER (stamp),
10262 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
10263 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
10264 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
10265 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
10266
10267 if (UNW_VER (stamp) != 1)
10268 {
10269 printf (_("\tUnknown version.\n"));
10270 continue;
10271 }
10272
10273 in_body = 0;
10274 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
10275 /* PR 17531: file: 16ceda89. */
10276 if (end > aux->info + aux->info_size)
10277 end = aux->info + aux->info_size;
10278 for (dp = head + 8; dp < end;)
10279 dp = unw_decode (dp, in_body, & in_body, end);
10280 }
10281
10282 free (aux->funtab);
10283
10284 return res;
10285}
10286
10287static bool
10288slurp_ia64_unwind_table (Filedata * filedata,
10289 struct ia64_unw_aux_info * aux,
10290 Elf_Internal_Shdr * sec)
10291{
10292 uint64_t size, nrelas, i;
10293 Elf_Internal_Phdr * seg;
10294 struct ia64_unw_table_entry * tep;
10295 Elf_Internal_Shdr * relsec;
10296 Elf_Internal_Rela * rela;
10297 Elf_Internal_Rela * rp;
10298 unsigned char * table;
10299 unsigned char * tp;
10300 Elf_Internal_Sym * sym;
10301 const char * relname;
10302
10303 aux->table_len = 0;
10304
10305 /* First, find the starting address of the segment that includes
10306 this section: */
10307
10308 if (filedata->file_header.e_phnum)
10309 {
10310 if (! get_program_headers (filedata))
10311 return false;
10312
10313 for (seg = filedata->program_headers;
10314 seg < filedata->program_headers + filedata->file_header.e_phnum;
10315 ++seg)
10316 {
10317 if (seg->p_type != PT_LOAD)
10318 continue;
10319
10320 if (sec->sh_addr >= seg->p_vaddr
10321 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
10322 {
10323 aux->seg_base = seg->p_vaddr;
10324 break;
10325 }
10326 }
10327 }
10328
10329 /* Second, build the unwind table from the contents of the unwind section: */
10330 size = sec->sh_size;
10331 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
10332 _("unwind table"));
10333 if (!table)
10334 return false;
10335
10336 aux->table_len = size / (3 * eh_addr_size);
10337 aux->table = (struct ia64_unw_table_entry *)
10338 xcmalloc (aux->table_len, sizeof (aux->table[0]));
10339 tep = aux->table;
10340
10341 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
10342 {
10343 tep->start.section = SHN_UNDEF;
10344 tep->end.section = SHN_UNDEF;
10345 tep->info.section = SHN_UNDEF;
10346 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
10347 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
10348 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
10349 tep->start.offset += aux->seg_base;
10350 tep->end.offset += aux->seg_base;
10351 tep->info.offset += aux->seg_base;
10352 }
10353 free (table);
10354
10355 /* Third, apply any relocations to the unwind table: */
10356 for (relsec = filedata->section_headers;
10357 relsec < filedata->section_headers + filedata->file_header.e_shnum;
10358 ++relsec)
10359 {
10360 if (relsec->sh_type != SHT_RELA
10361 || relsec->sh_info >= filedata->file_header.e_shnum
10362 || filedata->section_headers + relsec->sh_info != sec)
10363 continue;
10364
10365 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
10366 & rela, & nrelas))
10367 {
10368 free (aux->table);
10369 aux->table = NULL;
10370 aux->table_len = 0;
10371 return false;
10372 }
10373
10374 for (rp = rela; rp < rela + nrelas; ++rp)
10375 {
10376 unsigned int sym_ndx;
10377 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
10378 relname = elf_ia64_reloc_type (r_type);
10379
10380 /* PR 17531: file: 9fa67536. */
10381 if (relname == NULL)
10382 {
10383 warn (_("Skipping unknown relocation type: %u\n"), r_type);
10384 continue;
10385 }
10386
10387 if (! startswith (relname, "R_IA64_SEGREL"))
10388 {
10389 warn (_("Skipping unexpected relocation type: %s\n"), relname);
10390 continue;
10391 }
10392
10393 i = rp->r_offset / (3 * eh_addr_size);
10394
10395 /* PR 17531: file: 5bc8d9bf. */
10396 if (i >= aux->table_len)
10397 {
10398 warn (_("Skipping reloc with overlarge offset: %#" PRIx64 "\n"),
10399 i);
10400 continue;
10401 }
10402
10403 sym_ndx = get_reloc_symindex (rp->r_info);
10404 if (sym_ndx >= aux->nsyms)
10405 {
10406 warn (_("Skipping reloc with invalid symbol index: %u\n"),
10407 sym_ndx);
10408 continue;
10409 }
10410 sym = aux->symtab + sym_ndx;
10411
10412 switch (rp->r_offset / eh_addr_size % 3)
10413 {
10414 case 0:
10415 aux->table[i].start.section = sym->st_shndx;
10416 aux->table[i].start.offset = rp->r_addend + sym->st_value;
10417 break;
10418 case 1:
10419 aux->table[i].end.section = sym->st_shndx;
10420 aux->table[i].end.offset = rp->r_addend + sym->st_value;
10421 break;
10422 case 2:
10423 aux->table[i].info.section = sym->st_shndx;
10424 aux->table[i].info.offset = rp->r_addend + sym->st_value;
10425 break;
10426 default:
10427 break;
10428 }
10429 }
10430
10431 free (rela);
10432 }
10433
10434 return true;
10435}
10436
10437static bool
10438ia64_process_unwind (Filedata * filedata)
10439{
10440 Elf_Internal_Shdr * sec;
10441 Elf_Internal_Shdr * unwsec = NULL;
10442 uint64_t i, unwcount = 0, unwstart = 0;
10443 struct ia64_unw_aux_info aux;
10444 bool res = true;
10445
10446 memset (& aux, 0, sizeof (aux));
10447
10448 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10449 {
10450 if (sec->sh_type == SHT_SYMTAB)
10451 {
10452 if (aux.symtab)
10453 {
10454 error (_("Multiple symbol tables encountered\n"));
10455 free (aux.symtab);
10456 aux.symtab = NULL;
10457 free (aux.strtab);
10458 aux.strtab = NULL;
10459 }
10460 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
10461 &aux.strtab, &aux.strtab_size))
10462 return false;
10463 }
10464 else if (sec->sh_type == SHT_IA_64_UNWIND)
10465 unwcount++;
10466 }
10467
10468 if (!unwcount)
10469 printf (_("\nThere are no unwind sections in this file.\n"));
10470
10471 while (unwcount-- > 0)
10472 {
10473 const char *suffix;
10474 size_t len, len2;
10475
10476 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
10477 i < filedata->file_header.e_shnum; ++i, ++sec)
10478 if (sec->sh_type == SHT_IA_64_UNWIND)
10479 {
10480 unwsec = sec;
10481 break;
10482 }
10483 /* We have already counted the number of SHT_IA64_UNWIND
10484 sections so the loop above should never fail. */
10485 assert (unwsec != NULL);
10486
10487 unwstart = i + 1;
10488 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
10489
10490 if ((unwsec->sh_flags & SHF_GROUP) != 0)
10491 {
10492 /* We need to find which section group it is in. */
10493 struct group_list * g;
10494
10495 if (filedata->section_headers_groups == NULL
10496 || filedata->section_headers_groups[i] == NULL)
10497 i = filedata->file_header.e_shnum;
10498 else
10499 {
10500 g = filedata->section_headers_groups[i]->root;
10501
10502 for (; g != NULL; g = g->next)
10503 {
10504 sec = filedata->section_headers + g->section_index;
10505
10506 if (section_name_valid (filedata, sec)
10507 && streq (section_name (filedata, sec),
10508 ELF_STRING_ia64_unwind_info))
10509 break;
10510 }
10511
10512 if (g == NULL)
10513 i = filedata->file_header.e_shnum;
10514 }
10515 }
10516 else if (section_name_valid (filedata, unwsec)
10517 && startswith (section_name (filedata, unwsec),
10518 ELF_STRING_ia64_unwind_once))
10519 {
10520 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
10521 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
10522 suffix = section_name (filedata, unwsec) + len;
10523 for (i = 0, sec = filedata->section_headers;
10524 i < filedata->file_header.e_shnum;
10525 ++i, ++sec)
10526 if (section_name_valid (filedata, sec)
10527 && startswith (section_name (filedata, sec),
10528 ELF_STRING_ia64_unwind_info_once)
10529 && streq (section_name (filedata, sec) + len2, suffix))
10530 break;
10531 }
10532 else
10533 {
10534 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
10535 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
10536 len = sizeof (ELF_STRING_ia64_unwind) - 1;
10537 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
10538 suffix = "";
10539 if (section_name_valid (filedata, unwsec)
10540 && startswith (section_name (filedata, unwsec),
10541 ELF_STRING_ia64_unwind))
10542 suffix = section_name (filedata, unwsec) + len;
10543 for (i = 0, sec = filedata->section_headers;
10544 i < filedata->file_header.e_shnum;
10545 ++i, ++sec)
10546 if (section_name_valid (filedata, sec)
10547 && startswith (section_name (filedata, sec),
10548 ELF_STRING_ia64_unwind_info)
10549 && streq (section_name (filedata, sec) + len2, suffix))
10550 break;
10551 }
10552
10553 if (i == filedata->file_header.e_shnum)
10554 {
10555 printf (_("\nCould not find unwind info section for "));
10556
10557 if (filedata->string_table == NULL)
10558 printf ("%d", unwsec->sh_name);
10559 else
10560 printf ("'%s'", printable_section_name (filedata, unwsec));
10561 }
10562 else
10563 {
10564 aux.info_addr = sec->sh_addr;
10565 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
10566 sec->sh_size,
10567 _("unwind info"));
10568 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
10569
10570 printf (_("\nUnwind section "));
10571
10572 if (filedata->string_table == NULL)
10573 printf ("%d", unwsec->sh_name);
10574 else
10575 printf ("'%s'", printable_section_name (filedata, unwsec));
10576
10577 printf (_(" at offset %#" PRIx64 " contains %" PRIu64 " entries:\n"),
10578 unwsec->sh_offset,
10579 unwsec->sh_size / (3 * eh_addr_size));
10580
10581 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
10582 && aux.table_len > 0)
10583 dump_ia64_unwind (filedata, & aux);
10584
10585 free ((char *) aux.table);
10586 free ((char *) aux.info);
10587 aux.table = NULL;
10588 aux.info = NULL;
10589 }
10590 }
10591
10592 free (aux.symtab);
10593 free ((char *) aux.strtab);
10594
10595 return res;
10596}
10597
10598struct hppa_unw_table_entry
10599{
10600 struct absaddr start;
10601 struct absaddr end;
10602 unsigned int Cannot_unwind:1; /* 0 */
10603 unsigned int Millicode:1; /* 1 */
10604 unsigned int Millicode_save_sr0:1; /* 2 */
10605 unsigned int Region_description:2; /* 3..4 */
10606 unsigned int reserved1:1; /* 5 */
10607 unsigned int Entry_SR:1; /* 6 */
10608 unsigned int Entry_FR:4; /* Number saved 7..10 */
10609 unsigned int Entry_GR:5; /* Number saved 11..15 */
10610 unsigned int Args_stored:1; /* 16 */
10611 unsigned int Variable_Frame:1; /* 17 */
10612 unsigned int Separate_Package_Body:1; /* 18 */
10613 unsigned int Frame_Extension_Millicode:1; /* 19 */
10614 unsigned int Stack_Overflow_Check:1; /* 20 */
10615 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
10616 unsigned int Ada_Region:1; /* 22 */
10617 unsigned int cxx_info:1; /* 23 */
10618 unsigned int cxx_try_catch:1; /* 24 */
10619 unsigned int sched_entry_seq:1; /* 25 */
10620 unsigned int reserved2:1; /* 26 */
10621 unsigned int Save_SP:1; /* 27 */
10622 unsigned int Save_RP:1; /* 28 */
10623 unsigned int Save_MRP_in_frame:1; /* 29 */
10624 unsigned int extn_ptr_defined:1; /* 30 */
10625 unsigned int Cleanup_defined:1; /* 31 */
10626
10627 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
10628 unsigned int HP_UX_interrupt_marker:1; /* 1 */
10629 unsigned int Large_frame:1; /* 2 */
10630 unsigned int Pseudo_SP_Set:1; /* 3 */
10631 unsigned int reserved4:1; /* 4 */
10632 unsigned int Total_frame_size:27; /* 5..31 */
10633};
10634
10635struct hppa_unw_aux_info
10636{
10637 struct hppa_unw_table_entry * table; /* Unwind table. */
10638 uint64_t table_len; /* Length of unwind table. */
10639 uint64_t seg_base; /* Starting address of segment. */
10640 Elf_Internal_Sym * symtab; /* The symbol table. */
10641 uint64_t nsyms; /* Number of symbols. */
10642 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
10643 uint64_t nfuns; /* Number of entries in funtab. */
10644 char * strtab; /* The string table. */
10645 uint64_t strtab_size; /* Size of string table. */
10646};
10647
10648static bool
10649dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
10650{
10651 struct hppa_unw_table_entry * tp;
10652 uint64_t j, nfuns;
10653 bool res = true;
10654
10655 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
10656 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
10657 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
10658 aux->funtab[nfuns++] = aux->symtab[j];
10659 aux->nfuns = nfuns;
10660 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
10661
10662 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
10663 {
10664 uint64_t offset;
10665 const char * procname;
10666
10667 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
10668 aux->strtab_size, tp->start, &procname,
10669 &offset);
10670
10671 fputs ("\n<", stdout);
10672
10673 if (procname)
10674 {
10675 fputs (procname, stdout);
10676
10677 if (offset)
10678 printf ("+%" PRIx64, offset);
10679 }
10680
10681 fputs (">: [", stdout);
10682 print_vma (tp->start.offset, PREFIX_HEX);
10683 fputc ('-', stdout);
10684 print_vma (tp->end.offset, PREFIX_HEX);
10685 printf ("]\n\t");
10686
10687#define PF(_m) if (tp->_m) printf (#_m " ");
10688#define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
10689 PF(Cannot_unwind);
10690 PF(Millicode);
10691 PF(Millicode_save_sr0);
10692 /* PV(Region_description); */
10693 PF(Entry_SR);
10694 PV(Entry_FR);
10695 PV(Entry_GR);
10696 PF(Args_stored);
10697 PF(Variable_Frame);
10698 PF(Separate_Package_Body);
10699 PF(Frame_Extension_Millicode);
10700 PF(Stack_Overflow_Check);
10701 PF(Two_Instruction_SP_Increment);
10702 PF(Ada_Region);
10703 PF(cxx_info);
10704 PF(cxx_try_catch);
10705 PF(sched_entry_seq);
10706 PF(Save_SP);
10707 PF(Save_RP);
10708 PF(Save_MRP_in_frame);
10709 PF(extn_ptr_defined);
10710 PF(Cleanup_defined);
10711 PF(MPE_XL_interrupt_marker);
10712 PF(HP_UX_interrupt_marker);
10713 PF(Large_frame);
10714 PF(Pseudo_SP_Set);
10715 PV(Total_frame_size);
10716#undef PF
10717#undef PV
10718 }
10719
10720 printf ("\n");
10721
10722 free (aux->funtab);
10723
10724 return res;
10725}
10726
10727static bool
10728slurp_hppa_unwind_table (Filedata * filedata,
10729 struct hppa_unw_aux_info * aux,
10730 Elf_Internal_Shdr * sec)
10731{
10732 uint64_t size, unw_ent_size, nentries, nrelas, i;
10733 Elf_Internal_Phdr * seg;
10734 struct hppa_unw_table_entry * tep;
10735 Elf_Internal_Shdr * relsec;
10736 Elf_Internal_Rela * rela;
10737 Elf_Internal_Rela * rp;
10738 unsigned char * table;
10739 unsigned char * tp;
10740 Elf_Internal_Sym * sym;
10741 const char * relname;
10742
10743 /* First, find the starting address of the segment that includes
10744 this section. */
10745 if (filedata->file_header.e_phnum)
10746 {
10747 if (! get_program_headers (filedata))
10748 return false;
10749
10750 for (seg = filedata->program_headers;
10751 seg < filedata->program_headers + filedata->file_header.e_phnum;
10752 ++seg)
10753 {
10754 if (seg->p_type != PT_LOAD)
10755 continue;
10756
10757 if (sec->sh_addr >= seg->p_vaddr
10758 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
10759 {
10760 aux->seg_base = seg->p_vaddr;
10761 break;
10762 }
10763 }
10764 }
10765
10766 /* Second, build the unwind table from the contents of the unwind
10767 section. */
10768 size = sec->sh_size;
10769 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
10770 _("unwind table"));
10771 if (!table)
10772 return false;
10773
10774 unw_ent_size = 16;
10775 nentries = size / unw_ent_size;
10776 size = unw_ent_size * nentries;
10777
10778 aux->table_len = nentries;
10779 tep = aux->table = (struct hppa_unw_table_entry *)
10780 xcmalloc (nentries, sizeof (aux->table[0]));
10781
10782 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
10783 {
10784 unsigned int tmp1, tmp2;
10785
10786 tep->start.section = SHN_UNDEF;
10787 tep->end.section = SHN_UNDEF;
10788
10789 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
10790 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
10791 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
10792 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
10793
10794 tep->start.offset += aux->seg_base;
10795 tep->end.offset += aux->seg_base;
10796
10797 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
10798 tep->Millicode = (tmp1 >> 30) & 0x1;
10799 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
10800 tep->Region_description = (tmp1 >> 27) & 0x3;
10801 tep->reserved1 = (tmp1 >> 26) & 0x1;
10802 tep->Entry_SR = (tmp1 >> 25) & 0x1;
10803 tep->Entry_FR = (tmp1 >> 21) & 0xf;
10804 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
10805 tep->Args_stored = (tmp1 >> 15) & 0x1;
10806 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
10807 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
10808 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
10809 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
10810 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
10811 tep->Ada_Region = (tmp1 >> 9) & 0x1;
10812 tep->cxx_info = (tmp1 >> 8) & 0x1;
10813 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
10814 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
10815 tep->reserved2 = (tmp1 >> 5) & 0x1;
10816 tep->Save_SP = (tmp1 >> 4) & 0x1;
10817 tep->Save_RP = (tmp1 >> 3) & 0x1;
10818 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
10819 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
10820 tep->Cleanup_defined = tmp1 & 0x1;
10821
10822 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
10823 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
10824 tep->Large_frame = (tmp2 >> 29) & 0x1;
10825 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
10826 tep->reserved4 = (tmp2 >> 27) & 0x1;
10827 tep->Total_frame_size = tmp2 & 0x7ffffff;
10828 }
10829 free (table);
10830
10831 /* Third, apply any relocations to the unwind table. */
10832 for (relsec = filedata->section_headers;
10833 relsec < filedata->section_headers + filedata->file_header.e_shnum;
10834 ++relsec)
10835 {
10836 if (relsec->sh_type != SHT_RELA
10837 || relsec->sh_info >= filedata->file_header.e_shnum
10838 || filedata->section_headers + relsec->sh_info != sec)
10839 continue;
10840
10841 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
10842 & rela, & nrelas))
10843 return false;
10844
10845 for (rp = rela; rp < rela + nrelas; ++rp)
10846 {
10847 unsigned int sym_ndx;
10848 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
10849 relname = elf_hppa_reloc_type (r_type);
10850
10851 if (relname == NULL)
10852 {
10853 warn (_("Skipping unknown relocation type: %u\n"), r_type);
10854 continue;
10855 }
10856
10857 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
10858 if (! startswith (relname, "R_PARISC_SEGREL"))
10859 {
10860 warn (_("Skipping unexpected relocation type: %s\n"), relname);
10861 continue;
10862 }
10863
10864 i = rp->r_offset / unw_ent_size;
10865 if (i >= aux->table_len)
10866 {
10867 warn (_("Skipping reloc with overlarge offset: %#" PRIx64 "\n"),
10868 i);
10869 continue;
10870 }
10871
10872 sym_ndx = get_reloc_symindex (rp->r_info);
10873 if (sym_ndx >= aux->nsyms)
10874 {
10875 warn (_("Skipping reloc with invalid symbol index: %u\n"),
10876 sym_ndx);
10877 continue;
10878 }
10879 sym = aux->symtab + sym_ndx;
10880
10881 switch ((rp->r_offset % unw_ent_size) / 4)
10882 {
10883 case 0:
10884 aux->table[i].start.section = sym->st_shndx;
10885 aux->table[i].start.offset = sym->st_value + rp->r_addend;
10886 break;
10887 case 1:
10888 aux->table[i].end.section = sym->st_shndx;
10889 aux->table[i].end.offset = sym->st_value + rp->r_addend;
10890 break;
10891 default:
10892 break;
10893 }
10894 }
10895
10896 free (rela);
10897 }
10898
10899 return true;
10900}
10901
10902static bool
10903hppa_process_unwind (Filedata * filedata)
10904{
10905 struct hppa_unw_aux_info aux;
10906 Elf_Internal_Shdr * unwsec = NULL;
10907 Elf_Internal_Shdr * sec;
10908 size_t i;
10909 bool res = true;
10910
10911 if (filedata->string_table == NULL)
10912 return false;
10913
10914 memset (& aux, 0, sizeof (aux));
10915
10916 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10917 {
10918 if (sec->sh_type == SHT_SYMTAB)
10919 {
10920 if (aux.symtab)
10921 {
10922 error (_("Multiple symbol tables encountered\n"));
10923 free (aux.symtab);
10924 aux.symtab = NULL;
10925 free (aux.strtab);
10926 aux.strtab = NULL;
10927 }
10928 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
10929 &aux.strtab, &aux.strtab_size))
10930 return false;
10931 }
10932 else if (section_name_valid (filedata, sec)
10933 && streq (section_name (filedata, sec), ".PARISC.unwind"))
10934 unwsec = sec;
10935 }
10936
10937 if (!unwsec)
10938 printf (_("\nThere are no unwind sections in this file.\n"));
10939
10940 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10941 {
10942 if (section_name_valid (filedata, sec)
10943 && streq (section_name (filedata, sec), ".PARISC.unwind"))
10944 {
10945 uint64_t num_unwind = sec->sh_size / 16;
10946
10947 printf (ngettext ("\nUnwind section '%s' at offset %#" PRIx64 " "
10948 "contains %" PRIu64 " entry:\n",
10949 "\nUnwind section '%s' at offset %#" PRIx64 " "
10950 "contains %" PRIu64 " entries:\n",
10951 num_unwind),
10952 printable_section_name (filedata, sec),
10953 sec->sh_offset,
10954 num_unwind);
10955
10956 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
10957 res = false;
10958
10959 if (res && aux.table_len > 0)
10960 {
10961 if (! dump_hppa_unwind (filedata, &aux))
10962 res = false;
10963 }
10964
10965 free ((char *) aux.table);
10966 aux.table = NULL;
10967 }
10968 }
10969
10970 free (aux.symtab);
10971 free ((char *) aux.strtab);
10972
10973 return res;
10974}
10975
10976struct arm_section
10977{
10978 unsigned char * data; /* The unwind data. */
10979 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
10980 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
10981 uint64_t nrelas; /* The number of relocations. */
10982 unsigned int rel_type; /* REL or RELA ? */
10983 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
10984};
10985
10986struct arm_unw_aux_info
10987{
10988 Filedata * filedata; /* The file containing the unwind sections. */
10989 Elf_Internal_Sym * symtab; /* The file's symbol table. */
10990 uint64_t nsyms; /* Number of symbols. */
10991 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
10992 uint64_t nfuns; /* Number of these symbols. */
10993 char * strtab; /* The file's string table. */
10994 uint64_t strtab_size; /* Size of string table. */
10995};
10996
10997static const char *
10998arm_print_vma_and_name (Filedata * filedata,
10999 struct arm_unw_aux_info * aux,
11000 uint64_t fn,
11001 struct absaddr addr)
11002{
11003 const char *procname;
11004 uint64_t sym_offset;
11005
11006 if (addr.section == SHN_UNDEF)
11007 addr.offset = fn;
11008
11009 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
11010 aux->strtab_size, addr, &procname,
11011 &sym_offset);
11012
11013 print_vma (fn, PREFIX_HEX);
11014
11015 if (procname)
11016 {
11017 fputs (" <", stdout);
11018 fputs (procname, stdout);
11019
11020 if (sym_offset)
11021 printf ("+0x%" PRIx64, sym_offset);
11022 fputc ('>', stdout);
11023 }
11024
11025 return procname;
11026}
11027
11028static void
11029arm_free_section (struct arm_section *arm_sec)
11030{
11031 free (arm_sec->data);
11032 free (arm_sec->rela);
11033}
11034
11035/* 1) If SEC does not match the one cached in ARM_SEC, then free the current
11036 cached section and install SEC instead.
11037 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
11038 and return its valued in * WORDP, relocating if necessary.
11039 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
11040 relocation's offset in ADDR.
11041 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
11042 into the string table of the symbol associated with the reloc. If no
11043 reloc was applied store -1 there.
11044 5) Return TRUE upon success, FALSE otherwise. */
11045
11046static bool
11047get_unwind_section_word (Filedata * filedata,
11048 struct arm_unw_aux_info * aux,
11049 struct arm_section * arm_sec,
11050 Elf_Internal_Shdr * sec,
11051 uint64_t word_offset,
11052 unsigned int * wordp,
11053 struct absaddr * addr,
11054 uint64_t * sym_name)
11055{
11056 Elf_Internal_Rela *rp;
11057 Elf_Internal_Sym *sym;
11058 const char * relname;
11059 unsigned int word;
11060 bool wrapped;
11061
11062 if (sec == NULL || arm_sec == NULL)
11063 return false;
11064
11065 addr->section = SHN_UNDEF;
11066 addr->offset = 0;
11067
11068 if (sym_name != NULL)
11069 *sym_name = (uint64_t) -1;
11070
11071 /* If necessary, update the section cache. */
11072 if (sec != arm_sec->sec)
11073 {
11074 Elf_Internal_Shdr *relsec;
11075
11076 arm_free_section (arm_sec);
11077
11078 arm_sec->sec = sec;
11079 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
11080 sec->sh_size, _("unwind data"));
11081 arm_sec->rela = NULL;
11082 arm_sec->nrelas = 0;
11083
11084 for (relsec = filedata->section_headers;
11085 relsec < filedata->section_headers + filedata->file_header.e_shnum;
11086 ++relsec)
11087 {
11088 if (relsec->sh_info >= filedata->file_header.e_shnum
11089 || filedata->section_headers + relsec->sh_info != sec
11090 /* PR 15745: Check the section type as well. */
11091 || (relsec->sh_type != SHT_REL
11092 && relsec->sh_type != SHT_RELA))
11093 continue;
11094
11095 arm_sec->rel_type = relsec->sh_type;
11096 if (relsec->sh_type == SHT_REL)
11097 {
11098 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
11099 relsec->sh_size,
11100 & arm_sec->rela, & arm_sec->nrelas))
11101 return false;
11102 }
11103 else /* relsec->sh_type == SHT_RELA */
11104 {
11105 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
11106 relsec->sh_size,
11107 & arm_sec->rela, & arm_sec->nrelas))
11108 return false;
11109 }
11110 break;
11111 }
11112
11113 arm_sec->next_rela = arm_sec->rela;
11114 }
11115
11116 /* If there is no unwind data we can do nothing. */
11117 if (arm_sec->data == NULL)
11118 return false;
11119
11120 /* If the offset is invalid then fail. */
11121 if (/* PR 21343 *//* PR 18879 */
11122 sec->sh_size < 4
11123 || word_offset > sec->sh_size - 4)
11124 return false;
11125
11126 /* Get the word at the required offset. */
11127 word = byte_get (arm_sec->data + word_offset, 4);
11128
11129 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
11130 if (arm_sec->rela == NULL)
11131 {
11132 * wordp = word;
11133 return true;
11134 }
11135
11136 /* Look through the relocs to find the one that applies to the provided offset. */
11137 wrapped = false;
11138 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
11139 {
11140 uint64_t prelval, offset;
11141
11142 if (rp->r_offset > word_offset && !wrapped)
11143 {
11144 rp = arm_sec->rela;
11145 wrapped = true;
11146 }
11147 if (rp->r_offset > word_offset)
11148 break;
11149
11150 if (rp->r_offset & 3)
11151 {
11152 warn (_("Skipping unexpected relocation at offset %#" PRIx64 "\n"),
11153 rp->r_offset);
11154 continue;
11155 }
11156
11157 if (rp->r_offset < word_offset)
11158 continue;
11159
11160 /* PR 17531: file: 027-161405-0.004 */
11161 if (aux->symtab == NULL)
11162 continue;
11163
11164 if (arm_sec->rel_type == SHT_REL)
11165 {
11166 offset = word & 0x7fffffff;
11167 if (offset & 0x40000000)
11168 offset |= ~ (uint64_t) 0x7fffffff;
11169 }
11170 else if (arm_sec->rel_type == SHT_RELA)
11171 offset = rp->r_addend;
11172 else
11173 {
11174 error (_("Unknown section relocation type %d encountered\n"),
11175 arm_sec->rel_type);
11176 break;
11177 }
11178
11179 /* PR 17531 file: 027-1241568-0.004. */
11180 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
11181 {
11182 error (_("Bad symbol index in unwind relocation "
11183 "(%" PRIu64 " > %" PRIu64 ")\n"),
11184 ELF32_R_SYM (rp->r_info), aux->nsyms);
11185 break;
11186 }
11187
11188 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
11189 offset += sym->st_value;
11190 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
11191
11192 /* Check that we are processing the expected reloc type. */
11193 if (filedata->file_header.e_machine == EM_ARM)
11194 {
11195 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
11196 if (relname == NULL)
11197 {
11198 warn (_("Skipping unknown ARM relocation type: %d\n"),
11199 (int) ELF32_R_TYPE (rp->r_info));
11200 continue;
11201 }
11202
11203 if (streq (relname, "R_ARM_NONE"))
11204 continue;
11205
11206 if (! streq (relname, "R_ARM_PREL31"))
11207 {
11208 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
11209 continue;
11210 }
11211 }
11212 else if (filedata->file_header.e_machine == EM_TI_C6000)
11213 {
11214 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
11215 if (relname == NULL)
11216 {
11217 warn (_("Skipping unknown C6000 relocation type: %d\n"),
11218 (int) ELF32_R_TYPE (rp->r_info));
11219 continue;
11220 }
11221
11222 if (streq (relname, "R_C6000_NONE"))
11223 continue;
11224
11225 if (! streq (relname, "R_C6000_PREL31"))
11226 {
11227 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
11228 continue;
11229 }
11230
11231 prelval >>= 1;
11232 }
11233 else
11234 {
11235 /* This function currently only supports ARM and TI unwinders. */
11236 warn (_("Only TI and ARM unwinders are currently supported\n"));
11237 break;
11238 }
11239
11240 word = (word & ~ (uint64_t) 0x7fffffff) | (prelval & 0x7fffffff);
11241 addr->section = sym->st_shndx;
11242 addr->offset = offset;
11243
11244 if (sym_name)
11245 * sym_name = sym->st_name;
11246 break;
11247 }
11248
11249 *wordp = word;
11250 arm_sec->next_rela = rp;
11251
11252 return true;
11253}
11254
11255static const char *tic6x_unwind_regnames[16] =
11256{
11257 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
11258 "A14", "A13", "A12", "A11", "A10",
11259 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
11260};
11261
11262static void
11263decode_tic6x_unwind_regmask (unsigned int mask)
11264{
11265 int i;
11266
11267 for (i = 12; mask; mask >>= 1, i--)
11268 {
11269 if (mask & 1)
11270 {
11271 fputs (tic6x_unwind_regnames[i], stdout);
11272 if (mask > 1)
11273 fputs (", ", stdout);
11274 }
11275 }
11276}
11277
11278#define ADVANCE \
11279 if (remaining == 0 && more_words) \
11280 { \
11281 data_offset += 4; \
11282 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
11283 data_offset, & word, & addr, NULL)) \
11284 return false; \
11285 remaining = 4; \
11286 more_words--; \
11287 } \
11288
11289#define GET_OP(OP) \
11290 ADVANCE; \
11291 if (remaining) \
11292 { \
11293 remaining--; \
11294 (OP) = word >> 24; \
11295 word <<= 8; \
11296 } \
11297 else \
11298 { \
11299 printf (_("[Truncated opcode]\n")); \
11300 return false; \
11301 } \
11302 printf ("0x%02x ", OP)
11303
11304static bool
11305decode_arm_unwind_bytecode (Filedata * filedata,
11306 struct arm_unw_aux_info * aux,
11307 unsigned int word,
11308 unsigned int remaining,
11309 unsigned int more_words,
11310 uint64_t data_offset,
11311 Elf_Internal_Shdr * data_sec,
11312 struct arm_section * data_arm_sec)
11313{
11314 struct absaddr addr;
11315 bool res = true;
11316
11317 /* Decode the unwinding instructions. */
11318 while (1)
11319 {
11320 unsigned int op, op2;
11321
11322 ADVANCE;
11323 if (remaining == 0)
11324 break;
11325 remaining--;
11326 op = word >> 24;
11327 word <<= 8;
11328
11329 printf (" 0x%02x ", op);
11330
11331 if ((op & 0xc0) == 0x00)
11332 {
11333 int offset = ((op & 0x3f) << 2) + 4;
11334
11335 printf (" vsp = vsp + %d", offset);
11336 }
11337 else if ((op & 0xc0) == 0x40)
11338 {
11339 int offset = ((op & 0x3f) << 2) + 4;
11340
11341 printf (" vsp = vsp - %d", offset);
11342 }
11343 else if ((op & 0xf0) == 0x80)
11344 {
11345 GET_OP (op2);
11346 if (op == 0x80 && op2 == 0)
11347 printf (_("Refuse to unwind"));
11348 else
11349 {
11350 unsigned int mask = ((op & 0x0f) << 8) | op2;
11351 bool first = true;
11352 int i;
11353
11354 printf ("pop {");
11355 for (i = 0; i < 12; i++)
11356 if (mask & (1 << i))
11357 {
11358 if (first)
11359 first = false;
11360 else
11361 printf (", ");
11362 printf ("r%d", 4 + i);
11363 }
11364 printf ("}");
11365 }
11366 }
11367 else if ((op & 0xf0) == 0x90)
11368 {
11369 if (op == 0x9d || op == 0x9f)
11370 printf (_(" [Reserved]"));
11371 else
11372 printf (" vsp = r%d", op & 0x0f);
11373 }
11374 else if ((op & 0xf0) == 0xa0)
11375 {
11376 int end = 4 + (op & 0x07);
11377 bool first = true;
11378 int i;
11379
11380 printf (" pop {");
11381 for (i = 4; i <= end; i++)
11382 {
11383 if (first)
11384 first = false;
11385 else
11386 printf (", ");
11387 printf ("r%d", i);
11388 }
11389 if (op & 0x08)
11390 {
11391 if (!first)
11392 printf (", ");
11393 printf ("r14");
11394 }
11395 printf ("}");
11396 }
11397 else if (op == 0xb0)
11398 printf (_(" finish"));
11399 else if (op == 0xb1)
11400 {
11401 GET_OP (op2);
11402 if (op2 == 0 || (op2 & 0xf0) != 0)
11403 printf (_("[Spare]"));
11404 else
11405 {
11406 unsigned int mask = op2 & 0x0f;
11407 bool first = true;
11408 int i;
11409
11410 printf ("pop {");
11411 for (i = 0; i < 12; i++)
11412 if (mask & (1 << i))
11413 {
11414 if (first)
11415 first = false;
11416 else
11417 printf (", ");
11418 printf ("r%d", i);
11419 }
11420 printf ("}");
11421 }
11422 }
11423 else if (op == 0xb2)
11424 {
11425 unsigned char buf[9];
11426 unsigned int i, len;
11427 uint64_t offset;
11428
11429 for (i = 0; i < sizeof (buf); i++)
11430 {
11431 GET_OP (buf[i]);
11432 if ((buf[i] & 0x80) == 0)
11433 break;
11434 }
11435 if (i == sizeof (buf))
11436 {
11437 error (_("corrupt change to vsp\n"));
11438 res = false;
11439 }
11440 else
11441 {
11442 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
11443 assert (len == i + 1);
11444 offset = offset * 4 + 0x204;
11445 printf ("vsp = vsp + %" PRId64, offset);
11446 }
11447 }
11448 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
11449 {
11450 unsigned int first, last;
11451
11452 GET_OP (op2);
11453 first = op2 >> 4;
11454 last = op2 & 0x0f;
11455 if (op == 0xc8)
11456 first = first + 16;
11457 printf ("pop {D%d", first);
11458 if (last)
11459 printf ("-D%d", first + last);
11460 printf ("}");
11461 }
11462 else if (op == 0xb4)
11463 printf (_(" pop {ra_auth_code}"));
11464 else if (op == 0xb5)
11465 printf (_(" vsp as modifier for PAC validation"));
11466 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
11467 {
11468 unsigned int count = op & 0x07;
11469
11470 printf ("pop {D8");
11471 if (count)
11472 printf ("-D%d", 8 + count);
11473 printf ("}");
11474 }
11475 else if (op >= 0xc0 && op <= 0xc5)
11476 {
11477 unsigned int count = op & 0x07;
11478
11479 printf (" pop {wR10");
11480 if (count)
11481 printf ("-wR%d", 10 + count);
11482 printf ("}");
11483 }
11484 else if (op == 0xc6)
11485 {
11486 unsigned int first, last;
11487
11488 GET_OP (op2);
11489 first = op2 >> 4;
11490 last = op2 & 0x0f;
11491 printf ("pop {wR%d", first);
11492 if (last)
11493 printf ("-wR%d", first + last);
11494 printf ("}");
11495 }
11496 else if (op == 0xc7)
11497 {
11498 GET_OP (op2);
11499 if (op2 == 0 || (op2 & 0xf0) != 0)
11500 printf (_("[Spare]"));
11501 else
11502 {
11503 unsigned int mask = op2 & 0x0f;
11504 bool first = true;
11505 int i;
11506
11507 printf ("pop {");
11508 for (i = 0; i < 4; i++)
11509 if (mask & (1 << i))
11510 {
11511 if (first)
11512 first = false;
11513 else
11514 printf (", ");
11515 printf ("wCGR%d", i);
11516 }
11517 printf ("}");
11518 }
11519 }
11520 else
11521 {
11522 printf (_(" [unsupported opcode]"));
11523 res = false;
11524 }
11525
11526 printf ("\n");
11527 }
11528
11529 return res;
11530}
11531
11532static bool
11533decode_tic6x_unwind_bytecode (Filedata * filedata,
11534 struct arm_unw_aux_info * aux,
11535 unsigned int word,
11536 unsigned int remaining,
11537 unsigned int more_words,
11538 uint64_t data_offset,
11539 Elf_Internal_Shdr * data_sec,
11540 struct arm_section * data_arm_sec)
11541{
11542 struct absaddr addr;
11543
11544 /* Decode the unwinding instructions. */
11545 while (1)
11546 {
11547 unsigned int op, op2;
11548
11549 ADVANCE;
11550 if (remaining == 0)
11551 break;
11552 remaining--;
11553 op = word >> 24;
11554 word <<= 8;
11555
11556 printf (" 0x%02x ", op);
11557
11558 if ((op & 0xc0) == 0x00)
11559 {
11560 int offset = ((op & 0x3f) << 3) + 8;
11561 printf (" sp = sp + %d", offset);
11562 }
11563 else if ((op & 0xc0) == 0x80)
11564 {
11565 GET_OP (op2);
11566 if (op == 0x80 && op2 == 0)
11567 printf (_("Refuse to unwind"));
11568 else
11569 {
11570 unsigned int mask = ((op & 0x1f) << 8) | op2;
11571 if (op & 0x20)
11572 printf ("pop compact {");
11573 else
11574 printf ("pop {");
11575
11576 decode_tic6x_unwind_regmask (mask);
11577 printf("}");
11578 }
11579 }
11580 else if ((op & 0xf0) == 0xc0)
11581 {
11582 unsigned int reg;
11583 unsigned int nregs;
11584 unsigned int i;
11585 const char *name;
11586 struct
11587 {
11588 unsigned int offset;
11589 unsigned int reg;
11590 } regpos[16];
11591
11592 /* Scan entire instruction first so that GET_OP output is not
11593 interleaved with disassembly. */
11594 nregs = 0;
11595 for (i = 0; nregs < (op & 0xf); i++)
11596 {
11597 GET_OP (op2);
11598 reg = op2 >> 4;
11599 if (reg != 0xf)
11600 {
11601 regpos[nregs].offset = i * 2;
11602 regpos[nregs].reg = reg;
11603 nregs++;
11604 }
11605
11606 reg = op2 & 0xf;
11607 if (reg != 0xf)
11608 {
11609 regpos[nregs].offset = i * 2 + 1;
11610 regpos[nregs].reg = reg;
11611 nregs++;
11612 }
11613 }
11614
11615 printf (_("pop frame {"));
11616 if (nregs == 0)
11617 {
11618 printf (_("*corrupt* - no registers specified"));
11619 }
11620 else
11621 {
11622 reg = nregs - 1;
11623 for (i = i * 2; i > 0; i--)
11624 {
11625 if (regpos[reg].offset == i - 1)
11626 {
11627 name = tic6x_unwind_regnames[regpos[reg].reg];
11628 if (reg > 0)
11629 reg--;
11630 }
11631 else
11632 name = _("[pad]");
11633
11634 fputs (name, stdout);
11635 if (i > 1)
11636 printf (", ");
11637 }
11638 }
11639
11640 printf ("}");
11641 }
11642 else if (op == 0xd0)
11643 printf (" MOV FP, SP");
11644 else if (op == 0xd1)
11645 printf (" __c6xabi_pop_rts");
11646 else if (op == 0xd2)
11647 {
11648 unsigned char buf[9];
11649 unsigned int i, len;
11650 uint64_t offset;
11651
11652 for (i = 0; i < sizeof (buf); i++)
11653 {
11654 GET_OP (buf[i]);
11655 if ((buf[i] & 0x80) == 0)
11656 break;
11657 }
11658 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
11659 if (i == sizeof (buf))
11660 {
11661 warn (_("Corrupt stack pointer adjustment detected\n"));
11662 return false;
11663 }
11664
11665 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
11666 assert (len == i + 1);
11667 offset = offset * 8 + 0x408;
11668 printf (_("sp = sp + %" PRId64), offset);
11669 }
11670 else if ((op & 0xf0) == 0xe0)
11671 {
11672 if ((op & 0x0f) == 7)
11673 printf (" RETURN");
11674 else
11675 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
11676 }
11677 else
11678 {
11679 printf (_(" [unsupported opcode]"));
11680 }
11681 putchar ('\n');
11682 }
11683
11684 return true;
11685}
11686
11687static uint64_t
11688arm_expand_prel31 (Filedata * filedata, uint64_t word, uint64_t where)
11689{
11690 uint64_t offset;
11691
11692 offset = word & 0x7fffffff;
11693 if (offset & 0x40000000)
11694 offset |= ~ (uint64_t) 0x7fffffff;
11695
11696 if (filedata->file_header.e_machine == EM_TI_C6000)
11697 offset <<= 1;
11698
11699 return offset + where;
11700}
11701
11702static bool
11703decode_arm_unwind (Filedata * filedata,
11704 struct arm_unw_aux_info * aux,
11705 unsigned int word,
11706 unsigned int remaining,
11707 uint64_t data_offset,
11708 Elf_Internal_Shdr * data_sec,
11709 struct arm_section * data_arm_sec)
11710{
11711 int per_index;
11712 unsigned int more_words = 0;
11713 struct absaddr addr;
11714 uint64_t sym_name = (uint64_t) -1;
11715 bool res = true;
11716
11717 if (remaining == 0)
11718 {
11719 /* Fetch the first word.
11720 Note - when decoding an object file the address extracted
11721 here will always be 0. So we also pass in the sym_name
11722 parameter so that we can find the symbol associated with
11723 the personality routine. */
11724 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
11725 & word, & addr, & sym_name))
11726 return false;
11727
11728 remaining = 4;
11729 }
11730 else
11731 {
11732 addr.section = SHN_UNDEF;
11733 addr.offset = 0;
11734 }
11735
11736 if ((word & 0x80000000) == 0)
11737 {
11738 /* Expand prel31 for personality routine. */
11739 uint64_t fn;
11740 const char *procname;
11741
11742 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
11743 printf (_(" Personality routine: "));
11744 if (fn == 0
11745 && addr.section == SHN_UNDEF && addr.offset == 0
11746 && sym_name != (uint64_t) -1 && sym_name < aux->strtab_size)
11747 {
11748 procname = aux->strtab + sym_name;
11749 print_vma (fn, PREFIX_HEX);
11750 if (procname)
11751 {
11752 fputs (" <", stdout);
11753 fputs (procname, stdout);
11754 fputc ('>', stdout);
11755 }
11756 }
11757 else
11758 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
11759 fputc ('\n', stdout);
11760
11761 /* The GCC personality routines use the standard compact
11762 encoding, starting with one byte giving the number of
11763 words. */
11764 if (procname != NULL
11765 && (startswith (procname, "__gcc_personality_v0")
11766 || startswith (procname, "__gxx_personality_v0")
11767 || startswith (procname, "__gcj_personality_v0")
11768 || startswith (procname, "__gnu_objc_personality_v0")))
11769 {
11770 remaining = 0;
11771 more_words = 1;
11772 ADVANCE;
11773 if (!remaining)
11774 {
11775 printf (_(" [Truncated data]\n"));
11776 return false;
11777 }
11778 more_words = word >> 24;
11779 word <<= 8;
11780 remaining--;
11781 per_index = -1;
11782 }
11783 else
11784 return true;
11785 }
11786 else
11787 {
11788 /* ARM EHABI Section 6.3:
11789
11790 An exception-handling table entry for the compact model looks like:
11791
11792 31 30-28 27-24 23-0
11793 -- ----- ----- ----
11794 1 0 index Data for personalityRoutine[index] */
11795
11796 if (filedata->file_header.e_machine == EM_ARM
11797 && (word & 0x70000000))
11798 {
11799 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
11800 res = false;
11801 }
11802
11803 per_index = (word >> 24) & 0x7f;
11804 printf (_(" Compact model index: %d\n"), per_index);
11805 if (per_index == 0)
11806 {
11807 more_words = 0;
11808 word <<= 8;
11809 remaining--;
11810 }
11811 else if (per_index < 3)
11812 {
11813 more_words = (word >> 16) & 0xff;
11814 word <<= 16;
11815 remaining -= 2;
11816 }
11817 }
11818
11819 switch (filedata->file_header.e_machine)
11820 {
11821 case EM_ARM:
11822 if (per_index < 3)
11823 {
11824 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
11825 data_offset, data_sec, data_arm_sec))
11826 res = false;
11827 }
11828 else
11829 {
11830 warn (_("Unknown ARM compact model index encountered\n"));
11831 printf (_(" [reserved]\n"));
11832 res = false;
11833 }
11834 break;
11835
11836 case EM_TI_C6000:
11837 if (per_index < 3)
11838 {
11839 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
11840 data_offset, data_sec, data_arm_sec))
11841 res = false;
11842 }
11843 else if (per_index < 5)
11844 {
11845 if (((word >> 17) & 0x7f) == 0x7f)
11846 printf (_(" Restore stack from frame pointer\n"));
11847 else
11848 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
11849 printf (_(" Registers restored: "));
11850 if (per_index == 4)
11851 printf (" (compact) ");
11852 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
11853 putchar ('\n');
11854 printf (_(" Return register: %s\n"),
11855 tic6x_unwind_regnames[word & 0xf]);
11856 }
11857 else
11858 printf (_(" [reserved (%d)]\n"), per_index);
11859 break;
11860
11861 default:
11862 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
11863 filedata->file_header.e_machine);
11864 res = false;
11865 }
11866
11867 /* Decode the descriptors. Not implemented. */
11868
11869 return res;
11870}
11871
11872static bool
11873dump_arm_unwind (Filedata * filedata,
11874 struct arm_unw_aux_info * aux,
11875 Elf_Internal_Shdr * exidx_sec)
11876{
11877 struct arm_section exidx_arm_sec, extab_arm_sec;
11878 unsigned int i, exidx_len;
11879 uint64_t j, nfuns;
11880 bool res = true;
11881
11882 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
11883 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
11884 exidx_len = exidx_sec->sh_size / 8;
11885
11886 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
11887 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
11888 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
11889 aux->funtab[nfuns++] = aux->symtab[j];
11890 aux->nfuns = nfuns;
11891 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
11892
11893 for (i = 0; i < exidx_len; i++)
11894 {
11895 unsigned int exidx_fn, exidx_entry;
11896 struct absaddr fn_addr, entry_addr;
11897 uint64_t fn;
11898
11899 fputc ('\n', stdout);
11900
11901 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
11902 8 * i, & exidx_fn, & fn_addr, NULL)
11903 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
11904 8 * i + 4, & exidx_entry, & entry_addr, NULL))
11905 {
11906 free (aux->funtab);
11907 arm_free_section (& exidx_arm_sec);
11908 arm_free_section (& extab_arm_sec);
11909 return false;
11910 }
11911
11912 /* ARM EHABI, Section 5:
11913 An index table entry consists of 2 words.
11914 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
11915 if (exidx_fn & 0x80000000)
11916 {
11917 warn (_("corrupt index table entry: %x\n"), exidx_fn);
11918 res = false;
11919 }
11920
11921 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
11922
11923 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
11924 fputs (": ", stdout);
11925
11926 if (exidx_entry == 1)
11927 {
11928 print_vma (exidx_entry, PREFIX_HEX);
11929 fputs (" [cantunwind]\n", stdout);
11930 }
11931 else if (exidx_entry & 0x80000000)
11932 {
11933 print_vma (exidx_entry, PREFIX_HEX);
11934 fputc ('\n', stdout);
11935 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
11936 }
11937 else
11938 {
11939 uint64_t table, table_offset = 0;
11940 Elf_Internal_Shdr *table_sec;
11941
11942 fputs ("@", stdout);
11943 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
11944 print_vma (table, PREFIX_HEX);
11945 printf ("\n");
11946
11947 /* Locate the matching .ARM.extab. */
11948 if (entry_addr.section != SHN_UNDEF
11949 && entry_addr.section < filedata->file_header.e_shnum)
11950 {
11951 table_sec = filedata->section_headers + entry_addr.section;
11952 table_offset = entry_addr.offset;
11953 /* PR 18879 */
11954 if (table_offset > table_sec->sh_size)
11955 {
11956 warn (_("Unwind entry contains corrupt offset (%#" PRIx64 ") into section %s\n"),
11957 table_offset,
11958 printable_section_name (filedata, table_sec));
11959 res = false;
11960 continue;
11961 }
11962 }
11963 else
11964 {
11965 table_sec = find_section_by_address (filedata, table);
11966 if (table_sec != NULL)
11967 table_offset = table - table_sec->sh_addr;
11968 }
11969
11970 if (table_sec == NULL)
11971 {
11972 warn (_("Could not locate .ARM.extab section containing %#" PRIx64 ".\n"),
11973 table);
11974 res = false;
11975 continue;
11976 }
11977
11978 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
11979 &extab_arm_sec))
11980 res = false;
11981 }
11982 }
11983
11984 printf ("\n");
11985
11986 free (aux->funtab);
11987 arm_free_section (&exidx_arm_sec);
11988 arm_free_section (&extab_arm_sec);
11989
11990 return res;
11991}
11992
11993/* Used for both ARM and C6X unwinding tables. */
11994
11995static bool
11996arm_process_unwind (Filedata * filedata)
11997{
11998 struct arm_unw_aux_info aux;
11999 Elf_Internal_Shdr *unwsec = NULL;
12000 Elf_Internal_Shdr *sec;
12001 size_t i;
12002 unsigned int sec_type;
12003 bool res = true;
12004
12005 switch (filedata->file_header.e_machine)
12006 {
12007 case EM_ARM:
12008 sec_type = SHT_ARM_EXIDX;
12009 break;
12010
12011 case EM_TI_C6000:
12012 sec_type = SHT_C6000_UNWIND;
12013 break;
12014
12015 default:
12016 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
12017 filedata->file_header.e_machine);
12018 return false;
12019 }
12020
12021 if (filedata->string_table == NULL)
12022 return false;
12023
12024 memset (& aux, 0, sizeof (aux));
12025 aux.filedata = filedata;
12026
12027 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
12028 {
12029 if (sec->sh_type == SHT_SYMTAB)
12030 {
12031 if (aux.symtab)
12032 {
12033 error (_("Multiple symbol tables encountered\n"));
12034 free (aux.symtab);
12035 aux.symtab = NULL;
12036 free (aux.strtab);
12037 aux.strtab = NULL;
12038 }
12039 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
12040 &aux.strtab, &aux.strtab_size))
12041 return false;
12042 }
12043 else if (sec->sh_type == sec_type)
12044 unwsec = sec;
12045 }
12046
12047 if (unwsec == NULL)
12048 printf (_("\nThere are no unwind sections in this file.\n"));
12049 else
12050 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
12051 {
12052 if (sec->sh_type == sec_type)
12053 {
12054 uint64_t num_unwind = sec->sh_size / (2 * eh_addr_size);
12055 printf (ngettext ("\nUnwind section '%s' at offset %#" PRIx64 " "
12056 "contains %" PRIu64 " entry:\n",
12057 "\nUnwind section '%s' at offset %#" PRIx64 " "
12058 "contains %" PRIu64 " entries:\n",
12059 num_unwind),
12060 printable_section_name (filedata, sec),
12061 sec->sh_offset,
12062 num_unwind);
12063
12064 if (! dump_arm_unwind (filedata, &aux, sec))
12065 res = false;
12066 }
12067 }
12068
12069 free (aux.symtab);
12070 free ((char *) aux.strtab);
12071
12072 return res;
12073}
12074
12075static bool
12076no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
12077{
12078 printf (_("No processor specific unwind information to decode\n"));
12079 return true;
12080}
12081
12082static bool
12083process_unwind (Filedata * filedata)
12084{
12085 struct unwind_handler
12086 {
12087 unsigned int machtype;
12088 bool (* handler)(Filedata *);
12089 } handlers[] =
12090 {
12091 { EM_ARM, arm_process_unwind },
12092 { EM_IA_64, ia64_process_unwind },
12093 { EM_PARISC, hppa_process_unwind },
12094 { EM_TI_C6000, arm_process_unwind },
12095 { EM_386, no_processor_specific_unwind },
12096 { EM_X86_64, no_processor_specific_unwind },
12097 { 0, NULL }
12098 };
12099 int i;
12100
12101 if (!do_unwind)
12102 return true;
12103
12104 for (i = 0; handlers[i].handler != NULL; i++)
12105 if (filedata->file_header.e_machine == handlers[i].machtype)
12106 return handlers[i].handler (filedata);
12107
12108 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
12109 get_machine_name (filedata->file_header.e_machine));
12110 return true;
12111}
12112
12113static void
12114dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
12115{
12116 switch (entry->d_tag)
12117 {
12118 case DT_AARCH64_BTI_PLT:
12119 case DT_AARCH64_PAC_PLT:
12120 break;
12121 default:
12122 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
12123 break;
12124 }
12125 putchar ('\n');
12126}
12127
12128static void
12129dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
12130{
12131 switch (entry->d_tag)
12132 {
12133 case DT_MIPS_FLAGS:
12134 if (entry->d_un.d_val == 0)
12135 printf (_("NONE"));
12136 else
12137 {
12138 static const char * opts[] =
12139 {
12140 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
12141 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
12142 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
12143 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
12144 "RLD_ORDER_SAFE"
12145 };
12146 unsigned int cnt;
12147 bool first = true;
12148
12149 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
12150 if (entry->d_un.d_val & (1 << cnt))
12151 {
12152 printf ("%s%s", first ? "" : " ", opts[cnt]);
12153 first = false;
12154 }
12155 }
12156 break;
12157
12158 case DT_MIPS_IVERSION:
12159 if (valid_dynamic_name (filedata, entry->d_un.d_val))
12160 printf (_("Interface Version: %s"),
12161 get_dynamic_name (filedata, entry->d_un.d_val));
12162 else
12163 printf (_("Interface Version: <corrupt: %" PRIx64 ">"),
12164 entry->d_un.d_ptr);
12165 break;
12166
12167 case DT_MIPS_TIME_STAMP:
12168 {
12169 char timebuf[128];
12170 struct tm * tmp;
12171 time_t atime = entry->d_un.d_val;
12172
12173 tmp = gmtime (&atime);
12174 /* PR 17531: file: 6accc532. */
12175 if (tmp == NULL)
12176 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
12177 else
12178 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
12179 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
12180 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
12181 printf (_("Time Stamp: %s"), timebuf);
12182 }
12183 break;
12184
12185 case DT_MIPS_RLD_VERSION:
12186 case DT_MIPS_LOCAL_GOTNO:
12187 case DT_MIPS_CONFLICTNO:
12188 case DT_MIPS_LIBLISTNO:
12189 case DT_MIPS_SYMTABNO:
12190 case DT_MIPS_UNREFEXTNO:
12191 case DT_MIPS_HIPAGENO:
12192 case DT_MIPS_DELTA_CLASS_NO:
12193 case DT_MIPS_DELTA_INSTANCE_NO:
12194 case DT_MIPS_DELTA_RELOC_NO:
12195 case DT_MIPS_DELTA_SYM_NO:
12196 case DT_MIPS_DELTA_CLASSSYM_NO:
12197 case DT_MIPS_COMPACT_SIZE:
12198 print_vma (entry->d_un.d_val, DEC);
12199 break;
12200
12201 case DT_MIPS_XHASH:
12202 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
12203 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
12204 /* Falls through. */
12205
12206 default:
12207 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
12208 }
12209 putchar ('\n');
12210}
12211
12212static void
12213dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
12214{
12215 switch (entry->d_tag)
12216 {
12217 case DT_HP_DLD_FLAGS:
12218 {
12219 static struct
12220 {
12221 unsigned int bit;
12222 const char * str;
12223 }
12224 flags[] =
12225 {
12226 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
12227 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
12228 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
12229 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
12230 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
12231 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
12232 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
12233 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
12234 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
12235 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
12236 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
12237 { DT_HP_GST, "HP_GST" },
12238 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
12239 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
12240 { DT_HP_NODELETE, "HP_NODELETE" },
12241 { DT_HP_GROUP, "HP_GROUP" },
12242 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
12243 };
12244 bool first = true;
12245 size_t cnt;
12246 uint64_t val = entry->d_un.d_val;
12247
12248 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
12249 if (val & flags[cnt].bit)
12250 {
12251 if (! first)
12252 putchar (' ');
12253 fputs (flags[cnt].str, stdout);
12254 first = false;
12255 val ^= flags[cnt].bit;
12256 }
12257
12258 if (val != 0 || first)
12259 {
12260 if (! first)
12261 putchar (' ');
12262 print_vma (val, HEX);
12263 }
12264 }
12265 break;
12266
12267 default:
12268 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
12269 break;
12270 }
12271 putchar ('\n');
12272}
12273
12274/* VMS vs Unix time offset and factor. */
12275
12276#define VMS_EPOCH_OFFSET 35067168000000000LL
12277#define VMS_GRANULARITY_FACTOR 10000000
12278#ifndef INT64_MIN
12279#define INT64_MIN (-9223372036854775807LL - 1)
12280#endif
12281
12282/* Display a VMS time in a human readable format. */
12283
12284static void
12285print_vms_time (int64_t vmstime)
12286{
12287 struct tm *tm = NULL;
12288 time_t unxtime;
12289
12290 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
12291 {
12292 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
12293 unxtime = vmstime;
12294 if (unxtime == vmstime)
12295 tm = gmtime (&unxtime);
12296 }
12297 if (tm != NULL)
12298 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
12299 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
12300 tm->tm_hour, tm->tm_min, tm->tm_sec);
12301}
12302
12303static void
12304dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
12305{
12306 switch (entry->d_tag)
12307 {
12308 case DT_IA_64_PLT_RESERVE:
12309 /* First 3 slots reserved. */
12310 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
12311 printf (" -- ");
12312 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
12313 break;
12314
12315 case DT_IA_64_VMS_LINKTIME:
12316 print_vms_time (entry->d_un.d_val);
12317 break;
12318
12319 case DT_IA_64_VMS_LNKFLAGS:
12320 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
12321 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
12322 printf (" CALL_DEBUG");
12323 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
12324 printf (" NOP0BUFS");
12325 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
12326 printf (" P0IMAGE");
12327 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
12328 printf (" MKTHREADS");
12329 if (entry->d_un.d_val & VMS_LF_UPCALLS)
12330 printf (" UPCALLS");
12331 if (entry->d_un.d_val & VMS_LF_IMGSTA)
12332 printf (" IMGSTA");
12333 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
12334 printf (" INITIALIZE");
12335 if (entry->d_un.d_val & VMS_LF_MAIN)
12336 printf (" MAIN");
12337 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
12338 printf (" EXE_INIT");
12339 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
12340 printf (" TBK_IN_IMG");
12341 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
12342 printf (" DBG_IN_IMG");
12343 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
12344 printf (" TBK_IN_DSF");
12345 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
12346 printf (" DBG_IN_DSF");
12347 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
12348 printf (" SIGNATURES");
12349 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
12350 printf (" REL_SEG_OFF");
12351 break;
12352
12353 default:
12354 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
12355 break;
12356 }
12357 putchar ('\n');
12358}
12359
12360static bool
12361get_32bit_dynamic_section (Filedata * filedata)
12362{
12363 Elf32_External_Dyn * edyn;
12364 Elf32_External_Dyn * ext;
12365 Elf_Internal_Dyn * entry;
12366
12367 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
12368 filedata->dynamic_addr, 1,
12369 filedata->dynamic_size,
12370 _("dynamic section"));
12371 if (!edyn)
12372 return false;
12373
12374 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
12375 might not have the luxury of section headers. Look for the DT_NULL
12376 terminator to determine the number of entries. */
12377 for (ext = edyn, filedata->dynamic_nent = 0;
12378 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
12379 ext++)
12380 {
12381 filedata->dynamic_nent++;
12382 if (BYTE_GET (ext->d_tag) == DT_NULL)
12383 break;
12384 }
12385
12386 filedata->dynamic_section
12387 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
12388 if (filedata->dynamic_section == NULL)
12389 {
12390 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
12391 filedata->dynamic_nent);
12392 free (edyn);
12393 return false;
12394 }
12395
12396 for (ext = edyn, entry = filedata->dynamic_section;
12397 entry < filedata->dynamic_section + filedata->dynamic_nent;
12398 ext++, entry++)
12399 {
12400 entry->d_tag = BYTE_GET (ext->d_tag);
12401 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
12402 }
12403
12404 free (edyn);
12405
12406 return true;
12407}
12408
12409static bool
12410get_64bit_dynamic_section (Filedata * filedata)
12411{
12412 Elf64_External_Dyn * edyn;
12413 Elf64_External_Dyn * ext;
12414 Elf_Internal_Dyn * entry;
12415
12416 /* Read in the data. */
12417 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
12418 filedata->dynamic_addr, 1,
12419 filedata->dynamic_size,
12420 _("dynamic section"));
12421 if (!edyn)
12422 return false;
12423
12424 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
12425 might not have the luxury of section headers. Look for the DT_NULL
12426 terminator to determine the number of entries. */
12427 for (ext = edyn, filedata->dynamic_nent = 0;
12428 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
12429 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
12430 ext++)
12431 {
12432 filedata->dynamic_nent++;
12433 if (BYTE_GET (ext->d_tag) == DT_NULL)
12434 break;
12435 }
12436
12437 filedata->dynamic_section
12438 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
12439 if (filedata->dynamic_section == NULL)
12440 {
12441 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
12442 filedata->dynamic_nent);
12443 free (edyn);
12444 return false;
12445 }
12446
12447 /* Convert from external to internal formats. */
12448 for (ext = edyn, entry = filedata->dynamic_section;
12449 entry < filedata->dynamic_section + filedata->dynamic_nent;
12450 ext++, entry++)
12451 {
12452 entry->d_tag = BYTE_GET (ext->d_tag);
12453 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
12454 }
12455
12456 free (edyn);
12457
12458 return true;
12459}
12460
12461static bool
12462get_dynamic_section (Filedata *filedata)
12463{
12464 if (filedata->dynamic_section)
12465 return true;
12466
12467 if (is_32bit_elf)
12468 return get_32bit_dynamic_section (filedata);
12469 else
12470 return get_64bit_dynamic_section (filedata);
12471}
12472
12473static void
12474print_dynamic_flags (uint64_t flags)
12475{
12476 bool first = true;
12477
12478 while (flags)
12479 {
12480 uint64_t flag;
12481
12482 flag = flags & - flags;
12483 flags &= ~ flag;
12484
12485 if (first)
12486 first = false;
12487 else
12488 putc (' ', stdout);
12489
12490 switch (flag)
12491 {
12492 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
12493 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
12494 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
12495 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
12496 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
12497 default: fputs (_("unknown"), stdout); break;
12498 }
12499 }
12500 puts ("");
12501}
12502
12503static uint64_t *
12504get_dynamic_data (Filedata * filedata, uint64_t number, unsigned int ent_size)
12505{
12506 unsigned char * e_data;
12507 uint64_t * i_data;
12508
12509 /* If size_t is smaller than uint64_t, eg because you are building
12510 on a 32-bit host, then make sure that when number is cast to
12511 size_t no information is lost. */
12512 if ((size_t) number != number
12513 || ent_size * number / ent_size != number)
12514 {
12515 error (_("Size overflow prevents reading %" PRIu64
12516 " elements of size %u\n"),
12517 number, ent_size);
12518 return NULL;
12519 }
12520
12521 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
12522 attempting to allocate memory when the read is bound to fail. */
12523 if (ent_size * number > filedata->file_size)
12524 {
12525 error (_("Invalid number of dynamic entries: %" PRIu64 "\n"),
12526 number);
12527 return NULL;
12528 }
12529
12530 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
12531 if (e_data == NULL)
12532 {
12533 error (_("Out of memory reading %" PRIu64 " dynamic entries\n"),
12534 number);
12535 return NULL;
12536 }
12537
12538 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
12539 {
12540 error (_("Unable to read in %" PRIu64 " bytes of dynamic data\n"),
12541 number * ent_size);
12542 free (e_data);
12543 return NULL;
12544 }
12545
12546 i_data = (uint64_t *) cmalloc ((size_t) number, sizeof (*i_data));
12547 if (i_data == NULL)
12548 {
12549 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
12550 number);
12551 free (e_data);
12552 return NULL;
12553 }
12554
12555 while (number--)
12556 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
12557
12558 free (e_data);
12559
12560 return i_data;
12561}
12562
12563static uint64_t
12564get_num_dynamic_syms (Filedata * filedata)
12565{
12566 uint64_t num_of_syms = 0;
12567
12568 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
12569 return num_of_syms;
12570
12571 if (filedata->dynamic_info[DT_HASH])
12572 {
12573 unsigned char nb[8];
12574 unsigned char nc[8];
12575 unsigned int hash_ent_size = 4;
12576
12577 if ((filedata->file_header.e_machine == EM_ALPHA
12578 || filedata->file_header.e_machine == EM_S390
12579 || filedata->file_header.e_machine == EM_S390_OLD)
12580 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
12581 hash_ent_size = 8;
12582
12583 if (fseek64 (filedata->handle,
12584 (filedata->archive_file_offset
12585 + offset_from_vma (filedata,
12586 filedata->dynamic_info[DT_HASH],
12587 sizeof nb + sizeof nc)),
12588 SEEK_SET))
12589 {
12590 error (_("Unable to seek to start of dynamic information\n"));
12591 goto no_hash;
12592 }
12593
12594 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
12595 {
12596 error (_("Failed to read in number of buckets\n"));
12597 goto no_hash;
12598 }
12599
12600 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
12601 {
12602 error (_("Failed to read in number of chains\n"));
12603 goto no_hash;
12604 }
12605
12606 filedata->nbuckets = byte_get (nb, hash_ent_size);
12607 filedata->nchains = byte_get (nc, hash_ent_size);
12608
12609 if (filedata->nbuckets != 0 && filedata->nchains != 0)
12610 {
12611 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
12612 hash_ent_size);
12613 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
12614 hash_ent_size);
12615
12616 if (filedata->buckets != NULL && filedata->chains != NULL)
12617 num_of_syms = filedata->nchains;
12618 }
12619 no_hash:
12620 if (num_of_syms == 0)
12621 {
12622 free (filedata->buckets);
12623 filedata->buckets = NULL;
12624 free (filedata->chains);
12625 filedata->chains = NULL;
12626 filedata->nbuckets = 0;
12627 }
12628 }
12629
12630 if (filedata->dynamic_info_DT_GNU_HASH)
12631 {
12632 unsigned char nb[16];
12633 uint64_t i, maxchain = 0xffffffff, bitmaskwords;
12634 uint64_t buckets_vma;
12635 uint64_t hn;
12636
12637 if (fseek64 (filedata->handle,
12638 (filedata->archive_file_offset
12639 + offset_from_vma (filedata,
12640 filedata->dynamic_info_DT_GNU_HASH,
12641 sizeof nb)),
12642 SEEK_SET))
12643 {
12644 error (_("Unable to seek to start of dynamic information\n"));
12645 goto no_gnu_hash;
12646 }
12647
12648 if (fread (nb, 16, 1, filedata->handle) != 1)
12649 {
12650 error (_("Failed to read in number of buckets\n"));
12651 goto no_gnu_hash;
12652 }
12653
12654 filedata->ngnubuckets = byte_get (nb, 4);
12655 filedata->gnusymidx = byte_get (nb + 4, 4);
12656 bitmaskwords = byte_get (nb + 8, 4);
12657 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
12658 if (is_32bit_elf)
12659 buckets_vma += bitmaskwords * 4;
12660 else
12661 buckets_vma += bitmaskwords * 8;
12662
12663 if (fseek64 (filedata->handle,
12664 (filedata->archive_file_offset
12665 + offset_from_vma (filedata, buckets_vma, 4)),
12666 SEEK_SET))
12667 {
12668 error (_("Unable to seek to start of dynamic information\n"));
12669 goto no_gnu_hash;
12670 }
12671
12672 filedata->gnubuckets
12673 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
12674
12675 if (filedata->gnubuckets == NULL)
12676 goto no_gnu_hash;
12677
12678 for (i = 0; i < filedata->ngnubuckets; i++)
12679 if (filedata->gnubuckets[i] != 0)
12680 {
12681 if (filedata->gnubuckets[i] < filedata->gnusymidx)
12682 goto no_gnu_hash;
12683
12684 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
12685 maxchain = filedata->gnubuckets[i];
12686 }
12687
12688 if (maxchain == 0xffffffff)
12689 goto no_gnu_hash;
12690
12691 maxchain -= filedata->gnusymidx;
12692
12693 if (fseek64 (filedata->handle,
12694 (filedata->archive_file_offset
12695 + offset_from_vma (filedata,
12696 buckets_vma + 4 * (filedata->ngnubuckets
12697 + maxchain),
12698 4)),
12699 SEEK_SET))
12700 {
12701 error (_("Unable to seek to start of dynamic information\n"));
12702 goto no_gnu_hash;
12703 }
12704
12705 do
12706 {
12707 if (fread (nb, 4, 1, filedata->handle) != 1)
12708 {
12709 error (_("Failed to determine last chain length\n"));
12710 goto no_gnu_hash;
12711 }
12712
12713 if (maxchain + 1 == 0)
12714 goto no_gnu_hash;
12715
12716 ++maxchain;
12717 }
12718 while ((byte_get (nb, 4) & 1) == 0);
12719
12720 if (fseek64 (filedata->handle,
12721 (filedata->archive_file_offset
12722 + offset_from_vma (filedata, (buckets_vma
12723 + 4 * filedata->ngnubuckets),
12724 4)),
12725 SEEK_SET))
12726 {
12727 error (_("Unable to seek to start of dynamic information\n"));
12728 goto no_gnu_hash;
12729 }
12730
12731 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
12732 filedata->ngnuchains = maxchain;
12733
12734 if (filedata->gnuchains == NULL)
12735 goto no_gnu_hash;
12736
12737 if (filedata->dynamic_info_DT_MIPS_XHASH)
12738 {
12739 if (fseek64 (filedata->handle,
12740 (filedata->archive_file_offset
12741 + offset_from_vma (filedata, (buckets_vma
12742 + 4 * (filedata->ngnubuckets
12743 + maxchain)), 4)),
12744 SEEK_SET))
12745 {
12746 error (_("Unable to seek to start of dynamic information\n"));
12747 goto no_gnu_hash;
12748 }
12749
12750 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
12751 if (filedata->mipsxlat == NULL)
12752 goto no_gnu_hash;
12753 }
12754
12755 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
12756 if (filedata->gnubuckets[hn] != 0)
12757 {
12758 uint64_t si = filedata->gnubuckets[hn];
12759 uint64_t off = si - filedata->gnusymidx;
12760
12761 do
12762 {
12763 if (filedata->dynamic_info_DT_MIPS_XHASH)
12764 {
12765 if (off < filedata->ngnuchains
12766 && filedata->mipsxlat[off] >= num_of_syms)
12767 num_of_syms = filedata->mipsxlat[off] + 1;
12768 }
12769 else
12770 {
12771 if (si >= num_of_syms)
12772 num_of_syms = si + 1;
12773 }
12774 si++;
12775 }
12776 while (off < filedata->ngnuchains
12777 && (filedata->gnuchains[off++] & 1) == 0);
12778 }
12779
12780 if (num_of_syms == 0)
12781 {
12782 no_gnu_hash:
12783 free (filedata->mipsxlat);
12784 filedata->mipsxlat = NULL;
12785 free (filedata->gnuchains);
12786 filedata->gnuchains = NULL;
12787 free (filedata->gnubuckets);
12788 filedata->gnubuckets = NULL;
12789 filedata->ngnubuckets = 0;
12790 filedata->ngnuchains = 0;
12791 }
12792 }
12793
12794 return num_of_syms;
12795}
12796
12797/* Parse and display the contents of the dynamic section. */
12798
12799static bool
12800process_dynamic_section (Filedata * filedata)
12801{
12802 Elf_Internal_Dyn * entry;
12803
12804 if (filedata->dynamic_size <= 1)
12805 {
12806 if (do_dynamic)
12807 {
12808 if (filedata->is_separate)
12809 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
12810 printable_string (filedata->file_name, 0));
12811 else
12812 printf (_("\nThere is no dynamic section in this file.\n"));
12813 }
12814
12815 return true;
12816 }
12817
12818 if (!get_dynamic_section (filedata))
12819 return false;
12820
12821 /* Find the appropriate symbol table. */
12822 if (filedata->dynamic_symbols == NULL || do_histogram)
12823 {
12824 uint64_t num_of_syms;
12825
12826 for (entry = filedata->dynamic_section;
12827 entry < filedata->dynamic_section + filedata->dynamic_nent;
12828 ++entry)
12829 if (entry->d_tag == DT_SYMTAB)
12830 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
12831 else if (entry->d_tag == DT_SYMENT)
12832 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
12833 else if (entry->d_tag == DT_HASH)
12834 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
12835 else if (entry->d_tag == DT_GNU_HASH)
12836 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
12837 else if ((filedata->file_header.e_machine == EM_MIPS
12838 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
12839 && entry->d_tag == DT_MIPS_XHASH)
12840 {
12841 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
12842 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
12843 }
12844
12845 num_of_syms = get_num_dynamic_syms (filedata);
12846
12847 if (num_of_syms != 0
12848 && filedata->dynamic_symbols == NULL
12849 && filedata->dynamic_info[DT_SYMTAB]
12850 && filedata->dynamic_info[DT_SYMENT])
12851 {
12852 Elf_Internal_Phdr *seg;
12853 uint64_t vma = filedata->dynamic_info[DT_SYMTAB];
12854
12855 if (! get_program_headers (filedata))
12856 {
12857 error (_("Cannot interpret virtual addresses "
12858 "without program headers.\n"));
12859 return false;
12860 }
12861
12862 for (seg = filedata->program_headers;
12863 seg < filedata->program_headers + filedata->file_header.e_phnum;
12864 ++seg)
12865 {
12866 if (seg->p_type != PT_LOAD)
12867 continue;
12868
12869 if (seg->p_offset + seg->p_filesz > filedata->file_size)
12870 {
12871 /* See PR 21379 for a reproducer. */
12872 error (_("Invalid PT_LOAD entry\n"));
12873 return false;
12874 }
12875
12876 if (vma >= (seg->p_vaddr & -seg->p_align)
12877 && vma < seg->p_vaddr + seg->p_filesz)
12878 {
12879 /* Since we do not know how big the symbol table is,
12880 we default to reading in up to the end of PT_LOAD
12881 segment and processing that. This is overkill, I
12882 know, but it should work. */
12883 Elf_Internal_Shdr section;
12884 section.sh_offset = (vma - seg->p_vaddr
12885 + seg->p_offset);
12886 section.sh_size = (num_of_syms
12887 * filedata->dynamic_info[DT_SYMENT]);
12888 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
12889
12890 if (do_checks
12891 && filedata->dynamic_symtab_section != NULL
12892 && ((filedata->dynamic_symtab_section->sh_offset
12893 != section.sh_offset)
12894 || (filedata->dynamic_symtab_section->sh_size
12895 != section.sh_size)
12896 || (filedata->dynamic_symtab_section->sh_entsize
12897 != section.sh_entsize)))
12898 warn (_("\
12899the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
12900
12901 section.sh_name = filedata->string_table_length;
12902 filedata->dynamic_symbols
12903 = get_elf_symbols (filedata, &section,
12904 &filedata->num_dynamic_syms);
12905 if (filedata->dynamic_symbols == NULL
12906 || filedata->num_dynamic_syms != num_of_syms)
12907 {
12908 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
12909 return false;
12910 }
12911 break;
12912 }
12913 }
12914 }
12915 }
12916
12917 /* Similarly find a string table. */
12918 if (filedata->dynamic_strings == NULL)
12919 for (entry = filedata->dynamic_section;
12920 entry < filedata->dynamic_section + filedata->dynamic_nent;
12921 ++entry)
12922 {
12923 if (entry->d_tag == DT_STRTAB)
12924 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
12925
12926 if (entry->d_tag == DT_STRSZ)
12927 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
12928
12929 if (filedata->dynamic_info[DT_STRTAB]
12930 && filedata->dynamic_info[DT_STRSZ])
12931 {
12932 uint64_t offset;
12933 uint64_t str_tab_len = filedata->dynamic_info[DT_STRSZ];
12934
12935 offset = offset_from_vma (filedata,
12936 filedata->dynamic_info[DT_STRTAB],
12937 str_tab_len);
12938 if (do_checks
12939 && filedata->dynamic_strtab_section
12940 && ((filedata->dynamic_strtab_section->sh_offset
12941 != (file_ptr) offset)
12942 || (filedata->dynamic_strtab_section->sh_size
12943 != str_tab_len)))
12944 warn (_("\
12945the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
12946
12947 filedata->dynamic_strings
12948 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
12949 _("dynamic string table"));
12950 if (filedata->dynamic_strings == NULL)
12951 {
12952 error (_("Corrupt DT_STRTAB dynamic entry\n"));
12953 break;
12954 }
12955
12956 filedata->dynamic_strings_length = str_tab_len;
12957 break;
12958 }
12959 }
12960
12961 /* And find the syminfo section if available. */
12962 if (filedata->dynamic_syminfo == NULL)
12963 {
12964 uint64_t syminsz = 0;
12965
12966 for (entry = filedata->dynamic_section;
12967 entry < filedata->dynamic_section + filedata->dynamic_nent;
12968 ++entry)
12969 {
12970 if (entry->d_tag == DT_SYMINENT)
12971 {
12972 /* Note: these braces are necessary to avoid a syntax
12973 error from the SunOS4 C compiler. */
12974 /* PR binutils/17531: A corrupt file can trigger this test.
12975 So do not use an assert, instead generate an error message. */
12976 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
12977 error (_("Bad value (%d) for SYMINENT entry\n"),
12978 (int) entry->d_un.d_val);
12979 }
12980 else if (entry->d_tag == DT_SYMINSZ)
12981 syminsz = entry->d_un.d_val;
12982 else if (entry->d_tag == DT_SYMINFO)
12983 filedata->dynamic_syminfo_offset
12984 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
12985 }
12986
12987 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
12988 {
12989 Elf_External_Syminfo * extsyminfo;
12990 Elf_External_Syminfo * extsym;
12991 Elf_Internal_Syminfo * syminfo;
12992
12993 /* There is a syminfo section. Read the data. */
12994 extsyminfo = (Elf_External_Syminfo *)
12995 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
12996 1, syminsz, _("symbol information"));
12997 if (!extsyminfo)
12998 return false;
12999
13000 if (filedata->dynamic_syminfo != NULL)
13001 {
13002 error (_("Multiple dynamic symbol information sections found\n"));
13003 free (filedata->dynamic_syminfo);
13004 }
13005 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
13006 if (filedata->dynamic_syminfo == NULL)
13007 {
13008 error (_("Out of memory allocating %" PRIu64
13009 " bytes for dynamic symbol info\n"),
13010 syminsz);
13011 return false;
13012 }
13013
13014 filedata->dynamic_syminfo_nent
13015 = syminsz / sizeof (Elf_External_Syminfo);
13016 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
13017 syminfo < (filedata->dynamic_syminfo
13018 + filedata->dynamic_syminfo_nent);
13019 ++syminfo, ++extsym)
13020 {
13021 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
13022 syminfo->si_flags = BYTE_GET (extsym->si_flags);
13023 }
13024
13025 free (extsyminfo);
13026 }
13027 }
13028
13029 if (do_dynamic && filedata->dynamic_addr)
13030 {
13031 if (filedata->is_separate)
13032 printf (ngettext ("\nIn linked file '%s' the dynamic section at offset %#" PRIx64 " contains %" PRIu64 " entry:\n",
13033 "\nIn linked file '%s' the dynamic section at offset %#" PRIx64 " contains %" PRIu64 " entries:\n",
13034 filedata->dynamic_nent),
13035 filedata->file_name,
13036 filedata->dynamic_addr,
13037 filedata->dynamic_nent);
13038 else
13039 printf (ngettext ("\nDynamic section at offset %#" PRIx64 " contains %" PRIu64 " entry:\n",
13040 "\nDynamic section at offset %#" PRIx64 " contains %" PRIu64 " entries:\n",
13041 filedata->dynamic_nent),
13042 filedata->dynamic_addr,
13043 filedata->dynamic_nent);
13044 }
13045 if (do_dynamic)
13046 printf (_(" Tag Type Name/Value\n"));
13047
13048 for (entry = filedata->dynamic_section;
13049 entry < filedata->dynamic_section + filedata->dynamic_nent;
13050 entry++)
13051 {
13052 if (do_dynamic)
13053 {
13054 const char * dtype;
13055
13056 putchar (' ');
13057 print_vma (entry->d_tag, FULL_HEX);
13058 dtype = get_dynamic_type (filedata, entry->d_tag);
13059 printf (" (%s)%*s", dtype,
13060 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
13061 }
13062
13063 switch (entry->d_tag)
13064 {
13065 case DT_FLAGS:
13066 if (do_dynamic)
13067 print_dynamic_flags (entry->d_un.d_val);
13068 break;
13069
13070 case DT_AUXILIARY:
13071 case DT_FILTER:
13072 case DT_CONFIG:
13073 case DT_DEPAUDIT:
13074 case DT_AUDIT:
13075 if (do_dynamic)
13076 {
13077 switch (entry->d_tag)
13078 {
13079 case DT_AUXILIARY:
13080 printf (_("Auxiliary library"));
13081 break;
13082
13083 case DT_FILTER:
13084 printf (_("Filter library"));
13085 break;
13086
13087 case DT_CONFIG:
13088 printf (_("Configuration file"));
13089 break;
13090
13091 case DT_DEPAUDIT:
13092 printf (_("Dependency audit library"));
13093 break;
13094
13095 case DT_AUDIT:
13096 printf (_("Audit library"));
13097 break;
13098 }
13099
13100 if (valid_dynamic_name (filedata, entry->d_un.d_val))
13101 printf (": [%s]\n",
13102 get_dynamic_name (filedata, entry->d_un.d_val));
13103 else
13104 {
13105 printf (": ");
13106 print_vma (entry->d_un.d_val, PREFIX_HEX);
13107 putchar ('\n');
13108 }
13109 }
13110 break;
13111
13112 case DT_FEATURE:
13113 if (do_dynamic)
13114 {
13115 printf (_("Flags:"));
13116
13117 if (entry->d_un.d_val == 0)
13118 printf (_(" None\n"));
13119 else
13120 {
13121 uint64_t val = entry->d_un.d_val;
13122
13123 if (val & DTF_1_PARINIT)
13124 {
13125 printf (" PARINIT");
13126 val ^= DTF_1_PARINIT;
13127 }
13128 if (val & DTF_1_CONFEXP)
13129 {
13130 printf (" CONFEXP");
13131 val ^= DTF_1_CONFEXP;
13132 }
13133 if (val != 0)
13134 printf (" %" PRIx64, val);
13135 puts ("");
13136 }
13137 }
13138 break;
13139
13140 case DT_POSFLAG_1:
13141 if (do_dynamic)
13142 {
13143 printf (_("Flags:"));
13144
13145 if (entry->d_un.d_val == 0)
13146 printf (_(" None\n"));
13147 else
13148 {
13149 uint64_t val = entry->d_un.d_val;
13150
13151 if (val & DF_P1_LAZYLOAD)
13152 {
13153 printf (" LAZYLOAD");
13154 val ^= DF_P1_LAZYLOAD;
13155 }
13156 if (val & DF_P1_GROUPPERM)
13157 {
13158 printf (" GROUPPERM");
13159 val ^= DF_P1_GROUPPERM;
13160 }
13161 if (val != 0)
13162 printf (" %" PRIx64, val);
13163 puts ("");
13164 }
13165 }
13166 break;
13167
13168 case DT_FLAGS_1:
13169 if (do_dynamic)
13170 {
13171 printf (_("Flags:"));
13172 if (entry->d_un.d_val == 0)
13173 printf (_(" None\n"));
13174 else
13175 {
13176 uint64_t val = entry->d_un.d_val;
13177
13178 if (val & DF_1_NOW)
13179 {
13180 printf (" NOW");
13181 val ^= DF_1_NOW;
13182 }
13183 if (val & DF_1_GLOBAL)
13184 {
13185 printf (" GLOBAL");
13186 val ^= DF_1_GLOBAL;
13187 }
13188 if (val & DF_1_GROUP)
13189 {
13190 printf (" GROUP");
13191 val ^= DF_1_GROUP;
13192 }
13193 if (val & DF_1_NODELETE)
13194 {
13195 printf (" NODELETE");
13196 val ^= DF_1_NODELETE;
13197 }
13198 if (val & DF_1_LOADFLTR)
13199 {
13200 printf (" LOADFLTR");
13201 val ^= DF_1_LOADFLTR;
13202 }
13203 if (val & DF_1_INITFIRST)
13204 {
13205 printf (" INITFIRST");
13206 val ^= DF_1_INITFIRST;
13207 }
13208 if (val & DF_1_NOOPEN)
13209 {
13210 printf (" NOOPEN");
13211 val ^= DF_1_NOOPEN;
13212 }
13213 if (val & DF_1_ORIGIN)
13214 {
13215 printf (" ORIGIN");
13216 val ^= DF_1_ORIGIN;
13217 }
13218 if (val & DF_1_DIRECT)
13219 {
13220 printf (" DIRECT");
13221 val ^= DF_1_DIRECT;
13222 }
13223 if (val & DF_1_TRANS)
13224 {
13225 printf (" TRANS");
13226 val ^= DF_1_TRANS;
13227 }
13228 if (val & DF_1_INTERPOSE)
13229 {
13230 printf (" INTERPOSE");
13231 val ^= DF_1_INTERPOSE;
13232 }
13233 if (val & DF_1_NODEFLIB)
13234 {
13235 printf (" NODEFLIB");
13236 val ^= DF_1_NODEFLIB;
13237 }
13238 if (val & DF_1_NODUMP)
13239 {
13240 printf (" NODUMP");
13241 val ^= DF_1_NODUMP;
13242 }
13243 if (val & DF_1_CONFALT)
13244 {
13245 printf (" CONFALT");
13246 val ^= DF_1_CONFALT;
13247 }
13248 if (val & DF_1_ENDFILTEE)
13249 {
13250 printf (" ENDFILTEE");
13251 val ^= DF_1_ENDFILTEE;
13252 }
13253 if (val & DF_1_DISPRELDNE)
13254 {
13255 printf (" DISPRELDNE");
13256 val ^= DF_1_DISPRELDNE;
13257 }
13258 if (val & DF_1_DISPRELPND)
13259 {
13260 printf (" DISPRELPND");
13261 val ^= DF_1_DISPRELPND;
13262 }
13263 if (val & DF_1_NODIRECT)
13264 {
13265 printf (" NODIRECT");
13266 val ^= DF_1_NODIRECT;
13267 }
13268 if (val & DF_1_IGNMULDEF)
13269 {
13270 printf (" IGNMULDEF");
13271 val ^= DF_1_IGNMULDEF;
13272 }
13273 if (val & DF_1_NOKSYMS)
13274 {
13275 printf (" NOKSYMS");
13276 val ^= DF_1_NOKSYMS;
13277 }
13278 if (val & DF_1_NOHDR)
13279 {
13280 printf (" NOHDR");
13281 val ^= DF_1_NOHDR;
13282 }
13283 if (val & DF_1_EDITED)
13284 {
13285 printf (" EDITED");
13286 val ^= DF_1_EDITED;
13287 }
13288 if (val & DF_1_NORELOC)
13289 {
13290 printf (" NORELOC");
13291 val ^= DF_1_NORELOC;
13292 }
13293 if (val & DF_1_SYMINTPOSE)
13294 {
13295 printf (" SYMINTPOSE");
13296 val ^= DF_1_SYMINTPOSE;
13297 }
13298 if (val & DF_1_GLOBAUDIT)
13299 {
13300 printf (" GLOBAUDIT");
13301 val ^= DF_1_GLOBAUDIT;
13302 }
13303 if (val & DF_1_SINGLETON)
13304 {
13305 printf (" SINGLETON");
13306 val ^= DF_1_SINGLETON;
13307 }
13308 if (val & DF_1_STUB)
13309 {
13310 printf (" STUB");
13311 val ^= DF_1_STUB;
13312 }
13313 if (val & DF_1_PIE)
13314 {
13315 printf (" PIE");
13316 val ^= DF_1_PIE;
13317 }
13318 if (val & DF_1_KMOD)
13319 {
13320 printf (" KMOD");
13321 val ^= DF_1_KMOD;
13322 }
13323 if (val & DF_1_WEAKFILTER)
13324 {
13325 printf (" WEAKFILTER");
13326 val ^= DF_1_WEAKFILTER;
13327 }
13328 if (val & DF_1_NOCOMMON)
13329 {
13330 printf (" NOCOMMON");
13331 val ^= DF_1_NOCOMMON;
13332 }
13333 if (val != 0)
13334 printf (" %" PRIx64, val);
13335 puts ("");
13336 }
13337 }
13338 break;
13339
13340 case DT_PLTREL:
13341 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
13342 if (do_dynamic)
13343 puts (get_dynamic_type (filedata, entry->d_un.d_val));
13344 break;
13345
13346 case DT_NULL :
13347 case DT_NEEDED :
13348 case DT_PLTGOT :
13349 case DT_HASH :
13350 case DT_STRTAB :
13351 case DT_SYMTAB :
13352 case DT_RELA :
13353 case DT_INIT :
13354 case DT_FINI :
13355 case DT_SONAME :
13356 case DT_RPATH :
13357 case DT_SYMBOLIC:
13358 case DT_REL :
13359 case DT_RELR :
13360 case DT_DEBUG :
13361 case DT_TEXTREL :
13362 case DT_JMPREL :
13363 case DT_RUNPATH :
13364 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
13365
13366 if (do_dynamic)
13367 {
13368 const char *name;
13369
13370 if (valid_dynamic_name (filedata, entry->d_un.d_val))
13371 name = get_dynamic_name (filedata, entry->d_un.d_val);
13372 else
13373 name = NULL;
13374
13375 if (name)
13376 {
13377 switch (entry->d_tag)
13378 {
13379 case DT_NEEDED:
13380 printf (_("Shared library: [%s]"), name);
13381
13382 if (filedata->program_interpreter
13383 && streq (name, filedata->program_interpreter))
13384 printf (_(" program interpreter"));
13385 break;
13386
13387 case DT_SONAME:
13388 printf (_("Library soname: [%s]"), name);
13389 break;
13390
13391 case DT_RPATH:
13392 printf (_("Library rpath: [%s]"), name);
13393 break;
13394
13395 case DT_RUNPATH:
13396 printf (_("Library runpath: [%s]"), name);
13397 break;
13398
13399 default:
13400 print_vma (entry->d_un.d_val, PREFIX_HEX);
13401 break;
13402 }
13403 }
13404 else
13405 print_vma (entry->d_un.d_val, PREFIX_HEX);
13406
13407 putchar ('\n');
13408 }
13409 break;
13410
13411 case DT_PLTRELSZ:
13412 case DT_RELASZ :
13413 case DT_STRSZ :
13414 case DT_RELSZ :
13415 case DT_RELAENT :
13416 case DT_RELRENT :
13417 case DT_RELRSZ :
13418 case DT_SYMENT :
13419 case DT_RELENT :
13420 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
13421 /* Fall through. */
13422 case DT_PLTPADSZ:
13423 case DT_MOVEENT :
13424 case DT_MOVESZ :
13425 case DT_PREINIT_ARRAYSZ:
13426 case DT_INIT_ARRAYSZ:
13427 case DT_FINI_ARRAYSZ:
13428 case DT_GNU_CONFLICTSZ:
13429 case DT_GNU_LIBLISTSZ:
13430 if (do_dynamic)
13431 {
13432 print_vma (entry->d_un.d_val, UNSIGNED);
13433 printf (_(" (bytes)\n"));
13434 }
13435 break;
13436
13437 case DT_VERDEFNUM:
13438 case DT_VERNEEDNUM:
13439 case DT_RELACOUNT:
13440 case DT_RELCOUNT:
13441 if (do_dynamic)
13442 {
13443 print_vma (entry->d_un.d_val, UNSIGNED);
13444 putchar ('\n');
13445 }
13446 break;
13447
13448 case DT_SYMINSZ:
13449 case DT_SYMINENT:
13450 case DT_SYMINFO:
13451 case DT_USED:
13452 case DT_INIT_ARRAY:
13453 case DT_FINI_ARRAY:
13454 if (do_dynamic)
13455 {
13456 if (entry->d_tag == DT_USED
13457 && valid_dynamic_name (filedata, entry->d_un.d_val))
13458 {
13459 const char *name
13460 = get_dynamic_name (filedata, entry->d_un.d_val);
13461
13462 if (*name)
13463 {
13464 printf (_("Not needed object: [%s]\n"), name);
13465 break;
13466 }
13467 }
13468
13469 print_vma (entry->d_un.d_val, PREFIX_HEX);
13470 putchar ('\n');
13471 }
13472 break;
13473
13474 case DT_BIND_NOW:
13475 /* The value of this entry is ignored. */
13476 if (do_dynamic)
13477 putchar ('\n');
13478 break;
13479
13480 case DT_GNU_PRELINKED:
13481 if (do_dynamic)
13482 {
13483 struct tm * tmp;
13484 time_t atime = entry->d_un.d_val;
13485
13486 tmp = gmtime (&atime);
13487 /* PR 17533 file: 041-1244816-0.004. */
13488 if (tmp == NULL)
13489 printf (_("<corrupt time val: %" PRIx64),
13490 (uint64_t) atime);
13491 else
13492 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
13493 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
13494 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
13495
13496 }
13497 break;
13498
13499 case DT_GNU_HASH:
13500 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
13501 if (do_dynamic)
13502 {
13503 print_vma (entry->d_un.d_val, PREFIX_HEX);
13504 putchar ('\n');
13505 }
13506 break;
13507
13508 case DT_GNU_FLAGS_1:
13509 if (do_dynamic)
13510 {
13511 printf (_("Flags:"));
13512 if (entry->d_un.d_val == 0)
13513 printf (_(" None\n"));
13514 else
13515 {
13516 uint64_t val = entry->d_un.d_val;
13517
13518 if (val & DF_GNU_1_UNIQUE)
13519 {
13520 printf (" UNIQUE");
13521 val ^= DF_GNU_1_UNIQUE;
13522 }
13523 if (val != 0)
13524 printf (" %" PRIx64, val);
13525 puts ("");
13526 }
13527 }
13528 break;
13529
13530 default:
13531 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
13532 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
13533 = entry->d_un.d_val;
13534
13535 if (do_dynamic)
13536 {
13537 switch (filedata->file_header.e_machine)
13538 {
13539 case EM_AARCH64:
13540 dynamic_section_aarch64_val (entry);
13541 break;
13542 case EM_MIPS:
13543 case EM_MIPS_RS3_LE:
13544 dynamic_section_mips_val (filedata, entry);
13545 break;
13546 case EM_PARISC:
13547 dynamic_section_parisc_val (entry);
13548 break;
13549 case EM_IA_64:
13550 dynamic_section_ia64_val (entry);
13551 break;
13552 default:
13553 print_vma (entry->d_un.d_val, PREFIX_HEX);
13554 putchar ('\n');
13555 }
13556 }
13557 break;
13558 }
13559 }
13560
13561 return true;
13562}
13563
13564static char *
13565get_ver_flags (unsigned int flags)
13566{
13567 static char buff[128];
13568
13569 buff[0] = 0;
13570
13571 if (flags == 0)
13572 return _("none");
13573
13574 if (flags & VER_FLG_BASE)
13575 strcat (buff, "BASE");
13576
13577 if (flags & VER_FLG_WEAK)
13578 {
13579 if (flags & VER_FLG_BASE)
13580 strcat (buff, " | ");
13581
13582 strcat (buff, "WEAK");
13583 }
13584
13585 if (flags & VER_FLG_INFO)
13586 {
13587 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
13588 strcat (buff, " | ");
13589
13590 strcat (buff, "INFO");
13591 }
13592
13593 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
13594 {
13595 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
13596 strcat (buff, " | ");
13597
13598 strcat (buff, _("<unknown>"));
13599 }
13600
13601 return buff;
13602}
13603
13604/* Display the contents of the version sections. */
13605
13606static bool
13607process_version_sections (Filedata * filedata)
13608{
13609 Elf_Internal_Shdr * section;
13610 unsigned i;
13611 bool found = false;
13612
13613 if (! do_version)
13614 return true;
13615
13616 for (i = 0, section = filedata->section_headers;
13617 i < filedata->file_header.e_shnum;
13618 i++, section++)
13619 {
13620 switch (section->sh_type)
13621 {
13622 case SHT_GNU_verdef:
13623 {
13624 Elf_External_Verdef * edefs;
13625 size_t idx;
13626 size_t cnt;
13627 char * endbuf;
13628
13629 found = true;
13630
13631 if (filedata->is_separate)
13632 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
13633 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
13634 section->sh_info),
13635 filedata->file_name,
13636 printable_section_name (filedata, section),
13637 section->sh_info);
13638 else
13639 printf (ngettext ("\nVersion definition section '%s' "
13640 "contains %u entry:\n",
13641 "\nVersion definition section '%s' "
13642 "contains %u entries:\n",
13643 section->sh_info),
13644 printable_section_name (filedata, section),
13645 section->sh_info);
13646
13647 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
13648 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
13649 section->sh_offset, section->sh_link,
13650 printable_section_name_from_index (filedata, section->sh_link, NULL));
13651
13652 edefs = (Elf_External_Verdef *)
13653 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
13654 _("version definition section"));
13655 if (!edefs)
13656 break;
13657 endbuf = (char *) edefs + section->sh_size;
13658
13659 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
13660 {
13661 char * vstart;
13662 Elf_External_Verdef * edef;
13663 Elf_Internal_Verdef ent;
13664 Elf_External_Verdaux * eaux;
13665 Elf_Internal_Verdaux aux;
13666 size_t isum;
13667 int j;
13668
13669 vstart = ((char *) edefs) + idx;
13670 if (vstart + sizeof (*edef) > endbuf)
13671 break;
13672
13673 edef = (Elf_External_Verdef *) vstart;
13674
13675 ent.vd_version = BYTE_GET (edef->vd_version);
13676 ent.vd_flags = BYTE_GET (edef->vd_flags);
13677 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
13678 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
13679 ent.vd_hash = BYTE_GET (edef->vd_hash);
13680 ent.vd_aux = BYTE_GET (edef->vd_aux);
13681 ent.vd_next = BYTE_GET (edef->vd_next);
13682
13683 printf (_(" %#06zx: Rev: %d Flags: %s"),
13684 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
13685
13686 printf (_(" Index: %d Cnt: %d "),
13687 ent.vd_ndx, ent.vd_cnt);
13688
13689 /* Check for overflow. */
13690 if (ent.vd_aux > (size_t) (endbuf - vstart))
13691 break;
13692
13693 vstart += ent.vd_aux;
13694
13695 if (vstart + sizeof (*eaux) > endbuf)
13696 break;
13697 eaux = (Elf_External_Verdaux *) vstart;
13698
13699 aux.vda_name = BYTE_GET (eaux->vda_name);
13700 aux.vda_next = BYTE_GET (eaux->vda_next);
13701
13702 if (valid_dynamic_name (filedata, aux.vda_name))
13703 printf (_("Name: %s\n"),
13704 get_dynamic_name (filedata, aux.vda_name));
13705 else
13706 printf (_("Name index: %ld\n"), aux.vda_name);
13707
13708 isum = idx + ent.vd_aux;
13709
13710 for (j = 1; j < ent.vd_cnt; j++)
13711 {
13712 if (aux.vda_next < sizeof (*eaux)
13713 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
13714 {
13715 warn (_("Invalid vda_next field of %lx\n"),
13716 aux.vda_next);
13717 j = ent.vd_cnt;
13718 break;
13719 }
13720 /* Check for overflow. */
13721 if (aux.vda_next > (size_t) (endbuf - vstart))
13722 break;
13723
13724 isum += aux.vda_next;
13725 vstart += aux.vda_next;
13726
13727 if (vstart + sizeof (*eaux) > endbuf)
13728 break;
13729 eaux = (Elf_External_Verdaux *) vstart;
13730
13731 aux.vda_name = BYTE_GET (eaux->vda_name);
13732 aux.vda_next = BYTE_GET (eaux->vda_next);
13733
13734 if (valid_dynamic_name (filedata, aux.vda_name))
13735 printf (_(" %#06zx: Parent %d: %s\n"),
13736 isum, j,
13737 get_dynamic_name (filedata, aux.vda_name));
13738 else
13739 printf (_(" %#06zx: Parent %d, name index: %ld\n"),
13740 isum, j, aux.vda_name);
13741 }
13742
13743 if (j < ent.vd_cnt)
13744 printf (_(" Version def aux past end of section\n"));
13745
13746 /* PR 17531:
13747 file: id:000001,src:000172+005151,op:splice,rep:2. */
13748 if (ent.vd_next < sizeof (*edef)
13749 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
13750 {
13751 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
13752 cnt = section->sh_info;
13753 break;
13754 }
13755 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
13756 break;
13757
13758 idx += ent.vd_next;
13759 }
13760
13761 if (cnt < section->sh_info)
13762 printf (_(" Version definition past end of section\n"));
13763
13764 free (edefs);
13765 }
13766 break;
13767
13768 case SHT_GNU_verneed:
13769 {
13770 Elf_External_Verneed * eneed;
13771 size_t idx;
13772 size_t cnt;
13773 char * endbuf;
13774
13775 found = true;
13776
13777 if (filedata->is_separate)
13778 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
13779 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
13780 section->sh_info),
13781 filedata->file_name,
13782 printable_section_name (filedata, section),
13783 section->sh_info);
13784 else
13785 printf (ngettext ("\nVersion needs section '%s' "
13786 "contains %u entry:\n",
13787 "\nVersion needs section '%s' "
13788 "contains %u entries:\n",
13789 section->sh_info),
13790 printable_section_name (filedata, section),
13791 section->sh_info);
13792
13793 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
13794 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
13795 section->sh_offset, section->sh_link,
13796 printable_section_name_from_index (filedata, section->sh_link, NULL));
13797
13798 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
13799 section->sh_offset, 1,
13800 section->sh_size,
13801 _("Version Needs section"));
13802 if (!eneed)
13803 break;
13804 endbuf = (char *) eneed + section->sh_size;
13805
13806 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
13807 {
13808 Elf_External_Verneed * entry;
13809 Elf_Internal_Verneed ent;
13810 size_t isum;
13811 int j;
13812 char * vstart;
13813
13814 vstart = ((char *) eneed) + idx;
13815 if (vstart + sizeof (*entry) > endbuf)
13816 break;
13817
13818 entry = (Elf_External_Verneed *) vstart;
13819
13820 ent.vn_version = BYTE_GET (entry->vn_version);
13821 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
13822 ent.vn_file = BYTE_GET (entry->vn_file);
13823 ent.vn_aux = BYTE_GET (entry->vn_aux);
13824 ent.vn_next = BYTE_GET (entry->vn_next);
13825
13826 printf (_(" %#06zx: Version: %d"), idx, ent.vn_version);
13827
13828 if (valid_dynamic_name (filedata, ent.vn_file))
13829 printf (_(" File: %s"),
13830 get_dynamic_name (filedata, ent.vn_file));
13831 else
13832 printf (_(" File: %lx"), ent.vn_file);
13833
13834 printf (_(" Cnt: %d\n"), ent.vn_cnt);
13835
13836 /* Check for overflow. */
13837 if (ent.vn_aux > (size_t) (endbuf - vstart))
13838 break;
13839 vstart += ent.vn_aux;
13840
13841 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
13842 {
13843 Elf_External_Vernaux * eaux;
13844 Elf_Internal_Vernaux aux;
13845
13846 if (vstart + sizeof (*eaux) > endbuf)
13847 break;
13848 eaux = (Elf_External_Vernaux *) vstart;
13849
13850 aux.vna_hash = BYTE_GET (eaux->vna_hash);
13851 aux.vna_flags = BYTE_GET (eaux->vna_flags);
13852 aux.vna_other = BYTE_GET (eaux->vna_other);
13853 aux.vna_name = BYTE_GET (eaux->vna_name);
13854 aux.vna_next = BYTE_GET (eaux->vna_next);
13855
13856 if (valid_dynamic_name (filedata, aux.vna_name))
13857 printf (_(" %#06zx: Name: %s"),
13858 isum, get_dynamic_name (filedata, aux.vna_name));
13859 else
13860 printf (_(" %#06zx: Name index: %lx"),
13861 isum, aux.vna_name);
13862
13863 printf (_(" Flags: %s Version: %d\n"),
13864 get_ver_flags (aux.vna_flags), aux.vna_other);
13865
13866 if (aux.vna_next < sizeof (*eaux)
13867 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
13868 {
13869 warn (_("Invalid vna_next field of %lx\n"),
13870 aux.vna_next);
13871 j = ent.vn_cnt;
13872 break;
13873 }
13874 /* Check for overflow. */
13875 if (aux.vna_next > (size_t) (endbuf - vstart))
13876 break;
13877 isum += aux.vna_next;
13878 vstart += aux.vna_next;
13879 }
13880
13881 if (j < ent.vn_cnt)
13882 warn (_("Missing Version Needs auxiliary information\n"));
13883
13884 if (ent.vn_next < sizeof (*entry)
13885 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
13886 {
13887 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
13888 cnt = section->sh_info;
13889 break;
13890 }
13891 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
13892 break;
13893 idx += ent.vn_next;
13894 }
13895
13896 if (cnt < section->sh_info)
13897 warn (_("Missing Version Needs information\n"));
13898
13899 free (eneed);
13900 }
13901 break;
13902
13903 case SHT_GNU_versym:
13904 {
13905 Elf_Internal_Shdr * link_section;
13906 uint64_t total;
13907 unsigned int cnt;
13908 unsigned char * edata;
13909 unsigned short * data;
13910 char * strtab;
13911 Elf_Internal_Sym * symbols;
13912 Elf_Internal_Shdr * string_sec;
13913 uint64_t num_syms;
13914 uint64_t off;
13915
13916 if (section->sh_link >= filedata->file_header.e_shnum)
13917 break;
13918
13919 link_section = filedata->section_headers + section->sh_link;
13920 total = section->sh_size / sizeof (Elf_External_Versym);
13921
13922 if (link_section->sh_link >= filedata->file_header.e_shnum)
13923 break;
13924
13925 found = true;
13926
13927 symbols = get_elf_symbols (filedata, link_section, & num_syms);
13928 if (symbols == NULL)
13929 break;
13930
13931 string_sec = filedata->section_headers + link_section->sh_link;
13932
13933 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
13934 string_sec->sh_size,
13935 _("version string table"));
13936 if (!strtab)
13937 {
13938 free (symbols);
13939 break;
13940 }
13941
13942 if (filedata->is_separate)
13943 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %" PRIu64 " entry:\n",
13944 "\nIn linked file '%s' the version symbols section '%s' contains %" PRIu64 " entries:\n",
13945 total),
13946 filedata->file_name,
13947 printable_section_name (filedata, section),
13948 total);
13949 else
13950 printf (ngettext ("\nVersion symbols section '%s' "
13951 "contains %" PRIu64 " entry:\n",
13952 "\nVersion symbols section '%s' "
13953 "contains %" PRIu64 " entries:\n",
13954 total),
13955 printable_section_name (filedata, section),
13956 total);
13957
13958 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
13959 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
13960 section->sh_offset, section->sh_link,
13961 printable_section_name (filedata, link_section));
13962
13963 off = offset_from_vma (filedata,
13964 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
13965 total * sizeof (short));
13966 edata = (unsigned char *) get_data (NULL, filedata, off,
13967 sizeof (short), total,
13968 _("version symbol data"));
13969 if (!edata)
13970 {
13971 free (strtab);
13972 free (symbols);
13973 break;
13974 }
13975
13976 data = (short unsigned int *) cmalloc (total, sizeof (short));
13977
13978 for (cnt = total; cnt --;)
13979 data[cnt] = byte_get (edata + cnt * sizeof (short),
13980 sizeof (short));
13981
13982 free (edata);
13983
13984 for (cnt = 0; cnt < total; cnt += 4)
13985 {
13986 int j, nn;
13987 char *name;
13988 char *invalid = _("*invalid*");
13989
13990 printf (" %03x:", cnt);
13991
13992 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
13993 switch (data[cnt + j])
13994 {
13995 case 0:
13996 fputs (_(" 0 (*local*) "), stdout);
13997 break;
13998
13999 case 1:
14000 fputs (_(" 1 (*global*) "), stdout);
14001 break;
14002
14003 default:
14004 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
14005 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
14006
14007 /* If this index value is greater than the size of the symbols
14008 array, break to avoid an out-of-bounds read. */
14009 if (cnt + j >= num_syms)
14010 {
14011 warn (_("invalid index into symbol array\n"));
14012 break;
14013 }
14014
14015 name = NULL;
14016 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
14017 {
14018 Elf_Internal_Verneed ivn;
14019 uint64_t offset;
14020
14021 offset = offset_from_vma
14022 (filedata,
14023 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
14024 sizeof (Elf_External_Verneed));
14025
14026 do
14027 {
14028 Elf_Internal_Vernaux ivna;
14029 Elf_External_Verneed evn;
14030 Elf_External_Vernaux evna;
14031 uint64_t a_off;
14032
14033 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
14034 _("version need")) == NULL)
14035 break;
14036
14037 ivn.vn_aux = BYTE_GET (evn.vn_aux);
14038 ivn.vn_next = BYTE_GET (evn.vn_next);
14039
14040 a_off = offset + ivn.vn_aux;
14041
14042 do
14043 {
14044 if (get_data (&evna, filedata, a_off, sizeof (evna),
14045 1, _("version need aux (2)")) == NULL)
14046 {
14047 ivna.vna_next = 0;
14048 ivna.vna_other = 0;
14049 }
14050 else
14051 {
14052 ivna.vna_next = BYTE_GET (evna.vna_next);
14053 ivna.vna_other = BYTE_GET (evna.vna_other);
14054 }
14055
14056 a_off += ivna.vna_next;
14057 }
14058 while (ivna.vna_other != data[cnt + j]
14059 && ivna.vna_next != 0);
14060
14061 if (ivna.vna_other == data[cnt + j])
14062 {
14063 ivna.vna_name = BYTE_GET (evna.vna_name);
14064
14065 if (ivna.vna_name >= string_sec->sh_size)
14066 name = invalid;
14067 else
14068 name = strtab + ivna.vna_name;
14069 break;
14070 }
14071
14072 offset += ivn.vn_next;
14073 }
14074 while (ivn.vn_next);
14075 }
14076
14077 if (data[cnt + j] != (VERSYM_HIDDEN | VERSYM_BASE)
14078 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
14079 {
14080 Elf_Internal_Verdef ivd;
14081 Elf_External_Verdef evd;
14082 uint64_t offset;
14083
14084 offset = offset_from_vma
14085 (filedata,
14086 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
14087 sizeof evd);
14088
14089 do
14090 {
14091 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
14092 _("version def")) == NULL)
14093 {
14094 ivd.vd_next = 0;
14095 /* PR 17531: file: 046-1082287-0.004. */
14096 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
14097 break;
14098 }
14099 else
14100 {
14101 ivd.vd_next = BYTE_GET (evd.vd_next);
14102 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
14103 }
14104
14105 offset += ivd.vd_next;
14106 }
14107 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
14108 && ivd.vd_next != 0);
14109
14110 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
14111 {
14112 Elf_External_Verdaux evda;
14113 Elf_Internal_Verdaux ivda;
14114
14115 ivd.vd_aux = BYTE_GET (evd.vd_aux);
14116
14117 if (get_data (&evda, filedata,
14118 offset - ivd.vd_next + ivd.vd_aux,
14119 sizeof (evda), 1,
14120 _("version def aux")) == NULL)
14121 break;
14122
14123 ivda.vda_name = BYTE_GET (evda.vda_name);
14124
14125 if (ivda.vda_name >= string_sec->sh_size)
14126 name = invalid;
14127 else if (name != NULL && name != invalid)
14128 name = _("*both*");
14129 else
14130 name = strtab + ivda.vda_name;
14131 }
14132 }
14133 if (name != NULL)
14134 nn += printf ("(%s%-*s",
14135 name,
14136 12 - (int) strlen (name),
14137 ")");
14138
14139 if (nn < 18)
14140 printf ("%*c", 18 - nn, ' ');
14141 }
14142
14143 putchar ('\n');
14144 }
14145
14146 free (data);
14147 free (strtab);
14148 free (symbols);
14149 }
14150 break;
14151
14152 default:
14153 break;
14154 }
14155 }
14156
14157 if (! found)
14158 {
14159 if (filedata->is_separate)
14160 printf (_("\nNo version information found in linked file '%s'.\n"),
14161 filedata->file_name);
14162 else
14163 printf (_("\nNo version information found in this file.\n"));
14164 }
14165
14166 return true;
14167}
14168
14169static const char *
14170get_symbol_binding (Filedata * filedata, unsigned int binding)
14171{
14172 static char buff[64];
14173
14174 switch (binding)
14175 {
14176 case STB_LOCAL: return "LOCAL";
14177 case STB_GLOBAL: return "GLOBAL";
14178 case STB_WEAK: return "WEAK";
14179 default:
14180 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
14181 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
14182 binding);
14183 else if (binding >= STB_LOOS && binding <= STB_HIOS)
14184 {
14185 if (binding == STB_GNU_UNIQUE
14186 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
14187 return "UNIQUE";
14188 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
14189 }
14190 else
14191 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
14192 return buff;
14193 }
14194}
14195
14196static const char *
14197get_symbol_type (Filedata * filedata, unsigned int type)
14198{
14199 static char buff[64];
14200
14201 switch (type)
14202 {
14203 case STT_NOTYPE: return "NOTYPE";
14204 case STT_OBJECT: return "OBJECT";
14205 case STT_FUNC: return "FUNC";
14206 case STT_SECTION: return "SECTION";
14207 case STT_FILE: return "FILE";
14208 case STT_COMMON: return "COMMON";
14209 case STT_TLS: return "TLS";
14210 case STT_RELC: return "RELC";
14211 case STT_SRELC: return "SRELC";
14212 default:
14213 if (type >= STT_LOPROC && type <= STT_HIPROC)
14214 {
14215 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
14216 return "THUMB_FUNC";
14217
14218 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
14219 return "REGISTER";
14220
14221 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
14222 return "PARISC_MILLI";
14223
14224 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
14225 }
14226 else if (type >= STT_LOOS && type <= STT_HIOS)
14227 {
14228 if (filedata->file_header.e_machine == EM_PARISC)
14229 {
14230 if (type == STT_HP_OPAQUE)
14231 return "HP_OPAQUE";
14232 if (type == STT_HP_STUB)
14233 return "HP_STUB";
14234 }
14235
14236 if (type == STT_GNU_IFUNC
14237 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
14238 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
14239 return "IFUNC";
14240
14241 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
14242 }
14243 else
14244 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
14245 return buff;
14246 }
14247}
14248
14249static const char *
14250get_symbol_visibility (unsigned int visibility)
14251{
14252 switch (visibility)
14253 {
14254 case STV_DEFAULT: return "DEFAULT";
14255 case STV_INTERNAL: return "INTERNAL";
14256 case STV_HIDDEN: return "HIDDEN";
14257 case STV_PROTECTED: return "PROTECTED";
14258 default:
14259 error (_("Unrecognized visibility value: %u\n"), visibility);
14260 return _("<unknown>");
14261 }
14262}
14263
14264static const char *
14265get_alpha_symbol_other (unsigned int other)
14266{
14267 switch (other)
14268 {
14269 case STO_ALPHA_NOPV: return "NOPV";
14270 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
14271 default:
14272 error (_("Unrecognized alpha specific other value: %u\n"), other);
14273 return _("<unknown>");
14274 }
14275}
14276
14277static const char *
14278get_solaris_symbol_visibility (unsigned int visibility)
14279{
14280 switch (visibility)
14281 {
14282 case 4: return "EXPORTED";
14283 case 5: return "SINGLETON";
14284 case 6: return "ELIMINATE";
14285 default: return get_symbol_visibility (visibility);
14286 }
14287}
14288
14289static const char *
14290get_aarch64_symbol_other (unsigned int other)
14291{
14292 static char buf[32];
14293
14294 if (other & STO_AARCH64_VARIANT_PCS)
14295 {
14296 other &= ~STO_AARCH64_VARIANT_PCS;
14297 if (other == 0)
14298 return "VARIANT_PCS";
14299 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
14300 return buf;
14301 }
14302 return NULL;
14303}
14304
14305static const char *
14306get_mips_symbol_other (unsigned int other)
14307{
14308 switch (other)
14309 {
14310 case STO_OPTIONAL: return "OPTIONAL";
14311 case STO_MIPS_PLT: return "MIPS PLT";
14312 case STO_MIPS_PIC: return "MIPS PIC";
14313 case STO_MICROMIPS: return "MICROMIPS";
14314 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
14315 case STO_MIPS16: return "MIPS16";
14316 default: return NULL;
14317 }
14318}
14319
14320static const char *
14321get_ia64_symbol_other (Filedata * filedata, unsigned int other)
14322{
14323 if (is_ia64_vms (filedata))
14324 {
14325 static char res[32];
14326
14327 res[0] = 0;
14328
14329 /* Function types is for images and .STB files only. */
14330 switch (filedata->file_header.e_type)
14331 {
14332 case ET_DYN:
14333 case ET_EXEC:
14334 switch (VMS_ST_FUNC_TYPE (other))
14335 {
14336 case VMS_SFT_CODE_ADDR:
14337 strcat (res, " CA");
14338 break;
14339 case VMS_SFT_SYMV_IDX:
14340 strcat (res, " VEC");
14341 break;
14342 case VMS_SFT_FD:
14343 strcat (res, " FD");
14344 break;
14345 case VMS_SFT_RESERVE:
14346 strcat (res, " RSV");
14347 break;
14348 default:
14349 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
14350 VMS_ST_FUNC_TYPE (other));
14351 strcat (res, " <unknown>");
14352 break;
14353 }
14354 break;
14355 default:
14356 break;
14357 }
14358 switch (VMS_ST_LINKAGE (other))
14359 {
14360 case VMS_STL_IGNORE:
14361 strcat (res, " IGN");
14362 break;
14363 case VMS_STL_RESERVE:
14364 strcat (res, " RSV");
14365 break;
14366 case VMS_STL_STD:
14367 strcat (res, " STD");
14368 break;
14369 case VMS_STL_LNK:
14370 strcat (res, " LNK");
14371 break;
14372 default:
14373 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
14374 VMS_ST_LINKAGE (other));
14375 strcat (res, " <unknown>");
14376 break;
14377 }
14378
14379 if (res[0] != 0)
14380 return res + 1;
14381 else
14382 return res;
14383 }
14384 return NULL;
14385}
14386
14387static const char *
14388get_ppc64_symbol_other (unsigned int other)
14389{
14390 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
14391 return NULL;
14392
14393 other >>= STO_PPC64_LOCAL_BIT;
14394 if (other <= 6)
14395 {
14396 static char buf[64];
14397 if (other >= 2)
14398 other = ppc64_decode_local_entry (other);
14399 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
14400 return buf;
14401 }
14402 return NULL;
14403}
14404
14405static const char *
14406get_riscv_symbol_other (unsigned int other)
14407{
14408 static char buf[32];
14409 buf[0] = 0;
14410
14411 if (other & STO_RISCV_VARIANT_CC)
14412 {
14413 strcat (buf, _(" VARIANT_CC"));
14414 other &= ~STO_RISCV_VARIANT_CC;
14415 }
14416
14417 if (other != 0)
14418 snprintf (buf, sizeof buf, " %x", other);
14419
14420
14421 if (buf[0] != 0)
14422 return buf + 1;
14423 else
14424 return buf;
14425}
14426
14427static const char *
14428get_symbol_other (Filedata * filedata, unsigned int other)
14429{
14430 const char * result = NULL;
14431 static char buff [64];
14432
14433 if (other == 0)
14434 return "";
14435
14436 switch (filedata->file_header.e_machine)
14437 {
14438 case EM_ALPHA:
14439 result = get_alpha_symbol_other (other);
14440 break;
14441 case EM_AARCH64:
14442 result = get_aarch64_symbol_other (other);
14443 break;
14444 case EM_MIPS:
14445 result = get_mips_symbol_other (other);
14446 break;
14447 case EM_IA_64:
14448 result = get_ia64_symbol_other (filedata, other);
14449 break;
14450 case EM_PPC64:
14451 result = get_ppc64_symbol_other (other);
14452 break;
14453 case EM_RISCV:
14454 result = get_riscv_symbol_other (other);
14455 break;
14456 default:
14457 result = NULL;
14458 break;
14459 }
14460
14461 if (result)
14462 return result;
14463
14464 snprintf (buff, sizeof buff, _("<other>: %x"), other);
14465 return buff;
14466}
14467
14468static const char *
14469get_symbol_version_string (Filedata *filedata,
14470 bool is_dynsym,
14471 const char *strtab,
14472 size_t strtab_size,
14473 unsigned int si,
14474 Elf_Internal_Sym *psym,
14475 enum versioned_symbol_info *sym_info,
14476 unsigned short *vna_other)
14477{
14478 unsigned char data[2];
14479 unsigned short vers_data;
14480 uint64_t offset;
14481 unsigned short max_vd_ndx;
14482
14483 if (!is_dynsym
14484 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
14485 return NULL;
14486
14487 offset = offset_from_vma (filedata,
14488 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
14489 sizeof data + si * sizeof (vers_data));
14490
14491 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
14492 sizeof (data), 1, _("version data")) == NULL)
14493 return NULL;
14494
14495 vers_data = byte_get (data, 2);
14496
14497 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
14498 return NULL;
14499
14500 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
14501 max_vd_ndx = 0;
14502
14503 /* Return the empty string for the hidden base version. */
14504 if (vers_data == (VERSYM_HIDDEN | VERSYM_BASE))
14505 return "";
14506
14507 /* Usually we'd only see verdef for defined symbols, and verneed for
14508 undefined symbols. However, symbols defined by the linker in
14509 .dynbss for variables copied from a shared library in order to
14510 avoid text relocations are defined yet have verneed. We could
14511 use a heuristic to detect the special case, for example, check
14512 for verneed first on symbols defined in SHT_NOBITS sections, but
14513 it is simpler and more reliable to just look for both verdef and
14514 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
14515
14516 if (psym->st_shndx != SHN_UNDEF
14517 && vers_data != (VERSYM_HIDDEN | VERSYM_BASE)
14518 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
14519 {
14520 Elf_Internal_Verdef ivd;
14521 Elf_Internal_Verdaux ivda;
14522 Elf_External_Verdaux evda;
14523 uint64_t off;
14524
14525 off = offset_from_vma (filedata,
14526 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
14527 sizeof (Elf_External_Verdef));
14528
14529 do
14530 {
14531 Elf_External_Verdef evd;
14532
14533 if (get_data (&evd, filedata, off, sizeof (evd), 1,
14534 _("version def")) == NULL)
14535 {
14536 ivd.vd_ndx = 0;
14537 ivd.vd_aux = 0;
14538 ivd.vd_next = 0;
14539 ivd.vd_flags = 0;
14540 }
14541 else
14542 {
14543 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
14544 ivd.vd_aux = BYTE_GET (evd.vd_aux);
14545 ivd.vd_next = BYTE_GET (evd.vd_next);
14546 ivd.vd_flags = BYTE_GET (evd.vd_flags);
14547 }
14548
14549 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
14550 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
14551
14552 off += ivd.vd_next;
14553 }
14554 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
14555
14556 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
14557 {
14558 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
14559 return NULL;
14560
14561 off -= ivd.vd_next;
14562 off += ivd.vd_aux;
14563
14564 if (get_data (&evda, filedata, off, sizeof (evda), 1,
14565 _("version def aux")) != NULL)
14566 {
14567 ivda.vda_name = BYTE_GET (evda.vda_name);
14568
14569 if (psym->st_name != ivda.vda_name)
14570 return (ivda.vda_name < strtab_size
14571 ? strtab + ivda.vda_name : _("<corrupt>"));
14572 }
14573 }
14574 }
14575
14576 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
14577 {
14578 Elf_External_Verneed evn;
14579 Elf_Internal_Verneed ivn;
14580 Elf_Internal_Vernaux ivna;
14581
14582 offset = offset_from_vma (filedata,
14583 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
14584 sizeof evn);
14585 do
14586 {
14587 uint64_t vna_off;
14588
14589 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
14590 _("version need")) == NULL)
14591 {
14592 ivna.vna_next = 0;
14593 ivna.vna_other = 0;
14594 ivna.vna_name = 0;
14595 break;
14596 }
14597
14598 ivn.vn_aux = BYTE_GET (evn.vn_aux);
14599 ivn.vn_next = BYTE_GET (evn.vn_next);
14600
14601 vna_off = offset + ivn.vn_aux;
14602
14603 do
14604 {
14605 Elf_External_Vernaux evna;
14606
14607 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
14608 _("version need aux (3)")) == NULL)
14609 {
14610 ivna.vna_next = 0;
14611 ivna.vna_other = 0;
14612 ivna.vna_name = 0;
14613 }
14614 else
14615 {
14616 ivna.vna_other = BYTE_GET (evna.vna_other);
14617 ivna.vna_next = BYTE_GET (evna.vna_next);
14618 ivna.vna_name = BYTE_GET (evna.vna_name);
14619 }
14620
14621 vna_off += ivna.vna_next;
14622 }
14623 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
14624
14625 if (ivna.vna_other == vers_data)
14626 break;
14627
14628 offset += ivn.vn_next;
14629 }
14630 while (ivn.vn_next != 0);
14631
14632 if (ivna.vna_other == vers_data)
14633 {
14634 *sym_info = symbol_undefined;
14635 *vna_other = ivna.vna_other;
14636 return (ivna.vna_name < strtab_size
14637 ? strtab + ivna.vna_name : _("<corrupt>"));
14638 }
14639 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
14640 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
14641 return _("<corrupt>");
14642 }
14643 return NULL;
14644}
14645
14646/* Display a symbol size on stdout. Format is based on --sym-base setting. */
14647
14648static unsigned int
14649print_symbol_size (uint64_t vma, int base)
14650{
14651 switch (base)
14652 {
14653 case 8:
14654 return print_vma (vma, OCTAL_5);
14655
14656 case 10:
14657 return print_vma (vma, UNSIGNED_5);
14658
14659 case 16:
14660 return print_vma (vma, PREFIX_HEX_5);
14661
14662 case 0:
14663 default:
14664 return print_vma (vma, DEC_5);
14665 }
14666}
14667
14668/* Print information on a single symbol. */
14669
14670static void
14671print_symbol (Filedata * filedata,
14672 uint64_t symbol_index,
14673 Elf_Internal_Sym * symtab,
14674 Elf_Internal_Shdr * section,
14675 char * strtab,
14676 size_t strtab_size)
14677{
14678 const char *version_string;
14679 enum versioned_symbol_info sym_info;
14680 unsigned short vna_other;
14681 const char * sstr;
14682 Elf_Internal_Sym *psym = symtab + symbol_index;
14683
14684 /* FIXME: We should have a table of field widths,
14685 rather than using hard coded constants. */
14686 printf ("%6" PRId64 ": ", symbol_index);
14687 print_vma (psym->st_value, LONG_HEX);
14688 putchar (' ');
14689 print_symbol_size (psym->st_size, sym_base);
14690 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
14691 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
14692 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
14693 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
14694 else
14695 {
14696 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
14697
14698 printf (" %-7s", get_symbol_visibility (vis));
14699
14700 /* Check to see if any other bits in the st_other field are set.
14701 FIXME: Displaying this information here disrupts the layout
14702 of the table being generated. */
14703 if (psym->st_other ^ vis)
14704 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
14705 }
14706
14707 bool is_special;
14708
14709 sstr = printable_section_name_from_index (filedata, psym->st_shndx, & is_special);
14710
14711 /* Print the symbol's section index. If the index is special
14712 then print the index's name rather than its number. */
14713 if (is_special)
14714 {
14715 int printed;
14716
14717 /* Special case: If there are no section headers, and the printable
14718 name is "<section 0x...." then just display the section number
14719 as a decimal. This happens when objcopy --strip -section-headers
14720 is used. */
14721 if (filedata->file_header.e_shnum == 0 && startswith (sstr, "<section"))
14722 printed = printf (" %4d ", psym->st_shndx);
14723 else
14724 printed = printf (" %4s ", sstr);
14725
14726 if (extra_sym_info && printed < 16)
14727 printf ("%*s", 16 - printed, "");
14728 }
14729 else
14730 {
14731 printf (" %4u ", psym->st_shndx);
14732
14733 if (extra_sym_info)
14734 {
14735 /* Display the section name referenced by the section index. */
14736 int printed = printf ("(%s) ", sstr);
14737 if (printed < 10)
14738 printf ("%*s", 10 - printed, "");
14739 }
14740 }
14741
14742 bool is_valid = false;
14743
14744 /* Get the symbol's name. For section symbols without a
14745 specific name use the (already computed) section name. */
14746 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
14747 && section_index_real (filedata, psym->st_shndx)
14748 && psym->st_name == 0)
14749 {
14750 ;
14751 }
14752 else
14753 {
14754 is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
14755 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
14756 }
14757
14758 version_string
14759 = get_symbol_version_string (filedata,
14760 (section == NULL
14761 || section->sh_type == SHT_DYNSYM),
14762 strtab, strtab_size, symbol_index,
14763 psym, &sym_info, &vna_other);
14764
14765 int len_avail = 21;
14766 if (! do_wide && version_string != NULL)
14767 {
14768 char buffer[16];
14769
14770 len_avail -= 1 + strlen (version_string);
14771
14772 if (sym_info == symbol_undefined)
14773 len_avail -= sprintf (buffer," (%d)", vna_other);
14774 else if (sym_info != symbol_hidden)
14775 len_avail -= 1;
14776 }
14777
14778 print_symbol_name (len_avail, sstr);
14779
14780 if (version_string)
14781 {
14782 if (sym_info == symbol_undefined)
14783 printf ("@%s (%d)", version_string, vna_other);
14784 else
14785 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
14786 version_string);
14787 }
14788
14789 putchar ('\n');
14790
14791 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
14792 && section != NULL
14793 && symbol_index >= section->sh_info
14794 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
14795 && filedata->file_header.e_machine != EM_MIPS
14796 /* Solaris binaries have been found to violate this requirement as
14797 well. Not sure if this is a bug or an ABI requirement. */
14798 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
14799 warn (_("local symbol %" PRIu64 " found at index >= %s's sh_info value of %u\n"),
14800 symbol_index, printable_section_name (filedata, section), section->sh_info);
14801
14802 /* Local symbols whose value is larger than their section's size are suspicious
14803 especially if that section is mergeable - and hence might change offsets of
14804 the contents inside the section. */
14805 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
14806 && ! is_special
14807 && is_valid
14808 && psym->st_shndx < filedata->file_header.e_shnum
14809 && filedata->section_headers != NULL
14810 /* FIXME: Should we warn for non-mergeable sections ? */
14811 && (filedata->section_headers[psym->st_shndx].sh_flags & SHF_MERGE)
14812 && psym->st_value > filedata->section_headers[psym->st_shndx].sh_size)
14813 warn (_("local symbol %s has a value (%#" PRIx64 ") which is larger than mergeable section %s's size (%#" PRIx64 ")\n"),
14814 strtab + psym->st_name,
14815 psym->st_value,
14816 printable_section_name_from_index (filedata, psym->st_shndx, NULL),
14817 filedata->section_headers[psym->st_shndx].sh_size);
14818}
14819
14820static const char *
14821get_lto_kind (unsigned int kind)
14822{
14823 switch (kind)
14824 {
14825 case 0: return "DEF";
14826 case 1: return "WEAKDEF";
14827 case 2: return "UNDEF";
14828 case 3: return "WEAKUNDEF";
14829 case 4: return "COMMON";
14830 default:
14831 break;
14832 }
14833
14834 static char buffer[30];
14835 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
14836 sprintf (buffer, "<unknown: %u>", kind);
14837 return buffer;
14838}
14839
14840static const char *
14841get_lto_visibility (unsigned int visibility)
14842{
14843 switch (visibility)
14844 {
14845 case 0: return "DEFAULT";
14846 case 1: return "PROTECTED";
14847 case 2: return "INTERNAL";
14848 case 3: return "HIDDEN";
14849 default:
14850 break;
14851 }
14852
14853 static char buffer[30];
14854 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
14855 sprintf (buffer, "<unknown: %u>", visibility);
14856 return buffer;
14857}
14858
14859static const char *
14860get_lto_sym_type (unsigned int sym_type)
14861{
14862 switch (sym_type)
14863 {
14864 case 0: return "UNKNOWN";
14865 case 1: return "FUNCTION";
14866 case 2: return "VARIABLE";
14867 default:
14868 break;
14869 }
14870
14871 static char buffer[30];
14872 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
14873 sprintf (buffer, "<unknown: %u>", sym_type);
14874 return buffer;
14875}
14876
14877/* Display an LTO format symbol table.
14878 FIXME: The format of LTO symbol tables is not formalized.
14879 So this code could need changing in the future. */
14880
14881static bool
14882display_lto_symtab (Filedata * filedata,
14883 Elf_Internal_Shdr * section)
14884{
14885 if (section->sh_size == 0)
14886 {
14887 if (filedata->is_separate)
14888 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
14889 printable_section_name (filedata, section),
14890 filedata->file_name);
14891 else
14892 printf (_("\nLTO Symbol table '%s' is empty!\n"),
14893 printable_section_name (filedata, section));
14894
14895 return true;
14896 }
14897
14898 if (section->sh_size > filedata->file_size)
14899 {
14900 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
14901 printable_section_name (filedata, section),
14902 section->sh_size);
14903 return false;
14904 }
14905
14906 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
14907 section->sh_size, 1, _("LTO symbols"));
14908 if (alloced_data == NULL)
14909 return false;
14910
14911 /* Look for extended data for the symbol table. */
14912 void * ext_data_orig = NULL;
14913 char * ext_data = NULL;
14914 char * ext_data_end = NULL;
14915 char *ext_name = xasprintf (".gnu.lto_.ext_symtab.%s",
14916 (section_name (filedata, section)
14917 + sizeof (".gnu.lto_.symtab.")));
14918 Elf_Internal_Shdr *ext = find_section (filedata, ext_name);
14919 if (ext != NULL)
14920 {
14921 if (ext->sh_size < 3)
14922 error (_("LTO Symbol extension table '%s' is empty!\n"),
14923 printable_section_name (filedata, ext));
14924 else
14925 {
14926 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
14927 ext->sh_size, 1,
14928 _("LTO ext symbol data"));
14929 if (ext_data != NULL)
14930 {
14931 ext_data_end = ext_data + ext->sh_size;
14932 if (* ext_data++ != 1)
14933 error (_("Unexpected version number in symbol extension table\n"));
14934 }
14935 }
14936 }
14937
14938 const unsigned char * data = (const unsigned char *) alloced_data;
14939 const unsigned char * end = data + section->sh_size;
14940
14941 if (filedata->is_separate)
14942 printf (_("\nIn linked file '%s': "), filedata->file_name);
14943 else
14944 printf ("\n");
14945
14946 if (ext_data_orig != NULL)
14947 {
14948 if (do_wide)
14949 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
14950 printable_section_name (filedata, section),
14951 printable_section_name (filedata, ext));
14952 else
14953 {
14954 printf (_("LTO Symbol table '%s'\n"),
14955 printable_section_name (filedata, section));
14956 printf (_(" and extension table '%s' contain:\n"),
14957 printable_section_name (filedata, ext));
14958 }
14959 }
14960 else
14961 printf (_("LTO Symbol table '%s' contains:\n"),
14962 printable_section_name (filedata, section));
14963
14964 /* FIXME: Add a wide version. */
14965 if (ext_data_orig != NULL)
14966 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
14967 else
14968 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
14969
14970 /* FIXME: We do not handle style prefixes. */
14971
14972 while (data < end)
14973 {
14974 const unsigned char * sym_name = data;
14975 data += strnlen ((const char *) sym_name, end - data) + 1;
14976 if (data >= end)
14977 goto fail;
14978
14979 const unsigned char * comdat_key = data;
14980 data += strnlen ((const char *) comdat_key, end - data) + 1;
14981 if (data >= end)
14982 goto fail;
14983
14984 if (data + 2 + 8 + 4 > end)
14985 goto fail;
14986
14987 unsigned int kind = *data++;
14988 unsigned int visibility = *data++;
14989
14990 uint64_t size = byte_get (data, 8);
14991 data += 8;
14992
14993 uint64_t slot = byte_get (data, 4);
14994 data += 4;
14995
14996 if (ext_data != NULL)
14997 {
14998 if (ext_data < (ext_data_end - 1))
14999 {
15000 unsigned int sym_type = * ext_data ++;
15001 unsigned int sec_kind = * ext_data ++;
15002
15003 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " %9s %08x _",
15004 * comdat_key == 0 ? "-" : (char *) comdat_key,
15005 get_lto_kind (kind),
15006 get_lto_visibility (visibility),
15007 size,
15008 slot,
15009 get_lto_sym_type (sym_type),
15010 sec_kind);
15011 print_symbol_name (6, (const char *) sym_name);
15012 }
15013 else
15014 {
15015 error (_("Ran out of LTO symbol extension data\n"));
15016 ext_data = NULL;
15017 /* FIXME: return FAIL result ? */
15018 }
15019 }
15020 else
15021 {
15022 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " _",
15023 * comdat_key == 0 ? "-" : (char *) comdat_key,
15024 get_lto_kind (kind),
15025 get_lto_visibility (visibility),
15026 size,
15027 slot);
15028 print_symbol_name (21, (const char *) sym_name);
15029 }
15030 putchar ('\n');
15031 }
15032
15033 if (ext_data != NULL && ext_data < ext_data_end)
15034 {
15035 error (_("Data remains in the LTO symbol extension table\n"));
15036 goto fail;
15037 }
15038
15039 free (alloced_data);
15040 free (ext_data_orig);
15041 free (ext_name);
15042 return true;
15043
15044 fail:
15045 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
15046 free (alloced_data);
15047 free (ext_data_orig);
15048 free (ext_name);
15049 return false;
15050}
15051
15052/* Display LTO symbol tables. */
15053
15054static bool
15055process_lto_symbol_tables (Filedata * filedata)
15056{
15057 Elf_Internal_Shdr * section;
15058 unsigned int i;
15059 bool res = true;
15060
15061 if (!do_lto_syms)
15062 return true;
15063
15064 if (filedata->section_headers == NULL)
15065 return true;
15066
15067 for (i = 0, section = filedata->section_headers;
15068 i < filedata->file_header.e_shnum;
15069 i++, section++)
15070 if (section_name_valid (filedata, section)
15071 && startswith (section_name (filedata, section), ".gnu.lto_.symtab."))
15072 res &= display_lto_symtab (filedata, section);
15073
15074 return res;
15075}
15076
15077static void
15078print_symbol_table_heading (void)
15079{
15080 /* FIXME: We should store the size of each field in the display in a table and
15081 then use the values inside print_symbol(), instead of that function using
15082 hard coded constants. */
15083 if (is_32bit_elf)
15084 {
15085 if (extra_sym_info)
15086 {
15087 printf (_(" Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]\n"));
15088 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |---8--| |----13.....| |........... */
15089 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 (.text) get_sections */
15090 }
15091 else if (do_wide)
15092 {
15093 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
15094 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |-4| |........... */
15095 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 get_sections */
15096 }
15097 else
15098 {
15099 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
15100 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |-4| |------------29-------------| */
15101 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 get_sections */
15102 }
15103 }
15104 else
15105 {
15106 if (extra_sym_info)
15107 {
15108 printf (_(" Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]\n"));
15109 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-------14---| |..... */
15110 /* eg: 2: 0000000000000000 0 FUNC LOCAL DEFAULT 1 (.text) .very_long_function_name */
15111
15112 }
15113 else if (do_wide)
15114 {
15115 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
15116 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-4| |........... */
15117 /* eg: 5: 0000000000000000 14 FUNC LOCAL DEFAULT 1 very_long_function_name */
15118 }
15119 else
15120 {
15121 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
15122 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-4| |--------21---------| */
15123 /* eg: 5: 0000000000000000 14 FUNC LOCAL DEFAULT 1 very_long_functi[...] */
15124 }
15125 }
15126}
15127
15128static bool
15129dump_symbol_section (Elf_Internal_Shdr * section,
15130 Filedata * filedata)
15131{
15132 if (section->sh_entsize == 0)
15133 {
15134 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
15135 printable_section_name (filedata, section));
15136 return false;
15137 }
15138
15139 uint64_t num_syms = section->sh_size / section->sh_entsize;
15140
15141 if (filedata->is_separate)
15142 printf (ngettext ("\nIn linked file '%s' symbol section '%s'"
15143 " contains %" PRIu64 " entry:\n",
15144 "\nIn linked file '%s' symbol section '%s'"
15145 " contains %" PRIu64 " entries:\n",
15146 num_syms),
15147 filedata->file_name,
15148 printable_section_name (filedata, section),
15149 num_syms);
15150 else
15151 printf (ngettext ("\nSymbol table '%s' contains %" PRIu64
15152 " entry:\n",
15153 "\nSymbol table '%s' contains %" PRIu64
15154 " entries:\n",
15155 num_syms),
15156 printable_section_name (filedata, section),
15157 num_syms);
15158
15159 print_symbol_table_heading ();
15160
15161 Elf_Internal_Sym * symtab = get_elf_symbols (filedata, section, & num_syms);
15162 if (symtab == NULL)
15163 /* An error message will have already been displayed. */
15164 return false;
15165
15166 char * strtab = NULL;
15167 uint64_t strtab_size = 0;
15168
15169 if (section->sh_link == filedata->file_header.e_shstrndx)
15170 {
15171 strtab = filedata->string_table;
15172 strtab_size = filedata->string_table_length;
15173 }
15174 else if (section->sh_link < filedata->file_header.e_shnum)
15175 {
15176 Elf_Internal_Shdr * string_sec;
15177
15178 string_sec = filedata->section_headers + section->sh_link;
15179
15180 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
15181 1, string_sec->sh_size,
15182 _("string table"));
15183 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
15184 }
15185
15186 uint64_t si;
15187
15188 for (si = 0; si < num_syms; si++)
15189 print_symbol (filedata, si, symtab, section, strtab, strtab_size);
15190
15191 free (symtab);
15192
15193 if (strtab != filedata->string_table)
15194 free (strtab);
15195
15196 return true;
15197}
15198
15199/* Dump the symbol table. */
15200
15201static bool
15202process_symbol_table (Filedata * filedata)
15203{
15204 Elf_Internal_Shdr * section;
15205
15206 if (!do_syms && !do_dyn_syms && !do_histogram)
15207 return true;
15208
15209 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
15210 && do_syms
15211 && do_using_dynamic
15212 && filedata->dynamic_strings != NULL
15213 && filedata->dynamic_symbols != NULL)
15214 {
15215 uint64_t si;
15216
15217 if (filedata->is_separate)
15218 {
15219 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table"
15220 " contains %" PRIu64 " entry:\n",
15221 "\nIn linked file '%s' the dynamic symbol table"
15222 " contains %" PRIu64 " entries:\n",
15223 filedata->num_dynamic_syms),
15224 filedata->file_name,
15225 filedata->num_dynamic_syms);
15226 }
15227 else
15228 {
15229 printf (ngettext ("\nSymbol table for image contains %" PRIu64
15230 " entry:\n",
15231 "\nSymbol table for image contains %" PRIu64
15232 " entries:\n",
15233 filedata->num_dynamic_syms),
15234 filedata->num_dynamic_syms);
15235 }
15236
15237 print_symbol_table_heading ();
15238
15239 for (si = 0; si < filedata->num_dynamic_syms; si++)
15240 print_symbol (filedata, si, filedata->dynamic_symbols, NULL,
15241 filedata->dynamic_strings,
15242 filedata->dynamic_strings_length);
15243 }
15244 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
15245 && filedata->section_headers != NULL)
15246 {
15247 unsigned int i;
15248
15249 for (i = 0, section = filedata->section_headers;
15250 i < filedata->file_header.e_shnum;
15251 i++, section++)
15252 {
15253 if ((section->sh_type != SHT_SYMTAB
15254 && section->sh_type != SHT_DYNSYM)
15255 || (!do_syms
15256 && section->sh_type == SHT_SYMTAB))
15257 continue;
15258
15259 dump_symbol_section (section, filedata);
15260 }
15261 }
15262 else if (do_syms)
15263 printf
15264 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
15265
15266 if (do_histogram && filedata->buckets != NULL)
15267 {
15268 uint64_t *lengths;
15269 uint64_t *counts;
15270 uint64_t hn;
15271 uint64_t si;
15272 uint64_t maxlength = 0;
15273 uint64_t nzero_counts = 0;
15274 uint64_t nsyms = 0;
15275 char *visited;
15276
15277 printf (ngettext ("\nHistogram for bucket list length "
15278 "(total of %" PRIu64 " bucket):\n",
15279 "\nHistogram for bucket list length "
15280 "(total of %" PRIu64 " buckets):\n",
15281 filedata->nbuckets),
15282 filedata->nbuckets);
15283
15284 lengths = calloc (filedata->nbuckets, sizeof (*lengths));
15285 if (lengths == NULL)
15286 {
15287 error (_("Out of memory allocating space for histogram buckets\n"));
15288 goto err_out;
15289 }
15290 visited = xcmalloc (filedata->nchains, 1);
15291 memset (visited, 0, filedata->nchains);
15292
15293 printf (_(" Length Number %% of total Coverage\n"));
15294 for (hn = 0; hn < filedata->nbuckets; ++hn)
15295 {
15296 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
15297 {
15298 ++nsyms;
15299 if (maxlength < ++lengths[hn])
15300 ++maxlength;
15301 if (si >= filedata->nchains || visited[si])
15302 {
15303 error (_("histogram chain is corrupt\n"));
15304 break;
15305 }
15306 visited[si] = 1;
15307 }
15308 }
15309 free (visited);
15310
15311 counts = calloc (maxlength + 1, sizeof (*counts));
15312 if (counts == NULL)
15313 {
15314 free (lengths);
15315 error (_("Out of memory allocating space for histogram counts\n"));
15316 goto err_out;
15317 }
15318
15319 for (hn = 0; hn < filedata->nbuckets; ++hn)
15320 ++counts[lengths[hn]];
15321
15322 if (filedata->nbuckets > 0)
15323 {
15324 uint64_t i;
15325 printf (" 0 %-10" PRIu64 " (%5.1f%%)\n",
15326 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
15327 for (i = 1; i <= maxlength; ++i)
15328 {
15329 nzero_counts += counts[i] * i;
15330 printf ("%7" PRIu64 " %-10" PRIu64 " (%5.1f%%) %5.1f%%\n",
15331 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
15332 (nzero_counts * 100.0) / nsyms);
15333 }
15334 }
15335
15336 free (counts);
15337 free (lengths);
15338 }
15339
15340 free (filedata->buckets);
15341 filedata->buckets = NULL;
15342 filedata->nbuckets = 0;
15343 free (filedata->chains);
15344 filedata->chains = NULL;
15345
15346 if (do_histogram && filedata->gnubuckets != NULL)
15347 {
15348 uint64_t *lengths;
15349 uint64_t *counts;
15350 uint64_t hn;
15351 uint64_t maxlength = 0;
15352 uint64_t nzero_counts = 0;
15353 uint64_t nsyms = 0;
15354
15355 printf (ngettext ("\nHistogram for `%s' bucket list length "
15356 "(total of %" PRIu64 " bucket):\n",
15357 "\nHistogram for `%s' bucket list length "
15358 "(total of %" PRIu64 " buckets):\n",
15359 filedata->ngnubuckets),
15360 GNU_HASH_SECTION_NAME (filedata),
15361 filedata->ngnubuckets);
15362
15363 lengths = calloc (filedata->ngnubuckets, sizeof (*lengths));
15364 if (lengths == NULL)
15365 {
15366 error (_("Out of memory allocating space for gnu histogram buckets\n"));
15367 goto err_out;
15368 }
15369
15370 printf (_(" Length Number %% of total Coverage\n"));
15371
15372 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
15373 if (filedata->gnubuckets[hn] != 0)
15374 {
15375 uint64_t off, length = 1;
15376
15377 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
15378 /* PR 17531 file: 010-77222-0.004. */
15379 off < filedata->ngnuchains
15380 && (filedata->gnuchains[off] & 1) == 0;
15381 ++off)
15382 ++length;
15383 lengths[hn] = length;
15384 if (length > maxlength)
15385 maxlength = length;
15386 nsyms += length;
15387 }
15388
15389 counts = calloc (maxlength + 1, sizeof (*counts));
15390 if (counts == NULL)
15391 {
15392 free (lengths);
15393 error (_("Out of memory allocating space for gnu histogram counts\n"));
15394 goto err_out;
15395 }
15396
15397 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
15398 ++counts[lengths[hn]];
15399
15400 if (filedata->ngnubuckets > 0)
15401 {
15402 uint64_t j;
15403 printf (" 0 %-10" PRIu64 " (%5.1f%%)\n",
15404 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
15405 for (j = 1; j <= maxlength; ++j)
15406 {
15407 nzero_counts += counts[j] * j;
15408 printf ("%7" PRIu64 " %-10" PRIu64 " (%5.1f%%) %5.1f%%\n",
15409 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
15410 (nzero_counts * 100.0) / nsyms);
15411 }
15412 }
15413
15414 free (counts);
15415 free (lengths);
15416 }
15417 free (filedata->gnubuckets);
15418 filedata->gnubuckets = NULL;
15419 filedata->ngnubuckets = 0;
15420 free (filedata->gnuchains);
15421 filedata->gnuchains = NULL;
15422 filedata->ngnuchains = 0;
15423 free (filedata->mipsxlat);
15424 filedata->mipsxlat = NULL;
15425 return true;
15426
15427 err_out:
15428 free (filedata->gnubuckets);
15429 filedata->gnubuckets = NULL;
15430 filedata->ngnubuckets = 0;
15431 free (filedata->gnuchains);
15432 filedata->gnuchains = NULL;
15433 filedata->ngnuchains = 0;
15434 free (filedata->mipsxlat);
15435 filedata->mipsxlat = NULL;
15436 free (filedata->buckets);
15437 filedata->buckets = NULL;
15438 filedata->nbuckets = 0;
15439 free (filedata->chains);
15440 filedata->chains = NULL;
15441 return false;
15442}
15443
15444static bool
15445process_syminfo (Filedata * filedata)
15446{
15447 unsigned int i;
15448
15449 if (filedata->dynamic_syminfo == NULL
15450 || !do_dynamic)
15451 /* No syminfo, this is ok. */
15452 return true;
15453
15454 /* There better should be a dynamic symbol section. */
15455 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
15456 return false;
15457
15458 if (filedata->is_separate)
15459 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset %#" PRIx64 " contains %d entry:\n",
15460 "\nIn linked file '%s: the dynamic info segment at offset %#" PRIx64 " contains %d entries:\n",
15461 filedata->dynamic_syminfo_nent),
15462 filedata->file_name,
15463 filedata->dynamic_syminfo_offset,
15464 filedata->dynamic_syminfo_nent);
15465 else
15466 printf (ngettext ("\nDynamic info segment at offset %#" PRIx64
15467 " contains %d entry:\n",
15468 "\nDynamic info segment at offset %#" PRIx64
15469 " contains %d entries:\n",
15470 filedata->dynamic_syminfo_nent),
15471 filedata->dynamic_syminfo_offset,
15472 filedata->dynamic_syminfo_nent);
15473
15474 printf (_(" Num: Name BoundTo Flags\n"));
15475 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
15476 {
15477 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
15478
15479 printf ("%4d: ", i);
15480 if (i >= filedata->num_dynamic_syms)
15481 printf (_("<corrupt index>"));
15482 else if (valid_dynamic_name (filedata, filedata->dynamic_symbols[i].st_name))
15483 print_symbol_name (30, get_dynamic_name (filedata,
15484 filedata->dynamic_symbols[i].st_name));
15485 else
15486 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
15487 putchar (' ');
15488
15489 switch (filedata->dynamic_syminfo[i].si_boundto)
15490 {
15491 case SYMINFO_BT_SELF:
15492 fputs ("SELF ", stdout);
15493 break;
15494 case SYMINFO_BT_PARENT:
15495 fputs ("PARENT ", stdout);
15496 break;
15497 default:
15498 if (filedata->dynamic_syminfo[i].si_boundto > 0
15499 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
15500 && valid_dynamic_name (filedata,
15501 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
15502 {
15503 print_symbol_name (10, get_dynamic_name (filedata,
15504 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
15505 putchar (' ' );
15506 }
15507 else
15508 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
15509 break;
15510 }
15511
15512 if (flags & SYMINFO_FLG_DIRECT)
15513 printf (" DIRECT");
15514 if (flags & SYMINFO_FLG_PASSTHRU)
15515 printf (" PASSTHRU");
15516 if (flags & SYMINFO_FLG_COPY)
15517 printf (" COPY");
15518 if (flags & SYMINFO_FLG_LAZYLOAD)
15519 printf (" LAZYLOAD");
15520
15521 puts ("");
15522 }
15523
15524 return true;
15525}
15526
15527/* Check that reloc at R_OFFSET of size R_SIZE can apply to LEN bytes. */
15528
15529static inline bool
15530in_range (size_t len, bfd_vma r_offset, unsigned int r_size)
15531{
15532 return r_offset <= len && r_size <= len - r_offset;
15533}
15534
15535/* Check to see if the given reloc needs to be handled in a target specific
15536 manner. If so then process the reloc and return TRUE otherwise return
15537 FALSE.
15538
15539 If called with reloc == NULL, then this is a signal that reloc processing
15540 for the current section has finished, and any saved state should be
15541 discarded. */
15542
15543static bool
15544target_specific_reloc_handling (Filedata *filedata,
15545 Elf_Internal_Rela *reloc,
15546 unsigned char *start,
15547 size_t size,
15548 Elf_Internal_Sym *symtab,
15549 uint64_t num_syms)
15550{
15551 unsigned int reloc_type = 0;
15552 uint64_t sym_index = 0;
15553
15554 if (reloc)
15555 {
15556 reloc_type = get_reloc_type (filedata, reloc->r_info);
15557 sym_index = get_reloc_symindex (reloc->r_info);
15558 }
15559
15560 switch (filedata->file_header.e_machine)
15561 {
15562 case EM_LOONGARCH:
15563 {
15564 switch (reloc_type)
15565 {
15566 /* For .uleb128 .LFE1-.LFB1, loongarch write 0 to object file
15567 at assembly time. */
15568 case 107: /* R_LARCH_ADD_ULEB128. */
15569 case 108: /* R_LARCH_SUB_ULEB128. */
15570 {
15571 uint64_t value = 0;
15572 unsigned int reloc_size = 0;
15573 int leb_ret = 0;
15574
15575 if (reloc->r_offset < size)
15576 value = read_leb128 (start + reloc->r_offset, start + size,
15577 false, &reloc_size, &leb_ret);
15578 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
15579 error (_("LoongArch ULEB128 field at 0x%lx contains invalid "
15580 "ULEB128 value\n"),
15581 (long) reloc->r_offset);
15582
15583 else if (sym_index >= num_syms)
15584 error (_("%s reloc contains invalid symbol index "
15585 "%" PRIu64 "\n"),
15586 (reloc_type == 107
15587 ? "R_LARCH_ADD_ULEB128"
15588 : "R_LARCH_SUB_ULEB128"),
15589 sym_index);
15590 else
15591 {
15592 if (reloc_type == 107)
15593 value += reloc->r_addend + symtab[sym_index].st_value;
15594 else
15595 value -= reloc->r_addend + symtab[sym_index].st_value;
15596
15597 /* Write uleb128 value to p. */
15598 bfd_byte *p = start + reloc->r_offset;
15599 do
15600 {
15601 bfd_byte c = value & 0x7f;
15602 value >>= 7;
15603 if (--reloc_size != 0)
15604 c |= 0x80;
15605 *p++ = c;
15606 }
15607 while (reloc_size);
15608 }
15609
15610 return true;
15611 }
15612 }
15613 break;
15614 }
15615
15616 case EM_MSP430:
15617 case EM_MSP430_OLD:
15618 {
15619 static Elf_Internal_Sym * saved_sym = NULL;
15620
15621 if (reloc == NULL)
15622 {
15623 saved_sym = NULL;
15624 return true;
15625 }
15626
15627 switch (reloc_type)
15628 {
15629 case 10: /* R_MSP430_SYM_DIFF */
15630 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
15631 if (uses_msp430x_relocs (filedata))
15632 break;
15633 /* Fall through. */
15634 case 21: /* R_MSP430X_SYM_DIFF */
15635 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
15636 /* PR 21139. */
15637 if (sym_index >= num_syms)
15638 error (_("%s reloc contains invalid symbol index "
15639 "%" PRIu64 "\n"), "MSP430 SYM_DIFF", sym_index);
15640 else
15641 saved_sym = symtab + sym_index;
15642 return true;
15643
15644 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
15645 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
15646 goto handle_sym_diff;
15647
15648 case 5: /* R_MSP430_16_BYTE */
15649 case 9: /* R_MSP430_8 */
15650 case 11: /* R_MSP430_GNU_SET_ULEB128 */
15651 if (uses_msp430x_relocs (filedata))
15652 break;
15653 goto handle_sym_diff;
15654
15655 case 2: /* R_MSP430_ABS16 */
15656 case 15: /* R_MSP430X_ABS16 */
15657 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
15658 if (! uses_msp430x_relocs (filedata))
15659 break;
15660 goto handle_sym_diff;
15661
15662 handle_sym_diff:
15663 if (saved_sym != NULL)
15664 {
15665 uint64_t value;
15666 unsigned int reloc_size = 0;
15667 int leb_ret = 0;
15668 switch (reloc_type)
15669 {
15670 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
15671 reloc_size = 4;
15672 break;
15673 case 11: /* R_MSP430_GNU_SET_ULEB128 */
15674 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
15675 if (reloc->r_offset < size)
15676 read_leb128 (start + reloc->r_offset, start + size, false,
15677 &reloc_size, &leb_ret);
15678 break;
15679 default:
15680 reloc_size = 2;
15681 break;
15682 }
15683
15684 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
15685 error (_("MSP430 ULEB128 field at %#" PRIx64
15686 " contains invalid ULEB128 value\n"),
15687 reloc->r_offset);
15688 else if (sym_index >= num_syms)
15689 error (_("%s reloc contains invalid symbol index "
15690 "%" PRIu64 "\n"), "MSP430", sym_index);
15691 else
15692 {
15693 value = reloc->r_addend + (symtab[sym_index].st_value
15694 - saved_sym->st_value);
15695
15696 if (in_range (size, reloc->r_offset, reloc_size))
15697 byte_put (start + reloc->r_offset, value, reloc_size);
15698 else
15699 /* PR 21137 */
15700 error (_("MSP430 sym diff reloc contains invalid offset: "
15701 "%#" PRIx64 "\n"),
15702 reloc->r_offset);
15703 }
15704
15705 saved_sym = NULL;
15706 return true;
15707 }
15708 break;
15709
15710 default:
15711 if (saved_sym != NULL)
15712 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
15713 break;
15714 }
15715 break;
15716 }
15717
15718 case EM_MN10300:
15719 case EM_CYGNUS_MN10300:
15720 {
15721 static Elf_Internal_Sym * saved_sym = NULL;
15722
15723 if (reloc == NULL)
15724 {
15725 saved_sym = NULL;
15726 return true;
15727 }
15728
15729 switch (reloc_type)
15730 {
15731 case 34: /* R_MN10300_ALIGN */
15732 return true;
15733 case 33: /* R_MN10300_SYM_DIFF */
15734 if (sym_index >= num_syms)
15735 error (_("%s reloc contains invalid symbol index "
15736 "%" PRIu64 "\n"), "MN10300_SYM_DIFF", sym_index);
15737 else
15738 saved_sym = symtab + sym_index;
15739 return true;
15740
15741 case 1: /* R_MN10300_32 */
15742 case 2: /* R_MN10300_16 */
15743 if (saved_sym != NULL)
15744 {
15745 int reloc_size = reloc_type == 1 ? 4 : 2;
15746 uint64_t value;
15747
15748 if (sym_index >= num_syms)
15749 error (_("%s reloc contains invalid symbol index "
15750 "%" PRIu64 "\n"), "MN10300", sym_index);
15751 else
15752 {
15753 value = reloc->r_addend + (symtab[sym_index].st_value
15754 - saved_sym->st_value);
15755
15756 if (in_range (size, reloc->r_offset, reloc_size))
15757 byte_put (start + reloc->r_offset, value, reloc_size);
15758 else
15759 error (_("MN10300 sym diff reloc contains invalid offset:"
15760 " %#" PRIx64 "\n"),
15761 reloc->r_offset);
15762 }
15763
15764 saved_sym = NULL;
15765 return true;
15766 }
15767 break;
15768 default:
15769 if (saved_sym != NULL)
15770 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
15771 break;
15772 }
15773 break;
15774 }
15775
15776 case EM_RL78:
15777 {
15778 static uint64_t saved_sym1 = 0;
15779 static uint64_t saved_sym2 = 0;
15780 static uint64_t value;
15781
15782 if (reloc == NULL)
15783 {
15784 saved_sym1 = saved_sym2 = 0;
15785 return true;
15786 }
15787
15788 switch (reloc_type)
15789 {
15790 case 0x80: /* R_RL78_SYM. */
15791 saved_sym1 = saved_sym2;
15792 if (sym_index >= num_syms)
15793 error (_("%s reloc contains invalid symbol index "
15794 "%" PRIu64 "\n"), "RL78_SYM", sym_index);
15795 else
15796 {
15797 saved_sym2 = symtab[sym_index].st_value;
15798 saved_sym2 += reloc->r_addend;
15799 }
15800 return true;
15801
15802 case 0x83: /* R_RL78_OPsub. */
15803 value = saved_sym1 - saved_sym2;
15804 saved_sym2 = saved_sym1 = 0;
15805 return true;
15806 break;
15807
15808 case 0x41: /* R_RL78_ABS32. */
15809 if (in_range (size, reloc->r_offset, 4))
15810 byte_put (start + reloc->r_offset, value, 4);
15811 else
15812 error (_("RL78 sym diff reloc contains invalid offset: "
15813 "%#" PRIx64 "\n"),
15814 reloc->r_offset);
15815 value = 0;
15816 return true;
15817
15818 case 0x43: /* R_RL78_ABS16. */
15819 if (in_range (size, reloc->r_offset, 2))
15820 byte_put (start + reloc->r_offset, value, 2);
15821 else
15822 error (_("RL78 sym diff reloc contains invalid offset: "
15823 "%#" PRIx64 "\n"),
15824 reloc->r_offset);
15825 value = 0;
15826 return true;
15827
15828 default:
15829 break;
15830 }
15831 break;
15832 }
15833 }
15834
15835 return false;
15836}
15837
15838/* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
15839 DWARF debug sections. This is a target specific test. Note - we do not
15840 go through the whole including-target-headers-multiple-times route, (as
15841 we have already done with <elf/h8.h>) because this would become very
15842 messy and even then this function would have to contain target specific
15843 information (the names of the relocs instead of their numeric values).
15844 FIXME: This is not the correct way to solve this problem. The proper way
15845 is to have target specific reloc sizing and typing functions created by
15846 the reloc-macros.h header, in the same way that it already creates the
15847 reloc naming functions. */
15848
15849static bool
15850is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15851{
15852 /* Please keep this table alpha-sorted for ease of visual lookup. */
15853 switch (filedata->file_header.e_machine)
15854 {
15855 case EM_386:
15856 case EM_IAMCU:
15857 return reloc_type == 1; /* R_386_32. */
15858 case EM_68K:
15859 return reloc_type == 1; /* R_68K_32. */
15860 case EM_860:
15861 return reloc_type == 1; /* R_860_32. */
15862 case EM_960:
15863 return reloc_type == 2; /* R_960_32. */
15864 case EM_AARCH64:
15865 return (reloc_type == 258
15866 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
15867 case EM_BPF:
15868 return reloc_type == 11; /* R_BPF_DATA_32 */
15869 case EM_ADAPTEVA_EPIPHANY:
15870 return reloc_type == 3;
15871 case EM_ALPHA:
15872 return reloc_type == 1; /* R_ALPHA_REFLONG. */
15873 case EM_ARC:
15874 return reloc_type == 1; /* R_ARC_32. */
15875 case EM_ARC_COMPACT:
15876 case EM_ARC_COMPACT2:
15877 case EM_ARC_COMPACT3:
15878 case EM_ARC_COMPACT3_64:
15879 return reloc_type == 4; /* R_ARC_32. */
15880 case EM_ARM:
15881 return reloc_type == 2; /* R_ARM_ABS32 */
15882 case EM_AVR_OLD:
15883 case EM_AVR:
15884 return reloc_type == 1;
15885 case EM_BLACKFIN:
15886 return reloc_type == 0x12; /* R_byte4_data. */
15887 case EM_CRIS:
15888 return reloc_type == 3; /* R_CRIS_32. */
15889 case EM_CR16:
15890 return reloc_type == 3; /* R_CR16_NUM32. */
15891 case EM_CRX:
15892 return reloc_type == 15; /* R_CRX_NUM32. */
15893 case EM_CSKY:
15894 return reloc_type == 1; /* R_CKCORE_ADDR32. */
15895 case EM_CYGNUS_FRV:
15896 return reloc_type == 1;
15897 case EM_CYGNUS_D10V:
15898 case EM_D10V:
15899 return reloc_type == 6; /* R_D10V_32. */
15900 case EM_CYGNUS_D30V:
15901 case EM_D30V:
15902 return reloc_type == 12; /* R_D30V_32_NORMAL. */
15903 case EM_DLX:
15904 return reloc_type == 3; /* R_DLX_RELOC_32. */
15905 case EM_CYGNUS_FR30:
15906 case EM_FR30:
15907 return reloc_type == 3; /* R_FR30_32. */
15908 case EM_FT32:
15909 return reloc_type == 1; /* R_FT32_32. */
15910 case EM_H8S:
15911 case EM_H8_300:
15912 case EM_H8_300H:
15913 return reloc_type == 1; /* R_H8_DIR32. */
15914 case EM_IA_64:
15915 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
15916 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
15917 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
15918 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
15919 case EM_IP2K_OLD:
15920 case EM_IP2K:
15921 return reloc_type == 2; /* R_IP2K_32. */
15922 case EM_IQ2000:
15923 return reloc_type == 2; /* R_IQ2000_32. */
15924 case EM_KVX:
15925 return reloc_type == 2; /* R_KVX_32. */
15926 case EM_LATTICEMICO32:
15927 return reloc_type == 3; /* R_LM32_32. */
15928 case EM_LOONGARCH:
15929 return reloc_type == 1; /* R_LARCH_32. */
15930 case EM_M32C_OLD:
15931 case EM_M32C:
15932 return reloc_type == 3; /* R_M32C_32. */
15933 case EM_M32R:
15934 return reloc_type == 34; /* R_M32R_32_RELA. */
15935 case EM_68HC11:
15936 case EM_68HC12:
15937 return reloc_type == 6; /* R_M68HC11_32. */
15938 case EM_S12Z:
15939 return reloc_type == 7 || /* R_S12Z_EXT32 */
15940 reloc_type == 6; /* R_S12Z_CW32. */
15941 case EM_MCORE:
15942 return reloc_type == 1; /* R_MCORE_ADDR32. */
15943 case EM_CYGNUS_MEP:
15944 return reloc_type == 4; /* R_MEP_32. */
15945 case EM_METAG:
15946 return reloc_type == 2; /* R_METAG_ADDR32. */
15947 case EM_MICROBLAZE:
15948 return reloc_type == 1; /* R_MICROBLAZE_32. */
15949 case EM_MIPS:
15950 return reloc_type == 2; /* R_MIPS_32. */
15951 case EM_MMIX:
15952 return reloc_type == 4; /* R_MMIX_32. */
15953 case EM_CYGNUS_MN10200:
15954 case EM_MN10200:
15955 return reloc_type == 1; /* R_MN10200_32. */
15956 case EM_CYGNUS_MN10300:
15957 case EM_MN10300:
15958 return reloc_type == 1; /* R_MN10300_32. */
15959 case EM_MOXIE:
15960 return reloc_type == 1; /* R_MOXIE_32. */
15961 case EM_MSP430_OLD:
15962 case EM_MSP430:
15963 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
15964 case EM_MT:
15965 return reloc_type == 2; /* R_MT_32. */
15966 case EM_NDS32:
15967 return reloc_type == 20; /* R_NDS32_32_RELA. */
15968 case EM_ALTERA_NIOS2:
15969 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
15970 case EM_NIOS32:
15971 return reloc_type == 1; /* R_NIOS_32. */
15972 case EM_OR1K:
15973 return reloc_type == 1; /* R_OR1K_32. */
15974 case EM_PARISC:
15975 return (reloc_type == 1 /* R_PARISC_DIR32. */
15976 || reloc_type == 2 /* R_PARISC_DIR21L. */
15977 || reloc_type == 41); /* R_PARISC_SECREL32. */
15978 case EM_PJ:
15979 case EM_PJ_OLD:
15980 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
15981 case EM_PPC64:
15982 return reloc_type == 1; /* R_PPC64_ADDR32. */
15983 case EM_PPC:
15984 return reloc_type == 1; /* R_PPC_ADDR32. */
15985 case EM_TI_PRU:
15986 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
15987 case EM_RISCV:
15988 return reloc_type == 1; /* R_RISCV_32. */
15989 case EM_RL78:
15990 return reloc_type == 1; /* R_RL78_DIR32. */
15991 case EM_RX:
15992 return reloc_type == 1; /* R_RX_DIR32. */
15993 case EM_S370:
15994 return reloc_type == 1; /* R_I370_ADDR31. */
15995 case EM_S390_OLD:
15996 case EM_S390:
15997 return reloc_type == 4; /* R_S390_32. */
15998 case EM_SCORE:
15999 return reloc_type == 8; /* R_SCORE_ABS32. */
16000 case EM_SH:
16001 return reloc_type == 1; /* R_SH_DIR32. */
16002 case EM_SPARC32PLUS:
16003 case EM_SPARCV9:
16004 case EM_SPARC:
16005 return reloc_type == 3 /* R_SPARC_32. */
16006 || reloc_type == 23; /* R_SPARC_UA32. */
16007 case EM_SPU:
16008 return reloc_type == 6; /* R_SPU_ADDR32 */
16009 case EM_TI_C6000:
16010 return reloc_type == 1; /* R_C6000_ABS32. */
16011 case EM_TILEGX:
16012 return reloc_type == 2; /* R_TILEGX_32. */
16013 case EM_TILEPRO:
16014 return reloc_type == 1; /* R_TILEPRO_32. */
16015 case EM_CYGNUS_V850:
16016 case EM_V850:
16017 return reloc_type == 6; /* R_V850_ABS32. */
16018 case EM_V800:
16019 return reloc_type == 0x33; /* R_V810_WORD. */
16020 case EM_VAX:
16021 return reloc_type == 1; /* R_VAX_32. */
16022 case EM_VISIUM:
16023 return reloc_type == 3; /* R_VISIUM_32. */
16024 case EM_WEBASSEMBLY:
16025 return reloc_type == 1; /* R_WASM32_32. */
16026 case EM_X86_64:
16027 case EM_L1OM:
16028 case EM_K1OM:
16029 return reloc_type == 10; /* R_X86_64_32. */
16030 case EM_XGATE:
16031 return reloc_type == 4; /* R_XGATE_32. */
16032 case EM_XSTORMY16:
16033 return reloc_type == 1; /* R_XSTROMY16_32. */
16034 case EM_XTENSA_OLD:
16035 case EM_XTENSA:
16036 return reloc_type == 1; /* R_XTENSA_32. */
16037 case EM_Z80:
16038 return reloc_type == 6; /* R_Z80_32. */
16039 default:
16040 {
16041 static unsigned int prev_warn = 0;
16042
16043 /* Avoid repeating the same warning multiple times. */
16044 if (prev_warn != filedata->file_header.e_machine)
16045 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
16046 filedata->file_header.e_machine);
16047 prev_warn = filedata->file_header.e_machine;
16048 return false;
16049 }
16050 }
16051}
16052
16053/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16054 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
16055
16056static bool
16057is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
16058{
16059 switch (filedata->file_header.e_machine)
16060 /* Please keep this table alpha-sorted for ease of visual lookup. */
16061 {
16062 case EM_386:
16063 case EM_IAMCU:
16064 return reloc_type == 2; /* R_386_PC32. */
16065 case EM_68K:
16066 return reloc_type == 4; /* R_68K_PC32. */
16067 case EM_AARCH64:
16068 return reloc_type == 261; /* R_AARCH64_PREL32 */
16069 case EM_ADAPTEVA_EPIPHANY:
16070 return reloc_type == 6;
16071 case EM_ALPHA:
16072 return reloc_type == 10; /* R_ALPHA_SREL32. */
16073 case EM_ARC_COMPACT:
16074 case EM_ARC_COMPACT2:
16075 case EM_ARC_COMPACT3:
16076 case EM_ARC_COMPACT3_64:
16077 return reloc_type == 49; /* R_ARC_32_PCREL. */
16078 case EM_ARM:
16079 return reloc_type == 3; /* R_ARM_REL32 */
16080 case EM_AVR_OLD:
16081 case EM_AVR:
16082 return reloc_type == 36; /* R_AVR_32_PCREL. */
16083 case EM_LOONGARCH:
16084 return reloc_type == 99; /* R_LARCH_32_PCREL. */
16085 case EM_MICROBLAZE:
16086 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
16087 case EM_OR1K:
16088 return reloc_type == 9; /* R_OR1K_32_PCREL. */
16089 case EM_PARISC:
16090 return reloc_type == 9; /* R_PARISC_PCREL32. */
16091 case EM_PPC:
16092 return reloc_type == 26; /* R_PPC_REL32. */
16093 case EM_PPC64:
16094 return reloc_type == 26; /* R_PPC64_REL32. */
16095 case EM_RISCV:
16096 return reloc_type == 57; /* R_RISCV_32_PCREL. */
16097 case EM_S390_OLD:
16098 case EM_S390:
16099 return reloc_type == 5; /* R_390_PC32. */
16100 case EM_SH:
16101 return reloc_type == 2; /* R_SH_REL32. */
16102 case EM_SPARC32PLUS:
16103 case EM_SPARCV9:
16104 case EM_SPARC:
16105 return reloc_type == 6; /* R_SPARC_DISP32. */
16106 case EM_SPU:
16107 return reloc_type == 13; /* R_SPU_REL32. */
16108 case EM_TILEGX:
16109 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
16110 case EM_TILEPRO:
16111 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
16112 case EM_VISIUM:
16113 return reloc_type == 6; /* R_VISIUM_32_PCREL */
16114 case EM_X86_64:
16115 case EM_L1OM:
16116 case EM_K1OM:
16117 return reloc_type == 2; /* R_X86_64_PC32. */
16118 case EM_VAX:
16119 return reloc_type == 4; /* R_VAX_PCREL32. */
16120 case EM_XTENSA_OLD:
16121 case EM_XTENSA:
16122 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
16123 case EM_KVX:
16124 return reloc_type == 7; /* R_KVX_32_PCREL */
16125 default:
16126 /* Do not abort or issue an error message here. Not all targets use
16127 pc-relative 32-bit relocs in their DWARF debug information and we
16128 have already tested for target coverage in is_32bit_abs_reloc. A
16129 more helpful warning message will be generated by apply_relocations
16130 anyway, so just return. */
16131 return false;
16132 }
16133}
16134
16135/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16136 a 64-bit absolute RELA relocation used in DWARF debug sections. */
16137
16138static bool
16139is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
16140{
16141 switch (filedata->file_header.e_machine)
16142 {
16143 case EM_AARCH64:
16144 return reloc_type == 257; /* R_AARCH64_ABS64. */
16145 case EM_ARC_COMPACT3_64:
16146 return reloc_type == 5; /* R_ARC_64. */
16147 case EM_ALPHA:
16148 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
16149 case EM_IA_64:
16150 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
16151 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
16152 case EM_LOONGARCH:
16153 return reloc_type == 2; /* R_LARCH_64 */
16154 case EM_PARISC:
16155 return reloc_type == 80; /* R_PARISC_DIR64. */
16156 case EM_PPC64:
16157 return reloc_type == 38; /* R_PPC64_ADDR64. */
16158 case EM_RISCV:
16159 return reloc_type == 2; /* R_RISCV_64. */
16160 case EM_SPARC32PLUS:
16161 case EM_SPARCV9:
16162 case EM_SPARC:
16163 return reloc_type == 32 /* R_SPARC_64. */
16164 || reloc_type == 54; /* R_SPARC_UA64. */
16165 case EM_X86_64:
16166 case EM_L1OM:
16167 case EM_K1OM:
16168 return reloc_type == 1; /* R_X86_64_64. */
16169 case EM_S390_OLD:
16170 case EM_S390:
16171 return reloc_type == 22; /* R_S390_64. */
16172 case EM_TILEGX:
16173 return reloc_type == 1; /* R_TILEGX_64. */
16174 case EM_MIPS:
16175 return reloc_type == 18; /* R_MIPS_64. */
16176 case EM_KVX:
16177 return reloc_type == 3; /* R_KVX_64 */
16178 default:
16179 return false;
16180 }
16181}
16182
16183/* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
16184 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
16185
16186static bool
16187is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
16188{
16189 switch (filedata->file_header.e_machine)
16190 {
16191 case EM_AARCH64:
16192 return reloc_type == 260; /* R_AARCH64_PREL64. */
16193 case EM_ALPHA:
16194 return reloc_type == 11; /* R_ALPHA_SREL64. */
16195 case EM_IA_64:
16196 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
16197 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
16198 case EM_PARISC:
16199 return reloc_type == 72; /* R_PARISC_PCREL64. */
16200 case EM_PPC64:
16201 return reloc_type == 44; /* R_PPC64_REL64. */
16202 case EM_SPARC32PLUS:
16203 case EM_SPARCV9:
16204 case EM_SPARC:
16205 return reloc_type == 46; /* R_SPARC_DISP64. */
16206 case EM_X86_64:
16207 case EM_L1OM:
16208 case EM_K1OM:
16209 return reloc_type == 24; /* R_X86_64_PC64. */
16210 case EM_S390_OLD:
16211 case EM_S390:
16212 return reloc_type == 23; /* R_S390_PC64. */
16213 case EM_TILEGX:
16214 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
16215 default:
16216 return false;
16217 }
16218}
16219
16220/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16221 a 24-bit absolute RELA relocation used in DWARF debug sections. */
16222
16223static bool
16224is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
16225{
16226 switch (filedata->file_header.e_machine)
16227 {
16228 case EM_CYGNUS_MN10200:
16229 case EM_MN10200:
16230 return reloc_type == 4; /* R_MN10200_24. */
16231 case EM_FT32:
16232 return reloc_type == 5; /* R_FT32_20. */
16233 case EM_Z80:
16234 return reloc_type == 5; /* R_Z80_24. */
16235 default:
16236 return false;
16237 }
16238}
16239
16240/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16241 a 16-bit absolute RELA relocation used in DWARF debug sections. */
16242
16243static bool
16244is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
16245{
16246 /* Please keep this table alpha-sorted for ease of visual lookup. */
16247 switch (filedata->file_header.e_machine)
16248 {
16249 case EM_ARC:
16250 case EM_ARC_COMPACT:
16251 case EM_ARC_COMPACT2:
16252 case EM_ARC_COMPACT3:
16253 case EM_ARC_COMPACT3_64:
16254 return reloc_type == 2; /* R_ARC_16. */
16255 case EM_ADAPTEVA_EPIPHANY:
16256 return reloc_type == 5;
16257 case EM_AVR_OLD:
16258 case EM_AVR:
16259 return reloc_type == 4; /* R_AVR_16. */
16260 case EM_CYGNUS_D10V:
16261 case EM_D10V:
16262 return reloc_type == 3; /* R_D10V_16. */
16263 case EM_FT32:
16264 return reloc_type == 2; /* R_FT32_16. */
16265 case EM_H8S:
16266 case EM_H8_300:
16267 case EM_H8_300H:
16268 return reloc_type == R_H8_DIR16;
16269 case EM_IP2K_OLD:
16270 case EM_IP2K:
16271 return reloc_type == 1; /* R_IP2K_16. */
16272 case EM_M32C_OLD:
16273 case EM_M32C:
16274 return reloc_type == 1; /* R_M32C_16 */
16275 case EM_CYGNUS_MN10200:
16276 case EM_MN10200:
16277 return reloc_type == 2; /* R_MN10200_16. */
16278 case EM_CYGNUS_MN10300:
16279 case EM_MN10300:
16280 return reloc_type == 2; /* R_MN10300_16. */
16281 case EM_KVX:
16282 return reloc_type == 1; /* R_KVX_16 */
16283 case EM_MSP430:
16284 if (uses_msp430x_relocs (filedata))
16285 return reloc_type == 2; /* R_MSP430_ABS16. */
16286 /* Fall through. */
16287 case EM_MSP430_OLD:
16288 return reloc_type == 5; /* R_MSP430_16_BYTE. */
16289 case EM_NDS32:
16290 return reloc_type == 19; /* R_NDS32_16_RELA. */
16291 case EM_ALTERA_NIOS2:
16292 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
16293 case EM_NIOS32:
16294 return reloc_type == 9; /* R_NIOS_16. */
16295 case EM_OR1K:
16296 return reloc_type == 2; /* R_OR1K_16. */
16297 case EM_RISCV:
16298 return reloc_type == 55; /* R_RISCV_SET16. */
16299 case EM_TI_PRU:
16300 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
16301 case EM_TI_C6000:
16302 return reloc_type == 2; /* R_C6000_ABS16. */
16303 case EM_VISIUM:
16304 return reloc_type == 2; /* R_VISIUM_16. */
16305 case EM_XGATE:
16306 return reloc_type == 3; /* R_XGATE_16. */
16307 case EM_Z80:
16308 return reloc_type == 4; /* R_Z80_16. */
16309 default:
16310 return false;
16311 }
16312}
16313
16314/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16315 a 8-bit absolute RELA relocation used in DWARF debug sections. */
16316
16317static bool
16318is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
16319{
16320 switch (filedata->file_header.e_machine)
16321 {
16322 case EM_RISCV:
16323 return reloc_type == 54; /* R_RISCV_SET8. */
16324 case EM_Z80:
16325 return reloc_type == 1; /* R_Z80_8. */
16326 case EM_MICROBLAZE:
16327 return (reloc_type == 33 /* R_MICROBLAZE_32_NONE. */
16328 || reloc_type == 0 /* R_MICROBLAZE_NONE. */
16329 || reloc_type == 9 /* R_MICROBLAZE_64_NONE. */);
16330 default:
16331 return false;
16332 }
16333}
16334
16335/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16336 a 6-bit absolute RELA relocation used in DWARF debug sections. */
16337
16338static bool
16339is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
16340{
16341 switch (filedata->file_header.e_machine)
16342 {
16343 case EM_RISCV:
16344 return reloc_type == 53; /* R_RISCV_SET6. */
16345 default:
16346 return false;
16347 }
16348}
16349
16350/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16351 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
16352
16353static bool
16354is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
16355{
16356 /* Please keep this table alpha-sorted for ease of visual lookup. */
16357 switch (filedata->file_header.e_machine)
16358 {
16359 case EM_LOONGARCH:
16360 return reloc_type == 50; /* R_LARCH_ADD32. */
16361 case EM_RISCV:
16362 return reloc_type == 35; /* R_RISCV_ADD32. */
16363 default:
16364 return false;
16365 }
16366}
16367
16368/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16369 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
16370
16371static bool
16372is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
16373{
16374 /* Please keep this table alpha-sorted for ease of visual lookup. */
16375 switch (filedata->file_header.e_machine)
16376 {
16377 case EM_LOONGARCH:
16378 return reloc_type == 55; /* R_LARCH_SUB32. */
16379 case EM_RISCV:
16380 return reloc_type == 39; /* R_RISCV_SUB32. */
16381 default:
16382 return false;
16383 }
16384}
16385
16386/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16387 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
16388
16389static bool
16390is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
16391{
16392 /* Please keep this table alpha-sorted for ease of visual lookup. */
16393 switch (filedata->file_header.e_machine)
16394 {
16395 case EM_LOONGARCH:
16396 return reloc_type == 51; /* R_LARCH_ADD64. */
16397 case EM_RISCV:
16398 return reloc_type == 36; /* R_RISCV_ADD64. */
16399 default:
16400 return false;
16401 }
16402}
16403
16404/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16405 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
16406
16407static bool
16408is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
16409{
16410 /* Please keep this table alpha-sorted for ease of visual lookup. */
16411 switch (filedata->file_header.e_machine)
16412 {
16413 case EM_LOONGARCH:
16414 return reloc_type == 56; /* R_LARCH_SUB64. */
16415 case EM_RISCV:
16416 return reloc_type == 40; /* R_RISCV_SUB64. */
16417 default:
16418 return false;
16419 }
16420}
16421
16422/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16423 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
16424
16425static bool
16426is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
16427{
16428 /* Please keep this table alpha-sorted for ease of visual lookup. */
16429 switch (filedata->file_header.e_machine)
16430 {
16431 case EM_LOONGARCH:
16432 return reloc_type == 48; /* R_LARCH_ADD16. */
16433 case EM_RISCV:
16434 return reloc_type == 34; /* R_RISCV_ADD16. */
16435 default:
16436 return false;
16437 }
16438}
16439
16440/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16441 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
16442
16443static bool
16444is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
16445{
16446 /* Please keep this table alpha-sorted for ease of visual lookup. */
16447 switch (filedata->file_header.e_machine)
16448 {
16449 case EM_LOONGARCH:
16450 return reloc_type == 53; /* R_LARCH_SUB16. */
16451 case EM_RISCV:
16452 return reloc_type == 38; /* R_RISCV_SUB16. */
16453 default:
16454 return false;
16455 }
16456}
16457
16458/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16459 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
16460
16461static bool
16462is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
16463{
16464 /* Please keep this table alpha-sorted for ease of visual lookup. */
16465 switch (filedata->file_header.e_machine)
16466 {
16467 case EM_LOONGARCH:
16468 return reloc_type == 47; /* R_LARCH_ADD8. */
16469 case EM_RISCV:
16470 return reloc_type == 33; /* R_RISCV_ADD8. */
16471 default:
16472 return false;
16473 }
16474}
16475
16476/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16477 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
16478
16479static bool
16480is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
16481{
16482 /* Please keep this table alpha-sorted for ease of visual lookup. */
16483 switch (filedata->file_header.e_machine)
16484 {
16485 case EM_LOONGARCH:
16486 return reloc_type == 52; /* R_LARCH_SUB8. */
16487 case EM_RISCV:
16488 return reloc_type == 37; /* R_RISCV_SUB8. */
16489 default:
16490 return false;
16491 }
16492}
16493
16494/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16495 a 6-bit inplace add RELA relocation used in DWARF debug sections. */
16496
16497static bool
16498is_6bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
16499{
16500 switch (filedata->file_header.e_machine)
16501 {
16502 case EM_LOONGARCH:
16503 return reloc_type == 105; /* R_LARCH_ADD6. */
16504 default:
16505 return false;
16506 }
16507}
16508
16509/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
16510 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
16511
16512static bool
16513is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
16514{
16515 switch (filedata->file_header.e_machine)
16516 {
16517 case EM_LOONGARCH:
16518 return reloc_type == 106; /* R_LARCH_SUB6. */
16519 case EM_RISCV:
16520 return reloc_type == 52; /* R_RISCV_SUB6. */
16521 default:
16522 return false;
16523 }
16524}
16525
16526/* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
16527 relocation entries (possibly formerly used for SHT_GROUP sections). */
16528
16529static bool
16530is_none_reloc (Filedata * filedata, unsigned int reloc_type)
16531{
16532 switch (filedata->file_header.e_machine)
16533 {
16534 case EM_386: /* R_386_NONE. */
16535 case EM_68K: /* R_68K_NONE. */
16536 case EM_ADAPTEVA_EPIPHANY:
16537 case EM_ALPHA: /* R_ALPHA_NONE. */
16538 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
16539 case EM_ARC: /* R_ARC_NONE. */
16540 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
16541 case EM_ARC_COMPACT: /* R_ARC_NONE. */
16542 case EM_ARC_COMPACT3: /* R_ARC_NONE. */
16543 case EM_ARC_COMPACT3_64: /* R_ARC_NONE. */
16544 case EM_ARM: /* R_ARM_NONE. */
16545 case EM_CRIS: /* R_CRIS_NONE. */
16546 case EM_FT32: /* R_FT32_NONE. */
16547 case EM_IA_64: /* R_IA64_NONE. */
16548 case EM_K1OM: /* R_X86_64_NONE. */
16549 case EM_KVX: /* R_KVX_NONE. */
16550 case EM_L1OM: /* R_X86_64_NONE. */
16551 case EM_M32R: /* R_M32R_NONE. */
16552 case EM_MIPS: /* R_MIPS_NONE. */
16553 case EM_MN10300: /* R_MN10300_NONE. */
16554 case EM_MOXIE: /* R_MOXIE_NONE. */
16555 case EM_NIOS32: /* R_NIOS_NONE. */
16556 case EM_OR1K: /* R_OR1K_NONE. */
16557 case EM_PARISC: /* R_PARISC_NONE. */
16558 case EM_PPC64: /* R_PPC64_NONE. */
16559 case EM_PPC: /* R_PPC_NONE. */
16560 case EM_RISCV: /* R_RISCV_NONE. */
16561 case EM_S390: /* R_390_NONE. */
16562 case EM_S390_OLD:
16563 case EM_SH: /* R_SH_NONE. */
16564 case EM_SPARC32PLUS:
16565 case EM_SPARC: /* R_SPARC_NONE. */
16566 case EM_SPARCV9:
16567 case EM_TILEGX: /* R_TILEGX_NONE. */
16568 case EM_TILEPRO: /* R_TILEPRO_NONE. */
16569 case EM_TI_C6000:/* R_C6000_NONE. */
16570 case EM_X86_64: /* R_X86_64_NONE. */
16571 case EM_Z80: /* R_Z80_NONE. */
16572 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
16573 return reloc_type == 0;
16574
16575 case EM_AARCH64:
16576 return reloc_type == 0 || reloc_type == 256;
16577 case EM_AVR_OLD:
16578 case EM_AVR:
16579 return (reloc_type == 0 /* R_AVR_NONE. */
16580 || reloc_type == 30 /* R_AVR_DIFF8. */
16581 || reloc_type == 31 /* R_AVR_DIFF16. */
16582 || reloc_type == 32 /* R_AVR_DIFF32. */);
16583 case EM_METAG:
16584 return reloc_type == 3; /* R_METAG_NONE. */
16585 case EM_NDS32:
16586 return (reloc_type == 0 /* R_NDS32_NONE. */
16587 || reloc_type == 205 /* R_NDS32_DIFF8. */
16588 || reloc_type == 206 /* R_NDS32_DIFF16. */
16589 || reloc_type == 207 /* R_NDS32_DIFF32. */
16590 || reloc_type == 208 /* R_NDS32_DIFF_ULEB128. */);
16591 case EM_TI_PRU:
16592 return (reloc_type == 0 /* R_PRU_NONE. */
16593 || reloc_type == 65 /* R_PRU_DIFF8. */
16594 || reloc_type == 66 /* R_PRU_DIFF16. */
16595 || reloc_type == 67 /* R_PRU_DIFF32. */);
16596 case EM_XTENSA_OLD:
16597 case EM_XTENSA:
16598 return (reloc_type == 0 /* R_XTENSA_NONE. */
16599 || reloc_type == 17 /* R_XTENSA_DIFF8. */
16600 || reloc_type == 18 /* R_XTENSA_DIFF16. */
16601 || reloc_type == 19 /* R_XTENSA_DIFF32. */
16602 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
16603 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
16604 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
16605 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
16606 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
16607 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
16608 }
16609 return false;
16610}
16611
16612/* Returns TRUE if there is a relocation against
16613 section NAME at OFFSET bytes. */
16614
16615bool
16616reloc_at (struct dwarf_section * dsec, uint64_t offset)
16617{
16618 Elf_Internal_Rela * relocs;
16619 Elf_Internal_Rela * rp;
16620
16621 if (dsec == NULL || dsec->reloc_info == NULL)
16622 return false;
16623
16624 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
16625
16626 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
16627 if (rp->r_offset == offset)
16628 return true;
16629
16630 return false;
16631}
16632
16633/* Apply relocations to a section.
16634 Returns TRUE upon success, FALSE otherwise.
16635 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
16636 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
16637 will be set to the number of relocs loaded.
16638
16639 Note: So far support has been added only for those relocations
16640 which can be found in debug sections. FIXME: Add support for
16641 more relocations ? */
16642
16643static bool
16644apply_relocations (Filedata *filedata,
16645 const Elf_Internal_Shdr *section,
16646 unsigned char *start,
16647 size_t size,
16648 void **relocs_return,
16649 uint64_t *num_relocs_return)
16650{
16651 Elf_Internal_Shdr * relsec;
16652
16653 if (relocs_return != NULL)
16654 {
16655 * (Elf_Internal_Rela **) relocs_return = NULL;
16656 * num_relocs_return = 0;
16657 }
16658
16659 if (filedata->file_header.e_type != ET_REL)
16660 /* No relocs to apply. */
16661 return true;
16662
16663 /* Find the reloc section associated with the section. */
16664 for (relsec = filedata->section_headers;
16665 relsec < filedata->section_headers + filedata->file_header.e_shnum;
16666 ++relsec)
16667 {
16668 bool is_rela;
16669 uint64_t num_relocs;
16670 Elf_Internal_Rela * relocs;
16671 Elf_Internal_Rela * rp;
16672 Elf_Internal_Shdr * symsec;
16673 Elf_Internal_Sym * symtab;
16674 uint64_t num_syms;
16675 Elf_Internal_Sym * sym;
16676
16677 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
16678 || relsec->sh_info >= filedata->file_header.e_shnum
16679 || filedata->section_headers + relsec->sh_info != section
16680 || relsec->sh_size == 0
16681 || relsec->sh_link >= filedata->file_header.e_shnum)
16682 continue;
16683
16684 symsec = filedata->section_headers + relsec->sh_link;
16685 if (symsec->sh_type != SHT_SYMTAB
16686 && symsec->sh_type != SHT_DYNSYM)
16687 return false;
16688
16689 is_rela = relsec->sh_type == SHT_RELA;
16690
16691 if (is_rela)
16692 {
16693 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
16694 relsec->sh_size, & relocs, & num_relocs))
16695 return false;
16696 }
16697 else
16698 {
16699 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
16700 relsec->sh_size, & relocs, & num_relocs))
16701 return false;
16702 }
16703
16704 /* SH uses RELA but uses in place value instead of the addend field. */
16705 if (filedata->file_header.e_machine == EM_SH)
16706 is_rela = false;
16707
16708 symtab = get_elf_symbols (filedata, symsec, & num_syms);
16709
16710 for (rp = relocs; rp < relocs + num_relocs; ++rp)
16711 {
16712 uint64_t addend;
16713 unsigned int reloc_type;
16714 unsigned int reloc_size;
16715 bool reloc_inplace = false;
16716 bool reloc_subtract = false;
16717 unsigned char *rloc;
16718 uint64_t sym_index;
16719
16720 reloc_type = get_reloc_type (filedata, rp->r_info);
16721
16722 if (target_specific_reloc_handling (filedata, rp, start, size, symtab, num_syms))
16723 continue;
16724 else if (is_none_reloc (filedata, reloc_type))
16725 continue;
16726 else if (is_32bit_abs_reloc (filedata, reloc_type)
16727 || is_32bit_pcrel_reloc (filedata, reloc_type))
16728 reloc_size = 4;
16729 else if (is_64bit_abs_reloc (filedata, reloc_type)
16730 || is_64bit_pcrel_reloc (filedata, reloc_type))
16731 reloc_size = 8;
16732 else if (is_24bit_abs_reloc (filedata, reloc_type))
16733 reloc_size = 3;
16734 else if (is_16bit_abs_reloc (filedata, reloc_type))
16735 reloc_size = 2;
16736 else if (is_8bit_abs_reloc (filedata, reloc_type)
16737 || is_6bit_abs_reloc (filedata, reloc_type))
16738 reloc_size = 1;
16739 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
16740 reloc_type))
16741 || is_32bit_inplace_add_reloc (filedata, reloc_type))
16742 {
16743 reloc_size = 4;
16744 reloc_inplace = true;
16745 }
16746 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
16747 reloc_type))
16748 || is_64bit_inplace_add_reloc (filedata, reloc_type))
16749 {
16750 reloc_size = 8;
16751 reloc_inplace = true;
16752 }
16753 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
16754 reloc_type))
16755 || is_16bit_inplace_add_reloc (filedata, reloc_type))
16756 {
16757 reloc_size = 2;
16758 reloc_inplace = true;
16759 }
16760 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
16761 reloc_type))
16762 || is_8bit_inplace_add_reloc (filedata, reloc_type))
16763 {
16764 reloc_size = 1;
16765 reloc_inplace = true;
16766 }
16767 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
16768 reloc_type))
16769 || is_6bit_inplace_add_reloc (filedata, reloc_type))
16770 {
16771 reloc_size = 1;
16772 reloc_inplace = true;
16773 }
16774 else
16775 {
16776 static unsigned int prev_reloc = 0;
16777
16778 if (reloc_type != prev_reloc)
16779 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
16780 reloc_type, printable_section_name (filedata, section));
16781 prev_reloc = reloc_type;
16782 continue;
16783 }
16784
16785 if (!in_range (size, rp->r_offset, reloc_size))
16786 {
16787 warn (_("skipping invalid relocation offset %#" PRIx64
16788 " in section %s\n"),
16789 rp->r_offset,
16790 printable_section_name (filedata, section));
16791 continue;
16792 }
16793 rloc = start + rp->r_offset;
16794
16795 sym_index = get_reloc_symindex (rp->r_info);
16796 if (sym_index >= num_syms)
16797 {
16798 warn (_("skipping invalid relocation symbol index %#" PRIx64
16799 " in section %s\n"),
16800 sym_index, printable_section_name (filedata, section));
16801 continue;
16802 }
16803 sym = symtab + sym_index;
16804
16805 /* If the reloc has a symbol associated with it,
16806 make sure that it is of an appropriate type.
16807
16808 Relocations against symbols without type can happen.
16809 Gcc -feliminate-dwarf2-dups may generate symbols
16810 without type for debug info.
16811
16812 Icc generates relocations against function symbols
16813 instead of local labels.
16814
16815 Relocations against object symbols can happen, eg when
16816 referencing a global array. For an example of this see
16817 the _clz.o binary in libgcc.a. */
16818 if (sym != symtab
16819 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
16820 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
16821 {
16822 warn (_("skipping unexpected symbol type %s in section %s relocation %tu\n"),
16823 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
16824 printable_section_name (filedata, relsec),
16825 rp - relocs);
16826 continue;
16827 }
16828
16829 addend = 0;
16830 if (is_rela)
16831 addend += rp->r_addend;
16832 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
16833 partial_inplace. */
16834 if (!is_rela
16835 || (filedata->file_header.e_machine == EM_XTENSA
16836 && reloc_type == 1)
16837 || ((filedata->file_header.e_machine == EM_PJ
16838 || filedata->file_header.e_machine == EM_PJ_OLD)
16839 && reloc_type == 1)
16840 || ((filedata->file_header.e_machine == EM_D30V
16841 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
16842 && reloc_type == 12)
16843 || reloc_inplace)
16844 {
16845 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
16846 addend += byte_get (rloc, reloc_size) & 0x3f;
16847 else
16848 addend += byte_get (rloc, reloc_size);
16849 }
16850
16851 if (is_32bit_pcrel_reloc (filedata, reloc_type)
16852 || is_64bit_pcrel_reloc (filedata, reloc_type))
16853 {
16854 /* On HPPA, all pc-relative relocations are biased by 8. */
16855 if (filedata->file_header.e_machine == EM_PARISC)
16856 addend -= 8;
16857 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
16858 reloc_size);
16859 }
16860 else if (is_6bit_abs_reloc (filedata, reloc_type)
16861 || is_6bit_inplace_sub_reloc (filedata, reloc_type)
16862 || is_6bit_inplace_add_reloc (filedata, reloc_type))
16863 {
16864 if (reloc_subtract)
16865 addend -= sym->st_value;
16866 else
16867 addend += sym->st_value;
16868 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
16869 byte_put (rloc, addend, reloc_size);
16870 }
16871 else if (reloc_subtract)
16872 byte_put (rloc, addend - sym->st_value, reloc_size);
16873 else
16874 byte_put (rloc, addend + sym->st_value, reloc_size);
16875 }
16876
16877 free (symtab);
16878 /* Let the target specific reloc processing code know that
16879 we have finished with these relocs. */
16880 target_specific_reloc_handling (filedata, NULL, NULL, 0, NULL, 0);
16881
16882 if (relocs_return)
16883 {
16884 * (Elf_Internal_Rela **) relocs_return = relocs;
16885 * num_relocs_return = num_relocs;
16886 }
16887 else
16888 free (relocs);
16889
16890 break;
16891 }
16892
16893 return true;
16894}
16895
16896#ifdef SUPPORT_DISASSEMBLY
16897static bool
16898disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
16899{
16900 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
16901
16902 /* FIXME: XXX -- to be done --- XXX */
16903
16904 return true;
16905}
16906#endif
16907
16908/* Reads in the contents of SECTION from FILE, returning a pointer
16909 to a malloc'ed buffer or NULL if something went wrong. */
16910
16911static char *
16912get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
16913{
16914 uint64_t num_bytes = section->sh_size;
16915
16916 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
16917 {
16918 printf (_("Section '%s' has no data to dump.\n"),
16919 printable_section_name (filedata, section));
16920 return NULL;
16921 }
16922
16923 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
16924 _("section contents"));
16925}
16926
16927/* Uncompresses a section that was compressed using zlib/zstd, in place. */
16928
16929static bool
16930uncompress_section_contents (bool is_zstd,
16931 unsigned char ** buffer,
16932 uint64_t uncompressed_size,
16933 uint64_t * size,
16934 uint64_t file_size)
16935{
16936 uint64_t compressed_size = *size;
16937 unsigned char *compressed_buffer = *buffer;
16938 unsigned char *uncompressed_buffer = NULL;
16939 z_stream strm;
16940 int rc;
16941
16942 /* Similar to bfd_section_size_insane() in the BFD library we expect an
16943 upper limit of ~10x compression. Any compression larger than that is
16944 thought to be due to fuzzing of the compression header. */
16945 if (uncompressed_size > file_size * 10)
16946 {
16947 error (_("Uncompressed section size is suspiciously large: 0x%" PRIu64 "\n"),
16948 uncompressed_size);
16949 goto fail;
16950 }
16951
16952 uncompressed_buffer = xmalloc (uncompressed_size);
16953
16954 if (is_zstd)
16955 {
16956#ifdef HAVE_ZSTD
16957 size_t ret = ZSTD_decompress (uncompressed_buffer, uncompressed_size,
16958 compressed_buffer, compressed_size);
16959 if (ZSTD_isError (ret))
16960 goto fail;
16961#endif
16962 }
16963 else
16964 {
16965 /* It is possible the section consists of several compressed
16966 buffers concatenated together, so we uncompress in a loop. */
16967 /* PR 18313: The state field in the z_stream structure is supposed
16968 to be invisible to the user (ie us), but some compilers will
16969 still complain about it being used without initialisation. So
16970 we first zero the entire z_stream structure and then set the fields
16971 that we need. */
16972 memset (&strm, 0, sizeof strm);
16973 strm.avail_in = compressed_size;
16974 strm.next_in = (Bytef *)compressed_buffer;
16975 strm.avail_out = uncompressed_size;
16976
16977 rc = inflateInit (&strm);
16978 while (strm.avail_in > 0)
16979 {
16980 if (rc != Z_OK)
16981 break;
16982 strm.next_out = ((Bytef *)uncompressed_buffer
16983 + (uncompressed_size - strm.avail_out));
16984 rc = inflate (&strm, Z_FINISH);
16985 if (rc != Z_STREAM_END)
16986 break;
16987 rc = inflateReset (&strm);
16988 }
16989 if (inflateEnd (&strm) != Z_OK || rc != Z_OK || strm.avail_out != 0)
16990 goto fail;
16991 }
16992
16993 *buffer = uncompressed_buffer;
16994 *size = uncompressed_size;
16995 return true;
16996
16997 fail:
16998 free (uncompressed_buffer);
16999 /* Indicate decompression failure. */
17000 *buffer = NULL;
17001 return false;
17002}
17003
17004static uint64_t
17005maybe_expand_or_relocate_section (Elf_Internal_Shdr * section,
17006 Filedata * filedata,
17007 unsigned char ** start_ptr,
17008 unsigned char ** decomp_buf,
17009 bool relocate)
17010{
17011 uint64_t section_size = section->sh_size;
17012 unsigned char * start = * start_ptr;
17013
17014 if (decompress_dumps)
17015 {
17016 uint64_t new_size = section_size;
17017 uint64_t uncompressed_size = 0;
17018 bool is_zstd = false;
17019
17020 if ((section->sh_flags & SHF_COMPRESSED) != 0)
17021 {
17022 Elf_Internal_Chdr chdr;
17023 unsigned int compression_header_size
17024 = get_compression_header (& chdr, start, section_size);
17025
17026 if (compression_header_size == 0)
17027 /* An error message will have already been generated
17028 by get_compression_header. */
17029 return (uint64_t) -1;
17030
17031 if (chdr.ch_type == ch_compress_zlib)
17032 ;
17033#ifdef HAVE_ZSTD
17034 else if (chdr.ch_type == ch_compress_zstd)
17035 is_zstd = true;
17036#endif
17037 else
17038 {
17039 warn (_("section '%s' has unsupported compress type: %d\n"),
17040 printable_section_name (filedata, section), chdr.ch_type);
17041 return (uint64_t) -1;
17042 }
17043
17044 uncompressed_size = chdr.ch_size;
17045 start += compression_header_size;
17046 new_size -= compression_header_size;
17047 }
17048 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
17049 {
17050 /* Read the zlib header. In this case, it should be "ZLIB"
17051 followed by the uncompressed section size, 8 bytes in
17052 big-endian order. */
17053 uncompressed_size = start[4]; uncompressed_size <<= 8;
17054 uncompressed_size += start[5]; uncompressed_size <<= 8;
17055 uncompressed_size += start[6]; uncompressed_size <<= 8;
17056 uncompressed_size += start[7]; uncompressed_size <<= 8;
17057 uncompressed_size += start[8]; uncompressed_size <<= 8;
17058 uncompressed_size += start[9]; uncompressed_size <<= 8;
17059 uncompressed_size += start[10]; uncompressed_size <<= 8;
17060 uncompressed_size += start[11];
17061 start += 12;
17062 new_size -= 12;
17063 }
17064
17065 if (uncompressed_size)
17066 {
17067 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
17068 &new_size, filedata->file_size))
17069 {
17070 *decomp_buf = start;
17071 section_size = new_size;
17072 }
17073 else
17074 {
17075 error (_("Unable to decompress section %s\n"),
17076 printable_section_name (filedata, section));
17077 return (uint64_t) -1;
17078 }
17079 }
17080 else
17081 start = * start_ptr;
17082 }
17083 else if (((section->sh_flags & SHF_COMPRESSED) != 0)
17084 || (section_size > 12 && streq ((char *) start, "ZLIB")))
17085 {
17086 printf (_(" NOTE: This section is compressed, but its contents have NOT been expanded for this dump.\n"));
17087 }
17088
17089 if (relocate)
17090 {
17091 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
17092 return (uint64_t) -1;
17093 }
17094 else
17095 {
17096 Elf_Internal_Shdr *relsec;
17097
17098 /* If the section being dumped has relocations against it the user might
17099 be expecting these relocations to have been applied. Check for this
17100 case and issue a warning message in order to avoid confusion.
17101 FIXME: Maybe we ought to have an option that dumps a section with
17102 relocs applied ? */
17103 for (relsec = filedata->section_headers;
17104 relsec < filedata->section_headers + filedata->file_header.e_shnum;
17105 ++relsec)
17106 {
17107 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
17108 || relsec->sh_info >= filedata->file_header.e_shnum
17109 || filedata->section_headers + relsec->sh_info != section
17110 || relsec->sh_size == 0
17111 || relsec->sh_link >= filedata->file_header.e_shnum)
17112 continue;
17113
17114 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
17115 break;
17116 }
17117 }
17118
17119 * start_ptr = start;
17120 return section_size;
17121}
17122
17123static bool
17124dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
17125{
17126 uint64_t num_bytes;
17127 unsigned char *data;
17128 unsigned char *end;
17129 unsigned char *real_start;
17130 unsigned char *start;
17131 unsigned char *decomp_buf;
17132 bool some_strings_shown;
17133
17134 real_start = start = (unsigned char *) get_section_contents (section, filedata);
17135 if (start == NULL)
17136 /* PR 21820: Do not fail if the section was empty. */
17137 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
17138
17139 num_bytes = section->sh_size;
17140
17141 if (filedata->is_separate)
17142 printf (_("\nString dump of section '%s' in linked file %s:\n"),
17143 printable_section_name (filedata, section),
17144 filedata->file_name);
17145 else
17146 printf (_("\nString dump of section '%s':\n"),
17147 printable_section_name (filedata, section));
17148
17149 decomp_buf = NULL;
17150 num_bytes = maybe_expand_or_relocate_section (section, filedata, &start,
17151 &decomp_buf, false);
17152 if (num_bytes == (uint64_t) -1)
17153 goto error_out;
17154
17155 data = start;
17156 end = start + num_bytes;
17157 some_strings_shown = false;
17158
17159#ifdef HAVE_MBSTATE_T
17160 mbstate_t state;
17161 /* Initialise the multibyte conversion state. */
17162 memset (& state, 0, sizeof (state));
17163#endif
17164
17165 bool continuing = false;
17166
17167 while (data < end)
17168 {
17169 while (!ISPRINT (* data))
17170 if (++ data >= end)
17171 break;
17172
17173 if (data < end)
17174 {
17175 size_t maxlen = end - data;
17176
17177 if (continuing)
17178 {
17179 printf (" ");
17180 continuing = false;
17181 }
17182 else
17183 {
17184 printf (" [%6tx] ", data - start);
17185 }
17186
17187 if (maxlen > 0)
17188 {
17189 char c = 0;
17190
17191 while (maxlen)
17192 {
17193 c = *data++;
17194
17195 if (c == 0)
17196 break;
17197
17198 /* PR 25543: Treat new-lines as string-ending characters. */
17199 if (c == '\n')
17200 {
17201 printf ("\\n\n");
17202 if (*data != 0)
17203 continuing = true;
17204 break;
17205 }
17206
17207 /* Do not print control characters directly as they can affect terminal
17208 settings. Such characters usually appear in the names generated
17209 by the assembler for local labels. */
17210 if (ISCNTRL (c))
17211 {
17212 printf ("^%c", c + 0x40);
17213 }
17214 else if (ISPRINT (c))
17215 {
17216 putchar (c);
17217 }
17218 else
17219 {
17220 size_t n;
17221#ifdef HAVE_MBSTATE_T
17222 wchar_t w;
17223#endif
17224 /* Let printf do the hard work of displaying multibyte characters. */
17225 printf ("%.1s", data - 1);
17226#ifdef HAVE_MBSTATE_T
17227 /* Try to find out how many bytes made up the character that was
17228 just printed. Advance the symbol pointer past the bytes that
17229 were displayed. */
17230 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
17231#else
17232 n = 1;
17233#endif
17234 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
17235 data += (n - 1);
17236 }
17237 }
17238
17239 if (c != '\n')
17240 putchar ('\n');
17241 }
17242 else
17243 {
17244 printf (_("<corrupt>\n"));
17245 data = end;
17246 }
17247 some_strings_shown = true;
17248 }
17249 }
17250
17251 if (! some_strings_shown)
17252 printf (_(" No strings found in this section."));
17253
17254 free (decomp_buf);
17255 free (real_start);
17256
17257 putchar ('\n');
17258 return true;
17259
17260error_out:
17261 free (decomp_buf);
17262 free (real_start);
17263 return false;
17264}
17265
17266static bool
17267dump_section_as_bytes (Elf_Internal_Shdr *section,
17268 Filedata *filedata,
17269 bool relocate)
17270{
17271 size_t bytes;
17272 uint64_t section_size;
17273 uint64_t addr;
17274 unsigned char *data;
17275 unsigned char *real_start;
17276 unsigned char *start;
17277 unsigned char *decomp_buf;
17278
17279 real_start = start = (unsigned char *) get_section_contents (section, filedata);
17280 if (start == NULL)
17281 /* PR 21820: Do not fail if the section was empty. */
17282 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
17283
17284 section_size = section->sh_size;
17285
17286 if (filedata->is_separate)
17287 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
17288 printable_section_name (filedata, section),
17289 filedata->file_name);
17290 else
17291 printf (_("\nHex dump of section '%s':\n"),
17292 printable_section_name (filedata, section));
17293
17294 decomp_buf = NULL;
17295 section_size = maybe_expand_or_relocate_section (section, filedata, &start,
17296 &decomp_buf, relocate);
17297 if (section_size == (uint64_t) -1)
17298 goto error_out;
17299
17300 addr = section->sh_addr;
17301 bytes = section_size;
17302 data = start;
17303
17304 while (bytes)
17305 {
17306 int j;
17307 int k;
17308 int lbytes;
17309
17310 lbytes = (bytes > 16 ? 16 : bytes);
17311
17312 printf (" 0x%8.8" PRIx64 " ", addr);
17313
17314 for (j = 0; j < 16; j++)
17315 {
17316 if (j < lbytes)
17317 printf ("%2.2x", data[j]);
17318 else
17319 printf (" ");
17320
17321 if ((j & 3) == 3)
17322 printf (" ");
17323 }
17324
17325 for (j = 0; j < lbytes; j++)
17326 {
17327 k = data[j];
17328 if (k >= ' ' && k < 0x7f)
17329 printf ("%c", k);
17330 else
17331 printf (".");
17332 }
17333
17334 putchar ('\n');
17335
17336 data += lbytes;
17337 addr += lbytes;
17338 bytes -= lbytes;
17339 }
17340
17341 free (decomp_buf);
17342 free (real_start);
17343
17344 putchar ('\n');
17345 return true;
17346
17347 error_out:
17348 free (decomp_buf);
17349 free (real_start);
17350 return false;
17351}
17352
17353#ifdef ENABLE_LIBCTF
17354static ctf_sect_t *
17355shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
17356{
17357 buf->cts_name = printable_section_name (filedata, shdr);
17358 buf->cts_size = shdr->sh_size;
17359 buf->cts_entsize = shdr->sh_entsize;
17360
17361 return buf;
17362}
17363
17364/* Formatting callback function passed to ctf_dump. Returns either the pointer
17365 it is passed, or a pointer to newly-allocated storage, in which case
17366 dump_ctf() will free it when it no longer needs it. */
17367
17368static char *
17369dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
17370 char *s, void *arg)
17371{
17372 const char *blanks = arg;
17373 return xasprintf ("%s%s", blanks, s);
17374}
17375
17376/* Dump CTF errors/warnings. */
17377static void
17378dump_ctf_errs (ctf_dict_t *fp)
17379{
17380 ctf_next_t *it = NULL;
17381 char *errtext;
17382 int is_warning;
17383 int err;
17384
17385 /* Dump accumulated errors and warnings. */
17386 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
17387 {
17388 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
17389 errtext);
17390 free (errtext);
17391 }
17392 if (err != ECTF_NEXT_END)
17393 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
17394}
17395
17396/* Dump one CTF archive member. */
17397
17398static void
17399dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
17400 size_t member)
17401{
17402 const char *things[] = {"Header", "Labels", "Data objects",
17403 "Function objects", "Variables", "Types", "Strings",
17404 ""};
17405 const char **thing;
17406 size_t i;
17407
17408 /* Don't print out the name of the default-named archive member if it appears
17409 first in the list. The name .ctf appears everywhere, even for things that
17410 aren't really archives, so printing it out is liable to be confusing; also,
17411 the common case by far is for only one archive member to exist, and hiding
17412 it in that case seems worthwhile. */
17413
17414 if (strcmp (name, ".ctf") != 0 || member != 0)
17415 printf (_("\nCTF archive member: %s:\n"), name);
17416
17417 if (ctf_parent_name (ctf) != NULL)
17418 ctf_import (ctf, parent);
17419
17420 for (i = 0, thing = things; *thing[0]; thing++, i++)
17421 {
17422 ctf_dump_state_t *s = NULL;
17423 char *item;
17424
17425 printf ("\n %s:\n", *thing);
17426 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
17427 (void *) " ")) != NULL)
17428 {
17429 printf ("%s\n", item);
17430 free (item);
17431 }
17432
17433 if (ctf_errno (ctf))
17434 {
17435 error (_("Iteration failed: %s, %s\n"), *thing,
17436 ctf_errmsg (ctf_errno (ctf)));
17437 break;
17438 }
17439 }
17440
17441 dump_ctf_errs (ctf);
17442}
17443
17444static bool
17445dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
17446{
17447 Elf_Internal_Shdr * symtab_sec = NULL;
17448 Elf_Internal_Shdr * strtab_sec = NULL;
17449 void * data = NULL;
17450 void * symdata = NULL;
17451 void * strdata = NULL;
17452 ctf_sect_t ctfsect, symsect, strsect;
17453 ctf_sect_t * symsectp = NULL;
17454 ctf_sect_t * strsectp = NULL;
17455 ctf_archive_t * ctfa = NULL;
17456 ctf_dict_t * parent = NULL;
17457 ctf_dict_t * fp;
17458
17459 ctf_next_t *i = NULL;
17460 const char *name;
17461 size_t member = 0;
17462 int err;
17463 bool ret = false;
17464
17465 shdr_to_ctf_sect (&ctfsect, section, filedata);
17466 data = get_section_contents (section, filedata);
17467 ctfsect.cts_data = data;
17468
17469 if (!dump_ctf_symtab_name)
17470 dump_ctf_symtab_name = strdup (".dynsym");
17471
17472 if (!dump_ctf_strtab_name)
17473 dump_ctf_strtab_name = strdup (".dynstr");
17474
17475 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
17476 {
17477 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
17478 {
17479 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
17480 goto fail;
17481 }
17482 if ((symdata = (void *) get_data (NULL, filedata,
17483 symtab_sec->sh_offset, 1,
17484 symtab_sec->sh_size,
17485 _("symbols"))) == NULL)
17486 goto fail;
17487 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
17488 symsect.cts_data = symdata;
17489 }
17490
17491 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
17492 {
17493 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
17494 {
17495 error (_("No string table section named %s\n"),
17496 dump_ctf_strtab_name);
17497 goto fail;
17498 }
17499 if ((strdata = (void *) get_data (NULL, filedata,
17500 strtab_sec->sh_offset, 1,
17501 strtab_sec->sh_size,
17502 _("strings"))) == NULL)
17503 goto fail;
17504 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
17505 strsect.cts_data = strdata;
17506 }
17507
17508 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
17509 libctf papers over the difference, so we can pretend it is always an
17510 archive. */
17511
17512 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
17513 {
17514 dump_ctf_errs (NULL);
17515 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
17516 goto fail;
17517 }
17518
17519 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
17520 != ELFDATA2MSB);
17521
17522 /* Preload the parent dict, since it will need to be imported into every
17523 child in turn. */
17524 if ((parent = ctf_dict_open (ctfa, dump_ctf_parent_name, &err)) == NULL)
17525 {
17526 dump_ctf_errs (NULL);
17527 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
17528 goto fail;
17529 }
17530
17531 ret = true;
17532
17533 if (filedata->is_separate)
17534 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
17535 printable_section_name (filedata, section),
17536 filedata->file_name);
17537 else
17538 printf (_("\nDump of CTF section '%s':\n"),
17539 printable_section_name (filedata, section));
17540
17541 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
17542 {
17543 dump_ctf_archive_member (fp, name, parent, member++);
17544 ctf_dict_close (fp);
17545 }
17546 if (err != ECTF_NEXT_END)
17547 {
17548 dump_ctf_errs (NULL);
17549 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
17550 ret = false;
17551 }
17552
17553 fail:
17554 ctf_dict_close (parent);
17555 ctf_close (ctfa);
17556 free (data);
17557 free (symdata);
17558 free (strdata);
17559 return ret;
17560}
17561#endif
17562
17563static bool
17564load_specific_debug_section (enum dwarf_section_display_enum debug,
17565 const Elf_Internal_Shdr * sec,
17566 void * data)
17567{
17568 struct dwarf_section * section = &debug_displays [debug].section;
17569 char buf [64];
17570 Filedata * filedata = (Filedata *) data;
17571
17572 if (section->start != NULL)
17573 {
17574 /* If it is already loaded, do nothing. */
17575 if (streq (section->filename, filedata->file_name))
17576 return true;
17577 free (section->start);
17578 }
17579
17580 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
17581 section->address = sec->sh_addr;
17582 section->filename = filedata->file_name;
17583 section->start = (unsigned char *) get_data (NULL, filedata,
17584 sec->sh_offset, 1,
17585 sec->sh_size, buf);
17586 if (section->start == NULL)
17587 section->size = 0;
17588 else
17589 {
17590 unsigned char *start = section->start;
17591 uint64_t size = sec->sh_size;
17592 uint64_t uncompressed_size = 0;
17593 bool is_zstd = false;
17594
17595 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
17596 {
17597 Elf_Internal_Chdr chdr;
17598 unsigned int compression_header_size;
17599
17600 if (size < (is_32bit_elf
17601 ? sizeof (Elf32_External_Chdr)
17602 : sizeof (Elf64_External_Chdr)))
17603 {
17604 warn (_("compressed section %s is too small to contain a compression header\n"),
17605 section->name);
17606 return false;
17607 }
17608
17609 compression_header_size = get_compression_header (&chdr, start, size);
17610 if (compression_header_size == 0)
17611 /* An error message will have already been generated
17612 by get_compression_header. */
17613 return false;
17614
17615 if (chdr.ch_type == ch_compress_zlib)
17616 ;
17617#ifdef HAVE_ZSTD
17618 else if (chdr.ch_type == ch_compress_zstd)
17619 is_zstd = true;
17620#endif
17621 else
17622 {
17623 warn (_("section '%s' has unsupported compress type: %d\n"),
17624 section->name, chdr.ch_type);
17625 return false;
17626 }
17627 uncompressed_size = chdr.ch_size;
17628 start += compression_header_size;
17629 size -= compression_header_size;
17630 }
17631 else if (size > 12 && streq ((char *) start, "ZLIB"))
17632 {
17633 /* Read the zlib header. In this case, it should be "ZLIB"
17634 followed by the uncompressed section size, 8 bytes in
17635 big-endian order. */
17636 uncompressed_size = start[4]; uncompressed_size <<= 8;
17637 uncompressed_size += start[5]; uncompressed_size <<= 8;
17638 uncompressed_size += start[6]; uncompressed_size <<= 8;
17639 uncompressed_size += start[7]; uncompressed_size <<= 8;
17640 uncompressed_size += start[8]; uncompressed_size <<= 8;
17641 uncompressed_size += start[9]; uncompressed_size <<= 8;
17642 uncompressed_size += start[10]; uncompressed_size <<= 8;
17643 uncompressed_size += start[11];
17644 start += 12;
17645 size -= 12;
17646 }
17647
17648 if (uncompressed_size)
17649 {
17650 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
17651 &size, filedata->file_size))
17652 {
17653 /* Free the compressed buffer, update the section buffer
17654 and the section size if uncompress is successful. */
17655 free (section->start);
17656 section->start = start;
17657 }
17658 else
17659 {
17660 error (_("Unable to decompress section %s\n"),
17661 printable_section_name (filedata, sec));
17662 return false;
17663 }
17664 }
17665
17666 section->size = size;
17667 }
17668
17669 if (section->start == NULL)
17670 return false;
17671
17672 if (debug_displays [debug].relocate)
17673 {
17674 if (! apply_relocations (filedata, sec, section->start, section->size,
17675 & section->reloc_info, & section->num_relocs))
17676 return false;
17677 }
17678 else
17679 {
17680 section->reloc_info = NULL;
17681 section->num_relocs = 0;
17682 }
17683
17684 return true;
17685}
17686
17687#if HAVE_LIBDEBUGINFOD
17688/* Return a hex string representation of the build-id. */
17689unsigned char *
17690get_build_id (void * data)
17691{
17692 Filedata * filedata = (Filedata *) data;
17693 Elf_Internal_Shdr * shdr;
17694 size_t i;
17695
17696 /* Iterate through notes to find note.gnu.build-id.
17697 FIXME: Only the first note in any note section is examined. */
17698 for (i = 0, shdr = filedata->section_headers;
17699 i < filedata->file_header.e_shnum && shdr != NULL;
17700 i++, shdr++)
17701 {
17702 if (shdr->sh_type != SHT_NOTE)
17703 continue;
17704
17705 char * next;
17706 char * end;
17707 size_t data_remaining;
17708 size_t min_notesz;
17709 Elf_External_Note * enote;
17710 Elf_Internal_Note inote;
17711
17712 uint64_t offset = shdr->sh_offset;
17713 uint64_t align = shdr->sh_addralign;
17714 uint64_t length = shdr->sh_size;
17715
17716 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
17717 if (enote == NULL)
17718 continue;
17719
17720 if (align < 4)
17721 align = 4;
17722 else if (align != 4 && align != 8)
17723 {
17724 free (enote);
17725 continue;
17726 }
17727
17728 end = (char *) enote + length;
17729 data_remaining = end - (char *) enote;
17730
17731 if (!is_ia64_vms (filedata))
17732 {
17733 min_notesz = offsetof (Elf_External_Note, name);
17734 if (data_remaining < min_notesz)
17735 {
17736 warn (_("\
17737malformed note encountered in section %s whilst scanning for build-id note\n"),
17738 printable_section_name (filedata, shdr));
17739 free (enote);
17740 continue;
17741 }
17742 data_remaining -= min_notesz;
17743
17744 inote.type = BYTE_GET (enote->type);
17745 inote.namesz = BYTE_GET (enote->namesz);
17746 inote.namedata = enote->name;
17747 inote.descsz = BYTE_GET (enote->descsz);
17748 inote.descdata = ((char *) enote
17749 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
17750 inote.descpos = offset + (inote.descdata - (char *) enote);
17751 next = ((char *) enote
17752 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
17753 }
17754 else
17755 {
17756 Elf64_External_VMS_Note *vms_enote;
17757
17758 /* PR binutils/15191
17759 Make sure that there is enough data to read. */
17760 min_notesz = offsetof (Elf64_External_VMS_Note, name);
17761 if (data_remaining < min_notesz)
17762 {
17763 warn (_("\
17764malformed note encountered in section %s whilst scanning for build-id note\n"),
17765 printable_section_name (filedata, shdr));
17766 free (enote);
17767 continue;
17768 }
17769 data_remaining -= min_notesz;
17770
17771 vms_enote = (Elf64_External_VMS_Note *) enote;
17772 inote.type = BYTE_GET (vms_enote->type);
17773 inote.namesz = BYTE_GET (vms_enote->namesz);
17774 inote.namedata = vms_enote->name;
17775 inote.descsz = BYTE_GET (vms_enote->descsz);
17776 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
17777 inote.descpos = offset + (inote.descdata - (char *) enote);
17778 next = inote.descdata + align_power (inote.descsz, 3);
17779 }
17780
17781 /* Skip malformed notes. */
17782 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
17783 || (size_t) (inote.descdata - inote.namedata) > data_remaining
17784 || (size_t) (next - inote.descdata) < inote.descsz
17785 || ((size_t) (next - inote.descdata)
17786 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
17787 {
17788 warn (_("\
17789malformed note encountered in section %s whilst scanning for build-id note\n"),
17790 printable_section_name (filedata, shdr));
17791 free (enote);
17792 continue;
17793 }
17794
17795 /* Check if this is the build-id note. If so then convert the build-id
17796 bytes to a hex string. */
17797 if (inote.namesz > 0
17798 && startswith (inote.namedata, "GNU")
17799 && inote.type == NT_GNU_BUILD_ID)
17800 {
17801 size_t j;
17802 char * build_id;
17803
17804 build_id = malloc (inote.descsz * 2 + 1);
17805 if (build_id == NULL)
17806 {
17807 free (enote);
17808 return NULL;
17809 }
17810
17811 for (j = 0; j < inote.descsz; ++j)
17812 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
17813 build_id[inote.descsz * 2] = '\0';
17814 free (enote);
17815
17816 return (unsigned char *) build_id;
17817 }
17818 free (enote);
17819 }
17820
17821 return NULL;
17822}
17823#endif /* HAVE_LIBDEBUGINFOD */
17824
17825/* If this is not NULL, load_debug_section will only look for sections
17826 within the list of sections given here. */
17827static unsigned int * section_subset = NULL;
17828
17829bool
17830load_debug_section (enum dwarf_section_display_enum debug, void * data)
17831{
17832 struct dwarf_section * section = &debug_displays [debug].section;
17833 Elf_Internal_Shdr * sec;
17834 Filedata * filedata = (Filedata *) data;
17835
17836 if (!dump_any_debugging)
17837 return false;
17838
17839 /* Without section headers we cannot find any sections. */
17840 if (filedata->section_headers == NULL)
17841 return false;
17842
17843 if (filedata->string_table == NULL
17844 && filedata->file_header.e_shstrndx != SHN_UNDEF
17845 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
17846 {
17847 Elf_Internal_Shdr * strs;
17848
17849 /* Read in the string table, so that we have section names to scan. */
17850 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
17851
17852 if (strs != NULL && strs->sh_size != 0)
17853 {
17854 filedata->string_table
17855 = (char *) get_data (NULL, filedata, strs->sh_offset,
17856 1, strs->sh_size, _("string table"));
17857
17858 filedata->string_table_length
17859 = filedata->string_table != NULL ? strs->sh_size : 0;
17860 }
17861 }
17862
17863 /* Locate the debug section. */
17864 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
17865 if (sec != NULL)
17866 section->name = section->uncompressed_name;
17867 else
17868 {
17869 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
17870 if (sec != NULL)
17871 section->name = section->compressed_name;
17872 }
17873 if (sec == NULL)
17874 return false;
17875
17876 /* If we're loading from a subset of sections, and we've loaded
17877 a section matching this name before, it's likely that it's a
17878 different one. */
17879 if (section_subset != NULL)
17880 free_debug_section (debug);
17881
17882 return load_specific_debug_section (debug, sec, data);
17883}
17884
17885void
17886free_debug_section (enum dwarf_section_display_enum debug)
17887{
17888 struct dwarf_section * section = &debug_displays [debug].section;
17889
17890 if (section->start == NULL)
17891 return;
17892
17893 free ((char *) section->start);
17894 section->start = NULL;
17895 section->address = 0;
17896 section->size = 0;
17897
17898 free (section->reloc_info);
17899 section->reloc_info = NULL;
17900 section->num_relocs = 0;
17901}
17902
17903static bool
17904display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
17905{
17906 const char *name = (section_name_valid (filedata, section)
17907 ? section_name (filedata, section) : "");
17908 const char *print_name = printable_section_name (filedata, section);
17909 uint64_t length;
17910 bool result = true;
17911 int i;
17912
17913 length = section->sh_size;
17914 if (length == 0)
17915 {
17916 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
17917 return true;
17918 }
17919 if (section->sh_type == SHT_NOBITS)
17920 {
17921 /* There is no point in dumping the contents of a debugging section
17922 which has the NOBITS type - the bits in the file will be random.
17923 This can happen when a file containing a .eh_frame section is
17924 stripped with the --only-keep-debug command line option. */
17925 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
17926 print_name);
17927 return false;
17928 }
17929
17930 if (startswith (name, ".gnu.linkonce.wi."))
17931 name = ".debug_info";
17932
17933 /* See if we know how to display the contents of this section. */
17934 for (i = 0; i < max; i++)
17935 {
17936 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
17937 struct dwarf_section_display * display = debug_displays + i;
17938 struct dwarf_section * sec = & display->section;
17939
17940 if (streq (sec->uncompressed_name, name)
17941 || (id == line && startswith (name, ".debug_line."))
17942 || (id == sframe && section->sh_type == SHT_GNU_SFRAME)
17943 || streq (sec->compressed_name, name))
17944 {
17945 bool secondary = (section != find_section (filedata, name));
17946
17947 if (secondary)
17948 free_debug_section (id);
17949
17950 if (i == line && startswith (name, ".debug_line."))
17951 sec->name = name;
17952 else if (id == sframe && section->sh_type == SHT_GNU_SFRAME)
17953 sec->name = name;
17954 else if (streq (sec->uncompressed_name, name))
17955 sec->name = sec->uncompressed_name;
17956 else
17957 sec->name = sec->compressed_name;
17958
17959 if (load_specific_debug_section (id, section, filedata))
17960 {
17961 /* If this debug section is part of a CU/TU set in a .dwp file,
17962 restrict load_debug_section to the sections in that set. */
17963 section_subset = find_cu_tu_set (filedata, shndx);
17964
17965 result &= display->display (sec, filedata);
17966
17967 section_subset = NULL;
17968
17969 if (secondary || (id != info && id != abbrev && id != debug_addr))
17970 free_debug_section (id);
17971 }
17972 break;
17973 }
17974 }
17975
17976 if (i == max)
17977 {
17978 printf (_("Unrecognized debug section: %s\n"), print_name);
17979 result = false;
17980 }
17981
17982 return result;
17983}
17984
17985/* Set DUMP_SECTS for all sections where dumps were requested
17986 based on section name. */
17987
17988static void
17989initialise_dumps_byname (Filedata * filedata)
17990{
17991 struct dump_list_entry * cur;
17992
17993 for (cur = dump_sects_byname; cur; cur = cur->next)
17994 {
17995 unsigned int i;
17996 bool any = false;
17997
17998 for (i = 0; i < filedata->file_header.e_shnum; i++)
17999 if (section_name_valid (filedata, filedata->section_headers + i)
18000 && streq (section_name (filedata, filedata->section_headers + i),
18001 cur->name))
18002 {
18003 request_dump_bynumber (&filedata->dump, i, cur->type);
18004 any = true;
18005 }
18006
18007 if (!any && !filedata->is_separate)
18008 warn (_("Section '%s' was not dumped because it does not exist\n"),
18009 cur->name);
18010 }
18011}
18012
18013static bool
18014process_section_contents (Filedata * filedata)
18015{
18016 Elf_Internal_Shdr * section;
18017 unsigned int i;
18018 bool res = true;
18019
18020 if (! do_dump)
18021 return true;
18022
18023 initialise_dumps_byname (filedata);
18024
18025 for (i = 0, section = filedata->section_headers;
18026 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
18027 i++, section++)
18028 {
18029 dump_type dump = filedata->dump.dump_sects[i];
18030
18031 if (filedata->is_separate && ! process_links)
18032 dump &= DEBUG_DUMP;
18033
18034 if (dump & AUTO_DUMP)
18035 {
18036 switch (section->sh_type)
18037 {
18038 case SHT_PROGBITS:
18039 /* FIXME: There are lots of different type of section that have
18040 SHT_PROGBITS set in their header - code, debug info, etc. So
18041 we should check the section's name and interpret its contents
18042 that way, rather than just defaulting to a byte dump. */
18043#ifdef SUPPORT_DISASSEMBLY
18044 res &= disassemble_section (section, filedata);
18045#else
18046 res &= dump_section_as_bytes (section, filedata, false);
18047#endif
18048 break;
18049
18050 case SHT_DYNSYM:
18051 case SHT_SYMTAB:
18052 res &= dump_symbol_section (section, filedata);
18053 break;
18054
18055 case SHT_STRTAB:
18056 res &= dump_section_as_strings (section, filedata);
18057 break;
18058
18059 case SHT_RELA:
18060 case SHT_REL:
18061 case SHT_RELR:
18062 res &= display_relocations (section, filedata, true);
18063 break;
18064
18065 case SHT_NOTE:
18066 res &= process_notes_at (filedata, section, section->sh_offset,
18067 section->sh_size, section->sh_addralign);
18068 break;
18069
18070 case SHT_NULL:
18071 inform (_("Unable to display section %d - it has a NULL type\n"), i);
18072 break;
18073
18074 case SHT_NOBITS:
18075 inform (_("Unable to display section %d - it has no contents\n"), i);
18076 break;
18077
18078 case SHT_HASH:
18079 case SHT_DYNAMIC:
18080 case SHT_GROUP:
18081 case SHT_GNU_ATTRIBUTES:
18082 /* FIXME: Implement these. */
18083 /* Fall through. */
18084 default:
18085 /* FIXME: Add Proc and OS specific section types ? */
18086 warn (_("Unable to determine how to dump section %d (type %#x)\n"),
18087 i, section->sh_type);
18088 res = false;
18089 break;
18090 }
18091 }
18092
18093#ifdef SUPPORT_DISASSEMBLY
18094 if (dump & DISASS_DUMP)
18095 {
18096 if (! disassemble_section (section, filedata))
18097 res = false;
18098 }
18099#endif
18100 if (dump & HEX_DUMP)
18101 {
18102 if (! dump_section_as_bytes (section, filedata, false))
18103 res = false;
18104 }
18105
18106 if (dump & RELOC_DUMP)
18107 {
18108 if (! dump_section_as_bytes (section, filedata, true))
18109 res = false;
18110 }
18111
18112 if (dump & STRING_DUMP)
18113 {
18114 if (! dump_section_as_strings (section, filedata))
18115 res = false;
18116 }
18117
18118 if (dump & DEBUG_DUMP)
18119 {
18120 if (! display_debug_section (i, section, filedata))
18121 res = false;
18122 }
18123
18124#ifdef ENABLE_LIBCTF
18125 if (dump & CTF_DUMP)
18126 {
18127 if (! dump_section_as_ctf (section, filedata))
18128 res = false;
18129 }
18130#endif
18131 if (dump & SFRAME_DUMP)
18132 {
18133 if (! display_debug_section (i, section, filedata))
18134 res = false;
18135 }
18136 }
18137
18138 if (! filedata->is_separate)
18139 {
18140 /* Check to see if the user requested a
18141 dump of a section that does not exist. */
18142 for (; i < filedata->dump.num_dump_sects; i++)
18143 if (filedata->dump.dump_sects[i])
18144 {
18145 warn (_("Section %d was not dumped because it does not exist!\n"), i);
18146 res = false;
18147 }
18148 }
18149
18150 return res;
18151}
18152
18153static void
18154process_mips_fpe_exception (int mask)
18155{
18156 if (mask)
18157 {
18158 bool first = true;
18159
18160 if (mask & OEX_FPU_INEX)
18161 fputs ("INEX", stdout), first = false;
18162 if (mask & OEX_FPU_UFLO)
18163 printf ("%sUFLO", first ? "" : "|"), first = false;
18164 if (mask & OEX_FPU_OFLO)
18165 printf ("%sOFLO", first ? "" : "|"), first = false;
18166 if (mask & OEX_FPU_DIV0)
18167 printf ("%sDIV0", first ? "" : "|"), first = false;
18168 if (mask & OEX_FPU_INVAL)
18169 printf ("%sINVAL", first ? "" : "|");
18170 }
18171 else
18172 fputs ("0", stdout);
18173}
18174
18175/* Display's the value of TAG at location P. If TAG is
18176 greater than 0 it is assumed to be an unknown tag, and
18177 a message is printed to this effect. Otherwise it is
18178 assumed that a message has already been printed.
18179
18180 If the bottom bit of TAG is set it assumed to have a
18181 string value, otherwise it is assumed to have an integer
18182 value.
18183
18184 Returns an updated P pointing to the first unread byte
18185 beyond the end of TAG's value.
18186
18187 Reads at or beyond END will not be made. */
18188
18189static unsigned char *
18190display_tag_value (signed int tag,
18191 unsigned char * p,
18192 const unsigned char * const end)
18193{
18194 uint64_t val;
18195
18196 if (tag > 0)
18197 printf (" Tag_unknown_%d: ", tag);
18198
18199 if (p >= end)
18200 {
18201 warn (_("<corrupt tag>\n"));
18202 }
18203 else if (tag & 1)
18204 {
18205 /* PR 17531 file: 027-19978-0.004. */
18206 size_t maxlen = end - p;
18207
18208 putchar ('"');
18209 if (maxlen > 0)
18210 {
18211 maxlen -= 1; /* Remove \0 from the character count. */
18212 print_symbol_name ((int) maxlen, (const char *) p);
18213 size_t len = strnlen ((char *) p, maxlen);
18214 if (len == maxlen && p[maxlen] != '\0')
18215 printf (_("<corrupt string tag>"));
18216 p += len + 1;
18217 }
18218 else
18219 {
18220 printf (_("<corrupt string tag>"));
18221 p = (unsigned char *) end;
18222 }
18223 printf ("\"\n");
18224 }
18225 else
18226 {
18227 READ_ULEB (val, p, end);
18228 printf ("%" PRId64 " (0x%" PRIx64 ")\n", val, val);
18229 }
18230
18231 assert (p <= end);
18232 return p;
18233}
18234
18235/* ARC ABI attributes section. */
18236
18237static unsigned char *
18238display_arc_attribute (unsigned char * p,
18239 const unsigned char * const end)
18240{
18241 unsigned int tag;
18242 unsigned int val;
18243
18244 READ_ULEB (tag, p, end);
18245
18246 switch (tag)
18247 {
18248 case Tag_ARC_PCS_config:
18249 READ_ULEB (val, p, end);
18250 printf (" Tag_ARC_PCS_config: ");
18251 switch (val)
18252 {
18253 case 0:
18254 printf (_("Absent/Non standard\n"));
18255 break;
18256 case 1:
18257 printf (_("Bare metal/mwdt\n"));
18258 break;
18259 case 2:
18260 printf (_("Bare metal/newlib\n"));
18261 break;
18262 case 3:
18263 printf (_("Linux/uclibc\n"));
18264 break;
18265 case 4:
18266 printf (_("Linux/glibc\n"));
18267 break;
18268 default:
18269 printf (_("Unknown\n"));
18270 break;
18271 }
18272 break;
18273
18274 case Tag_ARC_CPU_base:
18275 READ_ULEB (val, p, end);
18276 printf (" Tag_ARC_CPU_base: ");
18277 switch (val)
18278 {
18279 default:
18280 case TAG_CPU_NONE:
18281 printf (_("Absent\n"));
18282 break;
18283 case TAG_CPU_ARC6xx:
18284 printf ("ARC6xx\n");
18285 break;
18286 case TAG_CPU_ARC7xx:
18287 printf ("ARC7xx\n");
18288 break;
18289 case TAG_CPU_ARCEM:
18290 printf ("ARCEM\n");
18291 break;
18292 case TAG_CPU_ARCHS:
18293 printf ("ARCHS\n");
18294 break;
18295 }
18296 break;
18297
18298 case Tag_ARC_CPU_variation:
18299 READ_ULEB (val, p, end);
18300 printf (" Tag_ARC_CPU_variation: ");
18301 switch (val)
18302 {
18303 default:
18304 if (val > 0 && val < 16)
18305 printf ("Core%d\n", val);
18306 else
18307 printf ("Unknown\n");
18308 break;
18309
18310 case 0:
18311 printf (_("Absent\n"));
18312 break;
18313 }
18314 break;
18315
18316 case Tag_ARC_CPU_name:
18317 printf (" Tag_ARC_CPU_name: ");
18318 p = display_tag_value (-1, p, end);
18319 break;
18320
18321 case Tag_ARC_ABI_rf16:
18322 READ_ULEB (val, p, end);
18323 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
18324 break;
18325
18326 case Tag_ARC_ABI_osver:
18327 READ_ULEB (val, p, end);
18328 printf (" Tag_ARC_ABI_osver: v%d\n", val);
18329 break;
18330
18331 case Tag_ARC_ABI_pic:
18332 case Tag_ARC_ABI_sda:
18333 READ_ULEB (val, p, end);
18334 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
18335 : " Tag_ARC_ABI_pic: ");
18336 switch (val)
18337 {
18338 case 0:
18339 printf (_("Absent\n"));
18340 break;
18341 case 1:
18342 printf ("MWDT\n");
18343 break;
18344 case 2:
18345 printf ("GNU\n");
18346 break;
18347 default:
18348 printf (_("Unknown\n"));
18349 break;
18350 }
18351 break;
18352
18353 case Tag_ARC_ABI_tls:
18354 READ_ULEB (val, p, end);
18355 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
18356 break;
18357
18358 case Tag_ARC_ABI_enumsize:
18359 READ_ULEB (val, p, end);
18360 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
18361 _("smallest"));
18362 break;
18363
18364 case Tag_ARC_ABI_exceptions:
18365 READ_ULEB (val, p, end);
18366 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
18367 : _("default"));
18368 break;
18369
18370 case Tag_ARC_ABI_double_size:
18371 READ_ULEB (val, p, end);
18372 printf (" Tag_ARC_ABI_double_size: %d\n", val);
18373 break;
18374
18375 case Tag_ARC_ISA_config:
18376 printf (" Tag_ARC_ISA_config: ");
18377 p = display_tag_value (-1, p, end);
18378 break;
18379
18380 case Tag_ARC_ISA_apex:
18381 printf (" Tag_ARC_ISA_apex: ");
18382 p = display_tag_value (-1, p, end);
18383 break;
18384
18385 case Tag_ARC_ISA_mpy_option:
18386 READ_ULEB (val, p, end);
18387 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
18388 break;
18389
18390 case Tag_ARC_ATR_version:
18391 READ_ULEB (val, p, end);
18392 printf (" Tag_ARC_ATR_version: %d\n", val);
18393 break;
18394
18395 default:
18396 return display_tag_value (tag & 1, p, end);
18397 }
18398
18399 return p;
18400}
18401
18402/* ARM EABI attributes section. */
18403typedef struct
18404{
18405 unsigned int tag;
18406 const char * name;
18407 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
18408 unsigned int type;
18409 const char *const *table;
18410} arm_attr_public_tag;
18411
18412static const char *const arm_attr_tag_CPU_arch[] =
18413 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
18414 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
18415 "v8-M.mainline", "v8.1-A", "v8.2-A", "v8.3-A",
18416 "v8.1-M.mainline", "v9"};
18417static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
18418static const char *const arm_attr_tag_THUMB_ISA_use[] =
18419 {"No", "Thumb-1", "Thumb-2", "Yes"};
18420static const char *const arm_attr_tag_FP_arch[] =
18421 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
18422 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
18423static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
18424static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
18425 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
18426 "NEON for ARMv8.1"};
18427static const char *const arm_attr_tag_PCS_config[] =
18428 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
18429 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
18430static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
18431 {"V6", "SB", "TLS", "Unused"};
18432static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
18433 {"Absolute", "PC-relative", "SB-relative", "None"};
18434static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
18435 {"Absolute", "PC-relative", "None"};
18436static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
18437 {"None", "direct", "GOT-indirect"};
18438static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
18439 {"None", "??? 1", "2", "??? 3", "4"};
18440static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
18441static const char *const arm_attr_tag_ABI_FP_denormal[] =
18442 {"Unused", "Needed", "Sign only"};
18443static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
18444static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
18445static const char *const arm_attr_tag_ABI_FP_number_model[] =
18446 {"Unused", "Finite", "RTABI", "IEEE 754"};
18447static const char *const arm_attr_tag_ABI_enum_size[] =
18448 {"Unused", "small", "int", "forced to int"};
18449static const char *const arm_attr_tag_ABI_HardFP_use[] =
18450 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
18451static const char *const arm_attr_tag_ABI_VFP_args[] =
18452 {"AAPCS", "VFP registers", "custom", "compatible"};
18453static const char *const arm_attr_tag_ABI_WMMX_args[] =
18454 {"AAPCS", "WMMX registers", "custom"};
18455static const char *const arm_attr_tag_ABI_optimization_goals[] =
18456 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
18457 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
18458static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
18459 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
18460 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
18461static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
18462static const char *const arm_attr_tag_FP_HP_extension[] =
18463 {"Not Allowed", "Allowed"};
18464static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
18465 {"None", "IEEE 754", "Alternative Format"};
18466static const char *const arm_attr_tag_DSP_extension[] =
18467 {"Follow architecture", "Allowed"};
18468static const char *const arm_attr_tag_MPextension_use[] =
18469 {"Not Allowed", "Allowed"};
18470static const char *const arm_attr_tag_DIV_use[] =
18471 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
18472 "Allowed in v7-A with integer division extension"};
18473static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
18474static const char *const arm_attr_tag_Virtualization_use[] =
18475 {"Not Allowed", "TrustZone", "Virtualization Extensions",
18476 "TrustZone and Virtualization Extensions"};
18477static const char *const arm_attr_tag_MPextension_use_legacy[] =
18478 {"Not Allowed", "Allowed"};
18479
18480static const char *const arm_attr_tag_MVE_arch[] =
18481 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
18482
18483static const char * arm_attr_tag_PAC_extension[] =
18484 {"No PAC/AUT instructions",
18485 "PAC/AUT instructions permitted in the NOP space",
18486 "PAC/AUT instructions permitted in the NOP and in the non-NOP space"};
18487
18488static const char * arm_attr_tag_BTI_extension[] =
18489 {"BTI instructions not permitted",
18490 "BTI instructions permitted in the NOP space",
18491 "BTI instructions permitted in the NOP and in the non-NOP space"};
18492
18493static const char * arm_attr_tag_BTI_use[] =
18494 {"Compiled without branch target enforcement",
18495 "Compiled with branch target enforcement"};
18496
18497static const char * arm_attr_tag_PACRET_use[] =
18498 {"Compiled without return address signing and authentication",
18499 "Compiled with return address signing and authentication"};
18500
18501#define LOOKUP(id, name) \
18502 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
18503static arm_attr_public_tag arm_attr_public_tags[] =
18504{
18505 {4, "CPU_raw_name", 1, NULL},
18506 {5, "CPU_name", 1, NULL},
18507 LOOKUP(6, CPU_arch),
18508 {7, "CPU_arch_profile", 0, NULL},
18509 LOOKUP(8, ARM_ISA_use),
18510 LOOKUP(9, THUMB_ISA_use),
18511 LOOKUP(10, FP_arch),
18512 LOOKUP(11, WMMX_arch),
18513 LOOKUP(12, Advanced_SIMD_arch),
18514 LOOKUP(13, PCS_config),
18515 LOOKUP(14, ABI_PCS_R9_use),
18516 LOOKUP(15, ABI_PCS_RW_data),
18517 LOOKUP(16, ABI_PCS_RO_data),
18518 LOOKUP(17, ABI_PCS_GOT_use),
18519 LOOKUP(18, ABI_PCS_wchar_t),
18520 LOOKUP(19, ABI_FP_rounding),
18521 LOOKUP(20, ABI_FP_denormal),
18522 LOOKUP(21, ABI_FP_exceptions),
18523 LOOKUP(22, ABI_FP_user_exceptions),
18524 LOOKUP(23, ABI_FP_number_model),
18525 {24, "ABI_align_needed", 0, NULL},
18526 {25, "ABI_align_preserved", 0, NULL},
18527 LOOKUP(26, ABI_enum_size),
18528 LOOKUP(27, ABI_HardFP_use),
18529 LOOKUP(28, ABI_VFP_args),
18530 LOOKUP(29, ABI_WMMX_args),
18531 LOOKUP(30, ABI_optimization_goals),
18532 LOOKUP(31, ABI_FP_optimization_goals),
18533 {32, "compatibility", 0, NULL},
18534 LOOKUP(34, CPU_unaligned_access),
18535 LOOKUP(36, FP_HP_extension),
18536 LOOKUP(38, ABI_FP_16bit_format),
18537 LOOKUP(42, MPextension_use),
18538 LOOKUP(44, DIV_use),
18539 LOOKUP(46, DSP_extension),
18540 LOOKUP(48, MVE_arch),
18541 LOOKUP(50, PAC_extension),
18542 LOOKUP(52, BTI_extension),
18543 LOOKUP(74, BTI_use),
18544 LOOKUP(76, PACRET_use),
18545 {64, "nodefaults", 0, NULL},
18546 {65, "also_compatible_with", 0, NULL},
18547 LOOKUP(66, T2EE_use),
18548 {67, "conformance", 1, NULL},
18549 LOOKUP(68, Virtualization_use),
18550 LOOKUP(70, MPextension_use_legacy)
18551};
18552#undef LOOKUP
18553
18554static unsigned char *
18555display_arm_attribute (unsigned char * p,
18556 const unsigned char * const end)
18557{
18558 unsigned int tag;
18559 unsigned int val;
18560 arm_attr_public_tag * attr;
18561 unsigned i;
18562 unsigned int type;
18563
18564 READ_ULEB (tag, p, end);
18565 attr = NULL;
18566 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
18567 {
18568 if (arm_attr_public_tags[i].tag == tag)
18569 {
18570 attr = &arm_attr_public_tags[i];
18571 break;
18572 }
18573 }
18574
18575 if (attr)
18576 {
18577 printf (" Tag_%s: ", attr->name);
18578 switch (attr->type)
18579 {
18580 case 0:
18581 switch (tag)
18582 {
18583 case 7: /* Tag_CPU_arch_profile. */
18584 READ_ULEB (val, p, end);
18585 switch (val)
18586 {
18587 case 0: printf (_("None\n")); break;
18588 case 'A': printf (_("Application\n")); break;
18589 case 'R': printf (_("Realtime\n")); break;
18590 case 'M': printf (_("Microcontroller\n")); break;
18591 case 'S': printf (_("Application or Realtime\n")); break;
18592 default: printf ("??? (%d)\n", val); break;
18593 }
18594 break;
18595
18596 case 24: /* Tag_align_needed. */
18597 READ_ULEB (val, p, end);
18598 switch (val)
18599 {
18600 case 0: printf (_("None\n")); break;
18601 case 1: printf (_("8-byte\n")); break;
18602 case 2: printf (_("4-byte\n")); break;
18603 case 3: printf ("??? 3\n"); break;
18604 default:
18605 if (val <= 12)
18606 printf (_("8-byte and up to %d-byte extended\n"),
18607 1 << val);
18608 else
18609 printf ("??? (%d)\n", val);
18610 break;
18611 }
18612 break;
18613
18614 case 25: /* Tag_align_preserved. */
18615 READ_ULEB (val, p, end);
18616 switch (val)
18617 {
18618 case 0: printf (_("None\n")); break;
18619 case 1: printf (_("8-byte, except leaf SP\n")); break;
18620 case 2: printf (_("8-byte\n")); break;
18621 case 3: printf ("??? 3\n"); break;
18622 default:
18623 if (val <= 12)
18624 printf (_("8-byte and up to %d-byte extended\n"),
18625 1 << val);
18626 else
18627 printf ("??? (%d)\n", val);
18628 break;
18629 }
18630 break;
18631
18632 case 32: /* Tag_compatibility. */
18633 {
18634 READ_ULEB (val, p, end);
18635 printf (_("flag = %d, vendor = "), val);
18636 if (p < end - 1)
18637 {
18638 size_t maxlen = (end - p) - 1;
18639
18640 print_symbol_name ((int) maxlen, (const char *) p);
18641 p += strnlen ((char *) p, maxlen) + 1;
18642 }
18643 else
18644 {
18645 printf (_("<corrupt>"));
18646 p = (unsigned char *) end;
18647 }
18648 putchar ('\n');
18649 }
18650 break;
18651
18652 case 64: /* Tag_nodefaults. */
18653 /* PR 17531: file: 001-505008-0.01. */
18654 if (p < end)
18655 p++;
18656 printf (_("True\n"));
18657 break;
18658
18659 case 65: /* Tag_also_compatible_with. */
18660 READ_ULEB (val, p, end);
18661 if (val == 6 /* Tag_CPU_arch. */)
18662 {
18663 READ_ULEB (val, p, end);
18664 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
18665 printf ("??? (%d)\n", val);
18666 else
18667 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
18668 }
18669 else
18670 printf ("???\n");
18671 while (p < end && *(p++) != '\0' /* NUL terminator. */)
18672 ;
18673 break;
18674
18675 default:
18676 printf (_("<unknown: %d>\n"), tag);
18677 break;
18678 }
18679 return p;
18680
18681 case 1:
18682 return display_tag_value (-1, p, end);
18683 case 2:
18684 return display_tag_value (0, p, end);
18685
18686 default:
18687 assert (attr->type & 0x80);
18688 READ_ULEB (val, p, end);
18689 type = attr->type & 0x7f;
18690 if (val >= type)
18691 printf ("??? (%d)\n", val);
18692 else
18693 printf ("%s\n", attr->table[val]);
18694 return p;
18695 }
18696 }
18697
18698 return display_tag_value (tag, p, end);
18699}
18700
18701static unsigned char *
18702display_gnu_attribute (unsigned char * p,
18703 unsigned char * (* display_proc_gnu_attribute)
18704 (unsigned char *, unsigned int, const unsigned char * const),
18705 const unsigned char * const end)
18706{
18707 unsigned int tag;
18708 unsigned int val;
18709
18710 READ_ULEB (tag, p, end);
18711
18712 /* Tag_compatibility is the only generic GNU attribute defined at
18713 present. */
18714 if (tag == 32)
18715 {
18716 READ_ULEB (val, p, end);
18717
18718 printf (_("flag = %d, vendor = "), val);
18719 if (p == end)
18720 {
18721 printf (_("<corrupt>\n"));
18722 warn (_("corrupt vendor attribute\n"));
18723 }
18724 else
18725 {
18726 if (p < end - 1)
18727 {
18728 size_t maxlen = (end - p) - 1;
18729
18730 print_symbol_name ((int) maxlen, (const char *) p);
18731 p += strnlen ((char *) p, maxlen) + 1;
18732 }
18733 else
18734 {
18735 printf (_("<corrupt>"));
18736 p = (unsigned char *) end;
18737 }
18738 putchar ('\n');
18739 }
18740 return p;
18741 }
18742
18743 if ((tag & 2) == 0 && display_proc_gnu_attribute)
18744 return display_proc_gnu_attribute (p, tag, end);
18745
18746 return display_tag_value (tag, p, end);
18747}
18748
18749static unsigned char *
18750display_m68k_gnu_attribute (unsigned char * p,
18751 unsigned int tag,
18752 const unsigned char * const end)
18753{
18754 unsigned int val;
18755
18756 if (tag == Tag_GNU_M68K_ABI_FP)
18757 {
18758 printf (" Tag_GNU_M68K_ABI_FP: ");
18759 if (p == end)
18760 {
18761 printf (_("<corrupt>\n"));
18762 return p;
18763 }
18764 READ_ULEB (val, p, end);
18765
18766 if (val > 3)
18767 printf ("(%#x), ", val);
18768
18769 switch (val & 3)
18770 {
18771 case 0:
18772 printf (_("unspecified hard/soft float\n"));
18773 break;
18774 case 1:
18775 printf (_("hard float\n"));
18776 break;
18777 case 2:
18778 printf (_("soft float\n"));
18779 break;
18780 }
18781 return p;
18782 }
18783
18784 return display_tag_value (tag & 1, p, end);
18785}
18786
18787static unsigned char *
18788display_power_gnu_attribute (unsigned char * p,
18789 unsigned int tag,
18790 const unsigned char * const end)
18791{
18792 unsigned int val;
18793
18794 if (tag == Tag_GNU_Power_ABI_FP)
18795 {
18796 printf (" Tag_GNU_Power_ABI_FP: ");
18797 if (p == end)
18798 {
18799 printf (_("<corrupt>\n"));
18800 return p;
18801 }
18802 READ_ULEB (val, p, end);
18803
18804 if (val > 15)
18805 printf ("(%#x), ", val);
18806
18807 switch (val & 3)
18808 {
18809 case 0:
18810 printf (_("unspecified hard/soft float, "));
18811 break;
18812 case 1:
18813 printf (_("hard float, "));
18814 break;
18815 case 2:
18816 printf (_("soft float, "));
18817 break;
18818 case 3:
18819 printf (_("single-precision hard float, "));
18820 break;
18821 }
18822
18823 switch (val & 0xC)
18824 {
18825 case 0:
18826 printf (_("unspecified long double\n"));
18827 break;
18828 case 4:
18829 printf (_("128-bit IBM long double\n"));
18830 break;
18831 case 8:
18832 printf (_("64-bit long double\n"));
18833 break;
18834 case 12:
18835 printf (_("128-bit IEEE long double\n"));
18836 break;
18837 }
18838 return p;
18839 }
18840
18841 if (tag == Tag_GNU_Power_ABI_Vector)
18842 {
18843 printf (" Tag_GNU_Power_ABI_Vector: ");
18844 if (p == end)
18845 {
18846 printf (_("<corrupt>\n"));
18847 return p;
18848 }
18849 READ_ULEB (val, p, end);
18850
18851 if (val > 3)
18852 printf ("(%#x), ", val);
18853
18854 switch (val & 3)
18855 {
18856 case 0:
18857 printf (_("unspecified\n"));
18858 break;
18859 case 1:
18860 printf (_("generic\n"));
18861 break;
18862 case 2:
18863 printf ("AltiVec\n");
18864 break;
18865 case 3:
18866 printf ("SPE\n");
18867 break;
18868 }
18869 return p;
18870 }
18871
18872 if (tag == Tag_GNU_Power_ABI_Struct_Return)
18873 {
18874 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
18875 if (p == end)
18876 {
18877 printf (_("<corrupt>\n"));
18878 return p;
18879 }
18880 READ_ULEB (val, p, end);
18881
18882 if (val > 2)
18883 printf ("(%#x), ", val);
18884
18885 switch (val & 3)
18886 {
18887 case 0:
18888 printf (_("unspecified\n"));
18889 break;
18890 case 1:
18891 printf ("r3/r4\n");
18892 break;
18893 case 2:
18894 printf (_("memory\n"));
18895 break;
18896 case 3:
18897 printf ("???\n");
18898 break;
18899 }
18900 return p;
18901 }
18902
18903 return display_tag_value (tag & 1, p, end);
18904}
18905
18906static unsigned char *
18907display_s390_gnu_attribute (unsigned char * p,
18908 unsigned int tag,
18909 const unsigned char * const end)
18910{
18911 unsigned int val;
18912
18913 if (tag == Tag_GNU_S390_ABI_Vector)
18914 {
18915 printf (" Tag_GNU_S390_ABI_Vector: ");
18916 READ_ULEB (val, p, end);
18917
18918 switch (val)
18919 {
18920 case 0:
18921 printf (_("any\n"));
18922 break;
18923 case 1:
18924 printf (_("software\n"));
18925 break;
18926 case 2:
18927 printf (_("hardware\n"));
18928 break;
18929 default:
18930 printf ("??? (%d)\n", val);
18931 break;
18932 }
18933 return p;
18934 }
18935
18936 return display_tag_value (tag & 1, p, end);
18937}
18938
18939static void
18940display_sparc_hwcaps (unsigned int mask)
18941{
18942 if (mask)
18943 {
18944 bool first = true;
18945
18946 if (mask & ELF_SPARC_HWCAP_MUL32)
18947 fputs ("mul32", stdout), first = false;
18948 if (mask & ELF_SPARC_HWCAP_DIV32)
18949 printf ("%sdiv32", first ? "" : "|"), first = false;
18950 if (mask & ELF_SPARC_HWCAP_FSMULD)
18951 printf ("%sfsmuld", first ? "" : "|"), first = false;
18952 if (mask & ELF_SPARC_HWCAP_V8PLUS)
18953 printf ("%sv8plus", first ? "" : "|"), first = false;
18954 if (mask & ELF_SPARC_HWCAP_POPC)
18955 printf ("%spopc", first ? "" : "|"), first = false;
18956 if (mask & ELF_SPARC_HWCAP_VIS)
18957 printf ("%svis", first ? "" : "|"), first = false;
18958 if (mask & ELF_SPARC_HWCAP_VIS2)
18959 printf ("%svis2", first ? "" : "|"), first = false;
18960 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
18961 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
18962 if (mask & ELF_SPARC_HWCAP_FMAF)
18963 printf ("%sfmaf", first ? "" : "|"), first = false;
18964 if (mask & ELF_SPARC_HWCAP_VIS3)
18965 printf ("%svis3", first ? "" : "|"), first = false;
18966 if (mask & ELF_SPARC_HWCAP_HPC)
18967 printf ("%shpc", first ? "" : "|"), first = false;
18968 if (mask & ELF_SPARC_HWCAP_RANDOM)
18969 printf ("%srandom", first ? "" : "|"), first = false;
18970 if (mask & ELF_SPARC_HWCAP_TRANS)
18971 printf ("%strans", first ? "" : "|"), first = false;
18972 if (mask & ELF_SPARC_HWCAP_FJFMAU)
18973 printf ("%sfjfmau", first ? "" : "|"), first = false;
18974 if (mask & ELF_SPARC_HWCAP_IMA)
18975 printf ("%sima", first ? "" : "|"), first = false;
18976 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
18977 printf ("%scspare", first ? "" : "|"), first = false;
18978 }
18979 else
18980 fputc ('0', stdout);
18981 fputc ('\n', stdout);
18982}
18983
18984static void
18985display_sparc_hwcaps2 (unsigned int mask)
18986{
18987 if (mask)
18988 {
18989 bool first = true;
18990
18991 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
18992 fputs ("fjathplus", stdout), first = false;
18993 if (mask & ELF_SPARC_HWCAP2_VIS3B)
18994 printf ("%svis3b", first ? "" : "|"), first = false;
18995 if (mask & ELF_SPARC_HWCAP2_ADP)
18996 printf ("%sadp", first ? "" : "|"), first = false;
18997 if (mask & ELF_SPARC_HWCAP2_SPARC5)
18998 printf ("%ssparc5", first ? "" : "|"), first = false;
18999 if (mask & ELF_SPARC_HWCAP2_MWAIT)
19000 printf ("%smwait", first ? "" : "|"), first = false;
19001 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
19002 printf ("%sxmpmul", first ? "" : "|"), first = false;
19003 if (mask & ELF_SPARC_HWCAP2_XMONT)
19004 printf ("%sxmont2", first ? "" : "|"), first = false;
19005 if (mask & ELF_SPARC_HWCAP2_NSEC)
19006 printf ("%snsec", first ? "" : "|"), first = false;
19007 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
19008 printf ("%sfjathhpc", first ? "" : "|"), first = false;
19009 if (mask & ELF_SPARC_HWCAP2_FJDES)
19010 printf ("%sfjdes", first ? "" : "|"), first = false;
19011 if (mask & ELF_SPARC_HWCAP2_FJAES)
19012 printf ("%sfjaes", first ? "" : "|"), first = false;
19013 }
19014 else
19015 fputc ('0', stdout);
19016 fputc ('\n', stdout);
19017}
19018
19019static unsigned char *
19020display_sparc_gnu_attribute (unsigned char * p,
19021 unsigned int tag,
19022 const unsigned char * const end)
19023{
19024 unsigned int val;
19025
19026 if (tag == Tag_GNU_Sparc_HWCAPS)
19027 {
19028 READ_ULEB (val, p, end);
19029 printf (" Tag_GNU_Sparc_HWCAPS: ");
19030 display_sparc_hwcaps (val);
19031 return p;
19032 }
19033 if (tag == Tag_GNU_Sparc_HWCAPS2)
19034 {
19035 READ_ULEB (val, p, end);
19036 printf (" Tag_GNU_Sparc_HWCAPS2: ");
19037 display_sparc_hwcaps2 (val);
19038 return p;
19039 }
19040
19041 return display_tag_value (tag, p, end);
19042}
19043
19044static void
19045print_mips_fp_abi_value (unsigned int val)
19046{
19047 switch (val)
19048 {
19049 case Val_GNU_MIPS_ABI_FP_ANY:
19050 printf (_("Hard or soft float\n"));
19051 break;
19052 case Val_GNU_MIPS_ABI_FP_DOUBLE:
19053 printf (_("Hard float (double precision)\n"));
19054 break;
19055 case Val_GNU_MIPS_ABI_FP_SINGLE:
19056 printf (_("Hard float (single precision)\n"));
19057 break;
19058 case Val_GNU_MIPS_ABI_FP_SOFT:
19059 printf (_("Soft float\n"));
19060 break;
19061 case Val_GNU_MIPS_ABI_FP_OLD_64:
19062 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
19063 break;
19064 case Val_GNU_MIPS_ABI_FP_XX:
19065 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
19066 break;
19067 case Val_GNU_MIPS_ABI_FP_64:
19068 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
19069 break;
19070 case Val_GNU_MIPS_ABI_FP_64A:
19071 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
19072 break;
19073 case Val_GNU_MIPS_ABI_FP_NAN2008:
19074 printf (_("NaN 2008 compatibility\n"));
19075 break;
19076 default:
19077 printf ("??? (%d)\n", val);
19078 break;
19079 }
19080}
19081
19082static unsigned char *
19083display_mips_gnu_attribute (unsigned char * p,
19084 unsigned int tag,
19085 const unsigned char * const end)
19086{
19087 if (tag == Tag_GNU_MIPS_ABI_FP)
19088 {
19089 unsigned int val;
19090
19091 printf (" Tag_GNU_MIPS_ABI_FP: ");
19092 READ_ULEB (val, p, end);
19093 print_mips_fp_abi_value (val);
19094 return p;
19095 }
19096
19097 if (tag == Tag_GNU_MIPS_ABI_MSA)
19098 {
19099 unsigned int val;
19100
19101 printf (" Tag_GNU_MIPS_ABI_MSA: ");
19102 READ_ULEB (val, p, end);
19103
19104 switch (val)
19105 {
19106 case Val_GNU_MIPS_ABI_MSA_ANY:
19107 printf (_("Any MSA or not\n"));
19108 break;
19109 case Val_GNU_MIPS_ABI_MSA_128:
19110 printf (_("128-bit MSA\n"));
19111 break;
19112 default:
19113 printf ("??? (%d)\n", val);
19114 break;
19115 }
19116 return p;
19117 }
19118
19119 return display_tag_value (tag & 1, p, end);
19120}
19121
19122static unsigned char *
19123display_tic6x_attribute (unsigned char * p,
19124 const unsigned char * const end)
19125{
19126 unsigned int tag;
19127 unsigned int val;
19128
19129 READ_ULEB (tag, p, end);
19130
19131 switch (tag)
19132 {
19133 case Tag_ISA:
19134 printf (" Tag_ISA: ");
19135 READ_ULEB (val, p, end);
19136
19137 switch (val)
19138 {
19139 case C6XABI_Tag_ISA_none:
19140 printf (_("None\n"));
19141 break;
19142 case C6XABI_Tag_ISA_C62X:
19143 printf ("C62x\n");
19144 break;
19145 case C6XABI_Tag_ISA_C67X:
19146 printf ("C67x\n");
19147 break;
19148 case C6XABI_Tag_ISA_C67XP:
19149 printf ("C67x+\n");
19150 break;
19151 case C6XABI_Tag_ISA_C64X:
19152 printf ("C64x\n");
19153 break;
19154 case C6XABI_Tag_ISA_C64XP:
19155 printf ("C64x+\n");
19156 break;
19157 case C6XABI_Tag_ISA_C674X:
19158 printf ("C674x\n");
19159 break;
19160 default:
19161 printf ("??? (%d)\n", val);
19162 break;
19163 }
19164 return p;
19165
19166 case Tag_ABI_wchar_t:
19167 printf (" Tag_ABI_wchar_t: ");
19168 READ_ULEB (val, p, end);
19169 switch (val)
19170 {
19171 case 0:
19172 printf (_("Not used\n"));
19173 break;
19174 case 1:
19175 printf (_("2 bytes\n"));
19176 break;
19177 case 2:
19178 printf (_("4 bytes\n"));
19179 break;
19180 default:
19181 printf ("??? (%d)\n", val);
19182 break;
19183 }
19184 return p;
19185
19186 case Tag_ABI_stack_align_needed:
19187 printf (" Tag_ABI_stack_align_needed: ");
19188 READ_ULEB (val, p, end);
19189 switch (val)
19190 {
19191 case 0:
19192 printf (_("8-byte\n"));
19193 break;
19194 case 1:
19195 printf (_("16-byte\n"));
19196 break;
19197 default:
19198 printf ("??? (%d)\n", val);
19199 break;
19200 }
19201 return p;
19202
19203 case Tag_ABI_stack_align_preserved:
19204 READ_ULEB (val, p, end);
19205 printf (" Tag_ABI_stack_align_preserved: ");
19206 switch (val)
19207 {
19208 case 0:
19209 printf (_("8-byte\n"));
19210 break;
19211 case 1:
19212 printf (_("16-byte\n"));
19213 break;
19214 default:
19215 printf ("??? (%d)\n", val);
19216 break;
19217 }
19218 return p;
19219
19220 case Tag_ABI_DSBT:
19221 READ_ULEB (val, p, end);
19222 printf (" Tag_ABI_DSBT: ");
19223 switch (val)
19224 {
19225 case 0:
19226 printf (_("DSBT addressing not used\n"));
19227 break;
19228 case 1:
19229 printf (_("DSBT addressing used\n"));
19230 break;
19231 default:
19232 printf ("??? (%d)\n", val);
19233 break;
19234 }
19235 return p;
19236
19237 case Tag_ABI_PID:
19238 READ_ULEB (val, p, end);
19239 printf (" Tag_ABI_PID: ");
19240 switch (val)
19241 {
19242 case 0:
19243 printf (_("Data addressing position-dependent\n"));
19244 break;
19245 case 1:
19246 printf (_("Data addressing position-independent, GOT near DP\n"));
19247 break;
19248 case 2:
19249 printf (_("Data addressing position-independent, GOT far from DP\n"));
19250 break;
19251 default:
19252 printf ("??? (%d)\n", val);
19253 break;
19254 }
19255 return p;
19256
19257 case Tag_ABI_PIC:
19258 READ_ULEB (val, p, end);
19259 printf (" Tag_ABI_PIC: ");
19260 switch (val)
19261 {
19262 case 0:
19263 printf (_("Code addressing position-dependent\n"));
19264 break;
19265 case 1:
19266 printf (_("Code addressing position-independent\n"));
19267 break;
19268 default:
19269 printf ("??? (%d)\n", val);
19270 break;
19271 }
19272 return p;
19273
19274 case Tag_ABI_array_object_alignment:
19275 READ_ULEB (val, p, end);
19276 printf (" Tag_ABI_array_object_alignment: ");
19277 switch (val)
19278 {
19279 case 0:
19280 printf (_("8-byte\n"));
19281 break;
19282 case 1:
19283 printf (_("4-byte\n"));
19284 break;
19285 case 2:
19286 printf (_("16-byte\n"));
19287 break;
19288 default:
19289 printf ("??? (%d)\n", val);
19290 break;
19291 }
19292 return p;
19293
19294 case Tag_ABI_array_object_align_expected:
19295 READ_ULEB (val, p, end);
19296 printf (" Tag_ABI_array_object_align_expected: ");
19297 switch (val)
19298 {
19299 case 0:
19300 printf (_("8-byte\n"));
19301 break;
19302 case 1:
19303 printf (_("4-byte\n"));
19304 break;
19305 case 2:
19306 printf (_("16-byte\n"));
19307 break;
19308 default:
19309 printf ("??? (%d)\n", val);
19310 break;
19311 }
19312 return p;
19313
19314 case Tag_ABI_compatibility:
19315 {
19316 READ_ULEB (val, p, end);
19317 printf (" Tag_ABI_compatibility: ");
19318 printf (_("flag = %d, vendor = "), val);
19319 if (p < end - 1)
19320 {
19321 size_t maxlen = (end - p) - 1;
19322
19323 print_symbol_name ((int) maxlen, (const char *) p);
19324 p += strnlen ((char *) p, maxlen) + 1;
19325 }
19326 else
19327 {
19328 printf (_("<corrupt>"));
19329 p = (unsigned char *) end;
19330 }
19331 putchar ('\n');
19332 return p;
19333 }
19334
19335 case Tag_ABI_conformance:
19336 {
19337 printf (" Tag_ABI_conformance: \"");
19338 if (p < end - 1)
19339 {
19340 size_t maxlen = (end - p) - 1;
19341
19342 print_symbol_name ((int) maxlen, (const char *) p);
19343 p += strnlen ((char *) p, maxlen) + 1;
19344 }
19345 else
19346 {
19347 printf (_("<corrupt>"));
19348 p = (unsigned char *) end;
19349 }
19350 printf ("\"\n");
19351 return p;
19352 }
19353 }
19354
19355 return display_tag_value (tag, p, end);
19356}
19357
19358static void
19359display_raw_attribute (unsigned char * p, unsigned char const * const end)
19360{
19361 uint64_t addr = 0;
19362 size_t bytes = end - p;
19363
19364 assert (end >= p);
19365 while (bytes)
19366 {
19367 int j;
19368 int k;
19369 int lbytes = (bytes > 16 ? 16 : bytes);
19370
19371 printf (" 0x%8.8" PRIx64 " ", addr);
19372
19373 for (j = 0; j < 16; j++)
19374 {
19375 if (j < lbytes)
19376 printf ("%2.2x", p[j]);
19377 else
19378 printf (" ");
19379
19380 if ((j & 3) == 3)
19381 printf (" ");
19382 }
19383
19384 for (j = 0; j < lbytes; j++)
19385 {
19386 k = p[j];
19387 if (k >= ' ' && k < 0x7f)
19388 printf ("%c", k);
19389 else
19390 printf (".");
19391 }
19392
19393 putchar ('\n');
19394
19395 p += lbytes;
19396 bytes -= lbytes;
19397 addr += lbytes;
19398 }
19399
19400 putchar ('\n');
19401}
19402
19403static unsigned char *
19404display_msp430_attribute (unsigned char * p,
19405 const unsigned char * const end)
19406{
19407 uint64_t val;
19408 uint64_t tag;
19409
19410 READ_ULEB (tag, p, end);
19411
19412 switch (tag)
19413 {
19414 case OFBA_MSPABI_Tag_ISA:
19415 printf (" Tag_ISA: ");
19416 READ_ULEB (val, p, end);
19417 switch (val)
19418 {
19419 case 0: printf (_("None\n")); break;
19420 case 1: printf (_("MSP430\n")); break;
19421 case 2: printf (_("MSP430X\n")); break;
19422 default: printf ("??? (%" PRId64 ")\n", val); break;
19423 }
19424 break;
19425
19426 case OFBA_MSPABI_Tag_Code_Model:
19427 printf (" Tag_Code_Model: ");
19428 READ_ULEB (val, p, end);
19429 switch (val)
19430 {
19431 case 0: printf (_("None\n")); break;
19432 case 1: printf (_("Small\n")); break;
19433 case 2: printf (_("Large\n")); break;
19434 default: printf ("??? (%" PRId64 ")\n", val); break;
19435 }
19436 break;
19437
19438 case OFBA_MSPABI_Tag_Data_Model:
19439 printf (" Tag_Data_Model: ");
19440 READ_ULEB (val, p, end);
19441 switch (val)
19442 {
19443 case 0: printf (_("None\n")); break;
19444 case 1: printf (_("Small\n")); break;
19445 case 2: printf (_("Large\n")); break;
19446 case 3: printf (_("Restricted Large\n")); break;
19447 default: printf ("??? (%" PRId64 ")\n", val); break;
19448 }
19449 break;
19450
19451 default:
19452 printf (_(" <unknown tag %" PRId64 ">: "), tag);
19453
19454 if (tag & 1)
19455 {
19456 putchar ('"');
19457 if (p < end - 1)
19458 {
19459 size_t maxlen = (end - p) - 1;
19460
19461 print_symbol_name ((int) maxlen, (const char *) p);
19462 p += strnlen ((char *) p, maxlen) + 1;
19463 }
19464 else
19465 {
19466 printf (_("<corrupt>"));
19467 p = (unsigned char *) end;
19468 }
19469 printf ("\"\n");
19470 }
19471 else
19472 {
19473 READ_ULEB (val, p, end);
19474 printf ("%" PRId64 " (0x%" PRIx64 ")\n", val, val);
19475 }
19476 break;
19477 }
19478
19479 assert (p <= end);
19480 return p;
19481}
19482
19483static unsigned char *
19484display_msp430_gnu_attribute (unsigned char * p,
19485 unsigned int tag,
19486 const unsigned char * const end)
19487{
19488 if (tag == Tag_GNU_MSP430_Data_Region)
19489 {
19490 uint64_t val;
19491
19492 printf (" Tag_GNU_MSP430_Data_Region: ");
19493 READ_ULEB (val, p, end);
19494
19495 switch (val)
19496 {
19497 case Val_GNU_MSP430_Data_Region_Any:
19498 printf (_("Any Region\n"));
19499 break;
19500 case Val_GNU_MSP430_Data_Region_Lower:
19501 printf (_("Lower Region Only\n"));
19502 break;
19503 default:
19504 printf ("??? (%" PRIu64 ")\n", val);
19505 }
19506 return p;
19507 }
19508 return display_tag_value (tag & 1, p, end);
19509}
19510
19511struct riscv_attr_tag_t {
19512 const char *name;
19513 unsigned int tag;
19514};
19515
19516static struct riscv_attr_tag_t riscv_attr_tag[] =
19517{
19518#define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
19519 T(arch),
19520 T(priv_spec),
19521 T(priv_spec_minor),
19522 T(priv_spec_revision),
19523 T(unaligned_access),
19524 T(stack_align),
19525#undef T
19526};
19527
19528static unsigned char *
19529display_riscv_attribute (unsigned char *p,
19530 const unsigned char * const end)
19531{
19532 uint64_t val;
19533 uint64_t tag;
19534 struct riscv_attr_tag_t *attr = NULL;
19535 unsigned i;
19536
19537 READ_ULEB (tag, p, end);
19538
19539 /* Find the name of attribute. */
19540 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
19541 {
19542 if (riscv_attr_tag[i].tag == tag)
19543 {
19544 attr = &riscv_attr_tag[i];
19545 break;
19546 }
19547 }
19548
19549 if (attr)
19550 printf (" %s: ", attr->name);
19551 else
19552 return display_tag_value (tag, p, end);
19553
19554 switch (tag)
19555 {
19556 case Tag_RISCV_priv_spec:
19557 case Tag_RISCV_priv_spec_minor:
19558 case Tag_RISCV_priv_spec_revision:
19559 READ_ULEB (val, p, end);
19560 printf ("%" PRIu64 "\n", val);
19561 break;
19562 case Tag_RISCV_unaligned_access:
19563 READ_ULEB (val, p, end);
19564 switch (val)
19565 {
19566 case 0:
19567 printf (_("No unaligned access\n"));
19568 break;
19569 case 1:
19570 printf (_("Unaligned access\n"));
19571 break;
19572 }
19573 break;
19574 case Tag_RISCV_stack_align:
19575 READ_ULEB (val, p, end);
19576 printf (_("%" PRIu64 "-bytes\n"), val);
19577 break;
19578 case Tag_RISCV_arch:
19579 p = display_tag_value (-1, p, end);
19580 break;
19581 default:
19582 return display_tag_value (tag, p, end);
19583 }
19584
19585 return p;
19586}
19587
19588static unsigned char *
19589display_csky_attribute (unsigned char * p,
19590 const unsigned char * const end)
19591{
19592 uint64_t tag;
19593 uint64_t val;
19594 READ_ULEB (tag, p, end);
19595
19596 if (tag >= Tag_CSKY_MAX)
19597 {
19598 return display_tag_value (-1, p, end);
19599 }
19600
19601 switch (tag)
19602 {
19603 case Tag_CSKY_ARCH_NAME:
19604 printf (" Tag_CSKY_ARCH_NAME:\t\t");
19605 return display_tag_value (-1, p, end);
19606 case Tag_CSKY_CPU_NAME:
19607 printf (" Tag_CSKY_CPU_NAME:\t\t");
19608 return display_tag_value (-1, p, end);
19609
19610 case Tag_CSKY_ISA_FLAGS:
19611 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
19612 return display_tag_value (0, p, end);
19613 case Tag_CSKY_ISA_EXT_FLAGS:
19614 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
19615 return display_tag_value (0, p, end);
19616
19617 case Tag_CSKY_DSP_VERSION:
19618 printf (" Tag_CSKY_DSP_VERSION:\t\t");
19619 READ_ULEB (val, p, end);
19620 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
19621 printf ("DSP Extension\n");
19622 else if (val == VAL_CSKY_DSP_VERSION_2)
19623 printf ("DSP 2.0\n");
19624 break;
19625
19626 case Tag_CSKY_VDSP_VERSION:
19627 printf (" Tag_CSKY_VDSP_VERSION:\t");
19628 READ_ULEB (val, p, end);
19629 printf ("VDSP Version %" PRId64 "\n", val);
19630 break;
19631
19632 case Tag_CSKY_FPU_VERSION:
19633 printf (" Tag_CSKY_FPU_VERSION:\t\t");
19634 READ_ULEB (val, p, end);
19635 if (val == VAL_CSKY_FPU_VERSION_1)
19636 printf ("ABIV1 FPU Version 1\n");
19637 else if (val == VAL_CSKY_FPU_VERSION_2)
19638 printf ("FPU Version 2\n");
19639 break;
19640
19641 case Tag_CSKY_FPU_ABI:
19642 printf (" Tag_CSKY_FPU_ABI:\t\t");
19643 READ_ULEB (val, p, end);
19644 if (val == VAL_CSKY_FPU_ABI_HARD)
19645 printf ("Hard\n");
19646 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
19647 printf ("SoftFP\n");
19648 else if (val == VAL_CSKY_FPU_ABI_SOFT)
19649 printf ("Soft\n");
19650 break;
19651 case Tag_CSKY_FPU_ROUNDING:
19652 READ_ULEB (val, p, end);
19653 if (val == 1)
19654 {
19655 printf (" Tag_CSKY_FPU_ROUNDING:\t");
19656 printf ("Needed\n");
19657 }
19658 break;
19659 case Tag_CSKY_FPU_DENORMAL:
19660 READ_ULEB (val, p, end);
19661 if (val == 1)
19662 {
19663 printf (" Tag_CSKY_FPU_DENORMAL:\t");
19664 printf ("Needed\n");
19665 }
19666 break;
19667 case Tag_CSKY_FPU_Exception:
19668 READ_ULEB (val, p, end);
19669 if (val == 1)
19670 {
19671 printf (" Tag_CSKY_FPU_Exception:\t");
19672 printf ("Needed\n");
19673 }
19674 break;
19675 case Tag_CSKY_FPU_NUMBER_MODULE:
19676 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
19677 return display_tag_value (-1, p, end);
19678 case Tag_CSKY_FPU_HARDFP:
19679 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
19680 READ_ULEB (val, p, end);
19681 if (val & VAL_CSKY_FPU_HARDFP_HALF)
19682 printf (" Half");
19683 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
19684 printf (" Single");
19685 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
19686 printf (" Double");
19687 printf ("\n");
19688 break;
19689 default:
19690 return display_tag_value (tag, p, end);
19691 }
19692 return p;
19693}
19694
19695static bool
19696process_attributes (Filedata * filedata,
19697 const char * public_name,
19698 unsigned int proc_type,
19699 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
19700 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
19701{
19702 /* Find the section header so that we get the size. */
19703 Elf_Internal_Shdr * sect = find_section_by_type (filedata, proc_type);
19704 if (sect == NULL)
19705 sect = find_section_by_type (filedata, SHT_GNU_ATTRIBUTES);
19706
19707 if (sect == NULL)
19708 /* No section, exit without error. */
19709 return true;
19710
19711 unsigned char * contents = (unsigned char *)
19712 get_data (NULL, filedata, sect->sh_offset, 1, sect->sh_size, _("attributes"));
19713 if (contents == NULL)
19714 return false;
19715
19716 bool res = true;
19717 unsigned char * p = contents;
19718 /* The first character is the version of the attributes.
19719 Currently only version 1, (aka 'A') is recognised here. */
19720 if (*p != 'A')
19721 {
19722 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
19723 res = false;
19724 goto free_data;
19725 }
19726
19727 uint64_t section_len = sect->sh_size - 1;
19728 p++;
19729
19730 while (section_len > 0)
19731 {
19732 uint64_t attr_len;
19733 unsigned int namelen;
19734 bool public_section;
19735 bool gnu_section;
19736
19737 if (section_len <= 4)
19738 {
19739 error (_("Tag section ends prematurely\n"));
19740 res = false;
19741 break;
19742 }
19743 attr_len = byte_get (p, 4);
19744 p += 4;
19745
19746 if (attr_len > section_len)
19747 {
19748 error (_("Bad attribute length (%u > %u)\n"),
19749 (unsigned) attr_len, (unsigned) section_len);
19750 attr_len = section_len;
19751 res = false;
19752 }
19753 /* PR 17531: file: 001-101425-0.004 */
19754 else if (attr_len < 5)
19755 {
19756 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
19757 res = false;
19758 break;
19759 }
19760
19761 section_len -= attr_len;
19762 attr_len -= 4;
19763
19764 namelen = strnlen ((char *) p, attr_len) + 1;
19765 if (namelen == 0 || namelen >= attr_len)
19766 {
19767 error (_("Corrupt attribute section name\n"));
19768 res = false;
19769 break;
19770 }
19771
19772 printf (_("Attribute Section: "));
19773 print_symbol_name (INT_MAX, (const char *) p);
19774 putchar ('\n');
19775
19776 if (public_name && streq ((char *) p, public_name))
19777 public_section = true;
19778 else
19779 public_section = false;
19780
19781 if (streq ((char *) p, "gnu"))
19782 gnu_section = true;
19783 else
19784 gnu_section = false;
19785
19786 p += namelen;
19787 attr_len -= namelen;
19788
19789 while (attr_len > 0 && p < contents + sect->sh_size)
19790 {
19791 int tag;
19792 unsigned int val;
19793 uint64_t size;
19794 unsigned char * end;
19795
19796 /* PR binutils/17531: Safe handling of corrupt files. */
19797 if (attr_len < 6)
19798 {
19799 error (_("Unused bytes at end of section\n"));
19800 res = false;
19801 section_len = 0;
19802 break;
19803 }
19804
19805 tag = *(p++);
19806 size = byte_get (p, 4);
19807 if (size > attr_len)
19808 {
19809 error (_("Bad subsection length (%u > %u)\n"),
19810 (unsigned) size, (unsigned) attr_len);
19811 res = false;
19812 size = attr_len;
19813 }
19814 /* PR binutils/17531: Safe handling of corrupt files. */
19815 if (size < 6)
19816 {
19817 error (_("Bad subsection length (%u < 6)\n"),
19818 (unsigned) size);
19819 res = false;
19820 section_len = 0;
19821 break;
19822 }
19823
19824 attr_len -= size;
19825 end = p + size - 1;
19826 assert (end <= contents + sect->sh_size);
19827 p += 4;
19828
19829 switch (tag)
19830 {
19831 case 1:
19832 printf (_("File Attributes\n"));
19833 break;
19834 case 2:
19835 printf (_("Section Attributes:"));
19836 goto do_numlist;
19837 case 3:
19838 printf (_("Symbol Attributes:"));
19839 /* Fall through. */
19840 do_numlist:
19841 for (;;)
19842 {
19843 READ_ULEB (val, p, end);
19844 if (val == 0)
19845 break;
19846 printf (" %d", val);
19847 }
19848 printf ("\n");
19849 break;
19850 default:
19851 printf (_("Unknown tag: %d\n"), tag);
19852 public_section = false;
19853 break;
19854 }
19855
19856 if (public_section && display_pub_attribute != NULL)
19857 {
19858 while (p < end)
19859 p = display_pub_attribute (p, end);
19860 assert (p == end);
19861 }
19862 else if (gnu_section && display_proc_gnu_attribute != NULL)
19863 {
19864 while (p < end)
19865 p = display_gnu_attribute (p,
19866 display_proc_gnu_attribute,
19867 end);
19868 assert (p == end);
19869 }
19870 else if (p < end)
19871 {
19872 printf (_(" Unknown attribute:\n"));
19873 display_raw_attribute (p, end);
19874 p = end;
19875 }
19876 else
19877 attr_len = 0;
19878 }
19879 }
19880
19881free_data:
19882 free (contents);
19883
19884 return res;
19885}
19886
19887/* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
19888 Print the Address, Access and Initial fields of an entry at VMA ADDR
19889 and return the VMA of the next entry, or -1 if there was a problem.
19890 Does not read from DATA_END or beyond. */
19891
19892static uint64_t
19893print_mips_got_entry (unsigned char * data, uint64_t pltgot, uint64_t addr,
19894 unsigned char * data_end)
19895{
19896 printf (" ");
19897 print_vma (addr, LONG_HEX);
19898 printf (" ");
19899 if (addr < pltgot + 0xfff0)
19900 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
19901 else
19902 printf ("%10s", "");
19903 printf (" ");
19904 if (data == NULL)
19905 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
19906 else
19907 {
19908 uint64_t entry;
19909 unsigned char * from = data + addr - pltgot;
19910
19911 if (from + (is_32bit_elf ? 4 : 8) > data_end)
19912 {
19913 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
19914 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
19915 return (uint64_t) -1;
19916 }
19917 else
19918 {
19919 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
19920 print_vma (entry, LONG_HEX);
19921 }
19922 }
19923 return addr + (is_32bit_elf ? 4 : 8);
19924}
19925
19926/* DATA points to the contents of a MIPS PLT GOT that starts at VMA
19927 PLTGOT. Print the Address and Initial fields of an entry at VMA
19928 ADDR and return the VMA of the next entry. */
19929
19930static uint64_t
19931print_mips_pltgot_entry (unsigned char * data, uint64_t pltgot, uint64_t addr)
19932{
19933 printf (" ");
19934 print_vma (addr, LONG_HEX);
19935 printf (" ");
19936 if (data == NULL)
19937 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
19938 else
19939 {
19940 uint64_t entry;
19941
19942 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
19943 print_vma (entry, LONG_HEX);
19944 }
19945 return addr + (is_32bit_elf ? 4 : 8);
19946}
19947
19948static void
19949print_mips_ases (unsigned int mask)
19950{
19951 if (mask & AFL_ASE_DSP)
19952 fputs ("\n\tDSP ASE", stdout);
19953 if (mask & AFL_ASE_DSPR2)
19954 fputs ("\n\tDSP R2 ASE", stdout);
19955 if (mask & AFL_ASE_DSPR3)
19956 fputs ("\n\tDSP R3 ASE", stdout);
19957 if (mask & AFL_ASE_EVA)
19958 fputs ("\n\tEnhanced VA Scheme", stdout);
19959 if (mask & AFL_ASE_MCU)
19960 fputs ("\n\tMCU (MicroController) ASE", stdout);
19961 if (mask & AFL_ASE_MDMX)
19962 fputs ("\n\tMDMX ASE", stdout);
19963 if (mask & AFL_ASE_MIPS3D)
19964 fputs ("\n\tMIPS-3D ASE", stdout);
19965 if (mask & AFL_ASE_MT)
19966 fputs ("\n\tMT ASE", stdout);
19967 if (mask & AFL_ASE_SMARTMIPS)
19968 fputs ("\n\tSmartMIPS ASE", stdout);
19969 if (mask & AFL_ASE_VIRT)
19970 fputs ("\n\tVZ ASE", stdout);
19971 if (mask & AFL_ASE_MSA)
19972 fputs ("\n\tMSA ASE", stdout);
19973 if (mask & AFL_ASE_MIPS16)
19974 fputs ("\n\tMIPS16 ASE", stdout);
19975 if (mask & AFL_ASE_MICROMIPS)
19976 fputs ("\n\tMICROMIPS ASE", stdout);
19977 if (mask & AFL_ASE_XPA)
19978 fputs ("\n\tXPA ASE", stdout);
19979 if (mask & AFL_ASE_MIPS16E2)
19980 fputs ("\n\tMIPS16e2 ASE", stdout);
19981 if (mask & AFL_ASE_CRC)
19982 fputs ("\n\tCRC ASE", stdout);
19983 if (mask & AFL_ASE_GINV)
19984 fputs ("\n\tGINV ASE", stdout);
19985 if (mask & AFL_ASE_LOONGSON_MMI)
19986 fputs ("\n\tLoongson MMI ASE", stdout);
19987 if (mask & AFL_ASE_LOONGSON_CAM)
19988 fputs ("\n\tLoongson CAM ASE", stdout);
19989 if (mask & AFL_ASE_LOONGSON_EXT)
19990 fputs ("\n\tLoongson EXT ASE", stdout);
19991 if (mask & AFL_ASE_LOONGSON_EXT2)
19992 fputs ("\n\tLoongson EXT2 ASE", stdout);
19993 if (mask == 0)
19994 fprintf (stdout, "\n\t%s", _("None"));
19995 else if ((mask & ~AFL_ASE_MASK) != 0)
19996 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
19997}
19998
19999static void
20000print_mips_isa_ext (unsigned int isa_ext)
20001{
20002 switch (isa_ext)
20003 {
20004 case 0:
20005 fputs (_("None"), stdout);
20006 break;
20007 case AFL_EXT_XLR:
20008 fputs ("RMI XLR", stdout);
20009 break;
20010 case AFL_EXT_OCTEON3:
20011 fputs ("Cavium Networks Octeon3", stdout);
20012 break;
20013 case AFL_EXT_OCTEON2:
20014 fputs ("Cavium Networks Octeon2", stdout);
20015 break;
20016 case AFL_EXT_OCTEONP:
20017 fputs ("Cavium Networks OcteonP", stdout);
20018 break;
20019 case AFL_EXT_OCTEON:
20020 fputs ("Cavium Networks Octeon", stdout);
20021 break;
20022 case AFL_EXT_5900:
20023 fputs ("Toshiba R5900", stdout);
20024 break;
20025 case AFL_EXT_4650:
20026 fputs ("MIPS R4650", stdout);
20027 break;
20028 case AFL_EXT_4010:
20029 fputs ("LSI R4010", stdout);
20030 break;
20031 case AFL_EXT_4100:
20032 fputs ("NEC VR4100", stdout);
20033 break;
20034 case AFL_EXT_3900:
20035 fputs ("Toshiba R3900", stdout);
20036 break;
20037 case AFL_EXT_10000:
20038 fputs ("MIPS R10000", stdout);
20039 break;
20040 case AFL_EXT_SB1:
20041 fputs ("Broadcom SB-1", stdout);
20042 break;
20043 case AFL_EXT_4111:
20044 fputs ("NEC VR4111/VR4181", stdout);
20045 break;
20046 case AFL_EXT_4120:
20047 fputs ("NEC VR4120", stdout);
20048 break;
20049 case AFL_EXT_5400:
20050 fputs ("NEC VR5400", stdout);
20051 break;
20052 case AFL_EXT_5500:
20053 fputs ("NEC VR5500", stdout);
20054 break;
20055 case AFL_EXT_LOONGSON_2E:
20056 fputs ("ST Microelectronics Loongson 2E", stdout);
20057 break;
20058 case AFL_EXT_LOONGSON_2F:
20059 fputs ("ST Microelectronics Loongson 2F", stdout);
20060 break;
20061 case AFL_EXT_INTERAPTIV_MR2:
20062 fputs ("Imagination interAptiv MR2", stdout);
20063 break;
20064 default:
20065 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
20066 }
20067}
20068
20069static signed int
20070get_mips_reg_size (int reg_size)
20071{
20072 return (reg_size == AFL_REG_NONE) ? 0
20073 : (reg_size == AFL_REG_32) ? 32
20074 : (reg_size == AFL_REG_64) ? 64
20075 : (reg_size == AFL_REG_128) ? 128
20076 : -1;
20077}
20078
20079/* If DUMP_GOT is true, display only the GOT related contents.
20080 Otherwise, display all MIPS specific information. */
20081
20082static bool
20083process_mips_specific (Filedata * filedata, bool dump_got)
20084{
20085 Elf_Internal_Dyn * entry;
20086 Elf_Internal_Shdr *sect = NULL;
20087 size_t liblist_offset = 0;
20088 size_t liblistno = 0;
20089 size_t conflictsno = 0;
20090 size_t options_offset = 0;
20091 size_t conflicts_offset = 0;
20092 size_t pltrelsz = 0;
20093 size_t pltrel = 0;
20094 uint64_t pltgot = 0;
20095 uint64_t mips_pltgot = 0;
20096 uint64_t jmprel = 0;
20097 uint64_t local_gotno = 0;
20098 uint64_t gotsym = 0;
20099 uint64_t symtabno = 0;
20100 bool res = true;
20101
20102 if (!dump_got)
20103 {
20104 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
20105 display_mips_gnu_attribute))
20106 res = false;
20107
20108 sect = find_section (filedata, ".MIPS.abiflags");
20109
20110 if (sect != NULL)
20111 {
20112 Elf_External_ABIFlags_v0 *abiflags_ext;
20113 Elf_Internal_ABIFlags_v0 abiflags_in;
20114
20115 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
20116 {
20117 error (_("Corrupt MIPS ABI Flags section.\n"));
20118 res = false;
20119 }
20120 else
20121 {
20122 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
20123 sect->sh_size, _("MIPS ABI Flags section"));
20124 if (abiflags_ext)
20125 {
20126 abiflags_in.version = BYTE_GET (abiflags_ext->version);
20127 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
20128 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
20129 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
20130 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
20131 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
20132 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
20133 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
20134 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
20135 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
20136 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
20137
20138 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
20139 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
20140 if (abiflags_in.isa_rev > 1)
20141 printf ("r%d", abiflags_in.isa_rev);
20142 printf ("\nGPR size: %d",
20143 get_mips_reg_size (abiflags_in.gpr_size));
20144 printf ("\nCPR1 size: %d",
20145 get_mips_reg_size (abiflags_in.cpr1_size));
20146 printf ("\nCPR2 size: %d",
20147 get_mips_reg_size (abiflags_in.cpr2_size));
20148 fputs ("\nFP ABI: ", stdout);
20149 print_mips_fp_abi_value (abiflags_in.fp_abi);
20150 fputs ("ISA Extension: ", stdout);
20151 print_mips_isa_ext (abiflags_in.isa_ext);
20152 fputs ("\nASEs:", stdout);
20153 print_mips_ases (abiflags_in.ases);
20154 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
20155 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
20156 fputc ('\n', stdout);
20157 free (abiflags_ext);
20158 }
20159 }
20160 }
20161 }
20162
20163 /* We have a lot of special sections. Thanks SGI! */
20164 if (filedata->dynamic_section == NULL)
20165 {
20166 /* No dynamic information available. See if there is static GOT. */
20167 sect = find_section (filedata, ".got");
20168 if (sect != NULL)
20169 {
20170 unsigned char *data_end;
20171 unsigned char *data;
20172 uint64_t ent, end;
20173 int addr_size;
20174
20175 pltgot = sect->sh_addr;
20176
20177 ent = pltgot;
20178 addr_size = (is_32bit_elf ? 4 : 8);
20179 end = pltgot + sect->sh_size;
20180
20181 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
20182 end - pltgot, 1,
20183 _("Global Offset Table data"));
20184 /* PR 12855: Null data is handled gracefully throughout. */
20185 data_end = data + (end - pltgot);
20186
20187 printf (_("\nStatic GOT:\n"));
20188 printf (_(" Canonical gp value: "));
20189 print_vma (ent + 0x7ff0, LONG_HEX);
20190 printf ("\n\n");
20191
20192 /* In a dynamic binary GOT[0] is reserved for the dynamic
20193 loader to store the lazy resolver pointer, however in
20194 a static binary it may well have been omitted and GOT
20195 reduced to a table of addresses.
20196 PR 21344: Check for the entry being fully available
20197 before fetching it. */
20198 if (data
20199 && data + ent - pltgot + addr_size <= data_end
20200 && byte_get (data + ent - pltgot, addr_size) == 0)
20201 {
20202 printf (_(" Reserved entries:\n"));
20203 printf (_(" %*s %10s %*s\n"),
20204 addr_size * 2, _("Address"), _("Access"),
20205 addr_size * 2, _("Value"));
20206 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20207 printf ("\n");
20208 if (ent == (uint64_t) -1)
20209 goto sgot_print_fail;
20210
20211 /* Check for the MSB of GOT[1] being set, identifying a
20212 GNU object. This entry will be used by some runtime
20213 loaders, to store the module pointer. Otherwise this
20214 is an ordinary local entry.
20215 PR 21344: Check for the entry being fully available
20216 before fetching it. */
20217 if (data
20218 && data + ent - pltgot + addr_size <= data_end
20219 && (byte_get (data + ent - pltgot, addr_size)
20220 >> (addr_size * 8 - 1)) != 0)
20221 {
20222 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20223 printf ("\n");
20224 if (ent == (uint64_t) -1)
20225 goto sgot_print_fail;
20226 }
20227 printf ("\n");
20228 }
20229
20230 if (data != NULL && ent < end)
20231 {
20232 printf (_(" Local entries:\n"));
20233 printf (" %*s %10s %*s\n",
20234 addr_size * 2, _("Address"), _("Access"),
20235 addr_size * 2, _("Value"));
20236 while (ent < end)
20237 {
20238 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20239 printf ("\n");
20240 if (ent == (uint64_t) -1)
20241 goto sgot_print_fail;
20242 }
20243 printf ("\n");
20244 }
20245
20246 sgot_print_fail:
20247 free (data);
20248 }
20249 return res;
20250 }
20251
20252 for (entry = filedata->dynamic_section;
20253 /* PR 17531 file: 012-50589-0.004. */
20254 (entry < filedata->dynamic_section + filedata->dynamic_nent
20255 && entry->d_tag != DT_NULL);
20256 ++entry)
20257 switch (entry->d_tag)
20258 {
20259 case DT_MIPS_LIBLIST:
20260 liblist_offset
20261 = offset_from_vma (filedata, entry->d_un.d_val,
20262 liblistno * sizeof (Elf32_External_Lib));
20263 break;
20264 case DT_MIPS_LIBLISTNO:
20265 liblistno = entry->d_un.d_val;
20266 break;
20267 case DT_MIPS_OPTIONS:
20268 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
20269 break;
20270 case DT_MIPS_CONFLICT:
20271 conflicts_offset
20272 = offset_from_vma (filedata, entry->d_un.d_val,
20273 conflictsno * sizeof (Elf32_External_Conflict));
20274 break;
20275 case DT_MIPS_CONFLICTNO:
20276 conflictsno = entry->d_un.d_val;
20277 break;
20278 case DT_PLTGOT:
20279 pltgot = entry->d_un.d_ptr;
20280 break;
20281 case DT_MIPS_LOCAL_GOTNO:
20282 local_gotno = entry->d_un.d_val;
20283 break;
20284 case DT_MIPS_GOTSYM:
20285 gotsym = entry->d_un.d_val;
20286 break;
20287 case DT_MIPS_SYMTABNO:
20288 symtabno = entry->d_un.d_val;
20289 break;
20290 case DT_MIPS_PLTGOT:
20291 mips_pltgot = entry->d_un.d_ptr;
20292 break;
20293 case DT_PLTREL:
20294 pltrel = entry->d_un.d_val;
20295 break;
20296 case DT_PLTRELSZ:
20297 pltrelsz = entry->d_un.d_val;
20298 break;
20299 case DT_JMPREL:
20300 jmprel = entry->d_un.d_ptr;
20301 break;
20302 default:
20303 break;
20304 }
20305
20306 if (!dump_got && liblist_offset != 0 && liblistno != 0 && do_dynamic)
20307 {
20308 Elf32_External_Lib * elib;
20309 size_t cnt;
20310
20311 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
20312 sizeof (Elf32_External_Lib),
20313 liblistno,
20314 _("liblist section data"));
20315 if (elib)
20316 {
20317 printf (ngettext ("\nSection '.liblist' contains %zu entry:\n",
20318 "\nSection '.liblist' contains %zu entries:\n",
20319 liblistno),
20320 liblistno);
20321 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
20322 stdout);
20323
20324 for (cnt = 0; cnt < liblistno; ++cnt)
20325 {
20326 Elf32_Lib liblist;
20327 time_t atime;
20328 char timebuf[128];
20329 struct tm * tmp;
20330
20331 liblist.l_name = BYTE_GET (elib[cnt].l_name);
20332 atime = BYTE_GET (elib[cnt].l_time_stamp);
20333 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
20334 liblist.l_version = BYTE_GET (elib[cnt].l_version);
20335 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
20336
20337 tmp = gmtime (&atime);
20338 snprintf (timebuf, sizeof (timebuf),
20339 "%04u-%02u-%02uT%02u:%02u:%02u",
20340 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
20341 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
20342
20343 printf ("%3zu: ", cnt);
20344 if (valid_dynamic_name (filedata, liblist.l_name))
20345 print_symbol_name (20, get_dynamic_name (filedata, liblist.l_name));
20346 else
20347 printf (_("<corrupt: %9ld>"), liblist.l_name);
20348 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
20349 liblist.l_version);
20350
20351 if (liblist.l_flags == 0)
20352 puts (_(" NONE"));
20353 else
20354 {
20355 static const struct
20356 {
20357 const char * name;
20358 int bit;
20359 }
20360 l_flags_vals[] =
20361 {
20362 { " EXACT_MATCH", LL_EXACT_MATCH },
20363 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
20364 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
20365 { " EXPORTS", LL_EXPORTS },
20366 { " DELAY_LOAD", LL_DELAY_LOAD },
20367 { " DELTA", LL_DELTA }
20368 };
20369 int flags = liblist.l_flags;
20370 size_t fcnt;
20371
20372 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
20373 if ((flags & l_flags_vals[fcnt].bit) != 0)
20374 {
20375 fputs (l_flags_vals[fcnt].name, stdout);
20376 flags ^= l_flags_vals[fcnt].bit;
20377 }
20378 if (flags != 0)
20379 printf (" %#x", (unsigned int) flags);
20380
20381 puts ("");
20382 }
20383 }
20384
20385 free (elib);
20386 }
20387 else
20388 res = false;
20389 }
20390
20391 if (!dump_got && options_offset != 0)
20392 {
20393 Elf_External_Options * eopt;
20394 size_t offset;
20395 int cnt;
20396
20397 /* Find the section header so that we get the size. */
20398 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
20399 /* PR 17533 file: 012-277276-0.004. */
20400 if (sect == NULL)
20401 {
20402 error (_("No MIPS_OPTIONS header found\n"));
20403 return false;
20404 }
20405 /* PR 24243 */
20406 if (sect->sh_size < sizeof (* eopt))
20407 {
20408 error (_("The MIPS options section is too small.\n"));
20409 return false;
20410 }
20411
20412 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
20413 sect->sh_size, _("options"));
20414 if (eopt)
20415 {
20416 Elf_Internal_Options option;
20417
20418 offset = cnt = 0;
20419 while (offset <= sect->sh_size - sizeof (* eopt))
20420 {
20421 Elf_External_Options * eoption;
20422 unsigned int optsize;
20423
20424 eoption = (Elf_External_Options *) ((char *) eopt + offset);
20425
20426 optsize = BYTE_GET (eoption->size);
20427
20428 /* PR 17531: file: ffa0fa3b. */
20429 if (optsize < sizeof (* eopt)
20430 || optsize > sect->sh_size - offset)
20431 {
20432 error (_("Invalid size (%u) for MIPS option\n"),
20433 optsize);
20434 free (eopt);
20435 return false;
20436 }
20437 offset += optsize;
20438 ++cnt;
20439 }
20440
20441 printf (ngettext ("\nSection '%s' contains %d entry:\n",
20442 "\nSection '%s' contains %d entries:\n",
20443 cnt),
20444 printable_section_name (filedata, sect), cnt);
20445
20446 offset = 0;
20447 while (cnt-- > 0)
20448 {
20449 size_t len;
20450 Elf_External_Options * eoption;
20451
20452 eoption = (Elf_External_Options *) ((char *) eopt + offset);
20453
20454 option.kind = BYTE_GET (eoption->kind);
20455 option.size = BYTE_GET (eoption->size);
20456 option.section = BYTE_GET (eoption->section);
20457 option.info = BYTE_GET (eoption->info);
20458
20459 switch (option.kind)
20460 {
20461 case ODK_NULL:
20462 /* This shouldn't happen. */
20463 printf (" NULL %" PRId16 " %" PRIx32,
20464 option.section, option.info);
20465 break;
20466
20467 case ODK_REGINFO:
20468 printf (" REGINFO ");
20469 if (filedata->file_header.e_machine == EM_MIPS)
20470 {
20471 Elf32_External_RegInfo * ereg;
20472 Elf32_RegInfo reginfo;
20473
20474 /* 32bit form. */
20475 if (option.size < (sizeof (Elf_External_Options)
20476 + sizeof (Elf32_External_RegInfo)))
20477 {
20478 printf (_("<corrupt>\n"));
20479 error (_("Truncated MIPS REGINFO option\n"));
20480 cnt = 0;
20481 break;
20482 }
20483
20484 ereg = (Elf32_External_RegInfo *) (eoption + 1);
20485
20486 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
20487 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
20488 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
20489 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
20490 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
20491 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
20492
20493 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
20494 reginfo.ri_gprmask, reginfo.ri_gp_value);
20495 printf (" "
20496 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
20497 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
20498 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
20499 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
20500 }
20501 else
20502 {
20503 /* 64 bit form. */
20504 Elf64_External_RegInfo * ereg;
20505 Elf64_Internal_RegInfo reginfo;
20506
20507 if (option.size < (sizeof (Elf_External_Options)
20508 + sizeof (Elf64_External_RegInfo)))
20509 {
20510 printf (_("<corrupt>\n"));
20511 error (_("Truncated MIPS REGINFO option\n"));
20512 cnt = 0;
20513 break;
20514 }
20515
20516 ereg = (Elf64_External_RegInfo *) (eoption + 1);
20517 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
20518 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
20519 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
20520 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
20521 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
20522 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
20523
20524 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
20525 reginfo.ri_gprmask, reginfo.ri_gp_value);
20526 printf (" "
20527 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
20528 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
20529 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
20530 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
20531 }
20532 offset += option.size;
20533 continue;
20534
20535 case ODK_EXCEPTIONS:
20536 fputs (" EXCEPTIONS fpe_min(", stdout);
20537 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
20538 fputs (") fpe_max(", stdout);
20539 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
20540 fputs (")", stdout);
20541
20542 if (option.info & OEX_PAGE0)
20543 fputs (" PAGE0", stdout);
20544 if (option.info & OEX_SMM)
20545 fputs (" SMM", stdout);
20546 if (option.info & OEX_FPDBUG)
20547 fputs (" FPDBUG", stdout);
20548 if (option.info & OEX_DISMISS)
20549 fputs (" DISMISS", stdout);
20550 break;
20551
20552 case ODK_PAD:
20553 fputs (" PAD ", stdout);
20554 if (option.info & OPAD_PREFIX)
20555 fputs (" PREFIX", stdout);
20556 if (option.info & OPAD_POSTFIX)
20557 fputs (" POSTFIX", stdout);
20558 if (option.info & OPAD_SYMBOL)
20559 fputs (" SYMBOL", stdout);
20560 break;
20561
20562 case ODK_HWPATCH:
20563 fputs (" HWPATCH ", stdout);
20564 if (option.info & OHW_R4KEOP)
20565 fputs (" R4KEOP", stdout);
20566 if (option.info & OHW_R8KPFETCH)
20567 fputs (" R8KPFETCH", stdout);
20568 if (option.info & OHW_R5KEOP)
20569 fputs (" R5KEOP", stdout);
20570 if (option.info & OHW_R5KCVTL)
20571 fputs (" R5KCVTL", stdout);
20572 break;
20573
20574 case ODK_FILL:
20575 fputs (" FILL ", stdout);
20576 /* XXX Print content of info word? */
20577 break;
20578
20579 case ODK_TAGS:
20580 fputs (" TAGS ", stdout);
20581 /* XXX Print content of info word? */
20582 break;
20583
20584 case ODK_HWAND:
20585 fputs (" HWAND ", stdout);
20586 if (option.info & OHWA0_R4KEOP_CHECKED)
20587 fputs (" R4KEOP_CHECKED", stdout);
20588 if (option.info & OHWA0_R4KEOP_CLEAN)
20589 fputs (" R4KEOP_CLEAN", stdout);
20590 break;
20591
20592 case ODK_HWOR:
20593 fputs (" HWOR ", stdout);
20594 if (option.info & OHWA0_R4KEOP_CHECKED)
20595 fputs (" R4KEOP_CHECKED", stdout);
20596 if (option.info & OHWA0_R4KEOP_CLEAN)
20597 fputs (" R4KEOP_CLEAN", stdout);
20598 break;
20599
20600 case ODK_GP_GROUP:
20601 printf (" GP_GROUP %#06x self-contained %#06x",
20602 option.info & OGP_GROUP,
20603 (option.info & OGP_SELF) >> 16);
20604 break;
20605
20606 case ODK_IDENT:
20607 printf (" IDENT %#06x self-contained %#06x",
20608 option.info & OGP_GROUP,
20609 (option.info & OGP_SELF) >> 16);
20610 break;
20611
20612 default:
20613 /* This shouldn't happen. */
20614 printf (" %3d ??? %" PRId16 " %" PRIx32,
20615 option.kind, option.section, option.info);
20616 break;
20617 }
20618
20619 len = sizeof (* eopt);
20620 while (len < option.size)
20621 {
20622 unsigned char datum = *((unsigned char *) eoption + len);
20623
20624 if (ISPRINT (datum))
20625 printf ("%c", datum);
20626 else
20627 printf ("\\%03o", datum);
20628 len ++;
20629 }
20630 fputs ("\n", stdout);
20631
20632 offset += option.size;
20633 }
20634 free (eopt);
20635 }
20636 else
20637 res = false;
20638 }
20639
20640 if (!dump_got && conflicts_offset != 0 && conflictsno != 0)
20641 {
20642 Elf32_Conflict * iconf;
20643 size_t cnt;
20644
20645 if (filedata->dynamic_symbols == NULL)
20646 {
20647 error (_("conflict list found without a dynamic symbol table\n"));
20648 return false;
20649 }
20650
20651 /* PR 21345 - print a slightly more helpful error message
20652 if we are sure that the cmalloc will fail. */
20653 if (conflictsno > filedata->file_size / sizeof (* iconf))
20654 {
20655 error (_("Overlarge number of conflicts detected: %zx\n"),
20656 conflictsno);
20657 return false;
20658 }
20659
20660 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
20661 if (iconf == NULL)
20662 {
20663 error (_("Out of memory allocating space for dynamic conflicts\n"));
20664 return false;
20665 }
20666
20667 if (is_32bit_elf)
20668 {
20669 Elf32_External_Conflict * econf32;
20670
20671 econf32 = (Elf32_External_Conflict *)
20672 get_data (NULL, filedata, conflicts_offset,
20673 sizeof (*econf32), conflictsno, _("conflict"));
20674 if (!econf32)
20675 {
20676 free (iconf);
20677 return false;
20678 }
20679
20680 for (cnt = 0; cnt < conflictsno; ++cnt)
20681 iconf[cnt] = BYTE_GET (econf32[cnt]);
20682
20683 free (econf32);
20684 }
20685 else
20686 {
20687 Elf64_External_Conflict * econf64;
20688
20689 econf64 = (Elf64_External_Conflict *)
20690 get_data (NULL, filedata, conflicts_offset,
20691 sizeof (*econf64), conflictsno, _("conflict"));
20692 if (!econf64)
20693 {
20694 free (iconf);
20695 return false;
20696 }
20697
20698 for (cnt = 0; cnt < conflictsno; ++cnt)
20699 iconf[cnt] = BYTE_GET (econf64[cnt]);
20700
20701 free (econf64);
20702 }
20703
20704 printf (ngettext ("\nSection '.conflict' contains %zu entry:\n",
20705 "\nSection '.conflict' contains %zu entries:\n",
20706 conflictsno),
20707 conflictsno);
20708 puts (_(" Num: Index Value Name"));
20709
20710 for (cnt = 0; cnt < conflictsno; ++cnt)
20711 {
20712 printf ("%5zu: %8lu ", cnt, iconf[cnt]);
20713
20714 if (iconf[cnt] >= filedata->num_dynamic_syms)
20715 printf (_("<corrupt symbol index>"));
20716 else
20717 {
20718 Elf_Internal_Sym * psym;
20719
20720 psym = & filedata->dynamic_symbols[iconf[cnt]];
20721 print_vma (psym->st_value, FULL_HEX);
20722 putchar (' ');
20723 if (valid_dynamic_name (filedata, psym->st_name))
20724 print_symbol_name (25, get_dynamic_name (filedata, psym->st_name));
20725 else
20726 printf (_("<corrupt: %14ld>"), psym->st_name);
20727 }
20728 putchar ('\n');
20729 }
20730
20731 free (iconf);
20732 }
20733
20734 if (pltgot != 0 && local_gotno != 0)
20735 {
20736 uint64_t ent, local_end, global_end;
20737 size_t i, offset;
20738 unsigned char * data;
20739 unsigned char * data_end;
20740 int addr_size;
20741
20742 ent = pltgot;
20743 addr_size = (is_32bit_elf ? 4 : 8);
20744 local_end = pltgot + local_gotno * addr_size;
20745
20746 /* PR binutils/17533 file: 012-111227-0.004 */
20747 if (symtabno < gotsym)
20748 {
20749 error (_("The GOT symbol offset (%" PRIu64
20750 ") is greater than the symbol table size (%" PRIu64 ")\n"),
20751 gotsym, symtabno);
20752 return false;
20753 }
20754
20755 global_end = local_end + (symtabno - gotsym) * addr_size;
20756 /* PR 17531: file: 54c91a34. */
20757 if (global_end < local_end)
20758 {
20759 error (_("Too many GOT symbols: %" PRIu64 "\n"), symtabno);
20760 return false;
20761 }
20762
20763 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
20764 data = (unsigned char *) get_data (NULL, filedata, offset,
20765 global_end - pltgot, 1,
20766 _("Global Offset Table data"));
20767 /* PR 12855: Null data is handled gracefully throughout. */
20768 data_end = data + (global_end - pltgot);
20769
20770 printf (_("\nPrimary GOT:\n"));
20771 printf (_(" Canonical gp value: "));
20772 print_vma (pltgot + 0x7ff0, LONG_HEX);
20773 printf ("\n\n");
20774
20775 printf (_(" Reserved entries:\n"));
20776 printf (_(" %*s %10s %*s Purpose\n"),
20777 addr_size * 2, _("Address"), _("Access"),
20778 addr_size * 2, _("Initial"));
20779 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20780 printf (_(" Lazy resolver\n"));
20781 if (ent == (uint64_t) -1)
20782 goto got_print_fail;
20783
20784 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
20785 This entry will be used by some runtime loaders, to store the
20786 module pointer. Otherwise this is an ordinary local entry.
20787 PR 21344: Check for the entry being fully available before
20788 fetching it. */
20789 if (data
20790 && data + ent - pltgot + addr_size <= data_end
20791 && (byte_get (data + ent - pltgot, addr_size)
20792 >> (addr_size * 8 - 1)) != 0)
20793 {
20794 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20795 printf (_(" Module pointer (GNU extension)\n"));
20796 if (ent == (uint64_t) -1)
20797 goto got_print_fail;
20798 }
20799 printf ("\n");
20800
20801 if (data != NULL && ent < local_end)
20802 {
20803 printf (_(" Local entries:\n"));
20804 printf (" %*s %10s %*s\n",
20805 addr_size * 2, _("Address"), _("Access"),
20806 addr_size * 2, _("Initial"));
20807 while (ent < local_end)
20808 {
20809 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20810 printf ("\n");
20811 if (ent == (uint64_t) -1)
20812 goto got_print_fail;
20813 }
20814 printf ("\n");
20815 }
20816
20817 if (data != NULL && gotsym < symtabno)
20818 {
20819 int sym_width;
20820
20821 printf (_(" Global entries:\n"));
20822 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
20823 addr_size * 2, _("Address"),
20824 _("Access"),
20825 addr_size * 2, _("Initial"),
20826 addr_size * 2, _("Sym.Val."),
20827 _("Type"),
20828 /* Note for translators: "Ndx" = abbreviated form of "Index". */
20829 _("Ndx"), _("Name"));
20830
20831 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
20832
20833 for (i = gotsym; i < symtabno; i++)
20834 {
20835 ent = print_mips_got_entry (data, pltgot, ent, data_end);
20836 printf (" ");
20837
20838 if (filedata->dynamic_symbols == NULL)
20839 printf (_("<no dynamic symbols>"));
20840 else if (i < filedata->num_dynamic_syms)
20841 {
20842 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
20843
20844 print_vma (psym->st_value, LONG_HEX);
20845 printf (" %-7s ", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
20846
20847 bool is_special;
20848 const char * s = printable_section_name_from_index (filedata, psym->st_shndx, & is_special);
20849 if (is_special)
20850 printf ("%3s ", s);
20851 else
20852 printf ("%3u ", psym->st_shndx);
20853
20854 if (valid_dynamic_name (filedata, psym->st_name))
20855 print_symbol_name (sym_width,
20856 get_dynamic_name (filedata, psym->st_name));
20857 else
20858 printf (_("<corrupt: %14ld>"), psym->st_name);
20859 }
20860 else
20861 printf (_("<symbol index %zu exceeds number of dynamic symbols>"),
20862 i);
20863
20864 printf ("\n");
20865 if (ent == (uint64_t) -1)
20866 break;
20867 }
20868 printf ("\n");
20869 }
20870
20871 got_print_fail:
20872 free (data);
20873 }
20874
20875 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
20876 {
20877 uint64_t ent, end;
20878 uint64_t offset, rel_offset;
20879 uint64_t count, i;
20880 unsigned char * data;
20881 int addr_size, sym_width;
20882 Elf_Internal_Rela * rels;
20883
20884 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
20885 if (pltrel == DT_RELA)
20886 {
20887 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
20888 return false;
20889 }
20890 else
20891 {
20892 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
20893 return false;
20894 }
20895
20896 ent = mips_pltgot;
20897 addr_size = (is_32bit_elf ? 4 : 8);
20898 end = mips_pltgot + (2 + count) * addr_size;
20899
20900 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
20901 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
20902 1, _("Procedure Linkage Table data"));
20903 if (data == NULL)
20904 {
20905 free (rels);
20906 return false;
20907 }
20908
20909 printf ("\nPLT GOT:\n\n");
20910 printf (_(" Reserved entries:\n"));
20911 printf (_(" %*s %*s Purpose\n"),
20912 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
20913 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
20914 printf (_(" PLT lazy resolver\n"));
20915 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
20916 printf (_(" Module pointer\n"));
20917 printf ("\n");
20918
20919 printf (_(" Entries:\n"));
20920 printf (" %*s %*s %*s %-7s %3s %s\n",
20921 addr_size * 2, _("Address"),
20922 addr_size * 2, _("Initial"),
20923 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
20924 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
20925 for (i = 0; i < count; i++)
20926 {
20927 uint64_t idx = get_reloc_symindex (rels[i].r_info);
20928
20929 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
20930 printf (" ");
20931
20932 if (idx >= filedata->num_dynamic_syms)
20933 printf (_("<corrupt symbol index: %" PRIu64 ">"), idx);
20934 else
20935 {
20936 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
20937
20938 print_vma (psym->st_value, LONG_HEX);
20939 printf (" %-7s %3s ",
20940 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
20941 printable_section_name_from_index (filedata, psym->st_shndx, NULL));
20942 if (valid_dynamic_name (filedata, psym->st_name))
20943 print_symbol_name (sym_width,
20944 get_dynamic_name (filedata, psym->st_name));
20945 else
20946 printf (_("<corrupt: %14ld>"), psym->st_name);
20947 }
20948 printf ("\n");
20949 }
20950 printf ("\n");
20951
20952 free (data);
20953 free (rels);
20954 }
20955
20956 return res;
20957}
20958
20959static bool
20960process_nds32_specific (Filedata * filedata)
20961{
20962 Elf_Internal_Shdr *sect = NULL;
20963
20964 sect = find_section (filedata, ".nds32_e_flags");
20965 if (sect != NULL && sect->sh_size >= 4)
20966 {
20967 unsigned char *buf;
20968 unsigned int flag;
20969
20970 printf ("\nNDS32 elf flags section:\n");
20971 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
20972 _("NDS32 elf flags section"));
20973
20974 if (buf == NULL)
20975 return false;
20976
20977 flag = byte_get (buf, 4);
20978 free (buf);
20979 switch (flag & 0x3)
20980 {
20981 case 0:
20982 printf ("(VEC_SIZE):\tNo entry.\n");
20983 break;
20984 case 1:
20985 printf ("(VEC_SIZE):\t4 bytes\n");
20986 break;
20987 case 2:
20988 printf ("(VEC_SIZE):\t16 bytes\n");
20989 break;
20990 case 3:
20991 printf ("(VEC_SIZE):\treserved\n");
20992 break;
20993 }
20994 }
20995
20996 return true;
20997}
20998
20999static int
21000elf_relocation_cmp (const void *p, const void *q)
21001{
21002 const elf_relocation *rp = (const elf_relocation *) p;
21003 const elf_relocation *rq = (const elf_relocation *) q;
21004
21005 return (rp->r_offset > rq->r_offset
21006 ? 1
21007 : (rp->r_offset < rq->r_offset ? -1 : 0));
21008}
21009
21010static void
21011display_elf_relocation_at (uint64_t offset, uint64_t g)
21012{
21013 bool matched = false;
21014
21015 for (size_t i = 0; i < all_relocations_count; i++)
21016 if (all_relocations_root[i].r_offset == offset)
21017 {
21018 if (do_wide)
21019 printf (" %-22s", all_relocations_root[i].r_name);
21020 else
21021 printf (" %-17.17s", all_relocations_root[i].r_name);
21022
21023 uint64_t off;
21024 switch (all_relocations_root[i].r_type)
21025 {
21026 default:
21027 abort ();
21028 case reltype_rel:
21029 case reltype_relr:
21030 off = g;
21031 break;
21032 case reltype_rela:
21033 off = all_relocations_root[i].r_addend;
21034 break;
21035 }
21036
21037 if (all_relocations_root[i].r_symbol)
21038 {
21039 printf (" %s", all_relocations_root[i].r_symbol);
21040 if ((int64_t) off < 0)
21041 printf (" - %" PRIx64, -off);
21042 else
21043 printf (" + %" PRIx64, off);
21044 }
21045 else
21046 {
21047 if ((int64_t) off < 0)
21048 printf (" -%" PRIx64, -off);
21049 else
21050 printf (" %" PRIx64, off);
21051 }
21052
21053 matched = true;
21054 break;
21055 }
21056 else if (all_relocations_root[i].r_offset > offset)
21057 break;
21058
21059 if (!matched)
21060 {
21061 if (do_wide)
21062 printf ("%*c", 24, ' ');
21063 else
21064 printf ("%*c", 19, ' ');
21065 printf ("%" PRIx64, g);
21066 }
21067}
21068
21069static bool
21070process_got_section_contents (Filedata * filedata)
21071{
21072 Elf_Internal_Shdr * section;
21073 unsigned int i;
21074 uint64_t entries;
21075 unsigned char *data;
21076 bool res = true;
21077 bool found = false;
21078
21079 if (!do_got_section_contents)
21080 return res;
21081
21082 switch (filedata->file_header.e_type)
21083 {
21084 case ET_DYN:
21085 case ET_EXEC:
21086 break;
21087 default:
21088 goto out;
21089 }
21090
21091 switch (filedata->file_header.e_machine)
21092 {
21093 case EM_MIPS:
21094 case EM_MIPS_RS3_LE:
21095 /* process_mips_specific also displays GOT related contents. */
21096 if (!do_arch)
21097 res = process_mips_specific (filedata, true);
21098 found = true;
21099 goto out;
21100 }
21101
21102 if (all_relocations_count > 1)
21103 qsort (all_relocations_root, all_relocations_count,
21104 sizeof (elf_relocation), elf_relocation_cmp);
21105
21106 initialise_dumps_byname (filedata);
21107
21108 for (i = 0, section = filedata->section_headers;
21109 i < filedata->file_header.e_shnum;
21110 i++, section++)
21111 if (section->sh_type == SHT_PROGBITS
21112 && section->sh_size != 0)
21113 {
21114 const char *name = printable_section_name (filedata, section);
21115
21116 if (!startswith (name, ".got"))
21117 continue;
21118
21119 found = true;
21120
21121 data = (unsigned char *) get_section_contents (section,
21122 filedata);
21123 if (data == NULL)
21124 {
21125 res = false;
21126 goto out;
21127 }
21128
21129 uint32_t entsz = section->sh_entsize;
21130 /* NB: Gold, lld and mold set sh_entsize to 0 on .got and
21131 .got.plt sections. */
21132 if (entsz == 0)
21133 {
21134 if (is_32bit_elf)
21135 switch (filedata->file_header.e_machine)
21136 {
21137 default:
21138 entsz = 4;
21139 break;
21140 case EM_X86_64:
21141 /* x32 uses 8 byte GOT sh_entsize. */
21142 entsz = 8;
21143 break;
21144 }
21145 else
21146 entsz = 8;
21147 }
21148
21149 entries = section->sh_size / entsz;
21150 if (entries == 1)
21151 printf (_("\nGlobal Offset Table '%s' contains 1 entry:\n"),
21152 name);
21153 else
21154 printf (_("\nGlobal Offset Table '%s' contains %" PRIu64
21155 " entries:\n"), name, entries);
21156
21157 uint64_t g;
21158
21159 if (is_32bit_elf)
21160 {
21161 uint32_t j, n = entries;
21162 uint32_t addr;
21163 struct got32
21164 {
21165 unsigned char bytes[4];
21166 } *got;
21167
21168 if (do_wide)
21169 printf (_(" Index: Address Reloc Sym. Name + Addend/Value\n"));
21170 /* |---9---| |---8--| |---------22---------| |........... */
21171 else
21172 printf (_(" Index: Address Reloc Sym. Name + Addend/Value\n"));
21173 /* |--7--| |---8--| |-------17------| |........... */
21174
21175 addr = section->sh_addr;
21176 got = (struct got32 *) data;
21177 for (j = 0; j < n; j++)
21178 {
21179 g = BYTE_GET (got[j].bytes);
21180 if (do_wide)
21181 printf ("%8" PRIu32 ": %8.8" PRIx32, j, addr);
21182 else
21183 printf ("%6" PRIu32 ": %8.8" PRIx32, j, addr);
21184 display_elf_relocation_at (addr, g);
21185 putchar ('\n');
21186 addr += entsz;
21187 }
21188 }
21189 else
21190 {
21191 uint64_t j, addr;
21192 struct got64
21193 {
21194 unsigned char bytes[4];
21195 } *got;
21196
21197 if (do_wide)
21198 printf (_(" Index: Address Reloc Sym. Name + Addend/Value\n"));
21199 /* |---9---| |------16------| |---------22---------| |........... */
21200 else
21201 printf (_(" Index: Address Reloc Sym. Name + Addend/Value\n"));
21202 /* |--7--| |----12----| |------17-------| |........... */
21203
21204 addr = section->sh_addr;
21205 got = (struct got64 *) data;
21206 for (j = 0; j < entries; j++)
21207 {
21208 g = BYTE_GET (got[j].bytes);
21209 if (do_wide)
21210 printf ("%8" PRIu64 ": %16.16" PRIx64, j, addr);
21211 else
21212 printf ("%6" PRIu64 ": %12.12" PRIx64, j, addr);
21213 display_elf_relocation_at (addr, g);
21214 putchar ('\n');
21215 addr += entsz;
21216 }
21217 }
21218
21219 free (data);
21220 }
21221
21222 out:
21223 if (! found)
21224 {
21225 if (filedata->is_separate)
21226 printf (_("\nThere is no GOT section in linked file '%s'.\n"),
21227 filedata->file_name);
21228 else
21229 printf (_("\nThere is no GOT section in this file.\n"));
21230 }
21231
21232 for (size_t j = 0; j < all_relocations_count; j++)
21233 free (all_relocations_root[j].r_symbol);
21234 free (all_relocations_root);
21235 all_relocations_root = NULL;
21236 all_relocations = NULL;
21237 all_relocations_count = 0;
21238 return res;
21239}
21240
21241static bool
21242process_gnu_liblist (Filedata * filedata)
21243{
21244 Elf_Internal_Shdr * section;
21245 Elf_Internal_Shdr * string_sec;
21246 Elf32_External_Lib * elib;
21247 char * strtab;
21248 size_t strtab_size;
21249 size_t cnt;
21250 uint64_t num_liblist;
21251 unsigned i;
21252 bool res = true;
21253
21254 if (! do_arch)
21255 return true;
21256
21257 for (i = 0, section = filedata->section_headers;
21258 i < filedata->file_header.e_shnum;
21259 i++, section++)
21260 {
21261 switch (section->sh_type)
21262 {
21263 case SHT_GNU_LIBLIST:
21264 if (section->sh_link >= filedata->file_header.e_shnum)
21265 break;
21266
21267 elib = (Elf32_External_Lib *)
21268 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
21269 _("liblist section data"));
21270
21271 if (elib == NULL)
21272 {
21273 res = false;
21274 break;
21275 }
21276
21277 string_sec = filedata->section_headers + section->sh_link;
21278 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
21279 string_sec->sh_size,
21280 _("liblist string table"));
21281 if (strtab == NULL
21282 || section->sh_entsize != sizeof (Elf32_External_Lib))
21283 {
21284 free (elib);
21285 free (strtab);
21286 res = false;
21287 break;
21288 }
21289 strtab_size = string_sec->sh_size;
21290
21291 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
21292 printf (ngettext ("\nLibrary list section '%s' contains %" PRIu64
21293 " entries:\n",
21294 "\nLibrary list section '%s' contains %" PRIu64
21295 " entries:\n",
21296 num_liblist),
21297 printable_section_name (filedata, section),
21298 num_liblist);
21299
21300 puts (_(" Library Time Stamp Checksum Version Flags"));
21301
21302 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
21303 ++cnt)
21304 {
21305 Elf32_Lib liblist;
21306 time_t atime;
21307 char timebuf[128];
21308 struct tm * tmp;
21309
21310 liblist.l_name = BYTE_GET (elib[cnt].l_name);
21311 atime = BYTE_GET (elib[cnt].l_time_stamp);
21312 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
21313 liblist.l_version = BYTE_GET (elib[cnt].l_version);
21314 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
21315
21316 tmp = gmtime (&atime);
21317 snprintf (timebuf, sizeof (timebuf),
21318 "%04u-%02u-%02uT%02u:%02u:%02u",
21319 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
21320 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
21321
21322 printf ("%3zu: ", cnt);
21323 if (do_wide)
21324 printf ("%-20s", liblist.l_name < strtab_size
21325 ? strtab + liblist.l_name : _("<corrupt>"));
21326 else
21327 printf ("%-20.20s", liblist.l_name < strtab_size
21328 ? strtab + liblist.l_name : _("<corrupt>"));
21329 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
21330 liblist.l_version, liblist.l_flags);
21331 }
21332
21333 free (elib);
21334 free (strtab);
21335 }
21336 }
21337
21338 return res;
21339}
21340
21341static const char *
21342get_note_type (Filedata * filedata, unsigned e_type)
21343{
21344 static char buff[64];
21345
21346 if (filedata->file_header.e_type == ET_CORE)
21347 switch (e_type)
21348 {
21349 case NT_AUXV:
21350 return _("NT_AUXV (auxiliary vector)");
21351 case NT_PRSTATUS:
21352 return _("NT_PRSTATUS (prstatus structure)");
21353 case NT_FPREGSET:
21354 return _("NT_FPREGSET (floating point registers)");
21355 case NT_PRPSINFO:
21356 return _("NT_PRPSINFO (prpsinfo structure)");
21357 case NT_TASKSTRUCT:
21358 return _("NT_TASKSTRUCT (task structure)");
21359 case NT_GDB_TDESC:
21360 return _("NT_GDB_TDESC (GDB XML target description)");
21361 case NT_PRXFPREG:
21362 return _("NT_PRXFPREG (user_xfpregs structure)");
21363 case NT_PPC_VMX:
21364 return _("NT_PPC_VMX (ppc Altivec registers)");
21365 case NT_PPC_VSX:
21366 return _("NT_PPC_VSX (ppc VSX registers)");
21367 case NT_PPC_TAR:
21368 return _("NT_PPC_TAR (ppc TAR register)");
21369 case NT_PPC_PPR:
21370 return _("NT_PPC_PPR (ppc PPR register)");
21371 case NT_PPC_DSCR:
21372 return _("NT_PPC_DSCR (ppc DSCR register)");
21373 case NT_PPC_EBB:
21374 return _("NT_PPC_EBB (ppc EBB registers)");
21375 case NT_PPC_PMU:
21376 return _("NT_PPC_PMU (ppc PMU registers)");
21377 case NT_PPC_TM_CGPR:
21378 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
21379 case NT_PPC_TM_CFPR:
21380 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
21381 case NT_PPC_TM_CVMX:
21382 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
21383 case NT_PPC_TM_CVSX:
21384 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
21385 case NT_PPC_TM_SPR:
21386 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
21387 case NT_PPC_TM_CTAR:
21388 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
21389 case NT_PPC_TM_CPPR:
21390 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
21391 case NT_PPC_TM_CDSCR:
21392 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
21393 case NT_386_TLS:
21394 return _("NT_386_TLS (x86 TLS information)");
21395 case NT_386_IOPERM:
21396 return _("NT_386_IOPERM (x86 I/O permissions)");
21397 case NT_X86_XSTATE:
21398 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
21399 case NT_X86_CET:
21400 return _("NT_X86_CET (x86 CET state)");
21401 case NT_X86_SHSTK:
21402 return _("NT_X86_SHSTK (x86 SHSTK state)");
21403 case NT_S390_HIGH_GPRS:
21404 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
21405 case NT_S390_TIMER:
21406 return _("NT_S390_TIMER (s390 timer register)");
21407 case NT_S390_TODCMP:
21408 return _("NT_S390_TODCMP (s390 TOD comparator register)");
21409 case NT_S390_TODPREG:
21410 return _("NT_S390_TODPREG (s390 TOD programmable register)");
21411 case NT_S390_CTRS:
21412 return _("NT_S390_CTRS (s390 control registers)");
21413 case NT_S390_PREFIX:
21414 return _("NT_S390_PREFIX (s390 prefix register)");
21415 case NT_S390_LAST_BREAK:
21416 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
21417 case NT_S390_SYSTEM_CALL:
21418 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
21419 case NT_S390_TDB:
21420 return _("NT_S390_TDB (s390 transaction diagnostic block)");
21421 case NT_S390_VXRS_LOW:
21422 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
21423 case NT_S390_VXRS_HIGH:
21424 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
21425 case NT_S390_GS_CB:
21426 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
21427 case NT_S390_GS_BC:
21428 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
21429 case NT_ARM_VFP:
21430 return _("NT_ARM_VFP (arm VFP registers)");
21431 case NT_ARM_TLS:
21432 return _("NT_ARM_TLS (AArch TLS registers)");
21433 case NT_ARM_HW_BREAK:
21434 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
21435 case NT_ARM_HW_WATCH:
21436 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
21437 case NT_ARM_SYSTEM_CALL:
21438 return _("NT_ARM_SYSTEM_CALL (AArch system call number)");
21439 case NT_ARM_SVE:
21440 return _("NT_ARM_SVE (AArch SVE registers)");
21441 case NT_ARM_PAC_MASK:
21442 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
21443 case NT_ARM_PACA_KEYS:
21444 return _("NT_ARM_PACA_KEYS (ARM pointer authentication address keys)");
21445 case NT_ARM_PACG_KEYS:
21446 return _("NT_ARM_PACG_KEYS (ARM pointer authentication generic keys)");
21447 case NT_ARM_TAGGED_ADDR_CTRL:
21448 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
21449 case NT_ARM_SSVE:
21450 return _("NT_ARM_SSVE (AArch64 streaming SVE registers)");
21451 case NT_ARM_ZA:
21452 return _("NT_ARM_ZA (AArch64 SME ZA register)");
21453 case NT_ARM_ZT:
21454 return _("NT_ARM_ZT (AArch64 SME2 ZT registers)");
21455 case NT_ARM_PAC_ENABLED_KEYS:
21456 return _("NT_ARM_PAC_ENABLED_KEYS (AArch64 pointer authentication enabled keys)");
21457 case NT_ARM_FPMR:
21458 return _("NT_ARM_FPMR (AArch64 Floating Point Mode Register)");
21459 case NT_ARC_V2:
21460 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
21461 case NT_RISCV_CSR:
21462 return _("NT_RISCV_CSR (RISC-V control and status registers)");
21463 case NT_PSTATUS:
21464 return _("NT_PSTATUS (pstatus structure)");
21465 case NT_FPREGS:
21466 return _("NT_FPREGS (floating point registers)");
21467 case NT_PSINFO:
21468 return _("NT_PSINFO (psinfo structure)");
21469 case NT_LWPSTATUS:
21470 return _("NT_LWPSTATUS (lwpstatus_t structure)");
21471 case NT_LWPSINFO:
21472 return _("NT_LWPSINFO (lwpsinfo_t structure)");
21473 case NT_WIN32PSTATUS:
21474 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
21475 case NT_SIGINFO:
21476 return _("NT_SIGINFO (siginfo_t data)");
21477 case NT_FILE:
21478 return _("NT_FILE (mapped files)");
21479 default:
21480 break;
21481 }
21482 else
21483 switch (e_type)
21484 {
21485 case NT_VERSION:
21486 return _("NT_VERSION (version)");
21487 case NT_ARCH:
21488 return _("NT_ARCH (architecture)");
21489 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
21490 return _("OPEN");
21491 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
21492 return _("func");
21493 case NT_GO_BUILDID:
21494 return _("GO BUILDID");
21495 case FDO_PACKAGING_METADATA:
21496 return _("FDO_PACKAGING_METADATA");
21497 case FDO_DLOPEN_METADATA:
21498 return _("FDO_DLOPEN_METADATA");
21499 default:
21500 break;
21501 }
21502
21503 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
21504 return buff;
21505}
21506
21507static bool
21508print_core_note (Elf_Internal_Note *pnote)
21509{
21510 unsigned int addr_size = is_32bit_elf ? 4 : 8;
21511 uint64_t count, page_size;
21512 unsigned char *descdata, *filenames, *descend;
21513
21514 if (pnote->type != NT_FILE)
21515 {
21516 if (do_wide)
21517 printf ("\n");
21518 return true;
21519 }
21520
21521 if (pnote->descsz < 2 * addr_size)
21522 {
21523 error (_(" Malformed note - too short for header\n"));
21524 return false;
21525 }
21526
21527 descdata = (unsigned char *) pnote->descdata;
21528 descend = descdata + pnote->descsz;
21529
21530 if (descdata[pnote->descsz - 1] != '\0')
21531 {
21532 error (_(" Malformed note - does not end with \\0\n"));
21533 return false;
21534 }
21535
21536 count = byte_get (descdata, addr_size);
21537 descdata += addr_size;
21538
21539 page_size = byte_get (descdata, addr_size);
21540 descdata += addr_size;
21541
21542 if (count > ((uint64_t) -1 - 2 * addr_size) / (3 * addr_size)
21543 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
21544 {
21545 error (_(" Malformed note - too short for supplied file count\n"));
21546 return false;
21547 }
21548
21549 printf (_(" Page size: "));
21550 print_vma (page_size, DEC);
21551 printf ("\n");
21552
21553 printf (_(" %*s%*s%*s\n"),
21554 (int) (2 + 2 * addr_size), _("Start"),
21555 (int) (4 + 2 * addr_size), _("End"),
21556 (int) (4 + 2 * addr_size), _("Page Offset"));
21557 filenames = descdata + count * 3 * addr_size;
21558 while (count-- > 0)
21559 {
21560 uint64_t start, end, file_ofs;
21561
21562 if (filenames == descend)
21563 {
21564 error (_(" Malformed note - filenames end too early\n"));
21565 return false;
21566 }
21567
21568 start = byte_get (descdata, addr_size);
21569 descdata += addr_size;
21570 end = byte_get (descdata, addr_size);
21571 descdata += addr_size;
21572 file_ofs = byte_get (descdata, addr_size);
21573 descdata += addr_size;
21574
21575 printf (" ");
21576 print_vma (start, FULL_HEX);
21577 printf (" ");
21578 print_vma (end, FULL_HEX);
21579 printf (" ");
21580 print_vma (file_ofs, FULL_HEX);
21581 printf ("\n %s\n", filenames);
21582
21583 filenames += 1 + strlen ((char *) filenames);
21584 }
21585
21586 return true;
21587}
21588
21589static const char *
21590get_gnu_elf_note_type (unsigned e_type)
21591{
21592 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
21593 switch (e_type)
21594 {
21595 case NT_GNU_ABI_TAG:
21596 return _("NT_GNU_ABI_TAG (ABI version tag)");
21597 case NT_GNU_HWCAP:
21598 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
21599 case NT_GNU_BUILD_ID:
21600 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
21601 case NT_GNU_GOLD_VERSION:
21602 return _("NT_GNU_GOLD_VERSION (gold version)");
21603 case NT_GNU_PROPERTY_TYPE_0:
21604 return _("NT_GNU_PROPERTY_TYPE_0");
21605 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
21606 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
21607 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
21608 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
21609 default:
21610 {
21611 static char buff[64];
21612
21613 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
21614 return buff;
21615 }
21616 }
21617}
21618
21619static void
21620decode_x86_compat_isa (unsigned int bitmask)
21621{
21622 while (bitmask)
21623 {
21624 unsigned int bit = bitmask & (- bitmask);
21625
21626 bitmask &= ~ bit;
21627 switch (bit)
21628 {
21629 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
21630 printf ("i486");
21631 break;
21632 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
21633 printf ("586");
21634 break;
21635 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
21636 printf ("686");
21637 break;
21638 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
21639 printf ("SSE");
21640 break;
21641 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
21642 printf ("SSE2");
21643 break;
21644 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
21645 printf ("SSE3");
21646 break;
21647 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
21648 printf ("SSSE3");
21649 break;
21650 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
21651 printf ("SSE4_1");
21652 break;
21653 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
21654 printf ("SSE4_2");
21655 break;
21656 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
21657 printf ("AVX");
21658 break;
21659 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
21660 printf ("AVX2");
21661 break;
21662 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
21663 printf ("AVX512F");
21664 break;
21665 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
21666 printf ("AVX512CD");
21667 break;
21668 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
21669 printf ("AVX512ER");
21670 break;
21671 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
21672 printf ("AVX512PF");
21673 break;
21674 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
21675 printf ("AVX512VL");
21676 break;
21677 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
21678 printf ("AVX512DQ");
21679 break;
21680 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
21681 printf ("AVX512BW");
21682 break;
21683 default:
21684 printf (_("<unknown: %x>"), bit);
21685 break;
21686 }
21687 if (bitmask)
21688 printf (", ");
21689 }
21690}
21691
21692static void
21693decode_x86_compat_2_isa (unsigned int bitmask)
21694{
21695 if (!bitmask)
21696 {
21697 printf (_("<None>"));
21698 return;
21699 }
21700
21701 while (bitmask)
21702 {
21703 unsigned int bit = bitmask & (- bitmask);
21704
21705 bitmask &= ~ bit;
21706 switch (bit)
21707 {
21708 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
21709 printf ("CMOV");
21710 break;
21711 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
21712 printf ("SSE");
21713 break;
21714 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
21715 printf ("SSE2");
21716 break;
21717 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
21718 printf ("SSE3");
21719 break;
21720 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
21721 printf ("SSSE3");
21722 break;
21723 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
21724 printf ("SSE4_1");
21725 break;
21726 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
21727 printf ("SSE4_2");
21728 break;
21729 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
21730 printf ("AVX");
21731 break;
21732 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
21733 printf ("AVX2");
21734 break;
21735 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
21736 printf ("FMA");
21737 break;
21738 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
21739 printf ("AVX512F");
21740 break;
21741 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
21742 printf ("AVX512CD");
21743 break;
21744 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
21745 printf ("AVX512ER");
21746 break;
21747 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
21748 printf ("AVX512PF");
21749 break;
21750 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
21751 printf ("AVX512VL");
21752 break;
21753 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
21754 printf ("AVX512DQ");
21755 break;
21756 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
21757 printf ("AVX512BW");
21758 break;
21759 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
21760 printf ("AVX512_4FMAPS");
21761 break;
21762 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
21763 printf ("AVX512_4VNNIW");
21764 break;
21765 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
21766 printf ("AVX512_BITALG");
21767 break;
21768 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
21769 printf ("AVX512_IFMA");
21770 break;
21771 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
21772 printf ("AVX512_VBMI");
21773 break;
21774 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
21775 printf ("AVX512_VBMI2");
21776 break;
21777 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
21778 printf ("AVX512_VNNI");
21779 break;
21780 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
21781 printf ("AVX512_BF16");
21782 break;
21783 default:
21784 printf (_("<unknown: %x>"), bit);
21785 break;
21786 }
21787 if (bitmask)
21788 printf (", ");
21789 }
21790}
21791
21792static const char *
21793get_amdgpu_elf_note_type (unsigned int e_type)
21794{
21795 switch (e_type)
21796 {
21797 case NT_AMDGPU_METADATA:
21798 return _("NT_AMDGPU_METADATA (code object metadata)");
21799 default:
21800 {
21801 static char buf[64];
21802 snprintf (buf, sizeof (buf), _("Unknown note type: (0x%08x)"), e_type);
21803 return buf;
21804 }
21805 }
21806}
21807
21808static void
21809decode_x86_isa (unsigned int bitmask)
21810{
21811 while (bitmask)
21812 {
21813 unsigned int bit = bitmask & (- bitmask);
21814
21815 bitmask &= ~ bit;
21816 switch (bit)
21817 {
21818 case GNU_PROPERTY_X86_ISA_1_BASELINE:
21819 printf ("x86-64-baseline");
21820 break;
21821 case GNU_PROPERTY_X86_ISA_1_V2:
21822 printf ("x86-64-v2");
21823 break;
21824 case GNU_PROPERTY_X86_ISA_1_V3:
21825 printf ("x86-64-v3");
21826 break;
21827 case GNU_PROPERTY_X86_ISA_1_V4:
21828 printf ("x86-64-v4");
21829 break;
21830 default:
21831 printf (_("<unknown: %x>"), bit);
21832 break;
21833 }
21834 if (bitmask)
21835 printf (", ");
21836 }
21837}
21838
21839static void
21840decode_x86_feature_1 (unsigned int bitmask)
21841{
21842 if (!bitmask)
21843 {
21844 printf (_("<None>"));
21845 return;
21846 }
21847
21848 while (bitmask)
21849 {
21850 unsigned int bit = bitmask & (- bitmask);
21851
21852 bitmask &= ~ bit;
21853 switch (bit)
21854 {
21855 case GNU_PROPERTY_X86_FEATURE_1_IBT:
21856 printf ("IBT");
21857 break;
21858 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
21859 printf ("SHSTK");
21860 break;
21861 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
21862 printf ("LAM_U48");
21863 break;
21864 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
21865 printf ("LAM_U57");
21866 break;
21867 default:
21868 printf (_("<unknown: %x>"), bit);
21869 break;
21870 }
21871 if (bitmask)
21872 printf (", ");
21873 }
21874}
21875
21876static void
21877decode_x86_feature_2 (unsigned int bitmask)
21878{
21879 if (!bitmask)
21880 {
21881 printf (_("<None>"));
21882 return;
21883 }
21884
21885 while (bitmask)
21886 {
21887 unsigned int bit = bitmask & (- bitmask);
21888
21889 bitmask &= ~ bit;
21890 switch (bit)
21891 {
21892 case GNU_PROPERTY_X86_FEATURE_2_X86:
21893 printf ("x86");
21894 break;
21895 case GNU_PROPERTY_X86_FEATURE_2_X87:
21896 printf ("x87");
21897 break;
21898 case GNU_PROPERTY_X86_FEATURE_2_MMX:
21899 printf ("MMX");
21900 break;
21901 case GNU_PROPERTY_X86_FEATURE_2_XMM:
21902 printf ("XMM");
21903 break;
21904 case GNU_PROPERTY_X86_FEATURE_2_YMM:
21905 printf ("YMM");
21906 break;
21907 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
21908 printf ("ZMM");
21909 break;
21910 case GNU_PROPERTY_X86_FEATURE_2_TMM:
21911 printf ("TMM");
21912 break;
21913 case GNU_PROPERTY_X86_FEATURE_2_MASK:
21914 printf ("MASK");
21915 break;
21916 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
21917 printf ("FXSR");
21918 break;
21919 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
21920 printf ("XSAVE");
21921 break;
21922 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
21923 printf ("XSAVEOPT");
21924 break;
21925 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
21926 printf ("XSAVEC");
21927 break;
21928 default:
21929 printf (_("<unknown: %x>"), bit);
21930 break;
21931 }
21932 if (bitmask)
21933 printf (", ");
21934 }
21935}
21936
21937static void
21938decode_aarch64_feature_1_and (unsigned int bitmask)
21939{
21940 while (bitmask)
21941 {
21942 unsigned int bit = bitmask & (- bitmask);
21943
21944 bitmask &= ~ bit;
21945 switch (bit)
21946 {
21947 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
21948 printf ("BTI");
21949 break;
21950
21951 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
21952 printf ("PAC");
21953 break;
21954
21955 case GNU_PROPERTY_AARCH64_FEATURE_1_GCS:
21956 printf ("GCS");
21957 break;
21958
21959 default:
21960 printf (_("<unknown: %x>"), bit);
21961 break;
21962 }
21963 if (bitmask)
21964 printf (", ");
21965 }
21966}
21967
21968static void
21969decode_riscv_feature_1_and (unsigned int bitmask)
21970{
21971 while (bitmask)
21972 {
21973 unsigned int bit = bitmask & (- bitmask);
21974
21975 bitmask &= ~ bit;
21976 switch (bit)
21977 {
21978 case GNU_PROPERTY_RISCV_FEATURE_1_CFI_LP_UNLABELED:
21979 printf ("CFI_LP_UNLABELED");
21980 break;
21981
21982 case GNU_PROPERTY_RISCV_FEATURE_1_CFI_SS:
21983 printf ("CFI_SS");
21984 break;
21985
21986 default:
21987 printf (_("<unknown: %x>"), bit);
21988 break;
21989 }
21990 if (bitmask)
21991 printf (", ");
21992 }
21993}
21994
21995static void
21996decode_1_needed (unsigned int bitmask)
21997{
21998 while (bitmask)
21999 {
22000 unsigned int bit = bitmask & (- bitmask);
22001
22002 bitmask &= ~ bit;
22003 switch (bit)
22004 {
22005 case GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
22006 printf ("indirect external access");
22007 break;
22008 default:
22009 printf (_("<unknown: %x>"), bit);
22010 break;
22011 }
22012 if (bitmask)
22013 printf (", ");
22014 }
22015}
22016
22017static void
22018print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
22019{
22020 unsigned char * ptr = (unsigned char *) pnote->descdata;
22021 unsigned char * ptr_end = ptr + pnote->descsz;
22022 unsigned int size = is_32bit_elf ? 4 : 8;
22023
22024 printf (_(" Properties: "));
22025
22026 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
22027 {
22028 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
22029 return;
22030 }
22031
22032 while (ptr < ptr_end)
22033 {
22034 unsigned int j;
22035 unsigned int type;
22036 unsigned int datasz;
22037
22038 if ((size_t) (ptr_end - ptr) < 8)
22039 {
22040 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
22041 break;
22042 }
22043
22044 type = byte_get (ptr, 4);
22045 datasz = byte_get (ptr + 4, 4);
22046
22047 ptr += 8;
22048
22049 if (datasz > (size_t) (ptr_end - ptr))
22050 {
22051 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
22052 type, datasz);
22053 break;
22054 }
22055
22056 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
22057 {
22058 if (filedata->file_header.e_machine == EM_X86_64
22059 || filedata->file_header.e_machine == EM_IAMCU
22060 || filedata->file_header.e_machine == EM_386)
22061 {
22062 unsigned int bitmask;
22063
22064 if (datasz == 4)
22065 bitmask = byte_get (ptr, 4);
22066 else
22067 bitmask = 0;
22068
22069 switch (type)
22070 {
22071 case GNU_PROPERTY_X86_ISA_1_USED:
22072 if (datasz != 4)
22073 printf (_("x86 ISA used: <corrupt length: %#x> "),
22074 datasz);
22075 else
22076 {
22077 printf ("x86 ISA used: ");
22078 decode_x86_isa (bitmask);
22079 }
22080 goto next;
22081
22082 case GNU_PROPERTY_X86_ISA_1_NEEDED:
22083 if (datasz != 4)
22084 printf (_("x86 ISA needed: <corrupt length: %#x> "),
22085 datasz);
22086 else
22087 {
22088 printf ("x86 ISA needed: ");
22089 decode_x86_isa (bitmask);
22090 }
22091 goto next;
22092
22093 case GNU_PROPERTY_X86_FEATURE_1_AND:
22094 if (datasz != 4)
22095 printf (_("x86 feature: <corrupt length: %#x> "),
22096 datasz);
22097 else
22098 {
22099 printf ("x86 feature: ");
22100 decode_x86_feature_1 (bitmask);
22101 }
22102 goto next;
22103
22104 case GNU_PROPERTY_X86_FEATURE_2_USED:
22105 if (datasz != 4)
22106 printf (_("x86 feature used: <corrupt length: %#x> "),
22107 datasz);
22108 else
22109 {
22110 printf ("x86 feature used: ");
22111 decode_x86_feature_2 (bitmask);
22112 }
22113 goto next;
22114
22115 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
22116 if (datasz != 4)
22117 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
22118 else
22119 {
22120 printf ("x86 feature needed: ");
22121 decode_x86_feature_2 (bitmask);
22122 }
22123 goto next;
22124
22125 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
22126 if (datasz != 4)
22127 printf (_("x86 ISA used: <corrupt length: %#x> "),
22128 datasz);
22129 else
22130 {
22131 printf ("x86 ISA used: ");
22132 decode_x86_compat_isa (bitmask);
22133 }
22134 goto next;
22135
22136 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
22137 if (datasz != 4)
22138 printf (_("x86 ISA needed: <corrupt length: %#x> "),
22139 datasz);
22140 else
22141 {
22142 printf ("x86 ISA needed: ");
22143 decode_x86_compat_isa (bitmask);
22144 }
22145 goto next;
22146
22147 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
22148 if (datasz != 4)
22149 printf (_("x86 ISA used: <corrupt length: %#x> "),
22150 datasz);
22151 else
22152 {
22153 printf ("x86 ISA used: ");
22154 decode_x86_compat_2_isa (bitmask);
22155 }
22156 goto next;
22157
22158 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
22159 if (datasz != 4)
22160 printf (_("x86 ISA needed: <corrupt length: %#x> "),
22161 datasz);
22162 else
22163 {
22164 printf ("x86 ISA needed: ");
22165 decode_x86_compat_2_isa (bitmask);
22166 }
22167 goto next;
22168
22169 default:
22170 break;
22171 }
22172 }
22173 else if (filedata->file_header.e_machine == EM_AARCH64)
22174 {
22175 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
22176 {
22177 printf ("AArch64 feature: ");
22178 if (datasz != 4)
22179 printf (_("<corrupt length: %#x> "), datasz);
22180 else
22181 decode_aarch64_feature_1_and (byte_get (ptr, 4));
22182 goto next;
22183 }
22184 }
22185 else if (filedata->file_header.e_machine == EM_RISCV)
22186 {
22187 if (type == GNU_PROPERTY_RISCV_FEATURE_1_AND)
22188 {
22189 printf ("RISC-V AND feature: ");
22190 if (datasz != 4)
22191 printf (_("<corrupt length: %#x> "), datasz);
22192 else
22193 decode_riscv_feature_1_and (byte_get (ptr, 4));
22194 goto next;
22195 }
22196 }
22197 }
22198 else
22199 {
22200 switch (type)
22201 {
22202 case GNU_PROPERTY_STACK_SIZE:
22203 printf (_("stack size: "));
22204 if (datasz != size)
22205 printf (_("<corrupt length: %#x> "), datasz);
22206 else
22207 printf ("%#" PRIx64, byte_get (ptr, size));
22208 goto next;
22209
22210 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
22211 printf ("no copy on protected ");
22212 if (datasz)
22213 printf (_("<corrupt length: %#x> "), datasz);
22214 goto next;
22215
22216 case GNU_PROPERTY_MEMORY_SEAL:
22217 printf ("memory seal ");
22218 if (datasz)
22219 printf (_("<corrupt length: %#x> "), datasz);
22220 goto next;
22221
22222 default:
22223 if ((type >= GNU_PROPERTY_UINT32_AND_LO
22224 && type <= GNU_PROPERTY_UINT32_AND_HI)
22225 || (type >= GNU_PROPERTY_UINT32_OR_LO
22226 && type <= GNU_PROPERTY_UINT32_OR_HI))
22227 {
22228 switch (type)
22229 {
22230 case GNU_PROPERTY_1_NEEDED:
22231 if (datasz != 4)
22232 printf (_("1_needed: <corrupt length: %#x> "),
22233 datasz);
22234 else
22235 {
22236 unsigned int bitmask = byte_get (ptr, 4);
22237 printf ("1_needed: ");
22238 decode_1_needed (bitmask);
22239 }
22240 goto next;
22241
22242 default:
22243 break;
22244 }
22245 if (type <= GNU_PROPERTY_UINT32_AND_HI)
22246 printf (_("UINT32_AND (%#x): "), type);
22247 else
22248 printf (_("UINT32_OR (%#x): "), type);
22249 if (datasz != 4)
22250 printf (_("<corrupt length: %#x> "), datasz);
22251 else
22252 printf ("%#x", (unsigned int) byte_get (ptr, 4));
22253 goto next;
22254 }
22255 break;
22256 }
22257 }
22258
22259 if (type < GNU_PROPERTY_LOPROC)
22260 printf (_("<unknown type %#x data: "), type);
22261 else if (type < GNU_PROPERTY_LOUSER)
22262 printf (_("<processor-specific type %#x data: "), type);
22263 else
22264 printf (_("<application-specific type %#x data: "), type);
22265 for (j = 0; j < datasz; ++j)
22266 printf ("%02x ", ptr[j] & 0xff);
22267 printf (">");
22268
22269 next:
22270 ptr += ((datasz + (size - 1)) & ~ (size - 1));
22271 if (ptr == ptr_end)
22272 break;
22273
22274 if (do_wide)
22275 printf (", ");
22276 else
22277 printf ("\n\t");
22278 }
22279
22280 printf ("\n");
22281}
22282
22283static bool
22284print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
22285{
22286 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
22287 switch (pnote->type)
22288 {
22289 case NT_GNU_BUILD_ID:
22290 {
22291 size_t i;
22292
22293 printf (_(" Build ID: "));
22294 for (i = 0; i < pnote->descsz; ++i)
22295 printf ("%02x", pnote->descdata[i] & 0xff);
22296 printf ("\n");
22297 }
22298 break;
22299
22300 case NT_GNU_ABI_TAG:
22301 {
22302 unsigned int os, major, minor, subminor;
22303 const char *osname;
22304
22305 /* PR 17531: file: 030-599401-0.004. */
22306 if (pnote->descsz < 16)
22307 {
22308 printf (_(" <corrupt GNU_ABI_TAG>\n"));
22309 break;
22310 }
22311
22312 os = byte_get ((unsigned char *) pnote->descdata, 4);
22313 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
22314 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
22315 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
22316
22317 switch (os)
22318 {
22319 case GNU_ABI_TAG_LINUX:
22320 osname = "Linux";
22321 break;
22322 case GNU_ABI_TAG_HURD:
22323 osname = "Hurd";
22324 break;
22325 case GNU_ABI_TAG_SOLARIS:
22326 osname = "Solaris";
22327 break;
22328 case GNU_ABI_TAG_FREEBSD:
22329 osname = "FreeBSD";
22330 break;
22331 case GNU_ABI_TAG_NETBSD:
22332 osname = "NetBSD";
22333 break;
22334 case GNU_ABI_TAG_SYLLABLE:
22335 osname = "Syllable";
22336 break;
22337 case GNU_ABI_TAG_NACL:
22338 osname = "NaCl";
22339 break;
22340 default:
22341 osname = "Unknown";
22342 break;
22343 }
22344
22345 printf (_(" OS: %s, ABI: %d.%d.%d\n"), osname,
22346 major, minor, subminor);
22347 }
22348 break;
22349
22350 case NT_GNU_GOLD_VERSION:
22351 {
22352 size_t i;
22353
22354 printf (_(" Version: "));
22355 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
22356 printf ("%c", pnote->descdata[i]);
22357 printf ("\n");
22358 }
22359 break;
22360
22361 case NT_GNU_HWCAP:
22362 {
22363 unsigned int num_entries, mask;
22364
22365 /* Hardware capabilities information. Word 0 is the number of entries.
22366 Word 1 is a bitmask of enabled entries. The rest of the descriptor
22367 is a series of entries, where each entry is a single byte followed
22368 by a nul terminated string. The byte gives the bit number to test
22369 if enabled in the bitmask. */
22370 printf (_(" Hardware Capabilities: "));
22371 if (pnote->descsz < 8)
22372 {
22373 error (_("<corrupt GNU_HWCAP>\n"));
22374 return false;
22375 }
22376 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
22377 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
22378 printf (_("num entries: %d, enabled mask: %x\n"), num_entries, mask);
22379 /* FIXME: Add code to display the entries... */
22380 }
22381 break;
22382
22383 case NT_GNU_PROPERTY_TYPE_0:
22384 print_gnu_property_note (filedata, pnote);
22385 break;
22386
22387 default:
22388 /* Handle unrecognised types. An error message should have already been
22389 created by get_gnu_elf_note_type(), so all that we need to do is to
22390 display the data. */
22391 {
22392 size_t i;
22393
22394 printf (_(" Description data: "));
22395 for (i = 0; i < pnote->descsz; ++i)
22396 printf ("%02x ", pnote->descdata[i] & 0xff);
22397 printf ("\n");
22398 }
22399 break;
22400 }
22401
22402 return true;
22403}
22404
22405static const char *
22406get_v850_elf_note_type (enum v850_notes n_type)
22407{
22408 static char buff[64];
22409
22410 switch (n_type)
22411 {
22412 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
22413 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
22414 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
22415 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
22416 case V850_NOTE_CACHE_INFO: return _("Use of cache");
22417 case V850_NOTE_MMU_INFO: return _("Use of MMU");
22418 default:
22419 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
22420 return buff;
22421 }
22422}
22423
22424static bool
22425print_v850_note (Elf_Internal_Note * pnote)
22426{
22427 unsigned int val;
22428
22429 printf (" %s: ", get_v850_elf_note_type (pnote->type));
22430
22431 if (pnote->descsz != 4)
22432 {
22433 printf ("<corrupt descsz: %#lx>\n", pnote->descsz);
22434 return false;
22435 }
22436
22437 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
22438
22439 if (val == 0)
22440 {
22441 printf (_("not set\n"));
22442 return true;
22443 }
22444
22445 switch (pnote->type)
22446 {
22447 case V850_NOTE_ALIGNMENT:
22448 switch (val)
22449 {
22450 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
22451 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
22452 }
22453 break;
22454
22455 case V850_NOTE_DATA_SIZE:
22456 switch (val)
22457 {
22458 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
22459 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
22460 }
22461 break;
22462
22463 case V850_NOTE_FPU_INFO:
22464 switch (val)
22465 {
22466 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
22467 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
22468 }
22469 break;
22470
22471 case V850_NOTE_MMU_INFO:
22472 case V850_NOTE_CACHE_INFO:
22473 case V850_NOTE_SIMD_INFO:
22474 if (val == EF_RH850_SIMD)
22475 {
22476 printf (_("yes\n"));
22477 return true;
22478 }
22479 break;
22480
22481 default:
22482 /* An 'unknown note type' message will already have been displayed. */
22483 break;
22484 }
22485
22486 printf (_("unknown value: %x\n"), val);
22487 return false;
22488}
22489
22490static bool
22491process_netbsd_elf_note (Elf_Internal_Note * pnote)
22492{
22493 unsigned int version;
22494
22495 switch (pnote->type)
22496 {
22497 case NT_NETBSD_IDENT:
22498 if (pnote->descsz < 1)
22499 break;
22500 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
22501 if ((version / 10000) % 100)
22502 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
22503 version, version / 100000000, (version / 1000000) % 100,
22504 (version / 10000) % 100 > 26 ? "Z" : "",
22505 'A' + (version / 10000) % 26);
22506 else
22507 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
22508 version, version / 100000000, (version / 1000000) % 100,
22509 (version / 100) % 100);
22510 return true;
22511
22512 case NT_NETBSD_MARCH:
22513 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
22514 pnote->descdata);
22515 return true;
22516
22517 case NT_NETBSD_PAX:
22518 if (pnote->descsz < 1)
22519 break;
22520 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
22521 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
22522 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
22523 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
22524 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
22525 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
22526 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
22527 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
22528 return true;
22529 }
22530
22531 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
22532 pnote->descsz, pnote->type);
22533 return false;
22534}
22535
22536static const char *
22537get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
22538{
22539 switch (e_type)
22540 {
22541 case NT_FREEBSD_THRMISC:
22542 return _("NT_THRMISC (thrmisc structure)");
22543 case NT_FREEBSD_PROCSTAT_PROC:
22544 return _("NT_PROCSTAT_PROC (proc data)");
22545 case NT_FREEBSD_PROCSTAT_FILES:
22546 return _("NT_PROCSTAT_FILES (files data)");
22547 case NT_FREEBSD_PROCSTAT_VMMAP:
22548 return _("NT_PROCSTAT_VMMAP (vmmap data)");
22549 case NT_FREEBSD_PROCSTAT_GROUPS:
22550 return _("NT_PROCSTAT_GROUPS (groups data)");
22551 case NT_FREEBSD_PROCSTAT_UMASK:
22552 return _("NT_PROCSTAT_UMASK (umask data)");
22553 case NT_FREEBSD_PROCSTAT_RLIMIT:
22554 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
22555 case NT_FREEBSD_PROCSTAT_OSREL:
22556 return _("NT_PROCSTAT_OSREL (osreldate data)");
22557 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
22558 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
22559 case NT_FREEBSD_PROCSTAT_AUXV:
22560 return _("NT_PROCSTAT_AUXV (auxv data)");
22561 case NT_FREEBSD_PTLWPINFO:
22562 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
22563 case NT_FREEBSD_X86_SEGBASES:
22564 return _("NT_X86_SEGBASES (x86 segment base registers)");
22565 }
22566 return get_note_type (filedata, e_type);
22567}
22568
22569static const char *
22570get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
22571{
22572 static char buff[64];
22573
22574 switch (e_type)
22575 {
22576 case NT_NETBSDCORE_PROCINFO:
22577 /* NetBSD core "procinfo" structure. */
22578 return _("NetBSD procinfo structure");
22579
22580 case NT_NETBSDCORE_AUXV:
22581 return _("NetBSD ELF auxiliary vector data");
22582
22583 case NT_NETBSDCORE_LWPSTATUS:
22584 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
22585
22586 default:
22587 /* As of Jan 2020 there are no other machine-independent notes
22588 defined for NetBSD core files. If the note type is less
22589 than the start of the machine-dependent note types, we don't
22590 understand it. */
22591
22592 if (e_type < NT_NETBSDCORE_FIRSTMACH)
22593 {
22594 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
22595 return buff;
22596 }
22597 break;
22598 }
22599
22600 switch (filedata->file_header.e_machine)
22601 {
22602 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
22603 and PT_GETFPREGS == mach+2. */
22604
22605 case EM_OLD_ALPHA:
22606 case EM_ALPHA:
22607 case EM_SPARC:
22608 case EM_SPARC32PLUS:
22609 case EM_SPARCV9:
22610 switch (e_type)
22611 {
22612 case NT_NETBSDCORE_FIRSTMACH + 0:
22613 return _("PT_GETREGS (reg structure)");
22614 case NT_NETBSDCORE_FIRSTMACH + 2:
22615 return _("PT_GETFPREGS (fpreg structure)");
22616 default:
22617 break;
22618 }
22619 break;
22620
22621 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
22622 There's also old PT___GETREGS40 == mach + 1 for old reg
22623 structure which lacks GBR. */
22624 case EM_SH:
22625 switch (e_type)
22626 {
22627 case NT_NETBSDCORE_FIRSTMACH + 1:
22628 return _("PT___GETREGS40 (old reg structure)");
22629 case NT_NETBSDCORE_FIRSTMACH + 3:
22630 return _("PT_GETREGS (reg structure)");
22631 case NT_NETBSDCORE_FIRSTMACH + 5:
22632 return _("PT_GETFPREGS (fpreg structure)");
22633 default:
22634 break;
22635 }
22636 break;
22637
22638 /* On all other arch's, PT_GETREGS == mach+1 and
22639 PT_GETFPREGS == mach+3. */
22640 default:
22641 switch (e_type)
22642 {
22643 case NT_NETBSDCORE_FIRSTMACH + 1:
22644 return _("PT_GETREGS (reg structure)");
22645 case NT_NETBSDCORE_FIRSTMACH + 3:
22646 return _("PT_GETFPREGS (fpreg structure)");
22647 default:
22648 break;
22649 }
22650 }
22651
22652 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
22653 e_type - NT_NETBSDCORE_FIRSTMACH);
22654 return buff;
22655}
22656
22657static const char *
22658get_openbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
22659{
22660 switch (e_type)
22661 {
22662 case NT_OPENBSD_PROCINFO:
22663 return _("OpenBSD procinfo structure");
22664 case NT_OPENBSD_AUXV:
22665 return _("OpenBSD ELF auxiliary vector data");
22666 case NT_OPENBSD_REGS:
22667 return _("OpenBSD regular registers");
22668 case NT_OPENBSD_FPREGS:
22669 return _("OpenBSD floating point registers");
22670 case NT_OPENBSD_WCOOKIE:
22671 return _("OpenBSD window cookie");
22672 }
22673
22674 return get_note_type (filedata, e_type);
22675}
22676
22677static const char *
22678get_qnx_elfcore_note_type (Filedata * filedata, unsigned e_type)
22679{
22680 switch (e_type)
22681 {
22682 case QNT_DEBUG_FULLPATH:
22683 return _("QNX debug fullpath");
22684 case QNT_DEBUG_RELOC:
22685 return _("QNX debug relocation");
22686 case QNT_STACK:
22687 return _("QNX stack");
22688 case QNT_GENERATOR:
22689 return _("QNX generator");
22690 case QNT_DEFAULT_LIB:
22691 return _("QNX default library");
22692 case QNT_CORE_SYSINFO:
22693 return _("QNX core sysinfo");
22694 case QNT_CORE_INFO:
22695 return _("QNX core info");
22696 case QNT_CORE_STATUS:
22697 return _("QNX core status");
22698 case QNT_CORE_GREG:
22699 return _("QNX general registers");
22700 case QNT_CORE_FPREG:
22701 return _("QNX floating point registers");
22702 case QNT_LINK_MAP:
22703 return _("QNX link map");
22704 }
22705
22706 return get_note_type (filedata, e_type);
22707}
22708
22709static const char *
22710get_stapsdt_note_type (unsigned e_type)
22711{
22712 static char buff[64];
22713
22714 switch (e_type)
22715 {
22716 case NT_STAPSDT:
22717 return _("NT_STAPSDT (SystemTap probe descriptors)");
22718
22719 default:
22720 break;
22721 }
22722
22723 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
22724 return buff;
22725}
22726
22727static bool
22728print_stapsdt_note (Elf_Internal_Note *pnote)
22729{
22730 size_t len, maxlen;
22731 size_t addr_size = is_32bit_elf ? 4 : 8;
22732 char *data = pnote->descdata;
22733 char *data_end = pnote->descdata + pnote->descsz;
22734 uint64_t pc, base_addr, semaphore;
22735 char *provider, *probe, *arg_fmt;
22736
22737 if (pnote->descsz < (addr_size * 3))
22738 goto stapdt_note_too_small;
22739
22740 pc = byte_get ((unsigned char *) data, addr_size);
22741 data += addr_size;
22742
22743 base_addr = byte_get ((unsigned char *) data, addr_size);
22744 data += addr_size;
22745
22746 semaphore = byte_get ((unsigned char *) data, addr_size);
22747 data += addr_size;
22748
22749 if (data >= data_end)
22750 goto stapdt_note_too_small;
22751 maxlen = data_end - data;
22752 len = strnlen (data, maxlen);
22753 if (len < maxlen)
22754 {
22755 provider = data;
22756 data += len + 1;
22757 }
22758 else
22759 goto stapdt_note_too_small;
22760
22761 if (data >= data_end)
22762 goto stapdt_note_too_small;
22763 maxlen = data_end - data;
22764 len = strnlen (data, maxlen);
22765 if (len < maxlen)
22766 {
22767 probe = data;
22768 data += len + 1;
22769 }
22770 else
22771 goto stapdt_note_too_small;
22772
22773 if (data >= data_end)
22774 goto stapdt_note_too_small;
22775 maxlen = data_end - data;
22776 len = strnlen (data, maxlen);
22777 if (len < maxlen)
22778 {
22779 arg_fmt = data;
22780 data += len + 1;
22781 }
22782 else
22783 goto stapdt_note_too_small;
22784
22785 printf (_(" Provider: %s\n"), provider);
22786 printf (_(" Name: %s\n"), probe);
22787 printf (_(" Location: "));
22788 print_vma (pc, FULL_HEX);
22789 printf (_(", Base: "));
22790 print_vma (base_addr, FULL_HEX);
22791 printf (_(", Semaphore: "));
22792 print_vma (semaphore, FULL_HEX);
22793 printf ("\n");
22794 printf (_(" Arguments: %s\n"), arg_fmt);
22795
22796 return data == data_end;
22797
22798 stapdt_note_too_small:
22799 printf (_(" <corrupt - note is too small>\n"));
22800 error (_("corrupt stapdt note - the data size is too small\n"));
22801 return false;
22802}
22803
22804static bool
22805print_fdo_note (Elf_Internal_Note * pnote)
22806{
22807 if (pnote->descsz > 0 && pnote->type == FDO_PACKAGING_METADATA)
22808 {
22809 printf (_(" Packaging Metadata: %.*s\n"), (int) pnote->descsz, pnote->descdata);
22810 return true;
22811 }
22812 if (pnote->descsz > 0 && pnote->type == FDO_DLOPEN_METADATA)
22813 {
22814 printf (_(" Dlopen Metadata: %.*s\n"), (int) pnote->descsz, pnote->descdata);
22815 return true;
22816 }
22817 return false;
22818}
22819
22820static const char *
22821get_ia64_vms_note_type (unsigned e_type)
22822{
22823 static char buff[64];
22824
22825 switch (e_type)
22826 {
22827 case NT_VMS_MHD:
22828 return _("NT_VMS_MHD (module header)");
22829 case NT_VMS_LNM:
22830 return _("NT_VMS_LNM (language name)");
22831 case NT_VMS_SRC:
22832 return _("NT_VMS_SRC (source files)");
22833 case NT_VMS_TITLE:
22834 return "NT_VMS_TITLE";
22835 case NT_VMS_EIDC:
22836 return _("NT_VMS_EIDC (consistency check)");
22837 case NT_VMS_FPMODE:
22838 return _("NT_VMS_FPMODE (FP mode)");
22839 case NT_VMS_LINKTIME:
22840 return "NT_VMS_LINKTIME";
22841 case NT_VMS_IMGNAM:
22842 return _("NT_VMS_IMGNAM (image name)");
22843 case NT_VMS_IMGID:
22844 return _("NT_VMS_IMGID (image id)");
22845 case NT_VMS_LINKID:
22846 return _("NT_VMS_LINKID (link id)");
22847 case NT_VMS_IMGBID:
22848 return _("NT_VMS_IMGBID (build id)");
22849 case NT_VMS_GSTNAM:
22850 return _("NT_VMS_GSTNAM (sym table name)");
22851 case NT_VMS_ORIG_DYN:
22852 return "NT_VMS_ORIG_DYN";
22853 case NT_VMS_PATCHTIME:
22854 return "NT_VMS_PATCHTIME";
22855 default:
22856 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
22857 return buff;
22858 }
22859}
22860
22861static bool
22862print_ia64_vms_note (Elf_Internal_Note * pnote)
22863{
22864 unsigned int maxlen = pnote->descsz;
22865
22866 if (maxlen < 2 || maxlen != pnote->descsz)
22867 goto desc_size_fail;
22868
22869 switch (pnote->type)
22870 {
22871 case NT_VMS_MHD:
22872 if (maxlen <= 36)
22873 goto desc_size_fail;
22874
22875 size_t l = strnlen (pnote->descdata + 34, maxlen - 34);
22876
22877 printf (_(" Creation date : %.17s\n"), pnote->descdata);
22878 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
22879 if (l + 34 < maxlen)
22880 {
22881 printf (_(" Module name : %s\n"), pnote->descdata + 34);
22882 if (l + 35 < maxlen)
22883 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
22884 else
22885 printf (_(" Module version : <missing>\n"));
22886 }
22887 else
22888 {
22889 printf (_(" Module name : <missing>\n"));
22890 printf (_(" Module version : <missing>\n"));
22891 }
22892 break;
22893
22894 case NT_VMS_LNM:
22895 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
22896 break;
22897
22898 case NT_VMS_FPMODE:
22899 printf (_(" Floating Point mode: "));
22900 if (maxlen < 8)
22901 goto desc_size_fail;
22902 /* FIXME: Generate an error if descsz > 8 ? */
22903
22904 printf ("0x%016" PRIx64 "\n",
22905 byte_get ((unsigned char *) pnote->descdata, 8));
22906 break;
22907
22908 case NT_VMS_LINKTIME:
22909 printf (_(" Link time: "));
22910 if (maxlen < 8)
22911 goto desc_size_fail;
22912 /* FIXME: Generate an error if descsz > 8 ? */
22913
22914 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
22915 printf ("\n");
22916 break;
22917
22918 case NT_VMS_PATCHTIME:
22919 printf (_(" Patch time: "));
22920 if (maxlen < 8)
22921 goto desc_size_fail;
22922 /* FIXME: Generate an error if descsz > 8 ? */
22923
22924 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
22925 printf ("\n");
22926 break;
22927
22928 case NT_VMS_ORIG_DYN:
22929 if (maxlen < 34)
22930 goto desc_size_fail;
22931
22932 printf (_(" Major id: %u, minor id: %u\n"),
22933 (unsigned) byte_get ((unsigned char *) pnote->descdata, 4),
22934 (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
22935 printf (_(" Last modified : "));
22936 print_vms_time (byte_get ((unsigned char *) pnote->descdata + 8, 8));
22937 printf (_("\n Link flags : "));
22938 printf ("0x%016" PRIx64 "\n",
22939 byte_get ((unsigned char *) pnote->descdata + 16, 8));
22940 printf (_(" Header flags: 0x%08x\n"),
22941 (unsigned) byte_get ((unsigned char *) pnote->descdata + 24, 4));
22942 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
22943 break;
22944
22945 case NT_VMS_IMGNAM:
22946 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
22947 break;
22948
22949 case NT_VMS_GSTNAM:
22950 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
22951 break;
22952
22953 case NT_VMS_IMGID:
22954 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
22955 break;
22956
22957 case NT_VMS_LINKID:
22958 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
22959 break;
22960
22961 default:
22962 return false;
22963 }
22964
22965 return true;
22966
22967 desc_size_fail:
22968 printf (_(" <corrupt - data size is too small>\n"));
22969 error (_("corrupt IA64 note: data size is too small\n"));
22970 return false;
22971}
22972
22973struct build_attr_cache {
22974 Filedata *filedata;
22975 char *strtab;
22976 uint64_t strtablen;
22977 Elf_Internal_Sym *symtab;
22978 uint64_t nsyms;
22979} ba_cache;
22980
22981/* Find the symbol associated with a build attribute that is attached
22982 to address OFFSET. If PNAME is non-NULL then store the name of
22983 the symbol (if found) in the provided pointer, Returns NULL if a
22984 symbol could not be found. */
22985
22986static Elf_Internal_Sym *
22987get_symbol_for_build_attribute (Filedata *filedata,
22988 uint64_t offset,
22989 bool is_open_attr,
22990 const char **pname)
22991{
22992 Elf_Internal_Sym *saved_sym = NULL;
22993 Elf_Internal_Sym *sym;
22994
22995 if (filedata->section_headers != NULL
22996 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
22997 {
22998 Elf_Internal_Shdr * symsec;
22999
23000 free (ba_cache.strtab);
23001 ba_cache.strtab = NULL;
23002 free (ba_cache.symtab);
23003 ba_cache.symtab = NULL;
23004
23005 /* Load the symbol and string sections. */
23006 for (symsec = filedata->section_headers;
23007 symsec < filedata->section_headers + filedata->file_header.e_shnum;
23008 symsec ++)
23009 {
23010 if (symsec->sh_type == SHT_SYMTAB
23011 && get_symtab (filedata, symsec,
23012 &ba_cache.symtab, &ba_cache.nsyms,
23013 &ba_cache.strtab, &ba_cache.strtablen))
23014 break;
23015 }
23016 ba_cache.filedata = filedata;
23017 }
23018
23019 if (ba_cache.symtab == NULL)
23020 return NULL;
23021
23022 /* Find a symbol whose value matches offset. */
23023 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
23024 if (sym->st_value == offset)
23025 {
23026 if (sym->st_name >= ba_cache.strtablen)
23027 /* Huh ? This should not happen. */
23028 continue;
23029
23030 if (ba_cache.strtab[sym->st_name] == 0)
23031 continue;
23032
23033 /* The AArch64, ARM and RISC-V architectures define mapping symbols
23034 (eg $d, $x, $t) which we want to ignore. */
23035 if (ba_cache.strtab[sym->st_name] == '$'
23036 && ba_cache.strtab[sym->st_name + 1] != 0
23037 && ba_cache.strtab[sym->st_name + 2] == 0)
23038 continue;
23039
23040 if (is_open_attr)
23041 {
23042 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
23043 and FILE or OBJECT symbols over NOTYPE symbols. We skip
23044 FUNC symbols entirely. */
23045 switch (ELF_ST_TYPE (sym->st_info))
23046 {
23047 case STT_OBJECT:
23048 case STT_FILE:
23049 saved_sym = sym;
23050 if (sym->st_size)
23051 {
23052 /* If the symbol has a size associated
23053 with it then we can stop searching. */
23054 sym = ba_cache.symtab + ba_cache.nsyms;
23055 }
23056 continue;
23057
23058 case STT_FUNC:
23059 /* Ignore function symbols. */
23060 continue;
23061
23062 default:
23063 break;
23064 }
23065
23066 switch (ELF_ST_BIND (sym->st_info))
23067 {
23068 case STB_GLOBAL:
23069 if (saved_sym == NULL
23070 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
23071 saved_sym = sym;
23072 break;
23073
23074 case STB_LOCAL:
23075 if (saved_sym == NULL)
23076 saved_sym = sym;
23077 break;
23078
23079 default:
23080 break;
23081 }
23082 }
23083 else
23084 {
23085 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
23086 continue;
23087
23088 saved_sym = sym;
23089 break;
23090 }
23091 }
23092
23093 if (saved_sym && pname)
23094 * pname = ba_cache.strtab + saved_sym->st_name;
23095
23096 return saved_sym;
23097}
23098
23099/* Returns true iff addr1 and addr2 are in the same section. */
23100
23101static bool
23102same_section (Filedata * filedata, uint64_t addr1, uint64_t addr2)
23103{
23104 Elf_Internal_Shdr * a1;
23105 Elf_Internal_Shdr * a2;
23106
23107 a1 = find_section_by_address (filedata, addr1);
23108 a2 = find_section_by_address (filedata, addr2);
23109
23110 return a1 == a2 && a1 != NULL;
23111}
23112
23113static bool
23114print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
23115 Filedata * filedata)
23116{
23117 static uint64_t global_offset = 0;
23118 static uint64_t global_end = 0;
23119 static uint64_t func_offset = 0;
23120 static uint64_t func_end = 0;
23121
23122 Elf_Internal_Sym *sym;
23123 const char *name;
23124 uint64_t start;
23125 uint64_t end;
23126 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
23127
23128 switch (pnote->descsz)
23129 {
23130 case 0:
23131 /* A zero-length description means that the range of
23132 the previous note of the same type should be used. */
23133 if (is_open_attr)
23134 {
23135 if (global_end > global_offset)
23136 printf (_(" Applies to region from %#" PRIx64
23137 " to %#" PRIx64 "\n"), global_offset, global_end);
23138 else
23139 printf (_(" Applies to region from %#" PRIx64
23140 "\n"), global_offset);
23141 }
23142 else
23143 {
23144 if (func_end > func_offset)
23145 printf (_(" Applies to region from %#" PRIx64
23146 " to %#" PRIx64 "\n"), func_offset, func_end);
23147 else
23148 printf (_(" Applies to region from %#" PRIx64
23149 "\n"), func_offset);
23150 }
23151 return true;
23152
23153 case 4:
23154 start = byte_get ((unsigned char *) pnote->descdata, 4);
23155 end = 0;
23156 break;
23157
23158 case 8:
23159 start = byte_get ((unsigned char *) pnote->descdata, 4);
23160 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
23161 break;
23162
23163 case 16:
23164 start = byte_get ((unsigned char *) pnote->descdata, 8);
23165 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
23166 break;
23167
23168 default:
23169 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
23170 printf (_(" <invalid descsz>"));
23171 return false;
23172 }
23173
23174 name = NULL;
23175 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
23176 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
23177 in order to avoid them being confused with the start address of the
23178 first function in the file... */
23179 if (sym == NULL && is_open_attr)
23180 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
23181 & name);
23182
23183 if (end == 0 && sym != NULL && sym->st_size > 0)
23184 end = start + sym->st_size;
23185
23186 if (is_open_attr)
23187 {
23188 /* FIXME: Need to properly allow for section alignment.
23189 16 is just the alignment used on x86_64. */
23190 if (global_end > 0
23191 && start > BFD_ALIGN (global_end, 16)
23192 /* Build notes are not guaranteed to be organised in order of
23193 increasing address, but we should find the all of the notes
23194 for one section in the same place. */
23195 && same_section (filedata, start, global_end))
23196 warn (_("Gap in build notes detected from %#" PRIx64
23197 " to %#" PRIx64 "\n"),
23198 global_end + 1, start - 1);
23199
23200 printf (_(" Applies to region from %#" PRIx64), start);
23201 global_offset = start;
23202
23203 if (end)
23204 {
23205 printf (_(" to %#" PRIx64), end);
23206 global_end = end;
23207 }
23208 }
23209 else
23210 {
23211 printf (_(" Applies to region from %#" PRIx64), start);
23212 func_offset = start;
23213
23214 if (end)
23215 {
23216 printf (_(" to %#" PRIx64), end);
23217 func_end = end;
23218 }
23219 }
23220
23221 if (sym && name)
23222 printf (_(" (%s)"), name);
23223
23224 printf ("\n");
23225 return true;
23226}
23227
23228static bool
23229print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
23230{
23231 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
23232 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
23233 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
23234 char name_type;
23235 char name_attribute;
23236 const char * expected_types;
23237 const char * name = pnote->namedata;
23238 const char * text;
23239 signed int left;
23240
23241 if (name == NULL || pnote->namesz < 2)
23242 {
23243 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
23244 print_symbol_name (-20, _(" <corrupt name>"));
23245 return false;
23246 }
23247
23248 if (do_wide)
23249 left = 28;
23250 else
23251 left = 20;
23252
23253 /* Version 2 of the spec adds a "GA" prefix to the name field. */
23254 if (name[0] == 'G' && name[1] == 'A')
23255 {
23256 if (pnote->namesz < 4)
23257 {
23258 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
23259 print_symbol_name (-20, _(" <corrupt name>"));
23260 return false;
23261 }
23262
23263 printf ("GA");
23264 name += 2;
23265 left -= 2;
23266 }
23267
23268 switch ((name_type = * name))
23269 {
23270 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
23271 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
23272 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
23273 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
23274 printf ("%c", * name);
23275 left --;
23276 break;
23277 default:
23278 error (_("unrecognised attribute type in name field: %d\n"), name_type);
23279 print_symbol_name (-20, _("<unknown name type>"));
23280 return false;
23281 }
23282
23283 ++ name;
23284 text = NULL;
23285
23286 switch ((name_attribute = * name))
23287 {
23288 case GNU_BUILD_ATTRIBUTE_VERSION:
23289 text = _("<version>");
23290 expected_types = string_expected;
23291 ++ name;
23292 break;
23293 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
23294 text = _("<stack prot>");
23295 expected_types = "!+*";
23296 ++ name;
23297 break;
23298 case GNU_BUILD_ATTRIBUTE_RELRO:
23299 text = _("<relro>");
23300 expected_types = bool_expected;
23301 ++ name;
23302 break;
23303 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
23304 text = _("<stack size>");
23305 expected_types = number_expected;
23306 ++ name;
23307 break;
23308 case GNU_BUILD_ATTRIBUTE_TOOL:
23309 text = _("<tool>");
23310 expected_types = string_expected;
23311 ++ name;
23312 break;
23313 case GNU_BUILD_ATTRIBUTE_ABI:
23314 text = _("<ABI>");
23315 expected_types = "$*";
23316 ++ name;
23317 break;
23318 case GNU_BUILD_ATTRIBUTE_PIC:
23319 text = _("<PIC>");
23320 expected_types = number_expected;
23321 ++ name;
23322 break;
23323 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
23324 text = _("<short enum>");
23325 expected_types = bool_expected;
23326 ++ name;
23327 break;
23328 default:
23329 if (ISPRINT (* name))
23330 {
23331 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
23332
23333 if (len > left && ! do_wide)
23334 len = left;
23335 printf ("%.*s:", len, name);
23336 left -= len;
23337 name += len;
23338 }
23339 else
23340 {
23341 static char tmpbuf [128];
23342
23343 error (_("unrecognised byte in name field: %d\n"), * name);
23344 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
23345 text = tmpbuf;
23346 name ++;
23347 }
23348 expected_types = "*$!+";
23349 break;
23350 }
23351
23352 if (text)
23353 left -= printf ("%s", text);
23354
23355 if (strchr (expected_types, name_type) == NULL)
23356 warn (_("attribute does not have an expected type (%c)\n"), name_type);
23357
23358 if ((size_t) (name - pnote->namedata) > pnote->namesz)
23359 {
23360 error (_("corrupt name field: namesz: %lu but parsing gets to %td\n"),
23361 pnote->namesz,
23362 name - pnote->namedata);
23363 return false;
23364 }
23365
23366 if (left < 1 && ! do_wide)
23367 return true;
23368
23369 switch (name_type)
23370 {
23371 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
23372 {
23373 unsigned int bytes;
23374 uint64_t val = 0;
23375 unsigned int shift = 0;
23376 char *decoded = NULL;
23377
23378 bytes = pnote->namesz - (name - pnote->namedata);
23379 if (bytes > 0)
23380 /* The -1 is because the name field is always 0 terminated, and we
23381 want to be able to ensure that the shift in the while loop below
23382 will not overflow. */
23383 -- bytes;
23384
23385 if (bytes > sizeof (val))
23386 {
23387 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
23388 bytes);
23389 bytes = sizeof (val);
23390 }
23391 /* We do not bother to warn if bytes == 0 as this can
23392 happen with some early versions of the gcc plugin. */
23393
23394 while (bytes --)
23395 {
23396 uint64_t byte = *name++ & 0xff;
23397
23398 val |= byte << shift;
23399 shift += 8;
23400 }
23401
23402 switch (name_attribute)
23403 {
23404 case GNU_BUILD_ATTRIBUTE_PIC:
23405 switch (val)
23406 {
23407 case 0: decoded = "static"; break;
23408 case 1: decoded = "pic"; break;
23409 case 2: decoded = "PIC"; break;
23410 case 3: decoded = "pie"; break;
23411 case 4: decoded = "PIE"; break;
23412 default: break;
23413 }
23414 break;
23415 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
23416 switch (val)
23417 {
23418 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
23419 case 0: decoded = "off"; break;
23420 case 1: decoded = "on"; break;
23421 case 2: decoded = "all"; break;
23422 case 3: decoded = "strong"; break;
23423 case 4: decoded = "explicit"; break;
23424 default: break;
23425 }
23426 break;
23427 default:
23428 break;
23429 }
23430
23431 if (decoded != NULL)
23432 {
23433 print_symbol_name (-left, decoded);
23434 left = 0;
23435 }
23436 else if (val == 0)
23437 {
23438 printf ("0x0");
23439 left -= 3;
23440 }
23441 else
23442 {
23443 if (do_wide)
23444 left -= printf ("0x%" PRIx64, val);
23445 else
23446 left -= printf ("0x%-.*" PRIx64, left, val);
23447 }
23448 }
23449 break;
23450 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
23451 left -= print_symbol_name (- left, name);
23452 break;
23453 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
23454 left -= print_symbol_name (- left, "true");
23455 break;
23456 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
23457 left -= print_symbol_name (- left, "false");
23458 break;
23459 }
23460
23461 if (do_wide && left > 0)
23462 printf ("%-*s", left, " ");
23463
23464 return true;
23465}
23466
23467/* Print the contents of PNOTE as hex. */
23468
23469static void
23470print_note_contents_hex (Elf_Internal_Note *pnote)
23471{
23472 if (pnote->descsz)
23473 {
23474 size_t i;
23475
23476 printf (_(" description data: "));
23477 for (i = 0; i < pnote->descsz; i++)
23478 printf ("%02x ", pnote->descdata[i] & 0xff);
23479 if (!do_wide)
23480 printf ("\n");
23481 }
23482
23483 if (do_wide)
23484 printf ("\n");
23485}
23486
23487#if defined HAVE_MSGPACK
23488
23489static void
23490print_indents (int n)
23491{
23492 printf (" ");
23493
23494 for (int i = 0; i < n; i++)
23495 printf (" ");
23496}
23497
23498/* Print OBJ in human-readable form. */
23499
23500static void
23501dump_msgpack_obj (const msgpack_object *obj, int indent)
23502{
23503 switch (obj->type)
23504 {
23505 case MSGPACK_OBJECT_NIL:
23506 printf ("(nil)");
23507 break;
23508
23509 case MSGPACK_OBJECT_BOOLEAN:
23510 printf ("%s", obj->via.boolean ? "true" : "false");
23511 break;
23512
23513 case MSGPACK_OBJECT_POSITIVE_INTEGER:
23514 printf ("%" PRIu64, obj->via.u64);
23515 break;
23516
23517 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
23518 printf ("%" PRIi64, obj->via.i64);
23519 break;
23520
23521 case MSGPACK_OBJECT_FLOAT32:
23522 case MSGPACK_OBJECT_FLOAT64:
23523 printf ("%f", obj->via.f64);
23524 break;
23525
23526 case MSGPACK_OBJECT_STR:
23527 printf ("\"%.*s\"", obj->via.str.size, obj->via.str.ptr);
23528 break;
23529
23530 case MSGPACK_OBJECT_ARRAY:
23531 {
23532 const msgpack_object_array *array = &obj->via.array;
23533
23534 printf ("[\n");
23535 ++indent;
23536
23537 for (uint32_t i = 0; i < array->size; ++i)
23538 {
23539 const msgpack_object *item = &array->ptr[i];
23540
23541 print_indents (indent);
23542 dump_msgpack_obj (item, indent);
23543 printf (",\n");
23544 }
23545
23546 --indent;
23547 print_indents (indent);
23548 printf ("]");
23549 break;
23550 }
23551 break;
23552
23553 case MSGPACK_OBJECT_MAP:
23554 {
23555 const msgpack_object_map *map = &obj->via.map;
23556
23557 printf ("{\n");
23558 ++indent;
23559
23560 for (uint32_t i = 0; i < map->size; ++i)
23561 {
23562 const msgpack_object_kv *kv = &map->ptr[i];
23563 const msgpack_object *key = &kv->key;
23564 const msgpack_object *val = &kv->val;
23565
23566 print_indents (indent);
23567 dump_msgpack_obj (key, indent);
23568 printf (": ");
23569 dump_msgpack_obj (val, indent);
23570
23571 printf (",\n");
23572 }
23573
23574 --indent;
23575 print_indents (indent);
23576 printf ("}");
23577
23578 break;
23579 }
23580
23581 case MSGPACK_OBJECT_BIN:
23582 printf ("(bin)");
23583 break;
23584
23585 case MSGPACK_OBJECT_EXT:
23586 printf ("(ext)");
23587 break;
23588 }
23589}
23590
23591static void
23592dump_msgpack (const msgpack_unpacked *msg)
23593{
23594 print_indents (0);
23595 dump_msgpack_obj (&msg->data, 0);
23596 printf ("\n");
23597}
23598
23599#endif /* defined HAVE_MSGPACK */
23600
23601static bool
23602print_amdgpu_note (Elf_Internal_Note *pnote)
23603{
23604#if defined HAVE_MSGPACK
23605 /* If msgpack is available, decode and dump the note's content. */
23606 bool ret;
23607 msgpack_unpacked msg;
23608 msgpack_unpack_return msgpack_ret;
23609
23610 assert (pnote->type == NT_AMDGPU_METADATA);
23611
23612 msgpack_unpacked_init (&msg);
23613 msgpack_ret = msgpack_unpack_next (&msg, pnote->descdata, pnote->descsz,
23614 NULL);
23615
23616 switch (msgpack_ret)
23617 {
23618 case MSGPACK_UNPACK_SUCCESS:
23619 dump_msgpack (&msg);
23620 ret = true;
23621 break;
23622
23623 default:
23624 error (_("failed to unpack msgpack contents in NT_AMDGPU_METADATA note"));
23625 ret = false;
23626 break;
23627 }
23628
23629 msgpack_unpacked_destroy (&msg);
23630 return ret;
23631#else
23632 /* msgpack is not available, dump contents as hex. */
23633 print_note_contents_hex (pnote);
23634 return true;
23635#endif
23636}
23637
23638static bool
23639print_qnx_note (Elf_Internal_Note *pnote)
23640{
23641 switch (pnote->type)
23642 {
23643 case QNT_STACK:
23644 if (pnote->descsz != 12)
23645 goto desc_size_fail;
23646
23647 printf (_(" Stack Size: 0x%" PRIx32 "\n"),
23648 (unsigned) byte_get ((unsigned char *) pnote->descdata, 4));
23649 printf (_(" Stack allocated: %" PRIx32 "\n"),
23650 (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
23651 printf (_(" Executable: %s\n"),
23652 ((unsigned) byte_get ((unsigned char *) pnote->descdata + 8, 1)) ? "no": "yes");
23653 break;
23654
23655 default:
23656 print_note_contents_hex(pnote);
23657 }
23658 return true;
23659
23660desc_size_fail:
23661 printf (_(" <corrupt - data size is too small>\n"));
23662 error (_("corrupt QNX note: data size is too small\n"));
23663 return false;
23664}
23665
23666
23667/* Note that by the ELF standard, the name field is already null byte
23668 terminated, and namesz includes the terminating null byte.
23669 I.E. the value of namesz for the name "FSF" is 4.
23670
23671 If the value of namesz is zero, there is no name present. */
23672
23673static bool
23674process_note (Elf_Internal_Note * pnote,
23675 Filedata * filedata)
23676{
23677 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
23678 const char * nt;
23679
23680 if (pnote->namesz == 0)
23681 /* If there is no note name, then use the default set of
23682 note type strings. */
23683 nt = get_note_type (filedata, pnote->type);
23684
23685 else if (startswith (pnote->namedata, "GNU"))
23686 /* GNU-specific object file notes. */
23687 nt = get_gnu_elf_note_type (pnote->type);
23688
23689 else if (startswith (pnote->namedata, "AMDGPU"))
23690 /* AMDGPU-specific object file notes. */
23691 nt = get_amdgpu_elf_note_type (pnote->type);
23692
23693 else if (startswith (pnote->namedata, "FreeBSD"))
23694 /* FreeBSD-specific core file notes. */
23695 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
23696
23697 else if (startswith (pnote->namedata, "NetBSD-CORE"))
23698 /* NetBSD-specific core file notes. */
23699 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
23700
23701 else if (startswith (pnote->namedata, "NetBSD"))
23702 /* NetBSD-specific core file notes. */
23703 return process_netbsd_elf_note (pnote);
23704
23705 else if (startswith (pnote->namedata, "PaX"))
23706 /* NetBSD-specific core file notes. */
23707 return process_netbsd_elf_note (pnote);
23708
23709 else if (startswith (pnote->namedata, "OpenBSD"))
23710 /* OpenBSD-specific core file notes. */
23711 nt = get_openbsd_elfcore_note_type (filedata, pnote->type);
23712
23713 else if (startswith (pnote->namedata, "QNX"))
23714 /* QNX-specific core file notes. */
23715 nt = get_qnx_elfcore_note_type (filedata, pnote->type);
23716
23717 else if (startswith (pnote->namedata, "SPU/"))
23718 {
23719 /* SPU-specific core file notes. */
23720 nt = pnote->namedata + 4;
23721 name = "SPU";
23722 }
23723
23724 else if (startswith (pnote->namedata, "IPF/VMS"))
23725 /* VMS/ia64-specific file notes. */
23726 nt = get_ia64_vms_note_type (pnote->type);
23727
23728 else if (startswith (pnote->namedata, "stapsdt"))
23729 nt = get_stapsdt_note_type (pnote->type);
23730
23731 else
23732 /* Don't recognize this note name; just use the default set of
23733 note type strings. */
23734 nt = get_note_type (filedata, pnote->type);
23735
23736 printf (" ");
23737
23738 if (((startswith (pnote->namedata, "GA")
23739 && strchr ("*$!+", pnote->namedata[2]) != NULL)
23740 || strchr ("*$!+", pnote->namedata[0]) != NULL)
23741 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
23742 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
23743 print_gnu_build_attribute_name (pnote);
23744 else
23745 print_symbol_name (-20, name);
23746
23747 if (do_wide)
23748 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
23749 else
23750 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
23751
23752 if (startswith (pnote->namedata, "IPF/VMS"))
23753 return print_ia64_vms_note (pnote);
23754 else if (startswith (pnote->namedata, "GNU"))
23755 return print_gnu_note (filedata, pnote);
23756 else if (startswith (pnote->namedata, "stapsdt"))
23757 return print_stapsdt_note (pnote);
23758 else if (startswith (pnote->namedata, "CORE"))
23759 return print_core_note (pnote);
23760 else if (startswith (pnote->namedata, "FDO"))
23761 return print_fdo_note (pnote);
23762 else if (((startswith (pnote->namedata, "GA")
23763 && strchr ("*$!+", pnote->namedata[2]) != NULL)
23764 || strchr ("*$!+", pnote->namedata[0]) != NULL)
23765 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
23766 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
23767 return print_gnu_build_attribute_description (pnote, filedata);
23768 else if (startswith (pnote->namedata, "AMDGPU")
23769 && pnote->type == NT_AMDGPU_METADATA)
23770 return print_amdgpu_note (pnote);
23771 else if (startswith (pnote->namedata, "QNX"))
23772 return print_qnx_note (pnote);
23773
23774 print_note_contents_hex (pnote);
23775 return true;
23776}
23777
23778static bool
23779process_notes_at (Filedata * filedata,
23780 Elf_Internal_Shdr * section,
23781 uint64_t offset,
23782 uint64_t length,
23783 uint64_t align)
23784{
23785 Elf_External_Note *pnotes;
23786 Elf_External_Note *external;
23787 char *end;
23788 bool res = true;
23789
23790 if (length <= 0)
23791 return false;
23792
23793 if (section)
23794 {
23795 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
23796 if (pnotes)
23797 {
23798 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
23799 {
23800 free (pnotes);
23801 return false;
23802 }
23803 }
23804 }
23805 else
23806 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
23807 _("notes"));
23808
23809 if (pnotes == NULL)
23810 return false;
23811
23812 external = pnotes;
23813
23814 if (filedata->is_separate)
23815 printf (_("In linked file '%s': "), filedata->file_name);
23816 else
23817 printf ("\n");
23818 if (section)
23819 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
23820 else
23821 printf (_("Displaying notes found at file offset 0x%08" PRIx64
23822 " with length 0x%08" PRIx64 ":\n"),
23823 offset, length);
23824
23825 /* NB: Some note sections may have alignment value of 0 or 1. gABI
23826 specifies that notes should be aligned to 4 bytes in 32-bit
23827 objects and to 8 bytes in 64-bit objects. As a Linux extension,
23828 we also support 4 byte alignment in 64-bit objects. If section
23829 alignment is less than 4, we treate alignment as 4 bytes. */
23830 if (align < 4)
23831 align = 4;
23832 else if (align != 4 && align != 8)
23833 {
23834 warn (_("Corrupt note: alignment %" PRId64 ", expecting 4 or 8\n"),
23835 align);
23836 free (pnotes);
23837 return false;
23838 }
23839
23840 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
23841
23842 end = (char *) pnotes + length;
23843 while ((char *) external < end)
23844 {
23845 Elf_Internal_Note inote;
23846 size_t min_notesz;
23847 char * next;
23848 char * temp = NULL;
23849 size_t data_remaining = end - (char *) external;
23850
23851 if (!is_ia64_vms (filedata))
23852 {
23853 /* PR binutils/15191
23854 Make sure that there is enough data to read. */
23855 min_notesz = offsetof (Elf_External_Note, name);
23856 if (data_remaining < min_notesz)
23857 {
23858 warn (ngettext ("Corrupt note: only %zd byte remains, "
23859 "not enough for a full note\n",
23860 "Corrupt note: only %zd bytes remain, "
23861 "not enough for a full note\n",
23862 data_remaining),
23863 data_remaining);
23864 break;
23865 }
23866 data_remaining -= min_notesz;
23867
23868 inote.type = BYTE_GET (external->type);
23869 inote.namesz = BYTE_GET (external->namesz);
23870 inote.namedata = external->name;
23871 inote.descsz = BYTE_GET (external->descsz);
23872 inote.descdata = ((char *) external
23873 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
23874 inote.descpos = offset + (inote.descdata - (char *) pnotes);
23875 next = ((char *) external
23876 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
23877 }
23878 else
23879 {
23880 Elf64_External_VMS_Note *vms_external;
23881
23882 /* PR binutils/15191
23883 Make sure that there is enough data to read. */
23884 min_notesz = offsetof (Elf64_External_VMS_Note, name);
23885 if (data_remaining < min_notesz)
23886 {
23887 warn (ngettext ("Corrupt note: only %zd byte remains, "
23888 "not enough for a full note\n",
23889 "Corrupt note: only %zd bytes remain, "
23890 "not enough for a full note\n",
23891 data_remaining),
23892 data_remaining);
23893 break;
23894 }
23895 data_remaining -= min_notesz;
23896
23897 vms_external = (Elf64_External_VMS_Note *) external;
23898 inote.type = BYTE_GET (vms_external->type);
23899 inote.namesz = BYTE_GET (vms_external->namesz);
23900 inote.namedata = vms_external->name;
23901 inote.descsz = BYTE_GET (vms_external->descsz);
23902 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
23903 inote.descpos = offset + (inote.descdata - (char *) pnotes);
23904 next = inote.descdata + align_power (inote.descsz, 3);
23905 }
23906
23907 /* PR 17531: file: 3443835e. */
23908 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
23909 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
23910 || (size_t) (inote.descdata - inote.namedata) > data_remaining
23911 || (size_t) (next - inote.descdata) < inote.descsz
23912 || ((size_t) (next - inote.descdata)
23913 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
23914 {
23915 warn (_("note with invalid namesz and/or descsz found at offset %#tx\n"),
23916 (char *) external - (char *) pnotes);
23917 warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx, alignment: %u\n"),
23918 inote.type, inote.namesz, inote.descsz, (int) align);
23919 break;
23920 }
23921
23922 external = (Elf_External_Note *) next;
23923
23924 /* Verify that name is null terminated. It appears that at least
23925 one version of Linux (RedHat 6.0) generates corefiles that don't
23926 comply with the ELF spec by failing to include the null byte in
23927 namesz. */
23928 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
23929 {
23930 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
23931 {
23932 temp = (char *) malloc (inote.namesz + 1);
23933 if (temp == NULL)
23934 {
23935 error (_("Out of memory allocating space for inote name\n"));
23936 res = false;
23937 break;
23938 }
23939
23940 memcpy (temp, inote.namedata, inote.namesz);
23941 inote.namedata = temp;
23942 }
23943 inote.namedata[inote.namesz] = 0;
23944 }
23945
23946 if (! process_note (& inote, filedata))
23947 res = false;
23948
23949 free (temp);
23950 temp = NULL;
23951 }
23952
23953 free (pnotes);
23954
23955 return res;
23956}
23957
23958static bool
23959process_corefile_note_segments (Filedata * filedata)
23960{
23961 Elf_Internal_Phdr *segment;
23962 unsigned int i;
23963 bool res = true;
23964
23965 if (! get_program_headers (filedata))
23966 return true;
23967
23968 for (i = 0, segment = filedata->program_headers;
23969 i < filedata->file_header.e_phnum;
23970 i++, segment++)
23971 {
23972 if (segment->p_type == PT_NOTE)
23973 if (! process_notes_at (filedata, NULL, segment->p_offset,
23974 segment->p_filesz, segment->p_align))
23975 res = false;
23976 }
23977
23978 return res;
23979}
23980
23981static bool
23982process_v850_notes (Filedata * filedata, uint64_t offset, uint64_t length)
23983{
23984 Elf_External_Note * pnotes;
23985 Elf_External_Note * external;
23986 char * end;
23987 bool res = true;
23988
23989 if (length <= 0)
23990 return false;
23991
23992 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
23993 _("v850 notes"));
23994 if (pnotes == NULL)
23995 return false;
23996
23997 external = pnotes;
23998 end = (char*) pnotes + length;
23999
24000 printf (_("\nDisplaying contents of Renesas V850 notes section at offset"
24001 " %#" PRIx64 " with length %#" PRIx64 ":\n"),
24002 offset, length);
24003
24004 while ((char *) external < end)
24005 {
24006 char *next;
24007 Elf_Internal_Note inote;
24008 size_t data_remaining = end - (char *) external;
24009
24010 if (data_remaining < offsetof (Elf_External_Note, name))
24011 break;
24012 data_remaining -= offsetof (Elf_External_Note, name);
24013
24014 inote.type = BYTE_GET (external->type);
24015 inote.namesz = BYTE_GET (external->namesz);
24016 inote.namedata = external->name;
24017 inote.descsz = BYTE_GET (external->descsz);
24018 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
24019 inote.descpos = offset + (inote.descdata - (char *) pnotes);
24020 next = inote.descdata + align_power (inote.descsz, 2);
24021
24022 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
24023 || (size_t) (inote.descdata - inote.namedata) > data_remaining
24024 || (size_t) (next - inote.descdata) < inote.descsz
24025 || ((size_t) (next - inote.descdata)
24026 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
24027 {
24028 warn (_("note with invalid namesz and/or descsz found at offset %#tx\n"),
24029 (char *) external - (char *) pnotes);
24030 warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx, alignment: %u\n"),
24031 inote.type, inote.namesz, inote.descsz, 2);
24032 break;
24033 }
24034
24035 external = (Elf_External_Note *) next;
24036
24037 if (! print_v850_note (& inote))
24038 res = false;
24039 }
24040
24041 free (pnotes);
24042
24043 return res;
24044}
24045
24046static bool
24047process_note_sections (Filedata * filedata)
24048{
24049 Elf_Internal_Shdr *section;
24050 size_t i;
24051 unsigned int n = 0;
24052 bool res = true;
24053
24054 for (i = 0, section = filedata->section_headers;
24055 i < filedata->file_header.e_shnum && section != NULL;
24056 i++, section++)
24057 {
24058 if (section->sh_type == SHT_NOTE)
24059 {
24060 if (! process_notes_at (filedata, section, section->sh_offset,
24061 section->sh_size, section->sh_addralign))
24062 res = false;
24063 n++;
24064 }
24065
24066 if (( filedata->file_header.e_machine == EM_V800
24067 || filedata->file_header.e_machine == EM_V850
24068 || filedata->file_header.e_machine == EM_CYGNUS_V850)
24069 && section->sh_type == SHT_RENESAS_INFO)
24070 {
24071 if (! process_v850_notes (filedata, section->sh_offset,
24072 section->sh_size))
24073 res = false;
24074 n++;
24075 }
24076 }
24077
24078 if (n == 0)
24079 /* Try processing NOTE segments instead. */
24080 return process_corefile_note_segments (filedata);
24081
24082 return res;
24083}
24084
24085static bool
24086process_notes (Filedata * filedata)
24087{
24088 /* If we have not been asked to display the notes then do nothing. */
24089 if (! do_notes)
24090 return true;
24091
24092 if (filedata->file_header.e_type != ET_CORE)
24093 return process_note_sections (filedata);
24094
24095 /* No program headers means no NOTE segment. */
24096 if (filedata->file_header.e_phnum > 0)
24097 return process_corefile_note_segments (filedata);
24098
24099 if (filedata->is_separate)
24100 printf (_("No notes found in linked file '%s'.\n"),
24101 filedata->file_name);
24102 else
24103 printf (_("No notes found file.\n"));
24104
24105 return true;
24106}
24107
24108static unsigned char *
24109display_public_gnu_attributes (unsigned char * start,
24110 const unsigned char * const end)
24111{
24112 printf (_(" Unknown GNU attribute: %s\n"), start);
24113
24114 start += strnlen ((char *) start, end - start);
24115 display_raw_attribute (start, end);
24116
24117 return (unsigned char *) end;
24118}
24119
24120static unsigned char *
24121display_generic_attribute (unsigned char * start,
24122 unsigned int tag,
24123 const unsigned char * const end)
24124{
24125 if (tag == 0)
24126 return (unsigned char *) end;
24127
24128 return display_tag_value (tag, start, end);
24129}
24130
24131static bool
24132process_arch_specific (Filedata * filedata)
24133{
24134 if (! do_arch)
24135 return true;
24136
24137 switch (filedata->file_header.e_machine)
24138 {
24139 case EM_ARC:
24140 case EM_ARC_COMPACT:
24141 case EM_ARC_COMPACT2:
24142 case EM_ARC_COMPACT3:
24143 case EM_ARC_COMPACT3_64:
24144 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
24145 display_arc_attribute,
24146 display_generic_attribute);
24147 case EM_ARM:
24148 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
24149 display_arm_attribute,
24150 display_generic_attribute);
24151
24152 case EM_MIPS:
24153 case EM_MIPS_RS3_LE:
24154 return process_mips_specific (filedata, false);
24155
24156 case EM_MSP430:
24157 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
24158 display_msp430_attribute,
24159 display_msp430_gnu_attribute);
24160
24161 case EM_RISCV:
24162 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
24163 display_riscv_attribute,
24164 display_generic_attribute);
24165
24166 case EM_NDS32:
24167 return process_nds32_specific (filedata);
24168
24169 case EM_68K:
24170 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
24171 display_m68k_gnu_attribute);
24172
24173 case EM_PPC:
24174 case EM_PPC64:
24175 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
24176 display_power_gnu_attribute);
24177
24178 case EM_S390:
24179 case EM_S390_OLD:
24180 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
24181 display_s390_gnu_attribute);
24182
24183 case EM_SPARC:
24184 case EM_SPARC32PLUS:
24185 case EM_SPARCV9:
24186 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
24187 display_sparc_gnu_attribute);
24188
24189 case EM_TI_C6000:
24190 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
24191 display_tic6x_attribute,
24192 display_generic_attribute);
24193
24194 case EM_CSKY:
24195 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
24196 display_csky_attribute, NULL);
24197
24198 default:
24199 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
24200 display_public_gnu_attributes,
24201 display_generic_attribute);
24202 }
24203}
24204
24205static bool
24206get_file_header (Filedata * filedata)
24207{
24208 /* Read in the identity array. */
24209 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
24210 return false;
24211
24212 /* Determine how to read the rest of the header. */
24213 switch (filedata->file_header.e_ident[EI_DATA])
24214 {
24215 default:
24216 case ELFDATANONE:
24217 case ELFDATA2LSB:
24218 byte_get = byte_get_little_endian;
24219 byte_put = byte_put_little_endian;
24220 break;
24221 case ELFDATA2MSB:
24222 byte_get = byte_get_big_endian;
24223 byte_put = byte_put_big_endian;
24224 break;
24225 }
24226
24227 /* For now we only support 32 bit and 64 bit ELF files. */
24228 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
24229
24230 /* Read in the rest of the header. */
24231 if (is_32bit_elf)
24232 {
24233 Elf32_External_Ehdr ehdr32;
24234
24235 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
24236 return false;
24237
24238 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
24239 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
24240 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
24241 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
24242 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
24243 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
24244 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
24245 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
24246 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
24247 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
24248 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
24249 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
24250 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
24251 }
24252 else
24253 {
24254 Elf64_External_Ehdr ehdr64;
24255
24256 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
24257 return false;
24258
24259 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
24260 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
24261 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
24262 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
24263 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
24264 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
24265 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
24266 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
24267 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
24268 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
24269 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
24270 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
24271 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
24272 }
24273
24274 return true;
24275}
24276
24277static void
24278free_filedata (Filedata *filedata)
24279{
24280 free (filedata->program_interpreter);
24281 free (filedata->program_headers);
24282 free (filedata->section_headers);
24283 free (filedata->string_table);
24284 free (filedata->dump.dump_sects);
24285 free (filedata->dynamic_strings);
24286 free (filedata->dynamic_symbols);
24287 free (filedata->dynamic_syminfo);
24288 free (filedata->dynamic_section);
24289
24290 while (filedata->symtab_shndx_list != NULL)
24291 {
24292 elf_section_list *next = filedata->symtab_shndx_list->next;
24293 free (filedata->symtab_shndx_list);
24294 filedata->symtab_shndx_list = next;
24295 }
24296
24297 free (filedata->section_headers_groups);
24298
24299 if (filedata->section_groups)
24300 {
24301 size_t i;
24302 struct group_list * g;
24303 struct group_list * next;
24304
24305 for (i = 0; i < filedata->group_count; i++)
24306 {
24307 for (g = filedata->section_groups [i].root; g != NULL; g = next)
24308 {
24309 next = g->next;
24310 free (g);
24311 }
24312 }
24313
24314 free (filedata->section_groups);
24315 }
24316 memset (&filedata->section_headers, 0,
24317 sizeof (Filedata) - offsetof (Filedata, section_headers));
24318}
24319
24320static void
24321close_file (Filedata * filedata)
24322{
24323 if (filedata)
24324 {
24325 if (filedata->handle)
24326 fclose (filedata->handle);
24327 free (filedata);
24328 }
24329}
24330
24331void
24332close_debug_file (void * data)
24333{
24334 free_filedata ((Filedata *) data);
24335 close_file ((Filedata *) data);
24336}
24337
24338static Filedata *
24339open_file (const char * pathname, bool is_separate)
24340{
24341 struct stat statbuf;
24342 Filedata * filedata = NULL;
24343
24344 if (stat (pathname, & statbuf) < 0
24345 || ! S_ISREG (statbuf.st_mode))
24346 goto fail;
24347
24348 filedata = calloc (1, sizeof * filedata);
24349 if (filedata == NULL)
24350 goto fail;
24351
24352 filedata->handle = fopen (pathname, "rb");
24353 if (filedata->handle == NULL)
24354 goto fail;
24355
24356 filedata->file_size = statbuf.st_size;
24357 filedata->file_name = pathname;
24358 filedata->is_separate = is_separate;
24359
24360 if (! get_file_header (filedata))
24361 goto fail;
24362
24363 if (!get_section_headers (filedata, false))
24364 goto fail;
24365
24366 return filedata;
24367
24368 fail:
24369 if (filedata)
24370 {
24371 if (filedata->handle)
24372 fclose (filedata->handle);
24373 free (filedata);
24374 }
24375 return NULL;
24376}
24377
24378void *
24379open_debug_file (const char * pathname)
24380{
24381 return open_file (pathname, true);
24382}
24383
24384static void
24385initialise_dump_sects (Filedata * filedata)
24386{
24387 /* Initialise the dump_sects array from the cmdline_dump_sects array.
24388 Note we do this even if cmdline_dump_sects is empty because we
24389 must make sure that the dump_sets array is zeroed out before each
24390 object file is processed. */
24391 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
24392 memset (filedata->dump.dump_sects, 0,
24393 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
24394
24395 if (cmdline.num_dump_sects > 0)
24396 {
24397 if (filedata->dump.num_dump_sects == 0)
24398 /* A sneaky way of allocating the dump_sects array. */
24399 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
24400
24401 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
24402 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
24403 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
24404 }
24405}
24406
24407static bool
24408might_need_separate_debug_info (Filedata * filedata)
24409{
24410 /* Debuginfo files do not need further separate file loading. */
24411 if (filedata->file_header.e_shstrndx == SHN_UNDEF)
24412 return false;
24413
24414 /* Since do_follow_links might be enabled by default, only treat it as an
24415 indication that separate files should be loaded if setting it was a
24416 deliberate user action. */
24417 if (DEFAULT_FOR_FOLLOW_LINKS == 0 && do_follow_links)
24418 return true;
24419
24420 if (process_links || do_syms || do_unwind
24421 || dump_any_debugging || do_dump || do_debugging)
24422 return true;
24423
24424 return false;
24425}
24426
24427/* Process one ELF object file according to the command line options.
24428 This file may actually be stored in an archive. The file is
24429 positioned at the start of the ELF object. Returns TRUE if no
24430 problems were encountered, FALSE otherwise. */
24431
24432static bool
24433process_object (Filedata * filedata)
24434{
24435 bool have_separate_files;
24436 unsigned int i;
24437 bool res;
24438
24439 if (! get_file_header (filedata))
24440 {
24441 error (_("%s: Failed to read file header\n"), filedata->file_name);
24442 return false;
24443 }
24444
24445 /* Initialise per file variables. */
24446 for (i = ARRAY_SIZE (filedata->version_info); i--;)
24447 filedata->version_info[i] = 0;
24448
24449 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
24450 filedata->dynamic_info[i] = 0;
24451 filedata->dynamic_info_DT_GNU_HASH = 0;
24452 filedata->dynamic_info_DT_MIPS_XHASH = 0;
24453
24454 /* Process the file. */
24455 if (show_name)
24456 printf (_("\nFile: %s\n"), filedata->file_name);
24457
24458 initialise_dump_sects (filedata);
24459
24460 /* There may be some extensions in the first section header. Don't
24461 bomb if we can't read it. */
24462 get_section_headers (filedata, true);
24463
24464 if (! process_file_header (filedata))
24465 {
24466 res = false;
24467 goto out;
24468 }
24469
24470 /* Throw away the single section header read above, so that we
24471 re-read the entire set. */
24472 free (filedata->section_headers);
24473 filedata->section_headers = NULL;
24474
24475 if (! process_section_headers (filedata))
24476 {
24477 /* Without loaded section headers we cannot process lots of things. */
24478 do_unwind = do_version = do_dump = do_arch = false;
24479
24480 if (! do_using_dynamic)
24481 do_syms = do_dyn_syms = do_reloc = false;
24482 }
24483
24484 if (! process_section_groups (filedata))
24485 /* Without loaded section groups we cannot process unwind. */
24486 do_unwind = false;
24487
24488 process_program_headers (filedata);
24489
24490 res = process_dynamic_section (filedata);
24491
24492 if (! process_relocs (filedata))
24493 res = false;
24494
24495 if (! process_unwind (filedata))
24496 res = false;
24497
24498 if (! process_symbol_table (filedata))
24499 res = false;
24500
24501 if (! process_lto_symbol_tables (filedata))
24502 res = false;
24503
24504 if (! process_syminfo (filedata))
24505 res = false;
24506
24507 if (! process_version_sections (filedata))
24508 res = false;
24509
24510 if (might_need_separate_debug_info (filedata))
24511 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
24512 else
24513 have_separate_files = false;
24514
24515 if (! process_section_contents (filedata))
24516 res = false;
24517
24518 if (! process_got_section_contents (filedata))
24519 res = false;
24520
24521 if (have_separate_files)
24522 {
24523 separate_info * d;
24524
24525 for (d = first_separate_info; d != NULL; d = d->next)
24526 {
24527 initialise_dump_sects (d->handle);
24528
24529 if (process_links && ! process_file_header (d->handle))
24530 res = false;
24531 else if (! process_section_headers (d->handle))
24532 res = false;
24533 else if (! process_section_contents (d->handle))
24534 res = false;
24535 else if (process_links)
24536 {
24537 if (! process_section_groups (d->handle))
24538 res = false;
24539 process_program_headers (d->handle);
24540 if (! process_dynamic_section (d->handle))
24541 res = false;
24542 if (! process_relocs (d->handle))
24543 res = false;
24544 if (! process_unwind (d->handle))
24545 res = false;
24546 if (! process_symbol_table (d->handle))
24547 res = false;
24548 if (! process_lto_symbol_tables (d->handle))
24549 res = false;
24550 if (! process_syminfo (d->handle))
24551 res = false;
24552 if (! process_version_sections (d->handle))
24553 res = false;
24554 if (! process_notes (d->handle))
24555 res = false;
24556 }
24557 }
24558
24559 /* The file handles are closed by the call to free_debug_memory() below. */
24560 }
24561
24562 if (! process_notes (filedata))
24563 res = false;
24564
24565 if (! process_gnu_liblist (filedata))
24566 res = false;
24567
24568 if (! process_arch_specific (filedata))
24569 res = false;
24570
24571 out:
24572 free_filedata (filedata);
24573
24574 free_debug_memory ();
24575
24576 return res;
24577}
24578
24579/* Process an ELF archive.
24580 On entry the file is positioned just after the ARMAG string.
24581 Returns TRUE upon success, FALSE otherwise. */
24582
24583static bool
24584process_archive (Filedata * filedata, bool is_thin_archive)
24585{
24586 struct archive_info arch;
24587 struct archive_info nested_arch;
24588 size_t got;
24589 bool ret = true;
24590
24591 show_name = true;
24592
24593 /* The ARCH structure is used to hold information about this archive. */
24594 arch.file_name = NULL;
24595 arch.file = NULL;
24596 arch.index_array = NULL;
24597 arch.sym_table = NULL;
24598 arch.longnames = NULL;
24599
24600 /* The NESTED_ARCH structure is used as a single-item cache of information
24601 about a nested archive (when members of a thin archive reside within
24602 another regular archive file). */
24603 nested_arch.file_name = NULL;
24604 nested_arch.file = NULL;
24605 nested_arch.index_array = NULL;
24606 nested_arch.sym_table = NULL;
24607 nested_arch.longnames = NULL;
24608
24609 if (setup_archive (&arch, filedata->file_name, filedata->handle,
24610 filedata->file_size, is_thin_archive,
24611 do_archive_index) != 0)
24612 {
24613 ret = false;
24614 goto out;
24615 }
24616
24617 if (do_archive_index)
24618 {
24619 if (arch.sym_table == NULL)
24620 error (_("%s: unable to dump the index as none was found\n"),
24621 filedata->file_name);
24622 else
24623 {
24624 uint64_t i, l;
24625 uint64_t current_pos;
24626
24627 printf (_("Index of archive %s: (%" PRIu64 " entries,"
24628 " %#" PRIx64 " bytes in the symbol table)\n"),
24629 filedata->file_name, arch.index_num,
24630 arch.sym_size);
24631
24632 current_pos = ftell (filedata->handle);
24633
24634 for (i = l = 0; i < arch.index_num; i++)
24635 {
24636 if (i == 0
24637 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
24638 {
24639 char * member_name
24640 = get_archive_member_name_at (&arch, arch.index_array[i],
24641 &nested_arch);
24642
24643 if (member_name != NULL)
24644 {
24645 char * qualified_name
24646 = make_qualified_name (&arch, &nested_arch,
24647 member_name);
24648
24649 if (qualified_name != NULL)
24650 {
24651 printf (_("Contents of binary %s at offset "),
24652 qualified_name);
24653 (void) print_vma (arch.index_array[i], PREFIX_HEX);
24654 putchar ('\n');
24655 free (qualified_name);
24656 }
24657 free (member_name);
24658 }
24659 }
24660
24661 if (l >= arch.sym_size)
24662 {
24663 error (_("%s: end of the symbol table reached "
24664 "before the end of the index\n"),
24665 filedata->file_name);
24666 ret = false;
24667 break;
24668 }
24669 /* PR 17531: file: 0b6630b2. */
24670 printf ("\t%.*s\n",
24671 (int) (arch.sym_size - l), arch.sym_table + l);
24672 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
24673 }
24674
24675 if (arch.uses_64bit_indices)
24676 l = (l + 7) & ~ 7;
24677 else
24678 l += l & 1;
24679
24680 if (l < arch.sym_size)
24681 {
24682 error (ngettext ("%s: %" PRId64 " byte remains in the symbol table, "
24683 "but without corresponding entries in "
24684 "the index table\n",
24685 "%s: %" PRId64 " bytes remain in the symbol table, "
24686 "but without corresponding entries in "
24687 "the index table\n",
24688 arch.sym_size - l),
24689 filedata->file_name, arch.sym_size - l);
24690 ret = false;
24691 }
24692
24693 if (fseek64 (filedata->handle, current_pos, SEEK_SET) != 0)
24694 {
24695 error (_("%s: failed to seek back to start of object files "
24696 "in the archive\n"),
24697 filedata->file_name);
24698 ret = false;
24699 goto out;
24700 }
24701 }
24702
24703 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
24704 && !do_segments && !do_header && !do_dump && !do_version
24705 && !do_histogram && !do_debugging && !do_arch && !do_notes
24706 && !do_section_groups && !do_dyn_syms)
24707 {
24708 ret = true; /* Archive index only. */
24709 goto out;
24710 }
24711 }
24712
24713 while (1)
24714 {
24715 char * name;
24716 size_t namelen;
24717 char * qualified_name;
24718
24719 /* Read the next archive header. */
24720 if (fseek64 (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
24721 {
24722 error (_("%s: failed to seek to next archive header\n"),
24723 arch.file_name);
24724 ret = false;
24725 break;
24726 }
24727 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
24728 if (got != sizeof arch.arhdr)
24729 {
24730 if (got == 0)
24731 break;
24732 /* PR 24049 - we cannot use filedata->file_name as this will
24733 have already been freed. */
24734 error (_("%s: failed to read archive header\n"), arch.file_name);
24735
24736 ret = false;
24737 break;
24738 }
24739 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
24740 {
24741 error (_("%s: did not find a valid archive header\n"),
24742 arch.file_name);
24743 ret = false;
24744 break;
24745 }
24746
24747 arch.next_arhdr_offset += sizeof arch.arhdr;
24748
24749 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
24750
24751 name = get_archive_member_name (&arch, &nested_arch);
24752 if (name == NULL)
24753 {
24754 error (_("%s: bad archive file name\n"), arch.file_name);
24755 ret = false;
24756 break;
24757 }
24758 namelen = strlen (name);
24759
24760 qualified_name = make_qualified_name (&arch, &nested_arch, name);
24761 if (qualified_name == NULL)
24762 {
24763 error (_("%s: bad archive file name\n"), arch.file_name);
24764 free (name);
24765 ret = false;
24766 break;
24767 }
24768
24769 if (is_thin_archive && arch.nested_member_origin == 0)
24770 {
24771 /* This is a proxy for an external member of a thin archive. */
24772 Filedata * member_filedata;
24773 char * member_file_name = adjust_relative_path
24774 (filedata->file_name, name, namelen);
24775
24776 free (name);
24777 if (member_file_name == NULL)
24778 {
24779 free (qualified_name);
24780 ret = false;
24781 break;
24782 }
24783
24784 member_filedata = open_file (member_file_name, false);
24785 if (member_filedata == NULL)
24786 {
24787 error (_("Input file '%s' is not readable.\n"), member_file_name);
24788 free (member_file_name);
24789 free (qualified_name);
24790 ret = false;
24791 break;
24792 }
24793
24794 filedata->archive_file_offset = arch.nested_member_origin;
24795 member_filedata->file_name = qualified_name;
24796
24797 /* The call to process_object() expects the file to be at the beginning. */
24798 rewind (member_filedata->handle);
24799
24800 if (! process_object (member_filedata))
24801 ret = false;
24802
24803 close_file (member_filedata);
24804 free (member_file_name);
24805 }
24806 else if (is_thin_archive)
24807 {
24808 Filedata thin_filedata;
24809
24810 memset (&thin_filedata, 0, sizeof (thin_filedata));
24811
24812 /* PR 15140: Allow for corrupt thin archives. */
24813 if (nested_arch.file == NULL)
24814 {
24815 error (_("%s: contains corrupt thin archive: %s\n"),
24816 qualified_name, name);
24817 free (qualified_name);
24818 free (name);
24819 ret = false;
24820 break;
24821 }
24822 free (name);
24823
24824 /* This is a proxy for a member of a nested archive. */
24825 filedata->archive_file_offset
24826 = arch.nested_member_origin + sizeof arch.arhdr;
24827
24828 /* The nested archive file will have been opened and setup by
24829 get_archive_member_name. */
24830 if (fseek64 (nested_arch.file, filedata->archive_file_offset,
24831 SEEK_SET) != 0)
24832 {
24833 error (_("%s: failed to seek to archive member.\n"),
24834 nested_arch.file_name);
24835 free (qualified_name);
24836 ret = false;
24837 break;
24838 }
24839
24840 thin_filedata.handle = nested_arch.file;
24841 thin_filedata.file_name = qualified_name;
24842
24843 if (! process_object (& thin_filedata))
24844 ret = false;
24845 }
24846 else
24847 {
24848 free (name);
24849 filedata->archive_file_offset = arch.next_arhdr_offset;
24850 filedata->file_name = qualified_name;
24851 if (! process_object (filedata))
24852 ret = false;
24853 arch.next_arhdr_offset += (filedata->archive_file_size + 1) & -2;
24854 /* Stop looping with "negative" archive_file_size. */
24855 if (arch.next_arhdr_offset < filedata->archive_file_size)
24856 arch.next_arhdr_offset = -1ul;
24857 }
24858
24859 free (qualified_name);
24860 }
24861
24862 out:
24863 if (nested_arch.file != NULL)
24864 fclose (nested_arch.file);
24865 release_archive (&nested_arch);
24866 release_archive (&arch);
24867
24868 return ret;
24869}
24870
24871static bool
24872process_file (char * file_name)
24873{
24874 Filedata * filedata = NULL;
24875 struct stat statbuf;
24876 char armag[SARMAG];
24877 bool ret = true;
24878
24879 if (stat (file_name, &statbuf) < 0)
24880 {
24881 if (errno == ENOENT)
24882 error (_("'%s': No such file\n"), file_name);
24883 else
24884 error (_("Could not locate '%s'. System error message: %s\n"),
24885 file_name, strerror (errno));
24886 return false;
24887 }
24888
24889 if (! S_ISREG (statbuf.st_mode))
24890 {
24891 error (_("'%s' is not an ordinary file\n"), file_name);
24892 return false;
24893 }
24894
24895 filedata = calloc (1, sizeof * filedata);
24896 if (filedata == NULL)
24897 {
24898 error (_("Out of memory allocating file data structure\n"));
24899 return false;
24900 }
24901
24902 filedata->file_name = file_name;
24903 filedata->handle = fopen (file_name, "rb");
24904 if (filedata->handle == NULL)
24905 {
24906 error (_("Input file '%s' is not readable.\n"), file_name);
24907 free (filedata);
24908 return false;
24909 }
24910
24911 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
24912 {
24913 error (_("%s: Failed to read file's magic number\n"), file_name);
24914 fclose (filedata->handle);
24915 free (filedata);
24916 return false;
24917 }
24918
24919 filedata->file_size = statbuf.st_size;
24920 filedata->is_separate = false;
24921
24922 if (memcmp (armag, ARMAG, SARMAG) == 0)
24923 {
24924 if (! process_archive (filedata, false))
24925 ret = false;
24926 }
24927 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
24928 {
24929 if ( ! process_archive (filedata, true))
24930 ret = false;
24931 }
24932 else
24933 {
24934 if (do_archive_index && !check_all)
24935 error (_("File %s is not an archive so its index cannot be displayed.\n"),
24936 file_name);
24937
24938 rewind (filedata->handle);
24939 filedata->archive_file_size = filedata->archive_file_offset = 0;
24940
24941 if (! process_object (filedata))
24942 ret = false;
24943 }
24944
24945 close_debug_file (filedata);
24946
24947 free (ba_cache.strtab);
24948 ba_cache.strtab = NULL;
24949 free (ba_cache.symtab);
24950 ba_cache.symtab = NULL;
24951 ba_cache.filedata = NULL;
24952
24953 return ret;
24954}
24955
24956#ifdef SUPPORT_DISASSEMBLY
24957/* Needed by the i386 disassembler. For extra credit, someone could
24958 fix this so that we insert symbolic addresses here, esp for GOT/PLT
24959 symbols. */
24960
24961void
24962print_address (unsigned int addr, FILE * outfile)
24963{
24964 fprintf (outfile,"0x%8.8x", addr);
24965}
24966
24967/* Needed by the i386 disassembler. */
24968
24969void
24970db_task_printsym (unsigned int addr)
24971{
24972 print_address (addr, stderr);
24973}
24974#endif
24975
24976int
24977main (int argc, char ** argv)
24978{
24979 int err;
24980
24981#ifdef HAVE_LC_MESSAGES
24982 setlocale (LC_MESSAGES, "");
24983#endif
24984 setlocale (LC_CTYPE, "");
24985 bindtextdomain (PACKAGE, LOCALEDIR);
24986 textdomain (PACKAGE);
24987
24988 expandargv (&argc, &argv);
24989
24990 parse_args (& cmdline, argc, argv);
24991
24992 if (optind < (argc - 1))
24993 /* When displaying information for more than one file,
24994 prefix the information with the file name. */
24995 show_name = true;
24996 else if (optind >= argc)
24997 {
24998 /* Ensure that the warning is always displayed. */
24999 do_checks = true;
25000
25001 warn (_("Nothing to do.\n"));
25002 usage (stderr);
25003 }
25004
25005 err = false;
25006 while (optind < argc)
25007 if (! process_file (argv[optind++]))
25008 err = true;
25009
25010 free (cmdline.dump_sects);
25011
25012 free (dump_ctf_symtab_name);
25013 free (dump_ctf_strtab_name);
25014 free (dump_ctf_parent_name);
25015
25016 return err ? EXIT_FAILURE : EXIT_SUCCESS;
25017}