]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame_incremental - binutils/readelf.c
sim: sh: simplify testsuite a bit
[thirdparty/binutils-gdb.git] / binutils / readelf.c
... / ...
CommitLineData
1/* readelf.c -- display contents of an ELF format file
2 Copyright (C) 1998-2021 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#include <wchar.h>
48
49#if __GNUC__ >= 2
50/* Define BFD64 here, even if our default architecture is 32 bit ELF
51 as this will allow us to read in and parse 64bit and 32bit ELF files.
52 Only do this if we believe that the compiler can support a 64 bit
53 data type. For now we only rely on GCC being able to do this. */
54#define BFD64
55#endif
56
57#include "bfd.h"
58#include "bucomm.h"
59#include "elfcomm.h"
60#include "dwarf.h"
61#include "ctf-api.h"
62#include "demangle.h"
63
64#include "elf/common.h"
65#include "elf/external.h"
66#include "elf/internal.h"
67
68
69/* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
70 we can obtain the H8 reloc numbers. We need these for the
71 get_reloc_size() function. We include h8.h again after defining
72 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
73
74#include "elf/h8.h"
75#undef _ELF_H8_H
76
77/* Undo the effects of #including reloc-macros.h. */
78
79#undef START_RELOC_NUMBERS
80#undef RELOC_NUMBER
81#undef FAKE_RELOC
82#undef EMPTY_RELOC
83#undef END_RELOC_NUMBERS
84#undef _RELOC_MACROS_H
85
86/* The following headers use the elf/reloc-macros.h file to
87 automatically generate relocation recognition functions
88 such as elf_mips_reloc_type() */
89
90#define RELOC_MACROS_GEN_FUNC
91
92#include "elf/aarch64.h"
93#include "elf/alpha.h"
94#include "elf/arc.h"
95#include "elf/arm.h"
96#include "elf/avr.h"
97#include "elf/bfin.h"
98#include "elf/cr16.h"
99#include "elf/cris.h"
100#include "elf/crx.h"
101#include "elf/csky.h"
102#include "elf/d10v.h"
103#include "elf/d30v.h"
104#include "elf/dlx.h"
105#include "elf/bpf.h"
106#include "elf/epiphany.h"
107#include "elf/fr30.h"
108#include "elf/frv.h"
109#include "elf/ft32.h"
110#include "elf/h8.h"
111#include "elf/hppa.h"
112#include "elf/i386.h"
113#include "elf/i370.h"
114#include "elf/i860.h"
115#include "elf/i960.h"
116#include "elf/ia64.h"
117#include "elf/ip2k.h"
118#include "elf/lm32.h"
119#include "elf/iq2000.h"
120#include "elf/m32c.h"
121#include "elf/m32r.h"
122#include "elf/m68k.h"
123#include "elf/m68hc11.h"
124#include "elf/s12z.h"
125#include "elf/mcore.h"
126#include "elf/mep.h"
127#include "elf/metag.h"
128#include "elf/microblaze.h"
129#include "elf/mips.h"
130#include "elf/mmix.h"
131#include "elf/mn10200.h"
132#include "elf/mn10300.h"
133#include "elf/moxie.h"
134#include "elf/mt.h"
135#include "elf/msp430.h"
136#include "elf/nds32.h"
137#include "elf/nfp.h"
138#include "elf/nios2.h"
139#include "elf/or1k.h"
140#include "elf/pj.h"
141#include "elf/ppc.h"
142#include "elf/ppc64.h"
143#include "elf/pru.h"
144#include "elf/riscv.h"
145#include "elf/rl78.h"
146#include "elf/rx.h"
147#include "elf/s390.h"
148#include "elf/score.h"
149#include "elf/sh.h"
150#include "elf/sparc.h"
151#include "elf/spu.h"
152#include "elf/tic6x.h"
153#include "elf/tilegx.h"
154#include "elf/tilepro.h"
155#include "elf/v850.h"
156#include "elf/vax.h"
157#include "elf/visium.h"
158#include "elf/wasm32.h"
159#include "elf/x86-64.h"
160#include "elf/xc16x.h"
161#include "elf/xgate.h"
162#include "elf/xstormy16.h"
163#include "elf/xtensa.h"
164#include "elf/z80.h"
165#include "elf/loongarch.h"
166
167#include "getopt.h"
168#include "libiberty.h"
169#include "safe-ctype.h"
170#include "filenames.h"
171
172#ifndef offsetof
173#define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
174#endif
175
176typedef struct elf_section_list
177{
178 Elf_Internal_Shdr * hdr;
179 struct elf_section_list * next;
180} elf_section_list;
181
182/* Flag bits indicating particular types of dump. */
183#define HEX_DUMP (1 << 0) /* The -x command line switch. */
184#define DISASS_DUMP (1 << 1) /* The -i command line switch. */
185#define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
186#define STRING_DUMP (1 << 3) /* The -p command line switch. */
187#define RELOC_DUMP (1 << 4) /* The -R command line switch. */
188#define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
189
190typedef unsigned char dump_type;
191
192/* A linked list of the section names for which dumps were requested. */
193struct dump_list_entry
194{
195 char * name;
196 dump_type type;
197 struct dump_list_entry * next;
198};
199
200/* A dynamic array of flags indicating for which sections a dump
201 has been requested via command line switches. */
202struct dump_data
203{
204 dump_type * dump_sects;
205 unsigned int num_dump_sects;
206};
207
208static struct dump_data cmdline;
209
210static struct dump_list_entry * dump_sects_byname;
211
212char * program_name = "readelf";
213
214static bool show_name = false;
215static bool do_dynamic = false;
216static bool do_syms = false;
217static bool do_dyn_syms = false;
218static bool do_lto_syms = false;
219static bool do_reloc = false;
220static bool do_sections = false;
221static bool do_section_groups = false;
222static bool do_section_details = false;
223static bool do_segments = false;
224static bool do_unwind = false;
225static bool do_using_dynamic = false;
226static bool do_header = false;
227static bool do_dump = false;
228static bool do_version = false;
229static bool do_histogram = false;
230static bool do_debugging = false;
231static bool do_ctf = false;
232static bool do_arch = false;
233static bool do_notes = false;
234static bool do_archive_index = false;
235static bool check_all = false;
236static bool is_32bit_elf = false;
237static bool decompress_dumps = false;
238static bool do_not_show_symbol_truncation = false;
239static bool do_demangle = false; /* Pretty print C++ symbol names. */
240static bool process_links = false;
241static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
242static int sym_base = 0;
243
244static char *dump_ctf_parent_name;
245static char *dump_ctf_symtab_name;
246static char *dump_ctf_strtab_name;
247
248struct group_list
249{
250 struct group_list * next;
251 unsigned int section_index;
252};
253
254struct group
255{
256 struct group_list * root;
257 unsigned int group_index;
258};
259
260typedef struct filedata
261{
262 const char * file_name;
263 bool is_separate;
264 FILE * handle;
265 bfd_size_type file_size;
266 Elf_Internal_Ehdr file_header;
267 unsigned long archive_file_offset;
268 unsigned long archive_file_size;
269 /* Everything below this point is cleared out by free_filedata. */
270 Elf_Internal_Shdr * section_headers;
271 Elf_Internal_Phdr * program_headers;
272 char * string_table;
273 unsigned long string_table_length;
274 unsigned long dynamic_addr;
275 bfd_size_type dynamic_size;
276 size_t dynamic_nent;
277 Elf_Internal_Dyn * dynamic_section;
278 Elf_Internal_Shdr * dynamic_strtab_section;
279 char * dynamic_strings;
280 unsigned long dynamic_strings_length;
281 Elf_Internal_Shdr * dynamic_symtab_section;
282 unsigned long num_dynamic_syms;
283 Elf_Internal_Sym * dynamic_symbols;
284 bfd_vma version_info[16];
285 unsigned int dynamic_syminfo_nent;
286 Elf_Internal_Syminfo * dynamic_syminfo;
287 unsigned long dynamic_syminfo_offset;
288 bfd_size_type nbuckets;
289 bfd_size_type nchains;
290 bfd_vma * buckets;
291 bfd_vma * chains;
292 bfd_size_type ngnubuckets;
293 bfd_size_type ngnuchains;
294 bfd_vma * gnubuckets;
295 bfd_vma * gnuchains;
296 bfd_vma * mipsxlat;
297 bfd_vma gnusymidx;
298 char * program_interpreter;
299 bfd_vma dynamic_info[DT_ENCODING];
300 bfd_vma dynamic_info_DT_GNU_HASH;
301 bfd_vma dynamic_info_DT_MIPS_XHASH;
302 elf_section_list * symtab_shndx_list;
303 size_t group_count;
304 struct group * section_groups;
305 struct group ** section_headers_groups;
306 /* A dynamic array of flags indicating for which sections a dump of
307 some kind has been requested. It is reset on a per-object file
308 basis and then initialised from the cmdline_dump_sects array,
309 the results of interpreting the -w switch, and the
310 dump_sects_byname list. */
311 struct dump_data dump;
312} Filedata;
313
314/* How to print a vma value. */
315typedef enum print_mode
316{
317 HEX,
318 HEX_5,
319 DEC,
320 DEC_5,
321 UNSIGNED,
322 UNSIGNED_5,
323 PREFIX_HEX,
324 PREFIX_HEX_5,
325 FULL_HEX,
326 LONG_HEX,
327 OCTAL,
328 OCTAL_5
329}
330print_mode;
331
332/* Versioned symbol info. */
333enum versioned_symbol_info
334{
335 symbol_undefined,
336 symbol_hidden,
337 symbol_public
338};
339
340static const char * get_symbol_version_string
341 (Filedata *, bool, const char *, unsigned long, unsigned,
342 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
343
344#define UNKNOWN -1
345
346static inline const char *
347section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
348{
349 return filedata->string_table + hdr->sh_name;
350}
351
352static inline bool
353section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
354{
355 return (hdr != NULL
356 && filedata->string_table != NULL
357 && hdr->sh_name < filedata->string_table_length);
358}
359
360static inline const char *
361section_name_print (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
362{
363 if (hdr == NULL)
364 return _("<none>");
365 if (filedata->string_table == NULL)
366 return _("<no-strings>");
367 if (hdr->sh_name >= filedata->string_table_length)
368 return _("<corrupt>");
369 return section_name (filedata, hdr);
370}
371
372#define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
373
374static inline bool
375valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
376{
377 return strtab != NULL && offset < strtab_size;
378}
379
380static inline bool
381valid_dynamic_name (const Filedata *filedata, uint64_t offset)
382{
383 return valid_symbol_name (filedata->dynamic_strings,
384 filedata->dynamic_strings_length, offset);
385}
386
387/* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
388 already been called and verified that the string exists. */
389static inline const char *
390get_dynamic_name (const Filedata *filedata, size_t offset)
391{
392 return filedata->dynamic_strings + offset;
393}
394
395#define REMOVE_ARCH_BITS(ADDR) \
396 do \
397 { \
398 if (filedata->file_header.e_machine == EM_ARM) \
399 (ADDR) &= ~1; \
400 } \
401 while (0)
402
403/* Get the correct GNU hash section name. */
404#define GNU_HASH_SECTION_NAME(filedata) \
405 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
406\f
407/* Print a BFD_VMA to an internal buffer, for use in error messages.
408 BFD_FMA_FMT can't be used in translated strings. */
409
410static const char *
411bfd_vmatoa (char *fmtch, bfd_vma value)
412{
413 /* bfd_vmatoa is used more then once in a printf call for output.
414 Cycle through an array of buffers. */
415 static int buf_pos = 0;
416 static struct bfd_vmatoa_buf
417 {
418 char place[64];
419 } buf[4];
420 char *ret;
421 char fmt[32];
422
423 ret = buf[buf_pos++].place;
424 buf_pos %= ARRAY_SIZE (buf);
425
426 sprintf (fmt, "%%%s%s", BFD_VMA_FMT, fmtch);
427 snprintf (ret, sizeof (buf[0].place), fmt, value);
428 return ret;
429}
430
431/* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
432 OFFSET + the offset of the current archive member, if we are examining an
433 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
434 allocate a buffer using malloc and fill that. In either case return the
435 pointer to the start of the retrieved data or NULL if something went wrong.
436 If something does go wrong and REASON is not NULL then emit an error
437 message using REASON as part of the context. */
438
439static void *
440get_data (void * var,
441 Filedata * filedata,
442 unsigned long offset,
443 bfd_size_type size,
444 bfd_size_type nmemb,
445 const char * reason)
446{
447 void * mvar;
448 bfd_size_type amt = size * nmemb;
449
450 if (size == 0 || nmemb == 0)
451 return NULL;
452
453 /* If the size_t type is smaller than the bfd_size_type, eg because
454 you are building a 32-bit tool on a 64-bit host, then make sure
455 that when the sizes are cast to (size_t) no information is lost. */
456 if ((size_t) size != size
457 || (size_t) nmemb != nmemb
458 || (size_t) amt != amt)
459 {
460 if (reason)
461 error (_("Size truncation prevents reading %s"
462 " elements of size %s for %s\n"),
463 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
464 return NULL;
465 }
466
467 /* Check for size overflow. */
468 if (amt / size != nmemb || (size_t) amt + 1 == 0)
469 {
470 if (reason)
471 error (_("Size overflow prevents reading %s"
472 " elements of size %s for %s\n"),
473 bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
474 return NULL;
475 }
476
477 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
478 attempting to allocate memory when the read is bound to fail. */
479 if (filedata->archive_file_offset > filedata->file_size
480 || offset > filedata->file_size - filedata->archive_file_offset
481 || amt > filedata->file_size - filedata->archive_file_offset - offset)
482 {
483 if (reason)
484 error (_("Reading %s bytes extends past end of file for %s\n"),
485 bfd_vmatoa ("u", amt), reason);
486 return NULL;
487 }
488
489 if (fseek (filedata->handle, filedata->archive_file_offset + offset,
490 SEEK_SET))
491 {
492 if (reason)
493 error (_("Unable to seek to 0x%lx for %s\n"),
494 filedata->archive_file_offset + offset, reason);
495 return NULL;
496 }
497
498 mvar = var;
499 if (mvar == NULL)
500 {
501 /* + 1 so that we can '\0' terminate invalid string table sections. */
502 mvar = malloc ((size_t) amt + 1);
503
504 if (mvar == NULL)
505 {
506 if (reason)
507 error (_("Out of memory allocating %s bytes for %s\n"),
508 bfd_vmatoa ("u", amt), reason);
509 return NULL;
510 }
511
512 ((char *) mvar)[amt] = '\0';
513 }
514
515 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
516 {
517 if (reason)
518 error (_("Unable to read in %s bytes of %s\n"),
519 bfd_vmatoa ("u", amt), reason);
520 if (mvar != var)
521 free (mvar);
522 return NULL;
523 }
524
525 return mvar;
526}
527
528/* Print a VMA value in the MODE specified.
529 Returns the number of characters displayed. */
530
531static unsigned int
532print_vma (bfd_vma vma, print_mode mode)
533{
534 unsigned int nc = 0;
535
536 switch (mode)
537 {
538 case FULL_HEX:
539 nc = printf ("0x");
540 /* Fall through. */
541 case LONG_HEX:
542#ifdef BFD64
543 if (is_32bit_elf)
544 return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
545#endif
546 printf_vma (vma);
547 return nc + 16;
548
549 case DEC_5:
550 if (vma <= 99999)
551 return printf ("%5" BFD_VMA_FMT "d", vma);
552 /* Fall through. */
553 case PREFIX_HEX:
554 nc = printf ("0x");
555 /* Fall through. */
556 case HEX:
557 return nc + printf ("%" BFD_VMA_FMT "x", vma);
558
559 case PREFIX_HEX_5:
560 nc = printf ("0x");
561 /* Fall through. */
562 case HEX_5:
563 return nc + printf ("%05" BFD_VMA_FMT "x", vma);
564
565 case DEC:
566 return printf ("%" BFD_VMA_FMT "d", vma);
567
568 case UNSIGNED:
569 return printf ("%" BFD_VMA_FMT "u", vma);
570
571 case UNSIGNED_5:
572 return printf ("%5" BFD_VMA_FMT "u", vma);
573
574 case OCTAL:
575 return printf ("%" BFD_VMA_FMT "o", vma);
576
577 case OCTAL_5:
578 return printf ("%5" BFD_VMA_FMT "o", vma);
579
580 default:
581 /* FIXME: Report unrecognised mode ? */
582 return 0;
583 }
584}
585
586
587/* Display a symbol on stdout. Handles the display of control characters and
588 multibye characters (assuming the host environment supports them).
589
590 Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
591
592 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
593 abs(WIDTH) - 5 characters followed by "[...]".
594
595 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
596 padding as necessary.
597
598 Returns the number of emitted characters. */
599
600static unsigned int
601print_symbol (signed int width, const char * symbol)
602{
603 bool extra_padding = false;
604 bool do_dots = false;
605 signed int num_printed = 0;
606#ifdef HAVE_MBSTATE_T
607 mbstate_t state;
608#endif
609 unsigned int width_remaining;
610 const void * alloced_symbol = NULL;
611
612 if (width < 0)
613 {
614 /* Keep the width positive. This helps the code below. */
615 width = - width;
616 extra_padding = true;
617 }
618 else if (width == 0)
619 return 0;
620
621 if (do_wide)
622 /* Set the remaining width to a very large value.
623 This simplifies the code below. */
624 width_remaining = INT_MAX;
625 else
626 {
627 width_remaining = width;
628 if (! do_not_show_symbol_truncation
629 && (int) strlen (symbol) > width)
630 {
631 width_remaining -= 5;
632 if ((int) width_remaining < 0)
633 width_remaining = 0;
634 do_dots = true;
635 }
636 }
637
638#ifdef HAVE_MBSTATE_T
639 /* Initialise the multibyte conversion state. */
640 memset (& state, 0, sizeof (state));
641#endif
642
643 if (do_demangle && *symbol)
644 {
645 const char * res = cplus_demangle (symbol, demangle_flags);
646
647 if (res != NULL)
648 alloced_symbol = symbol = res;
649 }
650
651 while (width_remaining)
652 {
653 size_t n;
654 const char c = *symbol++;
655
656 if (c == 0)
657 break;
658
659 /* Do not print control characters directly as they can affect terminal
660 settings. Such characters usually appear in the names generated
661 by the assembler for local labels. */
662 if (ISCNTRL (c))
663 {
664 if (width_remaining < 2)
665 break;
666
667 printf ("^%c", c + 0x40);
668 width_remaining -= 2;
669 num_printed += 2;
670 }
671 else if (ISPRINT (c))
672 {
673 putchar (c);
674 width_remaining --;
675 num_printed ++;
676 }
677 else
678 {
679#ifdef HAVE_MBSTATE_T
680 wchar_t w;
681#endif
682 /* Let printf do the hard work of displaying multibyte characters. */
683 printf ("%.1s", symbol - 1);
684 width_remaining --;
685 num_printed ++;
686
687#ifdef HAVE_MBSTATE_T
688 /* Try to find out how many bytes made up the character that was
689 just printed. Advance the symbol pointer past the bytes that
690 were displayed. */
691 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
692#else
693 n = 1;
694#endif
695 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
696 symbol += (n - 1);
697 }
698 }
699
700 if (do_dots)
701 num_printed += printf ("[...]");
702
703 if (extra_padding && num_printed < width)
704 {
705 /* Fill in the remaining spaces. */
706 printf ("%-*s", width - num_printed, " ");
707 num_printed = width;
708 }
709
710 free ((void *) alloced_symbol);
711 return num_printed;
712}
713
714/* Returns a pointer to a static buffer containing a printable version of
715 the given section's name. Like print_symbol, except that it does not try
716 to print multibyte characters, it just interprets them as hex values. */
717
718static const char *
719printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
720{
721#define MAX_PRINT_SEC_NAME_LEN 256
722 static char sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
723 const char * name = section_name_print (filedata, sec);
724 char * buf = sec_name_buf;
725 char c;
726 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
727
728 while ((c = * name ++) != 0)
729 {
730 if (ISCNTRL (c))
731 {
732 if (remaining < 2)
733 break;
734
735 * buf ++ = '^';
736 * buf ++ = c + 0x40;
737 remaining -= 2;
738 }
739 else if (ISPRINT (c))
740 {
741 * buf ++ = c;
742 remaining -= 1;
743 }
744 else
745 {
746 static char hex[17] = "0123456789ABCDEF";
747
748 if (remaining < 4)
749 break;
750 * buf ++ = '<';
751 * buf ++ = hex[(c & 0xf0) >> 4];
752 * buf ++ = hex[c & 0x0f];
753 * buf ++ = '>';
754 remaining -= 4;
755 }
756
757 if (remaining == 0)
758 break;
759 }
760
761 * buf = 0;
762 return sec_name_buf;
763}
764
765static const char *
766printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
767{
768 if (ndx >= filedata->file_header.e_shnum)
769 return _("<corrupt>");
770
771 return printable_section_name (filedata, filedata->section_headers + ndx);
772}
773
774/* Return a pointer to section NAME, or NULL if no such section exists. */
775
776static Elf_Internal_Shdr *
777find_section (Filedata * filedata, const char * name)
778{
779 unsigned int i;
780
781 if (filedata->section_headers == NULL)
782 return NULL;
783
784 for (i = 0; i < filedata->file_header.e_shnum; i++)
785 if (section_name_valid (filedata, filedata->section_headers + i)
786 && streq (section_name (filedata, filedata->section_headers + i),
787 name))
788 return filedata->section_headers + i;
789
790 return NULL;
791}
792
793/* Return a pointer to a section containing ADDR, or NULL if no such
794 section exists. */
795
796static Elf_Internal_Shdr *
797find_section_by_address (Filedata * filedata, bfd_vma addr)
798{
799 unsigned int i;
800
801 if (filedata->section_headers == NULL)
802 return NULL;
803
804 for (i = 0; i < filedata->file_header.e_shnum; i++)
805 {
806 Elf_Internal_Shdr *sec = filedata->section_headers + i;
807
808 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
809 return sec;
810 }
811
812 return NULL;
813}
814
815static Elf_Internal_Shdr *
816find_section_by_type (Filedata * filedata, unsigned int type)
817{
818 unsigned int i;
819
820 if (filedata->section_headers == NULL)
821 return NULL;
822
823 for (i = 0; i < filedata->file_header.e_shnum; i++)
824 {
825 Elf_Internal_Shdr *sec = filedata->section_headers + i;
826
827 if (sec->sh_type == type)
828 return sec;
829 }
830
831 return NULL;
832}
833
834/* Return a pointer to section NAME, or NULL if no such section exists,
835 restricted to the list of sections given in SET. */
836
837static Elf_Internal_Shdr *
838find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
839{
840 unsigned int i;
841
842 if (filedata->section_headers == NULL)
843 return NULL;
844
845 if (set != NULL)
846 {
847 while ((i = *set++) > 0)
848 {
849 /* See PR 21156 for a reproducer. */
850 if (i >= filedata->file_header.e_shnum)
851 continue; /* FIXME: Should we issue an error message ? */
852
853 if (section_name_valid (filedata, filedata->section_headers + i)
854 && streq (section_name (filedata, filedata->section_headers + i),
855 name))
856 return filedata->section_headers + i;
857 }
858 }
859
860 return find_section (filedata, name);
861}
862
863/* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
864 This OS has so many departures from the ELF standard that we test it at
865 many places. */
866
867static inline bool
868is_ia64_vms (Filedata * filedata)
869{
870 return filedata->file_header.e_machine == EM_IA_64
871 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
872}
873
874/* Guess the relocation size commonly used by the specific machines. */
875
876static bool
877guess_is_rela (unsigned int e_machine)
878{
879 switch (e_machine)
880 {
881 /* Targets that use REL relocations. */
882 case EM_386:
883 case EM_IAMCU:
884 case EM_960:
885 case EM_ARM:
886 case EM_D10V:
887 case EM_CYGNUS_D10V:
888 case EM_DLX:
889 case EM_MIPS:
890 case EM_MIPS_RS3_LE:
891 case EM_CYGNUS_M32R:
892 case EM_SCORE:
893 case EM_XGATE:
894 case EM_NFP:
895 case EM_BPF:
896 return false;
897
898 /* Targets that use RELA relocations. */
899 case EM_68K:
900 case EM_860:
901 case EM_AARCH64:
902 case EM_ADAPTEVA_EPIPHANY:
903 case EM_ALPHA:
904 case EM_ALTERA_NIOS2:
905 case EM_ARC:
906 case EM_ARC_COMPACT:
907 case EM_ARC_COMPACT2:
908 case EM_AVR:
909 case EM_AVR_OLD:
910 case EM_BLACKFIN:
911 case EM_CR16:
912 case EM_CRIS:
913 case EM_CRX:
914 case EM_CSKY:
915 case EM_D30V:
916 case EM_CYGNUS_D30V:
917 case EM_FR30:
918 case EM_FT32:
919 case EM_CYGNUS_FR30:
920 case EM_CYGNUS_FRV:
921 case EM_H8S:
922 case EM_H8_300:
923 case EM_H8_300H:
924 case EM_IA_64:
925 case EM_IP2K:
926 case EM_IP2K_OLD:
927 case EM_IQ2000:
928 case EM_LATTICEMICO32:
929 case EM_M32C_OLD:
930 case EM_M32C:
931 case EM_M32R:
932 case EM_MCORE:
933 case EM_CYGNUS_MEP:
934 case EM_METAG:
935 case EM_MMIX:
936 case EM_MN10200:
937 case EM_CYGNUS_MN10200:
938 case EM_MN10300:
939 case EM_CYGNUS_MN10300:
940 case EM_MOXIE:
941 case EM_MSP430:
942 case EM_MSP430_OLD:
943 case EM_MT:
944 case EM_NDS32:
945 case EM_NIOS32:
946 case EM_OR1K:
947 case EM_PPC64:
948 case EM_PPC:
949 case EM_TI_PRU:
950 case EM_RISCV:
951 case EM_RL78:
952 case EM_RX:
953 case EM_S390:
954 case EM_S390_OLD:
955 case EM_SH:
956 case EM_SPARC:
957 case EM_SPARC32PLUS:
958 case EM_SPARCV9:
959 case EM_SPU:
960 case EM_TI_C6000:
961 case EM_TILEGX:
962 case EM_TILEPRO:
963 case EM_V800:
964 case EM_V850:
965 case EM_CYGNUS_V850:
966 case EM_VAX:
967 case EM_VISIUM:
968 case EM_X86_64:
969 case EM_L1OM:
970 case EM_K1OM:
971 case EM_XSTORMY16:
972 case EM_XTENSA:
973 case EM_XTENSA_OLD:
974 case EM_MICROBLAZE:
975 case EM_MICROBLAZE_OLD:
976 case EM_WEBASSEMBLY:
977 return true;
978
979 case EM_68HC05:
980 case EM_68HC08:
981 case EM_68HC11:
982 case EM_68HC16:
983 case EM_FX66:
984 case EM_ME16:
985 case EM_MMA:
986 case EM_NCPU:
987 case EM_NDR1:
988 case EM_PCP:
989 case EM_ST100:
990 case EM_ST19:
991 case EM_ST7:
992 case EM_ST9PLUS:
993 case EM_STARCORE:
994 case EM_SVX:
995 case EM_TINYJ:
996 default:
997 warn (_("Don't know about relocations on this machine architecture\n"));
998 return false;
999 }
1000}
1001
1002/* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1003 Returns TRUE upon success, FALSE otherwise. If successful then a
1004 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
1005 and the number of relocs loaded is placed in *NRELASP. It is the caller's
1006 responsibility to free the allocated buffer. */
1007
1008static bool
1009slurp_rela_relocs (Filedata * filedata,
1010 unsigned long rel_offset,
1011 unsigned long rel_size,
1012 Elf_Internal_Rela ** relasp,
1013 unsigned long * nrelasp)
1014{
1015 Elf_Internal_Rela * relas;
1016 size_t nrelas;
1017 unsigned int i;
1018
1019 if (is_32bit_elf)
1020 {
1021 Elf32_External_Rela * erelas;
1022
1023 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1024 rel_size, _("32-bit relocation data"));
1025 if (!erelas)
1026 return false;
1027
1028 nrelas = rel_size / sizeof (Elf32_External_Rela);
1029
1030 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1031 sizeof (Elf_Internal_Rela));
1032
1033 if (relas == NULL)
1034 {
1035 free (erelas);
1036 error (_("out of memory parsing relocs\n"));
1037 return false;
1038 }
1039
1040 for (i = 0; i < nrelas; i++)
1041 {
1042 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1043 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1044 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1045 }
1046
1047 free (erelas);
1048 }
1049 else
1050 {
1051 Elf64_External_Rela * erelas;
1052
1053 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1054 rel_size, _("64-bit relocation data"));
1055 if (!erelas)
1056 return false;
1057
1058 nrelas = rel_size / sizeof (Elf64_External_Rela);
1059
1060 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1061 sizeof (Elf_Internal_Rela));
1062
1063 if (relas == NULL)
1064 {
1065 free (erelas);
1066 error (_("out of memory parsing relocs\n"));
1067 return false;
1068 }
1069
1070 for (i = 0; i < nrelas; i++)
1071 {
1072 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1073 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1074 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1075
1076 /* The #ifdef BFD64 below is to prevent a compile time
1077 warning. We know that if we do not have a 64 bit data
1078 type that we will never execute this code anyway. */
1079#ifdef BFD64
1080 if (filedata->file_header.e_machine == EM_MIPS
1081 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1082 {
1083 /* In little-endian objects, r_info isn't really a
1084 64-bit little-endian value: it has a 32-bit
1085 little-endian symbol index followed by four
1086 individual byte fields. Reorder INFO
1087 accordingly. */
1088 bfd_vma inf = relas[i].r_info;
1089 inf = (((inf & 0xffffffff) << 32)
1090 | ((inf >> 56) & 0xff)
1091 | ((inf >> 40) & 0xff00)
1092 | ((inf >> 24) & 0xff0000)
1093 | ((inf >> 8) & 0xff000000));
1094 relas[i].r_info = inf;
1095 }
1096#endif /* BFD64 */
1097 }
1098
1099 free (erelas);
1100 }
1101
1102 *relasp = relas;
1103 *nrelasp = nrelas;
1104 return true;
1105}
1106
1107/* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1108 Returns TRUE upon success, FALSE otherwise. If successful then a
1109 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1110 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1111 responsibility to free the allocated buffer. */
1112
1113static bool
1114slurp_rel_relocs (Filedata * filedata,
1115 unsigned long rel_offset,
1116 unsigned long rel_size,
1117 Elf_Internal_Rela ** relsp,
1118 unsigned long * nrelsp)
1119{
1120 Elf_Internal_Rela * rels;
1121 size_t nrels;
1122 unsigned int i;
1123
1124 if (is_32bit_elf)
1125 {
1126 Elf32_External_Rel * erels;
1127
1128 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1129 rel_size, _("32-bit relocation data"));
1130 if (!erels)
1131 return false;
1132
1133 nrels = rel_size / sizeof (Elf32_External_Rel);
1134
1135 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1136
1137 if (rels == NULL)
1138 {
1139 free (erels);
1140 error (_("out of memory parsing relocs\n"));
1141 return false;
1142 }
1143
1144 for (i = 0; i < nrels; i++)
1145 {
1146 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1147 rels[i].r_info = BYTE_GET (erels[i].r_info);
1148 rels[i].r_addend = 0;
1149 }
1150
1151 free (erels);
1152 }
1153 else
1154 {
1155 Elf64_External_Rel * erels;
1156
1157 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1158 rel_size, _("64-bit relocation data"));
1159 if (!erels)
1160 return false;
1161
1162 nrels = rel_size / sizeof (Elf64_External_Rel);
1163
1164 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1165
1166 if (rels == NULL)
1167 {
1168 free (erels);
1169 error (_("out of memory parsing relocs\n"));
1170 return false;
1171 }
1172
1173 for (i = 0; i < nrels; i++)
1174 {
1175 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1176 rels[i].r_info = BYTE_GET (erels[i].r_info);
1177 rels[i].r_addend = 0;
1178
1179 /* The #ifdef BFD64 below is to prevent a compile time
1180 warning. We know that if we do not have a 64 bit data
1181 type that we will never execute this code anyway. */
1182#ifdef BFD64
1183 if (filedata->file_header.e_machine == EM_MIPS
1184 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1185 {
1186 /* In little-endian objects, r_info isn't really a
1187 64-bit little-endian value: it has a 32-bit
1188 little-endian symbol index followed by four
1189 individual byte fields. Reorder INFO
1190 accordingly. */
1191 bfd_vma inf = rels[i].r_info;
1192 inf = (((inf & 0xffffffff) << 32)
1193 | ((inf >> 56) & 0xff)
1194 | ((inf >> 40) & 0xff00)
1195 | ((inf >> 24) & 0xff0000)
1196 | ((inf >> 8) & 0xff000000));
1197 rels[i].r_info = inf;
1198 }
1199#endif /* BFD64 */
1200 }
1201
1202 free (erels);
1203 }
1204
1205 *relsp = rels;
1206 *nrelsp = nrels;
1207 return true;
1208}
1209
1210/* Returns the reloc type extracted from the reloc info field. */
1211
1212static unsigned int
1213get_reloc_type (Filedata * filedata, bfd_vma reloc_info)
1214{
1215 if (is_32bit_elf)
1216 return ELF32_R_TYPE (reloc_info);
1217
1218 switch (filedata->file_header.e_machine)
1219 {
1220 case EM_MIPS:
1221 /* Note: We assume that reloc_info has already been adjusted for us. */
1222 return ELF64_MIPS_R_TYPE (reloc_info);
1223
1224 case EM_SPARCV9:
1225 return ELF64_R_TYPE_ID (reloc_info);
1226
1227 default:
1228 return ELF64_R_TYPE (reloc_info);
1229 }
1230}
1231
1232/* Return the symbol index extracted from the reloc info field. */
1233
1234static bfd_vma
1235get_reloc_symindex (bfd_vma reloc_info)
1236{
1237 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1238}
1239
1240static inline bool
1241uses_msp430x_relocs (Filedata * filedata)
1242{
1243 return
1244 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
1245 /* GCC uses osabi == ELFOSBI_STANDALONE. */
1246 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1247 /* TI compiler uses ELFOSABI_NONE. */
1248 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1249}
1250
1251/* Display the contents of the relocation data found at the specified
1252 offset. */
1253
1254static bool
1255dump_relocations (Filedata * filedata,
1256 unsigned long rel_offset,
1257 unsigned long rel_size,
1258 Elf_Internal_Sym * symtab,
1259 unsigned long nsyms,
1260 char * strtab,
1261 unsigned long strtablen,
1262 int is_rela,
1263 bool is_dynsym)
1264{
1265 unsigned long i;
1266 Elf_Internal_Rela * rels;
1267 bool res = true;
1268
1269 if (is_rela == UNKNOWN)
1270 is_rela = guess_is_rela (filedata->file_header.e_machine);
1271
1272 if (is_rela)
1273 {
1274 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1275 return false;
1276 }
1277 else
1278 {
1279 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1280 return false;
1281 }
1282
1283 if (is_32bit_elf)
1284 {
1285 if (is_rela)
1286 {
1287 if (do_wide)
1288 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1289 else
1290 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1291 }
1292 else
1293 {
1294 if (do_wide)
1295 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1296 else
1297 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1298 }
1299 }
1300 else
1301 {
1302 if (is_rela)
1303 {
1304 if (do_wide)
1305 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
1306 else
1307 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1308 }
1309 else
1310 {
1311 if (do_wide)
1312 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
1313 else
1314 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1315 }
1316 }
1317
1318 for (i = 0; i < rel_size; i++)
1319 {
1320 const char * rtype;
1321 bfd_vma offset;
1322 bfd_vma inf;
1323 bfd_vma symtab_index;
1324 bfd_vma type;
1325
1326 offset = rels[i].r_offset;
1327 inf = rels[i].r_info;
1328
1329 type = get_reloc_type (filedata, inf);
1330 symtab_index = get_reloc_symindex (inf);
1331
1332 if (is_32bit_elf)
1333 {
1334 printf ("%8.8lx %8.8lx ",
1335 (unsigned long) offset & 0xffffffff,
1336 (unsigned long) inf & 0xffffffff);
1337 }
1338 else
1339 {
1340 printf (do_wide
1341 ? "%16.16" BFD_VMA_FMT "x %16.16" BFD_VMA_FMT "x "
1342 : "%12.12" BFD_VMA_FMT "x %12.12" BFD_VMA_FMT "x ",
1343 offset, inf);
1344 }
1345
1346 switch (filedata->file_header.e_machine)
1347 {
1348 default:
1349 rtype = NULL;
1350 break;
1351
1352 case EM_AARCH64:
1353 rtype = elf_aarch64_reloc_type (type);
1354 break;
1355
1356 case EM_M32R:
1357 case EM_CYGNUS_M32R:
1358 rtype = elf_m32r_reloc_type (type);
1359 break;
1360
1361 case EM_386:
1362 case EM_IAMCU:
1363 rtype = elf_i386_reloc_type (type);
1364 break;
1365
1366 case EM_68HC11:
1367 case EM_68HC12:
1368 rtype = elf_m68hc11_reloc_type (type);
1369 break;
1370
1371 case EM_S12Z:
1372 rtype = elf_s12z_reloc_type (type);
1373 break;
1374
1375 case EM_68K:
1376 rtype = elf_m68k_reloc_type (type);
1377 break;
1378
1379 case EM_960:
1380 rtype = elf_i960_reloc_type (type);
1381 break;
1382
1383 case EM_AVR:
1384 case EM_AVR_OLD:
1385 rtype = elf_avr_reloc_type (type);
1386 break;
1387
1388 case EM_OLD_SPARCV9:
1389 case EM_SPARC32PLUS:
1390 case EM_SPARCV9:
1391 case EM_SPARC:
1392 rtype = elf_sparc_reloc_type (type);
1393 break;
1394
1395 case EM_SPU:
1396 rtype = elf_spu_reloc_type (type);
1397 break;
1398
1399 case EM_V800:
1400 rtype = v800_reloc_type (type);
1401 break;
1402 case EM_V850:
1403 case EM_CYGNUS_V850:
1404 rtype = v850_reloc_type (type);
1405 break;
1406
1407 case EM_D10V:
1408 case EM_CYGNUS_D10V:
1409 rtype = elf_d10v_reloc_type (type);
1410 break;
1411
1412 case EM_D30V:
1413 case EM_CYGNUS_D30V:
1414 rtype = elf_d30v_reloc_type (type);
1415 break;
1416
1417 case EM_DLX:
1418 rtype = elf_dlx_reloc_type (type);
1419 break;
1420
1421 case EM_SH:
1422 rtype = elf_sh_reloc_type (type);
1423 break;
1424
1425 case EM_MN10300:
1426 case EM_CYGNUS_MN10300:
1427 rtype = elf_mn10300_reloc_type (type);
1428 break;
1429
1430 case EM_MN10200:
1431 case EM_CYGNUS_MN10200:
1432 rtype = elf_mn10200_reloc_type (type);
1433 break;
1434
1435 case EM_FR30:
1436 case EM_CYGNUS_FR30:
1437 rtype = elf_fr30_reloc_type (type);
1438 break;
1439
1440 case EM_CYGNUS_FRV:
1441 rtype = elf_frv_reloc_type (type);
1442 break;
1443
1444 case EM_CSKY:
1445 rtype = elf_csky_reloc_type (type);
1446 break;
1447
1448 case EM_FT32:
1449 rtype = elf_ft32_reloc_type (type);
1450 break;
1451
1452 case EM_MCORE:
1453 rtype = elf_mcore_reloc_type (type);
1454 break;
1455
1456 case EM_MMIX:
1457 rtype = elf_mmix_reloc_type (type);
1458 break;
1459
1460 case EM_MOXIE:
1461 rtype = elf_moxie_reloc_type (type);
1462 break;
1463
1464 case EM_MSP430:
1465 if (uses_msp430x_relocs (filedata))
1466 {
1467 rtype = elf_msp430x_reloc_type (type);
1468 break;
1469 }
1470 /* Fall through. */
1471 case EM_MSP430_OLD:
1472 rtype = elf_msp430_reloc_type (type);
1473 break;
1474
1475 case EM_NDS32:
1476 rtype = elf_nds32_reloc_type (type);
1477 break;
1478
1479 case EM_PPC:
1480 rtype = elf_ppc_reloc_type (type);
1481 break;
1482
1483 case EM_PPC64:
1484 rtype = elf_ppc64_reloc_type (type);
1485 break;
1486
1487 case EM_MIPS:
1488 case EM_MIPS_RS3_LE:
1489 rtype = elf_mips_reloc_type (type);
1490 break;
1491
1492 case EM_RISCV:
1493 rtype = elf_riscv_reloc_type (type);
1494 break;
1495
1496 case EM_ALPHA:
1497 rtype = elf_alpha_reloc_type (type);
1498 break;
1499
1500 case EM_ARM:
1501 rtype = elf_arm_reloc_type (type);
1502 break;
1503
1504 case EM_ARC:
1505 case EM_ARC_COMPACT:
1506 case EM_ARC_COMPACT2:
1507 rtype = elf_arc_reloc_type (type);
1508 break;
1509
1510 case EM_PARISC:
1511 rtype = elf_hppa_reloc_type (type);
1512 break;
1513
1514 case EM_H8_300:
1515 case EM_H8_300H:
1516 case EM_H8S:
1517 rtype = elf_h8_reloc_type (type);
1518 break;
1519
1520 case EM_OR1K:
1521 rtype = elf_or1k_reloc_type (type);
1522 break;
1523
1524 case EM_PJ:
1525 case EM_PJ_OLD:
1526 rtype = elf_pj_reloc_type (type);
1527 break;
1528 case EM_IA_64:
1529 rtype = elf_ia64_reloc_type (type);
1530 break;
1531
1532 case EM_CRIS:
1533 rtype = elf_cris_reloc_type (type);
1534 break;
1535
1536 case EM_860:
1537 rtype = elf_i860_reloc_type (type);
1538 break;
1539
1540 case EM_X86_64:
1541 case EM_L1OM:
1542 case EM_K1OM:
1543 rtype = elf_x86_64_reloc_type (type);
1544 break;
1545
1546 case EM_S370:
1547 rtype = i370_reloc_type (type);
1548 break;
1549
1550 case EM_S390_OLD:
1551 case EM_S390:
1552 rtype = elf_s390_reloc_type (type);
1553 break;
1554
1555 case EM_SCORE:
1556 rtype = elf_score_reloc_type (type);
1557 break;
1558
1559 case EM_XSTORMY16:
1560 rtype = elf_xstormy16_reloc_type (type);
1561 break;
1562
1563 case EM_CRX:
1564 rtype = elf_crx_reloc_type (type);
1565 break;
1566
1567 case EM_VAX:
1568 rtype = elf_vax_reloc_type (type);
1569 break;
1570
1571 case EM_VISIUM:
1572 rtype = elf_visium_reloc_type (type);
1573 break;
1574
1575 case EM_BPF:
1576 rtype = elf_bpf_reloc_type (type);
1577 break;
1578
1579 case EM_ADAPTEVA_EPIPHANY:
1580 rtype = elf_epiphany_reloc_type (type);
1581 break;
1582
1583 case EM_IP2K:
1584 case EM_IP2K_OLD:
1585 rtype = elf_ip2k_reloc_type (type);
1586 break;
1587
1588 case EM_IQ2000:
1589 rtype = elf_iq2000_reloc_type (type);
1590 break;
1591
1592 case EM_XTENSA_OLD:
1593 case EM_XTENSA:
1594 rtype = elf_xtensa_reloc_type (type);
1595 break;
1596
1597 case EM_LATTICEMICO32:
1598 rtype = elf_lm32_reloc_type (type);
1599 break;
1600
1601 case EM_M32C_OLD:
1602 case EM_M32C:
1603 rtype = elf_m32c_reloc_type (type);
1604 break;
1605
1606 case EM_MT:
1607 rtype = elf_mt_reloc_type (type);
1608 break;
1609
1610 case EM_BLACKFIN:
1611 rtype = elf_bfin_reloc_type (type);
1612 break;
1613
1614 case EM_CYGNUS_MEP:
1615 rtype = elf_mep_reloc_type (type);
1616 break;
1617
1618 case EM_CR16:
1619 rtype = elf_cr16_reloc_type (type);
1620 break;
1621
1622 case EM_MICROBLAZE:
1623 case EM_MICROBLAZE_OLD:
1624 rtype = elf_microblaze_reloc_type (type);
1625 break;
1626
1627 case EM_RL78:
1628 rtype = elf_rl78_reloc_type (type);
1629 break;
1630
1631 case EM_RX:
1632 rtype = elf_rx_reloc_type (type);
1633 break;
1634
1635 case EM_METAG:
1636 rtype = elf_metag_reloc_type (type);
1637 break;
1638
1639 case EM_XC16X:
1640 case EM_C166:
1641 rtype = elf_xc16x_reloc_type (type);
1642 break;
1643
1644 case EM_TI_C6000:
1645 rtype = elf_tic6x_reloc_type (type);
1646 break;
1647
1648 case EM_TILEGX:
1649 rtype = elf_tilegx_reloc_type (type);
1650 break;
1651
1652 case EM_TILEPRO:
1653 rtype = elf_tilepro_reloc_type (type);
1654 break;
1655
1656 case EM_WEBASSEMBLY:
1657 rtype = elf_wasm32_reloc_type (type);
1658 break;
1659
1660 case EM_XGATE:
1661 rtype = elf_xgate_reloc_type (type);
1662 break;
1663
1664 case EM_ALTERA_NIOS2:
1665 rtype = elf_nios2_reloc_type (type);
1666 break;
1667
1668 case EM_TI_PRU:
1669 rtype = elf_pru_reloc_type (type);
1670 break;
1671
1672 case EM_NFP:
1673 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
1674 rtype = elf_nfp3200_reloc_type (type);
1675 else
1676 rtype = elf_nfp_reloc_type (type);
1677 break;
1678
1679 case EM_Z80:
1680 rtype = elf_z80_reloc_type (type);
1681 break;
1682
1683 case EM_LOONGARCH:
1684 rtype = elf_loongarch_reloc_type (type);
1685 break;
1686
1687 }
1688
1689 if (rtype == NULL)
1690 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
1691 else
1692 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
1693
1694 if (filedata->file_header.e_machine == EM_ALPHA
1695 && rtype != NULL
1696 && streq (rtype, "R_ALPHA_LITUSE")
1697 && is_rela)
1698 {
1699 switch (rels[i].r_addend)
1700 {
1701 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
1702 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
1703 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
1704 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
1705 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
1706 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
1707 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
1708 default: rtype = NULL;
1709 }
1710
1711 if (rtype)
1712 printf (" (%s)", rtype);
1713 else
1714 {
1715 putchar (' ');
1716 printf (_("<unknown addend: %lx>"),
1717 (unsigned long) rels[i].r_addend);
1718 res = false;
1719 }
1720 }
1721 else if (symtab_index)
1722 {
1723 if (symtab == NULL || symtab_index >= nsyms)
1724 {
1725 error (_(" bad symbol index: %08lx in reloc\n"),
1726 (unsigned long) symtab_index);
1727 res = false;
1728 }
1729 else
1730 {
1731 Elf_Internal_Sym * psym;
1732 const char * version_string;
1733 enum versioned_symbol_info sym_info;
1734 unsigned short vna_other;
1735
1736 psym = symtab + symtab_index;
1737
1738 version_string
1739 = get_symbol_version_string (filedata, is_dynsym,
1740 strtab, strtablen,
1741 symtab_index,
1742 psym,
1743 &sym_info,
1744 &vna_other);
1745
1746 printf (" ");
1747
1748 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
1749 {
1750 const char * name;
1751 unsigned int len;
1752 unsigned int width = is_32bit_elf ? 8 : 14;
1753
1754 /* Relocations against GNU_IFUNC symbols do not use the value
1755 of the symbol as the address to relocate against. Instead
1756 they invoke the function named by the symbol and use its
1757 result as the address for relocation.
1758
1759 To indicate this to the user, do not display the value of
1760 the symbol in the "Symbols's Value" field. Instead show
1761 its name followed by () as a hint that the symbol is
1762 invoked. */
1763
1764 if (strtab == NULL
1765 || psym->st_name == 0
1766 || psym->st_name >= strtablen)
1767 name = "??";
1768 else
1769 name = strtab + psym->st_name;
1770
1771 len = print_symbol (width, name);
1772 if (version_string)
1773 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1774 version_string);
1775 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
1776 }
1777 else
1778 {
1779 print_vma (psym->st_value, LONG_HEX);
1780
1781 printf (is_32bit_elf ? " " : " ");
1782 }
1783
1784 if (psym->st_name == 0)
1785 {
1786 const char * sec_name = "<null>";
1787 char name_buf[40];
1788
1789 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1790 {
1791 if (psym->st_shndx < filedata->file_header.e_shnum)
1792 sec_name = section_name_print (filedata,
1793 filedata->section_headers
1794 + psym->st_shndx);
1795 else if (psym->st_shndx == SHN_ABS)
1796 sec_name = "ABS";
1797 else if (psym->st_shndx == SHN_COMMON)
1798 sec_name = "COMMON";
1799 else if ((filedata->file_header.e_machine == EM_MIPS
1800 && psym->st_shndx == SHN_MIPS_SCOMMON)
1801 || (filedata->file_header.e_machine == EM_TI_C6000
1802 && psym->st_shndx == SHN_TIC6X_SCOMMON))
1803 sec_name = "SCOMMON";
1804 else if (filedata->file_header.e_machine == EM_MIPS
1805 && psym->st_shndx == SHN_MIPS_SUNDEFINED)
1806 sec_name = "SUNDEF";
1807 else if ((filedata->file_header.e_machine == EM_X86_64
1808 || filedata->file_header.e_machine == EM_L1OM
1809 || filedata->file_header.e_machine == EM_K1OM)
1810 && psym->st_shndx == SHN_X86_64_LCOMMON)
1811 sec_name = "LARGE_COMMON";
1812 else if (filedata->file_header.e_machine == EM_IA_64
1813 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1814 && psym->st_shndx == SHN_IA_64_ANSI_COMMON)
1815 sec_name = "ANSI_COM";
1816 else if (is_ia64_vms (filedata)
1817 && psym->st_shndx == SHN_IA_64_VMS_SYMVEC)
1818 sec_name = "VMS_SYMVEC";
1819 else
1820 {
1821 sprintf (name_buf, "<section 0x%x>",
1822 (unsigned int) psym->st_shndx);
1823 sec_name = name_buf;
1824 }
1825 }
1826 print_symbol (22, sec_name);
1827 }
1828 else if (strtab == NULL)
1829 printf (_("<string table index: %3ld>"), psym->st_name);
1830 else if (psym->st_name >= strtablen)
1831 {
1832 error (_("<corrupt string table index: %3ld>\n"),
1833 psym->st_name);
1834 res = false;
1835 }
1836 else
1837 {
1838 print_symbol (22, strtab + psym->st_name);
1839 if (version_string)
1840 printf (sym_info == symbol_public ? "@@%s" : "@%s",
1841 version_string);
1842 }
1843
1844 if (is_rela)
1845 {
1846 bfd_vma off = rels[i].r_addend;
1847
1848 if ((bfd_signed_vma) off < 0)
1849 printf (" - %" BFD_VMA_FMT "x", - off);
1850 else
1851 printf (" + %" BFD_VMA_FMT "x", off);
1852 }
1853 }
1854 }
1855 else if (is_rela)
1856 {
1857 bfd_vma off = rels[i].r_addend;
1858
1859 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
1860 if ((bfd_signed_vma) off < 0)
1861 printf ("-%" BFD_VMA_FMT "x", - off);
1862 else
1863 printf ("%" BFD_VMA_FMT "x", off);
1864 }
1865
1866 if (filedata->file_header.e_machine == EM_SPARCV9
1867 && rtype != NULL
1868 && streq (rtype, "R_SPARC_OLO10"))
1869 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (inf));
1870
1871 putchar ('\n');
1872
1873#ifdef BFD64
1874 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
1875 {
1876 bfd_vma type2 = ELF64_MIPS_R_TYPE2 (inf);
1877 bfd_vma type3 = ELF64_MIPS_R_TYPE3 (inf);
1878 const char * rtype2 = elf_mips_reloc_type (type2);
1879 const char * rtype3 = elf_mips_reloc_type (type3);
1880
1881 printf (" Type2: ");
1882
1883 if (rtype2 == NULL)
1884 printf (_("unrecognized: %-7lx"),
1885 (unsigned long) type2 & 0xffffffff);
1886 else
1887 printf ("%-17.17s", rtype2);
1888
1889 printf ("\n Type3: ");
1890
1891 if (rtype3 == NULL)
1892 printf (_("unrecognized: %-7lx"),
1893 (unsigned long) type3 & 0xffffffff);
1894 else
1895 printf ("%-17.17s", rtype3);
1896
1897 putchar ('\n');
1898 }
1899#endif /* BFD64 */
1900 }
1901
1902 free (rels);
1903
1904 return res;
1905}
1906
1907static const char *
1908get_aarch64_dynamic_type (unsigned long type)
1909{
1910 switch (type)
1911 {
1912 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
1913 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
1914 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
1915 default:
1916 return NULL;
1917 }
1918}
1919
1920static const char *
1921get_mips_dynamic_type (unsigned long type)
1922{
1923 switch (type)
1924 {
1925 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1926 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1927 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1928 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1929 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1930 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1931 case DT_MIPS_MSYM: return "MIPS_MSYM";
1932 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1933 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1934 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1935 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1936 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1937 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1938 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1939 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1940 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1941 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1942 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
1943 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1944 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1945 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1946 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1947 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1948 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1949 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1950 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1951 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1952 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1953 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1954 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1955 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1956 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1957 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1958 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1959 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1960 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1961 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1962 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1963 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1964 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1965 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1966 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1967 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1968 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1969 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
1970 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
1971 case DT_MIPS_XHASH: return "MIPS_XHASH";
1972 default:
1973 return NULL;
1974 }
1975}
1976
1977static const char *
1978get_sparc64_dynamic_type (unsigned long type)
1979{
1980 switch (type)
1981 {
1982 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1983 default:
1984 return NULL;
1985 }
1986}
1987
1988static const char *
1989get_ppc_dynamic_type (unsigned long type)
1990{
1991 switch (type)
1992 {
1993 case DT_PPC_GOT: return "PPC_GOT";
1994 case DT_PPC_OPT: return "PPC_OPT";
1995 default:
1996 return NULL;
1997 }
1998}
1999
2000static const char *
2001get_ppc64_dynamic_type (unsigned long type)
2002{
2003 switch (type)
2004 {
2005 case DT_PPC64_GLINK: return "PPC64_GLINK";
2006 case DT_PPC64_OPD: return "PPC64_OPD";
2007 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
2008 case DT_PPC64_OPT: return "PPC64_OPT";
2009 default:
2010 return NULL;
2011 }
2012}
2013
2014static const char *
2015get_parisc_dynamic_type (unsigned long type)
2016{
2017 switch (type)
2018 {
2019 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
2020 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
2021 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
2022 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
2023 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
2024 case DT_HP_PREINIT: return "HP_PREINIT";
2025 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
2026 case DT_HP_NEEDED: return "HP_NEEDED";
2027 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
2028 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
2029 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
2030 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
2031 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
2032 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
2033 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
2034 case DT_HP_FILTERED: return "HP_FILTERED";
2035 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
2036 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
2037 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
2038 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
2039 case DT_PLT: return "PLT";
2040 case DT_PLT_SIZE: return "PLT_SIZE";
2041 case DT_DLT: return "DLT";
2042 case DT_DLT_SIZE: return "DLT_SIZE";
2043 default:
2044 return NULL;
2045 }
2046}
2047
2048static const char *
2049get_ia64_dynamic_type (unsigned long type)
2050{
2051 switch (type)
2052 {
2053 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2054 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2055 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2056 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2057 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2058 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2059 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2060 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2061 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2062 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2063 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2064 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2065 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2066 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2067 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2068 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2069 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2070 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2071 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2072 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2073 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2074 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2075 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2076 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2077 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2078 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2079 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2080 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2081 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2082 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2083 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
2084 default:
2085 return NULL;
2086 }
2087}
2088
2089static const char *
2090get_solaris_section_type (unsigned long type)
2091{
2092 switch (type)
2093 {
2094 case 0x6fffffee: return "SUNW_ancillary";
2095 case 0x6fffffef: return "SUNW_capchain";
2096 case 0x6ffffff0: return "SUNW_capinfo";
2097 case 0x6ffffff1: return "SUNW_symsort";
2098 case 0x6ffffff2: return "SUNW_tlssort";
2099 case 0x6ffffff3: return "SUNW_LDYNSYM";
2100 case 0x6ffffff4: return "SUNW_dof";
2101 case 0x6ffffff5: return "SUNW_cap";
2102 case 0x6ffffff6: return "SUNW_SIGNATURE";
2103 case 0x6ffffff7: return "SUNW_ANNOTATE";
2104 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2105 case 0x6ffffff9: return "SUNW_DEBUG";
2106 case 0x6ffffffa: return "SUNW_move";
2107 case 0x6ffffffb: return "SUNW_COMDAT";
2108 case 0x6ffffffc: return "SUNW_syminfo";
2109 case 0x6ffffffd: return "SUNW_verdef";
2110 case 0x6ffffffe: return "SUNW_verneed";
2111 case 0x6fffffff: return "SUNW_versym";
2112 case 0x70000000: return "SPARC_GOTDATA";
2113 default: return NULL;
2114 }
2115}
2116
2117static const char *
2118get_alpha_dynamic_type (unsigned long type)
2119{
2120 switch (type)
2121 {
2122 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2123 default: return NULL;
2124 }
2125}
2126
2127static const char *
2128get_score_dynamic_type (unsigned long type)
2129{
2130 switch (type)
2131 {
2132 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2133 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2134 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2135 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2136 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2137 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
2138 default: return NULL;
2139 }
2140}
2141
2142static const char *
2143get_tic6x_dynamic_type (unsigned long type)
2144{
2145 switch (type)
2146 {
2147 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2148 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2149 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2150 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2151 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2152 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
2153 default: return NULL;
2154 }
2155}
2156
2157static const char *
2158get_nios2_dynamic_type (unsigned long type)
2159{
2160 switch (type)
2161 {
2162 case DT_NIOS2_GP: return "NIOS2_GP";
2163 default: return NULL;
2164 }
2165}
2166
2167static const char *
2168get_solaris_dynamic_type (unsigned long type)
2169{
2170 switch (type)
2171 {
2172 case 0x6000000d: return "SUNW_AUXILIARY";
2173 case 0x6000000e: return "SUNW_RTLDINF";
2174 case 0x6000000f: return "SUNW_FILTER";
2175 case 0x60000010: return "SUNW_CAP";
2176 case 0x60000011: return "SUNW_SYMTAB";
2177 case 0x60000012: return "SUNW_SYMSZ";
2178 case 0x60000013: return "SUNW_SORTENT";
2179 case 0x60000014: return "SUNW_SYMSORT";
2180 case 0x60000015: return "SUNW_SYMSORTSZ";
2181 case 0x60000016: return "SUNW_TLSSORT";
2182 case 0x60000017: return "SUNW_TLSSORTSZ";
2183 case 0x60000018: return "SUNW_CAPINFO";
2184 case 0x60000019: return "SUNW_STRPAD";
2185 case 0x6000001a: return "SUNW_CAPCHAIN";
2186 case 0x6000001b: return "SUNW_LDMACH";
2187 case 0x6000001d: return "SUNW_CAPCHAINENT";
2188 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2189 case 0x60000021: return "SUNW_PARENT";
2190 case 0x60000023: return "SUNW_ASLR";
2191 case 0x60000025: return "SUNW_RELAX";
2192 case 0x60000029: return "SUNW_NXHEAP";
2193 case 0x6000002b: return "SUNW_NXSTACK";
2194
2195 case 0x70000001: return "SPARC_REGISTER";
2196 case 0x7ffffffd: return "AUXILIARY";
2197 case 0x7ffffffe: return "USED";
2198 case 0x7fffffff: return "FILTER";
2199
2200 default: return NULL;
2201 }
2202}
2203
2204static const char *
2205get_dynamic_type (Filedata * filedata, unsigned long type)
2206{
2207 static char buff[64];
2208
2209 switch (type)
2210 {
2211 case DT_NULL: return "NULL";
2212 case DT_NEEDED: return "NEEDED";
2213 case DT_PLTRELSZ: return "PLTRELSZ";
2214 case DT_PLTGOT: return "PLTGOT";
2215 case DT_HASH: return "HASH";
2216 case DT_STRTAB: return "STRTAB";
2217 case DT_SYMTAB: return "SYMTAB";
2218 case DT_RELA: return "RELA";
2219 case DT_RELASZ: return "RELASZ";
2220 case DT_RELAENT: return "RELAENT";
2221 case DT_STRSZ: return "STRSZ";
2222 case DT_SYMENT: return "SYMENT";
2223 case DT_INIT: return "INIT";
2224 case DT_FINI: return "FINI";
2225 case DT_SONAME: return "SONAME";
2226 case DT_RPATH: return "RPATH";
2227 case DT_SYMBOLIC: return "SYMBOLIC";
2228 case DT_REL: return "REL";
2229 case DT_RELSZ: return "RELSZ";
2230 case DT_RELENT: return "RELENT";
2231 case DT_RELR: return "RELR";
2232 case DT_RELRSZ: return "RELRSZ";
2233 case DT_RELRENT: return "RELRENT";
2234 case DT_PLTREL: return "PLTREL";
2235 case DT_DEBUG: return "DEBUG";
2236 case DT_TEXTREL: return "TEXTREL";
2237 case DT_JMPREL: return "JMPREL";
2238 case DT_BIND_NOW: return "BIND_NOW";
2239 case DT_INIT_ARRAY: return "INIT_ARRAY";
2240 case DT_FINI_ARRAY: return "FINI_ARRAY";
2241 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2242 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2243 case DT_RUNPATH: return "RUNPATH";
2244 case DT_FLAGS: return "FLAGS";
2245
2246 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2247 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2248 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2249
2250 case DT_CHECKSUM: return "CHECKSUM";
2251 case DT_PLTPADSZ: return "PLTPADSZ";
2252 case DT_MOVEENT: return "MOVEENT";
2253 case DT_MOVESZ: return "MOVESZ";
2254 case DT_FEATURE: return "FEATURE";
2255 case DT_POSFLAG_1: return "POSFLAG_1";
2256 case DT_SYMINSZ: return "SYMINSZ";
2257 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
2258
2259 case DT_ADDRRNGLO: return "ADDRRNGLO";
2260 case DT_CONFIG: return "CONFIG";
2261 case DT_DEPAUDIT: return "DEPAUDIT";
2262 case DT_AUDIT: return "AUDIT";
2263 case DT_PLTPAD: return "PLTPAD";
2264 case DT_MOVETAB: return "MOVETAB";
2265 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
2266
2267 case DT_VERSYM: return "VERSYM";
2268
2269 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2270 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2271 case DT_RELACOUNT: return "RELACOUNT";
2272 case DT_RELCOUNT: return "RELCOUNT";
2273 case DT_FLAGS_1: return "FLAGS_1";
2274 case DT_VERDEF: return "VERDEF";
2275 case DT_VERDEFNUM: return "VERDEFNUM";
2276 case DT_VERNEED: return "VERNEED";
2277 case DT_VERNEEDNUM: return "VERNEEDNUM";
2278
2279 case DT_AUXILIARY: return "AUXILIARY";
2280 case DT_USED: return "USED";
2281 case DT_FILTER: return "FILTER";
2282
2283 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2284 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2285 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2286 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2287 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2288 case DT_GNU_HASH: return "GNU_HASH";
2289 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
2290
2291 default:
2292 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2293 {
2294 const char * result;
2295
2296 switch (filedata->file_header.e_machine)
2297 {
2298 case EM_AARCH64:
2299 result = get_aarch64_dynamic_type (type);
2300 break;
2301 case EM_MIPS:
2302 case EM_MIPS_RS3_LE:
2303 result = get_mips_dynamic_type (type);
2304 break;
2305 case EM_SPARCV9:
2306 result = get_sparc64_dynamic_type (type);
2307 break;
2308 case EM_PPC:
2309 result = get_ppc_dynamic_type (type);
2310 break;
2311 case EM_PPC64:
2312 result = get_ppc64_dynamic_type (type);
2313 break;
2314 case EM_IA_64:
2315 result = get_ia64_dynamic_type (type);
2316 break;
2317 case EM_ALPHA:
2318 result = get_alpha_dynamic_type (type);
2319 break;
2320 case EM_SCORE:
2321 result = get_score_dynamic_type (type);
2322 break;
2323 case EM_TI_C6000:
2324 result = get_tic6x_dynamic_type (type);
2325 break;
2326 case EM_ALTERA_NIOS2:
2327 result = get_nios2_dynamic_type (type);
2328 break;
2329 default:
2330 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2331 result = get_solaris_dynamic_type (type);
2332 else
2333 result = NULL;
2334 break;
2335 }
2336
2337 if (result != NULL)
2338 return result;
2339
2340 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2341 }
2342 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2343 || (filedata->file_header.e_machine == EM_PARISC
2344 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2345 {
2346 const char * result;
2347
2348 switch (filedata->file_header.e_machine)
2349 {
2350 case EM_PARISC:
2351 result = get_parisc_dynamic_type (type);
2352 break;
2353 case EM_IA_64:
2354 result = get_ia64_dynamic_type (type);
2355 break;
2356 default:
2357 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2358 result = get_solaris_dynamic_type (type);
2359 else
2360 result = NULL;
2361 break;
2362 }
2363
2364 if (result != NULL)
2365 return result;
2366
2367 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2368 type);
2369 }
2370 else
2371 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2372
2373 return buff;
2374 }
2375}
2376
2377static bool get_program_headers (Filedata *);
2378static bool get_dynamic_section (Filedata *);
2379
2380static void
2381locate_dynamic_section (Filedata *filedata)
2382{
2383 unsigned long dynamic_addr = 0;
2384 bfd_size_type dynamic_size = 0;
2385
2386 if (filedata->file_header.e_phnum != 0
2387 && get_program_headers (filedata))
2388 {
2389 Elf_Internal_Phdr *segment;
2390 unsigned int i;
2391
2392 for (i = 0, segment = filedata->program_headers;
2393 i < filedata->file_header.e_phnum;
2394 i++, segment++)
2395 {
2396 if (segment->p_type == PT_DYNAMIC)
2397 {
2398 dynamic_addr = segment->p_offset;
2399 dynamic_size = segment->p_filesz;
2400
2401 if (filedata->section_headers != NULL)
2402 {
2403 Elf_Internal_Shdr *sec;
2404
2405 sec = find_section (filedata, ".dynamic");
2406 if (sec != NULL)
2407 {
2408 if (sec->sh_size == 0
2409 || sec->sh_type == SHT_NOBITS)
2410 {
2411 dynamic_addr = 0;
2412 dynamic_size = 0;
2413 }
2414 else
2415 {
2416 dynamic_addr = sec->sh_offset;
2417 dynamic_size = sec->sh_size;
2418 }
2419 }
2420 }
2421
2422 if (dynamic_addr > filedata->file_size
2423 || (dynamic_size > filedata->file_size - dynamic_addr))
2424 {
2425 dynamic_addr = 0;
2426 dynamic_size = 0;
2427 }
2428 break;
2429 }
2430 }
2431 }
2432 filedata->dynamic_addr = dynamic_addr;
2433 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
2434}
2435
2436static bool
2437is_pie (Filedata *filedata)
2438{
2439 Elf_Internal_Dyn *entry;
2440
2441 if (filedata->dynamic_size == 0)
2442 locate_dynamic_section (filedata);
2443 if (filedata->dynamic_size <= 1)
2444 return false;
2445
2446 if (!get_dynamic_section (filedata))
2447 return false;
2448
2449 for (entry = filedata->dynamic_section;
2450 entry < filedata->dynamic_section + filedata->dynamic_nent;
2451 entry++)
2452 {
2453 if (entry->d_tag == DT_FLAGS_1)
2454 {
2455 if ((entry->d_un.d_val & DF_1_PIE) != 0)
2456 return true;
2457 break;
2458 }
2459 }
2460 return false;
2461}
2462
2463static char *
2464get_file_type (Filedata *filedata)
2465{
2466 unsigned e_type = filedata->file_header.e_type;
2467 static char buff[64];
2468
2469 switch (e_type)
2470 {
2471 case ET_NONE: return _("NONE (None)");
2472 case ET_REL: return _("REL (Relocatable file)");
2473 case ET_EXEC: return _("EXEC (Executable file)");
2474 case ET_DYN:
2475 if (is_pie (filedata))
2476 return _("DYN (Position-Independent Executable file)");
2477 else
2478 return _("DYN (Shared object file)");
2479 case ET_CORE: return _("CORE (Core file)");
2480
2481 default:
2482 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2483 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2484 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2485 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2486 else
2487 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2488 return buff;
2489 }
2490}
2491
2492static char *
2493get_machine_name (unsigned e_machine)
2494{
2495 static char buff[64]; /* XXX */
2496
2497 switch (e_machine)
2498 {
2499 /* Please keep this switch table sorted by increasing EM_ value. */
2500 /* 0 */
2501 case EM_NONE: return _("None");
2502 case EM_M32: return "WE32100";
2503 case EM_SPARC: return "Sparc";
2504 case EM_386: return "Intel 80386";
2505 case EM_68K: return "MC68000";
2506 case EM_88K: return "MC88000";
2507 case EM_IAMCU: return "Intel MCU";
2508 case EM_860: return "Intel 80860";
2509 case EM_MIPS: return "MIPS R3000";
2510 case EM_S370: return "IBM System/370";
2511 /* 10 */
2512 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
2513 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
2514 case EM_PARISC: return "HPPA";
2515 case EM_VPP550: return "Fujitsu VPP500";
2516 case EM_SPARC32PLUS: return "Sparc v8+" ;
2517 case EM_960: return "Intel 80960";
2518 case EM_PPC: return "PowerPC";
2519 /* 20 */
2520 case EM_PPC64: return "PowerPC64";
2521 case EM_S390_OLD:
2522 case EM_S390: return "IBM S/390";
2523 case EM_SPU: return "SPU";
2524 /* 30 */
2525 case EM_V800: return "Renesas V850 (using RH850 ABI)";
2526 case EM_FR20: return "Fujitsu FR20";
2527 case EM_RH32: return "TRW RH32";
2528 case EM_MCORE: return "MCORE";
2529 /* 40 */
2530 case EM_ARM: return "ARM";
2531 case EM_OLD_ALPHA: return "Digital Alpha (old)";
2532 case EM_SH: return "Renesas / SuperH SH";
2533 case EM_SPARCV9: return "Sparc v9";
2534 case EM_TRICORE: return "Siemens Tricore";
2535 case EM_ARC: return "ARC";
2536 case EM_H8_300: return "Renesas H8/300";
2537 case EM_H8_300H: return "Renesas H8/300H";
2538 case EM_H8S: return "Renesas H8S";
2539 case EM_H8_500: return "Renesas H8/500";
2540 /* 50 */
2541 case EM_IA_64: return "Intel IA-64";
2542 case EM_MIPS_X: return "Stanford MIPS-X";
2543 case EM_COLDFIRE: return "Motorola Coldfire";
2544 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
2545 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2546 case EM_PCP: return "Siemens PCP";
2547 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2548 case EM_NDR1: return "Denso NDR1 microprocesspr";
2549 case EM_STARCORE: return "Motorola Star*Core processor";
2550 case EM_ME16: return "Toyota ME16 processor";
2551 /* 60 */
2552 case EM_ST100: return "STMicroelectronics ST100 processor";
2553 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
2554 case EM_X86_64: return "Advanced Micro Devices X86-64";
2555 case EM_PDSP: return "Sony DSP processor";
2556 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2557 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
2558 case EM_FX66: return "Siemens FX66 microcontroller";
2559 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2560 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2561 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
2562 /* 70 */
2563 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2564 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2565 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2566 case EM_SVX: return "Silicon Graphics SVx";
2567 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2568 case EM_VAX: return "Digital VAX";
2569 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
2570 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2571 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2572 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
2573 /* 80 */
2574 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
2575 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
2576 case EM_PRISM: return "Vitesse Prism";
2577 case EM_AVR_OLD:
2578 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2579 case EM_CYGNUS_FR30:
2580 case EM_FR30: return "Fujitsu FR30";
2581 case EM_CYGNUS_D10V:
2582 case EM_D10V: return "d10v";
2583 case EM_CYGNUS_D30V:
2584 case EM_D30V: return "d30v";
2585 case EM_CYGNUS_V850:
2586 case EM_V850: return "Renesas V850";
2587 case EM_CYGNUS_M32R:
2588 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2589 case EM_CYGNUS_MN10300:
2590 case EM_MN10300: return "mn10300";
2591 /* 90 */
2592 case EM_CYGNUS_MN10200:
2593 case EM_MN10200: return "mn10200";
2594 case EM_PJ: return "picoJava";
2595 case EM_OR1K: return "OpenRISC 1000";
2596 case EM_ARC_COMPACT: return "ARCompact";
2597 case EM_XTENSA_OLD:
2598 case EM_XTENSA: return "Tensilica Xtensa Processor";
2599 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2600 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2601 case EM_NS32K: return "National Semiconductor 32000 series";
2602 case EM_TPC: return "Tenor Network TPC processor";
2603 case EM_SNP1K: return "Trebia SNP 1000 processor";
2604 /* 100 */
2605 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
2606 case EM_IP2K_OLD:
2607 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
2608 case EM_MAX: return "MAX Processor";
2609 case EM_CR: return "National Semiconductor CompactRISC";
2610 case EM_F2MC16: return "Fujitsu F2MC16";
2611 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
2612 case EM_BLACKFIN: return "Analog Devices Blackfin";
2613 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2614 case EM_SEP: return "Sharp embedded microprocessor";
2615 case EM_ARCA: return "Arca RISC microprocessor";
2616 /* 110 */
2617 case EM_UNICORE: return "Unicore";
2618 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2619 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
2620 case EM_ALTERA_NIOS2: return "Altera Nios II";
2621 case EM_CRX: return "National Semiconductor CRX microprocessor";
2622 case EM_XGATE: return "Motorola XGATE embedded processor";
2623 case EM_C166:
2624 case EM_XC16X: return "Infineon Technologies xc16x";
2625 case EM_M16C: return "Renesas M16C series microprocessors";
2626 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2627 case EM_CE: return "Freescale Communication Engine RISC core";
2628 /* 120 */
2629 case EM_M32C: return "Renesas M32c";
2630 /* 130 */
2631 case EM_TSK3000: return "Altium TSK3000 core";
2632 case EM_RS08: return "Freescale RS08 embedded processor";
2633 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
2634 case EM_SCORE: return "SUNPLUS S+Core";
2635 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2636 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
2637 case EM_LATTICEMICO32: return "Lattice Mico32";
2638 case EM_SE_C17: return "Seiko Epson C17 family";
2639 /* 140 */
2640 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2641 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2642 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
2643 case EM_TI_PRU: return "TI PRU I/O processor";
2644 /* 160 */
2645 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2646 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2647 case EM_R32C: return "Renesas R32C series microprocessors";
2648 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2649 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2650 case EM_8051: return "Intel 8051 and variants";
2651 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2652 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2653 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2654 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2655 /* 170 */
2656 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
2657 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
2658 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
2659 case EM_RX: return "Renesas RX";
2660 case EM_METAG: return "Imagination Technologies Meta processor architecture";
2661 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
2662 case EM_ECOG16: return "Cyan Technology eCOG16 family";
2663 case EM_CR16:
2664 case EM_MICROBLAZE:
2665 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
2666 case EM_ETPU: return "Freescale Extended Time Processing Unit";
2667 case EM_SLE9X: return "Infineon Technologies SLE9X core";
2668 /* 180 */
2669 case EM_L1OM: return "Intel L1OM";
2670 case EM_K1OM: return "Intel K1OM";
2671 case EM_INTEL182: return "Intel (reserved)";
2672 case EM_AARCH64: return "AArch64";
2673 case EM_ARM184: return "ARM (reserved)";
2674 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
2675 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
2676 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
2677 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
2678 /* 190 */
2679 case EM_CUDA: return "NVIDIA CUDA architecture";
2680 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
2681 case EM_CLOUDSHIELD: return "CloudShield architecture family";
2682 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
2683 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
2684 case EM_ARC_COMPACT2: return "ARCv2";
2685 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
2686 case EM_RL78: return "Renesas RL78";
2687 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
2688 case EM_78K0R: return "Renesas 78K0R";
2689 /* 200 */
2690 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
2691 case EM_BA1: return "Beyond BA1 CPU architecture";
2692 case EM_BA2: return "Beyond BA2 CPU architecture";
2693 case EM_XCORE: return "XMOS xCORE processor family";
2694 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
2695 case EM_INTELGT: return "Intel Graphics Technology";
2696 /* 210 */
2697 case EM_KM32: return "KM211 KM32 32-bit processor";
2698 case EM_KMX32: return "KM211 KMX32 32-bit processor";
2699 case EM_KMX16: return "KM211 KMX16 16-bit processor";
2700 case EM_KMX8: return "KM211 KMX8 8-bit processor";
2701 case EM_KVARC: return "KM211 KVARC processor";
2702 case EM_CDP: return "Paneve CDP architecture family";
2703 case EM_COGE: return "Cognitive Smart Memory Processor";
2704 case EM_COOL: return "Bluechip Systems CoolEngine";
2705 case EM_NORC: return "Nanoradio Optimized RISC";
2706 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
2707 /* 220 */
2708 case EM_Z80: return "Zilog Z80";
2709 case EM_VISIUM: return "CDS VISIUMcore processor";
2710 case EM_FT32: return "FTDI Chip FT32";
2711 case EM_MOXIE: return "Moxie";
2712 case EM_AMDGPU: return "AMD GPU";
2713 /* 230 (all reserved) */
2714 /* 240 */
2715 case EM_RISCV: return "RISC-V";
2716 case EM_LANAI: return "Lanai 32-bit processor";
2717 case EM_CEVA: return "CEVA Processor Architecture Family";
2718 case EM_CEVA_X2: return "CEVA X2 Processor Family";
2719 case EM_BPF: return "Linux BPF";
2720 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
2721 case EM_IMG1: return "Imagination Technologies";
2722 /* 250 */
2723 case EM_NFP: return "Netronome Flow Processor";
2724 case EM_VE: return "NEC Vector Engine";
2725 case EM_CSKY: return "C-SKY";
2726 case EM_ARC_COMPACT3_64: return "Synopsys ARCv2.3 64-bit";
2727 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
2728 case EM_ARC_COMPACT3: return "Synopsys ARCv2.3 32-bit";
2729 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
2730 case EM_65816: return "WDC 65816/65C816";
2731 case EM_LOONGARCH: return "LoongArch";
2732 case EM_KF32: return "ChipON KungFu32";
2733
2734 /* Large numbers... */
2735 case EM_MT: return "Morpho Techologies MT processor";
2736 case EM_ALPHA: return "Alpha";
2737 case EM_WEBASSEMBLY: return "Web Assembly";
2738 case EM_DLX: return "OpenDLX";
2739 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
2740 case EM_IQ2000: return "Vitesse IQ2000";
2741 case EM_M32C_OLD:
2742 case EM_NIOS32: return "Altera Nios";
2743 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
2744 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
2745 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
2746 case EM_S12Z: return "Freescale S12Z";
2747
2748 default:
2749 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
2750 return buff;
2751 }
2752}
2753
2754static void
2755decode_ARC_machine_flags (unsigned e_flags, unsigned e_machine, char buf[])
2756{
2757 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
2758 other compilers don't specify an architecture type in the e_flags, and
2759 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
2760 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
2761 architectures.
2762
2763 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
2764 but also sets a specific architecture type in the e_flags field.
2765
2766 However, when decoding the flags we don't worry if we see an
2767 unexpected pairing, for example EM_ARC_COMPACT machine type, with
2768 ARCEM architecture type. */
2769
2770 switch (e_flags & EF_ARC_MACH_MSK)
2771 {
2772 /* We only expect these to occur for EM_ARC_COMPACT2. */
2773 case EF_ARC_CPU_ARCV2EM:
2774 strcat (buf, ", ARC EM");
2775 break;
2776 case EF_ARC_CPU_ARCV2HS:
2777 strcat (buf, ", ARC HS");
2778 break;
2779
2780 /* We only expect these to occur for EM_ARC_COMPACT. */
2781 case E_ARC_MACH_ARC600:
2782 strcat (buf, ", ARC600");
2783 break;
2784 case E_ARC_MACH_ARC601:
2785 strcat (buf, ", ARC601");
2786 break;
2787 case E_ARC_MACH_ARC700:
2788 strcat (buf, ", ARC700");
2789 break;
2790
2791 /* The only times we should end up here are (a) A corrupt ELF, (b) A
2792 new ELF with new architecture being read by an old version of
2793 readelf, or (c) An ELF built with non-GNU compiler that does not
2794 set the architecture in the e_flags. */
2795 default:
2796 if (e_machine == EM_ARC_COMPACT)
2797 strcat (buf, ", Unknown ARCompact");
2798 else
2799 strcat (buf, ", Unknown ARC");
2800 break;
2801 }
2802
2803 switch (e_flags & EF_ARC_OSABI_MSK)
2804 {
2805 case E_ARC_OSABI_ORIG:
2806 strcat (buf, ", (ABI:legacy)");
2807 break;
2808 case E_ARC_OSABI_V2:
2809 strcat (buf, ", (ABI:v2)");
2810 break;
2811 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
2812 case E_ARC_OSABI_V3:
2813 strcat (buf, ", v3 no-legacy-syscalls ABI");
2814 break;
2815 case E_ARC_OSABI_V4:
2816 strcat (buf, ", v4 ABI");
2817 break;
2818 default:
2819 strcat (buf, ", unrecognised ARC OSABI flag");
2820 break;
2821 }
2822}
2823
2824static void
2825decode_ARM_machine_flags (unsigned e_flags, char buf[])
2826{
2827 unsigned eabi;
2828 bool unknown = false;
2829
2830 eabi = EF_ARM_EABI_VERSION (e_flags);
2831 e_flags &= ~ EF_ARM_EABIMASK;
2832
2833 /* Handle "generic" ARM flags. */
2834 if (e_flags & EF_ARM_RELEXEC)
2835 {
2836 strcat (buf, ", relocatable executable");
2837 e_flags &= ~ EF_ARM_RELEXEC;
2838 }
2839
2840 if (e_flags & EF_ARM_PIC)
2841 {
2842 strcat (buf, ", position independent");
2843 e_flags &= ~ EF_ARM_PIC;
2844 }
2845
2846 /* Now handle EABI specific flags. */
2847 switch (eabi)
2848 {
2849 default:
2850 strcat (buf, ", <unrecognized EABI>");
2851 if (e_flags)
2852 unknown = true;
2853 break;
2854
2855 case EF_ARM_EABI_VER1:
2856 strcat (buf, ", Version1 EABI");
2857 while (e_flags)
2858 {
2859 unsigned flag;
2860
2861 /* Process flags one bit at a time. */
2862 flag = e_flags & - e_flags;
2863 e_flags &= ~ flag;
2864
2865 switch (flag)
2866 {
2867 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
2868 strcat (buf, ", sorted symbol tables");
2869 break;
2870
2871 default:
2872 unknown = true;
2873 break;
2874 }
2875 }
2876 break;
2877
2878 case EF_ARM_EABI_VER2:
2879 strcat (buf, ", Version2 EABI");
2880 while (e_flags)
2881 {
2882 unsigned flag;
2883
2884 /* Process flags one bit at a time. */
2885 flag = e_flags & - e_flags;
2886 e_flags &= ~ flag;
2887
2888 switch (flag)
2889 {
2890 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
2891 strcat (buf, ", sorted symbol tables");
2892 break;
2893
2894 case EF_ARM_DYNSYMSUSESEGIDX:
2895 strcat (buf, ", dynamic symbols use segment index");
2896 break;
2897
2898 case EF_ARM_MAPSYMSFIRST:
2899 strcat (buf, ", mapping symbols precede others");
2900 break;
2901
2902 default:
2903 unknown = true;
2904 break;
2905 }
2906 }
2907 break;
2908
2909 case EF_ARM_EABI_VER3:
2910 strcat (buf, ", Version3 EABI");
2911 break;
2912
2913 case EF_ARM_EABI_VER4:
2914 strcat (buf, ", Version4 EABI");
2915 while (e_flags)
2916 {
2917 unsigned flag;
2918
2919 /* Process flags one bit at a time. */
2920 flag = e_flags & - e_flags;
2921 e_flags &= ~ flag;
2922
2923 switch (flag)
2924 {
2925 case EF_ARM_BE8:
2926 strcat (buf, ", BE8");
2927 break;
2928
2929 case EF_ARM_LE8:
2930 strcat (buf, ", LE8");
2931 break;
2932
2933 default:
2934 unknown = true;
2935 break;
2936 }
2937 }
2938 break;
2939
2940 case EF_ARM_EABI_VER5:
2941 strcat (buf, ", Version5 EABI");
2942 while (e_flags)
2943 {
2944 unsigned flag;
2945
2946 /* Process flags one bit at a time. */
2947 flag = e_flags & - e_flags;
2948 e_flags &= ~ flag;
2949
2950 switch (flag)
2951 {
2952 case EF_ARM_BE8:
2953 strcat (buf, ", BE8");
2954 break;
2955
2956 case EF_ARM_LE8:
2957 strcat (buf, ", LE8");
2958 break;
2959
2960 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
2961 strcat (buf, ", soft-float ABI");
2962 break;
2963
2964 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
2965 strcat (buf, ", hard-float ABI");
2966 break;
2967
2968 default:
2969 unknown = true;
2970 break;
2971 }
2972 }
2973 break;
2974
2975 case EF_ARM_EABI_UNKNOWN:
2976 strcat (buf, ", GNU EABI");
2977 while (e_flags)
2978 {
2979 unsigned flag;
2980
2981 /* Process flags one bit at a time. */
2982 flag = e_flags & - e_flags;
2983 e_flags &= ~ flag;
2984
2985 switch (flag)
2986 {
2987 case EF_ARM_INTERWORK:
2988 strcat (buf, ", interworking enabled");
2989 break;
2990
2991 case EF_ARM_APCS_26:
2992 strcat (buf, ", uses APCS/26");
2993 break;
2994
2995 case EF_ARM_APCS_FLOAT:
2996 strcat (buf, ", uses APCS/float");
2997 break;
2998
2999 case EF_ARM_PIC:
3000 strcat (buf, ", position independent");
3001 break;
3002
3003 case EF_ARM_ALIGN8:
3004 strcat (buf, ", 8 bit structure alignment");
3005 break;
3006
3007 case EF_ARM_NEW_ABI:
3008 strcat (buf, ", uses new ABI");
3009 break;
3010
3011 case EF_ARM_OLD_ABI:
3012 strcat (buf, ", uses old ABI");
3013 break;
3014
3015 case EF_ARM_SOFT_FLOAT:
3016 strcat (buf, ", software FP");
3017 break;
3018
3019 case EF_ARM_VFP_FLOAT:
3020 strcat (buf, ", VFP");
3021 break;
3022
3023 case EF_ARM_MAVERICK_FLOAT:
3024 strcat (buf, ", Maverick FP");
3025 break;
3026
3027 default:
3028 unknown = true;
3029 break;
3030 }
3031 }
3032 }
3033
3034 if (unknown)
3035 strcat (buf,_(", <unknown>"));
3036}
3037
3038static void
3039decode_AVR_machine_flags (unsigned e_flags, char buf[], size_t size)
3040{
3041 --size; /* Leave space for null terminator. */
3042
3043 switch (e_flags & EF_AVR_MACH)
3044 {
3045 case E_AVR_MACH_AVR1:
3046 strncat (buf, ", avr:1", size);
3047 break;
3048 case E_AVR_MACH_AVR2:
3049 strncat (buf, ", avr:2", size);
3050 break;
3051 case E_AVR_MACH_AVR25:
3052 strncat (buf, ", avr:25", size);
3053 break;
3054 case E_AVR_MACH_AVR3:
3055 strncat (buf, ", avr:3", size);
3056 break;
3057 case E_AVR_MACH_AVR31:
3058 strncat (buf, ", avr:31", size);
3059 break;
3060 case E_AVR_MACH_AVR35:
3061 strncat (buf, ", avr:35", size);
3062 break;
3063 case E_AVR_MACH_AVR4:
3064 strncat (buf, ", avr:4", size);
3065 break;
3066 case E_AVR_MACH_AVR5:
3067 strncat (buf, ", avr:5", size);
3068 break;
3069 case E_AVR_MACH_AVR51:
3070 strncat (buf, ", avr:51", size);
3071 break;
3072 case E_AVR_MACH_AVR6:
3073 strncat (buf, ", avr:6", size);
3074 break;
3075 case E_AVR_MACH_AVRTINY:
3076 strncat (buf, ", avr:100", size);
3077 break;
3078 case E_AVR_MACH_XMEGA1:
3079 strncat (buf, ", avr:101", size);
3080 break;
3081 case E_AVR_MACH_XMEGA2:
3082 strncat (buf, ", avr:102", size);
3083 break;
3084 case E_AVR_MACH_XMEGA3:
3085 strncat (buf, ", avr:103", size);
3086 break;
3087 case E_AVR_MACH_XMEGA4:
3088 strncat (buf, ", avr:104", size);
3089 break;
3090 case E_AVR_MACH_XMEGA5:
3091 strncat (buf, ", avr:105", size);
3092 break;
3093 case E_AVR_MACH_XMEGA6:
3094 strncat (buf, ", avr:106", size);
3095 break;
3096 case E_AVR_MACH_XMEGA7:
3097 strncat (buf, ", avr:107", size);
3098 break;
3099 default:
3100 strncat (buf, ", avr:<unknown>", size);
3101 break;
3102 }
3103
3104 size -= strlen (buf);
3105 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
3106 strncat (buf, ", link-relax", size);
3107}
3108
3109static void
3110decode_NDS32_machine_flags (unsigned e_flags, char buf[], size_t size)
3111{
3112 unsigned abi;
3113 unsigned arch;
3114 unsigned config;
3115 unsigned version;
3116 bool has_fpu = false;
3117 unsigned int r = 0;
3118
3119 static const char *ABI_STRINGS[] =
3120 {
3121 "ABI v0", /* use r5 as return register; only used in N1213HC */
3122 "ABI v1", /* use r0 as return register */
3123 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
3124 "ABI v2fp", /* for FPU */
3125 "AABI",
3126 "ABI2 FP+"
3127 };
3128 static const char *VER_STRINGS[] =
3129 {
3130 "Andes ELF V1.3 or older",
3131 "Andes ELF V1.3.1",
3132 "Andes ELF V1.4"
3133 };
3134 static const char *ARCH_STRINGS[] =
3135 {
3136 "",
3137 "Andes Star v1.0",
3138 "Andes Star v2.0",
3139 "Andes Star v3.0",
3140 "Andes Star v3.0m"
3141 };
3142
3143 abi = EF_NDS_ABI & e_flags;
3144 arch = EF_NDS_ARCH & e_flags;
3145 config = EF_NDS_INST & e_flags;
3146 version = EF_NDS32_ELF_VERSION & e_flags;
3147
3148 memset (buf, 0, size);
3149
3150 switch (abi)
3151 {
3152 case E_NDS_ABI_V0:
3153 case E_NDS_ABI_V1:
3154 case E_NDS_ABI_V2:
3155 case E_NDS_ABI_V2FP:
3156 case E_NDS_ABI_AABI:
3157 case E_NDS_ABI_V2FP_PLUS:
3158 /* In case there are holes in the array. */
3159 r += snprintf (buf + r, size - r, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3160 break;
3161
3162 default:
3163 r += snprintf (buf + r, size - r, ", <unrecognized ABI>");
3164 break;
3165 }
3166
3167 switch (version)
3168 {
3169 case E_NDS32_ELF_VER_1_2:
3170 case E_NDS32_ELF_VER_1_3:
3171 case E_NDS32_ELF_VER_1_4:
3172 r += snprintf (buf + r, size - r, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
3173 break;
3174
3175 default:
3176 r += snprintf (buf + r, size - r, ", <unrecognized ELF version number>");
3177 break;
3178 }
3179
3180 if (E_NDS_ABI_V0 == abi)
3181 {
3182 /* OLD ABI; only used in N1213HC, has performance extension 1. */
3183 r += snprintf (buf + r, size - r, ", Andes Star v1.0, N1213HC, MAC, PERF1");
3184 if (arch == E_NDS_ARCH_STAR_V1_0)
3185 r += snprintf (buf + r, size -r, ", 16b"); /* has 16-bit instructions */
3186 return;
3187 }
3188
3189 switch (arch)
3190 {
3191 case E_NDS_ARCH_STAR_V1_0:
3192 case E_NDS_ARCH_STAR_V2_0:
3193 case E_NDS_ARCH_STAR_V3_0:
3194 case E_NDS_ARCH_STAR_V3_M:
3195 r += snprintf (buf + r, size - r, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
3196 break;
3197
3198 default:
3199 r += snprintf (buf + r, size - r, ", <unrecognized architecture>");
3200 /* ARCH version determines how the e_flags are interpreted.
3201 If it is unknown, we cannot proceed. */
3202 return;
3203 }
3204
3205 /* Newer ABI; Now handle architecture specific flags. */
3206 if (arch == E_NDS_ARCH_STAR_V1_0)
3207 {
3208 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3209 r += snprintf (buf + r, size -r, ", MFUSR_PC");
3210
3211 if (!(config & E_NDS32_HAS_NO_MAC_INST))
3212 r += snprintf (buf + r, size -r, ", MAC");
3213
3214 if (config & E_NDS32_HAS_DIV_INST)
3215 r += snprintf (buf + r, size -r, ", DIV");
3216
3217 if (config & E_NDS32_HAS_16BIT_INST)
3218 r += snprintf (buf + r, size -r, ", 16b");
3219 }
3220 else
3221 {
3222 if (config & E_NDS32_HAS_MFUSR_PC_INST)
3223 {
3224 if (version <= E_NDS32_ELF_VER_1_3)
3225 r += snprintf (buf + r, size -r, ", [B8]");
3226 else
3227 r += snprintf (buf + r, size -r, ", EX9");
3228 }
3229
3230 if (config & E_NDS32_HAS_MAC_DX_INST)
3231 r += snprintf (buf + r, size -r, ", MAC_DX");
3232
3233 if (config & E_NDS32_HAS_DIV_DX_INST)
3234 r += snprintf (buf + r, size -r, ", DIV_DX");
3235
3236 if (config & E_NDS32_HAS_16BIT_INST)
3237 {
3238 if (version <= E_NDS32_ELF_VER_1_3)
3239 r += snprintf (buf + r, size -r, ", 16b");
3240 else
3241 r += snprintf (buf + r, size -r, ", IFC");
3242 }
3243 }
3244
3245 if (config & E_NDS32_HAS_EXT_INST)
3246 r += snprintf (buf + r, size -r, ", PERF1");
3247
3248 if (config & E_NDS32_HAS_EXT2_INST)
3249 r += snprintf (buf + r, size -r, ", PERF2");
3250
3251 if (config & E_NDS32_HAS_FPU_INST)
3252 {
3253 has_fpu = true;
3254 r += snprintf (buf + r, size -r, ", FPU_SP");
3255 }
3256
3257 if (config & E_NDS32_HAS_FPU_DP_INST)
3258 {
3259 has_fpu = true;
3260 r += snprintf (buf + r, size -r, ", FPU_DP");
3261 }
3262
3263 if (config & E_NDS32_HAS_FPU_MAC_INST)
3264 {
3265 has_fpu = true;
3266 r += snprintf (buf + r, size -r, ", FPU_MAC");
3267 }
3268
3269 if (has_fpu)
3270 {
3271 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
3272 {
3273 case E_NDS32_FPU_REG_8SP_4DP:
3274 r += snprintf (buf + r, size -r, ", FPU_REG:8/4");
3275 break;
3276 case E_NDS32_FPU_REG_16SP_8DP:
3277 r += snprintf (buf + r, size -r, ", FPU_REG:16/8");
3278 break;
3279 case E_NDS32_FPU_REG_32SP_16DP:
3280 r += snprintf (buf + r, size -r, ", FPU_REG:32/16");
3281 break;
3282 case E_NDS32_FPU_REG_32SP_32DP:
3283 r += snprintf (buf + r, size -r, ", FPU_REG:32/32");
3284 break;
3285 }
3286 }
3287
3288 if (config & E_NDS32_HAS_AUDIO_INST)
3289 r += snprintf (buf + r, size -r, ", AUDIO");
3290
3291 if (config & E_NDS32_HAS_STRING_INST)
3292 r += snprintf (buf + r, size -r, ", STR");
3293
3294 if (config & E_NDS32_HAS_REDUCED_REGS)
3295 r += snprintf (buf + r, size -r, ", 16REG");
3296
3297 if (config & E_NDS32_HAS_VIDEO_INST)
3298 {
3299 if (version <= E_NDS32_ELF_VER_1_3)
3300 r += snprintf (buf + r, size -r, ", VIDEO");
3301 else
3302 r += snprintf (buf + r, size -r, ", SATURATION");
3303 }
3304
3305 if (config & E_NDS32_HAS_ENCRIPT_INST)
3306 r += snprintf (buf + r, size -r, ", ENCRP");
3307
3308 if (config & E_NDS32_HAS_L2C_INST)
3309 r += snprintf (buf + r, size -r, ", L2C");
3310}
3311
3312static char *
3313get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
3314{
3315 static char buf[1024];
3316
3317 buf[0] = '\0';
3318
3319 if (e_flags)
3320 {
3321 switch (e_machine)
3322 {
3323 default:
3324 break;
3325
3326 case EM_ARC_COMPACT2:
3327 case EM_ARC_COMPACT:
3328 decode_ARC_machine_flags (e_flags, e_machine, buf);
3329 break;
3330
3331 case EM_ARM:
3332 decode_ARM_machine_flags (e_flags, buf);
3333 break;
3334
3335 case EM_AVR:
3336 decode_AVR_machine_flags (e_flags, buf, sizeof buf);
3337 break;
3338
3339 case EM_BLACKFIN:
3340 if (e_flags & EF_BFIN_PIC)
3341 strcat (buf, ", PIC");
3342
3343 if (e_flags & EF_BFIN_FDPIC)
3344 strcat (buf, ", FDPIC");
3345
3346 if (e_flags & EF_BFIN_CODE_IN_L1)
3347 strcat (buf, ", code in L1");
3348
3349 if (e_flags & EF_BFIN_DATA_IN_L1)
3350 strcat (buf, ", data in L1");
3351
3352 break;
3353
3354 case EM_CYGNUS_FRV:
3355 switch (e_flags & EF_FRV_CPU_MASK)
3356 {
3357 case EF_FRV_CPU_GENERIC:
3358 break;
3359
3360 default:
3361 strcat (buf, ", fr???");
3362 break;
3363
3364 case EF_FRV_CPU_FR300:
3365 strcat (buf, ", fr300");
3366 break;
3367
3368 case EF_FRV_CPU_FR400:
3369 strcat (buf, ", fr400");
3370 break;
3371 case EF_FRV_CPU_FR405:
3372 strcat (buf, ", fr405");
3373 break;
3374
3375 case EF_FRV_CPU_FR450:
3376 strcat (buf, ", fr450");
3377 break;
3378
3379 case EF_FRV_CPU_FR500:
3380 strcat (buf, ", fr500");
3381 break;
3382 case EF_FRV_CPU_FR550:
3383 strcat (buf, ", fr550");
3384 break;
3385
3386 case EF_FRV_CPU_SIMPLE:
3387 strcat (buf, ", simple");
3388 break;
3389 case EF_FRV_CPU_TOMCAT:
3390 strcat (buf, ", tomcat");
3391 break;
3392 }
3393 break;
3394
3395 case EM_68K:
3396 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3397 strcat (buf, ", m68000");
3398 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3399 strcat (buf, ", cpu32");
3400 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3401 strcat (buf, ", fido_a");
3402 else
3403 {
3404 char const * isa = _("unknown");
3405 char const * mac = _("unknown mac");
3406 char const * additional = NULL;
3407
3408 switch (e_flags & EF_M68K_CF_ISA_MASK)
3409 {
3410 case EF_M68K_CF_ISA_A_NODIV:
3411 isa = "A";
3412 additional = ", nodiv";
3413 break;
3414 case EF_M68K_CF_ISA_A:
3415 isa = "A";
3416 break;
3417 case EF_M68K_CF_ISA_A_PLUS:
3418 isa = "A+";
3419 break;
3420 case EF_M68K_CF_ISA_B_NOUSP:
3421 isa = "B";
3422 additional = ", nousp";
3423 break;
3424 case EF_M68K_CF_ISA_B:
3425 isa = "B";
3426 break;
3427 case EF_M68K_CF_ISA_C:
3428 isa = "C";
3429 break;
3430 case EF_M68K_CF_ISA_C_NODIV:
3431 isa = "C";
3432 additional = ", nodiv";
3433 break;
3434 }
3435 strcat (buf, ", cf, isa ");
3436 strcat (buf, isa);
3437 if (additional)
3438 strcat (buf, additional);
3439 if (e_flags & EF_M68K_CF_FLOAT)
3440 strcat (buf, ", float");
3441 switch (e_flags & EF_M68K_CF_MAC_MASK)
3442 {
3443 case 0:
3444 mac = NULL;
3445 break;
3446 case EF_M68K_CF_MAC:
3447 mac = "mac";
3448 break;
3449 case EF_M68K_CF_EMAC:
3450 mac = "emac";
3451 break;
3452 case EF_M68K_CF_EMAC_B:
3453 mac = "emac_b";
3454 break;
3455 }
3456 if (mac)
3457 {
3458 strcat (buf, ", ");
3459 strcat (buf, mac);
3460 }
3461 }
3462 break;
3463
3464 case EM_CYGNUS_MEP:
3465 switch (e_flags & EF_MEP_CPU_MASK)
3466 {
3467 case EF_MEP_CPU_MEP: strcat (buf, ", generic MeP"); break;
3468 case EF_MEP_CPU_C2: strcat (buf, ", MeP C2"); break;
3469 case EF_MEP_CPU_C3: strcat (buf, ", MeP C3"); break;
3470 case EF_MEP_CPU_C4: strcat (buf, ", MeP C4"); break;
3471 case EF_MEP_CPU_C5: strcat (buf, ", MeP C5"); break;
3472 case EF_MEP_CPU_H1: strcat (buf, ", MeP H1"); break;
3473 default: strcat (buf, _(", <unknown MeP cpu type>")); break;
3474 }
3475
3476 switch (e_flags & EF_MEP_COP_MASK)
3477 {
3478 case EF_MEP_COP_NONE: break;
3479 case EF_MEP_COP_AVC: strcat (buf, ", AVC coprocessor"); break;
3480 case EF_MEP_COP_AVC2: strcat (buf, ", AVC2 coprocessor"); break;
3481 case EF_MEP_COP_FMAX: strcat (buf, ", FMAX coprocessor"); break;
3482 case EF_MEP_COP_IVC2: strcat (buf, ", IVC2 coprocessor"); break;
3483 default: strcat (buf, _("<unknown MeP copro type>")); break;
3484 }
3485
3486 if (e_flags & EF_MEP_LIBRARY)
3487 strcat (buf, ", Built for Library");
3488
3489 if (e_flags & EF_MEP_INDEX_MASK)
3490 sprintf (buf + strlen (buf), ", Configuration Index: %#x",
3491 e_flags & EF_MEP_INDEX_MASK);
3492
3493 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3494 sprintf (buf + strlen (buf), _(", unknown flags bits: %#x"),
3495 e_flags & ~ EF_MEP_ALL_FLAGS);
3496 break;
3497
3498 case EM_PPC:
3499 if (e_flags & EF_PPC_EMB)
3500 strcat (buf, ", emb");
3501
3502 if (e_flags & EF_PPC_RELOCATABLE)
3503 strcat (buf, _(", relocatable"));
3504
3505 if (e_flags & EF_PPC_RELOCATABLE_LIB)
3506 strcat (buf, _(", relocatable-lib"));
3507 break;
3508
3509 case EM_PPC64:
3510 if (e_flags & EF_PPC64_ABI)
3511 {
3512 char abi[] = ", abiv0";
3513
3514 abi[6] += e_flags & EF_PPC64_ABI;
3515 strcat (buf, abi);
3516 }
3517 break;
3518
3519 case EM_V800:
3520 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
3521 strcat (buf, ", RH850 ABI");
3522
3523 if (e_flags & EF_V800_850E3)
3524 strcat (buf, ", V3 architecture");
3525
3526 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
3527 strcat (buf, ", FPU not used");
3528
3529 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
3530 strcat (buf, ", regmode: COMMON");
3531
3532 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
3533 strcat (buf, ", r4 not used");
3534
3535 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
3536 strcat (buf, ", r30 not used");
3537
3538 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
3539 strcat (buf, ", r5 not used");
3540
3541 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
3542 strcat (buf, ", r2 not used");
3543
3544 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
3545 {
3546 switch (e_flags & - e_flags)
3547 {
3548 case EF_RH850_FPU_DOUBLE: strcat (buf, ", double precision FPU"); break;
3549 case EF_RH850_FPU_SINGLE: strcat (buf, ", single precision FPU"); break;
3550 case EF_RH850_REGMODE22: strcat (buf, ", regmode:22"); break;
3551 case EF_RH850_REGMODE32: strcat (buf, ", regmode:23"); break;
3552 case EF_RH850_GP_FIX: strcat (buf, ", r4 fixed"); break;
3553 case EF_RH850_GP_NOFIX: strcat (buf, ", r4 free"); break;
3554 case EF_RH850_EP_FIX: strcat (buf, ", r30 fixed"); break;
3555 case EF_RH850_EP_NOFIX: strcat (buf, ", r30 free"); break;
3556 case EF_RH850_TP_FIX: strcat (buf, ", r5 fixed"); break;
3557 case EF_RH850_TP_NOFIX: strcat (buf, ", r5 free"); break;
3558 case EF_RH850_REG2_RESERVE: strcat (buf, ", r2 fixed"); break;
3559 case EF_RH850_REG2_NORESERVE: strcat (buf, ", r2 free"); break;
3560 default: break;
3561 }
3562 }
3563 break;
3564
3565 case EM_V850:
3566 case EM_CYGNUS_V850:
3567 switch (e_flags & EF_V850_ARCH)
3568 {
3569 case E_V850E3V5_ARCH:
3570 strcat (buf, ", v850e3v5");
3571 break;
3572 case E_V850E2V3_ARCH:
3573 strcat (buf, ", v850e2v3");
3574 break;
3575 case E_V850E2_ARCH:
3576 strcat (buf, ", v850e2");
3577 break;
3578 case E_V850E1_ARCH:
3579 strcat (buf, ", v850e1");
3580 break;
3581 case E_V850E_ARCH:
3582 strcat (buf, ", v850e");
3583 break;
3584 case E_V850_ARCH:
3585 strcat (buf, ", v850");
3586 break;
3587 default:
3588 strcat (buf, _(", unknown v850 architecture variant"));
3589 break;
3590 }
3591 break;
3592
3593 case EM_M32R:
3594 case EM_CYGNUS_M32R:
3595 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
3596 strcat (buf, ", m32r");
3597 break;
3598
3599 case EM_MIPS:
3600 case EM_MIPS_RS3_LE:
3601 if (e_flags & EF_MIPS_NOREORDER)
3602 strcat (buf, ", noreorder");
3603
3604 if (e_flags & EF_MIPS_PIC)
3605 strcat (buf, ", pic");
3606
3607 if (e_flags & EF_MIPS_CPIC)
3608 strcat (buf, ", cpic");
3609
3610 if (e_flags & EF_MIPS_UCODE)
3611 strcat (buf, ", ugen_reserved");
3612
3613 if (e_flags & EF_MIPS_ABI2)
3614 strcat (buf, ", abi2");
3615
3616 if (e_flags & EF_MIPS_OPTIONS_FIRST)
3617 strcat (buf, ", odk first");
3618
3619 if (e_flags & EF_MIPS_32BITMODE)
3620 strcat (buf, ", 32bitmode");
3621
3622 if (e_flags & EF_MIPS_NAN2008)
3623 strcat (buf, ", nan2008");
3624
3625 if (e_flags & EF_MIPS_FP64)
3626 strcat (buf, ", fp64");
3627
3628 switch ((e_flags & EF_MIPS_MACH))
3629 {
3630 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
3631 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
3632 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
3633 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
3634 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
3635 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
3636 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
3637 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
3638 case E_MIPS_MACH_5900: strcat (buf, ", 5900"); break;
3639 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
3640 case E_MIPS_MACH_9000: strcat (buf, ", 9000"); break;
3641 case E_MIPS_MACH_LS2E: strcat (buf, ", loongson-2e"); break;
3642 case E_MIPS_MACH_LS2F: strcat (buf, ", loongson-2f"); break;
3643 case E_MIPS_MACH_GS464: strcat (buf, ", gs464"); break;
3644 case E_MIPS_MACH_GS464E: strcat (buf, ", gs464e"); break;
3645 case E_MIPS_MACH_GS264E: strcat (buf, ", gs264e"); break;
3646 case E_MIPS_MACH_OCTEON: strcat (buf, ", octeon"); break;
3647 case E_MIPS_MACH_OCTEON2: strcat (buf, ", octeon2"); break;
3648 case E_MIPS_MACH_OCTEON3: strcat (buf, ", octeon3"); break;
3649 case E_MIPS_MACH_XLR: strcat (buf, ", xlr"); break;
3650 case E_MIPS_MACH_IAMR2: strcat (buf, ", interaptiv-mr2"); break;
3651 case 0:
3652 /* We simply ignore the field in this case to avoid confusion:
3653 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
3654 extension. */
3655 break;
3656 default: strcat (buf, _(", unknown CPU")); break;
3657 }
3658
3659 switch ((e_flags & EF_MIPS_ABI))
3660 {
3661 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
3662 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
3663 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
3664 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
3665 case 0:
3666 /* We simply ignore the field in this case to avoid confusion:
3667 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
3668 This means it is likely to be an o32 file, but not for
3669 sure. */
3670 break;
3671 default: strcat (buf, _(", unknown ABI")); break;
3672 }
3673
3674 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
3675 strcat (buf, ", mdmx");
3676
3677 if (e_flags & EF_MIPS_ARCH_ASE_M16)
3678 strcat (buf, ", mips16");
3679
3680 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
3681 strcat (buf, ", micromips");
3682
3683 switch ((e_flags & EF_MIPS_ARCH))
3684 {
3685 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
3686 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
3687 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
3688 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
3689 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
3690 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
3691 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
3692 case E_MIPS_ARCH_32R6: strcat (buf, ", mips32r6"); break;
3693 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
3694 case E_MIPS_ARCH_64R2: strcat (buf, ", mips64r2"); break;
3695 case E_MIPS_ARCH_64R6: strcat (buf, ", mips64r6"); break;
3696 default: strcat (buf, _(", unknown ISA")); break;
3697 }
3698 break;
3699
3700 case EM_NDS32:
3701 decode_NDS32_machine_flags (e_flags, buf, sizeof buf);
3702 break;
3703
3704 case EM_NFP:
3705 switch (EF_NFP_MACH (e_flags))
3706 {
3707 case E_NFP_MACH_3200:
3708 strcat (buf, ", NFP-32xx");
3709 break;
3710 case E_NFP_MACH_6000:
3711 strcat (buf, ", NFP-6xxx");
3712 break;
3713 }
3714 break;
3715
3716 case EM_RISCV:
3717 if (e_flags & EF_RISCV_RVC)
3718 strcat (buf, ", RVC");
3719
3720 if (e_flags & EF_RISCV_RVE)
3721 strcat (buf, ", RVE");
3722
3723 switch (e_flags & EF_RISCV_FLOAT_ABI)
3724 {
3725 case EF_RISCV_FLOAT_ABI_SOFT:
3726 strcat (buf, ", soft-float ABI");
3727 break;
3728
3729 case EF_RISCV_FLOAT_ABI_SINGLE:
3730 strcat (buf, ", single-float ABI");
3731 break;
3732
3733 case EF_RISCV_FLOAT_ABI_DOUBLE:
3734 strcat (buf, ", double-float ABI");
3735 break;
3736
3737 case EF_RISCV_FLOAT_ABI_QUAD:
3738 strcat (buf, ", quad-float ABI");
3739 break;
3740 }
3741 break;
3742
3743 case EM_SH:
3744 switch ((e_flags & EF_SH_MACH_MASK))
3745 {
3746 case EF_SH1: strcat (buf, ", sh1"); break;
3747 case EF_SH2: strcat (buf, ", sh2"); break;
3748 case EF_SH3: strcat (buf, ", sh3"); break;
3749 case EF_SH_DSP: strcat (buf, ", sh-dsp"); break;
3750 case EF_SH3_DSP: strcat (buf, ", sh3-dsp"); break;
3751 case EF_SH4AL_DSP: strcat (buf, ", sh4al-dsp"); break;
3752 case EF_SH3E: strcat (buf, ", sh3e"); break;
3753 case EF_SH4: strcat (buf, ", sh4"); break;
3754 case EF_SH5: strcat (buf, ", sh5"); break;
3755 case EF_SH2E: strcat (buf, ", sh2e"); break;
3756 case EF_SH4A: strcat (buf, ", sh4a"); break;
3757 case EF_SH2A: strcat (buf, ", sh2a"); break;
3758 case EF_SH4_NOFPU: strcat (buf, ", sh4-nofpu"); break;
3759 case EF_SH4A_NOFPU: strcat (buf, ", sh4a-nofpu"); break;
3760 case EF_SH2A_NOFPU: strcat (buf, ", sh2a-nofpu"); break;
3761 case EF_SH3_NOMMU: strcat (buf, ", sh3-nommu"); break;
3762 case EF_SH4_NOMMU_NOFPU: strcat (buf, ", sh4-nommu-nofpu"); break;
3763 case EF_SH2A_SH4_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh4-nommu-nofpu"); break;
3764 case EF_SH2A_SH3_NOFPU: strcat (buf, ", sh2a-nofpu-or-sh3-nommu"); break;
3765 case EF_SH2A_SH4: strcat (buf, ", sh2a-or-sh4"); break;
3766 case EF_SH2A_SH3E: strcat (buf, ", sh2a-or-sh3e"); break;
3767 default: strcat (buf, _(", unknown ISA")); break;
3768 }
3769
3770 if (e_flags & EF_SH_PIC)
3771 strcat (buf, ", pic");
3772
3773 if (e_flags & EF_SH_FDPIC)
3774 strcat (buf, ", fdpic");
3775 break;
3776
3777 case EM_OR1K:
3778 if (e_flags & EF_OR1K_NODELAY)
3779 strcat (buf, ", no delay");
3780 break;
3781
3782 case EM_SPARCV9:
3783 if (e_flags & EF_SPARC_32PLUS)
3784 strcat (buf, ", v8+");
3785
3786 if (e_flags & EF_SPARC_SUN_US1)
3787 strcat (buf, ", ultrasparcI");
3788
3789 if (e_flags & EF_SPARC_SUN_US3)
3790 strcat (buf, ", ultrasparcIII");
3791
3792 if (e_flags & EF_SPARC_HAL_R1)
3793 strcat (buf, ", halr1");
3794
3795 if (e_flags & EF_SPARC_LEDATA)
3796 strcat (buf, ", ledata");
3797
3798 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
3799 strcat (buf, ", tso");
3800
3801 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
3802 strcat (buf, ", pso");
3803
3804 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
3805 strcat (buf, ", rmo");
3806 break;
3807
3808 case EM_PARISC:
3809 switch (e_flags & EF_PARISC_ARCH)
3810 {
3811 case EFA_PARISC_1_0:
3812 strcpy (buf, ", PA-RISC 1.0");
3813 break;
3814 case EFA_PARISC_1_1:
3815 strcpy (buf, ", PA-RISC 1.1");
3816 break;
3817 case EFA_PARISC_2_0:
3818 strcpy (buf, ", PA-RISC 2.0");
3819 break;
3820 default:
3821 break;
3822 }
3823 if (e_flags & EF_PARISC_TRAPNIL)
3824 strcat (buf, ", trapnil");
3825 if (e_flags & EF_PARISC_EXT)
3826 strcat (buf, ", ext");
3827 if (e_flags & EF_PARISC_LSB)
3828 strcat (buf, ", lsb");
3829 if (e_flags & EF_PARISC_WIDE)
3830 strcat (buf, ", wide");
3831 if (e_flags & EF_PARISC_NO_KABP)
3832 strcat (buf, ", no kabp");
3833 if (e_flags & EF_PARISC_LAZYSWAP)
3834 strcat (buf, ", lazyswap");
3835 break;
3836
3837 case EM_PJ:
3838 case EM_PJ_OLD:
3839 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
3840 strcat (buf, ", new calling convention");
3841
3842 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
3843 strcat (buf, ", gnu calling convention");
3844 break;
3845
3846 case EM_IA_64:
3847 if ((e_flags & EF_IA_64_ABI64))
3848 strcat (buf, ", 64-bit");
3849 else
3850 strcat (buf, ", 32-bit");
3851 if ((e_flags & EF_IA_64_REDUCEDFP))
3852 strcat (buf, ", reduced fp model");
3853 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3854 strcat (buf, ", no function descriptors, constant gp");
3855 else if ((e_flags & EF_IA_64_CONS_GP))
3856 strcat (buf, ", constant gp");
3857 if ((e_flags & EF_IA_64_ABSOLUTE))
3858 strcat (buf, ", absolute");
3859 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3860 {
3861 if ((e_flags & EF_IA_64_VMS_LINKAGES))
3862 strcat (buf, ", vms_linkages");
3863 switch ((e_flags & EF_IA_64_VMS_COMCOD))
3864 {
3865 case EF_IA_64_VMS_COMCOD_SUCCESS:
3866 break;
3867 case EF_IA_64_VMS_COMCOD_WARNING:
3868 strcat (buf, ", warning");
3869 break;
3870 case EF_IA_64_VMS_COMCOD_ERROR:
3871 strcat (buf, ", error");
3872 break;
3873 case EF_IA_64_VMS_COMCOD_ABORT:
3874 strcat (buf, ", abort");
3875 break;
3876 default:
3877 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
3878 e_flags & EF_IA_64_VMS_COMCOD);
3879 strcat (buf, ", <unknown>");
3880 }
3881 }
3882 break;
3883
3884 case EM_VAX:
3885 if ((e_flags & EF_VAX_NONPIC))
3886 strcat (buf, ", non-PIC");
3887 if ((e_flags & EF_VAX_DFLOAT))
3888 strcat (buf, ", D-Float");
3889 if ((e_flags & EF_VAX_GFLOAT))
3890 strcat (buf, ", G-Float");
3891 break;
3892
3893 case EM_VISIUM:
3894 if (e_flags & EF_VISIUM_ARCH_MCM)
3895 strcat (buf, ", mcm");
3896 else if (e_flags & EF_VISIUM_ARCH_MCM24)
3897 strcat (buf, ", mcm24");
3898 if (e_flags & EF_VISIUM_ARCH_GR6)
3899 strcat (buf, ", gr6");
3900 break;
3901
3902 case EM_RL78:
3903 switch (e_flags & E_FLAG_RL78_CPU_MASK)
3904 {
3905 case E_FLAG_RL78_ANY_CPU: break;
3906 case E_FLAG_RL78_G10: strcat (buf, ", G10"); break;
3907 case E_FLAG_RL78_G13: strcat (buf, ", G13"); break;
3908 case E_FLAG_RL78_G14: strcat (buf, ", G14"); break;
3909 }
3910 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
3911 strcat (buf, ", 64-bit doubles");
3912 break;
3913
3914 case EM_RX:
3915 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
3916 strcat (buf, ", 64-bit doubles");
3917 if (e_flags & E_FLAG_RX_DSP)
3918 strcat (buf, ", dsp");
3919 if (e_flags & E_FLAG_RX_PID)
3920 strcat (buf, ", pid");
3921 if (e_flags & E_FLAG_RX_ABI)
3922 strcat (buf, ", RX ABI");
3923 if (e_flags & E_FLAG_RX_SINSNS_SET)
3924 strcat (buf, e_flags & E_FLAG_RX_SINSNS_YES
3925 ? ", uses String instructions" : ", bans String instructions");
3926 if (e_flags & E_FLAG_RX_V2)
3927 strcat (buf, ", V2");
3928 if (e_flags & E_FLAG_RX_V3)
3929 strcat (buf, ", V3");
3930 break;
3931
3932 case EM_S390:
3933 if (e_flags & EF_S390_HIGH_GPRS)
3934 strcat (buf, ", highgprs");
3935 break;
3936
3937 case EM_TI_C6000:
3938 if ((e_flags & EF_C6000_REL))
3939 strcat (buf, ", relocatable module");
3940 break;
3941
3942 case EM_MSP430:
3943 strcat (buf, _(": architecture variant: "));
3944 switch (e_flags & EF_MSP430_MACH)
3945 {
3946 case E_MSP430_MACH_MSP430x11: strcat (buf, "MSP430x11"); break;
3947 case E_MSP430_MACH_MSP430x11x1 : strcat (buf, "MSP430x11x1 "); break;
3948 case E_MSP430_MACH_MSP430x12: strcat (buf, "MSP430x12"); break;
3949 case E_MSP430_MACH_MSP430x13: strcat (buf, "MSP430x13"); break;
3950 case E_MSP430_MACH_MSP430x14: strcat (buf, "MSP430x14"); break;
3951 case E_MSP430_MACH_MSP430x15: strcat (buf, "MSP430x15"); break;
3952 case E_MSP430_MACH_MSP430x16: strcat (buf, "MSP430x16"); break;
3953 case E_MSP430_MACH_MSP430x31: strcat (buf, "MSP430x31"); break;
3954 case E_MSP430_MACH_MSP430x32: strcat (buf, "MSP430x32"); break;
3955 case E_MSP430_MACH_MSP430x33: strcat (buf, "MSP430x33"); break;
3956 case E_MSP430_MACH_MSP430x41: strcat (buf, "MSP430x41"); break;
3957 case E_MSP430_MACH_MSP430x42: strcat (buf, "MSP430x42"); break;
3958 case E_MSP430_MACH_MSP430x43: strcat (buf, "MSP430x43"); break;
3959 case E_MSP430_MACH_MSP430x44: strcat (buf, "MSP430x44"); break;
3960 case E_MSP430_MACH_MSP430X : strcat (buf, "MSP430X"); break;
3961 default:
3962 strcat (buf, _(": unknown")); break;
3963 }
3964
3965 if (e_flags & ~ EF_MSP430_MACH)
3966 strcat (buf, _(": unknown extra flag bits also present"));
3967 break;
3968
3969 case EM_Z80:
3970 switch (e_flags & EF_Z80_MACH_MSK)
3971 {
3972 case EF_Z80_MACH_Z80: strcat (buf, ", Z80"); break;
3973 case EF_Z80_MACH_Z180: strcat (buf, ", Z180"); break;
3974 case EF_Z80_MACH_R800: strcat (buf, ", R800"); break;
3975 case EF_Z80_MACH_EZ80_Z80: strcat (buf, ", EZ80"); break;
3976 case EF_Z80_MACH_EZ80_ADL: strcat (buf, ", EZ80, ADL"); break;
3977 case EF_Z80_MACH_GBZ80: strcat (buf, ", GBZ80"); break;
3978 case EF_Z80_MACH_Z80N: strcat (buf, ", Z80N"); break;
3979 default:
3980 strcat (buf, _(", unknown")); break;
3981 }
3982 break;
3983 case EM_LOONGARCH:
3984 if (EF_LOONGARCH_IS_LP64 (e_flags))
3985 strcat (buf, ", LP64");
3986 else if (EF_LOONGARCH_IS_ILP32 (e_flags))
3987 strcat (buf, ", ILP32");
3988
3989 if (EF_LOONGARCH_IS_SOFT_FLOAT (e_flags))
3990 strcat (buf, ", SOFT-FLOAT");
3991 else if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
3992 strcat (buf, ", SINGLE-FLOAT");
3993 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
3994 strcat (buf, ", DOUBLE-FLOAT");
3995
3996 break;
3997 }
3998 }
3999
4000 return buf;
4001}
4002
4003static const char *
4004get_osabi_name (Filedata * filedata, unsigned int osabi)
4005{
4006 static char buff[32];
4007
4008 switch (osabi)
4009 {
4010 case ELFOSABI_NONE: return "UNIX - System V";
4011 case ELFOSABI_HPUX: return "UNIX - HP-UX";
4012 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
4013 case ELFOSABI_GNU: return "UNIX - GNU";
4014 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
4015 case ELFOSABI_AIX: return "UNIX - AIX";
4016 case ELFOSABI_IRIX: return "UNIX - IRIX";
4017 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
4018 case ELFOSABI_TRU64: return "UNIX - TRU64";
4019 case ELFOSABI_MODESTO: return "Novell - Modesto";
4020 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
4021 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
4022 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
4023 case ELFOSABI_AROS: return "AROS";
4024 case ELFOSABI_FENIXOS: return "FenixOS";
4025 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
4026 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
4027 default:
4028 if (osabi >= 64)
4029 switch (filedata->file_header.e_machine)
4030 {
4031 case EM_ARM:
4032 switch (osabi)
4033 {
4034 case ELFOSABI_ARM: return "ARM";
4035 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
4036 default:
4037 break;
4038 }
4039 break;
4040
4041 case EM_MSP430:
4042 case EM_MSP430_OLD:
4043 case EM_VISIUM:
4044 switch (osabi)
4045 {
4046 case ELFOSABI_STANDALONE: return _("Standalone App");
4047 default:
4048 break;
4049 }
4050 break;
4051
4052 case EM_TI_C6000:
4053 switch (osabi)
4054 {
4055 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
4056 case ELFOSABI_C6000_LINUX: return "Linux C6000";
4057 default:
4058 break;
4059 }
4060 break;
4061
4062 default:
4063 break;
4064 }
4065 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
4066 return buff;
4067 }
4068}
4069
4070static const char *
4071get_aarch64_segment_type (unsigned long type)
4072{
4073 switch (type)
4074 {
4075 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
4076 default: return NULL;
4077 }
4078}
4079
4080static const char *
4081get_arm_segment_type (unsigned long type)
4082{
4083 switch (type)
4084 {
4085 case PT_ARM_EXIDX: return "EXIDX";
4086 default: return NULL;
4087 }
4088}
4089
4090static const char *
4091get_s390_segment_type (unsigned long type)
4092{
4093 switch (type)
4094 {
4095 case PT_S390_PGSTE: return "S390_PGSTE";
4096 default: return NULL;
4097 }
4098}
4099
4100static const char *
4101get_mips_segment_type (unsigned long type)
4102{
4103 switch (type)
4104 {
4105 case PT_MIPS_REGINFO: return "REGINFO";
4106 case PT_MIPS_RTPROC: return "RTPROC";
4107 case PT_MIPS_OPTIONS: return "OPTIONS";
4108 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
4109 default: return NULL;
4110 }
4111}
4112
4113static const char *
4114get_parisc_segment_type (unsigned long type)
4115{
4116 switch (type)
4117 {
4118 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
4119 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
4120 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
4121 default: return NULL;
4122 }
4123}
4124
4125static const char *
4126get_ia64_segment_type (unsigned long type)
4127{
4128 switch (type)
4129 {
4130 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
4131 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
4132 default: return NULL;
4133 }
4134}
4135
4136static const char *
4137get_tic6x_segment_type (unsigned long type)
4138{
4139 switch (type)
4140 {
4141 case PT_C6000_PHATTR: return "C6000_PHATTR";
4142 default: return NULL;
4143 }
4144}
4145
4146static const char *
4147get_riscv_segment_type (unsigned long type)
4148{
4149 switch (type)
4150 {
4151 case PT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4152 default: return NULL;
4153 }
4154}
4155
4156static const char *
4157get_hpux_segment_type (unsigned long type, unsigned e_machine)
4158{
4159 if (e_machine == EM_PARISC)
4160 switch (type)
4161 {
4162 case PT_HP_TLS: return "HP_TLS";
4163 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
4164 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
4165 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
4166 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
4167 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
4168 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
4169 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
4170 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
4171 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
4172 case PT_HP_PARALLEL: return "HP_PARALLEL";
4173 case PT_HP_FASTBIND: return "HP_FASTBIND";
4174 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
4175 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
4176 case PT_HP_STACK: return "HP_STACK";
4177 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
4178 default: return NULL;
4179 }
4180
4181 if (e_machine == EM_IA_64)
4182 switch (type)
4183 {
4184 case PT_HP_TLS: return "HP_TLS";
4185 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
4186 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
4187 case PT_IA_64_HP_STACK: return "HP_STACK";
4188 default: return NULL;
4189 }
4190
4191 return NULL;
4192}
4193
4194static const char *
4195get_solaris_segment_type (unsigned long type)
4196{
4197 switch (type)
4198 {
4199 case 0x6464e550: return "PT_SUNW_UNWIND";
4200 case 0x6474e550: return "PT_SUNW_EH_FRAME";
4201 case 0x6ffffff7: return "PT_LOSUNW";
4202 case 0x6ffffffa: return "PT_SUNWBSS";
4203 case 0x6ffffffb: return "PT_SUNWSTACK";
4204 case 0x6ffffffc: return "PT_SUNWDTRACE";
4205 case 0x6ffffffd: return "PT_SUNWCAP";
4206 case 0x6fffffff: return "PT_HISUNW";
4207 default: return NULL;
4208 }
4209}
4210
4211static const char *
4212get_segment_type (Filedata * filedata, unsigned long p_type)
4213{
4214 static char buff[32];
4215
4216 switch (p_type)
4217 {
4218 case PT_NULL: return "NULL";
4219 case PT_LOAD: return "LOAD";
4220 case PT_DYNAMIC: return "DYNAMIC";
4221 case PT_INTERP: return "INTERP";
4222 case PT_NOTE: return "NOTE";
4223 case PT_SHLIB: return "SHLIB";
4224 case PT_PHDR: return "PHDR";
4225 case PT_TLS: return "TLS";
4226 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
4227 case PT_GNU_STACK: return "GNU_STACK";
4228 case PT_GNU_RELRO: return "GNU_RELRO";
4229 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
4230
4231 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
4232 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
4233 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
4234
4235 default:
4236 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
4237 {
4238 const char * result;
4239
4240 switch (filedata->file_header.e_machine)
4241 {
4242 case EM_AARCH64:
4243 result = get_aarch64_segment_type (p_type);
4244 break;
4245 case EM_ARM:
4246 result = get_arm_segment_type (p_type);
4247 break;
4248 case EM_MIPS:
4249 case EM_MIPS_RS3_LE:
4250 result = get_mips_segment_type (p_type);
4251 break;
4252 case EM_PARISC:
4253 result = get_parisc_segment_type (p_type);
4254 break;
4255 case EM_IA_64:
4256 result = get_ia64_segment_type (p_type);
4257 break;
4258 case EM_TI_C6000:
4259 result = get_tic6x_segment_type (p_type);
4260 break;
4261 case EM_S390:
4262 case EM_S390_OLD:
4263 result = get_s390_segment_type (p_type);
4264 break;
4265 case EM_RISCV:
4266 result = get_riscv_segment_type (p_type);
4267 break;
4268 default:
4269 result = NULL;
4270 break;
4271 }
4272
4273 if (result != NULL)
4274 return result;
4275
4276 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
4277 }
4278 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
4279 {
4280 const char * result = NULL;
4281
4282 switch (filedata->file_header.e_ident[EI_OSABI])
4283 {
4284 case ELFOSABI_GNU:
4285 case ELFOSABI_FREEBSD:
4286 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
4287 {
4288 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
4289 result = buff;
4290 }
4291 break;
4292 case ELFOSABI_HPUX:
4293 result = get_hpux_segment_type (p_type,
4294 filedata->file_header.e_machine);
4295 break;
4296 case ELFOSABI_SOLARIS:
4297 result = get_solaris_segment_type (p_type);
4298 break;
4299 default:
4300 break;
4301 }
4302 if (result != NULL)
4303 return result;
4304
4305 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
4306 }
4307 else
4308 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
4309
4310 return buff;
4311 }
4312}
4313
4314static const char *
4315get_arc_section_type_name (unsigned int sh_type)
4316{
4317 switch (sh_type)
4318 {
4319 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
4320 default:
4321 break;
4322 }
4323 return NULL;
4324}
4325
4326static const char *
4327get_mips_section_type_name (unsigned int sh_type)
4328{
4329 switch (sh_type)
4330 {
4331 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
4332 case SHT_MIPS_MSYM: return "MIPS_MSYM";
4333 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
4334 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
4335 case SHT_MIPS_UCODE: return "MIPS_UCODE";
4336 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
4337 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
4338 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
4339 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
4340 case SHT_MIPS_RELD: return "MIPS_RELD";
4341 case SHT_MIPS_IFACE: return "MIPS_IFACE";
4342 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
4343 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
4344 case SHT_MIPS_SHDR: return "MIPS_SHDR";
4345 case SHT_MIPS_FDESC: return "MIPS_FDESC";
4346 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
4347 case SHT_MIPS_DENSE: return "MIPS_DENSE";
4348 case SHT_MIPS_PDESC: return "MIPS_PDESC";
4349 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
4350 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
4351 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
4352 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
4353 case SHT_MIPS_LINE: return "MIPS_LINE";
4354 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
4355 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
4356 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
4357 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
4358 case SHT_MIPS_DWARF: return "MIPS_DWARF";
4359 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
4360 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
4361 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
4362 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
4363 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
4364 case SHT_MIPS_XLATE: return "MIPS_XLATE";
4365 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
4366 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
4367 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
4368 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
4369 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
4370 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
4371 case SHT_MIPS_XHASH: return "MIPS_XHASH";
4372 default:
4373 break;
4374 }
4375 return NULL;
4376}
4377
4378static const char *
4379get_parisc_section_type_name (unsigned int sh_type)
4380{
4381 switch (sh_type)
4382 {
4383 case SHT_PARISC_EXT: return "PARISC_EXT";
4384 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
4385 case SHT_PARISC_DOC: return "PARISC_DOC";
4386 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
4387 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
4388 case SHT_PARISC_STUBS: return "PARISC_STUBS";
4389 case SHT_PARISC_DLKM: return "PARISC_DLKM";
4390 default: return NULL;
4391 }
4392}
4393
4394static const char *
4395get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
4396{
4397 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
4398 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
4399 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
4400
4401 switch (sh_type)
4402 {
4403 case SHT_IA_64_EXT: return "IA_64_EXT";
4404 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
4405 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
4406 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
4407 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
4408 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
4409 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
4410 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
4411 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
4412 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
4413 default:
4414 break;
4415 }
4416 return NULL;
4417}
4418
4419static const char *
4420get_x86_64_section_type_name (unsigned int sh_type)
4421{
4422 switch (sh_type)
4423 {
4424 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
4425 default: return NULL;
4426 }
4427}
4428
4429static const char *
4430get_aarch64_section_type_name (unsigned int sh_type)
4431{
4432 switch (sh_type)
4433 {
4434 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
4435 default: return NULL;
4436 }
4437}
4438
4439static const char *
4440get_arm_section_type_name (unsigned int sh_type)
4441{
4442 switch (sh_type)
4443 {
4444 case SHT_ARM_EXIDX: return "ARM_EXIDX";
4445 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
4446 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
4447 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
4448 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
4449 default: return NULL;
4450 }
4451}
4452
4453static const char *
4454get_tic6x_section_type_name (unsigned int sh_type)
4455{
4456 switch (sh_type)
4457 {
4458 case SHT_C6000_UNWIND: return "C6000_UNWIND";
4459 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
4460 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
4461 case SHT_TI_ICODE: return "TI_ICODE";
4462 case SHT_TI_XREF: return "TI_XREF";
4463 case SHT_TI_HANDLER: return "TI_HANDLER";
4464 case SHT_TI_INITINFO: return "TI_INITINFO";
4465 case SHT_TI_PHATTRS: return "TI_PHATTRS";
4466 default: return NULL;
4467 }
4468}
4469
4470static const char *
4471get_msp430_section_type_name (unsigned int sh_type)
4472{
4473 switch (sh_type)
4474 {
4475 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
4476 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
4477 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
4478 default: return NULL;
4479 }
4480}
4481
4482static const char *
4483get_nfp_section_type_name (unsigned int sh_type)
4484{
4485 switch (sh_type)
4486 {
4487 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
4488 case SHT_NFP_INITREG: return "NFP_INITREG";
4489 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
4490 default: return NULL;
4491 }
4492}
4493
4494static const char *
4495get_v850_section_type_name (unsigned int sh_type)
4496{
4497 switch (sh_type)
4498 {
4499 case SHT_V850_SCOMMON: return "V850 Small Common";
4500 case SHT_V850_TCOMMON: return "V850 Tiny Common";
4501 case SHT_V850_ZCOMMON: return "V850 Zero Common";
4502 case SHT_RENESAS_IOP: return "RENESAS IOP";
4503 case SHT_RENESAS_INFO: return "RENESAS INFO";
4504 default: return NULL;
4505 }
4506}
4507
4508static const char *
4509get_riscv_section_type_name (unsigned int sh_type)
4510{
4511 switch (sh_type)
4512 {
4513 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
4514 default: return NULL;
4515 }
4516}
4517
4518static const char *
4519get_csky_section_type_name (unsigned int sh_type)
4520{
4521 switch (sh_type)
4522 {
4523 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
4524 default: return NULL;
4525 }
4526}
4527
4528static const char *
4529get_section_type_name (Filedata * filedata, unsigned int sh_type)
4530{
4531 static char buff[32];
4532 const char * result;
4533
4534 switch (sh_type)
4535 {
4536 case SHT_NULL: return "NULL";
4537 case SHT_PROGBITS: return "PROGBITS";
4538 case SHT_SYMTAB: return "SYMTAB";
4539 case SHT_STRTAB: return "STRTAB";
4540 case SHT_RELA: return "RELA";
4541 case SHT_RELR: return "RELR";
4542 case SHT_HASH: return "HASH";
4543 case SHT_DYNAMIC: return "DYNAMIC";
4544 case SHT_NOTE: return "NOTE";
4545 case SHT_NOBITS: return "NOBITS";
4546 case SHT_REL: return "REL";
4547 case SHT_SHLIB: return "SHLIB";
4548 case SHT_DYNSYM: return "DYNSYM";
4549 case SHT_INIT_ARRAY: return "INIT_ARRAY";
4550 case SHT_FINI_ARRAY: return "FINI_ARRAY";
4551 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
4552 case SHT_GNU_HASH: return "GNU_HASH";
4553 case SHT_GROUP: return "GROUP";
4554 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
4555 case SHT_GNU_verdef: return "VERDEF";
4556 case SHT_GNU_verneed: return "VERNEED";
4557 case SHT_GNU_versym: return "VERSYM";
4558 case 0x6ffffff0: return "VERSYM";
4559 case 0x6ffffffc: return "VERDEF";
4560 case 0x7ffffffd: return "AUXILIARY";
4561 case 0x7fffffff: return "FILTER";
4562 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
4563
4564 default:
4565 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
4566 {
4567 switch (filedata->file_header.e_machine)
4568 {
4569 case EM_ARC:
4570 case EM_ARC_COMPACT:
4571 case EM_ARC_COMPACT2:
4572 result = get_arc_section_type_name (sh_type);
4573 break;
4574 case EM_MIPS:
4575 case EM_MIPS_RS3_LE:
4576 result = get_mips_section_type_name (sh_type);
4577 break;
4578 case EM_PARISC:
4579 result = get_parisc_section_type_name (sh_type);
4580 break;
4581 case EM_IA_64:
4582 result = get_ia64_section_type_name (filedata, sh_type);
4583 break;
4584 case EM_X86_64:
4585 case EM_L1OM:
4586 case EM_K1OM:
4587 result = get_x86_64_section_type_name (sh_type);
4588 break;
4589 case EM_AARCH64:
4590 result = get_aarch64_section_type_name (sh_type);
4591 break;
4592 case EM_ARM:
4593 result = get_arm_section_type_name (sh_type);
4594 break;
4595 case EM_TI_C6000:
4596 result = get_tic6x_section_type_name (sh_type);
4597 break;
4598 case EM_MSP430:
4599 result = get_msp430_section_type_name (sh_type);
4600 break;
4601 case EM_NFP:
4602 result = get_nfp_section_type_name (sh_type);
4603 break;
4604 case EM_V800:
4605 case EM_V850:
4606 case EM_CYGNUS_V850:
4607 result = get_v850_section_type_name (sh_type);
4608 break;
4609 case EM_RISCV:
4610 result = get_riscv_section_type_name (sh_type);
4611 break;
4612 case EM_CSKY:
4613 result = get_csky_section_type_name (sh_type);
4614 break;
4615 default:
4616 result = NULL;
4617 break;
4618 }
4619
4620 if (result != NULL)
4621 return result;
4622
4623 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
4624 }
4625 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
4626 {
4627 switch (filedata->file_header.e_machine)
4628 {
4629 case EM_IA_64:
4630 result = get_ia64_section_type_name (filedata, sh_type);
4631 break;
4632 default:
4633 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
4634 result = get_solaris_section_type (sh_type);
4635 else
4636 {
4637 switch (sh_type)
4638 {
4639 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
4640 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
4641 case SHT_GNU_HASH: result = "GNU_HASH"; break;
4642 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
4643 default:
4644 result = NULL;
4645 break;
4646 }
4647 }
4648 break;
4649 }
4650
4651 if (result != NULL)
4652 return result;
4653
4654 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
4655 }
4656 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
4657 {
4658 switch (filedata->file_header.e_machine)
4659 {
4660 case EM_V800:
4661 case EM_V850:
4662 case EM_CYGNUS_V850:
4663 result = get_v850_section_type_name (sh_type);
4664 break;
4665 default:
4666 result = NULL;
4667 break;
4668 }
4669
4670 if (result != NULL)
4671 return result;
4672
4673 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
4674 }
4675 else
4676 /* This message is probably going to be displayed in a 15
4677 character wide field, so put the hex value first. */
4678 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
4679
4680 return buff;
4681 }
4682}
4683
4684enum long_option_values
4685{
4686 OPTION_DEBUG_DUMP = 512,
4687 OPTION_DYN_SYMS,
4688 OPTION_LTO_SYMS,
4689 OPTION_DWARF_DEPTH,
4690 OPTION_DWARF_START,
4691 OPTION_DWARF_CHECK,
4692 OPTION_CTF_DUMP,
4693 OPTION_CTF_PARENT,
4694 OPTION_CTF_SYMBOLS,
4695 OPTION_CTF_STRINGS,
4696 OPTION_WITH_SYMBOL_VERSIONS,
4697 OPTION_RECURSE_LIMIT,
4698 OPTION_NO_RECURSE_LIMIT,
4699 OPTION_NO_DEMANGLING,
4700 OPTION_SYM_BASE
4701};
4702
4703static struct option options[] =
4704{
4705 /* Note - This table is alpha-sorted on the 'val'
4706 field in order to make adding new options easier. */
4707 {"arch-specific", no_argument, 0, 'A'},
4708 {"all", no_argument, 0, 'a'},
4709 {"demangle", optional_argument, 0, 'C'},
4710 {"archive-index", no_argument, 0, 'c'},
4711 {"use-dynamic", no_argument, 0, 'D'},
4712 {"dynamic", no_argument, 0, 'd'},
4713 {"headers", no_argument, 0, 'e'},
4714 {"section-groups", no_argument, 0, 'g'},
4715 {"help", no_argument, 0, 'H'},
4716 {"file-header", no_argument, 0, 'h'},
4717 {"histogram", no_argument, 0, 'I'},
4718 {"lint", no_argument, 0, 'L'},
4719 {"enable-checks", no_argument, 0, 'L'},
4720 {"program-headers", no_argument, 0, 'l'},
4721 {"segments", no_argument, 0, 'l'},
4722 {"full-section-name",no_argument, 0, 'N'},
4723 {"notes", no_argument, 0, 'n'},
4724 {"process-links", no_argument, 0, 'P'},
4725 {"string-dump", required_argument, 0, 'p'},
4726 {"relocated-dump", required_argument, 0, 'R'},
4727 {"relocs", no_argument, 0, 'r'},
4728 {"section-headers", no_argument, 0, 'S'},
4729 {"sections", no_argument, 0, 'S'},
4730 {"symbols", no_argument, 0, 's'},
4731 {"syms", no_argument, 0, 's'},
4732 {"silent-truncation",no_argument, 0, 'T'},
4733 {"section-details", no_argument, 0, 't'},
4734 {"unwind", no_argument, 0, 'u'},
4735 {"version-info", no_argument, 0, 'V'},
4736 {"version", no_argument, 0, 'v'},
4737 {"wide", no_argument, 0, 'W'},
4738 {"hex-dump", required_argument, 0, 'x'},
4739 {"decompress", no_argument, 0, 'z'},
4740
4741 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
4742 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
4743 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4744 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
4745 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
4746 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
4747 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
4748 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
4749 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
4750 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
4751#ifdef ENABLE_LIBCTF
4752 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
4753 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
4754 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
4755 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
4756#endif
4757 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
4758
4759 {0, no_argument, 0, 0}
4760};
4761
4762static void
4763usage (FILE * stream)
4764{
4765 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
4766 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
4767 fprintf (stream, _(" Options are:\n"));
4768 fprintf (stream, _("\
4769 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
4770 fprintf (stream, _("\
4771 -h --file-header Display the ELF file header\n"));
4772 fprintf (stream, _("\
4773 -l --program-headers Display the program headers\n"));
4774 fprintf (stream, _("\
4775 --segments An alias for --program-headers\n"));
4776 fprintf (stream, _("\
4777 -S --section-headers Display the sections' header\n"));
4778 fprintf (stream, _("\
4779 --sections An alias for --section-headers\n"));
4780 fprintf (stream, _("\
4781 -g --section-groups Display the section groups\n"));
4782 fprintf (stream, _("\
4783 -t --section-details Display the section details\n"));
4784 fprintf (stream, _("\
4785 -e --headers Equivalent to: -h -l -S\n"));
4786 fprintf (stream, _("\
4787 -s --syms Display the symbol table\n"));
4788 fprintf (stream, _("\
4789 --symbols An alias for --syms\n"));
4790 fprintf (stream, _("\
4791 --dyn-syms Display the dynamic symbol table\n"));
4792 fprintf (stream, _("\
4793 --lto-syms Display LTO symbol tables\n"));
4794 fprintf (stream, _("\
4795 --sym-base=[0|8|10|16] \n\
4796 Force base for symbol sizes. The options are \n\
4797 mixed (the default), octal, decimal, hexadecimal.\n"));
4798 fprintf (stream, _("\
4799 -C --demangle[=STYLE] Decode low-level symbol names into user-level names\n\
4800 The STYLE, if specified, can be `auto' (the default),\n\
4801 `gnu', `lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
4802 or `gnat'\n"));
4803 fprintf (stream, _("\
4804 --no-demangle Do not demangle low-level symbol names. (default)\n"));
4805 fprintf (stream, _("\
4806 --recurse-limit Enable a demangling recursion limit. (default)\n"));
4807 fprintf (stream, _("\
4808 --no-recurse-limit Disable a demangling recursion limit\n"));
4809 fprintf (stream, _("\
4810 -n --notes Display the core notes (if present)\n"));
4811 fprintf (stream, _("\
4812 -r --relocs Display the relocations (if present)\n"));
4813 fprintf (stream, _("\
4814 -u --unwind Display the unwind info (if present)\n"));
4815 fprintf (stream, _("\
4816 -d --dynamic Display the dynamic section (if present)\n"));
4817 fprintf (stream, _("\
4818 -V --version-info Display the version sections (if present)\n"));
4819 fprintf (stream, _("\
4820 -A --arch-specific Display architecture specific information (if any)\n"));
4821 fprintf (stream, _("\
4822 -c --archive-index Display the symbol/file index in an archive\n"));
4823 fprintf (stream, _("\
4824 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
4825 fprintf (stream, _("\
4826 -L --lint|--enable-checks\n\
4827 Display warning messages for possible problems\n"));
4828 fprintf (stream, _("\
4829 -x --hex-dump=<number|name>\n\
4830 Dump the contents of section <number|name> as bytes\n"));
4831 fprintf (stream, _("\
4832 -p --string-dump=<number|name>\n\
4833 Dump the contents of section <number|name> as strings\n"));
4834 fprintf (stream, _("\
4835 -R --relocated-dump=<number|name>\n\
4836 Dump the relocated contents of section <number|name>\n"));
4837 fprintf (stream, _("\
4838 -z --decompress Decompress section before dumping it\n"));
4839 fprintf (stream, _("\
4840 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
4841 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
4842 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
4843 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
4844 U/=trace_info]\n\
4845 Display the contents of DWARF debug sections\n"));
4846 fprintf (stream, _("\
4847 -wk --debug-dump=links Display the contents of sections that link to separate\n\
4848 debuginfo files\n"));
4849 fprintf (stream, _("\
4850 -P --process-links Display the contents of non-debug sections in separate\n\
4851 debuginfo files. (Implies -wK)\n"));
4852#if DEFAULT_FOR_FOLLOW_LINKS
4853 fprintf (stream, _("\
4854 -wK --debug-dump=follow-links\n\
4855 Follow links to separate debug info files (default)\n"));
4856 fprintf (stream, _("\
4857 -wN --debug-dump=no-follow-links\n\
4858 Do not follow links to separate debug info files\n"));
4859#else
4860 fprintf (stream, _("\
4861 -wK --debug-dump=follow-links\n\
4862 Follow links to separate debug info files\n"));
4863 fprintf (stream, _("\
4864 -wN --debug-dump=no-follow-links\n\
4865 Do not follow links to separate debug info files\n\
4866 (default)\n"));
4867#endif
4868 fprintf (stream, _("\
4869 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
4870 fprintf (stream, _("\
4871 --dwarf-start=N Display DIEs starting at offset N\n"));
4872#ifdef ENABLE_LIBCTF
4873 fprintf (stream, _("\
4874 --ctf=<number|name> Display CTF info from section <number|name>\n"));
4875 fprintf (stream, _("\
4876 --ctf-parent=<name> Use CTF archive member <name> as the CTF parent\n"));
4877 fprintf (stream, _("\
4878 --ctf-symbols=<number|name>\n\
4879 Use section <number|name> as the CTF external symtab\n"));
4880 fprintf (stream, _("\
4881 --ctf-strings=<number|name>\n\
4882 Use section <number|name> as the CTF external strtab\n"));
4883#endif
4884
4885#ifdef SUPPORT_DISASSEMBLY
4886 fprintf (stream, _("\
4887 -i --instruction-dump=<number|name>\n\
4888 Disassemble the contents of section <number|name>\n"));
4889#endif
4890 fprintf (stream, _("\
4891 -I --histogram Display histogram of bucket list lengths\n"));
4892 fprintf (stream, _("\
4893 -W --wide Allow output width to exceed 80 characters\n"));
4894 fprintf (stream, _("\
4895 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
4896 fprintf (stream, _("\
4897 @<file> Read options from <file>\n"));
4898 fprintf (stream, _("\
4899 -H --help Display this information\n"));
4900 fprintf (stream, _("\
4901 -v --version Display the version number of readelf\n"));
4902
4903 if (REPORT_BUGS_TO[0] && stream == stdout)
4904 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
4905
4906 exit (stream == stdout ? 0 : 1);
4907}
4908
4909/* Record the fact that the user wants the contents of section number
4910 SECTION to be displayed using the method(s) encoded as flags bits
4911 in TYPE. Note, TYPE can be zero if we are creating the array for
4912 the first time. */
4913
4914static void
4915request_dump_bynumber (struct dump_data *dumpdata,
4916 unsigned int section, dump_type type)
4917{
4918 if (section >= dumpdata->num_dump_sects)
4919 {
4920 dump_type * new_dump_sects;
4921
4922 new_dump_sects = (dump_type *) calloc (section + 1,
4923 sizeof (* new_dump_sects));
4924
4925 if (new_dump_sects == NULL)
4926 error (_("Out of memory allocating dump request table.\n"));
4927 else
4928 {
4929 if (dumpdata->dump_sects)
4930 {
4931 /* Copy current flag settings. */
4932 memcpy (new_dump_sects, dumpdata->dump_sects,
4933 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
4934
4935 free (dumpdata->dump_sects);
4936 }
4937
4938 dumpdata->dump_sects = new_dump_sects;
4939 dumpdata->num_dump_sects = section + 1;
4940 }
4941 }
4942
4943 if (dumpdata->dump_sects)
4944 dumpdata->dump_sects[section] |= type;
4945}
4946
4947/* Request a dump by section name. */
4948
4949static void
4950request_dump_byname (const char * section, dump_type type)
4951{
4952 struct dump_list_entry * new_request;
4953
4954 new_request = (struct dump_list_entry *)
4955 malloc (sizeof (struct dump_list_entry));
4956 if (!new_request)
4957 error (_("Out of memory allocating dump request table.\n"));
4958
4959 new_request->name = strdup (section);
4960 if (!new_request->name)
4961 error (_("Out of memory allocating dump request table.\n"));
4962
4963 new_request->type = type;
4964
4965 new_request->next = dump_sects_byname;
4966 dump_sects_byname = new_request;
4967}
4968
4969static inline void
4970request_dump (struct dump_data *dumpdata, dump_type type)
4971{
4972 int section;
4973 char * cp;
4974
4975 do_dump = true;
4976 section = strtoul (optarg, & cp, 0);
4977
4978 if (! *cp && section >= 0)
4979 request_dump_bynumber (dumpdata, section, type);
4980 else
4981 request_dump_byname (optarg, type);
4982}
4983
4984static void
4985parse_args (struct dump_data *dumpdata, int argc, char ** argv)
4986{
4987 int c;
4988
4989 if (argc < 2)
4990 usage (stderr);
4991
4992 while ((c = getopt_long
4993 (argc, argv, "ACDHILNPR:STVWacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
4994 {
4995 switch (c)
4996 {
4997 case 0:
4998 /* Long options. */
4999 break;
5000 case 'H':
5001 usage (stdout);
5002 break;
5003
5004 case 'a':
5005 do_syms = true;
5006 do_reloc = true;
5007 do_unwind = true;
5008 do_dynamic = true;
5009 do_header = true;
5010 do_sections = true;
5011 do_section_groups = true;
5012 do_segments = true;
5013 do_version = true;
5014 do_histogram = true;
5015 do_arch = true;
5016 do_notes = true;
5017 break;
5018
5019 case 'g':
5020 do_section_groups = true;
5021 break;
5022 case 't':
5023 case 'N':
5024 do_sections = true;
5025 do_section_details = true;
5026 break;
5027 case 'e':
5028 do_header = true;
5029 do_sections = true;
5030 do_segments = true;
5031 break;
5032 case 'A':
5033 do_arch = true;
5034 break;
5035 case 'D':
5036 do_using_dynamic = true;
5037 break;
5038 case 'r':
5039 do_reloc = true;
5040 break;
5041 case 'u':
5042 do_unwind = true;
5043 break;
5044 case 'h':
5045 do_header = true;
5046 break;
5047 case 'l':
5048 do_segments = true;
5049 break;
5050 case 's':
5051 do_syms = true;
5052 break;
5053 case 'S':
5054 do_sections = true;
5055 break;
5056 case 'd':
5057 do_dynamic = true;
5058 break;
5059 case 'I':
5060 do_histogram = true;
5061 break;
5062 case 'n':
5063 do_notes = true;
5064 break;
5065 case 'c':
5066 do_archive_index = true;
5067 break;
5068 case 'L':
5069 do_checks = true;
5070 break;
5071 case 'P':
5072 process_links = true;
5073 do_follow_links = true;
5074 break;
5075 case 'x':
5076 request_dump (dumpdata, HEX_DUMP);
5077 break;
5078 case 'p':
5079 request_dump (dumpdata, STRING_DUMP);
5080 break;
5081 case 'R':
5082 request_dump (dumpdata, RELOC_DUMP);
5083 break;
5084 case 'z':
5085 decompress_dumps = true;
5086 break;
5087 case 'w':
5088 do_dump = true;
5089 if (optarg == NULL)
5090 {
5091 do_debugging = true;
5092 dwarf_select_sections_all ();
5093 }
5094 else
5095 {
5096 do_debugging = false;
5097 dwarf_select_sections_by_letters (optarg);
5098 }
5099 break;
5100 case OPTION_DEBUG_DUMP:
5101 do_dump = true;
5102 if (optarg == NULL)
5103 {
5104 do_debugging = true;
5105 dwarf_select_sections_all ();
5106 }
5107 else
5108 {
5109 do_debugging = false;
5110 dwarf_select_sections_by_names (optarg);
5111 }
5112 break;
5113 case OPTION_DWARF_DEPTH:
5114 {
5115 char *cp;
5116
5117 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
5118 }
5119 break;
5120 case OPTION_DWARF_START:
5121 {
5122 char *cp;
5123
5124 dwarf_start_die = strtoul (optarg, & cp, 0);
5125 }
5126 break;
5127 case OPTION_DWARF_CHECK:
5128 dwarf_check = true;
5129 break;
5130 case OPTION_CTF_DUMP:
5131 do_ctf = true;
5132 request_dump (dumpdata, CTF_DUMP);
5133 break;
5134 case OPTION_CTF_SYMBOLS:
5135 free (dump_ctf_symtab_name);
5136 dump_ctf_symtab_name = strdup (optarg);
5137 break;
5138 case OPTION_CTF_STRINGS:
5139 free (dump_ctf_strtab_name);
5140 dump_ctf_strtab_name = strdup (optarg);
5141 break;
5142 case OPTION_CTF_PARENT:
5143 free (dump_ctf_parent_name);
5144 dump_ctf_parent_name = strdup (optarg);
5145 break;
5146 case OPTION_DYN_SYMS:
5147 do_dyn_syms = true;
5148 break;
5149 case OPTION_LTO_SYMS:
5150 do_lto_syms = true;
5151 break;
5152#ifdef SUPPORT_DISASSEMBLY
5153 case 'i':
5154 request_dump (dumpdata, DISASS_DUMP);
5155 break;
5156#endif
5157 case 'v':
5158 print_version (program_name);
5159 break;
5160 case 'V':
5161 do_version = true;
5162 break;
5163 case 'W':
5164 do_wide = true;
5165 break;
5166 case 'T':
5167 do_not_show_symbol_truncation = true;
5168 break;
5169 case 'C':
5170 do_demangle = true;
5171 if (optarg != NULL)
5172 {
5173 enum demangling_styles style;
5174
5175 style = cplus_demangle_name_to_style (optarg);
5176 if (style == unknown_demangling)
5177 error (_("unknown demangling style `%s'"), optarg);
5178
5179 cplus_demangle_set_style (style);
5180 }
5181 break;
5182 case OPTION_NO_DEMANGLING:
5183 do_demangle = false;
5184 break;
5185 case OPTION_RECURSE_LIMIT:
5186 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
5187 break;
5188 case OPTION_NO_RECURSE_LIMIT:
5189 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
5190 break;
5191 case OPTION_WITH_SYMBOL_VERSIONS:
5192 /* Ignored for backward compatibility. */
5193 break;
5194
5195 case OPTION_SYM_BASE:
5196 sym_base = 0;
5197 if (optarg != NULL)
5198 {
5199 sym_base = strtoul (optarg, NULL, 0);
5200 switch (sym_base)
5201 {
5202 case 0:
5203 case 8:
5204 case 10:
5205 case 16:
5206 break;
5207
5208 default:
5209 sym_base = 0;
5210 break;
5211 }
5212 }
5213 break;
5214
5215 default:
5216 /* xgettext:c-format */
5217 error (_("Invalid option '-%c'\n"), c);
5218 /* Fall through. */
5219 case '?':
5220 usage (stderr);
5221 }
5222 }
5223
5224 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
5225 && !do_segments && !do_header && !do_dump && !do_version
5226 && !do_histogram && !do_debugging && !do_arch && !do_notes
5227 && !do_section_groups && !do_archive_index
5228 && !do_dyn_syms && !do_lto_syms)
5229 {
5230 if (do_checks)
5231 {
5232 check_all = true;
5233 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
5234 do_segments = do_header = do_dump = do_version = true;
5235 do_histogram = do_debugging = do_arch = do_notes = true;
5236 do_section_groups = do_archive_index = do_dyn_syms = true;
5237 do_lto_syms = true;
5238 }
5239 else
5240 usage (stderr);
5241 }
5242}
5243
5244static const char *
5245get_elf_class (unsigned int elf_class)
5246{
5247 static char buff[32];
5248
5249 switch (elf_class)
5250 {
5251 case ELFCLASSNONE: return _("none");
5252 case ELFCLASS32: return "ELF32";
5253 case ELFCLASS64: return "ELF64";
5254 default:
5255 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
5256 return buff;
5257 }
5258}
5259
5260static const char *
5261get_data_encoding (unsigned int encoding)
5262{
5263 static char buff[32];
5264
5265 switch (encoding)
5266 {
5267 case ELFDATANONE: return _("none");
5268 case ELFDATA2LSB: return _("2's complement, little endian");
5269 case ELFDATA2MSB: return _("2's complement, big endian");
5270 default:
5271 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
5272 return buff;
5273 }
5274}
5275
5276/* Decode the data held in 'filedata->file_header'. */
5277
5278static bool
5279process_file_header (Filedata * filedata)
5280{
5281 Elf_Internal_Ehdr * header = & filedata->file_header;
5282
5283 if ( header->e_ident[EI_MAG0] != ELFMAG0
5284 || header->e_ident[EI_MAG1] != ELFMAG1
5285 || header->e_ident[EI_MAG2] != ELFMAG2
5286 || header->e_ident[EI_MAG3] != ELFMAG3)
5287 {
5288 error
5289 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
5290 return false;
5291 }
5292
5293 if (! filedata->is_separate)
5294 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
5295
5296 if (do_header)
5297 {
5298 unsigned i;
5299
5300 if (filedata->is_separate)
5301 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
5302 else
5303 printf (_("ELF Header:\n"));
5304 printf (_(" Magic: "));
5305 for (i = 0; i < EI_NIDENT; i++)
5306 printf ("%2.2x ", header->e_ident[i]);
5307 printf ("\n");
5308 printf (_(" Class: %s\n"),
5309 get_elf_class (header->e_ident[EI_CLASS]));
5310 printf (_(" Data: %s\n"),
5311 get_data_encoding (header->e_ident[EI_DATA]));
5312 printf (_(" Version: %d%s\n"),
5313 header->e_ident[EI_VERSION],
5314 (header->e_ident[EI_VERSION] == EV_CURRENT
5315 ? _(" (current)")
5316 : (header->e_ident[EI_VERSION] != EV_NONE
5317 ? _(" <unknown>")
5318 : "")));
5319 printf (_(" OS/ABI: %s\n"),
5320 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
5321 printf (_(" ABI Version: %d\n"),
5322 header->e_ident[EI_ABIVERSION]);
5323 printf (_(" Type: %s\n"),
5324 get_file_type (filedata));
5325 printf (_(" Machine: %s\n"),
5326 get_machine_name (header->e_machine));
5327 printf (_(" Version: 0x%lx\n"),
5328 header->e_version);
5329
5330 printf (_(" Entry point address: "));
5331 print_vma (header->e_entry, PREFIX_HEX);
5332 printf (_("\n Start of program headers: "));
5333 print_vma (header->e_phoff, DEC);
5334 printf (_(" (bytes into file)\n Start of section headers: "));
5335 print_vma (header->e_shoff, DEC);
5336 printf (_(" (bytes into file)\n"));
5337
5338 printf (_(" Flags: 0x%lx%s\n"),
5339 header->e_flags,
5340 get_machine_flags (filedata, header->e_flags, header->e_machine));
5341 printf (_(" Size of this header: %u (bytes)\n"),
5342 header->e_ehsize);
5343 printf (_(" Size of program headers: %u (bytes)\n"),
5344 header->e_phentsize);
5345 printf (_(" Number of program headers: %u"),
5346 header->e_phnum);
5347 if (filedata->section_headers != NULL
5348 && header->e_phnum == PN_XNUM
5349 && filedata->section_headers[0].sh_info != 0)
5350 {
5351 header->e_phnum = filedata->section_headers[0].sh_info;
5352 printf (" (%u)", header->e_phnum);
5353 }
5354 putc ('\n', stdout);
5355 printf (_(" Size of section headers: %u (bytes)\n"),
5356 header->e_shentsize);
5357 printf (_(" Number of section headers: %u"),
5358 header->e_shnum);
5359 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
5360 {
5361 header->e_shnum = filedata->section_headers[0].sh_size;
5362 printf (" (%u)", header->e_shnum);
5363 }
5364 putc ('\n', stdout);
5365 printf (_(" Section header string table index: %u"),
5366 header->e_shstrndx);
5367 if (filedata->section_headers != NULL
5368 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
5369 {
5370 header->e_shstrndx = filedata->section_headers[0].sh_link;
5371 printf (" (%u)", header->e_shstrndx);
5372 }
5373 if (header->e_shstrndx != SHN_UNDEF
5374 && header->e_shstrndx >= header->e_shnum)
5375 {
5376 header->e_shstrndx = SHN_UNDEF;
5377 printf (_(" <corrupt: out of range>"));
5378 }
5379 putc ('\n', stdout);
5380 }
5381
5382 if (filedata->section_headers != NULL)
5383 {
5384 if (header->e_phnum == PN_XNUM
5385 && filedata->section_headers[0].sh_info != 0)
5386 header->e_phnum = filedata->section_headers[0].sh_info;
5387 if (header->e_shnum == SHN_UNDEF)
5388 header->e_shnum = filedata->section_headers[0].sh_size;
5389 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
5390 header->e_shstrndx = filedata->section_headers[0].sh_link;
5391 if (header->e_shstrndx >= header->e_shnum)
5392 header->e_shstrndx = SHN_UNDEF;
5393 }
5394
5395 return true;
5396}
5397
5398/* Read in the program headers from FILEDATA and store them in PHEADERS.
5399 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
5400
5401static bool
5402get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5403{
5404 Elf32_External_Phdr * phdrs;
5405 Elf32_External_Phdr * external;
5406 Elf_Internal_Phdr * internal;
5407 unsigned int i;
5408 unsigned int size = filedata->file_header.e_phentsize;
5409 unsigned int num = filedata->file_header.e_phnum;
5410
5411 /* PR binutils/17531: Cope with unexpected section header sizes. */
5412 if (size == 0 || num == 0)
5413 return false;
5414 if (size < sizeof * phdrs)
5415 {
5416 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5417 return false;
5418 }
5419 if (size > sizeof * phdrs)
5420 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5421
5422 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5423 size, num, _("program headers"));
5424 if (phdrs == NULL)
5425 return false;
5426
5427 for (i = 0, internal = pheaders, external = phdrs;
5428 i < filedata->file_header.e_phnum;
5429 i++, internal++, external++)
5430 {
5431 internal->p_type = BYTE_GET (external->p_type);
5432 internal->p_offset = BYTE_GET (external->p_offset);
5433 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5434 internal->p_paddr = BYTE_GET (external->p_paddr);
5435 internal->p_filesz = BYTE_GET (external->p_filesz);
5436 internal->p_memsz = BYTE_GET (external->p_memsz);
5437 internal->p_flags = BYTE_GET (external->p_flags);
5438 internal->p_align = BYTE_GET (external->p_align);
5439 }
5440
5441 free (phdrs);
5442 return true;
5443}
5444
5445/* Read in the program headers from FILEDATA and store them in PHEADERS.
5446 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
5447
5448static bool
5449get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
5450{
5451 Elf64_External_Phdr * phdrs;
5452 Elf64_External_Phdr * external;
5453 Elf_Internal_Phdr * internal;
5454 unsigned int i;
5455 unsigned int size = filedata->file_header.e_phentsize;
5456 unsigned int num = filedata->file_header.e_phnum;
5457
5458 /* PR binutils/17531: Cope with unexpected section header sizes. */
5459 if (size == 0 || num == 0)
5460 return false;
5461 if (size < sizeof * phdrs)
5462 {
5463 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
5464 return false;
5465 }
5466 if (size > sizeof * phdrs)
5467 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
5468
5469 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
5470 size, num, _("program headers"));
5471 if (!phdrs)
5472 return false;
5473
5474 for (i = 0, internal = pheaders, external = phdrs;
5475 i < filedata->file_header.e_phnum;
5476 i++, internal++, external++)
5477 {
5478 internal->p_type = BYTE_GET (external->p_type);
5479 internal->p_flags = BYTE_GET (external->p_flags);
5480 internal->p_offset = BYTE_GET (external->p_offset);
5481 internal->p_vaddr = BYTE_GET (external->p_vaddr);
5482 internal->p_paddr = BYTE_GET (external->p_paddr);
5483 internal->p_filesz = BYTE_GET (external->p_filesz);
5484 internal->p_memsz = BYTE_GET (external->p_memsz);
5485 internal->p_align = BYTE_GET (external->p_align);
5486 }
5487
5488 free (phdrs);
5489 return true;
5490}
5491
5492/* Returns TRUE if the program headers were read into `program_headers'. */
5493
5494static bool
5495get_program_headers (Filedata * filedata)
5496{
5497 Elf_Internal_Phdr * phdrs;
5498
5499 /* Check cache of prior read. */
5500 if (filedata->program_headers != NULL)
5501 return true;
5502
5503 /* Be kind to memory checkers by looking for
5504 e_phnum values which we know must be invalid. */
5505 if (filedata->file_header.e_phnum
5506 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
5507 >= filedata->file_size)
5508 {
5509 error (_("Too many program headers - %#x - the file is not that big\n"),
5510 filedata->file_header.e_phnum);
5511 return false;
5512 }
5513
5514 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
5515 sizeof (Elf_Internal_Phdr));
5516 if (phdrs == NULL)
5517 {
5518 error (_("Out of memory reading %u program headers\n"),
5519 filedata->file_header.e_phnum);
5520 return false;
5521 }
5522
5523 if (is_32bit_elf
5524 ? get_32bit_program_headers (filedata, phdrs)
5525 : get_64bit_program_headers (filedata, phdrs))
5526 {
5527 filedata->program_headers = phdrs;
5528 return true;
5529 }
5530
5531 free (phdrs);
5532 return false;
5533}
5534
5535/* Print program header info and locate dynamic section. */
5536
5537static void
5538process_program_headers (Filedata * filedata)
5539{
5540 Elf_Internal_Phdr * segment;
5541 unsigned int i;
5542 Elf_Internal_Phdr * previous_load = NULL;
5543
5544 if (filedata->file_header.e_phnum == 0)
5545 {
5546 /* PR binutils/12467. */
5547 if (filedata->file_header.e_phoff != 0)
5548 warn (_("possibly corrupt ELF header - it has a non-zero program"
5549 " header offset, but no program headers\n"));
5550 else if (do_segments)
5551 {
5552 if (filedata->is_separate)
5553 printf (_("\nThere are no program headers in linked file '%s'.\n"),
5554 filedata->file_name);
5555 else
5556 printf (_("\nThere are no program headers in this file.\n"));
5557 }
5558 goto no_headers;
5559 }
5560
5561 if (do_segments && !do_header)
5562 {
5563 if (filedata->is_separate)
5564 printf ("\nIn linked file '%s' the ELF file type is %s\n",
5565 filedata->file_name, get_file_type (filedata));
5566 else
5567 printf (_("\nElf file type is %s\n"), get_file_type (filedata));
5568 printf (_("Entry point 0x%s\n"), bfd_vmatoa ("x", filedata->file_header.e_entry));
5569 printf (ngettext ("There is %d program header, starting at offset %s\n",
5570 "There are %d program headers, starting at offset %s\n",
5571 filedata->file_header.e_phnum),
5572 filedata->file_header.e_phnum,
5573 bfd_vmatoa ("u", filedata->file_header.e_phoff));
5574 }
5575
5576 if (! get_program_headers (filedata))
5577 goto no_headers;
5578
5579 if (do_segments)
5580 {
5581 if (filedata->file_header.e_phnum > 1)
5582 printf (_("\nProgram Headers:\n"));
5583 else
5584 printf (_("\nProgram Headers:\n"));
5585
5586 if (is_32bit_elf)
5587 printf
5588 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
5589 else if (do_wide)
5590 printf
5591 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
5592 else
5593 {
5594 printf
5595 (_(" Type Offset VirtAddr PhysAddr\n"));
5596 printf
5597 (_(" FileSiz MemSiz Flags Align\n"));
5598 }
5599 }
5600
5601 unsigned long dynamic_addr = 0;
5602 bfd_size_type dynamic_size = 0;
5603 for (i = 0, segment = filedata->program_headers;
5604 i < filedata->file_header.e_phnum;
5605 i++, segment++)
5606 {
5607 if (do_segments)
5608 {
5609 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
5610
5611 if (is_32bit_elf)
5612 {
5613 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5614 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
5615 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
5616 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
5617 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
5618 printf ("%c%c%c ",
5619 (segment->p_flags & PF_R ? 'R' : ' '),
5620 (segment->p_flags & PF_W ? 'W' : ' '),
5621 (segment->p_flags & PF_X ? 'E' : ' '));
5622 printf ("%#lx", (unsigned long) segment->p_align);
5623 }
5624 else if (do_wide)
5625 {
5626 if ((unsigned long) segment->p_offset == segment->p_offset)
5627 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
5628 else
5629 {
5630 print_vma (segment->p_offset, FULL_HEX);
5631 putchar (' ');
5632 }
5633
5634 print_vma (segment->p_vaddr, FULL_HEX);
5635 putchar (' ');
5636 print_vma (segment->p_paddr, FULL_HEX);
5637 putchar (' ');
5638
5639 if ((unsigned long) segment->p_filesz == segment->p_filesz)
5640 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
5641 else
5642 {
5643 print_vma (segment->p_filesz, FULL_HEX);
5644 putchar (' ');
5645 }
5646
5647 if ((unsigned long) segment->p_memsz == segment->p_memsz)
5648 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
5649 else
5650 {
5651 print_vma (segment->p_memsz, FULL_HEX);
5652 }
5653
5654 printf (" %c%c%c ",
5655 (segment->p_flags & PF_R ? 'R' : ' '),
5656 (segment->p_flags & PF_W ? 'W' : ' '),
5657 (segment->p_flags & PF_X ? 'E' : ' '));
5658
5659 if ((unsigned long) segment->p_align == segment->p_align)
5660 printf ("%#lx", (unsigned long) segment->p_align);
5661 else
5662 {
5663 print_vma (segment->p_align, PREFIX_HEX);
5664 }
5665 }
5666 else
5667 {
5668 print_vma (segment->p_offset, FULL_HEX);
5669 putchar (' ');
5670 print_vma (segment->p_vaddr, FULL_HEX);
5671 putchar (' ');
5672 print_vma (segment->p_paddr, FULL_HEX);
5673 printf ("\n ");
5674 print_vma (segment->p_filesz, FULL_HEX);
5675 putchar (' ');
5676 print_vma (segment->p_memsz, FULL_HEX);
5677 printf (" %c%c%c ",
5678 (segment->p_flags & PF_R ? 'R' : ' '),
5679 (segment->p_flags & PF_W ? 'W' : ' '),
5680 (segment->p_flags & PF_X ? 'E' : ' '));
5681 print_vma (segment->p_align, PREFIX_HEX);
5682 }
5683
5684 putc ('\n', stdout);
5685 }
5686
5687 switch (segment->p_type)
5688 {
5689 case PT_LOAD:
5690#if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
5691 required by the ELF standard, several programs, including the Linux
5692 kernel, make use of non-ordered segments. */
5693 if (previous_load
5694 && previous_load->p_vaddr > segment->p_vaddr)
5695 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
5696#endif
5697 if (segment->p_memsz < segment->p_filesz)
5698 error (_("the segment's file size is larger than its memory size\n"));
5699 previous_load = segment;
5700 break;
5701
5702 case PT_PHDR:
5703 /* PR 20815 - Verify that the program header is loaded into memory. */
5704 if (i > 0 && previous_load != NULL)
5705 error (_("the PHDR segment must occur before any LOAD segment\n"));
5706 if (filedata->file_header.e_machine != EM_PARISC)
5707 {
5708 unsigned int j;
5709
5710 for (j = 1; j < filedata->file_header.e_phnum; j++)
5711 {
5712 Elf_Internal_Phdr *load = filedata->program_headers + j;
5713 if (load->p_type == PT_LOAD
5714 && load->p_offset <= segment->p_offset
5715 && (load->p_offset + load->p_filesz
5716 >= segment->p_offset + segment->p_filesz)
5717 && load->p_vaddr <= segment->p_vaddr
5718 && (load->p_vaddr + load->p_filesz
5719 >= segment->p_vaddr + segment->p_filesz))
5720 break;
5721 }
5722 if (j == filedata->file_header.e_phnum)
5723 error (_("the PHDR segment is not covered by a LOAD segment\n"));
5724 }
5725 break;
5726
5727 case PT_DYNAMIC:
5728 if (dynamic_addr)
5729 error (_("more than one dynamic segment\n"));
5730
5731 /* By default, assume that the .dynamic section is the first
5732 section in the DYNAMIC segment. */
5733 dynamic_addr = segment->p_offset;
5734 dynamic_size = segment->p_filesz;
5735
5736 /* Try to locate the .dynamic section. If there is
5737 a section header table, we can easily locate it. */
5738 if (filedata->section_headers != NULL)
5739 {
5740 Elf_Internal_Shdr * sec;
5741
5742 sec = find_section (filedata, ".dynamic");
5743 if (sec == NULL || sec->sh_size == 0)
5744 {
5745 /* A corresponding .dynamic section is expected, but on
5746 IA-64/OpenVMS it is OK for it to be missing. */
5747 if (!is_ia64_vms (filedata))
5748 error (_("no .dynamic section in the dynamic segment\n"));
5749 break;
5750 }
5751
5752 if (sec->sh_type == SHT_NOBITS)
5753 {
5754 dynamic_addr = 0;
5755 dynamic_size = 0;
5756 break;
5757 }
5758
5759 dynamic_addr = sec->sh_offset;
5760 dynamic_size = sec->sh_size;
5761
5762 /* The PT_DYNAMIC segment, which is used by the run-time
5763 loader, should exactly match the .dynamic section. */
5764 if (do_checks
5765 && (dynamic_addr != segment->p_offset
5766 || dynamic_size != segment->p_filesz))
5767 warn (_("\
5768the .dynamic section is not the same as the dynamic segment\n"));
5769 }
5770
5771 /* PR binutils/17512: Avoid corrupt dynamic section info in the
5772 segment. Check this after matching against the section headers
5773 so we don't warn on debuginfo file (which have NOBITS .dynamic
5774 sections). */
5775 if (dynamic_addr > filedata->file_size
5776 || (dynamic_size > filedata->file_size - dynamic_addr))
5777 {
5778 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
5779 dynamic_addr = 0;
5780 dynamic_size = 0;
5781 }
5782 break;
5783
5784 case PT_INTERP:
5785 if (segment->p_offset >= filedata->file_size
5786 || segment->p_filesz > filedata->file_size - segment->p_offset
5787 || segment->p_filesz - 1 >= (size_t) -2
5788 || fseek (filedata->handle,
5789 filedata->archive_file_offset + (long) segment->p_offset,
5790 SEEK_SET))
5791 error (_("Unable to find program interpreter name\n"));
5792 else
5793 {
5794 size_t len = segment->p_filesz;
5795 free (filedata->program_interpreter);
5796 filedata->program_interpreter = xmalloc (len + 1);
5797 len = fread (filedata->program_interpreter, 1, len,
5798 filedata->handle);
5799 filedata->program_interpreter[len] = 0;
5800
5801 if (do_segments)
5802 printf (_(" [Requesting program interpreter: %s]\n"),
5803 filedata->program_interpreter);
5804 }
5805 break;
5806 }
5807 }
5808
5809 if (do_segments
5810 && filedata->section_headers != NULL
5811 && filedata->string_table != NULL)
5812 {
5813 printf (_("\n Section to Segment mapping:\n"));
5814 printf (_(" Segment Sections...\n"));
5815
5816 for (i = 0; i < filedata->file_header.e_phnum; i++)
5817 {
5818 unsigned int j;
5819 Elf_Internal_Shdr * section;
5820
5821 segment = filedata->program_headers + i;
5822 section = filedata->section_headers + 1;
5823
5824 printf (" %2.2d ", i);
5825
5826 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
5827 {
5828 if (!ELF_TBSS_SPECIAL (section, segment)
5829 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
5830 printf ("%s ", printable_section_name (filedata, section));
5831 }
5832
5833 putc ('\n',stdout);
5834 }
5835 }
5836
5837 filedata->dynamic_addr = dynamic_addr;
5838 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
5839 return;
5840
5841 no_headers:
5842 filedata->dynamic_addr = 0;
5843 filedata->dynamic_size = 1;
5844}
5845
5846
5847/* Find the file offset corresponding to VMA by using the program headers. */
5848
5849static long
5850offset_from_vma (Filedata * filedata, bfd_vma vma, bfd_size_type size)
5851{
5852 Elf_Internal_Phdr * seg;
5853
5854 if (! get_program_headers (filedata))
5855 {
5856 warn (_("Cannot interpret virtual addresses without program headers.\n"));
5857 return (long) vma;
5858 }
5859
5860 for (seg = filedata->program_headers;
5861 seg < filedata->program_headers + filedata->file_header.e_phnum;
5862 ++seg)
5863 {
5864 if (seg->p_type != PT_LOAD)
5865 continue;
5866
5867 if (vma >= (seg->p_vaddr & -seg->p_align)
5868 && vma + size <= seg->p_vaddr + seg->p_filesz)
5869 return vma - seg->p_vaddr + seg->p_offset;
5870 }
5871
5872 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
5873 (unsigned long) vma);
5874 return (long) vma;
5875}
5876
5877
5878/* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
5879 If PROBE is true, this is just a probe and we do not generate any error
5880 messages if the load fails. */
5881
5882static bool
5883get_32bit_section_headers (Filedata * filedata, bool probe)
5884{
5885 Elf32_External_Shdr * shdrs;
5886 Elf_Internal_Shdr * internal;
5887 unsigned int i;
5888 unsigned int size = filedata->file_header.e_shentsize;
5889 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
5890
5891 /* PR binutils/17531: Cope with unexpected section header sizes. */
5892 if (size == 0 || num == 0)
5893 return false;
5894 if (size < sizeof * shdrs)
5895 {
5896 if (! probe)
5897 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5898 return false;
5899 }
5900 if (!probe && size > sizeof * shdrs)
5901 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5902
5903 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
5904 size, num,
5905 probe ? NULL : _("section headers"));
5906 if (shdrs == NULL)
5907 return false;
5908
5909 filedata->section_headers = (Elf_Internal_Shdr *)
5910 cmalloc (num, sizeof (Elf_Internal_Shdr));
5911 if (filedata->section_headers == NULL)
5912 {
5913 if (!probe)
5914 error (_("Out of memory reading %u section headers\n"), num);
5915 free (shdrs);
5916 return false;
5917 }
5918
5919 for (i = 0, internal = filedata->section_headers;
5920 i < num;
5921 i++, internal++)
5922 {
5923 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
5924 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
5925 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
5926 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
5927 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
5928 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
5929 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
5930 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
5931 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5932 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
5933 if (!probe && internal->sh_link > num)
5934 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
5935 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
5936 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
5937 }
5938
5939 free (shdrs);
5940 return true;
5941}
5942
5943/* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
5944
5945static bool
5946get_64bit_section_headers (Filedata * filedata, bool probe)
5947{
5948 Elf64_External_Shdr * shdrs;
5949 Elf_Internal_Shdr * internal;
5950 unsigned int i;
5951 unsigned int size = filedata->file_header.e_shentsize;
5952 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
5953
5954 /* PR binutils/17531: Cope with unexpected section header sizes. */
5955 if (size == 0 || num == 0)
5956 return false;
5957
5958 if (size < sizeof * shdrs)
5959 {
5960 if (! probe)
5961 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
5962 return false;
5963 }
5964
5965 if (! probe && size > sizeof * shdrs)
5966 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
5967
5968 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
5969 filedata->file_header.e_shoff,
5970 size, num,
5971 probe ? NULL : _("section headers"));
5972 if (shdrs == NULL)
5973 return false;
5974
5975 filedata->section_headers = (Elf_Internal_Shdr *)
5976 cmalloc (num, sizeof (Elf_Internal_Shdr));
5977 if (filedata->section_headers == NULL)
5978 {
5979 if (! probe)
5980 error (_("Out of memory reading %u section headers\n"), num);
5981 free (shdrs);
5982 return false;
5983 }
5984
5985 for (i = 0, internal = filedata->section_headers;
5986 i < num;
5987 i++, internal++)
5988 {
5989 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
5990 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
5991 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
5992 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
5993 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
5994 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
5995 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
5996 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
5997 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
5998 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
5999 if (!probe && internal->sh_link > num)
6000 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6001 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6002 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6003 }
6004
6005 free (shdrs);
6006 return true;
6007}
6008
6009static bool
6010get_section_headers (Filedata *filedata, bool probe)
6011{
6012 if (filedata->section_headers != NULL)
6013 return true;
6014
6015 if (is_32bit_elf)
6016 return get_32bit_section_headers (filedata, probe);
6017 else
6018 return get_64bit_section_headers (filedata, probe);
6019}
6020
6021static Elf_Internal_Sym *
6022get_32bit_elf_symbols (Filedata * filedata,
6023 Elf_Internal_Shdr * section,
6024 unsigned long * num_syms_return)
6025{
6026 unsigned long number = 0;
6027 Elf32_External_Sym * esyms = NULL;
6028 Elf_External_Sym_Shndx * shndx = NULL;
6029 Elf_Internal_Sym * isyms = NULL;
6030 Elf_Internal_Sym * psym;
6031 unsigned int j;
6032 elf_section_list * entry;
6033
6034 if (section->sh_size == 0)
6035 {
6036 if (num_syms_return != NULL)
6037 * num_syms_return = 0;
6038 return NULL;
6039 }
6040
6041 /* Run some sanity checks first. */
6042 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6043 {
6044 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6045 printable_section_name (filedata, section),
6046 (unsigned long) section->sh_entsize);
6047 goto exit_point;
6048 }
6049
6050 if (section->sh_size > filedata->file_size)
6051 {
6052 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6053 printable_section_name (filedata, section),
6054 (unsigned long) section->sh_size);
6055 goto exit_point;
6056 }
6057
6058 number = section->sh_size / section->sh_entsize;
6059
6060 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
6061 {
6062 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6063 (unsigned long) section->sh_size,
6064 printable_section_name (filedata, section),
6065 (unsigned long) section->sh_entsize);
6066 goto exit_point;
6067 }
6068
6069 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6070 section->sh_size, _("symbols"));
6071 if (esyms == NULL)
6072 goto exit_point;
6073
6074 shndx = NULL;
6075 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6076 {
6077 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6078 continue;
6079
6080 if (shndx != NULL)
6081 {
6082 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6083 free (shndx);
6084 }
6085
6086 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6087 entry->hdr->sh_offset,
6088 1, entry->hdr->sh_size,
6089 _("symbol table section indices"));
6090 if (shndx == NULL)
6091 goto exit_point;
6092
6093 /* PR17531: file: heap-buffer-overflow */
6094 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6095 {
6096 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6097 printable_section_name (filedata, entry->hdr),
6098 (unsigned long) entry->hdr->sh_size,
6099 (unsigned long) section->sh_size);
6100 goto exit_point;
6101 }
6102 }
6103
6104 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6105
6106 if (isyms == NULL)
6107 {
6108 error (_("Out of memory reading %lu symbols\n"),
6109 (unsigned long) number);
6110 goto exit_point;
6111 }
6112
6113 for (j = 0, psym = isyms; j < number; j++, psym++)
6114 {
6115 psym->st_name = BYTE_GET (esyms[j].st_name);
6116 psym->st_value = BYTE_GET (esyms[j].st_value);
6117 psym->st_size = BYTE_GET (esyms[j].st_size);
6118 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6119 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6120 psym->st_shndx
6121 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6122 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6123 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6124 psym->st_info = BYTE_GET (esyms[j].st_info);
6125 psym->st_other = BYTE_GET (esyms[j].st_other);
6126 }
6127
6128 exit_point:
6129 free (shndx);
6130 free (esyms);
6131
6132 if (num_syms_return != NULL)
6133 * num_syms_return = isyms == NULL ? 0 : number;
6134
6135 return isyms;
6136}
6137
6138static Elf_Internal_Sym *
6139get_64bit_elf_symbols (Filedata * filedata,
6140 Elf_Internal_Shdr * section,
6141 unsigned long * num_syms_return)
6142{
6143 unsigned long number = 0;
6144 Elf64_External_Sym * esyms = NULL;
6145 Elf_External_Sym_Shndx * shndx = NULL;
6146 Elf_Internal_Sym * isyms = NULL;
6147 Elf_Internal_Sym * psym;
6148 unsigned int j;
6149 elf_section_list * entry;
6150
6151 if (section->sh_size == 0)
6152 {
6153 if (num_syms_return != NULL)
6154 * num_syms_return = 0;
6155 return NULL;
6156 }
6157
6158 /* Run some sanity checks first. */
6159 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
6160 {
6161 error (_("Section %s has an invalid sh_entsize of 0x%lx\n"),
6162 printable_section_name (filedata, section),
6163 (unsigned long) section->sh_entsize);
6164 goto exit_point;
6165 }
6166
6167 if (section->sh_size > filedata->file_size)
6168 {
6169 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
6170 printable_section_name (filedata, section),
6171 (unsigned long) section->sh_size);
6172 goto exit_point;
6173 }
6174
6175 number = section->sh_size / section->sh_entsize;
6176
6177 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
6178 {
6179 error (_("Size (0x%lx) of section %s is not a multiple of its sh_entsize (0x%lx)\n"),
6180 (unsigned long) section->sh_size,
6181 printable_section_name (filedata, section),
6182 (unsigned long) section->sh_entsize);
6183 goto exit_point;
6184 }
6185
6186 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
6187 section->sh_size, _("symbols"));
6188 if (!esyms)
6189 goto exit_point;
6190
6191 shndx = NULL;
6192 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
6193 {
6194 if (entry->hdr->sh_link != (unsigned long) (section - filedata->section_headers))
6195 continue;
6196
6197 if (shndx != NULL)
6198 {
6199 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
6200 free (shndx);
6201 }
6202
6203 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
6204 entry->hdr->sh_offset,
6205 1, entry->hdr->sh_size,
6206 _("symbol table section indices"));
6207 if (shndx == NULL)
6208 goto exit_point;
6209
6210 /* PR17531: file: heap-buffer-overflow */
6211 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
6212 {
6213 error (_("Index section %s has an sh_size of 0x%lx - expected 0x%lx\n"),
6214 printable_section_name (filedata, entry->hdr),
6215 (unsigned long) entry->hdr->sh_size,
6216 (unsigned long) section->sh_size);
6217 goto exit_point;
6218 }
6219 }
6220
6221 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
6222
6223 if (isyms == NULL)
6224 {
6225 error (_("Out of memory reading %lu symbols\n"),
6226 (unsigned long) number);
6227 goto exit_point;
6228 }
6229
6230 for (j = 0, psym = isyms; j < number; j++, psym++)
6231 {
6232 psym->st_name = BYTE_GET (esyms[j].st_name);
6233 psym->st_info = BYTE_GET (esyms[j].st_info);
6234 psym->st_other = BYTE_GET (esyms[j].st_other);
6235 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
6236
6237 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
6238 psym->st_shndx
6239 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
6240 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
6241 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
6242
6243 psym->st_value = BYTE_GET (esyms[j].st_value);
6244 psym->st_size = BYTE_GET (esyms[j].st_size);
6245 }
6246
6247 exit_point:
6248 free (shndx);
6249 free (esyms);
6250
6251 if (num_syms_return != NULL)
6252 * num_syms_return = isyms == NULL ? 0 : number;
6253
6254 return isyms;
6255}
6256
6257static Elf_Internal_Sym *
6258get_elf_symbols (Filedata *filedata,
6259 Elf_Internal_Shdr *section,
6260 unsigned long *num_syms_return)
6261{
6262 if (is_32bit_elf)
6263 return get_32bit_elf_symbols (filedata, section, num_syms_return);
6264 else
6265 return get_64bit_elf_symbols (filedata, section, num_syms_return);
6266}
6267
6268static const char *
6269get_elf_section_flags (Filedata * filedata, bfd_vma sh_flags)
6270{
6271 static char buff[1024];
6272 char * p = buff;
6273 unsigned int field_size = is_32bit_elf ? 8 : 16;
6274 signed int sindex;
6275 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
6276 bfd_vma os_flags = 0;
6277 bfd_vma proc_flags = 0;
6278 bfd_vma unknown_flags = 0;
6279 static const struct
6280 {
6281 const char * str;
6282 unsigned int len;
6283 }
6284 flags [] =
6285 {
6286 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
6287 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
6288 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
6289 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
6290 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
6291 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
6292 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
6293 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
6294 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
6295 /* 9 */ { STRING_COMMA_LEN ("TLS") },
6296 /* IA-64 specific. */
6297 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
6298 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
6299 /* IA-64 OpenVMS specific. */
6300 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
6301 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
6302 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
6303 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
6304 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
6305 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
6306 /* Generic. */
6307 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
6308 /* SPARC specific. */
6309 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
6310 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
6311 /* ARM specific. */
6312 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
6313 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
6314 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
6315 /* GNU specific. */
6316 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
6317 /* VLE specific. */
6318 /* 25 */ { STRING_COMMA_LEN ("VLE") },
6319 /* GNU specific. */
6320 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
6321 };
6322
6323 if (do_section_details)
6324 {
6325 sprintf (buff, "[%*.*lx]: ",
6326 field_size, field_size, (unsigned long) sh_flags);
6327 p += field_size + 4;
6328 }
6329
6330 while (sh_flags)
6331 {
6332 bfd_vma flag;
6333
6334 flag = sh_flags & - sh_flags;
6335 sh_flags &= ~ flag;
6336
6337 if (do_section_details)
6338 {
6339 switch (flag)
6340 {
6341 case SHF_WRITE: sindex = 0; break;
6342 case SHF_ALLOC: sindex = 1; break;
6343 case SHF_EXECINSTR: sindex = 2; break;
6344 case SHF_MERGE: sindex = 3; break;
6345 case SHF_STRINGS: sindex = 4; break;
6346 case SHF_INFO_LINK: sindex = 5; break;
6347 case SHF_LINK_ORDER: sindex = 6; break;
6348 case SHF_OS_NONCONFORMING: sindex = 7; break;
6349 case SHF_GROUP: sindex = 8; break;
6350 case SHF_TLS: sindex = 9; break;
6351 case SHF_EXCLUDE: sindex = 18; break;
6352 case SHF_COMPRESSED: sindex = 20; break;
6353
6354 default:
6355 sindex = -1;
6356 switch (filedata->file_header.e_machine)
6357 {
6358 case EM_IA_64:
6359 if (flag == SHF_IA_64_SHORT)
6360 sindex = 10;
6361 else if (flag == SHF_IA_64_NORECOV)
6362 sindex = 11;
6363#ifdef BFD64
6364 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
6365 switch (flag)
6366 {
6367 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
6368 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
6369 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
6370 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
6371 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
6372 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
6373 default: break;
6374 }
6375#endif
6376 break;
6377
6378 case EM_386:
6379 case EM_IAMCU:
6380 case EM_X86_64:
6381 case EM_L1OM:
6382 case EM_K1OM:
6383 case EM_OLD_SPARCV9:
6384 case EM_SPARC32PLUS:
6385 case EM_SPARCV9:
6386 case EM_SPARC:
6387 if (flag == SHF_ORDERED)
6388 sindex = 19;
6389 break;
6390
6391 case EM_ARM:
6392 switch (flag)
6393 {
6394 case SHF_ENTRYSECT: sindex = 21; break;
6395 case SHF_ARM_PURECODE: sindex = 22; break;
6396 case SHF_COMDEF: sindex = 23; break;
6397 default: break;
6398 }
6399 break;
6400 case EM_PPC:
6401 if (flag == SHF_PPC_VLE)
6402 sindex = 25;
6403 break;
6404 default:
6405 break;
6406 }
6407
6408 switch (filedata->file_header.e_ident[EI_OSABI])
6409 {
6410 case ELFOSABI_GNU:
6411 case ELFOSABI_FREEBSD:
6412 if (flag == SHF_GNU_RETAIN)
6413 sindex = 26;
6414 /* Fall through */
6415 case ELFOSABI_NONE:
6416 if (flag == SHF_GNU_MBIND)
6417 /* We should not recognize SHF_GNU_MBIND for
6418 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6419 not set the EI_OSABI header byte. */
6420 sindex = 24;
6421 break;
6422 default:
6423 break;
6424 }
6425 break;
6426 }
6427
6428 if (sindex != -1)
6429 {
6430 if (p != buff + field_size + 4)
6431 {
6432 if (size < (10 + 2))
6433 {
6434 warn (_("Internal error: not enough buffer room for section flag info"));
6435 return _("<unknown>");
6436 }
6437 size -= 2;
6438 *p++ = ',';
6439 *p++ = ' ';
6440 }
6441
6442 size -= flags [sindex].len;
6443 p = stpcpy (p, flags [sindex].str);
6444 }
6445 else if (flag & SHF_MASKOS)
6446 os_flags |= flag;
6447 else if (flag & SHF_MASKPROC)
6448 proc_flags |= flag;
6449 else
6450 unknown_flags |= flag;
6451 }
6452 else
6453 {
6454 switch (flag)
6455 {
6456 case SHF_WRITE: *p = 'W'; break;
6457 case SHF_ALLOC: *p = 'A'; break;
6458 case SHF_EXECINSTR: *p = 'X'; break;
6459 case SHF_MERGE: *p = 'M'; break;
6460 case SHF_STRINGS: *p = 'S'; break;
6461 case SHF_INFO_LINK: *p = 'I'; break;
6462 case SHF_LINK_ORDER: *p = 'L'; break;
6463 case SHF_OS_NONCONFORMING: *p = 'O'; break;
6464 case SHF_GROUP: *p = 'G'; break;
6465 case SHF_TLS: *p = 'T'; break;
6466 case SHF_EXCLUDE: *p = 'E'; break;
6467 case SHF_COMPRESSED: *p = 'C'; break;
6468
6469 default:
6470 if ((filedata->file_header.e_machine == EM_X86_64
6471 || filedata->file_header.e_machine == EM_L1OM
6472 || filedata->file_header.e_machine == EM_K1OM)
6473 && flag == SHF_X86_64_LARGE)
6474 *p = 'l';
6475 else if (filedata->file_header.e_machine == EM_ARM
6476 && flag == SHF_ARM_PURECODE)
6477 *p = 'y';
6478 else if (filedata->file_header.e_machine == EM_PPC
6479 && flag == SHF_PPC_VLE)
6480 *p = 'v';
6481 else if (flag & SHF_MASKOS)
6482 {
6483 switch (filedata->file_header.e_ident[EI_OSABI])
6484 {
6485 case ELFOSABI_GNU:
6486 case ELFOSABI_FREEBSD:
6487 if (flag == SHF_GNU_RETAIN)
6488 {
6489 *p = 'R';
6490 break;
6491 }
6492 /* Fall through */
6493 case ELFOSABI_NONE:
6494 if (flag == SHF_GNU_MBIND)
6495 {
6496 /* We should not recognize SHF_GNU_MBIND for
6497 ELFOSABI_NONE, but binutils as of 2019-07-23 did
6498 not set the EI_OSABI header byte. */
6499 *p = 'D';
6500 break;
6501 }
6502 /* Fall through */
6503 default:
6504 *p = 'o';
6505 sh_flags &= ~SHF_MASKOS;
6506 break;
6507 }
6508 }
6509 else if (flag & SHF_MASKPROC)
6510 {
6511 *p = 'p';
6512 sh_flags &= ~ SHF_MASKPROC;
6513 }
6514 else
6515 *p = 'x';
6516 break;
6517 }
6518 p++;
6519 }
6520 }
6521
6522 if (do_section_details)
6523 {
6524 if (os_flags)
6525 {
6526 size -= 5 + field_size;
6527 if (p != buff + field_size + 4)
6528 {
6529 if (size < (2 + 1))
6530 {
6531 warn (_("Internal error: not enough buffer room for section flag info"));
6532 return _("<unknown>");
6533 }
6534 size -= 2;
6535 *p++ = ',';
6536 *p++ = ' ';
6537 }
6538 sprintf (p, "OS (%*.*lx)", field_size, field_size,
6539 (unsigned long) os_flags);
6540 p += 5 + field_size;
6541 }
6542 if (proc_flags)
6543 {
6544 size -= 7 + field_size;
6545 if (p != buff + field_size + 4)
6546 {
6547 if (size < (2 + 1))
6548 {
6549 warn (_("Internal error: not enough buffer room for section flag info"));
6550 return _("<unknown>");
6551 }
6552 size -= 2;
6553 *p++ = ',';
6554 *p++ = ' ';
6555 }
6556 sprintf (p, "PROC (%*.*lx)", field_size, field_size,
6557 (unsigned long) proc_flags);
6558 p += 7 + field_size;
6559 }
6560 if (unknown_flags)
6561 {
6562 size -= 10 + field_size;
6563 if (p != buff + field_size + 4)
6564 {
6565 if (size < (2 + 1))
6566 {
6567 warn (_("Internal error: not enough buffer room for section flag info"));
6568 return _("<unknown>");
6569 }
6570 size -= 2;
6571 *p++ = ',';
6572 *p++ = ' ';
6573 }
6574 sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
6575 (unsigned long) unknown_flags);
6576 p += 10 + field_size;
6577 }
6578 }
6579
6580 *p = '\0';
6581 return buff;
6582}
6583
6584static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
6585get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf, bfd_size_type size)
6586{
6587 if (is_32bit_elf)
6588 {
6589 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
6590
6591 if (size < sizeof (* echdr))
6592 {
6593 error (_("Compressed section is too small even for a compression header\n"));
6594 return 0;
6595 }
6596
6597 chdr->ch_type = BYTE_GET (echdr->ch_type);
6598 chdr->ch_size = BYTE_GET (echdr->ch_size);
6599 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6600 return sizeof (*echdr);
6601 }
6602 else
6603 {
6604 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
6605
6606 if (size < sizeof (* echdr))
6607 {
6608 error (_("Compressed section is too small even for a compression header\n"));
6609 return 0;
6610 }
6611
6612 chdr->ch_type = BYTE_GET (echdr->ch_type);
6613 chdr->ch_size = BYTE_GET (echdr->ch_size);
6614 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
6615 return sizeof (*echdr);
6616 }
6617}
6618
6619static bool
6620process_section_headers (Filedata * filedata)
6621{
6622 Elf_Internal_Shdr * section;
6623 unsigned int i;
6624
6625 if (filedata->file_header.e_shnum == 0)
6626 {
6627 /* PR binutils/12467. */
6628 if (filedata->file_header.e_shoff != 0)
6629 {
6630 warn (_("possibly corrupt ELF file header - it has a non-zero"
6631 " section header offset, but no section headers\n"));
6632 return false;
6633 }
6634 else if (do_sections)
6635 printf (_("\nThere are no sections in this file.\n"));
6636
6637 return true;
6638 }
6639
6640 if (do_sections && !do_header)
6641 {
6642 if (filedata->is_separate && process_links)
6643 printf (_("In linked file '%s': "), filedata->file_name);
6644 if (! filedata->is_separate || process_links)
6645 printf (ngettext ("There is %d section header, "
6646 "starting at offset 0x%lx:\n",
6647 "There are %d section headers, "
6648 "starting at offset 0x%lx:\n",
6649 filedata->file_header.e_shnum),
6650 filedata->file_header.e_shnum,
6651 (unsigned long) filedata->file_header.e_shoff);
6652 }
6653
6654 if (!get_section_headers (filedata, false))
6655 return false;
6656
6657 /* Read in the string table, so that we have names to display. */
6658 if (filedata->file_header.e_shstrndx != SHN_UNDEF
6659 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
6660 {
6661 section = filedata->section_headers + filedata->file_header.e_shstrndx;
6662
6663 if (section->sh_size != 0)
6664 {
6665 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
6666 1, section->sh_size,
6667 _("string table"));
6668
6669 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
6670 }
6671 }
6672
6673 /* Scan the sections for the dynamic symbol table
6674 and dynamic string table and debug sections. */
6675 eh_addr_size = is_32bit_elf ? 4 : 8;
6676 switch (filedata->file_header.e_machine)
6677 {
6678 case EM_MIPS:
6679 case EM_MIPS_RS3_LE:
6680 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
6681 FDE addresses. However, the ABI also has a semi-official ILP32
6682 variant for which the normal FDE address size rules apply.
6683
6684 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
6685 section, where XX is the size of longs in bits. Unfortunately,
6686 earlier compilers provided no way of distinguishing ILP32 objects
6687 from LP64 objects, so if there's any doubt, we should assume that
6688 the official LP64 form is being used. */
6689 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
6690 && find_section (filedata, ".gcc_compiled_long32") == NULL)
6691 eh_addr_size = 8;
6692 break;
6693
6694 case EM_H8_300:
6695 case EM_H8_300H:
6696 switch (filedata->file_header.e_flags & EF_H8_MACH)
6697 {
6698 case E_H8_MACH_H8300:
6699 case E_H8_MACH_H8300HN:
6700 case E_H8_MACH_H8300SN:
6701 case E_H8_MACH_H8300SXN:
6702 eh_addr_size = 2;
6703 break;
6704 case E_H8_MACH_H8300H:
6705 case E_H8_MACH_H8300S:
6706 case E_H8_MACH_H8300SX:
6707 eh_addr_size = 4;
6708 break;
6709 }
6710 break;
6711
6712 case EM_M32C_OLD:
6713 case EM_M32C:
6714 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
6715 {
6716 case EF_M32C_CPU_M16C:
6717 eh_addr_size = 2;
6718 break;
6719 }
6720 break;
6721 }
6722
6723#define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
6724 do \
6725 { \
6726 bfd_size_type expected_entsize = is_32bit_elf ? size32 : size64; \
6727 if (section->sh_entsize != expected_entsize) \
6728 { \
6729 char buf[40]; \
6730 sprintf_vma (buf, section->sh_entsize); \
6731 /* Note: coded this way so that there is a single string for \
6732 translation. */ \
6733 error (_("Section %d has invalid sh_entsize of %s\n"), i, buf); \
6734 error (_("(Using the expected size of %u for the rest of this dump)\n"), \
6735 (unsigned) expected_entsize); \
6736 section->sh_entsize = expected_entsize; \
6737 } \
6738 } \
6739 while (0)
6740
6741#define CHECK_ENTSIZE(section, i, type) \
6742 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
6743 sizeof (Elf64_External_##type))
6744
6745 for (i = 0, section = filedata->section_headers;
6746 i < filedata->file_header.e_shnum;
6747 i++, section++)
6748 {
6749 const char *name = section_name_print (filedata, section);
6750
6751 /* Run some sanity checks on the headers and
6752 possibly fill in some file data as well. */
6753 switch (section->sh_type)
6754 {
6755 case SHT_DYNSYM:
6756 if (filedata->dynamic_symbols != NULL)
6757 {
6758 error (_("File contains multiple dynamic symbol tables\n"));
6759 continue;
6760 }
6761
6762 CHECK_ENTSIZE (section, i, Sym);
6763 filedata->dynamic_symbols
6764 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
6765 filedata->dynamic_symtab_section = section;
6766 break;
6767
6768 case SHT_STRTAB:
6769 if (streq (name, ".dynstr"))
6770 {
6771 if (filedata->dynamic_strings != NULL)
6772 {
6773 error (_("File contains multiple dynamic string tables\n"));
6774 continue;
6775 }
6776
6777 filedata->dynamic_strings
6778 = (char *) get_data (NULL, filedata, section->sh_offset,
6779 1, section->sh_size, _("dynamic strings"));
6780 filedata->dynamic_strings_length
6781 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
6782 filedata->dynamic_strtab_section = section;
6783 }
6784 break;
6785
6786 case SHT_SYMTAB_SHNDX:
6787 {
6788 elf_section_list * entry = xmalloc (sizeof * entry);
6789
6790 entry->hdr = section;
6791 entry->next = filedata->symtab_shndx_list;
6792 filedata->symtab_shndx_list = entry;
6793 }
6794 break;
6795
6796 case SHT_SYMTAB:
6797 CHECK_ENTSIZE (section, i, Sym);
6798 break;
6799
6800 case SHT_GROUP:
6801 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
6802 break;
6803
6804 case SHT_REL:
6805 CHECK_ENTSIZE (section, i, Rel);
6806 if (do_checks && section->sh_size == 0)
6807 warn (_("Section '%s': zero-sized relocation section\n"), name);
6808 break;
6809
6810 case SHT_RELA:
6811 CHECK_ENTSIZE (section, i, Rela);
6812 if (do_checks && section->sh_size == 0)
6813 warn (_("Section '%s': zero-sized relocation section\n"), name);
6814 break;
6815
6816 case SHT_NOTE:
6817 case SHT_PROGBITS:
6818 /* Having a zero sized section is not illegal according to the
6819 ELF standard, but it might be an indication that something
6820 is wrong. So issue a warning if we are running in lint mode. */
6821 if (do_checks && section->sh_size == 0)
6822 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
6823 break;
6824
6825 default:
6826 break;
6827 }
6828
6829 if ((do_debugging || do_debug_info || do_debug_abbrevs
6830 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
6831 || do_debug_aranges || do_debug_frames || do_debug_macinfo
6832 || do_debug_str || do_debug_str_offsets || do_debug_loc
6833 || do_debug_ranges
6834 || do_debug_addr || do_debug_cu_index || do_debug_links)
6835 && (startswith (name, ".debug_")
6836 || startswith (name, ".zdebug_")))
6837 {
6838 if (name[1] == 'z')
6839 name += sizeof (".zdebug_") - 1;
6840 else
6841 name += sizeof (".debug_") - 1;
6842
6843 if (do_debugging
6844 || (do_debug_info && startswith (name, "info"))
6845 || (do_debug_info && startswith (name, "types"))
6846 || (do_debug_abbrevs && startswith (name, "abbrev"))
6847 || (do_debug_lines && strcmp (name, "line") == 0)
6848 || (do_debug_lines && startswith (name, "line."))
6849 || (do_debug_pubnames && startswith (name, "pubnames"))
6850 || (do_debug_pubtypes && startswith (name, "pubtypes"))
6851 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
6852 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
6853 || (do_debug_aranges && startswith (name, "aranges"))
6854 || (do_debug_ranges && startswith (name, "ranges"))
6855 || (do_debug_ranges && startswith (name, "rnglists"))
6856 || (do_debug_frames && startswith (name, "frame"))
6857 || (do_debug_macinfo && startswith (name, "macinfo"))
6858 || (do_debug_macinfo && startswith (name, "macro"))
6859 || (do_debug_str && startswith (name, "str"))
6860 || (do_debug_links && startswith (name, "sup"))
6861 || (do_debug_str_offsets && startswith (name, "str_offsets"))
6862 || (do_debug_loc && startswith (name, "loc"))
6863 || (do_debug_loc && startswith (name, "loclists"))
6864 || (do_debug_addr && startswith (name, "addr"))
6865 || (do_debug_cu_index && startswith (name, "cu_index"))
6866 || (do_debug_cu_index && startswith (name, "tu_index"))
6867 )
6868 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6869 }
6870 /* Linkonce section to be combined with .debug_info at link time. */
6871 else if ((do_debugging || do_debug_info)
6872 && startswith (name, ".gnu.linkonce.wi."))
6873 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6874 else if (do_debug_frames && streq (name, ".eh_frame"))
6875 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6876 else if (do_gdb_index && (streq (name, ".gdb_index")
6877 || streq (name, ".debug_names")))
6878 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6879 /* Trace sections for Itanium VMS. */
6880 else if ((do_debugging || do_trace_info || do_trace_abbrevs
6881 || do_trace_aranges)
6882 && startswith (name, ".trace_"))
6883 {
6884 name += sizeof (".trace_") - 1;
6885
6886 if (do_debugging
6887 || (do_trace_info && streq (name, "info"))
6888 || (do_trace_abbrevs && streq (name, "abbrev"))
6889 || (do_trace_aranges && streq (name, "aranges"))
6890 )
6891 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6892 }
6893 else if ((do_debugging || do_debug_links)
6894 && (startswith (name, ".gnu_debuglink")
6895 || startswith (name, ".gnu_debugaltlink")))
6896 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
6897 }
6898
6899 if (! do_sections)
6900 return true;
6901
6902 if (filedata->is_separate && ! process_links)
6903 return true;
6904
6905 if (filedata->is_separate)
6906 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
6907 else if (filedata->file_header.e_shnum > 1)
6908 printf (_("\nSection Headers:\n"));
6909 else
6910 printf (_("\nSection Header:\n"));
6911
6912 if (is_32bit_elf)
6913 {
6914 if (do_section_details)
6915 {
6916 printf (_(" [Nr] Name\n"));
6917 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
6918 }
6919 else
6920 printf
6921 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
6922 }
6923 else if (do_wide)
6924 {
6925 if (do_section_details)
6926 {
6927 printf (_(" [Nr] Name\n"));
6928 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
6929 }
6930 else
6931 printf
6932 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
6933 }
6934 else
6935 {
6936 if (do_section_details)
6937 {
6938 printf (_(" [Nr] Name\n"));
6939 printf (_(" Type Address Offset Link\n"));
6940 printf (_(" Size EntSize Info Align\n"));
6941 }
6942 else
6943 {
6944 printf (_(" [Nr] Name Type Address Offset\n"));
6945 printf (_(" Size EntSize Flags Link Info Align\n"));
6946 }
6947 }
6948
6949 if (do_section_details)
6950 printf (_(" Flags\n"));
6951
6952 for (i = 0, section = filedata->section_headers;
6953 i < filedata->file_header.e_shnum;
6954 i++, section++)
6955 {
6956 /* Run some sanity checks on the section header. */
6957
6958 /* Check the sh_link field. */
6959 switch (section->sh_type)
6960 {
6961 case SHT_REL:
6962 case SHT_RELA:
6963 if (section->sh_link == 0
6964 && (filedata->file_header.e_type == ET_EXEC
6965 || filedata->file_header.e_type == ET_DYN))
6966 /* A dynamic relocation section where all entries use a
6967 zero symbol index need not specify a symtab section. */
6968 break;
6969 /* Fall through. */
6970 case SHT_SYMTAB_SHNDX:
6971 case SHT_GROUP:
6972 case SHT_HASH:
6973 case SHT_GNU_HASH:
6974 case SHT_GNU_versym:
6975 if (section->sh_link == 0
6976 || section->sh_link >= filedata->file_header.e_shnum
6977 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
6978 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
6979 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
6980 i, section->sh_link);
6981 break;
6982
6983 case SHT_DYNAMIC:
6984 case SHT_SYMTAB:
6985 case SHT_DYNSYM:
6986 case SHT_GNU_verneed:
6987 case SHT_GNU_verdef:
6988 case SHT_GNU_LIBLIST:
6989 if (section->sh_link == 0
6990 || section->sh_link >= filedata->file_header.e_shnum
6991 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
6992 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
6993 i, section->sh_link);
6994 break;
6995
6996 case SHT_INIT_ARRAY:
6997 case SHT_FINI_ARRAY:
6998 case SHT_PREINIT_ARRAY:
6999 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7000 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7001 i, section->sh_link);
7002 break;
7003
7004 default:
7005 /* FIXME: Add support for target specific section types. */
7006#if 0 /* Currently we do not check other section types as there are too
7007 many special cases. Stab sections for example have a type
7008 of SHT_PROGBITS but an sh_link field that links to the .stabstr
7009 section. */
7010 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
7011 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
7012 i, section->sh_link);
7013#endif
7014 break;
7015 }
7016
7017 /* Check the sh_info field. */
7018 switch (section->sh_type)
7019 {
7020 case SHT_REL:
7021 case SHT_RELA:
7022 if (section->sh_info == 0
7023 && (filedata->file_header.e_type == ET_EXEC
7024 || filedata->file_header.e_type == ET_DYN))
7025 /* Dynamic relocations apply to segments, so they do not
7026 need to specify the section they relocate. */
7027 break;
7028 if (section->sh_info == 0
7029 || section->sh_info >= filedata->file_header.e_shnum
7030 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
7031 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
7032 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
7033 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
7034 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
7035 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
7036 /* FIXME: Are other section types valid ? */
7037 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
7038 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
7039 i, section->sh_info);
7040 break;
7041
7042 case SHT_DYNAMIC:
7043 case SHT_HASH:
7044 case SHT_SYMTAB_SHNDX:
7045 case SHT_INIT_ARRAY:
7046 case SHT_FINI_ARRAY:
7047 case SHT_PREINIT_ARRAY:
7048 if (section->sh_info != 0)
7049 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7050 i, section->sh_info);
7051 break;
7052
7053 case SHT_GROUP:
7054 case SHT_SYMTAB:
7055 case SHT_DYNSYM:
7056 /* A symbol index - we assume that it is valid. */
7057 break;
7058
7059 default:
7060 /* FIXME: Add support for target specific section types. */
7061 if (section->sh_type == SHT_NOBITS)
7062 /* NOBITS section headers with non-zero sh_info fields can be
7063 created when a binary is stripped of everything but its debug
7064 information. The stripped sections have their headers
7065 preserved but their types set to SHT_NOBITS. So do not check
7066 this type of section. */
7067 ;
7068 else if (section->sh_flags & SHF_INFO_LINK)
7069 {
7070 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
7071 warn (_("[%2u]: Expected link to another section in info field"), i);
7072 }
7073 else if (section->sh_type < SHT_LOOS
7074 && (section->sh_flags & SHF_GNU_MBIND) == 0
7075 && section->sh_info != 0)
7076 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
7077 i, section->sh_info);
7078 break;
7079 }
7080
7081 /* Check the sh_size field. */
7082 if (section->sh_size > filedata->file_size
7083 && section->sh_type != SHT_NOBITS
7084 && section->sh_type != SHT_NULL
7085 && section->sh_type < SHT_LOOS)
7086 warn (_("Size of section %u is larger than the entire file!\n"), i);
7087
7088 printf (" [%2u] ", i);
7089 if (do_section_details)
7090 printf ("%s\n ", printable_section_name (filedata, section));
7091 else
7092 print_symbol (-17, section_name_print (filedata, section));
7093
7094 printf (do_wide ? " %-15s " : " %-15.15s ",
7095 get_section_type_name (filedata, section->sh_type));
7096
7097 if (is_32bit_elf)
7098 {
7099 const char * link_too_big = NULL;
7100
7101 print_vma (section->sh_addr, LONG_HEX);
7102
7103 printf ( " %6.6lx %6.6lx %2.2lx",
7104 (unsigned long) section->sh_offset,
7105 (unsigned long) section->sh_size,
7106 (unsigned long) section->sh_entsize);
7107
7108 if (do_section_details)
7109 fputs (" ", stdout);
7110 else
7111 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7112
7113 if (section->sh_link >= filedata->file_header.e_shnum)
7114 {
7115 link_too_big = "";
7116 /* The sh_link value is out of range. Normally this indicates
7117 an error but it can have special values in Solaris binaries. */
7118 switch (filedata->file_header.e_machine)
7119 {
7120 case EM_386:
7121 case EM_IAMCU:
7122 case EM_X86_64:
7123 case EM_L1OM:
7124 case EM_K1OM:
7125 case EM_OLD_SPARCV9:
7126 case EM_SPARC32PLUS:
7127 case EM_SPARCV9:
7128 case EM_SPARC:
7129 if (section->sh_link == (SHN_BEFORE & 0xffff))
7130 link_too_big = "BEFORE";
7131 else if (section->sh_link == (SHN_AFTER & 0xffff))
7132 link_too_big = "AFTER";
7133 break;
7134 default:
7135 break;
7136 }
7137 }
7138
7139 if (do_section_details)
7140 {
7141 if (link_too_big != NULL && * link_too_big)
7142 printf ("<%s> ", link_too_big);
7143 else
7144 printf ("%2u ", section->sh_link);
7145 printf ("%3u %2lu\n", section->sh_info,
7146 (unsigned long) section->sh_addralign);
7147 }
7148 else
7149 printf ("%2u %3u %2lu\n",
7150 section->sh_link,
7151 section->sh_info,
7152 (unsigned long) section->sh_addralign);
7153
7154 if (link_too_big && ! * link_too_big)
7155 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
7156 i, section->sh_link);
7157 }
7158 else if (do_wide)
7159 {
7160 print_vma (section->sh_addr, LONG_HEX);
7161
7162 if ((long) section->sh_offset == section->sh_offset)
7163 printf (" %6.6lx", (unsigned long) section->sh_offset);
7164 else
7165 {
7166 putchar (' ');
7167 print_vma (section->sh_offset, LONG_HEX);
7168 }
7169
7170 if ((unsigned long) section->sh_size == section->sh_size)
7171 printf (" %6.6lx", (unsigned long) section->sh_size);
7172 else
7173 {
7174 putchar (' ');
7175 print_vma (section->sh_size, LONG_HEX);
7176 }
7177
7178 if ((unsigned long) section->sh_entsize == section->sh_entsize)
7179 printf (" %2.2lx", (unsigned long) section->sh_entsize);
7180 else
7181 {
7182 putchar (' ');
7183 print_vma (section->sh_entsize, LONG_HEX);
7184 }
7185
7186 if (do_section_details)
7187 fputs (" ", stdout);
7188 else
7189 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7190
7191 printf ("%2u %3u ", section->sh_link, section->sh_info);
7192
7193 if ((unsigned long) section->sh_addralign == section->sh_addralign)
7194 printf ("%2lu\n", (unsigned long) section->sh_addralign);
7195 else
7196 {
7197 print_vma (section->sh_addralign, DEC);
7198 putchar ('\n');
7199 }
7200 }
7201 else if (do_section_details)
7202 {
7203 putchar (' ');
7204 print_vma (section->sh_addr, LONG_HEX);
7205 if ((long) section->sh_offset == section->sh_offset)
7206 printf (" %16.16lx", (unsigned long) section->sh_offset);
7207 else
7208 {
7209 printf (" ");
7210 print_vma (section->sh_offset, LONG_HEX);
7211 }
7212 printf (" %u\n ", section->sh_link);
7213 print_vma (section->sh_size, LONG_HEX);
7214 putchar (' ');
7215 print_vma (section->sh_entsize, LONG_HEX);
7216
7217 printf (" %-16u %lu\n",
7218 section->sh_info,
7219 (unsigned long) section->sh_addralign);
7220 }
7221 else
7222 {
7223 putchar (' ');
7224 print_vma (section->sh_addr, LONG_HEX);
7225 if ((long) section->sh_offset == section->sh_offset)
7226 printf (" %8.8lx", (unsigned long) section->sh_offset);
7227 else
7228 {
7229 printf (" ");
7230 print_vma (section->sh_offset, LONG_HEX);
7231 }
7232 printf ("\n ");
7233 print_vma (section->sh_size, LONG_HEX);
7234 printf (" ");
7235 print_vma (section->sh_entsize, LONG_HEX);
7236
7237 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
7238
7239 printf (" %2u %3u %lu\n",
7240 section->sh_link,
7241 section->sh_info,
7242 (unsigned long) section->sh_addralign);
7243 }
7244
7245 if (do_section_details)
7246 {
7247 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
7248 if ((section->sh_flags & SHF_COMPRESSED) != 0)
7249 {
7250 /* Minimum section size is 12 bytes for 32-bit compression
7251 header + 12 bytes for compressed data header. */
7252 unsigned char buf[24];
7253
7254 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
7255 if (get_data (&buf, filedata, section->sh_offset, 1,
7256 sizeof (buf), _("compression header")))
7257 {
7258 Elf_Internal_Chdr chdr;
7259
7260 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
7261 printf (_(" [<corrupt>]\n"));
7262 else
7263 {
7264 if (chdr.ch_type == ELFCOMPRESS_ZLIB)
7265 printf (" ZLIB, ");
7266 else
7267 printf (_(" [<unknown>: 0x%x], "),
7268 chdr.ch_type);
7269 print_vma (chdr.ch_size, LONG_HEX);
7270 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
7271 }
7272 }
7273 }
7274 }
7275 }
7276
7277 if (!do_section_details)
7278 {
7279 /* The ordering of the letters shown here matches the ordering of the
7280 corresponding SHF_xxx values, and hence the order in which these
7281 letters will be displayed to the user. */
7282 printf (_("Key to Flags:\n\
7283 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
7284 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
7285 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
7286 switch (filedata->file_header.e_ident[EI_OSABI])
7287 {
7288 case ELFOSABI_GNU:
7289 case ELFOSABI_FREEBSD:
7290 printf (_("R (retain), "));
7291 /* Fall through */
7292 case ELFOSABI_NONE:
7293 printf (_("D (mbind), "));
7294 break;
7295 default:
7296 break;
7297 }
7298 if (filedata->file_header.e_machine == EM_X86_64
7299 || filedata->file_header.e_machine == EM_L1OM
7300 || filedata->file_header.e_machine == EM_K1OM)
7301 printf (_("l (large), "));
7302 else if (filedata->file_header.e_machine == EM_ARM)
7303 printf (_("y (purecode), "));
7304 else if (filedata->file_header.e_machine == EM_PPC)
7305 printf (_("v (VLE), "));
7306 printf ("p (processor specific)\n");
7307 }
7308
7309 return true;
7310}
7311
7312static bool
7313get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
7314 Elf_Internal_Sym **symtab, unsigned long *nsyms,
7315 char **strtab, unsigned long *strtablen)
7316{
7317 *strtab = NULL;
7318 *strtablen = 0;
7319 *symtab = get_elf_symbols (filedata, symsec, nsyms);
7320
7321 if (*symtab == NULL)
7322 return false;
7323
7324 if (symsec->sh_link != 0)
7325 {
7326 Elf_Internal_Shdr *strsec;
7327
7328 if (symsec->sh_link >= filedata->file_header.e_shnum)
7329 {
7330 error (_("Bad sh_link in symbol table section\n"));
7331 free (*symtab);
7332 *symtab = NULL;
7333 *nsyms = 0;
7334 return false;
7335 }
7336
7337 strsec = filedata->section_headers + symsec->sh_link;
7338
7339 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
7340 1, strsec->sh_size, _("string table"));
7341 if (*strtab == NULL)
7342 {
7343 free (*symtab);
7344 *symtab = NULL;
7345 *nsyms = 0;
7346 return false;
7347 }
7348 *strtablen = strsec->sh_size;
7349 }
7350 return true;
7351}
7352
7353static const char *
7354get_group_flags (unsigned int flags)
7355{
7356 static char buff[128];
7357
7358 if (flags == 0)
7359 return "";
7360 else if (flags == GRP_COMDAT)
7361 return "COMDAT ";
7362
7363 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
7364 flags,
7365 flags & GRP_MASKOS ? _("<OS specific>") : "",
7366 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
7367 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
7368 ? _("<unknown>") : ""));
7369
7370 return buff;
7371}
7372
7373static bool
7374process_section_groups (Filedata * filedata)
7375{
7376 Elf_Internal_Shdr * section;
7377 unsigned int i;
7378 struct group * group;
7379 Elf_Internal_Shdr * symtab_sec;
7380 Elf_Internal_Shdr * strtab_sec;
7381 Elf_Internal_Sym * symtab;
7382 unsigned long num_syms;
7383 char * strtab;
7384 size_t strtab_size;
7385
7386 /* Don't process section groups unless needed. */
7387 if (!do_unwind && !do_section_groups)
7388 return true;
7389
7390 if (filedata->file_header.e_shnum == 0)
7391 {
7392 if (do_section_groups)
7393 {
7394 if (filedata->is_separate)
7395 printf (_("\nThere are no sections group in linked file '%s'.\n"),
7396 filedata->file_name);
7397 else
7398 printf (_("\nThere are no section groups in this file.\n"));
7399 }
7400 return true;
7401 }
7402
7403 if (filedata->section_headers == NULL)
7404 {
7405 error (_("Section headers are not available!\n"));
7406 /* PR 13622: This can happen with a corrupt ELF header. */
7407 return false;
7408 }
7409
7410 filedata->section_headers_groups
7411 = (struct group **) calloc (filedata->file_header.e_shnum,
7412 sizeof (struct group *));
7413
7414 if (filedata->section_headers_groups == NULL)
7415 {
7416 error (_("Out of memory reading %u section group headers\n"),
7417 filedata->file_header.e_shnum);
7418 return false;
7419 }
7420
7421 /* Scan the sections for the group section. */
7422 filedata->group_count = 0;
7423 for (i = 0, section = filedata->section_headers;
7424 i < filedata->file_header.e_shnum;
7425 i++, section++)
7426 if (section->sh_type == SHT_GROUP)
7427 filedata->group_count++;
7428
7429 if (filedata->group_count == 0)
7430 {
7431 if (do_section_groups)
7432 {
7433 if (filedata->is_separate)
7434 printf (_("\nThere are no section groups in linked file '%s'.\n"),
7435 filedata->file_name);
7436 else
7437 printf (_("\nThere are no section groups in this file.\n"));
7438 }
7439
7440 return true;
7441 }
7442
7443 filedata->section_groups = (struct group *) calloc (filedata->group_count,
7444 sizeof (struct group));
7445
7446 if (filedata->section_groups == NULL)
7447 {
7448 error (_("Out of memory reading %lu groups\n"),
7449 (unsigned long) filedata->group_count);
7450 return false;
7451 }
7452
7453 symtab_sec = NULL;
7454 strtab_sec = NULL;
7455 symtab = NULL;
7456 num_syms = 0;
7457 strtab = NULL;
7458 strtab_size = 0;
7459
7460 if (filedata->is_separate)
7461 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
7462
7463 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
7464 i < filedata->file_header.e_shnum;
7465 i++, section++)
7466 {
7467 if (section->sh_type == SHT_GROUP)
7468 {
7469 const char * name = printable_section_name (filedata, section);
7470 const char * group_name;
7471 unsigned char * start;
7472 unsigned char * indices;
7473 unsigned int entry, j, size;
7474 Elf_Internal_Shdr * sec;
7475 Elf_Internal_Sym * sym;
7476
7477 /* Get the symbol table. */
7478 if (section->sh_link >= filedata->file_header.e_shnum
7479 || ((sec = filedata->section_headers + section->sh_link)->sh_type
7480 != SHT_SYMTAB))
7481 {
7482 error (_("Bad sh_link in group section `%s'\n"), name);
7483 continue;
7484 }
7485
7486 if (symtab_sec != sec)
7487 {
7488 symtab_sec = sec;
7489 free (symtab);
7490 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
7491 }
7492
7493 if (symtab == NULL)
7494 {
7495 error (_("Corrupt header in group section `%s'\n"), name);
7496 continue;
7497 }
7498
7499 if (section->sh_info >= num_syms)
7500 {
7501 error (_("Bad sh_info in group section `%s'\n"), name);
7502 continue;
7503 }
7504
7505 sym = symtab + section->sh_info;
7506
7507 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7508 {
7509 if (sym->st_shndx == 0
7510 || sym->st_shndx >= filedata->file_header.e_shnum)
7511 {
7512 error (_("Bad sh_info in group section `%s'\n"), name);
7513 continue;
7514 }
7515
7516 group_name = section_name_print (filedata,
7517 filedata->section_headers
7518 + sym->st_shndx);
7519 strtab_sec = NULL;
7520 free (strtab);
7521 strtab = NULL;
7522 strtab_size = 0;
7523 }
7524 else
7525 {
7526 /* Get the string table. */
7527 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
7528 {
7529 strtab_sec = NULL;
7530 free (strtab);
7531 strtab = NULL;
7532 strtab_size = 0;
7533 }
7534 else if (strtab_sec
7535 != (sec = filedata->section_headers + symtab_sec->sh_link))
7536 {
7537 strtab_sec = sec;
7538 free (strtab);
7539
7540 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
7541 1, strtab_sec->sh_size,
7542 _("string table"));
7543 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
7544 }
7545 group_name = sym->st_name < strtab_size
7546 ? strtab + sym->st_name : _("<corrupt>");
7547 }
7548
7549 /* PR 17531: file: loop. */
7550 if (section->sh_entsize > section->sh_size)
7551 {
7552 error (_("Section %s has sh_entsize (0x%lx) which is larger than its size (0x%lx)\n"),
7553 printable_section_name (filedata, section),
7554 (unsigned long) section->sh_entsize,
7555 (unsigned long) section->sh_size);
7556 continue;
7557 }
7558
7559 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
7560 1, section->sh_size,
7561 _("section data"));
7562 if (start == NULL)
7563 continue;
7564
7565 indices = start;
7566 size = (section->sh_size / section->sh_entsize) - 1;
7567 entry = byte_get (indices, 4);
7568 indices += 4;
7569
7570 if (do_section_groups)
7571 {
7572 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
7573 get_group_flags (entry), i, name, group_name, size);
7574
7575 printf (_(" [Index] Name\n"));
7576 }
7577
7578 group->group_index = i;
7579
7580 for (j = 0; j < size; j++)
7581 {
7582 struct group_list * g;
7583
7584 entry = byte_get (indices, 4);
7585 indices += 4;
7586
7587 if (entry >= filedata->file_header.e_shnum)
7588 {
7589 static unsigned num_group_errors = 0;
7590
7591 if (num_group_errors ++ < 10)
7592 {
7593 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
7594 entry, i, filedata->file_header.e_shnum - 1);
7595 if (num_group_errors == 10)
7596 warn (_("Further error messages about overlarge group section indices suppressed\n"));
7597 }
7598 continue;
7599 }
7600
7601 if (filedata->section_headers_groups [entry] != NULL)
7602 {
7603 if (entry)
7604 {
7605 static unsigned num_errs = 0;
7606
7607 if (num_errs ++ < 10)
7608 {
7609 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
7610 entry, i,
7611 filedata->section_headers_groups [entry]->group_index);
7612 if (num_errs == 10)
7613 warn (_("Further error messages about already contained group sections suppressed\n"));
7614 }
7615 continue;
7616 }
7617 else
7618 {
7619 /* Intel C/C++ compiler may put section 0 in a
7620 section group. We just warn it the first time
7621 and ignore it afterwards. */
7622 static bool warned = false;
7623 if (!warned)
7624 {
7625 error (_("section 0 in group section [%5u]\n"),
7626 filedata->section_headers_groups [entry]->group_index);
7627 warned = true;
7628 }
7629 }
7630 }
7631
7632 filedata->section_headers_groups [entry] = group;
7633
7634 if (do_section_groups)
7635 {
7636 sec = filedata->section_headers + entry;
7637 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
7638 }
7639
7640 g = (struct group_list *) xmalloc (sizeof (struct group_list));
7641 g->section_index = entry;
7642 g->next = group->root;
7643 group->root = g;
7644 }
7645
7646 free (start);
7647
7648 group++;
7649 }
7650 }
7651
7652 free (symtab);
7653 free (strtab);
7654 return true;
7655}
7656
7657/* Data used to display dynamic fixups. */
7658
7659struct ia64_vms_dynfixup
7660{
7661 bfd_vma needed_ident; /* Library ident number. */
7662 bfd_vma needed; /* Index in the dstrtab of the library name. */
7663 bfd_vma fixup_needed; /* Index of the library. */
7664 bfd_vma fixup_rela_cnt; /* Number of fixups. */
7665 bfd_vma fixup_rela_off; /* Fixups offset in the dynamic segment. */
7666};
7667
7668/* Data used to display dynamic relocations. */
7669
7670struct ia64_vms_dynimgrela
7671{
7672 bfd_vma img_rela_cnt; /* Number of relocations. */
7673 bfd_vma img_rela_off; /* Reloc offset in the dynamic segment. */
7674};
7675
7676/* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
7677 library). */
7678
7679static bool
7680dump_ia64_vms_dynamic_fixups (Filedata * filedata,
7681 struct ia64_vms_dynfixup * fixup,
7682 const char * strtab,
7683 unsigned int strtab_sz)
7684{
7685 Elf64_External_VMS_IMAGE_FIXUP * imfs;
7686 long i;
7687 const char * lib_name;
7688
7689 imfs = get_data (NULL, filedata,
7690 filedata->dynamic_addr + fixup->fixup_rela_off,
7691 sizeof (*imfs), fixup->fixup_rela_cnt,
7692 _("dynamic section image fixups"));
7693 if (!imfs)
7694 return false;
7695
7696 if (fixup->needed < strtab_sz)
7697 lib_name = strtab + fixup->needed;
7698 else
7699 {
7700 warn (_("corrupt library name index of 0x%lx found in dynamic entry"),
7701 (unsigned long) fixup->needed);
7702 lib_name = "???";
7703 }
7704
7705 printf (_("\nImage fixups for needed library #%d: %s - ident: %lx\n"),
7706 (int) fixup->fixup_needed, lib_name, (long) fixup->needed_ident);
7707 printf
7708 (_("Seg Offset Type SymVec DataType\n"));
7709
7710 for (i = 0; i < (long) fixup->fixup_rela_cnt; i++)
7711 {
7712 unsigned int type;
7713 const char *rtype;
7714
7715 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
7716 printf_vma ((bfd_vma) BYTE_GET (imfs [i].fixup_offset));
7717 type = BYTE_GET (imfs [i].type);
7718 rtype = elf_ia64_reloc_type (type);
7719 if (rtype == NULL)
7720 printf (" 0x%08x ", type);
7721 else
7722 printf (" %-32s ", rtype);
7723 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
7724 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
7725 }
7726
7727 free (imfs);
7728 return true;
7729}
7730
7731/* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
7732
7733static bool
7734dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
7735{
7736 Elf64_External_VMS_IMAGE_RELA *imrs;
7737 long i;
7738
7739 imrs = get_data (NULL, filedata,
7740 filedata->dynamic_addr + imgrela->img_rela_off,
7741 sizeof (*imrs), imgrela->img_rela_cnt,
7742 _("dynamic section image relocations"));
7743 if (!imrs)
7744 return false;
7745
7746 printf (_("\nImage relocs\n"));
7747 printf
7748 (_("Seg Offset Type Addend Seg Sym Off\n"));
7749
7750 for (i = 0; i < (long) imgrela->img_rela_cnt; i++)
7751 {
7752 unsigned int type;
7753 const char *rtype;
7754
7755 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
7756 printf ("%08" BFD_VMA_FMT "x ",
7757 (bfd_vma) BYTE_GET (imrs [i].rela_offset));
7758 type = BYTE_GET (imrs [i].type);
7759 rtype = elf_ia64_reloc_type (type);
7760 if (rtype == NULL)
7761 printf ("0x%08x ", type);
7762 else
7763 printf ("%-31s ", rtype);
7764 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
7765 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
7766 printf ("%08" BFD_VMA_FMT "x\n",
7767 (bfd_vma) BYTE_GET (imrs [i].sym_offset));
7768 }
7769
7770 free (imrs);
7771 return true;
7772}
7773
7774/* Display IA-64 OpenVMS dynamic relocations and fixups. */
7775
7776static bool
7777process_ia64_vms_dynamic_relocs (Filedata * filedata)
7778{
7779 struct ia64_vms_dynfixup fixup;
7780 struct ia64_vms_dynimgrela imgrela;
7781 Elf_Internal_Dyn *entry;
7782 bfd_vma strtab_off = 0;
7783 bfd_vma strtab_sz = 0;
7784 char *strtab = NULL;
7785 bool res = true;
7786
7787 memset (&fixup, 0, sizeof (fixup));
7788 memset (&imgrela, 0, sizeof (imgrela));
7789
7790 /* Note: the order of the entries is specified by the OpenVMS specs. */
7791 for (entry = filedata->dynamic_section;
7792 entry < filedata->dynamic_section + filedata->dynamic_nent;
7793 entry++)
7794 {
7795 switch (entry->d_tag)
7796 {
7797 case DT_IA_64_VMS_STRTAB_OFFSET:
7798 strtab_off = entry->d_un.d_val;
7799 break;
7800 case DT_STRSZ:
7801 strtab_sz = entry->d_un.d_val;
7802 if (strtab == NULL)
7803 strtab = get_data (NULL, filedata,
7804 filedata->dynamic_addr + strtab_off,
7805 1, strtab_sz, _("dynamic string section"));
7806 if (strtab == NULL)
7807 strtab_sz = 0;
7808 break;
7809
7810 case DT_IA_64_VMS_NEEDED_IDENT:
7811 fixup.needed_ident = entry->d_un.d_val;
7812 break;
7813 case DT_NEEDED:
7814 fixup.needed = entry->d_un.d_val;
7815 break;
7816 case DT_IA_64_VMS_FIXUP_NEEDED:
7817 fixup.fixup_needed = entry->d_un.d_val;
7818 break;
7819 case DT_IA_64_VMS_FIXUP_RELA_CNT:
7820 fixup.fixup_rela_cnt = entry->d_un.d_val;
7821 break;
7822 case DT_IA_64_VMS_FIXUP_RELA_OFF:
7823 fixup.fixup_rela_off = entry->d_un.d_val;
7824 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
7825 res = false;
7826 break;
7827 case DT_IA_64_VMS_IMG_RELA_CNT:
7828 imgrela.img_rela_cnt = entry->d_un.d_val;
7829 break;
7830 case DT_IA_64_VMS_IMG_RELA_OFF:
7831 imgrela.img_rela_off = entry->d_un.d_val;
7832 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
7833 res = false;
7834 break;
7835
7836 default:
7837 break;
7838 }
7839 }
7840
7841 free (strtab);
7842
7843 return res;
7844}
7845
7846static struct
7847{
7848 const char * name;
7849 int reloc;
7850 int size;
7851 int rela;
7852}
7853 dynamic_relocations [] =
7854{
7855 { "REL", DT_REL, DT_RELSZ, false },
7856 { "RELA", DT_RELA, DT_RELASZ, true },
7857 { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
7858};
7859
7860/* Process the reloc section. */
7861
7862static bool
7863process_relocs (Filedata * filedata)
7864{
7865 unsigned long rel_size;
7866 unsigned long rel_offset;
7867
7868 if (!do_reloc)
7869 return true;
7870
7871 if (do_using_dynamic)
7872 {
7873 int is_rela;
7874 const char * name;
7875 bool has_dynamic_reloc;
7876 unsigned int i;
7877
7878 has_dynamic_reloc = false;
7879
7880 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
7881 {
7882 is_rela = dynamic_relocations [i].rela;
7883 name = dynamic_relocations [i].name;
7884 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
7885 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
7886
7887 if (rel_size)
7888 has_dynamic_reloc = true;
7889
7890 if (is_rela == UNKNOWN)
7891 {
7892 if (dynamic_relocations [i].reloc == DT_JMPREL)
7893 switch (filedata->dynamic_info[DT_PLTREL])
7894 {
7895 case DT_REL:
7896 is_rela = false;
7897 break;
7898 case DT_RELA:
7899 is_rela = true;
7900 break;
7901 }
7902 }
7903
7904 if (rel_size)
7905 {
7906 if (filedata->is_separate)
7907 printf
7908 (_("\nIn linked file '%s' section '%s' at offset 0x%lx contains %ld bytes:\n"),
7909 filedata->file_name, name, rel_offset, rel_size);
7910 else
7911 printf
7912 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
7913 name, rel_offset, rel_size);
7914
7915 dump_relocations (filedata,
7916 offset_from_vma (filedata, rel_offset, rel_size),
7917 rel_size,
7918 filedata->dynamic_symbols,
7919 filedata->num_dynamic_syms,
7920 filedata->dynamic_strings,
7921 filedata->dynamic_strings_length,
7922 is_rela, true /* is_dynamic */);
7923 }
7924 }
7925
7926 if (is_ia64_vms (filedata))
7927 if (process_ia64_vms_dynamic_relocs (filedata))
7928 has_dynamic_reloc = true;
7929
7930 if (! has_dynamic_reloc)
7931 {
7932 if (filedata->is_separate)
7933 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
7934 filedata->file_name);
7935 else
7936 printf (_("\nThere are no dynamic relocations in this file.\n"));
7937 }
7938 }
7939 else
7940 {
7941 Elf_Internal_Shdr * section;
7942 unsigned long i;
7943 bool found = false;
7944
7945 for (i = 0, section = filedata->section_headers;
7946 i < filedata->file_header.e_shnum;
7947 i++, section++)
7948 {
7949 if ( section->sh_type != SHT_RELA
7950 && section->sh_type != SHT_REL)
7951 continue;
7952
7953 rel_offset = section->sh_offset;
7954 rel_size = section->sh_size;
7955
7956 if (rel_size)
7957 {
7958 int is_rela;
7959 unsigned long num_rela;
7960
7961 if (filedata->is_separate)
7962 printf (_("\nIn linked file '%s' relocation section "),
7963 filedata->file_name);
7964 else
7965 printf (_("\nRelocation section "));
7966
7967 if (filedata->string_table == NULL)
7968 printf ("%d", section->sh_name);
7969 else
7970 printf ("'%s'", printable_section_name (filedata, section));
7971
7972 num_rela = rel_size / section->sh_entsize;
7973 printf (ngettext (" at offset 0x%lx contains %lu entry:\n",
7974 " at offset 0x%lx contains %lu entries:\n",
7975 num_rela),
7976 rel_offset, num_rela);
7977
7978 is_rela = section->sh_type == SHT_RELA;
7979
7980 if (section->sh_link != 0
7981 && section->sh_link < filedata->file_header.e_shnum)
7982 {
7983 Elf_Internal_Shdr * symsec;
7984 Elf_Internal_Sym * symtab;
7985 unsigned long nsyms;
7986 unsigned long strtablen = 0;
7987 char * strtab = NULL;
7988
7989 symsec = filedata->section_headers + section->sh_link;
7990 if (symsec->sh_type != SHT_SYMTAB
7991 && symsec->sh_type != SHT_DYNSYM)
7992 continue;
7993
7994 if (!get_symtab (filedata, symsec,
7995 &symtab, &nsyms, &strtab, &strtablen))
7996 continue;
7997
7998 dump_relocations (filedata, rel_offset, rel_size,
7999 symtab, nsyms, strtab, strtablen,
8000 is_rela,
8001 symsec->sh_type == SHT_DYNSYM);
8002 free (strtab);
8003 free (symtab);
8004 }
8005 else
8006 dump_relocations (filedata, rel_offset, rel_size,
8007 NULL, 0, NULL, 0, is_rela,
8008 false /* is_dynamic */);
8009
8010 found = true;
8011 }
8012 }
8013
8014 if (! found)
8015 {
8016 /* Users sometimes forget the -D option, so try to be helpful. */
8017 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8018 {
8019 if (filedata->dynamic_info[dynamic_relocations [i].size])
8020 {
8021 if (filedata->is_separate)
8022 printf (_("\nThere are no static relocations in linked file '%s'."),
8023 filedata->file_name);
8024 else
8025 printf (_("\nThere are no static relocations in this file."));
8026 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
8027
8028 break;
8029 }
8030 }
8031 if (i == ARRAY_SIZE (dynamic_relocations))
8032 {
8033 if (filedata->is_separate)
8034 printf (_("\nThere are no relocations in linked file '%s'.\n"),
8035 filedata->file_name);
8036 else
8037 printf (_("\nThere are no relocations in this file.\n"));
8038 }
8039 }
8040 }
8041
8042 return true;
8043}
8044
8045/* An absolute address consists of a section and an offset. If the
8046 section is NULL, the offset itself is the address, otherwise, the
8047 address equals to LOAD_ADDRESS(section) + offset. */
8048
8049struct absaddr
8050{
8051 unsigned short section;
8052 bfd_vma offset;
8053};
8054
8055/* Find the nearest symbol at or below ADDR. Returns the symbol
8056 name, if found, and the offset from the symbol to ADDR. */
8057
8058static void
8059find_symbol_for_address (Filedata * filedata,
8060 Elf_Internal_Sym * symtab,
8061 unsigned long nsyms,
8062 const char * strtab,
8063 unsigned long strtab_size,
8064 struct absaddr addr,
8065 const char ** symname,
8066 bfd_vma * offset)
8067{
8068 bfd_vma dist = 0x100000;
8069 Elf_Internal_Sym * sym;
8070 Elf_Internal_Sym * beg;
8071 Elf_Internal_Sym * end;
8072 Elf_Internal_Sym * best = NULL;
8073
8074 REMOVE_ARCH_BITS (addr.offset);
8075 beg = symtab;
8076 end = symtab + nsyms;
8077
8078 while (beg < end)
8079 {
8080 bfd_vma value;
8081
8082 sym = beg + (end - beg) / 2;
8083
8084 value = sym->st_value;
8085 REMOVE_ARCH_BITS (value);
8086
8087 if (sym->st_name != 0
8088 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
8089 && addr.offset >= value
8090 && addr.offset - value < dist)
8091 {
8092 best = sym;
8093 dist = addr.offset - value;
8094 if (!dist)
8095 break;
8096 }
8097
8098 if (addr.offset < value)
8099 end = sym;
8100 else
8101 beg = sym + 1;
8102 }
8103
8104 if (best)
8105 {
8106 *symname = (best->st_name >= strtab_size
8107 ? _("<corrupt>") : strtab + best->st_name);
8108 *offset = dist;
8109 return;
8110 }
8111
8112 *symname = NULL;
8113 *offset = addr.offset;
8114}
8115
8116static /* signed */ int
8117symcmp (const void *p, const void *q)
8118{
8119 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
8120 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
8121
8122 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
8123}
8124
8125/* Process the unwind section. */
8126
8127#include "unwind-ia64.h"
8128
8129struct ia64_unw_table_entry
8130{
8131 struct absaddr start;
8132 struct absaddr end;
8133 struct absaddr info;
8134};
8135
8136struct ia64_unw_aux_info
8137{
8138 struct ia64_unw_table_entry * table; /* Unwind table. */
8139 unsigned long table_len; /* Length of unwind table. */
8140 unsigned char * info; /* Unwind info. */
8141 unsigned long info_size; /* Size of unwind info. */
8142 bfd_vma info_addr; /* Starting address of unwind info. */
8143 bfd_vma seg_base; /* Starting address of segment. */
8144 Elf_Internal_Sym * symtab; /* The symbol table. */
8145 unsigned long nsyms; /* Number of symbols. */
8146 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8147 unsigned long nfuns; /* Number of entries in funtab. */
8148 char * strtab; /* The string table. */
8149 unsigned long strtab_size; /* Size of string table. */
8150};
8151
8152static bool
8153dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
8154{
8155 struct ia64_unw_table_entry * tp;
8156 unsigned long j, nfuns;
8157 int in_body;
8158 bool res = true;
8159
8160 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8161 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8162 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8163 aux->funtab[nfuns++] = aux->symtab[j];
8164 aux->nfuns = nfuns;
8165 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8166
8167 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8168 {
8169 bfd_vma stamp;
8170 bfd_vma offset;
8171 const unsigned char * dp;
8172 const unsigned char * head;
8173 const unsigned char * end;
8174 const char * procname;
8175
8176 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8177 aux->strtab_size, tp->start, &procname, &offset);
8178
8179 fputs ("\n<", stdout);
8180
8181 if (procname)
8182 {
8183 fputs (procname, stdout);
8184
8185 if (offset)
8186 printf ("+%lx", (unsigned long) offset);
8187 }
8188
8189 fputs (">: [", stdout);
8190 print_vma (tp->start.offset, PREFIX_HEX);
8191 fputc ('-', stdout);
8192 print_vma (tp->end.offset, PREFIX_HEX);
8193 printf ("], info at +0x%lx\n",
8194 (unsigned long) (tp->info.offset - aux->seg_base));
8195
8196 /* PR 17531: file: 86232b32. */
8197 if (aux->info == NULL)
8198 continue;
8199
8200 offset = tp->info.offset;
8201 if (tp->info.section)
8202 {
8203 if (tp->info.section >= filedata->file_header.e_shnum)
8204 {
8205 warn (_("Invalid section %u in table entry %ld\n"),
8206 tp->info.section, (long) (tp - aux->table));
8207 res = false;
8208 continue;
8209 }
8210 offset += filedata->section_headers[tp->info.section].sh_addr;
8211 }
8212 offset -= aux->info_addr;
8213 /* PR 17531: file: 0997b4d1. */
8214 if (offset >= aux->info_size
8215 || aux->info_size - offset < 8)
8216 {
8217 warn (_("Invalid offset %lx in table entry %ld\n"),
8218 (long) tp->info.offset, (long) (tp - aux->table));
8219 res = false;
8220 continue;
8221 }
8222
8223 head = aux->info + offset;
8224 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
8225
8226 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
8227 (unsigned) UNW_VER (stamp),
8228 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
8229 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
8230 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
8231 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
8232
8233 if (UNW_VER (stamp) != 1)
8234 {
8235 printf (_("\tUnknown version.\n"));
8236 continue;
8237 }
8238
8239 in_body = 0;
8240 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
8241 /* PR 17531: file: 16ceda89. */
8242 if (end > aux->info + aux->info_size)
8243 end = aux->info + aux->info_size;
8244 for (dp = head + 8; dp < end;)
8245 dp = unw_decode (dp, in_body, & in_body, end);
8246 }
8247
8248 free (aux->funtab);
8249
8250 return res;
8251}
8252
8253static bool
8254slurp_ia64_unwind_table (Filedata * filedata,
8255 struct ia64_unw_aux_info * aux,
8256 Elf_Internal_Shdr * sec)
8257{
8258 unsigned long size, nrelas, i;
8259 Elf_Internal_Phdr * seg;
8260 struct ia64_unw_table_entry * tep;
8261 Elf_Internal_Shdr * relsec;
8262 Elf_Internal_Rela * rela;
8263 Elf_Internal_Rela * rp;
8264 unsigned char * table;
8265 unsigned char * tp;
8266 Elf_Internal_Sym * sym;
8267 const char * relname;
8268
8269 aux->table_len = 0;
8270
8271 /* First, find the starting address of the segment that includes
8272 this section: */
8273
8274 if (filedata->file_header.e_phnum)
8275 {
8276 if (! get_program_headers (filedata))
8277 return false;
8278
8279 for (seg = filedata->program_headers;
8280 seg < filedata->program_headers + filedata->file_header.e_phnum;
8281 ++seg)
8282 {
8283 if (seg->p_type != PT_LOAD)
8284 continue;
8285
8286 if (sec->sh_addr >= seg->p_vaddr
8287 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8288 {
8289 aux->seg_base = seg->p_vaddr;
8290 break;
8291 }
8292 }
8293 }
8294
8295 /* Second, build the unwind table from the contents of the unwind section: */
8296 size = sec->sh_size;
8297 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8298 _("unwind table"));
8299 if (!table)
8300 return false;
8301
8302 aux->table_len = size / (3 * eh_addr_size);
8303 aux->table = (struct ia64_unw_table_entry *)
8304 xcmalloc (aux->table_len, sizeof (aux->table[0]));
8305 tep = aux->table;
8306
8307 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
8308 {
8309 tep->start.section = SHN_UNDEF;
8310 tep->end.section = SHN_UNDEF;
8311 tep->info.section = SHN_UNDEF;
8312 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8313 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8314 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
8315 tep->start.offset += aux->seg_base;
8316 tep->end.offset += aux->seg_base;
8317 tep->info.offset += aux->seg_base;
8318 }
8319 free (table);
8320
8321 /* Third, apply any relocations to the unwind table: */
8322 for (relsec = filedata->section_headers;
8323 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8324 ++relsec)
8325 {
8326 if (relsec->sh_type != SHT_RELA
8327 || relsec->sh_info >= filedata->file_header.e_shnum
8328 || filedata->section_headers + relsec->sh_info != sec)
8329 continue;
8330
8331 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8332 & rela, & nrelas))
8333 {
8334 free (aux->table);
8335 aux->table = NULL;
8336 aux->table_len = 0;
8337 return false;
8338 }
8339
8340 for (rp = rela; rp < rela + nrelas; ++rp)
8341 {
8342 unsigned int sym_ndx;
8343 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8344 relname = elf_ia64_reloc_type (r_type);
8345
8346 /* PR 17531: file: 9fa67536. */
8347 if (relname == NULL)
8348 {
8349 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8350 continue;
8351 }
8352
8353 if (! startswith (relname, "R_IA64_SEGREL"))
8354 {
8355 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8356 continue;
8357 }
8358
8359 i = rp->r_offset / (3 * eh_addr_size);
8360
8361 /* PR 17531: file: 5bc8d9bf. */
8362 if (i >= aux->table_len)
8363 {
8364 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8365 continue;
8366 }
8367
8368 sym_ndx = get_reloc_symindex (rp->r_info);
8369 if (sym_ndx >= aux->nsyms)
8370 {
8371 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8372 sym_ndx);
8373 continue;
8374 }
8375 sym = aux->symtab + sym_ndx;
8376
8377 switch (rp->r_offset / eh_addr_size % 3)
8378 {
8379 case 0:
8380 aux->table[i].start.section = sym->st_shndx;
8381 aux->table[i].start.offset = rp->r_addend + sym->st_value;
8382 break;
8383 case 1:
8384 aux->table[i].end.section = sym->st_shndx;
8385 aux->table[i].end.offset = rp->r_addend + sym->st_value;
8386 break;
8387 case 2:
8388 aux->table[i].info.section = sym->st_shndx;
8389 aux->table[i].info.offset = rp->r_addend + sym->st_value;
8390 break;
8391 default:
8392 break;
8393 }
8394 }
8395
8396 free (rela);
8397 }
8398
8399 return true;
8400}
8401
8402static bool
8403ia64_process_unwind (Filedata * filedata)
8404{
8405 Elf_Internal_Shdr * sec;
8406 Elf_Internal_Shdr * unwsec = NULL;
8407 unsigned long i, unwcount = 0, unwstart = 0;
8408 struct ia64_unw_aux_info aux;
8409 bool res = true;
8410
8411 memset (& aux, 0, sizeof (aux));
8412
8413 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8414 {
8415 if (sec->sh_type == SHT_SYMTAB)
8416 {
8417 if (aux.symtab)
8418 {
8419 error (_("Multiple symbol tables encountered\n"));
8420 free (aux.symtab);
8421 aux.symtab = NULL;
8422 free (aux.strtab);
8423 aux.strtab = NULL;
8424 }
8425 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8426 &aux.strtab, &aux.strtab_size))
8427 return false;
8428 }
8429 else if (sec->sh_type == SHT_IA_64_UNWIND)
8430 unwcount++;
8431 }
8432
8433 if (!unwcount)
8434 printf (_("\nThere are no unwind sections in this file.\n"));
8435
8436 while (unwcount-- > 0)
8437 {
8438 const char *suffix;
8439 size_t len, len2;
8440
8441 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
8442 i < filedata->file_header.e_shnum; ++i, ++sec)
8443 if (sec->sh_type == SHT_IA_64_UNWIND)
8444 {
8445 unwsec = sec;
8446 break;
8447 }
8448 /* We have already counted the number of SHT_IA64_UNWIND
8449 sections so the loop above should never fail. */
8450 assert (unwsec != NULL);
8451
8452 unwstart = i + 1;
8453 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
8454
8455 if ((unwsec->sh_flags & SHF_GROUP) != 0)
8456 {
8457 /* We need to find which section group it is in. */
8458 struct group_list * g;
8459
8460 if (filedata->section_headers_groups == NULL
8461 || filedata->section_headers_groups[i] == NULL)
8462 i = filedata->file_header.e_shnum;
8463 else
8464 {
8465 g = filedata->section_headers_groups[i]->root;
8466
8467 for (; g != NULL; g = g->next)
8468 {
8469 sec = filedata->section_headers + g->section_index;
8470
8471 if (section_name_valid (filedata, sec)
8472 && streq (section_name (filedata, sec),
8473 ELF_STRING_ia64_unwind_info))
8474 break;
8475 }
8476
8477 if (g == NULL)
8478 i = filedata->file_header.e_shnum;
8479 }
8480 }
8481 else if (section_name_valid (filedata, unwsec)
8482 && startswith (section_name (filedata, unwsec),
8483 ELF_STRING_ia64_unwind_once))
8484 {
8485 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
8486 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
8487 suffix = section_name (filedata, unwsec) + len;
8488 for (i = 0, sec = filedata->section_headers;
8489 i < filedata->file_header.e_shnum;
8490 ++i, ++sec)
8491 if (section_name_valid (filedata, sec)
8492 && startswith (section_name (filedata, sec),
8493 ELF_STRING_ia64_unwind_info_once)
8494 && streq (section_name (filedata, sec) + len2, suffix))
8495 break;
8496 }
8497 else
8498 {
8499 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
8500 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
8501 len = sizeof (ELF_STRING_ia64_unwind) - 1;
8502 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
8503 suffix = "";
8504 if (section_name_valid (filedata, unwsec)
8505 && startswith (section_name (filedata, unwsec),
8506 ELF_STRING_ia64_unwind))
8507 suffix = section_name (filedata, unwsec) + len;
8508 for (i = 0, sec = filedata->section_headers;
8509 i < filedata->file_header.e_shnum;
8510 ++i, ++sec)
8511 if (section_name_valid (filedata, sec)
8512 && startswith (section_name (filedata, sec),
8513 ELF_STRING_ia64_unwind_info)
8514 && streq (section_name (filedata, sec) + len2, suffix))
8515 break;
8516 }
8517
8518 if (i == filedata->file_header.e_shnum)
8519 {
8520 printf (_("\nCould not find unwind info section for "));
8521
8522 if (filedata->string_table == NULL)
8523 printf ("%d", unwsec->sh_name);
8524 else
8525 printf ("'%s'", printable_section_name (filedata, unwsec));
8526 }
8527 else
8528 {
8529 aux.info_addr = sec->sh_addr;
8530 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
8531 sec->sh_size,
8532 _("unwind info"));
8533 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
8534
8535 printf (_("\nUnwind section "));
8536
8537 if (filedata->string_table == NULL)
8538 printf ("%d", unwsec->sh_name);
8539 else
8540 printf ("'%s'", printable_section_name (filedata, unwsec));
8541
8542 printf (_(" at offset 0x%lx contains %lu entries:\n"),
8543 (unsigned long) unwsec->sh_offset,
8544 (unsigned long) (unwsec->sh_size / (3 * eh_addr_size)));
8545
8546 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
8547 && aux.table_len > 0)
8548 dump_ia64_unwind (filedata, & aux);
8549
8550 free ((char *) aux.table);
8551 free ((char *) aux.info);
8552 aux.table = NULL;
8553 aux.info = NULL;
8554 }
8555 }
8556
8557 free (aux.symtab);
8558 free ((char *) aux.strtab);
8559
8560 return res;
8561}
8562
8563struct hppa_unw_table_entry
8564{
8565 struct absaddr start;
8566 struct absaddr end;
8567 unsigned int Cannot_unwind:1; /* 0 */
8568 unsigned int Millicode:1; /* 1 */
8569 unsigned int Millicode_save_sr0:1; /* 2 */
8570 unsigned int Region_description:2; /* 3..4 */
8571 unsigned int reserved1:1; /* 5 */
8572 unsigned int Entry_SR:1; /* 6 */
8573 unsigned int Entry_FR:4; /* Number saved 7..10 */
8574 unsigned int Entry_GR:5; /* Number saved 11..15 */
8575 unsigned int Args_stored:1; /* 16 */
8576 unsigned int Variable_Frame:1; /* 17 */
8577 unsigned int Separate_Package_Body:1; /* 18 */
8578 unsigned int Frame_Extension_Millicode:1; /* 19 */
8579 unsigned int Stack_Overflow_Check:1; /* 20 */
8580 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
8581 unsigned int Ada_Region:1; /* 22 */
8582 unsigned int cxx_info:1; /* 23 */
8583 unsigned int cxx_try_catch:1; /* 24 */
8584 unsigned int sched_entry_seq:1; /* 25 */
8585 unsigned int reserved2:1; /* 26 */
8586 unsigned int Save_SP:1; /* 27 */
8587 unsigned int Save_RP:1; /* 28 */
8588 unsigned int Save_MRP_in_frame:1; /* 29 */
8589 unsigned int extn_ptr_defined:1; /* 30 */
8590 unsigned int Cleanup_defined:1; /* 31 */
8591
8592 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
8593 unsigned int HP_UX_interrupt_marker:1; /* 1 */
8594 unsigned int Large_frame:1; /* 2 */
8595 unsigned int Pseudo_SP_Set:1; /* 3 */
8596 unsigned int reserved4:1; /* 4 */
8597 unsigned int Total_frame_size:27; /* 5..31 */
8598};
8599
8600struct hppa_unw_aux_info
8601{
8602 struct hppa_unw_table_entry * table; /* Unwind table. */
8603 unsigned long table_len; /* Length of unwind table. */
8604 bfd_vma seg_base; /* Starting address of segment. */
8605 Elf_Internal_Sym * symtab; /* The symbol table. */
8606 unsigned long nsyms; /* Number of symbols. */
8607 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8608 unsigned long nfuns; /* Number of entries in funtab. */
8609 char * strtab; /* The string table. */
8610 unsigned long strtab_size; /* Size of string table. */
8611};
8612
8613static bool
8614dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
8615{
8616 struct hppa_unw_table_entry * tp;
8617 unsigned long j, nfuns;
8618 bool res = true;
8619
8620 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
8621 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
8622 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
8623 aux->funtab[nfuns++] = aux->symtab[j];
8624 aux->nfuns = nfuns;
8625 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
8626
8627 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
8628 {
8629 bfd_vma offset;
8630 const char * procname;
8631
8632 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8633 aux->strtab_size, tp->start, &procname,
8634 &offset);
8635
8636 fputs ("\n<", stdout);
8637
8638 if (procname)
8639 {
8640 fputs (procname, stdout);
8641
8642 if (offset)
8643 printf ("+%lx", (unsigned long) offset);
8644 }
8645
8646 fputs (">: [", stdout);
8647 print_vma (tp->start.offset, PREFIX_HEX);
8648 fputc ('-', stdout);
8649 print_vma (tp->end.offset, PREFIX_HEX);
8650 printf ("]\n\t");
8651
8652#define PF(_m) if (tp->_m) printf (#_m " ");
8653#define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
8654 PF(Cannot_unwind);
8655 PF(Millicode);
8656 PF(Millicode_save_sr0);
8657 /* PV(Region_description); */
8658 PF(Entry_SR);
8659 PV(Entry_FR);
8660 PV(Entry_GR);
8661 PF(Args_stored);
8662 PF(Variable_Frame);
8663 PF(Separate_Package_Body);
8664 PF(Frame_Extension_Millicode);
8665 PF(Stack_Overflow_Check);
8666 PF(Two_Instruction_SP_Increment);
8667 PF(Ada_Region);
8668 PF(cxx_info);
8669 PF(cxx_try_catch);
8670 PF(sched_entry_seq);
8671 PF(Save_SP);
8672 PF(Save_RP);
8673 PF(Save_MRP_in_frame);
8674 PF(extn_ptr_defined);
8675 PF(Cleanup_defined);
8676 PF(MPE_XL_interrupt_marker);
8677 PF(HP_UX_interrupt_marker);
8678 PF(Large_frame);
8679 PF(Pseudo_SP_Set);
8680 PV(Total_frame_size);
8681#undef PF
8682#undef PV
8683 }
8684
8685 printf ("\n");
8686
8687 free (aux->funtab);
8688
8689 return res;
8690}
8691
8692static bool
8693slurp_hppa_unwind_table (Filedata * filedata,
8694 struct hppa_unw_aux_info * aux,
8695 Elf_Internal_Shdr * sec)
8696{
8697 unsigned long size, unw_ent_size, nentries, nrelas, i;
8698 Elf_Internal_Phdr * seg;
8699 struct hppa_unw_table_entry * tep;
8700 Elf_Internal_Shdr * relsec;
8701 Elf_Internal_Rela * rela;
8702 Elf_Internal_Rela * rp;
8703 unsigned char * table;
8704 unsigned char * tp;
8705 Elf_Internal_Sym * sym;
8706 const char * relname;
8707
8708 /* First, find the starting address of the segment that includes
8709 this section. */
8710 if (filedata->file_header.e_phnum)
8711 {
8712 if (! get_program_headers (filedata))
8713 return false;
8714
8715 for (seg = filedata->program_headers;
8716 seg < filedata->program_headers + filedata->file_header.e_phnum;
8717 ++seg)
8718 {
8719 if (seg->p_type != PT_LOAD)
8720 continue;
8721
8722 if (sec->sh_addr >= seg->p_vaddr
8723 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
8724 {
8725 aux->seg_base = seg->p_vaddr;
8726 break;
8727 }
8728 }
8729 }
8730
8731 /* Second, build the unwind table from the contents of the unwind
8732 section. */
8733 size = sec->sh_size;
8734 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
8735 _("unwind table"));
8736 if (!table)
8737 return false;
8738
8739 unw_ent_size = 16;
8740 nentries = size / unw_ent_size;
8741 size = unw_ent_size * nentries;
8742
8743 aux->table_len = nentries;
8744 tep = aux->table = (struct hppa_unw_table_entry *)
8745 xcmalloc (nentries, sizeof (aux->table[0]));
8746
8747 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
8748 {
8749 unsigned int tmp1, tmp2;
8750
8751 tep->start.section = SHN_UNDEF;
8752 tep->end.section = SHN_UNDEF;
8753
8754 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
8755 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
8756 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
8757 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
8758
8759 tep->start.offset += aux->seg_base;
8760 tep->end.offset += aux->seg_base;
8761
8762 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
8763 tep->Millicode = (tmp1 >> 30) & 0x1;
8764 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
8765 tep->Region_description = (tmp1 >> 27) & 0x3;
8766 tep->reserved1 = (tmp1 >> 26) & 0x1;
8767 tep->Entry_SR = (tmp1 >> 25) & 0x1;
8768 tep->Entry_FR = (tmp1 >> 21) & 0xf;
8769 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
8770 tep->Args_stored = (tmp1 >> 15) & 0x1;
8771 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
8772 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
8773 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
8774 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
8775 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
8776 tep->Ada_Region = (tmp1 >> 9) & 0x1;
8777 tep->cxx_info = (tmp1 >> 8) & 0x1;
8778 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
8779 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
8780 tep->reserved2 = (tmp1 >> 5) & 0x1;
8781 tep->Save_SP = (tmp1 >> 4) & 0x1;
8782 tep->Save_RP = (tmp1 >> 3) & 0x1;
8783 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
8784 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
8785 tep->Cleanup_defined = tmp1 & 0x1;
8786
8787 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
8788 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
8789 tep->Large_frame = (tmp2 >> 29) & 0x1;
8790 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
8791 tep->reserved4 = (tmp2 >> 27) & 0x1;
8792 tep->Total_frame_size = tmp2 & 0x7ffffff;
8793 }
8794 free (table);
8795
8796 /* Third, apply any relocations to the unwind table. */
8797 for (relsec = filedata->section_headers;
8798 relsec < filedata->section_headers + filedata->file_header.e_shnum;
8799 ++relsec)
8800 {
8801 if (relsec->sh_type != SHT_RELA
8802 || relsec->sh_info >= filedata->file_header.e_shnum
8803 || filedata->section_headers + relsec->sh_info != sec)
8804 continue;
8805
8806 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
8807 & rela, & nrelas))
8808 return false;
8809
8810 for (rp = rela; rp < rela + nrelas; ++rp)
8811 {
8812 unsigned int sym_ndx;
8813 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
8814 relname = elf_hppa_reloc_type (r_type);
8815
8816 if (relname == NULL)
8817 {
8818 warn (_("Skipping unknown relocation type: %u\n"), r_type);
8819 continue;
8820 }
8821
8822 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
8823 if (! startswith (relname, "R_PARISC_SEGREL"))
8824 {
8825 warn (_("Skipping unexpected relocation type: %s\n"), relname);
8826 continue;
8827 }
8828
8829 i = rp->r_offset / unw_ent_size;
8830 if (i >= aux->table_len)
8831 {
8832 warn (_("Skipping reloc with overlarge offset: %lx\n"), i);
8833 continue;
8834 }
8835
8836 sym_ndx = get_reloc_symindex (rp->r_info);
8837 if (sym_ndx >= aux->nsyms)
8838 {
8839 warn (_("Skipping reloc with invalid symbol index: %u\n"),
8840 sym_ndx);
8841 continue;
8842 }
8843 sym = aux->symtab + sym_ndx;
8844
8845 switch ((rp->r_offset % unw_ent_size) / 4)
8846 {
8847 case 0:
8848 aux->table[i].start.section = sym->st_shndx;
8849 aux->table[i].start.offset = sym->st_value + rp->r_addend;
8850 break;
8851 case 1:
8852 aux->table[i].end.section = sym->st_shndx;
8853 aux->table[i].end.offset = sym->st_value + rp->r_addend;
8854 break;
8855 default:
8856 break;
8857 }
8858 }
8859
8860 free (rela);
8861 }
8862
8863 return true;
8864}
8865
8866static bool
8867hppa_process_unwind (Filedata * filedata)
8868{
8869 struct hppa_unw_aux_info aux;
8870 Elf_Internal_Shdr * unwsec = NULL;
8871 Elf_Internal_Shdr * sec;
8872 unsigned long i;
8873 bool res = true;
8874
8875 if (filedata->string_table == NULL)
8876 return false;
8877
8878 memset (& aux, 0, sizeof (aux));
8879
8880 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8881 {
8882 if (sec->sh_type == SHT_SYMTAB)
8883 {
8884 if (aux.symtab)
8885 {
8886 error (_("Multiple symbol tables encountered\n"));
8887 free (aux.symtab);
8888 aux.symtab = NULL;
8889 free (aux.strtab);
8890 aux.strtab = NULL;
8891 }
8892 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
8893 &aux.strtab, &aux.strtab_size))
8894 return false;
8895 }
8896 else if (section_name_valid (filedata, sec)
8897 && streq (section_name (filedata, sec), ".PARISC.unwind"))
8898 unwsec = sec;
8899 }
8900
8901 if (!unwsec)
8902 printf (_("\nThere are no unwind sections in this file.\n"));
8903
8904 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
8905 {
8906 if (section_name_valid (filedata, sec)
8907 && streq (section_name (filedata, sec), ".PARISC.unwind"))
8908 {
8909 unsigned long num_unwind = sec->sh_size / 16;
8910
8911 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
8912 "contains %lu entry:\n",
8913 "\nUnwind section '%s' at offset 0x%lx "
8914 "contains %lu entries:\n",
8915 num_unwind),
8916 printable_section_name (filedata, sec),
8917 (unsigned long) sec->sh_offset,
8918 num_unwind);
8919
8920 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
8921 res = false;
8922
8923 if (res && aux.table_len > 0)
8924 {
8925 if (! dump_hppa_unwind (filedata, &aux))
8926 res = false;
8927 }
8928
8929 free ((char *) aux.table);
8930 aux.table = NULL;
8931 }
8932 }
8933
8934 free (aux.symtab);
8935 free ((char *) aux.strtab);
8936
8937 return res;
8938}
8939
8940struct arm_section
8941{
8942 unsigned char * data; /* The unwind data. */
8943 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
8944 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
8945 unsigned long nrelas; /* The number of relocations. */
8946 unsigned int rel_type; /* REL or RELA ? */
8947 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
8948};
8949
8950struct arm_unw_aux_info
8951{
8952 Filedata * filedata; /* The file containing the unwind sections. */
8953 Elf_Internal_Sym * symtab; /* The file's symbol table. */
8954 unsigned long nsyms; /* Number of symbols. */
8955 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
8956 unsigned long nfuns; /* Number of these symbols. */
8957 char * strtab; /* The file's string table. */
8958 unsigned long strtab_size; /* Size of string table. */
8959};
8960
8961static const char *
8962arm_print_vma_and_name (Filedata * filedata,
8963 struct arm_unw_aux_info * aux,
8964 bfd_vma fn,
8965 struct absaddr addr)
8966{
8967 const char *procname;
8968 bfd_vma sym_offset;
8969
8970 if (addr.section == SHN_UNDEF)
8971 addr.offset = fn;
8972
8973 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
8974 aux->strtab_size, addr, &procname,
8975 &sym_offset);
8976
8977 print_vma (fn, PREFIX_HEX);
8978
8979 if (procname)
8980 {
8981 fputs (" <", stdout);
8982 fputs (procname, stdout);
8983
8984 if (sym_offset)
8985 printf ("+0x%lx", (unsigned long) sym_offset);
8986 fputc ('>', stdout);
8987 }
8988
8989 return procname;
8990}
8991
8992static void
8993arm_free_section (struct arm_section *arm_sec)
8994{
8995 free (arm_sec->data);
8996 free (arm_sec->rela);
8997}
8998
8999/* 1) If SEC does not match the one cached in ARM_SEC, then free the current
9000 cached section and install SEC instead.
9001 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
9002 and return its valued in * WORDP, relocating if necessary.
9003 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
9004 relocation's offset in ADDR.
9005 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
9006 into the string table of the symbol associated with the reloc. If no
9007 reloc was applied store -1 there.
9008 5) Return TRUE upon success, FALSE otherwise. */
9009
9010static bool
9011get_unwind_section_word (Filedata * filedata,
9012 struct arm_unw_aux_info * aux,
9013 struct arm_section * arm_sec,
9014 Elf_Internal_Shdr * sec,
9015 bfd_vma word_offset,
9016 unsigned int * wordp,
9017 struct absaddr * addr,
9018 bfd_vma * sym_name)
9019{
9020 Elf_Internal_Rela *rp;
9021 Elf_Internal_Sym *sym;
9022 const char * relname;
9023 unsigned int word;
9024 bool wrapped;
9025
9026 if (sec == NULL || arm_sec == NULL)
9027 return false;
9028
9029 addr->section = SHN_UNDEF;
9030 addr->offset = 0;
9031
9032 if (sym_name != NULL)
9033 *sym_name = (bfd_vma) -1;
9034
9035 /* If necessary, update the section cache. */
9036 if (sec != arm_sec->sec)
9037 {
9038 Elf_Internal_Shdr *relsec;
9039
9040 arm_free_section (arm_sec);
9041
9042 arm_sec->sec = sec;
9043 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
9044 sec->sh_size, _("unwind data"));
9045 arm_sec->rela = NULL;
9046 arm_sec->nrelas = 0;
9047
9048 for (relsec = filedata->section_headers;
9049 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9050 ++relsec)
9051 {
9052 if (relsec->sh_info >= filedata->file_header.e_shnum
9053 || filedata->section_headers + relsec->sh_info != sec
9054 /* PR 15745: Check the section type as well. */
9055 || (relsec->sh_type != SHT_REL
9056 && relsec->sh_type != SHT_RELA))
9057 continue;
9058
9059 arm_sec->rel_type = relsec->sh_type;
9060 if (relsec->sh_type == SHT_REL)
9061 {
9062 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
9063 relsec->sh_size,
9064 & arm_sec->rela, & arm_sec->nrelas))
9065 return false;
9066 }
9067 else /* relsec->sh_type == SHT_RELA */
9068 {
9069 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
9070 relsec->sh_size,
9071 & arm_sec->rela, & arm_sec->nrelas))
9072 return false;
9073 }
9074 break;
9075 }
9076
9077 arm_sec->next_rela = arm_sec->rela;
9078 }
9079
9080 /* If there is no unwind data we can do nothing. */
9081 if (arm_sec->data == NULL)
9082 return false;
9083
9084 /* If the offset is invalid then fail. */
9085 if (/* PR 21343 *//* PR 18879 */
9086 sec->sh_size < 4
9087 || word_offset > (sec->sh_size - 4)
9088 || ((bfd_signed_vma) word_offset) < 0)
9089 return false;
9090
9091 /* Get the word at the required offset. */
9092 word = byte_get (arm_sec->data + word_offset, 4);
9093
9094 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
9095 if (arm_sec->rela == NULL)
9096 {
9097 * wordp = word;
9098 return true;
9099 }
9100
9101 /* Look through the relocs to find the one that applies to the provided offset. */
9102 wrapped = false;
9103 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
9104 {
9105 bfd_vma prelval, offset;
9106
9107 if (rp->r_offset > word_offset && !wrapped)
9108 {
9109 rp = arm_sec->rela;
9110 wrapped = true;
9111 }
9112 if (rp->r_offset > word_offset)
9113 break;
9114
9115 if (rp->r_offset & 3)
9116 {
9117 warn (_("Skipping unexpected relocation at offset 0x%lx\n"),
9118 (unsigned long) rp->r_offset);
9119 continue;
9120 }
9121
9122 if (rp->r_offset < word_offset)
9123 continue;
9124
9125 /* PR 17531: file: 027-161405-0.004 */
9126 if (aux->symtab == NULL)
9127 continue;
9128
9129 if (arm_sec->rel_type == SHT_REL)
9130 {
9131 offset = word & 0x7fffffff;
9132 if (offset & 0x40000000)
9133 offset |= ~ (bfd_vma) 0x7fffffff;
9134 }
9135 else if (arm_sec->rel_type == SHT_RELA)
9136 offset = rp->r_addend;
9137 else
9138 {
9139 error (_("Unknown section relocation type %d encountered\n"),
9140 arm_sec->rel_type);
9141 break;
9142 }
9143
9144 /* PR 17531 file: 027-1241568-0.004. */
9145 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
9146 {
9147 error (_("Bad symbol index in unwind relocation (%lu > %lu)\n"),
9148 (unsigned long) ELF32_R_SYM (rp->r_info), aux->nsyms);
9149 break;
9150 }
9151
9152 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
9153 offset += sym->st_value;
9154 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
9155
9156 /* Check that we are processing the expected reloc type. */
9157 if (filedata->file_header.e_machine == EM_ARM)
9158 {
9159 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
9160 if (relname == NULL)
9161 {
9162 warn (_("Skipping unknown ARM relocation type: %d\n"),
9163 (int) ELF32_R_TYPE (rp->r_info));
9164 continue;
9165 }
9166
9167 if (streq (relname, "R_ARM_NONE"))
9168 continue;
9169
9170 if (! streq (relname, "R_ARM_PREL31"))
9171 {
9172 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
9173 continue;
9174 }
9175 }
9176 else if (filedata->file_header.e_machine == EM_TI_C6000)
9177 {
9178 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
9179 if (relname == NULL)
9180 {
9181 warn (_("Skipping unknown C6000 relocation type: %d\n"),
9182 (int) ELF32_R_TYPE (rp->r_info));
9183 continue;
9184 }
9185
9186 if (streq (relname, "R_C6000_NONE"))
9187 continue;
9188
9189 if (! streq (relname, "R_C6000_PREL31"))
9190 {
9191 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
9192 continue;
9193 }
9194
9195 prelval >>= 1;
9196 }
9197 else
9198 {
9199 /* This function currently only supports ARM and TI unwinders. */
9200 warn (_("Only TI and ARM unwinders are currently supported\n"));
9201 break;
9202 }
9203
9204 word = (word & ~ (bfd_vma) 0x7fffffff) | (prelval & 0x7fffffff);
9205 addr->section = sym->st_shndx;
9206 addr->offset = offset;
9207
9208 if (sym_name)
9209 * sym_name = sym->st_name;
9210 break;
9211 }
9212
9213 *wordp = word;
9214 arm_sec->next_rela = rp;
9215
9216 return true;
9217}
9218
9219static const char *tic6x_unwind_regnames[16] =
9220{
9221 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
9222 "A14", "A13", "A12", "A11", "A10",
9223 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
9224};
9225
9226static void
9227decode_tic6x_unwind_regmask (unsigned int mask)
9228{
9229 int i;
9230
9231 for (i = 12; mask; mask >>= 1, i--)
9232 {
9233 if (mask & 1)
9234 {
9235 fputs (tic6x_unwind_regnames[i], stdout);
9236 if (mask > 1)
9237 fputs (", ", stdout);
9238 }
9239 }
9240}
9241
9242#define ADVANCE \
9243 if (remaining == 0 && more_words) \
9244 { \
9245 data_offset += 4; \
9246 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
9247 data_offset, & word, & addr, NULL)) \
9248 return false; \
9249 remaining = 4; \
9250 more_words--; \
9251 } \
9252
9253#define GET_OP(OP) \
9254 ADVANCE; \
9255 if (remaining) \
9256 { \
9257 remaining--; \
9258 (OP) = word >> 24; \
9259 word <<= 8; \
9260 } \
9261 else \
9262 { \
9263 printf (_("[Truncated opcode]\n")); \
9264 return false; \
9265 } \
9266 printf ("0x%02x ", OP)
9267
9268static bool
9269decode_arm_unwind_bytecode (Filedata * filedata,
9270 struct arm_unw_aux_info * aux,
9271 unsigned int word,
9272 unsigned int remaining,
9273 unsigned int more_words,
9274 bfd_vma data_offset,
9275 Elf_Internal_Shdr * data_sec,
9276 struct arm_section * data_arm_sec)
9277{
9278 struct absaddr addr;
9279 bool res = true;
9280
9281 /* Decode the unwinding instructions. */
9282 while (1)
9283 {
9284 unsigned int op, op2;
9285
9286 ADVANCE;
9287 if (remaining == 0)
9288 break;
9289 remaining--;
9290 op = word >> 24;
9291 word <<= 8;
9292
9293 printf (" 0x%02x ", op);
9294
9295 if ((op & 0xc0) == 0x00)
9296 {
9297 int offset = ((op & 0x3f) << 2) + 4;
9298
9299 printf (" vsp = vsp + %d", offset);
9300 }
9301 else if ((op & 0xc0) == 0x40)
9302 {
9303 int offset = ((op & 0x3f) << 2) + 4;
9304
9305 printf (" vsp = vsp - %d", offset);
9306 }
9307 else if ((op & 0xf0) == 0x80)
9308 {
9309 GET_OP (op2);
9310 if (op == 0x80 && op2 == 0)
9311 printf (_("Refuse to unwind"));
9312 else
9313 {
9314 unsigned int mask = ((op & 0x0f) << 8) | op2;
9315 bool first = true;
9316 int i;
9317
9318 printf ("pop {");
9319 for (i = 0; i < 12; i++)
9320 if (mask & (1 << i))
9321 {
9322 if (first)
9323 first = false;
9324 else
9325 printf (", ");
9326 printf ("r%d", 4 + i);
9327 }
9328 printf ("}");
9329 }
9330 }
9331 else if ((op & 0xf0) == 0x90)
9332 {
9333 if (op == 0x9d || op == 0x9f)
9334 printf (_(" [Reserved]"));
9335 else
9336 printf (" vsp = r%d", op & 0x0f);
9337 }
9338 else if ((op & 0xf0) == 0xa0)
9339 {
9340 int end = 4 + (op & 0x07);
9341 bool first = true;
9342 int i;
9343
9344 printf (" pop {");
9345 for (i = 4; i <= end; i++)
9346 {
9347 if (first)
9348 first = false;
9349 else
9350 printf (", ");
9351 printf ("r%d", i);
9352 }
9353 if (op & 0x08)
9354 {
9355 if (!first)
9356 printf (", ");
9357 printf ("r14");
9358 }
9359 printf ("}");
9360 }
9361 else if (op == 0xb0)
9362 printf (_(" finish"));
9363 else if (op == 0xb1)
9364 {
9365 GET_OP (op2);
9366 if (op2 == 0 || (op2 & 0xf0) != 0)
9367 printf (_("[Spare]"));
9368 else
9369 {
9370 unsigned int mask = op2 & 0x0f;
9371 bool first = true;
9372 int i;
9373
9374 printf ("pop {");
9375 for (i = 0; i < 12; i++)
9376 if (mask & (1 << i))
9377 {
9378 if (first)
9379 first = false;
9380 else
9381 printf (", ");
9382 printf ("r%d", i);
9383 }
9384 printf ("}");
9385 }
9386 }
9387 else if (op == 0xb2)
9388 {
9389 unsigned char buf[9];
9390 unsigned int i, len;
9391 unsigned long offset;
9392
9393 for (i = 0; i < sizeof (buf); i++)
9394 {
9395 GET_OP (buf[i]);
9396 if ((buf[i] & 0x80) == 0)
9397 break;
9398 }
9399 if (i == sizeof (buf))
9400 {
9401 error (_("corrupt change to vsp\n"));
9402 res = false;
9403 }
9404 else
9405 {
9406 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9407 assert (len == i + 1);
9408 offset = offset * 4 + 0x204;
9409 printf ("vsp = vsp + %ld", offset);
9410 }
9411 }
9412 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
9413 {
9414 unsigned int first, last;
9415
9416 GET_OP (op2);
9417 first = op2 >> 4;
9418 last = op2 & 0x0f;
9419 if (op == 0xc8)
9420 first = first + 16;
9421 printf ("pop {D%d", first);
9422 if (last)
9423 printf ("-D%d", first + last);
9424 printf ("}");
9425 }
9426 else if (op == 0xb4)
9427 printf (_(" pop {ra_auth_code}"));
9428 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
9429 {
9430 unsigned int count = op & 0x07;
9431
9432 printf ("pop {D8");
9433 if (count)
9434 printf ("-D%d", 8 + count);
9435 printf ("}");
9436 }
9437 else if (op >= 0xc0 && op <= 0xc5)
9438 {
9439 unsigned int count = op & 0x07;
9440
9441 printf (" pop {wR10");
9442 if (count)
9443 printf ("-wR%d", 10 + count);
9444 printf ("}");
9445 }
9446 else if (op == 0xc6)
9447 {
9448 unsigned int first, last;
9449
9450 GET_OP (op2);
9451 first = op2 >> 4;
9452 last = op2 & 0x0f;
9453 printf ("pop {wR%d", first);
9454 if (last)
9455 printf ("-wR%d", first + last);
9456 printf ("}");
9457 }
9458 else if (op == 0xc7)
9459 {
9460 GET_OP (op2);
9461 if (op2 == 0 || (op2 & 0xf0) != 0)
9462 printf (_("[Spare]"));
9463 else
9464 {
9465 unsigned int mask = op2 & 0x0f;
9466 bool first = true;
9467 int i;
9468
9469 printf ("pop {");
9470 for (i = 0; i < 4; i++)
9471 if (mask & (1 << i))
9472 {
9473 if (first)
9474 first = false;
9475 else
9476 printf (", ");
9477 printf ("wCGR%d", i);
9478 }
9479 printf ("}");
9480 }
9481 }
9482 else
9483 {
9484 printf (_(" [unsupported opcode]"));
9485 res = false;
9486 }
9487
9488 printf ("\n");
9489 }
9490
9491 return res;
9492}
9493
9494static bool
9495decode_tic6x_unwind_bytecode (Filedata * filedata,
9496 struct arm_unw_aux_info * aux,
9497 unsigned int word,
9498 unsigned int remaining,
9499 unsigned int more_words,
9500 bfd_vma data_offset,
9501 Elf_Internal_Shdr * data_sec,
9502 struct arm_section * data_arm_sec)
9503{
9504 struct absaddr addr;
9505
9506 /* Decode the unwinding instructions. */
9507 while (1)
9508 {
9509 unsigned int op, op2;
9510
9511 ADVANCE;
9512 if (remaining == 0)
9513 break;
9514 remaining--;
9515 op = word >> 24;
9516 word <<= 8;
9517
9518 printf (" 0x%02x ", op);
9519
9520 if ((op & 0xc0) == 0x00)
9521 {
9522 int offset = ((op & 0x3f) << 3) + 8;
9523 printf (" sp = sp + %d", offset);
9524 }
9525 else if ((op & 0xc0) == 0x80)
9526 {
9527 GET_OP (op2);
9528 if (op == 0x80 && op2 == 0)
9529 printf (_("Refuse to unwind"));
9530 else
9531 {
9532 unsigned int mask = ((op & 0x1f) << 8) | op2;
9533 if (op & 0x20)
9534 printf ("pop compact {");
9535 else
9536 printf ("pop {");
9537
9538 decode_tic6x_unwind_regmask (mask);
9539 printf("}");
9540 }
9541 }
9542 else if ((op & 0xf0) == 0xc0)
9543 {
9544 unsigned int reg;
9545 unsigned int nregs;
9546 unsigned int i;
9547 const char *name;
9548 struct
9549 {
9550 unsigned int offset;
9551 unsigned int reg;
9552 } regpos[16];
9553
9554 /* Scan entire instruction first so that GET_OP output is not
9555 interleaved with disassembly. */
9556 nregs = 0;
9557 for (i = 0; nregs < (op & 0xf); i++)
9558 {
9559 GET_OP (op2);
9560 reg = op2 >> 4;
9561 if (reg != 0xf)
9562 {
9563 regpos[nregs].offset = i * 2;
9564 regpos[nregs].reg = reg;
9565 nregs++;
9566 }
9567
9568 reg = op2 & 0xf;
9569 if (reg != 0xf)
9570 {
9571 regpos[nregs].offset = i * 2 + 1;
9572 regpos[nregs].reg = reg;
9573 nregs++;
9574 }
9575 }
9576
9577 printf (_("pop frame {"));
9578 if (nregs == 0)
9579 {
9580 printf (_("*corrupt* - no registers specified"));
9581 }
9582 else
9583 {
9584 reg = nregs - 1;
9585 for (i = i * 2; i > 0; i--)
9586 {
9587 if (regpos[reg].offset == i - 1)
9588 {
9589 name = tic6x_unwind_regnames[regpos[reg].reg];
9590 if (reg > 0)
9591 reg--;
9592 }
9593 else
9594 name = _("[pad]");
9595
9596 fputs (name, stdout);
9597 if (i > 1)
9598 printf (", ");
9599 }
9600 }
9601
9602 printf ("}");
9603 }
9604 else if (op == 0xd0)
9605 printf (" MOV FP, SP");
9606 else if (op == 0xd1)
9607 printf (" __c6xabi_pop_rts");
9608 else if (op == 0xd2)
9609 {
9610 unsigned char buf[9];
9611 unsigned int i, len;
9612 unsigned long offset;
9613
9614 for (i = 0; i < sizeof (buf); i++)
9615 {
9616 GET_OP (buf[i]);
9617 if ((buf[i] & 0x80) == 0)
9618 break;
9619 }
9620 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
9621 if (i == sizeof (buf))
9622 {
9623 warn (_("Corrupt stack pointer adjustment detected\n"));
9624 return false;
9625 }
9626
9627 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
9628 assert (len == i + 1);
9629 offset = offset * 8 + 0x408;
9630 printf (_("sp = sp + %ld"), offset);
9631 }
9632 else if ((op & 0xf0) == 0xe0)
9633 {
9634 if ((op & 0x0f) == 7)
9635 printf (" RETURN");
9636 else
9637 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
9638 }
9639 else
9640 {
9641 printf (_(" [unsupported opcode]"));
9642 }
9643 putchar ('\n');
9644 }
9645
9646 return true;
9647}
9648
9649static bfd_vma
9650arm_expand_prel31 (Filedata * filedata, bfd_vma word, bfd_vma where)
9651{
9652 bfd_vma offset;
9653
9654 offset = word & 0x7fffffff;
9655 if (offset & 0x40000000)
9656 offset |= ~ (bfd_vma) 0x7fffffff;
9657
9658 if (filedata->file_header.e_machine == EM_TI_C6000)
9659 offset <<= 1;
9660
9661 return offset + where;
9662}
9663
9664static bool
9665decode_arm_unwind (Filedata * filedata,
9666 struct arm_unw_aux_info * aux,
9667 unsigned int word,
9668 unsigned int remaining,
9669 bfd_vma data_offset,
9670 Elf_Internal_Shdr * data_sec,
9671 struct arm_section * data_arm_sec)
9672{
9673 int per_index;
9674 unsigned int more_words = 0;
9675 struct absaddr addr;
9676 bfd_vma sym_name = (bfd_vma) -1;
9677 bool res = true;
9678
9679 if (remaining == 0)
9680 {
9681 /* Fetch the first word.
9682 Note - when decoding an object file the address extracted
9683 here will always be 0. So we also pass in the sym_name
9684 parameter so that we can find the symbol associated with
9685 the personality routine. */
9686 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
9687 & word, & addr, & sym_name))
9688 return false;
9689
9690 remaining = 4;
9691 }
9692 else
9693 {
9694 addr.section = SHN_UNDEF;
9695 addr.offset = 0;
9696 }
9697
9698 if ((word & 0x80000000) == 0)
9699 {
9700 /* Expand prel31 for personality routine. */
9701 bfd_vma fn;
9702 const char *procname;
9703
9704 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
9705 printf (_(" Personality routine: "));
9706 if (fn == 0
9707 && addr.section == SHN_UNDEF && addr.offset == 0
9708 && sym_name != (bfd_vma) -1 && sym_name < aux->strtab_size)
9709 {
9710 procname = aux->strtab + sym_name;
9711 print_vma (fn, PREFIX_HEX);
9712 if (procname)
9713 {
9714 fputs (" <", stdout);
9715 fputs (procname, stdout);
9716 fputc ('>', stdout);
9717 }
9718 }
9719 else
9720 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
9721 fputc ('\n', stdout);
9722
9723 /* The GCC personality routines use the standard compact
9724 encoding, starting with one byte giving the number of
9725 words. */
9726 if (procname != NULL
9727 && (startswith (procname, "__gcc_personality_v0")
9728 || startswith (procname, "__gxx_personality_v0")
9729 || startswith (procname, "__gcj_personality_v0")
9730 || startswith (procname, "__gnu_objc_personality_v0")))
9731 {
9732 remaining = 0;
9733 more_words = 1;
9734 ADVANCE;
9735 if (!remaining)
9736 {
9737 printf (_(" [Truncated data]\n"));
9738 return false;
9739 }
9740 more_words = word >> 24;
9741 word <<= 8;
9742 remaining--;
9743 per_index = -1;
9744 }
9745 else
9746 return true;
9747 }
9748 else
9749 {
9750 /* ARM EHABI Section 6.3:
9751
9752 An exception-handling table entry for the compact model looks like:
9753
9754 31 30-28 27-24 23-0
9755 -- ----- ----- ----
9756 1 0 index Data for personalityRoutine[index] */
9757
9758 if (filedata->file_header.e_machine == EM_ARM
9759 && (word & 0x70000000))
9760 {
9761 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
9762 res = false;
9763 }
9764
9765 per_index = (word >> 24) & 0x7f;
9766 printf (_(" Compact model index: %d\n"), per_index);
9767 if (per_index == 0)
9768 {
9769 more_words = 0;
9770 word <<= 8;
9771 remaining--;
9772 }
9773 else if (per_index < 3)
9774 {
9775 more_words = (word >> 16) & 0xff;
9776 word <<= 16;
9777 remaining -= 2;
9778 }
9779 }
9780
9781 switch (filedata->file_header.e_machine)
9782 {
9783 case EM_ARM:
9784 if (per_index < 3)
9785 {
9786 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
9787 data_offset, data_sec, data_arm_sec))
9788 res = false;
9789 }
9790 else
9791 {
9792 warn (_("Unknown ARM compact model index encountered\n"));
9793 printf (_(" [reserved]\n"));
9794 res = false;
9795 }
9796 break;
9797
9798 case EM_TI_C6000:
9799 if (per_index < 3)
9800 {
9801 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
9802 data_offset, data_sec, data_arm_sec))
9803 res = false;
9804 }
9805 else if (per_index < 5)
9806 {
9807 if (((word >> 17) & 0x7f) == 0x7f)
9808 printf (_(" Restore stack from frame pointer\n"));
9809 else
9810 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
9811 printf (_(" Registers restored: "));
9812 if (per_index == 4)
9813 printf (" (compact) ");
9814 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
9815 putchar ('\n');
9816 printf (_(" Return register: %s\n"),
9817 tic6x_unwind_regnames[word & 0xf]);
9818 }
9819 else
9820 printf (_(" [reserved (%d)]\n"), per_index);
9821 break;
9822
9823 default:
9824 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
9825 filedata->file_header.e_machine);
9826 res = false;
9827 }
9828
9829 /* Decode the descriptors. Not implemented. */
9830
9831 return res;
9832}
9833
9834static bool
9835dump_arm_unwind (Filedata * filedata,
9836 struct arm_unw_aux_info * aux,
9837 Elf_Internal_Shdr * exidx_sec)
9838{
9839 struct arm_section exidx_arm_sec, extab_arm_sec;
9840 unsigned int i, exidx_len;
9841 unsigned long j, nfuns;
9842 bool res = true;
9843
9844 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
9845 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
9846 exidx_len = exidx_sec->sh_size / 8;
9847
9848 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9849 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9850 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9851 aux->funtab[nfuns++] = aux->symtab[j];
9852 aux->nfuns = nfuns;
9853 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9854
9855 for (i = 0; i < exidx_len; i++)
9856 {
9857 unsigned int exidx_fn, exidx_entry;
9858 struct absaddr fn_addr, entry_addr;
9859 bfd_vma fn;
9860
9861 fputc ('\n', stdout);
9862
9863 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9864 8 * i, & exidx_fn, & fn_addr, NULL)
9865 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
9866 8 * i + 4, & exidx_entry, & entry_addr, NULL))
9867 {
9868 free (aux->funtab);
9869 arm_free_section (& exidx_arm_sec);
9870 arm_free_section (& extab_arm_sec);
9871 return false;
9872 }
9873
9874 /* ARM EHABI, Section 5:
9875 An index table entry consists of 2 words.
9876 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
9877 if (exidx_fn & 0x80000000)
9878 {
9879 warn (_("corrupt index table entry: %x\n"), exidx_fn);
9880 res = false;
9881 }
9882
9883 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
9884
9885 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
9886 fputs (": ", stdout);
9887
9888 if (exidx_entry == 1)
9889 {
9890 print_vma (exidx_entry, PREFIX_HEX);
9891 fputs (" [cantunwind]\n", stdout);
9892 }
9893 else if (exidx_entry & 0x80000000)
9894 {
9895 print_vma (exidx_entry, PREFIX_HEX);
9896 fputc ('\n', stdout);
9897 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
9898 }
9899 else
9900 {
9901 bfd_vma table, table_offset = 0;
9902 Elf_Internal_Shdr *table_sec;
9903
9904 fputs ("@", stdout);
9905 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
9906 print_vma (table, PREFIX_HEX);
9907 printf ("\n");
9908
9909 /* Locate the matching .ARM.extab. */
9910 if (entry_addr.section != SHN_UNDEF
9911 && entry_addr.section < filedata->file_header.e_shnum)
9912 {
9913 table_sec = filedata->section_headers + entry_addr.section;
9914 table_offset = entry_addr.offset;
9915 /* PR 18879 */
9916 if (table_offset > table_sec->sh_size
9917 || ((bfd_signed_vma) table_offset) < 0)
9918 {
9919 warn (_("Unwind entry contains corrupt offset (0x%lx) into section %s\n"),
9920 (unsigned long) table_offset,
9921 printable_section_name (filedata, table_sec));
9922 res = false;
9923 continue;
9924 }
9925 }
9926 else
9927 {
9928 table_sec = find_section_by_address (filedata, table);
9929 if (table_sec != NULL)
9930 table_offset = table - table_sec->sh_addr;
9931 }
9932
9933 if (table_sec == NULL)
9934 {
9935 warn (_("Could not locate .ARM.extab section containing 0x%lx.\n"),
9936 (unsigned long) table);
9937 res = false;
9938 continue;
9939 }
9940
9941 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
9942 &extab_arm_sec))
9943 res = false;
9944 }
9945 }
9946
9947 printf ("\n");
9948
9949 free (aux->funtab);
9950 arm_free_section (&exidx_arm_sec);
9951 arm_free_section (&extab_arm_sec);
9952
9953 return res;
9954}
9955
9956/* Used for both ARM and C6X unwinding tables. */
9957
9958static bool
9959arm_process_unwind (Filedata * filedata)
9960{
9961 struct arm_unw_aux_info aux;
9962 Elf_Internal_Shdr *unwsec = NULL;
9963 Elf_Internal_Shdr *sec;
9964 unsigned long i;
9965 unsigned int sec_type;
9966 bool res = true;
9967
9968 switch (filedata->file_header.e_machine)
9969 {
9970 case EM_ARM:
9971 sec_type = SHT_ARM_EXIDX;
9972 break;
9973
9974 case EM_TI_C6000:
9975 sec_type = SHT_C6000_UNWIND;
9976 break;
9977
9978 default:
9979 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
9980 filedata->file_header.e_machine);
9981 return false;
9982 }
9983
9984 if (filedata->string_table == NULL)
9985 return false;
9986
9987 memset (& aux, 0, sizeof (aux));
9988 aux.filedata = filedata;
9989
9990 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9991 {
9992 if (sec->sh_type == SHT_SYMTAB)
9993 {
9994 if (aux.symtab)
9995 {
9996 error (_("Multiple symbol tables encountered\n"));
9997 free (aux.symtab);
9998 aux.symtab = NULL;
9999 free (aux.strtab);
10000 aux.strtab = NULL;
10001 }
10002 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
10003 &aux.strtab, &aux.strtab_size))
10004 return false;
10005 }
10006 else if (sec->sh_type == sec_type)
10007 unwsec = sec;
10008 }
10009
10010 if (unwsec == NULL)
10011 printf (_("\nThere are no unwind sections in this file.\n"));
10012 else
10013 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
10014 {
10015 if (sec->sh_type == sec_type)
10016 {
10017 unsigned long num_unwind = sec->sh_size / (2 * eh_addr_size);
10018 printf (ngettext ("\nUnwind section '%s' at offset 0x%lx "
10019 "contains %lu entry:\n",
10020 "\nUnwind section '%s' at offset 0x%lx "
10021 "contains %lu entries:\n",
10022 num_unwind),
10023 printable_section_name (filedata, sec),
10024 (unsigned long) sec->sh_offset,
10025 num_unwind);
10026
10027 if (! dump_arm_unwind (filedata, &aux, sec))
10028 res = false;
10029 }
10030 }
10031
10032 free (aux.symtab);
10033 free ((char *) aux.strtab);
10034
10035 return res;
10036}
10037
10038static bool
10039no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
10040{
10041 printf (_("No processor specific unwind information to decode\n"));
10042 return true;
10043}
10044
10045static bool
10046process_unwind (Filedata * filedata)
10047{
10048 struct unwind_handler
10049 {
10050 unsigned int machtype;
10051 bool (* handler)(Filedata *);
10052 } handlers[] =
10053 {
10054 { EM_ARM, arm_process_unwind },
10055 { EM_IA_64, ia64_process_unwind },
10056 { EM_PARISC, hppa_process_unwind },
10057 { EM_TI_C6000, arm_process_unwind },
10058 { EM_386, no_processor_specific_unwind },
10059 { EM_X86_64, no_processor_specific_unwind },
10060 { 0, NULL }
10061 };
10062 int i;
10063
10064 if (!do_unwind)
10065 return true;
10066
10067 for (i = 0; handlers[i].handler != NULL; i++)
10068 if (filedata->file_header.e_machine == handlers[i].machtype)
10069 return handlers[i].handler (filedata);
10070
10071 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
10072 get_machine_name (filedata->file_header.e_machine));
10073 return true;
10074}
10075
10076static void
10077dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
10078{
10079 switch (entry->d_tag)
10080 {
10081 case DT_AARCH64_BTI_PLT:
10082 case DT_AARCH64_PAC_PLT:
10083 break;
10084 default:
10085 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10086 break;
10087 }
10088 putchar ('\n');
10089}
10090
10091static void
10092dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
10093{
10094 switch (entry->d_tag)
10095 {
10096 case DT_MIPS_FLAGS:
10097 if (entry->d_un.d_val == 0)
10098 printf (_("NONE"));
10099 else
10100 {
10101 static const char * opts[] =
10102 {
10103 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
10104 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
10105 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
10106 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
10107 "RLD_ORDER_SAFE"
10108 };
10109 unsigned int cnt;
10110 bool first = true;
10111
10112 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
10113 if (entry->d_un.d_val & (1 << cnt))
10114 {
10115 printf ("%s%s", first ? "" : " ", opts[cnt]);
10116 first = false;
10117 }
10118 }
10119 break;
10120
10121 case DT_MIPS_IVERSION:
10122 if (valid_dynamic_name (filedata, entry->d_un.d_val))
10123 printf (_("Interface Version: %s"),
10124 get_dynamic_name (filedata, entry->d_un.d_val));
10125 else
10126 {
10127 char buf[40];
10128 sprintf_vma (buf, entry->d_un.d_ptr);
10129 /* Note: coded this way so that there is a single string for translation. */
10130 printf (_("<corrupt: %s>"), buf);
10131 }
10132 break;
10133
10134 case DT_MIPS_TIME_STAMP:
10135 {
10136 char timebuf[128];
10137 struct tm * tmp;
10138 time_t atime = entry->d_un.d_val;
10139
10140 tmp = gmtime (&atime);
10141 /* PR 17531: file: 6accc532. */
10142 if (tmp == NULL)
10143 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
10144 else
10145 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
10146 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
10147 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
10148 printf (_("Time Stamp: %s"), timebuf);
10149 }
10150 break;
10151
10152 case DT_MIPS_RLD_VERSION:
10153 case DT_MIPS_LOCAL_GOTNO:
10154 case DT_MIPS_CONFLICTNO:
10155 case DT_MIPS_LIBLISTNO:
10156 case DT_MIPS_SYMTABNO:
10157 case DT_MIPS_UNREFEXTNO:
10158 case DT_MIPS_HIPAGENO:
10159 case DT_MIPS_DELTA_CLASS_NO:
10160 case DT_MIPS_DELTA_INSTANCE_NO:
10161 case DT_MIPS_DELTA_RELOC_NO:
10162 case DT_MIPS_DELTA_SYM_NO:
10163 case DT_MIPS_DELTA_CLASSSYM_NO:
10164 case DT_MIPS_COMPACT_SIZE:
10165 print_vma (entry->d_un.d_val, DEC);
10166 break;
10167
10168 case DT_MIPS_XHASH:
10169 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10170 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10171 /* Falls through. */
10172
10173 default:
10174 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10175 }
10176 putchar ('\n');
10177}
10178
10179static void
10180dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
10181{
10182 switch (entry->d_tag)
10183 {
10184 case DT_HP_DLD_FLAGS:
10185 {
10186 static struct
10187 {
10188 long int bit;
10189 const char * str;
10190 }
10191 flags[] =
10192 {
10193 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
10194 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
10195 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
10196 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
10197 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
10198 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
10199 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
10200 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
10201 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
10202 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
10203 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
10204 { DT_HP_GST, "HP_GST" },
10205 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
10206 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
10207 { DT_HP_NODELETE, "HP_NODELETE" },
10208 { DT_HP_GROUP, "HP_GROUP" },
10209 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
10210 };
10211 bool first = true;
10212 size_t cnt;
10213 bfd_vma val = entry->d_un.d_val;
10214
10215 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
10216 if (val & flags[cnt].bit)
10217 {
10218 if (! first)
10219 putchar (' ');
10220 fputs (flags[cnt].str, stdout);
10221 first = false;
10222 val ^= flags[cnt].bit;
10223 }
10224
10225 if (val != 0 || first)
10226 {
10227 if (! first)
10228 putchar (' ');
10229 print_vma (val, HEX);
10230 }
10231 }
10232 break;
10233
10234 default:
10235 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10236 break;
10237 }
10238 putchar ('\n');
10239}
10240
10241#ifdef BFD64
10242
10243/* VMS vs Unix time offset and factor. */
10244
10245#define VMS_EPOCH_OFFSET 35067168000000000LL
10246#define VMS_GRANULARITY_FACTOR 10000000
10247#ifndef INT64_MIN
10248#define INT64_MIN (-9223372036854775807LL - 1)
10249#endif
10250
10251/* Display a VMS time in a human readable format. */
10252
10253static void
10254print_vms_time (bfd_int64_t vmstime)
10255{
10256 struct tm *tm = NULL;
10257 time_t unxtime;
10258
10259 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
10260 {
10261 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
10262 unxtime = vmstime;
10263 if (unxtime == vmstime)
10264 tm = gmtime (&unxtime);
10265 }
10266 if (tm != NULL)
10267 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
10268 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
10269 tm->tm_hour, tm->tm_min, tm->tm_sec);
10270}
10271#endif /* BFD64 */
10272
10273static void
10274dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
10275{
10276 switch (entry->d_tag)
10277 {
10278 case DT_IA_64_PLT_RESERVE:
10279 /* First 3 slots reserved. */
10280 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10281 printf (" -- ");
10282 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
10283 break;
10284
10285 case DT_IA_64_VMS_LINKTIME:
10286#ifdef BFD64
10287 print_vms_time (entry->d_un.d_val);
10288#endif
10289 break;
10290
10291 case DT_IA_64_VMS_LNKFLAGS:
10292 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10293 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
10294 printf (" CALL_DEBUG");
10295 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
10296 printf (" NOP0BUFS");
10297 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
10298 printf (" P0IMAGE");
10299 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
10300 printf (" MKTHREADS");
10301 if (entry->d_un.d_val & VMS_LF_UPCALLS)
10302 printf (" UPCALLS");
10303 if (entry->d_un.d_val & VMS_LF_IMGSTA)
10304 printf (" IMGSTA");
10305 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
10306 printf (" INITIALIZE");
10307 if (entry->d_un.d_val & VMS_LF_MAIN)
10308 printf (" MAIN");
10309 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
10310 printf (" EXE_INIT");
10311 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
10312 printf (" TBK_IN_IMG");
10313 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
10314 printf (" DBG_IN_IMG");
10315 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
10316 printf (" TBK_IN_DSF");
10317 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
10318 printf (" DBG_IN_DSF");
10319 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
10320 printf (" SIGNATURES");
10321 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
10322 printf (" REL_SEG_OFF");
10323 break;
10324
10325 default:
10326 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
10327 break;
10328 }
10329 putchar ('\n');
10330}
10331
10332static bool
10333get_32bit_dynamic_section (Filedata * filedata)
10334{
10335 Elf32_External_Dyn * edyn;
10336 Elf32_External_Dyn * ext;
10337 Elf_Internal_Dyn * entry;
10338
10339 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
10340 filedata->dynamic_addr, 1,
10341 filedata->dynamic_size,
10342 _("dynamic section"));
10343 if (!edyn)
10344 return false;
10345
10346 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10347 might not have the luxury of section headers. Look for the DT_NULL
10348 terminator to determine the number of entries. */
10349 for (ext = edyn, filedata->dynamic_nent = 0;
10350 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10351 ext++)
10352 {
10353 filedata->dynamic_nent++;
10354 if (BYTE_GET (ext->d_tag) == DT_NULL)
10355 break;
10356 }
10357
10358 filedata->dynamic_section
10359 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10360 if (filedata->dynamic_section == NULL)
10361 {
10362 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10363 (unsigned long) filedata->dynamic_nent);
10364 free (edyn);
10365 return false;
10366 }
10367
10368 for (ext = edyn, entry = filedata->dynamic_section;
10369 entry < filedata->dynamic_section + filedata->dynamic_nent;
10370 ext++, entry++)
10371 {
10372 entry->d_tag = BYTE_GET (ext->d_tag);
10373 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10374 }
10375
10376 free (edyn);
10377
10378 return true;
10379}
10380
10381static bool
10382get_64bit_dynamic_section (Filedata * filedata)
10383{
10384 Elf64_External_Dyn * edyn;
10385 Elf64_External_Dyn * ext;
10386 Elf_Internal_Dyn * entry;
10387
10388 /* Read in the data. */
10389 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
10390 filedata->dynamic_addr, 1,
10391 filedata->dynamic_size,
10392 _("dynamic section"));
10393 if (!edyn)
10394 return false;
10395
10396 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
10397 might not have the luxury of section headers. Look for the DT_NULL
10398 terminator to determine the number of entries. */
10399 for (ext = edyn, filedata->dynamic_nent = 0;
10400 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
10401 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
10402 ext++)
10403 {
10404 filedata->dynamic_nent++;
10405 if (BYTE_GET (ext->d_tag) == DT_NULL)
10406 break;
10407 }
10408
10409 filedata->dynamic_section
10410 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
10411 if (filedata->dynamic_section == NULL)
10412 {
10413 error (_("Out of memory allocating space for %lu dynamic entries\n"),
10414 (unsigned long) filedata->dynamic_nent);
10415 free (edyn);
10416 return false;
10417 }
10418
10419 /* Convert from external to internal formats. */
10420 for (ext = edyn, entry = filedata->dynamic_section;
10421 entry < filedata->dynamic_section + filedata->dynamic_nent;
10422 ext++, entry++)
10423 {
10424 entry->d_tag = BYTE_GET (ext->d_tag);
10425 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
10426 }
10427
10428 free (edyn);
10429
10430 return true;
10431}
10432
10433static bool
10434get_dynamic_section (Filedata *filedata)
10435{
10436 if (filedata->dynamic_section)
10437 return true;
10438
10439 if (is_32bit_elf)
10440 return get_32bit_dynamic_section (filedata);
10441 else
10442 return get_64bit_dynamic_section (filedata);
10443}
10444
10445static void
10446print_dynamic_flags (bfd_vma flags)
10447{
10448 bool first = true;
10449
10450 while (flags)
10451 {
10452 bfd_vma flag;
10453
10454 flag = flags & - flags;
10455 flags &= ~ flag;
10456
10457 if (first)
10458 first = false;
10459 else
10460 putc (' ', stdout);
10461
10462 switch (flag)
10463 {
10464 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
10465 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
10466 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
10467 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
10468 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
10469 default: fputs (_("unknown"), stdout); break;
10470 }
10471 }
10472 puts ("");
10473}
10474
10475static bfd_vma *
10476get_dynamic_data (Filedata * filedata, bfd_size_type number, unsigned int ent_size)
10477{
10478 unsigned char * e_data;
10479 bfd_vma * i_data;
10480
10481 /* If the size_t type is smaller than the bfd_size_type, eg because
10482 you are building a 32-bit tool on a 64-bit host, then make sure
10483 that when (number) is cast to (size_t) no information is lost. */
10484 if (sizeof (size_t) < sizeof (bfd_size_type)
10485 && (bfd_size_type) ((size_t) number) != number)
10486 {
10487 error (_("Size truncation prevents reading %s elements of size %u\n"),
10488 bfd_vmatoa ("u", number), ent_size);
10489 return NULL;
10490 }
10491
10492 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
10493 attempting to allocate memory when the read is bound to fail. */
10494 if (ent_size * number > filedata->file_size)
10495 {
10496 error (_("Invalid number of dynamic entries: %s\n"),
10497 bfd_vmatoa ("u", number));
10498 return NULL;
10499 }
10500
10501 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
10502 if (e_data == NULL)
10503 {
10504 error (_("Out of memory reading %s dynamic entries\n"),
10505 bfd_vmatoa ("u", number));
10506 return NULL;
10507 }
10508
10509 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
10510 {
10511 error (_("Unable to read in %s bytes of dynamic data\n"),
10512 bfd_vmatoa ("u", number * ent_size));
10513 free (e_data);
10514 return NULL;
10515 }
10516
10517 i_data = (bfd_vma *) cmalloc ((size_t) number, sizeof (*i_data));
10518 if (i_data == NULL)
10519 {
10520 error (_("Out of memory allocating space for %s dynamic entries\n"),
10521 bfd_vmatoa ("u", number));
10522 free (e_data);
10523 return NULL;
10524 }
10525
10526 while (number--)
10527 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
10528
10529 free (e_data);
10530
10531 return i_data;
10532}
10533
10534static unsigned long
10535get_num_dynamic_syms (Filedata * filedata)
10536{
10537 unsigned long num_of_syms = 0;
10538
10539 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
10540 return num_of_syms;
10541
10542 if (filedata->dynamic_info[DT_HASH])
10543 {
10544 unsigned char nb[8];
10545 unsigned char nc[8];
10546 unsigned int hash_ent_size = 4;
10547
10548 if ((filedata->file_header.e_machine == EM_ALPHA
10549 || filedata->file_header.e_machine == EM_S390
10550 || filedata->file_header.e_machine == EM_S390_OLD)
10551 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
10552 hash_ent_size = 8;
10553
10554 if (fseek (filedata->handle,
10555 (filedata->archive_file_offset
10556 + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH],
10557 sizeof nb + sizeof nc)),
10558 SEEK_SET))
10559 {
10560 error (_("Unable to seek to start of dynamic information\n"));
10561 goto no_hash;
10562 }
10563
10564 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
10565 {
10566 error (_("Failed to read in number of buckets\n"));
10567 goto no_hash;
10568 }
10569
10570 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
10571 {
10572 error (_("Failed to read in number of chains\n"));
10573 goto no_hash;
10574 }
10575
10576 filedata->nbuckets = byte_get (nb, hash_ent_size);
10577 filedata->nchains = byte_get (nc, hash_ent_size);
10578
10579 if (filedata->nbuckets != 0 && filedata->nchains != 0)
10580 {
10581 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
10582 hash_ent_size);
10583 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
10584 hash_ent_size);
10585
10586 if (filedata->buckets != NULL && filedata->chains != NULL)
10587 num_of_syms = filedata->nchains;
10588 }
10589 no_hash:
10590 if (num_of_syms == 0)
10591 {
10592 free (filedata->buckets);
10593 filedata->buckets = NULL;
10594 free (filedata->chains);
10595 filedata->chains = NULL;
10596 filedata->nbuckets = 0;
10597 }
10598 }
10599
10600 if (filedata->dynamic_info_DT_GNU_HASH)
10601 {
10602 unsigned char nb[16];
10603 bfd_vma i, maxchain = 0xffffffff, bitmaskwords;
10604 bfd_vma buckets_vma;
10605 unsigned long hn;
10606
10607 if (fseek (filedata->handle,
10608 (filedata->archive_file_offset
10609 + offset_from_vma (filedata,
10610 filedata->dynamic_info_DT_GNU_HASH,
10611 sizeof nb)),
10612 SEEK_SET))
10613 {
10614 error (_("Unable to seek to start of dynamic information\n"));
10615 goto no_gnu_hash;
10616 }
10617
10618 if (fread (nb, 16, 1, filedata->handle) != 1)
10619 {
10620 error (_("Failed to read in number of buckets\n"));
10621 goto no_gnu_hash;
10622 }
10623
10624 filedata->ngnubuckets = byte_get (nb, 4);
10625 filedata->gnusymidx = byte_get (nb + 4, 4);
10626 bitmaskwords = byte_get (nb + 8, 4);
10627 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
10628 if (is_32bit_elf)
10629 buckets_vma += bitmaskwords * 4;
10630 else
10631 buckets_vma += bitmaskwords * 8;
10632
10633 if (fseek (filedata->handle,
10634 (filedata->archive_file_offset
10635 + offset_from_vma (filedata, buckets_vma, 4)),
10636 SEEK_SET))
10637 {
10638 error (_("Unable to seek to start of dynamic information\n"));
10639 goto no_gnu_hash;
10640 }
10641
10642 filedata->gnubuckets
10643 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
10644
10645 if (filedata->gnubuckets == NULL)
10646 goto no_gnu_hash;
10647
10648 for (i = 0; i < filedata->ngnubuckets; i++)
10649 if (filedata->gnubuckets[i] != 0)
10650 {
10651 if (filedata->gnubuckets[i] < filedata->gnusymidx)
10652 goto no_gnu_hash;
10653
10654 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
10655 maxchain = filedata->gnubuckets[i];
10656 }
10657
10658 if (maxchain == 0xffffffff)
10659 goto no_gnu_hash;
10660
10661 maxchain -= filedata->gnusymidx;
10662
10663 if (fseek (filedata->handle,
10664 (filedata->archive_file_offset
10665 + offset_from_vma (filedata,
10666 buckets_vma + 4 * (filedata->ngnubuckets
10667 + maxchain),
10668 4)),
10669 SEEK_SET))
10670 {
10671 error (_("Unable to seek to start of dynamic information\n"));
10672 goto no_gnu_hash;
10673 }
10674
10675 do
10676 {
10677 if (fread (nb, 4, 1, filedata->handle) != 1)
10678 {
10679 error (_("Failed to determine last chain length\n"));
10680 goto no_gnu_hash;
10681 }
10682
10683 if (maxchain + 1 == 0)
10684 goto no_gnu_hash;
10685
10686 ++maxchain;
10687 }
10688 while ((byte_get (nb, 4) & 1) == 0);
10689
10690 if (fseek (filedata->handle,
10691 (filedata->archive_file_offset
10692 + offset_from_vma (filedata, (buckets_vma
10693 + 4 * filedata->ngnubuckets),
10694 4)),
10695 SEEK_SET))
10696 {
10697 error (_("Unable to seek to start of dynamic information\n"));
10698 goto no_gnu_hash;
10699 }
10700
10701 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
10702 filedata->ngnuchains = maxchain;
10703
10704 if (filedata->gnuchains == NULL)
10705 goto no_gnu_hash;
10706
10707 if (filedata->dynamic_info_DT_MIPS_XHASH)
10708 {
10709 if (fseek (filedata->handle,
10710 (filedata->archive_file_offset
10711 + offset_from_vma (filedata, (buckets_vma
10712 + 4 * (filedata->ngnubuckets
10713 + maxchain)), 4)),
10714 SEEK_SET))
10715 {
10716 error (_("Unable to seek to start of dynamic information\n"));
10717 goto no_gnu_hash;
10718 }
10719
10720 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
10721 if (filedata->mipsxlat == NULL)
10722 goto no_gnu_hash;
10723 }
10724
10725 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
10726 if (filedata->gnubuckets[hn] != 0)
10727 {
10728 bfd_vma si = filedata->gnubuckets[hn];
10729 bfd_vma off = si - filedata->gnusymidx;
10730
10731 do
10732 {
10733 if (filedata->dynamic_info_DT_MIPS_XHASH)
10734 {
10735 if (off < filedata->ngnuchains
10736 && filedata->mipsxlat[off] >= num_of_syms)
10737 num_of_syms = filedata->mipsxlat[off] + 1;
10738 }
10739 else
10740 {
10741 if (si >= num_of_syms)
10742 num_of_syms = si + 1;
10743 }
10744 si++;
10745 }
10746 while (off < filedata->ngnuchains
10747 && (filedata->gnuchains[off++] & 1) == 0);
10748 }
10749
10750 if (num_of_syms == 0)
10751 {
10752 no_gnu_hash:
10753 free (filedata->mipsxlat);
10754 filedata->mipsxlat = NULL;
10755 free (filedata->gnuchains);
10756 filedata->gnuchains = NULL;
10757 free (filedata->gnubuckets);
10758 filedata->gnubuckets = NULL;
10759 filedata->ngnubuckets = 0;
10760 filedata->ngnuchains = 0;
10761 }
10762 }
10763
10764 return num_of_syms;
10765}
10766
10767/* Parse and display the contents of the dynamic section. */
10768
10769static bool
10770process_dynamic_section (Filedata * filedata)
10771{
10772 Elf_Internal_Dyn * entry;
10773
10774 if (filedata->dynamic_size <= 1)
10775 {
10776 if (do_dynamic)
10777 {
10778 if (filedata->is_separate)
10779 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
10780 filedata->file_name);
10781 else
10782 printf (_("\nThere is no dynamic section in this file.\n"));
10783 }
10784
10785 return true;
10786 }
10787
10788 if (!get_dynamic_section (filedata))
10789 return false;
10790
10791 /* Find the appropriate symbol table. */
10792 if (filedata->dynamic_symbols == NULL || do_histogram)
10793 {
10794 unsigned long num_of_syms;
10795
10796 for (entry = filedata->dynamic_section;
10797 entry < filedata->dynamic_section + filedata->dynamic_nent;
10798 ++entry)
10799 if (entry->d_tag == DT_SYMTAB)
10800 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
10801 else if (entry->d_tag == DT_SYMENT)
10802 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
10803 else if (entry->d_tag == DT_HASH)
10804 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
10805 else if (entry->d_tag == DT_GNU_HASH)
10806 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10807 else if ((filedata->file_header.e_machine == EM_MIPS
10808 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
10809 && entry->d_tag == DT_MIPS_XHASH)
10810 {
10811 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
10812 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
10813 }
10814
10815 num_of_syms = get_num_dynamic_syms (filedata);
10816
10817 if (num_of_syms != 0
10818 && filedata->dynamic_symbols == NULL
10819 && filedata->dynamic_info[DT_SYMTAB]
10820 && filedata->dynamic_info[DT_SYMENT])
10821 {
10822 Elf_Internal_Phdr *seg;
10823 bfd_vma vma = filedata->dynamic_info[DT_SYMTAB];
10824
10825 if (! get_program_headers (filedata))
10826 {
10827 error (_("Cannot interpret virtual addresses "
10828 "without program headers.\n"));
10829 return false;
10830 }
10831
10832 for (seg = filedata->program_headers;
10833 seg < filedata->program_headers + filedata->file_header.e_phnum;
10834 ++seg)
10835 {
10836 if (seg->p_type != PT_LOAD)
10837 continue;
10838
10839 if (seg->p_offset + seg->p_filesz > filedata->file_size)
10840 {
10841 /* See PR 21379 for a reproducer. */
10842 error (_("Invalid PT_LOAD entry\n"));
10843 return false;
10844 }
10845
10846 if (vma >= (seg->p_vaddr & -seg->p_align)
10847 && vma < seg->p_vaddr + seg->p_filesz)
10848 {
10849 /* Since we do not know how big the symbol table is,
10850 we default to reading in up to the end of PT_LOAD
10851 segment and processing that. This is overkill, I
10852 know, but it should work. */
10853 Elf_Internal_Shdr section;
10854 section.sh_offset = (vma - seg->p_vaddr
10855 + seg->p_offset);
10856 section.sh_size = (num_of_syms
10857 * filedata->dynamic_info[DT_SYMENT]);
10858 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
10859
10860 if (do_checks
10861 && filedata->dynamic_symtab_section != NULL
10862 && ((filedata->dynamic_symtab_section->sh_offset
10863 != section.sh_offset)
10864 || (filedata->dynamic_symtab_section->sh_size
10865 != section.sh_size)
10866 || (filedata->dynamic_symtab_section->sh_entsize
10867 != section.sh_entsize)))
10868 warn (_("\
10869the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
10870
10871 section.sh_name = filedata->string_table_length;
10872 filedata->dynamic_symbols
10873 = get_elf_symbols (filedata, &section,
10874 &filedata->num_dynamic_syms);
10875 if (filedata->dynamic_symbols == NULL
10876 || filedata->num_dynamic_syms != num_of_syms)
10877 {
10878 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
10879 return false;
10880 }
10881 break;
10882 }
10883 }
10884 }
10885 }
10886
10887 /* Similarly find a string table. */
10888 if (filedata->dynamic_strings == NULL)
10889 for (entry = filedata->dynamic_section;
10890 entry < filedata->dynamic_section + filedata->dynamic_nent;
10891 ++entry)
10892 {
10893 if (entry->d_tag == DT_STRTAB)
10894 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
10895
10896 if (entry->d_tag == DT_STRSZ)
10897 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
10898
10899 if (filedata->dynamic_info[DT_STRTAB]
10900 && filedata->dynamic_info[DT_STRSZ])
10901 {
10902 unsigned long offset;
10903 bfd_size_type str_tab_len = filedata->dynamic_info[DT_STRSZ];
10904
10905 offset = offset_from_vma (filedata,
10906 filedata->dynamic_info[DT_STRTAB],
10907 str_tab_len);
10908 if (do_checks
10909 && filedata->dynamic_strtab_section
10910 && ((filedata->dynamic_strtab_section->sh_offset
10911 != (file_ptr) offset)
10912 || (filedata->dynamic_strtab_section->sh_size
10913 != str_tab_len)))
10914 warn (_("\
10915the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
10916
10917 filedata->dynamic_strings
10918 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
10919 _("dynamic string table"));
10920 if (filedata->dynamic_strings == NULL)
10921 {
10922 error (_("Corrupt DT_STRTAB dynamic entry\n"));
10923 break;
10924 }
10925
10926 filedata->dynamic_strings_length = str_tab_len;
10927 break;
10928 }
10929 }
10930
10931 /* And find the syminfo section if available. */
10932 if (filedata->dynamic_syminfo == NULL)
10933 {
10934 unsigned long syminsz = 0;
10935
10936 for (entry = filedata->dynamic_section;
10937 entry < filedata->dynamic_section + filedata->dynamic_nent;
10938 ++entry)
10939 {
10940 if (entry->d_tag == DT_SYMINENT)
10941 {
10942 /* Note: these braces are necessary to avoid a syntax
10943 error from the SunOS4 C compiler. */
10944 /* PR binutils/17531: A corrupt file can trigger this test.
10945 So do not use an assert, instead generate an error message. */
10946 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
10947 error (_("Bad value (%d) for SYMINENT entry\n"),
10948 (int) entry->d_un.d_val);
10949 }
10950 else if (entry->d_tag == DT_SYMINSZ)
10951 syminsz = entry->d_un.d_val;
10952 else if (entry->d_tag == DT_SYMINFO)
10953 filedata->dynamic_syminfo_offset
10954 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
10955 }
10956
10957 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
10958 {
10959 Elf_External_Syminfo * extsyminfo;
10960 Elf_External_Syminfo * extsym;
10961 Elf_Internal_Syminfo * syminfo;
10962
10963 /* There is a syminfo section. Read the data. */
10964 extsyminfo = (Elf_External_Syminfo *)
10965 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
10966 1, syminsz, _("symbol information"));
10967 if (!extsyminfo)
10968 return false;
10969
10970 if (filedata->dynamic_syminfo != NULL)
10971 {
10972 error (_("Multiple dynamic symbol information sections found\n"));
10973 free (filedata->dynamic_syminfo);
10974 }
10975 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
10976 if (filedata->dynamic_syminfo == NULL)
10977 {
10978 error (_("Out of memory allocating %lu bytes "
10979 "for dynamic symbol info\n"),
10980 (unsigned long) syminsz);
10981 return false;
10982 }
10983
10984 filedata->dynamic_syminfo_nent
10985 = syminsz / sizeof (Elf_External_Syminfo);
10986 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
10987 syminfo < (filedata->dynamic_syminfo
10988 + filedata->dynamic_syminfo_nent);
10989 ++syminfo, ++extsym)
10990 {
10991 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
10992 syminfo->si_flags = BYTE_GET (extsym->si_flags);
10993 }
10994
10995 free (extsyminfo);
10996 }
10997 }
10998
10999 if (do_dynamic && filedata->dynamic_addr)
11000 {
11001 if (filedata->is_separate)
11002 printf (ngettext ("\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entry:\n",
11003 "\nIn linked file '%s' the dynamic section at offset 0x%lx contains %lu entries:\n",
11004 (unsigned long) filedata->dynamic_nent),
11005 filedata->file_name,
11006 filedata->dynamic_addr,
11007 (unsigned long) filedata->dynamic_nent);
11008 else
11009 printf (ngettext ("\nDynamic section at offset 0x%lx contains %lu entry:\n",
11010 "\nDynamic section at offset 0x%lx contains %lu entries:\n",
11011 (unsigned long) filedata->dynamic_nent),
11012 filedata->dynamic_addr,
11013 (unsigned long) filedata->dynamic_nent);
11014 }
11015 if (do_dynamic)
11016 printf (_(" Tag Type Name/Value\n"));
11017
11018 for (entry = filedata->dynamic_section;
11019 entry < filedata->dynamic_section + filedata->dynamic_nent;
11020 entry++)
11021 {
11022 if (do_dynamic)
11023 {
11024 const char * dtype;
11025
11026 putchar (' ');
11027 print_vma (entry->d_tag, FULL_HEX);
11028 dtype = get_dynamic_type (filedata, entry->d_tag);
11029 printf (" (%s)%*s", dtype,
11030 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
11031 }
11032
11033 switch (entry->d_tag)
11034 {
11035 case DT_FLAGS:
11036 if (do_dynamic)
11037 print_dynamic_flags (entry->d_un.d_val);
11038 break;
11039
11040 case DT_AUXILIARY:
11041 case DT_FILTER:
11042 case DT_CONFIG:
11043 case DT_DEPAUDIT:
11044 case DT_AUDIT:
11045 if (do_dynamic)
11046 {
11047 switch (entry->d_tag)
11048 {
11049 case DT_AUXILIARY:
11050 printf (_("Auxiliary library"));
11051 break;
11052
11053 case DT_FILTER:
11054 printf (_("Filter library"));
11055 break;
11056
11057 case DT_CONFIG:
11058 printf (_("Configuration file"));
11059 break;
11060
11061 case DT_DEPAUDIT:
11062 printf (_("Dependency audit library"));
11063 break;
11064
11065 case DT_AUDIT:
11066 printf (_("Audit library"));
11067 break;
11068 }
11069
11070 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11071 printf (": [%s]\n",
11072 get_dynamic_name (filedata, entry->d_un.d_val));
11073 else
11074 {
11075 printf (": ");
11076 print_vma (entry->d_un.d_val, PREFIX_HEX);
11077 putchar ('\n');
11078 }
11079 }
11080 break;
11081
11082 case DT_FEATURE:
11083 if (do_dynamic)
11084 {
11085 printf (_("Flags:"));
11086
11087 if (entry->d_un.d_val == 0)
11088 printf (_(" None\n"));
11089 else
11090 {
11091 unsigned long int val = entry->d_un.d_val;
11092
11093 if (val & DTF_1_PARINIT)
11094 {
11095 printf (" PARINIT");
11096 val ^= DTF_1_PARINIT;
11097 }
11098 if (val & DTF_1_CONFEXP)
11099 {
11100 printf (" CONFEXP");
11101 val ^= DTF_1_CONFEXP;
11102 }
11103 if (val != 0)
11104 printf (" %lx", val);
11105 puts ("");
11106 }
11107 }
11108 break;
11109
11110 case DT_POSFLAG_1:
11111 if (do_dynamic)
11112 {
11113 printf (_("Flags:"));
11114
11115 if (entry->d_un.d_val == 0)
11116 printf (_(" None\n"));
11117 else
11118 {
11119 unsigned long int val = entry->d_un.d_val;
11120
11121 if (val & DF_P1_LAZYLOAD)
11122 {
11123 printf (" LAZYLOAD");
11124 val ^= DF_P1_LAZYLOAD;
11125 }
11126 if (val & DF_P1_GROUPPERM)
11127 {
11128 printf (" GROUPPERM");
11129 val ^= DF_P1_GROUPPERM;
11130 }
11131 if (val != 0)
11132 printf (" %lx", val);
11133 puts ("");
11134 }
11135 }
11136 break;
11137
11138 case DT_FLAGS_1:
11139 if (do_dynamic)
11140 {
11141 printf (_("Flags:"));
11142 if (entry->d_un.d_val == 0)
11143 printf (_(" None\n"));
11144 else
11145 {
11146 unsigned long int val = entry->d_un.d_val;
11147
11148 if (val & DF_1_NOW)
11149 {
11150 printf (" NOW");
11151 val ^= DF_1_NOW;
11152 }
11153 if (val & DF_1_GLOBAL)
11154 {
11155 printf (" GLOBAL");
11156 val ^= DF_1_GLOBAL;
11157 }
11158 if (val & DF_1_GROUP)
11159 {
11160 printf (" GROUP");
11161 val ^= DF_1_GROUP;
11162 }
11163 if (val & DF_1_NODELETE)
11164 {
11165 printf (" NODELETE");
11166 val ^= DF_1_NODELETE;
11167 }
11168 if (val & DF_1_LOADFLTR)
11169 {
11170 printf (" LOADFLTR");
11171 val ^= DF_1_LOADFLTR;
11172 }
11173 if (val & DF_1_INITFIRST)
11174 {
11175 printf (" INITFIRST");
11176 val ^= DF_1_INITFIRST;
11177 }
11178 if (val & DF_1_NOOPEN)
11179 {
11180 printf (" NOOPEN");
11181 val ^= DF_1_NOOPEN;
11182 }
11183 if (val & DF_1_ORIGIN)
11184 {
11185 printf (" ORIGIN");
11186 val ^= DF_1_ORIGIN;
11187 }
11188 if (val & DF_1_DIRECT)
11189 {
11190 printf (" DIRECT");
11191 val ^= DF_1_DIRECT;
11192 }
11193 if (val & DF_1_TRANS)
11194 {
11195 printf (" TRANS");
11196 val ^= DF_1_TRANS;
11197 }
11198 if (val & DF_1_INTERPOSE)
11199 {
11200 printf (" INTERPOSE");
11201 val ^= DF_1_INTERPOSE;
11202 }
11203 if (val & DF_1_NODEFLIB)
11204 {
11205 printf (" NODEFLIB");
11206 val ^= DF_1_NODEFLIB;
11207 }
11208 if (val & DF_1_NODUMP)
11209 {
11210 printf (" NODUMP");
11211 val ^= DF_1_NODUMP;
11212 }
11213 if (val & DF_1_CONFALT)
11214 {
11215 printf (" CONFALT");
11216 val ^= DF_1_CONFALT;
11217 }
11218 if (val & DF_1_ENDFILTEE)
11219 {
11220 printf (" ENDFILTEE");
11221 val ^= DF_1_ENDFILTEE;
11222 }
11223 if (val & DF_1_DISPRELDNE)
11224 {
11225 printf (" DISPRELDNE");
11226 val ^= DF_1_DISPRELDNE;
11227 }
11228 if (val & DF_1_DISPRELPND)
11229 {
11230 printf (" DISPRELPND");
11231 val ^= DF_1_DISPRELPND;
11232 }
11233 if (val & DF_1_NODIRECT)
11234 {
11235 printf (" NODIRECT");
11236 val ^= DF_1_NODIRECT;
11237 }
11238 if (val & DF_1_IGNMULDEF)
11239 {
11240 printf (" IGNMULDEF");
11241 val ^= DF_1_IGNMULDEF;
11242 }
11243 if (val & DF_1_NOKSYMS)
11244 {
11245 printf (" NOKSYMS");
11246 val ^= DF_1_NOKSYMS;
11247 }
11248 if (val & DF_1_NOHDR)
11249 {
11250 printf (" NOHDR");
11251 val ^= DF_1_NOHDR;
11252 }
11253 if (val & DF_1_EDITED)
11254 {
11255 printf (" EDITED");
11256 val ^= DF_1_EDITED;
11257 }
11258 if (val & DF_1_NORELOC)
11259 {
11260 printf (" NORELOC");
11261 val ^= DF_1_NORELOC;
11262 }
11263 if (val & DF_1_SYMINTPOSE)
11264 {
11265 printf (" SYMINTPOSE");
11266 val ^= DF_1_SYMINTPOSE;
11267 }
11268 if (val & DF_1_GLOBAUDIT)
11269 {
11270 printf (" GLOBAUDIT");
11271 val ^= DF_1_GLOBAUDIT;
11272 }
11273 if (val & DF_1_SINGLETON)
11274 {
11275 printf (" SINGLETON");
11276 val ^= DF_1_SINGLETON;
11277 }
11278 if (val & DF_1_STUB)
11279 {
11280 printf (" STUB");
11281 val ^= DF_1_STUB;
11282 }
11283 if (val & DF_1_PIE)
11284 {
11285 printf (" PIE");
11286 val ^= DF_1_PIE;
11287 }
11288 if (val & DF_1_KMOD)
11289 {
11290 printf (" KMOD");
11291 val ^= DF_1_KMOD;
11292 }
11293 if (val & DF_1_WEAKFILTER)
11294 {
11295 printf (" WEAKFILTER");
11296 val ^= DF_1_WEAKFILTER;
11297 }
11298 if (val & DF_1_NOCOMMON)
11299 {
11300 printf (" NOCOMMON");
11301 val ^= DF_1_NOCOMMON;
11302 }
11303 if (val != 0)
11304 printf (" %lx", val);
11305 puts ("");
11306 }
11307 }
11308 break;
11309
11310 case DT_PLTREL:
11311 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11312 if (do_dynamic)
11313 puts (get_dynamic_type (filedata, entry->d_un.d_val));
11314 break;
11315
11316 case DT_NULL :
11317 case DT_NEEDED :
11318 case DT_PLTGOT :
11319 case DT_HASH :
11320 case DT_STRTAB :
11321 case DT_SYMTAB :
11322 case DT_RELA :
11323 case DT_INIT :
11324 case DT_FINI :
11325 case DT_SONAME :
11326 case DT_RPATH :
11327 case DT_SYMBOLIC:
11328 case DT_REL :
11329 case DT_DEBUG :
11330 case DT_TEXTREL :
11331 case DT_JMPREL :
11332 case DT_RUNPATH :
11333 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11334
11335 if (do_dynamic)
11336 {
11337 const char *name;
11338
11339 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11340 name = get_dynamic_name (filedata, entry->d_un.d_val);
11341 else
11342 name = NULL;
11343
11344 if (name)
11345 {
11346 switch (entry->d_tag)
11347 {
11348 case DT_NEEDED:
11349 printf (_("Shared library: [%s]"), name);
11350
11351 if (filedata->program_interpreter
11352 && streq (name, filedata->program_interpreter))
11353 printf (_(" program interpreter"));
11354 break;
11355
11356 case DT_SONAME:
11357 printf (_("Library soname: [%s]"), name);
11358 break;
11359
11360 case DT_RPATH:
11361 printf (_("Library rpath: [%s]"), name);
11362 break;
11363
11364 case DT_RUNPATH:
11365 printf (_("Library runpath: [%s]"), name);
11366 break;
11367
11368 default:
11369 print_vma (entry->d_un.d_val, PREFIX_HEX);
11370 break;
11371 }
11372 }
11373 else
11374 print_vma (entry->d_un.d_val, PREFIX_HEX);
11375
11376 putchar ('\n');
11377 }
11378 break;
11379
11380 case DT_PLTRELSZ:
11381 case DT_RELASZ :
11382 case DT_STRSZ :
11383 case DT_RELSZ :
11384 case DT_RELAENT :
11385 case DT_SYMENT :
11386 case DT_RELENT :
11387 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
11388 /* Fall through. */
11389 case DT_PLTPADSZ:
11390 case DT_MOVEENT :
11391 case DT_MOVESZ :
11392 case DT_RELRENT :
11393 case DT_RELRSZ :
11394 case DT_PREINIT_ARRAYSZ:
11395 case DT_INIT_ARRAYSZ:
11396 case DT_FINI_ARRAYSZ:
11397 case DT_GNU_CONFLICTSZ:
11398 case DT_GNU_LIBLISTSZ:
11399 if (do_dynamic)
11400 {
11401 print_vma (entry->d_un.d_val, UNSIGNED);
11402 printf (_(" (bytes)\n"));
11403 }
11404 break;
11405
11406 case DT_VERDEFNUM:
11407 case DT_VERNEEDNUM:
11408 case DT_RELACOUNT:
11409 case DT_RELCOUNT:
11410 if (do_dynamic)
11411 {
11412 print_vma (entry->d_un.d_val, UNSIGNED);
11413 putchar ('\n');
11414 }
11415 break;
11416
11417 case DT_SYMINSZ:
11418 case DT_SYMINENT:
11419 case DT_SYMINFO:
11420 case DT_USED:
11421 case DT_INIT_ARRAY:
11422 case DT_FINI_ARRAY:
11423 if (do_dynamic)
11424 {
11425 if (entry->d_tag == DT_USED
11426 && valid_dynamic_name (filedata, entry->d_un.d_val))
11427 {
11428 const char *name
11429 = get_dynamic_name (filedata, entry->d_un.d_val);
11430
11431 if (*name)
11432 {
11433 printf (_("Not needed object: [%s]\n"), name);
11434 break;
11435 }
11436 }
11437
11438 print_vma (entry->d_un.d_val, PREFIX_HEX);
11439 putchar ('\n');
11440 }
11441 break;
11442
11443 case DT_BIND_NOW:
11444 /* The value of this entry is ignored. */
11445 if (do_dynamic)
11446 putchar ('\n');
11447 break;
11448
11449 case DT_GNU_PRELINKED:
11450 if (do_dynamic)
11451 {
11452 struct tm * tmp;
11453 time_t atime = entry->d_un.d_val;
11454
11455 tmp = gmtime (&atime);
11456 /* PR 17533 file: 041-1244816-0.004. */
11457 if (tmp == NULL)
11458 printf (_("<corrupt time val: %lx"),
11459 (unsigned long) atime);
11460 else
11461 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
11462 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11463 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
11464
11465 }
11466 break;
11467
11468 case DT_GNU_HASH:
11469 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11470 if (do_dynamic)
11471 {
11472 print_vma (entry->d_un.d_val, PREFIX_HEX);
11473 putchar ('\n');
11474 }
11475 break;
11476
11477 case DT_GNU_FLAGS_1:
11478 if (do_dynamic)
11479 {
11480 printf (_("Flags:"));
11481 if (entry->d_un.d_val == 0)
11482 printf (_(" None\n"));
11483 else
11484 {
11485 unsigned long int val = entry->d_un.d_val;
11486
11487 if (val & DF_GNU_1_UNIQUE)
11488 {
11489 printf (" UNIQUE");
11490 val ^= DF_GNU_1_UNIQUE;
11491 }
11492 if (val != 0)
11493 printf (" %lx", val);
11494 puts ("");
11495 }
11496 }
11497 break;
11498
11499 default:
11500 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
11501 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
11502 = entry->d_un.d_val;
11503
11504 if (do_dynamic)
11505 {
11506 switch (filedata->file_header.e_machine)
11507 {
11508 case EM_AARCH64:
11509 dynamic_section_aarch64_val (entry);
11510 break;
11511 case EM_MIPS:
11512 case EM_MIPS_RS3_LE:
11513 dynamic_section_mips_val (filedata, entry);
11514 break;
11515 case EM_PARISC:
11516 dynamic_section_parisc_val (entry);
11517 break;
11518 case EM_IA_64:
11519 dynamic_section_ia64_val (entry);
11520 break;
11521 default:
11522 print_vma (entry->d_un.d_val, PREFIX_HEX);
11523 putchar ('\n');
11524 }
11525 }
11526 break;
11527 }
11528 }
11529
11530 return true;
11531}
11532
11533static char *
11534get_ver_flags (unsigned int flags)
11535{
11536 static char buff[128];
11537
11538 buff[0] = 0;
11539
11540 if (flags == 0)
11541 return _("none");
11542
11543 if (flags & VER_FLG_BASE)
11544 strcat (buff, "BASE");
11545
11546 if (flags & VER_FLG_WEAK)
11547 {
11548 if (flags & VER_FLG_BASE)
11549 strcat (buff, " | ");
11550
11551 strcat (buff, "WEAK");
11552 }
11553
11554 if (flags & VER_FLG_INFO)
11555 {
11556 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
11557 strcat (buff, " | ");
11558
11559 strcat (buff, "INFO");
11560 }
11561
11562 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11563 {
11564 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
11565 strcat (buff, " | ");
11566
11567 strcat (buff, _("<unknown>"));
11568 }
11569
11570 return buff;
11571}
11572
11573/* Display the contents of the version sections. */
11574
11575static bool
11576process_version_sections (Filedata * filedata)
11577{
11578 Elf_Internal_Shdr * section;
11579 unsigned i;
11580 bool found = false;
11581
11582 if (! do_version)
11583 return true;
11584
11585 for (i = 0, section = filedata->section_headers;
11586 i < filedata->file_header.e_shnum;
11587 i++, section++)
11588 {
11589 switch (section->sh_type)
11590 {
11591 case SHT_GNU_verdef:
11592 {
11593 Elf_External_Verdef * edefs;
11594 unsigned long idx;
11595 unsigned long cnt;
11596 char * endbuf;
11597
11598 found = true;
11599
11600 if (filedata->is_separate)
11601 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
11602 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
11603 section->sh_info),
11604 filedata->file_name,
11605 printable_section_name (filedata, section),
11606 section->sh_info);
11607 else
11608 printf (ngettext ("\nVersion definition section '%s' "
11609 "contains %u entry:\n",
11610 "\nVersion definition section '%s' "
11611 "contains %u entries:\n",
11612 section->sh_info),
11613 printable_section_name (filedata, section),
11614 section->sh_info);
11615
11616 printf (_(" Addr: 0x"));
11617 printf_vma (section->sh_addr);
11618 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11619 (unsigned long) section->sh_offset, section->sh_link,
11620 printable_section_name_from_index (filedata, section->sh_link));
11621
11622 edefs = (Elf_External_Verdef *)
11623 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
11624 _("version definition section"));
11625 if (!edefs)
11626 break;
11627 endbuf = (char *) edefs + section->sh_size;
11628
11629 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
11630 {
11631 char * vstart;
11632 Elf_External_Verdef * edef;
11633 Elf_Internal_Verdef ent;
11634 Elf_External_Verdaux * eaux;
11635 Elf_Internal_Verdaux aux;
11636 unsigned long isum;
11637 int j;
11638
11639 vstart = ((char *) edefs) + idx;
11640 if (vstart + sizeof (*edef) > endbuf)
11641 break;
11642
11643 edef = (Elf_External_Verdef *) vstart;
11644
11645 ent.vd_version = BYTE_GET (edef->vd_version);
11646 ent.vd_flags = BYTE_GET (edef->vd_flags);
11647 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
11648 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
11649 ent.vd_hash = BYTE_GET (edef->vd_hash);
11650 ent.vd_aux = BYTE_GET (edef->vd_aux);
11651 ent.vd_next = BYTE_GET (edef->vd_next);
11652
11653 printf (_(" %#06lx: Rev: %d Flags: %s"),
11654 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
11655
11656 printf (_(" Index: %d Cnt: %d "),
11657 ent.vd_ndx, ent.vd_cnt);
11658
11659 /* Check for overflow. */
11660 if (ent.vd_aux > (size_t) (endbuf - vstart))
11661 break;
11662
11663 vstart += ent.vd_aux;
11664
11665 if (vstart + sizeof (*eaux) > endbuf)
11666 break;
11667 eaux = (Elf_External_Verdaux *) vstart;
11668
11669 aux.vda_name = BYTE_GET (eaux->vda_name);
11670 aux.vda_next = BYTE_GET (eaux->vda_next);
11671
11672 if (valid_dynamic_name (filedata, aux.vda_name))
11673 printf (_("Name: %s\n"),
11674 get_dynamic_name (filedata, aux.vda_name));
11675 else
11676 printf (_("Name index: %ld\n"), aux.vda_name);
11677
11678 isum = idx + ent.vd_aux;
11679
11680 for (j = 1; j < ent.vd_cnt; j++)
11681 {
11682 if (aux.vda_next < sizeof (*eaux)
11683 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
11684 {
11685 warn (_("Invalid vda_next field of %lx\n"),
11686 aux.vda_next);
11687 j = ent.vd_cnt;
11688 break;
11689 }
11690 /* Check for overflow. */
11691 if (aux.vda_next > (size_t) (endbuf - vstart))
11692 break;
11693
11694 isum += aux.vda_next;
11695 vstart += aux.vda_next;
11696
11697 if (vstart + sizeof (*eaux) > endbuf)
11698 break;
11699 eaux = (Elf_External_Verdaux *) vstart;
11700
11701 aux.vda_name = BYTE_GET (eaux->vda_name);
11702 aux.vda_next = BYTE_GET (eaux->vda_next);
11703
11704 if (valid_dynamic_name (filedata, aux.vda_name))
11705 printf (_(" %#06lx: Parent %d: %s\n"),
11706 isum, j,
11707 get_dynamic_name (filedata, aux.vda_name));
11708 else
11709 printf (_(" %#06lx: Parent %d, name index: %ld\n"),
11710 isum, j, aux.vda_name);
11711 }
11712
11713 if (j < ent.vd_cnt)
11714 printf (_(" Version def aux past end of section\n"));
11715
11716 /* PR 17531:
11717 file: id:000001,src:000172+005151,op:splice,rep:2. */
11718 if (ent.vd_next < sizeof (*edef)
11719 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
11720 {
11721 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
11722 cnt = section->sh_info;
11723 break;
11724 }
11725 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
11726 break;
11727
11728 idx += ent.vd_next;
11729 }
11730
11731 if (cnt < section->sh_info)
11732 printf (_(" Version definition past end of section\n"));
11733
11734 free (edefs);
11735 }
11736 break;
11737
11738 case SHT_GNU_verneed:
11739 {
11740 Elf_External_Verneed * eneed;
11741 unsigned long idx;
11742 unsigned long cnt;
11743 char * endbuf;
11744
11745 found = true;
11746
11747 if (filedata->is_separate)
11748 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
11749 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
11750 section->sh_info),
11751 filedata->file_name,
11752 printable_section_name (filedata, section),
11753 section->sh_info);
11754 else
11755 printf (ngettext ("\nVersion needs section '%s' "
11756 "contains %u entry:\n",
11757 "\nVersion needs section '%s' "
11758 "contains %u entries:\n",
11759 section->sh_info),
11760 printable_section_name (filedata, section),
11761 section->sh_info);
11762
11763 printf (_(" Addr: 0x"));
11764 printf_vma (section->sh_addr);
11765 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11766 (unsigned long) section->sh_offset, section->sh_link,
11767 printable_section_name_from_index (filedata, section->sh_link));
11768
11769 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
11770 section->sh_offset, 1,
11771 section->sh_size,
11772 _("Version Needs section"));
11773 if (!eneed)
11774 break;
11775 endbuf = (char *) eneed + section->sh_size;
11776
11777 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
11778 {
11779 Elf_External_Verneed * entry;
11780 Elf_Internal_Verneed ent;
11781 unsigned long isum;
11782 int j;
11783 char * vstart;
11784
11785 vstart = ((char *) eneed) + idx;
11786 if (vstart + sizeof (*entry) > endbuf)
11787 break;
11788
11789 entry = (Elf_External_Verneed *) vstart;
11790
11791 ent.vn_version = BYTE_GET (entry->vn_version);
11792 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
11793 ent.vn_file = BYTE_GET (entry->vn_file);
11794 ent.vn_aux = BYTE_GET (entry->vn_aux);
11795 ent.vn_next = BYTE_GET (entry->vn_next);
11796
11797 printf (_(" %#06lx: Version: %d"), idx, ent.vn_version);
11798
11799 if (valid_dynamic_name (filedata, ent.vn_file))
11800 printf (_(" File: %s"),
11801 get_dynamic_name (filedata, ent.vn_file));
11802 else
11803 printf (_(" File: %lx"), ent.vn_file);
11804
11805 printf (_(" Cnt: %d\n"), ent.vn_cnt);
11806
11807 /* Check for overflow. */
11808 if (ent.vn_aux > (size_t) (endbuf - vstart))
11809 break;
11810 vstart += ent.vn_aux;
11811
11812 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
11813 {
11814 Elf_External_Vernaux * eaux;
11815 Elf_Internal_Vernaux aux;
11816
11817 if (vstart + sizeof (*eaux) > endbuf)
11818 break;
11819 eaux = (Elf_External_Vernaux *) vstart;
11820
11821 aux.vna_hash = BYTE_GET (eaux->vna_hash);
11822 aux.vna_flags = BYTE_GET (eaux->vna_flags);
11823 aux.vna_other = BYTE_GET (eaux->vna_other);
11824 aux.vna_name = BYTE_GET (eaux->vna_name);
11825 aux.vna_next = BYTE_GET (eaux->vna_next);
11826
11827 if (valid_dynamic_name (filedata, aux.vna_name))
11828 printf (_(" %#06lx: Name: %s"),
11829 isum, get_dynamic_name (filedata, aux.vna_name));
11830 else
11831 printf (_(" %#06lx: Name index: %lx"),
11832 isum, aux.vna_name);
11833
11834 printf (_(" Flags: %s Version: %d\n"),
11835 get_ver_flags (aux.vna_flags), aux.vna_other);
11836
11837 if (aux.vna_next < sizeof (*eaux)
11838 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
11839 {
11840 warn (_("Invalid vna_next field of %lx\n"),
11841 aux.vna_next);
11842 j = ent.vn_cnt;
11843 break;
11844 }
11845 /* Check for overflow. */
11846 if (aux.vna_next > (size_t) (endbuf - vstart))
11847 break;
11848 isum += aux.vna_next;
11849 vstart += aux.vna_next;
11850 }
11851
11852 if (j < ent.vn_cnt)
11853 warn (_("Missing Version Needs auxiliary information\n"));
11854
11855 if (ent.vn_next < sizeof (*entry)
11856 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
11857 {
11858 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
11859 cnt = section->sh_info;
11860 break;
11861 }
11862 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
11863 break;
11864 idx += ent.vn_next;
11865 }
11866
11867 if (cnt < section->sh_info)
11868 warn (_("Missing Version Needs information\n"));
11869
11870 free (eneed);
11871 }
11872 break;
11873
11874 case SHT_GNU_versym:
11875 {
11876 Elf_Internal_Shdr * link_section;
11877 size_t total;
11878 unsigned int cnt;
11879 unsigned char * edata;
11880 unsigned short * data;
11881 char * strtab;
11882 Elf_Internal_Sym * symbols;
11883 Elf_Internal_Shdr * string_sec;
11884 unsigned long num_syms;
11885 long off;
11886
11887 if (section->sh_link >= filedata->file_header.e_shnum)
11888 break;
11889
11890 link_section = filedata->section_headers + section->sh_link;
11891 total = section->sh_size / sizeof (Elf_External_Versym);
11892
11893 if (link_section->sh_link >= filedata->file_header.e_shnum)
11894 break;
11895
11896 found = true;
11897
11898 symbols = get_elf_symbols (filedata, link_section, & num_syms);
11899 if (symbols == NULL)
11900 break;
11901
11902 string_sec = filedata->section_headers + link_section->sh_link;
11903
11904 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
11905 string_sec->sh_size,
11906 _("version string table"));
11907 if (!strtab)
11908 {
11909 free (symbols);
11910 break;
11911 }
11912
11913 if (filedata->is_separate)
11914 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %lu entry:\n",
11915 "\nIn linked file '%s' the version symbols section '%s' contains %lu entries:\n",
11916 total),
11917 filedata->file_name,
11918 printable_section_name (filedata, section),
11919 (unsigned long) total);
11920 else
11921 printf (ngettext ("\nVersion symbols section '%s' "
11922 "contains %lu entry:\n",
11923 "\nVersion symbols section '%s' "
11924 "contains %lu entries:\n",
11925 total),
11926 printable_section_name (filedata, section),
11927 (unsigned long) total);
11928
11929 printf (_(" Addr: 0x"));
11930 printf_vma (section->sh_addr);
11931 printf (_(" Offset: %#08lx Link: %u (%s)\n"),
11932 (unsigned long) section->sh_offset, section->sh_link,
11933 printable_section_name (filedata, link_section));
11934
11935 off = offset_from_vma (filedata,
11936 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
11937 total * sizeof (short));
11938 edata = (unsigned char *) get_data (NULL, filedata, off,
11939 sizeof (short), total,
11940 _("version symbol data"));
11941 if (!edata)
11942 {
11943 free (strtab);
11944 free (symbols);
11945 break;
11946 }
11947
11948 data = (short unsigned int *) cmalloc (total, sizeof (short));
11949
11950 for (cnt = total; cnt --;)
11951 data[cnt] = byte_get (edata + cnt * sizeof (short),
11952 sizeof (short));
11953
11954 free (edata);
11955
11956 for (cnt = 0; cnt < total; cnt += 4)
11957 {
11958 int j, nn;
11959 char *name;
11960 char *invalid = _("*invalid*");
11961
11962 printf (" %03x:", cnt);
11963
11964 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
11965 switch (data[cnt + j])
11966 {
11967 case 0:
11968 fputs (_(" 0 (*local*) "), stdout);
11969 break;
11970
11971 case 1:
11972 fputs (_(" 1 (*global*) "), stdout);
11973 break;
11974
11975 default:
11976 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
11977 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
11978
11979 /* If this index value is greater than the size of the symbols
11980 array, break to avoid an out-of-bounds read. */
11981 if ((unsigned long)(cnt + j) >= num_syms)
11982 {
11983 warn (_("invalid index into symbol array\n"));
11984 break;
11985 }
11986
11987 name = NULL;
11988 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
11989 {
11990 Elf_Internal_Verneed ivn;
11991 unsigned long offset;
11992
11993 offset = offset_from_vma
11994 (filedata,
11995 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
11996 sizeof (Elf_External_Verneed));
11997
11998 do
11999 {
12000 Elf_Internal_Vernaux ivna;
12001 Elf_External_Verneed evn;
12002 Elf_External_Vernaux evna;
12003 unsigned long a_off;
12004
12005 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12006 _("version need")) == NULL)
12007 break;
12008
12009 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12010 ivn.vn_next = BYTE_GET (evn.vn_next);
12011
12012 a_off = offset + ivn.vn_aux;
12013
12014 do
12015 {
12016 if (get_data (&evna, filedata, a_off, sizeof (evna),
12017 1, _("version need aux (2)")) == NULL)
12018 {
12019 ivna.vna_next = 0;
12020 ivna.vna_other = 0;
12021 }
12022 else
12023 {
12024 ivna.vna_next = BYTE_GET (evna.vna_next);
12025 ivna.vna_other = BYTE_GET (evna.vna_other);
12026 }
12027
12028 a_off += ivna.vna_next;
12029 }
12030 while (ivna.vna_other != data[cnt + j]
12031 && ivna.vna_next != 0);
12032
12033 if (ivna.vna_other == data[cnt + j])
12034 {
12035 ivna.vna_name = BYTE_GET (evna.vna_name);
12036
12037 if (ivna.vna_name >= string_sec->sh_size)
12038 name = invalid;
12039 else
12040 name = strtab + ivna.vna_name;
12041 break;
12042 }
12043
12044 offset += ivn.vn_next;
12045 }
12046 while (ivn.vn_next);
12047 }
12048
12049 if (data[cnt + j] != 0x8001
12050 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12051 {
12052 Elf_Internal_Verdef ivd;
12053 Elf_External_Verdef evd;
12054 unsigned long offset;
12055
12056 offset = offset_from_vma
12057 (filedata,
12058 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
12059 sizeof evd);
12060
12061 do
12062 {
12063 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
12064 _("version def")) == NULL)
12065 {
12066 ivd.vd_next = 0;
12067 /* PR 17531: file: 046-1082287-0.004. */
12068 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
12069 break;
12070 }
12071 else
12072 {
12073 ivd.vd_next = BYTE_GET (evd.vd_next);
12074 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12075 }
12076
12077 offset += ivd.vd_next;
12078 }
12079 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
12080 && ivd.vd_next != 0);
12081
12082 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
12083 {
12084 Elf_External_Verdaux evda;
12085 Elf_Internal_Verdaux ivda;
12086
12087 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12088
12089 if (get_data (&evda, filedata,
12090 offset - ivd.vd_next + ivd.vd_aux,
12091 sizeof (evda), 1,
12092 _("version def aux")) == NULL)
12093 break;
12094
12095 ivda.vda_name = BYTE_GET (evda.vda_name);
12096
12097 if (ivda.vda_name >= string_sec->sh_size)
12098 name = invalid;
12099 else if (name != NULL && name != invalid)
12100 name = _("*both*");
12101 else
12102 name = strtab + ivda.vda_name;
12103 }
12104 }
12105 if (name != NULL)
12106 nn += printf ("(%s%-*s",
12107 name,
12108 12 - (int) strlen (name),
12109 ")");
12110
12111 if (nn < 18)
12112 printf ("%*c", 18 - nn, ' ');
12113 }
12114
12115 putchar ('\n');
12116 }
12117
12118 free (data);
12119 free (strtab);
12120 free (symbols);
12121 }
12122 break;
12123
12124 default:
12125 break;
12126 }
12127 }
12128
12129 if (! found)
12130 {
12131 if (filedata->is_separate)
12132 printf (_("\nNo version information found in linked file '%s'.\n"),
12133 filedata->file_name);
12134 else
12135 printf (_("\nNo version information found in this file.\n"));
12136 }
12137
12138 return true;
12139}
12140
12141static const char *
12142get_symbol_binding (Filedata * filedata, unsigned int binding)
12143{
12144 static char buff[64];
12145
12146 switch (binding)
12147 {
12148 case STB_LOCAL: return "LOCAL";
12149 case STB_GLOBAL: return "GLOBAL";
12150 case STB_WEAK: return "WEAK";
12151 default:
12152 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
12153 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
12154 binding);
12155 else if (binding >= STB_LOOS && binding <= STB_HIOS)
12156 {
12157 if (binding == STB_GNU_UNIQUE
12158 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
12159 return "UNIQUE";
12160 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
12161 }
12162 else
12163 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
12164 return buff;
12165 }
12166}
12167
12168static const char *
12169get_symbol_type (Filedata * filedata, unsigned int type)
12170{
12171 static char buff[64];
12172
12173 switch (type)
12174 {
12175 case STT_NOTYPE: return "NOTYPE";
12176 case STT_OBJECT: return "OBJECT";
12177 case STT_FUNC: return "FUNC";
12178 case STT_SECTION: return "SECTION";
12179 case STT_FILE: return "FILE";
12180 case STT_COMMON: return "COMMON";
12181 case STT_TLS: return "TLS";
12182 case STT_RELC: return "RELC";
12183 case STT_SRELC: return "SRELC";
12184 default:
12185 if (type >= STT_LOPROC && type <= STT_HIPROC)
12186 {
12187 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
12188 return "THUMB_FUNC";
12189
12190 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
12191 return "REGISTER";
12192
12193 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
12194 return "PARISC_MILLI";
12195
12196 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
12197 }
12198 else if (type >= STT_LOOS && type <= STT_HIOS)
12199 {
12200 if (filedata->file_header.e_machine == EM_PARISC)
12201 {
12202 if (type == STT_HP_OPAQUE)
12203 return "HP_OPAQUE";
12204 if (type == STT_HP_STUB)
12205 return "HP_STUB";
12206 }
12207
12208 if (type == STT_GNU_IFUNC
12209 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
12210 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
12211 return "IFUNC";
12212
12213 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
12214 }
12215 else
12216 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
12217 return buff;
12218 }
12219}
12220
12221static const char *
12222get_symbol_visibility (unsigned int visibility)
12223{
12224 switch (visibility)
12225 {
12226 case STV_DEFAULT: return "DEFAULT";
12227 case STV_INTERNAL: return "INTERNAL";
12228 case STV_HIDDEN: return "HIDDEN";
12229 case STV_PROTECTED: return "PROTECTED";
12230 default:
12231 error (_("Unrecognized visibility value: %u\n"), visibility);
12232 return _("<unknown>");
12233 }
12234}
12235
12236static const char *
12237get_alpha_symbol_other (unsigned int other)
12238{
12239 switch (other)
12240 {
12241 case STO_ALPHA_NOPV: return "NOPV";
12242 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
12243 default:
12244 error (_("Unrecognized alpha specific other value: %u\n"), other);
12245 return _("<unknown>");
12246 }
12247}
12248
12249static const char *
12250get_solaris_symbol_visibility (unsigned int visibility)
12251{
12252 switch (visibility)
12253 {
12254 case 4: return "EXPORTED";
12255 case 5: return "SINGLETON";
12256 case 6: return "ELIMINATE";
12257 default: return get_symbol_visibility (visibility);
12258 }
12259}
12260
12261static const char *
12262get_aarch64_symbol_other (unsigned int other)
12263{
12264 static char buf[32];
12265
12266 if (other & STO_AARCH64_VARIANT_PCS)
12267 {
12268 other &= ~STO_AARCH64_VARIANT_PCS;
12269 if (other == 0)
12270 return "VARIANT_PCS";
12271 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
12272 return buf;
12273 }
12274 return NULL;
12275}
12276
12277static const char *
12278get_mips_symbol_other (unsigned int other)
12279{
12280 switch (other)
12281 {
12282 case STO_OPTIONAL: return "OPTIONAL";
12283 case STO_MIPS_PLT: return "MIPS PLT";
12284 case STO_MIPS_PIC: return "MIPS PIC";
12285 case STO_MICROMIPS: return "MICROMIPS";
12286 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
12287 case STO_MIPS16: return "MIPS16";
12288 default: return NULL;
12289 }
12290}
12291
12292static const char *
12293get_ia64_symbol_other (Filedata * filedata, unsigned int other)
12294{
12295 if (is_ia64_vms (filedata))
12296 {
12297 static char res[32];
12298
12299 res[0] = 0;
12300
12301 /* Function types is for images and .STB files only. */
12302 switch (filedata->file_header.e_type)
12303 {
12304 case ET_DYN:
12305 case ET_EXEC:
12306 switch (VMS_ST_FUNC_TYPE (other))
12307 {
12308 case VMS_SFT_CODE_ADDR:
12309 strcat (res, " CA");
12310 break;
12311 case VMS_SFT_SYMV_IDX:
12312 strcat (res, " VEC");
12313 break;
12314 case VMS_SFT_FD:
12315 strcat (res, " FD");
12316 break;
12317 case VMS_SFT_RESERVE:
12318 strcat (res, " RSV");
12319 break;
12320 default:
12321 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
12322 VMS_ST_FUNC_TYPE (other));
12323 strcat (res, " <unknown>");
12324 break;
12325 }
12326 break;
12327 default:
12328 break;
12329 }
12330 switch (VMS_ST_LINKAGE (other))
12331 {
12332 case VMS_STL_IGNORE:
12333 strcat (res, " IGN");
12334 break;
12335 case VMS_STL_RESERVE:
12336 strcat (res, " RSV");
12337 break;
12338 case VMS_STL_STD:
12339 strcat (res, " STD");
12340 break;
12341 case VMS_STL_LNK:
12342 strcat (res, " LNK");
12343 break;
12344 default:
12345 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
12346 VMS_ST_LINKAGE (other));
12347 strcat (res, " <unknown>");
12348 break;
12349 }
12350
12351 if (res[0] != 0)
12352 return res + 1;
12353 else
12354 return res;
12355 }
12356 return NULL;
12357}
12358
12359static const char *
12360get_ppc64_symbol_other (unsigned int other)
12361{
12362 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
12363 return NULL;
12364
12365 other >>= STO_PPC64_LOCAL_BIT;
12366 if (other <= 6)
12367 {
12368 static char buf[64];
12369 if (other >= 2)
12370 other = ppc64_decode_local_entry (other);
12371 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
12372 return buf;
12373 }
12374 return NULL;
12375}
12376
12377static const char *
12378get_symbol_other (Filedata * filedata, unsigned int other)
12379{
12380 const char * result = NULL;
12381 static char buff [64];
12382
12383 if (other == 0)
12384 return "";
12385
12386 switch (filedata->file_header.e_machine)
12387 {
12388 case EM_ALPHA:
12389 result = get_alpha_symbol_other (other);
12390 break;
12391 case EM_AARCH64:
12392 result = get_aarch64_symbol_other (other);
12393 break;
12394 case EM_MIPS:
12395 result = get_mips_symbol_other (other);
12396 break;
12397 case EM_IA_64:
12398 result = get_ia64_symbol_other (filedata, other);
12399 break;
12400 case EM_PPC64:
12401 result = get_ppc64_symbol_other (other);
12402 break;
12403 default:
12404 result = NULL;
12405 break;
12406 }
12407
12408 if (result)
12409 return result;
12410
12411 snprintf (buff, sizeof buff, _("<other>: %x"), other);
12412 return buff;
12413}
12414
12415static const char *
12416get_symbol_index_type (Filedata * filedata, unsigned int type)
12417{
12418 static char buff[32];
12419
12420 switch (type)
12421 {
12422 case SHN_UNDEF: return "UND";
12423 case SHN_ABS: return "ABS";
12424 case SHN_COMMON: return "COM";
12425 default:
12426 if (type == SHN_IA_64_ANSI_COMMON
12427 && filedata->file_header.e_machine == EM_IA_64
12428 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX)
12429 return "ANSI_COM";
12430 else if ((filedata->file_header.e_machine == EM_X86_64
12431 || filedata->file_header.e_machine == EM_L1OM
12432 || filedata->file_header.e_machine == EM_K1OM)
12433 && type == SHN_X86_64_LCOMMON)
12434 return "LARGE_COM";
12435 else if ((type == SHN_MIPS_SCOMMON
12436 && filedata->file_header.e_machine == EM_MIPS)
12437 || (type == SHN_TIC6X_SCOMMON
12438 && filedata->file_header.e_machine == EM_TI_C6000))
12439 return "SCOM";
12440 else if (type == SHN_MIPS_SUNDEFINED
12441 && filedata->file_header.e_machine == EM_MIPS)
12442 return "SUND";
12443 else if (type >= SHN_LOPROC && type <= SHN_HIPROC)
12444 sprintf (buff, "PRC[0x%04x]", type & 0xffff);
12445 else if (type >= SHN_LOOS && type <= SHN_HIOS)
12446 sprintf (buff, "OS [0x%04x]", type & 0xffff);
12447 else if (type >= SHN_LORESERVE)
12448 sprintf (buff, "RSV[0x%04x]", type & 0xffff);
12449 else if (filedata->file_header.e_shnum != 0
12450 && type >= filedata->file_header.e_shnum)
12451 sprintf (buff, _("bad section index[%3d]"), type);
12452 else
12453 sprintf (buff, "%3d", type);
12454 break;
12455 }
12456
12457 return buff;
12458}
12459
12460static const char *
12461get_symbol_version_string (Filedata * filedata,
12462 bool is_dynsym,
12463 const char * strtab,
12464 unsigned long int strtab_size,
12465 unsigned int si,
12466 Elf_Internal_Sym * psym,
12467 enum versioned_symbol_info * sym_info,
12468 unsigned short * vna_other)
12469{
12470 unsigned char data[2];
12471 unsigned short vers_data;
12472 unsigned long offset;
12473 unsigned short max_vd_ndx;
12474
12475 if (!is_dynsym
12476 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
12477 return NULL;
12478
12479 offset = offset_from_vma (filedata,
12480 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12481 sizeof data + si * sizeof (vers_data));
12482
12483 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
12484 sizeof (data), 1, _("version data")) == NULL)
12485 return NULL;
12486
12487 vers_data = byte_get (data, 2);
12488
12489 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
12490 return NULL;
12491
12492 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
12493 max_vd_ndx = 0;
12494
12495 /* Usually we'd only see verdef for defined symbols, and verneed for
12496 undefined symbols. However, symbols defined by the linker in
12497 .dynbss for variables copied from a shared library in order to
12498 avoid text relocations are defined yet have verneed. We could
12499 use a heuristic to detect the special case, for example, check
12500 for verneed first on symbols defined in SHT_NOBITS sections, but
12501 it is simpler and more reliable to just look for both verdef and
12502 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
12503
12504 if (psym->st_shndx != SHN_UNDEF
12505 && vers_data != 0x8001
12506 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
12507 {
12508 Elf_Internal_Verdef ivd;
12509 Elf_Internal_Verdaux ivda;
12510 Elf_External_Verdaux evda;
12511 unsigned long off;
12512
12513 off = offset_from_vma (filedata,
12514 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
12515 sizeof (Elf_External_Verdef));
12516
12517 do
12518 {
12519 Elf_External_Verdef evd;
12520
12521 if (get_data (&evd, filedata, off, sizeof (evd), 1,
12522 _("version def")) == NULL)
12523 {
12524 ivd.vd_ndx = 0;
12525 ivd.vd_aux = 0;
12526 ivd.vd_next = 0;
12527 ivd.vd_flags = 0;
12528 }
12529 else
12530 {
12531 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
12532 ivd.vd_aux = BYTE_GET (evd.vd_aux);
12533 ivd.vd_next = BYTE_GET (evd.vd_next);
12534 ivd.vd_flags = BYTE_GET (evd.vd_flags);
12535 }
12536
12537 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
12538 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
12539
12540 off += ivd.vd_next;
12541 }
12542 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
12543
12544 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
12545 {
12546 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
12547 return NULL;
12548
12549 off -= ivd.vd_next;
12550 off += ivd.vd_aux;
12551
12552 if (get_data (&evda, filedata, off, sizeof (evda), 1,
12553 _("version def aux")) != NULL)
12554 {
12555 ivda.vda_name = BYTE_GET (evda.vda_name);
12556
12557 if (psym->st_name != ivda.vda_name)
12558 return (ivda.vda_name < strtab_size
12559 ? strtab + ivda.vda_name : _("<corrupt>"));
12560 }
12561 }
12562 }
12563
12564 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
12565 {
12566 Elf_External_Verneed evn;
12567 Elf_Internal_Verneed ivn;
12568 Elf_Internal_Vernaux ivna;
12569
12570 offset = offset_from_vma (filedata,
12571 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
12572 sizeof evn);
12573 do
12574 {
12575 unsigned long vna_off;
12576
12577 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
12578 _("version need")) == NULL)
12579 {
12580 ivna.vna_next = 0;
12581 ivna.vna_other = 0;
12582 ivna.vna_name = 0;
12583 break;
12584 }
12585
12586 ivn.vn_aux = BYTE_GET (evn.vn_aux);
12587 ivn.vn_next = BYTE_GET (evn.vn_next);
12588
12589 vna_off = offset + ivn.vn_aux;
12590
12591 do
12592 {
12593 Elf_External_Vernaux evna;
12594
12595 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
12596 _("version need aux (3)")) == NULL)
12597 {
12598 ivna.vna_next = 0;
12599 ivna.vna_other = 0;
12600 ivna.vna_name = 0;
12601 }
12602 else
12603 {
12604 ivna.vna_other = BYTE_GET (evna.vna_other);
12605 ivna.vna_next = BYTE_GET (evna.vna_next);
12606 ivna.vna_name = BYTE_GET (evna.vna_name);
12607 }
12608
12609 vna_off += ivna.vna_next;
12610 }
12611 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
12612
12613 if (ivna.vna_other == vers_data)
12614 break;
12615
12616 offset += ivn.vn_next;
12617 }
12618 while (ivn.vn_next != 0);
12619
12620 if (ivna.vna_other == vers_data)
12621 {
12622 *sym_info = symbol_undefined;
12623 *vna_other = ivna.vna_other;
12624 return (ivna.vna_name < strtab_size
12625 ? strtab + ivna.vna_name : _("<corrupt>"));
12626 }
12627 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
12628 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
12629 return _("<corrupt>");
12630 }
12631 return NULL;
12632}
12633
12634/* Display a symbol size on stdout. Format is based on --sym-base setting. */
12635
12636static unsigned int
12637print_dynamic_symbol_size (bfd_vma vma, int base)
12638{
12639 switch (base)
12640 {
12641 case 8:
12642 return print_vma (vma, OCTAL_5);
12643
12644 case 10:
12645 return print_vma (vma, UNSIGNED_5);
12646
12647 case 16:
12648 return print_vma (vma, PREFIX_HEX_5);
12649
12650 case 0:
12651 default:
12652 return print_vma (vma, DEC_5);
12653 }
12654}
12655
12656static void
12657print_dynamic_symbol (Filedata *filedata, unsigned long si,
12658 Elf_Internal_Sym *symtab,
12659 Elf_Internal_Shdr *section,
12660 char *strtab, size_t strtab_size)
12661{
12662 const char *version_string;
12663 enum versioned_symbol_info sym_info;
12664 unsigned short vna_other;
12665 bool is_valid;
12666 const char * sstr;
12667 Elf_Internal_Sym *psym = symtab + si;
12668
12669 printf ("%6ld: ", si);
12670 print_vma (psym->st_value, LONG_HEX);
12671 putchar (' ');
12672 print_dynamic_symbol_size (psym->st_size, sym_base);
12673 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
12674 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
12675 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
12676 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
12677 else
12678 {
12679 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
12680
12681 printf (" %-7s", get_symbol_visibility (vis));
12682 /* Check to see if any other bits in the st_other field are set.
12683 Note - displaying this information disrupts the layout of the
12684 table being generated, but for the moment this case is very rare. */
12685 if (psym->st_other ^ vis)
12686 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
12687 }
12688 printf (" %4s ", get_symbol_index_type (filedata, psym->st_shndx));
12689
12690 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
12691 && psym->st_shndx < filedata->file_header.e_shnum
12692 && psym->st_name == 0)
12693 {
12694 is_valid
12695 = section_name_valid (filedata,
12696 filedata->section_headers + psym->st_shndx);
12697 sstr = is_valid ?
12698 section_name_print (filedata,
12699 filedata->section_headers + psym->st_shndx)
12700 : _("<corrupt>");
12701 }
12702 else
12703 {
12704 is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
12705 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
12706 }
12707
12708 version_string
12709 = get_symbol_version_string (filedata,
12710 (section == NULL
12711 || section->sh_type == SHT_DYNSYM),
12712 strtab, strtab_size, si,
12713 psym, &sym_info, &vna_other);
12714
12715 int len_avail = 21;
12716 if (! do_wide && version_string != NULL)
12717 {
12718 char buffer[16];
12719
12720 len_avail -= 1 + strlen (version_string);
12721
12722 if (sym_info == symbol_undefined)
12723 len_avail -= sprintf (buffer," (%d)", vna_other);
12724 else if (sym_info != symbol_hidden)
12725 len_avail -= 1;
12726 }
12727
12728 print_symbol (len_avail, sstr);
12729
12730 if (version_string)
12731 {
12732 if (sym_info == symbol_undefined)
12733 printf ("@%s (%d)", version_string, vna_other);
12734 else
12735 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
12736 version_string);
12737 }
12738
12739 putchar ('\n');
12740
12741 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
12742 && section != NULL
12743 && si >= section->sh_info
12744 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
12745 && filedata->file_header.e_machine != EM_MIPS
12746 /* Solaris binaries have been found to violate this requirement as
12747 well. Not sure if this is a bug or an ABI requirement. */
12748 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
12749 warn (_("local symbol %lu found at index >= %s's sh_info value of %u\n"),
12750 si, printable_section_name (filedata, section), section->sh_info);
12751}
12752
12753static const char *
12754get_lto_kind (unsigned int kind)
12755{
12756 switch (kind)
12757 {
12758 case 0: return "DEF";
12759 case 1: return "WEAKDEF";
12760 case 2: return "UNDEF";
12761 case 3: return "WEAKUNDEF";
12762 case 4: return "COMMON";
12763 default:
12764 break;
12765 }
12766
12767 static char buffer[30];
12768 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
12769 sprintf (buffer, "<unknown: %u>", kind);
12770 return buffer;
12771}
12772
12773static const char *
12774get_lto_visibility (unsigned int visibility)
12775{
12776 switch (visibility)
12777 {
12778 case 0: return "DEFAULT";
12779 case 1: return "PROTECTED";
12780 case 2: return "INTERNAL";
12781 case 3: return "HIDDEN";
12782 default:
12783 break;
12784 }
12785
12786 static char buffer[30];
12787 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
12788 sprintf (buffer, "<unknown: %u>", visibility);
12789 return buffer;
12790}
12791
12792static const char *
12793get_lto_sym_type (unsigned int sym_type)
12794{
12795 switch (sym_type)
12796 {
12797 case 0: return "UNKNOWN";
12798 case 1: return "FUNCTION";
12799 case 2: return "VARIABLE";
12800 default:
12801 break;
12802 }
12803
12804 static char buffer[30];
12805 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
12806 sprintf (buffer, "<unknown: %u>", sym_type);
12807 return buffer;
12808}
12809
12810/* Display an LTO format symbol table.
12811 FIXME: The format of LTO symbol tables is not formalized.
12812 So this code could need changing in the future. */
12813
12814static bool
12815display_lto_symtab (Filedata * filedata,
12816 Elf_Internal_Shdr * section)
12817{
12818 if (section->sh_size == 0)
12819 {
12820 if (filedata->is_separate)
12821 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
12822 printable_section_name (filedata, section),
12823 filedata->file_name);
12824 else
12825 printf (_("\nLTO Symbol table '%s' is empty!\n"),
12826 printable_section_name (filedata, section));
12827
12828 return true;
12829 }
12830
12831 if (section->sh_size > filedata->file_size)
12832 {
12833 error (_("Section %s has an invalid sh_size of 0x%lx\n"),
12834 printable_section_name (filedata, section),
12835 (unsigned long) section->sh_size);
12836 return false;
12837 }
12838
12839 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
12840 section->sh_size, 1, _("LTO symbols"));
12841 if (alloced_data == NULL)
12842 return false;
12843
12844 /* Look for extended data for the symbol table. */
12845 Elf_Internal_Shdr * ext;
12846 void * ext_data_orig = NULL;
12847 char * ext_data = NULL;
12848 char * ext_data_end = NULL;
12849 char * ext_name = NULL;
12850
12851 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
12852 (section_name (filedata, section)
12853 + sizeof (".gnu.lto_.symtab.") - 1)) > 0
12854 && ext_name != NULL /* Paranoia. */
12855 && (ext = find_section (filedata, ext_name)) != NULL)
12856 {
12857 if (ext->sh_size < 3)
12858 error (_("LTO Symbol extension table '%s' is empty!\n"),
12859 printable_section_name (filedata, ext));
12860 else
12861 {
12862 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
12863 ext->sh_size, 1,
12864 _("LTO ext symbol data"));
12865 if (ext_data != NULL)
12866 {
12867 ext_data_end = ext_data + ext->sh_size;
12868 if (* ext_data++ != 1)
12869 error (_("Unexpected version number in symbol extension table\n"));
12870 }
12871 }
12872 }
12873
12874 const unsigned char * data = (const unsigned char *) alloced_data;
12875 const unsigned char * end = data + section->sh_size;
12876
12877 if (filedata->is_separate)
12878 printf (_("\nIn linked file '%s': "), filedata->file_name);
12879 else
12880 printf ("\n");
12881
12882 if (ext_data_orig != NULL)
12883 {
12884 if (do_wide)
12885 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
12886 printable_section_name (filedata, section),
12887 printable_section_name (filedata, ext));
12888 else
12889 {
12890 printf (_("LTO Symbol table '%s'\n"),
12891 printable_section_name (filedata, section));
12892 printf (_(" and extension table '%s' contain:\n"),
12893 printable_section_name (filedata, ext));
12894 }
12895 }
12896 else
12897 printf (_("LTO Symbol table '%s' contains:\n"),
12898 printable_section_name (filedata, section));
12899
12900 /* FIXME: Add a wide version. */
12901 if (ext_data_orig != NULL)
12902 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
12903 else
12904 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
12905
12906 /* FIXME: We do not handle style prefixes. */
12907
12908 while (data < end)
12909 {
12910 const unsigned char * sym_name = data;
12911 data += strnlen ((const char *) sym_name, end - data) + 1;
12912 if (data >= end)
12913 goto fail;
12914
12915 const unsigned char * comdat_key = data;
12916 data += strnlen ((const char *) comdat_key, end - data) + 1;
12917 if (data >= end)
12918 goto fail;
12919
12920 if (data + 2 + 8 + 4 > end)
12921 goto fail;
12922
12923 unsigned int kind = *data++;
12924 unsigned int visibility = *data++;
12925
12926 elf_vma size = byte_get (data, 8);
12927 data += 8;
12928
12929 elf_vma slot = byte_get (data, 4);
12930 data += 4;
12931
12932 if (ext_data != NULL)
12933 {
12934 if (ext_data < (ext_data_end - 1))
12935 {
12936 unsigned int sym_type = * ext_data ++;
12937 unsigned int sec_kind = * ext_data ++;
12938
12939 printf (" %10s %10s %11s %08lx %08lx %9s %08lx _",
12940 * comdat_key == 0 ? "-" : (char *) comdat_key,
12941 get_lto_kind (kind),
12942 get_lto_visibility (visibility),
12943 (long) size,
12944 (long) slot,
12945 get_lto_sym_type (sym_type),
12946 (long) sec_kind);
12947 print_symbol (6, (const char *) sym_name);
12948 }
12949 else
12950 {
12951 error (_("Ran out of LTO symbol extension data\n"));
12952 ext_data = NULL;
12953 /* FIXME: return FAIL result ? */
12954 }
12955 }
12956 else
12957 {
12958 printf (" %10s %10s %11s %08lx %08lx _",
12959 * comdat_key == 0 ? "-" : (char *) comdat_key,
12960 get_lto_kind (kind),
12961 get_lto_visibility (visibility),
12962 (long) size,
12963 (long) slot);
12964 print_symbol (21, (const char *) sym_name);
12965 }
12966 putchar ('\n');
12967 }
12968
12969 if (ext_data != NULL && ext_data < ext_data_end)
12970 {
12971 error (_("Data remains in the LTO symbol extension table\n"));
12972 goto fail;
12973 }
12974
12975 free (alloced_data);
12976 free (ext_data_orig);
12977 free (ext_name);
12978 return true;
12979
12980 fail:
12981 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
12982 free (alloced_data);
12983 free (ext_data_orig);
12984 free (ext_name);
12985 return false;
12986}
12987
12988/* Display LTO symbol tables. */
12989
12990static bool
12991process_lto_symbol_tables (Filedata * filedata)
12992{
12993 Elf_Internal_Shdr * section;
12994 unsigned int i;
12995 bool res = true;
12996
12997 if (!do_lto_syms)
12998 return true;
12999
13000 if (filedata->section_headers == NULL)
13001 return true;
13002
13003 for (i = 0, section = filedata->section_headers;
13004 i < filedata->file_header.e_shnum;
13005 i++, section++)
13006 if (section_name_valid (filedata, section)
13007 && startswith (section_name (filedata, section), ".gnu.lto_.symtab."))
13008 res &= display_lto_symtab (filedata, section);
13009
13010 return res;
13011}
13012
13013/* Dump the symbol table. */
13014
13015static bool
13016process_symbol_table (Filedata * filedata)
13017{
13018 Elf_Internal_Shdr * section;
13019
13020 if (!do_syms && !do_dyn_syms && !do_histogram)
13021 return true;
13022
13023 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
13024 && do_syms
13025 && do_using_dynamic
13026 && filedata->dynamic_strings != NULL
13027 && filedata->dynamic_symbols != NULL)
13028 {
13029 unsigned long si;
13030
13031 if (filedata->is_separate)
13032 {
13033 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table contains %lu entry:\n",
13034 "\nIn linked file '%s' the dynamic symbol table contains %lu entries:\n",
13035 filedata->num_dynamic_syms),
13036 filedata->file_name,
13037 filedata->num_dynamic_syms);
13038 }
13039 else
13040 {
13041 printf (ngettext ("\nSymbol table for image contains %lu entry:\n",
13042 "\nSymbol table for image contains %lu entries:\n",
13043 filedata->num_dynamic_syms),
13044 filedata->num_dynamic_syms);
13045 }
13046 if (is_32bit_elf)
13047 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13048 else
13049 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13050
13051 for (si = 0; si < filedata->num_dynamic_syms; si++)
13052 print_dynamic_symbol (filedata, si, filedata->dynamic_symbols, NULL,
13053 filedata->dynamic_strings,
13054 filedata->dynamic_strings_length);
13055 }
13056 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
13057 && filedata->section_headers != NULL)
13058 {
13059 unsigned int i;
13060
13061 for (i = 0, section = filedata->section_headers;
13062 i < filedata->file_header.e_shnum;
13063 i++, section++)
13064 {
13065 char * strtab = NULL;
13066 unsigned long int strtab_size = 0;
13067 Elf_Internal_Sym * symtab;
13068 unsigned long si, num_syms;
13069
13070 if ((section->sh_type != SHT_SYMTAB
13071 && section->sh_type != SHT_DYNSYM)
13072 || (!do_syms
13073 && section->sh_type == SHT_SYMTAB))
13074 continue;
13075
13076 if (section->sh_entsize == 0)
13077 {
13078 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
13079 printable_section_name (filedata, section));
13080 continue;
13081 }
13082
13083 num_syms = section->sh_size / section->sh_entsize;
13084
13085 if (filedata->is_separate)
13086 printf (ngettext ("\nIn linked file '%s' symbol section '%s' contains %lu entry:\n",
13087 "\nIn linked file '%s' symbol section '%s' contains %lu entries:\n",
13088 num_syms),
13089 filedata->file_name,
13090 printable_section_name (filedata, section),
13091 num_syms);
13092 else
13093 printf (ngettext ("\nSymbol table '%s' contains %lu entry:\n",
13094 "\nSymbol table '%s' contains %lu entries:\n",
13095 num_syms),
13096 printable_section_name (filedata, section),
13097 num_syms);
13098
13099 if (is_32bit_elf)
13100 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13101 else
13102 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
13103
13104 symtab = get_elf_symbols (filedata, section, & num_syms);
13105 if (symtab == NULL)
13106 continue;
13107
13108 if (section->sh_link == filedata->file_header.e_shstrndx)
13109 {
13110 strtab = filedata->string_table;
13111 strtab_size = filedata->string_table_length;
13112 }
13113 else if (section->sh_link < filedata->file_header.e_shnum)
13114 {
13115 Elf_Internal_Shdr * string_sec;
13116
13117 string_sec = filedata->section_headers + section->sh_link;
13118
13119 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
13120 1, string_sec->sh_size,
13121 _("string table"));
13122 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
13123 }
13124
13125 for (si = 0; si < num_syms; si++)
13126 print_dynamic_symbol (filedata, si, symtab, section,
13127 strtab, strtab_size);
13128
13129 free (symtab);
13130 if (strtab != filedata->string_table)
13131 free (strtab);
13132 }
13133 }
13134 else if (do_syms)
13135 printf
13136 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
13137
13138 if (do_histogram && filedata->buckets != NULL)
13139 {
13140 unsigned long * lengths;
13141 unsigned long * counts;
13142 unsigned long hn;
13143 bfd_vma si;
13144 unsigned long maxlength = 0;
13145 unsigned long nzero_counts = 0;
13146 unsigned long nsyms = 0;
13147 char *visited;
13148
13149 printf (ngettext ("\nHistogram for bucket list length "
13150 "(total of %lu bucket):\n",
13151 "\nHistogram for bucket list length "
13152 "(total of %lu buckets):\n",
13153 (unsigned long) filedata->nbuckets),
13154 (unsigned long) filedata->nbuckets);
13155
13156 lengths = (unsigned long *) calloc (filedata->nbuckets,
13157 sizeof (*lengths));
13158 if (lengths == NULL)
13159 {
13160 error (_("Out of memory allocating space for histogram buckets\n"));
13161 goto err_out;
13162 }
13163 visited = xcmalloc (filedata->nchains, 1);
13164 memset (visited, 0, filedata->nchains);
13165
13166 printf (_(" Length Number %% of total Coverage\n"));
13167 for (hn = 0; hn < filedata->nbuckets; ++hn)
13168 {
13169 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
13170 {
13171 ++nsyms;
13172 if (maxlength < ++lengths[hn])
13173 ++maxlength;
13174 if (si >= filedata->nchains || visited[si])
13175 {
13176 error (_("histogram chain is corrupt\n"));
13177 break;
13178 }
13179 visited[si] = 1;
13180 }
13181 }
13182 free (visited);
13183
13184 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13185 if (counts == NULL)
13186 {
13187 free (lengths);
13188 error (_("Out of memory allocating space for histogram counts\n"));
13189 goto err_out;
13190 }
13191
13192 for (hn = 0; hn < filedata->nbuckets; ++hn)
13193 ++counts[lengths[hn]];
13194
13195 if (filedata->nbuckets > 0)
13196 {
13197 unsigned long i;
13198 printf (" 0 %-10lu (%5.1f%%)\n",
13199 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
13200 for (i = 1; i <= maxlength; ++i)
13201 {
13202 nzero_counts += counts[i] * i;
13203 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13204 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
13205 (nzero_counts * 100.0) / nsyms);
13206 }
13207 }
13208
13209 free (counts);
13210 free (lengths);
13211 }
13212
13213 free (filedata->buckets);
13214 filedata->buckets = NULL;
13215 filedata->nbuckets = 0;
13216 free (filedata->chains);
13217 filedata->chains = NULL;
13218
13219 if (do_histogram && filedata->gnubuckets != NULL)
13220 {
13221 unsigned long * lengths;
13222 unsigned long * counts;
13223 unsigned long hn;
13224 unsigned long maxlength = 0;
13225 unsigned long nzero_counts = 0;
13226 unsigned long nsyms = 0;
13227
13228 printf (ngettext ("\nHistogram for `%s' bucket list length "
13229 "(total of %lu bucket):\n",
13230 "\nHistogram for `%s' bucket list length "
13231 "(total of %lu buckets):\n",
13232 (unsigned long) filedata->ngnubuckets),
13233 GNU_HASH_SECTION_NAME (filedata),
13234 (unsigned long) filedata->ngnubuckets);
13235
13236 lengths = (unsigned long *) calloc (filedata->ngnubuckets,
13237 sizeof (*lengths));
13238 if (lengths == NULL)
13239 {
13240 error (_("Out of memory allocating space for gnu histogram buckets\n"));
13241 goto err_out;
13242 }
13243
13244 printf (_(" Length Number %% of total Coverage\n"));
13245
13246 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13247 if (filedata->gnubuckets[hn] != 0)
13248 {
13249 bfd_vma off, length = 1;
13250
13251 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
13252 /* PR 17531 file: 010-77222-0.004. */
13253 off < filedata->ngnuchains
13254 && (filedata->gnuchains[off] & 1) == 0;
13255 ++off)
13256 ++length;
13257 lengths[hn] = length;
13258 if (length > maxlength)
13259 maxlength = length;
13260 nsyms += length;
13261 }
13262
13263 counts = (unsigned long *) calloc (maxlength + 1, sizeof (*counts));
13264 if (counts == NULL)
13265 {
13266 free (lengths);
13267 error (_("Out of memory allocating space for gnu histogram counts\n"));
13268 goto err_out;
13269 }
13270
13271 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
13272 ++counts[lengths[hn]];
13273
13274 if (filedata->ngnubuckets > 0)
13275 {
13276 unsigned long j;
13277 printf (" 0 %-10lu (%5.1f%%)\n",
13278 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
13279 for (j = 1; j <= maxlength; ++j)
13280 {
13281 nzero_counts += counts[j] * j;
13282 printf ("%7lu %-10lu (%5.1f%%) %5.1f%%\n",
13283 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
13284 (nzero_counts * 100.0) / nsyms);
13285 }
13286 }
13287
13288 free (counts);
13289 free (lengths);
13290 }
13291 free (filedata->gnubuckets);
13292 filedata->gnubuckets = NULL;
13293 filedata->ngnubuckets = 0;
13294 free (filedata->gnuchains);
13295 filedata->gnuchains = NULL;
13296 filedata->ngnuchains = 0;
13297 free (filedata->mipsxlat);
13298 filedata->mipsxlat = NULL;
13299 return true;
13300
13301 err_out:
13302 free (filedata->gnubuckets);
13303 filedata->gnubuckets = NULL;
13304 filedata->ngnubuckets = 0;
13305 free (filedata->gnuchains);
13306 filedata->gnuchains = NULL;
13307 filedata->ngnuchains = 0;
13308 free (filedata->mipsxlat);
13309 filedata->mipsxlat = NULL;
13310 free (filedata->buckets);
13311 filedata->buckets = NULL;
13312 filedata->nbuckets = 0;
13313 free (filedata->chains);
13314 filedata->chains = NULL;
13315 return false;
13316}
13317
13318static bool
13319process_syminfo (Filedata * filedata)
13320{
13321 unsigned int i;
13322
13323 if (filedata->dynamic_syminfo == NULL
13324 || !do_dynamic)
13325 /* No syminfo, this is ok. */
13326 return true;
13327
13328 /* There better should be a dynamic symbol section. */
13329 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
13330 return false;
13331
13332 if (filedata->is_separate)
13333 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entry:\n",
13334 "\nIn linked file '%s: the dynamic info segment at offset 0x%lx contains %d entries:\n",
13335 filedata->dynamic_syminfo_nent),
13336 filedata->file_name,
13337 filedata->dynamic_syminfo_offset,
13338 filedata->dynamic_syminfo_nent);
13339 else
13340 printf (ngettext ("\nDynamic info segment at offset 0x%lx "
13341 "contains %d entry:\n",
13342 "\nDynamic info segment at offset 0x%lx "
13343 "contains %d entries:\n",
13344 filedata->dynamic_syminfo_nent),
13345 filedata->dynamic_syminfo_offset,
13346 filedata->dynamic_syminfo_nent);
13347
13348 printf (_(" Num: Name BoundTo Flags\n"));
13349 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
13350 {
13351 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
13352
13353 printf ("%4d: ", i);
13354 if (i >= filedata->num_dynamic_syms)
13355 printf (_("<corrupt index>"));
13356 else if (valid_dynamic_name (filedata, filedata->dynamic_symbols[i].st_name))
13357 print_symbol (30, get_dynamic_name (filedata,
13358 filedata->dynamic_symbols[i].st_name));
13359 else
13360 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
13361 putchar (' ');
13362
13363 switch (filedata->dynamic_syminfo[i].si_boundto)
13364 {
13365 case SYMINFO_BT_SELF:
13366 fputs ("SELF ", stdout);
13367 break;
13368 case SYMINFO_BT_PARENT:
13369 fputs ("PARENT ", stdout);
13370 break;
13371 default:
13372 if (filedata->dynamic_syminfo[i].si_boundto > 0
13373 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
13374 && valid_dynamic_name (filedata,
13375 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
13376 {
13377 print_symbol (10, get_dynamic_name (filedata,
13378 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
13379 putchar (' ' );
13380 }
13381 else
13382 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
13383 break;
13384 }
13385
13386 if (flags & SYMINFO_FLG_DIRECT)
13387 printf (" DIRECT");
13388 if (flags & SYMINFO_FLG_PASSTHRU)
13389 printf (" PASSTHRU");
13390 if (flags & SYMINFO_FLG_COPY)
13391 printf (" COPY");
13392 if (flags & SYMINFO_FLG_LAZYLOAD)
13393 printf (" LAZYLOAD");
13394
13395 puts ("");
13396 }
13397
13398 return true;
13399}
13400
13401/* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
13402 is contained by the region START .. END. The types of ADDR, START
13403 and END should all be the same. Note both ADDR + NELEM and END
13404 point to just beyond the end of the regions that are being tested. */
13405#define IN_RANGE(START,END,ADDR,NELEM) \
13406 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
13407
13408/* Check to see if the given reloc needs to be handled in a target specific
13409 manner. If so then process the reloc and return TRUE otherwise return
13410 FALSE.
13411
13412 If called with reloc == NULL, then this is a signal that reloc processing
13413 for the current section has finished, and any saved state should be
13414 discarded. */
13415
13416static bool
13417target_specific_reloc_handling (Filedata * filedata,
13418 Elf_Internal_Rela * reloc,
13419 unsigned char * start,
13420 unsigned char * end,
13421 Elf_Internal_Sym * symtab,
13422 unsigned long num_syms)
13423{
13424 unsigned int reloc_type = 0;
13425 unsigned long sym_index = 0;
13426
13427 if (reloc)
13428 {
13429 reloc_type = get_reloc_type (filedata, reloc->r_info);
13430 sym_index = get_reloc_symindex (reloc->r_info);
13431 }
13432
13433 switch (filedata->file_header.e_machine)
13434 {
13435 case EM_MSP430:
13436 case EM_MSP430_OLD:
13437 {
13438 static Elf_Internal_Sym * saved_sym = NULL;
13439
13440 if (reloc == NULL)
13441 {
13442 saved_sym = NULL;
13443 return true;
13444 }
13445
13446 switch (reloc_type)
13447 {
13448 case 10: /* R_MSP430_SYM_DIFF */
13449 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
13450 if (uses_msp430x_relocs (filedata))
13451 break;
13452 /* Fall through. */
13453 case 21: /* R_MSP430X_SYM_DIFF */
13454 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
13455 /* PR 21139. */
13456 if (sym_index >= num_syms)
13457 error (_("MSP430 SYM_DIFF reloc contains invalid symbol index %lu\n"),
13458 sym_index);
13459 else
13460 saved_sym = symtab + sym_index;
13461 return true;
13462
13463 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13464 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
13465 goto handle_sym_diff;
13466
13467 case 5: /* R_MSP430_16_BYTE */
13468 case 9: /* R_MSP430_8 */
13469 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13470 if (uses_msp430x_relocs (filedata))
13471 break;
13472 goto handle_sym_diff;
13473
13474 case 2: /* R_MSP430_ABS16 */
13475 case 15: /* R_MSP430X_ABS16 */
13476 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13477 if (! uses_msp430x_relocs (filedata))
13478 break;
13479 goto handle_sym_diff;
13480
13481 handle_sym_diff:
13482 if (saved_sym != NULL)
13483 {
13484 bfd_vma value;
13485 unsigned int reloc_size = 0;
13486 int leb_ret = 0;
13487 switch (reloc_type)
13488 {
13489 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
13490 reloc_size = 4;
13491 break;
13492 case 11: /* R_MSP430_GNU_SET_ULEB128 */
13493 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
13494 if (reloc->r_offset < (size_t) (end - start))
13495 read_leb128 (start + reloc->r_offset, end, false,
13496 &reloc_size, &leb_ret);
13497 break;
13498 default:
13499 reloc_size = 2;
13500 break;
13501 }
13502
13503 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
13504 error (_("MSP430 ULEB128 field at 0x%lx contains invalid "
13505 "ULEB128 value\n"),
13506 (long) reloc->r_offset);
13507 else if (sym_index >= num_syms)
13508 error (_("MSP430 reloc contains invalid symbol index %lu\n"),
13509 sym_index);
13510 else
13511 {
13512 value = reloc->r_addend + (symtab[sym_index].st_value
13513 - saved_sym->st_value);
13514
13515 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
13516 byte_put (start + reloc->r_offset, value, reloc_size);
13517 else
13518 /* PR 21137 */
13519 error (_("MSP430 sym diff reloc contains invalid offset: 0x%lx\n"),
13520 (long) reloc->r_offset);
13521 }
13522
13523 saved_sym = NULL;
13524 return true;
13525 }
13526 break;
13527
13528 default:
13529 if (saved_sym != NULL)
13530 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
13531 break;
13532 }
13533 break;
13534 }
13535
13536 case EM_MN10300:
13537 case EM_CYGNUS_MN10300:
13538 {
13539 static Elf_Internal_Sym * saved_sym = NULL;
13540
13541 if (reloc == NULL)
13542 {
13543 saved_sym = NULL;
13544 return true;
13545 }
13546
13547 switch (reloc_type)
13548 {
13549 case 34: /* R_MN10300_ALIGN */
13550 return true;
13551 case 33: /* R_MN10300_SYM_DIFF */
13552 if (sym_index >= num_syms)
13553 error (_("MN10300_SYM_DIFF reloc contains invalid symbol index %lu\n"),
13554 sym_index);
13555 else
13556 saved_sym = symtab + sym_index;
13557 return true;
13558
13559 case 1: /* R_MN10300_32 */
13560 case 2: /* R_MN10300_16 */
13561 if (saved_sym != NULL)
13562 {
13563 int reloc_size = reloc_type == 1 ? 4 : 2;
13564 bfd_vma value;
13565
13566 if (sym_index >= num_syms)
13567 error (_("MN10300 reloc contains invalid symbol index %lu\n"),
13568 sym_index);
13569 else
13570 {
13571 value = reloc->r_addend + (symtab[sym_index].st_value
13572 - saved_sym->st_value);
13573
13574 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
13575 byte_put (start + reloc->r_offset, value, reloc_size);
13576 else
13577 error (_("MN10300 sym diff reloc contains invalid offset: 0x%lx\n"),
13578 (long) reloc->r_offset);
13579 }
13580
13581 saved_sym = NULL;
13582 return true;
13583 }
13584 break;
13585 default:
13586 if (saved_sym != NULL)
13587 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
13588 break;
13589 }
13590 break;
13591 }
13592
13593 case EM_RL78:
13594 {
13595 static bfd_vma saved_sym1 = 0;
13596 static bfd_vma saved_sym2 = 0;
13597 static bfd_vma value;
13598
13599 if (reloc == NULL)
13600 {
13601 saved_sym1 = saved_sym2 = 0;
13602 return true;
13603 }
13604
13605 switch (reloc_type)
13606 {
13607 case 0x80: /* R_RL78_SYM. */
13608 saved_sym1 = saved_sym2;
13609 if (sym_index >= num_syms)
13610 error (_("RL78_SYM reloc contains invalid symbol index %lu\n"),
13611 sym_index);
13612 else
13613 {
13614 saved_sym2 = symtab[sym_index].st_value;
13615 saved_sym2 += reloc->r_addend;
13616 }
13617 return true;
13618
13619 case 0x83: /* R_RL78_OPsub. */
13620 value = saved_sym1 - saved_sym2;
13621 saved_sym2 = saved_sym1 = 0;
13622 return true;
13623 break;
13624
13625 case 0x41: /* R_RL78_ABS32. */
13626 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
13627 byte_put (start + reloc->r_offset, value, 4);
13628 else
13629 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13630 (long) reloc->r_offset);
13631 value = 0;
13632 return true;
13633
13634 case 0x43: /* R_RL78_ABS16. */
13635 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
13636 byte_put (start + reloc->r_offset, value, 2);
13637 else
13638 error (_("RL78 sym diff reloc contains invalid offset: 0x%lx\n"),
13639 (long) reloc->r_offset);
13640 value = 0;
13641 return true;
13642
13643 default:
13644 break;
13645 }
13646 break;
13647 }
13648 }
13649
13650 return false;
13651}
13652
13653/* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
13654 DWARF debug sections. This is a target specific test. Note - we do not
13655 go through the whole including-target-headers-multiple-times route, (as
13656 we have already done with <elf/h8.h>) because this would become very
13657 messy and even then this function would have to contain target specific
13658 information (the names of the relocs instead of their numeric values).
13659 FIXME: This is not the correct way to solve this problem. The proper way
13660 is to have target specific reloc sizing and typing functions created by
13661 the reloc-macros.h header, in the same way that it already creates the
13662 reloc naming functions. */
13663
13664static bool
13665is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13666{
13667 /* Please keep this table alpha-sorted for ease of visual lookup. */
13668 switch (filedata->file_header.e_machine)
13669 {
13670 case EM_386:
13671 case EM_IAMCU:
13672 return reloc_type == 1; /* R_386_32. */
13673 case EM_68K:
13674 return reloc_type == 1; /* R_68K_32. */
13675 case EM_860:
13676 return reloc_type == 1; /* R_860_32. */
13677 case EM_960:
13678 return reloc_type == 2; /* R_960_32. */
13679 case EM_AARCH64:
13680 return (reloc_type == 258
13681 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
13682 case EM_BPF:
13683 return reloc_type == 11; /* R_BPF_DATA_32 */
13684 case EM_ADAPTEVA_EPIPHANY:
13685 return reloc_type == 3;
13686 case EM_ALPHA:
13687 return reloc_type == 1; /* R_ALPHA_REFLONG. */
13688 case EM_ARC:
13689 return reloc_type == 1; /* R_ARC_32. */
13690 case EM_ARC_COMPACT:
13691 case EM_ARC_COMPACT2:
13692 return reloc_type == 4; /* R_ARC_32. */
13693 case EM_ARM:
13694 return reloc_type == 2; /* R_ARM_ABS32 */
13695 case EM_AVR_OLD:
13696 case EM_AVR:
13697 return reloc_type == 1;
13698 case EM_BLACKFIN:
13699 return reloc_type == 0x12; /* R_byte4_data. */
13700 case EM_CRIS:
13701 return reloc_type == 3; /* R_CRIS_32. */
13702 case EM_CR16:
13703 return reloc_type == 3; /* R_CR16_NUM32. */
13704 case EM_CRX:
13705 return reloc_type == 15; /* R_CRX_NUM32. */
13706 case EM_CSKY:
13707 return reloc_type == 1; /* R_CKCORE_ADDR32. */
13708 case EM_CYGNUS_FRV:
13709 return reloc_type == 1;
13710 case EM_CYGNUS_D10V:
13711 case EM_D10V:
13712 return reloc_type == 6; /* R_D10V_32. */
13713 case EM_CYGNUS_D30V:
13714 case EM_D30V:
13715 return reloc_type == 12; /* R_D30V_32_NORMAL. */
13716 case EM_DLX:
13717 return reloc_type == 3; /* R_DLX_RELOC_32. */
13718 case EM_CYGNUS_FR30:
13719 case EM_FR30:
13720 return reloc_type == 3; /* R_FR30_32. */
13721 case EM_FT32:
13722 return reloc_type == 1; /* R_FT32_32. */
13723 case EM_H8S:
13724 case EM_H8_300:
13725 case EM_H8_300H:
13726 return reloc_type == 1; /* R_H8_DIR32. */
13727 case EM_IA_64:
13728 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
13729 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
13730 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
13731 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
13732 case EM_IP2K_OLD:
13733 case EM_IP2K:
13734 return reloc_type == 2; /* R_IP2K_32. */
13735 case EM_IQ2000:
13736 return reloc_type == 2; /* R_IQ2000_32. */
13737 case EM_LATTICEMICO32:
13738 return reloc_type == 3; /* R_LM32_32. */
13739 case EM_LOONGARCH:
13740 return reloc_type == 1; /* R_LARCH_32. */
13741 case EM_M32C_OLD:
13742 case EM_M32C:
13743 return reloc_type == 3; /* R_M32C_32. */
13744 case EM_M32R:
13745 return reloc_type == 34; /* R_M32R_32_RELA. */
13746 case EM_68HC11:
13747 case EM_68HC12:
13748 return reloc_type == 6; /* R_M68HC11_32. */
13749 case EM_S12Z:
13750 return reloc_type == 7 || /* R_S12Z_EXT32 */
13751 reloc_type == 6; /* R_S12Z_CW32. */
13752 case EM_MCORE:
13753 return reloc_type == 1; /* R_MCORE_ADDR32. */
13754 case EM_CYGNUS_MEP:
13755 return reloc_type == 4; /* R_MEP_32. */
13756 case EM_METAG:
13757 return reloc_type == 2; /* R_METAG_ADDR32. */
13758 case EM_MICROBLAZE:
13759 return reloc_type == 1; /* R_MICROBLAZE_32. */
13760 case EM_MIPS:
13761 return reloc_type == 2; /* R_MIPS_32. */
13762 case EM_MMIX:
13763 return reloc_type == 4; /* R_MMIX_32. */
13764 case EM_CYGNUS_MN10200:
13765 case EM_MN10200:
13766 return reloc_type == 1; /* R_MN10200_32. */
13767 case EM_CYGNUS_MN10300:
13768 case EM_MN10300:
13769 return reloc_type == 1; /* R_MN10300_32. */
13770 case EM_MOXIE:
13771 return reloc_type == 1; /* R_MOXIE_32. */
13772 case EM_MSP430_OLD:
13773 case EM_MSP430:
13774 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
13775 case EM_MT:
13776 return reloc_type == 2; /* R_MT_32. */
13777 case EM_NDS32:
13778 return reloc_type == 20; /* R_NDS32_RELA. */
13779 case EM_ALTERA_NIOS2:
13780 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
13781 case EM_NIOS32:
13782 return reloc_type == 1; /* R_NIOS_32. */
13783 case EM_OR1K:
13784 return reloc_type == 1; /* R_OR1K_32. */
13785 case EM_PARISC:
13786 return (reloc_type == 1 /* R_PARISC_DIR32. */
13787 || reloc_type == 2 /* R_PARISC_DIR21L. */
13788 || reloc_type == 41); /* R_PARISC_SECREL32. */
13789 case EM_PJ:
13790 case EM_PJ_OLD:
13791 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
13792 case EM_PPC64:
13793 return reloc_type == 1; /* R_PPC64_ADDR32. */
13794 case EM_PPC:
13795 return reloc_type == 1; /* R_PPC_ADDR32. */
13796 case EM_TI_PRU:
13797 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
13798 case EM_RISCV:
13799 return reloc_type == 1; /* R_RISCV_32. */
13800 case EM_RL78:
13801 return reloc_type == 1; /* R_RL78_DIR32. */
13802 case EM_RX:
13803 return reloc_type == 1; /* R_RX_DIR32. */
13804 case EM_S370:
13805 return reloc_type == 1; /* R_I370_ADDR31. */
13806 case EM_S390_OLD:
13807 case EM_S390:
13808 return reloc_type == 4; /* R_S390_32. */
13809 case EM_SCORE:
13810 return reloc_type == 8; /* R_SCORE_ABS32. */
13811 case EM_SH:
13812 return reloc_type == 1; /* R_SH_DIR32. */
13813 case EM_SPARC32PLUS:
13814 case EM_SPARCV9:
13815 case EM_SPARC:
13816 return reloc_type == 3 /* R_SPARC_32. */
13817 || reloc_type == 23; /* R_SPARC_UA32. */
13818 case EM_SPU:
13819 return reloc_type == 6; /* R_SPU_ADDR32 */
13820 case EM_TI_C6000:
13821 return reloc_type == 1; /* R_C6000_ABS32. */
13822 case EM_TILEGX:
13823 return reloc_type == 2; /* R_TILEGX_32. */
13824 case EM_TILEPRO:
13825 return reloc_type == 1; /* R_TILEPRO_32. */
13826 case EM_CYGNUS_V850:
13827 case EM_V850:
13828 return reloc_type == 6; /* R_V850_ABS32. */
13829 case EM_V800:
13830 return reloc_type == 0x33; /* R_V810_WORD. */
13831 case EM_VAX:
13832 return reloc_type == 1; /* R_VAX_32. */
13833 case EM_VISIUM:
13834 return reloc_type == 3; /* R_VISIUM_32. */
13835 case EM_WEBASSEMBLY:
13836 return reloc_type == 1; /* R_WASM32_32. */
13837 case EM_X86_64:
13838 case EM_L1OM:
13839 case EM_K1OM:
13840 return reloc_type == 10; /* R_X86_64_32. */
13841 case EM_XC16X:
13842 case EM_C166:
13843 return reloc_type == 3; /* R_XC16C_ABS_32. */
13844 case EM_XGATE:
13845 return reloc_type == 4; /* R_XGATE_32. */
13846 case EM_XSTORMY16:
13847 return reloc_type == 1; /* R_XSTROMY16_32. */
13848 case EM_XTENSA_OLD:
13849 case EM_XTENSA:
13850 return reloc_type == 1; /* R_XTENSA_32. */
13851 case EM_Z80:
13852 return reloc_type == 6; /* R_Z80_32. */
13853 default:
13854 {
13855 static unsigned int prev_warn = 0;
13856
13857 /* Avoid repeating the same warning multiple times. */
13858 if (prev_warn != filedata->file_header.e_machine)
13859 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
13860 filedata->file_header.e_machine);
13861 prev_warn = filedata->file_header.e_machine;
13862 return false;
13863 }
13864 }
13865}
13866
13867/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13868 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
13869
13870static bool
13871is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
13872{
13873 switch (filedata->file_header.e_machine)
13874 /* Please keep this table alpha-sorted for ease of visual lookup. */
13875 {
13876 case EM_386:
13877 case EM_IAMCU:
13878 return reloc_type == 2; /* R_386_PC32. */
13879 case EM_68K:
13880 return reloc_type == 4; /* R_68K_PC32. */
13881 case EM_AARCH64:
13882 return reloc_type == 261; /* R_AARCH64_PREL32 */
13883 case EM_ADAPTEVA_EPIPHANY:
13884 return reloc_type == 6;
13885 case EM_ALPHA:
13886 return reloc_type == 10; /* R_ALPHA_SREL32. */
13887 case EM_ARC_COMPACT:
13888 case EM_ARC_COMPACT2:
13889 return reloc_type == 49; /* R_ARC_32_PCREL. */
13890 case EM_ARM:
13891 return reloc_type == 3; /* R_ARM_REL32 */
13892 case EM_AVR_OLD:
13893 case EM_AVR:
13894 return reloc_type == 36; /* R_AVR_32_PCREL. */
13895 case EM_MICROBLAZE:
13896 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
13897 case EM_OR1K:
13898 return reloc_type == 9; /* R_OR1K_32_PCREL. */
13899 case EM_PARISC:
13900 return reloc_type == 9; /* R_PARISC_PCREL32. */
13901 case EM_PPC:
13902 return reloc_type == 26; /* R_PPC_REL32. */
13903 case EM_PPC64:
13904 return reloc_type == 26; /* R_PPC64_REL32. */
13905 case EM_RISCV:
13906 return reloc_type == 57; /* R_RISCV_32_PCREL. */
13907 case EM_S390_OLD:
13908 case EM_S390:
13909 return reloc_type == 5; /* R_390_PC32. */
13910 case EM_SH:
13911 return reloc_type == 2; /* R_SH_REL32. */
13912 case EM_SPARC32PLUS:
13913 case EM_SPARCV9:
13914 case EM_SPARC:
13915 return reloc_type == 6; /* R_SPARC_DISP32. */
13916 case EM_SPU:
13917 return reloc_type == 13; /* R_SPU_REL32. */
13918 case EM_TILEGX:
13919 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
13920 case EM_TILEPRO:
13921 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
13922 case EM_VISIUM:
13923 return reloc_type == 6; /* R_VISIUM_32_PCREL */
13924 case EM_X86_64:
13925 case EM_L1OM:
13926 case EM_K1OM:
13927 return reloc_type == 2; /* R_X86_64_PC32. */
13928 case EM_VAX:
13929 return reloc_type == 4; /* R_VAX_PCREL32. */
13930 case EM_XTENSA_OLD:
13931 case EM_XTENSA:
13932 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
13933 default:
13934 /* Do not abort or issue an error message here. Not all targets use
13935 pc-relative 32-bit relocs in their DWARF debug information and we
13936 have already tested for target coverage in is_32bit_abs_reloc. A
13937 more helpful warning message will be generated by apply_relocations
13938 anyway, so just return. */
13939 return false;
13940 }
13941}
13942
13943/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
13944 a 64-bit absolute RELA relocation used in DWARF debug sections. */
13945
13946static bool
13947is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
13948{
13949 switch (filedata->file_header.e_machine)
13950 {
13951 case EM_AARCH64:
13952 return reloc_type == 257; /* R_AARCH64_ABS64. */
13953 case EM_ALPHA:
13954 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
13955 case EM_IA_64:
13956 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
13957 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
13958 case EM_LOONGARCH:
13959 return reloc_type == 2; /* R_LARCH_64 */
13960 case EM_PARISC:
13961 return reloc_type == 80; /* R_PARISC_DIR64. */
13962 case EM_PPC64:
13963 return reloc_type == 38; /* R_PPC64_ADDR64. */
13964 case EM_RISCV:
13965 return reloc_type == 2; /* R_RISCV_64. */
13966 case EM_SPARC32PLUS:
13967 case EM_SPARCV9:
13968 case EM_SPARC:
13969 return reloc_type == 32 /* R_SPARC_64. */
13970 || reloc_type == 54; /* R_SPARC_UA64. */
13971 case EM_X86_64:
13972 case EM_L1OM:
13973 case EM_K1OM:
13974 return reloc_type == 1; /* R_X86_64_64. */
13975 case EM_S390_OLD:
13976 case EM_S390:
13977 return reloc_type == 22; /* R_S390_64. */
13978 case EM_TILEGX:
13979 return reloc_type == 1; /* R_TILEGX_64. */
13980 case EM_MIPS:
13981 return reloc_type == 18; /* R_MIPS_64. */
13982 default:
13983 return false;
13984 }
13985}
13986
13987/* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
13988 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
13989
13990static bool
13991is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
13992{
13993 switch (filedata->file_header.e_machine)
13994 {
13995 case EM_AARCH64:
13996 return reloc_type == 260; /* R_AARCH64_PREL64. */
13997 case EM_ALPHA:
13998 return reloc_type == 11; /* R_ALPHA_SREL64. */
13999 case EM_IA_64:
14000 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
14001 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
14002 case EM_PARISC:
14003 return reloc_type == 72; /* R_PARISC_PCREL64. */
14004 case EM_PPC64:
14005 return reloc_type == 44; /* R_PPC64_REL64. */
14006 case EM_SPARC32PLUS:
14007 case EM_SPARCV9:
14008 case EM_SPARC:
14009 return reloc_type == 46; /* R_SPARC_DISP64. */
14010 case EM_X86_64:
14011 case EM_L1OM:
14012 case EM_K1OM:
14013 return reloc_type == 24; /* R_X86_64_PC64. */
14014 case EM_S390_OLD:
14015 case EM_S390:
14016 return reloc_type == 23; /* R_S390_PC64. */
14017 case EM_TILEGX:
14018 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
14019 default:
14020 return false;
14021 }
14022}
14023
14024/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14025 a 24-bit absolute RELA relocation used in DWARF debug sections. */
14026
14027static bool
14028is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14029{
14030 switch (filedata->file_header.e_machine)
14031 {
14032 case EM_CYGNUS_MN10200:
14033 case EM_MN10200:
14034 return reloc_type == 4; /* R_MN10200_24. */
14035 case EM_FT32:
14036 return reloc_type == 5; /* R_FT32_20. */
14037 case EM_Z80:
14038 return reloc_type == 5; /* R_Z80_24. */
14039 default:
14040 return false;
14041 }
14042}
14043
14044/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14045 a 16-bit absolute RELA relocation used in DWARF debug sections. */
14046
14047static bool
14048is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14049{
14050 /* Please keep this table alpha-sorted for ease of visual lookup. */
14051 switch (filedata->file_header.e_machine)
14052 {
14053 case EM_ARC:
14054 case EM_ARC_COMPACT:
14055 case EM_ARC_COMPACT2:
14056 return reloc_type == 2; /* R_ARC_16. */
14057 case EM_ADAPTEVA_EPIPHANY:
14058 return reloc_type == 5;
14059 case EM_AVR_OLD:
14060 case EM_AVR:
14061 return reloc_type == 4; /* R_AVR_16. */
14062 case EM_CYGNUS_D10V:
14063 case EM_D10V:
14064 return reloc_type == 3; /* R_D10V_16. */
14065 case EM_FT32:
14066 return reloc_type == 2; /* R_FT32_16. */
14067 case EM_H8S:
14068 case EM_H8_300:
14069 case EM_H8_300H:
14070 return reloc_type == R_H8_DIR16;
14071 case EM_IP2K_OLD:
14072 case EM_IP2K:
14073 return reloc_type == 1; /* R_IP2K_16. */
14074 case EM_M32C_OLD:
14075 case EM_M32C:
14076 return reloc_type == 1; /* R_M32C_16 */
14077 case EM_CYGNUS_MN10200:
14078 case EM_MN10200:
14079 return reloc_type == 2; /* R_MN10200_16. */
14080 case EM_CYGNUS_MN10300:
14081 case EM_MN10300:
14082 return reloc_type == 2; /* R_MN10300_16. */
14083 case EM_MSP430:
14084 if (uses_msp430x_relocs (filedata))
14085 return reloc_type == 2; /* R_MSP430_ABS16. */
14086 /* Fall through. */
14087 case EM_MSP430_OLD:
14088 return reloc_type == 5; /* R_MSP430_16_BYTE. */
14089 case EM_NDS32:
14090 return reloc_type == 19; /* R_NDS32_RELA. */
14091 case EM_ALTERA_NIOS2:
14092 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
14093 case EM_NIOS32:
14094 return reloc_type == 9; /* R_NIOS_16. */
14095 case EM_OR1K:
14096 return reloc_type == 2; /* R_OR1K_16. */
14097 case EM_RISCV:
14098 return reloc_type == 55; /* R_RISCV_SET16. */
14099 case EM_TI_PRU:
14100 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
14101 case EM_TI_C6000:
14102 return reloc_type == 2; /* R_C6000_ABS16. */
14103 case EM_VISIUM:
14104 return reloc_type == 2; /* R_VISIUM_16. */
14105 case EM_XC16X:
14106 case EM_C166:
14107 return reloc_type == 2; /* R_XC16C_ABS_16. */
14108 case EM_XGATE:
14109 return reloc_type == 3; /* R_XGATE_16. */
14110 case EM_Z80:
14111 return reloc_type == 4; /* R_Z80_16. */
14112 default:
14113 return false;
14114 }
14115}
14116
14117/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14118 a 8-bit absolute RELA relocation used in DWARF debug sections. */
14119
14120static bool
14121is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14122{
14123 switch (filedata->file_header.e_machine)
14124 {
14125 case EM_RISCV:
14126 return reloc_type == 54; /* R_RISCV_SET8. */
14127 case EM_Z80:
14128 return reloc_type == 1; /* R_Z80_8. */
14129 default:
14130 return false;
14131 }
14132}
14133
14134/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14135 a 6-bit absolute RELA relocation used in DWARF debug sections. */
14136
14137static bool
14138is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14139{
14140 switch (filedata->file_header.e_machine)
14141 {
14142 case EM_RISCV:
14143 return reloc_type == 53; /* R_RISCV_SET6. */
14144 default:
14145 return false;
14146 }
14147}
14148
14149/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14150 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
14151
14152static bool
14153is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14154{
14155 /* Please keep this table alpha-sorted for ease of visual lookup. */
14156 switch (filedata->file_header.e_machine)
14157 {
14158 case EM_RISCV:
14159 return reloc_type == 35; /* R_RISCV_ADD32. */
14160 default:
14161 return false;
14162 }
14163}
14164
14165/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14166 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
14167
14168static bool
14169is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14170{
14171 /* Please keep this table alpha-sorted for ease of visual lookup. */
14172 switch (filedata->file_header.e_machine)
14173 {
14174 case EM_RISCV:
14175 return reloc_type == 39; /* R_RISCV_SUB32. */
14176 default:
14177 return false;
14178 }
14179}
14180
14181/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14182 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
14183
14184static bool
14185is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14186{
14187 /* Please keep this table alpha-sorted for ease of visual lookup. */
14188 switch (filedata->file_header.e_machine)
14189 {
14190 case EM_RISCV:
14191 return reloc_type == 36; /* R_RISCV_ADD64. */
14192 default:
14193 return false;
14194 }
14195}
14196
14197/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14198 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
14199
14200static bool
14201is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14202{
14203 /* Please keep this table alpha-sorted for ease of visual lookup. */
14204 switch (filedata->file_header.e_machine)
14205 {
14206 case EM_RISCV:
14207 return reloc_type == 40; /* R_RISCV_SUB64. */
14208 default:
14209 return false;
14210 }
14211}
14212
14213/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14214 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
14215
14216static bool
14217is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14218{
14219 /* Please keep this table alpha-sorted for ease of visual lookup. */
14220 switch (filedata->file_header.e_machine)
14221 {
14222 case EM_RISCV:
14223 return reloc_type == 34; /* R_RISCV_ADD16. */
14224 default:
14225 return false;
14226 }
14227}
14228
14229/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14230 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
14231
14232static bool
14233is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14234{
14235 /* Please keep this table alpha-sorted for ease of visual lookup. */
14236 switch (filedata->file_header.e_machine)
14237 {
14238 case EM_RISCV:
14239 return reloc_type == 38; /* R_RISCV_SUB16. */
14240 default:
14241 return false;
14242 }
14243}
14244
14245/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14246 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
14247
14248static bool
14249is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
14250{
14251 /* Please keep this table alpha-sorted for ease of visual lookup. */
14252 switch (filedata->file_header.e_machine)
14253 {
14254 case EM_RISCV:
14255 return reloc_type == 33; /* R_RISCV_ADD8. */
14256 default:
14257 return false;
14258 }
14259}
14260
14261/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14262 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
14263
14264static bool
14265is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14266{
14267 /* Please keep this table alpha-sorted for ease of visual lookup. */
14268 switch (filedata->file_header.e_machine)
14269 {
14270 case EM_RISCV:
14271 return reloc_type == 37; /* R_RISCV_SUB8. */
14272 default:
14273 return false;
14274 }
14275}
14276
14277/* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
14278 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
14279
14280static bool
14281is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
14282{
14283 switch (filedata->file_header.e_machine)
14284 {
14285 case EM_RISCV:
14286 return reloc_type == 52; /* R_RISCV_SUB6. */
14287 default:
14288 return false;
14289 }
14290}
14291
14292/* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
14293 relocation entries (possibly formerly used for SHT_GROUP sections). */
14294
14295static bool
14296is_none_reloc (Filedata * filedata, unsigned int reloc_type)
14297{
14298 switch (filedata->file_header.e_machine)
14299 {
14300 case EM_386: /* R_386_NONE. */
14301 case EM_68K: /* R_68K_NONE. */
14302 case EM_ADAPTEVA_EPIPHANY:
14303 case EM_ALPHA: /* R_ALPHA_NONE. */
14304 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
14305 case EM_ARC: /* R_ARC_NONE. */
14306 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
14307 case EM_ARC_COMPACT: /* R_ARC_NONE. */
14308 case EM_ARM: /* R_ARM_NONE. */
14309 case EM_C166: /* R_XC16X_NONE. */
14310 case EM_CRIS: /* R_CRIS_NONE. */
14311 case EM_FT32: /* R_FT32_NONE. */
14312 case EM_IA_64: /* R_IA64_NONE. */
14313 case EM_K1OM: /* R_X86_64_NONE. */
14314 case EM_L1OM: /* R_X86_64_NONE. */
14315 case EM_M32R: /* R_M32R_NONE. */
14316 case EM_MIPS: /* R_MIPS_NONE. */
14317 case EM_MN10300: /* R_MN10300_NONE. */
14318 case EM_MOXIE: /* R_MOXIE_NONE. */
14319 case EM_NIOS32: /* R_NIOS_NONE. */
14320 case EM_OR1K: /* R_OR1K_NONE. */
14321 case EM_PARISC: /* R_PARISC_NONE. */
14322 case EM_PPC64: /* R_PPC64_NONE. */
14323 case EM_PPC: /* R_PPC_NONE. */
14324 case EM_RISCV: /* R_RISCV_NONE. */
14325 case EM_S390: /* R_390_NONE. */
14326 case EM_S390_OLD:
14327 case EM_SH: /* R_SH_NONE. */
14328 case EM_SPARC32PLUS:
14329 case EM_SPARC: /* R_SPARC_NONE. */
14330 case EM_SPARCV9:
14331 case EM_TILEGX: /* R_TILEGX_NONE. */
14332 case EM_TILEPRO: /* R_TILEPRO_NONE. */
14333 case EM_TI_C6000:/* R_C6000_NONE. */
14334 case EM_X86_64: /* R_X86_64_NONE. */
14335 case EM_XC16X:
14336 case EM_Z80: /* R_Z80_NONE. */
14337 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
14338 return reloc_type == 0;
14339
14340 case EM_AARCH64:
14341 return reloc_type == 0 || reloc_type == 256;
14342 case EM_AVR_OLD:
14343 case EM_AVR:
14344 return (reloc_type == 0 /* R_AVR_NONE. */
14345 || reloc_type == 30 /* R_AVR_DIFF8. */
14346 || reloc_type == 31 /* R_AVR_DIFF16. */
14347 || reloc_type == 32 /* R_AVR_DIFF32. */);
14348 case EM_METAG:
14349 return reloc_type == 3; /* R_METAG_NONE. */
14350 case EM_NDS32:
14351 return (reloc_type == 0 /* R_XTENSA_NONE. */
14352 || reloc_type == 204 /* R_NDS32_DIFF8. */
14353 || reloc_type == 205 /* R_NDS32_DIFF16. */
14354 || reloc_type == 206 /* R_NDS32_DIFF32. */
14355 || reloc_type == 207 /* R_NDS32_ULEB128. */);
14356 case EM_TI_PRU:
14357 return (reloc_type == 0 /* R_PRU_NONE. */
14358 || reloc_type == 65 /* R_PRU_DIFF8. */
14359 || reloc_type == 66 /* R_PRU_DIFF16. */
14360 || reloc_type == 67 /* R_PRU_DIFF32. */);
14361 case EM_XTENSA_OLD:
14362 case EM_XTENSA:
14363 return (reloc_type == 0 /* R_XTENSA_NONE. */
14364 || reloc_type == 17 /* R_XTENSA_DIFF8. */
14365 || reloc_type == 18 /* R_XTENSA_DIFF16. */
14366 || reloc_type == 19 /* R_XTENSA_DIFF32. */
14367 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
14368 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
14369 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
14370 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
14371 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
14372 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
14373 }
14374 return false;
14375}
14376
14377/* Returns TRUE if there is a relocation against
14378 section NAME at OFFSET bytes. */
14379
14380bool
14381reloc_at (struct dwarf_section * dsec, dwarf_vma offset)
14382{
14383 Elf_Internal_Rela * relocs;
14384 Elf_Internal_Rela * rp;
14385
14386 if (dsec == NULL || dsec->reloc_info == NULL)
14387 return false;
14388
14389 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
14390
14391 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
14392 if (rp->r_offset == offset)
14393 return true;
14394
14395 return false;
14396}
14397
14398/* Apply relocations to a section.
14399 Returns TRUE upon success, FALSE otherwise.
14400 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
14401 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
14402 will be set to the number of relocs loaded.
14403
14404 Note: So far support has been added only for those relocations
14405 which can be found in debug sections. FIXME: Add support for
14406 more relocations ? */
14407
14408static bool
14409apply_relocations (Filedata * filedata,
14410 const Elf_Internal_Shdr * section,
14411 unsigned char * start,
14412 bfd_size_type size,
14413 void ** relocs_return,
14414 unsigned long * num_relocs_return)
14415{
14416 Elf_Internal_Shdr * relsec;
14417 unsigned char * end = start + size;
14418
14419 if (relocs_return != NULL)
14420 {
14421 * (Elf_Internal_Rela **) relocs_return = NULL;
14422 * num_relocs_return = 0;
14423 }
14424
14425 if (filedata->file_header.e_type != ET_REL)
14426 /* No relocs to apply. */
14427 return true;
14428
14429 /* Find the reloc section associated with the section. */
14430 for (relsec = filedata->section_headers;
14431 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14432 ++relsec)
14433 {
14434 bool is_rela;
14435 unsigned long num_relocs;
14436 Elf_Internal_Rela * relocs;
14437 Elf_Internal_Rela * rp;
14438 Elf_Internal_Shdr * symsec;
14439 Elf_Internal_Sym * symtab;
14440 unsigned long num_syms;
14441 Elf_Internal_Sym * sym;
14442
14443 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14444 || relsec->sh_info >= filedata->file_header.e_shnum
14445 || filedata->section_headers + relsec->sh_info != section
14446 || relsec->sh_size == 0
14447 || relsec->sh_link >= filedata->file_header.e_shnum)
14448 continue;
14449
14450 symsec = filedata->section_headers + relsec->sh_link;
14451 if (symsec->sh_type != SHT_SYMTAB
14452 && symsec->sh_type != SHT_DYNSYM)
14453 return false;
14454
14455 is_rela = relsec->sh_type == SHT_RELA;
14456
14457 if (is_rela)
14458 {
14459 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
14460 relsec->sh_size, & relocs, & num_relocs))
14461 return false;
14462 }
14463 else
14464 {
14465 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
14466 relsec->sh_size, & relocs, & num_relocs))
14467 return false;
14468 }
14469
14470 /* SH uses RELA but uses in place value instead of the addend field. */
14471 if (filedata->file_header.e_machine == EM_SH)
14472 is_rela = false;
14473
14474 symtab = get_elf_symbols (filedata, symsec, & num_syms);
14475
14476 for (rp = relocs; rp < relocs + num_relocs; ++rp)
14477 {
14478 bfd_vma addend;
14479 unsigned int reloc_type;
14480 unsigned int reloc_size;
14481 bool reloc_inplace = false;
14482 bool reloc_subtract = false;
14483 unsigned char *rloc;
14484 unsigned long sym_index;
14485
14486 reloc_type = get_reloc_type (filedata, rp->r_info);
14487
14488 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
14489 continue;
14490 else if (is_none_reloc (filedata, reloc_type))
14491 continue;
14492 else if (is_32bit_abs_reloc (filedata, reloc_type)
14493 || is_32bit_pcrel_reloc (filedata, reloc_type))
14494 reloc_size = 4;
14495 else if (is_64bit_abs_reloc (filedata, reloc_type)
14496 || is_64bit_pcrel_reloc (filedata, reloc_type))
14497 reloc_size = 8;
14498 else if (is_24bit_abs_reloc (filedata, reloc_type))
14499 reloc_size = 3;
14500 else if (is_16bit_abs_reloc (filedata, reloc_type))
14501 reloc_size = 2;
14502 else if (is_8bit_abs_reloc (filedata, reloc_type)
14503 || is_6bit_abs_reloc (filedata, reloc_type))
14504 reloc_size = 1;
14505 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
14506 reloc_type))
14507 || is_32bit_inplace_add_reloc (filedata, reloc_type))
14508 {
14509 reloc_size = 4;
14510 reloc_inplace = true;
14511 }
14512 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
14513 reloc_type))
14514 || is_64bit_inplace_add_reloc (filedata, reloc_type))
14515 {
14516 reloc_size = 8;
14517 reloc_inplace = true;
14518 }
14519 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
14520 reloc_type))
14521 || is_16bit_inplace_add_reloc (filedata, reloc_type))
14522 {
14523 reloc_size = 2;
14524 reloc_inplace = true;
14525 }
14526 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
14527 reloc_type))
14528 || is_8bit_inplace_add_reloc (filedata, reloc_type))
14529 {
14530 reloc_size = 1;
14531 reloc_inplace = true;
14532 }
14533 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
14534 reloc_type)))
14535 {
14536 reloc_size = 1;
14537 reloc_inplace = true;
14538 }
14539 else
14540 {
14541 static unsigned int prev_reloc = 0;
14542
14543 if (reloc_type != prev_reloc)
14544 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
14545 reloc_type, printable_section_name (filedata, section));
14546 prev_reloc = reloc_type;
14547 continue;
14548 }
14549
14550 rloc = start + rp->r_offset;
14551 if (!IN_RANGE (start, end, rloc, reloc_size))
14552 {
14553 warn (_("skipping invalid relocation offset 0x%lx in section %s\n"),
14554 (unsigned long) rp->r_offset,
14555 printable_section_name (filedata, section));
14556 continue;
14557 }
14558
14559 sym_index = (unsigned long) get_reloc_symindex (rp->r_info);
14560 if (sym_index >= num_syms)
14561 {
14562 warn (_("skipping invalid relocation symbol index 0x%lx in section %s\n"),
14563 sym_index, printable_section_name (filedata, section));
14564 continue;
14565 }
14566 sym = symtab + sym_index;
14567
14568 /* If the reloc has a symbol associated with it,
14569 make sure that it is of an appropriate type.
14570
14571 Relocations against symbols without type can happen.
14572 Gcc -feliminate-dwarf2-dups may generate symbols
14573 without type for debug info.
14574
14575 Icc generates relocations against function symbols
14576 instead of local labels.
14577
14578 Relocations against object symbols can happen, eg when
14579 referencing a global array. For an example of this see
14580 the _clz.o binary in libgcc.a. */
14581 if (sym != symtab
14582 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
14583 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
14584 {
14585 warn (_("skipping unexpected symbol type %s in section %s relocation %ld\n"),
14586 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
14587 printable_section_name (filedata, relsec),
14588 (long int)(rp - relocs));
14589 continue;
14590 }
14591
14592 addend = 0;
14593 if (is_rela)
14594 addend += rp->r_addend;
14595 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
14596 partial_inplace. */
14597 if (!is_rela
14598 || (filedata->file_header.e_machine == EM_XTENSA
14599 && reloc_type == 1)
14600 || ((filedata->file_header.e_machine == EM_PJ
14601 || filedata->file_header.e_machine == EM_PJ_OLD)
14602 && reloc_type == 1)
14603 || ((filedata->file_header.e_machine == EM_D30V
14604 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
14605 && reloc_type == 12)
14606 || reloc_inplace)
14607 {
14608 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
14609 addend += byte_get (rloc, reloc_size) & 0x3f;
14610 else
14611 addend += byte_get (rloc, reloc_size);
14612 }
14613
14614 if (is_32bit_pcrel_reloc (filedata, reloc_type)
14615 || is_64bit_pcrel_reloc (filedata, reloc_type))
14616 {
14617 /* On HPPA, all pc-relative relocations are biased by 8. */
14618 if (filedata->file_header.e_machine == EM_PARISC)
14619 addend -= 8;
14620 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
14621 reloc_size);
14622 }
14623 else if (is_6bit_abs_reloc (filedata, reloc_type)
14624 || is_6bit_inplace_sub_reloc (filedata, reloc_type))
14625 {
14626 if (reloc_subtract)
14627 addend -= sym->st_value;
14628 else
14629 addend += sym->st_value;
14630 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
14631 byte_put (rloc, addend, reloc_size);
14632 }
14633 else if (reloc_subtract)
14634 byte_put (rloc, addend - sym->st_value, reloc_size);
14635 else
14636 byte_put (rloc, addend + sym->st_value, reloc_size);
14637 }
14638
14639 free (symtab);
14640 /* Let the target specific reloc processing code know that
14641 we have finished with these relocs. */
14642 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
14643
14644 if (relocs_return)
14645 {
14646 * (Elf_Internal_Rela **) relocs_return = relocs;
14647 * num_relocs_return = num_relocs;
14648 }
14649 else
14650 free (relocs);
14651
14652 break;
14653 }
14654
14655 return true;
14656}
14657
14658#ifdef SUPPORT_DISASSEMBLY
14659static bool
14660disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
14661{
14662 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
14663
14664 /* FIXME: XXX -- to be done --- XXX */
14665
14666 return true;
14667}
14668#endif
14669
14670/* Reads in the contents of SECTION from FILE, returning a pointer
14671 to a malloc'ed buffer or NULL if something went wrong. */
14672
14673static char *
14674get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
14675{
14676 bfd_size_type num_bytes = section->sh_size;
14677
14678 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
14679 {
14680 printf (_("Section '%s' has no data to dump.\n"),
14681 printable_section_name (filedata, section));
14682 return NULL;
14683 }
14684
14685 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
14686 _("section contents"));
14687}
14688
14689/* Uncompresses a section that was compressed using zlib, in place. */
14690
14691static bool
14692uncompress_section_contents (unsigned char ** buffer,
14693 dwarf_size_type uncompressed_size,
14694 dwarf_size_type * size)
14695{
14696 dwarf_size_type compressed_size = *size;
14697 unsigned char * compressed_buffer = *buffer;
14698 unsigned char * uncompressed_buffer;
14699 z_stream strm;
14700 int rc;
14701
14702 /* It is possible the section consists of several compressed
14703 buffers concatenated together, so we uncompress in a loop. */
14704 /* PR 18313: The state field in the z_stream structure is supposed
14705 to be invisible to the user (ie us), but some compilers will
14706 still complain about it being used without initialisation. So
14707 we first zero the entire z_stream structure and then set the fields
14708 that we need. */
14709 memset (& strm, 0, sizeof strm);
14710 strm.avail_in = compressed_size;
14711 strm.next_in = (Bytef *) compressed_buffer;
14712 strm.avail_out = uncompressed_size;
14713 uncompressed_buffer = (unsigned char *) xmalloc (uncompressed_size);
14714
14715 rc = inflateInit (& strm);
14716 while (strm.avail_in > 0)
14717 {
14718 if (rc != Z_OK)
14719 break;
14720 strm.next_out = ((Bytef *) uncompressed_buffer
14721 + (uncompressed_size - strm.avail_out));
14722 rc = inflate (&strm, Z_FINISH);
14723 if (rc != Z_STREAM_END)
14724 break;
14725 rc = inflateReset (& strm);
14726 }
14727 if (inflateEnd (& strm) != Z_OK
14728 || rc != Z_OK
14729 || strm.avail_out != 0)
14730 goto fail;
14731
14732 *buffer = uncompressed_buffer;
14733 *size = uncompressed_size;
14734 return true;
14735
14736 fail:
14737 free (uncompressed_buffer);
14738 /* Indicate decompression failure. */
14739 *buffer = NULL;
14740 return false;
14741}
14742
14743static bool
14744dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
14745{
14746 Elf_Internal_Shdr *relsec;
14747 bfd_size_type num_bytes;
14748 unsigned char *data;
14749 unsigned char *end;
14750 unsigned char *real_start;
14751 unsigned char *start;
14752 bool some_strings_shown;
14753
14754 real_start = start = (unsigned char *) get_section_contents (section, filedata);
14755 if (start == NULL)
14756 /* PR 21820: Do not fail if the section was empty. */
14757 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
14758
14759 num_bytes = section->sh_size;
14760
14761 if (filedata->is_separate)
14762 printf (_("\nString dump of section '%s' in linked file %s:\n"),
14763 printable_section_name (filedata, section),
14764 filedata->file_name);
14765 else
14766 printf (_("\nString dump of section '%s':\n"),
14767 printable_section_name (filedata, section));
14768
14769 if (decompress_dumps)
14770 {
14771 dwarf_size_type new_size = num_bytes;
14772 dwarf_size_type uncompressed_size = 0;
14773
14774 if ((section->sh_flags & SHF_COMPRESSED) != 0)
14775 {
14776 Elf_Internal_Chdr chdr;
14777 unsigned int compression_header_size
14778 = get_compression_header (& chdr, (unsigned char *) start,
14779 num_bytes);
14780 if (compression_header_size == 0)
14781 /* An error message will have already been generated
14782 by get_compression_header. */
14783 goto error_out;
14784
14785 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
14786 {
14787 warn (_("section '%s' has unsupported compress type: %d\n"),
14788 printable_section_name (filedata, section), chdr.ch_type);
14789 goto error_out;
14790 }
14791 uncompressed_size = chdr.ch_size;
14792 start += compression_header_size;
14793 new_size -= compression_header_size;
14794 }
14795 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
14796 {
14797 /* Read the zlib header. In this case, it should be "ZLIB"
14798 followed by the uncompressed section size, 8 bytes in
14799 big-endian order. */
14800 uncompressed_size = start[4]; uncompressed_size <<= 8;
14801 uncompressed_size += start[5]; uncompressed_size <<= 8;
14802 uncompressed_size += start[6]; uncompressed_size <<= 8;
14803 uncompressed_size += start[7]; uncompressed_size <<= 8;
14804 uncompressed_size += start[8]; uncompressed_size <<= 8;
14805 uncompressed_size += start[9]; uncompressed_size <<= 8;
14806 uncompressed_size += start[10]; uncompressed_size <<= 8;
14807 uncompressed_size += start[11];
14808 start += 12;
14809 new_size -= 12;
14810 }
14811
14812 if (uncompressed_size)
14813 {
14814 if (uncompress_section_contents (& start,
14815 uncompressed_size, & new_size))
14816 num_bytes = new_size;
14817 else
14818 {
14819 error (_("Unable to decompress section %s\n"),
14820 printable_section_name (filedata, section));
14821 goto error_out;
14822 }
14823 }
14824 else
14825 start = real_start;
14826 }
14827
14828 /* If the section being dumped has relocations against it the user might
14829 be expecting these relocations to have been applied. Check for this
14830 case and issue a warning message in order to avoid confusion.
14831 FIXME: Maybe we ought to have an option that dumps a section with
14832 relocs applied ? */
14833 for (relsec = filedata->section_headers;
14834 relsec < filedata->section_headers + filedata->file_header.e_shnum;
14835 ++relsec)
14836 {
14837 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
14838 || relsec->sh_info >= filedata->file_header.e_shnum
14839 || filedata->section_headers + relsec->sh_info != section
14840 || relsec->sh_size == 0
14841 || relsec->sh_link >= filedata->file_header.e_shnum)
14842 continue;
14843
14844 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
14845 break;
14846 }
14847
14848 data = start;
14849 end = start + num_bytes;
14850 some_strings_shown = false;
14851
14852#ifdef HAVE_MBSTATE_T
14853 mbstate_t state;
14854 /* Initialise the multibyte conversion state. */
14855 memset (& state, 0, sizeof (state));
14856#endif
14857
14858 bool continuing = false;
14859
14860 while (data < end)
14861 {
14862 while (!ISPRINT (* data))
14863 if (++ data >= end)
14864 break;
14865
14866 if (data < end)
14867 {
14868 size_t maxlen = end - data;
14869
14870 if (continuing)
14871 {
14872 printf (" ");
14873 continuing = false;
14874 }
14875 else
14876 {
14877 printf (" [%6lx] ", (unsigned long) (data - start));
14878 }
14879
14880 if (maxlen > 0)
14881 {
14882 char c = 0;
14883
14884 while (maxlen)
14885 {
14886 c = *data++;
14887
14888 if (c == 0)
14889 break;
14890
14891 /* PR 25543: Treat new-lines as string-ending characters. */
14892 if (c == '\n')
14893 {
14894 printf ("\\n\n");
14895 if (*data != 0)
14896 continuing = true;
14897 break;
14898 }
14899
14900 /* Do not print control characters directly as they can affect terminal
14901 settings. Such characters usually appear in the names generated
14902 by the assembler for local labels. */
14903 if (ISCNTRL (c))
14904 {
14905 printf ("^%c", c + 0x40);
14906 }
14907 else if (ISPRINT (c))
14908 {
14909 putchar (c);
14910 }
14911 else
14912 {
14913 size_t n;
14914#ifdef HAVE_MBSTATE_T
14915 wchar_t w;
14916#endif
14917 /* Let printf do the hard work of displaying multibyte characters. */
14918 printf ("%.1s", data - 1);
14919#ifdef HAVE_MBSTATE_T
14920 /* Try to find out how many bytes made up the character that was
14921 just printed. Advance the symbol pointer past the bytes that
14922 were displayed. */
14923 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
14924#else
14925 n = 1;
14926#endif
14927 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
14928 data += (n - 1);
14929 }
14930 }
14931
14932 if (c != '\n')
14933 putchar ('\n');
14934 }
14935 else
14936 {
14937 printf (_("<corrupt>\n"));
14938 data = end;
14939 }
14940 some_strings_shown = true;
14941 }
14942 }
14943
14944 if (! some_strings_shown)
14945 printf (_(" No strings found in this section."));
14946
14947 free (real_start);
14948
14949 putchar ('\n');
14950 return true;
14951
14952error_out:
14953 free (real_start);
14954 return false;
14955}
14956
14957static bool
14958dump_section_as_bytes (Elf_Internal_Shdr *section,
14959 Filedata *filedata,
14960 bool relocate)
14961{
14962 Elf_Internal_Shdr * relsec;
14963 bfd_size_type bytes;
14964 bfd_size_type section_size;
14965 bfd_vma addr;
14966 unsigned char * data;
14967 unsigned char * real_start;
14968 unsigned char * start;
14969
14970 real_start = start = (unsigned char *) get_section_contents (section, filedata);
14971 if (start == NULL)
14972 /* PR 21820: Do not fail if the section was empty. */
14973 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
14974
14975 section_size = section->sh_size;
14976
14977 if (filedata->is_separate)
14978 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
14979 printable_section_name (filedata, section),
14980 filedata->file_name);
14981 else
14982 printf (_("\nHex dump of section '%s':\n"),
14983 printable_section_name (filedata, section));
14984
14985 if (decompress_dumps)
14986 {
14987 dwarf_size_type new_size = section_size;
14988 dwarf_size_type uncompressed_size = 0;
14989
14990 if ((section->sh_flags & SHF_COMPRESSED) != 0)
14991 {
14992 Elf_Internal_Chdr chdr;
14993 unsigned int compression_header_size
14994 = get_compression_header (& chdr, start, section_size);
14995
14996 if (compression_header_size == 0)
14997 /* An error message will have already been generated
14998 by get_compression_header. */
14999 goto error_out;
15000
15001 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15002 {
15003 warn (_("section '%s' has unsupported compress type: %d\n"),
15004 printable_section_name (filedata, section), chdr.ch_type);
15005 goto error_out;
15006 }
15007 uncompressed_size = chdr.ch_size;
15008 start += compression_header_size;
15009 new_size -= compression_header_size;
15010 }
15011 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
15012 {
15013 /* Read the zlib header. In this case, it should be "ZLIB"
15014 followed by the uncompressed section size, 8 bytes in
15015 big-endian order. */
15016 uncompressed_size = start[4]; uncompressed_size <<= 8;
15017 uncompressed_size += start[5]; uncompressed_size <<= 8;
15018 uncompressed_size += start[6]; uncompressed_size <<= 8;
15019 uncompressed_size += start[7]; uncompressed_size <<= 8;
15020 uncompressed_size += start[8]; uncompressed_size <<= 8;
15021 uncompressed_size += start[9]; uncompressed_size <<= 8;
15022 uncompressed_size += start[10]; uncompressed_size <<= 8;
15023 uncompressed_size += start[11];
15024 start += 12;
15025 new_size -= 12;
15026 }
15027
15028 if (uncompressed_size)
15029 {
15030 if (uncompress_section_contents (& start, uncompressed_size,
15031 & new_size))
15032 {
15033 section_size = new_size;
15034 }
15035 else
15036 {
15037 error (_("Unable to decompress section %s\n"),
15038 printable_section_name (filedata, section));
15039 /* FIXME: Print the section anyway ? */
15040 goto error_out;
15041 }
15042 }
15043 else
15044 start = real_start;
15045 }
15046
15047 if (relocate)
15048 {
15049 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
15050 goto error_out;
15051 }
15052 else
15053 {
15054 /* If the section being dumped has relocations against it the user might
15055 be expecting these relocations to have been applied. Check for this
15056 case and issue a warning message in order to avoid confusion.
15057 FIXME: Maybe we ought to have an option that dumps a section with
15058 relocs applied ? */
15059 for (relsec = filedata->section_headers;
15060 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15061 ++relsec)
15062 {
15063 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15064 || relsec->sh_info >= filedata->file_header.e_shnum
15065 || filedata->section_headers + relsec->sh_info != section
15066 || relsec->sh_size == 0
15067 || relsec->sh_link >= filedata->file_header.e_shnum)
15068 continue;
15069
15070 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
15071 break;
15072 }
15073 }
15074
15075 addr = section->sh_addr;
15076 bytes = section_size;
15077 data = start;
15078
15079 while (bytes)
15080 {
15081 int j;
15082 int k;
15083 int lbytes;
15084
15085 lbytes = (bytes > 16 ? 16 : bytes);
15086
15087 printf (" 0x%8.8lx ", (unsigned long) addr);
15088
15089 for (j = 0; j < 16; j++)
15090 {
15091 if (j < lbytes)
15092 printf ("%2.2x", data[j]);
15093 else
15094 printf (" ");
15095
15096 if ((j & 3) == 3)
15097 printf (" ");
15098 }
15099
15100 for (j = 0; j < lbytes; j++)
15101 {
15102 k = data[j];
15103 if (k >= ' ' && k < 0x7f)
15104 printf ("%c", k);
15105 else
15106 printf (".");
15107 }
15108
15109 putchar ('\n');
15110
15111 data += lbytes;
15112 addr += lbytes;
15113 bytes -= lbytes;
15114 }
15115
15116 free (real_start);
15117
15118 putchar ('\n');
15119 return true;
15120
15121 error_out:
15122 free (real_start);
15123 return false;
15124}
15125
15126#ifdef ENABLE_LIBCTF
15127static ctf_sect_t *
15128shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
15129{
15130 buf->cts_name = section_name_print (filedata, shdr);
15131 buf->cts_size = shdr->sh_size;
15132 buf->cts_entsize = shdr->sh_entsize;
15133
15134 return buf;
15135}
15136
15137/* Formatting callback function passed to ctf_dump. Returns either the pointer
15138 it is passed, or a pointer to newly-allocated storage, in which case
15139 dump_ctf() will free it when it no longer needs it. */
15140
15141static char *
15142dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
15143 char *s, void *arg)
15144{
15145 const char *blanks = arg;
15146 char *new_s;
15147
15148 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
15149 return s;
15150 return new_s;
15151}
15152
15153/* Dump CTF errors/warnings. */
15154static void
15155dump_ctf_errs (ctf_dict_t *fp)
15156{
15157 ctf_next_t *it = NULL;
15158 char *errtext;
15159 int is_warning;
15160 int err;
15161
15162 /* Dump accumulated errors and warnings. */
15163 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
15164 {
15165 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
15166 errtext);
15167 free (errtext);
15168 }
15169 if (err != ECTF_NEXT_END)
15170 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
15171}
15172
15173/* Dump one CTF archive member. */
15174
15175static void
15176dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
15177 size_t member)
15178{
15179 const char *things[] = {"Header", "Labels", "Data objects",
15180 "Function objects", "Variables", "Types", "Strings",
15181 ""};
15182 const char **thing;
15183 size_t i;
15184
15185 /* Don't print out the name of the default-named archive member if it appears
15186 first in the list. The name .ctf appears everywhere, even for things that
15187 aren't really archives, so printing it out is liable to be confusing; also,
15188 the common case by far is for only one archive member to exist, and hiding
15189 it in that case seems worthwhile. */
15190
15191 if (strcmp (name, ".ctf") != 0 || member != 0)
15192 printf (_("\nCTF archive member: %s:\n"), name);
15193
15194 if (ctf_parent_name (ctf) != NULL)
15195 ctf_import (ctf, parent);
15196
15197 for (i = 0, thing = things; *thing[0]; thing++, i++)
15198 {
15199 ctf_dump_state_t *s = NULL;
15200 char *item;
15201
15202 printf ("\n %s:\n", *thing);
15203 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
15204 (void *) " ")) != NULL)
15205 {
15206 printf ("%s\n", item);
15207 free (item);
15208 }
15209
15210 if (ctf_errno (ctf))
15211 {
15212 error (_("Iteration failed: %s, %s\n"), *thing,
15213 ctf_errmsg (ctf_errno (ctf)));
15214 break;
15215 }
15216 }
15217
15218 dump_ctf_errs (ctf);
15219}
15220
15221static bool
15222dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
15223{
15224 Elf_Internal_Shdr * symtab_sec = NULL;
15225 Elf_Internal_Shdr * strtab_sec = NULL;
15226 void * data = NULL;
15227 void * symdata = NULL;
15228 void * strdata = NULL;
15229 ctf_sect_t ctfsect, symsect, strsect;
15230 ctf_sect_t * symsectp = NULL;
15231 ctf_sect_t * strsectp = NULL;
15232 ctf_archive_t * ctfa = NULL;
15233 ctf_dict_t * parent = NULL;
15234 ctf_dict_t * fp;
15235
15236 ctf_next_t *i = NULL;
15237 const char *name;
15238 size_t member = 0;
15239 int err;
15240 bool ret = false;
15241
15242 shdr_to_ctf_sect (&ctfsect, section, filedata);
15243 data = get_section_contents (section, filedata);
15244 ctfsect.cts_data = data;
15245
15246 if (!dump_ctf_symtab_name)
15247 dump_ctf_symtab_name = strdup (".dynsym");
15248
15249 if (!dump_ctf_strtab_name)
15250 dump_ctf_strtab_name = strdup (".dynstr");
15251
15252 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
15253 {
15254 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
15255 {
15256 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
15257 goto fail;
15258 }
15259 if ((symdata = (void *) get_data (NULL, filedata,
15260 symtab_sec->sh_offset, 1,
15261 symtab_sec->sh_size,
15262 _("symbols"))) == NULL)
15263 goto fail;
15264 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
15265 symsect.cts_data = symdata;
15266 }
15267
15268 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
15269 {
15270 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
15271 {
15272 error (_("No string table section named %s\n"),
15273 dump_ctf_strtab_name);
15274 goto fail;
15275 }
15276 if ((strdata = (void *) get_data (NULL, filedata,
15277 strtab_sec->sh_offset, 1,
15278 strtab_sec->sh_size,
15279 _("strings"))) == NULL)
15280 goto fail;
15281 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
15282 strsect.cts_data = strdata;
15283 }
15284
15285 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
15286 libctf papers over the difference, so we can pretend it is always an
15287 archive. */
15288
15289 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
15290 {
15291 dump_ctf_errs (NULL);
15292 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15293 goto fail;
15294 }
15295
15296 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
15297 != ELFDATA2MSB);
15298
15299 /* Preload the parent dict, since it will need to be imported into every
15300 child in turn. */
15301 if ((parent = ctf_dict_open (ctfa, dump_ctf_parent_name, &err)) == NULL)
15302 {
15303 dump_ctf_errs (NULL);
15304 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
15305 goto fail;
15306 }
15307
15308 ret = true;
15309
15310 if (filedata->is_separate)
15311 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
15312 printable_section_name (filedata, section),
15313 filedata->file_name);
15314 else
15315 printf (_("\nDump of CTF section '%s':\n"),
15316 printable_section_name (filedata, section));
15317
15318 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
15319 dump_ctf_archive_member (fp, name, parent, member++);
15320 if (err != ECTF_NEXT_END)
15321 {
15322 dump_ctf_errs (NULL);
15323 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
15324 ret = false;
15325 }
15326
15327 fail:
15328 ctf_dict_close (parent);
15329 ctf_close (ctfa);
15330 free (data);
15331 free (symdata);
15332 free (strdata);
15333 return ret;
15334}
15335#endif
15336
15337static bool
15338load_specific_debug_section (enum dwarf_section_display_enum debug,
15339 const Elf_Internal_Shdr * sec,
15340 void * data)
15341{
15342 struct dwarf_section * section = &debug_displays [debug].section;
15343 char buf [64];
15344 Filedata * filedata = (Filedata *) data;
15345
15346 if (section->start != NULL)
15347 {
15348 /* If it is already loaded, do nothing. */
15349 if (streq (section->filename, filedata->file_name))
15350 return true;
15351 free (section->start);
15352 }
15353
15354 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
15355 section->address = sec->sh_addr;
15356 section->filename = filedata->file_name;
15357 section->start = (unsigned char *) get_data (NULL, filedata,
15358 sec->sh_offset, 1,
15359 sec->sh_size, buf);
15360 if (section->start == NULL)
15361 section->size = 0;
15362 else
15363 {
15364 unsigned char *start = section->start;
15365 dwarf_size_type size = sec->sh_size;
15366 dwarf_size_type uncompressed_size = 0;
15367
15368 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
15369 {
15370 Elf_Internal_Chdr chdr;
15371 unsigned int compression_header_size;
15372
15373 if (size < (is_32bit_elf
15374 ? sizeof (Elf32_External_Chdr)
15375 : sizeof (Elf64_External_Chdr)))
15376 {
15377 warn (_("compressed section %s is too small to contain a compression header\n"),
15378 section->name);
15379 return false;
15380 }
15381
15382 compression_header_size = get_compression_header (&chdr, start, size);
15383 if (compression_header_size == 0)
15384 /* An error message will have already been generated
15385 by get_compression_header. */
15386 return false;
15387
15388 if (chdr.ch_type != ELFCOMPRESS_ZLIB)
15389 {
15390 warn (_("section '%s' has unsupported compress type: %d\n"),
15391 section->name, chdr.ch_type);
15392 return false;
15393 }
15394 uncompressed_size = chdr.ch_size;
15395 start += compression_header_size;
15396 size -= compression_header_size;
15397 }
15398 else if (size > 12 && streq ((char *) start, "ZLIB"))
15399 {
15400 /* Read the zlib header. In this case, it should be "ZLIB"
15401 followed by the uncompressed section size, 8 bytes in
15402 big-endian order. */
15403 uncompressed_size = start[4]; uncompressed_size <<= 8;
15404 uncompressed_size += start[5]; uncompressed_size <<= 8;
15405 uncompressed_size += start[6]; uncompressed_size <<= 8;
15406 uncompressed_size += start[7]; uncompressed_size <<= 8;
15407 uncompressed_size += start[8]; uncompressed_size <<= 8;
15408 uncompressed_size += start[9]; uncompressed_size <<= 8;
15409 uncompressed_size += start[10]; uncompressed_size <<= 8;
15410 uncompressed_size += start[11];
15411 start += 12;
15412 size -= 12;
15413 }
15414
15415 if (uncompressed_size)
15416 {
15417 if (uncompress_section_contents (&start, uncompressed_size,
15418 &size))
15419 {
15420 /* Free the compressed buffer, update the section buffer
15421 and the section size if uncompress is successful. */
15422 free (section->start);
15423 section->start = start;
15424 }
15425 else
15426 {
15427 error (_("Unable to decompress section %s\n"),
15428 printable_section_name (filedata, sec));
15429 return false;
15430 }
15431 }
15432
15433 section->size = size;
15434 }
15435
15436 if (section->start == NULL)
15437 return false;
15438
15439 if (debug_displays [debug].relocate)
15440 {
15441 if (! apply_relocations (filedata, sec, section->start, section->size,
15442 & section->reloc_info, & section->num_relocs))
15443 return false;
15444 }
15445 else
15446 {
15447 section->reloc_info = NULL;
15448 section->num_relocs = 0;
15449 }
15450
15451 return true;
15452}
15453
15454#if HAVE_LIBDEBUGINFOD
15455/* Return a hex string representation of the build-id. */
15456unsigned char *
15457get_build_id (void * data)
15458{
15459 Filedata * filedata = (Filedata *) data;
15460 Elf_Internal_Shdr * shdr;
15461 unsigned long i;
15462
15463 /* Iterate through notes to find note.gnu.build-id.
15464 FIXME: Only the first note in any note section is examined. */
15465 for (i = 0, shdr = filedata->section_headers;
15466 i < filedata->file_header.e_shnum && shdr != NULL;
15467 i++, shdr++)
15468 {
15469 if (shdr->sh_type != SHT_NOTE)
15470 continue;
15471
15472 char * next;
15473 char * end;
15474 size_t data_remaining;
15475 size_t min_notesz;
15476 Elf_External_Note * enote;
15477 Elf_Internal_Note inote;
15478
15479 bfd_vma offset = shdr->sh_offset;
15480 bfd_vma align = shdr->sh_addralign;
15481 bfd_vma length = shdr->sh_size;
15482
15483 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
15484 if (enote == NULL)
15485 continue;
15486
15487 if (align < 4)
15488 align = 4;
15489 else if (align != 4 && align != 8)
15490 {
15491 free (enote);
15492 continue;
15493 }
15494
15495 end = (char *) enote + length;
15496 data_remaining = end - (char *) enote;
15497
15498 if (!is_ia64_vms (filedata))
15499 {
15500 min_notesz = offsetof (Elf_External_Note, name);
15501 if (data_remaining < min_notesz)
15502 {
15503 warn (_("\
15504malformed note encountered in section %s whilst scanning for build-id note\n"),
15505 printable_section_name (filedata, shdr));
15506 free (enote);
15507 continue;
15508 }
15509 data_remaining -= min_notesz;
15510
15511 inote.type = BYTE_GET (enote->type);
15512 inote.namesz = BYTE_GET (enote->namesz);
15513 inote.namedata = enote->name;
15514 inote.descsz = BYTE_GET (enote->descsz);
15515 inote.descdata = ((char *) enote
15516 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
15517 inote.descpos = offset + (inote.descdata - (char *) enote);
15518 next = ((char *) enote
15519 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
15520 }
15521 else
15522 {
15523 Elf64_External_VMS_Note *vms_enote;
15524
15525 /* PR binutils/15191
15526 Make sure that there is enough data to read. */
15527 min_notesz = offsetof (Elf64_External_VMS_Note, name);
15528 if (data_remaining < min_notesz)
15529 {
15530 warn (_("\
15531malformed note encountered in section %s whilst scanning for build-id note\n"),
15532 printable_section_name (filedata, shdr));
15533 free (enote);
15534 continue;
15535 }
15536 data_remaining -= min_notesz;
15537
15538 vms_enote = (Elf64_External_VMS_Note *) enote;
15539 inote.type = BYTE_GET (vms_enote->type);
15540 inote.namesz = BYTE_GET (vms_enote->namesz);
15541 inote.namedata = vms_enote->name;
15542 inote.descsz = BYTE_GET (vms_enote->descsz);
15543 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
15544 inote.descpos = offset + (inote.descdata - (char *) enote);
15545 next = inote.descdata + align_power (inote.descsz, 3);
15546 }
15547
15548 /* Skip malformed notes. */
15549 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
15550 || (size_t) (inote.descdata - inote.namedata) > data_remaining
15551 || (size_t) (next - inote.descdata) < inote.descsz
15552 || ((size_t) (next - inote.descdata)
15553 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
15554 {
15555 warn (_("\
15556malformed note encountered in section %s whilst scanning for build-id note\n"),
15557 printable_section_name (filedata, shdr));
15558 free (enote);
15559 continue;
15560 }
15561
15562 /* Check if this is the build-id note. If so then convert the build-id
15563 bytes to a hex string. */
15564 if (inote.namesz > 0
15565 && startswith (inote.namedata, "GNU")
15566 && inote.type == NT_GNU_BUILD_ID)
15567 {
15568 unsigned long j;
15569 char * build_id;
15570
15571 build_id = malloc (inote.descsz * 2 + 1);
15572 if (build_id == NULL)
15573 {
15574 free (enote);
15575 return NULL;
15576 }
15577
15578 for (j = 0; j < inote.descsz; ++j)
15579 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
15580 build_id[inote.descsz * 2] = '\0';
15581 free (enote);
15582
15583 return (unsigned char *) build_id;
15584 }
15585 free (enote);
15586 }
15587
15588 return NULL;
15589}
15590#endif /* HAVE_LIBDEBUGINFOD */
15591
15592/* If this is not NULL, load_debug_section will only look for sections
15593 within the list of sections given here. */
15594static unsigned int * section_subset = NULL;
15595
15596bool
15597load_debug_section (enum dwarf_section_display_enum debug, void * data)
15598{
15599 struct dwarf_section * section = &debug_displays [debug].section;
15600 Elf_Internal_Shdr * sec;
15601 Filedata * filedata = (Filedata *) data;
15602
15603 /* Without section headers we cannot find any sections. */
15604 if (filedata->section_headers == NULL)
15605 return false;
15606
15607 if (filedata->string_table == NULL
15608 && filedata->file_header.e_shstrndx != SHN_UNDEF
15609 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
15610 {
15611 Elf_Internal_Shdr * strs;
15612
15613 /* Read in the string table, so that we have section names to scan. */
15614 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
15615
15616 if (strs != NULL && strs->sh_size != 0)
15617 {
15618 filedata->string_table
15619 = (char *) get_data (NULL, filedata, strs->sh_offset,
15620 1, strs->sh_size, _("string table"));
15621
15622 filedata->string_table_length
15623 = filedata->string_table != NULL ? strs->sh_size : 0;
15624 }
15625 }
15626
15627 /* Locate the debug section. */
15628 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
15629 if (sec != NULL)
15630 section->name = section->uncompressed_name;
15631 else
15632 {
15633 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
15634 if (sec != NULL)
15635 section->name = section->compressed_name;
15636 }
15637 if (sec == NULL)
15638 return false;
15639
15640 /* If we're loading from a subset of sections, and we've loaded
15641 a section matching this name before, it's likely that it's a
15642 different one. */
15643 if (section_subset != NULL)
15644 free_debug_section (debug);
15645
15646 return load_specific_debug_section (debug, sec, data);
15647}
15648
15649void
15650free_debug_section (enum dwarf_section_display_enum debug)
15651{
15652 struct dwarf_section * section = &debug_displays [debug].section;
15653
15654 if (section->start == NULL)
15655 return;
15656
15657 free ((char *) section->start);
15658 section->start = NULL;
15659 section->address = 0;
15660 section->size = 0;
15661
15662 free (section->reloc_info);
15663 section->reloc_info = NULL;
15664 section->num_relocs = 0;
15665}
15666
15667static bool
15668display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
15669{
15670 const char *name = (section_name_valid (filedata, section)
15671 ? section_name (filedata, section) : "");
15672 const char *print_name = printable_section_name (filedata, section);
15673 bfd_size_type length;
15674 bool result = true;
15675 int i;
15676
15677 length = section->sh_size;
15678 if (length == 0)
15679 {
15680 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
15681 return true;
15682 }
15683 if (section->sh_type == SHT_NOBITS)
15684 {
15685 /* There is no point in dumping the contents of a debugging section
15686 which has the NOBITS type - the bits in the file will be random.
15687 This can happen when a file containing a .eh_frame section is
15688 stripped with the --only-keep-debug command line option. */
15689 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
15690 print_name);
15691 return false;
15692 }
15693
15694 if (startswith (name, ".gnu.linkonce.wi."))
15695 name = ".debug_info";
15696
15697 /* See if we know how to display the contents of this section. */
15698 for (i = 0; i < max; i++)
15699 {
15700 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
15701 struct dwarf_section_display * display = debug_displays + i;
15702 struct dwarf_section * sec = & display->section;
15703
15704 if (streq (sec->uncompressed_name, name)
15705 || (id == line && startswith (name, ".debug_line."))
15706 || streq (sec->compressed_name, name))
15707 {
15708 bool secondary = (section != find_section (filedata, name));
15709
15710 if (secondary)
15711 free_debug_section (id);
15712
15713 if (i == line && startswith (name, ".debug_line."))
15714 sec->name = name;
15715 else if (streq (sec->uncompressed_name, name))
15716 sec->name = sec->uncompressed_name;
15717 else
15718 sec->name = sec->compressed_name;
15719
15720 if (load_specific_debug_section (id, section, filedata))
15721 {
15722 /* If this debug section is part of a CU/TU set in a .dwp file,
15723 restrict load_debug_section to the sections in that set. */
15724 section_subset = find_cu_tu_set (filedata, shndx);
15725
15726 result &= display->display (sec, filedata);
15727
15728 section_subset = NULL;
15729
15730 if (secondary || (id != info && id != abbrev && id != debug_addr))
15731 free_debug_section (id);
15732 }
15733 break;
15734 }
15735 }
15736
15737 if (i == max)
15738 {
15739 printf (_("Unrecognized debug section: %s\n"), print_name);
15740 result = false;
15741 }
15742
15743 return result;
15744}
15745
15746/* Set DUMP_SECTS for all sections where dumps were requested
15747 based on section name. */
15748
15749static void
15750initialise_dumps_byname (Filedata * filedata)
15751{
15752 struct dump_list_entry * cur;
15753
15754 for (cur = dump_sects_byname; cur; cur = cur->next)
15755 {
15756 unsigned int i;
15757 bool any = false;
15758
15759 for (i = 0; i < filedata->file_header.e_shnum; i++)
15760 if (section_name_valid (filedata, filedata->section_headers + i)
15761 && streq (section_name (filedata, filedata->section_headers + i),
15762 cur->name))
15763 {
15764 request_dump_bynumber (&filedata->dump, i, cur->type);
15765 any = true;
15766 }
15767
15768 if (!any && !filedata->is_separate)
15769 warn (_("Section '%s' was not dumped because it does not exist\n"),
15770 cur->name);
15771 }
15772}
15773
15774static bool
15775process_section_contents (Filedata * filedata)
15776{
15777 Elf_Internal_Shdr * section;
15778 unsigned int i;
15779 bool res = true;
15780
15781 if (! do_dump)
15782 return true;
15783
15784 initialise_dumps_byname (filedata);
15785
15786 for (i = 0, section = filedata->section_headers;
15787 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
15788 i++, section++)
15789 {
15790 dump_type dump = filedata->dump.dump_sects[i];
15791
15792 if (filedata->is_separate && ! process_links)
15793 dump &= DEBUG_DUMP;
15794
15795#ifdef SUPPORT_DISASSEMBLY
15796 if (dump & DISASS_DUMP)
15797 {
15798 if (! disassemble_section (section, filedata))
15799 res = false;
15800 }
15801#endif
15802 if (dump & HEX_DUMP)
15803 {
15804 if (! dump_section_as_bytes (section, filedata, false))
15805 res = false;
15806 }
15807
15808 if (dump & RELOC_DUMP)
15809 {
15810 if (! dump_section_as_bytes (section, filedata, true))
15811 res = false;
15812 }
15813
15814 if (dump & STRING_DUMP)
15815 {
15816 if (! dump_section_as_strings (section, filedata))
15817 res = false;
15818 }
15819
15820 if (dump & DEBUG_DUMP)
15821 {
15822 if (! display_debug_section (i, section, filedata))
15823 res = false;
15824 }
15825
15826#ifdef ENABLE_LIBCTF
15827 if (dump & CTF_DUMP)
15828 {
15829 if (! dump_section_as_ctf (section, filedata))
15830 res = false;
15831 }
15832#endif
15833 }
15834
15835 if (! filedata->is_separate)
15836 {
15837 /* Check to see if the user requested a
15838 dump of a section that does not exist. */
15839 for (; i < filedata->dump.num_dump_sects; i++)
15840 if (filedata->dump.dump_sects[i])
15841 {
15842 warn (_("Section %d was not dumped because it does not exist!\n"), i);
15843 res = false;
15844 }
15845 }
15846
15847 return res;
15848}
15849
15850static void
15851process_mips_fpe_exception (int mask)
15852{
15853 if (mask)
15854 {
15855 bool first = true;
15856
15857 if (mask & OEX_FPU_INEX)
15858 fputs ("INEX", stdout), first = false;
15859 if (mask & OEX_FPU_UFLO)
15860 printf ("%sUFLO", first ? "" : "|"), first = false;
15861 if (mask & OEX_FPU_OFLO)
15862 printf ("%sOFLO", first ? "" : "|"), first = false;
15863 if (mask & OEX_FPU_DIV0)
15864 printf ("%sDIV0", first ? "" : "|"), first = false;
15865 if (mask & OEX_FPU_INVAL)
15866 printf ("%sINVAL", first ? "" : "|");
15867 }
15868 else
15869 fputs ("0", stdout);
15870}
15871
15872/* Display's the value of TAG at location P. If TAG is
15873 greater than 0 it is assumed to be an unknown tag, and
15874 a message is printed to this effect. Otherwise it is
15875 assumed that a message has already been printed.
15876
15877 If the bottom bit of TAG is set it assumed to have a
15878 string value, otherwise it is assumed to have an integer
15879 value.
15880
15881 Returns an updated P pointing to the first unread byte
15882 beyond the end of TAG's value.
15883
15884 Reads at or beyond END will not be made. */
15885
15886static unsigned char *
15887display_tag_value (signed int tag,
15888 unsigned char * p,
15889 const unsigned char * const end)
15890{
15891 unsigned long val;
15892
15893 if (tag > 0)
15894 printf (" Tag_unknown_%d: ", tag);
15895
15896 if (p >= end)
15897 {
15898 warn (_("<corrupt tag>\n"));
15899 }
15900 else if (tag & 1)
15901 {
15902 /* PR 17531 file: 027-19978-0.004. */
15903 size_t maxlen = (end - p) - 1;
15904
15905 putchar ('"');
15906 if (maxlen > 0)
15907 {
15908 print_symbol ((int) maxlen, (const char *) p);
15909 p += strnlen ((char *) p, maxlen) + 1;
15910 }
15911 else
15912 {
15913 printf (_("<corrupt string tag>"));
15914 p = (unsigned char *) end;
15915 }
15916 printf ("\"\n");
15917 }
15918 else
15919 {
15920 READ_ULEB (val, p, end);
15921 printf ("%ld (0x%lx)\n", val, val);
15922 }
15923
15924 assert (p <= end);
15925 return p;
15926}
15927
15928/* ARC ABI attributes section. */
15929
15930static unsigned char *
15931display_arc_attribute (unsigned char * p,
15932 const unsigned char * const end)
15933{
15934 unsigned int tag;
15935 unsigned int val;
15936
15937 READ_ULEB (tag, p, end);
15938
15939 switch (tag)
15940 {
15941 case Tag_ARC_PCS_config:
15942 READ_ULEB (val, p, end);
15943 printf (" Tag_ARC_PCS_config: ");
15944 switch (val)
15945 {
15946 case 0:
15947 printf (_("Absent/Non standard\n"));
15948 break;
15949 case 1:
15950 printf (_("Bare metal/mwdt\n"));
15951 break;
15952 case 2:
15953 printf (_("Bare metal/newlib\n"));
15954 break;
15955 case 3:
15956 printf (_("Linux/uclibc\n"));
15957 break;
15958 case 4:
15959 printf (_("Linux/glibc\n"));
15960 break;
15961 default:
15962 printf (_("Unknown\n"));
15963 break;
15964 }
15965 break;
15966
15967 case Tag_ARC_CPU_base:
15968 READ_ULEB (val, p, end);
15969 printf (" Tag_ARC_CPU_base: ");
15970 switch (val)
15971 {
15972 default:
15973 case TAG_CPU_NONE:
15974 printf (_("Absent\n"));
15975 break;
15976 case TAG_CPU_ARC6xx:
15977 printf ("ARC6xx\n");
15978 break;
15979 case TAG_CPU_ARC7xx:
15980 printf ("ARC7xx\n");
15981 break;
15982 case TAG_CPU_ARCEM:
15983 printf ("ARCEM\n");
15984 break;
15985 case TAG_CPU_ARCHS:
15986 printf ("ARCHS\n");
15987 break;
15988 }
15989 break;
15990
15991 case Tag_ARC_CPU_variation:
15992 READ_ULEB (val, p, end);
15993 printf (" Tag_ARC_CPU_variation: ");
15994 switch (val)
15995 {
15996 default:
15997 if (val > 0 && val < 16)
15998 printf ("Core%d\n", val);
15999 else
16000 printf ("Unknown\n");
16001 break;
16002
16003 case 0:
16004 printf (_("Absent\n"));
16005 break;
16006 }
16007 break;
16008
16009 case Tag_ARC_CPU_name:
16010 printf (" Tag_ARC_CPU_name: ");
16011 p = display_tag_value (-1, p, end);
16012 break;
16013
16014 case Tag_ARC_ABI_rf16:
16015 READ_ULEB (val, p, end);
16016 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
16017 break;
16018
16019 case Tag_ARC_ABI_osver:
16020 READ_ULEB (val, p, end);
16021 printf (" Tag_ARC_ABI_osver: v%d\n", val);
16022 break;
16023
16024 case Tag_ARC_ABI_pic:
16025 case Tag_ARC_ABI_sda:
16026 READ_ULEB (val, p, end);
16027 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
16028 : " Tag_ARC_ABI_pic: ");
16029 switch (val)
16030 {
16031 case 0:
16032 printf (_("Absent\n"));
16033 break;
16034 case 1:
16035 printf ("MWDT\n");
16036 break;
16037 case 2:
16038 printf ("GNU\n");
16039 break;
16040 default:
16041 printf (_("Unknown\n"));
16042 break;
16043 }
16044 break;
16045
16046 case Tag_ARC_ABI_tls:
16047 READ_ULEB (val, p, end);
16048 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
16049 break;
16050
16051 case Tag_ARC_ABI_enumsize:
16052 READ_ULEB (val, p, end);
16053 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
16054 _("smallest"));
16055 break;
16056
16057 case Tag_ARC_ABI_exceptions:
16058 READ_ULEB (val, p, end);
16059 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
16060 : _("default"));
16061 break;
16062
16063 case Tag_ARC_ABI_double_size:
16064 READ_ULEB (val, p, end);
16065 printf (" Tag_ARC_ABI_double_size: %d\n", val);
16066 break;
16067
16068 case Tag_ARC_ISA_config:
16069 printf (" Tag_ARC_ISA_config: ");
16070 p = display_tag_value (-1, p, end);
16071 break;
16072
16073 case Tag_ARC_ISA_apex:
16074 printf (" Tag_ARC_ISA_apex: ");
16075 p = display_tag_value (-1, p, end);
16076 break;
16077
16078 case Tag_ARC_ISA_mpy_option:
16079 READ_ULEB (val, p, end);
16080 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
16081 break;
16082
16083 case Tag_ARC_ATR_version:
16084 READ_ULEB (val, p, end);
16085 printf (" Tag_ARC_ATR_version: %d\n", val);
16086 break;
16087
16088 default:
16089 return display_tag_value (tag & 1, p, end);
16090 }
16091
16092 return p;
16093}
16094
16095/* ARM EABI attributes section. */
16096typedef struct
16097{
16098 unsigned int tag;
16099 const char * name;
16100 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
16101 unsigned int type;
16102 const char *const *table;
16103} arm_attr_public_tag;
16104
16105static const char *const arm_attr_tag_CPU_arch[] =
16106 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
16107 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
16108 "v8-M.mainline", "v8.1-A", "v8.2-A", "v8.3-A",
16109 "v8.1-M.mainline", "v9"};
16110static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
16111static const char *const arm_attr_tag_THUMB_ISA_use[] =
16112 {"No", "Thumb-1", "Thumb-2", "Yes"};
16113static const char *const arm_attr_tag_FP_arch[] =
16114 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
16115 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
16116static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
16117static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
16118 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
16119 "NEON for ARMv8.1"};
16120static const char *const arm_attr_tag_PCS_config[] =
16121 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
16122 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
16123static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
16124 {"V6", "SB", "TLS", "Unused"};
16125static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
16126 {"Absolute", "PC-relative", "SB-relative", "None"};
16127static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
16128 {"Absolute", "PC-relative", "None"};
16129static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
16130 {"None", "direct", "GOT-indirect"};
16131static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
16132 {"None", "??? 1", "2", "??? 3", "4"};
16133static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
16134static const char *const arm_attr_tag_ABI_FP_denormal[] =
16135 {"Unused", "Needed", "Sign only"};
16136static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
16137static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
16138static const char *const arm_attr_tag_ABI_FP_number_model[] =
16139 {"Unused", "Finite", "RTABI", "IEEE 754"};
16140static const char *const arm_attr_tag_ABI_enum_size[] =
16141 {"Unused", "small", "int", "forced to int"};
16142static const char *const arm_attr_tag_ABI_HardFP_use[] =
16143 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
16144static const char *const arm_attr_tag_ABI_VFP_args[] =
16145 {"AAPCS", "VFP registers", "custom", "compatible"};
16146static const char *const arm_attr_tag_ABI_WMMX_args[] =
16147 {"AAPCS", "WMMX registers", "custom"};
16148static const char *const arm_attr_tag_ABI_optimization_goals[] =
16149 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16150 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
16151static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
16152 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
16153 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
16154static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
16155static const char *const arm_attr_tag_FP_HP_extension[] =
16156 {"Not Allowed", "Allowed"};
16157static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
16158 {"None", "IEEE 754", "Alternative Format"};
16159static const char *const arm_attr_tag_DSP_extension[] =
16160 {"Follow architecture", "Allowed"};
16161static const char *const arm_attr_tag_MPextension_use[] =
16162 {"Not Allowed", "Allowed"};
16163static const char *const arm_attr_tag_DIV_use[] =
16164 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
16165 "Allowed in v7-A with integer division extension"};
16166static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
16167static const char *const arm_attr_tag_Virtualization_use[] =
16168 {"Not Allowed", "TrustZone", "Virtualization Extensions",
16169 "TrustZone and Virtualization Extensions"};
16170static const char *const arm_attr_tag_MPextension_use_legacy[] =
16171 {"Not Allowed", "Allowed"};
16172
16173static const char *const arm_attr_tag_MVE_arch[] =
16174 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
16175
16176static const char * arm_attr_tag_PAC_extension[] =
16177 {"No PAC/AUT instructions",
16178 "PAC/AUT instructions permitted in the NOP space",
16179 "PAC/AUT instructions permitted in the NOP and in the non-NOP space"};
16180
16181static const char * arm_attr_tag_BTI_extension[] =
16182 {"BTI instructions not permitted",
16183 "BTI instructions permitted in the NOP space",
16184 "BTI instructions permitted in the NOP and in the non-NOP space"};
16185
16186static const char * arm_attr_tag_BTI_use[] =
16187 {"Compiled without branch target enforcement",
16188 "Compiled with branch target enforcement"};
16189
16190static const char * arm_attr_tag_PACRET_use[] =
16191 {"Compiled without return address signing and authentication",
16192 "Compiled with return address signing and authentication"};
16193
16194#define LOOKUP(id, name) \
16195 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
16196static arm_attr_public_tag arm_attr_public_tags[] =
16197{
16198 {4, "CPU_raw_name", 1, NULL},
16199 {5, "CPU_name", 1, NULL},
16200 LOOKUP(6, CPU_arch),
16201 {7, "CPU_arch_profile", 0, NULL},
16202 LOOKUP(8, ARM_ISA_use),
16203 LOOKUP(9, THUMB_ISA_use),
16204 LOOKUP(10, FP_arch),
16205 LOOKUP(11, WMMX_arch),
16206 LOOKUP(12, Advanced_SIMD_arch),
16207 LOOKUP(13, PCS_config),
16208 LOOKUP(14, ABI_PCS_R9_use),
16209 LOOKUP(15, ABI_PCS_RW_data),
16210 LOOKUP(16, ABI_PCS_RO_data),
16211 LOOKUP(17, ABI_PCS_GOT_use),
16212 LOOKUP(18, ABI_PCS_wchar_t),
16213 LOOKUP(19, ABI_FP_rounding),
16214 LOOKUP(20, ABI_FP_denormal),
16215 LOOKUP(21, ABI_FP_exceptions),
16216 LOOKUP(22, ABI_FP_user_exceptions),
16217 LOOKUP(23, ABI_FP_number_model),
16218 {24, "ABI_align_needed", 0, NULL},
16219 {25, "ABI_align_preserved", 0, NULL},
16220 LOOKUP(26, ABI_enum_size),
16221 LOOKUP(27, ABI_HardFP_use),
16222 LOOKUP(28, ABI_VFP_args),
16223 LOOKUP(29, ABI_WMMX_args),
16224 LOOKUP(30, ABI_optimization_goals),
16225 LOOKUP(31, ABI_FP_optimization_goals),
16226 {32, "compatibility", 0, NULL},
16227 LOOKUP(34, CPU_unaligned_access),
16228 LOOKUP(36, FP_HP_extension),
16229 LOOKUP(38, ABI_FP_16bit_format),
16230 LOOKUP(42, MPextension_use),
16231 LOOKUP(44, DIV_use),
16232 LOOKUP(46, DSP_extension),
16233 LOOKUP(48, MVE_arch),
16234 LOOKUP(50, PAC_extension),
16235 LOOKUP(52, BTI_extension),
16236 LOOKUP(74, BTI_use),
16237 LOOKUP(76, PACRET_use),
16238 {64, "nodefaults", 0, NULL},
16239 {65, "also_compatible_with", 0, NULL},
16240 LOOKUP(66, T2EE_use),
16241 {67, "conformance", 1, NULL},
16242 LOOKUP(68, Virtualization_use),
16243 LOOKUP(70, MPextension_use_legacy)
16244};
16245#undef LOOKUP
16246
16247static unsigned char *
16248display_arm_attribute (unsigned char * p,
16249 const unsigned char * const end)
16250{
16251 unsigned int tag;
16252 unsigned int val;
16253 arm_attr_public_tag * attr;
16254 unsigned i;
16255 unsigned int type;
16256
16257 READ_ULEB (tag, p, end);
16258 attr = NULL;
16259 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
16260 {
16261 if (arm_attr_public_tags[i].tag == tag)
16262 {
16263 attr = &arm_attr_public_tags[i];
16264 break;
16265 }
16266 }
16267
16268 if (attr)
16269 {
16270 printf (" Tag_%s: ", attr->name);
16271 switch (attr->type)
16272 {
16273 case 0:
16274 switch (tag)
16275 {
16276 case 7: /* Tag_CPU_arch_profile. */
16277 READ_ULEB (val, p, end);
16278 switch (val)
16279 {
16280 case 0: printf (_("None\n")); break;
16281 case 'A': printf (_("Application\n")); break;
16282 case 'R': printf (_("Realtime\n")); break;
16283 case 'M': printf (_("Microcontroller\n")); break;
16284 case 'S': printf (_("Application or Realtime\n")); break;
16285 default: printf ("??? (%d)\n", val); break;
16286 }
16287 break;
16288
16289 case 24: /* Tag_align_needed. */
16290 READ_ULEB (val, p, end);
16291 switch (val)
16292 {
16293 case 0: printf (_("None\n")); break;
16294 case 1: printf (_("8-byte\n")); break;
16295 case 2: printf (_("4-byte\n")); break;
16296 case 3: printf ("??? 3\n"); break;
16297 default:
16298 if (val <= 12)
16299 printf (_("8-byte and up to %d-byte extended\n"),
16300 1 << val);
16301 else
16302 printf ("??? (%d)\n", val);
16303 break;
16304 }
16305 break;
16306
16307 case 25: /* Tag_align_preserved. */
16308 READ_ULEB (val, p, end);
16309 switch (val)
16310 {
16311 case 0: printf (_("None\n")); break;
16312 case 1: printf (_("8-byte, except leaf SP\n")); break;
16313 case 2: printf (_("8-byte\n")); break;
16314 case 3: printf ("??? 3\n"); break;
16315 default:
16316 if (val <= 12)
16317 printf (_("8-byte and up to %d-byte extended\n"),
16318 1 << val);
16319 else
16320 printf ("??? (%d)\n", val);
16321 break;
16322 }
16323 break;
16324
16325 case 32: /* Tag_compatibility. */
16326 {
16327 READ_ULEB (val, p, end);
16328 printf (_("flag = %d, vendor = "), val);
16329 if (p < end - 1)
16330 {
16331 size_t maxlen = (end - p) - 1;
16332
16333 print_symbol ((int) maxlen, (const char *) p);
16334 p += strnlen ((char *) p, maxlen) + 1;
16335 }
16336 else
16337 {
16338 printf (_("<corrupt>"));
16339 p = (unsigned char *) end;
16340 }
16341 putchar ('\n');
16342 }
16343 break;
16344
16345 case 64: /* Tag_nodefaults. */
16346 /* PR 17531: file: 001-505008-0.01. */
16347 if (p < end)
16348 p++;
16349 printf (_("True\n"));
16350 break;
16351
16352 case 65: /* Tag_also_compatible_with. */
16353 READ_ULEB (val, p, end);
16354 if (val == 6 /* Tag_CPU_arch. */)
16355 {
16356 READ_ULEB (val, p, end);
16357 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
16358 printf ("??? (%d)\n", val);
16359 else
16360 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
16361 }
16362 else
16363 printf ("???\n");
16364 while (p < end && *(p++) != '\0' /* NUL terminator. */)
16365 ;
16366 break;
16367
16368 default:
16369 printf (_("<unknown: %d>\n"), tag);
16370 break;
16371 }
16372 return p;
16373
16374 case 1:
16375 return display_tag_value (-1, p, end);
16376 case 2:
16377 return display_tag_value (0, p, end);
16378
16379 default:
16380 assert (attr->type & 0x80);
16381 READ_ULEB (val, p, end);
16382 type = attr->type & 0x7f;
16383 if (val >= type)
16384 printf ("??? (%d)\n", val);
16385 else
16386 printf ("%s\n", attr->table[val]);
16387 return p;
16388 }
16389 }
16390
16391 return display_tag_value (tag, p, end);
16392}
16393
16394static unsigned char *
16395display_gnu_attribute (unsigned char * p,
16396 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
16397 const unsigned char * const end)
16398{
16399 unsigned int tag;
16400 unsigned int val;
16401
16402 READ_ULEB (tag, p, end);
16403
16404 /* Tag_compatibility is the only generic GNU attribute defined at
16405 present. */
16406 if (tag == 32)
16407 {
16408 READ_ULEB (val, p, end);
16409
16410 printf (_("flag = %d, vendor = "), val);
16411 if (p == end)
16412 {
16413 printf (_("<corrupt>\n"));
16414 warn (_("corrupt vendor attribute\n"));
16415 }
16416 else
16417 {
16418 if (p < end - 1)
16419 {
16420 size_t maxlen = (end - p) - 1;
16421
16422 print_symbol ((int) maxlen, (const char *) p);
16423 p += strnlen ((char *) p, maxlen) + 1;
16424 }
16425 else
16426 {
16427 printf (_("<corrupt>"));
16428 p = (unsigned char *) end;
16429 }
16430 putchar ('\n');
16431 }
16432 return p;
16433 }
16434
16435 if ((tag & 2) == 0 && display_proc_gnu_attribute)
16436 return display_proc_gnu_attribute (p, tag, end);
16437
16438 return display_tag_value (tag, p, end);
16439}
16440
16441static unsigned char *
16442display_m68k_gnu_attribute (unsigned char * p,
16443 unsigned int tag,
16444 const unsigned char * const end)
16445{
16446 unsigned int val;
16447
16448 if (tag == Tag_GNU_M68K_ABI_FP)
16449 {
16450 printf (" Tag_GNU_M68K_ABI_FP: ");
16451 if (p == end)
16452 {
16453 printf (_("<corrupt>\n"));
16454 return p;
16455 }
16456 READ_ULEB (val, p, end);
16457
16458 if (val > 3)
16459 printf ("(%#x), ", val);
16460
16461 switch (val & 3)
16462 {
16463 case 0:
16464 printf (_("unspecified hard/soft float\n"));
16465 break;
16466 case 1:
16467 printf (_("hard float\n"));
16468 break;
16469 case 2:
16470 printf (_("soft float\n"));
16471 break;
16472 }
16473 return p;
16474 }
16475
16476 return display_tag_value (tag & 1, p, end);
16477}
16478
16479static unsigned char *
16480display_power_gnu_attribute (unsigned char * p,
16481 unsigned int tag,
16482 const unsigned char * const end)
16483{
16484 unsigned int val;
16485
16486 if (tag == Tag_GNU_Power_ABI_FP)
16487 {
16488 printf (" Tag_GNU_Power_ABI_FP: ");
16489 if (p == end)
16490 {
16491 printf (_("<corrupt>\n"));
16492 return p;
16493 }
16494 READ_ULEB (val, p, end);
16495
16496 if (val > 15)
16497 printf ("(%#x), ", val);
16498
16499 switch (val & 3)
16500 {
16501 case 0:
16502 printf (_("unspecified hard/soft float, "));
16503 break;
16504 case 1:
16505 printf (_("hard float, "));
16506 break;
16507 case 2:
16508 printf (_("soft float, "));
16509 break;
16510 case 3:
16511 printf (_("single-precision hard float, "));
16512 break;
16513 }
16514
16515 switch (val & 0xC)
16516 {
16517 case 0:
16518 printf (_("unspecified long double\n"));
16519 break;
16520 case 4:
16521 printf (_("128-bit IBM long double\n"));
16522 break;
16523 case 8:
16524 printf (_("64-bit long double\n"));
16525 break;
16526 case 12:
16527 printf (_("128-bit IEEE long double\n"));
16528 break;
16529 }
16530 return p;
16531 }
16532
16533 if (tag == Tag_GNU_Power_ABI_Vector)
16534 {
16535 printf (" Tag_GNU_Power_ABI_Vector: ");
16536 if (p == end)
16537 {
16538 printf (_("<corrupt>\n"));
16539 return p;
16540 }
16541 READ_ULEB (val, p, end);
16542
16543 if (val > 3)
16544 printf ("(%#x), ", val);
16545
16546 switch (val & 3)
16547 {
16548 case 0:
16549 printf (_("unspecified\n"));
16550 break;
16551 case 1:
16552 printf (_("generic\n"));
16553 break;
16554 case 2:
16555 printf ("AltiVec\n");
16556 break;
16557 case 3:
16558 printf ("SPE\n");
16559 break;
16560 }
16561 return p;
16562 }
16563
16564 if (tag == Tag_GNU_Power_ABI_Struct_Return)
16565 {
16566 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
16567 if (p == end)
16568 {
16569 printf (_("<corrupt>\n"));
16570 return p;
16571 }
16572 READ_ULEB (val, p, end);
16573
16574 if (val > 2)
16575 printf ("(%#x), ", val);
16576
16577 switch (val & 3)
16578 {
16579 case 0:
16580 printf (_("unspecified\n"));
16581 break;
16582 case 1:
16583 printf ("r3/r4\n");
16584 break;
16585 case 2:
16586 printf (_("memory\n"));
16587 break;
16588 case 3:
16589 printf ("???\n");
16590 break;
16591 }
16592 return p;
16593 }
16594
16595 return display_tag_value (tag & 1, p, end);
16596}
16597
16598static unsigned char *
16599display_s390_gnu_attribute (unsigned char * p,
16600 unsigned int tag,
16601 const unsigned char * const end)
16602{
16603 unsigned int val;
16604
16605 if (tag == Tag_GNU_S390_ABI_Vector)
16606 {
16607 printf (" Tag_GNU_S390_ABI_Vector: ");
16608 READ_ULEB (val, p, end);
16609
16610 switch (val)
16611 {
16612 case 0:
16613 printf (_("any\n"));
16614 break;
16615 case 1:
16616 printf (_("software\n"));
16617 break;
16618 case 2:
16619 printf (_("hardware\n"));
16620 break;
16621 default:
16622 printf ("??? (%d)\n", val);
16623 break;
16624 }
16625 return p;
16626 }
16627
16628 return display_tag_value (tag & 1, p, end);
16629}
16630
16631static void
16632display_sparc_hwcaps (unsigned int mask)
16633{
16634 if (mask)
16635 {
16636 bool first = true;
16637
16638 if (mask & ELF_SPARC_HWCAP_MUL32)
16639 fputs ("mul32", stdout), first = false;
16640 if (mask & ELF_SPARC_HWCAP_DIV32)
16641 printf ("%sdiv32", first ? "" : "|"), first = false;
16642 if (mask & ELF_SPARC_HWCAP_FSMULD)
16643 printf ("%sfsmuld", first ? "" : "|"), first = false;
16644 if (mask & ELF_SPARC_HWCAP_V8PLUS)
16645 printf ("%sv8plus", first ? "" : "|"), first = false;
16646 if (mask & ELF_SPARC_HWCAP_POPC)
16647 printf ("%spopc", first ? "" : "|"), first = false;
16648 if (mask & ELF_SPARC_HWCAP_VIS)
16649 printf ("%svis", first ? "" : "|"), first = false;
16650 if (mask & ELF_SPARC_HWCAP_VIS2)
16651 printf ("%svis2", first ? "" : "|"), first = false;
16652 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
16653 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
16654 if (mask & ELF_SPARC_HWCAP_FMAF)
16655 printf ("%sfmaf", first ? "" : "|"), first = false;
16656 if (mask & ELF_SPARC_HWCAP_VIS3)
16657 printf ("%svis3", first ? "" : "|"), first = false;
16658 if (mask & ELF_SPARC_HWCAP_HPC)
16659 printf ("%shpc", first ? "" : "|"), first = false;
16660 if (mask & ELF_SPARC_HWCAP_RANDOM)
16661 printf ("%srandom", first ? "" : "|"), first = false;
16662 if (mask & ELF_SPARC_HWCAP_TRANS)
16663 printf ("%strans", first ? "" : "|"), first = false;
16664 if (mask & ELF_SPARC_HWCAP_FJFMAU)
16665 printf ("%sfjfmau", first ? "" : "|"), first = false;
16666 if (mask & ELF_SPARC_HWCAP_IMA)
16667 printf ("%sima", first ? "" : "|"), first = false;
16668 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
16669 printf ("%scspare", first ? "" : "|"), first = false;
16670 }
16671 else
16672 fputc ('0', stdout);
16673 fputc ('\n', stdout);
16674}
16675
16676static void
16677display_sparc_hwcaps2 (unsigned int mask)
16678{
16679 if (mask)
16680 {
16681 bool first = true;
16682
16683 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
16684 fputs ("fjathplus", stdout), first = false;
16685 if (mask & ELF_SPARC_HWCAP2_VIS3B)
16686 printf ("%svis3b", first ? "" : "|"), first = false;
16687 if (mask & ELF_SPARC_HWCAP2_ADP)
16688 printf ("%sadp", first ? "" : "|"), first = false;
16689 if (mask & ELF_SPARC_HWCAP2_SPARC5)
16690 printf ("%ssparc5", first ? "" : "|"), first = false;
16691 if (mask & ELF_SPARC_HWCAP2_MWAIT)
16692 printf ("%smwait", first ? "" : "|"), first = false;
16693 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
16694 printf ("%sxmpmul", first ? "" : "|"), first = false;
16695 if (mask & ELF_SPARC_HWCAP2_XMONT)
16696 printf ("%sxmont2", first ? "" : "|"), first = false;
16697 if (mask & ELF_SPARC_HWCAP2_NSEC)
16698 printf ("%snsec", first ? "" : "|"), first = false;
16699 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
16700 printf ("%sfjathhpc", first ? "" : "|"), first = false;
16701 if (mask & ELF_SPARC_HWCAP2_FJDES)
16702 printf ("%sfjdes", first ? "" : "|"), first = false;
16703 if (mask & ELF_SPARC_HWCAP2_FJAES)
16704 printf ("%sfjaes", first ? "" : "|"), first = false;
16705 }
16706 else
16707 fputc ('0', stdout);
16708 fputc ('\n', stdout);
16709}
16710
16711static unsigned char *
16712display_sparc_gnu_attribute (unsigned char * p,
16713 unsigned int tag,
16714 const unsigned char * const end)
16715{
16716 unsigned int val;
16717
16718 if (tag == Tag_GNU_Sparc_HWCAPS)
16719 {
16720 READ_ULEB (val, p, end);
16721 printf (" Tag_GNU_Sparc_HWCAPS: ");
16722 display_sparc_hwcaps (val);
16723 return p;
16724 }
16725 if (tag == Tag_GNU_Sparc_HWCAPS2)
16726 {
16727 READ_ULEB (val, p, end);
16728 printf (" Tag_GNU_Sparc_HWCAPS2: ");
16729 display_sparc_hwcaps2 (val);
16730 return p;
16731 }
16732
16733 return display_tag_value (tag, p, end);
16734}
16735
16736static void
16737print_mips_fp_abi_value (unsigned int val)
16738{
16739 switch (val)
16740 {
16741 case Val_GNU_MIPS_ABI_FP_ANY:
16742 printf (_("Hard or soft float\n"));
16743 break;
16744 case Val_GNU_MIPS_ABI_FP_DOUBLE:
16745 printf (_("Hard float (double precision)\n"));
16746 break;
16747 case Val_GNU_MIPS_ABI_FP_SINGLE:
16748 printf (_("Hard float (single precision)\n"));
16749 break;
16750 case Val_GNU_MIPS_ABI_FP_SOFT:
16751 printf (_("Soft float\n"));
16752 break;
16753 case Val_GNU_MIPS_ABI_FP_OLD_64:
16754 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
16755 break;
16756 case Val_GNU_MIPS_ABI_FP_XX:
16757 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
16758 break;
16759 case Val_GNU_MIPS_ABI_FP_64:
16760 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
16761 break;
16762 case Val_GNU_MIPS_ABI_FP_64A:
16763 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
16764 break;
16765 case Val_GNU_MIPS_ABI_FP_NAN2008:
16766 printf (_("NaN 2008 compatibility\n"));
16767 break;
16768 default:
16769 printf ("??? (%d)\n", val);
16770 break;
16771 }
16772}
16773
16774static unsigned char *
16775display_mips_gnu_attribute (unsigned char * p,
16776 unsigned int tag,
16777 const unsigned char * const end)
16778{
16779 if (tag == Tag_GNU_MIPS_ABI_FP)
16780 {
16781 unsigned int val;
16782
16783 printf (" Tag_GNU_MIPS_ABI_FP: ");
16784 READ_ULEB (val, p, end);
16785 print_mips_fp_abi_value (val);
16786 return p;
16787 }
16788
16789 if (tag == Tag_GNU_MIPS_ABI_MSA)
16790 {
16791 unsigned int val;
16792
16793 printf (" Tag_GNU_MIPS_ABI_MSA: ");
16794 READ_ULEB (val, p, end);
16795
16796 switch (val)
16797 {
16798 case Val_GNU_MIPS_ABI_MSA_ANY:
16799 printf (_("Any MSA or not\n"));
16800 break;
16801 case Val_GNU_MIPS_ABI_MSA_128:
16802 printf (_("128-bit MSA\n"));
16803 break;
16804 default:
16805 printf ("??? (%d)\n", val);
16806 break;
16807 }
16808 return p;
16809 }
16810
16811 return display_tag_value (tag & 1, p, end);
16812}
16813
16814static unsigned char *
16815display_tic6x_attribute (unsigned char * p,
16816 const unsigned char * const end)
16817{
16818 unsigned int tag;
16819 unsigned int val;
16820
16821 READ_ULEB (tag, p, end);
16822
16823 switch (tag)
16824 {
16825 case Tag_ISA:
16826 printf (" Tag_ISA: ");
16827 READ_ULEB (val, p, end);
16828
16829 switch (val)
16830 {
16831 case C6XABI_Tag_ISA_none:
16832 printf (_("None\n"));
16833 break;
16834 case C6XABI_Tag_ISA_C62X:
16835 printf ("C62x\n");
16836 break;
16837 case C6XABI_Tag_ISA_C67X:
16838 printf ("C67x\n");
16839 break;
16840 case C6XABI_Tag_ISA_C67XP:
16841 printf ("C67x+\n");
16842 break;
16843 case C6XABI_Tag_ISA_C64X:
16844 printf ("C64x\n");
16845 break;
16846 case C6XABI_Tag_ISA_C64XP:
16847 printf ("C64x+\n");
16848 break;
16849 case C6XABI_Tag_ISA_C674X:
16850 printf ("C674x\n");
16851 break;
16852 default:
16853 printf ("??? (%d)\n", val);
16854 break;
16855 }
16856 return p;
16857
16858 case Tag_ABI_wchar_t:
16859 printf (" Tag_ABI_wchar_t: ");
16860 READ_ULEB (val, p, end);
16861 switch (val)
16862 {
16863 case 0:
16864 printf (_("Not used\n"));
16865 break;
16866 case 1:
16867 printf (_("2 bytes\n"));
16868 break;
16869 case 2:
16870 printf (_("4 bytes\n"));
16871 break;
16872 default:
16873 printf ("??? (%d)\n", val);
16874 break;
16875 }
16876 return p;
16877
16878 case Tag_ABI_stack_align_needed:
16879 printf (" Tag_ABI_stack_align_needed: ");
16880 READ_ULEB (val, p, end);
16881 switch (val)
16882 {
16883 case 0:
16884 printf (_("8-byte\n"));
16885 break;
16886 case 1:
16887 printf (_("16-byte\n"));
16888 break;
16889 default:
16890 printf ("??? (%d)\n", val);
16891 break;
16892 }
16893 return p;
16894
16895 case Tag_ABI_stack_align_preserved:
16896 READ_ULEB (val, p, end);
16897 printf (" Tag_ABI_stack_align_preserved: ");
16898 switch (val)
16899 {
16900 case 0:
16901 printf (_("8-byte\n"));
16902 break;
16903 case 1:
16904 printf (_("16-byte\n"));
16905 break;
16906 default:
16907 printf ("??? (%d)\n", val);
16908 break;
16909 }
16910 return p;
16911
16912 case Tag_ABI_DSBT:
16913 READ_ULEB (val, p, end);
16914 printf (" Tag_ABI_DSBT: ");
16915 switch (val)
16916 {
16917 case 0:
16918 printf (_("DSBT addressing not used\n"));
16919 break;
16920 case 1:
16921 printf (_("DSBT addressing used\n"));
16922 break;
16923 default:
16924 printf ("??? (%d)\n", val);
16925 break;
16926 }
16927 return p;
16928
16929 case Tag_ABI_PID:
16930 READ_ULEB (val, p, end);
16931 printf (" Tag_ABI_PID: ");
16932 switch (val)
16933 {
16934 case 0:
16935 printf (_("Data addressing position-dependent\n"));
16936 break;
16937 case 1:
16938 printf (_("Data addressing position-independent, GOT near DP\n"));
16939 break;
16940 case 2:
16941 printf (_("Data addressing position-independent, GOT far from DP\n"));
16942 break;
16943 default:
16944 printf ("??? (%d)\n", val);
16945 break;
16946 }
16947 return p;
16948
16949 case Tag_ABI_PIC:
16950 READ_ULEB (val, p, end);
16951 printf (" Tag_ABI_PIC: ");
16952 switch (val)
16953 {
16954 case 0:
16955 printf (_("Code addressing position-dependent\n"));
16956 break;
16957 case 1:
16958 printf (_("Code addressing position-independent\n"));
16959 break;
16960 default:
16961 printf ("??? (%d)\n", val);
16962 break;
16963 }
16964 return p;
16965
16966 case Tag_ABI_array_object_alignment:
16967 READ_ULEB (val, p, end);
16968 printf (" Tag_ABI_array_object_alignment: ");
16969 switch (val)
16970 {
16971 case 0:
16972 printf (_("8-byte\n"));
16973 break;
16974 case 1:
16975 printf (_("4-byte\n"));
16976 break;
16977 case 2:
16978 printf (_("16-byte\n"));
16979 break;
16980 default:
16981 printf ("??? (%d)\n", val);
16982 break;
16983 }
16984 return p;
16985
16986 case Tag_ABI_array_object_align_expected:
16987 READ_ULEB (val, p, end);
16988 printf (" Tag_ABI_array_object_align_expected: ");
16989 switch (val)
16990 {
16991 case 0:
16992 printf (_("8-byte\n"));
16993 break;
16994 case 1:
16995 printf (_("4-byte\n"));
16996 break;
16997 case 2:
16998 printf (_("16-byte\n"));
16999 break;
17000 default:
17001 printf ("??? (%d)\n", val);
17002 break;
17003 }
17004 return p;
17005
17006 case Tag_ABI_compatibility:
17007 {
17008 READ_ULEB (val, p, end);
17009 printf (" Tag_ABI_compatibility: ");
17010 printf (_("flag = %d, vendor = "), val);
17011 if (p < end - 1)
17012 {
17013 size_t maxlen = (end - p) - 1;
17014
17015 print_symbol ((int) maxlen, (const char *) p);
17016 p += strnlen ((char *) p, maxlen) + 1;
17017 }
17018 else
17019 {
17020 printf (_("<corrupt>"));
17021 p = (unsigned char *) end;
17022 }
17023 putchar ('\n');
17024 return p;
17025 }
17026
17027 case Tag_ABI_conformance:
17028 {
17029 printf (" Tag_ABI_conformance: \"");
17030 if (p < end - 1)
17031 {
17032 size_t maxlen = (end - p) - 1;
17033
17034 print_symbol ((int) maxlen, (const char *) p);
17035 p += strnlen ((char *) p, maxlen) + 1;
17036 }
17037 else
17038 {
17039 printf (_("<corrupt>"));
17040 p = (unsigned char *) end;
17041 }
17042 printf ("\"\n");
17043 return p;
17044 }
17045 }
17046
17047 return display_tag_value (tag, p, end);
17048}
17049
17050static void
17051display_raw_attribute (unsigned char * p, unsigned char const * const end)
17052{
17053 unsigned long addr = 0;
17054 size_t bytes = end - p;
17055
17056 assert (end >= p);
17057 while (bytes)
17058 {
17059 int j;
17060 int k;
17061 int lbytes = (bytes > 16 ? 16 : bytes);
17062
17063 printf (" 0x%8.8lx ", addr);
17064
17065 for (j = 0; j < 16; j++)
17066 {
17067 if (j < lbytes)
17068 printf ("%2.2x", p[j]);
17069 else
17070 printf (" ");
17071
17072 if ((j & 3) == 3)
17073 printf (" ");
17074 }
17075
17076 for (j = 0; j < lbytes; j++)
17077 {
17078 k = p[j];
17079 if (k >= ' ' && k < 0x7f)
17080 printf ("%c", k);
17081 else
17082 printf (".");
17083 }
17084
17085 putchar ('\n');
17086
17087 p += lbytes;
17088 bytes -= lbytes;
17089 addr += lbytes;
17090 }
17091
17092 putchar ('\n');
17093}
17094
17095static unsigned char *
17096display_msp430_attribute (unsigned char * p,
17097 const unsigned char * const end)
17098{
17099 unsigned int val;
17100 unsigned int tag;
17101
17102 READ_ULEB (tag, p, end);
17103
17104 switch (tag)
17105 {
17106 case OFBA_MSPABI_Tag_ISA:
17107 printf (" Tag_ISA: ");
17108 READ_ULEB (val, p, end);
17109 switch (val)
17110 {
17111 case 0: printf (_("None\n")); break;
17112 case 1: printf (_("MSP430\n")); break;
17113 case 2: printf (_("MSP430X\n")); break;
17114 default: printf ("??? (%d)\n", val); break;
17115 }
17116 break;
17117
17118 case OFBA_MSPABI_Tag_Code_Model:
17119 printf (" Tag_Code_Model: ");
17120 READ_ULEB (val, p, end);
17121 switch (val)
17122 {
17123 case 0: printf (_("None\n")); break;
17124 case 1: printf (_("Small\n")); break;
17125 case 2: printf (_("Large\n")); break;
17126 default: printf ("??? (%d)\n", val); break;
17127 }
17128 break;
17129
17130 case OFBA_MSPABI_Tag_Data_Model:
17131 printf (" Tag_Data_Model: ");
17132 READ_ULEB (val, p, end);
17133 switch (val)
17134 {
17135 case 0: printf (_("None\n")); break;
17136 case 1: printf (_("Small\n")); break;
17137 case 2: printf (_("Large\n")); break;
17138 case 3: printf (_("Restricted Large\n")); break;
17139 default: printf ("??? (%d)\n", val); break;
17140 }
17141 break;
17142
17143 default:
17144 printf (_(" <unknown tag %d>: "), tag);
17145
17146 if (tag & 1)
17147 {
17148 putchar ('"');
17149 if (p < end - 1)
17150 {
17151 size_t maxlen = (end - p) - 1;
17152
17153 print_symbol ((int) maxlen, (const char *) p);
17154 p += strnlen ((char *) p, maxlen) + 1;
17155 }
17156 else
17157 {
17158 printf (_("<corrupt>"));
17159 p = (unsigned char *) end;
17160 }
17161 printf ("\"\n");
17162 }
17163 else
17164 {
17165 READ_ULEB (val, p, end);
17166 printf ("%d (0x%x)\n", val, val);
17167 }
17168 break;
17169 }
17170
17171 assert (p <= end);
17172 return p;
17173}
17174
17175static unsigned char *
17176display_msp430_gnu_attribute (unsigned char * p,
17177 unsigned int tag,
17178 const unsigned char * const end)
17179{
17180 if (tag == Tag_GNU_MSP430_Data_Region)
17181 {
17182 unsigned int val;
17183
17184 printf (" Tag_GNU_MSP430_Data_Region: ");
17185 READ_ULEB (val, p, end);
17186
17187 switch (val)
17188 {
17189 case Val_GNU_MSP430_Data_Region_Any:
17190 printf (_("Any Region\n"));
17191 break;
17192 case Val_GNU_MSP430_Data_Region_Lower:
17193 printf (_("Lower Region Only\n"));
17194 break;
17195 default:
17196 printf ("??? (%u)\n", val);
17197 }
17198 return p;
17199 }
17200 return display_tag_value (tag & 1, p, end);
17201}
17202
17203struct riscv_attr_tag_t {
17204 const char *name;
17205 unsigned int tag;
17206};
17207
17208static struct riscv_attr_tag_t riscv_attr_tag[] =
17209{
17210#define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
17211 T(arch),
17212 T(priv_spec),
17213 T(priv_spec_minor),
17214 T(priv_spec_revision),
17215 T(unaligned_access),
17216 T(stack_align),
17217#undef T
17218};
17219
17220static unsigned char *
17221display_riscv_attribute (unsigned char *p,
17222 const unsigned char * const end)
17223{
17224 unsigned int val;
17225 unsigned int tag;
17226 struct riscv_attr_tag_t *attr = NULL;
17227 unsigned i;
17228
17229 READ_ULEB (tag, p, end);
17230
17231 /* Find the name of attribute. */
17232 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
17233 {
17234 if (riscv_attr_tag[i].tag == tag)
17235 {
17236 attr = &riscv_attr_tag[i];
17237 break;
17238 }
17239 }
17240
17241 if (attr)
17242 printf (" %s: ", attr->name);
17243 else
17244 return display_tag_value (tag, p, end);
17245
17246 switch (tag)
17247 {
17248 case Tag_RISCV_priv_spec:
17249 case Tag_RISCV_priv_spec_minor:
17250 case Tag_RISCV_priv_spec_revision:
17251 READ_ULEB (val, p, end);
17252 printf (_("%u\n"), val);
17253 break;
17254 case Tag_RISCV_unaligned_access:
17255 READ_ULEB (val, p, end);
17256 switch (val)
17257 {
17258 case 0:
17259 printf (_("No unaligned access\n"));
17260 break;
17261 case 1:
17262 printf (_("Unaligned access\n"));
17263 break;
17264 }
17265 break;
17266 case Tag_RISCV_stack_align:
17267 READ_ULEB (val, p, end);
17268 printf (_("%u-bytes\n"), val);
17269 break;
17270 case Tag_RISCV_arch:
17271 p = display_tag_value (-1, p, end);
17272 break;
17273 default:
17274 return display_tag_value (tag, p, end);
17275 }
17276
17277 return p;
17278}
17279
17280static unsigned char *
17281display_csky_attribute (unsigned char * p,
17282 const unsigned char * const end)
17283{
17284 unsigned int tag;
17285 unsigned int val;
17286 READ_ULEB (tag, p, end);
17287
17288 if (tag >= Tag_CSKY_MAX)
17289 {
17290 return display_tag_value (-1, p, end);
17291 }
17292
17293 switch (tag)
17294 {
17295 case Tag_CSKY_ARCH_NAME:
17296 printf (" Tag_CSKY_ARCH_NAME:\t\t");
17297 return display_tag_value (-1, p, end);
17298 case Tag_CSKY_CPU_NAME:
17299 printf (" Tag_CSKY_CPU_NAME:\t\t");
17300 return display_tag_value (-1, p, end);
17301
17302 case Tag_CSKY_ISA_FLAGS:
17303 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
17304 return display_tag_value (0, p, end);
17305 case Tag_CSKY_ISA_EXT_FLAGS:
17306 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
17307 return display_tag_value (0, p, end);
17308
17309 case Tag_CSKY_DSP_VERSION:
17310 printf (" Tag_CSKY_DSP_VERSION:\t\t");
17311 READ_ULEB (val, p, end);
17312 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
17313 printf ("DSP Extension\n");
17314 else if (val == VAL_CSKY_DSP_VERSION_2)
17315 printf ("DSP 2.0\n");
17316 break;
17317
17318 case Tag_CSKY_VDSP_VERSION:
17319 printf (" Tag_CSKY_VDSP_VERSION:\t");
17320 READ_ULEB (val, p, end);
17321 printf ("VDSP Version %d\n", val);
17322 break;
17323
17324 case Tag_CSKY_FPU_VERSION:
17325 printf (" Tag_CSKY_FPU_VERSION:\t\t");
17326 READ_ULEB (val, p, end);
17327 if (val == VAL_CSKY_FPU_VERSION_1)
17328 printf ("ABIV1 FPU Version 1\n");
17329 else if (val == VAL_CSKY_FPU_VERSION_2)
17330 printf ("FPU Version 2\n");
17331 break;
17332
17333 case Tag_CSKY_FPU_ABI:
17334 printf (" Tag_CSKY_FPU_ABI:\t\t");
17335 READ_ULEB (val, p, end);
17336 if (val == VAL_CSKY_FPU_ABI_HARD)
17337 printf ("Hard\n");
17338 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
17339 printf ("SoftFP\n");
17340 else if (val == VAL_CSKY_FPU_ABI_SOFT)
17341 printf ("Soft\n");
17342 break;
17343 case Tag_CSKY_FPU_ROUNDING:
17344 READ_ULEB (val, p, end);
17345 if (val == 1)
17346 {
17347 printf (" Tag_CSKY_FPU_ROUNDING:\t");
17348 printf ("Needed\n");
17349 }
17350 break;
17351 case Tag_CSKY_FPU_DENORMAL:
17352 READ_ULEB (val, p, end);
17353 if (val == 1)
17354 {
17355 printf (" Tag_CSKY_FPU_DENORMAL:\t");
17356 printf ("Needed\n");
17357 }
17358 break;
17359 case Tag_CSKY_FPU_Exception:
17360 READ_ULEB (val, p, end);
17361 if (val == 1)
17362 {
17363 printf (" Tag_CSKY_FPU_Exception:\t");
17364 printf ("Needed\n");
17365 }
17366 break;
17367 case Tag_CSKY_FPU_NUMBER_MODULE:
17368 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
17369 return display_tag_value (-1, p, end);
17370 case Tag_CSKY_FPU_HARDFP:
17371 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
17372 READ_ULEB (val, p, end);
17373 if (val & VAL_CSKY_FPU_HARDFP_HALF)
17374 printf (" Half");
17375 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
17376 printf (" Single");
17377 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
17378 printf (" Double");
17379 printf ("\n");
17380 break;
17381 default:
17382 return display_tag_value (tag, p, end);
17383 }
17384 return p;
17385}
17386
17387static bool
17388process_attributes (Filedata * filedata,
17389 const char * public_name,
17390 unsigned int proc_type,
17391 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
17392 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
17393{
17394 Elf_Internal_Shdr * sect;
17395 unsigned i;
17396 bool res = true;
17397
17398 /* Find the section header so that we get the size. */
17399 for (i = 0, sect = filedata->section_headers;
17400 i < filedata->file_header.e_shnum;
17401 i++, sect++)
17402 {
17403 unsigned char * contents;
17404 unsigned char * p;
17405
17406 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
17407 continue;
17408
17409 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
17410 sect->sh_size, _("attributes"));
17411 if (contents == NULL)
17412 {
17413 res = false;
17414 continue;
17415 }
17416
17417 p = contents;
17418 /* The first character is the version of the attributes.
17419 Currently only version 1, (aka 'A') is recognised here. */
17420 if (*p != 'A')
17421 {
17422 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
17423 res = false;
17424 }
17425 else
17426 {
17427 bfd_vma section_len;
17428
17429 section_len = sect->sh_size - 1;
17430 p++;
17431
17432 while (section_len > 0)
17433 {
17434 bfd_vma attr_len;
17435 unsigned int namelen;
17436 bool public_section;
17437 bool gnu_section;
17438
17439 if (section_len <= 4)
17440 {
17441 error (_("Tag section ends prematurely\n"));
17442 res = false;
17443 break;
17444 }
17445 attr_len = byte_get (p, 4);
17446 p += 4;
17447
17448 if (attr_len > section_len)
17449 {
17450 error (_("Bad attribute length (%u > %u)\n"),
17451 (unsigned) attr_len, (unsigned) section_len);
17452 attr_len = section_len;
17453 res = false;
17454 }
17455 /* PR 17531: file: 001-101425-0.004 */
17456 else if (attr_len < 5)
17457 {
17458 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
17459 res = false;
17460 break;
17461 }
17462
17463 section_len -= attr_len;
17464 attr_len -= 4;
17465
17466 namelen = strnlen ((char *) p, attr_len) + 1;
17467 if (namelen == 0 || namelen >= attr_len)
17468 {
17469 error (_("Corrupt attribute section name\n"));
17470 res = false;
17471 break;
17472 }
17473
17474 printf (_("Attribute Section: "));
17475 print_symbol (INT_MAX, (const char *) p);
17476 putchar ('\n');
17477
17478 if (public_name && streq ((char *) p, public_name))
17479 public_section = true;
17480 else
17481 public_section = false;
17482
17483 if (streq ((char *) p, "gnu"))
17484 gnu_section = true;
17485 else
17486 gnu_section = false;
17487
17488 p += namelen;
17489 attr_len -= namelen;
17490
17491 while (attr_len > 0 && p < contents + sect->sh_size)
17492 {
17493 int tag;
17494 unsigned int val;
17495 bfd_vma size;
17496 unsigned char * end;
17497
17498 /* PR binutils/17531: Safe handling of corrupt files. */
17499 if (attr_len < 6)
17500 {
17501 error (_("Unused bytes at end of section\n"));
17502 res = false;
17503 section_len = 0;
17504 break;
17505 }
17506
17507 tag = *(p++);
17508 size = byte_get (p, 4);
17509 if (size > attr_len)
17510 {
17511 error (_("Bad subsection length (%u > %u)\n"),
17512 (unsigned) size, (unsigned) attr_len);
17513 res = false;
17514 size = attr_len;
17515 }
17516 /* PR binutils/17531: Safe handling of corrupt files. */
17517 if (size < 6)
17518 {
17519 error (_("Bad subsection length (%u < 6)\n"),
17520 (unsigned) size);
17521 res = false;
17522 section_len = 0;
17523 break;
17524 }
17525
17526 attr_len -= size;
17527 end = p + size - 1;
17528 assert (end <= contents + sect->sh_size);
17529 p += 4;
17530
17531 switch (tag)
17532 {
17533 case 1:
17534 printf (_("File Attributes\n"));
17535 break;
17536 case 2:
17537 printf (_("Section Attributes:"));
17538 goto do_numlist;
17539 case 3:
17540 printf (_("Symbol Attributes:"));
17541 /* Fall through. */
17542 do_numlist:
17543 for (;;)
17544 {
17545 READ_ULEB (val, p, end);
17546 if (val == 0)
17547 break;
17548 printf (" %d", val);
17549 }
17550 printf ("\n");
17551 break;
17552 default:
17553 printf (_("Unknown tag: %d\n"), tag);
17554 public_section = false;
17555 break;
17556 }
17557
17558 if (public_section && display_pub_attribute != NULL)
17559 {
17560 while (p < end)
17561 p = display_pub_attribute (p, end);
17562 assert (p == end);
17563 }
17564 else if (gnu_section && display_proc_gnu_attribute != NULL)
17565 {
17566 while (p < end)
17567 p = display_gnu_attribute (p,
17568 display_proc_gnu_attribute,
17569 end);
17570 assert (p == end);
17571 }
17572 else if (p < end)
17573 {
17574 printf (_(" Unknown attribute:\n"));
17575 display_raw_attribute (p, end);
17576 p = end;
17577 }
17578 else
17579 attr_len = 0;
17580 }
17581 }
17582 }
17583
17584 free (contents);
17585 }
17586
17587 return res;
17588}
17589
17590/* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
17591 Print the Address, Access and Initial fields of an entry at VMA ADDR
17592 and return the VMA of the next entry, or -1 if there was a problem.
17593 Does not read from DATA_END or beyond. */
17594
17595static bfd_vma
17596print_mips_got_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr,
17597 unsigned char * data_end)
17598{
17599 printf (" ");
17600 print_vma (addr, LONG_HEX);
17601 printf (" ");
17602 if (addr < pltgot + 0xfff0)
17603 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
17604 else
17605 printf ("%10s", "");
17606 printf (" ");
17607 if (data == NULL)
17608 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
17609 else
17610 {
17611 bfd_vma entry;
17612 unsigned char * from = data + addr - pltgot;
17613
17614 if (from + (is_32bit_elf ? 4 : 8) > data_end)
17615 {
17616 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
17617 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
17618 return (bfd_vma) -1;
17619 }
17620 else
17621 {
17622 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17623 print_vma (entry, LONG_HEX);
17624 }
17625 }
17626 return addr + (is_32bit_elf ? 4 : 8);
17627}
17628
17629/* DATA points to the contents of a MIPS PLT GOT that starts at VMA
17630 PLTGOT. Print the Address and Initial fields of an entry at VMA
17631 ADDR and return the VMA of the next entry. */
17632
17633static bfd_vma
17634print_mips_pltgot_entry (unsigned char * data, bfd_vma pltgot, bfd_vma addr)
17635{
17636 printf (" ");
17637 print_vma (addr, LONG_HEX);
17638 printf (" ");
17639 if (data == NULL)
17640 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
17641 else
17642 {
17643 bfd_vma entry;
17644
17645 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
17646 print_vma (entry, LONG_HEX);
17647 }
17648 return addr + (is_32bit_elf ? 4 : 8);
17649}
17650
17651static void
17652print_mips_ases (unsigned int mask)
17653{
17654 if (mask & AFL_ASE_DSP)
17655 fputs ("\n\tDSP ASE", stdout);
17656 if (mask & AFL_ASE_DSPR2)
17657 fputs ("\n\tDSP R2 ASE", stdout);
17658 if (mask & AFL_ASE_DSPR3)
17659 fputs ("\n\tDSP R3 ASE", stdout);
17660 if (mask & AFL_ASE_EVA)
17661 fputs ("\n\tEnhanced VA Scheme", stdout);
17662 if (mask & AFL_ASE_MCU)
17663 fputs ("\n\tMCU (MicroController) ASE", stdout);
17664 if (mask & AFL_ASE_MDMX)
17665 fputs ("\n\tMDMX ASE", stdout);
17666 if (mask & AFL_ASE_MIPS3D)
17667 fputs ("\n\tMIPS-3D ASE", stdout);
17668 if (mask & AFL_ASE_MT)
17669 fputs ("\n\tMT ASE", stdout);
17670 if (mask & AFL_ASE_SMARTMIPS)
17671 fputs ("\n\tSmartMIPS ASE", stdout);
17672 if (mask & AFL_ASE_VIRT)
17673 fputs ("\n\tVZ ASE", stdout);
17674 if (mask & AFL_ASE_MSA)
17675 fputs ("\n\tMSA ASE", stdout);
17676 if (mask & AFL_ASE_MIPS16)
17677 fputs ("\n\tMIPS16 ASE", stdout);
17678 if (mask & AFL_ASE_MICROMIPS)
17679 fputs ("\n\tMICROMIPS ASE", stdout);
17680 if (mask & AFL_ASE_XPA)
17681 fputs ("\n\tXPA ASE", stdout);
17682 if (mask & AFL_ASE_MIPS16E2)
17683 fputs ("\n\tMIPS16e2 ASE", stdout);
17684 if (mask & AFL_ASE_CRC)
17685 fputs ("\n\tCRC ASE", stdout);
17686 if (mask & AFL_ASE_GINV)
17687 fputs ("\n\tGINV ASE", stdout);
17688 if (mask & AFL_ASE_LOONGSON_MMI)
17689 fputs ("\n\tLoongson MMI ASE", stdout);
17690 if (mask & AFL_ASE_LOONGSON_CAM)
17691 fputs ("\n\tLoongson CAM ASE", stdout);
17692 if (mask & AFL_ASE_LOONGSON_EXT)
17693 fputs ("\n\tLoongson EXT ASE", stdout);
17694 if (mask & AFL_ASE_LOONGSON_EXT2)
17695 fputs ("\n\tLoongson EXT2 ASE", stdout);
17696 if (mask == 0)
17697 fprintf (stdout, "\n\t%s", _("None"));
17698 else if ((mask & ~AFL_ASE_MASK) != 0)
17699 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
17700}
17701
17702static void
17703print_mips_isa_ext (unsigned int isa_ext)
17704{
17705 switch (isa_ext)
17706 {
17707 case 0:
17708 fputs (_("None"), stdout);
17709 break;
17710 case AFL_EXT_XLR:
17711 fputs ("RMI XLR", stdout);
17712 break;
17713 case AFL_EXT_OCTEON3:
17714 fputs ("Cavium Networks Octeon3", stdout);
17715 break;
17716 case AFL_EXT_OCTEON2:
17717 fputs ("Cavium Networks Octeon2", stdout);
17718 break;
17719 case AFL_EXT_OCTEONP:
17720 fputs ("Cavium Networks OcteonP", stdout);
17721 break;
17722 case AFL_EXT_OCTEON:
17723 fputs ("Cavium Networks Octeon", stdout);
17724 break;
17725 case AFL_EXT_5900:
17726 fputs ("Toshiba R5900", stdout);
17727 break;
17728 case AFL_EXT_4650:
17729 fputs ("MIPS R4650", stdout);
17730 break;
17731 case AFL_EXT_4010:
17732 fputs ("LSI R4010", stdout);
17733 break;
17734 case AFL_EXT_4100:
17735 fputs ("NEC VR4100", stdout);
17736 break;
17737 case AFL_EXT_3900:
17738 fputs ("Toshiba R3900", stdout);
17739 break;
17740 case AFL_EXT_10000:
17741 fputs ("MIPS R10000", stdout);
17742 break;
17743 case AFL_EXT_SB1:
17744 fputs ("Broadcom SB-1", stdout);
17745 break;
17746 case AFL_EXT_4111:
17747 fputs ("NEC VR4111/VR4181", stdout);
17748 break;
17749 case AFL_EXT_4120:
17750 fputs ("NEC VR4120", stdout);
17751 break;
17752 case AFL_EXT_5400:
17753 fputs ("NEC VR5400", stdout);
17754 break;
17755 case AFL_EXT_5500:
17756 fputs ("NEC VR5500", stdout);
17757 break;
17758 case AFL_EXT_LOONGSON_2E:
17759 fputs ("ST Microelectronics Loongson 2E", stdout);
17760 break;
17761 case AFL_EXT_LOONGSON_2F:
17762 fputs ("ST Microelectronics Loongson 2F", stdout);
17763 break;
17764 case AFL_EXT_INTERAPTIV_MR2:
17765 fputs ("Imagination interAptiv MR2", stdout);
17766 break;
17767 default:
17768 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
17769 }
17770}
17771
17772static signed int
17773get_mips_reg_size (int reg_size)
17774{
17775 return (reg_size == AFL_REG_NONE) ? 0
17776 : (reg_size == AFL_REG_32) ? 32
17777 : (reg_size == AFL_REG_64) ? 64
17778 : (reg_size == AFL_REG_128) ? 128
17779 : -1;
17780}
17781
17782static bool
17783process_mips_specific (Filedata * filedata)
17784{
17785 Elf_Internal_Dyn * entry;
17786 Elf_Internal_Shdr *sect = NULL;
17787 size_t liblist_offset = 0;
17788 size_t liblistno = 0;
17789 size_t conflictsno = 0;
17790 size_t options_offset = 0;
17791 size_t conflicts_offset = 0;
17792 size_t pltrelsz = 0;
17793 size_t pltrel = 0;
17794 bfd_vma pltgot = 0;
17795 bfd_vma mips_pltgot = 0;
17796 bfd_vma jmprel = 0;
17797 bfd_vma local_gotno = 0;
17798 bfd_vma gotsym = 0;
17799 bfd_vma symtabno = 0;
17800 bool res = true;
17801
17802 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
17803 display_mips_gnu_attribute))
17804 res = false;
17805
17806 sect = find_section (filedata, ".MIPS.abiflags");
17807
17808 if (sect != NULL)
17809 {
17810 Elf_External_ABIFlags_v0 *abiflags_ext;
17811 Elf_Internal_ABIFlags_v0 abiflags_in;
17812
17813 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
17814 {
17815 error (_("Corrupt MIPS ABI Flags section.\n"));
17816 res = false;
17817 }
17818 else
17819 {
17820 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
17821 sect->sh_size, _("MIPS ABI Flags section"));
17822 if (abiflags_ext)
17823 {
17824 abiflags_in.version = BYTE_GET (abiflags_ext->version);
17825 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
17826 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
17827 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
17828 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
17829 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
17830 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
17831 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
17832 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
17833 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
17834 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
17835
17836 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
17837 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
17838 if (abiflags_in.isa_rev > 1)
17839 printf ("r%d", abiflags_in.isa_rev);
17840 printf ("\nGPR size: %d",
17841 get_mips_reg_size (abiflags_in.gpr_size));
17842 printf ("\nCPR1 size: %d",
17843 get_mips_reg_size (abiflags_in.cpr1_size));
17844 printf ("\nCPR2 size: %d",
17845 get_mips_reg_size (abiflags_in.cpr2_size));
17846 fputs ("\nFP ABI: ", stdout);
17847 print_mips_fp_abi_value (abiflags_in.fp_abi);
17848 fputs ("ISA Extension: ", stdout);
17849 print_mips_isa_ext (abiflags_in.isa_ext);
17850 fputs ("\nASEs:", stdout);
17851 print_mips_ases (abiflags_in.ases);
17852 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
17853 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
17854 fputc ('\n', stdout);
17855 free (abiflags_ext);
17856 }
17857 }
17858 }
17859
17860 /* We have a lot of special sections. Thanks SGI! */
17861 if (filedata->dynamic_section == NULL)
17862 {
17863 /* No dynamic information available. See if there is static GOT. */
17864 sect = find_section (filedata, ".got");
17865 if (sect != NULL)
17866 {
17867 unsigned char *data_end;
17868 unsigned char *data;
17869 bfd_vma ent, end;
17870 int addr_size;
17871
17872 pltgot = sect->sh_addr;
17873
17874 ent = pltgot;
17875 addr_size = (is_32bit_elf ? 4 : 8);
17876 end = pltgot + sect->sh_size;
17877
17878 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
17879 end - pltgot, 1,
17880 _("Global Offset Table data"));
17881 /* PR 12855: Null data is handled gracefully throughout. */
17882 data_end = data + (end - pltgot);
17883
17884 printf (_("\nStatic GOT:\n"));
17885 printf (_(" Canonical gp value: "));
17886 print_vma (ent + 0x7ff0, LONG_HEX);
17887 printf ("\n\n");
17888
17889 /* In a dynamic binary GOT[0] is reserved for the dynamic
17890 loader to store the lazy resolver pointer, however in
17891 a static binary it may well have been omitted and GOT
17892 reduced to a table of addresses.
17893 PR 21344: Check for the entry being fully available
17894 before fetching it. */
17895 if (data
17896 && data + ent - pltgot + addr_size <= data_end
17897 && byte_get (data + ent - pltgot, addr_size) == 0)
17898 {
17899 printf (_(" Reserved entries:\n"));
17900 printf (_(" %*s %10s %*s\n"),
17901 addr_size * 2, _("Address"), _("Access"),
17902 addr_size * 2, _("Value"));
17903 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17904 printf ("\n");
17905 if (ent == (bfd_vma) -1)
17906 goto sgot_print_fail;
17907
17908 /* Check for the MSB of GOT[1] being set, identifying a
17909 GNU object. This entry will be used by some runtime
17910 loaders, to store the module pointer. Otherwise this
17911 is an ordinary local entry.
17912 PR 21344: Check for the entry being fully available
17913 before fetching it. */
17914 if (data
17915 && data + ent - pltgot + addr_size <= data_end
17916 && (byte_get (data + ent - pltgot, addr_size)
17917 >> (addr_size * 8 - 1)) != 0)
17918 {
17919 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17920 printf ("\n");
17921 if (ent == (bfd_vma) -1)
17922 goto sgot_print_fail;
17923 }
17924 printf ("\n");
17925 }
17926
17927 if (data != NULL && ent < end)
17928 {
17929 printf (_(" Local entries:\n"));
17930 printf (" %*s %10s %*s\n",
17931 addr_size * 2, _("Address"), _("Access"),
17932 addr_size * 2, _("Value"));
17933 while (ent < end)
17934 {
17935 ent = print_mips_got_entry (data, pltgot, ent, data_end);
17936 printf ("\n");
17937 if (ent == (bfd_vma) -1)
17938 goto sgot_print_fail;
17939 }
17940 printf ("\n");
17941 }
17942
17943 sgot_print_fail:
17944 free (data);
17945 }
17946 return res;
17947 }
17948
17949 for (entry = filedata->dynamic_section;
17950 /* PR 17531 file: 012-50589-0.004. */
17951 (entry < filedata->dynamic_section + filedata->dynamic_nent
17952 && entry->d_tag != DT_NULL);
17953 ++entry)
17954 switch (entry->d_tag)
17955 {
17956 case DT_MIPS_LIBLIST:
17957 liblist_offset
17958 = offset_from_vma (filedata, entry->d_un.d_val,
17959 liblistno * sizeof (Elf32_External_Lib));
17960 break;
17961 case DT_MIPS_LIBLISTNO:
17962 liblistno = entry->d_un.d_val;
17963 break;
17964 case DT_MIPS_OPTIONS:
17965 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
17966 break;
17967 case DT_MIPS_CONFLICT:
17968 conflicts_offset
17969 = offset_from_vma (filedata, entry->d_un.d_val,
17970 conflictsno * sizeof (Elf32_External_Conflict));
17971 break;
17972 case DT_MIPS_CONFLICTNO:
17973 conflictsno = entry->d_un.d_val;
17974 break;
17975 case DT_PLTGOT:
17976 pltgot = entry->d_un.d_ptr;
17977 break;
17978 case DT_MIPS_LOCAL_GOTNO:
17979 local_gotno = entry->d_un.d_val;
17980 break;
17981 case DT_MIPS_GOTSYM:
17982 gotsym = entry->d_un.d_val;
17983 break;
17984 case DT_MIPS_SYMTABNO:
17985 symtabno = entry->d_un.d_val;
17986 break;
17987 case DT_MIPS_PLTGOT:
17988 mips_pltgot = entry->d_un.d_ptr;
17989 break;
17990 case DT_PLTREL:
17991 pltrel = entry->d_un.d_val;
17992 break;
17993 case DT_PLTRELSZ:
17994 pltrelsz = entry->d_un.d_val;
17995 break;
17996 case DT_JMPREL:
17997 jmprel = entry->d_un.d_ptr;
17998 break;
17999 default:
18000 break;
18001 }
18002
18003 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
18004 {
18005 Elf32_External_Lib * elib;
18006 size_t cnt;
18007
18008 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
18009 sizeof (Elf32_External_Lib),
18010 liblistno,
18011 _("liblist section data"));
18012 if (elib)
18013 {
18014 printf (ngettext ("\nSection '.liblist' contains %lu entry:\n",
18015 "\nSection '.liblist' contains %lu entries:\n",
18016 (unsigned long) liblistno),
18017 (unsigned long) liblistno);
18018 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
18019 stdout);
18020
18021 for (cnt = 0; cnt < liblistno; ++cnt)
18022 {
18023 Elf32_Lib liblist;
18024 time_t atime;
18025 char timebuf[128];
18026 struct tm * tmp;
18027
18028 liblist.l_name = BYTE_GET (elib[cnt].l_name);
18029 atime = BYTE_GET (elib[cnt].l_time_stamp);
18030 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18031 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18032 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18033
18034 tmp = gmtime (&atime);
18035 snprintf (timebuf, sizeof (timebuf),
18036 "%04u-%02u-%02uT%02u:%02u:%02u",
18037 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18038 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
18039
18040 printf ("%3lu: ", (unsigned long) cnt);
18041 if (valid_dynamic_name (filedata, liblist.l_name))
18042 print_symbol (20, get_dynamic_name (filedata, liblist.l_name));
18043 else
18044 printf (_("<corrupt: %9ld>"), liblist.l_name);
18045 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
18046 liblist.l_version);
18047
18048 if (liblist.l_flags == 0)
18049 puts (_(" NONE"));
18050 else
18051 {
18052 static const struct
18053 {
18054 const char * name;
18055 int bit;
18056 }
18057 l_flags_vals[] =
18058 {
18059 { " EXACT_MATCH", LL_EXACT_MATCH },
18060 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
18061 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
18062 { " EXPORTS", LL_EXPORTS },
18063 { " DELAY_LOAD", LL_DELAY_LOAD },
18064 { " DELTA", LL_DELTA }
18065 };
18066 int flags = liblist.l_flags;
18067 size_t fcnt;
18068
18069 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
18070 if ((flags & l_flags_vals[fcnt].bit) != 0)
18071 {
18072 fputs (l_flags_vals[fcnt].name, stdout);
18073 flags ^= l_flags_vals[fcnt].bit;
18074 }
18075 if (flags != 0)
18076 printf (" %#x", (unsigned int) flags);
18077
18078 puts ("");
18079 }
18080 }
18081
18082 free (elib);
18083 }
18084 else
18085 res = false;
18086 }
18087
18088 if (options_offset != 0)
18089 {
18090 Elf_External_Options * eopt;
18091 size_t offset;
18092 int cnt;
18093 sect = filedata->section_headers;
18094
18095 /* Find the section header so that we get the size. */
18096 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
18097 /* PR 17533 file: 012-277276-0.004. */
18098 if (sect == NULL)
18099 {
18100 error (_("No MIPS_OPTIONS header found\n"));
18101 return false;
18102 }
18103 /* PR 24243 */
18104 if (sect->sh_size < sizeof (* eopt))
18105 {
18106 error (_("The MIPS options section is too small.\n"));
18107 return false;
18108 }
18109
18110 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
18111 sect->sh_size, _("options"));
18112 if (eopt)
18113 {
18114 Elf_Internal_Options option;
18115
18116 offset = cnt = 0;
18117 while (offset <= sect->sh_size - sizeof (* eopt))
18118 {
18119 Elf_External_Options * eoption;
18120 unsigned int optsize;
18121
18122 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18123
18124 optsize = BYTE_GET (eoption->size);
18125
18126 /* PR 17531: file: ffa0fa3b. */
18127 if (optsize < sizeof (* eopt)
18128 || optsize > sect->sh_size - offset)
18129 {
18130 error (_("Invalid size (%u) for MIPS option\n"),
18131 optsize);
18132 free (eopt);
18133 return false;
18134 }
18135 offset += optsize;
18136 ++cnt;
18137 }
18138
18139 printf (ngettext ("\nSection '%s' contains %d entry:\n",
18140 "\nSection '%s' contains %d entries:\n",
18141 cnt),
18142 printable_section_name (filedata, sect), cnt);
18143
18144 offset = 0;
18145 while (cnt-- > 0)
18146 {
18147 size_t len;
18148 Elf_External_Options * eoption;
18149
18150 eoption = (Elf_External_Options *) ((char *) eopt + offset);
18151
18152 option.kind = BYTE_GET (eoption->kind);
18153 option.size = BYTE_GET (eoption->size);
18154 option.section = BYTE_GET (eoption->section);
18155 option.info = BYTE_GET (eoption->info);
18156
18157 switch (option.kind)
18158 {
18159 case ODK_NULL:
18160 /* This shouldn't happen. */
18161 printf (" NULL %" PRId16 " %" PRIx32,
18162 option.section, option.info);
18163 break;
18164
18165 case ODK_REGINFO:
18166 printf (" REGINFO ");
18167 if (filedata->file_header.e_machine == EM_MIPS)
18168 {
18169 Elf32_External_RegInfo * ereg;
18170 Elf32_RegInfo reginfo;
18171
18172 /* 32bit form. */
18173 if (option.size < (sizeof (Elf_External_Options)
18174 + sizeof (Elf32_External_RegInfo)))
18175 {
18176 printf (_("<corrupt>\n"));
18177 error (_("Truncated MIPS REGINFO option\n"));
18178 cnt = 0;
18179 break;
18180 }
18181
18182 ereg = (Elf32_External_RegInfo *) (eoption + 1);
18183
18184 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18185 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18186 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18187 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18188 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18189 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18190
18191 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
18192 reginfo.ri_gprmask, reginfo.ri_gp_value);
18193 printf (" "
18194 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18195 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18196 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18197 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18198 }
18199 else
18200 {
18201 /* 64 bit form. */
18202 Elf64_External_RegInfo * ereg;
18203 Elf64_Internal_RegInfo reginfo;
18204
18205 if (option.size < (sizeof (Elf_External_Options)
18206 + sizeof (Elf64_External_RegInfo)))
18207 {
18208 printf (_("<corrupt>\n"));
18209 error (_("Truncated MIPS REGINFO option\n"));
18210 cnt = 0;
18211 break;
18212 }
18213
18214 ereg = (Elf64_External_RegInfo *) (eoption + 1);
18215 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
18216 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
18217 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
18218 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
18219 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
18220 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
18221
18222 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
18223 reginfo.ri_gprmask, reginfo.ri_gp_value);
18224 printf (" "
18225 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
18226 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
18227 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
18228 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
18229 }
18230 offset += option.size;
18231 continue;
18232
18233 case ODK_EXCEPTIONS:
18234 fputs (" EXCEPTIONS fpe_min(", stdout);
18235 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
18236 fputs (") fpe_max(", stdout);
18237 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
18238 fputs (")", stdout);
18239
18240 if (option.info & OEX_PAGE0)
18241 fputs (" PAGE0", stdout);
18242 if (option.info & OEX_SMM)
18243 fputs (" SMM", stdout);
18244 if (option.info & OEX_FPDBUG)
18245 fputs (" FPDBUG", stdout);
18246 if (option.info & OEX_DISMISS)
18247 fputs (" DISMISS", stdout);
18248 break;
18249
18250 case ODK_PAD:
18251 fputs (" PAD ", stdout);
18252 if (option.info & OPAD_PREFIX)
18253 fputs (" PREFIX", stdout);
18254 if (option.info & OPAD_POSTFIX)
18255 fputs (" POSTFIX", stdout);
18256 if (option.info & OPAD_SYMBOL)
18257 fputs (" SYMBOL", stdout);
18258 break;
18259
18260 case ODK_HWPATCH:
18261 fputs (" HWPATCH ", stdout);
18262 if (option.info & OHW_R4KEOP)
18263 fputs (" R4KEOP", stdout);
18264 if (option.info & OHW_R8KPFETCH)
18265 fputs (" R8KPFETCH", stdout);
18266 if (option.info & OHW_R5KEOP)
18267 fputs (" R5KEOP", stdout);
18268 if (option.info & OHW_R5KCVTL)
18269 fputs (" R5KCVTL", stdout);
18270 break;
18271
18272 case ODK_FILL:
18273 fputs (" FILL ", stdout);
18274 /* XXX Print content of info word? */
18275 break;
18276
18277 case ODK_TAGS:
18278 fputs (" TAGS ", stdout);
18279 /* XXX Print content of info word? */
18280 break;
18281
18282 case ODK_HWAND:
18283 fputs (" HWAND ", stdout);
18284 if (option.info & OHWA0_R4KEOP_CHECKED)
18285 fputs (" R4KEOP_CHECKED", stdout);
18286 if (option.info & OHWA0_R4KEOP_CLEAN)
18287 fputs (" R4KEOP_CLEAN", stdout);
18288 break;
18289
18290 case ODK_HWOR:
18291 fputs (" HWOR ", stdout);
18292 if (option.info & OHWA0_R4KEOP_CHECKED)
18293 fputs (" R4KEOP_CHECKED", stdout);
18294 if (option.info & OHWA0_R4KEOP_CLEAN)
18295 fputs (" R4KEOP_CLEAN", stdout);
18296 break;
18297
18298 case ODK_GP_GROUP:
18299 printf (" GP_GROUP %#06x self-contained %#06x",
18300 option.info & OGP_GROUP,
18301 (option.info & OGP_SELF) >> 16);
18302 break;
18303
18304 case ODK_IDENT:
18305 printf (" IDENT %#06x self-contained %#06x",
18306 option.info & OGP_GROUP,
18307 (option.info & OGP_SELF) >> 16);
18308 break;
18309
18310 default:
18311 /* This shouldn't happen. */
18312 printf (" %3d ??? %" PRId16 " %" PRIx32,
18313 option.kind, option.section, option.info);
18314 break;
18315 }
18316
18317 len = sizeof (* eopt);
18318 while (len < option.size)
18319 {
18320 unsigned char datum = *((unsigned char *) eoption + len);
18321
18322 if (ISPRINT (datum))
18323 printf ("%c", datum);
18324 else
18325 printf ("\\%03o", datum);
18326 len ++;
18327 }
18328 fputs ("\n", stdout);
18329
18330 offset += option.size;
18331 }
18332 free (eopt);
18333 }
18334 else
18335 res = false;
18336 }
18337
18338 if (conflicts_offset != 0 && conflictsno != 0)
18339 {
18340 Elf32_Conflict * iconf;
18341 size_t cnt;
18342
18343 if (filedata->dynamic_symbols == NULL)
18344 {
18345 error (_("conflict list found without a dynamic symbol table\n"));
18346 return false;
18347 }
18348
18349 /* PR 21345 - print a slightly more helpful error message
18350 if we are sure that the cmalloc will fail. */
18351 if (conflictsno > filedata->file_size / sizeof (* iconf))
18352 {
18353 error (_("Overlarge number of conflicts detected: %lx\n"),
18354 (long) conflictsno);
18355 return false;
18356 }
18357
18358 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
18359 if (iconf == NULL)
18360 {
18361 error (_("Out of memory allocating space for dynamic conflicts\n"));
18362 return false;
18363 }
18364
18365 if (is_32bit_elf)
18366 {
18367 Elf32_External_Conflict * econf32;
18368
18369 econf32 = (Elf32_External_Conflict *)
18370 get_data (NULL, filedata, conflicts_offset,
18371 sizeof (*econf32), conflictsno, _("conflict"));
18372 if (!econf32)
18373 {
18374 free (iconf);
18375 return false;
18376 }
18377
18378 for (cnt = 0; cnt < conflictsno; ++cnt)
18379 iconf[cnt] = BYTE_GET (econf32[cnt]);
18380
18381 free (econf32);
18382 }
18383 else
18384 {
18385 Elf64_External_Conflict * econf64;
18386
18387 econf64 = (Elf64_External_Conflict *)
18388 get_data (NULL, filedata, conflicts_offset,
18389 sizeof (*econf64), conflictsno, _("conflict"));
18390 if (!econf64)
18391 {
18392 free (iconf);
18393 return false;
18394 }
18395
18396 for (cnt = 0; cnt < conflictsno; ++cnt)
18397 iconf[cnt] = BYTE_GET (econf64[cnt]);
18398
18399 free (econf64);
18400 }
18401
18402 printf (ngettext ("\nSection '.conflict' contains %lu entry:\n",
18403 "\nSection '.conflict' contains %lu entries:\n",
18404 (unsigned long) conflictsno),
18405 (unsigned long) conflictsno);
18406 puts (_(" Num: Index Value Name"));
18407
18408 for (cnt = 0; cnt < conflictsno; ++cnt)
18409 {
18410 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
18411
18412 if (iconf[cnt] >= filedata->num_dynamic_syms)
18413 printf (_("<corrupt symbol index>"));
18414 else
18415 {
18416 Elf_Internal_Sym * psym;
18417
18418 psym = & filedata->dynamic_symbols[iconf[cnt]];
18419 print_vma (psym->st_value, FULL_HEX);
18420 putchar (' ');
18421 if (valid_dynamic_name (filedata, psym->st_name))
18422 print_symbol (25, get_dynamic_name (filedata, psym->st_name));
18423 else
18424 printf (_("<corrupt: %14ld>"), psym->st_name);
18425 }
18426 putchar ('\n');
18427 }
18428
18429 free (iconf);
18430 }
18431
18432 if (pltgot != 0 && local_gotno != 0)
18433 {
18434 bfd_vma ent, local_end, global_end;
18435 size_t i, offset;
18436 unsigned char * data;
18437 unsigned char * data_end;
18438 int addr_size;
18439
18440 ent = pltgot;
18441 addr_size = (is_32bit_elf ? 4 : 8);
18442 local_end = pltgot + local_gotno * addr_size;
18443
18444 /* PR binutils/17533 file: 012-111227-0.004 */
18445 if (symtabno < gotsym)
18446 {
18447 error (_("The GOT symbol offset (%lu) is greater than the symbol table size (%lu)\n"),
18448 (unsigned long) gotsym, (unsigned long) symtabno);
18449 return false;
18450 }
18451
18452 global_end = local_end + (symtabno - gotsym) * addr_size;
18453 /* PR 17531: file: 54c91a34. */
18454 if (global_end < local_end)
18455 {
18456 error (_("Too many GOT symbols: %lu\n"), (unsigned long) symtabno);
18457 return false;
18458 }
18459
18460 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
18461 data = (unsigned char *) get_data (NULL, filedata, offset,
18462 global_end - pltgot, 1,
18463 _("Global Offset Table data"));
18464 /* PR 12855: Null data is handled gracefully throughout. */
18465 data_end = data + (global_end - pltgot);
18466
18467 printf (_("\nPrimary GOT:\n"));
18468 printf (_(" Canonical gp value: "));
18469 print_vma (pltgot + 0x7ff0, LONG_HEX);
18470 printf ("\n\n");
18471
18472 printf (_(" Reserved entries:\n"));
18473 printf (_(" %*s %10s %*s Purpose\n"),
18474 addr_size * 2, _("Address"), _("Access"),
18475 addr_size * 2, _("Initial"));
18476 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18477 printf (_(" Lazy resolver\n"));
18478 if (ent == (bfd_vma) -1)
18479 goto got_print_fail;
18480
18481 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
18482 This entry will be used by some runtime loaders, to store the
18483 module pointer. Otherwise this is an ordinary local entry.
18484 PR 21344: Check for the entry being fully available before
18485 fetching it. */
18486 if (data
18487 && data + ent - pltgot + addr_size <= data_end
18488 && (byte_get (data + ent - pltgot, addr_size)
18489 >> (addr_size * 8 - 1)) != 0)
18490 {
18491 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18492 printf (_(" Module pointer (GNU extension)\n"));
18493 if (ent == (bfd_vma) -1)
18494 goto got_print_fail;
18495 }
18496 printf ("\n");
18497
18498 if (data != NULL && ent < local_end)
18499 {
18500 printf (_(" Local entries:\n"));
18501 printf (" %*s %10s %*s\n",
18502 addr_size * 2, _("Address"), _("Access"),
18503 addr_size * 2, _("Initial"));
18504 while (ent < local_end)
18505 {
18506 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18507 printf ("\n");
18508 if (ent == (bfd_vma) -1)
18509 goto got_print_fail;
18510 }
18511 printf ("\n");
18512 }
18513
18514 if (data != NULL && gotsym < symtabno)
18515 {
18516 int sym_width;
18517
18518 printf (_(" Global entries:\n"));
18519 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
18520 addr_size * 2, _("Address"),
18521 _("Access"),
18522 addr_size * 2, _("Initial"),
18523 addr_size * 2, _("Sym.Val."),
18524 _("Type"),
18525 /* Note for translators: "Ndx" = abbreviated form of "Index". */
18526 _("Ndx"), _("Name"));
18527
18528 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
18529
18530 for (i = gotsym; i < symtabno; i++)
18531 {
18532 ent = print_mips_got_entry (data, pltgot, ent, data_end);
18533 printf (" ");
18534
18535 if (filedata->dynamic_symbols == NULL)
18536 printf (_("<no dynamic symbols>"));
18537 else if (i < filedata->num_dynamic_syms)
18538 {
18539 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
18540
18541 print_vma (psym->st_value, LONG_HEX);
18542 printf (" %-7s %3s ",
18543 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18544 get_symbol_index_type (filedata, psym->st_shndx));
18545
18546 if (valid_dynamic_name (filedata, psym->st_name))
18547 print_symbol (sym_width,
18548 get_dynamic_name (filedata, psym->st_name));
18549 else
18550 printf (_("<corrupt: %14ld>"), psym->st_name);
18551 }
18552 else
18553 printf (_("<symbol index %lu exceeds number of dynamic symbols>"),
18554 (unsigned long) i);
18555
18556 printf ("\n");
18557 if (ent == (bfd_vma) -1)
18558 break;
18559 }
18560 printf ("\n");
18561 }
18562
18563 got_print_fail:
18564 free (data);
18565 }
18566
18567 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
18568 {
18569 bfd_vma ent, end;
18570 size_t offset, rel_offset;
18571 unsigned long count, i;
18572 unsigned char * data;
18573 int addr_size, sym_width;
18574 Elf_Internal_Rela * rels;
18575
18576 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
18577 if (pltrel == DT_RELA)
18578 {
18579 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
18580 return false;
18581 }
18582 else
18583 {
18584 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
18585 return false;
18586 }
18587
18588 ent = mips_pltgot;
18589 addr_size = (is_32bit_elf ? 4 : 8);
18590 end = mips_pltgot + (2 + count) * addr_size;
18591
18592 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
18593 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
18594 1, _("Procedure Linkage Table data"));
18595 if (data == NULL)
18596 {
18597 free (rels);
18598 return false;
18599 }
18600
18601 printf ("\nPLT GOT:\n\n");
18602 printf (_(" Reserved entries:\n"));
18603 printf (_(" %*s %*s Purpose\n"),
18604 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
18605 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18606 printf (_(" PLT lazy resolver\n"));
18607 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18608 printf (_(" Module pointer\n"));
18609 printf ("\n");
18610
18611 printf (_(" Entries:\n"));
18612 printf (" %*s %*s %*s %-7s %3s %s\n",
18613 addr_size * 2, _("Address"),
18614 addr_size * 2, _("Initial"),
18615 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
18616 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
18617 for (i = 0; i < count; i++)
18618 {
18619 unsigned long idx = get_reloc_symindex (rels[i].r_info);
18620
18621 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
18622 printf (" ");
18623
18624 if (idx >= filedata->num_dynamic_syms)
18625 printf (_("<corrupt symbol index: %lu>"), idx);
18626 else
18627 {
18628 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
18629
18630 print_vma (psym->st_value, LONG_HEX);
18631 printf (" %-7s %3s ",
18632 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
18633 get_symbol_index_type (filedata, psym->st_shndx));
18634 if (valid_dynamic_name (filedata, psym->st_name))
18635 print_symbol (sym_width,
18636 get_dynamic_name (filedata, psym->st_name));
18637 else
18638 printf (_("<corrupt: %14ld>"), psym->st_name);
18639 }
18640 printf ("\n");
18641 }
18642 printf ("\n");
18643
18644 free (data);
18645 free (rels);
18646 }
18647
18648 return res;
18649}
18650
18651static bool
18652process_nds32_specific (Filedata * filedata)
18653{
18654 Elf_Internal_Shdr *sect = NULL;
18655
18656 sect = find_section (filedata, ".nds32_e_flags");
18657 if (sect != NULL && sect->sh_size >= 4)
18658 {
18659 unsigned char *buf;
18660 unsigned int flag;
18661
18662 printf ("\nNDS32 elf flags section:\n");
18663 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
18664 _("NDS32 elf flags section"));
18665
18666 if (buf == NULL)
18667 return false;
18668
18669 flag = byte_get (buf, 4);
18670 free (buf);
18671 switch (flag & 0x3)
18672 {
18673 case 0:
18674 printf ("(VEC_SIZE):\tNo entry.\n");
18675 break;
18676 case 1:
18677 printf ("(VEC_SIZE):\t4 bytes\n");
18678 break;
18679 case 2:
18680 printf ("(VEC_SIZE):\t16 bytes\n");
18681 break;
18682 case 3:
18683 printf ("(VEC_SIZE):\treserved\n");
18684 break;
18685 }
18686 }
18687
18688 return true;
18689}
18690
18691static bool
18692process_gnu_liblist (Filedata * filedata)
18693{
18694 Elf_Internal_Shdr * section;
18695 Elf_Internal_Shdr * string_sec;
18696 Elf32_External_Lib * elib;
18697 char * strtab;
18698 size_t strtab_size;
18699 size_t cnt;
18700 unsigned long num_liblist;
18701 unsigned i;
18702 bool res = true;
18703
18704 if (! do_arch)
18705 return true;
18706
18707 for (i = 0, section = filedata->section_headers;
18708 i < filedata->file_header.e_shnum;
18709 i++, section++)
18710 {
18711 switch (section->sh_type)
18712 {
18713 case SHT_GNU_LIBLIST:
18714 if (section->sh_link >= filedata->file_header.e_shnum)
18715 break;
18716
18717 elib = (Elf32_External_Lib *)
18718 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
18719 _("liblist section data"));
18720
18721 if (elib == NULL)
18722 {
18723 res = false;
18724 break;
18725 }
18726
18727 string_sec = filedata->section_headers + section->sh_link;
18728 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
18729 string_sec->sh_size,
18730 _("liblist string table"));
18731 if (strtab == NULL
18732 || section->sh_entsize != sizeof (Elf32_External_Lib))
18733 {
18734 free (elib);
18735 free (strtab);
18736 res = false;
18737 break;
18738 }
18739 strtab_size = string_sec->sh_size;
18740
18741 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
18742 printf (ngettext ("\nLibrary list section '%s' contains %lu entries:\n",
18743 "\nLibrary list section '%s' contains %lu entries:\n",
18744 num_liblist),
18745 printable_section_name (filedata, section),
18746 num_liblist);
18747
18748 puts (_(" Library Time Stamp Checksum Version Flags"));
18749
18750 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
18751 ++cnt)
18752 {
18753 Elf32_Lib liblist;
18754 time_t atime;
18755 char timebuf[128];
18756 struct tm * tmp;
18757
18758 liblist.l_name = BYTE_GET (elib[cnt].l_name);
18759 atime = BYTE_GET (elib[cnt].l_time_stamp);
18760 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
18761 liblist.l_version = BYTE_GET (elib[cnt].l_version);
18762 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
18763
18764 tmp = gmtime (&atime);
18765 snprintf (timebuf, sizeof (timebuf),
18766 "%04u-%02u-%02uT%02u:%02u:%02u",
18767 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
18768 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
18769
18770 printf ("%3lu: ", (unsigned long) cnt);
18771 if (do_wide)
18772 printf ("%-20s", liblist.l_name < strtab_size
18773 ? strtab + liblist.l_name : _("<corrupt>"));
18774 else
18775 printf ("%-20.20s", liblist.l_name < strtab_size
18776 ? strtab + liblist.l_name : _("<corrupt>"));
18777 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
18778 liblist.l_version, liblist.l_flags);
18779 }
18780
18781 free (elib);
18782 free (strtab);
18783 }
18784 }
18785
18786 return res;
18787}
18788
18789static const char *
18790get_note_type (Filedata * filedata, unsigned e_type)
18791{
18792 static char buff[64];
18793
18794 if (filedata->file_header.e_type == ET_CORE)
18795 switch (e_type)
18796 {
18797 case NT_AUXV:
18798 return _("NT_AUXV (auxiliary vector)");
18799 case NT_PRSTATUS:
18800 return _("NT_PRSTATUS (prstatus structure)");
18801 case NT_FPREGSET:
18802 return _("NT_FPREGSET (floating point registers)");
18803 case NT_PRPSINFO:
18804 return _("NT_PRPSINFO (prpsinfo structure)");
18805 case NT_TASKSTRUCT:
18806 return _("NT_TASKSTRUCT (task structure)");
18807 case NT_GDB_TDESC:
18808 return _("NT_GDB_TDESC (GDB XML target description)");
18809 case NT_PRXFPREG:
18810 return _("NT_PRXFPREG (user_xfpregs structure)");
18811 case NT_PPC_VMX:
18812 return _("NT_PPC_VMX (ppc Altivec registers)");
18813 case NT_PPC_VSX:
18814 return _("NT_PPC_VSX (ppc VSX registers)");
18815 case NT_PPC_TAR:
18816 return _("NT_PPC_TAR (ppc TAR register)");
18817 case NT_PPC_PPR:
18818 return _("NT_PPC_PPR (ppc PPR register)");
18819 case NT_PPC_DSCR:
18820 return _("NT_PPC_DSCR (ppc DSCR register)");
18821 case NT_PPC_EBB:
18822 return _("NT_PPC_EBB (ppc EBB registers)");
18823 case NT_PPC_PMU:
18824 return _("NT_PPC_PMU (ppc PMU registers)");
18825 case NT_PPC_TM_CGPR:
18826 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
18827 case NT_PPC_TM_CFPR:
18828 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
18829 case NT_PPC_TM_CVMX:
18830 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
18831 case NT_PPC_TM_CVSX:
18832 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
18833 case NT_PPC_TM_SPR:
18834 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
18835 case NT_PPC_TM_CTAR:
18836 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
18837 case NT_PPC_TM_CPPR:
18838 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
18839 case NT_PPC_TM_CDSCR:
18840 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
18841 case NT_386_TLS:
18842 return _("NT_386_TLS (x86 TLS information)");
18843 case NT_386_IOPERM:
18844 return _("NT_386_IOPERM (x86 I/O permissions)");
18845 case NT_X86_XSTATE:
18846 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
18847 case NT_X86_CET:
18848 return _("NT_X86_CET (x86 CET state)");
18849 case NT_S390_HIGH_GPRS:
18850 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
18851 case NT_S390_TIMER:
18852 return _("NT_S390_TIMER (s390 timer register)");
18853 case NT_S390_TODCMP:
18854 return _("NT_S390_TODCMP (s390 TOD comparator register)");
18855 case NT_S390_TODPREG:
18856 return _("NT_S390_TODPREG (s390 TOD programmable register)");
18857 case NT_S390_CTRS:
18858 return _("NT_S390_CTRS (s390 control registers)");
18859 case NT_S390_PREFIX:
18860 return _("NT_S390_PREFIX (s390 prefix register)");
18861 case NT_S390_LAST_BREAK:
18862 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
18863 case NT_S390_SYSTEM_CALL:
18864 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
18865 case NT_S390_TDB:
18866 return _("NT_S390_TDB (s390 transaction diagnostic block)");
18867 case NT_S390_VXRS_LOW:
18868 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
18869 case NT_S390_VXRS_HIGH:
18870 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
18871 case NT_S390_GS_CB:
18872 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
18873 case NT_S390_GS_BC:
18874 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
18875 case NT_ARM_VFP:
18876 return _("NT_ARM_VFP (arm VFP registers)");
18877 case NT_ARM_TLS:
18878 return _("NT_ARM_TLS (AArch TLS registers)");
18879 case NT_ARM_HW_BREAK:
18880 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
18881 case NT_ARM_HW_WATCH:
18882 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
18883 case NT_ARM_SVE:
18884 return _("NT_ARM_SVE (AArch SVE registers)");
18885 case NT_ARM_PAC_MASK:
18886 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
18887 case NT_ARM_PACA_KEYS:
18888 return _("NT_ARM_PACA_KEYS (ARM pointer authentication address keys)");
18889 case NT_ARM_PACG_KEYS:
18890 return _("NT_ARM_PACG_KEYS (ARM pointer authentication generic keys)");
18891 case NT_ARM_TAGGED_ADDR_CTRL:
18892 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
18893 case NT_ARM_PAC_ENABLED_KEYS:
18894 return _("NT_ARM_PAC_ENABLED_KEYS (AArch64 pointer authentication enabled keys)");
18895 case NT_ARC_V2:
18896 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
18897 case NT_RISCV_CSR:
18898 return _("NT_RISCV_CSR (RISC-V control and status registers)");
18899 case NT_PSTATUS:
18900 return _("NT_PSTATUS (pstatus structure)");
18901 case NT_FPREGS:
18902 return _("NT_FPREGS (floating point registers)");
18903 case NT_PSINFO:
18904 return _("NT_PSINFO (psinfo structure)");
18905 case NT_LWPSTATUS:
18906 return _("NT_LWPSTATUS (lwpstatus_t structure)");
18907 case NT_LWPSINFO:
18908 return _("NT_LWPSINFO (lwpsinfo_t structure)");
18909 case NT_WIN32PSTATUS:
18910 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
18911 case NT_SIGINFO:
18912 return _("NT_SIGINFO (siginfo_t data)");
18913 case NT_FILE:
18914 return _("NT_FILE (mapped files)");
18915 default:
18916 break;
18917 }
18918 else
18919 switch (e_type)
18920 {
18921 case NT_VERSION:
18922 return _("NT_VERSION (version)");
18923 case NT_ARCH:
18924 return _("NT_ARCH (architecture)");
18925 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
18926 return _("OPEN");
18927 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
18928 return _("func");
18929 case NT_GO_BUILDID:
18930 return _("GO BUILDID");
18931 default:
18932 break;
18933 }
18934
18935 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
18936 return buff;
18937}
18938
18939static bool
18940print_core_note (Elf_Internal_Note *pnote)
18941{
18942 unsigned int addr_size = is_32bit_elf ? 4 : 8;
18943 bfd_vma count, page_size;
18944 unsigned char *descdata, *filenames, *descend;
18945
18946 if (pnote->type != NT_FILE)
18947 {
18948 if (do_wide)
18949 printf ("\n");
18950 return true;
18951 }
18952
18953#ifndef BFD64
18954 if (!is_32bit_elf)
18955 {
18956 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
18957 /* Still "successful". */
18958 return true;
18959 }
18960#endif
18961
18962 if (pnote->descsz < 2 * addr_size)
18963 {
18964 error (_(" Malformed note - too short for header\n"));
18965 return false;
18966 }
18967
18968 descdata = (unsigned char *) pnote->descdata;
18969 descend = descdata + pnote->descsz;
18970
18971 if (descdata[pnote->descsz - 1] != '\0')
18972 {
18973 error (_(" Malformed note - does not end with \\0\n"));
18974 return false;
18975 }
18976
18977 count = byte_get (descdata, addr_size);
18978 descdata += addr_size;
18979
18980 page_size = byte_get (descdata, addr_size);
18981 descdata += addr_size;
18982
18983 if (count > ((bfd_vma) -1 - 2 * addr_size) / (3 * addr_size)
18984 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
18985 {
18986 error (_(" Malformed note - too short for supplied file count\n"));
18987 return false;
18988 }
18989
18990 printf (_(" Page size: "));
18991 print_vma (page_size, DEC);
18992 printf ("\n");
18993
18994 printf (_(" %*s%*s%*s\n"),
18995 (int) (2 + 2 * addr_size), _("Start"),
18996 (int) (4 + 2 * addr_size), _("End"),
18997 (int) (4 + 2 * addr_size), _("Page Offset"));
18998 filenames = descdata + count * 3 * addr_size;
18999 while (count-- > 0)
19000 {
19001 bfd_vma start, end, file_ofs;
19002
19003 if (filenames == descend)
19004 {
19005 error (_(" Malformed note - filenames end too early\n"));
19006 return false;
19007 }
19008
19009 start = byte_get (descdata, addr_size);
19010 descdata += addr_size;
19011 end = byte_get (descdata, addr_size);
19012 descdata += addr_size;
19013 file_ofs = byte_get (descdata, addr_size);
19014 descdata += addr_size;
19015
19016 printf (" ");
19017 print_vma (start, FULL_HEX);
19018 printf (" ");
19019 print_vma (end, FULL_HEX);
19020 printf (" ");
19021 print_vma (file_ofs, FULL_HEX);
19022 printf ("\n %s\n", filenames);
19023
19024 filenames += 1 + strlen ((char *) filenames);
19025 }
19026
19027 return true;
19028}
19029
19030static const char *
19031get_gnu_elf_note_type (unsigned e_type)
19032{
19033 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
19034 switch (e_type)
19035 {
19036 case NT_GNU_ABI_TAG:
19037 return _("NT_GNU_ABI_TAG (ABI version tag)");
19038 case NT_GNU_HWCAP:
19039 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
19040 case NT_GNU_BUILD_ID:
19041 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
19042 case NT_GNU_GOLD_VERSION:
19043 return _("NT_GNU_GOLD_VERSION (gold version)");
19044 case NT_GNU_PROPERTY_TYPE_0:
19045 return _("NT_GNU_PROPERTY_TYPE_0");
19046 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
19047 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
19048 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
19049 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
19050 default:
19051 {
19052 static char buff[64];
19053
19054 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19055 return buff;
19056 }
19057 }
19058}
19059
19060static void
19061decode_x86_compat_isa (unsigned int bitmask)
19062{
19063 while (bitmask)
19064 {
19065 unsigned int bit = bitmask & (- bitmask);
19066
19067 bitmask &= ~ bit;
19068 switch (bit)
19069 {
19070 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
19071 printf ("i486");
19072 break;
19073 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
19074 printf ("586");
19075 break;
19076 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
19077 printf ("686");
19078 break;
19079 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
19080 printf ("SSE");
19081 break;
19082 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
19083 printf ("SSE2");
19084 break;
19085 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
19086 printf ("SSE3");
19087 break;
19088 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
19089 printf ("SSSE3");
19090 break;
19091 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
19092 printf ("SSE4_1");
19093 break;
19094 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
19095 printf ("SSE4_2");
19096 break;
19097 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
19098 printf ("AVX");
19099 break;
19100 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
19101 printf ("AVX2");
19102 break;
19103 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
19104 printf ("AVX512F");
19105 break;
19106 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
19107 printf ("AVX512CD");
19108 break;
19109 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
19110 printf ("AVX512ER");
19111 break;
19112 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
19113 printf ("AVX512PF");
19114 break;
19115 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
19116 printf ("AVX512VL");
19117 break;
19118 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
19119 printf ("AVX512DQ");
19120 break;
19121 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
19122 printf ("AVX512BW");
19123 break;
19124 default:
19125 printf (_("<unknown: %x>"), bit);
19126 break;
19127 }
19128 if (bitmask)
19129 printf (", ");
19130 }
19131}
19132
19133static void
19134decode_x86_compat_2_isa (unsigned int bitmask)
19135{
19136 if (!bitmask)
19137 {
19138 printf (_("<None>"));
19139 return;
19140 }
19141
19142 while (bitmask)
19143 {
19144 unsigned int bit = bitmask & (- bitmask);
19145
19146 bitmask &= ~ bit;
19147 switch (bit)
19148 {
19149 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
19150 printf ("CMOV");
19151 break;
19152 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
19153 printf ("SSE");
19154 break;
19155 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
19156 printf ("SSE2");
19157 break;
19158 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
19159 printf ("SSE3");
19160 break;
19161 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
19162 printf ("SSSE3");
19163 break;
19164 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
19165 printf ("SSE4_1");
19166 break;
19167 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
19168 printf ("SSE4_2");
19169 break;
19170 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
19171 printf ("AVX");
19172 break;
19173 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
19174 printf ("AVX2");
19175 break;
19176 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
19177 printf ("FMA");
19178 break;
19179 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
19180 printf ("AVX512F");
19181 break;
19182 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
19183 printf ("AVX512CD");
19184 break;
19185 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
19186 printf ("AVX512ER");
19187 break;
19188 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
19189 printf ("AVX512PF");
19190 break;
19191 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
19192 printf ("AVX512VL");
19193 break;
19194 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
19195 printf ("AVX512DQ");
19196 break;
19197 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
19198 printf ("AVX512BW");
19199 break;
19200 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
19201 printf ("AVX512_4FMAPS");
19202 break;
19203 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
19204 printf ("AVX512_4VNNIW");
19205 break;
19206 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
19207 printf ("AVX512_BITALG");
19208 break;
19209 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
19210 printf ("AVX512_IFMA");
19211 break;
19212 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
19213 printf ("AVX512_VBMI");
19214 break;
19215 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
19216 printf ("AVX512_VBMI2");
19217 break;
19218 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
19219 printf ("AVX512_VNNI");
19220 break;
19221 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
19222 printf ("AVX512_BF16");
19223 break;
19224 default:
19225 printf (_("<unknown: %x>"), bit);
19226 break;
19227 }
19228 if (bitmask)
19229 printf (", ");
19230 }
19231}
19232
19233static void
19234decode_x86_isa (unsigned int bitmask)
19235{
19236 while (bitmask)
19237 {
19238 unsigned int bit = bitmask & (- bitmask);
19239
19240 bitmask &= ~ bit;
19241 switch (bit)
19242 {
19243 case GNU_PROPERTY_X86_ISA_1_BASELINE:
19244 printf ("x86-64-baseline");
19245 break;
19246 case GNU_PROPERTY_X86_ISA_1_V2:
19247 printf ("x86-64-v2");
19248 break;
19249 case GNU_PROPERTY_X86_ISA_1_V3:
19250 printf ("x86-64-v3");
19251 break;
19252 case GNU_PROPERTY_X86_ISA_1_V4:
19253 printf ("x86-64-v4");
19254 break;
19255 default:
19256 printf (_("<unknown: %x>"), bit);
19257 break;
19258 }
19259 if (bitmask)
19260 printf (", ");
19261 }
19262}
19263
19264static void
19265decode_x86_feature_1 (unsigned int bitmask)
19266{
19267 if (!bitmask)
19268 {
19269 printf (_("<None>"));
19270 return;
19271 }
19272
19273 while (bitmask)
19274 {
19275 unsigned int bit = bitmask & (- bitmask);
19276
19277 bitmask &= ~ bit;
19278 switch (bit)
19279 {
19280 case GNU_PROPERTY_X86_FEATURE_1_IBT:
19281 printf ("IBT");
19282 break;
19283 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
19284 printf ("SHSTK");
19285 break;
19286 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
19287 printf ("LAM_U48");
19288 break;
19289 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
19290 printf ("LAM_U57");
19291 break;
19292 default:
19293 printf (_("<unknown: %x>"), bit);
19294 break;
19295 }
19296 if (bitmask)
19297 printf (", ");
19298 }
19299}
19300
19301static void
19302decode_x86_feature_2 (unsigned int bitmask)
19303{
19304 if (!bitmask)
19305 {
19306 printf (_("<None>"));
19307 return;
19308 }
19309
19310 while (bitmask)
19311 {
19312 unsigned int bit = bitmask & (- bitmask);
19313
19314 bitmask &= ~ bit;
19315 switch (bit)
19316 {
19317 case GNU_PROPERTY_X86_FEATURE_2_X86:
19318 printf ("x86");
19319 break;
19320 case GNU_PROPERTY_X86_FEATURE_2_X87:
19321 printf ("x87");
19322 break;
19323 case GNU_PROPERTY_X86_FEATURE_2_MMX:
19324 printf ("MMX");
19325 break;
19326 case GNU_PROPERTY_X86_FEATURE_2_XMM:
19327 printf ("XMM");
19328 break;
19329 case GNU_PROPERTY_X86_FEATURE_2_YMM:
19330 printf ("YMM");
19331 break;
19332 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
19333 printf ("ZMM");
19334 break;
19335 case GNU_PROPERTY_X86_FEATURE_2_TMM:
19336 printf ("TMM");
19337 break;
19338 case GNU_PROPERTY_X86_FEATURE_2_MASK:
19339 printf ("MASK");
19340 break;
19341 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
19342 printf ("FXSR");
19343 break;
19344 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
19345 printf ("XSAVE");
19346 break;
19347 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
19348 printf ("XSAVEOPT");
19349 break;
19350 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
19351 printf ("XSAVEC");
19352 break;
19353 default:
19354 printf (_("<unknown: %x>"), bit);
19355 break;
19356 }
19357 if (bitmask)
19358 printf (", ");
19359 }
19360}
19361
19362static void
19363decode_aarch64_feature_1_and (unsigned int bitmask)
19364{
19365 while (bitmask)
19366 {
19367 unsigned int bit = bitmask & (- bitmask);
19368
19369 bitmask &= ~ bit;
19370 switch (bit)
19371 {
19372 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
19373 printf ("BTI");
19374 break;
19375
19376 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
19377 printf ("PAC");
19378 break;
19379
19380 default:
19381 printf (_("<unknown: %x>"), bit);
19382 break;
19383 }
19384 if (bitmask)
19385 printf (", ");
19386 }
19387}
19388
19389static void
19390decode_1_needed (unsigned int bitmask)
19391{
19392 while (bitmask)
19393 {
19394 unsigned int bit = bitmask & (- bitmask);
19395
19396 bitmask &= ~ bit;
19397 switch (bit)
19398 {
19399 case GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
19400 printf ("indirect external access");
19401 break;
19402 default:
19403 printf (_("<unknown: %x>"), bit);
19404 break;
19405 }
19406 if (bitmask)
19407 printf (", ");
19408 }
19409}
19410
19411static void
19412print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
19413{
19414 unsigned char * ptr = (unsigned char *) pnote->descdata;
19415 unsigned char * ptr_end = ptr + pnote->descsz;
19416 unsigned int size = is_32bit_elf ? 4 : 8;
19417
19418 printf (_(" Properties: "));
19419
19420 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
19421 {
19422 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
19423 return;
19424 }
19425
19426 while (ptr < ptr_end)
19427 {
19428 unsigned int j;
19429 unsigned int type;
19430 unsigned int datasz;
19431
19432 if ((size_t) (ptr_end - ptr) < 8)
19433 {
19434 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
19435 break;
19436 }
19437
19438 type = byte_get (ptr, 4);
19439 datasz = byte_get (ptr + 4, 4);
19440
19441 ptr += 8;
19442
19443 if (datasz > (size_t) (ptr_end - ptr))
19444 {
19445 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
19446 type, datasz);
19447 break;
19448 }
19449
19450 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
19451 {
19452 if (filedata->file_header.e_machine == EM_X86_64
19453 || filedata->file_header.e_machine == EM_IAMCU
19454 || filedata->file_header.e_machine == EM_386)
19455 {
19456 unsigned int bitmask;
19457
19458 if (datasz == 4)
19459 bitmask = byte_get (ptr, 4);
19460 else
19461 bitmask = 0;
19462
19463 switch (type)
19464 {
19465 case GNU_PROPERTY_X86_ISA_1_USED:
19466 if (datasz != 4)
19467 printf (_("x86 ISA used: <corrupt length: %#x> "),
19468 datasz);
19469 else
19470 {
19471 printf ("x86 ISA used: ");
19472 decode_x86_isa (bitmask);
19473 }
19474 goto next;
19475
19476 case GNU_PROPERTY_X86_ISA_1_NEEDED:
19477 if (datasz != 4)
19478 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19479 datasz);
19480 else
19481 {
19482 printf ("x86 ISA needed: ");
19483 decode_x86_isa (bitmask);
19484 }
19485 goto next;
19486
19487 case GNU_PROPERTY_X86_FEATURE_1_AND:
19488 if (datasz != 4)
19489 printf (_("x86 feature: <corrupt length: %#x> "),
19490 datasz);
19491 else
19492 {
19493 printf ("x86 feature: ");
19494 decode_x86_feature_1 (bitmask);
19495 }
19496 goto next;
19497
19498 case GNU_PROPERTY_X86_FEATURE_2_USED:
19499 if (datasz != 4)
19500 printf (_("x86 feature used: <corrupt length: %#x> "),
19501 datasz);
19502 else
19503 {
19504 printf ("x86 feature used: ");
19505 decode_x86_feature_2 (bitmask);
19506 }
19507 goto next;
19508
19509 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
19510 if (datasz != 4)
19511 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
19512 else
19513 {
19514 printf ("x86 feature needed: ");
19515 decode_x86_feature_2 (bitmask);
19516 }
19517 goto next;
19518
19519 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
19520 if (datasz != 4)
19521 printf (_("x86 ISA used: <corrupt length: %#x> "),
19522 datasz);
19523 else
19524 {
19525 printf ("x86 ISA used: ");
19526 decode_x86_compat_isa (bitmask);
19527 }
19528 goto next;
19529
19530 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
19531 if (datasz != 4)
19532 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19533 datasz);
19534 else
19535 {
19536 printf ("x86 ISA needed: ");
19537 decode_x86_compat_isa (bitmask);
19538 }
19539 goto next;
19540
19541 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
19542 if (datasz != 4)
19543 printf (_("x86 ISA used: <corrupt length: %#x> "),
19544 datasz);
19545 else
19546 {
19547 printf ("x86 ISA used: ");
19548 decode_x86_compat_2_isa (bitmask);
19549 }
19550 goto next;
19551
19552 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
19553 if (datasz != 4)
19554 printf (_("x86 ISA needed: <corrupt length: %#x> "),
19555 datasz);
19556 else
19557 {
19558 printf ("x86 ISA needed: ");
19559 decode_x86_compat_2_isa (bitmask);
19560 }
19561 goto next;
19562
19563 default:
19564 break;
19565 }
19566 }
19567 else if (filedata->file_header.e_machine == EM_AARCH64)
19568 {
19569 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
19570 {
19571 printf ("AArch64 feature: ");
19572 if (datasz != 4)
19573 printf (_("<corrupt length: %#x> "), datasz);
19574 else
19575 decode_aarch64_feature_1_and (byte_get (ptr, 4));
19576 goto next;
19577 }
19578 }
19579 }
19580 else
19581 {
19582 switch (type)
19583 {
19584 case GNU_PROPERTY_STACK_SIZE:
19585 printf (_("stack size: "));
19586 if (datasz != size)
19587 printf (_("<corrupt length: %#x> "), datasz);
19588 else
19589 printf ("%#lx", (unsigned long) byte_get (ptr, size));
19590 goto next;
19591
19592 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
19593 printf ("no copy on protected ");
19594 if (datasz)
19595 printf (_("<corrupt length: %#x> "), datasz);
19596 goto next;
19597
19598 default:
19599 if ((type >= GNU_PROPERTY_UINT32_AND_LO
19600 && type <= GNU_PROPERTY_UINT32_AND_HI)
19601 || (type >= GNU_PROPERTY_UINT32_OR_LO
19602 && type <= GNU_PROPERTY_UINT32_OR_HI))
19603 {
19604 switch (type)
19605 {
19606 case GNU_PROPERTY_1_NEEDED:
19607 if (datasz != 4)
19608 printf (_("1_needed: <corrupt length: %#x> "),
19609 datasz);
19610 else
19611 {
19612 unsigned int bitmask = byte_get (ptr, 4);
19613 printf ("1_needed: ");
19614 decode_1_needed (bitmask);
19615 }
19616 goto next;
19617
19618 default:
19619 break;
19620 }
19621 if (type <= GNU_PROPERTY_UINT32_AND_HI)
19622 printf (_("UINT32_AND (%#x): "), type);
19623 else
19624 printf (_("UINT32_OR (%#x): "), type);
19625 if (datasz != 4)
19626 printf (_("<corrupt length: %#x> "), datasz);
19627 else
19628 printf ("%#x", (unsigned int) byte_get (ptr, 4));
19629 goto next;
19630 }
19631 break;
19632 }
19633 }
19634
19635 if (type < GNU_PROPERTY_LOPROC)
19636 printf (_("<unknown type %#x data: "), type);
19637 else if (type < GNU_PROPERTY_LOUSER)
19638 printf (_("<processor-specific type %#x data: "), type);
19639 else
19640 printf (_("<application-specific type %#x data: "), type);
19641 for (j = 0; j < datasz; ++j)
19642 printf ("%02x ", ptr[j] & 0xff);
19643 printf (">");
19644
19645 next:
19646 ptr += ((datasz + (size - 1)) & ~ (size - 1));
19647 if (ptr == ptr_end)
19648 break;
19649
19650 if (do_wide)
19651 printf (", ");
19652 else
19653 printf ("\n\t");
19654 }
19655
19656 printf ("\n");
19657}
19658
19659static bool
19660print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
19661{
19662 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
19663 switch (pnote->type)
19664 {
19665 case NT_GNU_BUILD_ID:
19666 {
19667 unsigned long i;
19668
19669 printf (_(" Build ID: "));
19670 for (i = 0; i < pnote->descsz; ++i)
19671 printf ("%02x", pnote->descdata[i] & 0xff);
19672 printf ("\n");
19673 }
19674 break;
19675
19676 case NT_GNU_ABI_TAG:
19677 {
19678 unsigned long os, major, minor, subminor;
19679 const char *osname;
19680
19681 /* PR 17531: file: 030-599401-0.004. */
19682 if (pnote->descsz < 16)
19683 {
19684 printf (_(" <corrupt GNU_ABI_TAG>\n"));
19685 break;
19686 }
19687
19688 os = byte_get ((unsigned char *) pnote->descdata, 4);
19689 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
19690 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
19691 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
19692
19693 switch (os)
19694 {
19695 case GNU_ABI_TAG_LINUX:
19696 osname = "Linux";
19697 break;
19698 case GNU_ABI_TAG_HURD:
19699 osname = "Hurd";
19700 break;
19701 case GNU_ABI_TAG_SOLARIS:
19702 osname = "Solaris";
19703 break;
19704 case GNU_ABI_TAG_FREEBSD:
19705 osname = "FreeBSD";
19706 break;
19707 case GNU_ABI_TAG_NETBSD:
19708 osname = "NetBSD";
19709 break;
19710 case GNU_ABI_TAG_SYLLABLE:
19711 osname = "Syllable";
19712 break;
19713 case GNU_ABI_TAG_NACL:
19714 osname = "NaCl";
19715 break;
19716 default:
19717 osname = "Unknown";
19718 break;
19719 }
19720
19721 printf (_(" OS: %s, ABI: %ld.%ld.%ld\n"), osname,
19722 major, minor, subminor);
19723 }
19724 break;
19725
19726 case NT_GNU_GOLD_VERSION:
19727 {
19728 unsigned long i;
19729
19730 printf (_(" Version: "));
19731 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
19732 printf ("%c", pnote->descdata[i]);
19733 printf ("\n");
19734 }
19735 break;
19736
19737 case NT_GNU_HWCAP:
19738 {
19739 unsigned long num_entries, mask;
19740
19741 /* Hardware capabilities information. Word 0 is the number of entries.
19742 Word 1 is a bitmask of enabled entries. The rest of the descriptor
19743 is a series of entries, where each entry is a single byte followed
19744 by a nul terminated string. The byte gives the bit number to test
19745 if enabled in the bitmask. */
19746 printf (_(" Hardware Capabilities: "));
19747 if (pnote->descsz < 8)
19748 {
19749 error (_("<corrupt GNU_HWCAP>\n"));
19750 return false;
19751 }
19752 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
19753 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
19754 printf (_("num entries: %ld, enabled mask: %lx\n"), num_entries, mask);
19755 /* FIXME: Add code to display the entries... */
19756 }
19757 break;
19758
19759 case NT_GNU_PROPERTY_TYPE_0:
19760 print_gnu_property_note (filedata, pnote);
19761 break;
19762
19763 default:
19764 /* Handle unrecognised types. An error message should have already been
19765 created by get_gnu_elf_note_type(), so all that we need to do is to
19766 display the data. */
19767 {
19768 unsigned long i;
19769
19770 printf (_(" Description data: "));
19771 for (i = 0; i < pnote->descsz; ++i)
19772 printf ("%02x ", pnote->descdata[i] & 0xff);
19773 printf ("\n");
19774 }
19775 break;
19776 }
19777
19778 return true;
19779}
19780
19781static const char *
19782get_v850_elf_note_type (enum v850_notes n_type)
19783{
19784 static char buff[64];
19785
19786 switch (n_type)
19787 {
19788 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
19789 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
19790 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
19791 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
19792 case V850_NOTE_CACHE_INFO: return _("Use of cache");
19793 case V850_NOTE_MMU_INFO: return _("Use of MMU");
19794 default:
19795 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
19796 return buff;
19797 }
19798}
19799
19800static bool
19801print_v850_note (Elf_Internal_Note * pnote)
19802{
19803 unsigned int val;
19804
19805 if (pnote->descsz != 4)
19806 return false;
19807
19808 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
19809
19810 if (val == 0)
19811 {
19812 printf (_("not set\n"));
19813 return true;
19814 }
19815
19816 switch (pnote->type)
19817 {
19818 case V850_NOTE_ALIGNMENT:
19819 switch (val)
19820 {
19821 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
19822 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
19823 }
19824 break;
19825
19826 case V850_NOTE_DATA_SIZE:
19827 switch (val)
19828 {
19829 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
19830 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
19831 }
19832 break;
19833
19834 case V850_NOTE_FPU_INFO:
19835 switch (val)
19836 {
19837 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
19838 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
19839 }
19840 break;
19841
19842 case V850_NOTE_MMU_INFO:
19843 case V850_NOTE_CACHE_INFO:
19844 case V850_NOTE_SIMD_INFO:
19845 if (val == EF_RH850_SIMD)
19846 {
19847 printf (_("yes\n"));
19848 return true;
19849 }
19850 break;
19851
19852 default:
19853 /* An 'unknown note type' message will already have been displayed. */
19854 break;
19855 }
19856
19857 printf (_("unknown value: %x\n"), val);
19858 return false;
19859}
19860
19861static bool
19862process_netbsd_elf_note (Elf_Internal_Note * pnote)
19863{
19864 unsigned int version;
19865
19866 switch (pnote->type)
19867 {
19868 case NT_NETBSD_IDENT:
19869 if (pnote->descsz < 1)
19870 break;
19871 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
19872 if ((version / 10000) % 100)
19873 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
19874 version, version / 100000000, (version / 1000000) % 100,
19875 (version / 10000) % 100 > 26 ? "Z" : "",
19876 'A' + (version / 10000) % 26);
19877 else
19878 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
19879 version, version / 100000000, (version / 1000000) % 100,
19880 (version / 100) % 100);
19881 return true;
19882
19883 case NT_NETBSD_MARCH:
19884 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
19885 pnote->descdata);
19886 return true;
19887
19888 case NT_NETBSD_PAX:
19889 if (pnote->descsz < 1)
19890 break;
19891 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
19892 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
19893 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
19894 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
19895 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
19896 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
19897 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
19898 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
19899 return true;
19900 }
19901
19902 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
19903 pnote->descsz, pnote->type);
19904 return false;
19905}
19906
19907static const char *
19908get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
19909{
19910 switch (e_type)
19911 {
19912 case NT_FREEBSD_THRMISC:
19913 return _("NT_THRMISC (thrmisc structure)");
19914 case NT_FREEBSD_PROCSTAT_PROC:
19915 return _("NT_PROCSTAT_PROC (proc data)");
19916 case NT_FREEBSD_PROCSTAT_FILES:
19917 return _("NT_PROCSTAT_FILES (files data)");
19918 case NT_FREEBSD_PROCSTAT_VMMAP:
19919 return _("NT_PROCSTAT_VMMAP (vmmap data)");
19920 case NT_FREEBSD_PROCSTAT_GROUPS:
19921 return _("NT_PROCSTAT_GROUPS (groups data)");
19922 case NT_FREEBSD_PROCSTAT_UMASK:
19923 return _("NT_PROCSTAT_UMASK (umask data)");
19924 case NT_FREEBSD_PROCSTAT_RLIMIT:
19925 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
19926 case NT_FREEBSD_PROCSTAT_OSREL:
19927 return _("NT_PROCSTAT_OSREL (osreldate data)");
19928 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
19929 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
19930 case NT_FREEBSD_PROCSTAT_AUXV:
19931 return _("NT_PROCSTAT_AUXV (auxv data)");
19932 case NT_FREEBSD_PTLWPINFO:
19933 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
19934 }
19935 return get_note_type (filedata, e_type);
19936}
19937
19938static const char *
19939get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
19940{
19941 static char buff[64];
19942
19943 switch (e_type)
19944 {
19945 case NT_NETBSDCORE_PROCINFO:
19946 /* NetBSD core "procinfo" structure. */
19947 return _("NetBSD procinfo structure");
19948
19949 case NT_NETBSDCORE_AUXV:
19950 return _("NetBSD ELF auxiliary vector data");
19951
19952 case NT_NETBSDCORE_LWPSTATUS:
19953 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
19954
19955 default:
19956 /* As of Jan 2020 there are no other machine-independent notes
19957 defined for NetBSD core files. If the note type is less
19958 than the start of the machine-dependent note types, we don't
19959 understand it. */
19960
19961 if (e_type < NT_NETBSDCORE_FIRSTMACH)
19962 {
19963 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
19964 return buff;
19965 }
19966 break;
19967 }
19968
19969 switch (filedata->file_header.e_machine)
19970 {
19971 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
19972 and PT_GETFPREGS == mach+2. */
19973
19974 case EM_OLD_ALPHA:
19975 case EM_ALPHA:
19976 case EM_SPARC:
19977 case EM_SPARC32PLUS:
19978 case EM_SPARCV9:
19979 switch (e_type)
19980 {
19981 case NT_NETBSDCORE_FIRSTMACH + 0:
19982 return _("PT_GETREGS (reg structure)");
19983 case NT_NETBSDCORE_FIRSTMACH + 2:
19984 return _("PT_GETFPREGS (fpreg structure)");
19985 default:
19986 break;
19987 }
19988 break;
19989
19990 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
19991 There's also old PT___GETREGS40 == mach + 1 for old reg
19992 structure which lacks GBR. */
19993 case EM_SH:
19994 switch (e_type)
19995 {
19996 case NT_NETBSDCORE_FIRSTMACH + 1:
19997 return _("PT___GETREGS40 (old reg structure)");
19998 case NT_NETBSDCORE_FIRSTMACH + 3:
19999 return _("PT_GETREGS (reg structure)");
20000 case NT_NETBSDCORE_FIRSTMACH + 5:
20001 return _("PT_GETFPREGS (fpreg structure)");
20002 default:
20003 break;
20004 }
20005 break;
20006
20007 /* On all other arch's, PT_GETREGS == mach+1 and
20008 PT_GETFPREGS == mach+3. */
20009 default:
20010 switch (e_type)
20011 {
20012 case NT_NETBSDCORE_FIRSTMACH + 1:
20013 return _("PT_GETREGS (reg structure)");
20014 case NT_NETBSDCORE_FIRSTMACH + 3:
20015 return _("PT_GETFPREGS (fpreg structure)");
20016 default:
20017 break;
20018 }
20019 }
20020
20021 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
20022 e_type - NT_NETBSDCORE_FIRSTMACH);
20023 return buff;
20024}
20025
20026static const char *
20027get_openbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
20028{
20029 switch (e_type)
20030 {
20031 case NT_OPENBSD_PROCINFO:
20032 return _("OpenBSD procinfo structure");
20033 case NT_OPENBSD_AUXV:
20034 return _("OpenBSD ELF auxiliary vector data");
20035 case NT_OPENBSD_REGS:
20036 return _("OpenBSD regular registers");
20037 case NT_OPENBSD_FPREGS:
20038 return _("OpenBSD floating point registers");
20039 case NT_OPENBSD_WCOOKIE:
20040 return _("OpenBSD window cookie");
20041 }
20042
20043 return get_note_type (filedata, e_type);
20044}
20045
20046static const char *
20047get_stapsdt_note_type (unsigned e_type)
20048{
20049 static char buff[64];
20050
20051 switch (e_type)
20052 {
20053 case NT_STAPSDT:
20054 return _("NT_STAPSDT (SystemTap probe descriptors)");
20055
20056 default:
20057 break;
20058 }
20059
20060 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20061 return buff;
20062}
20063
20064static bool
20065print_stapsdt_note (Elf_Internal_Note *pnote)
20066{
20067 size_t len, maxlen;
20068 unsigned long addr_size = is_32bit_elf ? 4 : 8;
20069 char *data = pnote->descdata;
20070 char *data_end = pnote->descdata + pnote->descsz;
20071 bfd_vma pc, base_addr, semaphore;
20072 char *provider, *probe, *arg_fmt;
20073
20074 if (pnote->descsz < (addr_size * 3))
20075 goto stapdt_note_too_small;
20076
20077 pc = byte_get ((unsigned char *) data, addr_size);
20078 data += addr_size;
20079
20080 base_addr = byte_get ((unsigned char *) data, addr_size);
20081 data += addr_size;
20082
20083 semaphore = byte_get ((unsigned char *) data, addr_size);
20084 data += addr_size;
20085
20086 if (data >= data_end)
20087 goto stapdt_note_too_small;
20088 maxlen = data_end - data;
20089 len = strnlen (data, maxlen);
20090 if (len < maxlen)
20091 {
20092 provider = data;
20093 data += len + 1;
20094 }
20095 else
20096 goto stapdt_note_too_small;
20097
20098 if (data >= data_end)
20099 goto stapdt_note_too_small;
20100 maxlen = data_end - data;
20101 len = strnlen (data, maxlen);
20102 if (len < maxlen)
20103 {
20104 probe = data;
20105 data += len + 1;
20106 }
20107 else
20108 goto stapdt_note_too_small;
20109
20110 if (data >= data_end)
20111 goto stapdt_note_too_small;
20112 maxlen = data_end - data;
20113 len = strnlen (data, maxlen);
20114 if (len < maxlen)
20115 {
20116 arg_fmt = data;
20117 data += len + 1;
20118 }
20119 else
20120 goto stapdt_note_too_small;
20121
20122 printf (_(" Provider: %s\n"), provider);
20123 printf (_(" Name: %s\n"), probe);
20124 printf (_(" Location: "));
20125 print_vma (pc, FULL_HEX);
20126 printf (_(", Base: "));
20127 print_vma (base_addr, FULL_HEX);
20128 printf (_(", Semaphore: "));
20129 print_vma (semaphore, FULL_HEX);
20130 printf ("\n");
20131 printf (_(" Arguments: %s\n"), arg_fmt);
20132
20133 return data == data_end;
20134
20135 stapdt_note_too_small:
20136 printf (_(" <corrupt - note is too small>\n"));
20137 error (_("corrupt stapdt note - the data size is too small\n"));
20138 return false;
20139}
20140
20141static const char *
20142get_ia64_vms_note_type (unsigned e_type)
20143{
20144 static char buff[64];
20145
20146 switch (e_type)
20147 {
20148 case NT_VMS_MHD:
20149 return _("NT_VMS_MHD (module header)");
20150 case NT_VMS_LNM:
20151 return _("NT_VMS_LNM (language name)");
20152 case NT_VMS_SRC:
20153 return _("NT_VMS_SRC (source files)");
20154 case NT_VMS_TITLE:
20155 return "NT_VMS_TITLE";
20156 case NT_VMS_EIDC:
20157 return _("NT_VMS_EIDC (consistency check)");
20158 case NT_VMS_FPMODE:
20159 return _("NT_VMS_FPMODE (FP mode)");
20160 case NT_VMS_LINKTIME:
20161 return "NT_VMS_LINKTIME";
20162 case NT_VMS_IMGNAM:
20163 return _("NT_VMS_IMGNAM (image name)");
20164 case NT_VMS_IMGID:
20165 return _("NT_VMS_IMGID (image id)");
20166 case NT_VMS_LINKID:
20167 return _("NT_VMS_LINKID (link id)");
20168 case NT_VMS_IMGBID:
20169 return _("NT_VMS_IMGBID (build id)");
20170 case NT_VMS_GSTNAM:
20171 return _("NT_VMS_GSTNAM (sym table name)");
20172 case NT_VMS_ORIG_DYN:
20173 return "NT_VMS_ORIG_DYN";
20174 case NT_VMS_PATCHTIME:
20175 return "NT_VMS_PATCHTIME";
20176 default:
20177 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20178 return buff;
20179 }
20180}
20181
20182static bool
20183print_ia64_vms_note (Elf_Internal_Note * pnote)
20184{
20185 int maxlen = pnote->descsz;
20186
20187 if (maxlen < 2 || (unsigned long) maxlen != pnote->descsz)
20188 goto desc_size_fail;
20189
20190 switch (pnote->type)
20191 {
20192 case NT_VMS_MHD:
20193 if (maxlen <= 36)
20194 goto desc_size_fail;
20195
20196 int l = (int) strnlen (pnote->descdata + 34, maxlen - 34);
20197
20198 printf (_(" Creation date : %.17s\n"), pnote->descdata);
20199 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
20200 if (l + 34 < maxlen)
20201 {
20202 printf (_(" Module name : %s\n"), pnote->descdata + 34);
20203 if (l + 35 < maxlen)
20204 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
20205 else
20206 printf (_(" Module version : <missing>\n"));
20207 }
20208 else
20209 {
20210 printf (_(" Module name : <missing>\n"));
20211 printf (_(" Module version : <missing>\n"));
20212 }
20213 break;
20214
20215 case NT_VMS_LNM:
20216 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
20217 break;
20218
20219#ifdef BFD64
20220 case NT_VMS_FPMODE:
20221 printf (_(" Floating Point mode: "));
20222 if (maxlen < 8)
20223 goto desc_size_fail;
20224 /* FIXME: Generate an error if descsz > 8 ? */
20225
20226 printf ("0x%016" BFD_VMA_FMT "x\n",
20227 (bfd_vma) byte_get ((unsigned char *)pnote->descdata, 8));
20228 break;
20229
20230 case NT_VMS_LINKTIME:
20231 printf (_(" Link time: "));
20232 if (maxlen < 8)
20233 goto desc_size_fail;
20234 /* FIXME: Generate an error if descsz > 8 ? */
20235
20236 print_vms_time
20237 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
20238 printf ("\n");
20239 break;
20240
20241 case NT_VMS_PATCHTIME:
20242 printf (_(" Patch time: "));
20243 if (maxlen < 8)
20244 goto desc_size_fail;
20245 /* FIXME: Generate an error if descsz > 8 ? */
20246
20247 print_vms_time
20248 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata, 8));
20249 printf ("\n");
20250 break;
20251
20252 case NT_VMS_ORIG_DYN:
20253 if (maxlen < 34)
20254 goto desc_size_fail;
20255
20256 printf (_(" Major id: %u, minor id: %u\n"),
20257 (unsigned) byte_get ((unsigned char *)pnote->descdata, 4),
20258 (unsigned) byte_get ((unsigned char *)pnote->descdata + 4, 4));
20259 printf (_(" Last modified : "));
20260 print_vms_time
20261 ((bfd_int64_t) byte_get ((unsigned char *)pnote->descdata + 8, 8));
20262 printf (_("\n Link flags : "));
20263 printf ("0x%016" BFD_VMA_FMT "x\n",
20264 (bfd_vma) byte_get ((unsigned char *)pnote->descdata + 16, 8));
20265 printf (_(" Header flags: 0x%08x\n"),
20266 (unsigned) byte_get ((unsigned char *)pnote->descdata + 24, 4));
20267 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
20268 break;
20269#endif
20270
20271 case NT_VMS_IMGNAM:
20272 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
20273 break;
20274
20275 case NT_VMS_GSTNAM:
20276 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
20277 break;
20278
20279 case NT_VMS_IMGID:
20280 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
20281 break;
20282
20283 case NT_VMS_LINKID:
20284 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
20285 break;
20286
20287 default:
20288 return false;
20289 }
20290
20291 return true;
20292
20293 desc_size_fail:
20294 printf (_(" <corrupt - data size is too small>\n"));
20295 error (_("corrupt IA64 note: data size is too small\n"));
20296 return false;
20297}
20298
20299struct build_attr_cache {
20300 Filedata *filedata;
20301 char *strtab;
20302 unsigned long strtablen;
20303 Elf_Internal_Sym *symtab;
20304 unsigned long nsyms;
20305} ba_cache;
20306
20307/* Find the symbol associated with a build attribute that is attached
20308 to address OFFSET. If PNAME is non-NULL then store the name of
20309 the symbol (if found) in the provided pointer, Returns NULL if a
20310 symbol could not be found. */
20311
20312static Elf_Internal_Sym *
20313get_symbol_for_build_attribute (Filedata *filedata,
20314 unsigned long offset,
20315 bool is_open_attr,
20316 const char **pname)
20317{
20318 Elf_Internal_Sym *saved_sym = NULL;
20319 Elf_Internal_Sym *sym;
20320
20321 if (filedata->section_headers != NULL
20322 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
20323 {
20324 Elf_Internal_Shdr * symsec;
20325
20326 free (ba_cache.strtab);
20327 ba_cache.strtab = NULL;
20328 free (ba_cache.symtab);
20329 ba_cache.symtab = NULL;
20330
20331 /* Load the symbol and string sections. */
20332 for (symsec = filedata->section_headers;
20333 symsec < filedata->section_headers + filedata->file_header.e_shnum;
20334 symsec ++)
20335 {
20336 if (symsec->sh_type == SHT_SYMTAB
20337 && get_symtab (filedata, symsec,
20338 &ba_cache.symtab, &ba_cache.nsyms,
20339 &ba_cache.strtab, &ba_cache.strtablen))
20340 break;
20341 }
20342 ba_cache.filedata = filedata;
20343 }
20344
20345 if (ba_cache.symtab == NULL)
20346 return NULL;
20347
20348 /* Find a symbol whose value matches offset. */
20349 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
20350 if (sym->st_value == offset)
20351 {
20352 if (sym->st_name >= ba_cache.strtablen)
20353 /* Huh ? This should not happen. */
20354 continue;
20355
20356 if (ba_cache.strtab[sym->st_name] == 0)
20357 continue;
20358
20359 /* The AArch64, ARM and RISC-V architectures define mapping symbols
20360 (eg $d, $x, $t) which we want to ignore. */
20361 if (ba_cache.strtab[sym->st_name] == '$'
20362 && ba_cache.strtab[sym->st_name + 1] != 0
20363 && ba_cache.strtab[sym->st_name + 2] == 0)
20364 continue;
20365
20366 if (is_open_attr)
20367 {
20368 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
20369 and FILE or OBJECT symbols over NOTYPE symbols. We skip
20370 FUNC symbols entirely. */
20371 switch (ELF_ST_TYPE (sym->st_info))
20372 {
20373 case STT_OBJECT:
20374 case STT_FILE:
20375 saved_sym = sym;
20376 if (sym->st_size)
20377 {
20378 /* If the symbol has a size associated
20379 with it then we can stop searching. */
20380 sym = ba_cache.symtab + ba_cache.nsyms;
20381 }
20382 continue;
20383
20384 case STT_FUNC:
20385 /* Ignore function symbols. */
20386 continue;
20387
20388 default:
20389 break;
20390 }
20391
20392 switch (ELF_ST_BIND (sym->st_info))
20393 {
20394 case STB_GLOBAL:
20395 if (saved_sym == NULL
20396 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
20397 saved_sym = sym;
20398 break;
20399
20400 case STB_LOCAL:
20401 if (saved_sym == NULL)
20402 saved_sym = sym;
20403 break;
20404
20405 default:
20406 break;
20407 }
20408 }
20409 else
20410 {
20411 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
20412 continue;
20413
20414 saved_sym = sym;
20415 break;
20416 }
20417 }
20418
20419 if (saved_sym && pname)
20420 * pname = ba_cache.strtab + saved_sym->st_name;
20421
20422 return saved_sym;
20423}
20424
20425/* Returns true iff addr1 and addr2 are in the same section. */
20426
20427static bool
20428same_section (Filedata * filedata, unsigned long addr1, unsigned long addr2)
20429{
20430 Elf_Internal_Shdr * a1;
20431 Elf_Internal_Shdr * a2;
20432
20433 a1 = find_section_by_address (filedata, addr1);
20434 a2 = find_section_by_address (filedata, addr2);
20435
20436 return a1 == a2 && a1 != NULL;
20437}
20438
20439static bool
20440print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
20441 Filedata * filedata)
20442{
20443 static unsigned long global_offset = 0;
20444 static unsigned long global_end = 0;
20445 static unsigned long func_offset = 0;
20446 static unsigned long func_end = 0;
20447
20448 Elf_Internal_Sym *sym;
20449 const char *name;
20450 unsigned long start;
20451 unsigned long end;
20452 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
20453
20454 switch (pnote->descsz)
20455 {
20456 case 0:
20457 /* A zero-length description means that the range of
20458 the previous note of the same type should be used. */
20459 if (is_open_attr)
20460 {
20461 if (global_end > global_offset)
20462 printf (_(" Applies to region from %#lx to %#lx\n"),
20463 global_offset, global_end);
20464 else
20465 printf (_(" Applies to region from %#lx\n"), global_offset);
20466 }
20467 else
20468 {
20469 if (func_end > func_offset)
20470 printf (_(" Applies to region from %#lx to %#lx\n"), func_offset, func_end);
20471 else
20472 printf (_(" Applies to region from %#lx\n"), func_offset);
20473 }
20474 return true;
20475
20476 case 4:
20477 start = byte_get ((unsigned char *) pnote->descdata, 4);
20478 end = 0;
20479 break;
20480
20481 case 8:
20482 start = byte_get ((unsigned char *) pnote->descdata, 4);
20483 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
20484 break;
20485
20486 case 16:
20487 start = byte_get ((unsigned char *) pnote->descdata, 8);
20488 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
20489 break;
20490
20491 default:
20492 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
20493 printf (_(" <invalid descsz>"));
20494 return false;
20495 }
20496
20497 name = NULL;
20498 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
20499 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
20500 in order to avoid them being confused with the start address of the
20501 first function in the file... */
20502 if (sym == NULL && is_open_attr)
20503 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
20504 & name);
20505
20506 if (end == 0 && sym != NULL && sym->st_size > 0)
20507 end = start + sym->st_size;
20508
20509 if (is_open_attr)
20510 {
20511 /* FIXME: Need to properly allow for section alignment.
20512 16 is just the alignment used on x86_64. */
20513 if (global_end > 0
20514 && start > BFD_ALIGN (global_end, 16)
20515 /* Build notes are not guaranteed to be organised in order of
20516 increasing address, but we should find the all of the notes
20517 for one section in the same place. */
20518 && same_section (filedata, start, global_end))
20519 warn (_("Gap in build notes detected from %#lx to %#lx\n"),
20520 global_end + 1, start - 1);
20521
20522 printf (_(" Applies to region from %#lx"), start);
20523 global_offset = start;
20524
20525 if (end)
20526 {
20527 printf (_(" to %#lx"), end);
20528 global_end = end;
20529 }
20530 }
20531 else
20532 {
20533 printf (_(" Applies to region from %#lx"), start);
20534 func_offset = start;
20535
20536 if (end)
20537 {
20538 printf (_(" to %#lx"), end);
20539 func_end = end;
20540 }
20541 }
20542
20543 if (sym && name)
20544 printf (_(" (%s)"), name);
20545
20546 printf ("\n");
20547 return true;
20548}
20549
20550static bool
20551print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
20552{
20553 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
20554 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
20555 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
20556 char name_type;
20557 char name_attribute;
20558 const char * expected_types;
20559 const char * name = pnote->namedata;
20560 const char * text;
20561 signed int left;
20562
20563 if (name == NULL || pnote->namesz < 2)
20564 {
20565 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
20566 print_symbol (-20, _(" <corrupt name>"));
20567 return false;
20568 }
20569
20570 if (do_wide)
20571 left = 28;
20572 else
20573 left = 20;
20574
20575 /* Version 2 of the spec adds a "GA" prefix to the name field. */
20576 if (name[0] == 'G' && name[1] == 'A')
20577 {
20578 if (pnote->namesz < 4)
20579 {
20580 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
20581 print_symbol (-20, _(" <corrupt name>"));
20582 return false;
20583 }
20584
20585 printf ("GA");
20586 name += 2;
20587 left -= 2;
20588 }
20589
20590 switch ((name_type = * name))
20591 {
20592 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
20593 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
20594 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
20595 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
20596 printf ("%c", * name);
20597 left --;
20598 break;
20599 default:
20600 error (_("unrecognised attribute type in name field: %d\n"), name_type);
20601 print_symbol (-20, _("<unknown name type>"));
20602 return false;
20603 }
20604
20605 ++ name;
20606 text = NULL;
20607
20608 switch ((name_attribute = * name))
20609 {
20610 case GNU_BUILD_ATTRIBUTE_VERSION:
20611 text = _("<version>");
20612 expected_types = string_expected;
20613 ++ name;
20614 break;
20615 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
20616 text = _("<stack prot>");
20617 expected_types = "!+*";
20618 ++ name;
20619 break;
20620 case GNU_BUILD_ATTRIBUTE_RELRO:
20621 text = _("<relro>");
20622 expected_types = bool_expected;
20623 ++ name;
20624 break;
20625 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
20626 text = _("<stack size>");
20627 expected_types = number_expected;
20628 ++ name;
20629 break;
20630 case GNU_BUILD_ATTRIBUTE_TOOL:
20631 text = _("<tool>");
20632 expected_types = string_expected;
20633 ++ name;
20634 break;
20635 case GNU_BUILD_ATTRIBUTE_ABI:
20636 text = _("<ABI>");
20637 expected_types = "$*";
20638 ++ name;
20639 break;
20640 case GNU_BUILD_ATTRIBUTE_PIC:
20641 text = _("<PIC>");
20642 expected_types = number_expected;
20643 ++ name;
20644 break;
20645 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
20646 text = _("<short enum>");
20647 expected_types = bool_expected;
20648 ++ name;
20649 break;
20650 default:
20651 if (ISPRINT (* name))
20652 {
20653 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
20654
20655 if (len > left && ! do_wide)
20656 len = left;
20657 printf ("%.*s:", len, name);
20658 left -= len;
20659 name += len;
20660 }
20661 else
20662 {
20663 static char tmpbuf [128];
20664
20665 error (_("unrecognised byte in name field: %d\n"), * name);
20666 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
20667 text = tmpbuf;
20668 name ++;
20669 }
20670 expected_types = "*$!+";
20671 break;
20672 }
20673
20674 if (text)
20675 left -= printf ("%s", text);
20676
20677 if (strchr (expected_types, name_type) == NULL)
20678 warn (_("attribute does not have an expected type (%c)\n"), name_type);
20679
20680 if ((unsigned long)(name - pnote->namedata) > pnote->namesz)
20681 {
20682 error (_("corrupt name field: namesz: %lu but parsing gets to %ld\n"),
20683 (unsigned long) pnote->namesz,
20684 (long) (name - pnote->namedata));
20685 return false;
20686 }
20687
20688 if (left < 1 && ! do_wide)
20689 return true;
20690
20691 switch (name_type)
20692 {
20693 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
20694 {
20695 unsigned int bytes;
20696 unsigned long long val = 0;
20697 unsigned int shift = 0;
20698 char * decoded = NULL;
20699
20700 bytes = pnote->namesz - (name - pnote->namedata);
20701 if (bytes > 0)
20702 /* The -1 is because the name field is always 0 terminated, and we
20703 want to be able to ensure that the shift in the while loop below
20704 will not overflow. */
20705 -- bytes;
20706
20707 if (bytes > sizeof (val))
20708 {
20709 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
20710 bytes);
20711 bytes = sizeof (val);
20712 }
20713 /* We do not bother to warn if bytes == 0 as this can
20714 happen with some early versions of the gcc plugin. */
20715
20716 while (bytes --)
20717 {
20718 unsigned long long byte = *name++ & 0xff;
20719
20720 val |= byte << shift;
20721 shift += 8;
20722 }
20723
20724 switch (name_attribute)
20725 {
20726 case GNU_BUILD_ATTRIBUTE_PIC:
20727 switch (val)
20728 {
20729 case 0: decoded = "static"; break;
20730 case 1: decoded = "pic"; break;
20731 case 2: decoded = "PIC"; break;
20732 case 3: decoded = "pie"; break;
20733 case 4: decoded = "PIE"; break;
20734 default: break;
20735 }
20736 break;
20737 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
20738 switch (val)
20739 {
20740 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
20741 case 0: decoded = "off"; break;
20742 case 1: decoded = "on"; break;
20743 case 2: decoded = "all"; break;
20744 case 3: decoded = "strong"; break;
20745 case 4: decoded = "explicit"; break;
20746 default: break;
20747 }
20748 break;
20749 default:
20750 break;
20751 }
20752
20753 if (decoded != NULL)
20754 {
20755 print_symbol (-left, decoded);
20756 left = 0;
20757 }
20758 else if (val == 0)
20759 {
20760 printf ("0x0");
20761 left -= 3;
20762 }
20763 else
20764 {
20765 if (do_wide)
20766 left -= printf ("0x%llx", val);
20767 else
20768 left -= printf ("0x%-.*llx", left, val);
20769 }
20770 }
20771 break;
20772 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
20773 left -= print_symbol (- left, name);
20774 break;
20775 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
20776 left -= print_symbol (- left, "true");
20777 break;
20778 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
20779 left -= print_symbol (- left, "false");
20780 break;
20781 }
20782
20783 if (do_wide && left > 0)
20784 printf ("%-*s", left, " ");
20785
20786 return true;
20787}
20788
20789/* Note that by the ELF standard, the name field is already null byte
20790 terminated, and namesz includes the terminating null byte.
20791 I.E. the value of namesz for the name "FSF" is 4.
20792
20793 If the value of namesz is zero, there is no name present. */
20794
20795static bool
20796process_note (Elf_Internal_Note * pnote,
20797 Filedata * filedata)
20798{
20799 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
20800 const char * nt;
20801
20802 if (pnote->namesz == 0)
20803 /* If there is no note name, then use the default set of
20804 note type strings. */
20805 nt = get_note_type (filedata, pnote->type);
20806
20807 else if (startswith (pnote->namedata, "GNU"))
20808 /* GNU-specific object file notes. */
20809 nt = get_gnu_elf_note_type (pnote->type);
20810
20811 else if (startswith (pnote->namedata, "FreeBSD"))
20812 /* FreeBSD-specific core file notes. */
20813 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
20814
20815 else if (startswith (pnote->namedata, "NetBSD-CORE"))
20816 /* NetBSD-specific core file notes. */
20817 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
20818
20819 else if (startswith (pnote->namedata, "NetBSD"))
20820 /* NetBSD-specific core file notes. */
20821 return process_netbsd_elf_note (pnote);
20822
20823 else if (startswith (pnote->namedata, "PaX"))
20824 /* NetBSD-specific core file notes. */
20825 return process_netbsd_elf_note (pnote);
20826
20827 else if (startswith (pnote->namedata, "OpenBSD"))
20828 /* OpenBSD-specific core file notes. */
20829 nt = get_openbsd_elfcore_note_type (filedata, pnote->type);
20830
20831 else if (startswith (pnote->namedata, "SPU/"))
20832 {
20833 /* SPU-specific core file notes. */
20834 nt = pnote->namedata + 4;
20835 name = "SPU";
20836 }
20837
20838 else if (startswith (pnote->namedata, "IPF/VMS"))
20839 /* VMS/ia64-specific file notes. */
20840 nt = get_ia64_vms_note_type (pnote->type);
20841
20842 else if (startswith (pnote->namedata, "stapsdt"))
20843 nt = get_stapsdt_note_type (pnote->type);
20844
20845 else
20846 /* Don't recognize this note name; just use the default set of
20847 note type strings. */
20848 nt = get_note_type (filedata, pnote->type);
20849
20850 printf (" ");
20851
20852 if (((startswith (pnote->namedata, "GA")
20853 && strchr ("*$!+", pnote->namedata[2]) != NULL)
20854 || strchr ("*$!+", pnote->namedata[0]) != NULL)
20855 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
20856 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
20857 print_gnu_build_attribute_name (pnote);
20858 else
20859 print_symbol (-20, name);
20860
20861 if (do_wide)
20862 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
20863 else
20864 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
20865
20866 if (startswith (pnote->namedata, "IPF/VMS"))
20867 return print_ia64_vms_note (pnote);
20868 else if (startswith (pnote->namedata, "GNU"))
20869 return print_gnu_note (filedata, pnote);
20870 else if (startswith (pnote->namedata, "stapsdt"))
20871 return print_stapsdt_note (pnote);
20872 else if (startswith (pnote->namedata, "CORE"))
20873 return print_core_note (pnote);
20874 else if (((startswith (pnote->namedata, "GA")
20875 && strchr ("*$!+", pnote->namedata[2]) != NULL)
20876 || strchr ("*$!+", pnote->namedata[0]) != NULL)
20877 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
20878 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
20879 return print_gnu_build_attribute_description (pnote, filedata);
20880
20881 if (pnote->descsz)
20882 {
20883 unsigned long i;
20884
20885 printf (_(" description data: "));
20886 for (i = 0; i < pnote->descsz; i++)
20887 printf ("%02x ", pnote->descdata[i] & 0xff);
20888 if (!do_wide)
20889 printf ("\n");
20890 }
20891
20892 if (do_wide)
20893 printf ("\n");
20894
20895 return true;
20896}
20897
20898static bool
20899process_notes_at (Filedata * filedata,
20900 Elf_Internal_Shdr * section,
20901 bfd_vma offset,
20902 bfd_vma length,
20903 bfd_vma align)
20904{
20905 Elf_External_Note *pnotes;
20906 Elf_External_Note *external;
20907 char *end;
20908 bool res = true;
20909
20910 if (length <= 0)
20911 return false;
20912
20913 if (section)
20914 {
20915 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
20916 if (pnotes)
20917 {
20918 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
20919 {
20920 free (pnotes);
20921 return false;
20922 }
20923 }
20924 }
20925 else
20926 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
20927 _("notes"));
20928
20929 if (pnotes == NULL)
20930 return false;
20931
20932 external = pnotes;
20933
20934 if (filedata->is_separate)
20935 printf (_("In linked file '%s': "), filedata->file_name);
20936 else
20937 printf ("\n");
20938 if (section)
20939 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
20940 else
20941 printf (_("Displaying notes found at file offset 0x%08lx with length 0x%08lx:\n"),
20942 (unsigned long) offset, (unsigned long) length);
20943
20944 /* NB: Some note sections may have alignment value of 0 or 1. gABI
20945 specifies that notes should be aligned to 4 bytes in 32-bit
20946 objects and to 8 bytes in 64-bit objects. As a Linux extension,
20947 we also support 4 byte alignment in 64-bit objects. If section
20948 alignment is less than 4, we treate alignment as 4 bytes. */
20949 if (align < 4)
20950 align = 4;
20951 else if (align != 4 && align != 8)
20952 {
20953 warn (_("Corrupt note: alignment %ld, expecting 4 or 8\n"),
20954 (long) align);
20955 free (pnotes);
20956 return false;
20957 }
20958
20959 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
20960
20961 end = (char *) pnotes + length;
20962 while ((char *) external < end)
20963 {
20964 Elf_Internal_Note inote;
20965 size_t min_notesz;
20966 char * next;
20967 char * temp = NULL;
20968 size_t data_remaining = end - (char *) external;
20969
20970 if (!is_ia64_vms (filedata))
20971 {
20972 /* PR binutils/15191
20973 Make sure that there is enough data to read. */
20974 min_notesz = offsetof (Elf_External_Note, name);
20975 if (data_remaining < min_notesz)
20976 {
20977 warn (ngettext ("Corrupt note: only %ld byte remains, "
20978 "not enough for a full note\n",
20979 "Corrupt note: only %ld bytes remain, "
20980 "not enough for a full note\n",
20981 data_remaining),
20982 (long) data_remaining);
20983 break;
20984 }
20985 data_remaining -= min_notesz;
20986
20987 inote.type = BYTE_GET (external->type);
20988 inote.namesz = BYTE_GET (external->namesz);
20989 inote.namedata = external->name;
20990 inote.descsz = BYTE_GET (external->descsz);
20991 inote.descdata = ((char *) external
20992 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
20993 inote.descpos = offset + (inote.descdata - (char *) pnotes);
20994 next = ((char *) external
20995 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
20996 }
20997 else
20998 {
20999 Elf64_External_VMS_Note *vms_external;
21000
21001 /* PR binutils/15191
21002 Make sure that there is enough data to read. */
21003 min_notesz = offsetof (Elf64_External_VMS_Note, name);
21004 if (data_remaining < min_notesz)
21005 {
21006 warn (ngettext ("Corrupt note: only %ld byte remains, "
21007 "not enough for a full note\n",
21008 "Corrupt note: only %ld bytes remain, "
21009 "not enough for a full note\n",
21010 data_remaining),
21011 (long) data_remaining);
21012 break;
21013 }
21014 data_remaining -= min_notesz;
21015
21016 vms_external = (Elf64_External_VMS_Note *) external;
21017 inote.type = BYTE_GET (vms_external->type);
21018 inote.namesz = BYTE_GET (vms_external->namesz);
21019 inote.namedata = vms_external->name;
21020 inote.descsz = BYTE_GET (vms_external->descsz);
21021 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
21022 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21023 next = inote.descdata + align_power (inote.descsz, 3);
21024 }
21025
21026 /* PR 17531: file: 3443835e. */
21027 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
21028 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
21029 || (size_t) (inote.descdata - inote.namedata) > data_remaining
21030 || (size_t) (next - inote.descdata) < inote.descsz
21031 || ((size_t) (next - inote.descdata)
21032 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
21033 {
21034 warn (_("note with invalid namesz and/or descsz found at offset 0x%lx\n"),
21035 (unsigned long) ((char *) external - (char *) pnotes));
21036 warn (_(" type: 0x%lx, namesize: 0x%08lx, descsize: 0x%08lx, alignment: %u\n"),
21037 inote.type, inote.namesz, inote.descsz, (int) align);
21038 break;
21039 }
21040
21041 external = (Elf_External_Note *) next;
21042
21043 /* Verify that name is null terminated. It appears that at least
21044 one version of Linux (RedHat 6.0) generates corefiles that don't
21045 comply with the ELF spec by failing to include the null byte in
21046 namesz. */
21047 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
21048 {
21049 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
21050 {
21051 temp = (char *) malloc (inote.namesz + 1);
21052 if (temp == NULL)
21053 {
21054 error (_("Out of memory allocating space for inote name\n"));
21055 res = false;
21056 break;
21057 }
21058
21059 memcpy (temp, inote.namedata, inote.namesz);
21060 inote.namedata = temp;
21061 }
21062 inote.namedata[inote.namesz] = 0;
21063 }
21064
21065 if (! process_note (& inote, filedata))
21066 res = false;
21067
21068 free (temp);
21069 temp = NULL;
21070 }
21071
21072 free (pnotes);
21073
21074 return res;
21075}
21076
21077static bool
21078process_corefile_note_segments (Filedata * filedata)
21079{
21080 Elf_Internal_Phdr *segment;
21081 unsigned int i;
21082 bool res = true;
21083
21084 if (! get_program_headers (filedata))
21085 return true;
21086
21087 for (i = 0, segment = filedata->program_headers;
21088 i < filedata->file_header.e_phnum;
21089 i++, segment++)
21090 {
21091 if (segment->p_type == PT_NOTE)
21092 if (! process_notes_at (filedata, NULL,
21093 (bfd_vma) segment->p_offset,
21094 (bfd_vma) segment->p_filesz,
21095 (bfd_vma) segment->p_align))
21096 res = false;
21097 }
21098
21099 return res;
21100}
21101
21102static bool
21103process_v850_notes (Filedata * filedata, bfd_vma offset, bfd_vma length)
21104{
21105 Elf_External_Note * pnotes;
21106 Elf_External_Note * external;
21107 char * end;
21108 bool res = true;
21109
21110 if (length <= 0)
21111 return false;
21112
21113 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
21114 _("v850 notes"));
21115 if (pnotes == NULL)
21116 return false;
21117
21118 external = pnotes;
21119 end = (char*) pnotes + length;
21120
21121 printf (_("\nDisplaying contents of Renesas V850 notes section at offset 0x%lx with length 0x%lx:\n"),
21122 (unsigned long) offset, (unsigned long) length);
21123
21124 while ((char *) external + sizeof (Elf_External_Note) < end)
21125 {
21126 Elf_External_Note * next;
21127 Elf_Internal_Note inote;
21128
21129 inote.type = BYTE_GET (external->type);
21130 inote.namesz = BYTE_GET (external->namesz);
21131 inote.namedata = external->name;
21132 inote.descsz = BYTE_GET (external->descsz);
21133 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
21134 inote.descpos = offset + (inote.descdata - (char *) pnotes);
21135
21136 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
21137 {
21138 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
21139 inote.descdata = inote.namedata;
21140 inote.namesz = 0;
21141 }
21142
21143 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
21144
21145 if ( ((char *) next > end)
21146 || ((char *) next < (char *) pnotes))
21147 {
21148 warn (_("corrupt descsz found in note at offset 0x%lx\n"),
21149 (unsigned long) ((char *) external - (char *) pnotes));
21150 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21151 inote.type, inote.namesz, inote.descsz);
21152 break;
21153 }
21154
21155 external = next;
21156
21157 /* Prevent out-of-bounds indexing. */
21158 if ( inote.namedata + inote.namesz > end
21159 || inote.namedata + inote.namesz < inote.namedata)
21160 {
21161 warn (_("corrupt namesz found in note at offset 0x%lx\n"),
21162 (unsigned long) ((char *) external - (char *) pnotes));
21163 warn (_(" type: 0x%lx, namesize: 0x%lx, descsize: 0x%lx\n"),
21164 inote.type, inote.namesz, inote.descsz);
21165 break;
21166 }
21167
21168 printf (" %s: ", get_v850_elf_note_type (inote.type));
21169
21170 if (! print_v850_note (& inote))
21171 {
21172 res = false;
21173 printf ("<corrupt sizes: namesz: %lx, descsz: %lx>\n",
21174 inote.namesz, inote.descsz);
21175 }
21176 }
21177
21178 free (pnotes);
21179
21180 return res;
21181}
21182
21183static bool
21184process_note_sections (Filedata * filedata)
21185{
21186 Elf_Internal_Shdr *section;
21187 unsigned long i;
21188 unsigned int n = 0;
21189 bool res = true;
21190
21191 for (i = 0, section = filedata->section_headers;
21192 i < filedata->file_header.e_shnum && section != NULL;
21193 i++, section++)
21194 {
21195 if (section->sh_type == SHT_NOTE)
21196 {
21197 if (! process_notes_at (filedata, section,
21198 (bfd_vma) section->sh_offset,
21199 (bfd_vma) section->sh_size,
21200 (bfd_vma) section->sh_addralign))
21201 res = false;
21202 n++;
21203 }
21204
21205 if (( filedata->file_header.e_machine == EM_V800
21206 || filedata->file_header.e_machine == EM_V850
21207 || filedata->file_header.e_machine == EM_CYGNUS_V850)
21208 && section->sh_type == SHT_RENESAS_INFO)
21209 {
21210 if (! process_v850_notes (filedata,
21211 (bfd_vma) section->sh_offset,
21212 (bfd_vma) section->sh_size))
21213 res = false;
21214 n++;
21215 }
21216 }
21217
21218 if (n == 0)
21219 /* Try processing NOTE segments instead. */
21220 return process_corefile_note_segments (filedata);
21221
21222 return res;
21223}
21224
21225static bool
21226process_notes (Filedata * filedata)
21227{
21228 /* If we have not been asked to display the notes then do nothing. */
21229 if (! do_notes)
21230 return true;
21231
21232 if (filedata->file_header.e_type != ET_CORE)
21233 return process_note_sections (filedata);
21234
21235 /* No program headers means no NOTE segment. */
21236 if (filedata->file_header.e_phnum > 0)
21237 return process_corefile_note_segments (filedata);
21238
21239 if (filedata->is_separate)
21240 printf (_("No notes found in linked file '%s'.\n"),
21241 filedata->file_name);
21242 else
21243 printf (_("No notes found file.\n"));
21244
21245 return true;
21246}
21247
21248static unsigned char *
21249display_public_gnu_attributes (unsigned char * start,
21250 const unsigned char * const end)
21251{
21252 printf (_(" Unknown GNU attribute: %s\n"), start);
21253
21254 start += strnlen ((char *) start, end - start);
21255 display_raw_attribute (start, end);
21256
21257 return (unsigned char *) end;
21258}
21259
21260static unsigned char *
21261display_generic_attribute (unsigned char * start,
21262 unsigned int tag,
21263 const unsigned char * const end)
21264{
21265 if (tag == 0)
21266 return (unsigned char *) end;
21267
21268 return display_tag_value (tag, start, end);
21269}
21270
21271static bool
21272process_arch_specific (Filedata * filedata)
21273{
21274 if (! do_arch)
21275 return true;
21276
21277 switch (filedata->file_header.e_machine)
21278 {
21279 case EM_ARC:
21280 case EM_ARC_COMPACT:
21281 case EM_ARC_COMPACT2:
21282 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
21283 display_arc_attribute,
21284 display_generic_attribute);
21285 case EM_ARM:
21286 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
21287 display_arm_attribute,
21288 display_generic_attribute);
21289
21290 case EM_MIPS:
21291 case EM_MIPS_RS3_LE:
21292 return process_mips_specific (filedata);
21293
21294 case EM_MSP430:
21295 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
21296 display_msp430_attribute,
21297 display_msp430_gnu_attribute);
21298
21299 case EM_RISCV:
21300 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
21301 display_riscv_attribute,
21302 display_generic_attribute);
21303
21304 case EM_NDS32:
21305 return process_nds32_specific (filedata);
21306
21307 case EM_68K:
21308 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21309 display_m68k_gnu_attribute);
21310
21311 case EM_PPC:
21312 case EM_PPC64:
21313 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21314 display_power_gnu_attribute);
21315
21316 case EM_S390:
21317 case EM_S390_OLD:
21318 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21319 display_s390_gnu_attribute);
21320
21321 case EM_SPARC:
21322 case EM_SPARC32PLUS:
21323 case EM_SPARCV9:
21324 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
21325 display_sparc_gnu_attribute);
21326
21327 case EM_TI_C6000:
21328 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
21329 display_tic6x_attribute,
21330 display_generic_attribute);
21331
21332 case EM_CSKY:
21333 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
21334 display_csky_attribute, NULL);
21335
21336 default:
21337 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
21338 display_public_gnu_attributes,
21339 display_generic_attribute);
21340 }
21341}
21342
21343static bool
21344get_file_header (Filedata * filedata)
21345{
21346 /* Read in the identity array. */
21347 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
21348 return false;
21349
21350 /* Determine how to read the rest of the header. */
21351 switch (filedata->file_header.e_ident[EI_DATA])
21352 {
21353 default:
21354 case ELFDATANONE:
21355 case ELFDATA2LSB:
21356 byte_get = byte_get_little_endian;
21357 byte_put = byte_put_little_endian;
21358 break;
21359 case ELFDATA2MSB:
21360 byte_get = byte_get_big_endian;
21361 byte_put = byte_put_big_endian;
21362 break;
21363 }
21364
21365 /* For now we only support 32 bit and 64 bit ELF files. */
21366 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
21367
21368 /* Read in the rest of the header. */
21369 if (is_32bit_elf)
21370 {
21371 Elf32_External_Ehdr ehdr32;
21372
21373 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
21374 return false;
21375
21376 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
21377 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
21378 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
21379 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
21380 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
21381 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
21382 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
21383 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
21384 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
21385 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
21386 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
21387 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
21388 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
21389 }
21390 else
21391 {
21392 Elf64_External_Ehdr ehdr64;
21393
21394 /* If we have been compiled with sizeof (bfd_vma) == 4, then
21395 we will not be able to cope with the 64bit data found in
21396 64 ELF files. Detect this now and abort before we start
21397 overwriting things. */
21398 if (sizeof (bfd_vma) < 8)
21399 {
21400 error (_("This instance of readelf has been built without support for a\n\
2140164 bit data type and so it cannot read 64 bit ELF files.\n"));
21402 return false;
21403 }
21404
21405 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
21406 return false;
21407
21408 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
21409 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
21410 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
21411 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
21412 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
21413 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
21414 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
21415 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
21416 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
21417 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
21418 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
21419 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
21420 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
21421 }
21422
21423 return true;
21424}
21425
21426static void
21427free_filedata (Filedata *filedata)
21428{
21429 free (filedata->program_interpreter);
21430 free (filedata->program_headers);
21431 free (filedata->section_headers);
21432 free (filedata->string_table);
21433 free (filedata->dump.dump_sects);
21434 free (filedata->dynamic_strings);
21435 free (filedata->dynamic_symbols);
21436 free (filedata->dynamic_syminfo);
21437 free (filedata->dynamic_section);
21438
21439 while (filedata->symtab_shndx_list != NULL)
21440 {
21441 elf_section_list *next = filedata->symtab_shndx_list->next;
21442 free (filedata->symtab_shndx_list);
21443 filedata->symtab_shndx_list = next;
21444 }
21445
21446 free (filedata->section_headers_groups);
21447
21448 if (filedata->section_groups)
21449 {
21450 size_t i;
21451 struct group_list * g;
21452 struct group_list * next;
21453
21454 for (i = 0; i < filedata->group_count; i++)
21455 {
21456 for (g = filedata->section_groups [i].root; g != NULL; g = next)
21457 {
21458 next = g->next;
21459 free (g);
21460 }
21461 }
21462
21463 free (filedata->section_groups);
21464 }
21465 memset (&filedata->section_headers, 0,
21466 sizeof (Filedata) - offsetof (Filedata, section_headers));
21467}
21468
21469static void
21470close_file (Filedata * filedata)
21471{
21472 if (filedata)
21473 {
21474 if (filedata->handle)
21475 fclose (filedata->handle);
21476 free (filedata);
21477 }
21478}
21479
21480void
21481close_debug_file (void * data)
21482{
21483 free_filedata ((Filedata *) data);
21484 close_file ((Filedata *) data);
21485}
21486
21487static Filedata *
21488open_file (const char * pathname, bool is_separate)
21489{
21490 struct stat statbuf;
21491 Filedata * filedata = NULL;
21492
21493 if (stat (pathname, & statbuf) < 0
21494 || ! S_ISREG (statbuf.st_mode))
21495 goto fail;
21496
21497 filedata = calloc (1, sizeof * filedata);
21498 if (filedata == NULL)
21499 goto fail;
21500
21501 filedata->handle = fopen (pathname, "rb");
21502 if (filedata->handle == NULL)
21503 goto fail;
21504
21505 filedata->file_size = (bfd_size_type) statbuf.st_size;
21506 filedata->file_name = pathname;
21507 filedata->is_separate = is_separate;
21508
21509 if (! get_file_header (filedata))
21510 goto fail;
21511
21512 if (!get_section_headers (filedata, false))
21513 goto fail;
21514
21515 return filedata;
21516
21517 fail:
21518 if (filedata)
21519 {
21520 if (filedata->handle)
21521 fclose (filedata->handle);
21522 free (filedata);
21523 }
21524 return NULL;
21525}
21526
21527void *
21528open_debug_file (const char * pathname)
21529{
21530 return open_file (pathname, true);
21531}
21532
21533static void
21534initialise_dump_sects (Filedata * filedata)
21535{
21536 /* Initialise the dump_sects array from the cmdline_dump_sects array.
21537 Note we do this even if cmdline_dump_sects is empty because we
21538 must make sure that the dump_sets array is zeroed out before each
21539 object file is processed. */
21540 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
21541 memset (filedata->dump.dump_sects, 0,
21542 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21543
21544 if (cmdline.num_dump_sects > 0)
21545 {
21546 if (filedata->dump.num_dump_sects == 0)
21547 /* A sneaky way of allocating the dump_sects array. */
21548 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
21549
21550 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
21551 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
21552 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
21553 }
21554}
21555
21556/* Process one ELF object file according to the command line options.
21557 This file may actually be stored in an archive. The file is
21558 positioned at the start of the ELF object. Returns TRUE if no
21559 problems were encountered, FALSE otherwise. */
21560
21561static bool
21562process_object (Filedata * filedata)
21563{
21564 bool have_separate_files;
21565 unsigned int i;
21566 bool res;
21567
21568 if (! get_file_header (filedata))
21569 {
21570 error (_("%s: Failed to read file header\n"), filedata->file_name);
21571 return false;
21572 }
21573
21574 /* Initialise per file variables. */
21575 for (i = ARRAY_SIZE (filedata->version_info); i--;)
21576 filedata->version_info[i] = 0;
21577
21578 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
21579 filedata->dynamic_info[i] = 0;
21580 filedata->dynamic_info_DT_GNU_HASH = 0;
21581 filedata->dynamic_info_DT_MIPS_XHASH = 0;
21582
21583 /* Process the file. */
21584 if (show_name)
21585 printf (_("\nFile: %s\n"), filedata->file_name);
21586
21587 initialise_dump_sects (filedata);
21588
21589 /* There may be some extensions in the first section header. Don't
21590 bomb if we can't read it. */
21591 get_section_headers (filedata, true);
21592
21593 if (! process_file_header (filedata))
21594 {
21595 res = false;
21596 goto out;
21597 }
21598
21599 /* Throw away the single section header read above, so that we
21600 re-read the entire set. */
21601 free (filedata->section_headers);
21602 filedata->section_headers = NULL;
21603
21604 if (! process_section_headers (filedata))
21605 {
21606 /* Without loaded section headers we cannot process lots of things. */
21607 do_unwind = do_version = do_dump = do_arch = false;
21608
21609 if (! do_using_dynamic)
21610 do_syms = do_dyn_syms = do_reloc = false;
21611 }
21612
21613 if (! process_section_groups (filedata))
21614 /* Without loaded section groups we cannot process unwind. */
21615 do_unwind = false;
21616
21617 process_program_headers (filedata);
21618
21619 res = process_dynamic_section (filedata);
21620
21621 if (! process_relocs (filedata))
21622 res = false;
21623
21624 if (! process_unwind (filedata))
21625 res = false;
21626
21627 if (! process_symbol_table (filedata))
21628 res = false;
21629
21630 if (! process_lto_symbol_tables (filedata))
21631 res = false;
21632
21633 if (! process_syminfo (filedata))
21634 res = false;
21635
21636 if (! process_version_sections (filedata))
21637 res = false;
21638
21639 if (filedata->file_header.e_shstrndx != SHN_UNDEF)
21640 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
21641 else
21642 have_separate_files = false;
21643
21644 if (! process_section_contents (filedata))
21645 res = false;
21646
21647 if (have_separate_files)
21648 {
21649 separate_info * d;
21650
21651 for (d = first_separate_info; d != NULL; d = d->next)
21652 {
21653 initialise_dump_sects (d->handle);
21654
21655 if (process_links && ! process_file_header (d->handle))
21656 res = false;
21657 else if (! process_section_headers (d->handle))
21658 res = false;
21659 else if (! process_section_contents (d->handle))
21660 res = false;
21661 else if (process_links)
21662 {
21663 if (! process_section_groups (d->handle))
21664 res = false;
21665 process_program_headers (d->handle);
21666 if (! process_dynamic_section (d->handle))
21667 res = false;
21668 if (! process_relocs (d->handle))
21669 res = false;
21670 if (! process_unwind (d->handle))
21671 res = false;
21672 if (! process_symbol_table (d->handle))
21673 res = false;
21674 if (! process_lto_symbol_tables (d->handle))
21675 res = false;
21676 if (! process_syminfo (d->handle))
21677 res = false;
21678 if (! process_version_sections (d->handle))
21679 res = false;
21680 if (! process_notes (d->handle))
21681 res = false;
21682 }
21683 }
21684
21685 /* The file handles are closed by the call to free_debug_memory() below. */
21686 }
21687
21688 if (! process_notes (filedata))
21689 res = false;
21690
21691 if (! process_gnu_liblist (filedata))
21692 res = false;
21693
21694 if (! process_arch_specific (filedata))
21695 res = false;
21696
21697 out:
21698 free_filedata (filedata);
21699
21700 free_debug_memory ();
21701
21702 return res;
21703}
21704
21705/* Process an ELF archive.
21706 On entry the file is positioned just after the ARMAG string.
21707 Returns TRUE upon success, FALSE otherwise. */
21708
21709static bool
21710process_archive (Filedata * filedata, bool is_thin_archive)
21711{
21712 struct archive_info arch;
21713 struct archive_info nested_arch;
21714 size_t got;
21715 bool ret = true;
21716
21717 show_name = true;
21718
21719 /* The ARCH structure is used to hold information about this archive. */
21720 arch.file_name = NULL;
21721 arch.file = NULL;
21722 arch.index_array = NULL;
21723 arch.sym_table = NULL;
21724 arch.longnames = NULL;
21725
21726 /* The NESTED_ARCH structure is used as a single-item cache of information
21727 about a nested archive (when members of a thin archive reside within
21728 another regular archive file). */
21729 nested_arch.file_name = NULL;
21730 nested_arch.file = NULL;
21731 nested_arch.index_array = NULL;
21732 nested_arch.sym_table = NULL;
21733 nested_arch.longnames = NULL;
21734
21735 if (setup_archive (&arch, filedata->file_name, filedata->handle,
21736 filedata->file_size, is_thin_archive,
21737 do_archive_index) != 0)
21738 {
21739 ret = false;
21740 goto out;
21741 }
21742
21743 if (do_archive_index)
21744 {
21745 if (arch.sym_table == NULL)
21746 error (_("%s: unable to dump the index as none was found\n"),
21747 filedata->file_name);
21748 else
21749 {
21750 unsigned long i, l;
21751 unsigned long current_pos;
21752
21753 printf (_("Index of archive %s: (%lu entries, 0x%lx bytes "
21754 "in the symbol table)\n"),
21755 filedata->file_name, (unsigned long) arch.index_num,
21756 arch.sym_size);
21757
21758 current_pos = ftell (filedata->handle);
21759
21760 for (i = l = 0; i < arch.index_num; i++)
21761 {
21762 if (i == 0
21763 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
21764 {
21765 char * member_name
21766 = get_archive_member_name_at (&arch, arch.index_array[i],
21767 &nested_arch);
21768
21769 if (member_name != NULL)
21770 {
21771 char * qualified_name
21772 = make_qualified_name (&arch, &nested_arch,
21773 member_name);
21774
21775 if (qualified_name != NULL)
21776 {
21777 printf (_("Contents of binary %s at offset "),
21778 qualified_name);
21779 (void) print_vma (arch.index_array[i], PREFIX_HEX);
21780 putchar ('\n');
21781 free (qualified_name);
21782 }
21783 free (member_name);
21784 }
21785 }
21786
21787 if (l >= arch.sym_size)
21788 {
21789 error (_("%s: end of the symbol table reached "
21790 "before the end of the index\n"),
21791 filedata->file_name);
21792 ret = false;
21793 break;
21794 }
21795 /* PR 17531: file: 0b6630b2. */
21796 printf ("\t%.*s\n",
21797 (int) (arch.sym_size - l), arch.sym_table + l);
21798 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
21799 }
21800
21801 if (arch.uses_64bit_indices)
21802 l = (l + 7) & ~ 7;
21803 else
21804 l += l & 1;
21805
21806 if (l < arch.sym_size)
21807 {
21808 error (ngettext ("%s: %ld byte remains in the symbol table, "
21809 "but without corresponding entries in "
21810 "the index table\n",
21811 "%s: %ld bytes remain in the symbol table, "
21812 "but without corresponding entries in "
21813 "the index table\n",
21814 arch.sym_size - l),
21815 filedata->file_name, arch.sym_size - l);
21816 ret = false;
21817 }
21818
21819 if (fseek (filedata->handle, current_pos, SEEK_SET) != 0)
21820 {
21821 error (_("%s: failed to seek back to start of object files "
21822 "in the archive\n"),
21823 filedata->file_name);
21824 ret = false;
21825 goto out;
21826 }
21827 }
21828
21829 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
21830 && !do_segments && !do_header && !do_dump && !do_version
21831 && !do_histogram && !do_debugging && !do_arch && !do_notes
21832 && !do_section_groups && !do_dyn_syms)
21833 {
21834 ret = true; /* Archive index only. */
21835 goto out;
21836 }
21837 }
21838
21839 while (1)
21840 {
21841 char * name;
21842 size_t namelen;
21843 char * qualified_name;
21844
21845 /* Read the next archive header. */
21846 if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
21847 {
21848 error (_("%s: failed to seek to next archive header\n"),
21849 arch.file_name);
21850 ret = false;
21851 break;
21852 }
21853 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
21854 if (got != sizeof arch.arhdr)
21855 {
21856 if (got == 0)
21857 break;
21858 /* PR 24049 - we cannot use filedata->file_name as this will
21859 have already been freed. */
21860 error (_("%s: failed to read archive header\n"), arch.file_name);
21861
21862 ret = false;
21863 break;
21864 }
21865 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
21866 {
21867 error (_("%s: did not find a valid archive header\n"),
21868 arch.file_name);
21869 ret = false;
21870 break;
21871 }
21872
21873 arch.next_arhdr_offset += sizeof arch.arhdr;
21874
21875 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
21876
21877 name = get_archive_member_name (&arch, &nested_arch);
21878 if (name == NULL)
21879 {
21880 error (_("%s: bad archive file name\n"), arch.file_name);
21881 ret = false;
21882 break;
21883 }
21884 namelen = strlen (name);
21885
21886 qualified_name = make_qualified_name (&arch, &nested_arch, name);
21887 if (qualified_name == NULL)
21888 {
21889 error (_("%s: bad archive file name\n"), arch.file_name);
21890 free (name);
21891 ret = false;
21892 break;
21893 }
21894
21895 if (is_thin_archive && arch.nested_member_origin == 0)
21896 {
21897 /* This is a proxy for an external member of a thin archive. */
21898 Filedata * member_filedata;
21899 char * member_file_name = adjust_relative_path
21900 (filedata->file_name, name, namelen);
21901
21902 free (name);
21903 if (member_file_name == NULL)
21904 {
21905 free (qualified_name);
21906 ret = false;
21907 break;
21908 }
21909
21910 member_filedata = open_file (member_file_name, false);
21911 if (member_filedata == NULL)
21912 {
21913 error (_("Input file '%s' is not readable.\n"), member_file_name);
21914 free (member_file_name);
21915 free (qualified_name);
21916 ret = false;
21917 break;
21918 }
21919
21920 filedata->archive_file_offset = arch.nested_member_origin;
21921 member_filedata->file_name = qualified_name;
21922
21923 /* The call to process_object() expects the file to be at the beginning. */
21924 rewind (member_filedata->handle);
21925
21926 if (! process_object (member_filedata))
21927 ret = false;
21928
21929 close_file (member_filedata);
21930 free (member_file_name);
21931 }
21932 else if (is_thin_archive)
21933 {
21934 Filedata thin_filedata;
21935
21936 memset (&thin_filedata, 0, sizeof (thin_filedata));
21937
21938 /* PR 15140: Allow for corrupt thin archives. */
21939 if (nested_arch.file == NULL)
21940 {
21941 error (_("%s: contains corrupt thin archive: %s\n"),
21942 qualified_name, name);
21943 free (qualified_name);
21944 free (name);
21945 ret = false;
21946 break;
21947 }
21948 free (name);
21949
21950 /* This is a proxy for a member of a nested archive. */
21951 filedata->archive_file_offset
21952 = arch.nested_member_origin + sizeof arch.arhdr;
21953
21954 /* The nested archive file will have been opened and setup by
21955 get_archive_member_name. */
21956 if (fseek (nested_arch.file, filedata->archive_file_offset,
21957 SEEK_SET) != 0)
21958 {
21959 error (_("%s: failed to seek to archive member.\n"),
21960 nested_arch.file_name);
21961 free (qualified_name);
21962 ret = false;
21963 break;
21964 }
21965
21966 thin_filedata.handle = nested_arch.file;
21967 thin_filedata.file_name = qualified_name;
21968
21969 if (! process_object (& thin_filedata))
21970 ret = false;
21971 }
21972 else
21973 {
21974 free (name);
21975 filedata->archive_file_offset = arch.next_arhdr_offset;
21976 filedata->file_name = qualified_name;
21977 if (! process_object (filedata))
21978 ret = false;
21979 arch.next_arhdr_offset += (filedata->archive_file_size + 1) & -2;
21980 /* Stop looping with "negative" archive_file_size. */
21981 if (arch.next_arhdr_offset < filedata->archive_file_size)
21982 arch.next_arhdr_offset = -1ul;
21983 }
21984
21985 free (qualified_name);
21986 }
21987
21988 out:
21989 if (nested_arch.file != NULL)
21990 fclose (nested_arch.file);
21991 release_archive (&nested_arch);
21992 release_archive (&arch);
21993
21994 return ret;
21995}
21996
21997static bool
21998process_file (char * file_name)
21999{
22000 Filedata * filedata = NULL;
22001 struct stat statbuf;
22002 char armag[SARMAG];
22003 bool ret = true;
22004
22005 if (stat (file_name, &statbuf) < 0)
22006 {
22007 if (errno == ENOENT)
22008 error (_("'%s': No such file\n"), file_name);
22009 else
22010 error (_("Could not locate '%s'. System error message: %s\n"),
22011 file_name, strerror (errno));
22012 return false;
22013 }
22014
22015 if (! S_ISREG (statbuf.st_mode))
22016 {
22017 error (_("'%s' is not an ordinary file\n"), file_name);
22018 return false;
22019 }
22020
22021 filedata = calloc (1, sizeof * filedata);
22022 if (filedata == NULL)
22023 {
22024 error (_("Out of memory allocating file data structure\n"));
22025 return false;
22026 }
22027
22028 filedata->file_name = file_name;
22029 filedata->handle = fopen (file_name, "rb");
22030 if (filedata->handle == NULL)
22031 {
22032 error (_("Input file '%s' is not readable.\n"), file_name);
22033 free (filedata);
22034 return false;
22035 }
22036
22037 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
22038 {
22039 error (_("%s: Failed to read file's magic number\n"), file_name);
22040 fclose (filedata->handle);
22041 free (filedata);
22042 return false;
22043 }
22044
22045 filedata->file_size = (bfd_size_type) statbuf.st_size;
22046 filedata->is_separate = false;
22047
22048 if (memcmp (armag, ARMAG, SARMAG) == 0)
22049 {
22050 if (! process_archive (filedata, false))
22051 ret = false;
22052 }
22053 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
22054 {
22055 if ( ! process_archive (filedata, true))
22056 ret = false;
22057 }
22058 else
22059 {
22060 if (do_archive_index && !check_all)
22061 error (_("File %s is not an archive so its index cannot be displayed.\n"),
22062 file_name);
22063
22064 rewind (filedata->handle);
22065 filedata->archive_file_size = filedata->archive_file_offset = 0;
22066
22067 if (! process_object (filedata))
22068 ret = false;
22069 }
22070
22071 fclose (filedata->handle);
22072 free (filedata->section_headers);
22073 free (filedata->program_headers);
22074 free (filedata->string_table);
22075 free (filedata->dump.dump_sects);
22076 free (filedata);
22077
22078 free (ba_cache.strtab);
22079 ba_cache.strtab = NULL;
22080 free (ba_cache.symtab);
22081 ba_cache.symtab = NULL;
22082 ba_cache.filedata = NULL;
22083
22084 return ret;
22085}
22086
22087#ifdef SUPPORT_DISASSEMBLY
22088/* Needed by the i386 disassembler. For extra credit, someone could
22089 fix this so that we insert symbolic addresses here, esp for GOT/PLT
22090 symbols. */
22091
22092void
22093print_address (unsigned int addr, FILE * outfile)
22094{
22095 fprintf (outfile,"0x%8.8x", addr);
22096}
22097
22098/* Needed by the i386 disassembler. */
22099
22100void
22101db_task_printsym (unsigned int addr)
22102{
22103 print_address (addr, stderr);
22104}
22105#endif
22106
22107int
22108main (int argc, char ** argv)
22109{
22110 int err;
22111
22112#ifdef HAVE_LC_MESSAGES
22113 setlocale (LC_MESSAGES, "");
22114#endif
22115 setlocale (LC_CTYPE, "");
22116 bindtextdomain (PACKAGE, LOCALEDIR);
22117 textdomain (PACKAGE);
22118
22119 expandargv (&argc, &argv);
22120
22121 parse_args (& cmdline, argc, argv);
22122
22123 if (optind < (argc - 1))
22124 /* When displaying information for more than one file,
22125 prefix the information with the file name. */
22126 show_name = true;
22127 else if (optind >= argc)
22128 {
22129 /* Ensure that the warning is always displayed. */
22130 do_checks = true;
22131
22132 warn (_("Nothing to do.\n"));
22133 usage (stderr);
22134 }
22135
22136 err = false;
22137 while (optind < argc)
22138 if (! process_file (argv[optind++]))
22139 err = true;
22140
22141 free (cmdline.dump_sects);
22142
22143 free (dump_ctf_symtab_name);
22144 free (dump_ctf_strtab_name);
22145 free (dump_ctf_parent_name);
22146
22147 return err ? EXIT_FAILURE : EXIT_SUCCESS;
22148}