]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/readelf.c
c9b6210e229dab30154e88ca244aab6227d8c245
[thirdparty/binutils-gdb.git] / binutils / readelf.c
1 /* readelf.c -- display contents of an ELF format file
2 Copyright (C) 1998-2023 Free Software Foundation, Inc.
3
4 Originally developed by Eric Youngdale <eric@andante.jic.com>
5 Modifications by Nick Clifton <nickc@redhat.com>
6
7 This file is part of GNU Binutils.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
22 02110-1301, USA. */
23 \f
24 /* The difference between readelf and objdump:
25
26 Both programs are capable of displaying the contents of ELF format files,
27 so why does the binutils project have two file dumpers ?
28
29 The reason is that objdump sees an ELF file through a BFD filter of the
30 world; if BFD has a bug where, say, it disagrees about a machine constant
31 in e_flags, then the odds are good that it will remain internally
32 consistent. The linker sees it the BFD way, objdump sees it the BFD way,
33 GAS sees it the BFD way. There was need for a tool to go find out what
34 the file actually says.
35
36 This is why the readelf program does not link against the BFD library - it
37 exists as an independent program to help verify the correct working of BFD.
38
39 There is also the case that readelf can provide more information about an
40 ELF file than is provided by objdump. In particular it can display DWARF
41 debugging information which (at the moment) objdump cannot. */
42 \f
43 #include "sysdep.h"
44 #include <assert.h>
45 #include <time.h>
46 #include <zlib.h>
47 #ifdef HAVE_ZSTD
48 #include <zstd.h>
49 #endif
50 #include <wchar.h>
51
52 #if defined HAVE_MSGPACK
53 #include <msgpack.h>
54 #endif
55
56 /* Define BFD64 here, even if our default architecture is 32 bit ELF
57 as this will allow us to read in and parse 64bit and 32bit ELF files. */
58 #define BFD64
59
60 #include "bfd.h"
61 #include "bucomm.h"
62 #include "elfcomm.h"
63 #include "demanguse.h"
64 #include "dwarf.h"
65 #include "ctf-api.h"
66 #include "sframe-api.h"
67 #include "demangle.h"
68
69 #include "elf/common.h"
70 #include "elf/external.h"
71 #include "elf/internal.h"
72
73
74 /* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
75 we can obtain the H8 reloc numbers. We need these for the
76 get_reloc_size() function. We include h8.h again after defining
77 RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
78
79 #include "elf/h8.h"
80 #undef _ELF_H8_H
81
82 /* Undo the effects of #including reloc-macros.h. */
83
84 #undef START_RELOC_NUMBERS
85 #undef RELOC_NUMBER
86 #undef FAKE_RELOC
87 #undef EMPTY_RELOC
88 #undef END_RELOC_NUMBERS
89 #undef _RELOC_MACROS_H
90
91 /* The following headers use the elf/reloc-macros.h file to
92 automatically generate relocation recognition functions
93 such as elf_mips_reloc_type() */
94
95 #define RELOC_MACROS_GEN_FUNC
96
97 #include "elf/aarch64.h"
98 #include "elf/alpha.h"
99 #include "elf/amdgpu.h"
100 #include "elf/arc.h"
101 #include "elf/arm.h"
102 #include "elf/avr.h"
103 #include "elf/bfin.h"
104 #include "elf/cr16.h"
105 #include "elf/cris.h"
106 #include "elf/crx.h"
107 #include "elf/csky.h"
108 #include "elf/d10v.h"
109 #include "elf/d30v.h"
110 #include "elf/dlx.h"
111 #include "elf/bpf.h"
112 #include "elf/epiphany.h"
113 #include "elf/fr30.h"
114 #include "elf/frv.h"
115 #include "elf/ft32.h"
116 #include "elf/h8.h"
117 #include "elf/hppa.h"
118 #include "elf/i386.h"
119 #include "elf/i370.h"
120 #include "elf/i860.h"
121 #include "elf/i960.h"
122 #include "elf/ia64.h"
123 #include "elf/ip2k.h"
124 #include "elf/kvx.h"
125 #include "elf/lm32.h"
126 #include "elf/iq2000.h"
127 #include "elf/m32c.h"
128 #include "elf/m32r.h"
129 #include "elf/m68k.h"
130 #include "elf/m68hc11.h"
131 #include "elf/s12z.h"
132 #include "elf/mcore.h"
133 #include "elf/mep.h"
134 #include "elf/metag.h"
135 #include "elf/microblaze.h"
136 #include "elf/mips.h"
137 #include "elf/mmix.h"
138 #include "elf/mn10200.h"
139 #include "elf/mn10300.h"
140 #include "elf/moxie.h"
141 #include "elf/mt.h"
142 #include "elf/msp430.h"
143 #include "elf/nds32.h"
144 #include "elf/nfp.h"
145 #include "elf/nios2.h"
146 #include "elf/or1k.h"
147 #include "elf/pj.h"
148 #include "elf/ppc.h"
149 #include "elf/ppc64.h"
150 #include "elf/pru.h"
151 #include "elf/riscv.h"
152 #include "elf/rl78.h"
153 #include "elf/rx.h"
154 #include "elf/s390.h"
155 #include "elf/score.h"
156 #include "elf/sh.h"
157 #include "elf/sparc.h"
158 #include "elf/spu.h"
159 #include "elf/tic6x.h"
160 #include "elf/tilegx.h"
161 #include "elf/tilepro.h"
162 #include "elf/v850.h"
163 #include "elf/vax.h"
164 #include "elf/visium.h"
165 #include "elf/wasm32.h"
166 #include "elf/x86-64.h"
167 #include "elf/xgate.h"
168 #include "elf/xstormy16.h"
169 #include "elf/xtensa.h"
170 #include "elf/z80.h"
171 #include "elf/loongarch.h"
172 #include "elf/bpf.h"
173
174 #include "getopt.h"
175 #include "libiberty.h"
176 #include "safe-ctype.h"
177 #include "filenames.h"
178
179 #ifndef offsetof
180 #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
181 #endif
182
183 typedef struct elf_section_list
184 {
185 Elf_Internal_Shdr * hdr;
186 struct elf_section_list * next;
187 } elf_section_list;
188
189 /* Flag bits indicating particular types of dump. */
190 #define HEX_DUMP (1 << 0) /* The -x command line switch. */
191 #define DISASS_DUMP (1 << 1) /* The -i command line switch. */
192 #define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
193 #define STRING_DUMP (1 << 3) /* The -p command line switch. */
194 #define RELOC_DUMP (1 << 4) /* The -R command line switch. */
195 #define CTF_DUMP (1 << 5) /* The --ctf command line switch. */
196 #define SFRAME_DUMP (1 << 6) /* The --sframe command line switch. */
197
198 typedef unsigned char dump_type;
199
200 /* A linked list of the section names for which dumps were requested. */
201 struct dump_list_entry
202 {
203 char * name;
204 dump_type type;
205 struct dump_list_entry * next;
206 };
207
208 /* A dynamic array of flags indicating for which sections a dump
209 has been requested via command line switches. */
210 struct dump_data
211 {
212 dump_type * dump_sects;
213 unsigned int num_dump_sects;
214 };
215
216 static struct dump_data cmdline;
217
218 static struct dump_list_entry * dump_sects_byname;
219
220 char * program_name = "readelf";
221
222 static bool show_name = false;
223 static bool do_dynamic = false;
224 static bool do_syms = false;
225 static bool do_dyn_syms = false;
226 static bool do_lto_syms = false;
227 static bool do_reloc = false;
228 static bool do_sections = false;
229 static bool do_section_groups = false;
230 static bool do_section_details = false;
231 static bool do_segments = false;
232 static bool do_unwind = false;
233 static bool do_using_dynamic = false;
234 static bool do_header = false;
235 static bool do_dump = false;
236 static bool do_version = false;
237 static bool do_histogram = false;
238 static bool do_debugging = false;
239 static bool do_ctf = false;
240 static bool do_sframe = false;
241 static bool do_arch = false;
242 static bool do_notes = false;
243 static bool do_archive_index = false;
244 static bool check_all = false;
245 static bool is_32bit_elf = false;
246 static bool decompress_dumps = false;
247 static bool do_not_show_symbol_truncation = false;
248 static bool do_demangle = false; /* Pretty print C++ symbol names. */
249 static bool process_links = false;
250 static bool dump_any_debugging = false;
251 static bool extra_sym_info = false;
252 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
253 static int sym_base = 0;
254
255 static char *dump_ctf_parent_name;
256 static char *dump_ctf_symtab_name;
257 static char *dump_ctf_strtab_name;
258
259 struct group_list
260 {
261 struct group_list * next;
262 unsigned int section_index;
263 };
264
265 struct group
266 {
267 struct group_list * root;
268 unsigned int group_index;
269 };
270
271 typedef struct filedata
272 {
273 const char * file_name;
274 bool is_separate;
275 FILE * handle;
276 uint64_t file_size;
277 Elf_Internal_Ehdr file_header;
278 uint64_t archive_file_offset;
279 uint64_t archive_file_size;
280 /* Everything below this point is cleared out by free_filedata. */
281 Elf_Internal_Shdr * section_headers;
282 Elf_Internal_Phdr * program_headers;
283 char * string_table;
284 uint64_t string_table_length;
285 uint64_t dynamic_addr;
286 uint64_t dynamic_size;
287 uint64_t dynamic_nent;
288 Elf_Internal_Dyn * dynamic_section;
289 Elf_Internal_Shdr * dynamic_strtab_section;
290 char * dynamic_strings;
291 uint64_t dynamic_strings_length;
292 Elf_Internal_Shdr * dynamic_symtab_section;
293 uint64_t num_dynamic_syms;
294 Elf_Internal_Sym * dynamic_symbols;
295 uint64_t version_info[16];
296 unsigned int dynamic_syminfo_nent;
297 Elf_Internal_Syminfo * dynamic_syminfo;
298 uint64_t dynamic_syminfo_offset;
299 uint64_t nbuckets;
300 uint64_t nchains;
301 uint64_t * buckets;
302 uint64_t * chains;
303 uint64_t ngnubuckets;
304 uint64_t ngnuchains;
305 uint64_t * gnubuckets;
306 uint64_t * gnuchains;
307 uint64_t * mipsxlat;
308 uint64_t gnusymidx;
309 char * program_interpreter;
310 uint64_t dynamic_info[DT_RELRENT + 1];
311 uint64_t dynamic_info_DT_GNU_HASH;
312 uint64_t dynamic_info_DT_MIPS_XHASH;
313 elf_section_list * symtab_shndx_list;
314 size_t group_count;
315 struct group * section_groups;
316 struct group ** section_headers_groups;
317 /* A dynamic array of flags indicating for which sections a dump of
318 some kind has been requested. It is reset on a per-object file
319 basis and then initialised from the cmdline_dump_sects array,
320 the results of interpreting the -w switch, and the
321 dump_sects_byname list. */
322 struct dump_data dump;
323 } Filedata;
324
325 /* How to print a vma value. */
326 typedef enum print_mode
327 {
328 HEX,
329 HEX_5,
330 DEC,
331 DEC_5,
332 UNSIGNED,
333 UNSIGNED_5,
334 PREFIX_HEX,
335 PREFIX_HEX_5,
336 FULL_HEX,
337 LONG_HEX,
338 OCTAL,
339 OCTAL_5
340 }
341 print_mode;
342
343 typedef enum unicode_display_type
344 {
345 unicode_default = 0,
346 unicode_locale,
347 unicode_escape,
348 unicode_hex,
349 unicode_highlight,
350 unicode_invalid
351 } unicode_display_type;
352
353 static unicode_display_type unicode_display = unicode_default;
354
355 typedef enum
356 {
357 reltype_unknown,
358 reltype_rel,
359 reltype_rela,
360 reltype_relr
361 } relocation_type;
362
363 /* Versioned symbol info. */
364 enum versioned_symbol_info
365 {
366 symbol_undefined,
367 symbol_hidden,
368 symbol_public
369 };
370
371 static int
372 fseek64 (FILE *stream, int64_t offset, int whence)
373 {
374 #if defined (HAVE_FSEEKO64)
375 off64_t o = offset;
376 if (o != offset)
377 {
378 errno = EINVAL;
379 return -1;
380 }
381 return fseeko64 (stream, o, whence);
382 #elif defined (HAVE_FSEEKO)
383 off_t o = offset;
384 if (o != offset)
385 {
386 errno = EINVAL;
387 return -1;
388 }
389 return fseeko (stream, o, whence);
390 #else
391 long o = offset;
392 if (o != offset)
393 {
394 errno = EINVAL;
395 return -1;
396 }
397 return fseek (stream, o, whence);
398 #endif
399 }
400
401 static const char * get_symbol_version_string
402 (Filedata *, bool, const char *, size_t, unsigned,
403 Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
404
405 #define UNKNOWN -1
406
407 static inline const char *
408 section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
409 {
410 return filedata->string_table + hdr->sh_name;
411 }
412
413 static inline bool
414 section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
415 {
416 return (filedata != NULL
417 && hdr != NULL
418 && filedata->string_table != NULL
419 && hdr->sh_name < filedata->string_table_length);
420 }
421
422 /* Returns true if the given index is real/valid. Note: "real" here
423 means "references a real section in the section header" and not
424 "is a valid section index as per the ELF standard". */
425
426 static inline bool
427 section_index_real (const Filedata *filedata, unsigned int ndx)
428 {
429 return (filedata != NULL
430 && filedata->section_headers != NULL
431 && ndx < filedata->file_header.e_shnum
432 && ndx > 0);
433 }
434
435 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
436
437 static inline bool
438 valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
439 {
440 return strtab != NULL && offset < strtab_size;
441 }
442
443 static inline bool
444 valid_dynamic_name (const Filedata *filedata, uint64_t offset)
445 {
446 return valid_symbol_name (filedata->dynamic_strings,
447 filedata->dynamic_strings_length, offset);
448 }
449
450 /* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
451 already been called and verified that the string exists. */
452 static inline const char *
453 get_dynamic_name (const Filedata *filedata, size_t offset)
454 {
455 return filedata->dynamic_strings + offset;
456 }
457
458 #define REMOVE_ARCH_BITS(ADDR) \
459 do \
460 { \
461 if (filedata->file_header.e_machine == EM_ARM) \
462 (ADDR) &= ~1; \
463 } \
464 while (0)
465
466 /* Get the correct GNU hash section name. */
467 #define GNU_HASH_SECTION_NAME(filedata) \
468 filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
469 \f
470 /* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
471 OFFSET + the offset of the current archive member, if we are examining an
472 archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
473 allocate a buffer using malloc and fill that. In either case return the
474 pointer to the start of the retrieved data or NULL if something went wrong.
475 If something does go wrong and REASON is not NULL then emit an error
476 message using REASON as part of the context. */
477
478 static void *
479 get_data (void *var,
480 Filedata *filedata,
481 uint64_t offset,
482 uint64_t size,
483 uint64_t nmemb,
484 const char *reason)
485 {
486 void * mvar;
487 uint64_t amt = size * nmemb;
488
489 if (size == 0 || nmemb == 0)
490 return NULL;
491
492 /* If size_t is smaller than uint64_t, eg because you are building
493 on a 32-bit host, then make sure that when the sizes are cast to
494 size_t no information is lost. */
495 if ((size_t) size != size
496 || (size_t) nmemb != nmemb
497 || (size_t) amt != amt
498 || amt / size != nmemb
499 || (size_t) amt + 1 == 0)
500 {
501 if (reason)
502 error (_("Size overflow prevents reading %" PRIu64
503 " elements of size %" PRIu64 " for %s\n"),
504 nmemb, size, reason);
505 return NULL;
506 }
507
508 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
509 attempting to allocate memory when the read is bound to fail. */
510 if (filedata->archive_file_offset > filedata->file_size
511 || offset > filedata->file_size - filedata->archive_file_offset
512 || amt > filedata->file_size - filedata->archive_file_offset - offset)
513 {
514 if (reason)
515 error (_("Reading %" PRIu64 " bytes extends past end of file for %s\n"),
516 amt, reason);
517 return NULL;
518 }
519
520 if (fseek64 (filedata->handle, filedata->archive_file_offset + offset,
521 SEEK_SET))
522 {
523 if (reason)
524 error (_("Unable to seek to %#" PRIx64 " for %s\n"),
525 filedata->archive_file_offset + offset, reason);
526 return NULL;
527 }
528
529 mvar = var;
530 if (mvar == NULL)
531 {
532 /* + 1 so that we can '\0' terminate invalid string table sections. */
533 mvar = malloc ((size_t) amt + 1);
534
535 if (mvar == NULL)
536 {
537 if (reason)
538 error (_("Out of memory allocating %" PRIu64 " bytes for %s\n"),
539 amt, reason);
540 return NULL;
541 }
542
543 ((char *) mvar)[amt] = '\0';
544 }
545
546 if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
547 {
548 if (reason)
549 error (_("Unable to read in %" PRIu64 " bytes of %s\n"),
550 amt, reason);
551 if (mvar != var)
552 free (mvar);
553 return NULL;
554 }
555
556 return mvar;
557 }
558
559 /* Print a VMA value in the MODE specified.
560 Returns the number of characters displayed. */
561
562 static unsigned int
563 print_vma (uint64_t vma, print_mode mode)
564 {
565 unsigned int nc = 0;
566
567 switch (mode)
568 {
569 case FULL_HEX:
570 nc = printf ("0x");
571 /* Fall through. */
572 case LONG_HEX:
573 if (!is_32bit_elf)
574 return nc + printf ("%16.16" PRIx64, vma);
575 return nc + printf ("%8.8" PRIx64, vma);
576
577 case DEC_5:
578 if (vma <= 99999)
579 return printf ("%5" PRId64, vma);
580 /* Fall through. */
581 case PREFIX_HEX:
582 nc = printf ("0x");
583 /* Fall through. */
584 case HEX:
585 return nc + printf ("%" PRIx64, vma);
586
587 case PREFIX_HEX_5:
588 nc = printf ("0x");
589 /* Fall through. */
590 case HEX_5:
591 return nc + printf ("%05" PRIx64, vma);
592
593 case DEC:
594 return printf ("%" PRId64, vma);
595
596 case UNSIGNED:
597 return printf ("%" PRIu64, vma);
598
599 case UNSIGNED_5:
600 return printf ("%5" PRIu64, vma);
601
602 case OCTAL:
603 return printf ("%" PRIo64, vma);
604
605 case OCTAL_5:
606 return printf ("%5" PRIo64, vma);
607
608 default:
609 /* FIXME: Report unrecognised mode ? */
610 return 0;
611 }
612 }
613
614
615 /* Display a symbol on stdout. Handles the display of control characters and
616 multibye characters (assuming the host environment supports them).
617
618 Display at most abs(WIDTH) characters, truncating as necessary,
619 unless do_wide or extra_sym_info is true.
620
621 If truncation will happen and do_not_show_symbol_truncation is FALSE then display
622 abs(WIDTH) - 5 characters followed by "[...]".
623
624 If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
625 padding as necessary.
626
627 Returns the number of emitted characters. */
628
629 static unsigned int
630 print_symbol_name (signed int width, const char * symbol)
631 {
632 bool extra_padding = false;
633 bool do_dots = false;
634 signed int num_printed = 0;
635 #ifdef HAVE_MBSTATE_T
636 mbstate_t state;
637 #endif
638 unsigned int width_remaining;
639 const void * alloced_symbol = NULL;
640
641 if (width < 0)
642 {
643 /* Keep the width positive. This helps the code below. */
644 width = - width;
645 extra_padding = true;
646 }
647 else if (width == 0)
648 return 0;
649
650 if (do_wide || extra_sym_info)
651 /* Set the remaining width to a very large value.
652 This simplifies the code below. */
653 width_remaining = INT_MAX;
654 else
655 {
656 width_remaining = width;
657
658 if (! do_not_show_symbol_truncation
659 && (int) strlen (symbol) > width)
660 {
661 width_remaining -= 5;
662 if ((int) width_remaining < 0)
663 width_remaining = 0;
664 do_dots = true;
665 }
666 }
667
668 #ifdef HAVE_MBSTATE_T
669 /* Initialise the multibyte conversion state. */
670 memset (& state, 0, sizeof (state));
671 #endif
672
673 if (do_demangle && *symbol)
674 {
675 const char * res = cplus_demangle (symbol, demangle_flags);
676
677 if (res != NULL)
678 alloced_symbol = symbol = res;
679 }
680
681 while (width_remaining)
682 {
683 size_t n;
684 const char c = *symbol++;
685
686 if (c == 0)
687 break;
688
689 if (ISPRINT (c))
690 {
691 putchar (c);
692 width_remaining --;
693 num_printed ++;
694 }
695 else if (ISCNTRL (c))
696 {
697 /* Do not print control characters directly as they can affect terminal
698 settings. Such characters usually appear in the names generated
699 by the assembler for local labels. */
700
701 if (width_remaining < 2)
702 break;
703
704 printf ("^%c", c + 0x40);
705 width_remaining -= 2;
706 num_printed += 2;
707 }
708 else if (c == 0x7f)
709 {
710 if (width_remaining < 5)
711 break;
712 printf ("<DEL>");
713 width_remaining -= 5;
714 num_printed += 5;
715 }
716 else if (unicode_display != unicode_locale
717 && unicode_display != unicode_default)
718 {
719 /* Display unicode characters as something else. */
720 unsigned char bytes[4];
721 bool is_utf8;
722 unsigned int nbytes;
723
724 bytes[0] = c;
725
726 if (bytes[0] < 0xc0)
727 {
728 nbytes = 1;
729 is_utf8 = false;
730 }
731 else
732 {
733 bytes[1] = *symbol++;
734
735 if ((bytes[1] & 0xc0) != 0x80)
736 {
737 is_utf8 = false;
738 /* Do not consume this character. It may only
739 be the first byte in the sequence that was
740 corrupt. */
741 --symbol;
742 nbytes = 1;
743 }
744 else if ((bytes[0] & 0x20) == 0)
745 {
746 is_utf8 = true;
747 nbytes = 2;
748 }
749 else
750 {
751 bytes[2] = *symbol++;
752
753 if ((bytes[2] & 0xc0) != 0x80)
754 {
755 is_utf8 = false;
756 symbol -= 2;
757 nbytes = 1;
758 }
759 else if ((bytes[0] & 0x10) == 0)
760 {
761 is_utf8 = true;
762 nbytes = 3;
763 }
764 else
765 {
766 bytes[3] = *symbol++;
767
768 nbytes = 4;
769
770 if ((bytes[3] & 0xc0) != 0x80)
771 {
772 is_utf8 = false;
773 symbol -= 3;
774 nbytes = 1;
775 }
776 else
777 is_utf8 = true;
778 }
779 }
780 }
781
782 if (unicode_display == unicode_invalid)
783 is_utf8 = false;
784
785 if (unicode_display == unicode_hex || ! is_utf8)
786 {
787 unsigned int i;
788
789 if (width_remaining < (nbytes * 2) + 2)
790 break;
791
792 putchar (is_utf8 ? '<' : '{');
793 printf ("0x");
794 for (i = 0; i < nbytes; i++)
795 printf ("%02x", bytes[i]);
796 putchar (is_utf8 ? '>' : '}');
797 }
798 else
799 {
800 if (unicode_display == unicode_highlight && isatty (1))
801 printf ("\x1B[31;47m"); /* Red. */
802
803 switch (nbytes)
804 {
805 case 2:
806 if (width_remaining < 6)
807 break;
808 printf ("\\u%02x%02x",
809 (bytes[0] & 0x1c) >> 2,
810 ((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
811 break;
812 case 3:
813 if (width_remaining < 6)
814 break;
815 printf ("\\u%02x%02x",
816 ((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
817 ((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
818 break;
819 case 4:
820 if (width_remaining < 8)
821 break;
822 printf ("\\u%02x%02x%02x",
823 ((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
824 ((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
825 ((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
826
827 break;
828 default:
829 /* URG. */
830 break;
831 }
832
833 if (unicode_display == unicode_highlight && isatty (1))
834 printf ("\033[0m"); /* Default colour. */
835 }
836
837 if (bytes[nbytes - 1] == 0)
838 break;
839 }
840 else
841 {
842 #ifdef HAVE_MBSTATE_T
843 wchar_t w;
844 #endif
845 /* Let printf do the hard work of displaying multibyte characters. */
846 printf ("%.1s", symbol - 1);
847 width_remaining --;
848 num_printed ++;
849
850 #ifdef HAVE_MBSTATE_T
851 /* Try to find out how many bytes made up the character that was
852 just printed. Advance the symbol pointer past the bytes that
853 were displayed. */
854 n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
855 #else
856 n = 1;
857 #endif
858 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
859 symbol += (n - 1);
860 }
861 }
862
863 if (do_dots)
864 num_printed += printf ("[...]");
865
866 if (extra_padding && num_printed < width)
867 {
868 /* Fill in the remaining spaces. */
869 printf ("%-*s", width - num_printed, " ");
870 num_printed = width;
871 }
872
873 free ((void *) alloced_symbol);
874 return num_printed;
875 }
876
877 /* Returns a pointer to a static buffer containing a printable version of
878 the given section's name. Like print_symbol, except that it does not try
879 to print multibyte characters, it just interprets them as hex values. */
880
881 static const char *
882 printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
883 {
884 #define NUM_SEC_NAME_BUFS 5
885 #define MAX_PRINT_SEC_NAME_LEN 256
886
887 static int sec_name_buf_index = 0;
888 /* We use a rotating array of static buffers, so that multiple successive calls
889 to printable_section_name() will still work. eg when used in a printf. */
890 static char sec_name_buf [NUM_SEC_NAME_BUFS][MAX_PRINT_SEC_NAME_LEN + 1];
891
892 const char * name;
893 char * buf;
894 char * buf_start;
895 char c;
896 unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
897
898 /* Validate the input parameters. */
899 if (filedata == NULL)
900 return _("<internal error>");
901 if (sec == NULL)
902 return _("<none>");
903 if (filedata->string_table == NULL)
904 return _("<no-strings>");
905 if (sec->sh_name >= filedata->string_table_length)
906 return _("<corrupt>");
907
908 /* Select a buffer to use. */
909 buf_start = buf = sec_name_buf[sec_name_buf_index];
910 if (++sec_name_buf_index >= NUM_SEC_NAME_BUFS)
911 sec_name_buf_index = 0;
912
913 name = section_name (filedata, sec);
914
915 while ((c = * name ++) != 0)
916 {
917 if (ISCNTRL (c))
918 {
919 if (remaining < 2)
920 break;
921
922 * buf ++ = '^';
923 * buf ++ = c + 0x40;
924 remaining -= 2;
925 }
926 else if (ISPRINT (c))
927 {
928 * buf ++ = c;
929 remaining -= 1;
930 }
931 else
932 {
933 static char hex[17] = "0123456789ABCDEF";
934
935 if (remaining < 4)
936 break;
937 * buf ++ = '<';
938 * buf ++ = hex[(c & 0xf0) >> 4];
939 * buf ++ = hex[c & 0x0f];
940 * buf ++ = '>';
941 remaining -= 4;
942 }
943
944 if (remaining == 0)
945 break;
946 }
947
948 * buf = 0;
949 return buf_start;
950 }
951
952 /* Return TRUE if the current file is for IA-64 machine and OpenVMS ABI.
953 This OS has so many departures from the ELF standard that we test it at
954 many places. */
955
956 static inline bool
957 is_ia64_vms (Filedata * filedata)
958 {
959 return filedata->file_header.e_machine == EM_IA_64
960 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
961 }
962
963 static const char *
964 printable_section_name_from_index (Filedata * filedata,
965 size_t ndx,
966 bool * is_special)
967 {
968 if (is_special != NULL)
969 * is_special = true;
970
971 switch (ndx)
972 {
973 case SHN_UNDEF: return "UND";
974 case SHN_ABS: return "ABS";
975 case SHN_COMMON: return "COM";
976 break;
977 }
978
979 if (filedata != NULL)
980 {
981 switch (filedata->file_header.e_machine)
982 {
983 case EM_MIPS:
984 if (ndx == SHN_MIPS_SCOMMON)
985 return "SCOMMON";
986 if (ndx == SHN_MIPS_SUNDEFINED)
987 return "SUNDEF";
988 break;
989
990 case EM_TI_C6000:
991 if (ndx == SHN_TIC6X_SCOMMON)
992 return "SCOM";
993 break;
994
995 case EM_X86_64:
996 case EM_L1OM:
997 case EM_K1OM:
998 if (ndx == SHN_X86_64_LCOMMON)
999 return "LARGE_COM";
1000 break;
1001
1002 case EM_IA_64:
1003 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_HPUX
1004 && ndx == SHN_IA_64_ANSI_COMMON)
1005 return "ANSI_COM";
1006
1007 if (is_ia64_vms (filedata) && ndx == SHN_IA_64_VMS_SYMVEC)
1008 return "VMS_SYMVEC";
1009 break;
1010
1011 default:
1012 break;
1013 }
1014
1015 if (filedata->section_headers != NULL
1016 && ndx < filedata->file_header.e_shnum)
1017 {
1018 const char * res;
1019
1020 res = printable_section_name (filedata, filedata->section_headers + ndx);
1021 if (is_special != NULL)
1022 * is_special = (res[0] == '<');
1023
1024 return res;
1025 }
1026 }
1027
1028 static char name_buf[40];
1029 unsigned int short_ndx = (unsigned int) (ndx & 0xffff);
1030
1031 if (ndx >= SHN_LOPROC && ndx <= SHN_HIPROC)
1032 sprintf (name_buf, "PRC[0x%04x]", short_ndx);
1033 else if (ndx >= SHN_LOOS && ndx <= SHN_HIOS)
1034 sprintf (name_buf, "OS [0x%04x]", short_ndx);
1035 else if (ndx >= SHN_LORESERVE)
1036 sprintf (name_buf, "RSV[0x%04x]", short_ndx);
1037 else if (filedata->file_header.e_shnum != 0
1038 && ndx >= filedata->file_header.e_shnum)
1039 sprintf (name_buf, _("BAD[0x%lx]"), (long) ndx);
1040 else
1041 sprintf (name_buf, "<section 0x%lx>", (long) ndx);
1042
1043 return name_buf;
1044 }
1045
1046 /* Return a pointer to section NAME, or NULL if no such section exists. */
1047
1048 static Elf_Internal_Shdr *
1049 find_section (Filedata * filedata, const char * name)
1050 {
1051 unsigned int i;
1052
1053 if (filedata->section_headers == NULL)
1054 return NULL;
1055
1056 for (i = 0; i < filedata->file_header.e_shnum; i++)
1057 if (section_name_valid (filedata, filedata->section_headers + i)
1058 && streq (section_name (filedata, filedata->section_headers + i),
1059 name))
1060 return filedata->section_headers + i;
1061
1062 return NULL;
1063 }
1064
1065 /* Return a pointer to a section containing ADDR, or NULL if no such
1066 section exists. */
1067
1068 static Elf_Internal_Shdr *
1069 find_section_by_address (Filedata * filedata, uint64_t addr)
1070 {
1071 unsigned int i;
1072
1073 if (filedata->section_headers == NULL)
1074 return NULL;
1075
1076 for (i = 0; i < filedata->file_header.e_shnum; i++)
1077 {
1078 Elf_Internal_Shdr *sec = filedata->section_headers + i;
1079
1080 if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
1081 return sec;
1082 }
1083
1084 return NULL;
1085 }
1086
1087 static Elf_Internal_Shdr *
1088 find_section_by_type (Filedata * filedata, unsigned int type)
1089 {
1090 unsigned int i;
1091
1092 if (filedata->section_headers == NULL)
1093 return NULL;
1094
1095 for (i = 0; i < filedata->file_header.e_shnum; i++)
1096 {
1097 Elf_Internal_Shdr *sec = filedata->section_headers + i;
1098
1099 if (sec->sh_type == type)
1100 return sec;
1101 }
1102
1103 return NULL;
1104 }
1105
1106 /* Return a pointer to section NAME, or NULL if no such section exists,
1107 restricted to the list of sections given in SET. */
1108
1109 static Elf_Internal_Shdr *
1110 find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
1111 {
1112 unsigned int i;
1113
1114 if (filedata->section_headers == NULL)
1115 return NULL;
1116
1117 if (set != NULL)
1118 {
1119 while ((i = *set++) > 0)
1120 {
1121 /* See PR 21156 for a reproducer. */
1122 if (i >= filedata->file_header.e_shnum)
1123 continue; /* FIXME: Should we issue an error message ? */
1124
1125 if (section_name_valid (filedata, filedata->section_headers + i)
1126 && streq (section_name (filedata, filedata->section_headers + i),
1127 name))
1128 return filedata->section_headers + i;
1129 }
1130 }
1131
1132 return find_section (filedata, name);
1133 }
1134
1135 /* Guess the relocation size commonly used by the specific machines. */
1136
1137 static bool
1138 guess_is_rela (unsigned int e_machine)
1139 {
1140 switch (e_machine)
1141 {
1142 /* Targets that use REL relocations. */
1143 case EM_386:
1144 case EM_IAMCU:
1145 case EM_960:
1146 case EM_ARM:
1147 case EM_D10V:
1148 case EM_CYGNUS_D10V:
1149 case EM_DLX:
1150 case EM_MIPS:
1151 case EM_MIPS_RS3_LE:
1152 case EM_CYGNUS_M32R:
1153 case EM_SCORE:
1154 case EM_XGATE:
1155 case EM_NFP:
1156 case EM_BPF:
1157 return false;
1158
1159 /* Targets that use RELA relocations. */
1160 case EM_68K:
1161 case EM_860:
1162 case EM_AARCH64:
1163 case EM_ADAPTEVA_EPIPHANY:
1164 case EM_ALPHA:
1165 case EM_ALTERA_NIOS2:
1166 case EM_ARC:
1167 case EM_ARC_COMPACT:
1168 case EM_ARC_COMPACT2:
1169 case EM_ARC_COMPACT3:
1170 case EM_ARC_COMPACT3_64:
1171 case EM_AVR:
1172 case EM_AVR_OLD:
1173 case EM_BLACKFIN:
1174 case EM_CR16:
1175 case EM_CRIS:
1176 case EM_CRX:
1177 case EM_CSKY:
1178 case EM_D30V:
1179 case EM_CYGNUS_D30V:
1180 case EM_FR30:
1181 case EM_FT32:
1182 case EM_CYGNUS_FR30:
1183 case EM_CYGNUS_FRV:
1184 case EM_H8S:
1185 case EM_H8_300:
1186 case EM_H8_300H:
1187 case EM_IA_64:
1188 case EM_IP2K:
1189 case EM_IP2K_OLD:
1190 case EM_IQ2000:
1191 case EM_KVX:
1192 case EM_LATTICEMICO32:
1193 case EM_M32C_OLD:
1194 case EM_M32C:
1195 case EM_M32R:
1196 case EM_MCORE:
1197 case EM_CYGNUS_MEP:
1198 case EM_METAG:
1199 case EM_MMIX:
1200 case EM_MN10200:
1201 case EM_CYGNUS_MN10200:
1202 case EM_MN10300:
1203 case EM_CYGNUS_MN10300:
1204 case EM_MOXIE:
1205 case EM_MSP430:
1206 case EM_MSP430_OLD:
1207 case EM_MT:
1208 case EM_NDS32:
1209 case EM_NIOS32:
1210 case EM_OR1K:
1211 case EM_PPC64:
1212 case EM_PPC:
1213 case EM_TI_PRU:
1214 case EM_RISCV:
1215 case EM_RL78:
1216 case EM_RX:
1217 case EM_S390:
1218 case EM_S390_OLD:
1219 case EM_SH:
1220 case EM_SPARC:
1221 case EM_SPARC32PLUS:
1222 case EM_SPARCV9:
1223 case EM_SPU:
1224 case EM_TI_C6000:
1225 case EM_TILEGX:
1226 case EM_TILEPRO:
1227 case EM_V800:
1228 case EM_V850:
1229 case EM_CYGNUS_V850:
1230 case EM_VAX:
1231 case EM_VISIUM:
1232 case EM_X86_64:
1233 case EM_L1OM:
1234 case EM_K1OM:
1235 case EM_XSTORMY16:
1236 case EM_XTENSA:
1237 case EM_XTENSA_OLD:
1238 case EM_MICROBLAZE:
1239 case EM_MICROBLAZE_OLD:
1240 case EM_WEBASSEMBLY:
1241 return true;
1242
1243 case EM_68HC05:
1244 case EM_68HC08:
1245 case EM_68HC11:
1246 case EM_68HC16:
1247 case EM_FX66:
1248 case EM_ME16:
1249 case EM_MMA:
1250 case EM_NCPU:
1251 case EM_NDR1:
1252 case EM_PCP:
1253 case EM_ST100:
1254 case EM_ST19:
1255 case EM_ST7:
1256 case EM_ST9PLUS:
1257 case EM_STARCORE:
1258 case EM_SVX:
1259 case EM_TINYJ:
1260 default:
1261 warn (_("Don't know about relocations on this machine architecture\n"));
1262 return false;
1263 }
1264 }
1265
1266 /* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1267 Returns TRUE upon success, FALSE otherwise. If successful then a
1268 pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
1269 and the number of relocs loaded is placed in *NRELASP. It is the caller's
1270 responsibility to free the allocated buffer. */
1271
1272 static bool
1273 slurp_rela_relocs (Filedata *filedata,
1274 uint64_t rel_offset,
1275 uint64_t rel_size,
1276 Elf_Internal_Rela **relasp,
1277 uint64_t *nrelasp)
1278 {
1279 Elf_Internal_Rela * relas;
1280 uint64_t nrelas;
1281 unsigned int i;
1282
1283 if (is_32bit_elf)
1284 {
1285 Elf32_External_Rela * erelas;
1286
1287 erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1288 rel_size, _("32-bit relocation data"));
1289 if (!erelas)
1290 return false;
1291
1292 nrelas = rel_size / sizeof (Elf32_External_Rela);
1293
1294 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1295 sizeof (Elf_Internal_Rela));
1296
1297 if (relas == NULL)
1298 {
1299 free (erelas);
1300 error (_("out of memory parsing relocs\n"));
1301 return false;
1302 }
1303
1304 for (i = 0; i < nrelas; i++)
1305 {
1306 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1307 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1308 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1309 }
1310
1311 free (erelas);
1312 }
1313 else
1314 {
1315 Elf64_External_Rela * erelas;
1316
1317 erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
1318 rel_size, _("64-bit relocation data"));
1319 if (!erelas)
1320 return false;
1321
1322 nrelas = rel_size / sizeof (Elf64_External_Rela);
1323
1324 relas = (Elf_Internal_Rela *) cmalloc (nrelas,
1325 sizeof (Elf_Internal_Rela));
1326
1327 if (relas == NULL)
1328 {
1329 free (erelas);
1330 error (_("out of memory parsing relocs\n"));
1331 return false;
1332 }
1333
1334 for (i = 0; i < nrelas; i++)
1335 {
1336 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
1337 relas[i].r_info = BYTE_GET (erelas[i].r_info);
1338 relas[i].r_addend = BYTE_GET_SIGNED (erelas[i].r_addend);
1339
1340 if (filedata->file_header.e_machine == EM_MIPS
1341 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1342 {
1343 /* In little-endian objects, r_info isn't really a
1344 64-bit little-endian value: it has a 32-bit
1345 little-endian symbol index followed by four
1346 individual byte fields. Reorder INFO
1347 accordingly. */
1348 uint64_t inf = relas[i].r_info;
1349 inf = (((inf & 0xffffffff) << 32)
1350 | ((inf >> 56) & 0xff)
1351 | ((inf >> 40) & 0xff00)
1352 | ((inf >> 24) & 0xff0000)
1353 | ((inf >> 8) & 0xff000000));
1354 relas[i].r_info = inf;
1355 }
1356 }
1357
1358 free (erelas);
1359 }
1360
1361 *relasp = relas;
1362 *nrelasp = nrelas;
1363 return true;
1364 }
1365
1366 /* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
1367 Returns TRUE upon success, FALSE otherwise. If successful then a
1368 pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
1369 and the number of relocs loaded is placed in *NRELSP. It is the caller's
1370 responsibility to free the allocated buffer. */
1371
1372 static bool
1373 slurp_rel_relocs (Filedata *filedata,
1374 uint64_t rel_offset,
1375 uint64_t rel_size,
1376 Elf_Internal_Rela **relsp,
1377 uint64_t *nrelsp)
1378 {
1379 Elf_Internal_Rela * rels;
1380 uint64_t nrels;
1381 unsigned int i;
1382
1383 if (is_32bit_elf)
1384 {
1385 Elf32_External_Rel * erels;
1386
1387 erels = (Elf32_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1388 rel_size, _("32-bit relocation data"));
1389 if (!erels)
1390 return false;
1391
1392 nrels = rel_size / sizeof (Elf32_External_Rel);
1393
1394 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1395
1396 if (rels == NULL)
1397 {
1398 free (erels);
1399 error (_("out of memory parsing relocs\n"));
1400 return false;
1401 }
1402
1403 for (i = 0; i < nrels; i++)
1404 {
1405 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1406 rels[i].r_info = BYTE_GET (erels[i].r_info);
1407 rels[i].r_addend = 0;
1408 }
1409
1410 free (erels);
1411 }
1412 else
1413 {
1414 Elf64_External_Rel * erels;
1415
1416 erels = (Elf64_External_Rel *) get_data (NULL, filedata, rel_offset, 1,
1417 rel_size, _("64-bit relocation data"));
1418 if (!erels)
1419 return false;
1420
1421 nrels = rel_size / sizeof (Elf64_External_Rel);
1422
1423 rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
1424
1425 if (rels == NULL)
1426 {
1427 free (erels);
1428 error (_("out of memory parsing relocs\n"));
1429 return false;
1430 }
1431
1432 for (i = 0; i < nrels; i++)
1433 {
1434 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
1435 rels[i].r_info = BYTE_GET (erels[i].r_info);
1436 rels[i].r_addend = 0;
1437
1438 if (filedata->file_header.e_machine == EM_MIPS
1439 && filedata->file_header.e_ident[EI_DATA] != ELFDATA2MSB)
1440 {
1441 /* In little-endian objects, r_info isn't really a
1442 64-bit little-endian value: it has a 32-bit
1443 little-endian symbol index followed by four
1444 individual byte fields. Reorder INFO
1445 accordingly. */
1446 uint64_t inf = rels[i].r_info;
1447 inf = (((inf & 0xffffffff) << 32)
1448 | ((inf >> 56) & 0xff)
1449 | ((inf >> 40) & 0xff00)
1450 | ((inf >> 24) & 0xff0000)
1451 | ((inf >> 8) & 0xff000000));
1452 rels[i].r_info = inf;
1453 }
1454 }
1455
1456 free (erels);
1457 }
1458
1459 *relsp = rels;
1460 *nrelsp = nrels;
1461 return true;
1462 }
1463
1464 static bool
1465 slurp_relr_relocs (Filedata *filedata,
1466 uint64_t relr_offset,
1467 uint64_t relr_size,
1468 uint64_t **relrsp,
1469 uint64_t *nrelrsp)
1470 {
1471 void *relrs;
1472 size_t size = 0, nentries, i;
1473 uint64_t base = 0, addr, entry;
1474
1475 relrs = get_data (NULL, filedata, relr_offset, 1, relr_size,
1476 _("RELR relocation data"));
1477 if (!relrs)
1478 return false;
1479
1480 if (is_32bit_elf)
1481 nentries = relr_size / sizeof (Elf32_External_Relr);
1482 else
1483 nentries = relr_size / sizeof (Elf64_External_Relr);
1484 for (i = 0; i < nentries; i++)
1485 {
1486 if (is_32bit_elf)
1487 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1488 else
1489 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1490 if ((entry & 1) == 0)
1491 size++;
1492 else
1493 while ((entry >>= 1) != 0)
1494 if ((entry & 1) == 1)
1495 size++;
1496 }
1497
1498 *relrsp = malloc (size * sizeof (**relrsp));
1499 if (*relrsp == NULL)
1500 {
1501 free (relrs);
1502 error (_("out of memory parsing relocs\n"));
1503 return false;
1504 }
1505
1506 size = 0;
1507 for (i = 0; i < nentries; i++)
1508 {
1509 const uint64_t entry_bytes = is_32bit_elf ? 4 : 8;
1510
1511 if (is_32bit_elf)
1512 entry = BYTE_GET (((Elf32_External_Relr *)relrs)[i].r_data);
1513 else
1514 entry = BYTE_GET (((Elf64_External_Relr *)relrs)[i].r_data);
1515 if ((entry & 1) == 0)
1516 {
1517 (*relrsp)[size++] = entry;
1518 base = entry + entry_bytes;
1519 }
1520 else
1521 {
1522 for (addr = base; (entry >>= 1) != 0; addr += entry_bytes)
1523 if ((entry & 1) != 0)
1524 (*relrsp)[size++] = addr;
1525 base += entry_bytes * (entry_bytes * CHAR_BIT - 1);
1526 }
1527 }
1528
1529 *nrelrsp = size;
1530 free (relrs);
1531 return true;
1532 }
1533
1534 /* Returns the reloc type extracted from the reloc info field. */
1535
1536 static unsigned int
1537 get_reloc_type (Filedata * filedata, uint64_t reloc_info)
1538 {
1539 if (is_32bit_elf)
1540 return ELF32_R_TYPE (reloc_info);
1541
1542 switch (filedata->file_header.e_machine)
1543 {
1544 case EM_MIPS:
1545 /* Note: We assume that reloc_info has already been adjusted for us. */
1546 return ELF64_MIPS_R_TYPE (reloc_info);
1547
1548 case EM_SPARCV9:
1549 return ELF64_R_TYPE_ID (reloc_info);
1550
1551 default:
1552 return ELF64_R_TYPE (reloc_info);
1553 }
1554 }
1555
1556 /* Return the symbol index extracted from the reloc info field. */
1557
1558 static uint64_t
1559 get_reloc_symindex (uint64_t reloc_info)
1560 {
1561 return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
1562 }
1563
1564 static inline bool
1565 uses_msp430x_relocs (Filedata * filedata)
1566 {
1567 return
1568 filedata->file_header.e_machine == EM_MSP430 /* Paranoia. */
1569 /* GCC uses osabi == ELFOSBI_STANDALONE. */
1570 && (((filedata->file_header.e_flags & EF_MSP430_MACH) == E_MSP430_MACH_MSP430X)
1571 /* TI compiler uses ELFOSABI_NONE. */
1572 || (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_NONE));
1573 }
1574
1575 /* Display the contents of the relocation data found at the specified
1576 offset. */
1577
1578 static bool
1579 dump_relocations (Filedata *filedata,
1580 uint64_t rel_offset,
1581 uint64_t rel_size,
1582 Elf_Internal_Sym *symtab,
1583 uint64_t nsyms,
1584 char *strtab,
1585 uint64_t strtablen,
1586 relocation_type rel_type,
1587 bool is_dynsym)
1588 {
1589 size_t i;
1590 Elf_Internal_Rela * rels;
1591 bool res = true;
1592
1593 if (rel_type == reltype_unknown)
1594 rel_type = guess_is_rela (filedata->file_header.e_machine) ? reltype_rela : reltype_rel;
1595
1596 if (rel_type == reltype_rela)
1597 {
1598 if (!slurp_rela_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1599 return false;
1600 }
1601 else if (rel_type == reltype_rel)
1602 {
1603 if (!slurp_rel_relocs (filedata, rel_offset, rel_size, &rels, &rel_size))
1604 return false;
1605 }
1606 else if (rel_type == reltype_relr)
1607 {
1608 uint64_t * relrs;
1609 const char *format
1610 = is_32bit_elf ? "%08" PRIx64 "\n" : "%016" PRIx64 "\n";
1611
1612 if (!slurp_relr_relocs (filedata, rel_offset, rel_size, &relrs,
1613 &rel_size))
1614 return false;
1615
1616 printf (ngettext (" %" PRIu64 " offset\n",
1617 " %" PRIu64 " offsets\n", rel_size),
1618 rel_size);
1619 for (i = 0; i < rel_size; i++)
1620 printf (format, relrs[i]);
1621 free (relrs);
1622 return true;
1623 }
1624
1625 if (is_32bit_elf)
1626 {
1627 if (rel_type == reltype_rela)
1628 {
1629 if (do_wide)
1630 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
1631 else
1632 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
1633 }
1634 else
1635 {
1636 if (do_wide)
1637 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
1638 else
1639 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
1640 }
1641 }
1642 else
1643 {
1644 if (rel_type == reltype_rela)
1645 {
1646 if (do_wide)
1647 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
1648 else
1649 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1650 }
1651 else
1652 {
1653 if (do_wide)
1654 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
1655 else
1656 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1657 }
1658 }
1659
1660 for (i = 0; i < rel_size; i++)
1661 {
1662 const char * rtype;
1663 uint64_t offset;
1664 uint64_t inf;
1665 uint64_t symtab_index;
1666 uint64_t type;
1667
1668 offset = rels[i].r_offset;
1669 inf = rels[i].r_info;
1670
1671 type = get_reloc_type (filedata, inf);
1672 symtab_index = get_reloc_symindex (inf);
1673
1674 if (is_32bit_elf)
1675 {
1676 printf ("%8.8lx %8.8lx ",
1677 (unsigned long) offset & 0xffffffff,
1678 (unsigned long) inf & 0xffffffff);
1679 }
1680 else
1681 {
1682 printf (do_wide
1683 ? "%16.16" PRIx64 " %16.16" PRIx64 " "
1684 : "%12.12" PRIx64 " %12.12" PRIx64 " ",
1685 offset, inf);
1686 }
1687
1688 switch (filedata->file_header.e_machine)
1689 {
1690 default:
1691 rtype = NULL;
1692 break;
1693
1694 case EM_AARCH64:
1695 rtype = elf_aarch64_reloc_type (type);
1696 break;
1697
1698 case EM_M32R:
1699 case EM_CYGNUS_M32R:
1700 rtype = elf_m32r_reloc_type (type);
1701 break;
1702
1703 case EM_386:
1704 case EM_IAMCU:
1705 rtype = elf_i386_reloc_type (type);
1706 break;
1707
1708 case EM_68HC11:
1709 case EM_68HC12:
1710 rtype = elf_m68hc11_reloc_type (type);
1711 break;
1712
1713 case EM_S12Z:
1714 rtype = elf_s12z_reloc_type (type);
1715 break;
1716
1717 case EM_68K:
1718 rtype = elf_m68k_reloc_type (type);
1719 break;
1720
1721 case EM_960:
1722 rtype = elf_i960_reloc_type (type);
1723 break;
1724
1725 case EM_AVR:
1726 case EM_AVR_OLD:
1727 rtype = elf_avr_reloc_type (type);
1728 break;
1729
1730 case EM_OLD_SPARCV9:
1731 case EM_SPARC32PLUS:
1732 case EM_SPARCV9:
1733 case EM_SPARC:
1734 rtype = elf_sparc_reloc_type (type);
1735 break;
1736
1737 case EM_SPU:
1738 rtype = elf_spu_reloc_type (type);
1739 break;
1740
1741 case EM_V800:
1742 rtype = v800_reloc_type (type);
1743 break;
1744 case EM_V850:
1745 case EM_CYGNUS_V850:
1746 rtype = v850_reloc_type (type);
1747 break;
1748
1749 case EM_D10V:
1750 case EM_CYGNUS_D10V:
1751 rtype = elf_d10v_reloc_type (type);
1752 break;
1753
1754 case EM_D30V:
1755 case EM_CYGNUS_D30V:
1756 rtype = elf_d30v_reloc_type (type);
1757 break;
1758
1759 case EM_DLX:
1760 rtype = elf_dlx_reloc_type (type);
1761 break;
1762
1763 case EM_SH:
1764 rtype = elf_sh_reloc_type (type);
1765 break;
1766
1767 case EM_MN10300:
1768 case EM_CYGNUS_MN10300:
1769 rtype = elf_mn10300_reloc_type (type);
1770 break;
1771
1772 case EM_MN10200:
1773 case EM_CYGNUS_MN10200:
1774 rtype = elf_mn10200_reloc_type (type);
1775 break;
1776
1777 case EM_FR30:
1778 case EM_CYGNUS_FR30:
1779 rtype = elf_fr30_reloc_type (type);
1780 break;
1781
1782 case EM_CYGNUS_FRV:
1783 rtype = elf_frv_reloc_type (type);
1784 break;
1785
1786 case EM_CSKY:
1787 rtype = elf_csky_reloc_type (type);
1788 break;
1789
1790 case EM_FT32:
1791 rtype = elf_ft32_reloc_type (type);
1792 break;
1793
1794 case EM_MCORE:
1795 rtype = elf_mcore_reloc_type (type);
1796 break;
1797
1798 case EM_MMIX:
1799 rtype = elf_mmix_reloc_type (type);
1800 break;
1801
1802 case EM_MOXIE:
1803 rtype = elf_moxie_reloc_type (type);
1804 break;
1805
1806 case EM_MSP430:
1807 if (uses_msp430x_relocs (filedata))
1808 {
1809 rtype = elf_msp430x_reloc_type (type);
1810 break;
1811 }
1812 /* Fall through. */
1813 case EM_MSP430_OLD:
1814 rtype = elf_msp430_reloc_type (type);
1815 break;
1816
1817 case EM_NDS32:
1818 rtype = elf_nds32_reloc_type (type);
1819 break;
1820
1821 case EM_PPC:
1822 rtype = elf_ppc_reloc_type (type);
1823 break;
1824
1825 case EM_PPC64:
1826 rtype = elf_ppc64_reloc_type (type);
1827 break;
1828
1829 case EM_MIPS:
1830 case EM_MIPS_RS3_LE:
1831 rtype = elf_mips_reloc_type (type);
1832 break;
1833
1834 case EM_RISCV:
1835 rtype = elf_riscv_reloc_type (type);
1836 break;
1837
1838 case EM_ALPHA:
1839 rtype = elf_alpha_reloc_type (type);
1840 break;
1841
1842 case EM_ARM:
1843 rtype = elf_arm_reloc_type (type);
1844 break;
1845
1846 case EM_ARC:
1847 case EM_ARC_COMPACT:
1848 case EM_ARC_COMPACT2:
1849 case EM_ARC_COMPACT3:
1850 case EM_ARC_COMPACT3_64:
1851 rtype = elf_arc_reloc_type (type);
1852 break;
1853
1854 case EM_PARISC:
1855 rtype = elf_hppa_reloc_type (type);
1856 break;
1857
1858 case EM_H8_300:
1859 case EM_H8_300H:
1860 case EM_H8S:
1861 rtype = elf_h8_reloc_type (type);
1862 break;
1863
1864 case EM_OR1K:
1865 rtype = elf_or1k_reloc_type (type);
1866 break;
1867
1868 case EM_PJ:
1869 case EM_PJ_OLD:
1870 rtype = elf_pj_reloc_type (type);
1871 break;
1872 case EM_IA_64:
1873 rtype = elf_ia64_reloc_type (type);
1874 break;
1875
1876 case EM_KVX:
1877 rtype = elf_kvx_reloc_type (type);
1878 break;
1879
1880 case EM_CRIS:
1881 rtype = elf_cris_reloc_type (type);
1882 break;
1883
1884 case EM_860:
1885 rtype = elf_i860_reloc_type (type);
1886 break;
1887
1888 case EM_X86_64:
1889 case EM_L1OM:
1890 case EM_K1OM:
1891 rtype = elf_x86_64_reloc_type (type);
1892 break;
1893
1894 case EM_S370:
1895 rtype = i370_reloc_type (type);
1896 break;
1897
1898 case EM_S390_OLD:
1899 case EM_S390:
1900 rtype = elf_s390_reloc_type (type);
1901 break;
1902
1903 case EM_SCORE:
1904 rtype = elf_score_reloc_type (type);
1905 break;
1906
1907 case EM_XSTORMY16:
1908 rtype = elf_xstormy16_reloc_type (type);
1909 break;
1910
1911 case EM_CRX:
1912 rtype = elf_crx_reloc_type (type);
1913 break;
1914
1915 case EM_VAX:
1916 rtype = elf_vax_reloc_type (type);
1917 break;
1918
1919 case EM_VISIUM:
1920 rtype = elf_visium_reloc_type (type);
1921 break;
1922
1923 case EM_BPF:
1924 rtype = elf_bpf_reloc_type (type);
1925 break;
1926
1927 case EM_ADAPTEVA_EPIPHANY:
1928 rtype = elf_epiphany_reloc_type (type);
1929 break;
1930
1931 case EM_IP2K:
1932 case EM_IP2K_OLD:
1933 rtype = elf_ip2k_reloc_type (type);
1934 break;
1935
1936 case EM_IQ2000:
1937 rtype = elf_iq2000_reloc_type (type);
1938 break;
1939
1940 case EM_XTENSA_OLD:
1941 case EM_XTENSA:
1942 rtype = elf_xtensa_reloc_type (type);
1943 break;
1944
1945 case EM_LATTICEMICO32:
1946 rtype = elf_lm32_reloc_type (type);
1947 break;
1948
1949 case EM_M32C_OLD:
1950 case EM_M32C:
1951 rtype = elf_m32c_reloc_type (type);
1952 break;
1953
1954 case EM_MT:
1955 rtype = elf_mt_reloc_type (type);
1956 break;
1957
1958 case EM_BLACKFIN:
1959 rtype = elf_bfin_reloc_type (type);
1960 break;
1961
1962 case EM_CYGNUS_MEP:
1963 rtype = elf_mep_reloc_type (type);
1964 break;
1965
1966 case EM_CR16:
1967 rtype = elf_cr16_reloc_type (type);
1968 break;
1969
1970 case EM_MICROBLAZE:
1971 case EM_MICROBLAZE_OLD:
1972 rtype = elf_microblaze_reloc_type (type);
1973 break;
1974
1975 case EM_RL78:
1976 rtype = elf_rl78_reloc_type (type);
1977 break;
1978
1979 case EM_RX:
1980 rtype = elf_rx_reloc_type (type);
1981 break;
1982
1983 case EM_METAG:
1984 rtype = elf_metag_reloc_type (type);
1985 break;
1986
1987 case EM_TI_C6000:
1988 rtype = elf_tic6x_reloc_type (type);
1989 break;
1990
1991 case EM_TILEGX:
1992 rtype = elf_tilegx_reloc_type (type);
1993 break;
1994
1995 case EM_TILEPRO:
1996 rtype = elf_tilepro_reloc_type (type);
1997 break;
1998
1999 case EM_WEBASSEMBLY:
2000 rtype = elf_wasm32_reloc_type (type);
2001 break;
2002
2003 case EM_XGATE:
2004 rtype = elf_xgate_reloc_type (type);
2005 break;
2006
2007 case EM_ALTERA_NIOS2:
2008 rtype = elf_nios2_reloc_type (type);
2009 break;
2010
2011 case EM_TI_PRU:
2012 rtype = elf_pru_reloc_type (type);
2013 break;
2014
2015 case EM_NFP:
2016 if (EF_NFP_MACH (filedata->file_header.e_flags) == E_NFP_MACH_3200)
2017 rtype = elf_nfp3200_reloc_type (type);
2018 else
2019 rtype = elf_nfp_reloc_type (type);
2020 break;
2021
2022 case EM_Z80:
2023 rtype = elf_z80_reloc_type (type);
2024 break;
2025
2026 case EM_LOONGARCH:
2027 rtype = elf_loongarch_reloc_type (type);
2028 break;
2029
2030 case EM_AMDGPU:
2031 rtype = elf_amdgpu_reloc_type (type);
2032 break;
2033 }
2034
2035 if (rtype == NULL)
2036 printf (_("unrecognized: %-7lx"), (unsigned long) type & 0xffffffff);
2037 else
2038 printf (do_wide ? "%-22s" : "%-17.17s", rtype);
2039
2040 if (filedata->file_header.e_machine == EM_ALPHA
2041 && rtype != NULL
2042 && streq (rtype, "R_ALPHA_LITUSE")
2043 && rel_type == reltype_rela)
2044 {
2045 switch (rels[i].r_addend)
2046 {
2047 case LITUSE_ALPHA_ADDR: rtype = "ADDR"; break;
2048 case LITUSE_ALPHA_BASE: rtype = "BASE"; break;
2049 case LITUSE_ALPHA_BYTOFF: rtype = "BYTOFF"; break;
2050 case LITUSE_ALPHA_JSR: rtype = "JSR"; break;
2051 case LITUSE_ALPHA_TLSGD: rtype = "TLSGD"; break;
2052 case LITUSE_ALPHA_TLSLDM: rtype = "TLSLDM"; break;
2053 case LITUSE_ALPHA_JSRDIRECT: rtype = "JSRDIRECT"; break;
2054 default: rtype = NULL;
2055 }
2056
2057 if (rtype)
2058 printf (" (%s)", rtype);
2059 else
2060 {
2061 putchar (' ');
2062 printf (_("<unknown addend: %" PRIx64 ">"),
2063 rels[i].r_addend);
2064 res = false;
2065 }
2066 }
2067 else if (symtab_index)
2068 {
2069 if (symtab == NULL || symtab_index >= nsyms)
2070 {
2071 error (_(" bad symbol index: %08lx in reloc\n"),
2072 (unsigned long) symtab_index);
2073 res = false;
2074 }
2075 else
2076 {
2077 Elf_Internal_Sym * psym;
2078 const char * version_string;
2079 enum versioned_symbol_info sym_info;
2080 unsigned short vna_other;
2081
2082 psym = symtab + symtab_index;
2083
2084 version_string
2085 = get_symbol_version_string (filedata, is_dynsym,
2086 strtab, strtablen,
2087 symtab_index,
2088 psym,
2089 &sym_info,
2090 &vna_other);
2091
2092 printf (" ");
2093
2094 if (ELF_ST_TYPE (psym->st_info) == STT_GNU_IFUNC)
2095 {
2096 const char * name;
2097 unsigned int len;
2098 unsigned int width = is_32bit_elf ? 8 : 14;
2099
2100 /* Relocations against GNU_IFUNC symbols do not use the value
2101 of the symbol as the address to relocate against. Instead
2102 they invoke the function named by the symbol and use its
2103 result as the address for relocation.
2104
2105 To indicate this to the user, do not display the value of
2106 the symbol in the "Symbols's Value" field. Instead show
2107 its name followed by () as a hint that the symbol is
2108 invoked. */
2109
2110 if (strtab == NULL
2111 || psym->st_name == 0
2112 || psym->st_name >= strtablen)
2113 name = "??";
2114 else
2115 name = strtab + psym->st_name;
2116
2117 len = print_symbol_name (width, name);
2118 if (version_string)
2119 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2120 version_string);
2121 printf ("()%-*s", len <= width ? (width + 1) - len : 1, " ");
2122 }
2123 else
2124 {
2125 print_vma (psym->st_value, LONG_HEX);
2126
2127 printf (is_32bit_elf ? " " : " ");
2128 }
2129
2130 if (psym->st_name == 0)
2131 {
2132 const char * sec_name = "<null>";
2133
2134 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
2135 sec_name = printable_section_name_from_index
2136 (filedata, psym->st_shndx, NULL);
2137
2138 print_symbol_name (22, sec_name);
2139 }
2140 else if (strtab == NULL)
2141 printf (_("<string table index: %3ld>"), psym->st_name);
2142 else if (psym->st_name >= strtablen)
2143 {
2144 error (_("<corrupt string table index: %3ld>\n"),
2145 psym->st_name);
2146 res = false;
2147 }
2148 else
2149 {
2150 print_symbol_name (22, strtab + psym->st_name);
2151 if (version_string)
2152 printf (sym_info == symbol_public ? "@@%s" : "@%s",
2153 version_string);
2154 }
2155
2156 if (rel_type == reltype_rela)
2157 {
2158 uint64_t off = rels[i].r_addend;
2159
2160 if ((int64_t) off < 0)
2161 printf (" - %" PRIx64, -off);
2162 else
2163 printf (" + %" PRIx64, off);
2164 }
2165 }
2166 }
2167 else if (rel_type == reltype_rela)
2168 {
2169 uint64_t off = rels[i].r_addend;
2170
2171 printf ("%*c", is_32bit_elf ? 12 : 20, ' ');
2172 if ((int64_t) off < 0)
2173 printf ("-%" PRIx64, -off);
2174 else
2175 printf ("%" PRIx64, off);
2176 }
2177
2178 if (filedata->file_header.e_machine == EM_SPARCV9
2179 && rtype != NULL
2180 && streq (rtype, "R_SPARC_OLO10"))
2181 printf (" + %" PRIx64, ELF64_R_TYPE_DATA (inf));
2182
2183 putchar ('\n');
2184
2185 if (! is_32bit_elf && filedata->file_header.e_machine == EM_MIPS)
2186 {
2187 uint64_t type2 = ELF64_MIPS_R_TYPE2 (inf);
2188 uint64_t type3 = ELF64_MIPS_R_TYPE3 (inf);
2189 const char * rtype2 = elf_mips_reloc_type (type2);
2190 const char * rtype3 = elf_mips_reloc_type (type3);
2191
2192 printf (" Type2: ");
2193
2194 if (rtype2 == NULL)
2195 printf (_("unrecognized: %-7lx"),
2196 (unsigned long) type2 & 0xffffffff);
2197 else
2198 printf ("%-17.17s", rtype2);
2199
2200 printf ("\n Type3: ");
2201
2202 if (rtype3 == NULL)
2203 printf (_("unrecognized: %-7lx"),
2204 (unsigned long) type3 & 0xffffffff);
2205 else
2206 printf ("%-17.17s", rtype3);
2207
2208 putchar ('\n');
2209 }
2210 }
2211
2212 free (rels);
2213
2214 return res;
2215 }
2216
2217 static const char *
2218 get_aarch64_dynamic_type (unsigned long type)
2219 {
2220 switch (type)
2221 {
2222 case DT_AARCH64_BTI_PLT: return "AARCH64_BTI_PLT";
2223 case DT_AARCH64_PAC_PLT: return "AARCH64_PAC_PLT";
2224 case DT_AARCH64_VARIANT_PCS: return "AARCH64_VARIANT_PCS";
2225 default:
2226 return NULL;
2227 }
2228 }
2229
2230 static const char *
2231 get_mips_dynamic_type (unsigned long type)
2232 {
2233 switch (type)
2234 {
2235 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
2236 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
2237 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
2238 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
2239 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
2240 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
2241 case DT_MIPS_MSYM: return "MIPS_MSYM";
2242 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2243 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2244 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
2245 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
2246 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
2247 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
2248 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
2249 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
2250 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
2251 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
2252 case DT_MIPS_RLD_MAP_REL: return "MIPS_RLD_MAP_REL";
2253 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
2254 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
2255 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
2256 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
2257 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
2258 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
2259 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
2260 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
2261 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
2262 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
2263 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
2264 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
2265 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2266 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
2267 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
2268 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
2269 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
2270 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2271 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
2272 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
2273 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
2274 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
2275 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
2276 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
2277 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
2278 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
2279 case DT_MIPS_PLTGOT: return "MIPS_PLTGOT";
2280 case DT_MIPS_RWPLT: return "MIPS_RWPLT";
2281 case DT_MIPS_XHASH: return "MIPS_XHASH";
2282 default:
2283 return NULL;
2284 }
2285 }
2286
2287 static const char *
2288 get_sparc64_dynamic_type (unsigned long type)
2289 {
2290 switch (type)
2291 {
2292 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
2293 default:
2294 return NULL;
2295 }
2296 }
2297
2298 static const char *
2299 get_ppc_dynamic_type (unsigned long type)
2300 {
2301 switch (type)
2302 {
2303 case DT_PPC_GOT: return "PPC_GOT";
2304 case DT_PPC_OPT: return "PPC_OPT";
2305 default:
2306 return NULL;
2307 }
2308 }
2309
2310 static const char *
2311 get_ppc64_dynamic_type (unsigned long type)
2312 {
2313 switch (type)
2314 {
2315 case DT_PPC64_GLINK: return "PPC64_GLINK";
2316 case DT_PPC64_OPD: return "PPC64_OPD";
2317 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
2318 case DT_PPC64_OPT: return "PPC64_OPT";
2319 default:
2320 return NULL;
2321 }
2322 }
2323
2324 static const char *
2325 get_parisc_dynamic_type (unsigned long type)
2326 {
2327 switch (type)
2328 {
2329 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
2330 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
2331 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
2332 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
2333 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
2334 case DT_HP_PREINIT: return "HP_PREINIT";
2335 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
2336 case DT_HP_NEEDED: return "HP_NEEDED";
2337 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
2338 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
2339 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
2340 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
2341 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
2342 case DT_HP_EPLTREL: return "HP_GST_EPLTREL";
2343 case DT_HP_EPLTRELSZ: return "HP_GST_EPLTRELSZ";
2344 case DT_HP_FILTERED: return "HP_FILTERED";
2345 case DT_HP_FILTER_TLS: return "HP_FILTER_TLS";
2346 case DT_HP_COMPAT_FILTERED: return "HP_COMPAT_FILTERED";
2347 case DT_HP_LAZYLOAD: return "HP_LAZYLOAD";
2348 case DT_HP_BIND_NOW_COUNT: return "HP_BIND_NOW_COUNT";
2349 case DT_PLT: return "PLT";
2350 case DT_PLT_SIZE: return "PLT_SIZE";
2351 case DT_DLT: return "DLT";
2352 case DT_DLT_SIZE: return "DLT_SIZE";
2353 default:
2354 return NULL;
2355 }
2356 }
2357
2358 static const char *
2359 get_ia64_dynamic_type (unsigned long type)
2360 {
2361 switch (type)
2362 {
2363 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
2364 case DT_IA_64_VMS_SUBTYPE: return "VMS_SUBTYPE";
2365 case DT_IA_64_VMS_IMGIOCNT: return "VMS_IMGIOCNT";
2366 case DT_IA_64_VMS_LNKFLAGS: return "VMS_LNKFLAGS";
2367 case DT_IA_64_VMS_VIR_MEM_BLK_SIZ: return "VMS_VIR_MEM_BLK_SIZ";
2368 case DT_IA_64_VMS_IDENT: return "VMS_IDENT";
2369 case DT_IA_64_VMS_NEEDED_IDENT: return "VMS_NEEDED_IDENT";
2370 case DT_IA_64_VMS_IMG_RELA_CNT: return "VMS_IMG_RELA_CNT";
2371 case DT_IA_64_VMS_SEG_RELA_CNT: return "VMS_SEG_RELA_CNT";
2372 case DT_IA_64_VMS_FIXUP_RELA_CNT: return "VMS_FIXUP_RELA_CNT";
2373 case DT_IA_64_VMS_FIXUP_NEEDED: return "VMS_FIXUP_NEEDED";
2374 case DT_IA_64_VMS_SYMVEC_CNT: return "VMS_SYMVEC_CNT";
2375 case DT_IA_64_VMS_XLATED: return "VMS_XLATED";
2376 case DT_IA_64_VMS_STACKSIZE: return "VMS_STACKSIZE";
2377 case DT_IA_64_VMS_UNWINDSZ: return "VMS_UNWINDSZ";
2378 case DT_IA_64_VMS_UNWIND_CODSEG: return "VMS_UNWIND_CODSEG";
2379 case DT_IA_64_VMS_UNWIND_INFOSEG: return "VMS_UNWIND_INFOSEG";
2380 case DT_IA_64_VMS_LINKTIME: return "VMS_LINKTIME";
2381 case DT_IA_64_VMS_SEG_NO: return "VMS_SEG_NO";
2382 case DT_IA_64_VMS_SYMVEC_OFFSET: return "VMS_SYMVEC_OFFSET";
2383 case DT_IA_64_VMS_SYMVEC_SEG: return "VMS_SYMVEC_SEG";
2384 case DT_IA_64_VMS_UNWIND_OFFSET: return "VMS_UNWIND_OFFSET";
2385 case DT_IA_64_VMS_UNWIND_SEG: return "VMS_UNWIND_SEG";
2386 case DT_IA_64_VMS_STRTAB_OFFSET: return "VMS_STRTAB_OFFSET";
2387 case DT_IA_64_VMS_SYSVER_OFFSET: return "VMS_SYSVER_OFFSET";
2388 case DT_IA_64_VMS_IMG_RELA_OFF: return "VMS_IMG_RELA_OFF";
2389 case DT_IA_64_VMS_SEG_RELA_OFF: return "VMS_SEG_RELA_OFF";
2390 case DT_IA_64_VMS_FIXUP_RELA_OFF: return "VMS_FIXUP_RELA_OFF";
2391 case DT_IA_64_VMS_PLTGOT_OFFSET: return "VMS_PLTGOT_OFFSET";
2392 case DT_IA_64_VMS_PLTGOT_SEG: return "VMS_PLTGOT_SEG";
2393 case DT_IA_64_VMS_FPMODE: return "VMS_FPMODE";
2394 default:
2395 return NULL;
2396 }
2397 }
2398
2399 static const char *
2400 get_solaris_section_type (unsigned long type)
2401 {
2402 switch (type)
2403 {
2404 case 0x6fffffee: return "SUNW_ancillary";
2405 case 0x6fffffef: return "SUNW_capchain";
2406 case 0x6ffffff0: return "SUNW_capinfo";
2407 case 0x6ffffff1: return "SUNW_symsort";
2408 case 0x6ffffff2: return "SUNW_tlssort";
2409 case 0x6ffffff3: return "SUNW_LDYNSYM";
2410 case 0x6ffffff4: return "SUNW_dof";
2411 case 0x6ffffff5: return "SUNW_cap";
2412 case 0x6ffffff6: return "SUNW_SIGNATURE";
2413 case 0x6ffffff7: return "SUNW_ANNOTATE";
2414 case 0x6ffffff8: return "SUNW_DEBUGSTR";
2415 case 0x6ffffff9: return "SUNW_DEBUG";
2416 case 0x6ffffffa: return "SUNW_move";
2417 case 0x6ffffffb: return "SUNW_COMDAT";
2418 case 0x6ffffffc: return "SUNW_syminfo";
2419 case 0x6ffffffd: return "SUNW_verdef";
2420 case 0x6ffffffe: return "SUNW_verneed";
2421 case 0x6fffffff: return "SUNW_versym";
2422 case 0x70000000: return "SPARC_GOTDATA";
2423 default: return NULL;
2424 }
2425 }
2426
2427 static const char *
2428 get_alpha_dynamic_type (unsigned long type)
2429 {
2430 switch (type)
2431 {
2432 case DT_ALPHA_PLTRO: return "ALPHA_PLTRO";
2433 default: return NULL;
2434 }
2435 }
2436
2437 static const char *
2438 get_score_dynamic_type (unsigned long type)
2439 {
2440 switch (type)
2441 {
2442 case DT_SCORE_BASE_ADDRESS: return "SCORE_BASE_ADDRESS";
2443 case DT_SCORE_LOCAL_GOTNO: return "SCORE_LOCAL_GOTNO";
2444 case DT_SCORE_SYMTABNO: return "SCORE_SYMTABNO";
2445 case DT_SCORE_GOTSYM: return "SCORE_GOTSYM";
2446 case DT_SCORE_UNREFEXTNO: return "SCORE_UNREFEXTNO";
2447 case DT_SCORE_HIPAGENO: return "SCORE_HIPAGENO";
2448 default: return NULL;
2449 }
2450 }
2451
2452 static const char *
2453 get_tic6x_dynamic_type (unsigned long type)
2454 {
2455 switch (type)
2456 {
2457 case DT_C6000_GSYM_OFFSET: return "C6000_GSYM_OFFSET";
2458 case DT_C6000_GSTR_OFFSET: return "C6000_GSTR_OFFSET";
2459 case DT_C6000_DSBT_BASE: return "C6000_DSBT_BASE";
2460 case DT_C6000_DSBT_SIZE: return "C6000_DSBT_SIZE";
2461 case DT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
2462 case DT_C6000_DSBT_INDEX: return "C6000_DSBT_INDEX";
2463 default: return NULL;
2464 }
2465 }
2466
2467 static const char *
2468 get_nios2_dynamic_type (unsigned long type)
2469 {
2470 switch (type)
2471 {
2472 case DT_NIOS2_GP: return "NIOS2_GP";
2473 default: return NULL;
2474 }
2475 }
2476
2477 static const char *
2478 get_solaris_dynamic_type (unsigned long type)
2479 {
2480 switch (type)
2481 {
2482 case 0x6000000d: return "SUNW_AUXILIARY";
2483 case 0x6000000e: return "SUNW_RTLDINF";
2484 case 0x6000000f: return "SUNW_FILTER";
2485 case 0x60000010: return "SUNW_CAP";
2486 case 0x60000011: return "SUNW_SYMTAB";
2487 case 0x60000012: return "SUNW_SYMSZ";
2488 case 0x60000013: return "SUNW_SORTENT";
2489 case 0x60000014: return "SUNW_SYMSORT";
2490 case 0x60000015: return "SUNW_SYMSORTSZ";
2491 case 0x60000016: return "SUNW_TLSSORT";
2492 case 0x60000017: return "SUNW_TLSSORTSZ";
2493 case 0x60000018: return "SUNW_CAPINFO";
2494 case 0x60000019: return "SUNW_STRPAD";
2495 case 0x6000001a: return "SUNW_CAPCHAIN";
2496 case 0x6000001b: return "SUNW_LDMACH";
2497 case 0x6000001d: return "SUNW_CAPCHAINENT";
2498 case 0x6000001f: return "SUNW_CAPCHAINSZ";
2499 case 0x60000021: return "SUNW_PARENT";
2500 case 0x60000023: return "SUNW_ASLR";
2501 case 0x60000025: return "SUNW_RELAX";
2502 case 0x60000029: return "SUNW_NXHEAP";
2503 case 0x6000002b: return "SUNW_NXSTACK";
2504
2505 case 0x70000001: return "SPARC_REGISTER";
2506 case 0x7ffffffd: return "AUXILIARY";
2507 case 0x7ffffffe: return "USED";
2508 case 0x7fffffff: return "FILTER";
2509
2510 default: return NULL;
2511 }
2512 }
2513
2514 static const char *
2515 get_riscv_dynamic_type (unsigned long type)
2516 {
2517 switch (type)
2518 {
2519 case DT_RISCV_VARIANT_CC: return "RISCV_VARIANT_CC";
2520 default:
2521 return NULL;
2522 }
2523 }
2524
2525 static const char *
2526 get_x86_64_dynamic_type (unsigned long type)
2527 {
2528 switch (type)
2529 {
2530 case DT_X86_64_PLT:
2531 return "DT_X86_64_PLT";
2532 case DT_X86_64_PLTSZ:
2533 return "DT_X86_64_PLTSZ";
2534 case DT_X86_64_PLTENT:
2535 return "DT_X86_64_PLTENT";
2536 default:
2537 return NULL;
2538 }
2539 }
2540
2541 static const char *
2542 get_dynamic_type (Filedata * filedata, unsigned long type)
2543 {
2544 static char buff[64];
2545
2546 switch (type)
2547 {
2548 case DT_NULL: return "NULL";
2549 case DT_NEEDED: return "NEEDED";
2550 case DT_PLTRELSZ: return "PLTRELSZ";
2551 case DT_PLTGOT: return "PLTGOT";
2552 case DT_HASH: return "HASH";
2553 case DT_STRTAB: return "STRTAB";
2554 case DT_SYMTAB: return "SYMTAB";
2555 case DT_RELA: return "RELA";
2556 case DT_RELASZ: return "RELASZ";
2557 case DT_RELAENT: return "RELAENT";
2558 case DT_STRSZ: return "STRSZ";
2559 case DT_SYMENT: return "SYMENT";
2560 case DT_INIT: return "INIT";
2561 case DT_FINI: return "FINI";
2562 case DT_SONAME: return "SONAME";
2563 case DT_RPATH: return "RPATH";
2564 case DT_SYMBOLIC: return "SYMBOLIC";
2565 case DT_REL: return "REL";
2566 case DT_RELSZ: return "RELSZ";
2567 case DT_RELENT: return "RELENT";
2568 case DT_RELR: return "RELR";
2569 case DT_RELRSZ: return "RELRSZ";
2570 case DT_RELRENT: return "RELRENT";
2571 case DT_PLTREL: return "PLTREL";
2572 case DT_DEBUG: return "DEBUG";
2573 case DT_TEXTREL: return "TEXTREL";
2574 case DT_JMPREL: return "JMPREL";
2575 case DT_BIND_NOW: return "BIND_NOW";
2576 case DT_INIT_ARRAY: return "INIT_ARRAY";
2577 case DT_FINI_ARRAY: return "FINI_ARRAY";
2578 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
2579 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
2580 case DT_RUNPATH: return "RUNPATH";
2581 case DT_FLAGS: return "FLAGS";
2582
2583 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2584 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
2585 case DT_SYMTAB_SHNDX: return "SYMTAB_SHNDX";
2586
2587 case DT_CHECKSUM: return "CHECKSUM";
2588 case DT_PLTPADSZ: return "PLTPADSZ";
2589 case DT_MOVEENT: return "MOVEENT";
2590 case DT_MOVESZ: return "MOVESZ";
2591 case DT_FEATURE: return "FEATURE";
2592 case DT_POSFLAG_1: return "POSFLAG_1";
2593 case DT_SYMINSZ: return "SYMINSZ";
2594 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
2595
2596 case DT_ADDRRNGLO: return "ADDRRNGLO";
2597 case DT_CONFIG: return "CONFIG";
2598 case DT_DEPAUDIT: return "DEPAUDIT";
2599 case DT_AUDIT: return "AUDIT";
2600 case DT_PLTPAD: return "PLTPAD";
2601 case DT_MOVETAB: return "MOVETAB";
2602 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
2603
2604 case DT_VERSYM: return "VERSYM";
2605
2606 case DT_TLSDESC_GOT: return "TLSDESC_GOT";
2607 case DT_TLSDESC_PLT: return "TLSDESC_PLT";
2608 case DT_RELACOUNT: return "RELACOUNT";
2609 case DT_RELCOUNT: return "RELCOUNT";
2610 case DT_FLAGS_1: return "FLAGS_1";
2611 case DT_VERDEF: return "VERDEF";
2612 case DT_VERDEFNUM: return "VERDEFNUM";
2613 case DT_VERNEED: return "VERNEED";
2614 case DT_VERNEEDNUM: return "VERNEEDNUM";
2615
2616 case DT_AUXILIARY: return "AUXILIARY";
2617 case DT_USED: return "USED";
2618 case DT_FILTER: return "FILTER";
2619
2620 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
2621 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
2622 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
2623 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
2624 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
2625 case DT_GNU_HASH: return "GNU_HASH";
2626 case DT_GNU_FLAGS_1: return "GNU_FLAGS_1";
2627
2628 default:
2629 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
2630 {
2631 const char * result;
2632
2633 switch (filedata->file_header.e_machine)
2634 {
2635 case EM_AARCH64:
2636 result = get_aarch64_dynamic_type (type);
2637 break;
2638 case EM_MIPS:
2639 case EM_MIPS_RS3_LE:
2640 result = get_mips_dynamic_type (type);
2641 break;
2642 case EM_SPARCV9:
2643 result = get_sparc64_dynamic_type (type);
2644 break;
2645 case EM_PPC:
2646 result = get_ppc_dynamic_type (type);
2647 break;
2648 case EM_PPC64:
2649 result = get_ppc64_dynamic_type (type);
2650 break;
2651 case EM_IA_64:
2652 result = get_ia64_dynamic_type (type);
2653 break;
2654 case EM_ALPHA:
2655 result = get_alpha_dynamic_type (type);
2656 break;
2657 case EM_SCORE:
2658 result = get_score_dynamic_type (type);
2659 break;
2660 case EM_TI_C6000:
2661 result = get_tic6x_dynamic_type (type);
2662 break;
2663 case EM_ALTERA_NIOS2:
2664 result = get_nios2_dynamic_type (type);
2665 break;
2666 case EM_RISCV:
2667 result = get_riscv_dynamic_type (type);
2668 break;
2669 case EM_X86_64:
2670 result = get_x86_64_dynamic_type (type);
2671 break;
2672 default:
2673 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2674 result = get_solaris_dynamic_type (type);
2675 else
2676 result = NULL;
2677 break;
2678 }
2679
2680 if (result != NULL)
2681 return result;
2682
2683 snprintf (buff, sizeof (buff), _("Processor Specific: %lx"), type);
2684 }
2685 else if (((type >= DT_LOOS) && (type <= DT_HIOS))
2686 || (filedata->file_header.e_machine == EM_PARISC
2687 && (type >= OLD_DT_LOOS) && (type <= OLD_DT_HIOS)))
2688 {
2689 const char * result;
2690
2691 switch (filedata->file_header.e_machine)
2692 {
2693 case EM_PARISC:
2694 result = get_parisc_dynamic_type (type);
2695 break;
2696 case EM_IA_64:
2697 result = get_ia64_dynamic_type (type);
2698 break;
2699 default:
2700 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
2701 result = get_solaris_dynamic_type (type);
2702 else
2703 result = NULL;
2704 break;
2705 }
2706
2707 if (result != NULL)
2708 return result;
2709
2710 snprintf (buff, sizeof (buff), _("Operating System specific: %lx"),
2711 type);
2712 }
2713 else
2714 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), type);
2715
2716 return buff;
2717 }
2718 }
2719
2720 static bool get_program_headers (Filedata *);
2721 static bool get_dynamic_section (Filedata *);
2722
2723 static void
2724 locate_dynamic_section (Filedata *filedata)
2725 {
2726 uint64_t dynamic_addr = 0;
2727 uint64_t dynamic_size = 0;
2728
2729 if (filedata->file_header.e_phnum != 0
2730 && get_program_headers (filedata))
2731 {
2732 Elf_Internal_Phdr *segment;
2733 unsigned int i;
2734
2735 for (i = 0, segment = filedata->program_headers;
2736 i < filedata->file_header.e_phnum;
2737 i++, segment++)
2738 {
2739 if (segment->p_type == PT_DYNAMIC)
2740 {
2741 dynamic_addr = segment->p_offset;
2742 dynamic_size = segment->p_filesz;
2743
2744 if (filedata->section_headers != NULL)
2745 {
2746 Elf_Internal_Shdr *sec;
2747
2748 sec = find_section (filedata, ".dynamic");
2749 if (sec != NULL)
2750 {
2751 if (sec->sh_size == 0
2752 || sec->sh_type == SHT_NOBITS)
2753 {
2754 dynamic_addr = 0;
2755 dynamic_size = 0;
2756 }
2757 else
2758 {
2759 dynamic_addr = sec->sh_offset;
2760 dynamic_size = sec->sh_size;
2761 }
2762 }
2763 }
2764
2765 if (dynamic_addr > filedata->file_size
2766 || (dynamic_size > filedata->file_size - dynamic_addr))
2767 {
2768 dynamic_addr = 0;
2769 dynamic_size = 0;
2770 }
2771 break;
2772 }
2773 }
2774 }
2775 filedata->dynamic_addr = dynamic_addr;
2776 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
2777 }
2778
2779 static bool
2780 is_pie (Filedata *filedata)
2781 {
2782 Elf_Internal_Dyn *entry;
2783
2784 if (filedata->dynamic_size == 0)
2785 locate_dynamic_section (filedata);
2786 if (filedata->dynamic_size <= 1)
2787 return false;
2788
2789 if (!get_dynamic_section (filedata))
2790 return false;
2791
2792 for (entry = filedata->dynamic_section;
2793 entry < filedata->dynamic_section + filedata->dynamic_nent;
2794 entry++)
2795 {
2796 if (entry->d_tag == DT_FLAGS_1)
2797 {
2798 if ((entry->d_un.d_val & DF_1_PIE) != 0)
2799 return true;
2800 break;
2801 }
2802 }
2803 return false;
2804 }
2805
2806 static char *
2807 get_file_type (Filedata *filedata)
2808 {
2809 unsigned e_type = filedata->file_header.e_type;
2810 static char buff[64];
2811
2812 switch (e_type)
2813 {
2814 case ET_NONE: return _("NONE (None)");
2815 case ET_REL: return _("REL (Relocatable file)");
2816 case ET_EXEC: return _("EXEC (Executable file)");
2817 case ET_DYN:
2818 if (is_pie (filedata))
2819 return _("DYN (Position-Independent Executable file)");
2820 else
2821 return _("DYN (Shared object file)");
2822 case ET_CORE: return _("CORE (Core file)");
2823
2824 default:
2825 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
2826 snprintf (buff, sizeof (buff), _("Processor Specific: (%x)"), e_type);
2827 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
2828 snprintf (buff, sizeof (buff), _("OS Specific: (%x)"), e_type);
2829 else
2830 snprintf (buff, sizeof (buff), _("<unknown>: %x"), e_type);
2831 return buff;
2832 }
2833 }
2834
2835 static char *
2836 get_machine_name (unsigned e_machine)
2837 {
2838 static char buff[64]; /* XXX */
2839
2840 switch (e_machine)
2841 {
2842 /* Please keep this switch table sorted by increasing EM_ value. */
2843 /* 0 */
2844 case EM_NONE: return _("None");
2845 case EM_M32: return "WE32100";
2846 case EM_SPARC: return "Sparc";
2847 case EM_386: return "Intel 80386";
2848 case EM_68K: return "MC68000";
2849 case EM_88K: return "MC88000";
2850 case EM_IAMCU: return "Intel MCU";
2851 case EM_860: return "Intel 80860";
2852 case EM_MIPS: return "MIPS R3000";
2853 case EM_S370: return "IBM System/370";
2854 /* 10 */
2855 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
2856 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
2857 case EM_PARISC: return "HPPA";
2858 case EM_VPP550: return "Fujitsu VPP500";
2859 case EM_SPARC32PLUS: return "Sparc v8+" ;
2860 case EM_960: return "Intel 80960";
2861 case EM_PPC: return "PowerPC";
2862 /* 20 */
2863 case EM_PPC64: return "PowerPC64";
2864 case EM_S390_OLD:
2865 case EM_S390: return "IBM S/390";
2866 case EM_SPU: return "SPU";
2867 /* 30 */
2868 case EM_V800: return "Renesas V850 (using RH850 ABI)";
2869 case EM_FR20: return "Fujitsu FR20";
2870 case EM_RH32: return "TRW RH32";
2871 case EM_MCORE: return "MCORE";
2872 /* 40 */
2873 case EM_ARM: return "ARM";
2874 case EM_OLD_ALPHA: return "Digital Alpha (old)";
2875 case EM_SH: return "Renesas / SuperH SH";
2876 case EM_SPARCV9: return "Sparc v9";
2877 case EM_TRICORE: return "Siemens Tricore";
2878 case EM_ARC: return "ARC";
2879 case EM_H8_300: return "Renesas H8/300";
2880 case EM_H8_300H: return "Renesas H8/300H";
2881 case EM_H8S: return "Renesas H8S";
2882 case EM_H8_500: return "Renesas H8/500";
2883 /* 50 */
2884 case EM_IA_64: return "Intel IA-64";
2885 case EM_MIPS_X: return "Stanford MIPS-X";
2886 case EM_COLDFIRE: return "Motorola Coldfire";
2887 case EM_68HC12: return "Motorola MC68HC12 Microcontroller";
2888 case EM_MMA: return "Fujitsu Multimedia Accelerator";
2889 case EM_PCP: return "Siemens PCP";
2890 case EM_NCPU: return "Sony nCPU embedded RISC processor";
2891 case EM_NDR1: return "Denso NDR1 microprocessor";
2892 case EM_STARCORE: return "Motorola Star*Core processor";
2893 case EM_ME16: return "Toyota ME16 processor";
2894 /* 60 */
2895 case EM_ST100: return "STMicroelectronics ST100 processor";
2896 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
2897 case EM_X86_64: return "Advanced Micro Devices X86-64";
2898 case EM_PDSP: return "Sony DSP processor";
2899 case EM_PDP10: return "Digital Equipment Corp. PDP-10";
2900 case EM_PDP11: return "Digital Equipment Corp. PDP-11";
2901 case EM_FX66: return "Siemens FX66 microcontroller";
2902 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
2903 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
2904 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
2905 /* 70 */
2906 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
2907 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
2908 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
2909 case EM_SVX: return "Silicon Graphics SVx";
2910 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
2911 case EM_VAX: return "Digital VAX";
2912 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
2913 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
2914 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
2915 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
2916 /* 80 */
2917 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
2918 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
2919 case EM_PRISM: return "Vitesse Prism";
2920 case EM_AVR_OLD:
2921 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
2922 case EM_CYGNUS_FR30:
2923 case EM_FR30: return "Fujitsu FR30";
2924 case EM_CYGNUS_D10V:
2925 case EM_D10V: return "d10v";
2926 case EM_CYGNUS_D30V:
2927 case EM_D30V: return "d30v";
2928 case EM_CYGNUS_V850:
2929 case EM_V850: return "Renesas V850";
2930 case EM_CYGNUS_M32R:
2931 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
2932 case EM_CYGNUS_MN10300:
2933 case EM_MN10300: return "mn10300";
2934 /* 90 */
2935 case EM_CYGNUS_MN10200:
2936 case EM_MN10200: return "mn10200";
2937 case EM_PJ: return "picoJava";
2938 case EM_OR1K: return "OpenRISC 1000";
2939 case EM_ARC_COMPACT: return "ARCompact";
2940 case EM_XTENSA_OLD:
2941 case EM_XTENSA: return "Tensilica Xtensa Processor";
2942 case EM_VIDEOCORE: return "Alphamosaic VideoCore processor";
2943 case EM_TMM_GPP: return "Thompson Multimedia General Purpose Processor";
2944 case EM_NS32K: return "National Semiconductor 32000 series";
2945 case EM_TPC: return "Tenor Network TPC processor";
2946 case EM_SNP1K: return "Trebia SNP 1000 processor";
2947 /* 100 */
2948 case EM_ST200: return "STMicroelectronics ST200 microcontroller";
2949 case EM_IP2K_OLD:
2950 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
2951 case EM_MAX: return "MAX Processor";
2952 case EM_CR: return "National Semiconductor CompactRISC";
2953 case EM_F2MC16: return "Fujitsu F2MC16";
2954 case EM_MSP430: return "Texas Instruments msp430 microcontroller";
2955 case EM_BLACKFIN: return "Analog Devices Blackfin";
2956 case EM_SE_C33: return "S1C33 Family of Seiko Epson processors";
2957 case EM_SEP: return "Sharp embedded microprocessor";
2958 case EM_ARCA: return "Arca RISC microprocessor";
2959 /* 110 */
2960 case EM_UNICORE: return "Unicore";
2961 case EM_EXCESS: return "eXcess 16/32/64-bit configurable embedded CPU";
2962 case EM_DXP: return "Icera Semiconductor Inc. Deep Execution Processor";
2963 case EM_ALTERA_NIOS2: return "Altera Nios II";
2964 case EM_CRX: return "National Semiconductor CRX microprocessor";
2965 case EM_XGATE: return "Motorola XGATE embedded processor";
2966 case EM_C166:
2967 case EM_XC16X: return "Infineon Technologies xc16x";
2968 case EM_M16C: return "Renesas M16C series microprocessors";
2969 case EM_DSPIC30F: return "Microchip Technology dsPIC30F Digital Signal Controller";
2970 case EM_CE: return "Freescale Communication Engine RISC core";
2971 /* 120 */
2972 case EM_M32C: return "Renesas M32c";
2973 /* 130 */
2974 case EM_TSK3000: return "Altium TSK3000 core";
2975 case EM_RS08: return "Freescale RS08 embedded processor";
2976 case EM_ECOG2: return "Cyan Technology eCOG2 microprocessor";
2977 case EM_SCORE: return "SUNPLUS S+Core";
2978 case EM_DSP24: return "New Japan Radio (NJR) 24-bit DSP Processor";
2979 case EM_VIDEOCORE3: return "Broadcom VideoCore III processor";
2980 case EM_LATTICEMICO32: return "Lattice Mico32";
2981 case EM_SE_C17: return "Seiko Epson C17 family";
2982 /* 140 */
2983 case EM_TI_C6000: return "Texas Instruments TMS320C6000 DSP family";
2984 case EM_TI_C2000: return "Texas Instruments TMS320C2000 DSP family";
2985 case EM_TI_C5500: return "Texas Instruments TMS320C55x DSP family";
2986 case EM_TI_PRU: return "TI PRU I/O processor";
2987 /* 160 */
2988 case EM_MMDSP_PLUS: return "STMicroelectronics 64bit VLIW Data Signal Processor";
2989 case EM_CYPRESS_M8C: return "Cypress M8C microprocessor";
2990 case EM_R32C: return "Renesas R32C series microprocessors";
2991 case EM_TRIMEDIA: return "NXP Semiconductors TriMedia architecture family";
2992 case EM_QDSP6: return "QUALCOMM DSP6 Processor";
2993 case EM_8051: return "Intel 8051 and variants";
2994 case EM_STXP7X: return "STMicroelectronics STxP7x family";
2995 case EM_NDS32: return "Andes Technology compact code size embedded RISC processor family";
2996 case EM_ECOG1X: return "Cyan Technology eCOG1X family";
2997 case EM_MAXQ30: return "Dallas Semiconductor MAXQ30 Core microcontrollers";
2998 /* 170 */
2999 case EM_XIMO16: return "New Japan Radio (NJR) 16-bit DSP Processor";
3000 case EM_MANIK: return "M2000 Reconfigurable RISC Microprocessor";
3001 case EM_CRAYNV2: return "Cray Inc. NV2 vector architecture";
3002 case EM_RX: return "Renesas RX";
3003 case EM_METAG: return "Imagination Technologies Meta processor architecture";
3004 case EM_MCST_ELBRUS: return "MCST Elbrus general purpose hardware architecture";
3005 case EM_ECOG16: return "Cyan Technology eCOG16 family";
3006 case EM_CR16:
3007 case EM_MICROBLAZE:
3008 case EM_MICROBLAZE_OLD: return "Xilinx MicroBlaze";
3009 case EM_ETPU: return "Freescale Extended Time Processing Unit";
3010 case EM_SLE9X: return "Infineon Technologies SLE9X core";
3011 /* 180 */
3012 case EM_L1OM: return "Intel L1OM";
3013 case EM_K1OM: return "Intel K1OM";
3014 case EM_INTEL182: return "Intel (reserved)";
3015 case EM_AARCH64: return "AArch64";
3016 case EM_ARM184: return "ARM (reserved)";
3017 case EM_AVR32: return "Atmel Corporation 32-bit microprocessor";
3018 case EM_STM8: return "STMicroeletronics STM8 8-bit microcontroller";
3019 case EM_TILE64: return "Tilera TILE64 multicore architecture family";
3020 case EM_TILEPRO: return "Tilera TILEPro multicore architecture family";
3021 /* 190 */
3022 case EM_CUDA: return "NVIDIA CUDA architecture";
3023 case EM_TILEGX: return "Tilera TILE-Gx multicore architecture family";
3024 case EM_CLOUDSHIELD: return "CloudShield architecture family";
3025 case EM_COREA_1ST: return "KIPO-KAIST Core-A 1st generation processor family";
3026 case EM_COREA_2ND: return "KIPO-KAIST Core-A 2nd generation processor family";
3027 case EM_ARC_COMPACT2: return "ARCv2";
3028 case EM_OPEN8: return "Open8 8-bit RISC soft processor core";
3029 case EM_RL78: return "Renesas RL78";
3030 case EM_VIDEOCORE5: return "Broadcom VideoCore V processor";
3031 case EM_78K0R: return "Renesas 78K0R";
3032 /* 200 */
3033 case EM_56800EX: return "Freescale 56800EX Digital Signal Controller (DSC)";
3034 case EM_BA1: return "Beyond BA1 CPU architecture";
3035 case EM_BA2: return "Beyond BA2 CPU architecture";
3036 case EM_XCORE: return "XMOS xCORE processor family";
3037 case EM_MCHP_PIC: return "Microchip 8-bit PIC(r) family";
3038 case EM_INTELGT: return "Intel Graphics Technology";
3039 /* 210 */
3040 case EM_KM32: return "KM211 KM32 32-bit processor";
3041 case EM_KMX32: return "KM211 KMX32 32-bit processor";
3042 case EM_KMX16: return "KM211 KMX16 16-bit processor";
3043 case EM_KMX8: return "KM211 KMX8 8-bit processor";
3044 case EM_KVARC: return "KM211 KVARC processor";
3045 case EM_CDP: return "Paneve CDP architecture family";
3046 case EM_COGE: return "Cognitive Smart Memory Processor";
3047 case EM_COOL: return "Bluechip Systems CoolEngine";
3048 case EM_NORC: return "Nanoradio Optimized RISC";
3049 case EM_CSR_KALIMBA: return "CSR Kalimba architecture family";
3050 /* 220 */
3051 case EM_Z80: return "Zilog Z80";
3052 case EM_VISIUM: return "CDS VISIUMcore processor";
3053 case EM_FT32: return "FTDI Chip FT32";
3054 case EM_MOXIE: return "Moxie";
3055 case EM_AMDGPU: return "AMD GPU";
3056 /* 230 (all reserved) */
3057 /* 240 */
3058 case EM_RISCV: return "RISC-V";
3059 case EM_LANAI: return "Lanai 32-bit processor";
3060 case EM_CEVA: return "CEVA Processor Architecture Family";
3061 case EM_CEVA_X2: return "CEVA X2 Processor Family";
3062 case EM_BPF: return "Linux BPF";
3063 case EM_GRAPHCORE_IPU: return "Graphcore Intelligent Processing Unit";
3064 case EM_IMG1: return "Imagination Technologies";
3065 /* 250 */
3066 case EM_NFP: return "Netronome Flow Processor";
3067 case EM_VE: return "NEC Vector Engine";
3068 case EM_CSKY: return "C-SKY";
3069 case EM_ARC_COMPACT3_64: return "Synopsys ARCv3 64-bit processor";
3070 case EM_MCS6502: return "MOS Technology MCS 6502 processor";
3071 case EM_ARC_COMPACT3: return "Synopsys ARCv3 32-bit processor";
3072 case EM_KVX: return "Kalray VLIW core of the MPPA processor family";
3073 case EM_65816: return "WDC 65816/65C816";
3074 case EM_LOONGARCH: return "LoongArch";
3075 case EM_KF32: return "ChipON KungFu32";
3076
3077 /* Large numbers... */
3078 case EM_MT: return "Morpho Techologies MT processor";
3079 case EM_ALPHA: return "Alpha";
3080 case EM_WEBASSEMBLY: return "Web Assembly";
3081 case EM_DLX: return "OpenDLX";
3082 case EM_XSTORMY16: return "Sanyo XStormy16 CPU core";
3083 case EM_IQ2000: return "Vitesse IQ2000";
3084 case EM_M32C_OLD:
3085 case EM_NIOS32: return "Altera Nios";
3086 case EM_CYGNUS_MEP: return "Toshiba MeP Media Engine";
3087 case EM_ADAPTEVA_EPIPHANY: return "Adapteva EPIPHANY";
3088 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
3089 case EM_S12Z: return "Freescale S12Z";
3090
3091 default:
3092 snprintf (buff, sizeof (buff), _("<unknown>: 0x%x"), e_machine);
3093 return buff;
3094 }
3095 }
3096
3097 static char *
3098 decode_ARC_machine_flags (char *out, unsigned e_flags, unsigned e_machine)
3099 {
3100 /* ARC has two machine types EM_ARC_COMPACT and EM_ARC_COMPACT2. Some
3101 other compilers don't specify an architecture type in the e_flags, and
3102 instead use EM_ARC_COMPACT for old ARC600, ARC601, and ARC700
3103 architectures, and switch to EM_ARC_COMPACT2 for newer ARCEM and ARCHS
3104 architectures.
3105
3106 Th GNU tools follows this use of EM_ARC_COMPACT and EM_ARC_COMPACT2,
3107 but also sets a specific architecture type in the e_flags field.
3108
3109 However, when decoding the flags we don't worry if we see an
3110 unexpected pairing, for example EM_ARC_COMPACT machine type, with
3111 ARCEM architecture type. */
3112
3113 switch (e_flags & EF_ARC_MACH_MSK)
3114 {
3115 /* We only expect these to occur for EM_ARC_COMPACT2. */
3116 case EF_ARC_CPU_ARCV2EM:
3117 out = stpcpy (out, ", ARC EM");
3118 break;
3119 case EF_ARC_CPU_ARCV2HS:
3120 out = stpcpy (out, ", ARC HS");
3121 break;
3122
3123 /* We only expect these to occur for EM_ARC_COMPACT. */
3124 case E_ARC_MACH_ARC600:
3125 out = stpcpy (out, ", ARC600");
3126 break;
3127 case E_ARC_MACH_ARC601:
3128 out = stpcpy (out, ", ARC601");
3129 break;
3130 case E_ARC_MACH_ARC700:
3131 out = stpcpy (out, ", ARC700");
3132 break;
3133
3134 /* The only times we should end up here are (a) A corrupt ELF, (b) A
3135 new ELF with new architecture being read by an old version of
3136 readelf, or (c) An ELF built with non-GNU compiler that does not
3137 set the architecture in the e_flags. */
3138 default:
3139 if (e_machine == EM_ARC_COMPACT)
3140 out = stpcpy (out, ", Unknown ARCompact");
3141 else
3142 out = stpcpy (out, ", Unknown ARC");
3143 break;
3144 }
3145
3146 switch (e_flags & EF_ARC_OSABI_MSK)
3147 {
3148 case E_ARC_OSABI_ORIG:
3149 out = stpcpy (out, ", (ABI:legacy)");
3150 break;
3151 case E_ARC_OSABI_V2:
3152 out = stpcpy (out, ", (ABI:v2)");
3153 break;
3154 /* Only upstream 3.9+ kernels will support ARCv2 ISA. */
3155 case E_ARC_OSABI_V3:
3156 out = stpcpy (out, ", v3 no-legacy-syscalls ABI");
3157 break;
3158 case E_ARC_OSABI_V4:
3159 out = stpcpy (out, ", v4 ABI");
3160 break;
3161 default:
3162 out = stpcpy (out, ", unrecognised ARC OSABI flag");
3163 break;
3164 }
3165 return out;
3166 }
3167
3168 static char *
3169 decode_ARM_machine_flags (char *out, unsigned e_flags)
3170 {
3171 unsigned eabi;
3172 bool unknown = false;
3173
3174 eabi = EF_ARM_EABI_VERSION (e_flags);
3175 e_flags &= ~ EF_ARM_EABIMASK;
3176
3177 /* Handle "generic" ARM flags. */
3178 if (e_flags & EF_ARM_RELEXEC)
3179 {
3180 out = stpcpy (out, ", relocatable executable");
3181 e_flags &= ~ EF_ARM_RELEXEC;
3182 }
3183
3184 if (e_flags & EF_ARM_PIC)
3185 {
3186 out = stpcpy (out, ", position independent");
3187 e_flags &= ~ EF_ARM_PIC;
3188 }
3189
3190 /* Now handle EABI specific flags. */
3191 switch (eabi)
3192 {
3193 default:
3194 out = stpcpy (out, ", <unrecognized EABI>");
3195 if (e_flags)
3196 unknown = true;
3197 break;
3198
3199 case EF_ARM_EABI_VER1:
3200 out = stpcpy (out, ", Version1 EABI");
3201 while (e_flags)
3202 {
3203 unsigned flag;
3204
3205 /* Process flags one bit at a time. */
3206 flag = e_flags & - e_flags;
3207 e_flags &= ~ flag;
3208
3209 switch (flag)
3210 {
3211 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3212 out = stpcpy (out, ", sorted symbol tables");
3213 break;
3214
3215 default:
3216 unknown = true;
3217 break;
3218 }
3219 }
3220 break;
3221
3222 case EF_ARM_EABI_VER2:
3223 out = stpcpy (out, ", Version2 EABI");
3224 while (e_flags)
3225 {
3226 unsigned flag;
3227
3228 /* Process flags one bit at a time. */
3229 flag = e_flags & - e_flags;
3230 e_flags &= ~ flag;
3231
3232 switch (flag)
3233 {
3234 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
3235 out = stpcpy (out, ", sorted symbol tables");
3236 break;
3237
3238 case EF_ARM_DYNSYMSUSESEGIDX:
3239 out = stpcpy (out, ", dynamic symbols use segment index");
3240 break;
3241
3242 case EF_ARM_MAPSYMSFIRST:
3243 out = stpcpy (out, ", mapping symbols precede others");
3244 break;
3245
3246 default:
3247 unknown = true;
3248 break;
3249 }
3250 }
3251 break;
3252
3253 case EF_ARM_EABI_VER3:
3254 out = stpcpy (out, ", Version3 EABI");
3255 break;
3256
3257 case EF_ARM_EABI_VER4:
3258 out = stpcpy (out, ", Version4 EABI");
3259 while (e_flags)
3260 {
3261 unsigned flag;
3262
3263 /* Process flags one bit at a time. */
3264 flag = e_flags & - e_flags;
3265 e_flags &= ~ flag;
3266
3267 switch (flag)
3268 {
3269 case EF_ARM_BE8:
3270 out = stpcpy (out, ", BE8");
3271 break;
3272
3273 case EF_ARM_LE8:
3274 out = stpcpy (out, ", LE8");
3275 break;
3276
3277 default:
3278 unknown = true;
3279 break;
3280 }
3281 }
3282 break;
3283
3284 case EF_ARM_EABI_VER5:
3285 out = stpcpy (out, ", Version5 EABI");
3286 while (e_flags)
3287 {
3288 unsigned flag;
3289
3290 /* Process flags one bit at a time. */
3291 flag = e_flags & - e_flags;
3292 e_flags &= ~ flag;
3293
3294 switch (flag)
3295 {
3296 case EF_ARM_BE8:
3297 out = stpcpy (out, ", BE8");
3298 break;
3299
3300 case EF_ARM_LE8:
3301 out = stpcpy (out, ", LE8");
3302 break;
3303
3304 case EF_ARM_ABI_FLOAT_SOFT: /* Conflicts with EF_ARM_SOFT_FLOAT. */
3305 out = stpcpy (out, ", soft-float ABI");
3306 break;
3307
3308 case EF_ARM_ABI_FLOAT_HARD: /* Conflicts with EF_ARM_VFP_FLOAT. */
3309 out = stpcpy (out, ", hard-float ABI");
3310 break;
3311
3312 default:
3313 unknown = true;
3314 break;
3315 }
3316 }
3317 break;
3318
3319 case EF_ARM_EABI_UNKNOWN:
3320 out = stpcpy (out, ", GNU EABI");
3321 while (e_flags)
3322 {
3323 unsigned flag;
3324
3325 /* Process flags one bit at a time. */
3326 flag = e_flags & - e_flags;
3327 e_flags &= ~ flag;
3328
3329 switch (flag)
3330 {
3331 case EF_ARM_INTERWORK:
3332 out = stpcpy (out, ", interworking enabled");
3333 break;
3334
3335 case EF_ARM_APCS_26:
3336 out = stpcpy (out, ", uses APCS/26");
3337 break;
3338
3339 case EF_ARM_APCS_FLOAT:
3340 out = stpcpy (out, ", uses APCS/float");
3341 break;
3342
3343 case EF_ARM_PIC:
3344 out = stpcpy (out, ", position independent");
3345 break;
3346
3347 case EF_ARM_ALIGN8:
3348 out = stpcpy (out, ", 8 bit structure alignment");
3349 break;
3350
3351 case EF_ARM_NEW_ABI:
3352 out = stpcpy (out, ", uses new ABI");
3353 break;
3354
3355 case EF_ARM_OLD_ABI:
3356 out = stpcpy (out, ", uses old ABI");
3357 break;
3358
3359 case EF_ARM_SOFT_FLOAT:
3360 out = stpcpy (out, ", software FP");
3361 break;
3362
3363 case EF_ARM_VFP_FLOAT:
3364 out = stpcpy (out, ", VFP");
3365 break;
3366
3367 case EF_ARM_MAVERICK_FLOAT:
3368 out = stpcpy (out, ", Maverick FP");
3369 break;
3370
3371 default:
3372 unknown = true;
3373 break;
3374 }
3375 }
3376 }
3377
3378 if (unknown)
3379 out = stpcpy (out,_(", <unknown>"));
3380 return out;
3381 }
3382
3383 static char *
3384 decode_AVR_machine_flags (char *out, unsigned e_flags)
3385 {
3386 switch (e_flags & EF_AVR_MACH)
3387 {
3388 case E_AVR_MACH_AVR1:
3389 out = stpcpy (out, ", avr:1");
3390 break;
3391 case E_AVR_MACH_AVR2:
3392 out = stpcpy (out, ", avr:2");
3393 break;
3394 case E_AVR_MACH_AVR25:
3395 out = stpcpy (out, ", avr:25");
3396 break;
3397 case E_AVR_MACH_AVR3:
3398 out = stpcpy (out, ", avr:3");
3399 break;
3400 case E_AVR_MACH_AVR31:
3401 out = stpcpy (out, ", avr:31");
3402 break;
3403 case E_AVR_MACH_AVR35:
3404 out = stpcpy (out, ", avr:35");
3405 break;
3406 case E_AVR_MACH_AVR4:
3407 out = stpcpy (out, ", avr:4");
3408 break;
3409 case E_AVR_MACH_AVR5:
3410 out = stpcpy (out, ", avr:5");
3411 break;
3412 case E_AVR_MACH_AVR51:
3413 out = stpcpy (out, ", avr:51");
3414 break;
3415 case E_AVR_MACH_AVR6:
3416 out = stpcpy (out, ", avr:6");
3417 break;
3418 case E_AVR_MACH_AVRTINY:
3419 out = stpcpy (out, ", avr:100");
3420 break;
3421 case E_AVR_MACH_XMEGA1:
3422 out = stpcpy (out, ", avr:101");
3423 break;
3424 case E_AVR_MACH_XMEGA2:
3425 out = stpcpy (out, ", avr:102");
3426 break;
3427 case E_AVR_MACH_XMEGA3:
3428 out = stpcpy (out, ", avr:103");
3429 break;
3430 case E_AVR_MACH_XMEGA4:
3431 out = stpcpy (out, ", avr:104");
3432 break;
3433 case E_AVR_MACH_XMEGA5:
3434 out = stpcpy (out, ", avr:105");
3435 break;
3436 case E_AVR_MACH_XMEGA6:
3437 out = stpcpy (out, ", avr:106");
3438 break;
3439 case E_AVR_MACH_XMEGA7:
3440 out = stpcpy (out, ", avr:107");
3441 break;
3442 default:
3443 out = stpcpy (out, ", avr:<unknown>");
3444 break;
3445 }
3446
3447 if (e_flags & EF_AVR_LINKRELAX_PREPARED)
3448 out = stpcpy (out, ", link-relax");
3449 return out;
3450 }
3451
3452 static char *
3453 decode_BLACKFIN_machine_flags (char *out, unsigned e_flags)
3454 {
3455 if (e_flags & EF_BFIN_PIC)
3456 out = stpcpy (out, ", PIC");
3457
3458 if (e_flags & EF_BFIN_FDPIC)
3459 out = stpcpy (out, ", FDPIC");
3460
3461 if (e_flags & EF_BFIN_CODE_IN_L1)
3462 out = stpcpy (out, ", code in L1");
3463
3464 if (e_flags & EF_BFIN_DATA_IN_L1)
3465 out = stpcpy (out, ", data in L1");
3466 return out;
3467 }
3468
3469 static char *
3470 decode_FRV_machine_flags (char *out, unsigned e_flags)
3471 {
3472 switch (e_flags & EF_FRV_CPU_MASK)
3473 {
3474 case EF_FRV_CPU_GENERIC:
3475 break;
3476
3477 default:
3478 out = stpcpy (out, ", fr???");
3479 break;
3480
3481 case EF_FRV_CPU_FR300:
3482 out = stpcpy (out, ", fr300");
3483 break;
3484
3485 case EF_FRV_CPU_FR400:
3486 out = stpcpy (out, ", fr400");
3487 break;
3488 case EF_FRV_CPU_FR405:
3489 out = stpcpy (out, ", fr405");
3490 break;
3491
3492 case EF_FRV_CPU_FR450:
3493 out = stpcpy (out, ", fr450");
3494 break;
3495
3496 case EF_FRV_CPU_FR500:
3497 out = stpcpy (out, ", fr500");
3498 break;
3499 case EF_FRV_CPU_FR550:
3500 out = stpcpy (out, ", fr550");
3501 break;
3502
3503 case EF_FRV_CPU_SIMPLE:
3504 out = stpcpy (out, ", simple");
3505 break;
3506 case EF_FRV_CPU_TOMCAT:
3507 out = stpcpy (out, ", tomcat");
3508 break;
3509 }
3510 return out;
3511 }
3512
3513 static char *
3514 decode_IA64_machine_flags (char *out, unsigned e_flags, Filedata *filedata)
3515 {
3516 if ((e_flags & EF_IA_64_ABI64))
3517 out = stpcpy (out, ", 64-bit");
3518 else
3519 out = stpcpy (out, ", 32-bit");
3520 if ((e_flags & EF_IA_64_REDUCEDFP))
3521 out = stpcpy (out, ", reduced fp model");
3522 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
3523 out = stpcpy (out, ", no function descriptors, constant gp");
3524 else if ((e_flags & EF_IA_64_CONS_GP))
3525 out = stpcpy (out, ", constant gp");
3526 if ((e_flags & EF_IA_64_ABSOLUTE))
3527 out = stpcpy (out, ", absolute");
3528 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
3529 {
3530 if ((e_flags & EF_IA_64_VMS_LINKAGES))
3531 out = stpcpy (out, ", vms_linkages");
3532 switch ((e_flags & EF_IA_64_VMS_COMCOD))
3533 {
3534 case EF_IA_64_VMS_COMCOD_SUCCESS:
3535 break;
3536 case EF_IA_64_VMS_COMCOD_WARNING:
3537 out = stpcpy (out, ", warning");
3538 break;
3539 case EF_IA_64_VMS_COMCOD_ERROR:
3540 out = stpcpy (out, ", error");
3541 break;
3542 case EF_IA_64_VMS_COMCOD_ABORT:
3543 out = stpcpy (out, ", abort");
3544 break;
3545 default:
3546 warn (_("Unrecognised IA64 VMS Command Code: %x\n"),
3547 e_flags & EF_IA_64_VMS_COMCOD);
3548 out = stpcpy (out, ", <unknown>");
3549 }
3550 }
3551 return out;
3552 }
3553
3554 static char *
3555 decode_LOONGARCH_machine_flags (char *out, unsigned int e_flags)
3556 {
3557 if (EF_LOONGARCH_IS_SOFT_FLOAT (e_flags))
3558 out = stpcpy (out, ", SOFT-FLOAT");
3559 else if (EF_LOONGARCH_IS_SINGLE_FLOAT (e_flags))
3560 out = stpcpy (out, ", SINGLE-FLOAT");
3561 else if (EF_LOONGARCH_IS_DOUBLE_FLOAT (e_flags))
3562 out = stpcpy (out, ", DOUBLE-FLOAT");
3563
3564 if (EF_LOONGARCH_IS_OBJ_V0 (e_flags))
3565 out = stpcpy (out, ", OBJ-v0");
3566 else if (EF_LOONGARCH_IS_OBJ_V1 (e_flags))
3567 out = stpcpy (out, ", OBJ-v1");
3568 return out;
3569 }
3570
3571 static char *
3572 decode_M68K_machine_flags (char *out, unsigned int e_flags)
3573 {
3574 if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
3575 out = stpcpy (out, ", m68000");
3576 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
3577 out = stpcpy (out, ", cpu32");
3578 else if ((e_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
3579 out = stpcpy (out, ", fido_a");
3580 else
3581 {
3582 char const *isa = _("unknown");
3583 char const *mac = _("unknown mac");
3584 char const *additional = NULL;
3585
3586 switch (e_flags & EF_M68K_CF_ISA_MASK)
3587 {
3588 case EF_M68K_CF_ISA_A_NODIV:
3589 isa = "A";
3590 additional = ", nodiv";
3591 break;
3592 case EF_M68K_CF_ISA_A:
3593 isa = "A";
3594 break;
3595 case EF_M68K_CF_ISA_A_PLUS:
3596 isa = "A+";
3597 break;
3598 case EF_M68K_CF_ISA_B_NOUSP:
3599 isa = "B";
3600 additional = ", nousp";
3601 break;
3602 case EF_M68K_CF_ISA_B:
3603 isa = "B";
3604 break;
3605 case EF_M68K_CF_ISA_C:
3606 isa = "C";
3607 break;
3608 case EF_M68K_CF_ISA_C_NODIV:
3609 isa = "C";
3610 additional = ", nodiv";
3611 break;
3612 }
3613 out = stpcpy (out, ", cf, isa ");
3614 out = stpcpy (out, isa);
3615 if (additional)
3616 out = stpcpy (out, additional);
3617 if (e_flags & EF_M68K_CF_FLOAT)
3618 out = stpcpy (out, ", float");
3619 switch (e_flags & EF_M68K_CF_MAC_MASK)
3620 {
3621 case 0:
3622 mac = NULL;
3623 break;
3624 case EF_M68K_CF_MAC:
3625 mac = "mac";
3626 break;
3627 case EF_M68K_CF_EMAC:
3628 mac = "emac";
3629 break;
3630 case EF_M68K_CF_EMAC_B:
3631 mac = "emac_b";
3632 break;
3633 }
3634 if (mac)
3635 {
3636 out = stpcpy (out, ", ");
3637 out = stpcpy (out, mac);
3638 }
3639 }
3640 return out;
3641 }
3642
3643 static char *
3644 decode_MeP_machine_flags (char *out, unsigned int e_flags)
3645 {
3646 switch (e_flags & EF_MEP_CPU_MASK)
3647 {
3648 case EF_MEP_CPU_MEP:
3649 out = stpcpy (out, ", generic MeP");
3650 break;
3651 case EF_MEP_CPU_C2:
3652 out = stpcpy (out, ", MeP C2");
3653 break;
3654 case EF_MEP_CPU_C3:
3655 out = stpcpy (out, ", MeP C3");
3656 break;
3657 case EF_MEP_CPU_C4:
3658 out = stpcpy (out, ", MeP C4");
3659 break;
3660 case EF_MEP_CPU_C5:
3661 out = stpcpy (out, ", MeP C5");
3662 break;
3663 case EF_MEP_CPU_H1:
3664 out = stpcpy (out, ", MeP H1");
3665 break;
3666 default:
3667 out = stpcpy (out, _(", <unknown MeP cpu type>"));
3668 break;
3669 }
3670
3671 switch (e_flags & EF_MEP_COP_MASK)
3672 {
3673 case EF_MEP_COP_NONE:
3674 break;
3675 case EF_MEP_COP_AVC:
3676 out = stpcpy (out, ", AVC coprocessor");
3677 break;
3678 case EF_MEP_COP_AVC2:
3679 out = stpcpy (out, ", AVC2 coprocessor");
3680 break;
3681 case EF_MEP_COP_FMAX:
3682 out = stpcpy (out, ", FMAX coprocessor");
3683 break;
3684 case EF_MEP_COP_IVC2:
3685 out = stpcpy (out, ", IVC2 coprocessor");
3686 break;
3687 default:
3688 out = stpcpy (out, _("<unknown MeP copro type>"));
3689 break;
3690 }
3691
3692 if (e_flags & EF_MEP_LIBRARY)
3693 out = stpcpy (out, ", Built for Library");
3694
3695 if (e_flags & EF_MEP_INDEX_MASK)
3696 out += sprintf (out, ", Configuration Index: %#x",
3697 e_flags & EF_MEP_INDEX_MASK);
3698
3699 if (e_flags & ~ EF_MEP_ALL_FLAGS)
3700 out += sprintf (out, _(", unknown flags bits: %#x"),
3701 e_flags & ~ EF_MEP_ALL_FLAGS);
3702 return out;
3703 }
3704
3705 static char *
3706 decode_MIPS_machine_flags (char *out, unsigned int e_flags)
3707 {
3708 if (e_flags & EF_MIPS_NOREORDER)
3709 out = stpcpy (out, ", noreorder");
3710
3711 if (e_flags & EF_MIPS_PIC)
3712 out = stpcpy (out, ", pic");
3713
3714 if (e_flags & EF_MIPS_CPIC)
3715 out = stpcpy (out, ", cpic");
3716
3717 if (e_flags & EF_MIPS_UCODE)
3718 out = stpcpy (out, ", ugen_reserved");
3719
3720 if (e_flags & EF_MIPS_ABI2)
3721 out = stpcpy (out, ", abi2");
3722
3723 if (e_flags & EF_MIPS_OPTIONS_FIRST)
3724 out = stpcpy (out, ", odk first");
3725
3726 if (e_flags & EF_MIPS_32BITMODE)
3727 out = stpcpy (out, ", 32bitmode");
3728
3729 if (e_flags & EF_MIPS_NAN2008)
3730 out = stpcpy (out, ", nan2008");
3731
3732 if (e_flags & EF_MIPS_FP64)
3733 out = stpcpy (out, ", fp64");
3734
3735 switch ((e_flags & EF_MIPS_MACH))
3736 {
3737 case E_MIPS_MACH_3900:
3738 out = stpcpy (out, ", 3900");
3739 break;
3740 case E_MIPS_MACH_4010:
3741 out = stpcpy (out, ", 4010");
3742 break;
3743 case E_MIPS_MACH_4100:
3744 out = stpcpy (out, ", 4100");
3745 break;
3746 case E_MIPS_MACH_4111:
3747 out = stpcpy (out, ", 4111");
3748 break;
3749 case E_MIPS_MACH_4120:
3750 out = stpcpy (out, ", 4120");
3751 break;
3752 case E_MIPS_MACH_4650:
3753 out = stpcpy (out, ", 4650");
3754 break;
3755 case E_MIPS_MACH_5400:
3756 out = stpcpy (out, ", 5400");
3757 break;
3758 case E_MIPS_MACH_5500:
3759 out = stpcpy (out, ", 5500");
3760 break;
3761 case E_MIPS_MACH_5900:
3762 out = stpcpy (out, ", 5900");
3763 break;
3764 case E_MIPS_MACH_SB1:
3765 out = stpcpy (out, ", sb1");
3766 break;
3767 case E_MIPS_MACH_9000:
3768 out = stpcpy (out, ", 9000");
3769 break;
3770 case E_MIPS_MACH_LS2E:
3771 out = stpcpy (out, ", loongson-2e");
3772 break;
3773 case E_MIPS_MACH_LS2F:
3774 out = stpcpy (out, ", loongson-2f");
3775 break;
3776 case E_MIPS_MACH_GS464:
3777 out = stpcpy (out, ", gs464");
3778 break;
3779 case E_MIPS_MACH_GS464E:
3780 out = stpcpy (out, ", gs464e");
3781 break;
3782 case E_MIPS_MACH_GS264E:
3783 out = stpcpy (out, ", gs264e");
3784 break;
3785 case E_MIPS_MACH_OCTEON:
3786 out = stpcpy (out, ", octeon");
3787 break;
3788 case E_MIPS_MACH_OCTEON2:
3789 out = stpcpy (out, ", octeon2");
3790 break;
3791 case E_MIPS_MACH_OCTEON3:
3792 out = stpcpy (out, ", octeon3");
3793 break;
3794 case E_MIPS_MACH_XLR:
3795 out = stpcpy (out, ", xlr");
3796 break;
3797 case E_MIPS_MACH_IAMR2:
3798 out = stpcpy (out, ", interaptiv-mr2");
3799 break;
3800 case E_MIPS_MACH_ALLEGREX:
3801 out = stpcpy (out, ", allegrex");
3802 break;
3803 case 0:
3804 /* We simply ignore the field in this case to avoid confusion:
3805 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
3806 extension. */
3807 break;
3808 default:
3809 out = stpcpy (out, _(", unknown CPU"));
3810 break;
3811 }
3812
3813 switch ((e_flags & EF_MIPS_ABI))
3814 {
3815 case E_MIPS_ABI_O32:
3816 out = stpcpy (out, ", o32");
3817 break;
3818 case E_MIPS_ABI_O64:
3819 out = stpcpy (out, ", o64");
3820 break;
3821 case E_MIPS_ABI_EABI32:
3822 out = stpcpy (out, ", eabi32");
3823 break;
3824 case E_MIPS_ABI_EABI64:
3825 out = stpcpy (out, ", eabi64");
3826 break;
3827 case 0:
3828 /* We simply ignore the field in this case to avoid confusion:
3829 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
3830 This means it is likely to be an o32 file, but not for
3831 sure. */
3832 break;
3833 default:
3834 out = stpcpy (out, _(", unknown ABI"));
3835 break;
3836 }
3837
3838 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
3839 out = stpcpy (out, ", mdmx");
3840
3841 if (e_flags & EF_MIPS_ARCH_ASE_M16)
3842 out = stpcpy (out, ", mips16");
3843
3844 if (e_flags & EF_MIPS_ARCH_ASE_MICROMIPS)
3845 out = stpcpy (out, ", micromips");
3846
3847 switch ((e_flags & EF_MIPS_ARCH))
3848 {
3849 case E_MIPS_ARCH_1:
3850 out = stpcpy (out, ", mips1");
3851 break;
3852 case E_MIPS_ARCH_2:
3853 out = stpcpy (out, ", mips2");
3854 break;
3855 case E_MIPS_ARCH_3:
3856 out = stpcpy (out, ", mips3");
3857 break;
3858 case E_MIPS_ARCH_4:
3859 out = stpcpy (out, ", mips4");
3860 break;
3861 case E_MIPS_ARCH_5:
3862 out = stpcpy (out, ", mips5");
3863 break;
3864 case E_MIPS_ARCH_32:
3865 out = stpcpy (out, ", mips32");
3866 break;
3867 case E_MIPS_ARCH_32R2:
3868 out = stpcpy (out, ", mips32r2");
3869 break;
3870 case E_MIPS_ARCH_32R6:
3871 out = stpcpy (out, ", mips32r6");
3872 break;
3873 case E_MIPS_ARCH_64:
3874 out = stpcpy (out, ", mips64");
3875 break;
3876 case E_MIPS_ARCH_64R2:
3877 out = stpcpy (out, ", mips64r2");
3878 break;
3879 case E_MIPS_ARCH_64R6:
3880 out = stpcpy (out, ", mips64r6");
3881 break;
3882 default:
3883 out = stpcpy (out, _(", unknown ISA"));
3884 break;
3885 }
3886 return out;
3887 }
3888
3889 static char *
3890 decode_MSP430_machine_flags (char *out, unsigned e_flags)
3891 {
3892 out = stpcpy (out, _(": architecture variant: "));
3893 switch (e_flags & EF_MSP430_MACH)
3894 {
3895 case E_MSP430_MACH_MSP430x11:
3896 out = stpcpy (out, "MSP430x11");
3897 break;
3898 case E_MSP430_MACH_MSP430x11x1:
3899 out = stpcpy (out, "MSP430x11x1 ");
3900 break;
3901 case E_MSP430_MACH_MSP430x12:
3902 out = stpcpy (out, "MSP430x12");
3903 break;
3904 case E_MSP430_MACH_MSP430x13:
3905 out = stpcpy (out, "MSP430x13");
3906 break;
3907 case E_MSP430_MACH_MSP430x14:
3908 out = stpcpy (out, "MSP430x14");
3909 break;
3910 case E_MSP430_MACH_MSP430x15:
3911 out = stpcpy (out, "MSP430x15");
3912 break;
3913 case E_MSP430_MACH_MSP430x16:
3914 out = stpcpy (out, "MSP430x16");
3915 break;
3916 case E_MSP430_MACH_MSP430x31:
3917 out = stpcpy (out, "MSP430x31");
3918 break;
3919 case E_MSP430_MACH_MSP430x32:
3920 out = stpcpy (out, "MSP430x32");
3921 break;
3922 case E_MSP430_MACH_MSP430x33:
3923 out = stpcpy (out, "MSP430x33");
3924 break;
3925 case E_MSP430_MACH_MSP430x41:
3926 out = stpcpy (out, "MSP430x41");
3927 break;
3928 case E_MSP430_MACH_MSP430x42:
3929 out = stpcpy (out, "MSP430x42");
3930 break;
3931 case E_MSP430_MACH_MSP430x43:
3932 out = stpcpy (out, "MSP430x43");
3933 break;
3934 case E_MSP430_MACH_MSP430x44:
3935 out = stpcpy (out, "MSP430x44");
3936 break;
3937 case E_MSP430_MACH_MSP430X :
3938 out = stpcpy (out, "MSP430X");
3939 break;
3940 default:
3941 out = stpcpy (out, _(": unknown"));
3942 break;
3943 }
3944
3945 if (e_flags & ~ EF_MSP430_MACH)
3946 out = stpcpy (out, _(": unknown extra flag bits also present"));
3947 return out;
3948 }
3949
3950 static char *
3951 decode_NDS32_machine_flags (char *out, unsigned e_flags)
3952 {
3953 unsigned abi;
3954 unsigned arch;
3955 unsigned config;
3956 unsigned version;
3957 bool has_fpu = false;
3958
3959 static const char *ABI_STRINGS[] =
3960 {
3961 "ABI v0", /* use r5 as return register; only used in N1213HC */
3962 "ABI v1", /* use r0 as return register */
3963 "ABI v2", /* use r0 as return register and don't reserve 24 bytes for arguments */
3964 "ABI v2fp", /* for FPU */
3965 "AABI",
3966 "ABI2 FP+"
3967 };
3968 static const char *VER_STRINGS[] =
3969 {
3970 "Andes ELF V1.3 or older",
3971 "Andes ELF V1.3.1",
3972 "Andes ELF V1.4"
3973 };
3974 static const char *ARCH_STRINGS[] =
3975 {
3976 "",
3977 "Andes Star v1.0",
3978 "Andes Star v2.0",
3979 "Andes Star v3.0",
3980 "Andes Star v3.0m"
3981 };
3982
3983 abi = EF_NDS_ABI & e_flags;
3984 arch = EF_NDS_ARCH & e_flags;
3985 config = EF_NDS_INST & e_flags;
3986 version = EF_NDS32_ELF_VERSION & e_flags;
3987
3988 switch (abi)
3989 {
3990 case E_NDS_ABI_V0:
3991 case E_NDS_ABI_V1:
3992 case E_NDS_ABI_V2:
3993 case E_NDS_ABI_V2FP:
3994 case E_NDS_ABI_AABI:
3995 case E_NDS_ABI_V2FP_PLUS:
3996 /* In case there are holes in the array. */
3997 out += sprintf (out, ", %s", ABI_STRINGS[abi >> EF_NDS_ABI_SHIFT]);
3998 break;
3999
4000 default:
4001 out = stpcpy (out, ", <unrecognized ABI>");
4002 break;
4003 }
4004
4005 switch (version)
4006 {
4007 case E_NDS32_ELF_VER_1_2:
4008 case E_NDS32_ELF_VER_1_3:
4009 case E_NDS32_ELF_VER_1_4:
4010 out += sprintf (out, ", %s", VER_STRINGS[version >> EF_NDS32_ELF_VERSION_SHIFT]);
4011 break;
4012
4013 default:
4014 out = stpcpy (out, ", <unrecognized ELF version number>");
4015 break;
4016 }
4017
4018 if (E_NDS_ABI_V0 == abi)
4019 {
4020 /* OLD ABI; only used in N1213HC, has performance extension 1. */
4021 out = stpcpy (out, ", Andes Star v1.0, N1213HC, MAC, PERF1");
4022 if (arch == E_NDS_ARCH_STAR_V1_0)
4023 out = stpcpy (out, ", 16b"); /* has 16-bit instructions */
4024 return out;
4025 }
4026
4027 switch (arch)
4028 {
4029 case E_NDS_ARCH_STAR_V1_0:
4030 case E_NDS_ARCH_STAR_V2_0:
4031 case E_NDS_ARCH_STAR_V3_0:
4032 case E_NDS_ARCH_STAR_V3_M:
4033 out += sprintf (out, ", %s", ARCH_STRINGS[arch >> EF_NDS_ARCH_SHIFT]);
4034 break;
4035
4036 default:
4037 out = stpcpy (out, ", <unrecognized architecture>");
4038 /* ARCH version determines how the e_flags are interpreted.
4039 If it is unknown, we cannot proceed. */
4040 return out;
4041 }
4042
4043 /* Newer ABI; Now handle architecture specific flags. */
4044 if (arch == E_NDS_ARCH_STAR_V1_0)
4045 {
4046 if (config & E_NDS32_HAS_MFUSR_PC_INST)
4047 out = stpcpy (out, ", MFUSR_PC");
4048
4049 if (!(config & E_NDS32_HAS_NO_MAC_INST))
4050 out = stpcpy (out, ", MAC");
4051
4052 if (config & E_NDS32_HAS_DIV_INST)
4053 out = stpcpy (out, ", DIV");
4054
4055 if (config & E_NDS32_HAS_16BIT_INST)
4056 out = stpcpy (out, ", 16b");
4057 }
4058 else
4059 {
4060 if (config & E_NDS32_HAS_MFUSR_PC_INST)
4061 {
4062 if (version <= E_NDS32_ELF_VER_1_3)
4063 out = stpcpy (out, ", [B8]");
4064 else
4065 out = stpcpy (out, ", EX9");
4066 }
4067
4068 if (config & E_NDS32_HAS_MAC_DX_INST)
4069 out = stpcpy (out, ", MAC_DX");
4070
4071 if (config & E_NDS32_HAS_DIV_DX_INST)
4072 out = stpcpy (out, ", DIV_DX");
4073
4074 if (config & E_NDS32_HAS_16BIT_INST)
4075 {
4076 if (version <= E_NDS32_ELF_VER_1_3)
4077 out = stpcpy (out, ", 16b");
4078 else
4079 out = stpcpy (out, ", IFC");
4080 }
4081 }
4082
4083 if (config & E_NDS32_HAS_EXT_INST)
4084 out = stpcpy (out, ", PERF1");
4085
4086 if (config & E_NDS32_HAS_EXT2_INST)
4087 out = stpcpy (out, ", PERF2");
4088
4089 if (config & E_NDS32_HAS_FPU_INST)
4090 {
4091 has_fpu = true;
4092 out = stpcpy (out, ", FPU_SP");
4093 }
4094
4095 if (config & E_NDS32_HAS_FPU_DP_INST)
4096 {
4097 has_fpu = true;
4098 out = stpcpy (out, ", FPU_DP");
4099 }
4100
4101 if (config & E_NDS32_HAS_FPU_MAC_INST)
4102 {
4103 has_fpu = true;
4104 out = stpcpy (out, ", FPU_MAC");
4105 }
4106
4107 if (has_fpu)
4108 {
4109 switch ((config & E_NDS32_FPU_REG_CONF) >> E_NDS32_FPU_REG_CONF_SHIFT)
4110 {
4111 case E_NDS32_FPU_REG_8SP_4DP:
4112 out = stpcpy (out, ", FPU_REG:8/4");
4113 break;
4114 case E_NDS32_FPU_REG_16SP_8DP:
4115 out = stpcpy (out, ", FPU_REG:16/8");
4116 break;
4117 case E_NDS32_FPU_REG_32SP_16DP:
4118 out = stpcpy (out, ", FPU_REG:32/16");
4119 break;
4120 case E_NDS32_FPU_REG_32SP_32DP:
4121 out = stpcpy (out, ", FPU_REG:32/32");
4122 break;
4123 }
4124 }
4125
4126 if (config & E_NDS32_HAS_AUDIO_INST)
4127 out = stpcpy (out, ", AUDIO");
4128
4129 if (config & E_NDS32_HAS_STRING_INST)
4130 out = stpcpy (out, ", STR");
4131
4132 if (config & E_NDS32_HAS_REDUCED_REGS)
4133 out = stpcpy (out, ", 16REG");
4134
4135 if (config & E_NDS32_HAS_VIDEO_INST)
4136 {
4137 if (version <= E_NDS32_ELF_VER_1_3)
4138 out = stpcpy (out, ", VIDEO");
4139 else
4140 out = stpcpy (out, ", SATURATION");
4141 }
4142
4143 if (config & E_NDS32_HAS_ENCRIPT_INST)
4144 out = stpcpy (out, ", ENCRP");
4145
4146 if (config & E_NDS32_HAS_L2C_INST)
4147 out = stpcpy (out, ", L2C");
4148
4149 return out;
4150 }
4151
4152 static char *
4153 decode_PARISC_machine_flags (char *out, unsigned e_flags)
4154 {
4155 switch (e_flags & EF_PARISC_ARCH)
4156 {
4157 case EFA_PARISC_1_0:
4158 out = stpcpy (out, ", PA-RISC 1.0");
4159 break;
4160 case EFA_PARISC_1_1:
4161 out = stpcpy (out, ", PA-RISC 1.1");
4162 break;
4163 case EFA_PARISC_2_0:
4164 out = stpcpy (out, ", PA-RISC 2.0");
4165 break;
4166 default:
4167 break;
4168 }
4169 if (e_flags & EF_PARISC_TRAPNIL)
4170 out = stpcpy (out, ", trapnil");
4171 if (e_flags & EF_PARISC_EXT)
4172 out = stpcpy (out, ", ext");
4173 if (e_flags & EF_PARISC_LSB)
4174 out = stpcpy (out, ", lsb");
4175 if (e_flags & EF_PARISC_WIDE)
4176 out = stpcpy (out, ", wide");
4177 if (e_flags & EF_PARISC_NO_KABP)
4178 out = stpcpy (out, ", no kabp");
4179 if (e_flags & EF_PARISC_LAZYSWAP)
4180 out = stpcpy (out, ", lazyswap");
4181 return out;
4182 }
4183
4184 static char *
4185 decode_RISCV_machine_flags (char *out, unsigned e_flags)
4186 {
4187 if (e_flags & EF_RISCV_RVC)
4188 out = stpcpy (out, ", RVC");
4189
4190 if (e_flags & EF_RISCV_RVE)
4191 out = stpcpy (out, ", RVE");
4192
4193 if (e_flags & EF_RISCV_TSO)
4194 out = stpcpy (out, ", TSO");
4195
4196 switch (e_flags & EF_RISCV_FLOAT_ABI)
4197 {
4198 case EF_RISCV_FLOAT_ABI_SOFT:
4199 out = stpcpy (out, ", soft-float ABI");
4200 break;
4201
4202 case EF_RISCV_FLOAT_ABI_SINGLE:
4203 out = stpcpy (out, ", single-float ABI");
4204 break;
4205
4206 case EF_RISCV_FLOAT_ABI_DOUBLE:
4207 out = stpcpy (out, ", double-float ABI");
4208 break;
4209
4210 case EF_RISCV_FLOAT_ABI_QUAD:
4211 out = stpcpy (out, ", quad-float ABI");
4212 break;
4213 }
4214 return out;
4215 }
4216
4217 static char *
4218 decode_RL78_machine_flags (char *out, unsigned e_flags)
4219 {
4220 switch (e_flags & E_FLAG_RL78_CPU_MASK)
4221 {
4222 case E_FLAG_RL78_ANY_CPU:
4223 break;
4224 case E_FLAG_RL78_G10:
4225 out = stpcpy (out, ", G10");
4226 break;
4227 case E_FLAG_RL78_G13:
4228 out = stpcpy (out, ", G13");
4229 break;
4230 case E_FLAG_RL78_G14:
4231 out = stpcpy (out, ", G14");
4232 break;
4233 }
4234 if (e_flags & E_FLAG_RL78_64BIT_DOUBLES)
4235 out = stpcpy (out, ", 64-bit doubles");
4236 return out;
4237 }
4238
4239 static char *
4240 decode_RX_machine_flags (char *out, unsigned e_flags)
4241 {
4242 if (e_flags & E_FLAG_RX_64BIT_DOUBLES)
4243 out = stpcpy (out, ", 64-bit doubles");
4244 if (e_flags & E_FLAG_RX_DSP)
4245 out = stpcpy (out, ", dsp");
4246 if (e_flags & E_FLAG_RX_PID)
4247 out = stpcpy (out, ", pid");
4248 if (e_flags & E_FLAG_RX_ABI)
4249 out = stpcpy (out, ", RX ABI");
4250 if (e_flags & E_FLAG_RX_SINSNS_SET)
4251 out = stpcpy (out, (e_flags & E_FLAG_RX_SINSNS_YES
4252 ? ", uses String instructions"
4253 : ", bans String instructions"));
4254 if (e_flags & E_FLAG_RX_V2)
4255 out = stpcpy (out, ", V2");
4256 if (e_flags & E_FLAG_RX_V3)
4257 out = stpcpy (out, ", V3");
4258 return out;
4259 }
4260
4261 static char *
4262 decode_SH_machine_flags (char *out, unsigned e_flags)
4263 {
4264 switch ((e_flags & EF_SH_MACH_MASK))
4265 {
4266 case EF_SH1:
4267 out = stpcpy (out, ", sh1");
4268 break;
4269 case EF_SH2:
4270 out = stpcpy (out, ", sh2");
4271 break;
4272 case EF_SH3:
4273 out = stpcpy (out, ", sh3");
4274 break;
4275 case EF_SH_DSP:
4276 out = stpcpy (out, ", sh-dsp");
4277 break;
4278 case EF_SH3_DSP:
4279 out = stpcpy (out, ", sh3-dsp");
4280 break;
4281 case EF_SH4AL_DSP:
4282 out = stpcpy (out, ", sh4al-dsp");
4283 break;
4284 case EF_SH3E:
4285 out = stpcpy (out, ", sh3e");
4286 break;
4287 case EF_SH4:
4288 out = stpcpy (out, ", sh4");
4289 break;
4290 case EF_SH5:
4291 out = stpcpy (out, ", sh5");
4292 break;
4293 case EF_SH2E:
4294 out = stpcpy (out, ", sh2e");
4295 break;
4296 case EF_SH4A:
4297 out = stpcpy (out, ", sh4a");
4298 break;
4299 case EF_SH2A:
4300 out = stpcpy (out, ", sh2a");
4301 break;
4302 case EF_SH4_NOFPU:
4303 out = stpcpy (out, ", sh4-nofpu");
4304 break;
4305 case EF_SH4A_NOFPU:
4306 out = stpcpy (out, ", sh4a-nofpu");
4307 break;
4308 case EF_SH2A_NOFPU:
4309 out = stpcpy (out, ", sh2a-nofpu");
4310 break;
4311 case EF_SH3_NOMMU:
4312 out = stpcpy (out, ", sh3-nommu");
4313 break;
4314 case EF_SH4_NOMMU_NOFPU:
4315 out = stpcpy (out, ", sh4-nommu-nofpu");
4316 break;
4317 case EF_SH2A_SH4_NOFPU:
4318 out = stpcpy (out, ", sh2a-nofpu-or-sh4-nommu-nofpu");
4319 break;
4320 case EF_SH2A_SH3_NOFPU:
4321 out = stpcpy (out, ", sh2a-nofpu-or-sh3-nommu");
4322 break;
4323 case EF_SH2A_SH4:
4324 out = stpcpy (out, ", sh2a-or-sh4");
4325 break;
4326 case EF_SH2A_SH3E:
4327 out = stpcpy (out, ", sh2a-or-sh3e");
4328 break;
4329 default:
4330 out = stpcpy (out, _(", unknown ISA"));
4331 break;
4332 }
4333
4334 if (e_flags & EF_SH_PIC)
4335 out = stpcpy (out, ", pic");
4336
4337 if (e_flags & EF_SH_FDPIC)
4338 out = stpcpy (out, ", fdpic");
4339 return out;
4340 }
4341
4342 static char *
4343 decode_SPARC_machine_flags (char *out, unsigned e_flags)
4344 {
4345 if (e_flags & EF_SPARC_32PLUS)
4346 out = stpcpy (out, ", v8+");
4347
4348 if (e_flags & EF_SPARC_SUN_US1)
4349 out = stpcpy (out, ", ultrasparcI");
4350
4351 if (e_flags & EF_SPARC_SUN_US3)
4352 out = stpcpy (out, ", ultrasparcIII");
4353
4354 if (e_flags & EF_SPARC_HAL_R1)
4355 out = stpcpy (out, ", halr1");
4356
4357 if (e_flags & EF_SPARC_LEDATA)
4358 out = stpcpy (out, ", ledata");
4359
4360 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
4361 out = stpcpy (out, ", tso");
4362
4363 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
4364 out = stpcpy (out, ", pso");
4365
4366 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
4367 out = stpcpy (out, ", rmo");
4368 return out;
4369 }
4370
4371 static char *
4372 decode_V800_machine_flags (char *out, unsigned int e_flags)
4373 {
4374 if ((e_flags & EF_RH850_ABI) == EF_RH850_ABI)
4375 out = stpcpy (out, ", RH850 ABI");
4376
4377 if (e_flags & EF_V800_850E3)
4378 out = stpcpy (out, ", V3 architecture");
4379
4380 if ((e_flags & (EF_RH850_FPU_DOUBLE | EF_RH850_FPU_SINGLE)) == 0)
4381 out = stpcpy (out, ", FPU not used");
4382
4383 if ((e_flags & (EF_RH850_REGMODE22 | EF_RH850_REGMODE32)) == 0)
4384 out = stpcpy (out, ", regmode: COMMON");
4385
4386 if ((e_flags & (EF_RH850_GP_FIX | EF_RH850_GP_NOFIX)) == 0)
4387 out = stpcpy (out, ", r4 not used");
4388
4389 if ((e_flags & (EF_RH850_EP_FIX | EF_RH850_EP_NOFIX)) == 0)
4390 out = stpcpy (out, ", r30 not used");
4391
4392 if ((e_flags & (EF_RH850_TP_FIX | EF_RH850_TP_NOFIX)) == 0)
4393 out = stpcpy (out, ", r5 not used");
4394
4395 if ((e_flags & (EF_RH850_REG2_RESERVE | EF_RH850_REG2_NORESERVE)) == 0)
4396 out = stpcpy (out, ", r2 not used");
4397
4398 for (e_flags &= 0xFFFF; e_flags; e_flags &= ~ (e_flags & - e_flags))
4399 {
4400 switch (e_flags & - e_flags)
4401 {
4402 case EF_RH850_FPU_DOUBLE:
4403 out = stpcpy (out, ", double precision FPU");
4404 break;
4405 case EF_RH850_FPU_SINGLE:
4406 out = stpcpy (out, ", single precision FPU");
4407 break;
4408 case EF_RH850_REGMODE22:
4409 out = stpcpy (out, ", regmode:22");
4410 break;
4411 case EF_RH850_REGMODE32:
4412 out = stpcpy (out, ", regmode:23");
4413 break;
4414 case EF_RH850_GP_FIX:
4415 out = stpcpy (out, ", r4 fixed");
4416 break;
4417 case EF_RH850_GP_NOFIX:
4418 out = stpcpy (out, ", r4 free");
4419 break;
4420 case EF_RH850_EP_FIX:
4421 out = stpcpy (out, ", r30 fixed");
4422 break;
4423 case EF_RH850_EP_NOFIX:
4424 out = stpcpy (out, ", r30 free");
4425 break;
4426 case EF_RH850_TP_FIX:
4427 out = stpcpy (out, ", r5 fixed");
4428 break;
4429 case EF_RH850_TP_NOFIX:
4430 out = stpcpy (out, ", r5 free");
4431 break;
4432 case EF_RH850_REG2_RESERVE:
4433 out = stpcpy (out, ", r2 fixed");
4434 break;
4435 case EF_RH850_REG2_NORESERVE:
4436 out = stpcpy (out, ", r2 free");
4437 break;
4438 default:
4439 break;
4440 }
4441 }
4442 return out;
4443 }
4444
4445 static char *
4446 decode_V850_machine_flags (char *out, unsigned int e_flags)
4447 {
4448 switch (e_flags & EF_V850_ARCH)
4449 {
4450 case E_V850E3V5_ARCH:
4451 out = stpcpy (out, ", v850e3v5");
4452 break;
4453 case E_V850E2V3_ARCH:
4454 out = stpcpy (out, ", v850e2v3");
4455 break;
4456 case E_V850E2_ARCH:
4457 out = stpcpy (out, ", v850e2");
4458 break;
4459 case E_V850E1_ARCH:
4460 out = stpcpy (out, ", v850e1");
4461 break;
4462 case E_V850E_ARCH:
4463 out = stpcpy (out, ", v850e");
4464 break;
4465 case E_V850_ARCH:
4466 out = stpcpy (out, ", v850");
4467 break;
4468 default:
4469 out = stpcpy (out, _(", unknown v850 architecture variant"));
4470 break;
4471 }
4472 return out;
4473 }
4474
4475 static char *
4476 decode_Z80_machine_flags (char *out, unsigned int e_flags)
4477 {
4478 switch (e_flags & EF_Z80_MACH_MSK)
4479 {
4480 case EF_Z80_MACH_Z80:
4481 out = stpcpy (out, ", Z80");
4482 break;
4483 case EF_Z80_MACH_Z180:
4484 out = stpcpy (out, ", Z180");
4485 break;
4486 case EF_Z80_MACH_R800:
4487 out = stpcpy (out, ", R800");
4488 break;
4489 case EF_Z80_MACH_EZ80_Z80:
4490 out = stpcpy (out, ", EZ80");
4491 break;
4492 case EF_Z80_MACH_EZ80_ADL:
4493 out = stpcpy (out, ", EZ80, ADL");
4494 break;
4495 case EF_Z80_MACH_GBZ80:
4496 out = stpcpy (out, ", GBZ80");
4497 break;
4498 case EF_Z80_MACH_Z80N:
4499 out = stpcpy (out, ", Z80N");
4500 break;
4501 default:
4502 out = stpcpy (out, _(", unknown"));
4503 break;
4504 }
4505 return out;
4506 }
4507
4508 static char *
4509 decode_AMDGPU_machine_flags (char *out, unsigned int e_flags, Filedata *filedata)
4510 {
4511 unsigned char *e_ident = filedata->file_header.e_ident;
4512 unsigned char osabi = e_ident[EI_OSABI];
4513 unsigned char abiversion = e_ident[EI_ABIVERSION];
4514 unsigned int mach;
4515
4516 /* HSA OS ABI v2 used a different encoding, but we don't need to support it,
4517 it has been deprecated for a while.
4518
4519 The PAL, MESA3D and NONE OS ABIs are not properly versioned, at the time
4520 of writing, they use the same flags as HSA v3, so the code below uses that
4521 assumption. */
4522 if (osabi == ELFOSABI_AMDGPU_HSA && abiversion < ELFABIVERSION_AMDGPU_HSA_V3)
4523 return out;
4524
4525 mach = e_flags & EF_AMDGPU_MACH;
4526 switch (mach)
4527 {
4528 #define AMDGPU_CASE(code, string) \
4529 case code: out = stpcpy (out, ", " string); break;
4530 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX600, "gfx600")
4531 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX601, "gfx601")
4532 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX700, "gfx700")
4533 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX701, "gfx701")
4534 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX702, "gfx702")
4535 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX703, "gfx703")
4536 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX704, "gfx704")
4537 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX801, "gfx801")
4538 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX802, "gfx802")
4539 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX803, "gfx803")
4540 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX810, "gfx810")
4541 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX900, "gfx900")
4542 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX902, "gfx902")
4543 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX904, "gfx904")
4544 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX906, "gfx906")
4545 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX908, "gfx908")
4546 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX909, "gfx909")
4547 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90C, "gfx90c")
4548 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1010, "gfx1010")
4549 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1011, "gfx1011")
4550 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1012, "gfx1012")
4551 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1030, "gfx1030")
4552 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1031, "gfx1031")
4553 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1032, "gfx1032")
4554 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1033, "gfx1033")
4555 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX602, "gfx602")
4556 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX705, "gfx705")
4557 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX805, "gfx805")
4558 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1035, "gfx1035")
4559 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1034, "gfx1034")
4560 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX90A, "gfx90a")
4561 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX940, "gfx940")
4562 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1013, "gfx1013")
4563 AMDGPU_CASE (EF_AMDGPU_MACH_AMDGCN_GFX1036, "gfx1036")
4564 default:
4565 out += sprintf (out, _(", <unknown AMDGPU GPU type: %#x>"), mach);
4566 break;
4567 #undef AMDGPU_CASE
4568 }
4569
4570 e_flags &= ~EF_AMDGPU_MACH;
4571
4572 if ((osabi == ELFOSABI_AMDGPU_HSA
4573 && abiversion == ELFABIVERSION_AMDGPU_HSA_V3)
4574 || osabi != ELFOSABI_AMDGPU_HSA)
4575 {
4576 /* For HSA v3 and other OS ABIs. */
4577 if (e_flags & EF_AMDGPU_FEATURE_XNACK_V3)
4578 {
4579 out = stpcpy (out, ", xnack on");
4580 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V3;
4581 }
4582
4583 if (e_flags & EF_AMDGPU_FEATURE_SRAMECC_V3)
4584 {
4585 out = stpcpy (out, ", sramecc on");
4586 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V3;
4587 }
4588 }
4589 else
4590 {
4591 /* For HSA v4+. */
4592 int xnack, sramecc;
4593
4594 xnack = e_flags & EF_AMDGPU_FEATURE_XNACK_V4;
4595 switch (xnack)
4596 {
4597 case EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4:
4598 break;
4599
4600 case EF_AMDGPU_FEATURE_XNACK_ANY_V4:
4601 out = stpcpy (out, ", xnack any");
4602 break;
4603
4604 case EF_AMDGPU_FEATURE_XNACK_OFF_V4:
4605 out = stpcpy (out, ", xnack off");
4606 break;
4607
4608 case EF_AMDGPU_FEATURE_XNACK_ON_V4:
4609 out = stpcpy (out, ", xnack on");
4610 break;
4611
4612 default:
4613 out += sprintf (out, _(", <unknown xnack value: %#x>"), xnack);
4614 break;
4615 }
4616
4617 e_flags &= ~EF_AMDGPU_FEATURE_XNACK_V4;
4618
4619 sramecc = e_flags & EF_AMDGPU_FEATURE_SRAMECC_V4;
4620 switch (sramecc)
4621 {
4622 case EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4:
4623 break;
4624
4625 case EF_AMDGPU_FEATURE_SRAMECC_ANY_V4:
4626 out = stpcpy (out, ", sramecc any");
4627 break;
4628
4629 case EF_AMDGPU_FEATURE_SRAMECC_OFF_V4:
4630 out = stpcpy (out, ", sramecc off");
4631 break;
4632
4633 case EF_AMDGPU_FEATURE_SRAMECC_ON_V4:
4634 out = stpcpy (out, ", sramecc on");
4635 break;
4636
4637 default:
4638 out += sprintf (out, _(", <unknown sramecc value: %#x>"), sramecc);
4639 break;
4640 }
4641
4642 e_flags &= ~EF_AMDGPU_FEATURE_SRAMECC_V4;
4643 }
4644
4645 if (e_flags != 0)
4646 out += sprintf (out, _(", unknown flags bits: %#x"), e_flags);
4647 return out;
4648 }
4649
4650 static char *
4651 get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine)
4652 {
4653 static char buf[1024];
4654 char *out = buf;
4655
4656 buf[0] = '\0';
4657
4658 if (e_flags)
4659 {
4660 switch (e_machine)
4661 {
4662 default:
4663 break;
4664
4665 case EM_ARC_COMPACT3:
4666 out = stpcpy (out, ", HS5x");
4667 break;
4668
4669 case EM_ARC_COMPACT3_64:
4670 out = stpcpy (out, ", HS6x");
4671 break;
4672
4673 case EM_ARC_COMPACT2:
4674 case EM_ARC_COMPACT:
4675 out = decode_ARC_machine_flags (out, e_flags, e_machine);
4676 break;
4677
4678 case EM_ARM:
4679 out = decode_ARM_machine_flags (out, e_flags);
4680 break;
4681
4682 case EM_AVR:
4683 out = decode_AVR_machine_flags (out, e_flags);
4684 break;
4685
4686 case EM_BLACKFIN:
4687 out = decode_BLACKFIN_machine_flags (out, e_flags);
4688 break;
4689
4690 case EM_CYGNUS_FRV:
4691 out = decode_FRV_machine_flags (out, e_flags);
4692 break;
4693
4694 case EM_68K:
4695 out = decode_M68K_machine_flags (out, e_flags);
4696 break;
4697
4698 case EM_AMDGPU:
4699 out = decode_AMDGPU_machine_flags (out, e_flags, filedata);
4700 break;
4701
4702 case EM_CYGNUS_MEP:
4703 out = decode_MeP_machine_flags (out, e_flags);
4704 break;
4705
4706 case EM_PPC:
4707 if (e_flags & EF_PPC_EMB)
4708 out = stpcpy (out, ", emb");
4709
4710 if (e_flags & EF_PPC_RELOCATABLE)
4711 out = stpcpy (out, _(", relocatable"));
4712
4713 if (e_flags & EF_PPC_RELOCATABLE_LIB)
4714 out = stpcpy (out, _(", relocatable-lib"));
4715 break;
4716
4717 case EM_PPC64:
4718 if (e_flags & EF_PPC64_ABI)
4719 out += sprintf (out, ", abiv%d", e_flags & EF_PPC64_ABI);
4720 break;
4721
4722 case EM_V800:
4723 out = decode_V800_machine_flags (out, e_flags);
4724 break;
4725
4726 case EM_V850:
4727 case EM_CYGNUS_V850:
4728 out = decode_V850_machine_flags (out, e_flags);
4729 break;
4730
4731 case EM_M32R:
4732 case EM_CYGNUS_M32R:
4733 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
4734 out = stpcpy (out, ", m32r");
4735 break;
4736
4737 case EM_MIPS:
4738 case EM_MIPS_RS3_LE:
4739 out = decode_MIPS_machine_flags (out, e_flags);
4740 break;
4741
4742 case EM_NDS32:
4743 out = decode_NDS32_machine_flags (out, e_flags);
4744 break;
4745
4746 case EM_NFP:
4747 switch (EF_NFP_MACH (e_flags))
4748 {
4749 case E_NFP_MACH_3200:
4750 out = stpcpy (out, ", NFP-32xx");
4751 break;
4752 case E_NFP_MACH_6000:
4753 out = stpcpy (out, ", NFP-6xxx");
4754 break;
4755 }
4756 break;
4757
4758 case EM_RISCV:
4759 out = decode_RISCV_machine_flags (out, e_flags);
4760 break;
4761
4762 case EM_SH:
4763 out = decode_SH_machine_flags (out, e_flags);
4764 break;
4765
4766 case EM_OR1K:
4767 if (e_flags & EF_OR1K_NODELAY)
4768 out = stpcpy (out, ", no delay");
4769 break;
4770
4771 case EM_BPF:
4772 out += sprintf (out, ", CPU Version: %u", e_flags & EF_BPF_CPUVER);
4773 break;
4774
4775 case EM_SPARCV9:
4776 out = decode_SPARC_machine_flags (out, e_flags);
4777 break;
4778
4779 case EM_PARISC:
4780 out = decode_PARISC_machine_flags (out, e_flags);
4781 break;
4782
4783 case EM_PJ:
4784 case EM_PJ_OLD:
4785 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
4786 out = stpcpy (out, ", new calling convention");
4787
4788 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
4789 out = stpcpy (out, ", gnu calling convention");
4790 break;
4791
4792 case EM_IA_64:
4793 out = decode_IA64_machine_flags (out, e_flags, filedata);
4794 break;
4795
4796 case EM_VAX:
4797 if ((e_flags & EF_VAX_NONPIC))
4798 out = stpcpy (out, ", non-PIC");
4799 if ((e_flags & EF_VAX_DFLOAT))
4800 out = stpcpy (out, ", D-Float");
4801 if ((e_flags & EF_VAX_GFLOAT))
4802 out = stpcpy (out, ", G-Float");
4803 break;
4804
4805 case EM_VISIUM:
4806 if (e_flags & EF_VISIUM_ARCH_MCM)
4807 out = stpcpy (out, ", mcm");
4808 else if (e_flags & EF_VISIUM_ARCH_MCM24)
4809 out = stpcpy (out, ", mcm24");
4810 if (e_flags & EF_VISIUM_ARCH_GR6)
4811 out = stpcpy (out, ", gr6");
4812 break;
4813
4814 case EM_RL78:
4815 out = decode_RL78_machine_flags (out, e_flags);
4816 break;
4817
4818 case EM_RX:
4819 out = decode_RX_machine_flags (out, e_flags);
4820 break;
4821
4822 case EM_S390:
4823 if (e_flags & EF_S390_HIGH_GPRS)
4824 out = stpcpy (out, ", highgprs");
4825 break;
4826
4827 case EM_TI_C6000:
4828 if ((e_flags & EF_C6000_REL))
4829 out = stpcpy (out, ", relocatable module");
4830 break;
4831
4832 case EM_KVX:
4833 if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV3_1)
4834 strcat (buf, ", Kalray VLIW kv3-1");
4835 else if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV3_2)
4836 strcat (buf, ", Kalray VLIW kv3-2");
4837 else if ((e_flags & (ELF_KVX_CORE_MAJOR_MASK | ELF_KVX_CORE_MINOR_MASK)) == ELF_KVX_CORE_KV4_1)
4838 strcat (buf, ", Kalray VLIW kv4-1");
4839 else
4840 strcat (buf, ", unknown KVX MPPA");
4841 break;
4842
4843 case EM_MSP430:
4844 out = decode_MSP430_machine_flags (out, e_flags);
4845 break;
4846
4847 case EM_Z80:
4848 out = decode_Z80_machine_flags (out, e_flags);
4849 break;
4850
4851 case EM_LOONGARCH:
4852 out = decode_LOONGARCH_machine_flags (out, e_flags);
4853 break;
4854 }
4855 }
4856
4857 return buf;
4858 }
4859
4860 static const char *
4861 get_osabi_name (Filedata * filedata, unsigned int osabi)
4862 {
4863 static char buff[32];
4864
4865 switch (osabi)
4866 {
4867 case ELFOSABI_NONE: return "UNIX - System V";
4868 case ELFOSABI_HPUX: return "UNIX - HP-UX";
4869 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
4870 case ELFOSABI_GNU: return "UNIX - GNU";
4871 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
4872 case ELFOSABI_AIX: return "UNIX - AIX";
4873 case ELFOSABI_IRIX: return "UNIX - IRIX";
4874 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
4875 case ELFOSABI_TRU64: return "UNIX - TRU64";
4876 case ELFOSABI_MODESTO: return "Novell - Modesto";
4877 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
4878 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
4879 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
4880 case ELFOSABI_AROS: return "AROS";
4881 case ELFOSABI_FENIXOS: return "FenixOS";
4882 case ELFOSABI_CLOUDABI: return "Nuxi CloudABI";
4883 case ELFOSABI_OPENVOS: return "Stratus Technologies OpenVOS";
4884 default:
4885 if (osabi >= 64)
4886 switch (filedata->file_header.e_machine)
4887 {
4888 case EM_AMDGPU:
4889 switch (osabi)
4890 {
4891 case ELFOSABI_AMDGPU_HSA: return "AMD HSA";
4892 case ELFOSABI_AMDGPU_PAL: return "AMD PAL";
4893 case ELFOSABI_AMDGPU_MESA3D: return "AMD Mesa3D";
4894 default:
4895 break;
4896 }
4897 break;
4898
4899 case EM_ARM:
4900 switch (osabi)
4901 {
4902 case ELFOSABI_ARM: return "ARM";
4903 case ELFOSABI_ARM_FDPIC: return "ARM FDPIC";
4904 default:
4905 break;
4906 }
4907 break;
4908
4909 case EM_MSP430:
4910 case EM_MSP430_OLD:
4911 case EM_VISIUM:
4912 switch (osabi)
4913 {
4914 case ELFOSABI_STANDALONE: return _("Standalone App");
4915 default:
4916 break;
4917 }
4918 break;
4919
4920 case EM_TI_C6000:
4921 switch (osabi)
4922 {
4923 case ELFOSABI_C6000_ELFABI: return _("Bare-metal C6000");
4924 case ELFOSABI_C6000_LINUX: return "Linux C6000";
4925 default:
4926 break;
4927 }
4928 break;
4929
4930 default:
4931 break;
4932 }
4933 snprintf (buff, sizeof (buff), _("<unknown: %x>"), osabi);
4934 return buff;
4935 }
4936 }
4937
4938 static const char *
4939 get_aarch64_segment_type (unsigned long type)
4940 {
4941 switch (type)
4942 {
4943 case PT_AARCH64_ARCHEXT: return "AARCH64_ARCHEXT";
4944 case PT_AARCH64_MEMTAG_MTE: return "AARCH64_MEMTAG_MTE";
4945 default: return NULL;
4946 }
4947 }
4948
4949 static const char *
4950 get_arm_segment_type (unsigned long type)
4951 {
4952 switch (type)
4953 {
4954 case PT_ARM_EXIDX: return "EXIDX";
4955 default: return NULL;
4956 }
4957 }
4958
4959 static const char *
4960 get_s390_segment_type (unsigned long type)
4961 {
4962 switch (type)
4963 {
4964 case PT_S390_PGSTE: return "S390_PGSTE";
4965 default: return NULL;
4966 }
4967 }
4968
4969 static const char *
4970 get_mips_segment_type (unsigned long type)
4971 {
4972 switch (type)
4973 {
4974 case PT_MIPS_REGINFO: return "REGINFO";
4975 case PT_MIPS_RTPROC: return "RTPROC";
4976 case PT_MIPS_OPTIONS: return "OPTIONS";
4977 case PT_MIPS_ABIFLAGS: return "ABIFLAGS";
4978 default: return NULL;
4979 }
4980 }
4981
4982 static const char *
4983 get_parisc_segment_type (unsigned long type)
4984 {
4985 switch (type)
4986 {
4987 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
4988 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
4989 case PT_PARISC_WEAKORDER: return "PARISC_WEAKORDER";
4990 default: return NULL;
4991 }
4992 }
4993
4994 static const char *
4995 get_ia64_segment_type (unsigned long type)
4996 {
4997 switch (type)
4998 {
4999 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
5000 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
5001 default: return NULL;
5002 }
5003 }
5004
5005 static const char *
5006 get_tic6x_segment_type (unsigned long type)
5007 {
5008 switch (type)
5009 {
5010 case PT_C6000_PHATTR: return "C6000_PHATTR";
5011 default: return NULL;
5012 }
5013 }
5014
5015 static const char *
5016 get_riscv_segment_type (unsigned long type)
5017 {
5018 switch (type)
5019 {
5020 case PT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
5021 default: return NULL;
5022 }
5023 }
5024
5025 static const char *
5026 get_hpux_segment_type (unsigned long type, unsigned e_machine)
5027 {
5028 if (e_machine == EM_PARISC)
5029 switch (type)
5030 {
5031 case PT_HP_TLS: return "HP_TLS";
5032 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
5033 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
5034 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
5035 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
5036 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
5037 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
5038 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
5039 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
5040 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
5041 case PT_HP_PARALLEL: return "HP_PARALLEL";
5042 case PT_HP_FASTBIND: return "HP_FASTBIND";
5043 case PT_HP_OPT_ANNOT: return "HP_OPT_ANNOT";
5044 case PT_HP_HSL_ANNOT: return "HP_HSL_ANNOT";
5045 case PT_HP_STACK: return "HP_STACK";
5046 case PT_HP_CORE_UTSNAME: return "HP_CORE_UTSNAME";
5047 default: return NULL;
5048 }
5049
5050 if (e_machine == EM_IA_64)
5051 switch (type)
5052 {
5053 case PT_HP_TLS: return "HP_TLS";
5054 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
5055 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
5056 case PT_IA_64_HP_STACK: return "HP_STACK";
5057 default: return NULL;
5058 }
5059
5060 return NULL;
5061 }
5062
5063 static const char *
5064 get_solaris_segment_type (unsigned long type)
5065 {
5066 switch (type)
5067 {
5068 case 0x6464e550: return "PT_SUNW_UNWIND";
5069 case 0x6474e550: return "PT_SUNW_EH_FRAME";
5070 case 0x6ffffff7: return "PT_LOSUNW";
5071 case 0x6ffffffa: return "PT_SUNWBSS";
5072 case 0x6ffffffb: return "PT_SUNWSTACK";
5073 case 0x6ffffffc: return "PT_SUNWDTRACE";
5074 case 0x6ffffffd: return "PT_SUNWCAP";
5075 case 0x6fffffff: return "PT_HISUNW";
5076 default: return NULL;
5077 }
5078 }
5079
5080 static const char *
5081 get_segment_type (Filedata * filedata, unsigned long p_type)
5082 {
5083 static char buff[32];
5084
5085 switch (p_type)
5086 {
5087 case PT_NULL: return "NULL";
5088 case PT_LOAD: return "LOAD";
5089 case PT_DYNAMIC: return "DYNAMIC";
5090 case PT_INTERP: return "INTERP";
5091 case PT_NOTE: return "NOTE";
5092 case PT_SHLIB: return "SHLIB";
5093 case PT_PHDR: return "PHDR";
5094 case PT_TLS: return "TLS";
5095 case PT_GNU_EH_FRAME: return "GNU_EH_FRAME";
5096 case PT_GNU_STACK: return "GNU_STACK";
5097 case PT_GNU_RELRO: return "GNU_RELRO";
5098 case PT_GNU_PROPERTY: return "GNU_PROPERTY";
5099 case PT_GNU_SFRAME: return "GNU_SFRAME";
5100
5101 case PT_OPENBSD_MUTABLE: return "OPENBSD_MUTABLE";
5102 case PT_OPENBSD_RANDOMIZE: return "OPENBSD_RANDOMIZE";
5103 case PT_OPENBSD_WXNEEDED: return "OPENBSD_WXNEEDED";
5104 case PT_OPENBSD_NOBTCFI: return "OPENBSD_NOBTCFI";
5105 case PT_OPENBSD_BOOTDATA: return "OPENBSD_BOOTDATA";
5106
5107 default:
5108 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
5109 {
5110 const char * result;
5111
5112 switch (filedata->file_header.e_machine)
5113 {
5114 case EM_AARCH64:
5115 result = get_aarch64_segment_type (p_type);
5116 break;
5117 case EM_ARM:
5118 result = get_arm_segment_type (p_type);
5119 break;
5120 case EM_MIPS:
5121 case EM_MIPS_RS3_LE:
5122 result = get_mips_segment_type (p_type);
5123 break;
5124 case EM_PARISC:
5125 result = get_parisc_segment_type (p_type);
5126 break;
5127 case EM_IA_64:
5128 result = get_ia64_segment_type (p_type);
5129 break;
5130 case EM_TI_C6000:
5131 result = get_tic6x_segment_type (p_type);
5132 break;
5133 case EM_S390:
5134 case EM_S390_OLD:
5135 result = get_s390_segment_type (p_type);
5136 break;
5137 case EM_RISCV:
5138 result = get_riscv_segment_type (p_type);
5139 break;
5140 default:
5141 result = NULL;
5142 break;
5143 }
5144
5145 if (result != NULL)
5146 return result;
5147
5148 sprintf (buff, "LOPROC+%#lx", p_type - PT_LOPROC);
5149 }
5150 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
5151 {
5152 const char * result = NULL;
5153
5154 switch (filedata->file_header.e_ident[EI_OSABI])
5155 {
5156 case ELFOSABI_GNU:
5157 case ELFOSABI_FREEBSD:
5158 if (p_type >= PT_GNU_MBIND_LO && p_type <= PT_GNU_MBIND_HI)
5159 {
5160 sprintf (buff, "GNU_MBIND+%#lx", p_type - PT_GNU_MBIND_LO);
5161 result = buff;
5162 }
5163 break;
5164 case ELFOSABI_HPUX:
5165 result = get_hpux_segment_type (p_type,
5166 filedata->file_header.e_machine);
5167 break;
5168 case ELFOSABI_SOLARIS:
5169 result = get_solaris_segment_type (p_type);
5170 break;
5171 default:
5172 break;
5173 }
5174 if (result != NULL)
5175 return result;
5176
5177 sprintf (buff, "LOOS+%#lx", p_type - PT_LOOS);
5178 }
5179 else
5180 snprintf (buff, sizeof (buff), _("<unknown>: %lx"), p_type);
5181
5182 return buff;
5183 }
5184 }
5185
5186 static const char *
5187 get_arc_section_type_name (unsigned int sh_type)
5188 {
5189 switch (sh_type)
5190 {
5191 case SHT_ARC_ATTRIBUTES: return "ARC_ATTRIBUTES";
5192 default:
5193 break;
5194 }
5195 return NULL;
5196 }
5197
5198 static const char *
5199 get_mips_section_type_name (unsigned int sh_type)
5200 {
5201 switch (sh_type)
5202 {
5203 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
5204 case SHT_MIPS_MSYM: return "MIPS_MSYM";
5205 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
5206 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
5207 case SHT_MIPS_UCODE: return "MIPS_UCODE";
5208 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
5209 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
5210 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
5211 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
5212 case SHT_MIPS_RELD: return "MIPS_RELD";
5213 case SHT_MIPS_IFACE: return "MIPS_IFACE";
5214 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
5215 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
5216 case SHT_MIPS_SHDR: return "MIPS_SHDR";
5217 case SHT_MIPS_FDESC: return "MIPS_FDESC";
5218 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
5219 case SHT_MIPS_DENSE: return "MIPS_DENSE";
5220 case SHT_MIPS_PDESC: return "MIPS_PDESC";
5221 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
5222 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
5223 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
5224 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
5225 case SHT_MIPS_LINE: return "MIPS_LINE";
5226 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
5227 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
5228 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
5229 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
5230 case SHT_MIPS_DWARF: return "MIPS_DWARF";
5231 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
5232 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
5233 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
5234 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
5235 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
5236 case SHT_MIPS_XLATE: return "MIPS_XLATE";
5237 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
5238 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
5239 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
5240 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
5241 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
5242 case SHT_MIPS_ABIFLAGS: return "MIPS_ABIFLAGS";
5243 case SHT_MIPS_XHASH: return "MIPS_XHASH";
5244 default:
5245 break;
5246 }
5247 return NULL;
5248 }
5249
5250 static const char *
5251 get_parisc_section_type_name (unsigned int sh_type)
5252 {
5253 switch (sh_type)
5254 {
5255 case SHT_PARISC_EXT: return "PARISC_EXT";
5256 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
5257 case SHT_PARISC_DOC: return "PARISC_DOC";
5258 case SHT_PARISC_ANNOT: return "PARISC_ANNOT";
5259 case SHT_PARISC_SYMEXTN: return "PARISC_SYMEXTN";
5260 case SHT_PARISC_STUBS: return "PARISC_STUBS";
5261 case SHT_PARISC_DLKM: return "PARISC_DLKM";
5262 default: return NULL;
5263 }
5264 }
5265
5266 static const char *
5267 get_ia64_section_type_name (Filedata * filedata, unsigned int sh_type)
5268 {
5269 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
5270 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
5271 return get_osabi_name (filedata, (sh_type & 0x00FF0000) >> 16);
5272
5273 switch (sh_type)
5274 {
5275 case SHT_IA_64_EXT: return "IA_64_EXT";
5276 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
5277 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
5278 case SHT_IA_64_VMS_TRACE: return "VMS_TRACE";
5279 case SHT_IA_64_VMS_TIE_SIGNATURES: return "VMS_TIE_SIGNATURES";
5280 case SHT_IA_64_VMS_DEBUG: return "VMS_DEBUG";
5281 case SHT_IA_64_VMS_DEBUG_STR: return "VMS_DEBUG_STR";
5282 case SHT_IA_64_VMS_LINKAGES: return "VMS_LINKAGES";
5283 case SHT_IA_64_VMS_SYMBOL_VECTOR: return "VMS_SYMBOL_VECTOR";
5284 case SHT_IA_64_VMS_FIXUP: return "VMS_FIXUP";
5285 default:
5286 break;
5287 }
5288 return NULL;
5289 }
5290
5291 static const char *
5292 get_x86_64_section_type_name (unsigned int sh_type)
5293 {
5294 switch (sh_type)
5295 {
5296 case SHT_X86_64_UNWIND: return "X86_64_UNWIND";
5297 default: return NULL;
5298 }
5299 }
5300
5301 static const char *
5302 get_aarch64_section_type_name (unsigned int sh_type)
5303 {
5304 switch (sh_type)
5305 {
5306 case SHT_AARCH64_ATTRIBUTES: return "AARCH64_ATTRIBUTES";
5307 default: return NULL;
5308 }
5309 }
5310
5311 static const char *
5312 get_arm_section_type_name (unsigned int sh_type)
5313 {
5314 switch (sh_type)
5315 {
5316 case SHT_ARM_EXIDX: return "ARM_EXIDX";
5317 case SHT_ARM_PREEMPTMAP: return "ARM_PREEMPTMAP";
5318 case SHT_ARM_ATTRIBUTES: return "ARM_ATTRIBUTES";
5319 case SHT_ARM_DEBUGOVERLAY: return "ARM_DEBUGOVERLAY";
5320 case SHT_ARM_OVERLAYSECTION: return "ARM_OVERLAYSECTION";
5321 default: return NULL;
5322 }
5323 }
5324
5325 static const char *
5326 get_tic6x_section_type_name (unsigned int sh_type)
5327 {
5328 switch (sh_type)
5329 {
5330 case SHT_C6000_UNWIND: return "C6000_UNWIND";
5331 case SHT_C6000_PREEMPTMAP: return "C6000_PREEMPTMAP";
5332 case SHT_C6000_ATTRIBUTES: return "C6000_ATTRIBUTES";
5333 case SHT_TI_ICODE: return "TI_ICODE";
5334 case SHT_TI_XREF: return "TI_XREF";
5335 case SHT_TI_HANDLER: return "TI_HANDLER";
5336 case SHT_TI_INITINFO: return "TI_INITINFO";
5337 case SHT_TI_PHATTRS: return "TI_PHATTRS";
5338 default: return NULL;
5339 }
5340 }
5341
5342 static const char *
5343 get_msp430_section_type_name (unsigned int sh_type)
5344 {
5345 switch (sh_type)
5346 {
5347 case SHT_MSP430_SEC_FLAGS: return "MSP430_SEC_FLAGS";
5348 case SHT_MSP430_SYM_ALIASES: return "MSP430_SYM_ALIASES";
5349 case SHT_MSP430_ATTRIBUTES: return "MSP430_ATTRIBUTES";
5350 default: return NULL;
5351 }
5352 }
5353
5354 static const char *
5355 get_nfp_section_type_name (unsigned int sh_type)
5356 {
5357 switch (sh_type)
5358 {
5359 case SHT_NFP_MECONFIG: return "NFP_MECONFIG";
5360 case SHT_NFP_INITREG: return "NFP_INITREG";
5361 case SHT_NFP_UDEBUG: return "NFP_UDEBUG";
5362 default: return NULL;
5363 }
5364 }
5365
5366 static const char *
5367 get_v850_section_type_name (unsigned int sh_type)
5368 {
5369 switch (sh_type)
5370 {
5371 case SHT_V850_SCOMMON: return "V850 Small Common";
5372 case SHT_V850_TCOMMON: return "V850 Tiny Common";
5373 case SHT_V850_ZCOMMON: return "V850 Zero Common";
5374 case SHT_RENESAS_IOP: return "RENESAS IOP";
5375 case SHT_RENESAS_INFO: return "RENESAS INFO";
5376 default: return NULL;
5377 }
5378 }
5379
5380 static const char *
5381 get_riscv_section_type_name (unsigned int sh_type)
5382 {
5383 switch (sh_type)
5384 {
5385 case SHT_RISCV_ATTRIBUTES: return "RISCV_ATTRIBUTES";
5386 default: return NULL;
5387 }
5388 }
5389
5390 static const char *
5391 get_csky_section_type_name (unsigned int sh_type)
5392 {
5393 switch (sh_type)
5394 {
5395 case SHT_CSKY_ATTRIBUTES: return "CSKY_ATTRIBUTES";
5396 default: return NULL;
5397 }
5398 }
5399
5400 static const char *
5401 get_section_type_name (Filedata * filedata, unsigned int sh_type)
5402 {
5403 static char buff[32];
5404 const char * result;
5405
5406 switch (sh_type)
5407 {
5408 case SHT_NULL: return "NULL";
5409 case SHT_PROGBITS: return "PROGBITS";
5410 case SHT_SYMTAB: return "SYMTAB";
5411 case SHT_STRTAB: return "STRTAB";
5412 case SHT_RELA: return "RELA";
5413 case SHT_RELR: return "RELR";
5414 case SHT_HASH: return "HASH";
5415 case SHT_DYNAMIC: return "DYNAMIC";
5416 case SHT_NOTE: return "NOTE";
5417 case SHT_NOBITS: return "NOBITS";
5418 case SHT_REL: return "REL";
5419 case SHT_SHLIB: return "SHLIB";
5420 case SHT_DYNSYM: return "DYNSYM";
5421 case SHT_INIT_ARRAY: return "INIT_ARRAY";
5422 case SHT_FINI_ARRAY: return "FINI_ARRAY";
5423 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
5424 case SHT_GNU_HASH: return "GNU_HASH";
5425 case SHT_GROUP: return "GROUP";
5426 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICES";
5427 case SHT_GNU_verdef: return "VERDEF";
5428 case SHT_GNU_verneed: return "VERNEED";
5429 case SHT_GNU_versym: return "VERSYM";
5430 case 0x6ffffff0: return "VERSYM";
5431 case 0x6ffffffc: return "VERDEF";
5432 case 0x7ffffffd: return "AUXILIARY";
5433 case 0x7fffffff: return "FILTER";
5434 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
5435
5436 default:
5437 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
5438 {
5439 switch (filedata->file_header.e_machine)
5440 {
5441 case EM_ARC:
5442 case EM_ARC_COMPACT:
5443 case EM_ARC_COMPACT2:
5444 case EM_ARC_COMPACT3:
5445 case EM_ARC_COMPACT3_64:
5446 result = get_arc_section_type_name (sh_type);
5447 break;
5448 case EM_MIPS:
5449 case EM_MIPS_RS3_LE:
5450 result = get_mips_section_type_name (sh_type);
5451 break;
5452 case EM_PARISC:
5453 result = get_parisc_section_type_name (sh_type);
5454 break;
5455 case EM_IA_64:
5456 result = get_ia64_section_type_name (filedata, sh_type);
5457 break;
5458 case EM_X86_64:
5459 case EM_L1OM:
5460 case EM_K1OM:
5461 result = get_x86_64_section_type_name (sh_type);
5462 break;
5463 case EM_AARCH64:
5464 result = get_aarch64_section_type_name (sh_type);
5465 break;
5466 case EM_ARM:
5467 result = get_arm_section_type_name (sh_type);
5468 break;
5469 case EM_TI_C6000:
5470 result = get_tic6x_section_type_name (sh_type);
5471 break;
5472 case EM_MSP430:
5473 result = get_msp430_section_type_name (sh_type);
5474 break;
5475 case EM_NFP:
5476 result = get_nfp_section_type_name (sh_type);
5477 break;
5478 case EM_V800:
5479 case EM_V850:
5480 case EM_CYGNUS_V850:
5481 result = get_v850_section_type_name (sh_type);
5482 break;
5483 case EM_RISCV:
5484 result = get_riscv_section_type_name (sh_type);
5485 break;
5486 case EM_CSKY:
5487 result = get_csky_section_type_name (sh_type);
5488 break;
5489 default:
5490 result = NULL;
5491 break;
5492 }
5493
5494 if (result != NULL)
5495 return result;
5496
5497 sprintf (buff, "LOPROC+%#x", sh_type - SHT_LOPROC);
5498 }
5499 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
5500 {
5501 switch (filedata->file_header.e_machine)
5502 {
5503 case EM_IA_64:
5504 result = get_ia64_section_type_name (filedata, sh_type);
5505 break;
5506 default:
5507 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
5508 result = get_solaris_section_type (sh_type);
5509 else
5510 {
5511 switch (sh_type)
5512 {
5513 case SHT_GNU_INCREMENTAL_INPUTS: result = "GNU_INCREMENTAL_INPUTS"; break;
5514 case SHT_GNU_ATTRIBUTES: result = "GNU_ATTRIBUTES"; break;
5515 case SHT_GNU_HASH: result = "GNU_HASH"; break;
5516 case SHT_GNU_LIBLIST: result = "GNU_LIBLIST"; break;
5517 default:
5518 result = NULL;
5519 break;
5520 }
5521 }
5522 break;
5523 }
5524
5525 if (result != NULL)
5526 return result;
5527
5528 sprintf (buff, "LOOS+%#x", sh_type - SHT_LOOS);
5529 }
5530 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
5531 {
5532 switch (filedata->file_header.e_machine)
5533 {
5534 case EM_V800:
5535 case EM_V850:
5536 case EM_CYGNUS_V850:
5537 result = get_v850_section_type_name (sh_type);
5538 break;
5539 default:
5540 result = NULL;
5541 break;
5542 }
5543
5544 if (result != NULL)
5545 return result;
5546
5547 sprintf (buff, "LOUSER+%#x", sh_type - SHT_LOUSER);
5548 }
5549 else
5550 /* This message is probably going to be displayed in a 15
5551 character wide field, so put the hex value first. */
5552 snprintf (buff, sizeof (buff), _("%08x: <unknown>"), sh_type);
5553
5554 return buff;
5555 }
5556 }
5557
5558 enum long_option_values
5559 {
5560 OPTION_DEBUG_DUMP = 512,
5561 OPTION_DYN_SYMS,
5562 OPTION_LTO_SYMS,
5563 OPTION_DWARF_DEPTH,
5564 OPTION_DWARF_START,
5565 OPTION_DWARF_CHECK,
5566 OPTION_CTF_DUMP,
5567 OPTION_CTF_PARENT,
5568 OPTION_CTF_SYMBOLS,
5569 OPTION_CTF_STRINGS,
5570 OPTION_SFRAME_DUMP,
5571 OPTION_WITH_SYMBOL_VERSIONS,
5572 OPTION_RECURSE_LIMIT,
5573 OPTION_NO_RECURSE_LIMIT,
5574 OPTION_NO_DEMANGLING,
5575 OPTION_NO_EXTRA_SYM_INFO,
5576 OPTION_SYM_BASE
5577 };
5578
5579 static struct option options[] =
5580 {
5581 /* Note - This table is alpha-sorted on the 'val'
5582 field in order to make adding new options easier. */
5583 {"arch-specific", no_argument, 0, 'A'},
5584 {"all", no_argument, 0, 'a'},
5585 {"demangle", optional_argument, 0, 'C'},
5586 {"archive-index", no_argument, 0, 'c'},
5587 {"use-dynamic", no_argument, 0, 'D'},
5588 {"dynamic", no_argument, 0, 'd'},
5589 {"headers", no_argument, 0, 'e'},
5590 {"section-groups", no_argument, 0, 'g'},
5591 {"help", no_argument, 0, 'H'},
5592 {"file-header", no_argument, 0, 'h'},
5593 {"histogram", no_argument, 0, 'I'},
5594 {"lint", no_argument, 0, 'L'},
5595 {"enable-checks", no_argument, 0, 'L'},
5596 {"program-headers", no_argument, 0, 'l'},
5597 {"segments", no_argument, 0, 'l'},
5598 {"full-section-name",no_argument, 0, 'N'},
5599 {"notes", no_argument, 0, 'n'},
5600 {"process-links", no_argument, 0, 'P'},
5601 {"string-dump", required_argument, 0, 'p'},
5602 {"relocated-dump", required_argument, 0, 'R'},
5603 {"relocs", no_argument, 0, 'r'},
5604 {"section-headers", no_argument, 0, 'S'},
5605 {"sections", no_argument, 0, 'S'},
5606 {"symbols", no_argument, 0, 's'},
5607 {"syms", no_argument, 0, 's'},
5608 {"silent-truncation",no_argument, 0, 'T'},
5609 {"section-details", no_argument, 0, 't'},
5610 {"unicode", required_argument, NULL, 'U'},
5611 {"unwind", no_argument, 0, 'u'},
5612 {"version-info", no_argument, 0, 'V'},
5613 {"version", no_argument, 0, 'v'},
5614 {"wide", no_argument, 0, 'W'},
5615 {"extra-sym-info", no_argument, 0, 'X'},
5616 {"hex-dump", required_argument, 0, 'x'},
5617 {"decompress", no_argument, 0, 'z'},
5618
5619 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLING},
5620 {"no-extra-sym-info",no_argument, 0, OPTION_NO_EXTRA_SYM_INFO},
5621 {"recurse-limit", no_argument, NULL, OPTION_RECURSE_LIMIT},
5622 {"no-recurse-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5623 {"no-recursion-limit", no_argument, NULL, OPTION_NO_RECURSE_LIMIT},
5624 {"dyn-syms", no_argument, 0, OPTION_DYN_SYMS},
5625 {"lto-syms", no_argument, 0, OPTION_LTO_SYMS},
5626 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
5627 {"dwarf-depth", required_argument, 0, OPTION_DWARF_DEPTH},
5628 {"dwarf-start", required_argument, 0, OPTION_DWARF_START},
5629 {"dwarf-check", no_argument, 0, OPTION_DWARF_CHECK},
5630 #ifdef ENABLE_LIBCTF
5631 {"ctf", required_argument, 0, OPTION_CTF_DUMP},
5632 {"ctf-symbols", required_argument, 0, OPTION_CTF_SYMBOLS},
5633 {"ctf-strings", required_argument, 0, OPTION_CTF_STRINGS},
5634 {"ctf-parent", required_argument, 0, OPTION_CTF_PARENT},
5635 #endif
5636 {"sframe", optional_argument, 0, OPTION_SFRAME_DUMP},
5637 {"sym-base", optional_argument, 0, OPTION_SYM_BASE},
5638
5639 {0, no_argument, 0, 0}
5640 };
5641
5642 static void
5643 usage (FILE * stream)
5644 {
5645 fprintf (stream, _("Usage: readelf <option(s)> elf-file(s)\n"));
5646 fprintf (stream, _(" Display information about the contents of ELF format files\n"));
5647 fprintf (stream, _(" Options are:\n"));
5648 fprintf (stream, _("\
5649 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n"));
5650 fprintf (stream, _("\
5651 -h --file-header Display the ELF file header\n"));
5652 fprintf (stream, _("\
5653 -l --program-headers Display the program headers\n"));
5654 fprintf (stream, _("\
5655 --segments An alias for --program-headers\n"));
5656 fprintf (stream, _("\
5657 -S --section-headers Display the sections' header\n"));
5658 fprintf (stream, _("\
5659 --sections An alias for --section-headers\n"));
5660 fprintf (stream, _("\
5661 -g --section-groups Display the section groups\n"));
5662 fprintf (stream, _("\
5663 -t --section-details Display the section details\n"));
5664 fprintf (stream, _("\
5665 -e --headers Equivalent to: -h -l -S\n"));
5666 fprintf (stream, _("\
5667 -s --syms Display the symbol table\n"));
5668 fprintf (stream, _("\
5669 --symbols An alias for --syms\n"));
5670 fprintf (stream, _("\
5671 --dyn-syms Display the dynamic symbol table\n"));
5672 fprintf (stream, _("\
5673 --lto-syms Display LTO symbol tables\n"));
5674 fprintf (stream, _("\
5675 --sym-base=[0|8|10|16] \n\
5676 Force base for symbol sizes. The options are \n\
5677 mixed (the default), octal, decimal, hexadecimal.\n"));
5678 fprintf (stream, _("\
5679 -C --demangle[=STYLE] Decode mangled/processed symbol names\n"));
5680 display_demangler_styles (stream, _("\
5681 STYLE can be "));
5682 fprintf (stream, _("\
5683 --no-demangle Do not demangle low-level symbol names. (default)\n"));
5684 fprintf (stream, _("\
5685 --recurse-limit Enable a demangling recursion limit. (default)\n"));
5686 fprintf (stream, _("\
5687 --no-recurse-limit Disable a demangling recursion limit\n"));
5688 fprintf (stream, _("\
5689 -U[dlexhi] --unicode=[default|locale|escape|hex|highlight|invalid]\n\
5690 Display unicode characters as determined by the current locale\n\
5691 (default), escape sequences, \"<hex sequences>\", highlighted\n\
5692 escape sequences, or treat them as invalid and display as\n\
5693 \"{hex sequences}\"\n"));
5694 fprintf (stream, _("\
5695 -X --extra-sym-info Display extra information when showing symbols\n"));
5696 fprintf (stream, _("\
5697 --no-extra-sym-info Do not display extra information when showing symbols (default)\n"));
5698 fprintf (stream, _("\
5699 -n --notes Display the contents of note sections (if present)\n"));
5700 fprintf (stream, _("\
5701 -r --relocs Display the relocations (if present)\n"));
5702 fprintf (stream, _("\
5703 -u --unwind Display the unwind info (if present)\n"));
5704 fprintf (stream, _("\
5705 -d --dynamic Display the dynamic section (if present)\n"));
5706 fprintf (stream, _("\
5707 -V --version-info Display the version sections (if present)\n"));
5708 fprintf (stream, _("\
5709 -A --arch-specific Display architecture specific information (if any)\n"));
5710 fprintf (stream, _("\
5711 -c --archive-index Display the symbol/file index in an archive\n"));
5712 fprintf (stream, _("\
5713 -D --use-dynamic Use the dynamic section info when displaying symbols\n"));
5714 fprintf (stream, _("\
5715 -L --lint|--enable-checks\n\
5716 Display warning messages for possible problems\n"));
5717 fprintf (stream, _("\
5718 -x --hex-dump=<number|name>\n\
5719 Dump the contents of section <number|name> as bytes\n"));
5720 fprintf (stream, _("\
5721 -p --string-dump=<number|name>\n\
5722 Dump the contents of section <number|name> as strings\n"));
5723 fprintf (stream, _("\
5724 -R --relocated-dump=<number|name>\n\
5725 Dump the relocated contents of section <number|name>\n"));
5726 fprintf (stream, _("\
5727 -z --decompress Decompress section before dumping it\n"));
5728 fprintf (stream, _("\
5729 -w --debug-dump[a/=abbrev, A/=addr, r/=aranges, c/=cu_index, L/=decodedline,\n\
5730 f/=frames, F/=frames-interp, g/=gdb_index, i/=info, o/=loc,\n\
5731 m/=macro, p/=pubnames, t/=pubtypes, R/=Ranges, l/=rawline,\n\
5732 s/=str, O/=str-offsets, u/=trace_abbrev, T/=trace_aranges,\n\
5733 U/=trace_info]\n\
5734 Display the contents of DWARF debug sections\n"));
5735 fprintf (stream, _("\
5736 -wk --debug-dump=links Display the contents of sections that link to separate\n\
5737 debuginfo files\n"));
5738 fprintf (stream, _("\
5739 -P --process-links Display the contents of non-debug sections in separate\n\
5740 debuginfo files. (Implies -wK)\n"));
5741 #if DEFAULT_FOR_FOLLOW_LINKS
5742 fprintf (stream, _("\
5743 -wK --debug-dump=follow-links\n\
5744 Follow links to separate debug info files (default)\n"));
5745 fprintf (stream, _("\
5746 -wN --debug-dump=no-follow-links\n\
5747 Do not follow links to separate debug info files\n"));
5748 #else
5749 fprintf (stream, _("\
5750 -wK --debug-dump=follow-links\n\
5751 Follow links to separate debug info files\n"));
5752 fprintf (stream, _("\
5753 -wN --debug-dump=no-follow-links\n\
5754 Do not follow links to separate debug info files\n\
5755 (default)\n"));
5756 #endif
5757 #if HAVE_LIBDEBUGINFOD
5758 fprintf (stream, _("\
5759 -wD --debug-dump=use-debuginfod\n\
5760 When following links, also query debuginfod servers (default)\n"));
5761 fprintf (stream, _("\
5762 -wE --debug-dump=do-not-use-debuginfod\n\
5763 When following links, do not query debuginfod servers\n"));
5764 #endif
5765 fprintf (stream, _("\
5766 --dwarf-depth=N Do not display DIEs at depth N or greater\n"));
5767 fprintf (stream, _("\
5768 --dwarf-start=N Display DIEs starting at offset N\n"));
5769 #ifdef ENABLE_LIBCTF
5770 fprintf (stream, _("\
5771 --ctf=<number|name> Display CTF info from section <number|name>\n"));
5772 fprintf (stream, _("\
5773 --ctf-parent=<name> Use CTF archive member <name> as the CTF parent\n"));
5774 fprintf (stream, _("\
5775 --ctf-symbols=<number|name>\n\
5776 Use section <number|name> as the CTF external symtab\n"));
5777 fprintf (stream, _("\
5778 --ctf-strings=<number|name>\n\
5779 Use section <number|name> as the CTF external strtab\n"));
5780 #endif
5781 fprintf (stream, _("\
5782 --sframe[=NAME] Display SFrame info from section NAME, (default '.sframe')\n"));
5783
5784 #ifdef SUPPORT_DISASSEMBLY
5785 fprintf (stream, _("\
5786 -i --instruction-dump=<number|name>\n\
5787 Disassemble the contents of section <number|name>\n"));
5788 #endif
5789 fprintf (stream, _("\
5790 -I --histogram Display histogram of bucket list lengths\n"));
5791 fprintf (stream, _("\
5792 -W --wide Allow output width to exceed 80 characters\n"));
5793 fprintf (stream, _("\
5794 -T --silent-truncation If a symbol name is truncated, do not add [...] suffix\n"));
5795 fprintf (stream, _("\
5796 @<file> Read options from <file>\n"));
5797 fprintf (stream, _("\
5798 -H --help Display this information\n"));
5799 fprintf (stream, _("\
5800 -v --version Display the version number of readelf\n"));
5801
5802 if (REPORT_BUGS_TO[0] && stream == stdout)
5803 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
5804
5805 exit (stream == stdout ? 0 : 1);
5806 }
5807
5808 /* Record the fact that the user wants the contents of section number
5809 SECTION to be displayed using the method(s) encoded as flags bits
5810 in TYPE. Note, TYPE can be zero if we are creating the array for
5811 the first time. */
5812
5813 static void
5814 request_dump_bynumber (struct dump_data *dumpdata,
5815 unsigned int section, dump_type type)
5816 {
5817 if (section >= dumpdata->num_dump_sects)
5818 {
5819 dump_type * new_dump_sects;
5820
5821 new_dump_sects = (dump_type *) calloc (section + 1,
5822 sizeof (* new_dump_sects));
5823
5824 if (new_dump_sects == NULL)
5825 error (_("Out of memory allocating dump request table.\n"));
5826 else
5827 {
5828 if (dumpdata->dump_sects)
5829 {
5830 /* Copy current flag settings. */
5831 memcpy (new_dump_sects, dumpdata->dump_sects,
5832 dumpdata->num_dump_sects * sizeof (* new_dump_sects));
5833
5834 free (dumpdata->dump_sects);
5835 }
5836
5837 dumpdata->dump_sects = new_dump_sects;
5838 dumpdata->num_dump_sects = section + 1;
5839 }
5840 }
5841
5842 if (dumpdata->dump_sects)
5843 dumpdata->dump_sects[section] |= type;
5844 }
5845
5846 /* Request a dump by section name. */
5847
5848 static void
5849 request_dump_byname (const char * section, dump_type type)
5850 {
5851 struct dump_list_entry * new_request;
5852
5853 new_request = (struct dump_list_entry *)
5854 malloc (sizeof (struct dump_list_entry));
5855 if (!new_request)
5856 error (_("Out of memory allocating dump request table.\n"));
5857
5858 new_request->name = strdup (section);
5859 if (!new_request->name)
5860 error (_("Out of memory allocating dump request table.\n"));
5861
5862 new_request->type = type;
5863
5864 new_request->next = dump_sects_byname;
5865 dump_sects_byname = new_request;
5866 }
5867
5868 static inline void
5869 request_dump (struct dump_data *dumpdata, dump_type type)
5870 {
5871 int section;
5872 char * cp;
5873
5874 do_dump = true;
5875 section = strtoul (optarg, & cp, 0);
5876
5877 if (! *cp && section >= 0)
5878 request_dump_bynumber (dumpdata, section, type);
5879 else
5880 request_dump_byname (optarg, type);
5881 }
5882
5883 static void
5884 parse_args (struct dump_data *dumpdata, int argc, char ** argv)
5885 {
5886 int c;
5887
5888 if (argc < 2)
5889 usage (stderr);
5890
5891 while ((c = getopt_long
5892 (argc, argv, "ACDHILNPR:STU:VWXacdeghi:lnp:rstuvw::x:z", options, NULL)) != EOF)
5893 {
5894 switch (c)
5895 {
5896 case 0:
5897 /* Long options. */
5898 break;
5899 case 'H':
5900 usage (stdout);
5901 break;
5902
5903 case 'a':
5904 do_syms = true;
5905 do_reloc = true;
5906 do_unwind = true;
5907 do_dynamic = true;
5908 do_header = true;
5909 do_sections = true;
5910 do_section_groups = true;
5911 do_segments = true;
5912 do_version = true;
5913 do_histogram = true;
5914 do_arch = true;
5915 do_notes = true;
5916 break;
5917
5918 case 'g':
5919 do_section_groups = true;
5920 break;
5921 case 't':
5922 case 'N':
5923 do_sections = true;
5924 do_section_details = true;
5925 break;
5926 case 'e':
5927 do_header = true;
5928 do_sections = true;
5929 do_segments = true;
5930 break;
5931 case 'A':
5932 do_arch = true;
5933 break;
5934 case 'D':
5935 do_using_dynamic = true;
5936 break;
5937 case 'r':
5938 do_reloc = true;
5939 break;
5940 case 'u':
5941 do_unwind = true;
5942 break;
5943 case 'h':
5944 do_header = true;
5945 break;
5946 case 'l':
5947 do_segments = true;
5948 break;
5949 case 's':
5950 do_syms = true;
5951 break;
5952 case 'S':
5953 do_sections = true;
5954 break;
5955 case 'd':
5956 do_dynamic = true;
5957 break;
5958 case 'I':
5959 do_histogram = true;
5960 break;
5961 case 'n':
5962 do_notes = true;
5963 break;
5964 case 'c':
5965 do_archive_index = true;
5966 break;
5967 case 'L':
5968 do_checks = true;
5969 break;
5970 case 'P':
5971 process_links = true;
5972 do_follow_links = true;
5973 dump_any_debugging = true;
5974 break;
5975 case 'x':
5976 request_dump (dumpdata, HEX_DUMP);
5977 break;
5978 case 'p':
5979 request_dump (dumpdata, STRING_DUMP);
5980 break;
5981 case 'R':
5982 request_dump (dumpdata, RELOC_DUMP);
5983 break;
5984 case 'z':
5985 decompress_dumps = true;
5986 break;
5987 case 'w':
5988 if (optarg == NULL)
5989 {
5990 do_debugging = true;
5991 do_dump = true;
5992 dump_any_debugging = true;
5993 dwarf_select_sections_all ();
5994 }
5995 else
5996 {
5997 do_debugging = false;
5998 if (dwarf_select_sections_by_letters (optarg))
5999 {
6000 do_dump = true;
6001 dump_any_debugging = true;
6002 }
6003 }
6004 break;
6005 case OPTION_DEBUG_DUMP:
6006 if (optarg == NULL)
6007 {
6008 do_dump = true;
6009 do_debugging = true;
6010 dump_any_debugging = true;
6011 dwarf_select_sections_all ();
6012 }
6013 else
6014 {
6015 do_debugging = false;
6016 if (dwarf_select_sections_by_names (optarg))
6017 {
6018 do_dump = true;
6019 dump_any_debugging = true;
6020 }
6021 }
6022 break;
6023 case OPTION_DWARF_DEPTH:
6024 {
6025 char *cp;
6026
6027 dwarf_cutoff_level = strtoul (optarg, & cp, 0);
6028 }
6029 break;
6030 case OPTION_DWARF_START:
6031 {
6032 char *cp;
6033
6034 dwarf_start_die = strtoul (optarg, & cp, 0);
6035 }
6036 break;
6037 case OPTION_DWARF_CHECK:
6038 dwarf_check = true;
6039 break;
6040 case OPTION_CTF_DUMP:
6041 do_ctf = true;
6042 request_dump (dumpdata, CTF_DUMP);
6043 break;
6044 case OPTION_CTF_SYMBOLS:
6045 free (dump_ctf_symtab_name);
6046 dump_ctf_symtab_name = strdup (optarg);
6047 break;
6048 case OPTION_CTF_STRINGS:
6049 free (dump_ctf_strtab_name);
6050 dump_ctf_strtab_name = strdup (optarg);
6051 break;
6052 case OPTION_CTF_PARENT:
6053 free (dump_ctf_parent_name);
6054 dump_ctf_parent_name = strdup (optarg);
6055 break;
6056 case OPTION_SFRAME_DUMP:
6057 do_sframe = true;
6058 /* Providing section name is optional. request_dump (), however,
6059 thrives on non NULL optarg. Handle it explicitly here. */
6060 if (optarg != NULL)
6061 request_dump (dumpdata, SFRAME_DUMP);
6062 else
6063 {
6064 do_dump = true;
6065 const char *sframe_sec_name = strdup (".sframe");
6066 request_dump_byname (sframe_sec_name, SFRAME_DUMP);
6067 }
6068 break;
6069 case OPTION_DYN_SYMS:
6070 do_dyn_syms = true;
6071 break;
6072 case OPTION_LTO_SYMS:
6073 do_lto_syms = true;
6074 break;
6075 case 'X':
6076 extra_sym_info = true;
6077 break;
6078 case OPTION_NO_EXTRA_SYM_INFO:
6079 extra_sym_info = false;
6080 break;
6081
6082 #ifdef SUPPORT_DISASSEMBLY
6083 case 'i':
6084 request_dump (dumpdata, DISASS_DUMP);
6085 break;
6086 #endif
6087 case 'v':
6088 print_version (program_name);
6089 break;
6090 case 'V':
6091 do_version = true;
6092 break;
6093 case 'W':
6094 do_wide = true;
6095 break;
6096 case 'T':
6097 do_not_show_symbol_truncation = true;
6098 break;
6099 case 'C':
6100 do_demangle = true;
6101 if (optarg != NULL)
6102 {
6103 enum demangling_styles style;
6104
6105 style = cplus_demangle_name_to_style (optarg);
6106 if (style == unknown_demangling)
6107 error (_("unknown demangling style `%s'"), optarg);
6108
6109 cplus_demangle_set_style (style);
6110 }
6111 break;
6112 case OPTION_NO_DEMANGLING:
6113 do_demangle = false;
6114 break;
6115 case OPTION_RECURSE_LIMIT:
6116 demangle_flags &= ~ DMGL_NO_RECURSE_LIMIT;
6117 break;
6118 case OPTION_NO_RECURSE_LIMIT:
6119 demangle_flags |= DMGL_NO_RECURSE_LIMIT;
6120 break;
6121 case OPTION_WITH_SYMBOL_VERSIONS:
6122 /* Ignored for backward compatibility. */
6123 break;
6124
6125 case 'U':
6126 if (optarg == NULL)
6127 error (_("Missing arg to -U/--unicode")); /* Can this happen ? */
6128 else if (streq (optarg, "default") || streq (optarg, "d"))
6129 unicode_display = unicode_default;
6130 else if (streq (optarg, "locale") || streq (optarg, "l"))
6131 unicode_display = unicode_locale;
6132 else if (streq (optarg, "escape") || streq (optarg, "e"))
6133 unicode_display = unicode_escape;
6134 else if (streq (optarg, "invalid") || streq (optarg, "i"))
6135 unicode_display = unicode_invalid;
6136 else if (streq (optarg, "hex") || streq (optarg, "x"))
6137 unicode_display = unicode_hex;
6138 else if (streq (optarg, "highlight") || streq (optarg, "h"))
6139 unicode_display = unicode_highlight;
6140 else
6141 error (_("invalid argument to -U/--unicode: %s"), optarg);
6142 break;
6143
6144 case OPTION_SYM_BASE:
6145 sym_base = 0;
6146 if (optarg != NULL)
6147 {
6148 sym_base = strtoul (optarg, NULL, 0);
6149 switch (sym_base)
6150 {
6151 case 0:
6152 case 8:
6153 case 10:
6154 case 16:
6155 break;
6156
6157 default:
6158 sym_base = 0;
6159 break;
6160 }
6161 }
6162 break;
6163
6164 default:
6165 /* xgettext:c-format */
6166 error (_("Invalid option '-%c'\n"), c);
6167 /* Fall through. */
6168 case '?':
6169 usage (stderr);
6170 }
6171 }
6172
6173 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
6174 && !do_segments && !do_header && !do_dump && !do_version
6175 && !do_histogram && !do_debugging && !do_arch && !do_notes
6176 && !do_section_groups && !do_archive_index
6177 && !do_dyn_syms && !do_lto_syms)
6178 {
6179 if (do_checks)
6180 {
6181 check_all = true;
6182 do_dynamic = do_syms = do_reloc = do_unwind = do_sections = true;
6183 do_segments = do_header = do_dump = do_version = true;
6184 do_histogram = do_debugging = do_arch = do_notes = true;
6185 do_section_groups = do_archive_index = do_dyn_syms = true;
6186 do_lto_syms = true;
6187 }
6188 else
6189 usage (stderr);
6190 }
6191 }
6192
6193 static const char *
6194 get_elf_class (unsigned int elf_class)
6195 {
6196 static char buff[32];
6197
6198 switch (elf_class)
6199 {
6200 case ELFCLASSNONE: return _("none");
6201 case ELFCLASS32: return "ELF32";
6202 case ELFCLASS64: return "ELF64";
6203 default:
6204 snprintf (buff, sizeof (buff), _("<unknown: %x>"), elf_class);
6205 return buff;
6206 }
6207 }
6208
6209 static const char *
6210 get_data_encoding (unsigned int encoding)
6211 {
6212 static char buff[32];
6213
6214 switch (encoding)
6215 {
6216 case ELFDATANONE: return _("none");
6217 case ELFDATA2LSB: return _("2's complement, little endian");
6218 case ELFDATA2MSB: return _("2's complement, big endian");
6219 default:
6220 snprintf (buff, sizeof (buff), _("<unknown: %x>"), encoding);
6221 return buff;
6222 }
6223 }
6224
6225 static bool
6226 check_magic_number (Filedata * filedata, Elf_Internal_Ehdr * header)
6227 {
6228 if (header->e_ident[EI_MAG0] == ELFMAG0
6229 && header->e_ident[EI_MAG1] == ELFMAG1
6230 && header->e_ident[EI_MAG2] == ELFMAG2
6231 && header->e_ident[EI_MAG3] == ELFMAG3)
6232 return true;
6233
6234 /* Some compilers produce object files that are not in the ELF file format.
6235 As an aid to users of readelf, try to identify these cases and suggest
6236 alternative tools.
6237
6238 FIXME: It is not clear if all four bytes are used as constant magic
6239 valus by all compilers. It may be necessary to recode this function if
6240 different tools use different length sequences. */
6241
6242 static struct
6243 {
6244 unsigned char magic[4];
6245 const char * obj_message;
6246 const char * ar_message;
6247 }
6248 known_magic[] =
6249 {
6250 { { 'B', 'C', 0xc0, 0xde },
6251 N_("This is a LLVM bitcode file - try using llvm-bcanalyzer\n"),
6252 N_("This is a LLVM bitcode file - try extracting and then using llvm-bcanalyzer\n")
6253 },
6254 { { 'g', 'o', ' ', 'o' },
6255 N_("This is a GO binary file - try using 'go tool objdump' or 'go tool nm'\n"),
6256 NULL
6257 }
6258 };
6259 int i;
6260
6261 for (i = ARRAY_SIZE (known_magic); i--;)
6262 {
6263 if (header->e_ident[EI_MAG0] == known_magic[i].magic[0]
6264 && header->e_ident[EI_MAG1] == known_magic[i].magic[1]
6265 && header->e_ident[EI_MAG2] == known_magic[i].magic[2]
6266 && header->e_ident[EI_MAG3] == known_magic[i].magic[3])
6267 {
6268 /* Some compiler's analyzer tools do not handle archives,
6269 so we provide two different kinds of error message. */
6270 if (filedata->archive_file_size > 0
6271 && known_magic[i].ar_message != NULL)
6272 error ("%s", known_magic[i].ar_message);
6273 else
6274 error ("%s", known_magic[i].obj_message);
6275 return false;
6276 }
6277 }
6278
6279 error (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
6280 return false;
6281 }
6282
6283 /* Decode the data held in 'filedata->file_header'. */
6284
6285 static bool
6286 process_file_header (Filedata * filedata)
6287 {
6288 Elf_Internal_Ehdr * header = & filedata->file_header;
6289
6290 if (! check_magic_number (filedata, header))
6291 return false;
6292
6293 if (! filedata->is_separate)
6294 init_dwarf_regnames_by_elf_machine_code (header->e_machine);
6295
6296 if (do_header)
6297 {
6298 unsigned i;
6299
6300 if (filedata->is_separate)
6301 printf (_("ELF Header in linked file '%s':\n"), filedata->file_name);
6302 else
6303 printf (_("ELF Header:\n"));
6304 printf (_(" Magic: "));
6305 for (i = 0; i < EI_NIDENT; i++)
6306 printf ("%2.2x ", header->e_ident[i]);
6307 printf ("\n");
6308 printf (_(" Class: %s\n"),
6309 get_elf_class (header->e_ident[EI_CLASS]));
6310 printf (_(" Data: %s\n"),
6311 get_data_encoding (header->e_ident[EI_DATA]));
6312 printf (_(" Version: %d%s\n"),
6313 header->e_ident[EI_VERSION],
6314 (header->e_ident[EI_VERSION] == EV_CURRENT
6315 ? _(" (current)")
6316 : (header->e_ident[EI_VERSION] != EV_NONE
6317 ? _(" <unknown>")
6318 : "")));
6319 printf (_(" OS/ABI: %s\n"),
6320 get_osabi_name (filedata, header->e_ident[EI_OSABI]));
6321 printf (_(" ABI Version: %d\n"),
6322 header->e_ident[EI_ABIVERSION]);
6323 printf (_(" Type: %s\n"),
6324 get_file_type (filedata));
6325 printf (_(" Machine: %s\n"),
6326 get_machine_name (header->e_machine));
6327 printf (_(" Version: 0x%lx\n"),
6328 header->e_version);
6329
6330 printf (_(" Entry point address: "));
6331 print_vma (header->e_entry, PREFIX_HEX);
6332 printf (_("\n Start of program headers: "));
6333 print_vma (header->e_phoff, DEC);
6334 printf (_(" (bytes into file)\n Start of section headers: "));
6335 print_vma (header->e_shoff, DEC);
6336 printf (_(" (bytes into file)\n"));
6337
6338 printf (_(" Flags: 0x%lx%s\n"),
6339 header->e_flags,
6340 get_machine_flags (filedata, header->e_flags, header->e_machine));
6341 printf (_(" Size of this header: %u (bytes)\n"),
6342 header->e_ehsize);
6343 printf (_(" Size of program headers: %u (bytes)\n"),
6344 header->e_phentsize);
6345 printf (_(" Number of program headers: %u"),
6346 header->e_phnum);
6347 if (filedata->section_headers != NULL
6348 && header->e_phnum == PN_XNUM
6349 && filedata->section_headers[0].sh_info != 0)
6350 printf (" (%u)", filedata->section_headers[0].sh_info);
6351 putc ('\n', stdout);
6352 printf (_(" Size of section headers: %u (bytes)\n"),
6353 header->e_shentsize);
6354 printf (_(" Number of section headers: %u"),
6355 header->e_shnum);
6356 if (filedata->section_headers != NULL && header->e_shnum == SHN_UNDEF)
6357 {
6358 header->e_shnum = filedata->section_headers[0].sh_size;
6359 printf (" (%u)", header->e_shnum);
6360 }
6361 putc ('\n', stdout);
6362 printf (_(" Section header string table index: %u"),
6363 header->e_shstrndx);
6364 if (filedata->section_headers != NULL
6365 && header->e_shstrndx == (SHN_XINDEX & 0xffff))
6366 {
6367 header->e_shstrndx = filedata->section_headers[0].sh_link;
6368 printf (" (%u)", header->e_shstrndx);
6369 }
6370 if (header->e_shstrndx != SHN_UNDEF
6371 && header->e_shstrndx >= header->e_shnum)
6372 {
6373 header->e_shstrndx = SHN_UNDEF;
6374 printf (_(" <corrupt: out of range>"));
6375 }
6376 putc ('\n', stdout);
6377 }
6378
6379 if (filedata->section_headers != NULL)
6380 {
6381 if (header->e_phnum == PN_XNUM
6382 && filedata->section_headers[0].sh_info != 0)
6383 {
6384 /* Throw away any cached read of PN_XNUM headers. */
6385 free (filedata->program_headers);
6386 filedata->program_headers = NULL;
6387 header->e_phnum = filedata->section_headers[0].sh_info;
6388 }
6389 if (header->e_shnum == SHN_UNDEF)
6390 header->e_shnum = filedata->section_headers[0].sh_size;
6391 if (header->e_shstrndx == (SHN_XINDEX & 0xffff))
6392 header->e_shstrndx = filedata->section_headers[0].sh_link;
6393 if (header->e_shstrndx >= header->e_shnum)
6394 header->e_shstrndx = SHN_UNDEF;
6395 }
6396
6397 return true;
6398 }
6399
6400 /* Read in the program headers from FILEDATA and store them in PHEADERS.
6401 Returns TRUE upon success, FALSE otherwise. Loads 32-bit headers. */
6402
6403 static bool
6404 get_32bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
6405 {
6406 Elf32_External_Phdr * phdrs;
6407 Elf32_External_Phdr * external;
6408 Elf_Internal_Phdr * internal;
6409 unsigned int i;
6410 unsigned int size = filedata->file_header.e_phentsize;
6411 unsigned int num = filedata->file_header.e_phnum;
6412
6413 /* PR binutils/17531: Cope with unexpected section header sizes. */
6414 if (size == 0 || num == 0)
6415 return false;
6416 if (size < sizeof * phdrs)
6417 {
6418 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
6419 return false;
6420 }
6421 if (size > sizeof * phdrs)
6422 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
6423
6424 phdrs = (Elf32_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
6425 size, num, _("program headers"));
6426 if (phdrs == NULL)
6427 return false;
6428
6429 for (i = 0, internal = pheaders, external = phdrs;
6430 i < filedata->file_header.e_phnum;
6431 i++, internal++, external++)
6432 {
6433 internal->p_type = BYTE_GET (external->p_type);
6434 internal->p_offset = BYTE_GET (external->p_offset);
6435 internal->p_vaddr = BYTE_GET (external->p_vaddr);
6436 internal->p_paddr = BYTE_GET (external->p_paddr);
6437 internal->p_filesz = BYTE_GET (external->p_filesz);
6438 internal->p_memsz = BYTE_GET (external->p_memsz);
6439 internal->p_flags = BYTE_GET (external->p_flags);
6440 internal->p_align = BYTE_GET (external->p_align);
6441 }
6442
6443 free (phdrs);
6444 return true;
6445 }
6446
6447 /* Read in the program headers from FILEDATA and store them in PHEADERS.
6448 Returns TRUE upon success, FALSE otherwise. Loads 64-bit headers. */
6449
6450 static bool
6451 get_64bit_program_headers (Filedata * filedata, Elf_Internal_Phdr * pheaders)
6452 {
6453 Elf64_External_Phdr * phdrs;
6454 Elf64_External_Phdr * external;
6455 Elf_Internal_Phdr * internal;
6456 unsigned int i;
6457 unsigned int size = filedata->file_header.e_phentsize;
6458 unsigned int num = filedata->file_header.e_phnum;
6459
6460 /* PR binutils/17531: Cope with unexpected section header sizes. */
6461 if (size == 0 || num == 0)
6462 return false;
6463 if (size < sizeof * phdrs)
6464 {
6465 error (_("The e_phentsize field in the ELF header is less than the size of an ELF program header\n"));
6466 return false;
6467 }
6468 if (size > sizeof * phdrs)
6469 warn (_("The e_phentsize field in the ELF header is larger than the size of an ELF program header\n"));
6470
6471 phdrs = (Elf64_External_Phdr *) get_data (NULL, filedata, filedata->file_header.e_phoff,
6472 size, num, _("program headers"));
6473 if (!phdrs)
6474 return false;
6475
6476 for (i = 0, internal = pheaders, external = phdrs;
6477 i < filedata->file_header.e_phnum;
6478 i++, internal++, external++)
6479 {
6480 internal->p_type = BYTE_GET (external->p_type);
6481 internal->p_flags = BYTE_GET (external->p_flags);
6482 internal->p_offset = BYTE_GET (external->p_offset);
6483 internal->p_vaddr = BYTE_GET (external->p_vaddr);
6484 internal->p_paddr = BYTE_GET (external->p_paddr);
6485 internal->p_filesz = BYTE_GET (external->p_filesz);
6486 internal->p_memsz = BYTE_GET (external->p_memsz);
6487 internal->p_align = BYTE_GET (external->p_align);
6488 }
6489
6490 free (phdrs);
6491 return true;
6492 }
6493
6494 /* Returns TRUE if the program headers were read into `program_headers'. */
6495
6496 static bool
6497 get_program_headers (Filedata * filedata)
6498 {
6499 Elf_Internal_Phdr * phdrs;
6500
6501 /* Check cache of prior read. */
6502 if (filedata->program_headers != NULL)
6503 return true;
6504
6505 /* Be kind to memory checkers by looking for
6506 e_phnum values which we know must be invalid. */
6507 if (filedata->file_header.e_phnum
6508 * (is_32bit_elf ? sizeof (Elf32_External_Phdr) : sizeof (Elf64_External_Phdr))
6509 >= filedata->file_size)
6510 {
6511 error (_("Too many program headers - %#x - the file is not that big\n"),
6512 filedata->file_header.e_phnum);
6513 return false;
6514 }
6515
6516 phdrs = (Elf_Internal_Phdr *) cmalloc (filedata->file_header.e_phnum,
6517 sizeof (Elf_Internal_Phdr));
6518 if (phdrs == NULL)
6519 {
6520 error (_("Out of memory reading %u program headers\n"),
6521 filedata->file_header.e_phnum);
6522 return false;
6523 }
6524
6525 if (is_32bit_elf
6526 ? get_32bit_program_headers (filedata, phdrs)
6527 : get_64bit_program_headers (filedata, phdrs))
6528 {
6529 filedata->program_headers = phdrs;
6530 return true;
6531 }
6532
6533 free (phdrs);
6534 return false;
6535 }
6536
6537 /* Print program header info and locate dynamic section. */
6538
6539 static void
6540 process_program_headers (Filedata * filedata)
6541 {
6542 Elf_Internal_Phdr * segment;
6543 unsigned int i;
6544 Elf_Internal_Phdr * previous_load = NULL;
6545
6546 if (filedata->file_header.e_phnum == 0)
6547 {
6548 /* PR binutils/12467. */
6549 if (filedata->file_header.e_phoff != 0)
6550 warn (_("possibly corrupt ELF header - it has a non-zero program"
6551 " header offset, but no program headers\n"));
6552 else if (do_segments)
6553 {
6554 if (filedata->is_separate)
6555 printf (_("\nThere are no program headers in linked file '%s'.\n"),
6556 filedata->file_name);
6557 else
6558 printf (_("\nThere are no program headers in this file.\n"));
6559 }
6560 goto no_headers;
6561 }
6562
6563 if (do_segments && !do_header)
6564 {
6565 if (filedata->is_separate)
6566 printf ("\nIn linked file '%s' the ELF file type is %s\n",
6567 filedata->file_name, get_file_type (filedata));
6568 else
6569 printf (_("\nElf file type is %s\n"), get_file_type (filedata));
6570 printf (_("Entry point 0x%" PRIx64 "\n"),
6571 filedata->file_header.e_entry);
6572 printf (ngettext ("There is %d program header,"
6573 " starting at offset %" PRIu64 "\n",
6574 "There are %d program headers,"
6575 " starting at offset %" PRIu64 "\n",
6576 filedata->file_header.e_phnum),
6577 filedata->file_header.e_phnum,
6578 filedata->file_header.e_phoff);
6579 }
6580
6581 if (! get_program_headers (filedata))
6582 goto no_headers;
6583
6584 if (do_segments)
6585 {
6586 if (filedata->file_header.e_phnum > 1)
6587 printf (_("\nProgram Headers:\n"));
6588 else
6589 printf (_("\nProgram Headers:\n"));
6590
6591 if (is_32bit_elf)
6592 printf
6593 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6594 else if (do_wide)
6595 printf
6596 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
6597 else
6598 {
6599 printf
6600 (_(" Type Offset VirtAddr PhysAddr\n"));
6601 printf
6602 (_(" FileSiz MemSiz Flags Align\n"));
6603 }
6604 }
6605
6606 uint64_t dynamic_addr = 0;
6607 uint64_t dynamic_size = 0;
6608 for (i = 0, segment = filedata->program_headers;
6609 i < filedata->file_header.e_phnum;
6610 i++, segment++)
6611 {
6612 if (do_segments)
6613 {
6614 printf (" %-14.14s ", get_segment_type (filedata, segment->p_type));
6615
6616 if (is_32bit_elf)
6617 {
6618 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6619 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
6620 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
6621 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
6622 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
6623 printf ("%c%c%c ",
6624 (segment->p_flags & PF_R ? 'R' : ' '),
6625 (segment->p_flags & PF_W ? 'W' : ' '),
6626 (segment->p_flags & PF_X ? 'E' : ' '));
6627 printf ("%#lx", (unsigned long) segment->p_align);
6628 }
6629 else if (do_wide)
6630 {
6631 if ((unsigned long) segment->p_offset == segment->p_offset)
6632 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
6633 else
6634 {
6635 print_vma (segment->p_offset, FULL_HEX);
6636 putchar (' ');
6637 }
6638
6639 print_vma (segment->p_vaddr, FULL_HEX);
6640 putchar (' ');
6641 print_vma (segment->p_paddr, FULL_HEX);
6642 putchar (' ');
6643
6644 if ((unsigned long) segment->p_filesz == segment->p_filesz)
6645 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
6646 else
6647 {
6648 print_vma (segment->p_filesz, FULL_HEX);
6649 putchar (' ');
6650 }
6651
6652 if ((unsigned long) segment->p_memsz == segment->p_memsz)
6653 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
6654 else
6655 {
6656 print_vma (segment->p_memsz, FULL_HEX);
6657 }
6658
6659 printf (" %c%c%c ",
6660 (segment->p_flags & PF_R ? 'R' : ' '),
6661 (segment->p_flags & PF_W ? 'W' : ' '),
6662 (segment->p_flags & PF_X ? 'E' : ' '));
6663
6664 if ((unsigned long) segment->p_align == segment->p_align)
6665 printf ("%#lx", (unsigned long) segment->p_align);
6666 else
6667 {
6668 print_vma (segment->p_align, PREFIX_HEX);
6669 }
6670 }
6671 else
6672 {
6673 print_vma (segment->p_offset, FULL_HEX);
6674 putchar (' ');
6675 print_vma (segment->p_vaddr, FULL_HEX);
6676 putchar (' ');
6677 print_vma (segment->p_paddr, FULL_HEX);
6678 printf ("\n ");
6679 print_vma (segment->p_filesz, FULL_HEX);
6680 putchar (' ');
6681 print_vma (segment->p_memsz, FULL_HEX);
6682 printf (" %c%c%c ",
6683 (segment->p_flags & PF_R ? 'R' : ' '),
6684 (segment->p_flags & PF_W ? 'W' : ' '),
6685 (segment->p_flags & PF_X ? 'E' : ' '));
6686 print_vma (segment->p_align, PREFIX_HEX);
6687 }
6688
6689 putc ('\n', stdout);
6690 }
6691
6692 switch (segment->p_type)
6693 {
6694 case PT_LOAD:
6695 #if 0 /* Do not warn about out of order PT_LOAD segments. Although officially
6696 required by the ELF standard, several programs, including the Linux
6697 kernel, make use of non-ordered segments. */
6698 if (previous_load
6699 && previous_load->p_vaddr > segment->p_vaddr)
6700 error (_("LOAD segments must be sorted in order of increasing VirtAddr\n"));
6701 #endif
6702 if (segment->p_memsz < segment->p_filesz)
6703 error (_("the segment's file size is larger than its memory size\n"));
6704 previous_load = segment;
6705 break;
6706
6707 case PT_PHDR:
6708 /* PR 20815 - Verify that the program header is loaded into memory. */
6709 if (i > 0 && previous_load != NULL)
6710 error (_("the PHDR segment must occur before any LOAD segment\n"));
6711 if (filedata->file_header.e_machine != EM_PARISC)
6712 {
6713 unsigned int j;
6714
6715 for (j = 1; j < filedata->file_header.e_phnum; j++)
6716 {
6717 Elf_Internal_Phdr *load = filedata->program_headers + j;
6718 if (load->p_type == PT_LOAD
6719 && load->p_offset <= segment->p_offset
6720 && (load->p_offset + load->p_filesz
6721 >= segment->p_offset + segment->p_filesz)
6722 && load->p_vaddr <= segment->p_vaddr
6723 && (load->p_vaddr + load->p_filesz
6724 >= segment->p_vaddr + segment->p_filesz))
6725 break;
6726 }
6727 if (j == filedata->file_header.e_phnum)
6728 error (_("the PHDR segment is not covered by a LOAD segment\n"));
6729 }
6730 break;
6731
6732 case PT_DYNAMIC:
6733 if (dynamic_addr)
6734 error (_("more than one dynamic segment\n"));
6735
6736 /* By default, assume that the .dynamic section is the first
6737 section in the DYNAMIC segment. */
6738 dynamic_addr = segment->p_offset;
6739 dynamic_size = segment->p_filesz;
6740
6741 /* Try to locate the .dynamic section. If there is
6742 a section header table, we can easily locate it. */
6743 if (filedata->section_headers != NULL)
6744 {
6745 Elf_Internal_Shdr * sec;
6746
6747 sec = find_section (filedata, ".dynamic");
6748 if (sec == NULL || sec->sh_size == 0)
6749 {
6750 /* A corresponding .dynamic section is expected, but on
6751 IA-64/OpenVMS it is OK for it to be missing. */
6752 if (!is_ia64_vms (filedata))
6753 error (_("no .dynamic section in the dynamic segment\n"));
6754 break;
6755 }
6756
6757 if (sec->sh_type == SHT_NOBITS)
6758 {
6759 dynamic_addr = 0;
6760 dynamic_size = 0;
6761 break;
6762 }
6763
6764 dynamic_addr = sec->sh_offset;
6765 dynamic_size = sec->sh_size;
6766
6767 /* The PT_DYNAMIC segment, which is used by the run-time
6768 loader, should exactly match the .dynamic section. */
6769 if (do_checks
6770 && (dynamic_addr != segment->p_offset
6771 || dynamic_size != segment->p_filesz))
6772 warn (_("\
6773 the .dynamic section is not the same as the dynamic segment\n"));
6774 }
6775
6776 /* PR binutils/17512: Avoid corrupt dynamic section info in the
6777 segment. Check this after matching against the section headers
6778 so we don't warn on debuginfo file (which have NOBITS .dynamic
6779 sections). */
6780 if (dynamic_addr > filedata->file_size
6781 || (dynamic_size > filedata->file_size - dynamic_addr))
6782 {
6783 error (_("the dynamic segment offset + size exceeds the size of the file\n"));
6784 dynamic_addr = 0;
6785 dynamic_size = 0;
6786 }
6787 break;
6788
6789 case PT_INTERP:
6790 if (segment->p_offset >= filedata->file_size
6791 || segment->p_filesz > filedata->file_size - segment->p_offset
6792 || segment->p_filesz - 1 >= (size_t) -2
6793 || fseek64 (filedata->handle,
6794 filedata->archive_file_offset + segment->p_offset,
6795 SEEK_SET))
6796 error (_("Unable to find program interpreter name\n"));
6797 else
6798 {
6799 size_t len = segment->p_filesz;
6800 free (filedata->program_interpreter);
6801 filedata->program_interpreter = xmalloc (len + 1);
6802 len = fread (filedata->program_interpreter, 1, len,
6803 filedata->handle);
6804 filedata->program_interpreter[len] = 0;
6805
6806 if (do_segments)
6807 printf (_(" [Requesting program interpreter: %s]\n"),
6808 filedata->program_interpreter);
6809 }
6810 break;
6811 }
6812 }
6813
6814 if (do_segments
6815 && filedata->section_headers != NULL
6816 && filedata->string_table != NULL)
6817 {
6818 printf (_("\n Section to Segment mapping:\n"));
6819 printf (_(" Segment Sections...\n"));
6820
6821 for (i = 0; i < filedata->file_header.e_phnum; i++)
6822 {
6823 unsigned int j;
6824 Elf_Internal_Shdr * section;
6825
6826 segment = filedata->program_headers + i;
6827 section = filedata->section_headers + 1;
6828
6829 printf (" %2.2d ", i);
6830
6831 for (j = 1; j < filedata->file_header.e_shnum; j++, section++)
6832 {
6833 if (!ELF_TBSS_SPECIAL (section, segment)
6834 && ELF_SECTION_IN_SEGMENT_STRICT (section, segment))
6835 printf ("%s ", printable_section_name (filedata, section));
6836 }
6837
6838 putc ('\n',stdout);
6839 }
6840 }
6841
6842 filedata->dynamic_addr = dynamic_addr;
6843 filedata->dynamic_size = dynamic_size ? dynamic_size : 1;
6844 return;
6845
6846 no_headers:
6847 filedata->dynamic_addr = 0;
6848 filedata->dynamic_size = 1;
6849 }
6850
6851
6852 /* Find the file offset corresponding to VMA by using the program headers. */
6853
6854 static int64_t
6855 offset_from_vma (Filedata * filedata, uint64_t vma, uint64_t size)
6856 {
6857 Elf_Internal_Phdr * seg;
6858
6859 if (! get_program_headers (filedata))
6860 {
6861 warn (_("Cannot interpret virtual addresses without program headers.\n"));
6862 return (long) vma;
6863 }
6864
6865 for (seg = filedata->program_headers;
6866 seg < filedata->program_headers + filedata->file_header.e_phnum;
6867 ++seg)
6868 {
6869 if (seg->p_type != PT_LOAD)
6870 continue;
6871
6872 if (vma >= (seg->p_vaddr & -seg->p_align)
6873 && vma + size <= seg->p_vaddr + seg->p_filesz)
6874 return vma - seg->p_vaddr + seg->p_offset;
6875 }
6876
6877 warn (_("Virtual address %#" PRIx64
6878 " not located in any PT_LOAD segment.\n"), vma);
6879 return vma;
6880 }
6881
6882
6883 /* Allocate memory and load the sections headers into FILEDATA->filedata->section_headers.
6884 If PROBE is true, this is just a probe and we do not generate any error
6885 messages if the load fails. */
6886
6887 static bool
6888 get_32bit_section_headers (Filedata * filedata, bool probe)
6889 {
6890 Elf32_External_Shdr * shdrs;
6891 Elf_Internal_Shdr * internal;
6892 unsigned int i;
6893 unsigned int size = filedata->file_header.e_shentsize;
6894 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6895
6896 /* PR binutils/17531: Cope with unexpected section header sizes. */
6897 if (size == 0 || num == 0)
6898 return false;
6899
6900 /* The section header cannot be at the start of the file - that is
6901 where the ELF file header is located. A file with absolutely no
6902 sections in it will use a shoff of 0. */
6903 if (filedata->file_header.e_shoff == 0)
6904 return false;
6905
6906 if (size < sizeof * shdrs)
6907 {
6908 if (! probe)
6909 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6910 return false;
6911 }
6912 if (!probe && size > sizeof * shdrs)
6913 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6914
6915 shdrs = (Elf32_External_Shdr *) get_data (NULL, filedata, filedata->file_header.e_shoff,
6916 size, num,
6917 probe ? NULL : _("section headers"));
6918 if (shdrs == NULL)
6919 return false;
6920
6921 filedata->section_headers = (Elf_Internal_Shdr *)
6922 cmalloc (num, sizeof (Elf_Internal_Shdr));
6923 if (filedata->section_headers == NULL)
6924 {
6925 if (!probe)
6926 error (_("Out of memory reading %u section headers\n"), num);
6927 free (shdrs);
6928 return false;
6929 }
6930
6931 for (i = 0, internal = filedata->section_headers;
6932 i < num;
6933 i++, internal++)
6934 {
6935 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
6936 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
6937 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
6938 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
6939 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
6940 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
6941 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
6942 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
6943 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
6944 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
6945 if (!probe && internal->sh_link > num)
6946 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
6947 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
6948 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
6949 }
6950
6951 free (shdrs);
6952 return true;
6953 }
6954
6955 /* Like get_32bit_section_headers, except that it fetches 64-bit headers. */
6956
6957 static bool
6958 get_64bit_section_headers (Filedata * filedata, bool probe)
6959 {
6960 Elf64_External_Shdr * shdrs;
6961 Elf_Internal_Shdr * internal;
6962 unsigned int i;
6963 unsigned int size = filedata->file_header.e_shentsize;
6964 unsigned int num = probe ? 1 : filedata->file_header.e_shnum;
6965
6966 /* PR binutils/17531: Cope with unexpected section header sizes. */
6967 if (size == 0 || num == 0)
6968 return false;
6969
6970 /* The section header cannot be at the start of the file - that is
6971 where the ELF file header is located. A file with absolutely no
6972 sections in it will use a shoff of 0. */
6973 if (filedata->file_header.e_shoff == 0)
6974 return false;
6975
6976 if (size < sizeof * shdrs)
6977 {
6978 if (! probe)
6979 error (_("The e_shentsize field in the ELF header is less than the size of an ELF section header\n"));
6980 return false;
6981 }
6982
6983 if (! probe && size > sizeof * shdrs)
6984 warn (_("The e_shentsize field in the ELF header is larger than the size of an ELF section header\n"));
6985
6986 shdrs = (Elf64_External_Shdr *) get_data (NULL, filedata,
6987 filedata->file_header.e_shoff,
6988 size, num,
6989 probe ? NULL : _("section headers"));
6990 if (shdrs == NULL)
6991 return false;
6992
6993 filedata->section_headers = (Elf_Internal_Shdr *)
6994 cmalloc (num, sizeof (Elf_Internal_Shdr));
6995 if (filedata->section_headers == NULL)
6996 {
6997 if (! probe)
6998 error (_("Out of memory reading %u section headers\n"), num);
6999 free (shdrs);
7000 return false;
7001 }
7002
7003 for (i = 0, internal = filedata->section_headers;
7004 i < num;
7005 i++, internal++)
7006 {
7007 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
7008 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
7009 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
7010 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
7011 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
7012 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
7013 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
7014 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
7015 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
7016 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
7017 if (!probe && internal->sh_link > num)
7018 warn (_("Section %u has an out of range sh_link value of %u\n"), i, internal->sh_link);
7019 if (!probe && internal->sh_flags & SHF_INFO_LINK && internal->sh_info > num)
7020 warn (_("Section %u has an out of range sh_info value of %u\n"), i, internal->sh_info);
7021 }
7022
7023 free (shdrs);
7024 return true;
7025 }
7026
7027 static bool
7028 get_section_headers (Filedata *filedata, bool probe)
7029 {
7030 if (filedata->section_headers != NULL)
7031 return true;
7032
7033 if (is_32bit_elf)
7034 return get_32bit_section_headers (filedata, probe);
7035 else
7036 return get_64bit_section_headers (filedata, probe);
7037 }
7038
7039 static Elf_Internal_Sym *
7040 get_32bit_elf_symbols (Filedata *filedata,
7041 Elf_Internal_Shdr *section,
7042 uint64_t *num_syms_return)
7043 {
7044 uint64_t number = 0;
7045 Elf32_External_Sym * esyms = NULL;
7046 Elf_External_Sym_Shndx * shndx = NULL;
7047 Elf_Internal_Sym * isyms = NULL;
7048 Elf_Internal_Sym * psym;
7049 unsigned int j;
7050 elf_section_list * entry;
7051
7052 if (section->sh_size == 0)
7053 {
7054 if (num_syms_return != NULL)
7055 * num_syms_return = 0;
7056 return NULL;
7057 }
7058
7059 /* Run some sanity checks first. */
7060 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
7061 {
7062 error (_("Section %s has an invalid sh_entsize of %#" PRIx64 "\n"),
7063 printable_section_name (filedata, section),
7064 section->sh_entsize);
7065 goto exit_point;
7066 }
7067
7068 if (section->sh_size > filedata->file_size)
7069 {
7070 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
7071 printable_section_name (filedata, section),
7072 section->sh_size);
7073 goto exit_point;
7074 }
7075
7076 number = section->sh_size / section->sh_entsize;
7077
7078 if (number * sizeof (Elf32_External_Sym) > section->sh_size + 1)
7079 {
7080 error (_("Size (%#" PRIx64 ") of section %s "
7081 "is not a multiple of its sh_entsize (%#" PRIx64 ")\n"),
7082 section->sh_size,
7083 printable_section_name (filedata, section),
7084 section->sh_entsize);
7085 goto exit_point;
7086 }
7087
7088 esyms = (Elf32_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
7089 section->sh_size, _("symbols"));
7090 if (esyms == NULL)
7091 goto exit_point;
7092
7093 shndx = NULL;
7094 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
7095 {
7096 if (entry->hdr->sh_link != (size_t) (section - filedata->section_headers))
7097 continue;
7098
7099 if (shndx != NULL)
7100 {
7101 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
7102 free (shndx);
7103 }
7104
7105 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
7106 entry->hdr->sh_offset,
7107 1, entry->hdr->sh_size,
7108 _("symbol table section indices"));
7109 if (shndx == NULL)
7110 goto exit_point;
7111
7112 /* PR17531: file: heap-buffer-overflow */
7113 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
7114 {
7115 error (_("Index section %s has an sh_size of %#" PRIx64 " - expected %#" PRIx64 "\n"),
7116 printable_section_name (filedata, entry->hdr),
7117 entry->hdr->sh_size,
7118 section->sh_size);
7119 goto exit_point;
7120 }
7121 }
7122
7123 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
7124
7125 if (isyms == NULL)
7126 {
7127 error (_("Out of memory reading %" PRIu64 " symbols\n"), number);
7128 goto exit_point;
7129 }
7130
7131 for (j = 0, psym = isyms; j < number; j++, psym++)
7132 {
7133 psym->st_name = BYTE_GET (esyms[j].st_name);
7134 psym->st_value = BYTE_GET (esyms[j].st_value);
7135 psym->st_size = BYTE_GET (esyms[j].st_size);
7136 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
7137 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
7138 psym->st_shndx
7139 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
7140 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
7141 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
7142 psym->st_info = BYTE_GET (esyms[j].st_info);
7143 psym->st_other = BYTE_GET (esyms[j].st_other);
7144 }
7145
7146 exit_point:
7147 free (shndx);
7148 free (esyms);
7149
7150 if (num_syms_return != NULL)
7151 * num_syms_return = isyms == NULL ? 0 : number;
7152
7153 return isyms;
7154 }
7155
7156 static Elf_Internal_Sym *
7157 get_64bit_elf_symbols (Filedata *filedata,
7158 Elf_Internal_Shdr *section,
7159 uint64_t *num_syms_return)
7160 {
7161 uint64_t number = 0;
7162 Elf64_External_Sym * esyms = NULL;
7163 Elf_External_Sym_Shndx * shndx = NULL;
7164 Elf_Internal_Sym * isyms = NULL;
7165 Elf_Internal_Sym * psym;
7166 unsigned int j;
7167 elf_section_list * entry;
7168
7169 if (section->sh_size == 0)
7170 {
7171 if (num_syms_return != NULL)
7172 * num_syms_return = 0;
7173 return NULL;
7174 }
7175
7176 /* Run some sanity checks first. */
7177 if (section->sh_entsize == 0 || section->sh_entsize > section->sh_size)
7178 {
7179 error (_("Section %s has an invalid sh_entsize of %#" PRIx64 "\n"),
7180 printable_section_name (filedata, section),
7181 section->sh_entsize);
7182 goto exit_point;
7183 }
7184
7185 if (section->sh_size > filedata->file_size)
7186 {
7187 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
7188 printable_section_name (filedata, section),
7189 section->sh_size);
7190 goto exit_point;
7191 }
7192
7193 number = section->sh_size / section->sh_entsize;
7194
7195 if (number * sizeof (Elf64_External_Sym) > section->sh_size + 1)
7196 {
7197 error (_("Size (%#" PRIx64 ") of section %s "
7198 "is not a multiple of its sh_entsize (%#" PRIx64 ")\n"),
7199 section->sh_size,
7200 printable_section_name (filedata, section),
7201 section->sh_entsize);
7202 goto exit_point;
7203 }
7204
7205 esyms = (Elf64_External_Sym *) get_data (NULL, filedata, section->sh_offset, 1,
7206 section->sh_size, _("symbols"));
7207 if (!esyms)
7208 goto exit_point;
7209
7210 shndx = NULL;
7211 for (entry = filedata->symtab_shndx_list; entry != NULL; entry = entry->next)
7212 {
7213 if (entry->hdr->sh_link != (size_t) (section - filedata->section_headers))
7214 continue;
7215
7216 if (shndx != NULL)
7217 {
7218 error (_("Multiple symbol table index sections associated with the same symbol section\n"));
7219 free (shndx);
7220 }
7221
7222 shndx = (Elf_External_Sym_Shndx *) get_data (NULL, filedata,
7223 entry->hdr->sh_offset,
7224 1, entry->hdr->sh_size,
7225 _("symbol table section indices"));
7226 if (shndx == NULL)
7227 goto exit_point;
7228
7229 /* PR17531: file: heap-buffer-overflow */
7230 if (entry->hdr->sh_size / sizeof (Elf_External_Sym_Shndx) < number)
7231 {
7232 error (_("Index section %s has an sh_size of %#" PRIx64 " - expected %#" PRIx64 "\n"),
7233 printable_section_name (filedata, entry->hdr),
7234 entry->hdr->sh_size,
7235 section->sh_size);
7236 goto exit_point;
7237 }
7238 }
7239
7240 isyms = (Elf_Internal_Sym *) cmalloc (number, sizeof (Elf_Internal_Sym));
7241
7242 if (isyms == NULL)
7243 {
7244 error (_("Out of memory reading %" PRIu64 " symbols\n"), number);
7245 goto exit_point;
7246 }
7247
7248 for (j = 0, psym = isyms; j < number; j++, psym++)
7249 {
7250 psym->st_name = BYTE_GET (esyms[j].st_name);
7251 psym->st_info = BYTE_GET (esyms[j].st_info);
7252 psym->st_other = BYTE_GET (esyms[j].st_other);
7253 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
7254
7255 if (psym->st_shndx == (SHN_XINDEX & 0xffff) && shndx != NULL)
7256 psym->st_shndx
7257 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
7258 else if (psym->st_shndx >= (SHN_LORESERVE & 0xffff))
7259 psym->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
7260
7261 psym->st_value = BYTE_GET (esyms[j].st_value);
7262 psym->st_size = BYTE_GET (esyms[j].st_size);
7263 }
7264
7265 exit_point:
7266 free (shndx);
7267 free (esyms);
7268
7269 if (num_syms_return != NULL)
7270 * num_syms_return = isyms == NULL ? 0 : number;
7271
7272 return isyms;
7273 }
7274
7275 static Elf_Internal_Sym *
7276 get_elf_symbols (Filedata *filedata,
7277 Elf_Internal_Shdr *section,
7278 uint64_t *num_syms_return)
7279 {
7280 if (is_32bit_elf)
7281 return get_32bit_elf_symbols (filedata, section, num_syms_return);
7282 else
7283 return get_64bit_elf_symbols (filedata, section, num_syms_return);
7284 }
7285
7286 static const char *
7287 get_elf_section_flags (Filedata * filedata, uint64_t sh_flags)
7288 {
7289 static char buff[1024];
7290 char * p = buff;
7291 unsigned int field_size = is_32bit_elf ? 8 : 16;
7292 signed int sindex;
7293 unsigned int size = sizeof (buff) - (field_size + 4 + 1);
7294 uint64_t os_flags = 0;
7295 uint64_t proc_flags = 0;
7296 uint64_t unknown_flags = 0;
7297 static const struct
7298 {
7299 const char * str;
7300 unsigned int len;
7301 }
7302 flags [] =
7303 {
7304 /* 0 */ { STRING_COMMA_LEN ("WRITE") },
7305 /* 1 */ { STRING_COMMA_LEN ("ALLOC") },
7306 /* 2 */ { STRING_COMMA_LEN ("EXEC") },
7307 /* 3 */ { STRING_COMMA_LEN ("MERGE") },
7308 /* 4 */ { STRING_COMMA_LEN ("STRINGS") },
7309 /* 5 */ { STRING_COMMA_LEN ("INFO LINK") },
7310 /* 6 */ { STRING_COMMA_LEN ("LINK ORDER") },
7311 /* 7 */ { STRING_COMMA_LEN ("OS NONCONF") },
7312 /* 8 */ { STRING_COMMA_LEN ("GROUP") },
7313 /* 9 */ { STRING_COMMA_LEN ("TLS") },
7314 /* IA-64 specific. */
7315 /* 10 */ { STRING_COMMA_LEN ("SHORT") },
7316 /* 11 */ { STRING_COMMA_LEN ("NORECOV") },
7317 /* IA-64 OpenVMS specific. */
7318 /* 12 */ { STRING_COMMA_LEN ("VMS_GLOBAL") },
7319 /* 13 */ { STRING_COMMA_LEN ("VMS_OVERLAID") },
7320 /* 14 */ { STRING_COMMA_LEN ("VMS_SHARED") },
7321 /* 15 */ { STRING_COMMA_LEN ("VMS_VECTOR") },
7322 /* 16 */ { STRING_COMMA_LEN ("VMS_ALLOC_64BIT") },
7323 /* 17 */ { STRING_COMMA_LEN ("VMS_PROTECTED") },
7324 /* Generic. */
7325 /* 18 */ { STRING_COMMA_LEN ("EXCLUDE") },
7326 /* SPARC specific. */
7327 /* 19 */ { STRING_COMMA_LEN ("ORDERED") },
7328 /* 20 */ { STRING_COMMA_LEN ("COMPRESSED") },
7329 /* ARM specific. */
7330 /* 21 */ { STRING_COMMA_LEN ("ENTRYSECT") },
7331 /* 22 */ { STRING_COMMA_LEN ("ARM_PURECODE") },
7332 /* 23 */ { STRING_COMMA_LEN ("COMDEF") },
7333 /* GNU specific. */
7334 /* 24 */ { STRING_COMMA_LEN ("GNU_MBIND") },
7335 /* VLE specific. */
7336 /* 25 */ { STRING_COMMA_LEN ("VLE") },
7337 /* GNU specific. */
7338 /* 26 */ { STRING_COMMA_LEN ("GNU_RETAIN") },
7339 };
7340
7341 if (do_section_details)
7342 p += sprintf (p, "[%*.*lx]: ",
7343 field_size, field_size, (unsigned long) sh_flags);
7344
7345 while (sh_flags)
7346 {
7347 uint64_t flag;
7348
7349 flag = sh_flags & - sh_flags;
7350 sh_flags &= ~ flag;
7351
7352 if (do_section_details)
7353 {
7354 switch (flag)
7355 {
7356 case SHF_WRITE: sindex = 0; break;
7357 case SHF_ALLOC: sindex = 1; break;
7358 case SHF_EXECINSTR: sindex = 2; break;
7359 case SHF_MERGE: sindex = 3; break;
7360 case SHF_STRINGS: sindex = 4; break;
7361 case SHF_INFO_LINK: sindex = 5; break;
7362 case SHF_LINK_ORDER: sindex = 6; break;
7363 case SHF_OS_NONCONFORMING: sindex = 7; break;
7364 case SHF_GROUP: sindex = 8; break;
7365 case SHF_TLS: sindex = 9; break;
7366 case SHF_EXCLUDE: sindex = 18; break;
7367 case SHF_COMPRESSED: sindex = 20; break;
7368
7369 default:
7370 sindex = -1;
7371 switch (filedata->file_header.e_machine)
7372 {
7373 case EM_IA_64:
7374 if (flag == SHF_IA_64_SHORT)
7375 sindex = 10;
7376 else if (flag == SHF_IA_64_NORECOV)
7377 sindex = 11;
7378 else if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS)
7379 switch (flag)
7380 {
7381 case SHF_IA_64_VMS_GLOBAL: sindex = 12; break;
7382 case SHF_IA_64_VMS_OVERLAID: sindex = 13; break;
7383 case SHF_IA_64_VMS_SHARED: sindex = 14; break;
7384 case SHF_IA_64_VMS_VECTOR: sindex = 15; break;
7385 case SHF_IA_64_VMS_ALLOC_64BIT: sindex = 16; break;
7386 case SHF_IA_64_VMS_PROTECTED: sindex = 17; break;
7387 default: break;
7388 }
7389 break;
7390
7391 case EM_386:
7392 case EM_IAMCU:
7393 case EM_X86_64:
7394 case EM_L1OM:
7395 case EM_K1OM:
7396 case EM_OLD_SPARCV9:
7397 case EM_SPARC32PLUS:
7398 case EM_SPARCV9:
7399 case EM_SPARC:
7400 if (flag == SHF_ORDERED)
7401 sindex = 19;
7402 break;
7403
7404 case EM_ARM:
7405 switch (flag)
7406 {
7407 case SHF_ENTRYSECT: sindex = 21; break;
7408 case SHF_ARM_PURECODE: sindex = 22; break;
7409 case SHF_COMDEF: sindex = 23; break;
7410 default: break;
7411 }
7412 break;
7413 case EM_PPC:
7414 if (flag == SHF_PPC_VLE)
7415 sindex = 25;
7416 break;
7417 default:
7418 break;
7419 }
7420
7421 switch (filedata->file_header.e_ident[EI_OSABI])
7422 {
7423 case ELFOSABI_GNU:
7424 case ELFOSABI_FREEBSD:
7425 if (flag == SHF_GNU_RETAIN)
7426 sindex = 26;
7427 /* Fall through */
7428 case ELFOSABI_NONE:
7429 if (flag == SHF_GNU_MBIND)
7430 /* We should not recognize SHF_GNU_MBIND for
7431 ELFOSABI_NONE, but binutils as of 2019-07-23 did
7432 not set the EI_OSABI header byte. */
7433 sindex = 24;
7434 break;
7435 default:
7436 break;
7437 }
7438 break;
7439 }
7440
7441 if (sindex != -1)
7442 {
7443 if (p != buff + field_size + 4)
7444 {
7445 if (size < (10 + 2))
7446 {
7447 warn (_("Internal error: not enough buffer room for section flag info"));
7448 return _("<unknown>");
7449 }
7450 size -= 2;
7451 *p++ = ',';
7452 *p++ = ' ';
7453 }
7454
7455 size -= flags [sindex].len;
7456 p = stpcpy (p, flags [sindex].str);
7457 }
7458 else if (flag & SHF_MASKOS)
7459 os_flags |= flag;
7460 else if (flag & SHF_MASKPROC)
7461 proc_flags |= flag;
7462 else
7463 unknown_flags |= flag;
7464 }
7465 else
7466 {
7467 switch (flag)
7468 {
7469 case SHF_WRITE: *p = 'W'; break;
7470 case SHF_ALLOC: *p = 'A'; break;
7471 case SHF_EXECINSTR: *p = 'X'; break;
7472 case SHF_MERGE: *p = 'M'; break;
7473 case SHF_STRINGS: *p = 'S'; break;
7474 case SHF_INFO_LINK: *p = 'I'; break;
7475 case SHF_LINK_ORDER: *p = 'L'; break;
7476 case SHF_OS_NONCONFORMING: *p = 'O'; break;
7477 case SHF_GROUP: *p = 'G'; break;
7478 case SHF_TLS: *p = 'T'; break;
7479 case SHF_EXCLUDE: *p = 'E'; break;
7480 case SHF_COMPRESSED: *p = 'C'; break;
7481
7482 default:
7483 if ((filedata->file_header.e_machine == EM_X86_64
7484 || filedata->file_header.e_machine == EM_L1OM
7485 || filedata->file_header.e_machine == EM_K1OM)
7486 && flag == SHF_X86_64_LARGE)
7487 *p = 'l';
7488 else if (filedata->file_header.e_machine == EM_ARM
7489 && flag == SHF_ARM_PURECODE)
7490 *p = 'y';
7491 else if (filedata->file_header.e_machine == EM_PPC
7492 && flag == SHF_PPC_VLE)
7493 *p = 'v';
7494 else if (flag & SHF_MASKOS)
7495 {
7496 switch (filedata->file_header.e_ident[EI_OSABI])
7497 {
7498 case ELFOSABI_GNU:
7499 case ELFOSABI_FREEBSD:
7500 if (flag == SHF_GNU_RETAIN)
7501 {
7502 *p = 'R';
7503 break;
7504 }
7505 /* Fall through */
7506 case ELFOSABI_NONE:
7507 if (flag == SHF_GNU_MBIND)
7508 {
7509 /* We should not recognize SHF_GNU_MBIND for
7510 ELFOSABI_NONE, but binutils as of 2019-07-23 did
7511 not set the EI_OSABI header byte. */
7512 *p = 'D';
7513 break;
7514 }
7515 /* Fall through */
7516 default:
7517 *p = 'o';
7518 sh_flags &= ~SHF_MASKOS;
7519 break;
7520 }
7521 }
7522 else if (flag & SHF_MASKPROC)
7523 {
7524 *p = 'p';
7525 sh_flags &= ~ SHF_MASKPROC;
7526 }
7527 else
7528 *p = 'x';
7529 break;
7530 }
7531 p++;
7532 }
7533 }
7534
7535 if (do_section_details)
7536 {
7537 if (os_flags)
7538 {
7539 if (p != buff + field_size + 4)
7540 {
7541 if (size < 2 + 5 + field_size + 1)
7542 {
7543 warn (_("Internal error: not enough buffer room for section flag info"));
7544 return _("<unknown>");
7545 }
7546 size -= 2;
7547 *p++ = ',';
7548 *p++ = ' ';
7549 }
7550 size -= 5 + field_size;
7551 p += sprintf (p, "OS (%*.*lx)", field_size, field_size,
7552 (unsigned long) os_flags);
7553 }
7554 if (proc_flags)
7555 {
7556 if (p != buff + field_size + 4)
7557 {
7558 if (size < 2 + 7 + field_size + 1)
7559 {
7560 warn (_("Internal error: not enough buffer room for section flag info"));
7561 return _("<unknown>");
7562 }
7563 size -= 2;
7564 *p++ = ',';
7565 *p++ = ' ';
7566 }
7567 size -= 7 + field_size;
7568 p += sprintf (p, "PROC (%*.*lx)", field_size, field_size,
7569 (unsigned long) proc_flags);
7570 }
7571 if (unknown_flags)
7572 {
7573 if (p != buff + field_size + 4)
7574 {
7575 if (size < 2 + 10 + field_size + 1)
7576 {
7577 warn (_("Internal error: not enough buffer room for section flag info"));
7578 return _("<unknown>");
7579 }
7580 size -= 2;
7581 *p++ = ',';
7582 *p++ = ' ';
7583 }
7584 size -= 10 + field_size;
7585 p += sprintf (p, _("UNKNOWN (%*.*lx)"), field_size, field_size,
7586 (unsigned long) unknown_flags);
7587 }
7588 }
7589
7590 *p = '\0';
7591 return buff;
7592 }
7593
7594 static unsigned int ATTRIBUTE_WARN_UNUSED_RESULT
7595 get_compression_header (Elf_Internal_Chdr *chdr, unsigned char *buf,
7596 uint64_t size)
7597 {
7598 if (is_32bit_elf)
7599 {
7600 Elf32_External_Chdr *echdr = (Elf32_External_Chdr *) buf;
7601
7602 if (size < sizeof (* echdr))
7603 {
7604 error (_("Compressed section is too small even for a compression header\n"));
7605 return 0;
7606 }
7607
7608 chdr->ch_type = BYTE_GET (echdr->ch_type);
7609 chdr->ch_size = BYTE_GET (echdr->ch_size);
7610 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7611 return sizeof (*echdr);
7612 }
7613 else
7614 {
7615 Elf64_External_Chdr *echdr = (Elf64_External_Chdr *) buf;
7616
7617 if (size < sizeof (* echdr))
7618 {
7619 error (_("Compressed section is too small even for a compression header\n"));
7620 return 0;
7621 }
7622
7623 chdr->ch_type = BYTE_GET (echdr->ch_type);
7624 chdr->ch_size = BYTE_GET (echdr->ch_size);
7625 chdr->ch_addralign = BYTE_GET (echdr->ch_addralign);
7626 return sizeof (*echdr);
7627 }
7628 }
7629
7630 static bool
7631 process_section_headers (Filedata * filedata)
7632 {
7633 Elf_Internal_Shdr * section;
7634 unsigned int i;
7635
7636 if (filedata->file_header.e_shnum == 0)
7637 {
7638 /* PR binutils/12467. */
7639 if (filedata->file_header.e_shoff != 0)
7640 {
7641 warn (_("possibly corrupt ELF file header - it has a non-zero"
7642 " section header offset, but no section headers\n"));
7643 return false;
7644 }
7645 else if (do_sections)
7646 printf (_("\nThere are no sections in this file.\n"));
7647
7648 return true;
7649 }
7650
7651 if (do_sections && !do_header)
7652 {
7653 if (filedata->is_separate && process_links)
7654 printf (_("In linked file '%s': "), filedata->file_name);
7655 if (! filedata->is_separate || process_links)
7656 printf (ngettext ("There is %d section header, "
7657 "starting at offset %#" PRIx64 ":\n",
7658 "There are %d section headers, "
7659 "starting at offset %#" PRIx64 ":\n",
7660 filedata->file_header.e_shnum),
7661 filedata->file_header.e_shnum,
7662 filedata->file_header.e_shoff);
7663 }
7664
7665 if (!get_section_headers (filedata, false))
7666 return false;
7667
7668 /* Read in the string table, so that we have names to display. */
7669 if (filedata->file_header.e_shstrndx != SHN_UNDEF
7670 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
7671 {
7672 section = filedata->section_headers + filedata->file_header.e_shstrndx;
7673
7674 if (section->sh_size != 0)
7675 {
7676 filedata->string_table = (char *) get_data (NULL, filedata, section->sh_offset,
7677 1, section->sh_size,
7678 _("string table"));
7679
7680 filedata->string_table_length = filedata->string_table != NULL ? section->sh_size : 0;
7681 }
7682 }
7683
7684 /* Scan the sections for the dynamic symbol table
7685 and dynamic string table and debug sections. */
7686 eh_addr_size = is_32bit_elf ? 4 : 8;
7687 switch (filedata->file_header.e_machine)
7688 {
7689 case EM_MIPS:
7690 case EM_MIPS_RS3_LE:
7691 /* The 64-bit MIPS EABI uses a combination of 32-bit ELF and 64-bit
7692 FDE addresses. However, the ABI also has a semi-official ILP32
7693 variant for which the normal FDE address size rules apply.
7694
7695 GCC 4.0 marks EABI64 objects with a dummy .gcc_compiled_longXX
7696 section, where XX is the size of longs in bits. Unfortunately,
7697 earlier compilers provided no way of distinguishing ILP32 objects
7698 from LP64 objects, so if there's any doubt, we should assume that
7699 the official LP64 form is being used. */
7700 if ((filedata->file_header.e_flags & EF_MIPS_ABI) == E_MIPS_ABI_EABI64
7701 && find_section (filedata, ".gcc_compiled_long32") == NULL)
7702 eh_addr_size = 8;
7703 break;
7704
7705 case EM_H8_300:
7706 case EM_H8_300H:
7707 switch (filedata->file_header.e_flags & EF_H8_MACH)
7708 {
7709 case E_H8_MACH_H8300:
7710 case E_H8_MACH_H8300HN:
7711 case E_H8_MACH_H8300SN:
7712 case E_H8_MACH_H8300SXN:
7713 eh_addr_size = 2;
7714 break;
7715 case E_H8_MACH_H8300H:
7716 case E_H8_MACH_H8300S:
7717 case E_H8_MACH_H8300SX:
7718 eh_addr_size = 4;
7719 break;
7720 }
7721 break;
7722
7723 case EM_M32C_OLD:
7724 case EM_M32C:
7725 switch (filedata->file_header.e_flags & EF_M32C_CPU_MASK)
7726 {
7727 case EF_M32C_CPU_M16C:
7728 eh_addr_size = 2;
7729 break;
7730 }
7731 break;
7732 }
7733
7734 #define CHECK_ENTSIZE_VALUES(section, i, size32, size64) \
7735 do \
7736 { \
7737 uint64_t expected_entsize = is_32bit_elf ? size32 : size64; \
7738 if (section->sh_entsize != expected_entsize) \
7739 { \
7740 error (_("Section %d has invalid sh_entsize of %" PRIx64 "\n"), \
7741 i, section->sh_entsize); \
7742 error (_("(Using the expected size of %" PRIx64 " for the rest of this dump)\n"), \
7743 expected_entsize); \
7744 section->sh_entsize = expected_entsize; \
7745 } \
7746 } \
7747 while (0)
7748
7749 #define CHECK_ENTSIZE(section, i, type) \
7750 CHECK_ENTSIZE_VALUES (section, i, sizeof (Elf32_External_##type), \
7751 sizeof (Elf64_External_##type))
7752
7753 for (i = 0, section = filedata->section_headers;
7754 i < filedata->file_header.e_shnum;
7755 i++, section++)
7756 {
7757 const char *name = printable_section_name (filedata, section);
7758
7759 /* Run some sanity checks on the headers and
7760 possibly fill in some file data as well. */
7761 switch (section->sh_type)
7762 {
7763 case SHT_DYNSYM:
7764 if (filedata->dynamic_symbols != NULL)
7765 {
7766 error (_("File contains multiple dynamic symbol tables\n"));
7767 continue;
7768 }
7769
7770 CHECK_ENTSIZE (section, i, Sym);
7771 filedata->dynamic_symbols
7772 = get_elf_symbols (filedata, section, &filedata->num_dynamic_syms);
7773 filedata->dynamic_symtab_section = section;
7774 break;
7775
7776 case SHT_STRTAB:
7777 if (streq (name, ".dynstr"))
7778 {
7779 if (filedata->dynamic_strings != NULL)
7780 {
7781 error (_("File contains multiple dynamic string tables\n"));
7782 continue;
7783 }
7784
7785 filedata->dynamic_strings
7786 = (char *) get_data (NULL, filedata, section->sh_offset,
7787 1, section->sh_size, _("dynamic strings"));
7788 filedata->dynamic_strings_length
7789 = filedata->dynamic_strings == NULL ? 0 : section->sh_size;
7790 filedata->dynamic_strtab_section = section;
7791 }
7792 break;
7793
7794 case SHT_SYMTAB_SHNDX:
7795 {
7796 elf_section_list * entry = xmalloc (sizeof * entry);
7797
7798 entry->hdr = section;
7799 entry->next = filedata->symtab_shndx_list;
7800 filedata->symtab_shndx_list = entry;
7801 }
7802 break;
7803
7804 case SHT_SYMTAB:
7805 CHECK_ENTSIZE (section, i, Sym);
7806 break;
7807
7808 case SHT_GROUP:
7809 CHECK_ENTSIZE_VALUES (section, i, GRP_ENTRY_SIZE, GRP_ENTRY_SIZE);
7810 break;
7811
7812 case SHT_REL:
7813 CHECK_ENTSIZE (section, i, Rel);
7814 if (do_checks && section->sh_size == 0)
7815 warn (_("Section '%s': zero-sized relocation section\n"), name);
7816 break;
7817
7818 case SHT_RELA:
7819 CHECK_ENTSIZE (section, i, Rela);
7820 if (do_checks && section->sh_size == 0)
7821 warn (_("Section '%s': zero-sized relocation section\n"), name);
7822 break;
7823
7824 case SHT_RELR:
7825 CHECK_ENTSIZE (section, i, Relr);
7826 break;
7827
7828 case SHT_NOTE:
7829 case SHT_PROGBITS:
7830 /* Having a zero sized section is not illegal according to the
7831 ELF standard, but it might be an indication that something
7832 is wrong. So issue a warning if we are running in lint mode. */
7833 if (do_checks && section->sh_size == 0)
7834 warn (_("Section '%s': has a size of zero - is this intended ?\n"), name);
7835 break;
7836
7837 default:
7838 break;
7839 }
7840
7841 if ((do_debugging || do_debug_info || do_debug_abbrevs
7842 || do_debug_lines || do_debug_pubnames || do_debug_pubtypes
7843 || do_debug_aranges || do_debug_frames || do_debug_macinfo
7844 || do_debug_str || do_debug_str_offsets || do_debug_loc
7845 || do_debug_ranges
7846 || do_debug_addr || do_debug_cu_index || do_debug_links)
7847 && (startswith (name, ".debug_")
7848 || startswith (name, ".zdebug_")))
7849 {
7850 if (name[1] == 'z')
7851 name += sizeof (".zdebug_") - 1;
7852 else
7853 name += sizeof (".debug_") - 1;
7854
7855 if (do_debugging
7856 || (do_debug_info && startswith (name, "info"))
7857 || (do_debug_info && startswith (name, "types"))
7858 || (do_debug_abbrevs && startswith (name, "abbrev"))
7859 || (do_debug_lines && strcmp (name, "line") == 0)
7860 || (do_debug_lines && startswith (name, "line."))
7861 || (do_debug_pubnames && startswith (name, "pubnames"))
7862 || (do_debug_pubtypes && startswith (name, "pubtypes"))
7863 || (do_debug_pubnames && startswith (name, "gnu_pubnames"))
7864 || (do_debug_pubtypes && startswith (name, "gnu_pubtypes"))
7865 || (do_debug_aranges && startswith (name, "aranges"))
7866 || (do_debug_ranges && startswith (name, "ranges"))
7867 || (do_debug_ranges && startswith (name, "rnglists"))
7868 || (do_debug_frames && startswith (name, "frame"))
7869 || (do_debug_macinfo && startswith (name, "macinfo"))
7870 || (do_debug_macinfo && startswith (name, "macro"))
7871 || (do_debug_str && startswith (name, "str"))
7872 || (do_debug_links && startswith (name, "sup"))
7873 || (do_debug_str_offsets && startswith (name, "str_offsets"))
7874 || (do_debug_loc && startswith (name, "loc"))
7875 || (do_debug_loc && startswith (name, "loclists"))
7876 || (do_debug_addr && startswith (name, "addr"))
7877 || (do_debug_cu_index && startswith (name, "cu_index"))
7878 || (do_debug_cu_index && startswith (name, "tu_index"))
7879 )
7880 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7881 }
7882 /* Linkonce section to be combined with .debug_info at link time. */
7883 else if ((do_debugging || do_debug_info)
7884 && startswith (name, ".gnu.linkonce.wi."))
7885 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7886 else if (do_debug_frames && streq (name, ".eh_frame"))
7887 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7888 else if (do_gdb_index && (streq (name, ".gdb_index")
7889 || streq (name, ".debug_names")))
7890 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7891 /* Trace sections for Itanium VMS. */
7892 else if ((do_debugging || do_trace_info || do_trace_abbrevs
7893 || do_trace_aranges)
7894 && startswith (name, ".trace_"))
7895 {
7896 name += sizeof (".trace_") - 1;
7897
7898 if (do_debugging
7899 || (do_trace_info && streq (name, "info"))
7900 || (do_trace_abbrevs && streq (name, "abbrev"))
7901 || (do_trace_aranges && streq (name, "aranges"))
7902 )
7903 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7904 }
7905 else if ((do_debugging || do_debug_links)
7906 && (startswith (name, ".gnu_debuglink")
7907 || startswith (name, ".gnu_debugaltlink")))
7908 request_dump_bynumber (&filedata->dump, i, DEBUG_DUMP);
7909 }
7910
7911 if (! do_sections)
7912 return true;
7913
7914 if (filedata->is_separate && ! process_links)
7915 return true;
7916
7917 if (filedata->is_separate)
7918 printf (_("\nSection Headers in linked file '%s':\n"), filedata->file_name);
7919 else if (filedata->file_header.e_shnum > 1)
7920 printf (_("\nSection Headers:\n"));
7921 else
7922 printf (_("\nSection Header:\n"));
7923
7924 if (is_32bit_elf)
7925 {
7926 if (do_section_details)
7927 {
7928 printf (_(" [Nr] Name\n"));
7929 printf (_(" Type Addr Off Size ES Lk Inf Al\n"));
7930 }
7931 else
7932 printf
7933 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
7934 }
7935 else if (do_wide)
7936 {
7937 if (do_section_details)
7938 {
7939 printf (_(" [Nr] Name\n"));
7940 printf (_(" Type Address Off Size ES Lk Inf Al\n"));
7941 }
7942 else
7943 printf
7944 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
7945 }
7946 else
7947 {
7948 if (do_section_details)
7949 {
7950 printf (_(" [Nr] Name\n"));
7951 printf (_(" Type Address Offset Link\n"));
7952 printf (_(" Size EntSize Info Align\n"));
7953 }
7954 else
7955 {
7956 printf (_(" [Nr] Name Type Address Offset\n"));
7957 printf (_(" Size EntSize Flags Link Info Align\n"));
7958 }
7959 }
7960
7961 if (do_section_details)
7962 printf (_(" Flags\n"));
7963
7964 for (i = 0, section = filedata->section_headers;
7965 i < filedata->file_header.e_shnum;
7966 i++, section++)
7967 {
7968 /* Run some sanity checks on the section header. */
7969
7970 /* Check the sh_link field. */
7971 switch (section->sh_type)
7972 {
7973 case SHT_REL:
7974 case SHT_RELA:
7975 if (section->sh_link == 0
7976 && (filedata->file_header.e_type == ET_EXEC
7977 || filedata->file_header.e_type == ET_DYN))
7978 /* A dynamic relocation section where all entries use a
7979 zero symbol index need not specify a symtab section. */
7980 break;
7981 /* Fall through. */
7982 case SHT_SYMTAB_SHNDX:
7983 case SHT_GROUP:
7984 case SHT_HASH:
7985 case SHT_GNU_HASH:
7986 case SHT_GNU_versym:
7987 if (section->sh_link == 0
7988 || section->sh_link >= filedata->file_header.e_shnum
7989 || (filedata->section_headers[section->sh_link].sh_type != SHT_SYMTAB
7990 && filedata->section_headers[section->sh_link].sh_type != SHT_DYNSYM))
7991 warn (_("[%2u]: Link field (%u) should index a symtab section.\n"),
7992 i, section->sh_link);
7993 break;
7994
7995 case SHT_DYNAMIC:
7996 case SHT_SYMTAB:
7997 case SHT_DYNSYM:
7998 case SHT_GNU_verneed:
7999 case SHT_GNU_verdef:
8000 case SHT_GNU_LIBLIST:
8001 if (section->sh_link == 0
8002 || section->sh_link >= filedata->file_header.e_shnum
8003 || filedata->section_headers[section->sh_link].sh_type != SHT_STRTAB)
8004 warn (_("[%2u]: Link field (%u) should index a string section.\n"),
8005 i, section->sh_link);
8006 break;
8007
8008 case SHT_INIT_ARRAY:
8009 case SHT_FINI_ARRAY:
8010 case SHT_PREINIT_ARRAY:
8011 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
8012 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
8013 i, section->sh_link);
8014 break;
8015
8016 default:
8017 /* FIXME: Add support for target specific section types. */
8018 #if 0 /* Currently we do not check other section types as there are too
8019 many special cases. Stab sections for example have a type
8020 of SHT_PROGBITS but an sh_link field that links to the .stabstr
8021 section. */
8022 if (section->sh_type < SHT_LOOS && section->sh_link != 0)
8023 warn (_("[%2u]: Unexpected value (%u) in link field.\n"),
8024 i, section->sh_link);
8025 #endif
8026 break;
8027 }
8028
8029 /* Check the sh_info field. */
8030 switch (section->sh_type)
8031 {
8032 case SHT_REL:
8033 case SHT_RELA:
8034 if (section->sh_info == 0
8035 && (filedata->file_header.e_type == ET_EXEC
8036 || filedata->file_header.e_type == ET_DYN))
8037 /* Dynamic relocations apply to segments, so they do not
8038 need to specify the section they relocate. */
8039 break;
8040 if (section->sh_info == 0
8041 || section->sh_info >= filedata->file_header.e_shnum
8042 || (filedata->section_headers[section->sh_info].sh_type != SHT_PROGBITS
8043 && filedata->section_headers[section->sh_info].sh_type != SHT_NOBITS
8044 && filedata->section_headers[section->sh_info].sh_type != SHT_NOTE
8045 && filedata->section_headers[section->sh_info].sh_type != SHT_INIT_ARRAY
8046 && filedata->section_headers[section->sh_info].sh_type != SHT_FINI_ARRAY
8047 && filedata->section_headers[section->sh_info].sh_type != SHT_PREINIT_ARRAY
8048 /* FIXME: Are other section types valid ? */
8049 && filedata->section_headers[section->sh_info].sh_type < SHT_LOOS))
8050 warn (_("[%2u]: Info field (%u) should index a relocatable section.\n"),
8051 i, section->sh_info);
8052 break;
8053
8054 case SHT_DYNAMIC:
8055 case SHT_HASH:
8056 case SHT_SYMTAB_SHNDX:
8057 case SHT_INIT_ARRAY:
8058 case SHT_FINI_ARRAY:
8059 case SHT_PREINIT_ARRAY:
8060 if (section->sh_info != 0)
8061 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
8062 i, section->sh_info);
8063 break;
8064
8065 case SHT_GROUP:
8066 case SHT_SYMTAB:
8067 case SHT_DYNSYM:
8068 /* A symbol index - we assume that it is valid. */
8069 break;
8070
8071 default:
8072 /* FIXME: Add support for target specific section types. */
8073 if (section->sh_type == SHT_NOBITS)
8074 /* NOBITS section headers with non-zero sh_info fields can be
8075 created when a binary is stripped of everything but its debug
8076 information. The stripped sections have their headers
8077 preserved but their types set to SHT_NOBITS. So do not check
8078 this type of section. */
8079 ;
8080 else if (section->sh_flags & SHF_INFO_LINK)
8081 {
8082 if (section->sh_info < 1 || section->sh_info >= filedata->file_header.e_shnum)
8083 warn (_("[%2u]: Expected link to another section in info field"), i);
8084 }
8085 else if (section->sh_type < SHT_LOOS
8086 && (section->sh_flags & SHF_GNU_MBIND) == 0
8087 && section->sh_info != 0)
8088 warn (_("[%2u]: Unexpected value (%u) in info field.\n"),
8089 i, section->sh_info);
8090 break;
8091 }
8092
8093 /* Check the sh_size field. */
8094 if (section->sh_size > filedata->file_size
8095 && section->sh_type != SHT_NOBITS
8096 && section->sh_type != SHT_NULL
8097 && section->sh_type < SHT_LOOS)
8098 warn (_("Size of section %u is larger than the entire file!\n"), i);
8099
8100 printf (" [%2u] ", i);
8101 if (do_section_details)
8102 printf ("%s\n ", printable_section_name (filedata, section));
8103 else
8104 print_symbol_name (-17, printable_section_name (filedata, section));
8105
8106 printf (do_wide ? " %-15s " : " %-15.15s ",
8107 get_section_type_name (filedata, section->sh_type));
8108
8109 if (is_32bit_elf)
8110 {
8111 const char * link_too_big = NULL;
8112
8113 print_vma (section->sh_addr, LONG_HEX);
8114
8115 printf ( " %6.6lx %6.6lx %2.2lx",
8116 (unsigned long) section->sh_offset,
8117 (unsigned long) section->sh_size,
8118 (unsigned long) section->sh_entsize);
8119
8120 if (do_section_details)
8121 fputs (" ", stdout);
8122 else
8123 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
8124
8125 if (section->sh_link >= filedata->file_header.e_shnum)
8126 {
8127 link_too_big = "";
8128 /* The sh_link value is out of range. Normally this indicates
8129 an error but it can have special values in Solaris binaries. */
8130 switch (filedata->file_header.e_machine)
8131 {
8132 case EM_386:
8133 case EM_IAMCU:
8134 case EM_X86_64:
8135 case EM_L1OM:
8136 case EM_K1OM:
8137 case EM_OLD_SPARCV9:
8138 case EM_SPARC32PLUS:
8139 case EM_SPARCV9:
8140 case EM_SPARC:
8141 if (section->sh_link == (SHN_BEFORE & 0xffff))
8142 link_too_big = "BEFORE";
8143 else if (section->sh_link == (SHN_AFTER & 0xffff))
8144 link_too_big = "AFTER";
8145 break;
8146 default:
8147 break;
8148 }
8149 }
8150
8151 if (do_section_details)
8152 {
8153 if (link_too_big != NULL && * link_too_big)
8154 printf ("<%s> ", link_too_big);
8155 else
8156 printf ("%2u ", section->sh_link);
8157 printf ("%3u %2lu\n", section->sh_info,
8158 (unsigned long) section->sh_addralign);
8159 }
8160 else
8161 printf ("%2u %3u %2lu\n",
8162 section->sh_link,
8163 section->sh_info,
8164 (unsigned long) section->sh_addralign);
8165
8166 if (link_too_big && ! * link_too_big)
8167 warn (_("section %u: sh_link value of %u is larger than the number of sections\n"),
8168 i, section->sh_link);
8169 }
8170 else if (do_wide)
8171 {
8172 print_vma (section->sh_addr, LONG_HEX);
8173
8174 if ((long) section->sh_offset == section->sh_offset)
8175 printf (" %6.6lx", (unsigned long) section->sh_offset);
8176 else
8177 {
8178 putchar (' ');
8179 print_vma (section->sh_offset, LONG_HEX);
8180 }
8181
8182 if ((unsigned long) section->sh_size == section->sh_size)
8183 printf (" %6.6lx", (unsigned long) section->sh_size);
8184 else
8185 {
8186 putchar (' ');
8187 print_vma (section->sh_size, LONG_HEX);
8188 }
8189
8190 if ((unsigned long) section->sh_entsize == section->sh_entsize)
8191 printf (" %2.2lx", (unsigned long) section->sh_entsize);
8192 else
8193 {
8194 putchar (' ');
8195 print_vma (section->sh_entsize, LONG_HEX);
8196 }
8197
8198 if (do_section_details)
8199 fputs (" ", stdout);
8200 else
8201 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
8202
8203 printf ("%2u %3u ", section->sh_link, section->sh_info);
8204
8205 if ((unsigned long) section->sh_addralign == section->sh_addralign)
8206 printf ("%2lu\n", (unsigned long) section->sh_addralign);
8207 else
8208 {
8209 print_vma (section->sh_addralign, DEC);
8210 putchar ('\n');
8211 }
8212 }
8213 else if (do_section_details)
8214 {
8215 putchar (' ');
8216 print_vma (section->sh_addr, LONG_HEX);
8217 if ((long) section->sh_offset == section->sh_offset)
8218 printf (" %16.16lx", (unsigned long) section->sh_offset);
8219 else
8220 {
8221 printf (" ");
8222 print_vma (section->sh_offset, LONG_HEX);
8223 }
8224 printf (" %u\n ", section->sh_link);
8225 print_vma (section->sh_size, LONG_HEX);
8226 putchar (' ');
8227 print_vma (section->sh_entsize, LONG_HEX);
8228
8229 printf (" %-16u %lu\n",
8230 section->sh_info,
8231 (unsigned long) section->sh_addralign);
8232 }
8233 else
8234 {
8235 putchar (' ');
8236 print_vma (section->sh_addr, LONG_HEX);
8237 if ((long) section->sh_offset == section->sh_offset)
8238 printf (" %8.8lx", (unsigned long) section->sh_offset);
8239 else
8240 {
8241 printf (" ");
8242 print_vma (section->sh_offset, LONG_HEX);
8243 }
8244 printf ("\n ");
8245 print_vma (section->sh_size, LONG_HEX);
8246 printf (" ");
8247 print_vma (section->sh_entsize, LONG_HEX);
8248
8249 printf (" %3s ", get_elf_section_flags (filedata, section->sh_flags));
8250
8251 printf (" %2u %3u %lu\n",
8252 section->sh_link,
8253 section->sh_info,
8254 (unsigned long) section->sh_addralign);
8255 }
8256
8257 if (do_section_details)
8258 {
8259 printf (" %s\n", get_elf_section_flags (filedata, section->sh_flags));
8260 if ((section->sh_flags & SHF_COMPRESSED) != 0)
8261 {
8262 /* Minimum section size is 12 bytes for 32-bit compression
8263 header + 12 bytes for compressed data header. */
8264 unsigned char buf[24];
8265
8266 assert (sizeof (buf) >= sizeof (Elf64_External_Chdr));
8267 if (get_data (&buf, filedata, section->sh_offset, 1,
8268 sizeof (buf), _("compression header")))
8269 {
8270 Elf_Internal_Chdr chdr;
8271
8272 if (get_compression_header (&chdr, buf, sizeof (buf)) == 0)
8273 printf (_(" [<corrupt>]\n"));
8274 else
8275 {
8276 if (chdr.ch_type == ch_compress_zlib)
8277 printf (" ZLIB, ");
8278 else if (chdr.ch_type == ch_compress_zstd)
8279 printf (" ZSTD, ");
8280 else
8281 printf (_(" [<unknown>: 0x%x], "),
8282 chdr.ch_type);
8283 print_vma (chdr.ch_size, LONG_HEX);
8284 printf (", %lu\n", (unsigned long) chdr.ch_addralign);
8285 }
8286 }
8287 }
8288 }
8289 }
8290
8291 if (!do_section_details)
8292 {
8293 /* The ordering of the letters shown here matches the ordering of the
8294 corresponding SHF_xxx values, and hence the order in which these
8295 letters will be displayed to the user. */
8296 printf (_("Key to Flags:\n\
8297 W (write), A (alloc), X (execute), M (merge), S (strings), I (info),\n\
8298 L (link order), O (extra OS processing required), G (group), T (TLS),\n\
8299 C (compressed), x (unknown), o (OS specific), E (exclude),\n "));
8300 switch (filedata->file_header.e_ident[EI_OSABI])
8301 {
8302 case ELFOSABI_GNU:
8303 case ELFOSABI_FREEBSD:
8304 printf (_("R (retain), "));
8305 /* Fall through */
8306 case ELFOSABI_NONE:
8307 printf (_("D (mbind), "));
8308 break;
8309 default:
8310 break;
8311 }
8312 if (filedata->file_header.e_machine == EM_X86_64
8313 || filedata->file_header.e_machine == EM_L1OM
8314 || filedata->file_header.e_machine == EM_K1OM)
8315 printf (_("l (large), "));
8316 else if (filedata->file_header.e_machine == EM_ARM)
8317 printf (_("y (purecode), "));
8318 else if (filedata->file_header.e_machine == EM_PPC)
8319 printf (_("v (VLE), "));
8320 printf ("p (processor specific)\n");
8321 }
8322
8323 return true;
8324 }
8325
8326 static bool
8327 get_symtab (Filedata *filedata, Elf_Internal_Shdr *symsec,
8328 Elf_Internal_Sym **symtab, uint64_t *nsyms,
8329 char **strtab, uint64_t *strtablen)
8330 {
8331 *strtab = NULL;
8332 *strtablen = 0;
8333 *symtab = get_elf_symbols (filedata, symsec, nsyms);
8334
8335 if (*symtab == NULL)
8336 return false;
8337
8338 if (symsec->sh_link != 0)
8339 {
8340 Elf_Internal_Shdr *strsec;
8341
8342 if (symsec->sh_link >= filedata->file_header.e_shnum)
8343 {
8344 error (_("Bad sh_link in symbol table section\n"));
8345 free (*symtab);
8346 *symtab = NULL;
8347 *nsyms = 0;
8348 return false;
8349 }
8350
8351 strsec = filedata->section_headers + symsec->sh_link;
8352
8353 *strtab = (char *) get_data (NULL, filedata, strsec->sh_offset,
8354 1, strsec->sh_size, _("string table"));
8355 if (*strtab == NULL)
8356 {
8357 free (*symtab);
8358 *symtab = NULL;
8359 *nsyms = 0;
8360 return false;
8361 }
8362 *strtablen = strsec->sh_size;
8363 }
8364 return true;
8365 }
8366
8367 static const char *
8368 get_group_flags (unsigned int flags)
8369 {
8370 static char buff[128];
8371
8372 if (flags == 0)
8373 return "";
8374 else if (flags == GRP_COMDAT)
8375 return "COMDAT ";
8376
8377 snprintf (buff, sizeof buff, "[0x%x: %s%s%s]",
8378 flags,
8379 flags & GRP_MASKOS ? _("<OS specific>") : "",
8380 flags & GRP_MASKPROC ? _("<PROC specific>") : "",
8381 (flags & ~(GRP_COMDAT | GRP_MASKOS | GRP_MASKPROC)
8382 ? _("<unknown>") : ""));
8383
8384 return buff;
8385 }
8386
8387 static bool
8388 process_section_groups (Filedata * filedata)
8389 {
8390 Elf_Internal_Shdr * section;
8391 unsigned int i;
8392 struct group * group;
8393 Elf_Internal_Shdr * symtab_sec;
8394 Elf_Internal_Shdr * strtab_sec;
8395 Elf_Internal_Sym * symtab;
8396 uint64_t num_syms;
8397 char * strtab;
8398 size_t strtab_size;
8399
8400 /* Don't process section groups unless needed. */
8401 if (!do_unwind && !do_section_groups)
8402 return true;
8403
8404 if (filedata->file_header.e_shnum == 0)
8405 {
8406 if (do_section_groups)
8407 {
8408 if (filedata->is_separate)
8409 printf (_("\nThere are no sections group in linked file '%s'.\n"),
8410 filedata->file_name);
8411 else
8412 printf (_("\nThere are no section groups in this file.\n"));
8413 }
8414 return true;
8415 }
8416
8417 if (filedata->section_headers == NULL)
8418 {
8419 error (_("Section headers are not available!\n"));
8420 /* PR 13622: This can happen with a corrupt ELF header. */
8421 return false;
8422 }
8423
8424 filedata->section_headers_groups
8425 = (struct group **) calloc (filedata->file_header.e_shnum,
8426 sizeof (struct group *));
8427
8428 if (filedata->section_headers_groups == NULL)
8429 {
8430 error (_("Out of memory reading %u section group headers\n"),
8431 filedata->file_header.e_shnum);
8432 return false;
8433 }
8434
8435 /* Scan the sections for the group section. */
8436 filedata->group_count = 0;
8437 for (i = 0, section = filedata->section_headers;
8438 i < filedata->file_header.e_shnum;
8439 i++, section++)
8440 if (section->sh_type == SHT_GROUP)
8441 filedata->group_count++;
8442
8443 if (filedata->group_count == 0)
8444 {
8445 if (do_section_groups)
8446 {
8447 if (filedata->is_separate)
8448 printf (_("\nThere are no section groups in linked file '%s'.\n"),
8449 filedata->file_name);
8450 else
8451 printf (_("\nThere are no section groups in this file.\n"));
8452 }
8453
8454 return true;
8455 }
8456
8457 filedata->section_groups = (struct group *) calloc (filedata->group_count,
8458 sizeof (struct group));
8459
8460 if (filedata->section_groups == NULL)
8461 {
8462 error (_("Out of memory reading %zu groups\n"), filedata->group_count);
8463 return false;
8464 }
8465
8466 symtab_sec = NULL;
8467 strtab_sec = NULL;
8468 symtab = NULL;
8469 num_syms = 0;
8470 strtab = NULL;
8471 strtab_size = 0;
8472
8473 if (filedata->is_separate)
8474 printf (_("Section groups in linked file '%s'\n"), filedata->file_name);
8475
8476 for (i = 0, section = filedata->section_headers, group = filedata->section_groups;
8477 i < filedata->file_header.e_shnum;
8478 i++, section++)
8479 {
8480 if (section->sh_type == SHT_GROUP)
8481 {
8482 const char * name = printable_section_name (filedata, section);
8483 const char * group_name;
8484 unsigned char * start;
8485 unsigned char * indices;
8486 unsigned int entry, j, size;
8487 Elf_Internal_Shdr * sec;
8488 Elf_Internal_Sym * sym;
8489
8490 /* Get the symbol table. */
8491 if (section->sh_link >= filedata->file_header.e_shnum
8492 || ((sec = filedata->section_headers + section->sh_link)->sh_type
8493 != SHT_SYMTAB))
8494 {
8495 error (_("Bad sh_link in group section `%s'\n"), name);
8496 continue;
8497 }
8498
8499 if (symtab_sec != sec)
8500 {
8501 symtab_sec = sec;
8502 free (symtab);
8503 symtab = get_elf_symbols (filedata, symtab_sec, & num_syms);
8504 }
8505
8506 if (symtab == NULL)
8507 {
8508 error (_("Corrupt header in group section `%s'\n"), name);
8509 continue;
8510 }
8511
8512 if (section->sh_info >= num_syms)
8513 {
8514 error (_("Bad sh_info in group section `%s'\n"), name);
8515 continue;
8516 }
8517
8518 sym = symtab + section->sh_info;
8519
8520 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8521 {
8522 if (sym->st_shndx == 0
8523 || sym->st_shndx >= filedata->file_header.e_shnum)
8524 {
8525 error (_("Bad sh_info in group section `%s'\n"), name);
8526 continue;
8527 }
8528
8529 group_name = printable_section_name (filedata,
8530 filedata->section_headers
8531 + sym->st_shndx);
8532 strtab_sec = NULL;
8533 free (strtab);
8534 strtab = NULL;
8535 strtab_size = 0;
8536 }
8537 else
8538 {
8539 /* Get the string table. */
8540 if (symtab_sec->sh_link >= filedata->file_header.e_shnum)
8541 {
8542 strtab_sec = NULL;
8543 free (strtab);
8544 strtab = NULL;
8545 strtab_size = 0;
8546 }
8547 else if (strtab_sec
8548 != (sec = filedata->section_headers + symtab_sec->sh_link))
8549 {
8550 strtab_sec = sec;
8551 free (strtab);
8552
8553 strtab = (char *) get_data (NULL, filedata, strtab_sec->sh_offset,
8554 1, strtab_sec->sh_size,
8555 _("string table"));
8556 strtab_size = strtab != NULL ? strtab_sec->sh_size : 0;
8557 }
8558 group_name = sym->st_name < strtab_size
8559 ? strtab + sym->st_name : _("<corrupt>");
8560 }
8561
8562 /* PR 17531: file: loop. */
8563 if (section->sh_entsize > section->sh_size)
8564 {
8565 error (_("Section %s has sh_entsize (%#" PRIx64 ")"
8566 " which is larger than its size (%#" PRIx64 ")\n"),
8567 printable_section_name (filedata, section),
8568 section->sh_entsize,
8569 section->sh_size);
8570 continue;
8571 }
8572
8573 start = (unsigned char *) get_data (NULL, filedata, section->sh_offset,
8574 1, section->sh_size,
8575 _("section data"));
8576 if (start == NULL)
8577 continue;
8578
8579 indices = start;
8580 size = (section->sh_size / section->sh_entsize) - 1;
8581 entry = byte_get (indices, 4);
8582 indices += 4;
8583
8584 if (do_section_groups)
8585 {
8586 printf (_("\n%sgroup section [%5u] `%s' [%s] contains %u sections:\n"),
8587 get_group_flags (entry), i, name, group_name, size);
8588
8589 printf (_(" [Index] Name\n"));
8590 }
8591
8592 group->group_index = i;
8593
8594 for (j = 0; j < size; j++)
8595 {
8596 struct group_list * g;
8597
8598 entry = byte_get (indices, 4);
8599 indices += 4;
8600
8601 if (entry >= filedata->file_header.e_shnum)
8602 {
8603 static unsigned num_group_errors = 0;
8604
8605 if (num_group_errors ++ < 10)
8606 {
8607 error (_("section [%5u] in group section [%5u] > maximum section [%5u]\n"),
8608 entry, i, filedata->file_header.e_shnum - 1);
8609 if (num_group_errors == 10)
8610 warn (_("Further error messages about overlarge group section indices suppressed\n"));
8611 }
8612 continue;
8613 }
8614
8615 if (filedata->section_headers_groups [entry] != NULL)
8616 {
8617 if (entry)
8618 {
8619 static unsigned num_errs = 0;
8620
8621 if (num_errs ++ < 10)
8622 {
8623 error (_("section [%5u] in group section [%5u] already in group section [%5u]\n"),
8624 entry, i,
8625 filedata->section_headers_groups [entry]->group_index);
8626 if (num_errs == 10)
8627 warn (_("Further error messages about already contained group sections suppressed\n"));
8628 }
8629 continue;
8630 }
8631 else
8632 {
8633 /* Intel C/C++ compiler may put section 0 in a
8634 section group. We just warn it the first time
8635 and ignore it afterwards. */
8636 static bool warned = false;
8637 if (!warned)
8638 {
8639 error (_("section 0 in group section [%5u]\n"),
8640 filedata->section_headers_groups [entry]->group_index);
8641 warned = true;
8642 }
8643 }
8644 }
8645
8646 filedata->section_headers_groups [entry] = group;
8647
8648 if (do_section_groups)
8649 {
8650 sec = filedata->section_headers + entry;
8651 printf (" [%5u] %s\n", entry, printable_section_name (filedata, sec));
8652 }
8653
8654 g = (struct group_list *) xmalloc (sizeof (struct group_list));
8655 g->section_index = entry;
8656 g->next = group->root;
8657 group->root = g;
8658 }
8659
8660 free (start);
8661
8662 group++;
8663 }
8664 }
8665
8666 free (symtab);
8667 free (strtab);
8668 return true;
8669 }
8670
8671 /* Data used to display dynamic fixups. */
8672
8673 struct ia64_vms_dynfixup
8674 {
8675 uint64_t needed_ident; /* Library ident number. */
8676 uint64_t needed; /* Index in the dstrtab of the library name. */
8677 uint64_t fixup_needed; /* Index of the library. */
8678 uint64_t fixup_rela_cnt; /* Number of fixups. */
8679 uint64_t fixup_rela_off; /* Fixups offset in the dynamic segment. */
8680 };
8681
8682 /* Data used to display dynamic relocations. */
8683
8684 struct ia64_vms_dynimgrela
8685 {
8686 uint64_t img_rela_cnt; /* Number of relocations. */
8687 uint64_t img_rela_off; /* Reloc offset in the dynamic segment. */
8688 };
8689
8690 /* Display IA-64 OpenVMS dynamic fixups (used to dynamically link a shared
8691 library). */
8692
8693 static bool
8694 dump_ia64_vms_dynamic_fixups (Filedata * filedata,
8695 struct ia64_vms_dynfixup * fixup,
8696 const char * strtab,
8697 unsigned int strtab_sz)
8698 {
8699 Elf64_External_VMS_IMAGE_FIXUP * imfs;
8700 size_t i;
8701 const char * lib_name;
8702
8703 imfs = get_data (NULL, filedata,
8704 filedata->dynamic_addr + fixup->fixup_rela_off,
8705 sizeof (*imfs), fixup->fixup_rela_cnt,
8706 _("dynamic section image fixups"));
8707 if (!imfs)
8708 return false;
8709
8710 if (fixup->needed < strtab_sz)
8711 lib_name = strtab + fixup->needed;
8712 else
8713 {
8714 warn (_("corrupt library name index of %#" PRIx64
8715 " found in dynamic entry"), fixup->needed);
8716 lib_name = "???";
8717 }
8718
8719 printf (_("\nImage fixups for needed library #%" PRId64
8720 ": %s - ident: %" PRIx64 "\n"),
8721 fixup->fixup_needed, lib_name, fixup->needed_ident);
8722 printf
8723 (_("Seg Offset Type SymVec DataType\n"));
8724
8725 for (i = 0; i < (size_t) fixup->fixup_rela_cnt; i++)
8726 {
8727 unsigned int type;
8728 const char *rtype;
8729
8730 printf ("%3u ", (unsigned) BYTE_GET (imfs [i].fixup_seg));
8731 printf ("%016" PRIx64 " ", BYTE_GET (imfs [i].fixup_offset));
8732 type = BYTE_GET (imfs [i].type);
8733 rtype = elf_ia64_reloc_type (type);
8734 if (rtype == NULL)
8735 printf ("0x%08x ", type);
8736 else
8737 printf ("%-32s ", rtype);
8738 printf ("%6u ", (unsigned) BYTE_GET (imfs [i].symvec_index));
8739 printf ("0x%08x\n", (unsigned) BYTE_GET (imfs [i].data_type));
8740 }
8741
8742 free (imfs);
8743 return true;
8744 }
8745
8746 /* Display IA-64 OpenVMS dynamic relocations (used to relocate an image). */
8747
8748 static bool
8749 dump_ia64_vms_dynamic_relocs (Filedata * filedata, struct ia64_vms_dynimgrela *imgrela)
8750 {
8751 Elf64_External_VMS_IMAGE_RELA *imrs;
8752 size_t i;
8753
8754 imrs = get_data (NULL, filedata,
8755 filedata->dynamic_addr + imgrela->img_rela_off,
8756 sizeof (*imrs), imgrela->img_rela_cnt,
8757 _("dynamic section image relocations"));
8758 if (!imrs)
8759 return false;
8760
8761 printf (_("\nImage relocs\n"));
8762 printf
8763 (_("Seg Offset Type Addend Seg Sym Off\n"));
8764
8765 for (i = 0; i < (size_t) imgrela->img_rela_cnt; i++)
8766 {
8767 unsigned int type;
8768 const char *rtype;
8769
8770 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].rela_seg));
8771 printf ("%08" PRIx64 " ", BYTE_GET (imrs [i].rela_offset));
8772 type = BYTE_GET (imrs [i].type);
8773 rtype = elf_ia64_reloc_type (type);
8774 if (rtype == NULL)
8775 printf ("0x%08x ", type);
8776 else
8777 printf ("%-31s ", rtype);
8778 print_vma (BYTE_GET (imrs [i].addend), FULL_HEX);
8779 printf ("%3u ", (unsigned) BYTE_GET (imrs [i].sym_seg));
8780 printf ("%08" PRIx64 "\n", BYTE_GET (imrs [i].sym_offset));
8781 }
8782
8783 free (imrs);
8784 return true;
8785 }
8786
8787 /* Display IA-64 OpenVMS dynamic relocations and fixups. */
8788
8789 static bool
8790 process_ia64_vms_dynamic_relocs (Filedata * filedata)
8791 {
8792 struct ia64_vms_dynfixup fixup;
8793 struct ia64_vms_dynimgrela imgrela;
8794 Elf_Internal_Dyn *entry;
8795 uint64_t strtab_off = 0;
8796 uint64_t strtab_sz = 0;
8797 char *strtab = NULL;
8798 bool res = true;
8799
8800 memset (&fixup, 0, sizeof (fixup));
8801 memset (&imgrela, 0, sizeof (imgrela));
8802
8803 /* Note: the order of the entries is specified by the OpenVMS specs. */
8804 for (entry = filedata->dynamic_section;
8805 entry < filedata->dynamic_section + filedata->dynamic_nent;
8806 entry++)
8807 {
8808 switch (entry->d_tag)
8809 {
8810 case DT_IA_64_VMS_STRTAB_OFFSET:
8811 strtab_off = entry->d_un.d_val;
8812 break;
8813 case DT_STRSZ:
8814 strtab_sz = entry->d_un.d_val;
8815 if (strtab == NULL)
8816 strtab = get_data (NULL, filedata,
8817 filedata->dynamic_addr + strtab_off,
8818 1, strtab_sz, _("dynamic string section"));
8819 if (strtab == NULL)
8820 strtab_sz = 0;
8821 break;
8822
8823 case DT_IA_64_VMS_NEEDED_IDENT:
8824 fixup.needed_ident = entry->d_un.d_val;
8825 break;
8826 case DT_NEEDED:
8827 fixup.needed = entry->d_un.d_val;
8828 break;
8829 case DT_IA_64_VMS_FIXUP_NEEDED:
8830 fixup.fixup_needed = entry->d_un.d_val;
8831 break;
8832 case DT_IA_64_VMS_FIXUP_RELA_CNT:
8833 fixup.fixup_rela_cnt = entry->d_un.d_val;
8834 break;
8835 case DT_IA_64_VMS_FIXUP_RELA_OFF:
8836 fixup.fixup_rela_off = entry->d_un.d_val;
8837 if (! dump_ia64_vms_dynamic_fixups (filedata, &fixup, strtab, strtab_sz))
8838 res = false;
8839 break;
8840 case DT_IA_64_VMS_IMG_RELA_CNT:
8841 imgrela.img_rela_cnt = entry->d_un.d_val;
8842 break;
8843 case DT_IA_64_VMS_IMG_RELA_OFF:
8844 imgrela.img_rela_off = entry->d_un.d_val;
8845 if (! dump_ia64_vms_dynamic_relocs (filedata, &imgrela))
8846 res = false;
8847 break;
8848
8849 default:
8850 break;
8851 }
8852 }
8853
8854 free (strtab);
8855
8856 return res;
8857 }
8858
8859 static struct
8860 {
8861 const char * name;
8862 int reloc;
8863 int size;
8864 relocation_type rel_type;
8865 }
8866 dynamic_relocations [] =
8867 {
8868 { "REL", DT_REL, DT_RELSZ, reltype_rel },
8869 { "RELA", DT_RELA, DT_RELASZ, reltype_rela },
8870 { "RELR", DT_RELR, DT_RELRSZ, reltype_relr },
8871 { "PLT", DT_JMPREL, DT_PLTRELSZ, reltype_unknown }
8872 };
8873
8874 /* Process the reloc section. */
8875
8876 static bool
8877 process_relocs (Filedata * filedata)
8878 {
8879 uint64_t rel_size;
8880 uint64_t rel_offset;
8881
8882 if (!do_reloc)
8883 return true;
8884
8885 if (do_using_dynamic)
8886 {
8887 relocation_type rel_type;
8888 const char * name;
8889 bool has_dynamic_reloc;
8890 unsigned int i;
8891
8892 has_dynamic_reloc = false;
8893
8894 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
8895 {
8896 rel_type = dynamic_relocations [i].rel_type;
8897 name = dynamic_relocations [i].name;
8898 rel_size = filedata->dynamic_info[dynamic_relocations [i].size];
8899 rel_offset = filedata->dynamic_info[dynamic_relocations [i].reloc];
8900
8901 if (rel_size)
8902 has_dynamic_reloc = true;
8903
8904 if (rel_type == reltype_unknown)
8905 {
8906 if (dynamic_relocations [i].reloc == DT_JMPREL)
8907 switch (filedata->dynamic_info[DT_PLTREL])
8908 {
8909 case DT_REL:
8910 rel_type = reltype_rel;
8911 break;
8912 case DT_RELA:
8913 rel_type = reltype_rela;
8914 break;
8915 }
8916 }
8917
8918 if (rel_size)
8919 {
8920 if (filedata->is_separate)
8921 printf
8922 (_("\nIn linked file '%s' section '%s' at offset %#" PRIx64
8923 " contains %" PRId64 " bytes:\n"),
8924 filedata->file_name, name, rel_offset, rel_size);
8925 else
8926 printf
8927 (_("\n'%s' relocation section at offset %#" PRIx64
8928 " contains %" PRId64 " bytes:\n"),
8929 name, rel_offset, rel_size);
8930
8931 dump_relocations (filedata,
8932 offset_from_vma (filedata, rel_offset, rel_size),
8933 rel_size,
8934 filedata->dynamic_symbols,
8935 filedata->num_dynamic_syms,
8936 filedata->dynamic_strings,
8937 filedata->dynamic_strings_length,
8938 rel_type, true /* is_dynamic */);
8939 }
8940 }
8941
8942 if (is_ia64_vms (filedata))
8943 if (process_ia64_vms_dynamic_relocs (filedata))
8944 has_dynamic_reloc = true;
8945
8946 if (! has_dynamic_reloc)
8947 {
8948 if (filedata->is_separate)
8949 printf (_("\nThere are no dynamic relocations in linked file '%s'.\n"),
8950 filedata->file_name);
8951 else
8952 printf (_("\nThere are no dynamic relocations in this file.\n"));
8953 }
8954 }
8955 else
8956 {
8957 Elf_Internal_Shdr * section;
8958 size_t i;
8959 bool found = false;
8960
8961 for (i = 0, section = filedata->section_headers;
8962 i < filedata->file_header.e_shnum;
8963 i++, section++)
8964 {
8965 if ( section->sh_type != SHT_RELA
8966 && section->sh_type != SHT_REL
8967 && section->sh_type != SHT_RELR)
8968 continue;
8969
8970 rel_offset = section->sh_offset;
8971 rel_size = section->sh_size;
8972
8973 if (rel_size)
8974 {
8975 relocation_type rel_type;
8976 uint64_t num_rela;
8977
8978 if (filedata->is_separate)
8979 printf (_("\nIn linked file '%s' relocation section "),
8980 filedata->file_name);
8981 else
8982 printf (_("\nRelocation section "));
8983
8984 if (filedata->string_table == NULL)
8985 printf ("%d", section->sh_name);
8986 else
8987 printf ("'%s'", printable_section_name (filedata, section));
8988
8989 num_rela = rel_size / section->sh_entsize;
8990 printf (ngettext (" at offset %#" PRIx64
8991 " contains %" PRIu64 " entry:\n",
8992 " at offset %#" PRIx64
8993 " contains %" PRId64 " entries:\n",
8994 num_rela),
8995 rel_offset, num_rela);
8996
8997 rel_type = section->sh_type == SHT_RELA ? reltype_rela :
8998 section->sh_type == SHT_REL ? reltype_rel : reltype_relr;
8999
9000 if (section->sh_link != 0
9001 && section->sh_link < filedata->file_header.e_shnum)
9002 {
9003 Elf_Internal_Shdr *symsec;
9004 Elf_Internal_Sym *symtab;
9005 uint64_t nsyms;
9006 uint64_t strtablen = 0;
9007 char *strtab = NULL;
9008
9009 symsec = filedata->section_headers + section->sh_link;
9010 if (symsec->sh_type != SHT_SYMTAB
9011 && symsec->sh_type != SHT_DYNSYM)
9012 continue;
9013
9014 if (!get_symtab (filedata, symsec,
9015 &symtab, &nsyms, &strtab, &strtablen))
9016 continue;
9017
9018 dump_relocations (filedata, rel_offset, rel_size,
9019 symtab, nsyms, strtab, strtablen,
9020 rel_type,
9021 symsec->sh_type == SHT_DYNSYM);
9022 free (strtab);
9023 free (symtab);
9024 }
9025 else
9026 dump_relocations (filedata, rel_offset, rel_size,
9027 NULL, 0, NULL, 0, rel_type, false /* is_dynamic */);
9028
9029 found = true;
9030 }
9031 }
9032
9033 if (! found)
9034 {
9035 /* Users sometimes forget the -D option, so try to be helpful. */
9036 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
9037 {
9038 if (filedata->dynamic_info[dynamic_relocations [i].size])
9039 {
9040 if (filedata->is_separate)
9041 printf (_("\nThere are no static relocations in linked file '%s'."),
9042 filedata->file_name);
9043 else
9044 printf (_("\nThere are no static relocations in this file."));
9045 printf (_("\nTo see the dynamic relocations add --use-dynamic to the command line.\n"));
9046
9047 break;
9048 }
9049 }
9050 if (i == ARRAY_SIZE (dynamic_relocations))
9051 {
9052 if (filedata->is_separate)
9053 printf (_("\nThere are no relocations in linked file '%s'.\n"),
9054 filedata->file_name);
9055 else
9056 printf (_("\nThere are no relocations in this file.\n"));
9057 }
9058 }
9059 }
9060
9061 return true;
9062 }
9063
9064 /* An absolute address consists of a section and an offset. If the
9065 section is NULL, the offset itself is the address, otherwise, the
9066 address equals to LOAD_ADDRESS(section) + offset. */
9067
9068 struct absaddr
9069 {
9070 unsigned short section;
9071 uint64_t offset;
9072 };
9073
9074 /* Find the nearest symbol at or below ADDR. Returns the symbol
9075 name, if found, and the offset from the symbol to ADDR. */
9076
9077 static void
9078 find_symbol_for_address (Filedata *filedata,
9079 Elf_Internal_Sym *symtab,
9080 uint64_t nsyms,
9081 const char *strtab,
9082 uint64_t strtab_size,
9083 struct absaddr addr,
9084 const char **symname,
9085 uint64_t *offset)
9086 {
9087 uint64_t dist = 0x100000;
9088 Elf_Internal_Sym * sym;
9089 Elf_Internal_Sym * beg;
9090 Elf_Internal_Sym * end;
9091 Elf_Internal_Sym * best = NULL;
9092
9093 REMOVE_ARCH_BITS (addr.offset);
9094 beg = symtab;
9095 end = symtab + nsyms;
9096
9097 while (beg < end)
9098 {
9099 uint64_t value;
9100
9101 sym = beg + (end - beg) / 2;
9102
9103 value = sym->st_value;
9104 REMOVE_ARCH_BITS (value);
9105
9106 if (sym->st_name != 0
9107 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
9108 && addr.offset >= value
9109 && addr.offset - value < dist)
9110 {
9111 best = sym;
9112 dist = addr.offset - value;
9113 if (!dist)
9114 break;
9115 }
9116
9117 if (addr.offset < value)
9118 end = sym;
9119 else
9120 beg = sym + 1;
9121 }
9122
9123 if (best)
9124 {
9125 *symname = (best->st_name >= strtab_size
9126 ? _("<corrupt>") : strtab + best->st_name);
9127 *offset = dist;
9128 return;
9129 }
9130
9131 *symname = NULL;
9132 *offset = addr.offset;
9133 }
9134
9135 static /* signed */ int
9136 symcmp (const void *p, const void *q)
9137 {
9138 Elf_Internal_Sym *sp = (Elf_Internal_Sym *) p;
9139 Elf_Internal_Sym *sq = (Elf_Internal_Sym *) q;
9140
9141 return sp->st_value > sq->st_value ? 1 : (sp->st_value < sq->st_value ? -1 : 0);
9142 }
9143
9144 /* Process the unwind section. */
9145
9146 #include "unwind-ia64.h"
9147
9148 struct ia64_unw_table_entry
9149 {
9150 struct absaddr start;
9151 struct absaddr end;
9152 struct absaddr info;
9153 };
9154
9155 struct ia64_unw_aux_info
9156 {
9157 struct ia64_unw_table_entry * table; /* Unwind table. */
9158 uint64_t table_len; /* Length of unwind table. */
9159 unsigned char * info; /* Unwind info. */
9160 uint64_t info_size; /* Size of unwind info. */
9161 uint64_t info_addr; /* Starting address of unwind info. */
9162 uint64_t seg_base; /* Starting address of segment. */
9163 Elf_Internal_Sym * symtab; /* The symbol table. */
9164 uint64_t nsyms; /* Number of symbols. */
9165 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9166 uint64_t nfuns; /* Number of entries in funtab. */
9167 char * strtab; /* The string table. */
9168 uint64_t strtab_size; /* Size of string table. */
9169 };
9170
9171 static bool
9172 dump_ia64_unwind (Filedata * filedata, struct ia64_unw_aux_info * aux)
9173 {
9174 struct ia64_unw_table_entry * tp;
9175 size_t j, nfuns;
9176 int in_body;
9177 bool res = true;
9178
9179 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9180 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9181 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9182 aux->funtab[nfuns++] = aux->symtab[j];
9183 aux->nfuns = nfuns;
9184 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9185
9186 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
9187 {
9188 uint64_t stamp;
9189 uint64_t offset;
9190 const unsigned char * dp;
9191 const unsigned char * head;
9192 const unsigned char * end;
9193 const char * procname;
9194
9195 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9196 aux->strtab_size, tp->start, &procname, &offset);
9197
9198 fputs ("\n<", stdout);
9199
9200 if (procname)
9201 {
9202 fputs (procname, stdout);
9203
9204 if (offset)
9205 printf ("+%" PRIx64, offset);
9206 }
9207
9208 fputs (">: [", stdout);
9209 print_vma (tp->start.offset, PREFIX_HEX);
9210 fputc ('-', stdout);
9211 print_vma (tp->end.offset, PREFIX_HEX);
9212 printf ("], info at +0x%" PRIx64 "\n",
9213 tp->info.offset - aux->seg_base);
9214
9215 /* PR 17531: file: 86232b32. */
9216 if (aux->info == NULL)
9217 continue;
9218
9219 offset = tp->info.offset;
9220 if (tp->info.section)
9221 {
9222 if (tp->info.section >= filedata->file_header.e_shnum)
9223 {
9224 warn (_("Invalid section %u in table entry %td\n"),
9225 tp->info.section, tp - aux->table);
9226 res = false;
9227 continue;
9228 }
9229 offset += filedata->section_headers[tp->info.section].sh_addr;
9230 }
9231 offset -= aux->info_addr;
9232 /* PR 17531: file: 0997b4d1. */
9233 if (offset >= aux->info_size
9234 || aux->info_size - offset < 8)
9235 {
9236 warn (_("Invalid offset %" PRIx64 " in table entry %td\n"),
9237 tp->info.offset, tp - aux->table);
9238 res = false;
9239 continue;
9240 }
9241
9242 head = aux->info + offset;
9243 stamp = byte_get ((unsigned char *) head, sizeof (stamp));
9244
9245 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
9246 (unsigned) UNW_VER (stamp),
9247 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
9248 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
9249 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
9250 (unsigned long) (eh_addr_size * UNW_LENGTH (stamp)));
9251
9252 if (UNW_VER (stamp) != 1)
9253 {
9254 printf (_("\tUnknown version.\n"));
9255 continue;
9256 }
9257
9258 in_body = 0;
9259 end = head + 8 + eh_addr_size * UNW_LENGTH (stamp);
9260 /* PR 17531: file: 16ceda89. */
9261 if (end > aux->info + aux->info_size)
9262 end = aux->info + aux->info_size;
9263 for (dp = head + 8; dp < end;)
9264 dp = unw_decode (dp, in_body, & in_body, end);
9265 }
9266
9267 free (aux->funtab);
9268
9269 return res;
9270 }
9271
9272 static bool
9273 slurp_ia64_unwind_table (Filedata * filedata,
9274 struct ia64_unw_aux_info * aux,
9275 Elf_Internal_Shdr * sec)
9276 {
9277 uint64_t size, nrelas, i;
9278 Elf_Internal_Phdr * seg;
9279 struct ia64_unw_table_entry * tep;
9280 Elf_Internal_Shdr * relsec;
9281 Elf_Internal_Rela * rela;
9282 Elf_Internal_Rela * rp;
9283 unsigned char * table;
9284 unsigned char * tp;
9285 Elf_Internal_Sym * sym;
9286 const char * relname;
9287
9288 aux->table_len = 0;
9289
9290 /* First, find the starting address of the segment that includes
9291 this section: */
9292
9293 if (filedata->file_header.e_phnum)
9294 {
9295 if (! get_program_headers (filedata))
9296 return false;
9297
9298 for (seg = filedata->program_headers;
9299 seg < filedata->program_headers + filedata->file_header.e_phnum;
9300 ++seg)
9301 {
9302 if (seg->p_type != PT_LOAD)
9303 continue;
9304
9305 if (sec->sh_addr >= seg->p_vaddr
9306 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
9307 {
9308 aux->seg_base = seg->p_vaddr;
9309 break;
9310 }
9311 }
9312 }
9313
9314 /* Second, build the unwind table from the contents of the unwind section: */
9315 size = sec->sh_size;
9316 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
9317 _("unwind table"));
9318 if (!table)
9319 return false;
9320
9321 aux->table_len = size / (3 * eh_addr_size);
9322 aux->table = (struct ia64_unw_table_entry *)
9323 xcmalloc (aux->table_len, sizeof (aux->table[0]));
9324 tep = aux->table;
9325
9326 for (tp = table; tp <= table + size - (3 * eh_addr_size); ++tep)
9327 {
9328 tep->start.section = SHN_UNDEF;
9329 tep->end.section = SHN_UNDEF;
9330 tep->info.section = SHN_UNDEF;
9331 tep->start.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
9332 tep->end.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
9333 tep->info.offset = byte_get (tp, eh_addr_size); tp += eh_addr_size;
9334 tep->start.offset += aux->seg_base;
9335 tep->end.offset += aux->seg_base;
9336 tep->info.offset += aux->seg_base;
9337 }
9338 free (table);
9339
9340 /* Third, apply any relocations to the unwind table: */
9341 for (relsec = filedata->section_headers;
9342 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9343 ++relsec)
9344 {
9345 if (relsec->sh_type != SHT_RELA
9346 || relsec->sh_info >= filedata->file_header.e_shnum
9347 || filedata->section_headers + relsec->sh_info != sec)
9348 continue;
9349
9350 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
9351 & rela, & nrelas))
9352 {
9353 free (aux->table);
9354 aux->table = NULL;
9355 aux->table_len = 0;
9356 return false;
9357 }
9358
9359 for (rp = rela; rp < rela + nrelas; ++rp)
9360 {
9361 unsigned int sym_ndx;
9362 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
9363 relname = elf_ia64_reloc_type (r_type);
9364
9365 /* PR 17531: file: 9fa67536. */
9366 if (relname == NULL)
9367 {
9368 warn (_("Skipping unknown relocation type: %u\n"), r_type);
9369 continue;
9370 }
9371
9372 if (! startswith (relname, "R_IA64_SEGREL"))
9373 {
9374 warn (_("Skipping unexpected relocation type: %s\n"), relname);
9375 continue;
9376 }
9377
9378 i = rp->r_offset / (3 * eh_addr_size);
9379
9380 /* PR 17531: file: 5bc8d9bf. */
9381 if (i >= aux->table_len)
9382 {
9383 warn (_("Skipping reloc with overlarge offset: %#" PRIx64 "\n"),
9384 i);
9385 continue;
9386 }
9387
9388 sym_ndx = get_reloc_symindex (rp->r_info);
9389 if (sym_ndx >= aux->nsyms)
9390 {
9391 warn (_("Skipping reloc with invalid symbol index: %u\n"),
9392 sym_ndx);
9393 continue;
9394 }
9395 sym = aux->symtab + sym_ndx;
9396
9397 switch (rp->r_offset / eh_addr_size % 3)
9398 {
9399 case 0:
9400 aux->table[i].start.section = sym->st_shndx;
9401 aux->table[i].start.offset = rp->r_addend + sym->st_value;
9402 break;
9403 case 1:
9404 aux->table[i].end.section = sym->st_shndx;
9405 aux->table[i].end.offset = rp->r_addend + sym->st_value;
9406 break;
9407 case 2:
9408 aux->table[i].info.section = sym->st_shndx;
9409 aux->table[i].info.offset = rp->r_addend + sym->st_value;
9410 break;
9411 default:
9412 break;
9413 }
9414 }
9415
9416 free (rela);
9417 }
9418
9419 return true;
9420 }
9421
9422 static bool
9423 ia64_process_unwind (Filedata * filedata)
9424 {
9425 Elf_Internal_Shdr * sec;
9426 Elf_Internal_Shdr * unwsec = NULL;
9427 uint64_t i, unwcount = 0, unwstart = 0;
9428 struct ia64_unw_aux_info aux;
9429 bool res = true;
9430
9431 memset (& aux, 0, sizeof (aux));
9432
9433 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9434 {
9435 if (sec->sh_type == SHT_SYMTAB)
9436 {
9437 if (aux.symtab)
9438 {
9439 error (_("Multiple symbol tables encountered\n"));
9440 free (aux.symtab);
9441 aux.symtab = NULL;
9442 free (aux.strtab);
9443 aux.strtab = NULL;
9444 }
9445 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9446 &aux.strtab, &aux.strtab_size))
9447 return false;
9448 }
9449 else if (sec->sh_type == SHT_IA_64_UNWIND)
9450 unwcount++;
9451 }
9452
9453 if (!unwcount)
9454 printf (_("\nThere are no unwind sections in this file.\n"));
9455
9456 while (unwcount-- > 0)
9457 {
9458 const char *suffix;
9459 size_t len, len2;
9460
9461 for (i = unwstart, sec = filedata->section_headers + unwstart, unwsec = NULL;
9462 i < filedata->file_header.e_shnum; ++i, ++sec)
9463 if (sec->sh_type == SHT_IA_64_UNWIND)
9464 {
9465 unwsec = sec;
9466 break;
9467 }
9468 /* We have already counted the number of SHT_IA64_UNWIND
9469 sections so the loop above should never fail. */
9470 assert (unwsec != NULL);
9471
9472 unwstart = i + 1;
9473 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
9474
9475 if ((unwsec->sh_flags & SHF_GROUP) != 0)
9476 {
9477 /* We need to find which section group it is in. */
9478 struct group_list * g;
9479
9480 if (filedata->section_headers_groups == NULL
9481 || filedata->section_headers_groups[i] == NULL)
9482 i = filedata->file_header.e_shnum;
9483 else
9484 {
9485 g = filedata->section_headers_groups[i]->root;
9486
9487 for (; g != NULL; g = g->next)
9488 {
9489 sec = filedata->section_headers + g->section_index;
9490
9491 if (section_name_valid (filedata, sec)
9492 && streq (section_name (filedata, sec),
9493 ELF_STRING_ia64_unwind_info))
9494 break;
9495 }
9496
9497 if (g == NULL)
9498 i = filedata->file_header.e_shnum;
9499 }
9500 }
9501 else if (section_name_valid (filedata, unwsec)
9502 && startswith (section_name (filedata, unwsec),
9503 ELF_STRING_ia64_unwind_once))
9504 {
9505 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO. */
9506 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
9507 suffix = section_name (filedata, unwsec) + len;
9508 for (i = 0, sec = filedata->section_headers;
9509 i < filedata->file_header.e_shnum;
9510 ++i, ++sec)
9511 if (section_name_valid (filedata, sec)
9512 && startswith (section_name (filedata, sec),
9513 ELF_STRING_ia64_unwind_info_once)
9514 && streq (section_name (filedata, sec) + len2, suffix))
9515 break;
9516 }
9517 else
9518 {
9519 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
9520 .IA_64.unwind or BAR -> .IA_64.unwind_info. */
9521 len = sizeof (ELF_STRING_ia64_unwind) - 1;
9522 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
9523 suffix = "";
9524 if (section_name_valid (filedata, unwsec)
9525 && startswith (section_name (filedata, unwsec),
9526 ELF_STRING_ia64_unwind))
9527 suffix = section_name (filedata, unwsec) + len;
9528 for (i = 0, sec = filedata->section_headers;
9529 i < filedata->file_header.e_shnum;
9530 ++i, ++sec)
9531 if (section_name_valid (filedata, sec)
9532 && startswith (section_name (filedata, sec),
9533 ELF_STRING_ia64_unwind_info)
9534 && streq (section_name (filedata, sec) + len2, suffix))
9535 break;
9536 }
9537
9538 if (i == filedata->file_header.e_shnum)
9539 {
9540 printf (_("\nCould not find unwind info section for "));
9541
9542 if (filedata->string_table == NULL)
9543 printf ("%d", unwsec->sh_name);
9544 else
9545 printf ("'%s'", printable_section_name (filedata, unwsec));
9546 }
9547 else
9548 {
9549 aux.info_addr = sec->sh_addr;
9550 aux.info = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1,
9551 sec->sh_size,
9552 _("unwind info"));
9553 aux.info_size = aux.info == NULL ? 0 : sec->sh_size;
9554
9555 printf (_("\nUnwind section "));
9556
9557 if (filedata->string_table == NULL)
9558 printf ("%d", unwsec->sh_name);
9559 else
9560 printf ("'%s'", printable_section_name (filedata, unwsec));
9561
9562 printf (_(" at offset %#" PRIx64 " contains %" PRIu64 " entries:\n"),
9563 unwsec->sh_offset,
9564 unwsec->sh_size / (3 * eh_addr_size));
9565
9566 if (slurp_ia64_unwind_table (filedata, & aux, unwsec)
9567 && aux.table_len > 0)
9568 dump_ia64_unwind (filedata, & aux);
9569
9570 free ((char *) aux.table);
9571 free ((char *) aux.info);
9572 aux.table = NULL;
9573 aux.info = NULL;
9574 }
9575 }
9576
9577 free (aux.symtab);
9578 free ((char *) aux.strtab);
9579
9580 return res;
9581 }
9582
9583 struct hppa_unw_table_entry
9584 {
9585 struct absaddr start;
9586 struct absaddr end;
9587 unsigned int Cannot_unwind:1; /* 0 */
9588 unsigned int Millicode:1; /* 1 */
9589 unsigned int Millicode_save_sr0:1; /* 2 */
9590 unsigned int Region_description:2; /* 3..4 */
9591 unsigned int reserved1:1; /* 5 */
9592 unsigned int Entry_SR:1; /* 6 */
9593 unsigned int Entry_FR:4; /* Number saved 7..10 */
9594 unsigned int Entry_GR:5; /* Number saved 11..15 */
9595 unsigned int Args_stored:1; /* 16 */
9596 unsigned int Variable_Frame:1; /* 17 */
9597 unsigned int Separate_Package_Body:1; /* 18 */
9598 unsigned int Frame_Extension_Millicode:1; /* 19 */
9599 unsigned int Stack_Overflow_Check:1; /* 20 */
9600 unsigned int Two_Instruction_SP_Increment:1; /* 21 */
9601 unsigned int Ada_Region:1; /* 22 */
9602 unsigned int cxx_info:1; /* 23 */
9603 unsigned int cxx_try_catch:1; /* 24 */
9604 unsigned int sched_entry_seq:1; /* 25 */
9605 unsigned int reserved2:1; /* 26 */
9606 unsigned int Save_SP:1; /* 27 */
9607 unsigned int Save_RP:1; /* 28 */
9608 unsigned int Save_MRP_in_frame:1; /* 29 */
9609 unsigned int extn_ptr_defined:1; /* 30 */
9610 unsigned int Cleanup_defined:1; /* 31 */
9611
9612 unsigned int MPE_XL_interrupt_marker:1; /* 0 */
9613 unsigned int HP_UX_interrupt_marker:1; /* 1 */
9614 unsigned int Large_frame:1; /* 2 */
9615 unsigned int Pseudo_SP_Set:1; /* 3 */
9616 unsigned int reserved4:1; /* 4 */
9617 unsigned int Total_frame_size:27; /* 5..31 */
9618 };
9619
9620 struct hppa_unw_aux_info
9621 {
9622 struct hppa_unw_table_entry * table; /* Unwind table. */
9623 uint64_t table_len; /* Length of unwind table. */
9624 uint64_t seg_base; /* Starting address of segment. */
9625 Elf_Internal_Sym * symtab; /* The symbol table. */
9626 uint64_t nsyms; /* Number of symbols. */
9627 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9628 uint64_t nfuns; /* Number of entries in funtab. */
9629 char * strtab; /* The string table. */
9630 uint64_t strtab_size; /* Size of string table. */
9631 };
9632
9633 static bool
9634 dump_hppa_unwind (Filedata * filedata, struct hppa_unw_aux_info * aux)
9635 {
9636 struct hppa_unw_table_entry * tp;
9637 uint64_t j, nfuns;
9638 bool res = true;
9639
9640 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
9641 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
9642 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
9643 aux->funtab[nfuns++] = aux->symtab[j];
9644 aux->nfuns = nfuns;
9645 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
9646
9647 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
9648 {
9649 uint64_t offset;
9650 const char * procname;
9651
9652 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9653 aux->strtab_size, tp->start, &procname,
9654 &offset);
9655
9656 fputs ("\n<", stdout);
9657
9658 if (procname)
9659 {
9660 fputs (procname, stdout);
9661
9662 if (offset)
9663 printf ("+%" PRIx64, offset);
9664 }
9665
9666 fputs (">: [", stdout);
9667 print_vma (tp->start.offset, PREFIX_HEX);
9668 fputc ('-', stdout);
9669 print_vma (tp->end.offset, PREFIX_HEX);
9670 printf ("]\n\t");
9671
9672 #define PF(_m) if (tp->_m) printf (#_m " ");
9673 #define PV(_m) if (tp->_m) printf (#_m "=%d ", tp->_m);
9674 PF(Cannot_unwind);
9675 PF(Millicode);
9676 PF(Millicode_save_sr0);
9677 /* PV(Region_description); */
9678 PF(Entry_SR);
9679 PV(Entry_FR);
9680 PV(Entry_GR);
9681 PF(Args_stored);
9682 PF(Variable_Frame);
9683 PF(Separate_Package_Body);
9684 PF(Frame_Extension_Millicode);
9685 PF(Stack_Overflow_Check);
9686 PF(Two_Instruction_SP_Increment);
9687 PF(Ada_Region);
9688 PF(cxx_info);
9689 PF(cxx_try_catch);
9690 PF(sched_entry_seq);
9691 PF(Save_SP);
9692 PF(Save_RP);
9693 PF(Save_MRP_in_frame);
9694 PF(extn_ptr_defined);
9695 PF(Cleanup_defined);
9696 PF(MPE_XL_interrupt_marker);
9697 PF(HP_UX_interrupt_marker);
9698 PF(Large_frame);
9699 PF(Pseudo_SP_Set);
9700 PV(Total_frame_size);
9701 #undef PF
9702 #undef PV
9703 }
9704
9705 printf ("\n");
9706
9707 free (aux->funtab);
9708
9709 return res;
9710 }
9711
9712 static bool
9713 slurp_hppa_unwind_table (Filedata * filedata,
9714 struct hppa_unw_aux_info * aux,
9715 Elf_Internal_Shdr * sec)
9716 {
9717 uint64_t size, unw_ent_size, nentries, nrelas, i;
9718 Elf_Internal_Phdr * seg;
9719 struct hppa_unw_table_entry * tep;
9720 Elf_Internal_Shdr * relsec;
9721 Elf_Internal_Rela * rela;
9722 Elf_Internal_Rela * rp;
9723 unsigned char * table;
9724 unsigned char * tp;
9725 Elf_Internal_Sym * sym;
9726 const char * relname;
9727
9728 /* First, find the starting address of the segment that includes
9729 this section. */
9730 if (filedata->file_header.e_phnum)
9731 {
9732 if (! get_program_headers (filedata))
9733 return false;
9734
9735 for (seg = filedata->program_headers;
9736 seg < filedata->program_headers + filedata->file_header.e_phnum;
9737 ++seg)
9738 {
9739 if (seg->p_type != PT_LOAD)
9740 continue;
9741
9742 if (sec->sh_addr >= seg->p_vaddr
9743 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
9744 {
9745 aux->seg_base = seg->p_vaddr;
9746 break;
9747 }
9748 }
9749 }
9750
9751 /* Second, build the unwind table from the contents of the unwind
9752 section. */
9753 size = sec->sh_size;
9754 table = (unsigned char *) get_data (NULL, filedata, sec->sh_offset, 1, size,
9755 _("unwind table"));
9756 if (!table)
9757 return false;
9758
9759 unw_ent_size = 16;
9760 nentries = size / unw_ent_size;
9761 size = unw_ent_size * nentries;
9762
9763 aux->table_len = nentries;
9764 tep = aux->table = (struct hppa_unw_table_entry *)
9765 xcmalloc (nentries, sizeof (aux->table[0]));
9766
9767 for (tp = table; tp < table + size; tp += unw_ent_size, ++tep)
9768 {
9769 unsigned int tmp1, tmp2;
9770
9771 tep->start.section = SHN_UNDEF;
9772 tep->end.section = SHN_UNDEF;
9773
9774 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
9775 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
9776 tmp1 = byte_get ((unsigned char *) tp + 8, 4);
9777 tmp2 = byte_get ((unsigned char *) tp + 12, 4);
9778
9779 tep->start.offset += aux->seg_base;
9780 tep->end.offset += aux->seg_base;
9781
9782 tep->Cannot_unwind = (tmp1 >> 31) & 0x1;
9783 tep->Millicode = (tmp1 >> 30) & 0x1;
9784 tep->Millicode_save_sr0 = (tmp1 >> 29) & 0x1;
9785 tep->Region_description = (tmp1 >> 27) & 0x3;
9786 tep->reserved1 = (tmp1 >> 26) & 0x1;
9787 tep->Entry_SR = (tmp1 >> 25) & 0x1;
9788 tep->Entry_FR = (tmp1 >> 21) & 0xf;
9789 tep->Entry_GR = (tmp1 >> 16) & 0x1f;
9790 tep->Args_stored = (tmp1 >> 15) & 0x1;
9791 tep->Variable_Frame = (tmp1 >> 14) & 0x1;
9792 tep->Separate_Package_Body = (tmp1 >> 13) & 0x1;
9793 tep->Frame_Extension_Millicode = (tmp1 >> 12) & 0x1;
9794 tep->Stack_Overflow_Check = (tmp1 >> 11) & 0x1;
9795 tep->Two_Instruction_SP_Increment = (tmp1 >> 10) & 0x1;
9796 tep->Ada_Region = (tmp1 >> 9) & 0x1;
9797 tep->cxx_info = (tmp1 >> 8) & 0x1;
9798 tep->cxx_try_catch = (tmp1 >> 7) & 0x1;
9799 tep->sched_entry_seq = (tmp1 >> 6) & 0x1;
9800 tep->reserved2 = (tmp1 >> 5) & 0x1;
9801 tep->Save_SP = (tmp1 >> 4) & 0x1;
9802 tep->Save_RP = (tmp1 >> 3) & 0x1;
9803 tep->Save_MRP_in_frame = (tmp1 >> 2) & 0x1;
9804 tep->extn_ptr_defined = (tmp1 >> 1) & 0x1;
9805 tep->Cleanup_defined = tmp1 & 0x1;
9806
9807 tep->MPE_XL_interrupt_marker = (tmp2 >> 31) & 0x1;
9808 tep->HP_UX_interrupt_marker = (tmp2 >> 30) & 0x1;
9809 tep->Large_frame = (tmp2 >> 29) & 0x1;
9810 tep->Pseudo_SP_Set = (tmp2 >> 28) & 0x1;
9811 tep->reserved4 = (tmp2 >> 27) & 0x1;
9812 tep->Total_frame_size = tmp2 & 0x7ffffff;
9813 }
9814 free (table);
9815
9816 /* Third, apply any relocations to the unwind table. */
9817 for (relsec = filedata->section_headers;
9818 relsec < filedata->section_headers + filedata->file_header.e_shnum;
9819 ++relsec)
9820 {
9821 if (relsec->sh_type != SHT_RELA
9822 || relsec->sh_info >= filedata->file_header.e_shnum
9823 || filedata->section_headers + relsec->sh_info != sec)
9824 continue;
9825
9826 if (!slurp_rela_relocs (filedata, relsec->sh_offset, relsec->sh_size,
9827 & rela, & nrelas))
9828 return false;
9829
9830 for (rp = rela; rp < rela + nrelas; ++rp)
9831 {
9832 unsigned int sym_ndx;
9833 unsigned int r_type = get_reloc_type (filedata, rp->r_info);
9834 relname = elf_hppa_reloc_type (r_type);
9835
9836 if (relname == NULL)
9837 {
9838 warn (_("Skipping unknown relocation type: %u\n"), r_type);
9839 continue;
9840 }
9841
9842 /* R_PARISC_SEGREL32 or R_PARISC_SEGREL64. */
9843 if (! startswith (relname, "R_PARISC_SEGREL"))
9844 {
9845 warn (_("Skipping unexpected relocation type: %s\n"), relname);
9846 continue;
9847 }
9848
9849 i = rp->r_offset / unw_ent_size;
9850 if (i >= aux->table_len)
9851 {
9852 warn (_("Skipping reloc with overlarge offset: %#" PRIx64 "\n"),
9853 i);
9854 continue;
9855 }
9856
9857 sym_ndx = get_reloc_symindex (rp->r_info);
9858 if (sym_ndx >= aux->nsyms)
9859 {
9860 warn (_("Skipping reloc with invalid symbol index: %u\n"),
9861 sym_ndx);
9862 continue;
9863 }
9864 sym = aux->symtab + sym_ndx;
9865
9866 switch ((rp->r_offset % unw_ent_size) / 4)
9867 {
9868 case 0:
9869 aux->table[i].start.section = sym->st_shndx;
9870 aux->table[i].start.offset = sym->st_value + rp->r_addend;
9871 break;
9872 case 1:
9873 aux->table[i].end.section = sym->st_shndx;
9874 aux->table[i].end.offset = sym->st_value + rp->r_addend;
9875 break;
9876 default:
9877 break;
9878 }
9879 }
9880
9881 free (rela);
9882 }
9883
9884 return true;
9885 }
9886
9887 static bool
9888 hppa_process_unwind (Filedata * filedata)
9889 {
9890 struct hppa_unw_aux_info aux;
9891 Elf_Internal_Shdr * unwsec = NULL;
9892 Elf_Internal_Shdr * sec;
9893 size_t i;
9894 bool res = true;
9895
9896 if (filedata->string_table == NULL)
9897 return false;
9898
9899 memset (& aux, 0, sizeof (aux));
9900
9901 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9902 {
9903 if (sec->sh_type == SHT_SYMTAB)
9904 {
9905 if (aux.symtab)
9906 {
9907 error (_("Multiple symbol tables encountered\n"));
9908 free (aux.symtab);
9909 aux.symtab = NULL;
9910 free (aux.strtab);
9911 aux.strtab = NULL;
9912 }
9913 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
9914 &aux.strtab, &aux.strtab_size))
9915 return false;
9916 }
9917 else if (section_name_valid (filedata, sec)
9918 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9919 unwsec = sec;
9920 }
9921
9922 if (!unwsec)
9923 printf (_("\nThere are no unwind sections in this file.\n"));
9924
9925 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
9926 {
9927 if (section_name_valid (filedata, sec)
9928 && streq (section_name (filedata, sec), ".PARISC.unwind"))
9929 {
9930 uint64_t num_unwind = sec->sh_size / 16;
9931
9932 printf (ngettext ("\nUnwind section '%s' at offset %#" PRIx64 " "
9933 "contains %" PRIu64 " entry:\n",
9934 "\nUnwind section '%s' at offset %#" PRIx64 " "
9935 "contains %" PRIu64 " entries:\n",
9936 num_unwind),
9937 printable_section_name (filedata, sec),
9938 sec->sh_offset,
9939 num_unwind);
9940
9941 if (! slurp_hppa_unwind_table (filedata, &aux, sec))
9942 res = false;
9943
9944 if (res && aux.table_len > 0)
9945 {
9946 if (! dump_hppa_unwind (filedata, &aux))
9947 res = false;
9948 }
9949
9950 free ((char *) aux.table);
9951 aux.table = NULL;
9952 }
9953 }
9954
9955 free (aux.symtab);
9956 free ((char *) aux.strtab);
9957
9958 return res;
9959 }
9960
9961 struct arm_section
9962 {
9963 unsigned char * data; /* The unwind data. */
9964 Elf_Internal_Shdr * sec; /* The cached unwind section header. */
9965 Elf_Internal_Rela * rela; /* The cached relocations for this section. */
9966 uint64_t nrelas; /* The number of relocations. */
9967 unsigned int rel_type; /* REL or RELA ? */
9968 Elf_Internal_Rela * next_rela; /* Cyclic pointer to the next reloc to process. */
9969 };
9970
9971 struct arm_unw_aux_info
9972 {
9973 Filedata * filedata; /* The file containing the unwind sections. */
9974 Elf_Internal_Sym * symtab; /* The file's symbol table. */
9975 uint64_t nsyms; /* Number of symbols. */
9976 Elf_Internal_Sym * funtab; /* Sorted table of STT_FUNC symbols. */
9977 uint64_t nfuns; /* Number of these symbols. */
9978 char * strtab; /* The file's string table. */
9979 uint64_t strtab_size; /* Size of string table. */
9980 };
9981
9982 static const char *
9983 arm_print_vma_and_name (Filedata * filedata,
9984 struct arm_unw_aux_info * aux,
9985 uint64_t fn,
9986 struct absaddr addr)
9987 {
9988 const char *procname;
9989 uint64_t sym_offset;
9990
9991 if (addr.section == SHN_UNDEF)
9992 addr.offset = fn;
9993
9994 find_symbol_for_address (filedata, aux->funtab, aux->nfuns, aux->strtab,
9995 aux->strtab_size, addr, &procname,
9996 &sym_offset);
9997
9998 print_vma (fn, PREFIX_HEX);
9999
10000 if (procname)
10001 {
10002 fputs (" <", stdout);
10003 fputs (procname, stdout);
10004
10005 if (sym_offset)
10006 printf ("+0x%" PRIx64, sym_offset);
10007 fputc ('>', stdout);
10008 }
10009
10010 return procname;
10011 }
10012
10013 static void
10014 arm_free_section (struct arm_section *arm_sec)
10015 {
10016 free (arm_sec->data);
10017 free (arm_sec->rela);
10018 }
10019
10020 /* 1) If SEC does not match the one cached in ARM_SEC, then free the current
10021 cached section and install SEC instead.
10022 2) Locate the 32-bit word at WORD_OFFSET in unwind section SEC
10023 and return its valued in * WORDP, relocating if necessary.
10024 3) Update the NEXT_RELA field in ARM_SEC and store the section index and
10025 relocation's offset in ADDR.
10026 4) If SYM_NAME is non-NULL and a relocation was applied, record the offset
10027 into the string table of the symbol associated with the reloc. If no
10028 reloc was applied store -1 there.
10029 5) Return TRUE upon success, FALSE otherwise. */
10030
10031 static bool
10032 get_unwind_section_word (Filedata * filedata,
10033 struct arm_unw_aux_info * aux,
10034 struct arm_section * arm_sec,
10035 Elf_Internal_Shdr * sec,
10036 uint64_t word_offset,
10037 unsigned int * wordp,
10038 struct absaddr * addr,
10039 uint64_t * sym_name)
10040 {
10041 Elf_Internal_Rela *rp;
10042 Elf_Internal_Sym *sym;
10043 const char * relname;
10044 unsigned int word;
10045 bool wrapped;
10046
10047 if (sec == NULL || arm_sec == NULL)
10048 return false;
10049
10050 addr->section = SHN_UNDEF;
10051 addr->offset = 0;
10052
10053 if (sym_name != NULL)
10054 *sym_name = (uint64_t) -1;
10055
10056 /* If necessary, update the section cache. */
10057 if (sec != arm_sec->sec)
10058 {
10059 Elf_Internal_Shdr *relsec;
10060
10061 arm_free_section (arm_sec);
10062
10063 arm_sec->sec = sec;
10064 arm_sec->data = get_data (NULL, aux->filedata, sec->sh_offset, 1,
10065 sec->sh_size, _("unwind data"));
10066 arm_sec->rela = NULL;
10067 arm_sec->nrelas = 0;
10068
10069 for (relsec = filedata->section_headers;
10070 relsec < filedata->section_headers + filedata->file_header.e_shnum;
10071 ++relsec)
10072 {
10073 if (relsec->sh_info >= filedata->file_header.e_shnum
10074 || filedata->section_headers + relsec->sh_info != sec
10075 /* PR 15745: Check the section type as well. */
10076 || (relsec->sh_type != SHT_REL
10077 && relsec->sh_type != SHT_RELA))
10078 continue;
10079
10080 arm_sec->rel_type = relsec->sh_type;
10081 if (relsec->sh_type == SHT_REL)
10082 {
10083 if (!slurp_rel_relocs (aux->filedata, relsec->sh_offset,
10084 relsec->sh_size,
10085 & arm_sec->rela, & arm_sec->nrelas))
10086 return false;
10087 }
10088 else /* relsec->sh_type == SHT_RELA */
10089 {
10090 if (!slurp_rela_relocs (aux->filedata, relsec->sh_offset,
10091 relsec->sh_size,
10092 & arm_sec->rela, & arm_sec->nrelas))
10093 return false;
10094 }
10095 break;
10096 }
10097
10098 arm_sec->next_rela = arm_sec->rela;
10099 }
10100
10101 /* If there is no unwind data we can do nothing. */
10102 if (arm_sec->data == NULL)
10103 return false;
10104
10105 /* If the offset is invalid then fail. */
10106 if (/* PR 21343 *//* PR 18879 */
10107 sec->sh_size < 4
10108 || word_offset > sec->sh_size - 4)
10109 return false;
10110
10111 /* Get the word at the required offset. */
10112 word = byte_get (arm_sec->data + word_offset, 4);
10113
10114 /* PR 17531: file: id:000001,src:001266+003044,op:splice,rep:128. */
10115 if (arm_sec->rela == NULL)
10116 {
10117 * wordp = word;
10118 return true;
10119 }
10120
10121 /* Look through the relocs to find the one that applies to the provided offset. */
10122 wrapped = false;
10123 for (rp = arm_sec->next_rela; rp != arm_sec->rela + arm_sec->nrelas; rp++)
10124 {
10125 uint64_t prelval, offset;
10126
10127 if (rp->r_offset > word_offset && !wrapped)
10128 {
10129 rp = arm_sec->rela;
10130 wrapped = true;
10131 }
10132 if (rp->r_offset > word_offset)
10133 break;
10134
10135 if (rp->r_offset & 3)
10136 {
10137 warn (_("Skipping unexpected relocation at offset %#" PRIx64 "\n"),
10138 rp->r_offset);
10139 continue;
10140 }
10141
10142 if (rp->r_offset < word_offset)
10143 continue;
10144
10145 /* PR 17531: file: 027-161405-0.004 */
10146 if (aux->symtab == NULL)
10147 continue;
10148
10149 if (arm_sec->rel_type == SHT_REL)
10150 {
10151 offset = word & 0x7fffffff;
10152 if (offset & 0x40000000)
10153 offset |= ~ (uint64_t) 0x7fffffff;
10154 }
10155 else if (arm_sec->rel_type == SHT_RELA)
10156 offset = rp->r_addend;
10157 else
10158 {
10159 error (_("Unknown section relocation type %d encountered\n"),
10160 arm_sec->rel_type);
10161 break;
10162 }
10163
10164 /* PR 17531 file: 027-1241568-0.004. */
10165 if (ELF32_R_SYM (rp->r_info) >= aux->nsyms)
10166 {
10167 error (_("Bad symbol index in unwind relocation "
10168 "(%" PRIu64 " > %" PRIu64 ")\n"),
10169 ELF32_R_SYM (rp->r_info), aux->nsyms);
10170 break;
10171 }
10172
10173 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
10174 offset += sym->st_value;
10175 prelval = offset - (arm_sec->sec->sh_addr + rp->r_offset);
10176
10177 /* Check that we are processing the expected reloc type. */
10178 if (filedata->file_header.e_machine == EM_ARM)
10179 {
10180 relname = elf_arm_reloc_type (ELF32_R_TYPE (rp->r_info));
10181 if (relname == NULL)
10182 {
10183 warn (_("Skipping unknown ARM relocation type: %d\n"),
10184 (int) ELF32_R_TYPE (rp->r_info));
10185 continue;
10186 }
10187
10188 if (streq (relname, "R_ARM_NONE"))
10189 continue;
10190
10191 if (! streq (relname, "R_ARM_PREL31"))
10192 {
10193 warn (_("Skipping unexpected ARM relocation type %s\n"), relname);
10194 continue;
10195 }
10196 }
10197 else if (filedata->file_header.e_machine == EM_TI_C6000)
10198 {
10199 relname = elf_tic6x_reloc_type (ELF32_R_TYPE (rp->r_info));
10200 if (relname == NULL)
10201 {
10202 warn (_("Skipping unknown C6000 relocation type: %d\n"),
10203 (int) ELF32_R_TYPE (rp->r_info));
10204 continue;
10205 }
10206
10207 if (streq (relname, "R_C6000_NONE"))
10208 continue;
10209
10210 if (! streq (relname, "R_C6000_PREL31"))
10211 {
10212 warn (_("Skipping unexpected C6000 relocation type %s\n"), relname);
10213 continue;
10214 }
10215
10216 prelval >>= 1;
10217 }
10218 else
10219 {
10220 /* This function currently only supports ARM and TI unwinders. */
10221 warn (_("Only TI and ARM unwinders are currently supported\n"));
10222 break;
10223 }
10224
10225 word = (word & ~ (uint64_t) 0x7fffffff) | (prelval & 0x7fffffff);
10226 addr->section = sym->st_shndx;
10227 addr->offset = offset;
10228
10229 if (sym_name)
10230 * sym_name = sym->st_name;
10231 break;
10232 }
10233
10234 *wordp = word;
10235 arm_sec->next_rela = rp;
10236
10237 return true;
10238 }
10239
10240 static const char *tic6x_unwind_regnames[16] =
10241 {
10242 "A15", "B15", "B14", "B13", "B12", "B11", "B10", "B3",
10243 "A14", "A13", "A12", "A11", "A10",
10244 "[invalid reg 13]", "[invalid reg 14]", "[invalid reg 15]"
10245 };
10246
10247 static void
10248 decode_tic6x_unwind_regmask (unsigned int mask)
10249 {
10250 int i;
10251
10252 for (i = 12; mask; mask >>= 1, i--)
10253 {
10254 if (mask & 1)
10255 {
10256 fputs (tic6x_unwind_regnames[i], stdout);
10257 if (mask > 1)
10258 fputs (", ", stdout);
10259 }
10260 }
10261 }
10262
10263 #define ADVANCE \
10264 if (remaining == 0 && more_words) \
10265 { \
10266 data_offset += 4; \
10267 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, \
10268 data_offset, & word, & addr, NULL)) \
10269 return false; \
10270 remaining = 4; \
10271 more_words--; \
10272 } \
10273
10274 #define GET_OP(OP) \
10275 ADVANCE; \
10276 if (remaining) \
10277 { \
10278 remaining--; \
10279 (OP) = word >> 24; \
10280 word <<= 8; \
10281 } \
10282 else \
10283 { \
10284 printf (_("[Truncated opcode]\n")); \
10285 return false; \
10286 } \
10287 printf ("0x%02x ", OP)
10288
10289 static bool
10290 decode_arm_unwind_bytecode (Filedata * filedata,
10291 struct arm_unw_aux_info * aux,
10292 unsigned int word,
10293 unsigned int remaining,
10294 unsigned int more_words,
10295 uint64_t data_offset,
10296 Elf_Internal_Shdr * data_sec,
10297 struct arm_section * data_arm_sec)
10298 {
10299 struct absaddr addr;
10300 bool res = true;
10301
10302 /* Decode the unwinding instructions. */
10303 while (1)
10304 {
10305 unsigned int op, op2;
10306
10307 ADVANCE;
10308 if (remaining == 0)
10309 break;
10310 remaining--;
10311 op = word >> 24;
10312 word <<= 8;
10313
10314 printf (" 0x%02x ", op);
10315
10316 if ((op & 0xc0) == 0x00)
10317 {
10318 int offset = ((op & 0x3f) << 2) + 4;
10319
10320 printf (" vsp = vsp + %d", offset);
10321 }
10322 else if ((op & 0xc0) == 0x40)
10323 {
10324 int offset = ((op & 0x3f) << 2) + 4;
10325
10326 printf (" vsp = vsp - %d", offset);
10327 }
10328 else if ((op & 0xf0) == 0x80)
10329 {
10330 GET_OP (op2);
10331 if (op == 0x80 && op2 == 0)
10332 printf (_("Refuse to unwind"));
10333 else
10334 {
10335 unsigned int mask = ((op & 0x0f) << 8) | op2;
10336 bool first = true;
10337 int i;
10338
10339 printf ("pop {");
10340 for (i = 0; i < 12; i++)
10341 if (mask & (1 << i))
10342 {
10343 if (first)
10344 first = false;
10345 else
10346 printf (", ");
10347 printf ("r%d", 4 + i);
10348 }
10349 printf ("}");
10350 }
10351 }
10352 else if ((op & 0xf0) == 0x90)
10353 {
10354 if (op == 0x9d || op == 0x9f)
10355 printf (_(" [Reserved]"));
10356 else
10357 printf (" vsp = r%d", op & 0x0f);
10358 }
10359 else if ((op & 0xf0) == 0xa0)
10360 {
10361 int end = 4 + (op & 0x07);
10362 bool first = true;
10363 int i;
10364
10365 printf (" pop {");
10366 for (i = 4; i <= end; i++)
10367 {
10368 if (first)
10369 first = false;
10370 else
10371 printf (", ");
10372 printf ("r%d", i);
10373 }
10374 if (op & 0x08)
10375 {
10376 if (!first)
10377 printf (", ");
10378 printf ("r14");
10379 }
10380 printf ("}");
10381 }
10382 else if (op == 0xb0)
10383 printf (_(" finish"));
10384 else if (op == 0xb1)
10385 {
10386 GET_OP (op2);
10387 if (op2 == 0 || (op2 & 0xf0) != 0)
10388 printf (_("[Spare]"));
10389 else
10390 {
10391 unsigned int mask = op2 & 0x0f;
10392 bool first = true;
10393 int i;
10394
10395 printf ("pop {");
10396 for (i = 0; i < 12; i++)
10397 if (mask & (1 << i))
10398 {
10399 if (first)
10400 first = false;
10401 else
10402 printf (", ");
10403 printf ("r%d", i);
10404 }
10405 printf ("}");
10406 }
10407 }
10408 else if (op == 0xb2)
10409 {
10410 unsigned char buf[9];
10411 unsigned int i, len;
10412 uint64_t offset;
10413
10414 for (i = 0; i < sizeof (buf); i++)
10415 {
10416 GET_OP (buf[i]);
10417 if ((buf[i] & 0x80) == 0)
10418 break;
10419 }
10420 if (i == sizeof (buf))
10421 {
10422 error (_("corrupt change to vsp\n"));
10423 res = false;
10424 }
10425 else
10426 {
10427 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
10428 assert (len == i + 1);
10429 offset = offset * 4 + 0x204;
10430 printf ("vsp = vsp + %" PRId64, offset);
10431 }
10432 }
10433 else if (op == 0xb3 || op == 0xc8 || op == 0xc9)
10434 {
10435 unsigned int first, last;
10436
10437 GET_OP (op2);
10438 first = op2 >> 4;
10439 last = op2 & 0x0f;
10440 if (op == 0xc8)
10441 first = first + 16;
10442 printf ("pop {D%d", first);
10443 if (last)
10444 printf ("-D%d", first + last);
10445 printf ("}");
10446 }
10447 else if (op == 0xb4)
10448 printf (_(" pop {ra_auth_code}"));
10449 else if (op == 0xb5)
10450 printf (_(" vsp as modifier for PAC validation"));
10451 else if ((op & 0xf8) == 0xb8 || (op & 0xf8) == 0xd0)
10452 {
10453 unsigned int count = op & 0x07;
10454
10455 printf ("pop {D8");
10456 if (count)
10457 printf ("-D%d", 8 + count);
10458 printf ("}");
10459 }
10460 else if (op >= 0xc0 && op <= 0xc5)
10461 {
10462 unsigned int count = op & 0x07;
10463
10464 printf (" pop {wR10");
10465 if (count)
10466 printf ("-wR%d", 10 + count);
10467 printf ("}");
10468 }
10469 else if (op == 0xc6)
10470 {
10471 unsigned int first, last;
10472
10473 GET_OP (op2);
10474 first = op2 >> 4;
10475 last = op2 & 0x0f;
10476 printf ("pop {wR%d", first);
10477 if (last)
10478 printf ("-wR%d", first + last);
10479 printf ("}");
10480 }
10481 else if (op == 0xc7)
10482 {
10483 GET_OP (op2);
10484 if (op2 == 0 || (op2 & 0xf0) != 0)
10485 printf (_("[Spare]"));
10486 else
10487 {
10488 unsigned int mask = op2 & 0x0f;
10489 bool first = true;
10490 int i;
10491
10492 printf ("pop {");
10493 for (i = 0; i < 4; i++)
10494 if (mask & (1 << i))
10495 {
10496 if (first)
10497 first = false;
10498 else
10499 printf (", ");
10500 printf ("wCGR%d", i);
10501 }
10502 printf ("}");
10503 }
10504 }
10505 else
10506 {
10507 printf (_(" [unsupported opcode]"));
10508 res = false;
10509 }
10510
10511 printf ("\n");
10512 }
10513
10514 return res;
10515 }
10516
10517 static bool
10518 decode_tic6x_unwind_bytecode (Filedata * filedata,
10519 struct arm_unw_aux_info * aux,
10520 unsigned int word,
10521 unsigned int remaining,
10522 unsigned int more_words,
10523 uint64_t data_offset,
10524 Elf_Internal_Shdr * data_sec,
10525 struct arm_section * data_arm_sec)
10526 {
10527 struct absaddr addr;
10528
10529 /* Decode the unwinding instructions. */
10530 while (1)
10531 {
10532 unsigned int op, op2;
10533
10534 ADVANCE;
10535 if (remaining == 0)
10536 break;
10537 remaining--;
10538 op = word >> 24;
10539 word <<= 8;
10540
10541 printf (" 0x%02x ", op);
10542
10543 if ((op & 0xc0) == 0x00)
10544 {
10545 int offset = ((op & 0x3f) << 3) + 8;
10546 printf (" sp = sp + %d", offset);
10547 }
10548 else if ((op & 0xc0) == 0x80)
10549 {
10550 GET_OP (op2);
10551 if (op == 0x80 && op2 == 0)
10552 printf (_("Refuse to unwind"));
10553 else
10554 {
10555 unsigned int mask = ((op & 0x1f) << 8) | op2;
10556 if (op & 0x20)
10557 printf ("pop compact {");
10558 else
10559 printf ("pop {");
10560
10561 decode_tic6x_unwind_regmask (mask);
10562 printf("}");
10563 }
10564 }
10565 else if ((op & 0xf0) == 0xc0)
10566 {
10567 unsigned int reg;
10568 unsigned int nregs;
10569 unsigned int i;
10570 const char *name;
10571 struct
10572 {
10573 unsigned int offset;
10574 unsigned int reg;
10575 } regpos[16];
10576
10577 /* Scan entire instruction first so that GET_OP output is not
10578 interleaved with disassembly. */
10579 nregs = 0;
10580 for (i = 0; nregs < (op & 0xf); i++)
10581 {
10582 GET_OP (op2);
10583 reg = op2 >> 4;
10584 if (reg != 0xf)
10585 {
10586 regpos[nregs].offset = i * 2;
10587 regpos[nregs].reg = reg;
10588 nregs++;
10589 }
10590
10591 reg = op2 & 0xf;
10592 if (reg != 0xf)
10593 {
10594 regpos[nregs].offset = i * 2 + 1;
10595 regpos[nregs].reg = reg;
10596 nregs++;
10597 }
10598 }
10599
10600 printf (_("pop frame {"));
10601 if (nregs == 0)
10602 {
10603 printf (_("*corrupt* - no registers specified"));
10604 }
10605 else
10606 {
10607 reg = nregs - 1;
10608 for (i = i * 2; i > 0; i--)
10609 {
10610 if (regpos[reg].offset == i - 1)
10611 {
10612 name = tic6x_unwind_regnames[regpos[reg].reg];
10613 if (reg > 0)
10614 reg--;
10615 }
10616 else
10617 name = _("[pad]");
10618
10619 fputs (name, stdout);
10620 if (i > 1)
10621 printf (", ");
10622 }
10623 }
10624
10625 printf ("}");
10626 }
10627 else if (op == 0xd0)
10628 printf (" MOV FP, SP");
10629 else if (op == 0xd1)
10630 printf (" __c6xabi_pop_rts");
10631 else if (op == 0xd2)
10632 {
10633 unsigned char buf[9];
10634 unsigned int i, len;
10635 uint64_t offset;
10636
10637 for (i = 0; i < sizeof (buf); i++)
10638 {
10639 GET_OP (buf[i]);
10640 if ((buf[i] & 0x80) == 0)
10641 break;
10642 }
10643 /* PR 17531: file: id:000001,src:001906+004739,op:splice,rep:2. */
10644 if (i == sizeof (buf))
10645 {
10646 warn (_("Corrupt stack pointer adjustment detected\n"));
10647 return false;
10648 }
10649
10650 offset = read_leb128 (buf, buf + i + 1, false, &len, NULL);
10651 assert (len == i + 1);
10652 offset = offset * 8 + 0x408;
10653 printf (_("sp = sp + %" PRId64), offset);
10654 }
10655 else if ((op & 0xf0) == 0xe0)
10656 {
10657 if ((op & 0x0f) == 7)
10658 printf (" RETURN");
10659 else
10660 printf (" MV %s, B3", tic6x_unwind_regnames[op & 0x0f]);
10661 }
10662 else
10663 {
10664 printf (_(" [unsupported opcode]"));
10665 }
10666 putchar ('\n');
10667 }
10668
10669 return true;
10670 }
10671
10672 static uint64_t
10673 arm_expand_prel31 (Filedata * filedata, uint64_t word, uint64_t where)
10674 {
10675 uint64_t offset;
10676
10677 offset = word & 0x7fffffff;
10678 if (offset & 0x40000000)
10679 offset |= ~ (uint64_t) 0x7fffffff;
10680
10681 if (filedata->file_header.e_machine == EM_TI_C6000)
10682 offset <<= 1;
10683
10684 return offset + where;
10685 }
10686
10687 static bool
10688 decode_arm_unwind (Filedata * filedata,
10689 struct arm_unw_aux_info * aux,
10690 unsigned int word,
10691 unsigned int remaining,
10692 uint64_t data_offset,
10693 Elf_Internal_Shdr * data_sec,
10694 struct arm_section * data_arm_sec)
10695 {
10696 int per_index;
10697 unsigned int more_words = 0;
10698 struct absaddr addr;
10699 uint64_t sym_name = (uint64_t) -1;
10700 bool res = true;
10701
10702 if (remaining == 0)
10703 {
10704 /* Fetch the first word.
10705 Note - when decoding an object file the address extracted
10706 here will always be 0. So we also pass in the sym_name
10707 parameter so that we can find the symbol associated with
10708 the personality routine. */
10709 if (! get_unwind_section_word (filedata, aux, data_arm_sec, data_sec, data_offset,
10710 & word, & addr, & sym_name))
10711 return false;
10712
10713 remaining = 4;
10714 }
10715 else
10716 {
10717 addr.section = SHN_UNDEF;
10718 addr.offset = 0;
10719 }
10720
10721 if ((word & 0x80000000) == 0)
10722 {
10723 /* Expand prel31 for personality routine. */
10724 uint64_t fn;
10725 const char *procname;
10726
10727 fn = arm_expand_prel31 (filedata, word, data_sec->sh_addr + data_offset);
10728 printf (_(" Personality routine: "));
10729 if (fn == 0
10730 && addr.section == SHN_UNDEF && addr.offset == 0
10731 && sym_name != (uint64_t) -1 && sym_name < aux->strtab_size)
10732 {
10733 procname = aux->strtab + sym_name;
10734 print_vma (fn, PREFIX_HEX);
10735 if (procname)
10736 {
10737 fputs (" <", stdout);
10738 fputs (procname, stdout);
10739 fputc ('>', stdout);
10740 }
10741 }
10742 else
10743 procname = arm_print_vma_and_name (filedata, aux, fn, addr);
10744 fputc ('\n', stdout);
10745
10746 /* The GCC personality routines use the standard compact
10747 encoding, starting with one byte giving the number of
10748 words. */
10749 if (procname != NULL
10750 && (startswith (procname, "__gcc_personality_v0")
10751 || startswith (procname, "__gxx_personality_v0")
10752 || startswith (procname, "__gcj_personality_v0")
10753 || startswith (procname, "__gnu_objc_personality_v0")))
10754 {
10755 remaining = 0;
10756 more_words = 1;
10757 ADVANCE;
10758 if (!remaining)
10759 {
10760 printf (_(" [Truncated data]\n"));
10761 return false;
10762 }
10763 more_words = word >> 24;
10764 word <<= 8;
10765 remaining--;
10766 per_index = -1;
10767 }
10768 else
10769 return true;
10770 }
10771 else
10772 {
10773 /* ARM EHABI Section 6.3:
10774
10775 An exception-handling table entry for the compact model looks like:
10776
10777 31 30-28 27-24 23-0
10778 -- ----- ----- ----
10779 1 0 index Data for personalityRoutine[index] */
10780
10781 if (filedata->file_header.e_machine == EM_ARM
10782 && (word & 0x70000000))
10783 {
10784 warn (_("Corrupt ARM compact model table entry: %x \n"), word);
10785 res = false;
10786 }
10787
10788 per_index = (word >> 24) & 0x7f;
10789 printf (_(" Compact model index: %d\n"), per_index);
10790 if (per_index == 0)
10791 {
10792 more_words = 0;
10793 word <<= 8;
10794 remaining--;
10795 }
10796 else if (per_index < 3)
10797 {
10798 more_words = (word >> 16) & 0xff;
10799 word <<= 16;
10800 remaining -= 2;
10801 }
10802 }
10803
10804 switch (filedata->file_header.e_machine)
10805 {
10806 case EM_ARM:
10807 if (per_index < 3)
10808 {
10809 if (! decode_arm_unwind_bytecode (filedata, aux, word, remaining, more_words,
10810 data_offset, data_sec, data_arm_sec))
10811 res = false;
10812 }
10813 else
10814 {
10815 warn (_("Unknown ARM compact model index encountered\n"));
10816 printf (_(" [reserved]\n"));
10817 res = false;
10818 }
10819 break;
10820
10821 case EM_TI_C6000:
10822 if (per_index < 3)
10823 {
10824 if (! decode_tic6x_unwind_bytecode (filedata, aux, word, remaining, more_words,
10825 data_offset, data_sec, data_arm_sec))
10826 res = false;
10827 }
10828 else if (per_index < 5)
10829 {
10830 if (((word >> 17) & 0x7f) == 0x7f)
10831 printf (_(" Restore stack from frame pointer\n"));
10832 else
10833 printf (_(" Stack increment %d\n"), (word >> 14) & 0x1fc);
10834 printf (_(" Registers restored: "));
10835 if (per_index == 4)
10836 printf (" (compact) ");
10837 decode_tic6x_unwind_regmask ((word >> 4) & 0x1fff);
10838 putchar ('\n');
10839 printf (_(" Return register: %s\n"),
10840 tic6x_unwind_regnames[word & 0xf]);
10841 }
10842 else
10843 printf (_(" [reserved (%d)]\n"), per_index);
10844 break;
10845
10846 default:
10847 error (_("Unsupported architecture type %d encountered when decoding unwind table\n"),
10848 filedata->file_header.e_machine);
10849 res = false;
10850 }
10851
10852 /* Decode the descriptors. Not implemented. */
10853
10854 return res;
10855 }
10856
10857 static bool
10858 dump_arm_unwind (Filedata * filedata,
10859 struct arm_unw_aux_info * aux,
10860 Elf_Internal_Shdr * exidx_sec)
10861 {
10862 struct arm_section exidx_arm_sec, extab_arm_sec;
10863 unsigned int i, exidx_len;
10864 uint64_t j, nfuns;
10865 bool res = true;
10866
10867 memset (&exidx_arm_sec, 0, sizeof (exidx_arm_sec));
10868 memset (&extab_arm_sec, 0, sizeof (extab_arm_sec));
10869 exidx_len = exidx_sec->sh_size / 8;
10870
10871 aux->funtab = xmalloc (aux->nsyms * sizeof (Elf_Internal_Sym));
10872 for (nfuns = 0, j = 0; j < aux->nsyms; j++)
10873 if (aux->symtab[j].st_value && ELF_ST_TYPE (aux->symtab[j].st_info) == STT_FUNC)
10874 aux->funtab[nfuns++] = aux->symtab[j];
10875 aux->nfuns = nfuns;
10876 qsort (aux->funtab, aux->nfuns, sizeof (Elf_Internal_Sym), symcmp);
10877
10878 for (i = 0; i < exidx_len; i++)
10879 {
10880 unsigned int exidx_fn, exidx_entry;
10881 struct absaddr fn_addr, entry_addr;
10882 uint64_t fn;
10883
10884 fputc ('\n', stdout);
10885
10886 if (! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10887 8 * i, & exidx_fn, & fn_addr, NULL)
10888 || ! get_unwind_section_word (filedata, aux, & exidx_arm_sec, exidx_sec,
10889 8 * i + 4, & exidx_entry, & entry_addr, NULL))
10890 {
10891 free (aux->funtab);
10892 arm_free_section (& exidx_arm_sec);
10893 arm_free_section (& extab_arm_sec);
10894 return false;
10895 }
10896
10897 /* ARM EHABI, Section 5:
10898 An index table entry consists of 2 words.
10899 The first word contains a prel31 offset to the start of a function, with bit 31 clear. */
10900 if (exidx_fn & 0x80000000)
10901 {
10902 warn (_("corrupt index table entry: %x\n"), exidx_fn);
10903 res = false;
10904 }
10905
10906 fn = arm_expand_prel31 (filedata, exidx_fn, exidx_sec->sh_addr + 8 * i);
10907
10908 arm_print_vma_and_name (filedata, aux, fn, fn_addr);
10909 fputs (": ", stdout);
10910
10911 if (exidx_entry == 1)
10912 {
10913 print_vma (exidx_entry, PREFIX_HEX);
10914 fputs (" [cantunwind]\n", stdout);
10915 }
10916 else if (exidx_entry & 0x80000000)
10917 {
10918 print_vma (exidx_entry, PREFIX_HEX);
10919 fputc ('\n', stdout);
10920 decode_arm_unwind (filedata, aux, exidx_entry, 4, 0, NULL, NULL);
10921 }
10922 else
10923 {
10924 uint64_t table, table_offset = 0;
10925 Elf_Internal_Shdr *table_sec;
10926
10927 fputs ("@", stdout);
10928 table = arm_expand_prel31 (filedata, exidx_entry, exidx_sec->sh_addr + 8 * i + 4);
10929 print_vma (table, PREFIX_HEX);
10930 printf ("\n");
10931
10932 /* Locate the matching .ARM.extab. */
10933 if (entry_addr.section != SHN_UNDEF
10934 && entry_addr.section < filedata->file_header.e_shnum)
10935 {
10936 table_sec = filedata->section_headers + entry_addr.section;
10937 table_offset = entry_addr.offset;
10938 /* PR 18879 */
10939 if (table_offset > table_sec->sh_size)
10940 {
10941 warn (_("Unwind entry contains corrupt offset (%#" PRIx64 ") into section %s\n"),
10942 table_offset,
10943 printable_section_name (filedata, table_sec));
10944 res = false;
10945 continue;
10946 }
10947 }
10948 else
10949 {
10950 table_sec = find_section_by_address (filedata, table);
10951 if (table_sec != NULL)
10952 table_offset = table - table_sec->sh_addr;
10953 }
10954
10955 if (table_sec == NULL)
10956 {
10957 warn (_("Could not locate .ARM.extab section containing %#" PRIx64 ".\n"),
10958 table);
10959 res = false;
10960 continue;
10961 }
10962
10963 if (! decode_arm_unwind (filedata, aux, 0, 0, table_offset, table_sec,
10964 &extab_arm_sec))
10965 res = false;
10966 }
10967 }
10968
10969 printf ("\n");
10970
10971 free (aux->funtab);
10972 arm_free_section (&exidx_arm_sec);
10973 arm_free_section (&extab_arm_sec);
10974
10975 return res;
10976 }
10977
10978 /* Used for both ARM and C6X unwinding tables. */
10979
10980 static bool
10981 arm_process_unwind (Filedata * filedata)
10982 {
10983 struct arm_unw_aux_info aux;
10984 Elf_Internal_Shdr *unwsec = NULL;
10985 Elf_Internal_Shdr *sec;
10986 size_t i;
10987 unsigned int sec_type;
10988 bool res = true;
10989
10990 switch (filedata->file_header.e_machine)
10991 {
10992 case EM_ARM:
10993 sec_type = SHT_ARM_EXIDX;
10994 break;
10995
10996 case EM_TI_C6000:
10997 sec_type = SHT_C6000_UNWIND;
10998 break;
10999
11000 default:
11001 error (_("Unsupported architecture type %d encountered when processing unwind table\n"),
11002 filedata->file_header.e_machine);
11003 return false;
11004 }
11005
11006 if (filedata->string_table == NULL)
11007 return false;
11008
11009 memset (& aux, 0, sizeof (aux));
11010 aux.filedata = filedata;
11011
11012 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
11013 {
11014 if (sec->sh_type == SHT_SYMTAB)
11015 {
11016 if (aux.symtab)
11017 {
11018 error (_("Multiple symbol tables encountered\n"));
11019 free (aux.symtab);
11020 aux.symtab = NULL;
11021 free (aux.strtab);
11022 aux.strtab = NULL;
11023 }
11024 if (!get_symtab (filedata, sec, &aux.symtab, &aux.nsyms,
11025 &aux.strtab, &aux.strtab_size))
11026 return false;
11027 }
11028 else if (sec->sh_type == sec_type)
11029 unwsec = sec;
11030 }
11031
11032 if (unwsec == NULL)
11033 printf (_("\nThere are no unwind sections in this file.\n"));
11034 else
11035 for (i = 0, sec = filedata->section_headers; i < filedata->file_header.e_shnum; ++i, ++sec)
11036 {
11037 if (sec->sh_type == sec_type)
11038 {
11039 uint64_t num_unwind = sec->sh_size / (2 * eh_addr_size);
11040 printf (ngettext ("\nUnwind section '%s' at offset %#" PRIx64 " "
11041 "contains %" PRIu64 " entry:\n",
11042 "\nUnwind section '%s' at offset %#" PRIx64 " "
11043 "contains %" PRIu64 " entries:\n",
11044 num_unwind),
11045 printable_section_name (filedata, sec),
11046 sec->sh_offset,
11047 num_unwind);
11048
11049 if (! dump_arm_unwind (filedata, &aux, sec))
11050 res = false;
11051 }
11052 }
11053
11054 free (aux.symtab);
11055 free ((char *) aux.strtab);
11056
11057 return res;
11058 }
11059
11060 static bool
11061 no_processor_specific_unwind (Filedata * filedata ATTRIBUTE_UNUSED)
11062 {
11063 printf (_("No processor specific unwind information to decode\n"));
11064 return true;
11065 }
11066
11067 static bool
11068 process_unwind (Filedata * filedata)
11069 {
11070 struct unwind_handler
11071 {
11072 unsigned int machtype;
11073 bool (* handler)(Filedata *);
11074 } handlers[] =
11075 {
11076 { EM_ARM, arm_process_unwind },
11077 { EM_IA_64, ia64_process_unwind },
11078 { EM_PARISC, hppa_process_unwind },
11079 { EM_TI_C6000, arm_process_unwind },
11080 { EM_386, no_processor_specific_unwind },
11081 { EM_X86_64, no_processor_specific_unwind },
11082 { 0, NULL }
11083 };
11084 int i;
11085
11086 if (!do_unwind)
11087 return true;
11088
11089 for (i = 0; handlers[i].handler != NULL; i++)
11090 if (filedata->file_header.e_machine == handlers[i].machtype)
11091 return handlers[i].handler (filedata);
11092
11093 printf (_("\nThe decoding of unwind sections for machine type %s is not currently supported.\n"),
11094 get_machine_name (filedata->file_header.e_machine));
11095 return true;
11096 }
11097
11098 static void
11099 dynamic_section_aarch64_val (Elf_Internal_Dyn * entry)
11100 {
11101 switch (entry->d_tag)
11102 {
11103 case DT_AARCH64_BTI_PLT:
11104 case DT_AARCH64_PAC_PLT:
11105 break;
11106 default:
11107 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11108 break;
11109 }
11110 putchar ('\n');
11111 }
11112
11113 static void
11114 dynamic_section_mips_val (Filedata * filedata, Elf_Internal_Dyn * entry)
11115 {
11116 switch (entry->d_tag)
11117 {
11118 case DT_MIPS_FLAGS:
11119 if (entry->d_un.d_val == 0)
11120 printf (_("NONE"));
11121 else
11122 {
11123 static const char * opts[] =
11124 {
11125 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
11126 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
11127 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
11128 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
11129 "RLD_ORDER_SAFE"
11130 };
11131 unsigned int cnt;
11132 bool first = true;
11133
11134 for (cnt = 0; cnt < ARRAY_SIZE (opts); ++cnt)
11135 if (entry->d_un.d_val & (1 << cnt))
11136 {
11137 printf ("%s%s", first ? "" : " ", opts[cnt]);
11138 first = false;
11139 }
11140 }
11141 break;
11142
11143 case DT_MIPS_IVERSION:
11144 if (valid_dynamic_name (filedata, entry->d_un.d_val))
11145 printf (_("Interface Version: %s"),
11146 get_dynamic_name (filedata, entry->d_un.d_val));
11147 else
11148 printf (_("Interface Version: <corrupt: %" PRIx64 ">"),
11149 entry->d_un.d_ptr);
11150 break;
11151
11152 case DT_MIPS_TIME_STAMP:
11153 {
11154 char timebuf[128];
11155 struct tm * tmp;
11156 time_t atime = entry->d_un.d_val;
11157
11158 tmp = gmtime (&atime);
11159 /* PR 17531: file: 6accc532. */
11160 if (tmp == NULL)
11161 snprintf (timebuf, sizeof (timebuf), _("<corrupt>"));
11162 else
11163 snprintf (timebuf, sizeof (timebuf), "%04u-%02u-%02uT%02u:%02u:%02u",
11164 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
11165 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
11166 printf (_("Time Stamp: %s"), timebuf);
11167 }
11168 break;
11169
11170 case DT_MIPS_RLD_VERSION:
11171 case DT_MIPS_LOCAL_GOTNO:
11172 case DT_MIPS_CONFLICTNO:
11173 case DT_MIPS_LIBLISTNO:
11174 case DT_MIPS_SYMTABNO:
11175 case DT_MIPS_UNREFEXTNO:
11176 case DT_MIPS_HIPAGENO:
11177 case DT_MIPS_DELTA_CLASS_NO:
11178 case DT_MIPS_DELTA_INSTANCE_NO:
11179 case DT_MIPS_DELTA_RELOC_NO:
11180 case DT_MIPS_DELTA_SYM_NO:
11181 case DT_MIPS_DELTA_CLASSSYM_NO:
11182 case DT_MIPS_COMPACT_SIZE:
11183 print_vma (entry->d_un.d_val, DEC);
11184 break;
11185
11186 case DT_MIPS_XHASH:
11187 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
11188 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11189 /* Falls through. */
11190
11191 default:
11192 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11193 }
11194 putchar ('\n');
11195 }
11196
11197 static void
11198 dynamic_section_parisc_val (Elf_Internal_Dyn * entry)
11199 {
11200 switch (entry->d_tag)
11201 {
11202 case DT_HP_DLD_FLAGS:
11203 {
11204 static struct
11205 {
11206 unsigned int bit;
11207 const char * str;
11208 }
11209 flags[] =
11210 {
11211 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
11212 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
11213 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
11214 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
11215 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
11216 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
11217 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
11218 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
11219 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
11220 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
11221 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" },
11222 { DT_HP_GST, "HP_GST" },
11223 { DT_HP_SHLIB_FIXED, "HP_SHLIB_FIXED" },
11224 { DT_HP_MERGE_SHLIB_SEG, "HP_MERGE_SHLIB_SEG" },
11225 { DT_HP_NODELETE, "HP_NODELETE" },
11226 { DT_HP_GROUP, "HP_GROUP" },
11227 { DT_HP_PROTECT_LINKAGE_TABLE, "HP_PROTECT_LINKAGE_TABLE" }
11228 };
11229 bool first = true;
11230 size_t cnt;
11231 uint64_t val = entry->d_un.d_val;
11232
11233 for (cnt = 0; cnt < ARRAY_SIZE (flags); ++cnt)
11234 if (val & flags[cnt].bit)
11235 {
11236 if (! first)
11237 putchar (' ');
11238 fputs (flags[cnt].str, stdout);
11239 first = false;
11240 val ^= flags[cnt].bit;
11241 }
11242
11243 if (val != 0 || first)
11244 {
11245 if (! first)
11246 putchar (' ');
11247 print_vma (val, HEX);
11248 }
11249 }
11250 break;
11251
11252 default:
11253 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11254 break;
11255 }
11256 putchar ('\n');
11257 }
11258
11259 /* VMS vs Unix time offset and factor. */
11260
11261 #define VMS_EPOCH_OFFSET 35067168000000000LL
11262 #define VMS_GRANULARITY_FACTOR 10000000
11263 #ifndef INT64_MIN
11264 #define INT64_MIN (-9223372036854775807LL - 1)
11265 #endif
11266
11267 /* Display a VMS time in a human readable format. */
11268
11269 static void
11270 print_vms_time (int64_t vmstime)
11271 {
11272 struct tm *tm = NULL;
11273 time_t unxtime;
11274
11275 if (vmstime >= INT64_MIN + VMS_EPOCH_OFFSET)
11276 {
11277 vmstime = (vmstime - VMS_EPOCH_OFFSET) / VMS_GRANULARITY_FACTOR;
11278 unxtime = vmstime;
11279 if (unxtime == vmstime)
11280 tm = gmtime (&unxtime);
11281 }
11282 if (tm != NULL)
11283 printf ("%04u-%02u-%02uT%02u:%02u:%02u",
11284 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
11285 tm->tm_hour, tm->tm_min, tm->tm_sec);
11286 }
11287
11288 static void
11289 dynamic_section_ia64_val (Elf_Internal_Dyn * entry)
11290 {
11291 switch (entry->d_tag)
11292 {
11293 case DT_IA_64_PLT_RESERVE:
11294 /* First 3 slots reserved. */
11295 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11296 printf (" -- ");
11297 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
11298 break;
11299
11300 case DT_IA_64_VMS_LINKTIME:
11301 print_vms_time (entry->d_un.d_val);
11302 break;
11303
11304 case DT_IA_64_VMS_LNKFLAGS:
11305 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11306 if (entry->d_un.d_val & VMS_LF_CALL_DEBUG)
11307 printf (" CALL_DEBUG");
11308 if (entry->d_un.d_val & VMS_LF_NOP0BUFS)
11309 printf (" NOP0BUFS");
11310 if (entry->d_un.d_val & VMS_LF_P0IMAGE)
11311 printf (" P0IMAGE");
11312 if (entry->d_un.d_val & VMS_LF_MKTHREADS)
11313 printf (" MKTHREADS");
11314 if (entry->d_un.d_val & VMS_LF_UPCALLS)
11315 printf (" UPCALLS");
11316 if (entry->d_un.d_val & VMS_LF_IMGSTA)
11317 printf (" IMGSTA");
11318 if (entry->d_un.d_val & VMS_LF_INITIALIZE)
11319 printf (" INITIALIZE");
11320 if (entry->d_un.d_val & VMS_LF_MAIN)
11321 printf (" MAIN");
11322 if (entry->d_un.d_val & VMS_LF_EXE_INIT)
11323 printf (" EXE_INIT");
11324 if (entry->d_un.d_val & VMS_LF_TBK_IN_IMG)
11325 printf (" TBK_IN_IMG");
11326 if (entry->d_un.d_val & VMS_LF_DBG_IN_IMG)
11327 printf (" DBG_IN_IMG");
11328 if (entry->d_un.d_val & VMS_LF_TBK_IN_DSF)
11329 printf (" TBK_IN_DSF");
11330 if (entry->d_un.d_val & VMS_LF_DBG_IN_DSF)
11331 printf (" DBG_IN_DSF");
11332 if (entry->d_un.d_val & VMS_LF_SIGNATURES)
11333 printf (" SIGNATURES");
11334 if (entry->d_un.d_val & VMS_LF_REL_SEG_OFF)
11335 printf (" REL_SEG_OFF");
11336 break;
11337
11338 default:
11339 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
11340 break;
11341 }
11342 putchar ('\n');
11343 }
11344
11345 static bool
11346 get_32bit_dynamic_section (Filedata * filedata)
11347 {
11348 Elf32_External_Dyn * edyn;
11349 Elf32_External_Dyn * ext;
11350 Elf_Internal_Dyn * entry;
11351
11352 edyn = (Elf32_External_Dyn *) get_data (NULL, filedata,
11353 filedata->dynamic_addr, 1,
11354 filedata->dynamic_size,
11355 _("dynamic section"));
11356 if (!edyn)
11357 return false;
11358
11359 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
11360 might not have the luxury of section headers. Look for the DT_NULL
11361 terminator to determine the number of entries. */
11362 for (ext = edyn, filedata->dynamic_nent = 0;
11363 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
11364 ext++)
11365 {
11366 filedata->dynamic_nent++;
11367 if (BYTE_GET (ext->d_tag) == DT_NULL)
11368 break;
11369 }
11370
11371 filedata->dynamic_section
11372 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
11373 if (filedata->dynamic_section == NULL)
11374 {
11375 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
11376 filedata->dynamic_nent);
11377 free (edyn);
11378 return false;
11379 }
11380
11381 for (ext = edyn, entry = filedata->dynamic_section;
11382 entry < filedata->dynamic_section + filedata->dynamic_nent;
11383 ext++, entry++)
11384 {
11385 entry->d_tag = BYTE_GET (ext->d_tag);
11386 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
11387 }
11388
11389 free (edyn);
11390
11391 return true;
11392 }
11393
11394 static bool
11395 get_64bit_dynamic_section (Filedata * filedata)
11396 {
11397 Elf64_External_Dyn * edyn;
11398 Elf64_External_Dyn * ext;
11399 Elf_Internal_Dyn * entry;
11400
11401 /* Read in the data. */
11402 edyn = (Elf64_External_Dyn *) get_data (NULL, filedata,
11403 filedata->dynamic_addr, 1,
11404 filedata->dynamic_size,
11405 _("dynamic section"));
11406 if (!edyn)
11407 return false;
11408
11409 /* SGI's ELF has more than one section in the DYNAMIC segment, and we
11410 might not have the luxury of section headers. Look for the DT_NULL
11411 terminator to determine the number of entries. */
11412 for (ext = edyn, filedata->dynamic_nent = 0;
11413 /* PR 17533 file: 033-67080-0.004 - do not read past end of buffer. */
11414 (char *) (ext + 1) <= (char *) edyn + filedata->dynamic_size;
11415 ext++)
11416 {
11417 filedata->dynamic_nent++;
11418 if (BYTE_GET (ext->d_tag) == DT_NULL)
11419 break;
11420 }
11421
11422 filedata->dynamic_section
11423 = (Elf_Internal_Dyn *) cmalloc (filedata->dynamic_nent, sizeof (* entry));
11424 if (filedata->dynamic_section == NULL)
11425 {
11426 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
11427 filedata->dynamic_nent);
11428 free (edyn);
11429 return false;
11430 }
11431
11432 /* Convert from external to internal formats. */
11433 for (ext = edyn, entry = filedata->dynamic_section;
11434 entry < filedata->dynamic_section + filedata->dynamic_nent;
11435 ext++, entry++)
11436 {
11437 entry->d_tag = BYTE_GET (ext->d_tag);
11438 entry->d_un.d_val = BYTE_GET (ext->d_un.d_val);
11439 }
11440
11441 free (edyn);
11442
11443 return true;
11444 }
11445
11446 static bool
11447 get_dynamic_section (Filedata *filedata)
11448 {
11449 if (filedata->dynamic_section)
11450 return true;
11451
11452 if (is_32bit_elf)
11453 return get_32bit_dynamic_section (filedata);
11454 else
11455 return get_64bit_dynamic_section (filedata);
11456 }
11457
11458 static void
11459 print_dynamic_flags (uint64_t flags)
11460 {
11461 bool first = true;
11462
11463 while (flags)
11464 {
11465 uint64_t flag;
11466
11467 flag = flags & - flags;
11468 flags &= ~ flag;
11469
11470 if (first)
11471 first = false;
11472 else
11473 putc (' ', stdout);
11474
11475 switch (flag)
11476 {
11477 case DF_ORIGIN: fputs ("ORIGIN", stdout); break;
11478 case DF_SYMBOLIC: fputs ("SYMBOLIC", stdout); break;
11479 case DF_TEXTREL: fputs ("TEXTREL", stdout); break;
11480 case DF_BIND_NOW: fputs ("BIND_NOW", stdout); break;
11481 case DF_STATIC_TLS: fputs ("STATIC_TLS", stdout); break;
11482 default: fputs (_("unknown"), stdout); break;
11483 }
11484 }
11485 puts ("");
11486 }
11487
11488 static uint64_t *
11489 get_dynamic_data (Filedata * filedata, uint64_t number, unsigned int ent_size)
11490 {
11491 unsigned char * e_data;
11492 uint64_t * i_data;
11493
11494 /* If size_t is smaller than uint64_t, eg because you are building
11495 on a 32-bit host, then make sure that when number is cast to
11496 size_t no information is lost. */
11497 if ((size_t) number != number
11498 || ent_size * number / ent_size != number)
11499 {
11500 error (_("Size overflow prevents reading %" PRIu64
11501 " elements of size %u\n"),
11502 number, ent_size);
11503 return NULL;
11504 }
11505
11506 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not
11507 attempting to allocate memory when the read is bound to fail. */
11508 if (ent_size * number > filedata->file_size)
11509 {
11510 error (_("Invalid number of dynamic entries: %" PRIu64 "\n"),
11511 number);
11512 return NULL;
11513 }
11514
11515 e_data = (unsigned char *) cmalloc ((size_t) number, ent_size);
11516 if (e_data == NULL)
11517 {
11518 error (_("Out of memory reading %" PRIu64 " dynamic entries\n"),
11519 number);
11520 return NULL;
11521 }
11522
11523 if (fread (e_data, ent_size, (size_t) number, filedata->handle) != number)
11524 {
11525 error (_("Unable to read in %" PRIu64 " bytes of dynamic data\n"),
11526 number * ent_size);
11527 free (e_data);
11528 return NULL;
11529 }
11530
11531 i_data = (uint64_t *) cmalloc ((size_t) number, sizeof (*i_data));
11532 if (i_data == NULL)
11533 {
11534 error (_("Out of memory allocating space for %" PRIu64 " dynamic entries\n"),
11535 number);
11536 free (e_data);
11537 return NULL;
11538 }
11539
11540 while (number--)
11541 i_data[number] = byte_get (e_data + number * ent_size, ent_size);
11542
11543 free (e_data);
11544
11545 return i_data;
11546 }
11547
11548 static uint64_t
11549 get_num_dynamic_syms (Filedata * filedata)
11550 {
11551 uint64_t num_of_syms = 0;
11552
11553 if (!do_histogram && (!do_using_dynamic || do_dyn_syms))
11554 return num_of_syms;
11555
11556 if (filedata->dynamic_info[DT_HASH])
11557 {
11558 unsigned char nb[8];
11559 unsigned char nc[8];
11560 unsigned int hash_ent_size = 4;
11561
11562 if ((filedata->file_header.e_machine == EM_ALPHA
11563 || filedata->file_header.e_machine == EM_S390
11564 || filedata->file_header.e_machine == EM_S390_OLD)
11565 && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64)
11566 hash_ent_size = 8;
11567
11568 if (fseek64 (filedata->handle,
11569 (filedata->archive_file_offset
11570 + offset_from_vma (filedata,
11571 filedata->dynamic_info[DT_HASH],
11572 sizeof nb + sizeof nc)),
11573 SEEK_SET))
11574 {
11575 error (_("Unable to seek to start of dynamic information\n"));
11576 goto no_hash;
11577 }
11578
11579 if (fread (nb, hash_ent_size, 1, filedata->handle) != 1)
11580 {
11581 error (_("Failed to read in number of buckets\n"));
11582 goto no_hash;
11583 }
11584
11585 if (fread (nc, hash_ent_size, 1, filedata->handle) != 1)
11586 {
11587 error (_("Failed to read in number of chains\n"));
11588 goto no_hash;
11589 }
11590
11591 filedata->nbuckets = byte_get (nb, hash_ent_size);
11592 filedata->nchains = byte_get (nc, hash_ent_size);
11593
11594 if (filedata->nbuckets != 0 && filedata->nchains != 0)
11595 {
11596 filedata->buckets = get_dynamic_data (filedata, filedata->nbuckets,
11597 hash_ent_size);
11598 filedata->chains = get_dynamic_data (filedata, filedata->nchains,
11599 hash_ent_size);
11600
11601 if (filedata->buckets != NULL && filedata->chains != NULL)
11602 num_of_syms = filedata->nchains;
11603 }
11604 no_hash:
11605 if (num_of_syms == 0)
11606 {
11607 free (filedata->buckets);
11608 filedata->buckets = NULL;
11609 free (filedata->chains);
11610 filedata->chains = NULL;
11611 filedata->nbuckets = 0;
11612 }
11613 }
11614
11615 if (filedata->dynamic_info_DT_GNU_HASH)
11616 {
11617 unsigned char nb[16];
11618 uint64_t i, maxchain = 0xffffffff, bitmaskwords;
11619 uint64_t buckets_vma;
11620 uint64_t hn;
11621
11622 if (fseek64 (filedata->handle,
11623 (filedata->archive_file_offset
11624 + offset_from_vma (filedata,
11625 filedata->dynamic_info_DT_GNU_HASH,
11626 sizeof nb)),
11627 SEEK_SET))
11628 {
11629 error (_("Unable to seek to start of dynamic information\n"));
11630 goto no_gnu_hash;
11631 }
11632
11633 if (fread (nb, 16, 1, filedata->handle) != 1)
11634 {
11635 error (_("Failed to read in number of buckets\n"));
11636 goto no_gnu_hash;
11637 }
11638
11639 filedata->ngnubuckets = byte_get (nb, 4);
11640 filedata->gnusymidx = byte_get (nb + 4, 4);
11641 bitmaskwords = byte_get (nb + 8, 4);
11642 buckets_vma = filedata->dynamic_info_DT_GNU_HASH + 16;
11643 if (is_32bit_elf)
11644 buckets_vma += bitmaskwords * 4;
11645 else
11646 buckets_vma += bitmaskwords * 8;
11647
11648 if (fseek64 (filedata->handle,
11649 (filedata->archive_file_offset
11650 + offset_from_vma (filedata, buckets_vma, 4)),
11651 SEEK_SET))
11652 {
11653 error (_("Unable to seek to start of dynamic information\n"));
11654 goto no_gnu_hash;
11655 }
11656
11657 filedata->gnubuckets
11658 = get_dynamic_data (filedata, filedata->ngnubuckets, 4);
11659
11660 if (filedata->gnubuckets == NULL)
11661 goto no_gnu_hash;
11662
11663 for (i = 0; i < filedata->ngnubuckets; i++)
11664 if (filedata->gnubuckets[i] != 0)
11665 {
11666 if (filedata->gnubuckets[i] < filedata->gnusymidx)
11667 goto no_gnu_hash;
11668
11669 if (maxchain == 0xffffffff || filedata->gnubuckets[i] > maxchain)
11670 maxchain = filedata->gnubuckets[i];
11671 }
11672
11673 if (maxchain == 0xffffffff)
11674 goto no_gnu_hash;
11675
11676 maxchain -= filedata->gnusymidx;
11677
11678 if (fseek64 (filedata->handle,
11679 (filedata->archive_file_offset
11680 + offset_from_vma (filedata,
11681 buckets_vma + 4 * (filedata->ngnubuckets
11682 + maxchain),
11683 4)),
11684 SEEK_SET))
11685 {
11686 error (_("Unable to seek to start of dynamic information\n"));
11687 goto no_gnu_hash;
11688 }
11689
11690 do
11691 {
11692 if (fread (nb, 4, 1, filedata->handle) != 1)
11693 {
11694 error (_("Failed to determine last chain length\n"));
11695 goto no_gnu_hash;
11696 }
11697
11698 if (maxchain + 1 == 0)
11699 goto no_gnu_hash;
11700
11701 ++maxchain;
11702 }
11703 while ((byte_get (nb, 4) & 1) == 0);
11704
11705 if (fseek64 (filedata->handle,
11706 (filedata->archive_file_offset
11707 + offset_from_vma (filedata, (buckets_vma
11708 + 4 * filedata->ngnubuckets),
11709 4)),
11710 SEEK_SET))
11711 {
11712 error (_("Unable to seek to start of dynamic information\n"));
11713 goto no_gnu_hash;
11714 }
11715
11716 filedata->gnuchains = get_dynamic_data (filedata, maxchain, 4);
11717 filedata->ngnuchains = maxchain;
11718
11719 if (filedata->gnuchains == NULL)
11720 goto no_gnu_hash;
11721
11722 if (filedata->dynamic_info_DT_MIPS_XHASH)
11723 {
11724 if (fseek64 (filedata->handle,
11725 (filedata->archive_file_offset
11726 + offset_from_vma (filedata, (buckets_vma
11727 + 4 * (filedata->ngnubuckets
11728 + maxchain)), 4)),
11729 SEEK_SET))
11730 {
11731 error (_("Unable to seek to start of dynamic information\n"));
11732 goto no_gnu_hash;
11733 }
11734
11735 filedata->mipsxlat = get_dynamic_data (filedata, maxchain, 4);
11736 if (filedata->mipsxlat == NULL)
11737 goto no_gnu_hash;
11738 }
11739
11740 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
11741 if (filedata->gnubuckets[hn] != 0)
11742 {
11743 uint64_t si = filedata->gnubuckets[hn];
11744 uint64_t off = si - filedata->gnusymidx;
11745
11746 do
11747 {
11748 if (filedata->dynamic_info_DT_MIPS_XHASH)
11749 {
11750 if (off < filedata->ngnuchains
11751 && filedata->mipsxlat[off] >= num_of_syms)
11752 num_of_syms = filedata->mipsxlat[off] + 1;
11753 }
11754 else
11755 {
11756 if (si >= num_of_syms)
11757 num_of_syms = si + 1;
11758 }
11759 si++;
11760 }
11761 while (off < filedata->ngnuchains
11762 && (filedata->gnuchains[off++] & 1) == 0);
11763 }
11764
11765 if (num_of_syms == 0)
11766 {
11767 no_gnu_hash:
11768 free (filedata->mipsxlat);
11769 filedata->mipsxlat = NULL;
11770 free (filedata->gnuchains);
11771 filedata->gnuchains = NULL;
11772 free (filedata->gnubuckets);
11773 filedata->gnubuckets = NULL;
11774 filedata->ngnubuckets = 0;
11775 filedata->ngnuchains = 0;
11776 }
11777 }
11778
11779 return num_of_syms;
11780 }
11781
11782 /* Parse and display the contents of the dynamic section. */
11783
11784 static bool
11785 process_dynamic_section (Filedata * filedata)
11786 {
11787 Elf_Internal_Dyn * entry;
11788
11789 if (filedata->dynamic_size <= 1)
11790 {
11791 if (do_dynamic)
11792 {
11793 if (filedata->is_separate)
11794 printf (_("\nThere is no dynamic section in linked file '%s'.\n"),
11795 filedata->file_name);
11796 else
11797 printf (_("\nThere is no dynamic section in this file.\n"));
11798 }
11799
11800 return true;
11801 }
11802
11803 if (!get_dynamic_section (filedata))
11804 return false;
11805
11806 /* Find the appropriate symbol table. */
11807 if (filedata->dynamic_symbols == NULL || do_histogram)
11808 {
11809 uint64_t num_of_syms;
11810
11811 for (entry = filedata->dynamic_section;
11812 entry < filedata->dynamic_section + filedata->dynamic_nent;
11813 ++entry)
11814 if (entry->d_tag == DT_SYMTAB)
11815 filedata->dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
11816 else if (entry->d_tag == DT_SYMENT)
11817 filedata->dynamic_info[DT_SYMENT] = entry->d_un.d_val;
11818 else if (entry->d_tag == DT_HASH)
11819 filedata->dynamic_info[DT_HASH] = entry->d_un.d_val;
11820 else if (entry->d_tag == DT_GNU_HASH)
11821 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11822 else if ((filedata->file_header.e_machine == EM_MIPS
11823 || filedata->file_header.e_machine == EM_MIPS_RS3_LE)
11824 && entry->d_tag == DT_MIPS_XHASH)
11825 {
11826 filedata->dynamic_info_DT_MIPS_XHASH = entry->d_un.d_val;
11827 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
11828 }
11829
11830 num_of_syms = get_num_dynamic_syms (filedata);
11831
11832 if (num_of_syms != 0
11833 && filedata->dynamic_symbols == NULL
11834 && filedata->dynamic_info[DT_SYMTAB]
11835 && filedata->dynamic_info[DT_SYMENT])
11836 {
11837 Elf_Internal_Phdr *seg;
11838 uint64_t vma = filedata->dynamic_info[DT_SYMTAB];
11839
11840 if (! get_program_headers (filedata))
11841 {
11842 error (_("Cannot interpret virtual addresses "
11843 "without program headers.\n"));
11844 return false;
11845 }
11846
11847 for (seg = filedata->program_headers;
11848 seg < filedata->program_headers + filedata->file_header.e_phnum;
11849 ++seg)
11850 {
11851 if (seg->p_type != PT_LOAD)
11852 continue;
11853
11854 if (seg->p_offset + seg->p_filesz > filedata->file_size)
11855 {
11856 /* See PR 21379 for a reproducer. */
11857 error (_("Invalid PT_LOAD entry\n"));
11858 return false;
11859 }
11860
11861 if (vma >= (seg->p_vaddr & -seg->p_align)
11862 && vma < seg->p_vaddr + seg->p_filesz)
11863 {
11864 /* Since we do not know how big the symbol table is,
11865 we default to reading in up to the end of PT_LOAD
11866 segment and processing that. This is overkill, I
11867 know, but it should work. */
11868 Elf_Internal_Shdr section;
11869 section.sh_offset = (vma - seg->p_vaddr
11870 + seg->p_offset);
11871 section.sh_size = (num_of_syms
11872 * filedata->dynamic_info[DT_SYMENT]);
11873 section.sh_entsize = filedata->dynamic_info[DT_SYMENT];
11874
11875 if (do_checks
11876 && filedata->dynamic_symtab_section != NULL
11877 && ((filedata->dynamic_symtab_section->sh_offset
11878 != section.sh_offset)
11879 || (filedata->dynamic_symtab_section->sh_size
11880 != section.sh_size)
11881 || (filedata->dynamic_symtab_section->sh_entsize
11882 != section.sh_entsize)))
11883 warn (_("\
11884 the .dynsym section doesn't match the DT_SYMTAB and DT_SYMENT tags\n"));
11885
11886 section.sh_name = filedata->string_table_length;
11887 filedata->dynamic_symbols
11888 = get_elf_symbols (filedata, &section,
11889 &filedata->num_dynamic_syms);
11890 if (filedata->dynamic_symbols == NULL
11891 || filedata->num_dynamic_syms != num_of_syms)
11892 {
11893 error (_("Corrupt DT_SYMTAB dynamic entry\n"));
11894 return false;
11895 }
11896 break;
11897 }
11898 }
11899 }
11900 }
11901
11902 /* Similarly find a string table. */
11903 if (filedata->dynamic_strings == NULL)
11904 for (entry = filedata->dynamic_section;
11905 entry < filedata->dynamic_section + filedata->dynamic_nent;
11906 ++entry)
11907 {
11908 if (entry->d_tag == DT_STRTAB)
11909 filedata->dynamic_info[DT_STRTAB] = entry->d_un.d_val;
11910
11911 if (entry->d_tag == DT_STRSZ)
11912 filedata->dynamic_info[DT_STRSZ] = entry->d_un.d_val;
11913
11914 if (filedata->dynamic_info[DT_STRTAB]
11915 && filedata->dynamic_info[DT_STRSZ])
11916 {
11917 uint64_t offset;
11918 uint64_t str_tab_len = filedata->dynamic_info[DT_STRSZ];
11919
11920 offset = offset_from_vma (filedata,
11921 filedata->dynamic_info[DT_STRTAB],
11922 str_tab_len);
11923 if (do_checks
11924 && filedata->dynamic_strtab_section
11925 && ((filedata->dynamic_strtab_section->sh_offset
11926 != (file_ptr) offset)
11927 || (filedata->dynamic_strtab_section->sh_size
11928 != str_tab_len)))
11929 warn (_("\
11930 the .dynstr section doesn't match the DT_STRTAB and DT_STRSZ tags\n"));
11931
11932 filedata->dynamic_strings
11933 = (char *) get_data (NULL, filedata, offset, 1, str_tab_len,
11934 _("dynamic string table"));
11935 if (filedata->dynamic_strings == NULL)
11936 {
11937 error (_("Corrupt DT_STRTAB dynamic entry\n"));
11938 break;
11939 }
11940
11941 filedata->dynamic_strings_length = str_tab_len;
11942 break;
11943 }
11944 }
11945
11946 /* And find the syminfo section if available. */
11947 if (filedata->dynamic_syminfo == NULL)
11948 {
11949 uint64_t syminsz = 0;
11950
11951 for (entry = filedata->dynamic_section;
11952 entry < filedata->dynamic_section + filedata->dynamic_nent;
11953 ++entry)
11954 {
11955 if (entry->d_tag == DT_SYMINENT)
11956 {
11957 /* Note: these braces are necessary to avoid a syntax
11958 error from the SunOS4 C compiler. */
11959 /* PR binutils/17531: A corrupt file can trigger this test.
11960 So do not use an assert, instead generate an error message. */
11961 if (sizeof (Elf_External_Syminfo) != entry->d_un.d_val)
11962 error (_("Bad value (%d) for SYMINENT entry\n"),
11963 (int) entry->d_un.d_val);
11964 }
11965 else if (entry->d_tag == DT_SYMINSZ)
11966 syminsz = entry->d_un.d_val;
11967 else if (entry->d_tag == DT_SYMINFO)
11968 filedata->dynamic_syminfo_offset
11969 = offset_from_vma (filedata, entry->d_un.d_val, syminsz);
11970 }
11971
11972 if (filedata->dynamic_syminfo_offset != 0 && syminsz != 0)
11973 {
11974 Elf_External_Syminfo * extsyminfo;
11975 Elf_External_Syminfo * extsym;
11976 Elf_Internal_Syminfo * syminfo;
11977
11978 /* There is a syminfo section. Read the data. */
11979 extsyminfo = (Elf_External_Syminfo *)
11980 get_data (NULL, filedata, filedata->dynamic_syminfo_offset,
11981 1, syminsz, _("symbol information"));
11982 if (!extsyminfo)
11983 return false;
11984
11985 if (filedata->dynamic_syminfo != NULL)
11986 {
11987 error (_("Multiple dynamic symbol information sections found\n"));
11988 free (filedata->dynamic_syminfo);
11989 }
11990 filedata->dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
11991 if (filedata->dynamic_syminfo == NULL)
11992 {
11993 error (_("Out of memory allocating %" PRIu64
11994 " bytes for dynamic symbol info\n"),
11995 syminsz);
11996 return false;
11997 }
11998
11999 filedata->dynamic_syminfo_nent
12000 = syminsz / sizeof (Elf_External_Syminfo);
12001 for (syminfo = filedata->dynamic_syminfo, extsym = extsyminfo;
12002 syminfo < (filedata->dynamic_syminfo
12003 + filedata->dynamic_syminfo_nent);
12004 ++syminfo, ++extsym)
12005 {
12006 syminfo->si_boundto = BYTE_GET (extsym->si_boundto);
12007 syminfo->si_flags = BYTE_GET (extsym->si_flags);
12008 }
12009
12010 free (extsyminfo);
12011 }
12012 }
12013
12014 if (do_dynamic && filedata->dynamic_addr)
12015 {
12016 if (filedata->is_separate)
12017 printf (ngettext ("\nIn linked file '%s' the dynamic section at offset %#" PRIx64 " contains %" PRIu64 " entry:\n",
12018 "\nIn linked file '%s' the dynamic section at offset %#" PRIx64 " contains %" PRIu64 " entries:\n",
12019 filedata->dynamic_nent),
12020 filedata->file_name,
12021 filedata->dynamic_addr,
12022 filedata->dynamic_nent);
12023 else
12024 printf (ngettext ("\nDynamic section at offset %#" PRIx64 " contains %" PRIu64 " entry:\n",
12025 "\nDynamic section at offset %#" PRIx64 " contains %" PRIu64 " entries:\n",
12026 filedata->dynamic_nent),
12027 filedata->dynamic_addr,
12028 filedata->dynamic_nent);
12029 }
12030 if (do_dynamic)
12031 printf (_(" Tag Type Name/Value\n"));
12032
12033 for (entry = filedata->dynamic_section;
12034 entry < filedata->dynamic_section + filedata->dynamic_nent;
12035 entry++)
12036 {
12037 if (do_dynamic)
12038 {
12039 const char * dtype;
12040
12041 putchar (' ');
12042 print_vma (entry->d_tag, FULL_HEX);
12043 dtype = get_dynamic_type (filedata, entry->d_tag);
12044 printf (" (%s)%*s", dtype,
12045 ((is_32bit_elf ? 27 : 19) - (int) strlen (dtype)), " ");
12046 }
12047
12048 switch (entry->d_tag)
12049 {
12050 case DT_FLAGS:
12051 if (do_dynamic)
12052 print_dynamic_flags (entry->d_un.d_val);
12053 break;
12054
12055 case DT_AUXILIARY:
12056 case DT_FILTER:
12057 case DT_CONFIG:
12058 case DT_DEPAUDIT:
12059 case DT_AUDIT:
12060 if (do_dynamic)
12061 {
12062 switch (entry->d_tag)
12063 {
12064 case DT_AUXILIARY:
12065 printf (_("Auxiliary library"));
12066 break;
12067
12068 case DT_FILTER:
12069 printf (_("Filter library"));
12070 break;
12071
12072 case DT_CONFIG:
12073 printf (_("Configuration file"));
12074 break;
12075
12076 case DT_DEPAUDIT:
12077 printf (_("Dependency audit library"));
12078 break;
12079
12080 case DT_AUDIT:
12081 printf (_("Audit library"));
12082 break;
12083 }
12084
12085 if (valid_dynamic_name (filedata, entry->d_un.d_val))
12086 printf (": [%s]\n",
12087 get_dynamic_name (filedata, entry->d_un.d_val));
12088 else
12089 {
12090 printf (": ");
12091 print_vma (entry->d_un.d_val, PREFIX_HEX);
12092 putchar ('\n');
12093 }
12094 }
12095 break;
12096
12097 case DT_FEATURE:
12098 if (do_dynamic)
12099 {
12100 printf (_("Flags:"));
12101
12102 if (entry->d_un.d_val == 0)
12103 printf (_(" None\n"));
12104 else
12105 {
12106 uint64_t val = entry->d_un.d_val;
12107
12108 if (val & DTF_1_PARINIT)
12109 {
12110 printf (" PARINIT");
12111 val ^= DTF_1_PARINIT;
12112 }
12113 if (val & DTF_1_CONFEXP)
12114 {
12115 printf (" CONFEXP");
12116 val ^= DTF_1_CONFEXP;
12117 }
12118 if (val != 0)
12119 printf (" %" PRIx64, val);
12120 puts ("");
12121 }
12122 }
12123 break;
12124
12125 case DT_POSFLAG_1:
12126 if (do_dynamic)
12127 {
12128 printf (_("Flags:"));
12129
12130 if (entry->d_un.d_val == 0)
12131 printf (_(" None\n"));
12132 else
12133 {
12134 uint64_t val = entry->d_un.d_val;
12135
12136 if (val & DF_P1_LAZYLOAD)
12137 {
12138 printf (" LAZYLOAD");
12139 val ^= DF_P1_LAZYLOAD;
12140 }
12141 if (val & DF_P1_GROUPPERM)
12142 {
12143 printf (" GROUPPERM");
12144 val ^= DF_P1_GROUPPERM;
12145 }
12146 if (val != 0)
12147 printf (" %" PRIx64, val);
12148 puts ("");
12149 }
12150 }
12151 break;
12152
12153 case DT_FLAGS_1:
12154 if (do_dynamic)
12155 {
12156 printf (_("Flags:"));
12157 if (entry->d_un.d_val == 0)
12158 printf (_(" None\n"));
12159 else
12160 {
12161 uint64_t val = entry->d_un.d_val;
12162
12163 if (val & DF_1_NOW)
12164 {
12165 printf (" NOW");
12166 val ^= DF_1_NOW;
12167 }
12168 if (val & DF_1_GLOBAL)
12169 {
12170 printf (" GLOBAL");
12171 val ^= DF_1_GLOBAL;
12172 }
12173 if (val & DF_1_GROUP)
12174 {
12175 printf (" GROUP");
12176 val ^= DF_1_GROUP;
12177 }
12178 if (val & DF_1_NODELETE)
12179 {
12180 printf (" NODELETE");
12181 val ^= DF_1_NODELETE;
12182 }
12183 if (val & DF_1_LOADFLTR)
12184 {
12185 printf (" LOADFLTR");
12186 val ^= DF_1_LOADFLTR;
12187 }
12188 if (val & DF_1_INITFIRST)
12189 {
12190 printf (" INITFIRST");
12191 val ^= DF_1_INITFIRST;
12192 }
12193 if (val & DF_1_NOOPEN)
12194 {
12195 printf (" NOOPEN");
12196 val ^= DF_1_NOOPEN;
12197 }
12198 if (val & DF_1_ORIGIN)
12199 {
12200 printf (" ORIGIN");
12201 val ^= DF_1_ORIGIN;
12202 }
12203 if (val & DF_1_DIRECT)
12204 {
12205 printf (" DIRECT");
12206 val ^= DF_1_DIRECT;
12207 }
12208 if (val & DF_1_TRANS)
12209 {
12210 printf (" TRANS");
12211 val ^= DF_1_TRANS;
12212 }
12213 if (val & DF_1_INTERPOSE)
12214 {
12215 printf (" INTERPOSE");
12216 val ^= DF_1_INTERPOSE;
12217 }
12218 if (val & DF_1_NODEFLIB)
12219 {
12220 printf (" NODEFLIB");
12221 val ^= DF_1_NODEFLIB;
12222 }
12223 if (val & DF_1_NODUMP)
12224 {
12225 printf (" NODUMP");
12226 val ^= DF_1_NODUMP;
12227 }
12228 if (val & DF_1_CONFALT)
12229 {
12230 printf (" CONFALT");
12231 val ^= DF_1_CONFALT;
12232 }
12233 if (val & DF_1_ENDFILTEE)
12234 {
12235 printf (" ENDFILTEE");
12236 val ^= DF_1_ENDFILTEE;
12237 }
12238 if (val & DF_1_DISPRELDNE)
12239 {
12240 printf (" DISPRELDNE");
12241 val ^= DF_1_DISPRELDNE;
12242 }
12243 if (val & DF_1_DISPRELPND)
12244 {
12245 printf (" DISPRELPND");
12246 val ^= DF_1_DISPRELPND;
12247 }
12248 if (val & DF_1_NODIRECT)
12249 {
12250 printf (" NODIRECT");
12251 val ^= DF_1_NODIRECT;
12252 }
12253 if (val & DF_1_IGNMULDEF)
12254 {
12255 printf (" IGNMULDEF");
12256 val ^= DF_1_IGNMULDEF;
12257 }
12258 if (val & DF_1_NOKSYMS)
12259 {
12260 printf (" NOKSYMS");
12261 val ^= DF_1_NOKSYMS;
12262 }
12263 if (val & DF_1_NOHDR)
12264 {
12265 printf (" NOHDR");
12266 val ^= DF_1_NOHDR;
12267 }
12268 if (val & DF_1_EDITED)
12269 {
12270 printf (" EDITED");
12271 val ^= DF_1_EDITED;
12272 }
12273 if (val & DF_1_NORELOC)
12274 {
12275 printf (" NORELOC");
12276 val ^= DF_1_NORELOC;
12277 }
12278 if (val & DF_1_SYMINTPOSE)
12279 {
12280 printf (" SYMINTPOSE");
12281 val ^= DF_1_SYMINTPOSE;
12282 }
12283 if (val & DF_1_GLOBAUDIT)
12284 {
12285 printf (" GLOBAUDIT");
12286 val ^= DF_1_GLOBAUDIT;
12287 }
12288 if (val & DF_1_SINGLETON)
12289 {
12290 printf (" SINGLETON");
12291 val ^= DF_1_SINGLETON;
12292 }
12293 if (val & DF_1_STUB)
12294 {
12295 printf (" STUB");
12296 val ^= DF_1_STUB;
12297 }
12298 if (val & DF_1_PIE)
12299 {
12300 printf (" PIE");
12301 val ^= DF_1_PIE;
12302 }
12303 if (val & DF_1_KMOD)
12304 {
12305 printf (" KMOD");
12306 val ^= DF_1_KMOD;
12307 }
12308 if (val & DF_1_WEAKFILTER)
12309 {
12310 printf (" WEAKFILTER");
12311 val ^= DF_1_WEAKFILTER;
12312 }
12313 if (val & DF_1_NOCOMMON)
12314 {
12315 printf (" NOCOMMON");
12316 val ^= DF_1_NOCOMMON;
12317 }
12318 if (val != 0)
12319 printf (" %" PRIx64, val);
12320 puts ("");
12321 }
12322 }
12323 break;
12324
12325 case DT_PLTREL:
12326 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
12327 if (do_dynamic)
12328 puts (get_dynamic_type (filedata, entry->d_un.d_val));
12329 break;
12330
12331 case DT_NULL :
12332 case DT_NEEDED :
12333 case DT_PLTGOT :
12334 case DT_HASH :
12335 case DT_STRTAB :
12336 case DT_SYMTAB :
12337 case DT_RELA :
12338 case DT_INIT :
12339 case DT_FINI :
12340 case DT_SONAME :
12341 case DT_RPATH :
12342 case DT_SYMBOLIC:
12343 case DT_REL :
12344 case DT_RELR :
12345 case DT_DEBUG :
12346 case DT_TEXTREL :
12347 case DT_JMPREL :
12348 case DT_RUNPATH :
12349 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
12350
12351 if (do_dynamic)
12352 {
12353 const char *name;
12354
12355 if (valid_dynamic_name (filedata, entry->d_un.d_val))
12356 name = get_dynamic_name (filedata, entry->d_un.d_val);
12357 else
12358 name = NULL;
12359
12360 if (name)
12361 {
12362 switch (entry->d_tag)
12363 {
12364 case DT_NEEDED:
12365 printf (_("Shared library: [%s]"), name);
12366
12367 if (filedata->program_interpreter
12368 && streq (name, filedata->program_interpreter))
12369 printf (_(" program interpreter"));
12370 break;
12371
12372 case DT_SONAME:
12373 printf (_("Library soname: [%s]"), name);
12374 break;
12375
12376 case DT_RPATH:
12377 printf (_("Library rpath: [%s]"), name);
12378 break;
12379
12380 case DT_RUNPATH:
12381 printf (_("Library runpath: [%s]"), name);
12382 break;
12383
12384 default:
12385 print_vma (entry->d_un.d_val, PREFIX_HEX);
12386 break;
12387 }
12388 }
12389 else
12390 print_vma (entry->d_un.d_val, PREFIX_HEX);
12391
12392 putchar ('\n');
12393 }
12394 break;
12395
12396 case DT_PLTRELSZ:
12397 case DT_RELASZ :
12398 case DT_STRSZ :
12399 case DT_RELSZ :
12400 case DT_RELAENT :
12401 case DT_RELRENT :
12402 case DT_RELRSZ :
12403 case DT_SYMENT :
12404 case DT_RELENT :
12405 filedata->dynamic_info[entry->d_tag] = entry->d_un.d_val;
12406 /* Fall through. */
12407 case DT_PLTPADSZ:
12408 case DT_MOVEENT :
12409 case DT_MOVESZ :
12410 case DT_PREINIT_ARRAYSZ:
12411 case DT_INIT_ARRAYSZ:
12412 case DT_FINI_ARRAYSZ:
12413 case DT_GNU_CONFLICTSZ:
12414 case DT_GNU_LIBLISTSZ:
12415 if (do_dynamic)
12416 {
12417 print_vma (entry->d_un.d_val, UNSIGNED);
12418 printf (_(" (bytes)\n"));
12419 }
12420 break;
12421
12422 case DT_VERDEFNUM:
12423 case DT_VERNEEDNUM:
12424 case DT_RELACOUNT:
12425 case DT_RELCOUNT:
12426 if (do_dynamic)
12427 {
12428 print_vma (entry->d_un.d_val, UNSIGNED);
12429 putchar ('\n');
12430 }
12431 break;
12432
12433 case DT_SYMINSZ:
12434 case DT_SYMINENT:
12435 case DT_SYMINFO:
12436 case DT_USED:
12437 case DT_INIT_ARRAY:
12438 case DT_FINI_ARRAY:
12439 if (do_dynamic)
12440 {
12441 if (entry->d_tag == DT_USED
12442 && valid_dynamic_name (filedata, entry->d_un.d_val))
12443 {
12444 const char *name
12445 = get_dynamic_name (filedata, entry->d_un.d_val);
12446
12447 if (*name)
12448 {
12449 printf (_("Not needed object: [%s]\n"), name);
12450 break;
12451 }
12452 }
12453
12454 print_vma (entry->d_un.d_val, PREFIX_HEX);
12455 putchar ('\n');
12456 }
12457 break;
12458
12459 case DT_BIND_NOW:
12460 /* The value of this entry is ignored. */
12461 if (do_dynamic)
12462 putchar ('\n');
12463 break;
12464
12465 case DT_GNU_PRELINKED:
12466 if (do_dynamic)
12467 {
12468 struct tm * tmp;
12469 time_t atime = entry->d_un.d_val;
12470
12471 tmp = gmtime (&atime);
12472 /* PR 17533 file: 041-1244816-0.004. */
12473 if (tmp == NULL)
12474 printf (_("<corrupt time val: %" PRIx64),
12475 (uint64_t) atime);
12476 else
12477 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
12478 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
12479 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
12480
12481 }
12482 break;
12483
12484 case DT_GNU_HASH:
12485 filedata->dynamic_info_DT_GNU_HASH = entry->d_un.d_val;
12486 if (do_dynamic)
12487 {
12488 print_vma (entry->d_un.d_val, PREFIX_HEX);
12489 putchar ('\n');
12490 }
12491 break;
12492
12493 case DT_GNU_FLAGS_1:
12494 if (do_dynamic)
12495 {
12496 printf (_("Flags:"));
12497 if (entry->d_un.d_val == 0)
12498 printf (_(" None\n"));
12499 else
12500 {
12501 uint64_t val = entry->d_un.d_val;
12502
12503 if (val & DF_GNU_1_UNIQUE)
12504 {
12505 printf (" UNIQUE");
12506 val ^= DF_GNU_1_UNIQUE;
12507 }
12508 if (val != 0)
12509 printf (" %" PRIx64, val);
12510 puts ("");
12511 }
12512 }
12513 break;
12514
12515 default:
12516 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
12517 filedata->version_info[DT_VERSIONTAGIDX (entry->d_tag)]
12518 = entry->d_un.d_val;
12519
12520 if (do_dynamic)
12521 {
12522 switch (filedata->file_header.e_machine)
12523 {
12524 case EM_AARCH64:
12525 dynamic_section_aarch64_val (entry);
12526 break;
12527 case EM_MIPS:
12528 case EM_MIPS_RS3_LE:
12529 dynamic_section_mips_val (filedata, entry);
12530 break;
12531 case EM_PARISC:
12532 dynamic_section_parisc_val (entry);
12533 break;
12534 case EM_IA_64:
12535 dynamic_section_ia64_val (entry);
12536 break;
12537 default:
12538 print_vma (entry->d_un.d_val, PREFIX_HEX);
12539 putchar ('\n');
12540 }
12541 }
12542 break;
12543 }
12544 }
12545
12546 return true;
12547 }
12548
12549 static char *
12550 get_ver_flags (unsigned int flags)
12551 {
12552 static char buff[128];
12553
12554 buff[0] = 0;
12555
12556 if (flags == 0)
12557 return _("none");
12558
12559 if (flags & VER_FLG_BASE)
12560 strcat (buff, "BASE");
12561
12562 if (flags & VER_FLG_WEAK)
12563 {
12564 if (flags & VER_FLG_BASE)
12565 strcat (buff, " | ");
12566
12567 strcat (buff, "WEAK");
12568 }
12569
12570 if (flags & VER_FLG_INFO)
12571 {
12572 if (flags & (VER_FLG_BASE|VER_FLG_WEAK))
12573 strcat (buff, " | ");
12574
12575 strcat (buff, "INFO");
12576 }
12577
12578 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12579 {
12580 if (flags & (VER_FLG_BASE | VER_FLG_WEAK | VER_FLG_INFO))
12581 strcat (buff, " | ");
12582
12583 strcat (buff, _("<unknown>"));
12584 }
12585
12586 return buff;
12587 }
12588
12589 /* Display the contents of the version sections. */
12590
12591 static bool
12592 process_version_sections (Filedata * filedata)
12593 {
12594 Elf_Internal_Shdr * section;
12595 unsigned i;
12596 bool found = false;
12597
12598 if (! do_version)
12599 return true;
12600
12601 for (i = 0, section = filedata->section_headers;
12602 i < filedata->file_header.e_shnum;
12603 i++, section++)
12604 {
12605 switch (section->sh_type)
12606 {
12607 case SHT_GNU_verdef:
12608 {
12609 Elf_External_Verdef * edefs;
12610 size_t idx;
12611 size_t cnt;
12612 char * endbuf;
12613
12614 found = true;
12615
12616 if (filedata->is_separate)
12617 printf (ngettext ("\nIn linked file '%s' the version definition section '%s' contains %u entry:\n",
12618 "\nIn linked file '%s' the version definition section '%s' contains %u entries:\n",
12619 section->sh_info),
12620 filedata->file_name,
12621 printable_section_name (filedata, section),
12622 section->sh_info);
12623 else
12624 printf (ngettext ("\nVersion definition section '%s' "
12625 "contains %u entry:\n",
12626 "\nVersion definition section '%s' "
12627 "contains %u entries:\n",
12628 section->sh_info),
12629 printable_section_name (filedata, section),
12630 section->sh_info);
12631
12632 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12633 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
12634 section->sh_offset, section->sh_link,
12635 printable_section_name_from_index (filedata, section->sh_link, NULL));
12636
12637 edefs = (Elf_External_Verdef *)
12638 get_data (NULL, filedata, section->sh_offset, 1,section->sh_size,
12639 _("version definition section"));
12640 if (!edefs)
12641 break;
12642 endbuf = (char *) edefs + section->sh_size;
12643
12644 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12645 {
12646 char * vstart;
12647 Elf_External_Verdef * edef;
12648 Elf_Internal_Verdef ent;
12649 Elf_External_Verdaux * eaux;
12650 Elf_Internal_Verdaux aux;
12651 size_t isum;
12652 int j;
12653
12654 vstart = ((char *) edefs) + idx;
12655 if (vstart + sizeof (*edef) > endbuf)
12656 break;
12657
12658 edef = (Elf_External_Verdef *) vstart;
12659
12660 ent.vd_version = BYTE_GET (edef->vd_version);
12661 ent.vd_flags = BYTE_GET (edef->vd_flags);
12662 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
12663 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
12664 ent.vd_hash = BYTE_GET (edef->vd_hash);
12665 ent.vd_aux = BYTE_GET (edef->vd_aux);
12666 ent.vd_next = BYTE_GET (edef->vd_next);
12667
12668 printf (_(" %#06zx: Rev: %d Flags: %s"),
12669 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
12670
12671 printf (_(" Index: %d Cnt: %d "),
12672 ent.vd_ndx, ent.vd_cnt);
12673
12674 /* Check for overflow. */
12675 if (ent.vd_aux > (size_t) (endbuf - vstart))
12676 break;
12677
12678 vstart += ent.vd_aux;
12679
12680 if (vstart + sizeof (*eaux) > endbuf)
12681 break;
12682 eaux = (Elf_External_Verdaux *) vstart;
12683
12684 aux.vda_name = BYTE_GET (eaux->vda_name);
12685 aux.vda_next = BYTE_GET (eaux->vda_next);
12686
12687 if (valid_dynamic_name (filedata, aux.vda_name))
12688 printf (_("Name: %s\n"),
12689 get_dynamic_name (filedata, aux.vda_name));
12690 else
12691 printf (_("Name index: %ld\n"), aux.vda_name);
12692
12693 isum = idx + ent.vd_aux;
12694
12695 for (j = 1; j < ent.vd_cnt; j++)
12696 {
12697 if (aux.vda_next < sizeof (*eaux)
12698 && !(j == ent.vd_cnt - 1 && aux.vda_next == 0))
12699 {
12700 warn (_("Invalid vda_next field of %lx\n"),
12701 aux.vda_next);
12702 j = ent.vd_cnt;
12703 break;
12704 }
12705 /* Check for overflow. */
12706 if (aux.vda_next > (size_t) (endbuf - vstart))
12707 break;
12708
12709 isum += aux.vda_next;
12710 vstart += aux.vda_next;
12711
12712 if (vstart + sizeof (*eaux) > endbuf)
12713 break;
12714 eaux = (Elf_External_Verdaux *) vstart;
12715
12716 aux.vda_name = BYTE_GET (eaux->vda_name);
12717 aux.vda_next = BYTE_GET (eaux->vda_next);
12718
12719 if (valid_dynamic_name (filedata, aux.vda_name))
12720 printf (_(" %#06zx: Parent %d: %s\n"),
12721 isum, j,
12722 get_dynamic_name (filedata, aux.vda_name));
12723 else
12724 printf (_(" %#06zx: Parent %d, name index: %ld\n"),
12725 isum, j, aux.vda_name);
12726 }
12727
12728 if (j < ent.vd_cnt)
12729 printf (_(" Version def aux past end of section\n"));
12730
12731 /* PR 17531:
12732 file: id:000001,src:000172+005151,op:splice,rep:2. */
12733 if (ent.vd_next < sizeof (*edef)
12734 && !(cnt == section->sh_info - 1 && ent.vd_next == 0))
12735 {
12736 warn (_("Invalid vd_next field of %lx\n"), ent.vd_next);
12737 cnt = section->sh_info;
12738 break;
12739 }
12740 if (ent.vd_next > (size_t) (endbuf - ((char *) edefs + idx)))
12741 break;
12742
12743 idx += ent.vd_next;
12744 }
12745
12746 if (cnt < section->sh_info)
12747 printf (_(" Version definition past end of section\n"));
12748
12749 free (edefs);
12750 }
12751 break;
12752
12753 case SHT_GNU_verneed:
12754 {
12755 Elf_External_Verneed * eneed;
12756 size_t idx;
12757 size_t cnt;
12758 char * endbuf;
12759
12760 found = true;
12761
12762 if (filedata->is_separate)
12763 printf (ngettext ("\nIn linked file '%s' the version needs section '%s' contains %u entry:\n",
12764 "\nIn linked file '%s' the version needs section '%s' contains %u entries:\n",
12765 section->sh_info),
12766 filedata->file_name,
12767 printable_section_name (filedata, section),
12768 section->sh_info);
12769 else
12770 printf (ngettext ("\nVersion needs section '%s' "
12771 "contains %u entry:\n",
12772 "\nVersion needs section '%s' "
12773 "contains %u entries:\n",
12774 section->sh_info),
12775 printable_section_name (filedata, section),
12776 section->sh_info);
12777
12778 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12779 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
12780 section->sh_offset, section->sh_link,
12781 printable_section_name_from_index (filedata, section->sh_link, NULL));
12782
12783 eneed = (Elf_External_Verneed *) get_data (NULL, filedata,
12784 section->sh_offset, 1,
12785 section->sh_size,
12786 _("Version Needs section"));
12787 if (!eneed)
12788 break;
12789 endbuf = (char *) eneed + section->sh_size;
12790
12791 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
12792 {
12793 Elf_External_Verneed * entry;
12794 Elf_Internal_Verneed ent;
12795 size_t isum;
12796 int j;
12797 char * vstart;
12798
12799 vstart = ((char *) eneed) + idx;
12800 if (vstart + sizeof (*entry) > endbuf)
12801 break;
12802
12803 entry = (Elf_External_Verneed *) vstart;
12804
12805 ent.vn_version = BYTE_GET (entry->vn_version);
12806 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
12807 ent.vn_file = BYTE_GET (entry->vn_file);
12808 ent.vn_aux = BYTE_GET (entry->vn_aux);
12809 ent.vn_next = BYTE_GET (entry->vn_next);
12810
12811 printf (_(" %#06zx: Version: %d"), idx, ent.vn_version);
12812
12813 if (valid_dynamic_name (filedata, ent.vn_file))
12814 printf (_(" File: %s"),
12815 get_dynamic_name (filedata, ent.vn_file));
12816 else
12817 printf (_(" File: %lx"), ent.vn_file);
12818
12819 printf (_(" Cnt: %d\n"), ent.vn_cnt);
12820
12821 /* Check for overflow. */
12822 if (ent.vn_aux > (size_t) (endbuf - vstart))
12823 break;
12824 vstart += ent.vn_aux;
12825
12826 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
12827 {
12828 Elf_External_Vernaux * eaux;
12829 Elf_Internal_Vernaux aux;
12830
12831 if (vstart + sizeof (*eaux) > endbuf)
12832 break;
12833 eaux = (Elf_External_Vernaux *) vstart;
12834
12835 aux.vna_hash = BYTE_GET (eaux->vna_hash);
12836 aux.vna_flags = BYTE_GET (eaux->vna_flags);
12837 aux.vna_other = BYTE_GET (eaux->vna_other);
12838 aux.vna_name = BYTE_GET (eaux->vna_name);
12839 aux.vna_next = BYTE_GET (eaux->vna_next);
12840
12841 if (valid_dynamic_name (filedata, aux.vna_name))
12842 printf (_(" %#06zx: Name: %s"),
12843 isum, get_dynamic_name (filedata, aux.vna_name));
12844 else
12845 printf (_(" %#06zx: Name index: %lx"),
12846 isum, aux.vna_name);
12847
12848 printf (_(" Flags: %s Version: %d\n"),
12849 get_ver_flags (aux.vna_flags), aux.vna_other);
12850
12851 if (aux.vna_next < sizeof (*eaux)
12852 && !(j == ent.vn_cnt - 1 && aux.vna_next == 0))
12853 {
12854 warn (_("Invalid vna_next field of %lx\n"),
12855 aux.vna_next);
12856 j = ent.vn_cnt;
12857 break;
12858 }
12859 /* Check for overflow. */
12860 if (aux.vna_next > (size_t) (endbuf - vstart))
12861 break;
12862 isum += aux.vna_next;
12863 vstart += aux.vna_next;
12864 }
12865
12866 if (j < ent.vn_cnt)
12867 warn (_("Missing Version Needs auxiliary information\n"));
12868
12869 if (ent.vn_next < sizeof (*entry)
12870 && !(cnt == section->sh_info - 1 && ent.vn_next == 0))
12871 {
12872 warn (_("Invalid vn_next field of %lx\n"), ent.vn_next);
12873 cnt = section->sh_info;
12874 break;
12875 }
12876 if (ent.vn_next > (size_t) (endbuf - ((char *) eneed + idx)))
12877 break;
12878 idx += ent.vn_next;
12879 }
12880
12881 if (cnt < section->sh_info)
12882 warn (_("Missing Version Needs information\n"));
12883
12884 free (eneed);
12885 }
12886 break;
12887
12888 case SHT_GNU_versym:
12889 {
12890 Elf_Internal_Shdr * link_section;
12891 uint64_t total;
12892 unsigned int cnt;
12893 unsigned char * edata;
12894 unsigned short * data;
12895 char * strtab;
12896 Elf_Internal_Sym * symbols;
12897 Elf_Internal_Shdr * string_sec;
12898 uint64_t num_syms;
12899 uint64_t off;
12900
12901 if (section->sh_link >= filedata->file_header.e_shnum)
12902 break;
12903
12904 link_section = filedata->section_headers + section->sh_link;
12905 total = section->sh_size / sizeof (Elf_External_Versym);
12906
12907 if (link_section->sh_link >= filedata->file_header.e_shnum)
12908 break;
12909
12910 found = true;
12911
12912 symbols = get_elf_symbols (filedata, link_section, & num_syms);
12913 if (symbols == NULL)
12914 break;
12915
12916 string_sec = filedata->section_headers + link_section->sh_link;
12917
12918 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
12919 string_sec->sh_size,
12920 _("version string table"));
12921 if (!strtab)
12922 {
12923 free (symbols);
12924 break;
12925 }
12926
12927 if (filedata->is_separate)
12928 printf (ngettext ("\nIn linked file '%s' the version symbols section '%s' contains %" PRIu64 " entry:\n",
12929 "\nIn linked file '%s' the version symbols section '%s' contains %" PRIu64 " entries:\n",
12930 total),
12931 filedata->file_name,
12932 printable_section_name (filedata, section),
12933 total);
12934 else
12935 printf (ngettext ("\nVersion symbols section '%s' "
12936 "contains %" PRIu64 " entry:\n",
12937 "\nVersion symbols section '%s' "
12938 "contains %" PRIu64 " entries:\n",
12939 total),
12940 printable_section_name (filedata, section),
12941 total);
12942
12943 printf (_(" Addr: 0x%016" PRIx64), section->sh_addr);
12944 printf (_(" Offset: 0x%08" PRIx64 " Link: %u (%s)\n"),
12945 section->sh_offset, section->sh_link,
12946 printable_section_name (filedata, link_section));
12947
12948 off = offset_from_vma (filedata,
12949 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
12950 total * sizeof (short));
12951 edata = (unsigned char *) get_data (NULL, filedata, off,
12952 sizeof (short), total,
12953 _("version symbol data"));
12954 if (!edata)
12955 {
12956 free (strtab);
12957 free (symbols);
12958 break;
12959 }
12960
12961 data = (short unsigned int *) cmalloc (total, sizeof (short));
12962
12963 for (cnt = total; cnt --;)
12964 data[cnt] = byte_get (edata + cnt * sizeof (short),
12965 sizeof (short));
12966
12967 free (edata);
12968
12969 for (cnt = 0; cnt < total; cnt += 4)
12970 {
12971 int j, nn;
12972 char *name;
12973 char *invalid = _("*invalid*");
12974
12975 printf (" %03x:", cnt);
12976
12977 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
12978 switch (data[cnt + j])
12979 {
12980 case 0:
12981 fputs (_(" 0 (*local*) "), stdout);
12982 break;
12983
12984 case 1:
12985 fputs (_(" 1 (*global*) "), stdout);
12986 break;
12987
12988 default:
12989 nn = printf ("%4x%c", data[cnt + j] & VERSYM_VERSION,
12990 data[cnt + j] & VERSYM_HIDDEN ? 'h' : ' ');
12991
12992 /* If this index value is greater than the size of the symbols
12993 array, break to avoid an out-of-bounds read. */
12994 if (cnt + j >= num_syms)
12995 {
12996 warn (_("invalid index into symbol array\n"));
12997 break;
12998 }
12999
13000 name = NULL;
13001 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
13002 {
13003 Elf_Internal_Verneed ivn;
13004 uint64_t offset;
13005
13006 offset = offset_from_vma
13007 (filedata,
13008 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
13009 sizeof (Elf_External_Verneed));
13010
13011 do
13012 {
13013 Elf_Internal_Vernaux ivna;
13014 Elf_External_Verneed evn;
13015 Elf_External_Vernaux evna;
13016 uint64_t a_off;
13017
13018 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
13019 _("version need")) == NULL)
13020 break;
13021
13022 ivn.vn_aux = BYTE_GET (evn.vn_aux);
13023 ivn.vn_next = BYTE_GET (evn.vn_next);
13024
13025 a_off = offset + ivn.vn_aux;
13026
13027 do
13028 {
13029 if (get_data (&evna, filedata, a_off, sizeof (evna),
13030 1, _("version need aux (2)")) == NULL)
13031 {
13032 ivna.vna_next = 0;
13033 ivna.vna_other = 0;
13034 }
13035 else
13036 {
13037 ivna.vna_next = BYTE_GET (evna.vna_next);
13038 ivna.vna_other = BYTE_GET (evna.vna_other);
13039 }
13040
13041 a_off += ivna.vna_next;
13042 }
13043 while (ivna.vna_other != data[cnt + j]
13044 && ivna.vna_next != 0);
13045
13046 if (ivna.vna_other == data[cnt + j])
13047 {
13048 ivna.vna_name = BYTE_GET (evna.vna_name);
13049
13050 if (ivna.vna_name >= string_sec->sh_size)
13051 name = invalid;
13052 else
13053 name = strtab + ivna.vna_name;
13054 break;
13055 }
13056
13057 offset += ivn.vn_next;
13058 }
13059 while (ivn.vn_next);
13060 }
13061
13062 if (data[cnt + j] != 0x8001
13063 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
13064 {
13065 Elf_Internal_Verdef ivd;
13066 Elf_External_Verdef evd;
13067 uint64_t offset;
13068
13069 offset = offset_from_vma
13070 (filedata,
13071 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
13072 sizeof evd);
13073
13074 do
13075 {
13076 if (get_data (&evd, filedata, offset, sizeof (evd), 1,
13077 _("version def")) == NULL)
13078 {
13079 ivd.vd_next = 0;
13080 /* PR 17531: file: 046-1082287-0.004. */
13081 ivd.vd_ndx = (data[cnt + j] & VERSYM_VERSION) + 1;
13082 break;
13083 }
13084 else
13085 {
13086 ivd.vd_next = BYTE_GET (evd.vd_next);
13087 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
13088 }
13089
13090 offset += ivd.vd_next;
13091 }
13092 while (ivd.vd_ndx != (data[cnt + j] & VERSYM_VERSION)
13093 && ivd.vd_next != 0);
13094
13095 if (ivd.vd_ndx == (data[cnt + j] & VERSYM_VERSION))
13096 {
13097 Elf_External_Verdaux evda;
13098 Elf_Internal_Verdaux ivda;
13099
13100 ivd.vd_aux = BYTE_GET (evd.vd_aux);
13101
13102 if (get_data (&evda, filedata,
13103 offset - ivd.vd_next + ivd.vd_aux,
13104 sizeof (evda), 1,
13105 _("version def aux")) == NULL)
13106 break;
13107
13108 ivda.vda_name = BYTE_GET (evda.vda_name);
13109
13110 if (ivda.vda_name >= string_sec->sh_size)
13111 name = invalid;
13112 else if (name != NULL && name != invalid)
13113 name = _("*both*");
13114 else
13115 name = strtab + ivda.vda_name;
13116 }
13117 }
13118 if (name != NULL)
13119 nn += printf ("(%s%-*s",
13120 name,
13121 12 - (int) strlen (name),
13122 ")");
13123
13124 if (nn < 18)
13125 printf ("%*c", 18 - nn, ' ');
13126 }
13127
13128 putchar ('\n');
13129 }
13130
13131 free (data);
13132 free (strtab);
13133 free (symbols);
13134 }
13135 break;
13136
13137 default:
13138 break;
13139 }
13140 }
13141
13142 if (! found)
13143 {
13144 if (filedata->is_separate)
13145 printf (_("\nNo version information found in linked file '%s'.\n"),
13146 filedata->file_name);
13147 else
13148 printf (_("\nNo version information found in this file.\n"));
13149 }
13150
13151 return true;
13152 }
13153
13154 static const char *
13155 get_symbol_binding (Filedata * filedata, unsigned int binding)
13156 {
13157 static char buff[64];
13158
13159 switch (binding)
13160 {
13161 case STB_LOCAL: return "LOCAL";
13162 case STB_GLOBAL: return "GLOBAL";
13163 case STB_WEAK: return "WEAK";
13164 default:
13165 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
13166 snprintf (buff, sizeof (buff), _("<processor specific>: %d"),
13167 binding);
13168 else if (binding >= STB_LOOS && binding <= STB_HIOS)
13169 {
13170 if (binding == STB_GNU_UNIQUE
13171 && filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU)
13172 return "UNIQUE";
13173 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), binding);
13174 }
13175 else
13176 snprintf (buff, sizeof (buff), _("<unknown>: %d"), binding);
13177 return buff;
13178 }
13179 }
13180
13181 static const char *
13182 get_symbol_type (Filedata * filedata, unsigned int type)
13183 {
13184 static char buff[64];
13185
13186 switch (type)
13187 {
13188 case STT_NOTYPE: return "NOTYPE";
13189 case STT_OBJECT: return "OBJECT";
13190 case STT_FUNC: return "FUNC";
13191 case STT_SECTION: return "SECTION";
13192 case STT_FILE: return "FILE";
13193 case STT_COMMON: return "COMMON";
13194 case STT_TLS: return "TLS";
13195 case STT_RELC: return "RELC";
13196 case STT_SRELC: return "SRELC";
13197 default:
13198 if (type >= STT_LOPROC && type <= STT_HIPROC)
13199 {
13200 if (filedata->file_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
13201 return "THUMB_FUNC";
13202
13203 if (filedata->file_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
13204 return "REGISTER";
13205
13206 if (filedata->file_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
13207 return "PARISC_MILLI";
13208
13209 snprintf (buff, sizeof (buff), _("<processor specific>: %d"), type);
13210 }
13211 else if (type >= STT_LOOS && type <= STT_HIOS)
13212 {
13213 if (filedata->file_header.e_machine == EM_PARISC)
13214 {
13215 if (type == STT_HP_OPAQUE)
13216 return "HP_OPAQUE";
13217 if (type == STT_HP_STUB)
13218 return "HP_STUB";
13219 }
13220
13221 if (type == STT_GNU_IFUNC
13222 && (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_GNU
13223 || filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_FREEBSD))
13224 return "IFUNC";
13225
13226 snprintf (buff, sizeof (buff), _("<OS specific>: %d"), type);
13227 }
13228 else
13229 snprintf (buff, sizeof (buff), _("<unknown>: %d"), type);
13230 return buff;
13231 }
13232 }
13233
13234 static const char *
13235 get_symbol_visibility (unsigned int visibility)
13236 {
13237 switch (visibility)
13238 {
13239 case STV_DEFAULT: return "DEFAULT";
13240 case STV_INTERNAL: return "INTERNAL";
13241 case STV_HIDDEN: return "HIDDEN";
13242 case STV_PROTECTED: return "PROTECTED";
13243 default:
13244 error (_("Unrecognized visibility value: %u\n"), visibility);
13245 return _("<unknown>");
13246 }
13247 }
13248
13249 static const char *
13250 get_alpha_symbol_other (unsigned int other)
13251 {
13252 switch (other)
13253 {
13254 case STO_ALPHA_NOPV: return "NOPV";
13255 case STO_ALPHA_STD_GPLOAD: return "STD GPLOAD";
13256 default:
13257 error (_("Unrecognized alpha specific other value: %u\n"), other);
13258 return _("<unknown>");
13259 }
13260 }
13261
13262 static const char *
13263 get_solaris_symbol_visibility (unsigned int visibility)
13264 {
13265 switch (visibility)
13266 {
13267 case 4: return "EXPORTED";
13268 case 5: return "SINGLETON";
13269 case 6: return "ELIMINATE";
13270 default: return get_symbol_visibility (visibility);
13271 }
13272 }
13273
13274 static const char *
13275 get_aarch64_symbol_other (unsigned int other)
13276 {
13277 static char buf[32];
13278
13279 if (other & STO_AARCH64_VARIANT_PCS)
13280 {
13281 other &= ~STO_AARCH64_VARIANT_PCS;
13282 if (other == 0)
13283 return "VARIANT_PCS";
13284 snprintf (buf, sizeof buf, "VARIANT_PCS | %x", other);
13285 return buf;
13286 }
13287 return NULL;
13288 }
13289
13290 static const char *
13291 get_mips_symbol_other (unsigned int other)
13292 {
13293 switch (other)
13294 {
13295 case STO_OPTIONAL: return "OPTIONAL";
13296 case STO_MIPS_PLT: return "MIPS PLT";
13297 case STO_MIPS_PIC: return "MIPS PIC";
13298 case STO_MICROMIPS: return "MICROMIPS";
13299 case STO_MICROMIPS | STO_MIPS_PIC: return "MICROMIPS, MIPS PIC";
13300 case STO_MIPS16: return "MIPS16";
13301 default: return NULL;
13302 }
13303 }
13304
13305 static const char *
13306 get_ia64_symbol_other (Filedata * filedata, unsigned int other)
13307 {
13308 if (is_ia64_vms (filedata))
13309 {
13310 static char res[32];
13311
13312 res[0] = 0;
13313
13314 /* Function types is for images and .STB files only. */
13315 switch (filedata->file_header.e_type)
13316 {
13317 case ET_DYN:
13318 case ET_EXEC:
13319 switch (VMS_ST_FUNC_TYPE (other))
13320 {
13321 case VMS_SFT_CODE_ADDR:
13322 strcat (res, " CA");
13323 break;
13324 case VMS_SFT_SYMV_IDX:
13325 strcat (res, " VEC");
13326 break;
13327 case VMS_SFT_FD:
13328 strcat (res, " FD");
13329 break;
13330 case VMS_SFT_RESERVE:
13331 strcat (res, " RSV");
13332 break;
13333 default:
13334 warn (_("Unrecognized IA64 VMS ST Function type: %d\n"),
13335 VMS_ST_FUNC_TYPE (other));
13336 strcat (res, " <unknown>");
13337 break;
13338 }
13339 break;
13340 default:
13341 break;
13342 }
13343 switch (VMS_ST_LINKAGE (other))
13344 {
13345 case VMS_STL_IGNORE:
13346 strcat (res, " IGN");
13347 break;
13348 case VMS_STL_RESERVE:
13349 strcat (res, " RSV");
13350 break;
13351 case VMS_STL_STD:
13352 strcat (res, " STD");
13353 break;
13354 case VMS_STL_LNK:
13355 strcat (res, " LNK");
13356 break;
13357 default:
13358 warn (_("Unrecognized IA64 VMS ST Linkage: %d\n"),
13359 VMS_ST_LINKAGE (other));
13360 strcat (res, " <unknown>");
13361 break;
13362 }
13363
13364 if (res[0] != 0)
13365 return res + 1;
13366 else
13367 return res;
13368 }
13369 return NULL;
13370 }
13371
13372 static const char *
13373 get_ppc64_symbol_other (unsigned int other)
13374 {
13375 if ((other & ~STO_PPC64_LOCAL_MASK) != 0)
13376 return NULL;
13377
13378 other >>= STO_PPC64_LOCAL_BIT;
13379 if (other <= 6)
13380 {
13381 static char buf[64];
13382 if (other >= 2)
13383 other = ppc64_decode_local_entry (other);
13384 snprintf (buf, sizeof buf, _("<localentry>: %d"), other);
13385 return buf;
13386 }
13387 return NULL;
13388 }
13389
13390 static const char *
13391 get_riscv_symbol_other (unsigned int other)
13392 {
13393 static char buf[32];
13394 buf[0] = 0;
13395
13396 if (other & STO_RISCV_VARIANT_CC)
13397 {
13398 strcat (buf, _(" VARIANT_CC"));
13399 other &= ~STO_RISCV_VARIANT_CC;
13400 }
13401
13402 if (other != 0)
13403 snprintf (buf, sizeof buf, " %x", other);
13404
13405
13406 if (buf[0] != 0)
13407 return buf + 1;
13408 else
13409 return buf;
13410 }
13411
13412 static const char *
13413 get_symbol_other (Filedata * filedata, unsigned int other)
13414 {
13415 const char * result = NULL;
13416 static char buff [64];
13417
13418 if (other == 0)
13419 return "";
13420
13421 switch (filedata->file_header.e_machine)
13422 {
13423 case EM_ALPHA:
13424 result = get_alpha_symbol_other (other);
13425 break;
13426 case EM_AARCH64:
13427 result = get_aarch64_symbol_other (other);
13428 break;
13429 case EM_MIPS:
13430 result = get_mips_symbol_other (other);
13431 break;
13432 case EM_IA_64:
13433 result = get_ia64_symbol_other (filedata, other);
13434 break;
13435 case EM_PPC64:
13436 result = get_ppc64_symbol_other (other);
13437 break;
13438 case EM_RISCV:
13439 result = get_riscv_symbol_other (other);
13440 break;
13441 default:
13442 result = NULL;
13443 break;
13444 }
13445
13446 if (result)
13447 return result;
13448
13449 snprintf (buff, sizeof buff, _("<other>: %x"), other);
13450 return buff;
13451 }
13452
13453 static const char *
13454 get_symbol_version_string (Filedata *filedata,
13455 bool is_dynsym,
13456 const char *strtab,
13457 size_t strtab_size,
13458 unsigned int si,
13459 Elf_Internal_Sym *psym,
13460 enum versioned_symbol_info *sym_info,
13461 unsigned short *vna_other)
13462 {
13463 unsigned char data[2];
13464 unsigned short vers_data;
13465 uint64_t offset;
13466 unsigned short max_vd_ndx;
13467
13468 if (!is_dynsym
13469 || filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)] == 0)
13470 return NULL;
13471
13472 offset = offset_from_vma (filedata,
13473 filedata->version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
13474 sizeof data + si * sizeof (vers_data));
13475
13476 if (get_data (&data, filedata, offset + si * sizeof (vers_data),
13477 sizeof (data), 1, _("version data")) == NULL)
13478 return NULL;
13479
13480 vers_data = byte_get (data, 2);
13481
13482 if ((vers_data & VERSYM_HIDDEN) == 0 && vers_data == 0)
13483 return NULL;
13484
13485 *sym_info = (vers_data & VERSYM_HIDDEN) != 0 ? symbol_hidden : symbol_public;
13486 max_vd_ndx = 0;
13487
13488 /* Usually we'd only see verdef for defined symbols, and verneed for
13489 undefined symbols. However, symbols defined by the linker in
13490 .dynbss for variables copied from a shared library in order to
13491 avoid text relocations are defined yet have verneed. We could
13492 use a heuristic to detect the special case, for example, check
13493 for verneed first on symbols defined in SHT_NOBITS sections, but
13494 it is simpler and more reliable to just look for both verdef and
13495 verneed. .dynbss might not be mapped to a SHT_NOBITS section. */
13496
13497 if (psym->st_shndx != SHN_UNDEF
13498 && vers_data != 0x8001
13499 && filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
13500 {
13501 Elf_Internal_Verdef ivd;
13502 Elf_Internal_Verdaux ivda;
13503 Elf_External_Verdaux evda;
13504 uint64_t off;
13505
13506 off = offset_from_vma (filedata,
13507 filedata->version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
13508 sizeof (Elf_External_Verdef));
13509
13510 do
13511 {
13512 Elf_External_Verdef evd;
13513
13514 if (get_data (&evd, filedata, off, sizeof (evd), 1,
13515 _("version def")) == NULL)
13516 {
13517 ivd.vd_ndx = 0;
13518 ivd.vd_aux = 0;
13519 ivd.vd_next = 0;
13520 ivd.vd_flags = 0;
13521 }
13522 else
13523 {
13524 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
13525 ivd.vd_aux = BYTE_GET (evd.vd_aux);
13526 ivd.vd_next = BYTE_GET (evd.vd_next);
13527 ivd.vd_flags = BYTE_GET (evd.vd_flags);
13528 }
13529
13530 if ((ivd.vd_ndx & VERSYM_VERSION) > max_vd_ndx)
13531 max_vd_ndx = ivd.vd_ndx & VERSYM_VERSION;
13532
13533 off += ivd.vd_next;
13534 }
13535 while (ivd.vd_ndx != (vers_data & VERSYM_VERSION) && ivd.vd_next != 0);
13536
13537 if (ivd.vd_ndx == (vers_data & VERSYM_VERSION))
13538 {
13539 if (ivd.vd_ndx == 1 && ivd.vd_flags == VER_FLG_BASE)
13540 return NULL;
13541
13542 off -= ivd.vd_next;
13543 off += ivd.vd_aux;
13544
13545 if (get_data (&evda, filedata, off, sizeof (evda), 1,
13546 _("version def aux")) != NULL)
13547 {
13548 ivda.vda_name = BYTE_GET (evda.vda_name);
13549
13550 if (psym->st_name != ivda.vda_name)
13551 return (ivda.vda_name < strtab_size
13552 ? strtab + ivda.vda_name : _("<corrupt>"));
13553 }
13554 }
13555 }
13556
13557 if (filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
13558 {
13559 Elf_External_Verneed evn;
13560 Elf_Internal_Verneed ivn;
13561 Elf_Internal_Vernaux ivna;
13562
13563 offset = offset_from_vma (filedata,
13564 filedata->version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
13565 sizeof evn);
13566 do
13567 {
13568 uint64_t vna_off;
13569
13570 if (get_data (&evn, filedata, offset, sizeof (evn), 1,
13571 _("version need")) == NULL)
13572 {
13573 ivna.vna_next = 0;
13574 ivna.vna_other = 0;
13575 ivna.vna_name = 0;
13576 break;
13577 }
13578
13579 ivn.vn_aux = BYTE_GET (evn.vn_aux);
13580 ivn.vn_next = BYTE_GET (evn.vn_next);
13581
13582 vna_off = offset + ivn.vn_aux;
13583
13584 do
13585 {
13586 Elf_External_Vernaux evna;
13587
13588 if (get_data (&evna, filedata, vna_off, sizeof (evna), 1,
13589 _("version need aux (3)")) == NULL)
13590 {
13591 ivna.vna_next = 0;
13592 ivna.vna_other = 0;
13593 ivna.vna_name = 0;
13594 }
13595 else
13596 {
13597 ivna.vna_other = BYTE_GET (evna.vna_other);
13598 ivna.vna_next = BYTE_GET (evna.vna_next);
13599 ivna.vna_name = BYTE_GET (evna.vna_name);
13600 }
13601
13602 vna_off += ivna.vna_next;
13603 }
13604 while (ivna.vna_other != vers_data && ivna.vna_next != 0);
13605
13606 if (ivna.vna_other == vers_data)
13607 break;
13608
13609 offset += ivn.vn_next;
13610 }
13611 while (ivn.vn_next != 0);
13612
13613 if (ivna.vna_other == vers_data)
13614 {
13615 *sym_info = symbol_undefined;
13616 *vna_other = ivna.vna_other;
13617 return (ivna.vna_name < strtab_size
13618 ? strtab + ivna.vna_name : _("<corrupt>"));
13619 }
13620 else if ((max_vd_ndx || (vers_data & VERSYM_VERSION) != 1)
13621 && (vers_data & VERSYM_VERSION) > max_vd_ndx)
13622 return _("<corrupt>");
13623 }
13624 return NULL;
13625 }
13626
13627 /* Display a symbol size on stdout. Format is based on --sym-base setting. */
13628
13629 static unsigned int
13630 print_symbol_size (uint64_t vma, int base)
13631 {
13632 switch (base)
13633 {
13634 case 8:
13635 return print_vma (vma, OCTAL_5);
13636
13637 case 10:
13638 return print_vma (vma, UNSIGNED_5);
13639
13640 case 16:
13641 return print_vma (vma, PREFIX_HEX_5);
13642
13643 case 0:
13644 default:
13645 return print_vma (vma, DEC_5);
13646 }
13647 }
13648
13649 /* Print information on a single symbol. */
13650
13651 static void
13652 print_symbol (Filedata * filedata,
13653 uint64_t symbol_index,
13654 Elf_Internal_Sym * symtab,
13655 Elf_Internal_Shdr * section,
13656 char * strtab,
13657 size_t strtab_size)
13658 {
13659 const char *version_string;
13660 enum versioned_symbol_info sym_info;
13661 unsigned short vna_other;
13662 const char * sstr;
13663 Elf_Internal_Sym *psym = symtab + symbol_index;
13664
13665 /* FIXME: We should have a table of field widths,
13666 rather than using hard coded constants. */
13667 printf ("%6" PRId64 ": ", symbol_index);
13668 print_vma (psym->st_value, LONG_HEX);
13669 putchar (' ');
13670 print_symbol_size (psym->st_size, sym_base);
13671 printf (" %-7s", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
13672 printf (" %-6s", get_symbol_binding (filedata, ELF_ST_BIND (psym->st_info)));
13673 if (filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_SOLARIS)
13674 printf (" %-7s", get_solaris_symbol_visibility (psym->st_other));
13675 else
13676 {
13677 unsigned int vis = ELF_ST_VISIBILITY (psym->st_other);
13678
13679 printf (" %-7s", get_symbol_visibility (vis));
13680
13681 /* Check to see if any other bits in the st_other field are set.
13682 FIXME: Displaying this information here disrupts the layout
13683 of the table being generated. */
13684 if (psym->st_other ^ vis)
13685 printf (" [%s] ", get_symbol_other (filedata, psym->st_other ^ vis));
13686 }
13687
13688 bool is_special;
13689
13690 sstr = printable_section_name_from_index (filedata, psym->st_shndx, & is_special);
13691
13692 /* Print the symbol's section index. If the index is special
13693 then print the index's name rather than its number. */
13694 if (is_special)
13695 {
13696 int printed;
13697
13698 /* Special case: If there are no section headers, and the printable
13699 name is "<section 0x...." then just display the section number
13700 as a decimal. This happens when objcopy --strip -section-headers
13701 is used. */
13702 if (filedata->file_header.e_shnum == 0 && startswith (sstr, "<section"))
13703 printed = printf (" %4d ", psym->st_shndx);
13704 else
13705 printed = printf (" %4s ", sstr);
13706
13707 if (extra_sym_info && printed < 16)
13708 printf ("%*s", 16 - printed, "");
13709 }
13710 else
13711 {
13712 printf (" %4u ", psym->st_shndx);
13713
13714 if (extra_sym_info)
13715 {
13716 /* Display the section name referenced by the section index. */
13717 int printed = printf ("(%s) ", sstr);
13718 if (printed < 10)
13719 printf ("%*s", 10 - printed, "");
13720 }
13721 }
13722
13723 /* Get the symbol's name. For section symbols without a
13724 specific name use the (already computed) section name. */
13725 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION
13726 && section_index_real (filedata, psym->st_shndx)
13727 && psym->st_name == 0)
13728 {
13729 ;
13730 }
13731 else
13732 {
13733 bool is_valid;
13734
13735 is_valid = valid_symbol_name (strtab, strtab_size, psym->st_name);
13736 sstr = is_valid ? strtab + psym->st_name : _("<corrupt>");
13737 }
13738
13739 version_string
13740 = get_symbol_version_string (filedata,
13741 (section == NULL
13742 || section->sh_type == SHT_DYNSYM),
13743 strtab, strtab_size, symbol_index,
13744 psym, &sym_info, &vna_other);
13745
13746 int len_avail = 21;
13747 if (! do_wide && version_string != NULL)
13748 {
13749 char buffer[16];
13750
13751 len_avail -= 1 + strlen (version_string);
13752
13753 if (sym_info == symbol_undefined)
13754 len_avail -= sprintf (buffer," (%d)", vna_other);
13755 else if (sym_info != symbol_hidden)
13756 len_avail -= 1;
13757 }
13758
13759 print_symbol_name (len_avail, sstr);
13760
13761 if (version_string)
13762 {
13763 if (sym_info == symbol_undefined)
13764 printf ("@%s (%d)", version_string, vna_other);
13765 else
13766 printf (sym_info == symbol_hidden ? "@%s" : "@@%s",
13767 version_string);
13768 }
13769
13770 putchar ('\n');
13771
13772 if (ELF_ST_BIND (psym->st_info) == STB_LOCAL
13773 && section != NULL
13774 && symbol_index >= section->sh_info
13775 /* Irix 5 and 6 MIPS binaries are known to ignore this requirement. */
13776 && filedata->file_header.e_machine != EM_MIPS
13777 /* Solaris binaries have been found to violate this requirement as
13778 well. Not sure if this is a bug or an ABI requirement. */
13779 && filedata->file_header.e_ident[EI_OSABI] != ELFOSABI_SOLARIS)
13780 warn (_("local symbol %" PRIu64 " found at index >= %s's sh_info value of %u\n"),
13781 symbol_index, printable_section_name (filedata, section), section->sh_info);
13782 }
13783
13784 static const char *
13785 get_lto_kind (unsigned int kind)
13786 {
13787 switch (kind)
13788 {
13789 case 0: return "DEF";
13790 case 1: return "WEAKDEF";
13791 case 2: return "UNDEF";
13792 case 3: return "WEAKUNDEF";
13793 case 4: return "COMMON";
13794 default:
13795 break;
13796 }
13797
13798 static char buffer[30];
13799 error (_("Unknown LTO symbol definition encountered: %u\n"), kind);
13800 sprintf (buffer, "<unknown: %u>", kind);
13801 return buffer;
13802 }
13803
13804 static const char *
13805 get_lto_visibility (unsigned int visibility)
13806 {
13807 switch (visibility)
13808 {
13809 case 0: return "DEFAULT";
13810 case 1: return "PROTECTED";
13811 case 2: return "INTERNAL";
13812 case 3: return "HIDDEN";
13813 default:
13814 break;
13815 }
13816
13817 static char buffer[30];
13818 error (_("Unknown LTO symbol visibility encountered: %u\n"), visibility);
13819 sprintf (buffer, "<unknown: %u>", visibility);
13820 return buffer;
13821 }
13822
13823 static const char *
13824 get_lto_sym_type (unsigned int sym_type)
13825 {
13826 switch (sym_type)
13827 {
13828 case 0: return "UNKNOWN";
13829 case 1: return "FUNCTION";
13830 case 2: return "VARIABLE";
13831 default:
13832 break;
13833 }
13834
13835 static char buffer[30];
13836 error (_("Unknown LTO symbol type encountered: %u\n"), sym_type);
13837 sprintf (buffer, "<unknown: %u>", sym_type);
13838 return buffer;
13839 }
13840
13841 /* Display an LTO format symbol table.
13842 FIXME: The format of LTO symbol tables is not formalized.
13843 So this code could need changing in the future. */
13844
13845 static bool
13846 display_lto_symtab (Filedata * filedata,
13847 Elf_Internal_Shdr * section)
13848 {
13849 if (section->sh_size == 0)
13850 {
13851 if (filedata->is_separate)
13852 printf (_("\nThe LTO Symbol table section '%s' in linked file '%s' is empty!\n"),
13853 printable_section_name (filedata, section),
13854 filedata->file_name);
13855 else
13856 printf (_("\nLTO Symbol table '%s' is empty!\n"),
13857 printable_section_name (filedata, section));
13858
13859 return true;
13860 }
13861
13862 if (section->sh_size > filedata->file_size)
13863 {
13864 error (_("Section %s has an invalid sh_size of %#" PRIx64 "\n"),
13865 printable_section_name (filedata, section),
13866 section->sh_size);
13867 return false;
13868 }
13869
13870 void * alloced_data = get_data (NULL, filedata, section->sh_offset,
13871 section->sh_size, 1, _("LTO symbols"));
13872 if (alloced_data == NULL)
13873 return false;
13874
13875 /* Look for extended data for the symbol table. */
13876 Elf_Internal_Shdr * ext = NULL;
13877 void * ext_data_orig = NULL;
13878 char * ext_data = NULL;
13879 char * ext_data_end = NULL;
13880 char * ext_name = NULL;
13881
13882 if (asprintf (& ext_name, ".gnu.lto_.ext_symtab.%s",
13883 (section_name (filedata, section)
13884 + sizeof (".gnu.lto_.symtab.") - 1)) > 0
13885 && ext_name != NULL /* Paranoia. */
13886 && (ext = find_section (filedata, ext_name)) != NULL)
13887 {
13888 if (ext->sh_size < 3)
13889 error (_("LTO Symbol extension table '%s' is empty!\n"),
13890 printable_section_name (filedata, ext));
13891 else
13892 {
13893 ext_data_orig = ext_data = get_data (NULL, filedata, ext->sh_offset,
13894 ext->sh_size, 1,
13895 _("LTO ext symbol data"));
13896 if (ext_data != NULL)
13897 {
13898 ext_data_end = ext_data + ext->sh_size;
13899 if (* ext_data++ != 1)
13900 error (_("Unexpected version number in symbol extension table\n"));
13901 }
13902 }
13903 }
13904
13905 const unsigned char * data = (const unsigned char *) alloced_data;
13906 const unsigned char * end = data + section->sh_size;
13907
13908 if (filedata->is_separate)
13909 printf (_("\nIn linked file '%s': "), filedata->file_name);
13910 else
13911 printf ("\n");
13912
13913 if (ext_data_orig != NULL)
13914 {
13915 if (do_wide)
13916 printf (_("LTO Symbol table '%s' and extension table '%s' contain:\n"),
13917 printable_section_name (filedata, section),
13918 printable_section_name (filedata, ext));
13919 else
13920 {
13921 printf (_("LTO Symbol table '%s'\n"),
13922 printable_section_name (filedata, section));
13923 printf (_(" and extension table '%s' contain:\n"),
13924 printable_section_name (filedata, ext));
13925 }
13926 }
13927 else
13928 printf (_("LTO Symbol table '%s' contains:\n"),
13929 printable_section_name (filedata, section));
13930
13931 /* FIXME: Add a wide version. */
13932 if (ext_data_orig != NULL)
13933 printf (_(" Comdat_Key Kind Visibility Size Slot Type Section Name\n"));
13934 else
13935 printf (_(" Comdat_Key Kind Visibility Size Slot Name\n"));
13936
13937 /* FIXME: We do not handle style prefixes. */
13938
13939 while (data < end)
13940 {
13941 const unsigned char * sym_name = data;
13942 data += strnlen ((const char *) sym_name, end - data) + 1;
13943 if (data >= end)
13944 goto fail;
13945
13946 const unsigned char * comdat_key = data;
13947 data += strnlen ((const char *) comdat_key, end - data) + 1;
13948 if (data >= end)
13949 goto fail;
13950
13951 if (data + 2 + 8 + 4 > end)
13952 goto fail;
13953
13954 unsigned int kind = *data++;
13955 unsigned int visibility = *data++;
13956
13957 uint64_t size = byte_get (data, 8);
13958 data += 8;
13959
13960 uint64_t slot = byte_get (data, 4);
13961 data += 4;
13962
13963 if (ext_data != NULL)
13964 {
13965 if (ext_data < (ext_data_end - 1))
13966 {
13967 unsigned int sym_type = * ext_data ++;
13968 unsigned int sec_kind = * ext_data ++;
13969
13970 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " %9s %08x _",
13971 * comdat_key == 0 ? "-" : (char *) comdat_key,
13972 get_lto_kind (kind),
13973 get_lto_visibility (visibility),
13974 size,
13975 slot,
13976 get_lto_sym_type (sym_type),
13977 sec_kind);
13978 print_symbol_name (6, (const char *) sym_name);
13979 }
13980 else
13981 {
13982 error (_("Ran out of LTO symbol extension data\n"));
13983 ext_data = NULL;
13984 /* FIXME: return FAIL result ? */
13985 }
13986 }
13987 else
13988 {
13989 printf (" %10s %10s %11s %08" PRIx64 " %08" PRIx64 " _",
13990 * comdat_key == 0 ? "-" : (char *) comdat_key,
13991 get_lto_kind (kind),
13992 get_lto_visibility (visibility),
13993 size,
13994 slot);
13995 print_symbol_name (21, (const char *) sym_name);
13996 }
13997 putchar ('\n');
13998 }
13999
14000 if (ext_data != NULL && ext_data < ext_data_end)
14001 {
14002 error (_("Data remains in the LTO symbol extension table\n"));
14003 goto fail;
14004 }
14005
14006 free (alloced_data);
14007 free (ext_data_orig);
14008 free (ext_name);
14009 return true;
14010
14011 fail:
14012 error (_("Buffer overrun encountered whilst decoding LTO symbol table\n"));
14013 free (alloced_data);
14014 free (ext_data_orig);
14015 free (ext_name);
14016 return false;
14017 }
14018
14019 /* Display LTO symbol tables. */
14020
14021 static bool
14022 process_lto_symbol_tables (Filedata * filedata)
14023 {
14024 Elf_Internal_Shdr * section;
14025 unsigned int i;
14026 bool res = true;
14027
14028 if (!do_lto_syms)
14029 return true;
14030
14031 if (filedata->section_headers == NULL)
14032 return true;
14033
14034 for (i = 0, section = filedata->section_headers;
14035 i < filedata->file_header.e_shnum;
14036 i++, section++)
14037 if (section_name_valid (filedata, section)
14038 && startswith (section_name (filedata, section), ".gnu.lto_.symtab."))
14039 res &= display_lto_symtab (filedata, section);
14040
14041 return res;
14042 }
14043
14044 static void
14045 print_symbol_table_heading (void)
14046 {
14047 /* FIXME: We should store the size of each field in the display in a table and
14048 then use the values inside print_symbol(), instead of that function using
14049 hard coded constants. */
14050 if (is_32bit_elf)
14051 {
14052 if (extra_sym_info)
14053 {
14054 printf (_(" Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]\n"));
14055 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |---8--| |----13.....| |........... */
14056 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 (.text) get_sections */
14057 }
14058 else if (do_wide)
14059 {
14060 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
14061 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |-4| |........... */
14062 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 get_sections */
14063 }
14064 else
14065 {
14066 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
14067 /* |--6-|: |--8---| |-5-| |--7--| |-6--| |--7--| |-4| |------------29-------------| */
14068 /* eg: 5: 00000000 14 FUNC LOCAL DEFAULT 1 get_sections */
14069 }
14070 }
14071 else
14072 {
14073 if (extra_sym_info)
14074 {
14075 printf (_(" Num: Value Size Type Bind Vis+Other Ndx(SecName) Name [+ Version Info]\n"));
14076 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-------14---| |..... */
14077 /* eg: 2: 0000000000000000 0 FUNC LOCAL DEFAULT 1 (.text) .very_long_function_name */
14078
14079 }
14080 else if (do_wide)
14081 {
14082 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
14083 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-4| |........... */
14084 /* eg: 5: 0000000000000000 14 FUNC LOCAL DEFAULT 1 very_long_function_name */
14085 }
14086 else
14087 {
14088 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
14089 /* |--6-|: |------16------| |-5-| |--7--| |-6--| |--7--| |-4| |--------21---------| */
14090 /* eg: 5: 0000000000000000 14 FUNC LOCAL DEFAULT 1 very_long_functi[...] */
14091 }
14092 }
14093 }
14094
14095 /* Dump the symbol table. */
14096
14097 static bool
14098 process_symbol_table (Filedata * filedata)
14099 {
14100 Elf_Internal_Shdr * section;
14101
14102 if (!do_syms && !do_dyn_syms && !do_histogram)
14103 return true;
14104
14105 if ((filedata->dynamic_info[DT_HASH] || filedata->dynamic_info_DT_GNU_HASH)
14106 && do_syms
14107 && do_using_dynamic
14108 && filedata->dynamic_strings != NULL
14109 && filedata->dynamic_symbols != NULL)
14110 {
14111 uint64_t si;
14112
14113 if (filedata->is_separate)
14114 {
14115 printf (ngettext ("\nIn linked file '%s' the dynamic symbol table"
14116 " contains %" PRIu64 " entry:\n",
14117 "\nIn linked file '%s' the dynamic symbol table"
14118 " contains %" PRIu64 " entries:\n",
14119 filedata->num_dynamic_syms),
14120 filedata->file_name,
14121 filedata->num_dynamic_syms);
14122 }
14123 else
14124 {
14125 printf (ngettext ("\nSymbol table for image contains %" PRIu64
14126 " entry:\n",
14127 "\nSymbol table for image contains %" PRIu64
14128 " entries:\n",
14129 filedata->num_dynamic_syms),
14130 filedata->num_dynamic_syms);
14131 }
14132
14133 print_symbol_table_heading ();
14134
14135 for (si = 0; si < filedata->num_dynamic_syms; si++)
14136 print_symbol (filedata, si, filedata->dynamic_symbols, NULL,
14137 filedata->dynamic_strings,
14138 filedata->dynamic_strings_length);
14139 }
14140 else if ((do_dyn_syms || (do_syms && !do_using_dynamic))
14141 && filedata->section_headers != NULL)
14142 {
14143 unsigned int i;
14144
14145 for (i = 0, section = filedata->section_headers;
14146 i < filedata->file_header.e_shnum;
14147 i++, section++)
14148 {
14149 char * strtab = NULL;
14150 uint64_t strtab_size = 0;
14151 Elf_Internal_Sym * symtab;
14152 uint64_t si, num_syms;
14153
14154 if ((section->sh_type != SHT_SYMTAB
14155 && section->sh_type != SHT_DYNSYM)
14156 || (!do_syms
14157 && section->sh_type == SHT_SYMTAB))
14158 continue;
14159
14160 if (section->sh_entsize == 0)
14161 {
14162 printf (_("\nSymbol table '%s' has a sh_entsize of zero!\n"),
14163 printable_section_name (filedata, section));
14164 continue;
14165 }
14166
14167 num_syms = section->sh_size / section->sh_entsize;
14168
14169 if (filedata->is_separate)
14170 printf (ngettext ("\nIn linked file '%s' symbol section '%s'"
14171 " contains %" PRIu64 " entry:\n",
14172 "\nIn linked file '%s' symbol section '%s'"
14173 " contains %" PRIu64 " entries:\n",
14174 num_syms),
14175 filedata->file_name,
14176 printable_section_name (filedata, section),
14177 num_syms);
14178 else
14179 printf (ngettext ("\nSymbol table '%s' contains %" PRIu64
14180 " entry:\n",
14181 "\nSymbol table '%s' contains %" PRIu64
14182 " entries:\n",
14183 num_syms),
14184 printable_section_name (filedata, section),
14185 num_syms);
14186
14187 print_symbol_table_heading ();
14188
14189 symtab = get_elf_symbols (filedata, section, & num_syms);
14190 if (symtab == NULL)
14191 continue;
14192
14193 if (section->sh_link == filedata->file_header.e_shstrndx)
14194 {
14195 strtab = filedata->string_table;
14196 strtab_size = filedata->string_table_length;
14197 }
14198 else if (section->sh_link < filedata->file_header.e_shnum)
14199 {
14200 Elf_Internal_Shdr * string_sec;
14201
14202 string_sec = filedata->section_headers + section->sh_link;
14203
14204 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset,
14205 1, string_sec->sh_size,
14206 _("string table"));
14207 strtab_size = strtab != NULL ? string_sec->sh_size : 0;
14208 }
14209
14210 for (si = 0; si < num_syms; si++)
14211 print_symbol (filedata, si, symtab, section,
14212 strtab, strtab_size);
14213
14214 free (symtab);
14215 if (strtab != filedata->string_table)
14216 free (strtab);
14217 }
14218 }
14219 else if (do_syms)
14220 printf
14221 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
14222
14223 if (do_histogram && filedata->buckets != NULL)
14224 {
14225 uint64_t *lengths;
14226 uint64_t *counts;
14227 uint64_t hn;
14228 uint64_t si;
14229 uint64_t maxlength = 0;
14230 uint64_t nzero_counts = 0;
14231 uint64_t nsyms = 0;
14232 char *visited;
14233
14234 printf (ngettext ("\nHistogram for bucket list length "
14235 "(total of %" PRIu64 " bucket):\n",
14236 "\nHistogram for bucket list length "
14237 "(total of %" PRIu64 " buckets):\n",
14238 filedata->nbuckets),
14239 filedata->nbuckets);
14240
14241 lengths = calloc (filedata->nbuckets, sizeof (*lengths));
14242 if (lengths == NULL)
14243 {
14244 error (_("Out of memory allocating space for histogram buckets\n"));
14245 goto err_out;
14246 }
14247 visited = xcmalloc (filedata->nchains, 1);
14248 memset (visited, 0, filedata->nchains);
14249
14250 printf (_(" Length Number %% of total Coverage\n"));
14251 for (hn = 0; hn < filedata->nbuckets; ++hn)
14252 {
14253 for (si = filedata->buckets[hn]; si > 0; si = filedata->chains[si])
14254 {
14255 ++nsyms;
14256 if (maxlength < ++lengths[hn])
14257 ++maxlength;
14258 if (si >= filedata->nchains || visited[si])
14259 {
14260 error (_("histogram chain is corrupt\n"));
14261 break;
14262 }
14263 visited[si] = 1;
14264 }
14265 }
14266 free (visited);
14267
14268 counts = calloc (maxlength + 1, sizeof (*counts));
14269 if (counts == NULL)
14270 {
14271 free (lengths);
14272 error (_("Out of memory allocating space for histogram counts\n"));
14273 goto err_out;
14274 }
14275
14276 for (hn = 0; hn < filedata->nbuckets; ++hn)
14277 ++counts[lengths[hn]];
14278
14279 if (filedata->nbuckets > 0)
14280 {
14281 uint64_t i;
14282 printf (" 0 %-10" PRIu64 " (%5.1f%%)\n",
14283 counts[0], (counts[0] * 100.0) / filedata->nbuckets);
14284 for (i = 1; i <= maxlength; ++i)
14285 {
14286 nzero_counts += counts[i] * i;
14287 printf ("%7" PRIu64 " %-10" PRIu64 " (%5.1f%%) %5.1f%%\n",
14288 i, counts[i], (counts[i] * 100.0) / filedata->nbuckets,
14289 (nzero_counts * 100.0) / nsyms);
14290 }
14291 }
14292
14293 free (counts);
14294 free (lengths);
14295 }
14296
14297 free (filedata->buckets);
14298 filedata->buckets = NULL;
14299 filedata->nbuckets = 0;
14300 free (filedata->chains);
14301 filedata->chains = NULL;
14302
14303 if (do_histogram && filedata->gnubuckets != NULL)
14304 {
14305 uint64_t *lengths;
14306 uint64_t *counts;
14307 uint64_t hn;
14308 uint64_t maxlength = 0;
14309 uint64_t nzero_counts = 0;
14310 uint64_t nsyms = 0;
14311
14312 printf (ngettext ("\nHistogram for `%s' bucket list length "
14313 "(total of %" PRIu64 " bucket):\n",
14314 "\nHistogram for `%s' bucket list length "
14315 "(total of %" PRIu64 " buckets):\n",
14316 filedata->ngnubuckets),
14317 GNU_HASH_SECTION_NAME (filedata),
14318 filedata->ngnubuckets);
14319
14320 lengths = calloc (filedata->ngnubuckets, sizeof (*lengths));
14321 if (lengths == NULL)
14322 {
14323 error (_("Out of memory allocating space for gnu histogram buckets\n"));
14324 goto err_out;
14325 }
14326
14327 printf (_(" Length Number %% of total Coverage\n"));
14328
14329 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
14330 if (filedata->gnubuckets[hn] != 0)
14331 {
14332 uint64_t off, length = 1;
14333
14334 for (off = filedata->gnubuckets[hn] - filedata->gnusymidx;
14335 /* PR 17531 file: 010-77222-0.004. */
14336 off < filedata->ngnuchains
14337 && (filedata->gnuchains[off] & 1) == 0;
14338 ++off)
14339 ++length;
14340 lengths[hn] = length;
14341 if (length > maxlength)
14342 maxlength = length;
14343 nsyms += length;
14344 }
14345
14346 counts = calloc (maxlength + 1, sizeof (*counts));
14347 if (counts == NULL)
14348 {
14349 free (lengths);
14350 error (_("Out of memory allocating space for gnu histogram counts\n"));
14351 goto err_out;
14352 }
14353
14354 for (hn = 0; hn < filedata->ngnubuckets; ++hn)
14355 ++counts[lengths[hn]];
14356
14357 if (filedata->ngnubuckets > 0)
14358 {
14359 uint64_t j;
14360 printf (" 0 %-10" PRIu64 " (%5.1f%%)\n",
14361 counts[0], (counts[0] * 100.0) / filedata->ngnubuckets);
14362 for (j = 1; j <= maxlength; ++j)
14363 {
14364 nzero_counts += counts[j] * j;
14365 printf ("%7" PRIu64 " %-10" PRIu64 " (%5.1f%%) %5.1f%%\n",
14366 j, counts[j], (counts[j] * 100.0) / filedata->ngnubuckets,
14367 (nzero_counts * 100.0) / nsyms);
14368 }
14369 }
14370
14371 free (counts);
14372 free (lengths);
14373 }
14374 free (filedata->gnubuckets);
14375 filedata->gnubuckets = NULL;
14376 filedata->ngnubuckets = 0;
14377 free (filedata->gnuchains);
14378 filedata->gnuchains = NULL;
14379 filedata->ngnuchains = 0;
14380 free (filedata->mipsxlat);
14381 filedata->mipsxlat = NULL;
14382 return true;
14383
14384 err_out:
14385 free (filedata->gnubuckets);
14386 filedata->gnubuckets = NULL;
14387 filedata->ngnubuckets = 0;
14388 free (filedata->gnuchains);
14389 filedata->gnuchains = NULL;
14390 filedata->ngnuchains = 0;
14391 free (filedata->mipsxlat);
14392 filedata->mipsxlat = NULL;
14393 free (filedata->buckets);
14394 filedata->buckets = NULL;
14395 filedata->nbuckets = 0;
14396 free (filedata->chains);
14397 filedata->chains = NULL;
14398 return false;
14399 }
14400
14401 static bool
14402 process_syminfo (Filedata * filedata)
14403 {
14404 unsigned int i;
14405
14406 if (filedata->dynamic_syminfo == NULL
14407 || !do_dynamic)
14408 /* No syminfo, this is ok. */
14409 return true;
14410
14411 /* There better should be a dynamic symbol section. */
14412 if (filedata->dynamic_symbols == NULL || filedata->dynamic_strings == NULL)
14413 return false;
14414
14415 if (filedata->is_separate)
14416 printf (ngettext ("\nIn linked file '%s: the dynamic info segment at offset %#" PRIx64 " contains %d entry:\n",
14417 "\nIn linked file '%s: the dynamic info segment at offset %#" PRIx64 " contains %d entries:\n",
14418 filedata->dynamic_syminfo_nent),
14419 filedata->file_name,
14420 filedata->dynamic_syminfo_offset,
14421 filedata->dynamic_syminfo_nent);
14422 else
14423 printf (ngettext ("\nDynamic info segment at offset %#" PRIx64
14424 " contains %d entry:\n",
14425 "\nDynamic info segment at offset %#" PRIx64
14426 " contains %d entries:\n",
14427 filedata->dynamic_syminfo_nent),
14428 filedata->dynamic_syminfo_offset,
14429 filedata->dynamic_syminfo_nent);
14430
14431 printf (_(" Num: Name BoundTo Flags\n"));
14432 for (i = 0; i < filedata->dynamic_syminfo_nent; ++i)
14433 {
14434 unsigned short int flags = filedata->dynamic_syminfo[i].si_flags;
14435
14436 printf ("%4d: ", i);
14437 if (i >= filedata->num_dynamic_syms)
14438 printf (_("<corrupt index>"));
14439 else if (valid_dynamic_name (filedata, filedata->dynamic_symbols[i].st_name))
14440 print_symbol_name (30, get_dynamic_name (filedata,
14441 filedata->dynamic_symbols[i].st_name));
14442 else
14443 printf (_("<corrupt: %19ld>"), filedata->dynamic_symbols[i].st_name);
14444 putchar (' ');
14445
14446 switch (filedata->dynamic_syminfo[i].si_boundto)
14447 {
14448 case SYMINFO_BT_SELF:
14449 fputs ("SELF ", stdout);
14450 break;
14451 case SYMINFO_BT_PARENT:
14452 fputs ("PARENT ", stdout);
14453 break;
14454 default:
14455 if (filedata->dynamic_syminfo[i].si_boundto > 0
14456 && filedata->dynamic_syminfo[i].si_boundto < filedata->dynamic_nent
14457 && valid_dynamic_name (filedata,
14458 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val))
14459 {
14460 print_symbol_name (10, get_dynamic_name (filedata,
14461 filedata->dynamic_section[filedata->dynamic_syminfo[i].si_boundto].d_un.d_val));
14462 putchar (' ' );
14463 }
14464 else
14465 printf ("%-10d ", filedata->dynamic_syminfo[i].si_boundto);
14466 break;
14467 }
14468
14469 if (flags & SYMINFO_FLG_DIRECT)
14470 printf (" DIRECT");
14471 if (flags & SYMINFO_FLG_PASSTHRU)
14472 printf (" PASSTHRU");
14473 if (flags & SYMINFO_FLG_COPY)
14474 printf (" COPY");
14475 if (flags & SYMINFO_FLG_LAZYLOAD)
14476 printf (" LAZYLOAD");
14477
14478 puts ("");
14479 }
14480
14481 return true;
14482 }
14483
14484 /* A macro which evaluates to TRUE if the region ADDR .. ADDR + NELEM
14485 is contained by the region START .. END. The types of ADDR, START
14486 and END should all be the same. Note both ADDR + NELEM and END
14487 point to just beyond the end of the regions that are being tested. */
14488 #define IN_RANGE(START,END,ADDR,NELEM) \
14489 (((ADDR) >= (START)) && ((ADDR) < (END)) && ((ADDR) + (NELEM) <= (END)))
14490
14491 /* Check to see if the given reloc needs to be handled in a target specific
14492 manner. If so then process the reloc and return TRUE otherwise return
14493 FALSE.
14494
14495 If called with reloc == NULL, then this is a signal that reloc processing
14496 for the current section has finished, and any saved state should be
14497 discarded. */
14498
14499 static bool
14500 target_specific_reloc_handling (Filedata *filedata,
14501 Elf_Internal_Rela *reloc,
14502 unsigned char *start,
14503 unsigned char *end,
14504 Elf_Internal_Sym *symtab,
14505 uint64_t num_syms)
14506 {
14507 unsigned int reloc_type = 0;
14508 uint64_t sym_index = 0;
14509
14510 if (reloc)
14511 {
14512 reloc_type = get_reloc_type (filedata, reloc->r_info);
14513 sym_index = get_reloc_symindex (reloc->r_info);
14514 }
14515
14516 switch (filedata->file_header.e_machine)
14517 {
14518 case EM_LOONGARCH:
14519 {
14520 switch (reloc_type)
14521 {
14522 /* For .uleb128 .LFE1-.LFB1, loongarch write 0 to object file
14523 at assembly time. */
14524 case 107: /* R_LARCH_ADD_ULEB128. */
14525 case 108: /* R_LARCH_SUB_ULEB128. */
14526 {
14527 uint64_t value = 0;
14528 unsigned int reloc_size = 0;
14529 int leb_ret = 0;
14530
14531 if (reloc->r_offset < (size_t) (end - start))
14532 value = read_leb128 (start + reloc->r_offset, end, false,
14533 &reloc_size, &leb_ret);
14534 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
14535 error (_("LoongArch ULEB128 field at 0x%lx contains invalid "
14536 "ULEB128 value\n"),
14537 (long) reloc->r_offset);
14538
14539 else if (sym_index >= num_syms)
14540 error (_("%s reloc contains invalid symbol index "
14541 "%" PRIu64 "\n"),
14542 (reloc_type == 107
14543 ? "R_LARCH_ADD_ULEB128"
14544 : "R_LARCH_SUB_ULEB128"),
14545 sym_index);
14546 else
14547 {
14548 if (reloc_type == 107)
14549 value += reloc->r_addend + symtab[sym_index].st_value;
14550 else
14551 value -= reloc->r_addend + symtab[sym_index].st_value;
14552
14553 /* Write uleb128 value to p. */
14554 bfd_byte *p = start + reloc->r_offset;
14555 do
14556 {
14557 bfd_byte c = value & 0x7f;
14558 value >>= 7;
14559 if (--reloc_size != 0)
14560 c |= 0x80;
14561 *p++ = c;
14562 }
14563 while (reloc_size);
14564 }
14565
14566 return true;
14567 }
14568 }
14569 break;
14570 }
14571
14572 case EM_MSP430:
14573 case EM_MSP430_OLD:
14574 {
14575 static Elf_Internal_Sym * saved_sym = NULL;
14576
14577 if (reloc == NULL)
14578 {
14579 saved_sym = NULL;
14580 return true;
14581 }
14582
14583 switch (reloc_type)
14584 {
14585 case 10: /* R_MSP430_SYM_DIFF */
14586 case 12: /* R_MSP430_GNU_SUB_ULEB128 */
14587 if (uses_msp430x_relocs (filedata))
14588 break;
14589 /* Fall through. */
14590 case 21: /* R_MSP430X_SYM_DIFF */
14591 case 23: /* R_MSP430X_GNU_SUB_ULEB128 */
14592 /* PR 21139. */
14593 if (sym_index >= num_syms)
14594 error (_("%s reloc contains invalid symbol index "
14595 "%" PRIu64 "\n"), "MSP430 SYM_DIFF", sym_index);
14596 else
14597 saved_sym = symtab + sym_index;
14598 return true;
14599
14600 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
14601 case 3: /* R_MSP430_16 or R_MSP430_ABS8 */
14602 goto handle_sym_diff;
14603
14604 case 5: /* R_MSP430_16_BYTE */
14605 case 9: /* R_MSP430_8 */
14606 case 11: /* R_MSP430_GNU_SET_ULEB128 */
14607 if (uses_msp430x_relocs (filedata))
14608 break;
14609 goto handle_sym_diff;
14610
14611 case 2: /* R_MSP430_ABS16 */
14612 case 15: /* R_MSP430X_ABS16 */
14613 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
14614 if (! uses_msp430x_relocs (filedata))
14615 break;
14616 goto handle_sym_diff;
14617
14618 handle_sym_diff:
14619 if (saved_sym != NULL)
14620 {
14621 uint64_t value;
14622 unsigned int reloc_size = 0;
14623 int leb_ret = 0;
14624 switch (reloc_type)
14625 {
14626 case 1: /* R_MSP430_32 or R_MSP430_ABS32 */
14627 reloc_size = 4;
14628 break;
14629 case 11: /* R_MSP430_GNU_SET_ULEB128 */
14630 case 22: /* R_MSP430X_GNU_SET_ULEB128 */
14631 if (reloc->r_offset < (size_t) (end - start))
14632 read_leb128 (start + reloc->r_offset, end, false,
14633 &reloc_size, &leb_ret);
14634 break;
14635 default:
14636 reloc_size = 2;
14637 break;
14638 }
14639
14640 if (leb_ret != 0 || reloc_size == 0 || reloc_size > 8)
14641 error (_("MSP430 ULEB128 field at %#" PRIx64
14642 " contains invalid ULEB128 value\n"),
14643 reloc->r_offset);
14644 else if (sym_index >= num_syms)
14645 error (_("%s reloc contains invalid symbol index "
14646 "%" PRIu64 "\n"), "MSP430", sym_index);
14647 else
14648 {
14649 value = reloc->r_addend + (symtab[sym_index].st_value
14650 - saved_sym->st_value);
14651
14652 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14653 byte_put (start + reloc->r_offset, value, reloc_size);
14654 else
14655 /* PR 21137 */
14656 error (_("MSP430 sym diff reloc contains invalid offset: "
14657 "%#" PRIx64 "\n"),
14658 reloc->r_offset);
14659 }
14660
14661 saved_sym = NULL;
14662 return true;
14663 }
14664 break;
14665
14666 default:
14667 if (saved_sym != NULL)
14668 error (_("Unhandled MSP430 reloc type found after SYM_DIFF reloc\n"));
14669 break;
14670 }
14671 break;
14672 }
14673
14674 case EM_MN10300:
14675 case EM_CYGNUS_MN10300:
14676 {
14677 static Elf_Internal_Sym * saved_sym = NULL;
14678
14679 if (reloc == NULL)
14680 {
14681 saved_sym = NULL;
14682 return true;
14683 }
14684
14685 switch (reloc_type)
14686 {
14687 case 34: /* R_MN10300_ALIGN */
14688 return true;
14689 case 33: /* R_MN10300_SYM_DIFF */
14690 if (sym_index >= num_syms)
14691 error (_("%s reloc contains invalid symbol index "
14692 "%" PRIu64 "\n"), "MN10300_SYM_DIFF", sym_index);
14693 else
14694 saved_sym = symtab + sym_index;
14695 return true;
14696
14697 case 1: /* R_MN10300_32 */
14698 case 2: /* R_MN10300_16 */
14699 if (saved_sym != NULL)
14700 {
14701 int reloc_size = reloc_type == 1 ? 4 : 2;
14702 uint64_t value;
14703
14704 if (sym_index >= num_syms)
14705 error (_("%s reloc contains invalid symbol index "
14706 "%" PRIu64 "\n"), "MN10300", sym_index);
14707 else
14708 {
14709 value = reloc->r_addend + (symtab[sym_index].st_value
14710 - saved_sym->st_value);
14711
14712 if (IN_RANGE (start, end, start + reloc->r_offset, reloc_size))
14713 byte_put (start + reloc->r_offset, value, reloc_size);
14714 else
14715 error (_("MN10300 sym diff reloc contains invalid offset:"
14716 " %#" PRIx64 "\n"),
14717 reloc->r_offset);
14718 }
14719
14720 saved_sym = NULL;
14721 return true;
14722 }
14723 break;
14724 default:
14725 if (saved_sym != NULL)
14726 error (_("Unhandled MN10300 reloc type found after SYM_DIFF reloc\n"));
14727 break;
14728 }
14729 break;
14730 }
14731
14732 case EM_RL78:
14733 {
14734 static uint64_t saved_sym1 = 0;
14735 static uint64_t saved_sym2 = 0;
14736 static uint64_t value;
14737
14738 if (reloc == NULL)
14739 {
14740 saved_sym1 = saved_sym2 = 0;
14741 return true;
14742 }
14743
14744 switch (reloc_type)
14745 {
14746 case 0x80: /* R_RL78_SYM. */
14747 saved_sym1 = saved_sym2;
14748 if (sym_index >= num_syms)
14749 error (_("%s reloc contains invalid symbol index "
14750 "%" PRIu64 "\n"), "RL78_SYM", sym_index);
14751 else
14752 {
14753 saved_sym2 = symtab[sym_index].st_value;
14754 saved_sym2 += reloc->r_addend;
14755 }
14756 return true;
14757
14758 case 0x83: /* R_RL78_OPsub. */
14759 value = saved_sym1 - saved_sym2;
14760 saved_sym2 = saved_sym1 = 0;
14761 return true;
14762 break;
14763
14764 case 0x41: /* R_RL78_ABS32. */
14765 if (IN_RANGE (start, end, start + reloc->r_offset, 4))
14766 byte_put (start + reloc->r_offset, value, 4);
14767 else
14768 error (_("RL78 sym diff reloc contains invalid offset: "
14769 "%#" PRIx64 "\n"),
14770 reloc->r_offset);
14771 value = 0;
14772 return true;
14773
14774 case 0x43: /* R_RL78_ABS16. */
14775 if (IN_RANGE (start, end, start + reloc->r_offset, 2))
14776 byte_put (start + reloc->r_offset, value, 2);
14777 else
14778 error (_("RL78 sym diff reloc contains invalid offset: "
14779 "%#" PRIx64 "\n"),
14780 reloc->r_offset);
14781 value = 0;
14782 return true;
14783
14784 default:
14785 break;
14786 }
14787 break;
14788 }
14789 }
14790
14791 return false;
14792 }
14793
14794 /* Returns TRUE iff RELOC_TYPE is a 32-bit absolute RELA relocation used in
14795 DWARF debug sections. This is a target specific test. Note - we do not
14796 go through the whole including-target-headers-multiple-times route, (as
14797 we have already done with <elf/h8.h>) because this would become very
14798 messy and even then this function would have to contain target specific
14799 information (the names of the relocs instead of their numeric values).
14800 FIXME: This is not the correct way to solve this problem. The proper way
14801 is to have target specific reloc sizing and typing functions created by
14802 the reloc-macros.h header, in the same way that it already creates the
14803 reloc naming functions. */
14804
14805 static bool
14806 is_32bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
14807 {
14808 /* Please keep this table alpha-sorted for ease of visual lookup. */
14809 switch (filedata->file_header.e_machine)
14810 {
14811 case EM_386:
14812 case EM_IAMCU:
14813 return reloc_type == 1; /* R_386_32. */
14814 case EM_68K:
14815 return reloc_type == 1; /* R_68K_32. */
14816 case EM_860:
14817 return reloc_type == 1; /* R_860_32. */
14818 case EM_960:
14819 return reloc_type == 2; /* R_960_32. */
14820 case EM_AARCH64:
14821 return (reloc_type == 258
14822 || reloc_type == 1); /* R_AARCH64_ABS32 || R_AARCH64_P32_ABS32 */
14823 case EM_BPF:
14824 return reloc_type == 11; /* R_BPF_DATA_32 */
14825 case EM_ADAPTEVA_EPIPHANY:
14826 return reloc_type == 3;
14827 case EM_ALPHA:
14828 return reloc_type == 1; /* R_ALPHA_REFLONG. */
14829 case EM_ARC:
14830 return reloc_type == 1; /* R_ARC_32. */
14831 case EM_ARC_COMPACT:
14832 case EM_ARC_COMPACT2:
14833 case EM_ARC_COMPACT3:
14834 case EM_ARC_COMPACT3_64:
14835 return reloc_type == 4; /* R_ARC_32. */
14836 case EM_ARM:
14837 return reloc_type == 2; /* R_ARM_ABS32 */
14838 case EM_AVR_OLD:
14839 case EM_AVR:
14840 return reloc_type == 1;
14841 case EM_BLACKFIN:
14842 return reloc_type == 0x12; /* R_byte4_data. */
14843 case EM_CRIS:
14844 return reloc_type == 3; /* R_CRIS_32. */
14845 case EM_CR16:
14846 return reloc_type == 3; /* R_CR16_NUM32. */
14847 case EM_CRX:
14848 return reloc_type == 15; /* R_CRX_NUM32. */
14849 case EM_CSKY:
14850 return reloc_type == 1; /* R_CKCORE_ADDR32. */
14851 case EM_CYGNUS_FRV:
14852 return reloc_type == 1;
14853 case EM_CYGNUS_D10V:
14854 case EM_D10V:
14855 return reloc_type == 6; /* R_D10V_32. */
14856 case EM_CYGNUS_D30V:
14857 case EM_D30V:
14858 return reloc_type == 12; /* R_D30V_32_NORMAL. */
14859 case EM_DLX:
14860 return reloc_type == 3; /* R_DLX_RELOC_32. */
14861 case EM_CYGNUS_FR30:
14862 case EM_FR30:
14863 return reloc_type == 3; /* R_FR30_32. */
14864 case EM_FT32:
14865 return reloc_type == 1; /* R_FT32_32. */
14866 case EM_H8S:
14867 case EM_H8_300:
14868 case EM_H8_300H:
14869 return reloc_type == 1; /* R_H8_DIR32. */
14870 case EM_IA_64:
14871 return (reloc_type == 0x64 /* R_IA64_SECREL32MSB. */
14872 || reloc_type == 0x65 /* R_IA64_SECREL32LSB. */
14873 || reloc_type == 0x24 /* R_IA64_DIR32MSB. */
14874 || reloc_type == 0x25 /* R_IA64_DIR32LSB. */);
14875 case EM_IP2K_OLD:
14876 case EM_IP2K:
14877 return reloc_type == 2; /* R_IP2K_32. */
14878 case EM_IQ2000:
14879 return reloc_type == 2; /* R_IQ2000_32. */
14880 case EM_KVX:
14881 return reloc_type == 2; /* R_KVX_32. */
14882 case EM_LATTICEMICO32:
14883 return reloc_type == 3; /* R_LM32_32. */
14884 case EM_LOONGARCH:
14885 return reloc_type == 1; /* R_LARCH_32. */
14886 case EM_M32C_OLD:
14887 case EM_M32C:
14888 return reloc_type == 3; /* R_M32C_32. */
14889 case EM_M32R:
14890 return reloc_type == 34; /* R_M32R_32_RELA. */
14891 case EM_68HC11:
14892 case EM_68HC12:
14893 return reloc_type == 6; /* R_M68HC11_32. */
14894 case EM_S12Z:
14895 return reloc_type == 7 || /* R_S12Z_EXT32 */
14896 reloc_type == 6; /* R_S12Z_CW32. */
14897 case EM_MCORE:
14898 return reloc_type == 1; /* R_MCORE_ADDR32. */
14899 case EM_CYGNUS_MEP:
14900 return reloc_type == 4; /* R_MEP_32. */
14901 case EM_METAG:
14902 return reloc_type == 2; /* R_METAG_ADDR32. */
14903 case EM_MICROBLAZE:
14904 return reloc_type == 1; /* R_MICROBLAZE_32. */
14905 case EM_MIPS:
14906 return reloc_type == 2; /* R_MIPS_32. */
14907 case EM_MMIX:
14908 return reloc_type == 4; /* R_MMIX_32. */
14909 case EM_CYGNUS_MN10200:
14910 case EM_MN10200:
14911 return reloc_type == 1; /* R_MN10200_32. */
14912 case EM_CYGNUS_MN10300:
14913 case EM_MN10300:
14914 return reloc_type == 1; /* R_MN10300_32. */
14915 case EM_MOXIE:
14916 return reloc_type == 1; /* R_MOXIE_32. */
14917 case EM_MSP430_OLD:
14918 case EM_MSP430:
14919 return reloc_type == 1; /* R_MSP430_32 or R_MSP320_ABS32. */
14920 case EM_MT:
14921 return reloc_type == 2; /* R_MT_32. */
14922 case EM_NDS32:
14923 return reloc_type == 20; /* R_NDS32_32_RELA. */
14924 case EM_ALTERA_NIOS2:
14925 return reloc_type == 12; /* R_NIOS2_BFD_RELOC_32. */
14926 case EM_NIOS32:
14927 return reloc_type == 1; /* R_NIOS_32. */
14928 case EM_OR1K:
14929 return reloc_type == 1; /* R_OR1K_32. */
14930 case EM_PARISC:
14931 return (reloc_type == 1 /* R_PARISC_DIR32. */
14932 || reloc_type == 2 /* R_PARISC_DIR21L. */
14933 || reloc_type == 41); /* R_PARISC_SECREL32. */
14934 case EM_PJ:
14935 case EM_PJ_OLD:
14936 return reloc_type == 1; /* R_PJ_DATA_DIR32. */
14937 case EM_PPC64:
14938 return reloc_type == 1; /* R_PPC64_ADDR32. */
14939 case EM_PPC:
14940 return reloc_type == 1; /* R_PPC_ADDR32. */
14941 case EM_TI_PRU:
14942 return reloc_type == 11; /* R_PRU_BFD_RELOC_32. */
14943 case EM_RISCV:
14944 return reloc_type == 1; /* R_RISCV_32. */
14945 case EM_RL78:
14946 return reloc_type == 1; /* R_RL78_DIR32. */
14947 case EM_RX:
14948 return reloc_type == 1; /* R_RX_DIR32. */
14949 case EM_S370:
14950 return reloc_type == 1; /* R_I370_ADDR31. */
14951 case EM_S390_OLD:
14952 case EM_S390:
14953 return reloc_type == 4; /* R_S390_32. */
14954 case EM_SCORE:
14955 return reloc_type == 8; /* R_SCORE_ABS32. */
14956 case EM_SH:
14957 return reloc_type == 1; /* R_SH_DIR32. */
14958 case EM_SPARC32PLUS:
14959 case EM_SPARCV9:
14960 case EM_SPARC:
14961 return reloc_type == 3 /* R_SPARC_32. */
14962 || reloc_type == 23; /* R_SPARC_UA32. */
14963 case EM_SPU:
14964 return reloc_type == 6; /* R_SPU_ADDR32 */
14965 case EM_TI_C6000:
14966 return reloc_type == 1; /* R_C6000_ABS32. */
14967 case EM_TILEGX:
14968 return reloc_type == 2; /* R_TILEGX_32. */
14969 case EM_TILEPRO:
14970 return reloc_type == 1; /* R_TILEPRO_32. */
14971 case EM_CYGNUS_V850:
14972 case EM_V850:
14973 return reloc_type == 6; /* R_V850_ABS32. */
14974 case EM_V800:
14975 return reloc_type == 0x33; /* R_V810_WORD. */
14976 case EM_VAX:
14977 return reloc_type == 1; /* R_VAX_32. */
14978 case EM_VISIUM:
14979 return reloc_type == 3; /* R_VISIUM_32. */
14980 case EM_WEBASSEMBLY:
14981 return reloc_type == 1; /* R_WASM32_32. */
14982 case EM_X86_64:
14983 case EM_L1OM:
14984 case EM_K1OM:
14985 return reloc_type == 10; /* R_X86_64_32. */
14986 case EM_XGATE:
14987 return reloc_type == 4; /* R_XGATE_32. */
14988 case EM_XSTORMY16:
14989 return reloc_type == 1; /* R_XSTROMY16_32. */
14990 case EM_XTENSA_OLD:
14991 case EM_XTENSA:
14992 return reloc_type == 1; /* R_XTENSA_32. */
14993 case EM_Z80:
14994 return reloc_type == 6; /* R_Z80_32. */
14995 default:
14996 {
14997 static unsigned int prev_warn = 0;
14998
14999 /* Avoid repeating the same warning multiple times. */
15000 if (prev_warn != filedata->file_header.e_machine)
15001 error (_("Missing knowledge of 32-bit reloc types used in DWARF sections of machine number %d\n"),
15002 filedata->file_header.e_machine);
15003 prev_warn = filedata->file_header.e_machine;
15004 return false;
15005 }
15006 }
15007 }
15008
15009 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15010 a 32-bit pc-relative RELA relocation used in DWARF debug sections. */
15011
15012 static bool
15013 is_32bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
15014 {
15015 switch (filedata->file_header.e_machine)
15016 /* Please keep this table alpha-sorted for ease of visual lookup. */
15017 {
15018 case EM_386:
15019 case EM_IAMCU:
15020 return reloc_type == 2; /* R_386_PC32. */
15021 case EM_68K:
15022 return reloc_type == 4; /* R_68K_PC32. */
15023 case EM_AARCH64:
15024 return reloc_type == 261; /* R_AARCH64_PREL32 */
15025 case EM_ADAPTEVA_EPIPHANY:
15026 return reloc_type == 6;
15027 case EM_ALPHA:
15028 return reloc_type == 10; /* R_ALPHA_SREL32. */
15029 case EM_ARC_COMPACT:
15030 case EM_ARC_COMPACT2:
15031 case EM_ARC_COMPACT3:
15032 case EM_ARC_COMPACT3_64:
15033 return reloc_type == 49; /* R_ARC_32_PCREL. */
15034 case EM_ARM:
15035 return reloc_type == 3; /* R_ARM_REL32 */
15036 case EM_AVR_OLD:
15037 case EM_AVR:
15038 return reloc_type == 36; /* R_AVR_32_PCREL. */
15039 case EM_LOONGARCH:
15040 return reloc_type == 99; /* R_LARCH_32_PCREL. */
15041 case EM_MICROBLAZE:
15042 return reloc_type == 2; /* R_MICROBLAZE_32_PCREL. */
15043 case EM_OR1K:
15044 return reloc_type == 9; /* R_OR1K_32_PCREL. */
15045 case EM_PARISC:
15046 return reloc_type == 9; /* R_PARISC_PCREL32. */
15047 case EM_PPC:
15048 return reloc_type == 26; /* R_PPC_REL32. */
15049 case EM_PPC64:
15050 return reloc_type == 26; /* R_PPC64_REL32. */
15051 case EM_RISCV:
15052 return reloc_type == 57; /* R_RISCV_32_PCREL. */
15053 case EM_S390_OLD:
15054 case EM_S390:
15055 return reloc_type == 5; /* R_390_PC32. */
15056 case EM_SH:
15057 return reloc_type == 2; /* R_SH_REL32. */
15058 case EM_SPARC32PLUS:
15059 case EM_SPARCV9:
15060 case EM_SPARC:
15061 return reloc_type == 6; /* R_SPARC_DISP32. */
15062 case EM_SPU:
15063 return reloc_type == 13; /* R_SPU_REL32. */
15064 case EM_TILEGX:
15065 return reloc_type == 6; /* R_TILEGX_32_PCREL. */
15066 case EM_TILEPRO:
15067 return reloc_type == 4; /* R_TILEPRO_32_PCREL. */
15068 case EM_VISIUM:
15069 return reloc_type == 6; /* R_VISIUM_32_PCREL */
15070 case EM_X86_64:
15071 case EM_L1OM:
15072 case EM_K1OM:
15073 return reloc_type == 2; /* R_X86_64_PC32. */
15074 case EM_VAX:
15075 return reloc_type == 4; /* R_VAX_PCREL32. */
15076 case EM_XTENSA_OLD:
15077 case EM_XTENSA:
15078 return reloc_type == 14; /* R_XTENSA_32_PCREL. */
15079 case EM_KVX:
15080 return reloc_type == 7; /* R_KVX_32_PCREL */
15081 default:
15082 /* Do not abort or issue an error message here. Not all targets use
15083 pc-relative 32-bit relocs in their DWARF debug information and we
15084 have already tested for target coverage in is_32bit_abs_reloc. A
15085 more helpful warning message will be generated by apply_relocations
15086 anyway, so just return. */
15087 return false;
15088 }
15089 }
15090
15091 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15092 a 64-bit absolute RELA relocation used in DWARF debug sections. */
15093
15094 static bool
15095 is_64bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15096 {
15097 switch (filedata->file_header.e_machine)
15098 {
15099 case EM_AARCH64:
15100 return reloc_type == 257; /* R_AARCH64_ABS64. */
15101 case EM_ARC_COMPACT3_64:
15102 return reloc_type == 5; /* R_ARC_64. */
15103 case EM_ALPHA:
15104 return reloc_type == 2; /* R_ALPHA_REFQUAD. */
15105 case EM_IA_64:
15106 return (reloc_type == 0x26 /* R_IA64_DIR64MSB. */
15107 || reloc_type == 0x27 /* R_IA64_DIR64LSB. */);
15108 case EM_LOONGARCH:
15109 return reloc_type == 2; /* R_LARCH_64 */
15110 case EM_PARISC:
15111 return reloc_type == 80; /* R_PARISC_DIR64. */
15112 case EM_PPC64:
15113 return reloc_type == 38; /* R_PPC64_ADDR64. */
15114 case EM_RISCV:
15115 return reloc_type == 2; /* R_RISCV_64. */
15116 case EM_SPARC32PLUS:
15117 case EM_SPARCV9:
15118 case EM_SPARC:
15119 return reloc_type == 32 /* R_SPARC_64. */
15120 || reloc_type == 54; /* R_SPARC_UA64. */
15121 case EM_X86_64:
15122 case EM_L1OM:
15123 case EM_K1OM:
15124 return reloc_type == 1; /* R_X86_64_64. */
15125 case EM_S390_OLD:
15126 case EM_S390:
15127 return reloc_type == 22; /* R_S390_64. */
15128 case EM_TILEGX:
15129 return reloc_type == 1; /* R_TILEGX_64. */
15130 case EM_MIPS:
15131 return reloc_type == 18; /* R_MIPS_64. */
15132 case EM_KVX:
15133 return reloc_type == 3; /* R_KVX_64 */
15134 default:
15135 return false;
15136 }
15137 }
15138
15139 /* Like is_32bit_pcrel_reloc except that it returns TRUE iff RELOC_TYPE is
15140 a 64-bit pc-relative RELA relocation used in DWARF debug sections. */
15141
15142 static bool
15143 is_64bit_pcrel_reloc (Filedata * filedata, unsigned int reloc_type)
15144 {
15145 switch (filedata->file_header.e_machine)
15146 {
15147 case EM_AARCH64:
15148 return reloc_type == 260; /* R_AARCH64_PREL64. */
15149 case EM_ALPHA:
15150 return reloc_type == 11; /* R_ALPHA_SREL64. */
15151 case EM_IA_64:
15152 return (reloc_type == 0x4e /* R_IA64_PCREL64MSB. */
15153 || reloc_type == 0x4f /* R_IA64_PCREL64LSB. */);
15154 case EM_PARISC:
15155 return reloc_type == 72; /* R_PARISC_PCREL64. */
15156 case EM_PPC64:
15157 return reloc_type == 44; /* R_PPC64_REL64. */
15158 case EM_SPARC32PLUS:
15159 case EM_SPARCV9:
15160 case EM_SPARC:
15161 return reloc_type == 46; /* R_SPARC_DISP64. */
15162 case EM_X86_64:
15163 case EM_L1OM:
15164 case EM_K1OM:
15165 return reloc_type == 24; /* R_X86_64_PC64. */
15166 case EM_S390_OLD:
15167 case EM_S390:
15168 return reloc_type == 23; /* R_S390_PC64. */
15169 case EM_TILEGX:
15170 return reloc_type == 5; /* R_TILEGX_64_PCREL. */
15171 default:
15172 return false;
15173 }
15174 }
15175
15176 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15177 a 24-bit absolute RELA relocation used in DWARF debug sections. */
15178
15179 static bool
15180 is_24bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15181 {
15182 switch (filedata->file_header.e_machine)
15183 {
15184 case EM_CYGNUS_MN10200:
15185 case EM_MN10200:
15186 return reloc_type == 4; /* R_MN10200_24. */
15187 case EM_FT32:
15188 return reloc_type == 5; /* R_FT32_20. */
15189 case EM_Z80:
15190 return reloc_type == 5; /* R_Z80_24. */
15191 default:
15192 return false;
15193 }
15194 }
15195
15196 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15197 a 16-bit absolute RELA relocation used in DWARF debug sections. */
15198
15199 static bool
15200 is_16bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15201 {
15202 /* Please keep this table alpha-sorted for ease of visual lookup. */
15203 switch (filedata->file_header.e_machine)
15204 {
15205 case EM_ARC:
15206 case EM_ARC_COMPACT:
15207 case EM_ARC_COMPACT2:
15208 case EM_ARC_COMPACT3:
15209 case EM_ARC_COMPACT3_64:
15210 return reloc_type == 2; /* R_ARC_16. */
15211 case EM_ADAPTEVA_EPIPHANY:
15212 return reloc_type == 5;
15213 case EM_AVR_OLD:
15214 case EM_AVR:
15215 return reloc_type == 4; /* R_AVR_16. */
15216 case EM_CYGNUS_D10V:
15217 case EM_D10V:
15218 return reloc_type == 3; /* R_D10V_16. */
15219 case EM_FT32:
15220 return reloc_type == 2; /* R_FT32_16. */
15221 case EM_H8S:
15222 case EM_H8_300:
15223 case EM_H8_300H:
15224 return reloc_type == R_H8_DIR16;
15225 case EM_IP2K_OLD:
15226 case EM_IP2K:
15227 return reloc_type == 1; /* R_IP2K_16. */
15228 case EM_M32C_OLD:
15229 case EM_M32C:
15230 return reloc_type == 1; /* R_M32C_16 */
15231 case EM_CYGNUS_MN10200:
15232 case EM_MN10200:
15233 return reloc_type == 2; /* R_MN10200_16. */
15234 case EM_CYGNUS_MN10300:
15235 case EM_MN10300:
15236 return reloc_type == 2; /* R_MN10300_16. */
15237 case EM_KVX:
15238 return reloc_type == 1; /* R_KVX_16 */
15239 case EM_MSP430:
15240 if (uses_msp430x_relocs (filedata))
15241 return reloc_type == 2; /* R_MSP430_ABS16. */
15242 /* Fall through. */
15243 case EM_MSP430_OLD:
15244 return reloc_type == 5; /* R_MSP430_16_BYTE. */
15245 case EM_NDS32:
15246 return reloc_type == 19; /* R_NDS32_16_RELA. */
15247 case EM_ALTERA_NIOS2:
15248 return reloc_type == 13; /* R_NIOS2_BFD_RELOC_16. */
15249 case EM_NIOS32:
15250 return reloc_type == 9; /* R_NIOS_16. */
15251 case EM_OR1K:
15252 return reloc_type == 2; /* R_OR1K_16. */
15253 case EM_RISCV:
15254 return reloc_type == 55; /* R_RISCV_SET16. */
15255 case EM_TI_PRU:
15256 return reloc_type == 8; /* R_PRU_BFD_RELOC_16. */
15257 case EM_TI_C6000:
15258 return reloc_type == 2; /* R_C6000_ABS16. */
15259 case EM_VISIUM:
15260 return reloc_type == 2; /* R_VISIUM_16. */
15261 case EM_XGATE:
15262 return reloc_type == 3; /* R_XGATE_16. */
15263 case EM_Z80:
15264 return reloc_type == 4; /* R_Z80_16. */
15265 default:
15266 return false;
15267 }
15268 }
15269
15270 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15271 a 8-bit absolute RELA relocation used in DWARF debug sections. */
15272
15273 static bool
15274 is_8bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15275 {
15276 switch (filedata->file_header.e_machine)
15277 {
15278 case EM_RISCV:
15279 return reloc_type == 54; /* R_RISCV_SET8. */
15280 case EM_Z80:
15281 return reloc_type == 1; /* R_Z80_8. */
15282 default:
15283 return false;
15284 }
15285 }
15286
15287 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15288 a 6-bit absolute RELA relocation used in DWARF debug sections. */
15289
15290 static bool
15291 is_6bit_abs_reloc (Filedata * filedata, unsigned int reloc_type)
15292 {
15293 switch (filedata->file_header.e_machine)
15294 {
15295 case EM_RISCV:
15296 return reloc_type == 53; /* R_RISCV_SET6. */
15297 default:
15298 return false;
15299 }
15300 }
15301
15302 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15303 a 32-bit inplace add RELA relocation used in DWARF debug sections. */
15304
15305 static bool
15306 is_32bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15307 {
15308 /* Please keep this table alpha-sorted for ease of visual lookup. */
15309 switch (filedata->file_header.e_machine)
15310 {
15311 case EM_LOONGARCH:
15312 return reloc_type == 50; /* R_LARCH_ADD32. */
15313 case EM_RISCV:
15314 return reloc_type == 35; /* R_RISCV_ADD32. */
15315 default:
15316 return false;
15317 }
15318 }
15319
15320 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15321 a 32-bit inplace sub RELA relocation used in DWARF debug sections. */
15322
15323 static bool
15324 is_32bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15325 {
15326 /* Please keep this table alpha-sorted for ease of visual lookup. */
15327 switch (filedata->file_header.e_machine)
15328 {
15329 case EM_LOONGARCH:
15330 return reloc_type == 55; /* R_LARCH_SUB32. */
15331 case EM_RISCV:
15332 return reloc_type == 39; /* R_RISCV_SUB32. */
15333 default:
15334 return false;
15335 }
15336 }
15337
15338 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15339 a 64-bit inplace add RELA relocation used in DWARF debug sections. */
15340
15341 static bool
15342 is_64bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15343 {
15344 /* Please keep this table alpha-sorted for ease of visual lookup. */
15345 switch (filedata->file_header.e_machine)
15346 {
15347 case EM_LOONGARCH:
15348 return reloc_type == 51; /* R_LARCH_ADD64. */
15349 case EM_RISCV:
15350 return reloc_type == 36; /* R_RISCV_ADD64. */
15351 default:
15352 return false;
15353 }
15354 }
15355
15356 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15357 a 64-bit inplace sub RELA relocation used in DWARF debug sections. */
15358
15359 static bool
15360 is_64bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15361 {
15362 /* Please keep this table alpha-sorted for ease of visual lookup. */
15363 switch (filedata->file_header.e_machine)
15364 {
15365 case EM_LOONGARCH:
15366 return reloc_type == 56; /* R_LARCH_SUB64. */
15367 case EM_RISCV:
15368 return reloc_type == 40; /* R_RISCV_SUB64. */
15369 default:
15370 return false;
15371 }
15372 }
15373
15374 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15375 a 16-bit inplace add RELA relocation used in DWARF debug sections. */
15376
15377 static bool
15378 is_16bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15379 {
15380 /* Please keep this table alpha-sorted for ease of visual lookup. */
15381 switch (filedata->file_header.e_machine)
15382 {
15383 case EM_LOONGARCH:
15384 return reloc_type == 48; /* R_LARCH_ADD16. */
15385 case EM_RISCV:
15386 return reloc_type == 34; /* R_RISCV_ADD16. */
15387 default:
15388 return false;
15389 }
15390 }
15391
15392 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15393 a 16-bit inplace sub RELA relocation used in DWARF debug sections. */
15394
15395 static bool
15396 is_16bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15397 {
15398 /* Please keep this table alpha-sorted for ease of visual lookup. */
15399 switch (filedata->file_header.e_machine)
15400 {
15401 case EM_LOONGARCH:
15402 return reloc_type == 53; /* R_LARCH_SUB16. */
15403 case EM_RISCV:
15404 return reloc_type == 38; /* R_RISCV_SUB16. */
15405 default:
15406 return false;
15407 }
15408 }
15409
15410 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15411 a 8-bit inplace add RELA relocation used in DWARF debug sections. */
15412
15413 static bool
15414 is_8bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15415 {
15416 /* Please keep this table alpha-sorted for ease of visual lookup. */
15417 switch (filedata->file_header.e_machine)
15418 {
15419 case EM_LOONGARCH:
15420 return reloc_type == 47; /* R_LARCH_ADD8. */
15421 case EM_RISCV:
15422 return reloc_type == 33; /* R_RISCV_ADD8. */
15423 default:
15424 return false;
15425 }
15426 }
15427
15428 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15429 a 8-bit inplace sub RELA relocation used in DWARF debug sections. */
15430
15431 static bool
15432 is_8bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15433 {
15434 /* Please keep this table alpha-sorted for ease of visual lookup. */
15435 switch (filedata->file_header.e_machine)
15436 {
15437 case EM_LOONGARCH:
15438 return reloc_type == 52; /* R_LARCH_SUB8. */
15439 case EM_RISCV:
15440 return reloc_type == 37; /* R_RISCV_SUB8. */
15441 default:
15442 return false;
15443 }
15444 }
15445
15446 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15447 a 6-bit inplace add RELA relocation used in DWARF debug sections. */
15448
15449 static bool
15450 is_6bit_inplace_add_reloc (Filedata * filedata, unsigned int reloc_type)
15451 {
15452 switch (filedata->file_header.e_machine)
15453 {
15454 case EM_LOONGARCH:
15455 return reloc_type == 105; /* R_LARCH_ADD6. */
15456 default:
15457 return false;
15458 }
15459 }
15460
15461 /* Like is_32bit_abs_reloc except that it returns TRUE iff RELOC_TYPE is
15462 a 6-bit inplace sub RELA relocation used in DWARF debug sections. */
15463
15464 static bool
15465 is_6bit_inplace_sub_reloc (Filedata * filedata, unsigned int reloc_type)
15466 {
15467 switch (filedata->file_header.e_machine)
15468 {
15469 case EM_LOONGARCH:
15470 return reloc_type == 106; /* R_LARCH_SUB6. */
15471 case EM_RISCV:
15472 return reloc_type == 52; /* R_RISCV_SUB6. */
15473 default:
15474 return false;
15475 }
15476 }
15477
15478 /* Returns TRUE iff RELOC_TYPE is a NONE relocation used for discarded
15479 relocation entries (possibly formerly used for SHT_GROUP sections). */
15480
15481 static bool
15482 is_none_reloc (Filedata * filedata, unsigned int reloc_type)
15483 {
15484 switch (filedata->file_header.e_machine)
15485 {
15486 case EM_386: /* R_386_NONE. */
15487 case EM_68K: /* R_68K_NONE. */
15488 case EM_ADAPTEVA_EPIPHANY:
15489 case EM_ALPHA: /* R_ALPHA_NONE. */
15490 case EM_ALTERA_NIOS2: /* R_NIOS2_NONE. */
15491 case EM_ARC: /* R_ARC_NONE. */
15492 case EM_ARC_COMPACT2: /* R_ARC_NONE. */
15493 case EM_ARC_COMPACT: /* R_ARC_NONE. */
15494 case EM_ARC_COMPACT3: /* R_ARC_NONE. */
15495 case EM_ARC_COMPACT3_64: /* R_ARC_NONE. */
15496 case EM_ARM: /* R_ARM_NONE. */
15497 case EM_CRIS: /* R_CRIS_NONE. */
15498 case EM_FT32: /* R_FT32_NONE. */
15499 case EM_IA_64: /* R_IA64_NONE. */
15500 case EM_K1OM: /* R_X86_64_NONE. */
15501 case EM_KVX: /* R_KVX_NONE. */
15502 case EM_L1OM: /* R_X86_64_NONE. */
15503 case EM_M32R: /* R_M32R_NONE. */
15504 case EM_MIPS: /* R_MIPS_NONE. */
15505 case EM_MN10300: /* R_MN10300_NONE. */
15506 case EM_MOXIE: /* R_MOXIE_NONE. */
15507 case EM_NIOS32: /* R_NIOS_NONE. */
15508 case EM_OR1K: /* R_OR1K_NONE. */
15509 case EM_PARISC: /* R_PARISC_NONE. */
15510 case EM_PPC64: /* R_PPC64_NONE. */
15511 case EM_PPC: /* R_PPC_NONE. */
15512 case EM_RISCV: /* R_RISCV_NONE. */
15513 case EM_S390: /* R_390_NONE. */
15514 case EM_S390_OLD:
15515 case EM_SH: /* R_SH_NONE. */
15516 case EM_SPARC32PLUS:
15517 case EM_SPARC: /* R_SPARC_NONE. */
15518 case EM_SPARCV9:
15519 case EM_TILEGX: /* R_TILEGX_NONE. */
15520 case EM_TILEPRO: /* R_TILEPRO_NONE. */
15521 case EM_TI_C6000:/* R_C6000_NONE. */
15522 case EM_X86_64: /* R_X86_64_NONE. */
15523 case EM_Z80: /* R_Z80_NONE. */
15524 case EM_WEBASSEMBLY: /* R_WASM32_NONE. */
15525 return reloc_type == 0;
15526
15527 case EM_AARCH64:
15528 return reloc_type == 0 || reloc_type == 256;
15529 case EM_AVR_OLD:
15530 case EM_AVR:
15531 return (reloc_type == 0 /* R_AVR_NONE. */
15532 || reloc_type == 30 /* R_AVR_DIFF8. */
15533 || reloc_type == 31 /* R_AVR_DIFF16. */
15534 || reloc_type == 32 /* R_AVR_DIFF32. */);
15535 case EM_METAG:
15536 return reloc_type == 3; /* R_METAG_NONE. */
15537 case EM_NDS32:
15538 return (reloc_type == 0 /* R_NDS32_NONE. */
15539 || reloc_type == 205 /* R_NDS32_DIFF8. */
15540 || reloc_type == 206 /* R_NDS32_DIFF16. */
15541 || reloc_type == 207 /* R_NDS32_DIFF32. */
15542 || reloc_type == 208 /* R_NDS32_DIFF_ULEB128. */);
15543 case EM_TI_PRU:
15544 return (reloc_type == 0 /* R_PRU_NONE. */
15545 || reloc_type == 65 /* R_PRU_DIFF8. */
15546 || reloc_type == 66 /* R_PRU_DIFF16. */
15547 || reloc_type == 67 /* R_PRU_DIFF32. */);
15548 case EM_XTENSA_OLD:
15549 case EM_XTENSA:
15550 return (reloc_type == 0 /* R_XTENSA_NONE. */
15551 || reloc_type == 17 /* R_XTENSA_DIFF8. */
15552 || reloc_type == 18 /* R_XTENSA_DIFF16. */
15553 || reloc_type == 19 /* R_XTENSA_DIFF32. */
15554 || reloc_type == 57 /* R_XTENSA_PDIFF8. */
15555 || reloc_type == 58 /* R_XTENSA_PDIFF16. */
15556 || reloc_type == 59 /* R_XTENSA_PDIFF32. */
15557 || reloc_type == 60 /* R_XTENSA_NDIFF8. */
15558 || reloc_type == 61 /* R_XTENSA_NDIFF16. */
15559 || reloc_type == 62 /* R_XTENSA_NDIFF32. */);
15560 }
15561 return false;
15562 }
15563
15564 /* Returns TRUE if there is a relocation against
15565 section NAME at OFFSET bytes. */
15566
15567 bool
15568 reloc_at (struct dwarf_section * dsec, uint64_t offset)
15569 {
15570 Elf_Internal_Rela * relocs;
15571 Elf_Internal_Rela * rp;
15572
15573 if (dsec == NULL || dsec->reloc_info == NULL)
15574 return false;
15575
15576 relocs = (Elf_Internal_Rela *) dsec->reloc_info;
15577
15578 for (rp = relocs; rp < relocs + dsec->num_relocs; ++rp)
15579 if (rp->r_offset == offset)
15580 return true;
15581
15582 return false;
15583 }
15584
15585 /* Apply relocations to a section.
15586 Returns TRUE upon success, FALSE otherwise.
15587 If RELOCS_RETURN is non-NULL then it is set to point to the loaded relocs.
15588 It is then the caller's responsibility to free them. NUM_RELOCS_RETURN
15589 will be set to the number of relocs loaded.
15590
15591 Note: So far support has been added only for those relocations
15592 which can be found in debug sections. FIXME: Add support for
15593 more relocations ? */
15594
15595 static bool
15596 apply_relocations (Filedata *filedata,
15597 const Elf_Internal_Shdr *section,
15598 unsigned char *start,
15599 size_t size,
15600 void **relocs_return,
15601 uint64_t *num_relocs_return)
15602 {
15603 Elf_Internal_Shdr * relsec;
15604 unsigned char * end = start + size;
15605
15606 if (relocs_return != NULL)
15607 {
15608 * (Elf_Internal_Rela **) relocs_return = NULL;
15609 * num_relocs_return = 0;
15610 }
15611
15612 if (filedata->file_header.e_type != ET_REL)
15613 /* No relocs to apply. */
15614 return true;
15615
15616 /* Find the reloc section associated with the section. */
15617 for (relsec = filedata->section_headers;
15618 relsec < filedata->section_headers + filedata->file_header.e_shnum;
15619 ++relsec)
15620 {
15621 bool is_rela;
15622 uint64_t num_relocs;
15623 Elf_Internal_Rela * relocs;
15624 Elf_Internal_Rela * rp;
15625 Elf_Internal_Shdr * symsec;
15626 Elf_Internal_Sym * symtab;
15627 uint64_t num_syms;
15628 Elf_Internal_Sym * sym;
15629
15630 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
15631 || relsec->sh_info >= filedata->file_header.e_shnum
15632 || filedata->section_headers + relsec->sh_info != section
15633 || relsec->sh_size == 0
15634 || relsec->sh_link >= filedata->file_header.e_shnum)
15635 continue;
15636
15637 symsec = filedata->section_headers + relsec->sh_link;
15638 if (symsec->sh_type != SHT_SYMTAB
15639 && symsec->sh_type != SHT_DYNSYM)
15640 return false;
15641
15642 is_rela = relsec->sh_type == SHT_RELA;
15643
15644 if (is_rela)
15645 {
15646 if (!slurp_rela_relocs (filedata, relsec->sh_offset,
15647 relsec->sh_size, & relocs, & num_relocs))
15648 return false;
15649 }
15650 else
15651 {
15652 if (!slurp_rel_relocs (filedata, relsec->sh_offset,
15653 relsec->sh_size, & relocs, & num_relocs))
15654 return false;
15655 }
15656
15657 /* SH uses RELA but uses in place value instead of the addend field. */
15658 if (filedata->file_header.e_machine == EM_SH)
15659 is_rela = false;
15660
15661 symtab = get_elf_symbols (filedata, symsec, & num_syms);
15662
15663 for (rp = relocs; rp < relocs + num_relocs; ++rp)
15664 {
15665 uint64_t addend;
15666 unsigned int reloc_type;
15667 unsigned int reloc_size;
15668 bool reloc_inplace = false;
15669 bool reloc_subtract = false;
15670 unsigned char *rloc;
15671 uint64_t sym_index;
15672
15673 reloc_type = get_reloc_type (filedata, rp->r_info);
15674
15675 if (target_specific_reloc_handling (filedata, rp, start, end, symtab, num_syms))
15676 continue;
15677 else if (is_none_reloc (filedata, reloc_type))
15678 continue;
15679 else if (is_32bit_abs_reloc (filedata, reloc_type)
15680 || is_32bit_pcrel_reloc (filedata, reloc_type))
15681 reloc_size = 4;
15682 else if (is_64bit_abs_reloc (filedata, reloc_type)
15683 || is_64bit_pcrel_reloc (filedata, reloc_type))
15684 reloc_size = 8;
15685 else if (is_24bit_abs_reloc (filedata, reloc_type))
15686 reloc_size = 3;
15687 else if (is_16bit_abs_reloc (filedata, reloc_type))
15688 reloc_size = 2;
15689 else if (is_8bit_abs_reloc (filedata, reloc_type)
15690 || is_6bit_abs_reloc (filedata, reloc_type))
15691 reloc_size = 1;
15692 else if ((reloc_subtract = is_32bit_inplace_sub_reloc (filedata,
15693 reloc_type))
15694 || is_32bit_inplace_add_reloc (filedata, reloc_type))
15695 {
15696 reloc_size = 4;
15697 reloc_inplace = true;
15698 }
15699 else if ((reloc_subtract = is_64bit_inplace_sub_reloc (filedata,
15700 reloc_type))
15701 || is_64bit_inplace_add_reloc (filedata, reloc_type))
15702 {
15703 reloc_size = 8;
15704 reloc_inplace = true;
15705 }
15706 else if ((reloc_subtract = is_16bit_inplace_sub_reloc (filedata,
15707 reloc_type))
15708 || is_16bit_inplace_add_reloc (filedata, reloc_type))
15709 {
15710 reloc_size = 2;
15711 reloc_inplace = true;
15712 }
15713 else if ((reloc_subtract = is_8bit_inplace_sub_reloc (filedata,
15714 reloc_type))
15715 || is_8bit_inplace_add_reloc (filedata, reloc_type))
15716 {
15717 reloc_size = 1;
15718 reloc_inplace = true;
15719 }
15720 else if ((reloc_subtract = is_6bit_inplace_sub_reloc (filedata,
15721 reloc_type))
15722 || is_6bit_inplace_add_reloc (filedata, reloc_type))
15723 {
15724 reloc_size = 1;
15725 reloc_inplace = true;
15726 }
15727 else
15728 {
15729 static unsigned int prev_reloc = 0;
15730
15731 if (reloc_type != prev_reloc)
15732 warn (_("unable to apply unsupported reloc type %d to section %s\n"),
15733 reloc_type, printable_section_name (filedata, section));
15734 prev_reloc = reloc_type;
15735 continue;
15736 }
15737
15738 rloc = start + rp->r_offset;
15739 if (!IN_RANGE (start, end, rloc, reloc_size))
15740 {
15741 warn (_("skipping invalid relocation offset %#" PRIx64
15742 " in section %s\n"),
15743 rp->r_offset,
15744 printable_section_name (filedata, section));
15745 continue;
15746 }
15747
15748 sym_index = get_reloc_symindex (rp->r_info);
15749 if (sym_index >= num_syms)
15750 {
15751 warn (_("skipping invalid relocation symbol index %#" PRIx64
15752 " in section %s\n"),
15753 sym_index, printable_section_name (filedata, section));
15754 continue;
15755 }
15756 sym = symtab + sym_index;
15757
15758 /* If the reloc has a symbol associated with it,
15759 make sure that it is of an appropriate type.
15760
15761 Relocations against symbols without type can happen.
15762 Gcc -feliminate-dwarf2-dups may generate symbols
15763 without type for debug info.
15764
15765 Icc generates relocations against function symbols
15766 instead of local labels.
15767
15768 Relocations against object symbols can happen, eg when
15769 referencing a global array. For an example of this see
15770 the _clz.o binary in libgcc.a. */
15771 if (sym != symtab
15772 && ELF_ST_TYPE (sym->st_info) != STT_COMMON
15773 && ELF_ST_TYPE (sym->st_info) > STT_SECTION)
15774 {
15775 warn (_("skipping unexpected symbol type %s in section %s relocation %tu\n"),
15776 get_symbol_type (filedata, ELF_ST_TYPE (sym->st_info)),
15777 printable_section_name (filedata, relsec),
15778 rp - relocs);
15779 continue;
15780 }
15781
15782 addend = 0;
15783 if (is_rela)
15784 addend += rp->r_addend;
15785 /* R_XTENSA_32, R_PJ_DATA_DIR32 and R_D30V_32_NORMAL are
15786 partial_inplace. */
15787 if (!is_rela
15788 || (filedata->file_header.e_machine == EM_XTENSA
15789 && reloc_type == 1)
15790 || ((filedata->file_header.e_machine == EM_PJ
15791 || filedata->file_header.e_machine == EM_PJ_OLD)
15792 && reloc_type == 1)
15793 || ((filedata->file_header.e_machine == EM_D30V
15794 || filedata->file_header.e_machine == EM_CYGNUS_D30V)
15795 && reloc_type == 12)
15796 || reloc_inplace)
15797 {
15798 if (is_6bit_inplace_sub_reloc (filedata, reloc_type))
15799 addend += byte_get (rloc, reloc_size) & 0x3f;
15800 else
15801 addend += byte_get (rloc, reloc_size);
15802 }
15803
15804 if (is_32bit_pcrel_reloc (filedata, reloc_type)
15805 || is_64bit_pcrel_reloc (filedata, reloc_type))
15806 {
15807 /* On HPPA, all pc-relative relocations are biased by 8. */
15808 if (filedata->file_header.e_machine == EM_PARISC)
15809 addend -= 8;
15810 byte_put (rloc, (addend + sym->st_value) - rp->r_offset,
15811 reloc_size);
15812 }
15813 else if (is_6bit_abs_reloc (filedata, reloc_type)
15814 || is_6bit_inplace_sub_reloc (filedata, reloc_type)
15815 || is_6bit_inplace_add_reloc (filedata, reloc_type))
15816 {
15817 if (reloc_subtract)
15818 addend -= sym->st_value;
15819 else
15820 addend += sym->st_value;
15821 addend = (addend & 0x3f) | (byte_get (rloc, reloc_size) & 0xc0);
15822 byte_put (rloc, addend, reloc_size);
15823 }
15824 else if (reloc_subtract)
15825 byte_put (rloc, addend - sym->st_value, reloc_size);
15826 else
15827 byte_put (rloc, addend + sym->st_value, reloc_size);
15828 }
15829
15830 free (symtab);
15831 /* Let the target specific reloc processing code know that
15832 we have finished with these relocs. */
15833 target_specific_reloc_handling (filedata, NULL, NULL, NULL, NULL, 0);
15834
15835 if (relocs_return)
15836 {
15837 * (Elf_Internal_Rela **) relocs_return = relocs;
15838 * num_relocs_return = num_relocs;
15839 }
15840 else
15841 free (relocs);
15842
15843 break;
15844 }
15845
15846 return true;
15847 }
15848
15849 #ifdef SUPPORT_DISASSEMBLY
15850 static bool
15851 disassemble_section (Elf_Internal_Shdr * section, Filedata * filedata)
15852 {
15853 printf (_("\nAssembly dump of section %s\n"), printable_section_name (filedata, section));
15854
15855 /* FIXME: XXX -- to be done --- XXX */
15856
15857 return true;
15858 }
15859 #endif
15860
15861 /* Reads in the contents of SECTION from FILE, returning a pointer
15862 to a malloc'ed buffer or NULL if something went wrong. */
15863
15864 static char *
15865 get_section_contents (Elf_Internal_Shdr * section, Filedata * filedata)
15866 {
15867 uint64_t num_bytes = section->sh_size;
15868
15869 if (num_bytes == 0 || section->sh_type == SHT_NOBITS)
15870 {
15871 printf (_("Section '%s' has no data to dump.\n"),
15872 printable_section_name (filedata, section));
15873 return NULL;
15874 }
15875
15876 return (char *) get_data (NULL, filedata, section->sh_offset, 1, num_bytes,
15877 _("section contents"));
15878 }
15879
15880 /* Uncompresses a section that was compressed using zlib/zstd, in place. */
15881
15882 static bool
15883 uncompress_section_contents (bool is_zstd,
15884 unsigned char ** buffer,
15885 uint64_t uncompressed_size,
15886 uint64_t * size,
15887 uint64_t file_size)
15888 {
15889 uint64_t compressed_size = *size;
15890 unsigned char *compressed_buffer = *buffer;
15891 unsigned char *uncompressed_buffer = NULL;
15892 z_stream strm;
15893 int rc;
15894
15895 /* Similar to _bfd_section_size_insane() in the BFD library we expect an
15896 upper limit of ~10x compression. Any compression larger than that is
15897 thought to be due to fuzzing of the compression header. */
15898 if (uncompressed_size > file_size * 10)
15899 {
15900 error (_("Uncompressed section size is suspiciously large: 0x%" PRIu64 "\n"),
15901 uncompressed_size);
15902 goto fail;
15903 }
15904
15905 uncompressed_buffer = xmalloc (uncompressed_size);
15906
15907 if (is_zstd)
15908 {
15909 #ifdef HAVE_ZSTD
15910 size_t ret = ZSTD_decompress (uncompressed_buffer, uncompressed_size,
15911 compressed_buffer, compressed_size);
15912 if (ZSTD_isError (ret))
15913 goto fail;
15914 #endif
15915 }
15916 else
15917 {
15918 /* It is possible the section consists of several compressed
15919 buffers concatenated together, so we uncompress in a loop. */
15920 /* PR 18313: The state field in the z_stream structure is supposed
15921 to be invisible to the user (ie us), but some compilers will
15922 still complain about it being used without initialisation. So
15923 we first zero the entire z_stream structure and then set the fields
15924 that we need. */
15925 memset (&strm, 0, sizeof strm);
15926 strm.avail_in = compressed_size;
15927 strm.next_in = (Bytef *)compressed_buffer;
15928 strm.avail_out = uncompressed_size;
15929
15930 rc = inflateInit (&strm);
15931 while (strm.avail_in > 0)
15932 {
15933 if (rc != Z_OK)
15934 break;
15935 strm.next_out = ((Bytef *)uncompressed_buffer
15936 + (uncompressed_size - strm.avail_out));
15937 rc = inflate (&strm, Z_FINISH);
15938 if (rc != Z_STREAM_END)
15939 break;
15940 rc = inflateReset (&strm);
15941 }
15942 if (inflateEnd (&strm) != Z_OK || rc != Z_OK || strm.avail_out != 0)
15943 goto fail;
15944 }
15945
15946 *buffer = uncompressed_buffer;
15947 *size = uncompressed_size;
15948 return true;
15949
15950 fail:
15951 free (uncompressed_buffer);
15952 /* Indicate decompression failure. */
15953 *buffer = NULL;
15954 return false;
15955 }
15956
15957 static bool
15958 dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
15959 {
15960 Elf_Internal_Shdr *relsec;
15961 uint64_t num_bytes;
15962 unsigned char *data;
15963 unsigned char *end;
15964 unsigned char *real_start;
15965 unsigned char *start;
15966 bool some_strings_shown;
15967
15968 real_start = start = (unsigned char *) get_section_contents (section, filedata);
15969 if (start == NULL)
15970 /* PR 21820: Do not fail if the section was empty. */
15971 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
15972
15973 num_bytes = section->sh_size;
15974
15975 if (filedata->is_separate)
15976 printf (_("\nString dump of section '%s' in linked file %s:\n"),
15977 printable_section_name (filedata, section),
15978 filedata->file_name);
15979 else
15980 printf (_("\nString dump of section '%s':\n"),
15981 printable_section_name (filedata, section));
15982
15983 if (decompress_dumps)
15984 {
15985 uint64_t new_size = num_bytes;
15986 uint64_t uncompressed_size = 0;
15987 bool is_zstd = false;
15988
15989 if ((section->sh_flags & SHF_COMPRESSED) != 0)
15990 {
15991 Elf_Internal_Chdr chdr;
15992 unsigned int compression_header_size
15993 = get_compression_header (& chdr, (unsigned char *) start,
15994 num_bytes);
15995 if (compression_header_size == 0)
15996 /* An error message will have already been generated
15997 by get_compression_header. */
15998 goto error_out;
15999
16000 if (chdr.ch_type == ch_compress_zlib)
16001 ;
16002 #ifdef HAVE_ZSTD
16003 else if (chdr.ch_type == ch_compress_zstd)
16004 is_zstd = true;
16005 #endif
16006 else
16007 {
16008 warn (_("section '%s' has unsupported compress type: %d\n"),
16009 printable_section_name (filedata, section), chdr.ch_type);
16010 goto error_out;
16011 }
16012 uncompressed_size = chdr.ch_size;
16013 start += compression_header_size;
16014 new_size -= compression_header_size;
16015 }
16016 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
16017 {
16018 /* Read the zlib header. In this case, it should be "ZLIB"
16019 followed by the uncompressed section size, 8 bytes in
16020 big-endian order. */
16021 uncompressed_size = start[4]; uncompressed_size <<= 8;
16022 uncompressed_size += start[5]; uncompressed_size <<= 8;
16023 uncompressed_size += start[6]; uncompressed_size <<= 8;
16024 uncompressed_size += start[7]; uncompressed_size <<= 8;
16025 uncompressed_size += start[8]; uncompressed_size <<= 8;
16026 uncompressed_size += start[9]; uncompressed_size <<= 8;
16027 uncompressed_size += start[10]; uncompressed_size <<= 8;
16028 uncompressed_size += start[11];
16029 start += 12;
16030 new_size -= 12;
16031 }
16032
16033 if (uncompressed_size)
16034 {
16035 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
16036 &new_size, filedata->file_size))
16037 num_bytes = new_size;
16038 else
16039 {
16040 error (_("Unable to decompress section %s\n"),
16041 printable_section_name (filedata, section));
16042 goto error_out;
16043 }
16044 }
16045 else
16046 start = real_start;
16047 }
16048
16049 /* If the section being dumped has relocations against it the user might
16050 be expecting these relocations to have been applied. Check for this
16051 case and issue a warning message in order to avoid confusion.
16052 FIXME: Maybe we ought to have an option that dumps a section with
16053 relocs applied ? */
16054 for (relsec = filedata->section_headers;
16055 relsec < filedata->section_headers + filedata->file_header.e_shnum;
16056 ++relsec)
16057 {
16058 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
16059 || relsec->sh_info >= filedata->file_header.e_shnum
16060 || filedata->section_headers + relsec->sh_info != section
16061 || relsec->sh_size == 0
16062 || relsec->sh_link >= filedata->file_header.e_shnum)
16063 continue;
16064
16065 printf (_(" Note: This section has relocations against it, but these have NOT been applied to this dump.\n"));
16066 break;
16067 }
16068
16069 data = start;
16070 end = start + num_bytes;
16071 some_strings_shown = false;
16072
16073 #ifdef HAVE_MBSTATE_T
16074 mbstate_t state;
16075 /* Initialise the multibyte conversion state. */
16076 memset (& state, 0, sizeof (state));
16077 #endif
16078
16079 bool continuing = false;
16080
16081 while (data < end)
16082 {
16083 while (!ISPRINT (* data))
16084 if (++ data >= end)
16085 break;
16086
16087 if (data < end)
16088 {
16089 size_t maxlen = end - data;
16090
16091 if (continuing)
16092 {
16093 printf (" ");
16094 continuing = false;
16095 }
16096 else
16097 {
16098 printf (" [%6tx] ", data - start);
16099 }
16100
16101 if (maxlen > 0)
16102 {
16103 char c = 0;
16104
16105 while (maxlen)
16106 {
16107 c = *data++;
16108
16109 if (c == 0)
16110 break;
16111
16112 /* PR 25543: Treat new-lines as string-ending characters. */
16113 if (c == '\n')
16114 {
16115 printf ("\\n\n");
16116 if (*data != 0)
16117 continuing = true;
16118 break;
16119 }
16120
16121 /* Do not print control characters directly as they can affect terminal
16122 settings. Such characters usually appear in the names generated
16123 by the assembler for local labels. */
16124 if (ISCNTRL (c))
16125 {
16126 printf ("^%c", c + 0x40);
16127 }
16128 else if (ISPRINT (c))
16129 {
16130 putchar (c);
16131 }
16132 else
16133 {
16134 size_t n;
16135 #ifdef HAVE_MBSTATE_T
16136 wchar_t w;
16137 #endif
16138 /* Let printf do the hard work of displaying multibyte characters. */
16139 printf ("%.1s", data - 1);
16140 #ifdef HAVE_MBSTATE_T
16141 /* Try to find out how many bytes made up the character that was
16142 just printed. Advance the symbol pointer past the bytes that
16143 were displayed. */
16144 n = mbrtowc (& w, (char *)(data - 1), MB_CUR_MAX, & state);
16145 #else
16146 n = 1;
16147 #endif
16148 if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
16149 data += (n - 1);
16150 }
16151 }
16152
16153 if (c != '\n')
16154 putchar ('\n');
16155 }
16156 else
16157 {
16158 printf (_("<corrupt>\n"));
16159 data = end;
16160 }
16161 some_strings_shown = true;
16162 }
16163 }
16164
16165 if (! some_strings_shown)
16166 printf (_(" No strings found in this section."));
16167
16168 free (real_start);
16169
16170 putchar ('\n');
16171 return true;
16172
16173 error_out:
16174 free (real_start);
16175 return false;
16176 }
16177
16178 static bool
16179 dump_section_as_bytes (Elf_Internal_Shdr *section,
16180 Filedata *filedata,
16181 bool relocate)
16182 {
16183 Elf_Internal_Shdr *relsec;
16184 size_t bytes;
16185 uint64_t section_size;
16186 uint64_t addr;
16187 unsigned char *data;
16188 unsigned char *real_start;
16189 unsigned char *start;
16190
16191 real_start = start = (unsigned char *) get_section_contents (section, filedata);
16192 if (start == NULL)
16193 /* PR 21820: Do not fail if the section was empty. */
16194 return section->sh_size == 0 || section->sh_type == SHT_NOBITS;
16195
16196 section_size = section->sh_size;
16197
16198 if (filedata->is_separate)
16199 printf (_("\nHex dump of section '%s' in linked file %s:\n"),
16200 printable_section_name (filedata, section),
16201 filedata->file_name);
16202 else
16203 printf (_("\nHex dump of section '%s':\n"),
16204 printable_section_name (filedata, section));
16205
16206 if (decompress_dumps)
16207 {
16208 uint64_t new_size = section_size;
16209 uint64_t uncompressed_size = 0;
16210 bool is_zstd = false;
16211
16212 if ((section->sh_flags & SHF_COMPRESSED) != 0)
16213 {
16214 Elf_Internal_Chdr chdr;
16215 unsigned int compression_header_size
16216 = get_compression_header (& chdr, start, section_size);
16217
16218 if (compression_header_size == 0)
16219 /* An error message will have already been generated
16220 by get_compression_header. */
16221 goto error_out;
16222
16223 if (chdr.ch_type == ch_compress_zlib)
16224 ;
16225 #ifdef HAVE_ZSTD
16226 else if (chdr.ch_type == ch_compress_zstd)
16227 is_zstd = true;
16228 #endif
16229 else
16230 {
16231 warn (_("section '%s' has unsupported compress type: %d\n"),
16232 printable_section_name (filedata, section), chdr.ch_type);
16233 goto error_out;
16234 }
16235 uncompressed_size = chdr.ch_size;
16236 start += compression_header_size;
16237 new_size -= compression_header_size;
16238 }
16239 else if (new_size > 12 && streq ((char *) start, "ZLIB"))
16240 {
16241 /* Read the zlib header. In this case, it should be "ZLIB"
16242 followed by the uncompressed section size, 8 bytes in
16243 big-endian order. */
16244 uncompressed_size = start[4]; uncompressed_size <<= 8;
16245 uncompressed_size += start[5]; uncompressed_size <<= 8;
16246 uncompressed_size += start[6]; uncompressed_size <<= 8;
16247 uncompressed_size += start[7]; uncompressed_size <<= 8;
16248 uncompressed_size += start[8]; uncompressed_size <<= 8;
16249 uncompressed_size += start[9]; uncompressed_size <<= 8;
16250 uncompressed_size += start[10]; uncompressed_size <<= 8;
16251 uncompressed_size += start[11];
16252 start += 12;
16253 new_size -= 12;
16254 }
16255
16256 if (uncompressed_size)
16257 {
16258 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
16259 &new_size, filedata->file_size))
16260 {
16261 section_size = new_size;
16262 }
16263 else
16264 {
16265 error (_("Unable to decompress section %s\n"),
16266 printable_section_name (filedata, section));
16267 /* FIXME: Print the section anyway ? */
16268 goto error_out;
16269 }
16270 }
16271 else
16272 start = real_start;
16273 }
16274
16275 if (relocate)
16276 {
16277 if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
16278 goto error_out;
16279 }
16280 else
16281 {
16282 /* If the section being dumped has relocations against it the user might
16283 be expecting these relocations to have been applied. Check for this
16284 case and issue a warning message in order to avoid confusion.
16285 FIXME: Maybe we ought to have an option that dumps a section with
16286 relocs applied ? */
16287 for (relsec = filedata->section_headers;
16288 relsec < filedata->section_headers + filedata->file_header.e_shnum;
16289 ++relsec)
16290 {
16291 if ((relsec->sh_type != SHT_RELA && relsec->sh_type != SHT_REL)
16292 || relsec->sh_info >= filedata->file_header.e_shnum
16293 || filedata->section_headers + relsec->sh_info != section
16294 || relsec->sh_size == 0
16295 || relsec->sh_link >= filedata->file_header.e_shnum)
16296 continue;
16297
16298 printf (_(" NOTE: This section has relocations against it, but these have NOT been applied to this dump.\n"));
16299 break;
16300 }
16301 }
16302
16303 addr = section->sh_addr;
16304 bytes = section_size;
16305 data = start;
16306
16307 while (bytes)
16308 {
16309 int j;
16310 int k;
16311 int lbytes;
16312
16313 lbytes = (bytes > 16 ? 16 : bytes);
16314
16315 printf (" 0x%8.8" PRIx64 " ", addr);
16316
16317 for (j = 0; j < 16; j++)
16318 {
16319 if (j < lbytes)
16320 printf ("%2.2x", data[j]);
16321 else
16322 printf (" ");
16323
16324 if ((j & 3) == 3)
16325 printf (" ");
16326 }
16327
16328 for (j = 0; j < lbytes; j++)
16329 {
16330 k = data[j];
16331 if (k >= ' ' && k < 0x7f)
16332 printf ("%c", k);
16333 else
16334 printf (".");
16335 }
16336
16337 putchar ('\n');
16338
16339 data += lbytes;
16340 addr += lbytes;
16341 bytes -= lbytes;
16342 }
16343
16344 free (real_start);
16345
16346 putchar ('\n');
16347 return true;
16348
16349 error_out:
16350 free (real_start);
16351 return false;
16352 }
16353
16354 #ifdef ENABLE_LIBCTF
16355 static ctf_sect_t *
16356 shdr_to_ctf_sect (ctf_sect_t *buf, Elf_Internal_Shdr *shdr, Filedata *filedata)
16357 {
16358 buf->cts_name = printable_section_name (filedata, shdr);
16359 buf->cts_size = shdr->sh_size;
16360 buf->cts_entsize = shdr->sh_entsize;
16361
16362 return buf;
16363 }
16364
16365 /* Formatting callback function passed to ctf_dump. Returns either the pointer
16366 it is passed, or a pointer to newly-allocated storage, in which case
16367 dump_ctf() will free it when it no longer needs it. */
16368
16369 static char *
16370 dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
16371 char *s, void *arg)
16372 {
16373 const char *blanks = arg;
16374 char *new_s;
16375
16376 if (asprintf (&new_s, "%s%s", blanks, s) < 0)
16377 return s;
16378 return new_s;
16379 }
16380
16381 /* Dump CTF errors/warnings. */
16382 static void
16383 dump_ctf_errs (ctf_dict_t *fp)
16384 {
16385 ctf_next_t *it = NULL;
16386 char *errtext;
16387 int is_warning;
16388 int err;
16389
16390 /* Dump accumulated errors and warnings. */
16391 while ((errtext = ctf_errwarning_next (fp, &it, &is_warning, &err)) != NULL)
16392 {
16393 error (_("%s: %s"), is_warning ? _("warning"): _("error"),
16394 errtext);
16395 free (errtext);
16396 }
16397 if (err != ECTF_NEXT_END)
16398 error (_("CTF error: cannot get CTF errors: `%s'"), ctf_errmsg (err));
16399 }
16400
16401 /* Dump one CTF archive member. */
16402
16403 static void
16404 dump_ctf_archive_member (ctf_dict_t *ctf, const char *name, ctf_dict_t *parent,
16405 size_t member)
16406 {
16407 const char *things[] = {"Header", "Labels", "Data objects",
16408 "Function objects", "Variables", "Types", "Strings",
16409 ""};
16410 const char **thing;
16411 size_t i;
16412
16413 /* Don't print out the name of the default-named archive member if it appears
16414 first in the list. The name .ctf appears everywhere, even for things that
16415 aren't really archives, so printing it out is liable to be confusing; also,
16416 the common case by far is for only one archive member to exist, and hiding
16417 it in that case seems worthwhile. */
16418
16419 if (strcmp (name, ".ctf") != 0 || member != 0)
16420 printf (_("\nCTF archive member: %s:\n"), name);
16421
16422 if (ctf_parent_name (ctf) != NULL)
16423 ctf_import (ctf, parent);
16424
16425 for (i = 0, thing = things; *thing[0]; thing++, i++)
16426 {
16427 ctf_dump_state_t *s = NULL;
16428 char *item;
16429
16430 printf ("\n %s:\n", *thing);
16431 while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
16432 (void *) " ")) != NULL)
16433 {
16434 printf ("%s\n", item);
16435 free (item);
16436 }
16437
16438 if (ctf_errno (ctf))
16439 {
16440 error (_("Iteration failed: %s, %s\n"), *thing,
16441 ctf_errmsg (ctf_errno (ctf)));
16442 break;
16443 }
16444 }
16445
16446 dump_ctf_errs (ctf);
16447 }
16448
16449 static bool
16450 dump_section_as_ctf (Elf_Internal_Shdr * section, Filedata * filedata)
16451 {
16452 Elf_Internal_Shdr * symtab_sec = NULL;
16453 Elf_Internal_Shdr * strtab_sec = NULL;
16454 void * data = NULL;
16455 void * symdata = NULL;
16456 void * strdata = NULL;
16457 ctf_sect_t ctfsect, symsect, strsect;
16458 ctf_sect_t * symsectp = NULL;
16459 ctf_sect_t * strsectp = NULL;
16460 ctf_archive_t * ctfa = NULL;
16461 ctf_dict_t * parent = NULL;
16462 ctf_dict_t * fp;
16463
16464 ctf_next_t *i = NULL;
16465 const char *name;
16466 size_t member = 0;
16467 int err;
16468 bool ret = false;
16469
16470 shdr_to_ctf_sect (&ctfsect, section, filedata);
16471 data = get_section_contents (section, filedata);
16472 ctfsect.cts_data = data;
16473
16474 if (!dump_ctf_symtab_name)
16475 dump_ctf_symtab_name = strdup (".dynsym");
16476
16477 if (!dump_ctf_strtab_name)
16478 dump_ctf_strtab_name = strdup (".dynstr");
16479
16480 if (dump_ctf_symtab_name && dump_ctf_symtab_name[0] != 0)
16481 {
16482 if ((symtab_sec = find_section (filedata, dump_ctf_symtab_name)) == NULL)
16483 {
16484 error (_("No symbol section named %s\n"), dump_ctf_symtab_name);
16485 goto fail;
16486 }
16487 if ((symdata = (void *) get_data (NULL, filedata,
16488 symtab_sec->sh_offset, 1,
16489 symtab_sec->sh_size,
16490 _("symbols"))) == NULL)
16491 goto fail;
16492 symsectp = shdr_to_ctf_sect (&symsect, symtab_sec, filedata);
16493 symsect.cts_data = symdata;
16494 }
16495
16496 if (dump_ctf_strtab_name && dump_ctf_strtab_name[0] != 0)
16497 {
16498 if ((strtab_sec = find_section (filedata, dump_ctf_strtab_name)) == NULL)
16499 {
16500 error (_("No string table section named %s\n"),
16501 dump_ctf_strtab_name);
16502 goto fail;
16503 }
16504 if ((strdata = (void *) get_data (NULL, filedata,
16505 strtab_sec->sh_offset, 1,
16506 strtab_sec->sh_size,
16507 _("strings"))) == NULL)
16508 goto fail;
16509 strsectp = shdr_to_ctf_sect (&strsect, strtab_sec, filedata);
16510 strsect.cts_data = strdata;
16511 }
16512
16513 /* Load the CTF file and dump it. It may be a raw CTF section, or an archive:
16514 libctf papers over the difference, so we can pretend it is always an
16515 archive. */
16516
16517 if ((ctfa = ctf_arc_bufopen (&ctfsect, symsectp, strsectp, &err)) == NULL)
16518 {
16519 dump_ctf_errs (NULL);
16520 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
16521 goto fail;
16522 }
16523
16524 ctf_arc_symsect_endianness (ctfa, filedata->file_header.e_ident[EI_DATA]
16525 != ELFDATA2MSB);
16526
16527 /* Preload the parent dict, since it will need to be imported into every
16528 child in turn. */
16529 if ((parent = ctf_dict_open (ctfa, dump_ctf_parent_name, &err)) == NULL)
16530 {
16531 dump_ctf_errs (NULL);
16532 error (_("CTF open failure: %s\n"), ctf_errmsg (err));
16533 goto fail;
16534 }
16535
16536 ret = true;
16537
16538 if (filedata->is_separate)
16539 printf (_("\nDump of CTF section '%s' in linked file %s:\n"),
16540 printable_section_name (filedata, section),
16541 filedata->file_name);
16542 else
16543 printf (_("\nDump of CTF section '%s':\n"),
16544 printable_section_name (filedata, section));
16545
16546 while ((fp = ctf_archive_next (ctfa, &i, &name, 0, &err)) != NULL)
16547 dump_ctf_archive_member (fp, name, parent, member++);
16548 if (err != ECTF_NEXT_END)
16549 {
16550 dump_ctf_errs (NULL);
16551 error (_("CTF member open failure: %s\n"), ctf_errmsg (err));
16552 ret = false;
16553 }
16554
16555 fail:
16556 ctf_dict_close (parent);
16557 ctf_close (ctfa);
16558 free (data);
16559 free (symdata);
16560 free (strdata);
16561 return ret;
16562 }
16563 #endif
16564
16565 static bool
16566 dump_section_as_sframe (Elf_Internal_Shdr * section, Filedata * filedata)
16567 {
16568 void * data = NULL;
16569 sframe_decoder_ctx *sfd_ctx = NULL;
16570 const char *print_name = printable_section_name (filedata, section);
16571
16572 bool ret = true;
16573 size_t sf_size;
16574 int err = 0;
16575
16576 if (strcmp (print_name, "") == 0)
16577 {
16578 error (_("Section name must be provided \n"));
16579 ret = false;
16580 return ret;
16581 }
16582
16583 data = get_section_contents (section, filedata);
16584 sf_size = section->sh_size;
16585 /* Decode the contents of the section. */
16586 sfd_ctx = sframe_decode ((const char*)data, sf_size, &err);
16587 if (!sfd_ctx)
16588 {
16589 ret = false;
16590 error (_("SFrame decode failure: %s\n"), sframe_errmsg (err));
16591 goto fail;
16592 }
16593
16594 printf (_("Contents of the SFrame section %s:"), print_name);
16595 /* Dump the contents as text. */
16596 dump_sframe (sfd_ctx, section->sh_addr);
16597
16598 fail:
16599 free (data);
16600 return ret;
16601 }
16602
16603 static bool
16604 load_specific_debug_section (enum dwarf_section_display_enum debug,
16605 const Elf_Internal_Shdr * sec,
16606 void * data)
16607 {
16608 struct dwarf_section * section = &debug_displays [debug].section;
16609 char buf [64];
16610 Filedata * filedata = (Filedata *) data;
16611
16612 if (section->start != NULL)
16613 {
16614 /* If it is already loaded, do nothing. */
16615 if (streq (section->filename, filedata->file_name))
16616 return true;
16617 free (section->start);
16618 }
16619
16620 snprintf (buf, sizeof (buf), _("%s section data"), section->name);
16621 section->address = sec->sh_addr;
16622 section->filename = filedata->file_name;
16623 section->start = (unsigned char *) get_data (NULL, filedata,
16624 sec->sh_offset, 1,
16625 sec->sh_size, buf);
16626 if (section->start == NULL)
16627 section->size = 0;
16628 else
16629 {
16630 unsigned char *start = section->start;
16631 uint64_t size = sec->sh_size;
16632 uint64_t uncompressed_size = 0;
16633 bool is_zstd = false;
16634
16635 if ((sec->sh_flags & SHF_COMPRESSED) != 0)
16636 {
16637 Elf_Internal_Chdr chdr;
16638 unsigned int compression_header_size;
16639
16640 if (size < (is_32bit_elf
16641 ? sizeof (Elf32_External_Chdr)
16642 : sizeof (Elf64_External_Chdr)))
16643 {
16644 warn (_("compressed section %s is too small to contain a compression header\n"),
16645 section->name);
16646 return false;
16647 }
16648
16649 compression_header_size = get_compression_header (&chdr, start, size);
16650 if (compression_header_size == 0)
16651 /* An error message will have already been generated
16652 by get_compression_header. */
16653 return false;
16654
16655 if (chdr.ch_type == ch_compress_zlib)
16656 ;
16657 #ifdef HAVE_ZSTD
16658 else if (chdr.ch_type == ch_compress_zstd)
16659 is_zstd = true;
16660 #endif
16661 else
16662 {
16663 warn (_("section '%s' has unsupported compress type: %d\n"),
16664 section->name, chdr.ch_type);
16665 return false;
16666 }
16667 uncompressed_size = chdr.ch_size;
16668 start += compression_header_size;
16669 size -= compression_header_size;
16670 }
16671 else if (size > 12 && streq ((char *) start, "ZLIB"))
16672 {
16673 /* Read the zlib header. In this case, it should be "ZLIB"
16674 followed by the uncompressed section size, 8 bytes in
16675 big-endian order. */
16676 uncompressed_size = start[4]; uncompressed_size <<= 8;
16677 uncompressed_size += start[5]; uncompressed_size <<= 8;
16678 uncompressed_size += start[6]; uncompressed_size <<= 8;
16679 uncompressed_size += start[7]; uncompressed_size <<= 8;
16680 uncompressed_size += start[8]; uncompressed_size <<= 8;
16681 uncompressed_size += start[9]; uncompressed_size <<= 8;
16682 uncompressed_size += start[10]; uncompressed_size <<= 8;
16683 uncompressed_size += start[11];
16684 start += 12;
16685 size -= 12;
16686 }
16687
16688 if (uncompressed_size)
16689 {
16690 if (uncompress_section_contents (is_zstd, &start, uncompressed_size,
16691 &size, filedata->file_size))
16692 {
16693 /* Free the compressed buffer, update the section buffer
16694 and the section size if uncompress is successful. */
16695 free (section->start);
16696 section->start = start;
16697 }
16698 else
16699 {
16700 error (_("Unable to decompress section %s\n"),
16701 printable_section_name (filedata, sec));
16702 return false;
16703 }
16704 }
16705
16706 section->size = size;
16707 }
16708
16709 if (section->start == NULL)
16710 return false;
16711
16712 if (debug_displays [debug].relocate)
16713 {
16714 if (! apply_relocations (filedata, sec, section->start, section->size,
16715 & section->reloc_info, & section->num_relocs))
16716 return false;
16717 }
16718 else
16719 {
16720 section->reloc_info = NULL;
16721 section->num_relocs = 0;
16722 }
16723
16724 return true;
16725 }
16726
16727 #if HAVE_LIBDEBUGINFOD
16728 /* Return a hex string representation of the build-id. */
16729 unsigned char *
16730 get_build_id (void * data)
16731 {
16732 Filedata * filedata = (Filedata *) data;
16733 Elf_Internal_Shdr * shdr;
16734 size_t i;
16735
16736 /* Iterate through notes to find note.gnu.build-id.
16737 FIXME: Only the first note in any note section is examined. */
16738 for (i = 0, shdr = filedata->section_headers;
16739 i < filedata->file_header.e_shnum && shdr != NULL;
16740 i++, shdr++)
16741 {
16742 if (shdr->sh_type != SHT_NOTE)
16743 continue;
16744
16745 char * next;
16746 char * end;
16747 size_t data_remaining;
16748 size_t min_notesz;
16749 Elf_External_Note * enote;
16750 Elf_Internal_Note inote;
16751
16752 uint64_t offset = shdr->sh_offset;
16753 uint64_t align = shdr->sh_addralign;
16754 uint64_t length = shdr->sh_size;
16755
16756 enote = (Elf_External_Note *) get_section_contents (shdr, filedata);
16757 if (enote == NULL)
16758 continue;
16759
16760 if (align < 4)
16761 align = 4;
16762 else if (align != 4 && align != 8)
16763 {
16764 free (enote);
16765 continue;
16766 }
16767
16768 end = (char *) enote + length;
16769 data_remaining = end - (char *) enote;
16770
16771 if (!is_ia64_vms (filedata))
16772 {
16773 min_notesz = offsetof (Elf_External_Note, name);
16774 if (data_remaining < min_notesz)
16775 {
16776 warn (_("\
16777 malformed note encountered in section %s whilst scanning for build-id note\n"),
16778 printable_section_name (filedata, shdr));
16779 free (enote);
16780 continue;
16781 }
16782 data_remaining -= min_notesz;
16783
16784 inote.type = BYTE_GET (enote->type);
16785 inote.namesz = BYTE_GET (enote->namesz);
16786 inote.namedata = enote->name;
16787 inote.descsz = BYTE_GET (enote->descsz);
16788 inote.descdata = ((char *) enote
16789 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
16790 inote.descpos = offset + (inote.descdata - (char *) enote);
16791 next = ((char *) enote
16792 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
16793 }
16794 else
16795 {
16796 Elf64_External_VMS_Note *vms_enote;
16797
16798 /* PR binutils/15191
16799 Make sure that there is enough data to read. */
16800 min_notesz = offsetof (Elf64_External_VMS_Note, name);
16801 if (data_remaining < min_notesz)
16802 {
16803 warn (_("\
16804 malformed note encountered in section %s whilst scanning for build-id note\n"),
16805 printable_section_name (filedata, shdr));
16806 free (enote);
16807 continue;
16808 }
16809 data_remaining -= min_notesz;
16810
16811 vms_enote = (Elf64_External_VMS_Note *) enote;
16812 inote.type = BYTE_GET (vms_enote->type);
16813 inote.namesz = BYTE_GET (vms_enote->namesz);
16814 inote.namedata = vms_enote->name;
16815 inote.descsz = BYTE_GET (vms_enote->descsz);
16816 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
16817 inote.descpos = offset + (inote.descdata - (char *) enote);
16818 next = inote.descdata + align_power (inote.descsz, 3);
16819 }
16820
16821 /* Skip malformed notes. */
16822 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
16823 || (size_t) (inote.descdata - inote.namedata) > data_remaining
16824 || (size_t) (next - inote.descdata) < inote.descsz
16825 || ((size_t) (next - inote.descdata)
16826 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
16827 {
16828 warn (_("\
16829 malformed note encountered in section %s whilst scanning for build-id note\n"),
16830 printable_section_name (filedata, shdr));
16831 free (enote);
16832 continue;
16833 }
16834
16835 /* Check if this is the build-id note. If so then convert the build-id
16836 bytes to a hex string. */
16837 if (inote.namesz > 0
16838 && startswith (inote.namedata, "GNU")
16839 && inote.type == NT_GNU_BUILD_ID)
16840 {
16841 size_t j;
16842 char * build_id;
16843
16844 build_id = malloc (inote.descsz * 2 + 1);
16845 if (build_id == NULL)
16846 {
16847 free (enote);
16848 return NULL;
16849 }
16850
16851 for (j = 0; j < inote.descsz; ++j)
16852 sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
16853 build_id[inote.descsz * 2] = '\0';
16854 free (enote);
16855
16856 return (unsigned char *) build_id;
16857 }
16858 free (enote);
16859 }
16860
16861 return NULL;
16862 }
16863 #endif /* HAVE_LIBDEBUGINFOD */
16864
16865 /* If this is not NULL, load_debug_section will only look for sections
16866 within the list of sections given here. */
16867 static unsigned int * section_subset = NULL;
16868
16869 bool
16870 load_debug_section (enum dwarf_section_display_enum debug, void * data)
16871 {
16872 struct dwarf_section * section = &debug_displays [debug].section;
16873 Elf_Internal_Shdr * sec;
16874 Filedata * filedata = (Filedata *) data;
16875
16876 if (!dump_any_debugging)
16877 return false;
16878
16879 /* Without section headers we cannot find any sections. */
16880 if (filedata->section_headers == NULL)
16881 return false;
16882
16883 if (filedata->string_table == NULL
16884 && filedata->file_header.e_shstrndx != SHN_UNDEF
16885 && filedata->file_header.e_shstrndx < filedata->file_header.e_shnum)
16886 {
16887 Elf_Internal_Shdr * strs;
16888
16889 /* Read in the string table, so that we have section names to scan. */
16890 strs = filedata->section_headers + filedata->file_header.e_shstrndx;
16891
16892 if (strs != NULL && strs->sh_size != 0)
16893 {
16894 filedata->string_table
16895 = (char *) get_data (NULL, filedata, strs->sh_offset,
16896 1, strs->sh_size, _("string table"));
16897
16898 filedata->string_table_length
16899 = filedata->string_table != NULL ? strs->sh_size : 0;
16900 }
16901 }
16902
16903 /* Locate the debug section. */
16904 sec = find_section_in_set (filedata, section->uncompressed_name, section_subset);
16905 if (sec != NULL)
16906 section->name = section->uncompressed_name;
16907 else
16908 {
16909 sec = find_section_in_set (filedata, section->compressed_name, section_subset);
16910 if (sec != NULL)
16911 section->name = section->compressed_name;
16912 }
16913 if (sec == NULL)
16914 return false;
16915
16916 /* If we're loading from a subset of sections, and we've loaded
16917 a section matching this name before, it's likely that it's a
16918 different one. */
16919 if (section_subset != NULL)
16920 free_debug_section (debug);
16921
16922 return load_specific_debug_section (debug, sec, data);
16923 }
16924
16925 void
16926 free_debug_section (enum dwarf_section_display_enum debug)
16927 {
16928 struct dwarf_section * section = &debug_displays [debug].section;
16929
16930 if (section->start == NULL)
16931 return;
16932
16933 free ((char *) section->start);
16934 section->start = NULL;
16935 section->address = 0;
16936 section->size = 0;
16937
16938 free (section->reloc_info);
16939 section->reloc_info = NULL;
16940 section->num_relocs = 0;
16941 }
16942
16943 static bool
16944 display_debug_section (int shndx, Elf_Internal_Shdr * section, Filedata * filedata)
16945 {
16946 const char *name = (section_name_valid (filedata, section)
16947 ? section_name (filedata, section) : "");
16948 const char *print_name = printable_section_name (filedata, section);
16949 uint64_t length;
16950 bool result = true;
16951 int i;
16952
16953 length = section->sh_size;
16954 if (length == 0)
16955 {
16956 printf (_("\nSection '%s' has no debugging data.\n"), print_name);
16957 return true;
16958 }
16959 if (section->sh_type == SHT_NOBITS)
16960 {
16961 /* There is no point in dumping the contents of a debugging section
16962 which has the NOBITS type - the bits in the file will be random.
16963 This can happen when a file containing a .eh_frame section is
16964 stripped with the --only-keep-debug command line option. */
16965 printf (_("section '%s' has the NOBITS type - its contents are unreliable.\n"),
16966 print_name);
16967 return false;
16968 }
16969
16970 if (startswith (name, ".gnu.linkonce.wi."))
16971 name = ".debug_info";
16972
16973 /* See if we know how to display the contents of this section. */
16974 for (i = 0; i < max; i++)
16975 {
16976 enum dwarf_section_display_enum id = (enum dwarf_section_display_enum) i;
16977 struct dwarf_section_display * display = debug_displays + i;
16978 struct dwarf_section * sec = & display->section;
16979
16980 if (streq (sec->uncompressed_name, name)
16981 || (id == line && startswith (name, ".debug_line."))
16982 || streq (sec->compressed_name, name))
16983 {
16984 bool secondary = (section != find_section (filedata, name));
16985
16986 if (secondary)
16987 free_debug_section (id);
16988
16989 if (i == line && startswith (name, ".debug_line."))
16990 sec->name = name;
16991 else if (streq (sec->uncompressed_name, name))
16992 sec->name = sec->uncompressed_name;
16993 else
16994 sec->name = sec->compressed_name;
16995
16996 if (load_specific_debug_section (id, section, filedata))
16997 {
16998 /* If this debug section is part of a CU/TU set in a .dwp file,
16999 restrict load_debug_section to the sections in that set. */
17000 section_subset = find_cu_tu_set (filedata, shndx);
17001
17002 result &= display->display (sec, filedata);
17003
17004 section_subset = NULL;
17005
17006 if (secondary || (id != info && id != abbrev && id != debug_addr))
17007 free_debug_section (id);
17008 }
17009 break;
17010 }
17011 }
17012
17013 if (i == max)
17014 {
17015 printf (_("Unrecognized debug section: %s\n"), print_name);
17016 result = false;
17017 }
17018
17019 return result;
17020 }
17021
17022 /* Set DUMP_SECTS for all sections where dumps were requested
17023 based on section name. */
17024
17025 static void
17026 initialise_dumps_byname (Filedata * filedata)
17027 {
17028 struct dump_list_entry * cur;
17029
17030 for (cur = dump_sects_byname; cur; cur = cur->next)
17031 {
17032 unsigned int i;
17033 bool any = false;
17034
17035 for (i = 0; i < filedata->file_header.e_shnum; i++)
17036 if (section_name_valid (filedata, filedata->section_headers + i)
17037 && streq (section_name (filedata, filedata->section_headers + i),
17038 cur->name))
17039 {
17040 request_dump_bynumber (&filedata->dump, i, cur->type);
17041 any = true;
17042 }
17043
17044 if (!any && !filedata->is_separate)
17045 warn (_("Section '%s' was not dumped because it does not exist\n"),
17046 cur->name);
17047 }
17048 }
17049
17050 static bool
17051 process_section_contents (Filedata * filedata)
17052 {
17053 Elf_Internal_Shdr * section;
17054 unsigned int i;
17055 bool res = true;
17056
17057 if (! do_dump)
17058 return true;
17059
17060 initialise_dumps_byname (filedata);
17061
17062 for (i = 0, section = filedata->section_headers;
17063 i < filedata->file_header.e_shnum && i < filedata->dump.num_dump_sects;
17064 i++, section++)
17065 {
17066 dump_type dump = filedata->dump.dump_sects[i];
17067
17068 if (filedata->is_separate && ! process_links)
17069 dump &= DEBUG_DUMP;
17070
17071 #ifdef SUPPORT_DISASSEMBLY
17072 if (dump & DISASS_DUMP)
17073 {
17074 if (! disassemble_section (section, filedata))
17075 res = false;
17076 }
17077 #endif
17078 if (dump & HEX_DUMP)
17079 {
17080 if (! dump_section_as_bytes (section, filedata, false))
17081 res = false;
17082 }
17083
17084 if (dump & RELOC_DUMP)
17085 {
17086 if (! dump_section_as_bytes (section, filedata, true))
17087 res = false;
17088 }
17089
17090 if (dump & STRING_DUMP)
17091 {
17092 if (! dump_section_as_strings (section, filedata))
17093 res = false;
17094 }
17095
17096 if (dump & DEBUG_DUMP)
17097 {
17098 if (! display_debug_section (i, section, filedata))
17099 res = false;
17100 }
17101
17102 #ifdef ENABLE_LIBCTF
17103 if (dump & CTF_DUMP)
17104 {
17105 if (! dump_section_as_ctf (section, filedata))
17106 res = false;
17107 }
17108 #endif
17109 if (dump & SFRAME_DUMP)
17110 {
17111 if (! dump_section_as_sframe (section, filedata))
17112 res = false;
17113 }
17114 }
17115
17116 if (! filedata->is_separate)
17117 {
17118 /* Check to see if the user requested a
17119 dump of a section that does not exist. */
17120 for (; i < filedata->dump.num_dump_sects; i++)
17121 if (filedata->dump.dump_sects[i])
17122 {
17123 warn (_("Section %d was not dumped because it does not exist!\n"), i);
17124 res = false;
17125 }
17126 }
17127
17128 return res;
17129 }
17130
17131 static void
17132 process_mips_fpe_exception (int mask)
17133 {
17134 if (mask)
17135 {
17136 bool first = true;
17137
17138 if (mask & OEX_FPU_INEX)
17139 fputs ("INEX", stdout), first = false;
17140 if (mask & OEX_FPU_UFLO)
17141 printf ("%sUFLO", first ? "" : "|"), first = false;
17142 if (mask & OEX_FPU_OFLO)
17143 printf ("%sOFLO", first ? "" : "|"), first = false;
17144 if (mask & OEX_FPU_DIV0)
17145 printf ("%sDIV0", first ? "" : "|"), first = false;
17146 if (mask & OEX_FPU_INVAL)
17147 printf ("%sINVAL", first ? "" : "|");
17148 }
17149 else
17150 fputs ("0", stdout);
17151 }
17152
17153 /* Display's the value of TAG at location P. If TAG is
17154 greater than 0 it is assumed to be an unknown tag, and
17155 a message is printed to this effect. Otherwise it is
17156 assumed that a message has already been printed.
17157
17158 If the bottom bit of TAG is set it assumed to have a
17159 string value, otherwise it is assumed to have an integer
17160 value.
17161
17162 Returns an updated P pointing to the first unread byte
17163 beyond the end of TAG's value.
17164
17165 Reads at or beyond END will not be made. */
17166
17167 static unsigned char *
17168 display_tag_value (signed int tag,
17169 unsigned char * p,
17170 const unsigned char * const end)
17171 {
17172 uint64_t val;
17173
17174 if (tag > 0)
17175 printf (" Tag_unknown_%d: ", tag);
17176
17177 if (p >= end)
17178 {
17179 warn (_("<corrupt tag>\n"));
17180 }
17181 else if (tag & 1)
17182 {
17183 /* PR 17531 file: 027-19978-0.004. */
17184 size_t maxlen = (end - p) - 1;
17185
17186 putchar ('"');
17187 if (maxlen > 0)
17188 {
17189 print_symbol_name ((int) maxlen, (const char *) p);
17190 p += strnlen ((char *) p, maxlen) + 1;
17191 }
17192 else
17193 {
17194 printf (_("<corrupt string tag>"));
17195 p = (unsigned char *) end;
17196 }
17197 printf ("\"\n");
17198 }
17199 else
17200 {
17201 READ_ULEB (val, p, end);
17202 printf ("%" PRId64 " (0x%" PRIx64 ")\n", val, val);
17203 }
17204
17205 assert (p <= end);
17206 return p;
17207 }
17208
17209 /* ARC ABI attributes section. */
17210
17211 static unsigned char *
17212 display_arc_attribute (unsigned char * p,
17213 const unsigned char * const end)
17214 {
17215 unsigned int tag;
17216 unsigned int val;
17217
17218 READ_ULEB (tag, p, end);
17219
17220 switch (tag)
17221 {
17222 case Tag_ARC_PCS_config:
17223 READ_ULEB (val, p, end);
17224 printf (" Tag_ARC_PCS_config: ");
17225 switch (val)
17226 {
17227 case 0:
17228 printf (_("Absent/Non standard\n"));
17229 break;
17230 case 1:
17231 printf (_("Bare metal/mwdt\n"));
17232 break;
17233 case 2:
17234 printf (_("Bare metal/newlib\n"));
17235 break;
17236 case 3:
17237 printf (_("Linux/uclibc\n"));
17238 break;
17239 case 4:
17240 printf (_("Linux/glibc\n"));
17241 break;
17242 default:
17243 printf (_("Unknown\n"));
17244 break;
17245 }
17246 break;
17247
17248 case Tag_ARC_CPU_base:
17249 READ_ULEB (val, p, end);
17250 printf (" Tag_ARC_CPU_base: ");
17251 switch (val)
17252 {
17253 default:
17254 case TAG_CPU_NONE:
17255 printf (_("Absent\n"));
17256 break;
17257 case TAG_CPU_ARC6xx:
17258 printf ("ARC6xx\n");
17259 break;
17260 case TAG_CPU_ARC7xx:
17261 printf ("ARC7xx\n");
17262 break;
17263 case TAG_CPU_ARCEM:
17264 printf ("ARCEM\n");
17265 break;
17266 case TAG_CPU_ARCHS:
17267 printf ("ARCHS\n");
17268 break;
17269 }
17270 break;
17271
17272 case Tag_ARC_CPU_variation:
17273 READ_ULEB (val, p, end);
17274 printf (" Tag_ARC_CPU_variation: ");
17275 switch (val)
17276 {
17277 default:
17278 if (val > 0 && val < 16)
17279 printf ("Core%d\n", val);
17280 else
17281 printf ("Unknown\n");
17282 break;
17283
17284 case 0:
17285 printf (_("Absent\n"));
17286 break;
17287 }
17288 break;
17289
17290 case Tag_ARC_CPU_name:
17291 printf (" Tag_ARC_CPU_name: ");
17292 p = display_tag_value (-1, p, end);
17293 break;
17294
17295 case Tag_ARC_ABI_rf16:
17296 READ_ULEB (val, p, end);
17297 printf (" Tag_ARC_ABI_rf16: %s\n", val ? _("yes") : _("no"));
17298 break;
17299
17300 case Tag_ARC_ABI_osver:
17301 READ_ULEB (val, p, end);
17302 printf (" Tag_ARC_ABI_osver: v%d\n", val);
17303 break;
17304
17305 case Tag_ARC_ABI_pic:
17306 case Tag_ARC_ABI_sda:
17307 READ_ULEB (val, p, end);
17308 printf (tag == Tag_ARC_ABI_sda ? " Tag_ARC_ABI_sda: "
17309 : " Tag_ARC_ABI_pic: ");
17310 switch (val)
17311 {
17312 case 0:
17313 printf (_("Absent\n"));
17314 break;
17315 case 1:
17316 printf ("MWDT\n");
17317 break;
17318 case 2:
17319 printf ("GNU\n");
17320 break;
17321 default:
17322 printf (_("Unknown\n"));
17323 break;
17324 }
17325 break;
17326
17327 case Tag_ARC_ABI_tls:
17328 READ_ULEB (val, p, end);
17329 printf (" Tag_ARC_ABI_tls: %s\n", val ? "r25": "none");
17330 break;
17331
17332 case Tag_ARC_ABI_enumsize:
17333 READ_ULEB (val, p, end);
17334 printf (" Tag_ARC_ABI_enumsize: %s\n", val ? _("default") :
17335 _("smallest"));
17336 break;
17337
17338 case Tag_ARC_ABI_exceptions:
17339 READ_ULEB (val, p, end);
17340 printf (" Tag_ARC_ABI_exceptions: %s\n", val ? _("OPTFP")
17341 : _("default"));
17342 break;
17343
17344 case Tag_ARC_ABI_double_size:
17345 READ_ULEB (val, p, end);
17346 printf (" Tag_ARC_ABI_double_size: %d\n", val);
17347 break;
17348
17349 case Tag_ARC_ISA_config:
17350 printf (" Tag_ARC_ISA_config: ");
17351 p = display_tag_value (-1, p, end);
17352 break;
17353
17354 case Tag_ARC_ISA_apex:
17355 printf (" Tag_ARC_ISA_apex: ");
17356 p = display_tag_value (-1, p, end);
17357 break;
17358
17359 case Tag_ARC_ISA_mpy_option:
17360 READ_ULEB (val, p, end);
17361 printf (" Tag_ARC_ISA_mpy_option: %d\n", val);
17362 break;
17363
17364 case Tag_ARC_ATR_version:
17365 READ_ULEB (val, p, end);
17366 printf (" Tag_ARC_ATR_version: %d\n", val);
17367 break;
17368
17369 default:
17370 return display_tag_value (tag & 1, p, end);
17371 }
17372
17373 return p;
17374 }
17375
17376 /* ARM EABI attributes section. */
17377 typedef struct
17378 {
17379 unsigned int tag;
17380 const char * name;
17381 /* 0 = special, 1 = string, 2 = uleb123, > 0x80 == table lookup. */
17382 unsigned int type;
17383 const char *const *table;
17384 } arm_attr_public_tag;
17385
17386 static const char *const arm_attr_tag_CPU_arch[] =
17387 {"Pre-v4", "v4", "v4T", "v5T", "v5TE", "v5TEJ", "v6", "v6KZ", "v6T2",
17388 "v6K", "v7", "v6-M", "v6S-M", "v7E-M", "v8", "v8-R", "v8-M.baseline",
17389 "v8-M.mainline", "v8.1-A", "v8.2-A", "v8.3-A",
17390 "v8.1-M.mainline", "v9"};
17391 static const char *const arm_attr_tag_ARM_ISA_use[] = {"No", "Yes"};
17392 static const char *const arm_attr_tag_THUMB_ISA_use[] =
17393 {"No", "Thumb-1", "Thumb-2", "Yes"};
17394 static const char *const arm_attr_tag_FP_arch[] =
17395 {"No", "VFPv1", "VFPv2", "VFPv3", "VFPv3-D16", "VFPv4", "VFPv4-D16",
17396 "FP for ARMv8", "FPv5/FP-D16 for ARMv8"};
17397 static const char *const arm_attr_tag_WMMX_arch[] = {"No", "WMMXv1", "WMMXv2"};
17398 static const char *const arm_attr_tag_Advanced_SIMD_arch[] =
17399 {"No", "NEONv1", "NEONv1 with Fused-MAC", "NEON for ARMv8",
17400 "NEON for ARMv8.1"};
17401 static const char *const arm_attr_tag_PCS_config[] =
17402 {"None", "Bare platform", "Linux application", "Linux DSO", "PalmOS 2004",
17403 "PalmOS (reserved)", "SymbianOS 2004", "SymbianOS (reserved)"};
17404 static const char *const arm_attr_tag_ABI_PCS_R9_use[] =
17405 {"V6", "SB", "TLS", "Unused"};
17406 static const char *const arm_attr_tag_ABI_PCS_RW_data[] =
17407 {"Absolute", "PC-relative", "SB-relative", "None"};
17408 static const char *const arm_attr_tag_ABI_PCS_RO_data[] =
17409 {"Absolute", "PC-relative", "None"};
17410 static const char *const arm_attr_tag_ABI_PCS_GOT_use[] =
17411 {"None", "direct", "GOT-indirect"};
17412 static const char *const arm_attr_tag_ABI_PCS_wchar_t[] =
17413 {"None", "??? 1", "2", "??? 3", "4"};
17414 static const char *const arm_attr_tag_ABI_FP_rounding[] = {"Unused", "Needed"};
17415 static const char *const arm_attr_tag_ABI_FP_denormal[] =
17416 {"Unused", "Needed", "Sign only"};
17417 static const char *const arm_attr_tag_ABI_FP_exceptions[] = {"Unused", "Needed"};
17418 static const char *const arm_attr_tag_ABI_FP_user_exceptions[] = {"Unused", "Needed"};
17419 static const char *const arm_attr_tag_ABI_FP_number_model[] =
17420 {"Unused", "Finite", "RTABI", "IEEE 754"};
17421 static const char *const arm_attr_tag_ABI_enum_size[] =
17422 {"Unused", "small", "int", "forced to int"};
17423 static const char *const arm_attr_tag_ABI_HardFP_use[] =
17424 {"As Tag_FP_arch", "SP only", "Reserved", "Deprecated"};
17425 static const char *const arm_attr_tag_ABI_VFP_args[] =
17426 {"AAPCS", "VFP registers", "custom", "compatible"};
17427 static const char *const arm_attr_tag_ABI_WMMX_args[] =
17428 {"AAPCS", "WMMX registers", "custom"};
17429 static const char *const arm_attr_tag_ABI_optimization_goals[] =
17430 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
17431 "Aggressive Size", "Prefer Debug", "Aggressive Debug"};
17432 static const char *const arm_attr_tag_ABI_FP_optimization_goals[] =
17433 {"None", "Prefer Speed", "Aggressive Speed", "Prefer Size",
17434 "Aggressive Size", "Prefer Accuracy", "Aggressive Accuracy"};
17435 static const char *const arm_attr_tag_CPU_unaligned_access[] = {"None", "v6"};
17436 static const char *const arm_attr_tag_FP_HP_extension[] =
17437 {"Not Allowed", "Allowed"};
17438 static const char *const arm_attr_tag_ABI_FP_16bit_format[] =
17439 {"None", "IEEE 754", "Alternative Format"};
17440 static const char *const arm_attr_tag_DSP_extension[] =
17441 {"Follow architecture", "Allowed"};
17442 static const char *const arm_attr_tag_MPextension_use[] =
17443 {"Not Allowed", "Allowed"};
17444 static const char *const arm_attr_tag_DIV_use[] =
17445 {"Allowed in Thumb-ISA, v7-R or v7-M", "Not allowed",
17446 "Allowed in v7-A with integer division extension"};
17447 static const char *const arm_attr_tag_T2EE_use[] = {"Not Allowed", "Allowed"};
17448 static const char *const arm_attr_tag_Virtualization_use[] =
17449 {"Not Allowed", "TrustZone", "Virtualization Extensions",
17450 "TrustZone and Virtualization Extensions"};
17451 static const char *const arm_attr_tag_MPextension_use_legacy[] =
17452 {"Not Allowed", "Allowed"};
17453
17454 static const char *const arm_attr_tag_MVE_arch[] =
17455 {"No MVE", "MVE Integer only", "MVE Integer and FP"};
17456
17457 static const char * arm_attr_tag_PAC_extension[] =
17458 {"No PAC/AUT instructions",
17459 "PAC/AUT instructions permitted in the NOP space",
17460 "PAC/AUT instructions permitted in the NOP and in the non-NOP space"};
17461
17462 static const char * arm_attr_tag_BTI_extension[] =
17463 {"BTI instructions not permitted",
17464 "BTI instructions permitted in the NOP space",
17465 "BTI instructions permitted in the NOP and in the non-NOP space"};
17466
17467 static const char * arm_attr_tag_BTI_use[] =
17468 {"Compiled without branch target enforcement",
17469 "Compiled with branch target enforcement"};
17470
17471 static const char * arm_attr_tag_PACRET_use[] =
17472 {"Compiled without return address signing and authentication",
17473 "Compiled with return address signing and authentication"};
17474
17475 #define LOOKUP(id, name) \
17476 {id, #name, 0x80 | ARRAY_SIZE(arm_attr_tag_##name), arm_attr_tag_##name}
17477 static arm_attr_public_tag arm_attr_public_tags[] =
17478 {
17479 {4, "CPU_raw_name", 1, NULL},
17480 {5, "CPU_name", 1, NULL},
17481 LOOKUP(6, CPU_arch),
17482 {7, "CPU_arch_profile", 0, NULL},
17483 LOOKUP(8, ARM_ISA_use),
17484 LOOKUP(9, THUMB_ISA_use),
17485 LOOKUP(10, FP_arch),
17486 LOOKUP(11, WMMX_arch),
17487 LOOKUP(12, Advanced_SIMD_arch),
17488 LOOKUP(13, PCS_config),
17489 LOOKUP(14, ABI_PCS_R9_use),
17490 LOOKUP(15, ABI_PCS_RW_data),
17491 LOOKUP(16, ABI_PCS_RO_data),
17492 LOOKUP(17, ABI_PCS_GOT_use),
17493 LOOKUP(18, ABI_PCS_wchar_t),
17494 LOOKUP(19, ABI_FP_rounding),
17495 LOOKUP(20, ABI_FP_denormal),
17496 LOOKUP(21, ABI_FP_exceptions),
17497 LOOKUP(22, ABI_FP_user_exceptions),
17498 LOOKUP(23, ABI_FP_number_model),
17499 {24, "ABI_align_needed", 0, NULL},
17500 {25, "ABI_align_preserved", 0, NULL},
17501 LOOKUP(26, ABI_enum_size),
17502 LOOKUP(27, ABI_HardFP_use),
17503 LOOKUP(28, ABI_VFP_args),
17504 LOOKUP(29, ABI_WMMX_args),
17505 LOOKUP(30, ABI_optimization_goals),
17506 LOOKUP(31, ABI_FP_optimization_goals),
17507 {32, "compatibility", 0, NULL},
17508 LOOKUP(34, CPU_unaligned_access),
17509 LOOKUP(36, FP_HP_extension),
17510 LOOKUP(38, ABI_FP_16bit_format),
17511 LOOKUP(42, MPextension_use),
17512 LOOKUP(44, DIV_use),
17513 LOOKUP(46, DSP_extension),
17514 LOOKUP(48, MVE_arch),
17515 LOOKUP(50, PAC_extension),
17516 LOOKUP(52, BTI_extension),
17517 LOOKUP(74, BTI_use),
17518 LOOKUP(76, PACRET_use),
17519 {64, "nodefaults", 0, NULL},
17520 {65, "also_compatible_with", 0, NULL},
17521 LOOKUP(66, T2EE_use),
17522 {67, "conformance", 1, NULL},
17523 LOOKUP(68, Virtualization_use),
17524 LOOKUP(70, MPextension_use_legacy)
17525 };
17526 #undef LOOKUP
17527
17528 static unsigned char *
17529 display_arm_attribute (unsigned char * p,
17530 const unsigned char * const end)
17531 {
17532 unsigned int tag;
17533 unsigned int val;
17534 arm_attr_public_tag * attr;
17535 unsigned i;
17536 unsigned int type;
17537
17538 READ_ULEB (tag, p, end);
17539 attr = NULL;
17540 for (i = 0; i < ARRAY_SIZE (arm_attr_public_tags); i++)
17541 {
17542 if (arm_attr_public_tags[i].tag == tag)
17543 {
17544 attr = &arm_attr_public_tags[i];
17545 break;
17546 }
17547 }
17548
17549 if (attr)
17550 {
17551 printf (" Tag_%s: ", attr->name);
17552 switch (attr->type)
17553 {
17554 case 0:
17555 switch (tag)
17556 {
17557 case 7: /* Tag_CPU_arch_profile. */
17558 READ_ULEB (val, p, end);
17559 switch (val)
17560 {
17561 case 0: printf (_("None\n")); break;
17562 case 'A': printf (_("Application\n")); break;
17563 case 'R': printf (_("Realtime\n")); break;
17564 case 'M': printf (_("Microcontroller\n")); break;
17565 case 'S': printf (_("Application or Realtime\n")); break;
17566 default: printf ("??? (%d)\n", val); break;
17567 }
17568 break;
17569
17570 case 24: /* Tag_align_needed. */
17571 READ_ULEB (val, p, end);
17572 switch (val)
17573 {
17574 case 0: printf (_("None\n")); break;
17575 case 1: printf (_("8-byte\n")); break;
17576 case 2: printf (_("4-byte\n")); break;
17577 case 3: printf ("??? 3\n"); break;
17578 default:
17579 if (val <= 12)
17580 printf (_("8-byte and up to %d-byte extended\n"),
17581 1 << val);
17582 else
17583 printf ("??? (%d)\n", val);
17584 break;
17585 }
17586 break;
17587
17588 case 25: /* Tag_align_preserved. */
17589 READ_ULEB (val, p, end);
17590 switch (val)
17591 {
17592 case 0: printf (_("None\n")); break;
17593 case 1: printf (_("8-byte, except leaf SP\n")); break;
17594 case 2: printf (_("8-byte\n")); break;
17595 case 3: printf ("??? 3\n"); break;
17596 default:
17597 if (val <= 12)
17598 printf (_("8-byte and up to %d-byte extended\n"),
17599 1 << val);
17600 else
17601 printf ("??? (%d)\n", val);
17602 break;
17603 }
17604 break;
17605
17606 case 32: /* Tag_compatibility. */
17607 {
17608 READ_ULEB (val, p, end);
17609 printf (_("flag = %d, vendor = "), val);
17610 if (p < end - 1)
17611 {
17612 size_t maxlen = (end - p) - 1;
17613
17614 print_symbol_name ((int) maxlen, (const char *) p);
17615 p += strnlen ((char *) p, maxlen) + 1;
17616 }
17617 else
17618 {
17619 printf (_("<corrupt>"));
17620 p = (unsigned char *) end;
17621 }
17622 putchar ('\n');
17623 }
17624 break;
17625
17626 case 64: /* Tag_nodefaults. */
17627 /* PR 17531: file: 001-505008-0.01. */
17628 if (p < end)
17629 p++;
17630 printf (_("True\n"));
17631 break;
17632
17633 case 65: /* Tag_also_compatible_with. */
17634 READ_ULEB (val, p, end);
17635 if (val == 6 /* Tag_CPU_arch. */)
17636 {
17637 READ_ULEB (val, p, end);
17638 if ((unsigned int) val >= ARRAY_SIZE (arm_attr_tag_CPU_arch))
17639 printf ("??? (%d)\n", val);
17640 else
17641 printf ("%s\n", arm_attr_tag_CPU_arch[val]);
17642 }
17643 else
17644 printf ("???\n");
17645 while (p < end && *(p++) != '\0' /* NUL terminator. */)
17646 ;
17647 break;
17648
17649 default:
17650 printf (_("<unknown: %d>\n"), tag);
17651 break;
17652 }
17653 return p;
17654
17655 case 1:
17656 return display_tag_value (-1, p, end);
17657 case 2:
17658 return display_tag_value (0, p, end);
17659
17660 default:
17661 assert (attr->type & 0x80);
17662 READ_ULEB (val, p, end);
17663 type = attr->type & 0x7f;
17664 if (val >= type)
17665 printf ("??? (%d)\n", val);
17666 else
17667 printf ("%s\n", attr->table[val]);
17668 return p;
17669 }
17670 }
17671
17672 return display_tag_value (tag, p, end);
17673 }
17674
17675 static unsigned char *
17676 display_gnu_attribute (unsigned char * p,
17677 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const),
17678 const unsigned char * const end)
17679 {
17680 unsigned int tag;
17681 unsigned int val;
17682
17683 READ_ULEB (tag, p, end);
17684
17685 /* Tag_compatibility is the only generic GNU attribute defined at
17686 present. */
17687 if (tag == 32)
17688 {
17689 READ_ULEB (val, p, end);
17690
17691 printf (_("flag = %d, vendor = "), val);
17692 if (p == end)
17693 {
17694 printf (_("<corrupt>\n"));
17695 warn (_("corrupt vendor attribute\n"));
17696 }
17697 else
17698 {
17699 if (p < end - 1)
17700 {
17701 size_t maxlen = (end - p) - 1;
17702
17703 print_symbol_name ((int) maxlen, (const char *) p);
17704 p += strnlen ((char *) p, maxlen) + 1;
17705 }
17706 else
17707 {
17708 printf (_("<corrupt>"));
17709 p = (unsigned char *) end;
17710 }
17711 putchar ('\n');
17712 }
17713 return p;
17714 }
17715
17716 if ((tag & 2) == 0 && display_proc_gnu_attribute)
17717 return display_proc_gnu_attribute (p, tag, end);
17718
17719 return display_tag_value (tag, p, end);
17720 }
17721
17722 static unsigned char *
17723 display_m68k_gnu_attribute (unsigned char * p,
17724 unsigned int tag,
17725 const unsigned char * const end)
17726 {
17727 unsigned int val;
17728
17729 if (tag == Tag_GNU_M68K_ABI_FP)
17730 {
17731 printf (" Tag_GNU_M68K_ABI_FP: ");
17732 if (p == end)
17733 {
17734 printf (_("<corrupt>\n"));
17735 return p;
17736 }
17737 READ_ULEB (val, p, end);
17738
17739 if (val > 3)
17740 printf ("(%#x), ", val);
17741
17742 switch (val & 3)
17743 {
17744 case 0:
17745 printf (_("unspecified hard/soft float\n"));
17746 break;
17747 case 1:
17748 printf (_("hard float\n"));
17749 break;
17750 case 2:
17751 printf (_("soft float\n"));
17752 break;
17753 }
17754 return p;
17755 }
17756
17757 return display_tag_value (tag & 1, p, end);
17758 }
17759
17760 static unsigned char *
17761 display_power_gnu_attribute (unsigned char * p,
17762 unsigned int tag,
17763 const unsigned char * const end)
17764 {
17765 unsigned int val;
17766
17767 if (tag == Tag_GNU_Power_ABI_FP)
17768 {
17769 printf (" Tag_GNU_Power_ABI_FP: ");
17770 if (p == end)
17771 {
17772 printf (_("<corrupt>\n"));
17773 return p;
17774 }
17775 READ_ULEB (val, p, end);
17776
17777 if (val > 15)
17778 printf ("(%#x), ", val);
17779
17780 switch (val & 3)
17781 {
17782 case 0:
17783 printf (_("unspecified hard/soft float, "));
17784 break;
17785 case 1:
17786 printf (_("hard float, "));
17787 break;
17788 case 2:
17789 printf (_("soft float, "));
17790 break;
17791 case 3:
17792 printf (_("single-precision hard float, "));
17793 break;
17794 }
17795
17796 switch (val & 0xC)
17797 {
17798 case 0:
17799 printf (_("unspecified long double\n"));
17800 break;
17801 case 4:
17802 printf (_("128-bit IBM long double\n"));
17803 break;
17804 case 8:
17805 printf (_("64-bit long double\n"));
17806 break;
17807 case 12:
17808 printf (_("128-bit IEEE long double\n"));
17809 break;
17810 }
17811 return p;
17812 }
17813
17814 if (tag == Tag_GNU_Power_ABI_Vector)
17815 {
17816 printf (" Tag_GNU_Power_ABI_Vector: ");
17817 if (p == end)
17818 {
17819 printf (_("<corrupt>\n"));
17820 return p;
17821 }
17822 READ_ULEB (val, p, end);
17823
17824 if (val > 3)
17825 printf ("(%#x), ", val);
17826
17827 switch (val & 3)
17828 {
17829 case 0:
17830 printf (_("unspecified\n"));
17831 break;
17832 case 1:
17833 printf (_("generic\n"));
17834 break;
17835 case 2:
17836 printf ("AltiVec\n");
17837 break;
17838 case 3:
17839 printf ("SPE\n");
17840 break;
17841 }
17842 return p;
17843 }
17844
17845 if (tag == Tag_GNU_Power_ABI_Struct_Return)
17846 {
17847 printf (" Tag_GNU_Power_ABI_Struct_Return: ");
17848 if (p == end)
17849 {
17850 printf (_("<corrupt>\n"));
17851 return p;
17852 }
17853 READ_ULEB (val, p, end);
17854
17855 if (val > 2)
17856 printf ("(%#x), ", val);
17857
17858 switch (val & 3)
17859 {
17860 case 0:
17861 printf (_("unspecified\n"));
17862 break;
17863 case 1:
17864 printf ("r3/r4\n");
17865 break;
17866 case 2:
17867 printf (_("memory\n"));
17868 break;
17869 case 3:
17870 printf ("???\n");
17871 break;
17872 }
17873 return p;
17874 }
17875
17876 return display_tag_value (tag & 1, p, end);
17877 }
17878
17879 static unsigned char *
17880 display_s390_gnu_attribute (unsigned char * p,
17881 unsigned int tag,
17882 const unsigned char * const end)
17883 {
17884 unsigned int val;
17885
17886 if (tag == Tag_GNU_S390_ABI_Vector)
17887 {
17888 printf (" Tag_GNU_S390_ABI_Vector: ");
17889 READ_ULEB (val, p, end);
17890
17891 switch (val)
17892 {
17893 case 0:
17894 printf (_("any\n"));
17895 break;
17896 case 1:
17897 printf (_("software\n"));
17898 break;
17899 case 2:
17900 printf (_("hardware\n"));
17901 break;
17902 default:
17903 printf ("??? (%d)\n", val);
17904 break;
17905 }
17906 return p;
17907 }
17908
17909 return display_tag_value (tag & 1, p, end);
17910 }
17911
17912 static void
17913 display_sparc_hwcaps (unsigned int mask)
17914 {
17915 if (mask)
17916 {
17917 bool first = true;
17918
17919 if (mask & ELF_SPARC_HWCAP_MUL32)
17920 fputs ("mul32", stdout), first = false;
17921 if (mask & ELF_SPARC_HWCAP_DIV32)
17922 printf ("%sdiv32", first ? "" : "|"), first = false;
17923 if (mask & ELF_SPARC_HWCAP_FSMULD)
17924 printf ("%sfsmuld", first ? "" : "|"), first = false;
17925 if (mask & ELF_SPARC_HWCAP_V8PLUS)
17926 printf ("%sv8plus", first ? "" : "|"), first = false;
17927 if (mask & ELF_SPARC_HWCAP_POPC)
17928 printf ("%spopc", first ? "" : "|"), first = false;
17929 if (mask & ELF_SPARC_HWCAP_VIS)
17930 printf ("%svis", first ? "" : "|"), first = false;
17931 if (mask & ELF_SPARC_HWCAP_VIS2)
17932 printf ("%svis2", first ? "" : "|"), first = false;
17933 if (mask & ELF_SPARC_HWCAP_ASI_BLK_INIT)
17934 printf ("%sASIBlkInit", first ? "" : "|"), first = false;
17935 if (mask & ELF_SPARC_HWCAP_FMAF)
17936 printf ("%sfmaf", first ? "" : "|"), first = false;
17937 if (mask & ELF_SPARC_HWCAP_VIS3)
17938 printf ("%svis3", first ? "" : "|"), first = false;
17939 if (mask & ELF_SPARC_HWCAP_HPC)
17940 printf ("%shpc", first ? "" : "|"), first = false;
17941 if (mask & ELF_SPARC_HWCAP_RANDOM)
17942 printf ("%srandom", first ? "" : "|"), first = false;
17943 if (mask & ELF_SPARC_HWCAP_TRANS)
17944 printf ("%strans", first ? "" : "|"), first = false;
17945 if (mask & ELF_SPARC_HWCAP_FJFMAU)
17946 printf ("%sfjfmau", first ? "" : "|"), first = false;
17947 if (mask & ELF_SPARC_HWCAP_IMA)
17948 printf ("%sima", first ? "" : "|"), first = false;
17949 if (mask & ELF_SPARC_HWCAP_ASI_CACHE_SPARING)
17950 printf ("%scspare", first ? "" : "|"), first = false;
17951 }
17952 else
17953 fputc ('0', stdout);
17954 fputc ('\n', stdout);
17955 }
17956
17957 static void
17958 display_sparc_hwcaps2 (unsigned int mask)
17959 {
17960 if (mask)
17961 {
17962 bool first = true;
17963
17964 if (mask & ELF_SPARC_HWCAP2_FJATHPLUS)
17965 fputs ("fjathplus", stdout), first = false;
17966 if (mask & ELF_SPARC_HWCAP2_VIS3B)
17967 printf ("%svis3b", first ? "" : "|"), first = false;
17968 if (mask & ELF_SPARC_HWCAP2_ADP)
17969 printf ("%sadp", first ? "" : "|"), first = false;
17970 if (mask & ELF_SPARC_HWCAP2_SPARC5)
17971 printf ("%ssparc5", first ? "" : "|"), first = false;
17972 if (mask & ELF_SPARC_HWCAP2_MWAIT)
17973 printf ("%smwait", first ? "" : "|"), first = false;
17974 if (mask & ELF_SPARC_HWCAP2_XMPMUL)
17975 printf ("%sxmpmul", first ? "" : "|"), first = false;
17976 if (mask & ELF_SPARC_HWCAP2_XMONT)
17977 printf ("%sxmont2", first ? "" : "|"), first = false;
17978 if (mask & ELF_SPARC_HWCAP2_NSEC)
17979 printf ("%snsec", first ? "" : "|"), first = false;
17980 if (mask & ELF_SPARC_HWCAP2_FJATHHPC)
17981 printf ("%sfjathhpc", first ? "" : "|"), first = false;
17982 if (mask & ELF_SPARC_HWCAP2_FJDES)
17983 printf ("%sfjdes", first ? "" : "|"), first = false;
17984 if (mask & ELF_SPARC_HWCAP2_FJAES)
17985 printf ("%sfjaes", first ? "" : "|"), first = false;
17986 }
17987 else
17988 fputc ('0', stdout);
17989 fputc ('\n', stdout);
17990 }
17991
17992 static unsigned char *
17993 display_sparc_gnu_attribute (unsigned char * p,
17994 unsigned int tag,
17995 const unsigned char * const end)
17996 {
17997 unsigned int val;
17998
17999 if (tag == Tag_GNU_Sparc_HWCAPS)
18000 {
18001 READ_ULEB (val, p, end);
18002 printf (" Tag_GNU_Sparc_HWCAPS: ");
18003 display_sparc_hwcaps (val);
18004 return p;
18005 }
18006 if (tag == Tag_GNU_Sparc_HWCAPS2)
18007 {
18008 READ_ULEB (val, p, end);
18009 printf (" Tag_GNU_Sparc_HWCAPS2: ");
18010 display_sparc_hwcaps2 (val);
18011 return p;
18012 }
18013
18014 return display_tag_value (tag, p, end);
18015 }
18016
18017 static void
18018 print_mips_fp_abi_value (unsigned int val)
18019 {
18020 switch (val)
18021 {
18022 case Val_GNU_MIPS_ABI_FP_ANY:
18023 printf (_("Hard or soft float\n"));
18024 break;
18025 case Val_GNU_MIPS_ABI_FP_DOUBLE:
18026 printf (_("Hard float (double precision)\n"));
18027 break;
18028 case Val_GNU_MIPS_ABI_FP_SINGLE:
18029 printf (_("Hard float (single precision)\n"));
18030 break;
18031 case Val_GNU_MIPS_ABI_FP_SOFT:
18032 printf (_("Soft float\n"));
18033 break;
18034 case Val_GNU_MIPS_ABI_FP_OLD_64:
18035 printf (_("Hard float (MIPS32r2 64-bit FPU 12 callee-saved)\n"));
18036 break;
18037 case Val_GNU_MIPS_ABI_FP_XX:
18038 printf (_("Hard float (32-bit CPU, Any FPU)\n"));
18039 break;
18040 case Val_GNU_MIPS_ABI_FP_64:
18041 printf (_("Hard float (32-bit CPU, 64-bit FPU)\n"));
18042 break;
18043 case Val_GNU_MIPS_ABI_FP_64A:
18044 printf (_("Hard float compat (32-bit CPU, 64-bit FPU)\n"));
18045 break;
18046 case Val_GNU_MIPS_ABI_FP_NAN2008:
18047 printf (_("NaN 2008 compatibility\n"));
18048 break;
18049 default:
18050 printf ("??? (%d)\n", val);
18051 break;
18052 }
18053 }
18054
18055 static unsigned char *
18056 display_mips_gnu_attribute (unsigned char * p,
18057 unsigned int tag,
18058 const unsigned char * const end)
18059 {
18060 if (tag == Tag_GNU_MIPS_ABI_FP)
18061 {
18062 unsigned int val;
18063
18064 printf (" Tag_GNU_MIPS_ABI_FP: ");
18065 READ_ULEB (val, p, end);
18066 print_mips_fp_abi_value (val);
18067 return p;
18068 }
18069
18070 if (tag == Tag_GNU_MIPS_ABI_MSA)
18071 {
18072 unsigned int val;
18073
18074 printf (" Tag_GNU_MIPS_ABI_MSA: ");
18075 READ_ULEB (val, p, end);
18076
18077 switch (val)
18078 {
18079 case Val_GNU_MIPS_ABI_MSA_ANY:
18080 printf (_("Any MSA or not\n"));
18081 break;
18082 case Val_GNU_MIPS_ABI_MSA_128:
18083 printf (_("128-bit MSA\n"));
18084 break;
18085 default:
18086 printf ("??? (%d)\n", val);
18087 break;
18088 }
18089 return p;
18090 }
18091
18092 return display_tag_value (tag & 1, p, end);
18093 }
18094
18095 static unsigned char *
18096 display_tic6x_attribute (unsigned char * p,
18097 const unsigned char * const end)
18098 {
18099 unsigned int tag;
18100 unsigned int val;
18101
18102 READ_ULEB (tag, p, end);
18103
18104 switch (tag)
18105 {
18106 case Tag_ISA:
18107 printf (" Tag_ISA: ");
18108 READ_ULEB (val, p, end);
18109
18110 switch (val)
18111 {
18112 case C6XABI_Tag_ISA_none:
18113 printf (_("None\n"));
18114 break;
18115 case C6XABI_Tag_ISA_C62X:
18116 printf ("C62x\n");
18117 break;
18118 case C6XABI_Tag_ISA_C67X:
18119 printf ("C67x\n");
18120 break;
18121 case C6XABI_Tag_ISA_C67XP:
18122 printf ("C67x+\n");
18123 break;
18124 case C6XABI_Tag_ISA_C64X:
18125 printf ("C64x\n");
18126 break;
18127 case C6XABI_Tag_ISA_C64XP:
18128 printf ("C64x+\n");
18129 break;
18130 case C6XABI_Tag_ISA_C674X:
18131 printf ("C674x\n");
18132 break;
18133 default:
18134 printf ("??? (%d)\n", val);
18135 break;
18136 }
18137 return p;
18138
18139 case Tag_ABI_wchar_t:
18140 printf (" Tag_ABI_wchar_t: ");
18141 READ_ULEB (val, p, end);
18142 switch (val)
18143 {
18144 case 0:
18145 printf (_("Not used\n"));
18146 break;
18147 case 1:
18148 printf (_("2 bytes\n"));
18149 break;
18150 case 2:
18151 printf (_("4 bytes\n"));
18152 break;
18153 default:
18154 printf ("??? (%d)\n", val);
18155 break;
18156 }
18157 return p;
18158
18159 case Tag_ABI_stack_align_needed:
18160 printf (" Tag_ABI_stack_align_needed: ");
18161 READ_ULEB (val, p, end);
18162 switch (val)
18163 {
18164 case 0:
18165 printf (_("8-byte\n"));
18166 break;
18167 case 1:
18168 printf (_("16-byte\n"));
18169 break;
18170 default:
18171 printf ("??? (%d)\n", val);
18172 break;
18173 }
18174 return p;
18175
18176 case Tag_ABI_stack_align_preserved:
18177 READ_ULEB (val, p, end);
18178 printf (" Tag_ABI_stack_align_preserved: ");
18179 switch (val)
18180 {
18181 case 0:
18182 printf (_("8-byte\n"));
18183 break;
18184 case 1:
18185 printf (_("16-byte\n"));
18186 break;
18187 default:
18188 printf ("??? (%d)\n", val);
18189 break;
18190 }
18191 return p;
18192
18193 case Tag_ABI_DSBT:
18194 READ_ULEB (val, p, end);
18195 printf (" Tag_ABI_DSBT: ");
18196 switch (val)
18197 {
18198 case 0:
18199 printf (_("DSBT addressing not used\n"));
18200 break;
18201 case 1:
18202 printf (_("DSBT addressing used\n"));
18203 break;
18204 default:
18205 printf ("??? (%d)\n", val);
18206 break;
18207 }
18208 return p;
18209
18210 case Tag_ABI_PID:
18211 READ_ULEB (val, p, end);
18212 printf (" Tag_ABI_PID: ");
18213 switch (val)
18214 {
18215 case 0:
18216 printf (_("Data addressing position-dependent\n"));
18217 break;
18218 case 1:
18219 printf (_("Data addressing position-independent, GOT near DP\n"));
18220 break;
18221 case 2:
18222 printf (_("Data addressing position-independent, GOT far from DP\n"));
18223 break;
18224 default:
18225 printf ("??? (%d)\n", val);
18226 break;
18227 }
18228 return p;
18229
18230 case Tag_ABI_PIC:
18231 READ_ULEB (val, p, end);
18232 printf (" Tag_ABI_PIC: ");
18233 switch (val)
18234 {
18235 case 0:
18236 printf (_("Code addressing position-dependent\n"));
18237 break;
18238 case 1:
18239 printf (_("Code addressing position-independent\n"));
18240 break;
18241 default:
18242 printf ("??? (%d)\n", val);
18243 break;
18244 }
18245 return p;
18246
18247 case Tag_ABI_array_object_alignment:
18248 READ_ULEB (val, p, end);
18249 printf (" Tag_ABI_array_object_alignment: ");
18250 switch (val)
18251 {
18252 case 0:
18253 printf (_("8-byte\n"));
18254 break;
18255 case 1:
18256 printf (_("4-byte\n"));
18257 break;
18258 case 2:
18259 printf (_("16-byte\n"));
18260 break;
18261 default:
18262 printf ("??? (%d)\n", val);
18263 break;
18264 }
18265 return p;
18266
18267 case Tag_ABI_array_object_align_expected:
18268 READ_ULEB (val, p, end);
18269 printf (" Tag_ABI_array_object_align_expected: ");
18270 switch (val)
18271 {
18272 case 0:
18273 printf (_("8-byte\n"));
18274 break;
18275 case 1:
18276 printf (_("4-byte\n"));
18277 break;
18278 case 2:
18279 printf (_("16-byte\n"));
18280 break;
18281 default:
18282 printf ("??? (%d)\n", val);
18283 break;
18284 }
18285 return p;
18286
18287 case Tag_ABI_compatibility:
18288 {
18289 READ_ULEB (val, p, end);
18290 printf (" Tag_ABI_compatibility: ");
18291 printf (_("flag = %d, vendor = "), val);
18292 if (p < end - 1)
18293 {
18294 size_t maxlen = (end - p) - 1;
18295
18296 print_symbol_name ((int) maxlen, (const char *) p);
18297 p += strnlen ((char *) p, maxlen) + 1;
18298 }
18299 else
18300 {
18301 printf (_("<corrupt>"));
18302 p = (unsigned char *) end;
18303 }
18304 putchar ('\n');
18305 return p;
18306 }
18307
18308 case Tag_ABI_conformance:
18309 {
18310 printf (" Tag_ABI_conformance: \"");
18311 if (p < end - 1)
18312 {
18313 size_t maxlen = (end - p) - 1;
18314
18315 print_symbol_name ((int) maxlen, (const char *) p);
18316 p += strnlen ((char *) p, maxlen) + 1;
18317 }
18318 else
18319 {
18320 printf (_("<corrupt>"));
18321 p = (unsigned char *) end;
18322 }
18323 printf ("\"\n");
18324 return p;
18325 }
18326 }
18327
18328 return display_tag_value (tag, p, end);
18329 }
18330
18331 static void
18332 display_raw_attribute (unsigned char * p, unsigned char const * const end)
18333 {
18334 uint64_t addr = 0;
18335 size_t bytes = end - p;
18336
18337 assert (end >= p);
18338 while (bytes)
18339 {
18340 int j;
18341 int k;
18342 int lbytes = (bytes > 16 ? 16 : bytes);
18343
18344 printf (" 0x%8.8" PRIx64 " ", addr);
18345
18346 for (j = 0; j < 16; j++)
18347 {
18348 if (j < lbytes)
18349 printf ("%2.2x", p[j]);
18350 else
18351 printf (" ");
18352
18353 if ((j & 3) == 3)
18354 printf (" ");
18355 }
18356
18357 for (j = 0; j < lbytes; j++)
18358 {
18359 k = p[j];
18360 if (k >= ' ' && k < 0x7f)
18361 printf ("%c", k);
18362 else
18363 printf (".");
18364 }
18365
18366 putchar ('\n');
18367
18368 p += lbytes;
18369 bytes -= lbytes;
18370 addr += lbytes;
18371 }
18372
18373 putchar ('\n');
18374 }
18375
18376 static unsigned char *
18377 display_msp430_attribute (unsigned char * p,
18378 const unsigned char * const end)
18379 {
18380 uint64_t val;
18381 uint64_t tag;
18382
18383 READ_ULEB (tag, p, end);
18384
18385 switch (tag)
18386 {
18387 case OFBA_MSPABI_Tag_ISA:
18388 printf (" Tag_ISA: ");
18389 READ_ULEB (val, p, end);
18390 switch (val)
18391 {
18392 case 0: printf (_("None\n")); break;
18393 case 1: printf (_("MSP430\n")); break;
18394 case 2: printf (_("MSP430X\n")); break;
18395 default: printf ("??? (%" PRId64 ")\n", val); break;
18396 }
18397 break;
18398
18399 case OFBA_MSPABI_Tag_Code_Model:
18400 printf (" Tag_Code_Model: ");
18401 READ_ULEB (val, p, end);
18402 switch (val)
18403 {
18404 case 0: printf (_("None\n")); break;
18405 case 1: printf (_("Small\n")); break;
18406 case 2: printf (_("Large\n")); break;
18407 default: printf ("??? (%" PRId64 ")\n", val); break;
18408 }
18409 break;
18410
18411 case OFBA_MSPABI_Tag_Data_Model:
18412 printf (" Tag_Data_Model: ");
18413 READ_ULEB (val, p, end);
18414 switch (val)
18415 {
18416 case 0: printf (_("None\n")); break;
18417 case 1: printf (_("Small\n")); break;
18418 case 2: printf (_("Large\n")); break;
18419 case 3: printf (_("Restricted Large\n")); break;
18420 default: printf ("??? (%" PRId64 ")\n", val); break;
18421 }
18422 break;
18423
18424 default:
18425 printf (_(" <unknown tag %" PRId64 ">: "), tag);
18426
18427 if (tag & 1)
18428 {
18429 putchar ('"');
18430 if (p < end - 1)
18431 {
18432 size_t maxlen = (end - p) - 1;
18433
18434 print_symbol_name ((int) maxlen, (const char *) p);
18435 p += strnlen ((char *) p, maxlen) + 1;
18436 }
18437 else
18438 {
18439 printf (_("<corrupt>"));
18440 p = (unsigned char *) end;
18441 }
18442 printf ("\"\n");
18443 }
18444 else
18445 {
18446 READ_ULEB (val, p, end);
18447 printf ("%" PRId64 " (0x%" PRIx64 ")\n", val, val);
18448 }
18449 break;
18450 }
18451
18452 assert (p <= end);
18453 return p;
18454 }
18455
18456 static unsigned char *
18457 display_msp430_gnu_attribute (unsigned char * p,
18458 unsigned int tag,
18459 const unsigned char * const end)
18460 {
18461 if (tag == Tag_GNU_MSP430_Data_Region)
18462 {
18463 uint64_t val;
18464
18465 printf (" Tag_GNU_MSP430_Data_Region: ");
18466 READ_ULEB (val, p, end);
18467
18468 switch (val)
18469 {
18470 case Val_GNU_MSP430_Data_Region_Any:
18471 printf (_("Any Region\n"));
18472 break;
18473 case Val_GNU_MSP430_Data_Region_Lower:
18474 printf (_("Lower Region Only\n"));
18475 break;
18476 default:
18477 printf ("??? (%" PRIu64 ")\n", val);
18478 }
18479 return p;
18480 }
18481 return display_tag_value (tag & 1, p, end);
18482 }
18483
18484 struct riscv_attr_tag_t {
18485 const char *name;
18486 unsigned int tag;
18487 };
18488
18489 static struct riscv_attr_tag_t riscv_attr_tag[] =
18490 {
18491 #define T(tag) {"Tag_RISCV_" #tag, Tag_RISCV_##tag}
18492 T(arch),
18493 T(priv_spec),
18494 T(priv_spec_minor),
18495 T(priv_spec_revision),
18496 T(unaligned_access),
18497 T(stack_align),
18498 #undef T
18499 };
18500
18501 static unsigned char *
18502 display_riscv_attribute (unsigned char *p,
18503 const unsigned char * const end)
18504 {
18505 uint64_t val;
18506 uint64_t tag;
18507 struct riscv_attr_tag_t *attr = NULL;
18508 unsigned i;
18509
18510 READ_ULEB (tag, p, end);
18511
18512 /* Find the name of attribute. */
18513 for (i = 0; i < ARRAY_SIZE (riscv_attr_tag); i++)
18514 {
18515 if (riscv_attr_tag[i].tag == tag)
18516 {
18517 attr = &riscv_attr_tag[i];
18518 break;
18519 }
18520 }
18521
18522 if (attr)
18523 printf (" %s: ", attr->name);
18524 else
18525 return display_tag_value (tag, p, end);
18526
18527 switch (tag)
18528 {
18529 case Tag_RISCV_priv_spec:
18530 case Tag_RISCV_priv_spec_minor:
18531 case Tag_RISCV_priv_spec_revision:
18532 READ_ULEB (val, p, end);
18533 printf ("%" PRIu64 "\n", val);
18534 break;
18535 case Tag_RISCV_unaligned_access:
18536 READ_ULEB (val, p, end);
18537 switch (val)
18538 {
18539 case 0:
18540 printf (_("No unaligned access\n"));
18541 break;
18542 case 1:
18543 printf (_("Unaligned access\n"));
18544 break;
18545 }
18546 break;
18547 case Tag_RISCV_stack_align:
18548 READ_ULEB (val, p, end);
18549 printf (_("%" PRIu64 "-bytes\n"), val);
18550 break;
18551 case Tag_RISCV_arch:
18552 p = display_tag_value (-1, p, end);
18553 break;
18554 default:
18555 return display_tag_value (tag, p, end);
18556 }
18557
18558 return p;
18559 }
18560
18561 static unsigned char *
18562 display_csky_attribute (unsigned char * p,
18563 const unsigned char * const end)
18564 {
18565 uint64_t tag;
18566 uint64_t val;
18567 READ_ULEB (tag, p, end);
18568
18569 if (tag >= Tag_CSKY_MAX)
18570 {
18571 return display_tag_value (-1, p, end);
18572 }
18573
18574 switch (tag)
18575 {
18576 case Tag_CSKY_ARCH_NAME:
18577 printf (" Tag_CSKY_ARCH_NAME:\t\t");
18578 return display_tag_value (-1, p, end);
18579 case Tag_CSKY_CPU_NAME:
18580 printf (" Tag_CSKY_CPU_NAME:\t\t");
18581 return display_tag_value (-1, p, end);
18582
18583 case Tag_CSKY_ISA_FLAGS:
18584 printf (" Tag_CSKY_ISA_FLAGS:\t\t");
18585 return display_tag_value (0, p, end);
18586 case Tag_CSKY_ISA_EXT_FLAGS:
18587 printf (" Tag_CSKY_ISA_EXT_FLAGS:\t");
18588 return display_tag_value (0, p, end);
18589
18590 case Tag_CSKY_DSP_VERSION:
18591 printf (" Tag_CSKY_DSP_VERSION:\t\t");
18592 READ_ULEB (val, p, end);
18593 if (val == VAL_CSKY_DSP_VERSION_EXTENSION)
18594 printf ("DSP Extension\n");
18595 else if (val == VAL_CSKY_DSP_VERSION_2)
18596 printf ("DSP 2.0\n");
18597 break;
18598
18599 case Tag_CSKY_VDSP_VERSION:
18600 printf (" Tag_CSKY_VDSP_VERSION:\t");
18601 READ_ULEB (val, p, end);
18602 printf ("VDSP Version %" PRId64 "\n", val);
18603 break;
18604
18605 case Tag_CSKY_FPU_VERSION:
18606 printf (" Tag_CSKY_FPU_VERSION:\t\t");
18607 READ_ULEB (val, p, end);
18608 if (val == VAL_CSKY_FPU_VERSION_1)
18609 printf ("ABIV1 FPU Version 1\n");
18610 else if (val == VAL_CSKY_FPU_VERSION_2)
18611 printf ("FPU Version 2\n");
18612 break;
18613
18614 case Tag_CSKY_FPU_ABI:
18615 printf (" Tag_CSKY_FPU_ABI:\t\t");
18616 READ_ULEB (val, p, end);
18617 if (val == VAL_CSKY_FPU_ABI_HARD)
18618 printf ("Hard\n");
18619 else if (val == VAL_CSKY_FPU_ABI_SOFTFP)
18620 printf ("SoftFP\n");
18621 else if (val == VAL_CSKY_FPU_ABI_SOFT)
18622 printf ("Soft\n");
18623 break;
18624 case Tag_CSKY_FPU_ROUNDING:
18625 READ_ULEB (val, p, end);
18626 if (val == 1)
18627 {
18628 printf (" Tag_CSKY_FPU_ROUNDING:\t");
18629 printf ("Needed\n");
18630 }
18631 break;
18632 case Tag_CSKY_FPU_DENORMAL:
18633 READ_ULEB (val, p, end);
18634 if (val == 1)
18635 {
18636 printf (" Tag_CSKY_FPU_DENORMAL:\t");
18637 printf ("Needed\n");
18638 }
18639 break;
18640 case Tag_CSKY_FPU_Exception:
18641 READ_ULEB (val, p, end);
18642 if (val == 1)
18643 {
18644 printf (" Tag_CSKY_FPU_Exception:\t");
18645 printf ("Needed\n");
18646 }
18647 break;
18648 case Tag_CSKY_FPU_NUMBER_MODULE:
18649 printf (" Tag_CSKY_FPU_NUMBER_MODULE:\t");
18650 return display_tag_value (-1, p, end);
18651 case Tag_CSKY_FPU_HARDFP:
18652 printf (" Tag_CSKY_FPU_HARDFP:\t\t");
18653 READ_ULEB (val, p, end);
18654 if (val & VAL_CSKY_FPU_HARDFP_HALF)
18655 printf (" Half");
18656 if (val & VAL_CSKY_FPU_HARDFP_SINGLE)
18657 printf (" Single");
18658 if (val & VAL_CSKY_FPU_HARDFP_DOUBLE)
18659 printf (" Double");
18660 printf ("\n");
18661 break;
18662 default:
18663 return display_tag_value (tag, p, end);
18664 }
18665 return p;
18666 }
18667
18668 static bool
18669 process_attributes (Filedata * filedata,
18670 const char * public_name,
18671 unsigned int proc_type,
18672 unsigned char * (* display_pub_attribute) (unsigned char *, const unsigned char * const),
18673 unsigned char * (* display_proc_gnu_attribute) (unsigned char *, unsigned int, const unsigned char * const))
18674 {
18675 Elf_Internal_Shdr * sect;
18676 unsigned i;
18677 bool res = true;
18678
18679 /* Find the section header so that we get the size. */
18680 for (i = 0, sect = filedata->section_headers;
18681 i < filedata->file_header.e_shnum;
18682 i++, sect++)
18683 {
18684 unsigned char * contents;
18685 unsigned char * p;
18686
18687 if (sect->sh_type != proc_type && sect->sh_type != SHT_GNU_ATTRIBUTES)
18688 continue;
18689
18690 contents = (unsigned char *) get_data (NULL, filedata, sect->sh_offset, 1,
18691 sect->sh_size, _("attributes"));
18692 if (contents == NULL)
18693 {
18694 res = false;
18695 continue;
18696 }
18697
18698 p = contents;
18699 /* The first character is the version of the attributes.
18700 Currently only version 1, (aka 'A') is recognised here. */
18701 if (*p != 'A')
18702 {
18703 printf (_("Unknown attributes version '%c'(%d) - expecting 'A'\n"), *p, *p);
18704 res = false;
18705 }
18706 else
18707 {
18708 uint64_t section_len;
18709
18710 section_len = sect->sh_size - 1;
18711 p++;
18712
18713 while (section_len > 0)
18714 {
18715 uint64_t attr_len;
18716 unsigned int namelen;
18717 bool public_section;
18718 bool gnu_section;
18719
18720 if (section_len <= 4)
18721 {
18722 error (_("Tag section ends prematurely\n"));
18723 res = false;
18724 break;
18725 }
18726 attr_len = byte_get (p, 4);
18727 p += 4;
18728
18729 if (attr_len > section_len)
18730 {
18731 error (_("Bad attribute length (%u > %u)\n"),
18732 (unsigned) attr_len, (unsigned) section_len);
18733 attr_len = section_len;
18734 res = false;
18735 }
18736 /* PR 17531: file: 001-101425-0.004 */
18737 else if (attr_len < 5)
18738 {
18739 error (_("Attribute length of %u is too small\n"), (unsigned) attr_len);
18740 res = false;
18741 break;
18742 }
18743
18744 section_len -= attr_len;
18745 attr_len -= 4;
18746
18747 namelen = strnlen ((char *) p, attr_len) + 1;
18748 if (namelen == 0 || namelen >= attr_len)
18749 {
18750 error (_("Corrupt attribute section name\n"));
18751 res = false;
18752 break;
18753 }
18754
18755 printf (_("Attribute Section: "));
18756 print_symbol_name (INT_MAX, (const char *) p);
18757 putchar ('\n');
18758
18759 if (public_name && streq ((char *) p, public_name))
18760 public_section = true;
18761 else
18762 public_section = false;
18763
18764 if (streq ((char *) p, "gnu"))
18765 gnu_section = true;
18766 else
18767 gnu_section = false;
18768
18769 p += namelen;
18770 attr_len -= namelen;
18771
18772 while (attr_len > 0 && p < contents + sect->sh_size)
18773 {
18774 int tag;
18775 unsigned int val;
18776 uint64_t size;
18777 unsigned char * end;
18778
18779 /* PR binutils/17531: Safe handling of corrupt files. */
18780 if (attr_len < 6)
18781 {
18782 error (_("Unused bytes at end of section\n"));
18783 res = false;
18784 section_len = 0;
18785 break;
18786 }
18787
18788 tag = *(p++);
18789 size = byte_get (p, 4);
18790 if (size > attr_len)
18791 {
18792 error (_("Bad subsection length (%u > %u)\n"),
18793 (unsigned) size, (unsigned) attr_len);
18794 res = false;
18795 size = attr_len;
18796 }
18797 /* PR binutils/17531: Safe handling of corrupt files. */
18798 if (size < 6)
18799 {
18800 error (_("Bad subsection length (%u < 6)\n"),
18801 (unsigned) size);
18802 res = false;
18803 section_len = 0;
18804 break;
18805 }
18806
18807 attr_len -= size;
18808 end = p + size - 1;
18809 assert (end <= contents + sect->sh_size);
18810 p += 4;
18811
18812 switch (tag)
18813 {
18814 case 1:
18815 printf (_("File Attributes\n"));
18816 break;
18817 case 2:
18818 printf (_("Section Attributes:"));
18819 goto do_numlist;
18820 case 3:
18821 printf (_("Symbol Attributes:"));
18822 /* Fall through. */
18823 do_numlist:
18824 for (;;)
18825 {
18826 READ_ULEB (val, p, end);
18827 if (val == 0)
18828 break;
18829 printf (" %d", val);
18830 }
18831 printf ("\n");
18832 break;
18833 default:
18834 printf (_("Unknown tag: %d\n"), tag);
18835 public_section = false;
18836 break;
18837 }
18838
18839 if (public_section && display_pub_attribute != NULL)
18840 {
18841 while (p < end)
18842 p = display_pub_attribute (p, end);
18843 assert (p == end);
18844 }
18845 else if (gnu_section && display_proc_gnu_attribute != NULL)
18846 {
18847 while (p < end)
18848 p = display_gnu_attribute (p,
18849 display_proc_gnu_attribute,
18850 end);
18851 assert (p == end);
18852 }
18853 else if (p < end)
18854 {
18855 printf (_(" Unknown attribute:\n"));
18856 display_raw_attribute (p, end);
18857 p = end;
18858 }
18859 else
18860 attr_len = 0;
18861 }
18862 }
18863 }
18864
18865 free (contents);
18866 }
18867
18868 return res;
18869 }
18870
18871 /* DATA points to the contents of a MIPS GOT that starts at VMA PLTGOT.
18872 Print the Address, Access and Initial fields of an entry at VMA ADDR
18873 and return the VMA of the next entry, or -1 if there was a problem.
18874 Does not read from DATA_END or beyond. */
18875
18876 static uint64_t
18877 print_mips_got_entry (unsigned char * data, uint64_t pltgot, uint64_t addr,
18878 unsigned char * data_end)
18879 {
18880 printf (" ");
18881 print_vma (addr, LONG_HEX);
18882 printf (" ");
18883 if (addr < pltgot + 0xfff0)
18884 printf ("%6d(gp)", (int) (addr - pltgot - 0x7ff0));
18885 else
18886 printf ("%10s", "");
18887 printf (" ");
18888 if (data == NULL)
18889 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18890 else
18891 {
18892 uint64_t entry;
18893 unsigned char * from = data + addr - pltgot;
18894
18895 if (from + (is_32bit_elf ? 4 : 8) > data_end)
18896 {
18897 warn (_("MIPS GOT entry extends beyond the end of available data\n"));
18898 printf ("%*s", is_32bit_elf ? 8 : 16, _("<corrupt>"));
18899 return (uint64_t) -1;
18900 }
18901 else
18902 {
18903 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18904 print_vma (entry, LONG_HEX);
18905 }
18906 }
18907 return addr + (is_32bit_elf ? 4 : 8);
18908 }
18909
18910 /* DATA points to the contents of a MIPS PLT GOT that starts at VMA
18911 PLTGOT. Print the Address and Initial fields of an entry at VMA
18912 ADDR and return the VMA of the next entry. */
18913
18914 static uint64_t
18915 print_mips_pltgot_entry (unsigned char * data, uint64_t pltgot, uint64_t addr)
18916 {
18917 printf (" ");
18918 print_vma (addr, LONG_HEX);
18919 printf (" ");
18920 if (data == NULL)
18921 printf ("%*s", is_32bit_elf ? 8 : 16, _("<unknown>"));
18922 else
18923 {
18924 uint64_t entry;
18925
18926 entry = byte_get (data + addr - pltgot, is_32bit_elf ? 4 : 8);
18927 print_vma (entry, LONG_HEX);
18928 }
18929 return addr + (is_32bit_elf ? 4 : 8);
18930 }
18931
18932 static void
18933 print_mips_ases (unsigned int mask)
18934 {
18935 if (mask & AFL_ASE_DSP)
18936 fputs ("\n\tDSP ASE", stdout);
18937 if (mask & AFL_ASE_DSPR2)
18938 fputs ("\n\tDSP R2 ASE", stdout);
18939 if (mask & AFL_ASE_DSPR3)
18940 fputs ("\n\tDSP R3 ASE", stdout);
18941 if (mask & AFL_ASE_EVA)
18942 fputs ("\n\tEnhanced VA Scheme", stdout);
18943 if (mask & AFL_ASE_MCU)
18944 fputs ("\n\tMCU (MicroController) ASE", stdout);
18945 if (mask & AFL_ASE_MDMX)
18946 fputs ("\n\tMDMX ASE", stdout);
18947 if (mask & AFL_ASE_MIPS3D)
18948 fputs ("\n\tMIPS-3D ASE", stdout);
18949 if (mask & AFL_ASE_MT)
18950 fputs ("\n\tMT ASE", stdout);
18951 if (mask & AFL_ASE_SMARTMIPS)
18952 fputs ("\n\tSmartMIPS ASE", stdout);
18953 if (mask & AFL_ASE_VIRT)
18954 fputs ("\n\tVZ ASE", stdout);
18955 if (mask & AFL_ASE_MSA)
18956 fputs ("\n\tMSA ASE", stdout);
18957 if (mask & AFL_ASE_MIPS16)
18958 fputs ("\n\tMIPS16 ASE", stdout);
18959 if (mask & AFL_ASE_MICROMIPS)
18960 fputs ("\n\tMICROMIPS ASE", stdout);
18961 if (mask & AFL_ASE_XPA)
18962 fputs ("\n\tXPA ASE", stdout);
18963 if (mask & AFL_ASE_MIPS16E2)
18964 fputs ("\n\tMIPS16e2 ASE", stdout);
18965 if (mask & AFL_ASE_CRC)
18966 fputs ("\n\tCRC ASE", stdout);
18967 if (mask & AFL_ASE_GINV)
18968 fputs ("\n\tGINV ASE", stdout);
18969 if (mask & AFL_ASE_LOONGSON_MMI)
18970 fputs ("\n\tLoongson MMI ASE", stdout);
18971 if (mask & AFL_ASE_LOONGSON_CAM)
18972 fputs ("\n\tLoongson CAM ASE", stdout);
18973 if (mask & AFL_ASE_LOONGSON_EXT)
18974 fputs ("\n\tLoongson EXT ASE", stdout);
18975 if (mask & AFL_ASE_LOONGSON_EXT2)
18976 fputs ("\n\tLoongson EXT2 ASE", stdout);
18977 if (mask == 0)
18978 fprintf (stdout, "\n\t%s", _("None"));
18979 else if ((mask & ~AFL_ASE_MASK) != 0)
18980 fprintf (stdout, "\n\t%s (%x)", _("Unknown"), mask & ~AFL_ASE_MASK);
18981 }
18982
18983 static void
18984 print_mips_isa_ext (unsigned int isa_ext)
18985 {
18986 switch (isa_ext)
18987 {
18988 case 0:
18989 fputs (_("None"), stdout);
18990 break;
18991 case AFL_EXT_XLR:
18992 fputs ("RMI XLR", stdout);
18993 break;
18994 case AFL_EXT_OCTEON3:
18995 fputs ("Cavium Networks Octeon3", stdout);
18996 break;
18997 case AFL_EXT_OCTEON2:
18998 fputs ("Cavium Networks Octeon2", stdout);
18999 break;
19000 case AFL_EXT_OCTEONP:
19001 fputs ("Cavium Networks OcteonP", stdout);
19002 break;
19003 case AFL_EXT_OCTEON:
19004 fputs ("Cavium Networks Octeon", stdout);
19005 break;
19006 case AFL_EXT_5900:
19007 fputs ("Toshiba R5900", stdout);
19008 break;
19009 case AFL_EXT_4650:
19010 fputs ("MIPS R4650", stdout);
19011 break;
19012 case AFL_EXT_4010:
19013 fputs ("LSI R4010", stdout);
19014 break;
19015 case AFL_EXT_4100:
19016 fputs ("NEC VR4100", stdout);
19017 break;
19018 case AFL_EXT_3900:
19019 fputs ("Toshiba R3900", stdout);
19020 break;
19021 case AFL_EXT_10000:
19022 fputs ("MIPS R10000", stdout);
19023 break;
19024 case AFL_EXT_SB1:
19025 fputs ("Broadcom SB-1", stdout);
19026 break;
19027 case AFL_EXT_4111:
19028 fputs ("NEC VR4111/VR4181", stdout);
19029 break;
19030 case AFL_EXT_4120:
19031 fputs ("NEC VR4120", stdout);
19032 break;
19033 case AFL_EXT_5400:
19034 fputs ("NEC VR5400", stdout);
19035 break;
19036 case AFL_EXT_5500:
19037 fputs ("NEC VR5500", stdout);
19038 break;
19039 case AFL_EXT_LOONGSON_2E:
19040 fputs ("ST Microelectronics Loongson 2E", stdout);
19041 break;
19042 case AFL_EXT_LOONGSON_2F:
19043 fputs ("ST Microelectronics Loongson 2F", stdout);
19044 break;
19045 case AFL_EXT_INTERAPTIV_MR2:
19046 fputs ("Imagination interAptiv MR2", stdout);
19047 break;
19048 default:
19049 fprintf (stdout, "%s (%d)", _("Unknown"), isa_ext);
19050 }
19051 }
19052
19053 static signed int
19054 get_mips_reg_size (int reg_size)
19055 {
19056 return (reg_size == AFL_REG_NONE) ? 0
19057 : (reg_size == AFL_REG_32) ? 32
19058 : (reg_size == AFL_REG_64) ? 64
19059 : (reg_size == AFL_REG_128) ? 128
19060 : -1;
19061 }
19062
19063 static bool
19064 process_mips_specific (Filedata * filedata)
19065 {
19066 Elf_Internal_Dyn * entry;
19067 Elf_Internal_Shdr *sect = NULL;
19068 size_t liblist_offset = 0;
19069 size_t liblistno = 0;
19070 size_t conflictsno = 0;
19071 size_t options_offset = 0;
19072 size_t conflicts_offset = 0;
19073 size_t pltrelsz = 0;
19074 size_t pltrel = 0;
19075 uint64_t pltgot = 0;
19076 uint64_t mips_pltgot = 0;
19077 uint64_t jmprel = 0;
19078 uint64_t local_gotno = 0;
19079 uint64_t gotsym = 0;
19080 uint64_t symtabno = 0;
19081 bool res = true;
19082
19083 if (! process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
19084 display_mips_gnu_attribute))
19085 res = false;
19086
19087 sect = find_section (filedata, ".MIPS.abiflags");
19088
19089 if (sect != NULL)
19090 {
19091 Elf_External_ABIFlags_v0 *abiflags_ext;
19092 Elf_Internal_ABIFlags_v0 abiflags_in;
19093
19094 if (sizeof (Elf_External_ABIFlags_v0) != sect->sh_size)
19095 {
19096 error (_("Corrupt MIPS ABI Flags section.\n"));
19097 res = false;
19098 }
19099 else
19100 {
19101 abiflags_ext = get_data (NULL, filedata, sect->sh_offset, 1,
19102 sect->sh_size, _("MIPS ABI Flags section"));
19103 if (abiflags_ext)
19104 {
19105 abiflags_in.version = BYTE_GET (abiflags_ext->version);
19106 abiflags_in.isa_level = BYTE_GET (abiflags_ext->isa_level);
19107 abiflags_in.isa_rev = BYTE_GET (abiflags_ext->isa_rev);
19108 abiflags_in.gpr_size = BYTE_GET (abiflags_ext->gpr_size);
19109 abiflags_in.cpr1_size = BYTE_GET (abiflags_ext->cpr1_size);
19110 abiflags_in.cpr2_size = BYTE_GET (abiflags_ext->cpr2_size);
19111 abiflags_in.fp_abi = BYTE_GET (abiflags_ext->fp_abi);
19112 abiflags_in.isa_ext = BYTE_GET (abiflags_ext->isa_ext);
19113 abiflags_in.ases = BYTE_GET (abiflags_ext->ases);
19114 abiflags_in.flags1 = BYTE_GET (abiflags_ext->flags1);
19115 abiflags_in.flags2 = BYTE_GET (abiflags_ext->flags2);
19116
19117 printf ("\nMIPS ABI Flags Version: %d\n", abiflags_in.version);
19118 printf ("\nISA: MIPS%d", abiflags_in.isa_level);
19119 if (abiflags_in.isa_rev > 1)
19120 printf ("r%d", abiflags_in.isa_rev);
19121 printf ("\nGPR size: %d",
19122 get_mips_reg_size (abiflags_in.gpr_size));
19123 printf ("\nCPR1 size: %d",
19124 get_mips_reg_size (abiflags_in.cpr1_size));
19125 printf ("\nCPR2 size: %d",
19126 get_mips_reg_size (abiflags_in.cpr2_size));
19127 fputs ("\nFP ABI: ", stdout);
19128 print_mips_fp_abi_value (abiflags_in.fp_abi);
19129 fputs ("ISA Extension: ", stdout);
19130 print_mips_isa_ext (abiflags_in.isa_ext);
19131 fputs ("\nASEs:", stdout);
19132 print_mips_ases (abiflags_in.ases);
19133 printf ("\nFLAGS 1: %8.8lx", abiflags_in.flags1);
19134 printf ("\nFLAGS 2: %8.8lx", abiflags_in.flags2);
19135 fputc ('\n', stdout);
19136 free (abiflags_ext);
19137 }
19138 }
19139 }
19140
19141 /* We have a lot of special sections. Thanks SGI! */
19142 if (filedata->dynamic_section == NULL)
19143 {
19144 /* No dynamic information available. See if there is static GOT. */
19145 sect = find_section (filedata, ".got");
19146 if (sect != NULL)
19147 {
19148 unsigned char *data_end;
19149 unsigned char *data;
19150 uint64_t ent, end;
19151 int addr_size;
19152
19153 pltgot = sect->sh_addr;
19154
19155 ent = pltgot;
19156 addr_size = (is_32bit_elf ? 4 : 8);
19157 end = pltgot + sect->sh_size;
19158
19159 data = (unsigned char *) get_data (NULL, filedata, sect->sh_offset,
19160 end - pltgot, 1,
19161 _("Global Offset Table data"));
19162 /* PR 12855: Null data is handled gracefully throughout. */
19163 data_end = data + (end - pltgot);
19164
19165 printf (_("\nStatic GOT:\n"));
19166 printf (_(" Canonical gp value: "));
19167 print_vma (ent + 0x7ff0, LONG_HEX);
19168 printf ("\n\n");
19169
19170 /* In a dynamic binary GOT[0] is reserved for the dynamic
19171 loader to store the lazy resolver pointer, however in
19172 a static binary it may well have been omitted and GOT
19173 reduced to a table of addresses.
19174 PR 21344: Check for the entry being fully available
19175 before fetching it. */
19176 if (data
19177 && data + ent - pltgot + addr_size <= data_end
19178 && byte_get (data + ent - pltgot, addr_size) == 0)
19179 {
19180 printf (_(" Reserved entries:\n"));
19181 printf (_(" %*s %10s %*s\n"),
19182 addr_size * 2, _("Address"), _("Access"),
19183 addr_size * 2, _("Value"));
19184 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19185 printf ("\n");
19186 if (ent == (uint64_t) -1)
19187 goto sgot_print_fail;
19188
19189 /* Check for the MSB of GOT[1] being set, identifying a
19190 GNU object. This entry will be used by some runtime
19191 loaders, to store the module pointer. Otherwise this
19192 is an ordinary local entry.
19193 PR 21344: Check for the entry being fully available
19194 before fetching it. */
19195 if (data
19196 && data + ent - pltgot + addr_size <= data_end
19197 && (byte_get (data + ent - pltgot, addr_size)
19198 >> (addr_size * 8 - 1)) != 0)
19199 {
19200 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19201 printf ("\n");
19202 if (ent == (uint64_t) -1)
19203 goto sgot_print_fail;
19204 }
19205 printf ("\n");
19206 }
19207
19208 if (data != NULL && ent < end)
19209 {
19210 printf (_(" Local entries:\n"));
19211 printf (" %*s %10s %*s\n",
19212 addr_size * 2, _("Address"), _("Access"),
19213 addr_size * 2, _("Value"));
19214 while (ent < end)
19215 {
19216 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19217 printf ("\n");
19218 if (ent == (uint64_t) -1)
19219 goto sgot_print_fail;
19220 }
19221 printf ("\n");
19222 }
19223
19224 sgot_print_fail:
19225 free (data);
19226 }
19227 return res;
19228 }
19229
19230 for (entry = filedata->dynamic_section;
19231 /* PR 17531 file: 012-50589-0.004. */
19232 (entry < filedata->dynamic_section + filedata->dynamic_nent
19233 && entry->d_tag != DT_NULL);
19234 ++entry)
19235 switch (entry->d_tag)
19236 {
19237 case DT_MIPS_LIBLIST:
19238 liblist_offset
19239 = offset_from_vma (filedata, entry->d_un.d_val,
19240 liblistno * sizeof (Elf32_External_Lib));
19241 break;
19242 case DT_MIPS_LIBLISTNO:
19243 liblistno = entry->d_un.d_val;
19244 break;
19245 case DT_MIPS_OPTIONS:
19246 options_offset = offset_from_vma (filedata, entry->d_un.d_val, 0);
19247 break;
19248 case DT_MIPS_CONFLICT:
19249 conflicts_offset
19250 = offset_from_vma (filedata, entry->d_un.d_val,
19251 conflictsno * sizeof (Elf32_External_Conflict));
19252 break;
19253 case DT_MIPS_CONFLICTNO:
19254 conflictsno = entry->d_un.d_val;
19255 break;
19256 case DT_PLTGOT:
19257 pltgot = entry->d_un.d_ptr;
19258 break;
19259 case DT_MIPS_LOCAL_GOTNO:
19260 local_gotno = entry->d_un.d_val;
19261 break;
19262 case DT_MIPS_GOTSYM:
19263 gotsym = entry->d_un.d_val;
19264 break;
19265 case DT_MIPS_SYMTABNO:
19266 symtabno = entry->d_un.d_val;
19267 break;
19268 case DT_MIPS_PLTGOT:
19269 mips_pltgot = entry->d_un.d_ptr;
19270 break;
19271 case DT_PLTREL:
19272 pltrel = entry->d_un.d_val;
19273 break;
19274 case DT_PLTRELSZ:
19275 pltrelsz = entry->d_un.d_val;
19276 break;
19277 case DT_JMPREL:
19278 jmprel = entry->d_un.d_ptr;
19279 break;
19280 default:
19281 break;
19282 }
19283
19284 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
19285 {
19286 Elf32_External_Lib * elib;
19287 size_t cnt;
19288
19289 elib = (Elf32_External_Lib *) get_data (NULL, filedata, liblist_offset,
19290 sizeof (Elf32_External_Lib),
19291 liblistno,
19292 _("liblist section data"));
19293 if (elib)
19294 {
19295 printf (ngettext ("\nSection '.liblist' contains %zu entry:\n",
19296 "\nSection '.liblist' contains %zu entries:\n",
19297 liblistno),
19298 liblistno);
19299 fputs (_(" Library Time Stamp Checksum Version Flags\n"),
19300 stdout);
19301
19302 for (cnt = 0; cnt < liblistno; ++cnt)
19303 {
19304 Elf32_Lib liblist;
19305 time_t atime;
19306 char timebuf[128];
19307 struct tm * tmp;
19308
19309 liblist.l_name = BYTE_GET (elib[cnt].l_name);
19310 atime = BYTE_GET (elib[cnt].l_time_stamp);
19311 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
19312 liblist.l_version = BYTE_GET (elib[cnt].l_version);
19313 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
19314
19315 tmp = gmtime (&atime);
19316 snprintf (timebuf, sizeof (timebuf),
19317 "%04u-%02u-%02uT%02u:%02u:%02u",
19318 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
19319 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
19320
19321 printf ("%3zu: ", cnt);
19322 if (valid_dynamic_name (filedata, liblist.l_name))
19323 print_symbol_name (20, get_dynamic_name (filedata, liblist.l_name));
19324 else
19325 printf (_("<corrupt: %9ld>"), liblist.l_name);
19326 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
19327 liblist.l_version);
19328
19329 if (liblist.l_flags == 0)
19330 puts (_(" NONE"));
19331 else
19332 {
19333 static const struct
19334 {
19335 const char * name;
19336 int bit;
19337 }
19338 l_flags_vals[] =
19339 {
19340 { " EXACT_MATCH", LL_EXACT_MATCH },
19341 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
19342 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
19343 { " EXPORTS", LL_EXPORTS },
19344 { " DELAY_LOAD", LL_DELAY_LOAD },
19345 { " DELTA", LL_DELTA }
19346 };
19347 int flags = liblist.l_flags;
19348 size_t fcnt;
19349
19350 for (fcnt = 0; fcnt < ARRAY_SIZE (l_flags_vals); ++fcnt)
19351 if ((flags & l_flags_vals[fcnt].bit) != 0)
19352 {
19353 fputs (l_flags_vals[fcnt].name, stdout);
19354 flags ^= l_flags_vals[fcnt].bit;
19355 }
19356 if (flags != 0)
19357 printf (" %#x", (unsigned int) flags);
19358
19359 puts ("");
19360 }
19361 }
19362
19363 free (elib);
19364 }
19365 else
19366 res = false;
19367 }
19368
19369 if (options_offset != 0)
19370 {
19371 Elf_External_Options * eopt;
19372 size_t offset;
19373 int cnt;
19374
19375 /* Find the section header so that we get the size. */
19376 sect = find_section_by_type (filedata, SHT_MIPS_OPTIONS);
19377 /* PR 17533 file: 012-277276-0.004. */
19378 if (sect == NULL)
19379 {
19380 error (_("No MIPS_OPTIONS header found\n"));
19381 return false;
19382 }
19383 /* PR 24243 */
19384 if (sect->sh_size < sizeof (* eopt))
19385 {
19386 error (_("The MIPS options section is too small.\n"));
19387 return false;
19388 }
19389
19390 eopt = (Elf_External_Options *) get_data (NULL, filedata, options_offset, 1,
19391 sect->sh_size, _("options"));
19392 if (eopt)
19393 {
19394 Elf_Internal_Options option;
19395
19396 offset = cnt = 0;
19397 while (offset <= sect->sh_size - sizeof (* eopt))
19398 {
19399 Elf_External_Options * eoption;
19400 unsigned int optsize;
19401
19402 eoption = (Elf_External_Options *) ((char *) eopt + offset);
19403
19404 optsize = BYTE_GET (eoption->size);
19405
19406 /* PR 17531: file: ffa0fa3b. */
19407 if (optsize < sizeof (* eopt)
19408 || optsize > sect->sh_size - offset)
19409 {
19410 error (_("Invalid size (%u) for MIPS option\n"),
19411 optsize);
19412 free (eopt);
19413 return false;
19414 }
19415 offset += optsize;
19416 ++cnt;
19417 }
19418
19419 printf (ngettext ("\nSection '%s' contains %d entry:\n",
19420 "\nSection '%s' contains %d entries:\n",
19421 cnt),
19422 printable_section_name (filedata, sect), cnt);
19423
19424 offset = 0;
19425 while (cnt-- > 0)
19426 {
19427 size_t len;
19428 Elf_External_Options * eoption;
19429
19430 eoption = (Elf_External_Options *) ((char *) eopt + offset);
19431
19432 option.kind = BYTE_GET (eoption->kind);
19433 option.size = BYTE_GET (eoption->size);
19434 option.section = BYTE_GET (eoption->section);
19435 option.info = BYTE_GET (eoption->info);
19436
19437 switch (option.kind)
19438 {
19439 case ODK_NULL:
19440 /* This shouldn't happen. */
19441 printf (" NULL %" PRId16 " %" PRIx32,
19442 option.section, option.info);
19443 break;
19444
19445 case ODK_REGINFO:
19446 printf (" REGINFO ");
19447 if (filedata->file_header.e_machine == EM_MIPS)
19448 {
19449 Elf32_External_RegInfo * ereg;
19450 Elf32_RegInfo reginfo;
19451
19452 /* 32bit form. */
19453 if (option.size < (sizeof (Elf_External_Options)
19454 + sizeof (Elf32_External_RegInfo)))
19455 {
19456 printf (_("<corrupt>\n"));
19457 error (_("Truncated MIPS REGINFO option\n"));
19458 cnt = 0;
19459 break;
19460 }
19461
19462 ereg = (Elf32_External_RegInfo *) (eoption + 1);
19463
19464 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
19465 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
19466 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
19467 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
19468 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
19469 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
19470
19471 printf ("GPR %08" PRIx32 " GP 0x%" PRIx32 "\n",
19472 reginfo.ri_gprmask, reginfo.ri_gp_value);
19473 printf (" "
19474 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
19475 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
19476 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
19477 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
19478 }
19479 else
19480 {
19481 /* 64 bit form. */
19482 Elf64_External_RegInfo * ereg;
19483 Elf64_Internal_RegInfo reginfo;
19484
19485 if (option.size < (sizeof (Elf_External_Options)
19486 + sizeof (Elf64_External_RegInfo)))
19487 {
19488 printf (_("<corrupt>\n"));
19489 error (_("Truncated MIPS REGINFO option\n"));
19490 cnt = 0;
19491 break;
19492 }
19493
19494 ereg = (Elf64_External_RegInfo *) (eoption + 1);
19495 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
19496 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
19497 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
19498 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
19499 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
19500 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
19501
19502 printf ("GPR %08" PRIx32 " GP 0x%" PRIx64 "\n",
19503 reginfo.ri_gprmask, reginfo.ri_gp_value);
19504 printf (" "
19505 " CPR0 %08" PRIx32 " CPR1 %08" PRIx32
19506 " CPR2 %08" PRIx32 " CPR3 %08" PRIx32 "\n",
19507 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
19508 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
19509 }
19510 offset += option.size;
19511 continue;
19512
19513 case ODK_EXCEPTIONS:
19514 fputs (" EXCEPTIONS fpe_min(", stdout);
19515 process_mips_fpe_exception (option.info & OEX_FPU_MIN);
19516 fputs (") fpe_max(", stdout);
19517 process_mips_fpe_exception ((option.info & OEX_FPU_MAX) >> 8);
19518 fputs (")", stdout);
19519
19520 if (option.info & OEX_PAGE0)
19521 fputs (" PAGE0", stdout);
19522 if (option.info & OEX_SMM)
19523 fputs (" SMM", stdout);
19524 if (option.info & OEX_FPDBUG)
19525 fputs (" FPDBUG", stdout);
19526 if (option.info & OEX_DISMISS)
19527 fputs (" DISMISS", stdout);
19528 break;
19529
19530 case ODK_PAD:
19531 fputs (" PAD ", stdout);
19532 if (option.info & OPAD_PREFIX)
19533 fputs (" PREFIX", stdout);
19534 if (option.info & OPAD_POSTFIX)
19535 fputs (" POSTFIX", stdout);
19536 if (option.info & OPAD_SYMBOL)
19537 fputs (" SYMBOL", stdout);
19538 break;
19539
19540 case ODK_HWPATCH:
19541 fputs (" HWPATCH ", stdout);
19542 if (option.info & OHW_R4KEOP)
19543 fputs (" R4KEOP", stdout);
19544 if (option.info & OHW_R8KPFETCH)
19545 fputs (" R8KPFETCH", stdout);
19546 if (option.info & OHW_R5KEOP)
19547 fputs (" R5KEOP", stdout);
19548 if (option.info & OHW_R5KCVTL)
19549 fputs (" R5KCVTL", stdout);
19550 break;
19551
19552 case ODK_FILL:
19553 fputs (" FILL ", stdout);
19554 /* XXX Print content of info word? */
19555 break;
19556
19557 case ODK_TAGS:
19558 fputs (" TAGS ", stdout);
19559 /* XXX Print content of info word? */
19560 break;
19561
19562 case ODK_HWAND:
19563 fputs (" HWAND ", stdout);
19564 if (option.info & OHWA0_R4KEOP_CHECKED)
19565 fputs (" R4KEOP_CHECKED", stdout);
19566 if (option.info & OHWA0_R4KEOP_CLEAN)
19567 fputs (" R4KEOP_CLEAN", stdout);
19568 break;
19569
19570 case ODK_HWOR:
19571 fputs (" HWOR ", stdout);
19572 if (option.info & OHWA0_R4KEOP_CHECKED)
19573 fputs (" R4KEOP_CHECKED", stdout);
19574 if (option.info & OHWA0_R4KEOP_CLEAN)
19575 fputs (" R4KEOP_CLEAN", stdout);
19576 break;
19577
19578 case ODK_GP_GROUP:
19579 printf (" GP_GROUP %#06x self-contained %#06x",
19580 option.info & OGP_GROUP,
19581 (option.info & OGP_SELF) >> 16);
19582 break;
19583
19584 case ODK_IDENT:
19585 printf (" IDENT %#06x self-contained %#06x",
19586 option.info & OGP_GROUP,
19587 (option.info & OGP_SELF) >> 16);
19588 break;
19589
19590 default:
19591 /* This shouldn't happen. */
19592 printf (" %3d ??? %" PRId16 " %" PRIx32,
19593 option.kind, option.section, option.info);
19594 break;
19595 }
19596
19597 len = sizeof (* eopt);
19598 while (len < option.size)
19599 {
19600 unsigned char datum = *((unsigned char *) eoption + len);
19601
19602 if (ISPRINT (datum))
19603 printf ("%c", datum);
19604 else
19605 printf ("\\%03o", datum);
19606 len ++;
19607 }
19608 fputs ("\n", stdout);
19609
19610 offset += option.size;
19611 }
19612 free (eopt);
19613 }
19614 else
19615 res = false;
19616 }
19617
19618 if (conflicts_offset != 0 && conflictsno != 0)
19619 {
19620 Elf32_Conflict * iconf;
19621 size_t cnt;
19622
19623 if (filedata->dynamic_symbols == NULL)
19624 {
19625 error (_("conflict list found without a dynamic symbol table\n"));
19626 return false;
19627 }
19628
19629 /* PR 21345 - print a slightly more helpful error message
19630 if we are sure that the cmalloc will fail. */
19631 if (conflictsno > filedata->file_size / sizeof (* iconf))
19632 {
19633 error (_("Overlarge number of conflicts detected: %zx\n"),
19634 conflictsno);
19635 return false;
19636 }
19637
19638 iconf = (Elf32_Conflict *) cmalloc (conflictsno, sizeof (* iconf));
19639 if (iconf == NULL)
19640 {
19641 error (_("Out of memory allocating space for dynamic conflicts\n"));
19642 return false;
19643 }
19644
19645 if (is_32bit_elf)
19646 {
19647 Elf32_External_Conflict * econf32;
19648
19649 econf32 = (Elf32_External_Conflict *)
19650 get_data (NULL, filedata, conflicts_offset,
19651 sizeof (*econf32), conflictsno, _("conflict"));
19652 if (!econf32)
19653 {
19654 free (iconf);
19655 return false;
19656 }
19657
19658 for (cnt = 0; cnt < conflictsno; ++cnt)
19659 iconf[cnt] = BYTE_GET (econf32[cnt]);
19660
19661 free (econf32);
19662 }
19663 else
19664 {
19665 Elf64_External_Conflict * econf64;
19666
19667 econf64 = (Elf64_External_Conflict *)
19668 get_data (NULL, filedata, conflicts_offset,
19669 sizeof (*econf64), conflictsno, _("conflict"));
19670 if (!econf64)
19671 {
19672 free (iconf);
19673 return false;
19674 }
19675
19676 for (cnt = 0; cnt < conflictsno; ++cnt)
19677 iconf[cnt] = BYTE_GET (econf64[cnt]);
19678
19679 free (econf64);
19680 }
19681
19682 printf (ngettext ("\nSection '.conflict' contains %zu entry:\n",
19683 "\nSection '.conflict' contains %zu entries:\n",
19684 conflictsno),
19685 conflictsno);
19686 puts (_(" Num: Index Value Name"));
19687
19688 for (cnt = 0; cnt < conflictsno; ++cnt)
19689 {
19690 printf ("%5zu: %8lu ", cnt, iconf[cnt]);
19691
19692 if (iconf[cnt] >= filedata->num_dynamic_syms)
19693 printf (_("<corrupt symbol index>"));
19694 else
19695 {
19696 Elf_Internal_Sym * psym;
19697
19698 psym = & filedata->dynamic_symbols[iconf[cnt]];
19699 print_vma (psym->st_value, FULL_HEX);
19700 putchar (' ');
19701 if (valid_dynamic_name (filedata, psym->st_name))
19702 print_symbol_name (25, get_dynamic_name (filedata, psym->st_name));
19703 else
19704 printf (_("<corrupt: %14ld>"), psym->st_name);
19705 }
19706 putchar ('\n');
19707 }
19708
19709 free (iconf);
19710 }
19711
19712 if (pltgot != 0 && local_gotno != 0)
19713 {
19714 uint64_t ent, local_end, global_end;
19715 size_t i, offset;
19716 unsigned char * data;
19717 unsigned char * data_end;
19718 int addr_size;
19719
19720 ent = pltgot;
19721 addr_size = (is_32bit_elf ? 4 : 8);
19722 local_end = pltgot + local_gotno * addr_size;
19723
19724 /* PR binutils/17533 file: 012-111227-0.004 */
19725 if (symtabno < gotsym)
19726 {
19727 error (_("The GOT symbol offset (%" PRIu64
19728 ") is greater than the symbol table size (%" PRIu64 ")\n"),
19729 gotsym, symtabno);
19730 return false;
19731 }
19732
19733 global_end = local_end + (symtabno - gotsym) * addr_size;
19734 /* PR 17531: file: 54c91a34. */
19735 if (global_end < local_end)
19736 {
19737 error (_("Too many GOT symbols: %" PRIu64 "\n"), symtabno);
19738 return false;
19739 }
19740
19741 offset = offset_from_vma (filedata, pltgot, global_end - pltgot);
19742 data = (unsigned char *) get_data (NULL, filedata, offset,
19743 global_end - pltgot, 1,
19744 _("Global Offset Table data"));
19745 /* PR 12855: Null data is handled gracefully throughout. */
19746 data_end = data + (global_end - pltgot);
19747
19748 printf (_("\nPrimary GOT:\n"));
19749 printf (_(" Canonical gp value: "));
19750 print_vma (pltgot + 0x7ff0, LONG_HEX);
19751 printf ("\n\n");
19752
19753 printf (_(" Reserved entries:\n"));
19754 printf (_(" %*s %10s %*s Purpose\n"),
19755 addr_size * 2, _("Address"), _("Access"),
19756 addr_size * 2, _("Initial"));
19757 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19758 printf (_(" Lazy resolver\n"));
19759 if (ent == (uint64_t) -1)
19760 goto got_print_fail;
19761
19762 /* Check for the MSB of GOT[1] being set, denoting a GNU object.
19763 This entry will be used by some runtime loaders, to store the
19764 module pointer. Otherwise this is an ordinary local entry.
19765 PR 21344: Check for the entry being fully available before
19766 fetching it. */
19767 if (data
19768 && data + ent - pltgot + addr_size <= data_end
19769 && (byte_get (data + ent - pltgot, addr_size)
19770 >> (addr_size * 8 - 1)) != 0)
19771 {
19772 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19773 printf (_(" Module pointer (GNU extension)\n"));
19774 if (ent == (uint64_t) -1)
19775 goto got_print_fail;
19776 }
19777 printf ("\n");
19778
19779 if (data != NULL && ent < local_end)
19780 {
19781 printf (_(" Local entries:\n"));
19782 printf (" %*s %10s %*s\n",
19783 addr_size * 2, _("Address"), _("Access"),
19784 addr_size * 2, _("Initial"));
19785 while (ent < local_end)
19786 {
19787 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19788 printf ("\n");
19789 if (ent == (uint64_t) -1)
19790 goto got_print_fail;
19791 }
19792 printf ("\n");
19793 }
19794
19795 if (data != NULL && gotsym < symtabno)
19796 {
19797 int sym_width;
19798
19799 printf (_(" Global entries:\n"));
19800 printf (" %*s %10s %*s %*s %-7s %3s %s\n",
19801 addr_size * 2, _("Address"),
19802 _("Access"),
19803 addr_size * 2, _("Initial"),
19804 addr_size * 2, _("Sym.Val."),
19805 _("Type"),
19806 /* Note for translators: "Ndx" = abbreviated form of "Index". */
19807 _("Ndx"), _("Name"));
19808
19809 sym_width = (is_32bit_elf ? 80 : 160) - 28 - addr_size * 6 - 1;
19810
19811 for (i = gotsym; i < symtabno; i++)
19812 {
19813 ent = print_mips_got_entry (data, pltgot, ent, data_end);
19814 printf (" ");
19815
19816 if (filedata->dynamic_symbols == NULL)
19817 printf (_("<no dynamic symbols>"));
19818 else if (i < filedata->num_dynamic_syms)
19819 {
19820 Elf_Internal_Sym * psym = filedata->dynamic_symbols + i;
19821
19822 print_vma (psym->st_value, LONG_HEX);
19823 printf (" %-7s ", get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)));
19824
19825 bool is_special;
19826 const char * s = printable_section_name_from_index (filedata, psym->st_shndx, & is_special);
19827 if (is_special)
19828 printf ("%3s ", s);
19829 else
19830 printf ("%3u ", psym->st_shndx);
19831
19832 if (valid_dynamic_name (filedata, psym->st_name))
19833 print_symbol_name (sym_width,
19834 get_dynamic_name (filedata, psym->st_name));
19835 else
19836 printf (_("<corrupt: %14ld>"), psym->st_name);
19837 }
19838 else
19839 printf (_("<symbol index %zu exceeds number of dynamic symbols>"),
19840 i);
19841
19842 printf ("\n");
19843 if (ent == (uint64_t) -1)
19844 break;
19845 }
19846 printf ("\n");
19847 }
19848
19849 got_print_fail:
19850 free (data);
19851 }
19852
19853 if (mips_pltgot != 0 && jmprel != 0 && pltrel != 0 && pltrelsz != 0)
19854 {
19855 uint64_t ent, end;
19856 uint64_t offset, rel_offset;
19857 uint64_t count, i;
19858 unsigned char * data;
19859 int addr_size, sym_width;
19860 Elf_Internal_Rela * rels;
19861
19862 rel_offset = offset_from_vma (filedata, jmprel, pltrelsz);
19863 if (pltrel == DT_RELA)
19864 {
19865 if (!slurp_rela_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19866 return false;
19867 }
19868 else
19869 {
19870 if (!slurp_rel_relocs (filedata, rel_offset, pltrelsz, &rels, &count))
19871 return false;
19872 }
19873
19874 ent = mips_pltgot;
19875 addr_size = (is_32bit_elf ? 4 : 8);
19876 end = mips_pltgot + (2 + count) * addr_size;
19877
19878 offset = offset_from_vma (filedata, mips_pltgot, end - mips_pltgot);
19879 data = (unsigned char *) get_data (NULL, filedata, offset, end - mips_pltgot,
19880 1, _("Procedure Linkage Table data"));
19881 if (data == NULL)
19882 {
19883 free (rels);
19884 return false;
19885 }
19886
19887 printf ("\nPLT GOT:\n\n");
19888 printf (_(" Reserved entries:\n"));
19889 printf (_(" %*s %*s Purpose\n"),
19890 addr_size * 2, _("Address"), addr_size * 2, _("Initial"));
19891 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19892 printf (_(" PLT lazy resolver\n"));
19893 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19894 printf (_(" Module pointer\n"));
19895 printf ("\n");
19896
19897 printf (_(" Entries:\n"));
19898 printf (" %*s %*s %*s %-7s %3s %s\n",
19899 addr_size * 2, _("Address"),
19900 addr_size * 2, _("Initial"),
19901 addr_size * 2, _("Sym.Val."), _("Type"), _("Ndx"), _("Name"));
19902 sym_width = (is_32bit_elf ? 80 : 160) - 17 - addr_size * 6 - 1;
19903 for (i = 0; i < count; i++)
19904 {
19905 uint64_t idx = get_reloc_symindex (rels[i].r_info);
19906
19907 ent = print_mips_pltgot_entry (data, mips_pltgot, ent);
19908 printf (" ");
19909
19910 if (idx >= filedata->num_dynamic_syms)
19911 printf (_("<corrupt symbol index: %" PRIu64 ">"), idx);
19912 else
19913 {
19914 Elf_Internal_Sym * psym = filedata->dynamic_symbols + idx;
19915
19916 print_vma (psym->st_value, LONG_HEX);
19917 printf (" %-7s %3s ",
19918 get_symbol_type (filedata, ELF_ST_TYPE (psym->st_info)),
19919 printable_section_name_from_index (filedata, psym->st_shndx, NULL));
19920 if (valid_dynamic_name (filedata, psym->st_name))
19921 print_symbol_name (sym_width,
19922 get_dynamic_name (filedata, psym->st_name));
19923 else
19924 printf (_("<corrupt: %14ld>"), psym->st_name);
19925 }
19926 printf ("\n");
19927 }
19928 printf ("\n");
19929
19930 free (data);
19931 free (rels);
19932 }
19933
19934 return res;
19935 }
19936
19937 static bool
19938 process_nds32_specific (Filedata * filedata)
19939 {
19940 Elf_Internal_Shdr *sect = NULL;
19941
19942 sect = find_section (filedata, ".nds32_e_flags");
19943 if (sect != NULL && sect->sh_size >= 4)
19944 {
19945 unsigned char *buf;
19946 unsigned int flag;
19947
19948 printf ("\nNDS32 elf flags section:\n");
19949 buf = get_data (NULL, filedata, sect->sh_offset, 1, 4,
19950 _("NDS32 elf flags section"));
19951
19952 if (buf == NULL)
19953 return false;
19954
19955 flag = byte_get (buf, 4);
19956 free (buf);
19957 switch (flag & 0x3)
19958 {
19959 case 0:
19960 printf ("(VEC_SIZE):\tNo entry.\n");
19961 break;
19962 case 1:
19963 printf ("(VEC_SIZE):\t4 bytes\n");
19964 break;
19965 case 2:
19966 printf ("(VEC_SIZE):\t16 bytes\n");
19967 break;
19968 case 3:
19969 printf ("(VEC_SIZE):\treserved\n");
19970 break;
19971 }
19972 }
19973
19974 return true;
19975 }
19976
19977 static bool
19978 process_gnu_liblist (Filedata * filedata)
19979 {
19980 Elf_Internal_Shdr * section;
19981 Elf_Internal_Shdr * string_sec;
19982 Elf32_External_Lib * elib;
19983 char * strtab;
19984 size_t strtab_size;
19985 size_t cnt;
19986 uint64_t num_liblist;
19987 unsigned i;
19988 bool res = true;
19989
19990 if (! do_arch)
19991 return true;
19992
19993 for (i = 0, section = filedata->section_headers;
19994 i < filedata->file_header.e_shnum;
19995 i++, section++)
19996 {
19997 switch (section->sh_type)
19998 {
19999 case SHT_GNU_LIBLIST:
20000 if (section->sh_link >= filedata->file_header.e_shnum)
20001 break;
20002
20003 elib = (Elf32_External_Lib *)
20004 get_data (NULL, filedata, section->sh_offset, 1, section->sh_size,
20005 _("liblist section data"));
20006
20007 if (elib == NULL)
20008 {
20009 res = false;
20010 break;
20011 }
20012
20013 string_sec = filedata->section_headers + section->sh_link;
20014 strtab = (char *) get_data (NULL, filedata, string_sec->sh_offset, 1,
20015 string_sec->sh_size,
20016 _("liblist string table"));
20017 if (strtab == NULL
20018 || section->sh_entsize != sizeof (Elf32_External_Lib))
20019 {
20020 free (elib);
20021 free (strtab);
20022 res = false;
20023 break;
20024 }
20025 strtab_size = string_sec->sh_size;
20026
20027 num_liblist = section->sh_size / sizeof (Elf32_External_Lib);
20028 printf (ngettext ("\nLibrary list section '%s' contains %" PRIu64
20029 " entries:\n",
20030 "\nLibrary list section '%s' contains %" PRIu64
20031 " entries:\n",
20032 num_liblist),
20033 printable_section_name (filedata, section),
20034 num_liblist);
20035
20036 puts (_(" Library Time Stamp Checksum Version Flags"));
20037
20038 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
20039 ++cnt)
20040 {
20041 Elf32_Lib liblist;
20042 time_t atime;
20043 char timebuf[128];
20044 struct tm * tmp;
20045
20046 liblist.l_name = BYTE_GET (elib[cnt].l_name);
20047 atime = BYTE_GET (elib[cnt].l_time_stamp);
20048 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
20049 liblist.l_version = BYTE_GET (elib[cnt].l_version);
20050 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
20051
20052 tmp = gmtime (&atime);
20053 snprintf (timebuf, sizeof (timebuf),
20054 "%04u-%02u-%02uT%02u:%02u:%02u",
20055 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
20056 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
20057
20058 printf ("%3zu: ", cnt);
20059 if (do_wide)
20060 printf ("%-20s", liblist.l_name < strtab_size
20061 ? strtab + liblist.l_name : _("<corrupt>"));
20062 else
20063 printf ("%-20.20s", liblist.l_name < strtab_size
20064 ? strtab + liblist.l_name : _("<corrupt>"));
20065 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
20066 liblist.l_version, liblist.l_flags);
20067 }
20068
20069 free (elib);
20070 free (strtab);
20071 }
20072 }
20073
20074 return res;
20075 }
20076
20077 static const char *
20078 get_note_type (Filedata * filedata, unsigned e_type)
20079 {
20080 static char buff[64];
20081
20082 if (filedata->file_header.e_type == ET_CORE)
20083 switch (e_type)
20084 {
20085 case NT_AUXV:
20086 return _("NT_AUXV (auxiliary vector)");
20087 case NT_PRSTATUS:
20088 return _("NT_PRSTATUS (prstatus structure)");
20089 case NT_FPREGSET:
20090 return _("NT_FPREGSET (floating point registers)");
20091 case NT_PRPSINFO:
20092 return _("NT_PRPSINFO (prpsinfo structure)");
20093 case NT_TASKSTRUCT:
20094 return _("NT_TASKSTRUCT (task structure)");
20095 case NT_GDB_TDESC:
20096 return _("NT_GDB_TDESC (GDB XML target description)");
20097 case NT_PRXFPREG:
20098 return _("NT_PRXFPREG (user_xfpregs structure)");
20099 case NT_PPC_VMX:
20100 return _("NT_PPC_VMX (ppc Altivec registers)");
20101 case NT_PPC_VSX:
20102 return _("NT_PPC_VSX (ppc VSX registers)");
20103 case NT_PPC_TAR:
20104 return _("NT_PPC_TAR (ppc TAR register)");
20105 case NT_PPC_PPR:
20106 return _("NT_PPC_PPR (ppc PPR register)");
20107 case NT_PPC_DSCR:
20108 return _("NT_PPC_DSCR (ppc DSCR register)");
20109 case NT_PPC_EBB:
20110 return _("NT_PPC_EBB (ppc EBB registers)");
20111 case NT_PPC_PMU:
20112 return _("NT_PPC_PMU (ppc PMU registers)");
20113 case NT_PPC_TM_CGPR:
20114 return _("NT_PPC_TM_CGPR (ppc checkpointed GPR registers)");
20115 case NT_PPC_TM_CFPR:
20116 return _("NT_PPC_TM_CFPR (ppc checkpointed floating point registers)");
20117 case NT_PPC_TM_CVMX:
20118 return _("NT_PPC_TM_CVMX (ppc checkpointed Altivec registers)");
20119 case NT_PPC_TM_CVSX:
20120 return _("NT_PPC_TM_CVSX (ppc checkpointed VSX registers)");
20121 case NT_PPC_TM_SPR:
20122 return _("NT_PPC_TM_SPR (ppc TM special purpose registers)");
20123 case NT_PPC_TM_CTAR:
20124 return _("NT_PPC_TM_CTAR (ppc checkpointed TAR register)");
20125 case NT_PPC_TM_CPPR:
20126 return _("NT_PPC_TM_CPPR (ppc checkpointed PPR register)");
20127 case NT_PPC_TM_CDSCR:
20128 return _("NT_PPC_TM_CDSCR (ppc checkpointed DSCR register)");
20129 case NT_386_TLS:
20130 return _("NT_386_TLS (x86 TLS information)");
20131 case NT_386_IOPERM:
20132 return _("NT_386_IOPERM (x86 I/O permissions)");
20133 case NT_X86_XSTATE:
20134 return _("NT_X86_XSTATE (x86 XSAVE extended state)");
20135 case NT_X86_CET:
20136 return _("NT_X86_CET (x86 CET state)");
20137 case NT_S390_HIGH_GPRS:
20138 return _("NT_S390_HIGH_GPRS (s390 upper register halves)");
20139 case NT_S390_TIMER:
20140 return _("NT_S390_TIMER (s390 timer register)");
20141 case NT_S390_TODCMP:
20142 return _("NT_S390_TODCMP (s390 TOD comparator register)");
20143 case NT_S390_TODPREG:
20144 return _("NT_S390_TODPREG (s390 TOD programmable register)");
20145 case NT_S390_CTRS:
20146 return _("NT_S390_CTRS (s390 control registers)");
20147 case NT_S390_PREFIX:
20148 return _("NT_S390_PREFIX (s390 prefix register)");
20149 case NT_S390_LAST_BREAK:
20150 return _("NT_S390_LAST_BREAK (s390 last breaking event address)");
20151 case NT_S390_SYSTEM_CALL:
20152 return _("NT_S390_SYSTEM_CALL (s390 system call restart data)");
20153 case NT_S390_TDB:
20154 return _("NT_S390_TDB (s390 transaction diagnostic block)");
20155 case NT_S390_VXRS_LOW:
20156 return _("NT_S390_VXRS_LOW (s390 vector registers 0-15 upper half)");
20157 case NT_S390_VXRS_HIGH:
20158 return _("NT_S390_VXRS_HIGH (s390 vector registers 16-31)");
20159 case NT_S390_GS_CB:
20160 return _("NT_S390_GS_CB (s390 guarded-storage registers)");
20161 case NT_S390_GS_BC:
20162 return _("NT_S390_GS_BC (s390 guarded-storage broadcast control)");
20163 case NT_ARM_VFP:
20164 return _("NT_ARM_VFP (arm VFP registers)");
20165 case NT_ARM_TLS:
20166 return _("NT_ARM_TLS (AArch TLS registers)");
20167 case NT_ARM_HW_BREAK:
20168 return _("NT_ARM_HW_BREAK (AArch hardware breakpoint registers)");
20169 case NT_ARM_HW_WATCH:
20170 return _("NT_ARM_HW_WATCH (AArch hardware watchpoint registers)");
20171 case NT_ARM_SYSTEM_CALL:
20172 return _("NT_ARM_SYSTEM_CALL (AArch system call number)");
20173 case NT_ARM_SVE:
20174 return _("NT_ARM_SVE (AArch SVE registers)");
20175 case NT_ARM_PAC_MASK:
20176 return _("NT_ARM_PAC_MASK (AArch pointer authentication code masks)");
20177 case NT_ARM_PACA_KEYS:
20178 return _("NT_ARM_PACA_KEYS (ARM pointer authentication address keys)");
20179 case NT_ARM_PACG_KEYS:
20180 return _("NT_ARM_PACG_KEYS (ARM pointer authentication generic keys)");
20181 case NT_ARM_TAGGED_ADDR_CTRL:
20182 return _("NT_ARM_TAGGED_ADDR_CTRL (AArch tagged address control)");
20183 case NT_ARM_SSVE:
20184 return _("NT_ARM_SSVE (AArch64 streaming SVE registers)");
20185 case NT_ARM_ZA:
20186 return _("NT_ARM_ZA (AArch64 SME ZA register)");
20187 case NT_ARM_ZT:
20188 return _("NT_ARM_ZT (AArch64 SME2 ZT registers)");
20189 case NT_ARM_PAC_ENABLED_KEYS:
20190 return _("NT_ARM_PAC_ENABLED_KEYS (AArch64 pointer authentication enabled keys)");
20191 case NT_ARC_V2:
20192 return _("NT_ARC_V2 (ARC HS accumulator/extra registers)");
20193 case NT_RISCV_CSR:
20194 return _("NT_RISCV_CSR (RISC-V control and status registers)");
20195 case NT_PSTATUS:
20196 return _("NT_PSTATUS (pstatus structure)");
20197 case NT_FPREGS:
20198 return _("NT_FPREGS (floating point registers)");
20199 case NT_PSINFO:
20200 return _("NT_PSINFO (psinfo structure)");
20201 case NT_LWPSTATUS:
20202 return _("NT_LWPSTATUS (lwpstatus_t structure)");
20203 case NT_LWPSINFO:
20204 return _("NT_LWPSINFO (lwpsinfo_t structure)");
20205 case NT_WIN32PSTATUS:
20206 return _("NT_WIN32PSTATUS (win32_pstatus structure)");
20207 case NT_SIGINFO:
20208 return _("NT_SIGINFO (siginfo_t data)");
20209 case NT_FILE:
20210 return _("NT_FILE (mapped files)");
20211 default:
20212 break;
20213 }
20214 else
20215 switch (e_type)
20216 {
20217 case NT_VERSION:
20218 return _("NT_VERSION (version)");
20219 case NT_ARCH:
20220 return _("NT_ARCH (architecture)");
20221 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
20222 return _("OPEN");
20223 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
20224 return _("func");
20225 case NT_GO_BUILDID:
20226 return _("GO BUILDID");
20227 case FDO_PACKAGING_METADATA:
20228 return _("FDO_PACKAGING_METADATA");
20229 default:
20230 break;
20231 }
20232
20233 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20234 return buff;
20235 }
20236
20237 static bool
20238 print_core_note (Elf_Internal_Note *pnote)
20239 {
20240 unsigned int addr_size = is_32bit_elf ? 4 : 8;
20241 uint64_t count, page_size;
20242 unsigned char *descdata, *filenames, *descend;
20243
20244 if (pnote->type != NT_FILE)
20245 {
20246 if (do_wide)
20247 printf ("\n");
20248 return true;
20249 }
20250
20251 if (!is_32bit_elf)
20252 {
20253 printf (_(" Cannot decode 64-bit note in 32-bit build\n"));
20254 /* Still "successful". */
20255 return true;
20256 }
20257
20258 if (pnote->descsz < 2 * addr_size)
20259 {
20260 error (_(" Malformed note - too short for header\n"));
20261 return false;
20262 }
20263
20264 descdata = (unsigned char *) pnote->descdata;
20265 descend = descdata + pnote->descsz;
20266
20267 if (descdata[pnote->descsz - 1] != '\0')
20268 {
20269 error (_(" Malformed note - does not end with \\0\n"));
20270 return false;
20271 }
20272
20273 count = byte_get (descdata, addr_size);
20274 descdata += addr_size;
20275
20276 page_size = byte_get (descdata, addr_size);
20277 descdata += addr_size;
20278
20279 if (count > ((uint64_t) -1 - 2 * addr_size) / (3 * addr_size)
20280 || pnote->descsz < 2 * addr_size + count * 3 * addr_size)
20281 {
20282 error (_(" Malformed note - too short for supplied file count\n"));
20283 return false;
20284 }
20285
20286 printf (_(" Page size: "));
20287 print_vma (page_size, DEC);
20288 printf ("\n");
20289
20290 printf (_(" %*s%*s%*s\n"),
20291 (int) (2 + 2 * addr_size), _("Start"),
20292 (int) (4 + 2 * addr_size), _("End"),
20293 (int) (4 + 2 * addr_size), _("Page Offset"));
20294 filenames = descdata + count * 3 * addr_size;
20295 while (count-- > 0)
20296 {
20297 uint64_t start, end, file_ofs;
20298
20299 if (filenames == descend)
20300 {
20301 error (_(" Malformed note - filenames end too early\n"));
20302 return false;
20303 }
20304
20305 start = byte_get (descdata, addr_size);
20306 descdata += addr_size;
20307 end = byte_get (descdata, addr_size);
20308 descdata += addr_size;
20309 file_ofs = byte_get (descdata, addr_size);
20310 descdata += addr_size;
20311
20312 printf (" ");
20313 print_vma (start, FULL_HEX);
20314 printf (" ");
20315 print_vma (end, FULL_HEX);
20316 printf (" ");
20317 print_vma (file_ofs, FULL_HEX);
20318 printf ("\n %s\n", filenames);
20319
20320 filenames += 1 + strlen ((char *) filenames);
20321 }
20322
20323 return true;
20324 }
20325
20326 static const char *
20327 get_gnu_elf_note_type (unsigned e_type)
20328 {
20329 /* NB/ Keep this switch statement in sync with print_gnu_note (). */
20330 switch (e_type)
20331 {
20332 case NT_GNU_ABI_TAG:
20333 return _("NT_GNU_ABI_TAG (ABI version tag)");
20334 case NT_GNU_HWCAP:
20335 return _("NT_GNU_HWCAP (DSO-supplied software HWCAP info)");
20336 case NT_GNU_BUILD_ID:
20337 return _("NT_GNU_BUILD_ID (unique build ID bitstring)");
20338 case NT_GNU_GOLD_VERSION:
20339 return _("NT_GNU_GOLD_VERSION (gold version)");
20340 case NT_GNU_PROPERTY_TYPE_0:
20341 return _("NT_GNU_PROPERTY_TYPE_0");
20342 case NT_GNU_BUILD_ATTRIBUTE_OPEN:
20343 return _("NT_GNU_BUILD_ATTRIBUTE_OPEN");
20344 case NT_GNU_BUILD_ATTRIBUTE_FUNC:
20345 return _("NT_GNU_BUILD_ATTRIBUTE_FUNC");
20346 default:
20347 {
20348 static char buff[64];
20349
20350 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
20351 return buff;
20352 }
20353 }
20354 }
20355
20356 static void
20357 decode_x86_compat_isa (unsigned int bitmask)
20358 {
20359 while (bitmask)
20360 {
20361 unsigned int bit = bitmask & (- bitmask);
20362
20363 bitmask &= ~ bit;
20364 switch (bit)
20365 {
20366 case GNU_PROPERTY_X86_COMPAT_ISA_1_486:
20367 printf ("i486");
20368 break;
20369 case GNU_PROPERTY_X86_COMPAT_ISA_1_586:
20370 printf ("586");
20371 break;
20372 case GNU_PROPERTY_X86_COMPAT_ISA_1_686:
20373 printf ("686");
20374 break;
20375 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE:
20376 printf ("SSE");
20377 break;
20378 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE2:
20379 printf ("SSE2");
20380 break;
20381 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE3:
20382 printf ("SSE3");
20383 break;
20384 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSSE3:
20385 printf ("SSSE3");
20386 break;
20387 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_1:
20388 printf ("SSE4_1");
20389 break;
20390 case GNU_PROPERTY_X86_COMPAT_ISA_1_SSE4_2:
20391 printf ("SSE4_2");
20392 break;
20393 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX:
20394 printf ("AVX");
20395 break;
20396 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX2:
20397 printf ("AVX2");
20398 break;
20399 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512F:
20400 printf ("AVX512F");
20401 break;
20402 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512CD:
20403 printf ("AVX512CD");
20404 break;
20405 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512ER:
20406 printf ("AVX512ER");
20407 break;
20408 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512PF:
20409 printf ("AVX512PF");
20410 break;
20411 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512VL:
20412 printf ("AVX512VL");
20413 break;
20414 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512DQ:
20415 printf ("AVX512DQ");
20416 break;
20417 case GNU_PROPERTY_X86_COMPAT_ISA_1_AVX512BW:
20418 printf ("AVX512BW");
20419 break;
20420 default:
20421 printf (_("<unknown: %x>"), bit);
20422 break;
20423 }
20424 if (bitmask)
20425 printf (", ");
20426 }
20427 }
20428
20429 static void
20430 decode_x86_compat_2_isa (unsigned int bitmask)
20431 {
20432 if (!bitmask)
20433 {
20434 printf (_("<None>"));
20435 return;
20436 }
20437
20438 while (bitmask)
20439 {
20440 unsigned int bit = bitmask & (- bitmask);
20441
20442 bitmask &= ~ bit;
20443 switch (bit)
20444 {
20445 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_CMOV:
20446 printf ("CMOV");
20447 break;
20448 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE:
20449 printf ("SSE");
20450 break;
20451 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE2:
20452 printf ("SSE2");
20453 break;
20454 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE3:
20455 printf ("SSE3");
20456 break;
20457 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSSE3:
20458 printf ("SSSE3");
20459 break;
20460 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_1:
20461 printf ("SSE4_1");
20462 break;
20463 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_SSE4_2:
20464 printf ("SSE4_2");
20465 break;
20466 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX:
20467 printf ("AVX");
20468 break;
20469 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX2:
20470 printf ("AVX2");
20471 break;
20472 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_FMA:
20473 printf ("FMA");
20474 break;
20475 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512F:
20476 printf ("AVX512F");
20477 break;
20478 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512CD:
20479 printf ("AVX512CD");
20480 break;
20481 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512ER:
20482 printf ("AVX512ER");
20483 break;
20484 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512PF:
20485 printf ("AVX512PF");
20486 break;
20487 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512VL:
20488 printf ("AVX512VL");
20489 break;
20490 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512DQ:
20491 printf ("AVX512DQ");
20492 break;
20493 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512BW:
20494 printf ("AVX512BW");
20495 break;
20496 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4FMAPS:
20497 printf ("AVX512_4FMAPS");
20498 break;
20499 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_4VNNIW:
20500 printf ("AVX512_4VNNIW");
20501 break;
20502 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BITALG:
20503 printf ("AVX512_BITALG");
20504 break;
20505 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_IFMA:
20506 printf ("AVX512_IFMA");
20507 break;
20508 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI:
20509 printf ("AVX512_VBMI");
20510 break;
20511 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VBMI2:
20512 printf ("AVX512_VBMI2");
20513 break;
20514 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_VNNI:
20515 printf ("AVX512_VNNI");
20516 break;
20517 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_AVX512_BF16:
20518 printf ("AVX512_BF16");
20519 break;
20520 default:
20521 printf (_("<unknown: %x>"), bit);
20522 break;
20523 }
20524 if (bitmask)
20525 printf (", ");
20526 }
20527 }
20528
20529 static const char *
20530 get_amdgpu_elf_note_type (unsigned int e_type)
20531 {
20532 switch (e_type)
20533 {
20534 case NT_AMDGPU_METADATA:
20535 return _("NT_AMDGPU_METADATA (code object metadata)");
20536 default:
20537 {
20538 static char buf[64];
20539 snprintf (buf, sizeof (buf), _("Unknown note type: (0x%08x)"), e_type);
20540 return buf;
20541 }
20542 }
20543 }
20544
20545 static void
20546 decode_x86_isa (unsigned int bitmask)
20547 {
20548 while (bitmask)
20549 {
20550 unsigned int bit = bitmask & (- bitmask);
20551
20552 bitmask &= ~ bit;
20553 switch (bit)
20554 {
20555 case GNU_PROPERTY_X86_ISA_1_BASELINE:
20556 printf ("x86-64-baseline");
20557 break;
20558 case GNU_PROPERTY_X86_ISA_1_V2:
20559 printf ("x86-64-v2");
20560 break;
20561 case GNU_PROPERTY_X86_ISA_1_V3:
20562 printf ("x86-64-v3");
20563 break;
20564 case GNU_PROPERTY_X86_ISA_1_V4:
20565 printf ("x86-64-v4");
20566 break;
20567 default:
20568 printf (_("<unknown: %x>"), bit);
20569 break;
20570 }
20571 if (bitmask)
20572 printf (", ");
20573 }
20574 }
20575
20576 static void
20577 decode_x86_feature_1 (unsigned int bitmask)
20578 {
20579 if (!bitmask)
20580 {
20581 printf (_("<None>"));
20582 return;
20583 }
20584
20585 while (bitmask)
20586 {
20587 unsigned int bit = bitmask & (- bitmask);
20588
20589 bitmask &= ~ bit;
20590 switch (bit)
20591 {
20592 case GNU_PROPERTY_X86_FEATURE_1_IBT:
20593 printf ("IBT");
20594 break;
20595 case GNU_PROPERTY_X86_FEATURE_1_SHSTK:
20596 printf ("SHSTK");
20597 break;
20598 case GNU_PROPERTY_X86_FEATURE_1_LAM_U48:
20599 printf ("LAM_U48");
20600 break;
20601 case GNU_PROPERTY_X86_FEATURE_1_LAM_U57:
20602 printf ("LAM_U57");
20603 break;
20604 default:
20605 printf (_("<unknown: %x>"), bit);
20606 break;
20607 }
20608 if (bitmask)
20609 printf (", ");
20610 }
20611 }
20612
20613 static void
20614 decode_x86_feature_2 (unsigned int bitmask)
20615 {
20616 if (!bitmask)
20617 {
20618 printf (_("<None>"));
20619 return;
20620 }
20621
20622 while (bitmask)
20623 {
20624 unsigned int bit = bitmask & (- bitmask);
20625
20626 bitmask &= ~ bit;
20627 switch (bit)
20628 {
20629 case GNU_PROPERTY_X86_FEATURE_2_X86:
20630 printf ("x86");
20631 break;
20632 case GNU_PROPERTY_X86_FEATURE_2_X87:
20633 printf ("x87");
20634 break;
20635 case GNU_PROPERTY_X86_FEATURE_2_MMX:
20636 printf ("MMX");
20637 break;
20638 case GNU_PROPERTY_X86_FEATURE_2_XMM:
20639 printf ("XMM");
20640 break;
20641 case GNU_PROPERTY_X86_FEATURE_2_YMM:
20642 printf ("YMM");
20643 break;
20644 case GNU_PROPERTY_X86_FEATURE_2_ZMM:
20645 printf ("ZMM");
20646 break;
20647 case GNU_PROPERTY_X86_FEATURE_2_TMM:
20648 printf ("TMM");
20649 break;
20650 case GNU_PROPERTY_X86_FEATURE_2_MASK:
20651 printf ("MASK");
20652 break;
20653 case GNU_PROPERTY_X86_FEATURE_2_FXSR:
20654 printf ("FXSR");
20655 break;
20656 case GNU_PROPERTY_X86_FEATURE_2_XSAVE:
20657 printf ("XSAVE");
20658 break;
20659 case GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT:
20660 printf ("XSAVEOPT");
20661 break;
20662 case GNU_PROPERTY_X86_FEATURE_2_XSAVEC:
20663 printf ("XSAVEC");
20664 break;
20665 default:
20666 printf (_("<unknown: %x>"), bit);
20667 break;
20668 }
20669 if (bitmask)
20670 printf (", ");
20671 }
20672 }
20673
20674 static void
20675 decode_aarch64_feature_1_and (unsigned int bitmask)
20676 {
20677 while (bitmask)
20678 {
20679 unsigned int bit = bitmask & (- bitmask);
20680
20681 bitmask &= ~ bit;
20682 switch (bit)
20683 {
20684 case GNU_PROPERTY_AARCH64_FEATURE_1_BTI:
20685 printf ("BTI");
20686 break;
20687
20688 case GNU_PROPERTY_AARCH64_FEATURE_1_PAC:
20689 printf ("PAC");
20690 break;
20691
20692 default:
20693 printf (_("<unknown: %x>"), bit);
20694 break;
20695 }
20696 if (bitmask)
20697 printf (", ");
20698 }
20699 }
20700
20701 static void
20702 decode_1_needed (unsigned int bitmask)
20703 {
20704 while (bitmask)
20705 {
20706 unsigned int bit = bitmask & (- bitmask);
20707
20708 bitmask &= ~ bit;
20709 switch (bit)
20710 {
20711 case GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS:
20712 printf ("indirect external access");
20713 break;
20714 default:
20715 printf (_("<unknown: %x>"), bit);
20716 break;
20717 }
20718 if (bitmask)
20719 printf (", ");
20720 }
20721 }
20722
20723 static void
20724 print_gnu_property_note (Filedata * filedata, Elf_Internal_Note * pnote)
20725 {
20726 unsigned char * ptr = (unsigned char *) pnote->descdata;
20727 unsigned char * ptr_end = ptr + pnote->descsz;
20728 unsigned int size = is_32bit_elf ? 4 : 8;
20729
20730 printf (_(" Properties: "));
20731
20732 if (pnote->descsz < 8 || (pnote->descsz % size) != 0)
20733 {
20734 printf (_("<corrupt GNU_PROPERTY_TYPE, size = %#lx>\n"), pnote->descsz);
20735 return;
20736 }
20737
20738 while (ptr < ptr_end)
20739 {
20740 unsigned int j;
20741 unsigned int type;
20742 unsigned int datasz;
20743
20744 if ((size_t) (ptr_end - ptr) < 8)
20745 {
20746 printf (_("<corrupt descsz: %#lx>\n"), pnote->descsz);
20747 break;
20748 }
20749
20750 type = byte_get (ptr, 4);
20751 datasz = byte_get (ptr + 4, 4);
20752
20753 ptr += 8;
20754
20755 if (datasz > (size_t) (ptr_end - ptr))
20756 {
20757 printf (_("<corrupt type (%#x) datasz: %#x>\n"),
20758 type, datasz);
20759 break;
20760 }
20761
20762 if (type >= GNU_PROPERTY_LOPROC && type <= GNU_PROPERTY_HIPROC)
20763 {
20764 if (filedata->file_header.e_machine == EM_X86_64
20765 || filedata->file_header.e_machine == EM_IAMCU
20766 || filedata->file_header.e_machine == EM_386)
20767 {
20768 unsigned int bitmask;
20769
20770 if (datasz == 4)
20771 bitmask = byte_get (ptr, 4);
20772 else
20773 bitmask = 0;
20774
20775 switch (type)
20776 {
20777 case GNU_PROPERTY_X86_ISA_1_USED:
20778 if (datasz != 4)
20779 printf (_("x86 ISA used: <corrupt length: %#x> "),
20780 datasz);
20781 else
20782 {
20783 printf ("x86 ISA used: ");
20784 decode_x86_isa (bitmask);
20785 }
20786 goto next;
20787
20788 case GNU_PROPERTY_X86_ISA_1_NEEDED:
20789 if (datasz != 4)
20790 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20791 datasz);
20792 else
20793 {
20794 printf ("x86 ISA needed: ");
20795 decode_x86_isa (bitmask);
20796 }
20797 goto next;
20798
20799 case GNU_PROPERTY_X86_FEATURE_1_AND:
20800 if (datasz != 4)
20801 printf (_("x86 feature: <corrupt length: %#x> "),
20802 datasz);
20803 else
20804 {
20805 printf ("x86 feature: ");
20806 decode_x86_feature_1 (bitmask);
20807 }
20808 goto next;
20809
20810 case GNU_PROPERTY_X86_FEATURE_2_USED:
20811 if (datasz != 4)
20812 printf (_("x86 feature used: <corrupt length: %#x> "),
20813 datasz);
20814 else
20815 {
20816 printf ("x86 feature used: ");
20817 decode_x86_feature_2 (bitmask);
20818 }
20819 goto next;
20820
20821 case GNU_PROPERTY_X86_FEATURE_2_NEEDED:
20822 if (datasz != 4)
20823 printf (_("x86 feature needed: <corrupt length: %#x> "), datasz);
20824 else
20825 {
20826 printf ("x86 feature needed: ");
20827 decode_x86_feature_2 (bitmask);
20828 }
20829 goto next;
20830
20831 case GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
20832 if (datasz != 4)
20833 printf (_("x86 ISA used: <corrupt length: %#x> "),
20834 datasz);
20835 else
20836 {
20837 printf ("x86 ISA used: ");
20838 decode_x86_compat_isa (bitmask);
20839 }
20840 goto next;
20841
20842 case GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
20843 if (datasz != 4)
20844 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20845 datasz);
20846 else
20847 {
20848 printf ("x86 ISA needed: ");
20849 decode_x86_compat_isa (bitmask);
20850 }
20851 goto next;
20852
20853 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
20854 if (datasz != 4)
20855 printf (_("x86 ISA used: <corrupt length: %#x> "),
20856 datasz);
20857 else
20858 {
20859 printf ("x86 ISA used: ");
20860 decode_x86_compat_2_isa (bitmask);
20861 }
20862 goto next;
20863
20864 case GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
20865 if (datasz != 4)
20866 printf (_("x86 ISA needed: <corrupt length: %#x> "),
20867 datasz);
20868 else
20869 {
20870 printf ("x86 ISA needed: ");
20871 decode_x86_compat_2_isa (bitmask);
20872 }
20873 goto next;
20874
20875 default:
20876 break;
20877 }
20878 }
20879 else if (filedata->file_header.e_machine == EM_AARCH64)
20880 {
20881 if (type == GNU_PROPERTY_AARCH64_FEATURE_1_AND)
20882 {
20883 printf ("AArch64 feature: ");
20884 if (datasz != 4)
20885 printf (_("<corrupt length: %#x> "), datasz);
20886 else
20887 decode_aarch64_feature_1_and (byte_get (ptr, 4));
20888 goto next;
20889 }
20890 }
20891 }
20892 else
20893 {
20894 switch (type)
20895 {
20896 case GNU_PROPERTY_STACK_SIZE:
20897 printf (_("stack size: "));
20898 if (datasz != size)
20899 printf (_("<corrupt length: %#x> "), datasz);
20900 else
20901 printf ("%#" PRIx64, byte_get (ptr, size));
20902 goto next;
20903
20904 case GNU_PROPERTY_NO_COPY_ON_PROTECTED:
20905 printf ("no copy on protected ");
20906 if (datasz)
20907 printf (_("<corrupt length: %#x> "), datasz);
20908 goto next;
20909
20910 default:
20911 if ((type >= GNU_PROPERTY_UINT32_AND_LO
20912 && type <= GNU_PROPERTY_UINT32_AND_HI)
20913 || (type >= GNU_PROPERTY_UINT32_OR_LO
20914 && type <= GNU_PROPERTY_UINT32_OR_HI))
20915 {
20916 switch (type)
20917 {
20918 case GNU_PROPERTY_1_NEEDED:
20919 if (datasz != 4)
20920 printf (_("1_needed: <corrupt length: %#x> "),
20921 datasz);
20922 else
20923 {
20924 unsigned int bitmask = byte_get (ptr, 4);
20925 printf ("1_needed: ");
20926 decode_1_needed (bitmask);
20927 }
20928 goto next;
20929
20930 default:
20931 break;
20932 }
20933 if (type <= GNU_PROPERTY_UINT32_AND_HI)
20934 printf (_("UINT32_AND (%#x): "), type);
20935 else
20936 printf (_("UINT32_OR (%#x): "), type);
20937 if (datasz != 4)
20938 printf (_("<corrupt length: %#x> "), datasz);
20939 else
20940 printf ("%#x", (unsigned int) byte_get (ptr, 4));
20941 goto next;
20942 }
20943 break;
20944 }
20945 }
20946
20947 if (type < GNU_PROPERTY_LOPROC)
20948 printf (_("<unknown type %#x data: "), type);
20949 else if (type < GNU_PROPERTY_LOUSER)
20950 printf (_("<processor-specific type %#x data: "), type);
20951 else
20952 printf (_("<application-specific type %#x data: "), type);
20953 for (j = 0; j < datasz; ++j)
20954 printf ("%02x ", ptr[j] & 0xff);
20955 printf (">");
20956
20957 next:
20958 ptr += ((datasz + (size - 1)) & ~ (size - 1));
20959 if (ptr == ptr_end)
20960 break;
20961
20962 if (do_wide)
20963 printf (", ");
20964 else
20965 printf ("\n\t");
20966 }
20967
20968 printf ("\n");
20969 }
20970
20971 static bool
20972 print_gnu_note (Filedata * filedata, Elf_Internal_Note *pnote)
20973 {
20974 /* NB/ Keep this switch statement in sync with get_gnu_elf_note_type (). */
20975 switch (pnote->type)
20976 {
20977 case NT_GNU_BUILD_ID:
20978 {
20979 size_t i;
20980
20981 printf (_(" Build ID: "));
20982 for (i = 0; i < pnote->descsz; ++i)
20983 printf ("%02x", pnote->descdata[i] & 0xff);
20984 printf ("\n");
20985 }
20986 break;
20987
20988 case NT_GNU_ABI_TAG:
20989 {
20990 unsigned int os, major, minor, subminor;
20991 const char *osname;
20992
20993 /* PR 17531: file: 030-599401-0.004. */
20994 if (pnote->descsz < 16)
20995 {
20996 printf (_(" <corrupt GNU_ABI_TAG>\n"));
20997 break;
20998 }
20999
21000 os = byte_get ((unsigned char *) pnote->descdata, 4);
21001 major = byte_get ((unsigned char *) pnote->descdata + 4, 4);
21002 minor = byte_get ((unsigned char *) pnote->descdata + 8, 4);
21003 subminor = byte_get ((unsigned char *) pnote->descdata + 12, 4);
21004
21005 switch (os)
21006 {
21007 case GNU_ABI_TAG_LINUX:
21008 osname = "Linux";
21009 break;
21010 case GNU_ABI_TAG_HURD:
21011 osname = "Hurd";
21012 break;
21013 case GNU_ABI_TAG_SOLARIS:
21014 osname = "Solaris";
21015 break;
21016 case GNU_ABI_TAG_FREEBSD:
21017 osname = "FreeBSD";
21018 break;
21019 case GNU_ABI_TAG_NETBSD:
21020 osname = "NetBSD";
21021 break;
21022 case GNU_ABI_TAG_SYLLABLE:
21023 osname = "Syllable";
21024 break;
21025 case GNU_ABI_TAG_NACL:
21026 osname = "NaCl";
21027 break;
21028 default:
21029 osname = "Unknown";
21030 break;
21031 }
21032
21033 printf (_(" OS: %s, ABI: %d.%d.%d\n"), osname,
21034 major, minor, subminor);
21035 }
21036 break;
21037
21038 case NT_GNU_GOLD_VERSION:
21039 {
21040 size_t i;
21041
21042 printf (_(" Version: "));
21043 for (i = 0; i < pnote->descsz && pnote->descdata[i] != '\0'; ++i)
21044 printf ("%c", pnote->descdata[i]);
21045 printf ("\n");
21046 }
21047 break;
21048
21049 case NT_GNU_HWCAP:
21050 {
21051 unsigned int num_entries, mask;
21052
21053 /* Hardware capabilities information. Word 0 is the number of entries.
21054 Word 1 is a bitmask of enabled entries. The rest of the descriptor
21055 is a series of entries, where each entry is a single byte followed
21056 by a nul terminated string. The byte gives the bit number to test
21057 if enabled in the bitmask. */
21058 printf (_(" Hardware Capabilities: "));
21059 if (pnote->descsz < 8)
21060 {
21061 error (_("<corrupt GNU_HWCAP>\n"));
21062 return false;
21063 }
21064 num_entries = byte_get ((unsigned char *) pnote->descdata, 4);
21065 mask = byte_get ((unsigned char *) pnote->descdata + 4, 4);
21066 printf (_("num entries: %d, enabled mask: %x\n"), num_entries, mask);
21067 /* FIXME: Add code to display the entries... */
21068 }
21069 break;
21070
21071 case NT_GNU_PROPERTY_TYPE_0:
21072 print_gnu_property_note (filedata, pnote);
21073 break;
21074
21075 default:
21076 /* Handle unrecognised types. An error message should have already been
21077 created by get_gnu_elf_note_type(), so all that we need to do is to
21078 display the data. */
21079 {
21080 size_t i;
21081
21082 printf (_(" Description data: "));
21083 for (i = 0; i < pnote->descsz; ++i)
21084 printf ("%02x ", pnote->descdata[i] & 0xff);
21085 printf ("\n");
21086 }
21087 break;
21088 }
21089
21090 return true;
21091 }
21092
21093 static const char *
21094 get_v850_elf_note_type (enum v850_notes n_type)
21095 {
21096 static char buff[64];
21097
21098 switch (n_type)
21099 {
21100 case V850_NOTE_ALIGNMENT: return _("Alignment of 8-byte objects");
21101 case V850_NOTE_DATA_SIZE: return _("Sizeof double and long double");
21102 case V850_NOTE_FPU_INFO: return _("Type of FPU support needed");
21103 case V850_NOTE_SIMD_INFO: return _("Use of SIMD instructions");
21104 case V850_NOTE_CACHE_INFO: return _("Use of cache");
21105 case V850_NOTE_MMU_INFO: return _("Use of MMU");
21106 default:
21107 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), n_type);
21108 return buff;
21109 }
21110 }
21111
21112 static bool
21113 print_v850_note (Elf_Internal_Note * pnote)
21114 {
21115 unsigned int val;
21116
21117 if (pnote->descsz != 4)
21118 return false;
21119
21120 val = byte_get ((unsigned char *) pnote->descdata, pnote->descsz);
21121
21122 if (val == 0)
21123 {
21124 printf (_("not set\n"));
21125 return true;
21126 }
21127
21128 switch (pnote->type)
21129 {
21130 case V850_NOTE_ALIGNMENT:
21131 switch (val)
21132 {
21133 case EF_RH850_DATA_ALIGN4: printf (_("4-byte\n")); return true;
21134 case EF_RH850_DATA_ALIGN8: printf (_("8-byte\n")); return true;
21135 }
21136 break;
21137
21138 case V850_NOTE_DATA_SIZE:
21139 switch (val)
21140 {
21141 case EF_RH850_DOUBLE32: printf (_("4-bytes\n")); return true;
21142 case EF_RH850_DOUBLE64: printf (_("8-bytes\n")); return true;
21143 }
21144 break;
21145
21146 case V850_NOTE_FPU_INFO:
21147 switch (val)
21148 {
21149 case EF_RH850_FPU20: printf (_("FPU-2.0\n")); return true;
21150 case EF_RH850_FPU30: printf (_("FPU-3.0\n")); return true;
21151 }
21152 break;
21153
21154 case V850_NOTE_MMU_INFO:
21155 case V850_NOTE_CACHE_INFO:
21156 case V850_NOTE_SIMD_INFO:
21157 if (val == EF_RH850_SIMD)
21158 {
21159 printf (_("yes\n"));
21160 return true;
21161 }
21162 break;
21163
21164 default:
21165 /* An 'unknown note type' message will already have been displayed. */
21166 break;
21167 }
21168
21169 printf (_("unknown value: %x\n"), val);
21170 return false;
21171 }
21172
21173 static bool
21174 process_netbsd_elf_note (Elf_Internal_Note * pnote)
21175 {
21176 unsigned int version;
21177
21178 switch (pnote->type)
21179 {
21180 case NT_NETBSD_IDENT:
21181 if (pnote->descsz < 1)
21182 break;
21183 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
21184 if ((version / 10000) % 100)
21185 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u%s%c)\n", pnote->descsz,
21186 version, version / 100000000, (version / 1000000) % 100,
21187 (version / 10000) % 100 > 26 ? "Z" : "",
21188 'A' + (version / 10000) % 26);
21189 else
21190 printf (" NetBSD\t\t0x%08lx\tIDENT %u (%u.%u.%u)\n", pnote->descsz,
21191 version, version / 100000000, (version / 1000000) % 100,
21192 (version / 100) % 100);
21193 return true;
21194
21195 case NT_NETBSD_MARCH:
21196 printf (" NetBSD\t\t0x%08lx\tMARCH <%s>\n", pnote->descsz,
21197 pnote->descdata);
21198 return true;
21199
21200 case NT_NETBSD_PAX:
21201 if (pnote->descsz < 1)
21202 break;
21203 version = byte_get ((unsigned char *) pnote->descdata, sizeof (version));
21204 printf (" NetBSD\t\t0x%08lx\tPaX <%s%s%s%s%s%s>\n", pnote->descsz,
21205 ((version & NT_NETBSD_PAX_MPROTECT) ? "+mprotect" : ""),
21206 ((version & NT_NETBSD_PAX_NOMPROTECT) ? "-mprotect" : ""),
21207 ((version & NT_NETBSD_PAX_GUARD) ? "+guard" : ""),
21208 ((version & NT_NETBSD_PAX_NOGUARD) ? "-guard" : ""),
21209 ((version & NT_NETBSD_PAX_ASLR) ? "+ASLR" : ""),
21210 ((version & NT_NETBSD_PAX_NOASLR) ? "-ASLR" : ""));
21211 return true;
21212 }
21213
21214 printf (" NetBSD\t0x%08lx\tUnknown note type: (0x%08lx)\n",
21215 pnote->descsz, pnote->type);
21216 return false;
21217 }
21218
21219 static const char *
21220 get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
21221 {
21222 switch (e_type)
21223 {
21224 case NT_FREEBSD_THRMISC:
21225 return _("NT_THRMISC (thrmisc structure)");
21226 case NT_FREEBSD_PROCSTAT_PROC:
21227 return _("NT_PROCSTAT_PROC (proc data)");
21228 case NT_FREEBSD_PROCSTAT_FILES:
21229 return _("NT_PROCSTAT_FILES (files data)");
21230 case NT_FREEBSD_PROCSTAT_VMMAP:
21231 return _("NT_PROCSTAT_VMMAP (vmmap data)");
21232 case NT_FREEBSD_PROCSTAT_GROUPS:
21233 return _("NT_PROCSTAT_GROUPS (groups data)");
21234 case NT_FREEBSD_PROCSTAT_UMASK:
21235 return _("NT_PROCSTAT_UMASK (umask data)");
21236 case NT_FREEBSD_PROCSTAT_RLIMIT:
21237 return _("NT_PROCSTAT_RLIMIT (rlimit data)");
21238 case NT_FREEBSD_PROCSTAT_OSREL:
21239 return _("NT_PROCSTAT_OSREL (osreldate data)");
21240 case NT_FREEBSD_PROCSTAT_PSSTRINGS:
21241 return _("NT_PROCSTAT_PSSTRINGS (ps_strings data)");
21242 case NT_FREEBSD_PROCSTAT_AUXV:
21243 return _("NT_PROCSTAT_AUXV (auxv data)");
21244 case NT_FREEBSD_PTLWPINFO:
21245 return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
21246 case NT_FREEBSD_X86_SEGBASES:
21247 return _("NT_X86_SEGBASES (x86 segment base registers)");
21248 }
21249 return get_note_type (filedata, e_type);
21250 }
21251
21252 static const char *
21253 get_netbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
21254 {
21255 static char buff[64];
21256
21257 switch (e_type)
21258 {
21259 case NT_NETBSDCORE_PROCINFO:
21260 /* NetBSD core "procinfo" structure. */
21261 return _("NetBSD procinfo structure");
21262
21263 case NT_NETBSDCORE_AUXV:
21264 return _("NetBSD ELF auxiliary vector data");
21265
21266 case NT_NETBSDCORE_LWPSTATUS:
21267 return _("PT_LWPSTATUS (ptrace_lwpstatus structure)");
21268
21269 default:
21270 /* As of Jan 2020 there are no other machine-independent notes
21271 defined for NetBSD core files. If the note type is less
21272 than the start of the machine-dependent note types, we don't
21273 understand it. */
21274
21275 if (e_type < NT_NETBSDCORE_FIRSTMACH)
21276 {
21277 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
21278 return buff;
21279 }
21280 break;
21281 }
21282
21283 switch (filedata->file_header.e_machine)
21284 {
21285 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
21286 and PT_GETFPREGS == mach+2. */
21287
21288 case EM_OLD_ALPHA:
21289 case EM_ALPHA:
21290 case EM_SPARC:
21291 case EM_SPARC32PLUS:
21292 case EM_SPARCV9:
21293 switch (e_type)
21294 {
21295 case NT_NETBSDCORE_FIRSTMACH + 0:
21296 return _("PT_GETREGS (reg structure)");
21297 case NT_NETBSDCORE_FIRSTMACH + 2:
21298 return _("PT_GETFPREGS (fpreg structure)");
21299 default:
21300 break;
21301 }
21302 break;
21303
21304 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5.
21305 There's also old PT___GETREGS40 == mach + 1 for old reg
21306 structure which lacks GBR. */
21307 case EM_SH:
21308 switch (e_type)
21309 {
21310 case NT_NETBSDCORE_FIRSTMACH + 1:
21311 return _("PT___GETREGS40 (old reg structure)");
21312 case NT_NETBSDCORE_FIRSTMACH + 3:
21313 return _("PT_GETREGS (reg structure)");
21314 case NT_NETBSDCORE_FIRSTMACH + 5:
21315 return _("PT_GETFPREGS (fpreg structure)");
21316 default:
21317 break;
21318 }
21319 break;
21320
21321 /* On all other arch's, PT_GETREGS == mach+1 and
21322 PT_GETFPREGS == mach+3. */
21323 default:
21324 switch (e_type)
21325 {
21326 case NT_NETBSDCORE_FIRSTMACH + 1:
21327 return _("PT_GETREGS (reg structure)");
21328 case NT_NETBSDCORE_FIRSTMACH + 3:
21329 return _("PT_GETFPREGS (fpreg structure)");
21330 default:
21331 break;
21332 }
21333 }
21334
21335 snprintf (buff, sizeof (buff), "PT_FIRSTMACH+%d",
21336 e_type - NT_NETBSDCORE_FIRSTMACH);
21337 return buff;
21338 }
21339
21340 static const char *
21341 get_openbsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
21342 {
21343 switch (e_type)
21344 {
21345 case NT_OPENBSD_PROCINFO:
21346 return _("OpenBSD procinfo structure");
21347 case NT_OPENBSD_AUXV:
21348 return _("OpenBSD ELF auxiliary vector data");
21349 case NT_OPENBSD_REGS:
21350 return _("OpenBSD regular registers");
21351 case NT_OPENBSD_FPREGS:
21352 return _("OpenBSD floating point registers");
21353 case NT_OPENBSD_WCOOKIE:
21354 return _("OpenBSD window cookie");
21355 }
21356
21357 return get_note_type (filedata, e_type);
21358 }
21359
21360 static const char *
21361 get_qnx_elfcore_note_type (Filedata * filedata, unsigned e_type)
21362 {
21363 switch (e_type)
21364 {
21365 case QNT_DEBUG_FULLPATH:
21366 return _("QNX debug fullpath");
21367 case QNT_DEBUG_RELOC:
21368 return _("QNX debug relocation");
21369 case QNT_STACK:
21370 return _("QNX stack");
21371 case QNT_GENERATOR:
21372 return _("QNX generator");
21373 case QNT_DEFAULT_LIB:
21374 return _("QNX default library");
21375 case QNT_CORE_SYSINFO:
21376 return _("QNX core sysinfo");
21377 case QNT_CORE_INFO:
21378 return _("QNX core info");
21379 case QNT_CORE_STATUS:
21380 return _("QNX core status");
21381 case QNT_CORE_GREG:
21382 return _("QNX general registers");
21383 case QNT_CORE_FPREG:
21384 return _("QNX floating point registers");
21385 case QNT_LINK_MAP:
21386 return _("QNX link map");
21387 }
21388
21389 return get_note_type (filedata, e_type);
21390 }
21391
21392 static const char *
21393 get_stapsdt_note_type (unsigned e_type)
21394 {
21395 static char buff[64];
21396
21397 switch (e_type)
21398 {
21399 case NT_STAPSDT:
21400 return _("NT_STAPSDT (SystemTap probe descriptors)");
21401
21402 default:
21403 break;
21404 }
21405
21406 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
21407 return buff;
21408 }
21409
21410 static bool
21411 print_stapsdt_note (Elf_Internal_Note *pnote)
21412 {
21413 size_t len, maxlen;
21414 size_t addr_size = is_32bit_elf ? 4 : 8;
21415 char *data = pnote->descdata;
21416 char *data_end = pnote->descdata + pnote->descsz;
21417 uint64_t pc, base_addr, semaphore;
21418 char *provider, *probe, *arg_fmt;
21419
21420 if (pnote->descsz < (addr_size * 3))
21421 goto stapdt_note_too_small;
21422
21423 pc = byte_get ((unsigned char *) data, addr_size);
21424 data += addr_size;
21425
21426 base_addr = byte_get ((unsigned char *) data, addr_size);
21427 data += addr_size;
21428
21429 semaphore = byte_get ((unsigned char *) data, addr_size);
21430 data += addr_size;
21431
21432 if (data >= data_end)
21433 goto stapdt_note_too_small;
21434 maxlen = data_end - data;
21435 len = strnlen (data, maxlen);
21436 if (len < maxlen)
21437 {
21438 provider = data;
21439 data += len + 1;
21440 }
21441 else
21442 goto stapdt_note_too_small;
21443
21444 if (data >= data_end)
21445 goto stapdt_note_too_small;
21446 maxlen = data_end - data;
21447 len = strnlen (data, maxlen);
21448 if (len < maxlen)
21449 {
21450 probe = data;
21451 data += len + 1;
21452 }
21453 else
21454 goto stapdt_note_too_small;
21455
21456 if (data >= data_end)
21457 goto stapdt_note_too_small;
21458 maxlen = data_end - data;
21459 len = strnlen (data, maxlen);
21460 if (len < maxlen)
21461 {
21462 arg_fmt = data;
21463 data += len + 1;
21464 }
21465 else
21466 goto stapdt_note_too_small;
21467
21468 printf (_(" Provider: %s\n"), provider);
21469 printf (_(" Name: %s\n"), probe);
21470 printf (_(" Location: "));
21471 print_vma (pc, FULL_HEX);
21472 printf (_(", Base: "));
21473 print_vma (base_addr, FULL_HEX);
21474 printf (_(", Semaphore: "));
21475 print_vma (semaphore, FULL_HEX);
21476 printf ("\n");
21477 printf (_(" Arguments: %s\n"), arg_fmt);
21478
21479 return data == data_end;
21480
21481 stapdt_note_too_small:
21482 printf (_(" <corrupt - note is too small>\n"));
21483 error (_("corrupt stapdt note - the data size is too small\n"));
21484 return false;
21485 }
21486
21487 static bool
21488 print_fdo_note (Elf_Internal_Note * pnote)
21489 {
21490 if (pnote->descsz > 0 && pnote->type == FDO_PACKAGING_METADATA)
21491 {
21492 printf (_(" Packaging Metadata: %.*s\n"), (int) pnote->descsz, pnote->descdata);
21493 return true;
21494 }
21495 return false;
21496 }
21497
21498 static const char *
21499 get_ia64_vms_note_type (unsigned e_type)
21500 {
21501 static char buff[64];
21502
21503 switch (e_type)
21504 {
21505 case NT_VMS_MHD:
21506 return _("NT_VMS_MHD (module header)");
21507 case NT_VMS_LNM:
21508 return _("NT_VMS_LNM (language name)");
21509 case NT_VMS_SRC:
21510 return _("NT_VMS_SRC (source files)");
21511 case NT_VMS_TITLE:
21512 return "NT_VMS_TITLE";
21513 case NT_VMS_EIDC:
21514 return _("NT_VMS_EIDC (consistency check)");
21515 case NT_VMS_FPMODE:
21516 return _("NT_VMS_FPMODE (FP mode)");
21517 case NT_VMS_LINKTIME:
21518 return "NT_VMS_LINKTIME";
21519 case NT_VMS_IMGNAM:
21520 return _("NT_VMS_IMGNAM (image name)");
21521 case NT_VMS_IMGID:
21522 return _("NT_VMS_IMGID (image id)");
21523 case NT_VMS_LINKID:
21524 return _("NT_VMS_LINKID (link id)");
21525 case NT_VMS_IMGBID:
21526 return _("NT_VMS_IMGBID (build id)");
21527 case NT_VMS_GSTNAM:
21528 return _("NT_VMS_GSTNAM (sym table name)");
21529 case NT_VMS_ORIG_DYN:
21530 return "NT_VMS_ORIG_DYN";
21531 case NT_VMS_PATCHTIME:
21532 return "NT_VMS_PATCHTIME";
21533 default:
21534 snprintf (buff, sizeof (buff), _("Unknown note type: (0x%08x)"), e_type);
21535 return buff;
21536 }
21537 }
21538
21539 static bool
21540 print_ia64_vms_note (Elf_Internal_Note * pnote)
21541 {
21542 unsigned int maxlen = pnote->descsz;
21543
21544 if (maxlen < 2 || maxlen != pnote->descsz)
21545 goto desc_size_fail;
21546
21547 switch (pnote->type)
21548 {
21549 case NT_VMS_MHD:
21550 if (maxlen <= 36)
21551 goto desc_size_fail;
21552
21553 size_t l = strnlen (pnote->descdata + 34, maxlen - 34);
21554
21555 printf (_(" Creation date : %.17s\n"), pnote->descdata);
21556 printf (_(" Last patch date: %.17s\n"), pnote->descdata + 17);
21557 if (l + 34 < maxlen)
21558 {
21559 printf (_(" Module name : %s\n"), pnote->descdata + 34);
21560 if (l + 35 < maxlen)
21561 printf (_(" Module version : %s\n"), pnote->descdata + 34 + l + 1);
21562 else
21563 printf (_(" Module version : <missing>\n"));
21564 }
21565 else
21566 {
21567 printf (_(" Module name : <missing>\n"));
21568 printf (_(" Module version : <missing>\n"));
21569 }
21570 break;
21571
21572 case NT_VMS_LNM:
21573 printf (_(" Language: %.*s\n"), maxlen, pnote->descdata);
21574 break;
21575
21576 case NT_VMS_FPMODE:
21577 printf (_(" Floating Point mode: "));
21578 if (maxlen < 8)
21579 goto desc_size_fail;
21580 /* FIXME: Generate an error if descsz > 8 ? */
21581
21582 printf ("0x%016" PRIx64 "\n",
21583 byte_get ((unsigned char *) pnote->descdata, 8));
21584 break;
21585
21586 case NT_VMS_LINKTIME:
21587 printf (_(" Link time: "));
21588 if (maxlen < 8)
21589 goto desc_size_fail;
21590 /* FIXME: Generate an error if descsz > 8 ? */
21591
21592 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
21593 printf ("\n");
21594 break;
21595
21596 case NT_VMS_PATCHTIME:
21597 printf (_(" Patch time: "));
21598 if (maxlen < 8)
21599 goto desc_size_fail;
21600 /* FIXME: Generate an error if descsz > 8 ? */
21601
21602 print_vms_time (byte_get ((unsigned char *) pnote->descdata, 8));
21603 printf ("\n");
21604 break;
21605
21606 case NT_VMS_ORIG_DYN:
21607 if (maxlen < 34)
21608 goto desc_size_fail;
21609
21610 printf (_(" Major id: %u, minor id: %u\n"),
21611 (unsigned) byte_get ((unsigned char *) pnote->descdata, 4),
21612 (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
21613 printf (_(" Last modified : "));
21614 print_vms_time (byte_get ((unsigned char *) pnote->descdata + 8, 8));
21615 printf (_("\n Link flags : "));
21616 printf ("0x%016" PRIx64 "\n",
21617 byte_get ((unsigned char *) pnote->descdata + 16, 8));
21618 printf (_(" Header flags: 0x%08x\n"),
21619 (unsigned) byte_get ((unsigned char *) pnote->descdata + 24, 4));
21620 printf (_(" Image id : %.*s\n"), maxlen - 32, pnote->descdata + 32);
21621 break;
21622
21623 case NT_VMS_IMGNAM:
21624 printf (_(" Image name: %.*s\n"), maxlen, pnote->descdata);
21625 break;
21626
21627 case NT_VMS_GSTNAM:
21628 printf (_(" Global symbol table name: %.*s\n"), maxlen, pnote->descdata);
21629 break;
21630
21631 case NT_VMS_IMGID:
21632 printf (_(" Image id: %.*s\n"), maxlen, pnote->descdata);
21633 break;
21634
21635 case NT_VMS_LINKID:
21636 printf (_(" Linker id: %.*s\n"), maxlen, pnote->descdata);
21637 break;
21638
21639 default:
21640 return false;
21641 }
21642
21643 return true;
21644
21645 desc_size_fail:
21646 printf (_(" <corrupt - data size is too small>\n"));
21647 error (_("corrupt IA64 note: data size is too small\n"));
21648 return false;
21649 }
21650
21651 struct build_attr_cache {
21652 Filedata *filedata;
21653 char *strtab;
21654 uint64_t strtablen;
21655 Elf_Internal_Sym *symtab;
21656 uint64_t nsyms;
21657 } ba_cache;
21658
21659 /* Find the symbol associated with a build attribute that is attached
21660 to address OFFSET. If PNAME is non-NULL then store the name of
21661 the symbol (if found) in the provided pointer, Returns NULL if a
21662 symbol could not be found. */
21663
21664 static Elf_Internal_Sym *
21665 get_symbol_for_build_attribute (Filedata *filedata,
21666 uint64_t offset,
21667 bool is_open_attr,
21668 const char **pname)
21669 {
21670 Elf_Internal_Sym *saved_sym = NULL;
21671 Elf_Internal_Sym *sym;
21672
21673 if (filedata->section_headers != NULL
21674 && (ba_cache.filedata == NULL || filedata != ba_cache.filedata))
21675 {
21676 Elf_Internal_Shdr * symsec;
21677
21678 free (ba_cache.strtab);
21679 ba_cache.strtab = NULL;
21680 free (ba_cache.symtab);
21681 ba_cache.symtab = NULL;
21682
21683 /* Load the symbol and string sections. */
21684 for (symsec = filedata->section_headers;
21685 symsec < filedata->section_headers + filedata->file_header.e_shnum;
21686 symsec ++)
21687 {
21688 if (symsec->sh_type == SHT_SYMTAB
21689 && get_symtab (filedata, symsec,
21690 &ba_cache.symtab, &ba_cache.nsyms,
21691 &ba_cache.strtab, &ba_cache.strtablen))
21692 break;
21693 }
21694 ba_cache.filedata = filedata;
21695 }
21696
21697 if (ba_cache.symtab == NULL)
21698 return NULL;
21699
21700 /* Find a symbol whose value matches offset. */
21701 for (sym = ba_cache.symtab; sym < ba_cache.symtab + ba_cache.nsyms; sym ++)
21702 if (sym->st_value == offset)
21703 {
21704 if (sym->st_name >= ba_cache.strtablen)
21705 /* Huh ? This should not happen. */
21706 continue;
21707
21708 if (ba_cache.strtab[sym->st_name] == 0)
21709 continue;
21710
21711 /* The AArch64, ARM and RISC-V architectures define mapping symbols
21712 (eg $d, $x, $t) which we want to ignore. */
21713 if (ba_cache.strtab[sym->st_name] == '$'
21714 && ba_cache.strtab[sym->st_name + 1] != 0
21715 && ba_cache.strtab[sym->st_name + 2] == 0)
21716 continue;
21717
21718 if (is_open_attr)
21719 {
21720 /* For OPEN attributes we prefer GLOBAL over LOCAL symbols
21721 and FILE or OBJECT symbols over NOTYPE symbols. We skip
21722 FUNC symbols entirely. */
21723 switch (ELF_ST_TYPE (sym->st_info))
21724 {
21725 case STT_OBJECT:
21726 case STT_FILE:
21727 saved_sym = sym;
21728 if (sym->st_size)
21729 {
21730 /* If the symbol has a size associated
21731 with it then we can stop searching. */
21732 sym = ba_cache.symtab + ba_cache.nsyms;
21733 }
21734 continue;
21735
21736 case STT_FUNC:
21737 /* Ignore function symbols. */
21738 continue;
21739
21740 default:
21741 break;
21742 }
21743
21744 switch (ELF_ST_BIND (sym->st_info))
21745 {
21746 case STB_GLOBAL:
21747 if (saved_sym == NULL
21748 || ELF_ST_TYPE (saved_sym->st_info) != STT_OBJECT)
21749 saved_sym = sym;
21750 break;
21751
21752 case STB_LOCAL:
21753 if (saved_sym == NULL)
21754 saved_sym = sym;
21755 break;
21756
21757 default:
21758 break;
21759 }
21760 }
21761 else
21762 {
21763 if (ELF_ST_TYPE (sym->st_info) != STT_FUNC)
21764 continue;
21765
21766 saved_sym = sym;
21767 break;
21768 }
21769 }
21770
21771 if (saved_sym && pname)
21772 * pname = ba_cache.strtab + saved_sym->st_name;
21773
21774 return saved_sym;
21775 }
21776
21777 /* Returns true iff addr1 and addr2 are in the same section. */
21778
21779 static bool
21780 same_section (Filedata * filedata, uint64_t addr1, uint64_t addr2)
21781 {
21782 Elf_Internal_Shdr * a1;
21783 Elf_Internal_Shdr * a2;
21784
21785 a1 = find_section_by_address (filedata, addr1);
21786 a2 = find_section_by_address (filedata, addr2);
21787
21788 return a1 == a2 && a1 != NULL;
21789 }
21790
21791 static bool
21792 print_gnu_build_attribute_description (Elf_Internal_Note * pnote,
21793 Filedata * filedata)
21794 {
21795 static uint64_t global_offset = 0;
21796 static uint64_t global_end = 0;
21797 static uint64_t func_offset = 0;
21798 static uint64_t func_end = 0;
21799
21800 Elf_Internal_Sym *sym;
21801 const char *name;
21802 uint64_t start;
21803 uint64_t end;
21804 bool is_open_attr = pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN;
21805
21806 switch (pnote->descsz)
21807 {
21808 case 0:
21809 /* A zero-length description means that the range of
21810 the previous note of the same type should be used. */
21811 if (is_open_attr)
21812 {
21813 if (global_end > global_offset)
21814 printf (_(" Applies to region from %#" PRIx64
21815 " to %#" PRIx64 "\n"), global_offset, global_end);
21816 else
21817 printf (_(" Applies to region from %#" PRIx64
21818 "\n"), global_offset);
21819 }
21820 else
21821 {
21822 if (func_end > func_offset)
21823 printf (_(" Applies to region from %#" PRIx64
21824 " to %#" PRIx64 "\n"), func_offset, func_end);
21825 else
21826 printf (_(" Applies to region from %#" PRIx64
21827 "\n"), func_offset);
21828 }
21829 return true;
21830
21831 case 4:
21832 start = byte_get ((unsigned char *) pnote->descdata, 4);
21833 end = 0;
21834 break;
21835
21836 case 8:
21837 start = byte_get ((unsigned char *) pnote->descdata, 4);
21838 end = byte_get ((unsigned char *) pnote->descdata + 4, 4);
21839 break;
21840
21841 case 16:
21842 start = byte_get ((unsigned char *) pnote->descdata, 8);
21843 end = byte_get ((unsigned char *) pnote->descdata + 8, 8);
21844 break;
21845
21846 default:
21847 error (_(" <invalid description size: %lx>\n"), pnote->descsz);
21848 printf (_(" <invalid descsz>"));
21849 return false;
21850 }
21851
21852 name = NULL;
21853 sym = get_symbol_for_build_attribute (filedata, start, is_open_attr, & name);
21854 /* As of version 5 of the annobin plugin, filename symbols are biased by 2
21855 in order to avoid them being confused with the start address of the
21856 first function in the file... */
21857 if (sym == NULL && is_open_attr)
21858 sym = get_symbol_for_build_attribute (filedata, start + 2, is_open_attr,
21859 & name);
21860
21861 if (end == 0 && sym != NULL && sym->st_size > 0)
21862 end = start + sym->st_size;
21863
21864 if (is_open_attr)
21865 {
21866 /* FIXME: Need to properly allow for section alignment.
21867 16 is just the alignment used on x86_64. */
21868 if (global_end > 0
21869 && start > BFD_ALIGN (global_end, 16)
21870 /* Build notes are not guaranteed to be organised in order of
21871 increasing address, but we should find the all of the notes
21872 for one section in the same place. */
21873 && same_section (filedata, start, global_end))
21874 warn (_("Gap in build notes detected from %#" PRIx64
21875 " to %#" PRIx64 "\n"),
21876 global_end + 1, start - 1);
21877
21878 printf (_(" Applies to region from %#" PRIx64), start);
21879 global_offset = start;
21880
21881 if (end)
21882 {
21883 printf (_(" to %#" PRIx64), end);
21884 global_end = end;
21885 }
21886 }
21887 else
21888 {
21889 printf (_(" Applies to region from %#" PRIx64), start);
21890 func_offset = start;
21891
21892 if (end)
21893 {
21894 printf (_(" to %#" PRIx64), end);
21895 func_end = end;
21896 }
21897 }
21898
21899 if (sym && name)
21900 printf (_(" (%s)"), name);
21901
21902 printf ("\n");
21903 return true;
21904 }
21905
21906 static bool
21907 print_gnu_build_attribute_name (Elf_Internal_Note * pnote)
21908 {
21909 static const char string_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_STRING, 0 };
21910 static const char number_expected [2] = { GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC, 0 };
21911 static const char bool_expected [3] = { GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE, GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE, 0 };
21912 char name_type;
21913 char name_attribute;
21914 const char * expected_types;
21915 const char * name = pnote->namedata;
21916 const char * text;
21917 signed int left;
21918
21919 if (name == NULL || pnote->namesz < 2)
21920 {
21921 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21922 print_symbol_name (-20, _(" <corrupt name>"));
21923 return false;
21924 }
21925
21926 if (do_wide)
21927 left = 28;
21928 else
21929 left = 20;
21930
21931 /* Version 2 of the spec adds a "GA" prefix to the name field. */
21932 if (name[0] == 'G' && name[1] == 'A')
21933 {
21934 if (pnote->namesz < 4)
21935 {
21936 error (_("corrupt name field in GNU build attribute note: size = %ld\n"), pnote->namesz);
21937 print_symbol_name (-20, _(" <corrupt name>"));
21938 return false;
21939 }
21940
21941 printf ("GA");
21942 name += 2;
21943 left -= 2;
21944 }
21945
21946 switch ((name_type = * name))
21947 {
21948 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
21949 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
21950 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
21951 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
21952 printf ("%c", * name);
21953 left --;
21954 break;
21955 default:
21956 error (_("unrecognised attribute type in name field: %d\n"), name_type);
21957 print_symbol_name (-20, _("<unknown name type>"));
21958 return false;
21959 }
21960
21961 ++ name;
21962 text = NULL;
21963
21964 switch ((name_attribute = * name))
21965 {
21966 case GNU_BUILD_ATTRIBUTE_VERSION:
21967 text = _("<version>");
21968 expected_types = string_expected;
21969 ++ name;
21970 break;
21971 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
21972 text = _("<stack prot>");
21973 expected_types = "!+*";
21974 ++ name;
21975 break;
21976 case GNU_BUILD_ATTRIBUTE_RELRO:
21977 text = _("<relro>");
21978 expected_types = bool_expected;
21979 ++ name;
21980 break;
21981 case GNU_BUILD_ATTRIBUTE_STACK_SIZE:
21982 text = _("<stack size>");
21983 expected_types = number_expected;
21984 ++ name;
21985 break;
21986 case GNU_BUILD_ATTRIBUTE_TOOL:
21987 text = _("<tool>");
21988 expected_types = string_expected;
21989 ++ name;
21990 break;
21991 case GNU_BUILD_ATTRIBUTE_ABI:
21992 text = _("<ABI>");
21993 expected_types = "$*";
21994 ++ name;
21995 break;
21996 case GNU_BUILD_ATTRIBUTE_PIC:
21997 text = _("<PIC>");
21998 expected_types = number_expected;
21999 ++ name;
22000 break;
22001 case GNU_BUILD_ATTRIBUTE_SHORT_ENUM:
22002 text = _("<short enum>");
22003 expected_types = bool_expected;
22004 ++ name;
22005 break;
22006 default:
22007 if (ISPRINT (* name))
22008 {
22009 int len = strnlen (name, pnote->namesz - (name - pnote->namedata)) + 1;
22010
22011 if (len > left && ! do_wide)
22012 len = left;
22013 printf ("%.*s:", len, name);
22014 left -= len;
22015 name += len;
22016 }
22017 else
22018 {
22019 static char tmpbuf [128];
22020
22021 error (_("unrecognised byte in name field: %d\n"), * name);
22022 sprintf (tmpbuf, _("<unknown:_%d>"), * name);
22023 text = tmpbuf;
22024 name ++;
22025 }
22026 expected_types = "*$!+";
22027 break;
22028 }
22029
22030 if (text)
22031 left -= printf ("%s", text);
22032
22033 if (strchr (expected_types, name_type) == NULL)
22034 warn (_("attribute does not have an expected type (%c)\n"), name_type);
22035
22036 if ((size_t) (name - pnote->namedata) > pnote->namesz)
22037 {
22038 error (_("corrupt name field: namesz: %lu but parsing gets to %td\n"),
22039 pnote->namesz,
22040 name - pnote->namedata);
22041 return false;
22042 }
22043
22044 if (left < 1 && ! do_wide)
22045 return true;
22046
22047 switch (name_type)
22048 {
22049 case GNU_BUILD_ATTRIBUTE_TYPE_NUMERIC:
22050 {
22051 unsigned int bytes;
22052 uint64_t val = 0;
22053 unsigned int shift = 0;
22054 char *decoded = NULL;
22055
22056 bytes = pnote->namesz - (name - pnote->namedata);
22057 if (bytes > 0)
22058 /* The -1 is because the name field is always 0 terminated, and we
22059 want to be able to ensure that the shift in the while loop below
22060 will not overflow. */
22061 -- bytes;
22062
22063 if (bytes > sizeof (val))
22064 {
22065 error (_("corrupt numeric name field: too many bytes in the value: %x\n"),
22066 bytes);
22067 bytes = sizeof (val);
22068 }
22069 /* We do not bother to warn if bytes == 0 as this can
22070 happen with some early versions of the gcc plugin. */
22071
22072 while (bytes --)
22073 {
22074 uint64_t byte = *name++ & 0xff;
22075
22076 val |= byte << shift;
22077 shift += 8;
22078 }
22079
22080 switch (name_attribute)
22081 {
22082 case GNU_BUILD_ATTRIBUTE_PIC:
22083 switch (val)
22084 {
22085 case 0: decoded = "static"; break;
22086 case 1: decoded = "pic"; break;
22087 case 2: decoded = "PIC"; break;
22088 case 3: decoded = "pie"; break;
22089 case 4: decoded = "PIE"; break;
22090 default: break;
22091 }
22092 break;
22093 case GNU_BUILD_ATTRIBUTE_STACK_PROT:
22094 switch (val)
22095 {
22096 /* Based upon the SPCT_FLAG_xxx enum values in gcc/cfgexpand.c. */
22097 case 0: decoded = "off"; break;
22098 case 1: decoded = "on"; break;
22099 case 2: decoded = "all"; break;
22100 case 3: decoded = "strong"; break;
22101 case 4: decoded = "explicit"; break;
22102 default: break;
22103 }
22104 break;
22105 default:
22106 break;
22107 }
22108
22109 if (decoded != NULL)
22110 {
22111 print_symbol_name (-left, decoded);
22112 left = 0;
22113 }
22114 else if (val == 0)
22115 {
22116 printf ("0x0");
22117 left -= 3;
22118 }
22119 else
22120 {
22121 if (do_wide)
22122 left -= printf ("0x%" PRIx64, val);
22123 else
22124 left -= printf ("0x%-.*" PRIx64, left, val);
22125 }
22126 }
22127 break;
22128 case GNU_BUILD_ATTRIBUTE_TYPE_STRING:
22129 left -= print_symbol_name (- left, name);
22130 break;
22131 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_TRUE:
22132 left -= print_symbol_name (- left, "true");
22133 break;
22134 case GNU_BUILD_ATTRIBUTE_TYPE_BOOL_FALSE:
22135 left -= print_symbol_name (- left, "false");
22136 break;
22137 }
22138
22139 if (do_wide && left > 0)
22140 printf ("%-*s", left, " ");
22141
22142 return true;
22143 }
22144
22145 /* Print the contents of PNOTE as hex. */
22146
22147 static void
22148 print_note_contents_hex (Elf_Internal_Note *pnote)
22149 {
22150 if (pnote->descsz)
22151 {
22152 size_t i;
22153
22154 printf (_(" description data: "));
22155 for (i = 0; i < pnote->descsz; i++)
22156 printf ("%02x ", pnote->descdata[i] & 0xff);
22157 if (!do_wide)
22158 printf ("\n");
22159 }
22160
22161 if (do_wide)
22162 printf ("\n");
22163 }
22164
22165 #if defined HAVE_MSGPACK
22166
22167 static void
22168 print_indents (int n)
22169 {
22170 printf (" ");
22171
22172 for (int i = 0; i < n; i++)
22173 printf (" ");
22174 }
22175
22176 /* Print OBJ in human-readable form. */
22177
22178 static void
22179 dump_msgpack_obj (const msgpack_object *obj, int indent)
22180 {
22181 switch (obj->type)
22182 {
22183 case MSGPACK_OBJECT_NIL:
22184 printf ("(nil)");
22185 break;
22186
22187 case MSGPACK_OBJECT_BOOLEAN:
22188 printf ("%s", obj->via.boolean ? "true" : "false");
22189 break;
22190
22191 case MSGPACK_OBJECT_POSITIVE_INTEGER:
22192 printf ("%" PRIu64, obj->via.u64);
22193 break;
22194
22195 case MSGPACK_OBJECT_NEGATIVE_INTEGER:
22196 printf ("%" PRIi64, obj->via.i64);
22197 break;
22198
22199 case MSGPACK_OBJECT_FLOAT32:
22200 case MSGPACK_OBJECT_FLOAT64:
22201 printf ("%f", obj->via.f64);
22202 break;
22203
22204 case MSGPACK_OBJECT_STR:
22205 printf ("\"%.*s\"", obj->via.str.size, obj->via.str.ptr);
22206 break;
22207
22208 case MSGPACK_OBJECT_ARRAY:
22209 {
22210 const msgpack_object_array *array = &obj->via.array;
22211
22212 printf ("[\n");
22213 ++indent;
22214
22215 for (uint32_t i = 0; i < array->size; ++i)
22216 {
22217 const msgpack_object *item = &array->ptr[i];
22218
22219 print_indents (indent);
22220 dump_msgpack_obj (item, indent);
22221 printf (",\n");
22222 }
22223
22224 --indent;
22225 print_indents (indent);
22226 printf ("]");
22227 break;
22228 }
22229 break;
22230
22231 case MSGPACK_OBJECT_MAP:
22232 {
22233 const msgpack_object_map *map = &obj->via.map;
22234
22235 printf ("{\n");
22236 ++indent;
22237
22238 for (uint32_t i = 0; i < map->size; ++i)
22239 {
22240 const msgpack_object_kv *kv = &map->ptr[i];
22241 const msgpack_object *key = &kv->key;
22242 const msgpack_object *val = &kv->val;
22243
22244 print_indents (indent);
22245 dump_msgpack_obj (key, indent);
22246 printf (": ");
22247 dump_msgpack_obj (val, indent);
22248
22249 printf (",\n");
22250 }
22251
22252 --indent;
22253 print_indents (indent);
22254 printf ("}");
22255
22256 break;
22257 }
22258
22259 case MSGPACK_OBJECT_BIN:
22260 printf ("(bin)");
22261 break;
22262
22263 case MSGPACK_OBJECT_EXT:
22264 printf ("(ext)");
22265 break;
22266 }
22267 }
22268
22269 static void
22270 dump_msgpack (const msgpack_unpacked *msg)
22271 {
22272 print_indents (0);
22273 dump_msgpack_obj (&msg->data, 0);
22274 printf ("\n");
22275 }
22276
22277 #endif /* defined HAVE_MSGPACK */
22278
22279 static bool
22280 print_amdgpu_note (Elf_Internal_Note *pnote)
22281 {
22282 #if defined HAVE_MSGPACK
22283 /* If msgpack is available, decode and dump the note's content. */
22284 bool ret;
22285 msgpack_unpacked msg;
22286 msgpack_unpack_return msgpack_ret;
22287
22288 assert (pnote->type == NT_AMDGPU_METADATA);
22289
22290 msgpack_unpacked_init (&msg);
22291 msgpack_ret = msgpack_unpack_next (&msg, pnote->descdata, pnote->descsz,
22292 NULL);
22293
22294 switch (msgpack_ret)
22295 {
22296 case MSGPACK_UNPACK_SUCCESS:
22297 dump_msgpack (&msg);
22298 ret = true;
22299 break;
22300
22301 default:
22302 error (_("failed to unpack msgpack contents in NT_AMDGPU_METADATA note"));
22303 ret = false;
22304 break;
22305 }
22306
22307 msgpack_unpacked_destroy (&msg);
22308 return ret;
22309 #else
22310 /* msgpack is not available, dump contents as hex. */
22311 print_note_contents_hex (pnote);
22312 return true;
22313 #endif
22314 }
22315
22316 static bool
22317 print_qnx_note (Elf_Internal_Note *pnote)
22318 {
22319 switch (pnote->type)
22320 {
22321 case QNT_STACK:
22322 if (pnote->descsz != 12)
22323 goto desc_size_fail;
22324
22325 printf (_(" Stack Size: 0x%" PRIx32 "\n"),
22326 (unsigned) byte_get ((unsigned char *) pnote->descdata, 4));
22327 printf (_(" Stack allocated: %" PRIx32 "\n"),
22328 (unsigned) byte_get ((unsigned char *) pnote->descdata + 4, 4));
22329 printf (_(" Executable: %s\n"),
22330 ((unsigned) byte_get ((unsigned char *) pnote->descdata + 8, 1)) ? "no": "yes");
22331 break;
22332
22333 default:
22334 print_note_contents_hex(pnote);
22335 }
22336 return true;
22337
22338 desc_size_fail:
22339 printf (_(" <corrupt - data size is too small>\n"));
22340 error (_("corrupt QNX note: data size is too small\n"));
22341 return false;
22342 }
22343
22344
22345 /* Note that by the ELF standard, the name field is already null byte
22346 terminated, and namesz includes the terminating null byte.
22347 I.E. the value of namesz for the name "FSF" is 4.
22348
22349 If the value of namesz is zero, there is no name present. */
22350
22351 static bool
22352 process_note (Elf_Internal_Note * pnote,
22353 Filedata * filedata)
22354 {
22355 const char * name = pnote->namesz ? pnote->namedata : "(NONE)";
22356 const char * nt;
22357
22358 if (pnote->namesz == 0)
22359 /* If there is no note name, then use the default set of
22360 note type strings. */
22361 nt = get_note_type (filedata, pnote->type);
22362
22363 else if (startswith (pnote->namedata, "GNU"))
22364 /* GNU-specific object file notes. */
22365 nt = get_gnu_elf_note_type (pnote->type);
22366
22367 else if (startswith (pnote->namedata, "AMDGPU"))
22368 /* AMDGPU-specific object file notes. */
22369 nt = get_amdgpu_elf_note_type (pnote->type);
22370
22371 else if (startswith (pnote->namedata, "FreeBSD"))
22372 /* FreeBSD-specific core file notes. */
22373 nt = get_freebsd_elfcore_note_type (filedata, pnote->type);
22374
22375 else if (startswith (pnote->namedata, "NetBSD-CORE"))
22376 /* NetBSD-specific core file notes. */
22377 nt = get_netbsd_elfcore_note_type (filedata, pnote->type);
22378
22379 else if (startswith (pnote->namedata, "NetBSD"))
22380 /* NetBSD-specific core file notes. */
22381 return process_netbsd_elf_note (pnote);
22382
22383 else if (startswith (pnote->namedata, "PaX"))
22384 /* NetBSD-specific core file notes. */
22385 return process_netbsd_elf_note (pnote);
22386
22387 else if (startswith (pnote->namedata, "OpenBSD"))
22388 /* OpenBSD-specific core file notes. */
22389 nt = get_openbsd_elfcore_note_type (filedata, pnote->type);
22390
22391 else if (startswith (pnote->namedata, "QNX"))
22392 /* QNX-specific core file notes. */
22393 nt = get_qnx_elfcore_note_type (filedata, pnote->type);
22394
22395 else if (startswith (pnote->namedata, "SPU/"))
22396 {
22397 /* SPU-specific core file notes. */
22398 nt = pnote->namedata + 4;
22399 name = "SPU";
22400 }
22401
22402 else if (startswith (pnote->namedata, "IPF/VMS"))
22403 /* VMS/ia64-specific file notes. */
22404 nt = get_ia64_vms_note_type (pnote->type);
22405
22406 else if (startswith (pnote->namedata, "stapsdt"))
22407 nt = get_stapsdt_note_type (pnote->type);
22408
22409 else
22410 /* Don't recognize this note name; just use the default set of
22411 note type strings. */
22412 nt = get_note_type (filedata, pnote->type);
22413
22414 printf (" ");
22415
22416 if (((startswith (pnote->namedata, "GA")
22417 && strchr ("*$!+", pnote->namedata[2]) != NULL)
22418 || strchr ("*$!+", pnote->namedata[0]) != NULL)
22419 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
22420 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
22421 print_gnu_build_attribute_name (pnote);
22422 else
22423 print_symbol_name (-20, name);
22424
22425 if (do_wide)
22426 printf (" 0x%08lx\t%s\t", pnote->descsz, nt);
22427 else
22428 printf (" 0x%08lx\t%s\n", pnote->descsz, nt);
22429
22430 if (startswith (pnote->namedata, "IPF/VMS"))
22431 return print_ia64_vms_note (pnote);
22432 else if (startswith (pnote->namedata, "GNU"))
22433 return print_gnu_note (filedata, pnote);
22434 else if (startswith (pnote->namedata, "stapsdt"))
22435 return print_stapsdt_note (pnote);
22436 else if (startswith (pnote->namedata, "CORE"))
22437 return print_core_note (pnote);
22438 else if (startswith (pnote->namedata, "FDO"))
22439 return print_fdo_note (pnote);
22440 else if (((startswith (pnote->namedata, "GA")
22441 && strchr ("*$!+", pnote->namedata[2]) != NULL)
22442 || strchr ("*$!+", pnote->namedata[0]) != NULL)
22443 && (pnote->type == NT_GNU_BUILD_ATTRIBUTE_OPEN
22444 || pnote->type == NT_GNU_BUILD_ATTRIBUTE_FUNC))
22445 return print_gnu_build_attribute_description (pnote, filedata);
22446 else if (startswith (pnote->namedata, "AMDGPU")
22447 && pnote->type == NT_AMDGPU_METADATA)
22448 return print_amdgpu_note (pnote);
22449 else if (startswith (pnote->namedata, "QNX"))
22450 return print_qnx_note (pnote);
22451
22452 print_note_contents_hex (pnote);
22453 return true;
22454 }
22455
22456 static bool
22457 process_notes_at (Filedata * filedata,
22458 Elf_Internal_Shdr * section,
22459 uint64_t offset,
22460 uint64_t length,
22461 uint64_t align)
22462 {
22463 Elf_External_Note *pnotes;
22464 Elf_External_Note *external;
22465 char *end;
22466 bool res = true;
22467
22468 if (length <= 0)
22469 return false;
22470
22471 if (section)
22472 {
22473 pnotes = (Elf_External_Note *) get_section_contents (section, filedata);
22474 if (pnotes)
22475 {
22476 if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
22477 {
22478 free (pnotes);
22479 return false;
22480 }
22481 }
22482 }
22483 else
22484 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
22485 _("notes"));
22486
22487 if (pnotes == NULL)
22488 return false;
22489
22490 external = pnotes;
22491
22492 if (filedata->is_separate)
22493 printf (_("In linked file '%s': "), filedata->file_name);
22494 else
22495 printf ("\n");
22496 if (section)
22497 printf (_("Displaying notes found in: %s\n"), printable_section_name (filedata, section));
22498 else
22499 printf (_("Displaying notes found at file offset 0x%08" PRIx64
22500 " with length 0x%08" PRIx64 ":\n"),
22501 offset, length);
22502
22503 /* NB: Some note sections may have alignment value of 0 or 1. gABI
22504 specifies that notes should be aligned to 4 bytes in 32-bit
22505 objects and to 8 bytes in 64-bit objects. As a Linux extension,
22506 we also support 4 byte alignment in 64-bit objects. If section
22507 alignment is less than 4, we treate alignment as 4 bytes. */
22508 if (align < 4)
22509 align = 4;
22510 else if (align != 4 && align != 8)
22511 {
22512 warn (_("Corrupt note: alignment %" PRId64 ", expecting 4 or 8\n"),
22513 align);
22514 free (pnotes);
22515 return false;
22516 }
22517
22518 printf (_(" %-20s %-10s\tDescription\n"), _("Owner"), _("Data size"));
22519
22520 end = (char *) pnotes + length;
22521 while ((char *) external < end)
22522 {
22523 Elf_Internal_Note inote;
22524 size_t min_notesz;
22525 char * next;
22526 char * temp = NULL;
22527 size_t data_remaining = end - (char *) external;
22528
22529 if (!is_ia64_vms (filedata))
22530 {
22531 /* PR binutils/15191
22532 Make sure that there is enough data to read. */
22533 min_notesz = offsetof (Elf_External_Note, name);
22534 if (data_remaining < min_notesz)
22535 {
22536 warn (ngettext ("Corrupt note: only %zd byte remains, "
22537 "not enough for a full note\n",
22538 "Corrupt note: only %zd bytes remain, "
22539 "not enough for a full note\n",
22540 data_remaining),
22541 data_remaining);
22542 break;
22543 }
22544 data_remaining -= min_notesz;
22545
22546 inote.type = BYTE_GET (external->type);
22547 inote.namesz = BYTE_GET (external->namesz);
22548 inote.namedata = external->name;
22549 inote.descsz = BYTE_GET (external->descsz);
22550 inote.descdata = ((char *) external
22551 + ELF_NOTE_DESC_OFFSET (inote.namesz, align));
22552 inote.descpos = offset + (inote.descdata - (char *) pnotes);
22553 next = ((char *) external
22554 + ELF_NOTE_NEXT_OFFSET (inote.namesz, inote.descsz, align));
22555 }
22556 else
22557 {
22558 Elf64_External_VMS_Note *vms_external;
22559
22560 /* PR binutils/15191
22561 Make sure that there is enough data to read. */
22562 min_notesz = offsetof (Elf64_External_VMS_Note, name);
22563 if (data_remaining < min_notesz)
22564 {
22565 warn (ngettext ("Corrupt note: only %zd byte remains, "
22566 "not enough for a full note\n",
22567 "Corrupt note: only %zd bytes remain, "
22568 "not enough for a full note\n",
22569 data_remaining),
22570 data_remaining);
22571 break;
22572 }
22573 data_remaining -= min_notesz;
22574
22575 vms_external = (Elf64_External_VMS_Note *) external;
22576 inote.type = BYTE_GET (vms_external->type);
22577 inote.namesz = BYTE_GET (vms_external->namesz);
22578 inote.namedata = vms_external->name;
22579 inote.descsz = BYTE_GET (vms_external->descsz);
22580 inote.descdata = inote.namedata + align_power (inote.namesz, 3);
22581 inote.descpos = offset + (inote.descdata - (char *) pnotes);
22582 next = inote.descdata + align_power (inote.descsz, 3);
22583 }
22584
22585 /* PR 17531: file: 3443835e. */
22586 /* PR 17531: file: id:000000,sig:11,src:006986,op:havoc,rep:4. */
22587 if ((size_t) (inote.descdata - inote.namedata) < inote.namesz
22588 || (size_t) (inote.descdata - inote.namedata) > data_remaining
22589 || (size_t) (next - inote.descdata) < inote.descsz
22590 || ((size_t) (next - inote.descdata)
22591 > data_remaining - (size_t) (inote.descdata - inote.namedata)))
22592 {
22593 warn (_("note with invalid namesz and/or descsz found at offset %#tx\n"),
22594 (char *) external - (char *) pnotes);
22595 warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx, alignment: %u\n"),
22596 inote.type, inote.namesz, inote.descsz, (int) align);
22597 break;
22598 }
22599
22600 external = (Elf_External_Note *) next;
22601
22602 /* Verify that name is null terminated. It appears that at least
22603 one version of Linux (RedHat 6.0) generates corefiles that don't
22604 comply with the ELF spec by failing to include the null byte in
22605 namesz. */
22606 if (inote.namesz > 0 && inote.namedata[inote.namesz - 1] != '\0')
22607 {
22608 if ((size_t) (inote.descdata - inote.namedata) == inote.namesz)
22609 {
22610 temp = (char *) malloc (inote.namesz + 1);
22611 if (temp == NULL)
22612 {
22613 error (_("Out of memory allocating space for inote name\n"));
22614 res = false;
22615 break;
22616 }
22617
22618 memcpy (temp, inote.namedata, inote.namesz);
22619 inote.namedata = temp;
22620 }
22621 inote.namedata[inote.namesz] = 0;
22622 }
22623
22624 if (! process_note (& inote, filedata))
22625 res = false;
22626
22627 free (temp);
22628 temp = NULL;
22629 }
22630
22631 free (pnotes);
22632
22633 return res;
22634 }
22635
22636 static bool
22637 process_corefile_note_segments (Filedata * filedata)
22638 {
22639 Elf_Internal_Phdr *segment;
22640 unsigned int i;
22641 bool res = true;
22642
22643 if (! get_program_headers (filedata))
22644 return true;
22645
22646 for (i = 0, segment = filedata->program_headers;
22647 i < filedata->file_header.e_phnum;
22648 i++, segment++)
22649 {
22650 if (segment->p_type == PT_NOTE)
22651 if (! process_notes_at (filedata, NULL, segment->p_offset,
22652 segment->p_filesz, segment->p_align))
22653 res = false;
22654 }
22655
22656 return res;
22657 }
22658
22659 static bool
22660 process_v850_notes (Filedata * filedata, uint64_t offset, uint64_t length)
22661 {
22662 Elf_External_Note * pnotes;
22663 Elf_External_Note * external;
22664 char * end;
22665 bool res = true;
22666
22667 if (length <= 0)
22668 return false;
22669
22670 pnotes = (Elf_External_Note *) get_data (NULL, filedata, offset, 1, length,
22671 _("v850 notes"));
22672 if (pnotes == NULL)
22673 return false;
22674
22675 external = pnotes;
22676 end = (char*) pnotes + length;
22677
22678 printf (_("\nDisplaying contents of Renesas V850 notes section at offset"
22679 " %#" PRIx64 " with length %#" PRIx64 ":\n"),
22680 offset, length);
22681
22682 while ((char *) external + sizeof (Elf_External_Note) < end)
22683 {
22684 Elf_External_Note * next;
22685 Elf_Internal_Note inote;
22686
22687 inote.type = BYTE_GET (external->type);
22688 inote.namesz = BYTE_GET (external->namesz);
22689 inote.namedata = external->name;
22690 inote.descsz = BYTE_GET (external->descsz);
22691 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
22692 inote.descpos = offset + (inote.descdata - (char *) pnotes);
22693
22694 if (inote.descdata < (char *) pnotes || inote.descdata >= end)
22695 {
22696 warn (_("Corrupt note: name size is too big: %lx\n"), inote.namesz);
22697 inote.descdata = inote.namedata;
22698 inote.namesz = 0;
22699 }
22700
22701 next = (Elf_External_Note *) (inote.descdata + align_power (inote.descsz, 2));
22702
22703 if ( ((char *) next > end)
22704 || ((char *) next < (char *) pnotes))
22705 {
22706 warn (_("corrupt descsz found in note at offset %#tx\n"),
22707 (char *) external - (char *) pnotes);
22708 warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx\n"),
22709 inote.type, inote.namesz, inote.descsz);
22710 break;
22711 }
22712
22713 external = next;
22714
22715 /* Prevent out-of-bounds indexing. */
22716 if ( inote.namedata + inote.namesz > end
22717 || inote.namedata + inote.namesz < inote.namedata)
22718 {
22719 warn (_("corrupt namesz found in note at offset %#zx\n"),
22720 (char *) external - (char *) pnotes);
22721 warn (_(" type: %#lx, namesize: %#lx, descsize: %#lx\n"),
22722 inote.type, inote.namesz, inote.descsz);
22723 break;
22724 }
22725
22726 printf (" %s: ", get_v850_elf_note_type (inote.type));
22727
22728 if (! print_v850_note (& inote))
22729 {
22730 res = false;
22731 printf ("<corrupt sizes: namesz: %#lx, descsz: %#lx>\n",
22732 inote.namesz, inote.descsz);
22733 }
22734 }
22735
22736 free (pnotes);
22737
22738 return res;
22739 }
22740
22741 static bool
22742 process_note_sections (Filedata * filedata)
22743 {
22744 Elf_Internal_Shdr *section;
22745 size_t i;
22746 unsigned int n = 0;
22747 bool res = true;
22748
22749 for (i = 0, section = filedata->section_headers;
22750 i < filedata->file_header.e_shnum && section != NULL;
22751 i++, section++)
22752 {
22753 if (section->sh_type == SHT_NOTE)
22754 {
22755 if (! process_notes_at (filedata, section, section->sh_offset,
22756 section->sh_size, section->sh_addralign))
22757 res = false;
22758 n++;
22759 }
22760
22761 if (( filedata->file_header.e_machine == EM_V800
22762 || filedata->file_header.e_machine == EM_V850
22763 || filedata->file_header.e_machine == EM_CYGNUS_V850)
22764 && section->sh_type == SHT_RENESAS_INFO)
22765 {
22766 if (! process_v850_notes (filedata, section->sh_offset,
22767 section->sh_size))
22768 res = false;
22769 n++;
22770 }
22771 }
22772
22773 if (n == 0)
22774 /* Try processing NOTE segments instead. */
22775 return process_corefile_note_segments (filedata);
22776
22777 return res;
22778 }
22779
22780 static bool
22781 process_notes (Filedata * filedata)
22782 {
22783 /* If we have not been asked to display the notes then do nothing. */
22784 if (! do_notes)
22785 return true;
22786
22787 if (filedata->file_header.e_type != ET_CORE)
22788 return process_note_sections (filedata);
22789
22790 /* No program headers means no NOTE segment. */
22791 if (filedata->file_header.e_phnum > 0)
22792 return process_corefile_note_segments (filedata);
22793
22794 if (filedata->is_separate)
22795 printf (_("No notes found in linked file '%s'.\n"),
22796 filedata->file_name);
22797 else
22798 printf (_("No notes found file.\n"));
22799
22800 return true;
22801 }
22802
22803 static unsigned char *
22804 display_public_gnu_attributes (unsigned char * start,
22805 const unsigned char * const end)
22806 {
22807 printf (_(" Unknown GNU attribute: %s\n"), start);
22808
22809 start += strnlen ((char *) start, end - start);
22810 display_raw_attribute (start, end);
22811
22812 return (unsigned char *) end;
22813 }
22814
22815 static unsigned char *
22816 display_generic_attribute (unsigned char * start,
22817 unsigned int tag,
22818 const unsigned char * const end)
22819 {
22820 if (tag == 0)
22821 return (unsigned char *) end;
22822
22823 return display_tag_value (tag, start, end);
22824 }
22825
22826 static bool
22827 process_arch_specific (Filedata * filedata)
22828 {
22829 if (! do_arch)
22830 return true;
22831
22832 switch (filedata->file_header.e_machine)
22833 {
22834 case EM_ARC:
22835 case EM_ARC_COMPACT:
22836 case EM_ARC_COMPACT2:
22837 case EM_ARC_COMPACT3:
22838 case EM_ARC_COMPACT3_64:
22839 return process_attributes (filedata, "ARC", SHT_ARC_ATTRIBUTES,
22840 display_arc_attribute,
22841 display_generic_attribute);
22842 case EM_ARM:
22843 return process_attributes (filedata, "aeabi", SHT_ARM_ATTRIBUTES,
22844 display_arm_attribute,
22845 display_generic_attribute);
22846
22847 case EM_MIPS:
22848 case EM_MIPS_RS3_LE:
22849 return process_mips_specific (filedata);
22850
22851 case EM_MSP430:
22852 return process_attributes (filedata, "mspabi", SHT_MSP430_ATTRIBUTES,
22853 display_msp430_attribute,
22854 display_msp430_gnu_attribute);
22855
22856 case EM_RISCV:
22857 return process_attributes (filedata, "riscv", SHT_RISCV_ATTRIBUTES,
22858 display_riscv_attribute,
22859 display_generic_attribute);
22860
22861 case EM_NDS32:
22862 return process_nds32_specific (filedata);
22863
22864 case EM_68K:
22865 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22866 display_m68k_gnu_attribute);
22867
22868 case EM_PPC:
22869 case EM_PPC64:
22870 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22871 display_power_gnu_attribute);
22872
22873 case EM_S390:
22874 case EM_S390_OLD:
22875 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22876 display_s390_gnu_attribute);
22877
22878 case EM_SPARC:
22879 case EM_SPARC32PLUS:
22880 case EM_SPARCV9:
22881 return process_attributes (filedata, NULL, SHT_GNU_ATTRIBUTES, NULL,
22882 display_sparc_gnu_attribute);
22883
22884 case EM_TI_C6000:
22885 return process_attributes (filedata, "c6xabi", SHT_C6000_ATTRIBUTES,
22886 display_tic6x_attribute,
22887 display_generic_attribute);
22888
22889 case EM_CSKY:
22890 return process_attributes (filedata, "csky", SHT_CSKY_ATTRIBUTES,
22891 display_csky_attribute, NULL);
22892
22893 default:
22894 return process_attributes (filedata, "gnu", SHT_GNU_ATTRIBUTES,
22895 display_public_gnu_attributes,
22896 display_generic_attribute);
22897 }
22898 }
22899
22900 static bool
22901 get_file_header (Filedata * filedata)
22902 {
22903 /* Read in the identity array. */
22904 if (fread (filedata->file_header.e_ident, EI_NIDENT, 1, filedata->handle) != 1)
22905 return false;
22906
22907 /* Determine how to read the rest of the header. */
22908 switch (filedata->file_header.e_ident[EI_DATA])
22909 {
22910 default:
22911 case ELFDATANONE:
22912 case ELFDATA2LSB:
22913 byte_get = byte_get_little_endian;
22914 byte_put = byte_put_little_endian;
22915 break;
22916 case ELFDATA2MSB:
22917 byte_get = byte_get_big_endian;
22918 byte_put = byte_put_big_endian;
22919 break;
22920 }
22921
22922 /* For now we only support 32 bit and 64 bit ELF files. */
22923 is_32bit_elf = (filedata->file_header.e_ident[EI_CLASS] != ELFCLASS64);
22924
22925 /* Read in the rest of the header. */
22926 if (is_32bit_elf)
22927 {
22928 Elf32_External_Ehdr ehdr32;
22929
22930 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, filedata->handle) != 1)
22931 return false;
22932
22933 filedata->file_header.e_type = BYTE_GET (ehdr32.e_type);
22934 filedata->file_header.e_machine = BYTE_GET (ehdr32.e_machine);
22935 filedata->file_header.e_version = BYTE_GET (ehdr32.e_version);
22936 filedata->file_header.e_entry = BYTE_GET (ehdr32.e_entry);
22937 filedata->file_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
22938 filedata->file_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
22939 filedata->file_header.e_flags = BYTE_GET (ehdr32.e_flags);
22940 filedata->file_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
22941 filedata->file_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
22942 filedata->file_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
22943 filedata->file_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
22944 filedata->file_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
22945 filedata->file_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
22946 }
22947 else
22948 {
22949 Elf64_External_Ehdr ehdr64;
22950
22951 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, filedata->handle) != 1)
22952 return false;
22953
22954 filedata->file_header.e_type = BYTE_GET (ehdr64.e_type);
22955 filedata->file_header.e_machine = BYTE_GET (ehdr64.e_machine);
22956 filedata->file_header.e_version = BYTE_GET (ehdr64.e_version);
22957 filedata->file_header.e_entry = BYTE_GET (ehdr64.e_entry);
22958 filedata->file_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
22959 filedata->file_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
22960 filedata->file_header.e_flags = BYTE_GET (ehdr64.e_flags);
22961 filedata->file_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
22962 filedata->file_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
22963 filedata->file_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
22964 filedata->file_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
22965 filedata->file_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
22966 filedata->file_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
22967 }
22968
22969 return true;
22970 }
22971
22972 static void
22973 free_filedata (Filedata *filedata)
22974 {
22975 free (filedata->program_interpreter);
22976 free (filedata->program_headers);
22977 free (filedata->section_headers);
22978 free (filedata->string_table);
22979 free (filedata->dump.dump_sects);
22980 free (filedata->dynamic_strings);
22981 free (filedata->dynamic_symbols);
22982 free (filedata->dynamic_syminfo);
22983 free (filedata->dynamic_section);
22984
22985 while (filedata->symtab_shndx_list != NULL)
22986 {
22987 elf_section_list *next = filedata->symtab_shndx_list->next;
22988 free (filedata->symtab_shndx_list);
22989 filedata->symtab_shndx_list = next;
22990 }
22991
22992 free (filedata->section_headers_groups);
22993
22994 if (filedata->section_groups)
22995 {
22996 size_t i;
22997 struct group_list * g;
22998 struct group_list * next;
22999
23000 for (i = 0; i < filedata->group_count; i++)
23001 {
23002 for (g = filedata->section_groups [i].root; g != NULL; g = next)
23003 {
23004 next = g->next;
23005 free (g);
23006 }
23007 }
23008
23009 free (filedata->section_groups);
23010 }
23011 memset (&filedata->section_headers, 0,
23012 sizeof (Filedata) - offsetof (Filedata, section_headers));
23013 }
23014
23015 static void
23016 close_file (Filedata * filedata)
23017 {
23018 if (filedata)
23019 {
23020 if (filedata->handle)
23021 fclose (filedata->handle);
23022 free (filedata);
23023 }
23024 }
23025
23026 void
23027 close_debug_file (void * data)
23028 {
23029 free_filedata ((Filedata *) data);
23030 close_file ((Filedata *) data);
23031 }
23032
23033 static Filedata *
23034 open_file (const char * pathname, bool is_separate)
23035 {
23036 struct stat statbuf;
23037 Filedata * filedata = NULL;
23038
23039 if (stat (pathname, & statbuf) < 0
23040 || ! S_ISREG (statbuf.st_mode))
23041 goto fail;
23042
23043 filedata = calloc (1, sizeof * filedata);
23044 if (filedata == NULL)
23045 goto fail;
23046
23047 filedata->handle = fopen (pathname, "rb");
23048 if (filedata->handle == NULL)
23049 goto fail;
23050
23051 filedata->file_size = statbuf.st_size;
23052 filedata->file_name = pathname;
23053 filedata->is_separate = is_separate;
23054
23055 if (! get_file_header (filedata))
23056 goto fail;
23057
23058 if (!get_section_headers (filedata, false))
23059 goto fail;
23060
23061 return filedata;
23062
23063 fail:
23064 if (filedata)
23065 {
23066 if (filedata->handle)
23067 fclose (filedata->handle);
23068 free (filedata);
23069 }
23070 return NULL;
23071 }
23072
23073 void *
23074 open_debug_file (const char * pathname)
23075 {
23076 return open_file (pathname, true);
23077 }
23078
23079 static void
23080 initialise_dump_sects (Filedata * filedata)
23081 {
23082 /* Initialise the dump_sects array from the cmdline_dump_sects array.
23083 Note we do this even if cmdline_dump_sects is empty because we
23084 must make sure that the dump_sets array is zeroed out before each
23085 object file is processed. */
23086 if (filedata->dump.num_dump_sects > cmdline.num_dump_sects)
23087 memset (filedata->dump.dump_sects, 0,
23088 filedata->dump.num_dump_sects * sizeof (*filedata->dump.dump_sects));
23089
23090 if (cmdline.num_dump_sects > 0)
23091 {
23092 if (filedata->dump.num_dump_sects == 0)
23093 /* A sneaky way of allocating the dump_sects array. */
23094 request_dump_bynumber (&filedata->dump, cmdline.num_dump_sects, 0);
23095
23096 assert (filedata->dump.num_dump_sects >= cmdline.num_dump_sects);
23097 memcpy (filedata->dump.dump_sects, cmdline.dump_sects,
23098 cmdline.num_dump_sects * sizeof (*filedata->dump.dump_sects));
23099 }
23100 }
23101
23102 static bool
23103 might_need_separate_debug_info (Filedata * filedata)
23104 {
23105 /* Debuginfo files do not need further separate file loading. */
23106 if (filedata->file_header.e_shstrndx == SHN_UNDEF)
23107 return false;
23108
23109 /* Since do_follow_links might be enabled by default, only treat it as an
23110 indication that separate files should be loaded if setting it was a
23111 deliberate user action. */
23112 if (DEFAULT_FOR_FOLLOW_LINKS == 0 && do_follow_links)
23113 return true;
23114
23115 if (process_links || do_syms || do_unwind
23116 || dump_any_debugging || do_dump || do_debugging)
23117 return true;
23118
23119 return false;
23120 }
23121
23122 /* Process one ELF object file according to the command line options.
23123 This file may actually be stored in an archive. The file is
23124 positioned at the start of the ELF object. Returns TRUE if no
23125 problems were encountered, FALSE otherwise. */
23126
23127 static bool
23128 process_object (Filedata * filedata)
23129 {
23130 bool have_separate_files;
23131 unsigned int i;
23132 bool res;
23133
23134 if (! get_file_header (filedata))
23135 {
23136 error (_("%s: Failed to read file header\n"), filedata->file_name);
23137 return false;
23138 }
23139
23140 /* Initialise per file variables. */
23141 for (i = ARRAY_SIZE (filedata->version_info); i--;)
23142 filedata->version_info[i] = 0;
23143
23144 for (i = ARRAY_SIZE (filedata->dynamic_info); i--;)
23145 filedata->dynamic_info[i] = 0;
23146 filedata->dynamic_info_DT_GNU_HASH = 0;
23147 filedata->dynamic_info_DT_MIPS_XHASH = 0;
23148
23149 /* Process the file. */
23150 if (show_name)
23151 printf (_("\nFile: %s\n"), filedata->file_name);
23152
23153 initialise_dump_sects (filedata);
23154
23155 /* There may be some extensions in the first section header. Don't
23156 bomb if we can't read it. */
23157 get_section_headers (filedata, true);
23158
23159 if (! process_file_header (filedata))
23160 {
23161 res = false;
23162 goto out;
23163 }
23164
23165 /* Throw away the single section header read above, so that we
23166 re-read the entire set. */
23167 free (filedata->section_headers);
23168 filedata->section_headers = NULL;
23169
23170 if (! process_section_headers (filedata))
23171 {
23172 /* Without loaded section headers we cannot process lots of things. */
23173 do_unwind = do_version = do_dump = do_arch = false;
23174
23175 if (! do_using_dynamic)
23176 do_syms = do_dyn_syms = do_reloc = false;
23177 }
23178
23179 if (! process_section_groups (filedata))
23180 /* Without loaded section groups we cannot process unwind. */
23181 do_unwind = false;
23182
23183 process_program_headers (filedata);
23184
23185 res = process_dynamic_section (filedata);
23186
23187 if (! process_relocs (filedata))
23188 res = false;
23189
23190 if (! process_unwind (filedata))
23191 res = false;
23192
23193 if (! process_symbol_table (filedata))
23194 res = false;
23195
23196 if (! process_lto_symbol_tables (filedata))
23197 res = false;
23198
23199 if (! process_syminfo (filedata))
23200 res = false;
23201
23202 if (! process_version_sections (filedata))
23203 res = false;
23204
23205 if (might_need_separate_debug_info (filedata))
23206 have_separate_files = load_separate_debug_files (filedata, filedata->file_name);
23207 else
23208 have_separate_files = false;
23209
23210 if (! process_section_contents (filedata))
23211 res = false;
23212
23213 if (have_separate_files)
23214 {
23215 separate_info * d;
23216
23217 for (d = first_separate_info; d != NULL; d = d->next)
23218 {
23219 initialise_dump_sects (d->handle);
23220
23221 if (process_links && ! process_file_header (d->handle))
23222 res = false;
23223 else if (! process_section_headers (d->handle))
23224 res = false;
23225 else if (! process_section_contents (d->handle))
23226 res = false;
23227 else if (process_links)
23228 {
23229 if (! process_section_groups (d->handle))
23230 res = false;
23231 process_program_headers (d->handle);
23232 if (! process_dynamic_section (d->handle))
23233 res = false;
23234 if (! process_relocs (d->handle))
23235 res = false;
23236 if (! process_unwind (d->handle))
23237 res = false;
23238 if (! process_symbol_table (d->handle))
23239 res = false;
23240 if (! process_lto_symbol_tables (d->handle))
23241 res = false;
23242 if (! process_syminfo (d->handle))
23243 res = false;
23244 if (! process_version_sections (d->handle))
23245 res = false;
23246 if (! process_notes (d->handle))
23247 res = false;
23248 }
23249 }
23250
23251 /* The file handles are closed by the call to free_debug_memory() below. */
23252 }
23253
23254 if (! process_notes (filedata))
23255 res = false;
23256
23257 if (! process_gnu_liblist (filedata))
23258 res = false;
23259
23260 if (! process_arch_specific (filedata))
23261 res = false;
23262
23263 out:
23264 free_filedata (filedata);
23265
23266 free_debug_memory ();
23267
23268 return res;
23269 }
23270
23271 /* Process an ELF archive.
23272 On entry the file is positioned just after the ARMAG string.
23273 Returns TRUE upon success, FALSE otherwise. */
23274
23275 static bool
23276 process_archive (Filedata * filedata, bool is_thin_archive)
23277 {
23278 struct archive_info arch;
23279 struct archive_info nested_arch;
23280 size_t got;
23281 bool ret = true;
23282
23283 show_name = true;
23284
23285 /* The ARCH structure is used to hold information about this archive. */
23286 arch.file_name = NULL;
23287 arch.file = NULL;
23288 arch.index_array = NULL;
23289 arch.sym_table = NULL;
23290 arch.longnames = NULL;
23291
23292 /* The NESTED_ARCH structure is used as a single-item cache of information
23293 about a nested archive (when members of a thin archive reside within
23294 another regular archive file). */
23295 nested_arch.file_name = NULL;
23296 nested_arch.file = NULL;
23297 nested_arch.index_array = NULL;
23298 nested_arch.sym_table = NULL;
23299 nested_arch.longnames = NULL;
23300
23301 if (setup_archive (&arch, filedata->file_name, filedata->handle,
23302 filedata->file_size, is_thin_archive,
23303 do_archive_index) != 0)
23304 {
23305 ret = false;
23306 goto out;
23307 }
23308
23309 if (do_archive_index)
23310 {
23311 if (arch.sym_table == NULL)
23312 error (_("%s: unable to dump the index as none was found\n"),
23313 filedata->file_name);
23314 else
23315 {
23316 uint64_t i, l;
23317 uint64_t current_pos;
23318
23319 printf (_("Index of archive %s: (%" PRIu64 " entries,"
23320 " %#" PRIx64 " bytes in the symbol table)\n"),
23321 filedata->file_name, arch.index_num,
23322 arch.sym_size);
23323
23324 current_pos = ftell (filedata->handle);
23325
23326 for (i = l = 0; i < arch.index_num; i++)
23327 {
23328 if (i == 0
23329 || (i > 0 && arch.index_array[i] != arch.index_array[i - 1]))
23330 {
23331 char * member_name
23332 = get_archive_member_name_at (&arch, arch.index_array[i],
23333 &nested_arch);
23334
23335 if (member_name != NULL)
23336 {
23337 char * qualified_name
23338 = make_qualified_name (&arch, &nested_arch,
23339 member_name);
23340
23341 if (qualified_name != NULL)
23342 {
23343 printf (_("Contents of binary %s at offset "),
23344 qualified_name);
23345 (void) print_vma (arch.index_array[i], PREFIX_HEX);
23346 putchar ('\n');
23347 free (qualified_name);
23348 }
23349 free (member_name);
23350 }
23351 }
23352
23353 if (l >= arch.sym_size)
23354 {
23355 error (_("%s: end of the symbol table reached "
23356 "before the end of the index\n"),
23357 filedata->file_name);
23358 ret = false;
23359 break;
23360 }
23361 /* PR 17531: file: 0b6630b2. */
23362 printf ("\t%.*s\n",
23363 (int) (arch.sym_size - l), arch.sym_table + l);
23364 l += strnlen (arch.sym_table + l, arch.sym_size - l) + 1;
23365 }
23366
23367 if (arch.uses_64bit_indices)
23368 l = (l + 7) & ~ 7;
23369 else
23370 l += l & 1;
23371
23372 if (l < arch.sym_size)
23373 {
23374 error (ngettext ("%s: %" PRId64 " byte remains in the symbol table, "
23375 "but without corresponding entries in "
23376 "the index table\n",
23377 "%s: %" PRId64 " bytes remain in the symbol table, "
23378 "but without corresponding entries in "
23379 "the index table\n",
23380 arch.sym_size - l),
23381 filedata->file_name, arch.sym_size - l);
23382 ret = false;
23383 }
23384
23385 if (fseek64 (filedata->handle, current_pos, SEEK_SET) != 0)
23386 {
23387 error (_("%s: failed to seek back to start of object files "
23388 "in the archive\n"),
23389 filedata->file_name);
23390 ret = false;
23391 goto out;
23392 }
23393 }
23394
23395 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
23396 && !do_segments && !do_header && !do_dump && !do_version
23397 && !do_histogram && !do_debugging && !do_arch && !do_notes
23398 && !do_section_groups && !do_dyn_syms)
23399 {
23400 ret = true; /* Archive index only. */
23401 goto out;
23402 }
23403 }
23404
23405 while (1)
23406 {
23407 char * name;
23408 size_t namelen;
23409 char * qualified_name;
23410
23411 /* Read the next archive header. */
23412 if (fseek64 (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0)
23413 {
23414 error (_("%s: failed to seek to next archive header\n"),
23415 arch.file_name);
23416 ret = false;
23417 break;
23418 }
23419 got = fread (&arch.arhdr, 1, sizeof arch.arhdr, filedata->handle);
23420 if (got != sizeof arch.arhdr)
23421 {
23422 if (got == 0)
23423 break;
23424 /* PR 24049 - we cannot use filedata->file_name as this will
23425 have already been freed. */
23426 error (_("%s: failed to read archive header\n"), arch.file_name);
23427
23428 ret = false;
23429 break;
23430 }
23431 if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
23432 {
23433 error (_("%s: did not find a valid archive header\n"),
23434 arch.file_name);
23435 ret = false;
23436 break;
23437 }
23438
23439 arch.next_arhdr_offset += sizeof arch.arhdr;
23440
23441 filedata->archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
23442
23443 name = get_archive_member_name (&arch, &nested_arch);
23444 if (name == NULL)
23445 {
23446 error (_("%s: bad archive file name\n"), arch.file_name);
23447 ret = false;
23448 break;
23449 }
23450 namelen = strlen (name);
23451
23452 qualified_name = make_qualified_name (&arch, &nested_arch, name);
23453 if (qualified_name == NULL)
23454 {
23455 error (_("%s: bad archive file name\n"), arch.file_name);
23456 free (name);
23457 ret = false;
23458 break;
23459 }
23460
23461 if (is_thin_archive && arch.nested_member_origin == 0)
23462 {
23463 /* This is a proxy for an external member of a thin archive. */
23464 Filedata * member_filedata;
23465 char * member_file_name = adjust_relative_path
23466 (filedata->file_name, name, namelen);
23467
23468 free (name);
23469 if (member_file_name == NULL)
23470 {
23471 free (qualified_name);
23472 ret = false;
23473 break;
23474 }
23475
23476 member_filedata = open_file (member_file_name, false);
23477 if (member_filedata == NULL)
23478 {
23479 error (_("Input file '%s' is not readable.\n"), member_file_name);
23480 free (member_file_name);
23481 free (qualified_name);
23482 ret = false;
23483 break;
23484 }
23485
23486 filedata->archive_file_offset = arch.nested_member_origin;
23487 member_filedata->file_name = qualified_name;
23488
23489 /* The call to process_object() expects the file to be at the beginning. */
23490 rewind (member_filedata->handle);
23491
23492 if (! process_object (member_filedata))
23493 ret = false;
23494
23495 close_file (member_filedata);
23496 free (member_file_name);
23497 }
23498 else if (is_thin_archive)
23499 {
23500 Filedata thin_filedata;
23501
23502 memset (&thin_filedata, 0, sizeof (thin_filedata));
23503
23504 /* PR 15140: Allow for corrupt thin archives. */
23505 if (nested_arch.file == NULL)
23506 {
23507 error (_("%s: contains corrupt thin archive: %s\n"),
23508 qualified_name, name);
23509 free (qualified_name);
23510 free (name);
23511 ret = false;
23512 break;
23513 }
23514 free (name);
23515
23516 /* This is a proxy for a member of a nested archive. */
23517 filedata->archive_file_offset
23518 = arch.nested_member_origin + sizeof arch.arhdr;
23519
23520 /* The nested archive file will have been opened and setup by
23521 get_archive_member_name. */
23522 if (fseek64 (nested_arch.file, filedata->archive_file_offset,
23523 SEEK_SET) != 0)
23524 {
23525 error (_("%s: failed to seek to archive member.\n"),
23526 nested_arch.file_name);
23527 free (qualified_name);
23528 ret = false;
23529 break;
23530 }
23531
23532 thin_filedata.handle = nested_arch.file;
23533 thin_filedata.file_name = qualified_name;
23534
23535 if (! process_object (& thin_filedata))
23536 ret = false;
23537 }
23538 else
23539 {
23540 free (name);
23541 filedata->archive_file_offset = arch.next_arhdr_offset;
23542 filedata->file_name = qualified_name;
23543 if (! process_object (filedata))
23544 ret = false;
23545 arch.next_arhdr_offset += (filedata->archive_file_size + 1) & -2;
23546 /* Stop looping with "negative" archive_file_size. */
23547 if (arch.next_arhdr_offset < filedata->archive_file_size)
23548 arch.next_arhdr_offset = -1ul;
23549 }
23550
23551 free (qualified_name);
23552 }
23553
23554 out:
23555 if (nested_arch.file != NULL)
23556 fclose (nested_arch.file);
23557 release_archive (&nested_arch);
23558 release_archive (&arch);
23559
23560 return ret;
23561 }
23562
23563 static bool
23564 process_file (char * file_name)
23565 {
23566 Filedata * filedata = NULL;
23567 struct stat statbuf;
23568 char armag[SARMAG];
23569 bool ret = true;
23570
23571 if (stat (file_name, &statbuf) < 0)
23572 {
23573 if (errno == ENOENT)
23574 error (_("'%s': No such file\n"), file_name);
23575 else
23576 error (_("Could not locate '%s'. System error message: %s\n"),
23577 file_name, strerror (errno));
23578 return false;
23579 }
23580
23581 if (! S_ISREG (statbuf.st_mode))
23582 {
23583 error (_("'%s' is not an ordinary file\n"), file_name);
23584 return false;
23585 }
23586
23587 filedata = calloc (1, sizeof * filedata);
23588 if (filedata == NULL)
23589 {
23590 error (_("Out of memory allocating file data structure\n"));
23591 return false;
23592 }
23593
23594 filedata->file_name = file_name;
23595 filedata->handle = fopen (file_name, "rb");
23596 if (filedata->handle == NULL)
23597 {
23598 error (_("Input file '%s' is not readable.\n"), file_name);
23599 free (filedata);
23600 return false;
23601 }
23602
23603 if (fread (armag, SARMAG, 1, filedata->handle) != 1)
23604 {
23605 error (_("%s: Failed to read file's magic number\n"), file_name);
23606 fclose (filedata->handle);
23607 free (filedata);
23608 return false;
23609 }
23610
23611 filedata->file_size = statbuf.st_size;
23612 filedata->is_separate = false;
23613
23614 if (memcmp (armag, ARMAG, SARMAG) == 0)
23615 {
23616 if (! process_archive (filedata, false))
23617 ret = false;
23618 }
23619 else if (memcmp (armag, ARMAGT, SARMAG) == 0)
23620 {
23621 if ( ! process_archive (filedata, true))
23622 ret = false;
23623 }
23624 else
23625 {
23626 if (do_archive_index && !check_all)
23627 error (_("File %s is not an archive so its index cannot be displayed.\n"),
23628 file_name);
23629
23630 rewind (filedata->handle);
23631 filedata->archive_file_size = filedata->archive_file_offset = 0;
23632
23633 if (! process_object (filedata))
23634 ret = false;
23635 }
23636
23637 fclose (filedata->handle);
23638 free (filedata->section_headers);
23639 free (filedata->program_headers);
23640 free (filedata->string_table);
23641 free (filedata->dump.dump_sects);
23642 free (filedata);
23643
23644 free (ba_cache.strtab);
23645 ba_cache.strtab = NULL;
23646 free (ba_cache.symtab);
23647 ba_cache.symtab = NULL;
23648 ba_cache.filedata = NULL;
23649
23650 return ret;
23651 }
23652
23653 #ifdef SUPPORT_DISASSEMBLY
23654 /* Needed by the i386 disassembler. For extra credit, someone could
23655 fix this so that we insert symbolic addresses here, esp for GOT/PLT
23656 symbols. */
23657
23658 void
23659 print_address (unsigned int addr, FILE * outfile)
23660 {
23661 fprintf (outfile,"0x%8.8x", addr);
23662 }
23663
23664 /* Needed by the i386 disassembler. */
23665
23666 void
23667 db_task_printsym (unsigned int addr)
23668 {
23669 print_address (addr, stderr);
23670 }
23671 #endif
23672
23673 int
23674 main (int argc, char ** argv)
23675 {
23676 int err;
23677
23678 #ifdef HAVE_LC_MESSAGES
23679 setlocale (LC_MESSAGES, "");
23680 #endif
23681 setlocale (LC_CTYPE, "");
23682 bindtextdomain (PACKAGE, LOCALEDIR);
23683 textdomain (PACKAGE);
23684
23685 expandargv (&argc, &argv);
23686
23687 parse_args (& cmdline, argc, argv);
23688
23689 if (optind < (argc - 1))
23690 /* When displaying information for more than one file,
23691 prefix the information with the file name. */
23692 show_name = true;
23693 else if (optind >= argc)
23694 {
23695 /* Ensure that the warning is always displayed. */
23696 do_checks = true;
23697
23698 warn (_("Nothing to do.\n"));
23699 usage (stderr);
23700 }
23701
23702 err = false;
23703 while (optind < argc)
23704 if (! process_file (argv[optind++]))
23705 err = true;
23706
23707 free (cmdline.dump_sects);
23708
23709 free (dump_ctf_symtab_name);
23710 free (dump_ctf_strtab_name);
23711 free (dump_ctf_parent_name);
23712
23713 return err ? EXIT_FAILURE : EXIT_SUCCESS;
23714 }